16cbd5570SChris Mason /* 26cbd5570SChris Mason * Copyright (C) 2007 Oracle. All rights reserved. 36cbd5570SChris Mason * 46cbd5570SChris Mason * This program is free software; you can redistribute it and/or 56cbd5570SChris Mason * modify it under the terms of the GNU General Public 66cbd5570SChris Mason * License v2 as published by the Free Software Foundation. 76cbd5570SChris Mason * 86cbd5570SChris Mason * This program is distributed in the hope that it will be useful, 96cbd5570SChris Mason * but WITHOUT ANY WARRANTY; without even the implied warranty of 106cbd5570SChris Mason * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 116cbd5570SChris Mason * General Public License for more details. 126cbd5570SChris Mason * 136cbd5570SChris Mason * You should have received a copy of the GNU General Public 146cbd5570SChris Mason * License along with this program; if not, write to the 156cbd5570SChris Mason * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 166cbd5570SChris Mason * Boston, MA 021110-1307, USA. 176cbd5570SChris Mason */ 186cbd5570SChris Mason 194b82d6e4SYan #include <linux/blkdev.h> 202e635a27SChris Mason #include <linux/module.h> 21e20d96d6SChris Mason #include <linux/buffer_head.h> 222e635a27SChris Mason #include <linux/fs.h> 232e635a27SChris Mason #include <linux/pagemap.h> 242e635a27SChris Mason #include <linux/highmem.h> 252e635a27SChris Mason #include <linux/time.h> 262e635a27SChris Mason #include <linux/init.h> 27a9572a15SEric Paris #include <linux/seq_file.h> 282e635a27SChris Mason #include <linux/string.h> 292e635a27SChris Mason #include <linux/backing-dev.h> 304b82d6e4SYan #include <linux/mount.h> 31dee26a9fSChris Mason #include <linux/mpage.h> 3275dfe396SChris Mason #include <linux/swap.h> 3375dfe396SChris Mason #include <linux/writeback.h> 348fd17795SChris Mason #include <linux/statfs.h> 3508607c1bSChris Mason #include <linux/compat.h> 3695e05289SChris Mason #include <linux/parser.h> 37c59f8951SChris Mason #include <linux/ctype.h> 386da6abaeSChris Mason #include <linux/namei.h> 39a9218f6bSChris Mason #include <linux/miscdevice.h> 401bcbf313SQinghuang Feng #include <linux/magic.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 4290a887c9SDan Magenheimer #include <linux/cleancache.h> 4322c44fe6SJosef Bacik #include <linux/ratelimit.h> 4455e301fdSFilipe Brandenburger #include <linux/btrfs.h> 454b4e25f2SChris Mason #include "compat.h" 4616cdcec7SMiao Xie #include "delayed-inode.h" 472e635a27SChris Mason #include "ctree.h" 48e20d96d6SChris Mason #include "disk-io.h" 49d5719762SChris Mason #include "transaction.h" 502c90e5d6SChris Mason #include "btrfs_inode.h" 513a686375SChris Mason #include "print-tree.h" 525103e947SJosef Bacik #include "xattr.h" 538a4b83ccSChris Mason #include "volumes.h" 54be6e8dc0SBalaji Rao #include "export.h" 55c8b97818SChris Mason #include "compression.h" 569c5085c1SJosef Bacik #include "rcu-string.h" 578dabb742SStefan Behrens #include "dev-replace.h" 5874255aa0SJosef Bacik #include "free-space-cache.h" 59b9e9a6cbSWang Shilong #include "backref.h" 60dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 612e635a27SChris Mason 621abe9b8aSliubo #define CREATE_TRACE_POINTS 631abe9b8aSliubo #include <trace/events/btrfs.h> 641abe9b8aSliubo 65b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 66830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 67e20d96d6SChris Mason 6808748810SDavid Sterba static const char *btrfs_decode_error(int errno) 69acce952bSliubo { 7008748810SDavid Sterba char *errstr = "unknown"; 71acce952bSliubo 72acce952bSliubo switch (errno) { 73acce952bSliubo case -EIO: 74acce952bSliubo errstr = "IO failure"; 75acce952bSliubo break; 76acce952bSliubo case -ENOMEM: 77acce952bSliubo errstr = "Out of memory"; 78acce952bSliubo break; 79acce952bSliubo case -EROFS: 80acce952bSliubo errstr = "Readonly filesystem"; 81acce952bSliubo break; 828c342930SJeff Mahoney case -EEXIST: 838c342930SJeff Mahoney errstr = "Object already exists"; 848c342930SJeff Mahoney break; 8594ef7280SDavid Sterba case -ENOSPC: 8694ef7280SDavid Sterba errstr = "No space left"; 8794ef7280SDavid Sterba break; 8894ef7280SDavid Sterba case -ENOENT: 8994ef7280SDavid Sterba errstr = "No such entry"; 9094ef7280SDavid Sterba break; 91acce952bSliubo } 92acce952bSliubo 93acce952bSliubo return errstr; 94acce952bSliubo } 95acce952bSliubo 96bbece8a3SDavid Sterba static void save_error_info(struct btrfs_fs_info *fs_info) 97acce952bSliubo { 98acce952bSliubo /* 99acce952bSliubo * today we only save the error info into ram. Long term we'll 100acce952bSliubo * also send it down to the disk 101acce952bSliubo */ 10287533c47SMiao Xie set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 103acce952bSliubo } 104acce952bSliubo 105acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 106acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 107acce952bSliubo { 108acce952bSliubo struct super_block *sb = fs_info->sb; 109acce952bSliubo 110acce952bSliubo if (sb->s_flags & MS_RDONLY) 111acce952bSliubo return; 112acce952bSliubo 11387533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 114acce952bSliubo sb->s_flags |= MS_RDONLY; 115c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1161acd6831SStefan Behrens /* 1171acd6831SStefan Behrens * Note that a running device replace operation is not 1181acd6831SStefan Behrens * canceled here although there is no way to update 1191acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 1201acd6831SStefan Behrens * therefore the canceling is ommited. The only penalty 1211acd6831SStefan Behrens * is that some I/O remains active until the procedure 1221acd6831SStefan Behrens * completes. The next time when the filesystem is 1231acd6831SStefan Behrens * mounted writeable again, the device replace 1241acd6831SStefan Behrens * operation continues. 1251acd6831SStefan Behrens */ 126acce952bSliubo } 127acce952bSliubo } 128acce952bSliubo 129533574c6SJoe Perches #ifdef CONFIG_PRINTK 130acce952bSliubo /* 131acce952bSliubo * __btrfs_std_error decodes expected errors from the caller and 132acce952bSliubo * invokes the approciate error response. 133acce952bSliubo */ 134acce952bSliubo void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 1354da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 136acce952bSliubo { 137acce952bSliubo struct super_block *sb = fs_info->sb; 138acce952bSliubo const char *errstr; 139acce952bSliubo 140acce952bSliubo /* 141acce952bSliubo * Special case: if the error is EROFS, and we're already 142acce952bSliubo * under MS_RDONLY, then it is safe here. 143acce952bSliubo */ 144acce952bSliubo if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 145acce952bSliubo return; 146acce952bSliubo 14708748810SDavid Sterba errstr = btrfs_decode_error(errno); 1484da35113SJeff Mahoney if (fmt) { 14937252a66SEric Sandeen struct va_format vaf; 15037252a66SEric Sandeen va_list args; 15137252a66SEric Sandeen 15237252a66SEric Sandeen va_start(args, fmt); 15337252a66SEric Sandeen vaf.fmt = fmt; 15437252a66SEric Sandeen vaf.va = &args; 1554da35113SJeff Mahoney 15608748810SDavid Sterba printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n", 15708748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 15837252a66SEric Sandeen va_end(args); 1594da35113SJeff Mahoney } else { 16008748810SDavid Sterba printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n", 16108748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1624da35113SJeff Mahoney } 163acce952bSliubo 1644da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1654da35113SJeff Mahoney save_error_info(fs_info); 166cf79ffb5SJosef Bacik if (sb->s_flags & MS_BORN) 167acce952bSliubo btrfs_handle_error(fs_info); 168acce952bSliubo } 1694da35113SJeff Mahoney 170533574c6SJoe Perches static const char * const logtypes[] = { 1714da35113SJeff Mahoney "emergency", 1724da35113SJeff Mahoney "alert", 1734da35113SJeff Mahoney "critical", 1744da35113SJeff Mahoney "error", 1754da35113SJeff Mahoney "warning", 1764da35113SJeff Mahoney "notice", 1774da35113SJeff Mahoney "info", 1784da35113SJeff Mahoney "debug", 1794da35113SJeff Mahoney }; 1804da35113SJeff Mahoney 181c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 1824da35113SJeff Mahoney { 1834da35113SJeff Mahoney struct super_block *sb = fs_info->sb; 1844da35113SJeff Mahoney char lvl[4]; 1854da35113SJeff Mahoney struct va_format vaf; 1864da35113SJeff Mahoney va_list args; 1874da35113SJeff Mahoney const char *type = logtypes[4]; 188533574c6SJoe Perches int kern_level; 1894da35113SJeff Mahoney 1904da35113SJeff Mahoney va_start(args, fmt); 1914da35113SJeff Mahoney 192533574c6SJoe Perches kern_level = printk_get_level(fmt); 193533574c6SJoe Perches if (kern_level) { 194533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 195533574c6SJoe Perches memcpy(lvl, fmt, size); 196533574c6SJoe Perches lvl[size] = '\0'; 197533574c6SJoe Perches fmt += size; 198533574c6SJoe Perches type = logtypes[kern_level - '0']; 1994da35113SJeff Mahoney } else 2004da35113SJeff Mahoney *lvl = '\0'; 2014da35113SJeff Mahoney 2024da35113SJeff Mahoney vaf.fmt = fmt; 2034da35113SJeff Mahoney vaf.va = &args; 204533574c6SJoe Perches 205c2cf52ebSSimon Kirby printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf); 206533574c6SJoe Perches 207533574c6SJoe Perches va_end(args); 2084da35113SJeff Mahoney } 209acce952bSliubo 210533574c6SJoe Perches #else 211533574c6SJoe Perches 212533574c6SJoe Perches void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function, 213533574c6SJoe Perches unsigned int line, int errno, const char *fmt, ...) 214533574c6SJoe Perches { 215533574c6SJoe Perches struct super_block *sb = fs_info->sb; 216533574c6SJoe Perches 217533574c6SJoe Perches /* 218533574c6SJoe Perches * Special case: if the error is EROFS, and we're already 219533574c6SJoe Perches * under MS_RDONLY, then it is safe here. 220533574c6SJoe Perches */ 221533574c6SJoe Perches if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 222533574c6SJoe Perches return; 223533574c6SJoe Perches 224533574c6SJoe Perches /* Don't go through full error handling during mount */ 225533574c6SJoe Perches if (sb->s_flags & MS_BORN) { 226533574c6SJoe Perches save_error_info(fs_info); 227533574c6SJoe Perches btrfs_handle_error(fs_info); 228533574c6SJoe Perches } 229533574c6SJoe Perches } 230533574c6SJoe Perches #endif 231533574c6SJoe Perches 2328c342930SJeff Mahoney /* 23349b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23449b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 23549b25e05SJeff Mahoney * one. 23649b25e05SJeff Mahoney * 23749b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 23849b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 23949b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24049b25e05SJeff Mahoney * in the call path but will return -EIO. 24149b25e05SJeff Mahoney * 24249b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24349b25e05SJeff Mahoney * btrfs_commit_transaction. 24449b25e05SJeff Mahoney */ 24549b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 24649b25e05SJeff Mahoney struct btrfs_root *root, const char *function, 24749b25e05SJeff Mahoney unsigned int line, int errno) 24849b25e05SJeff Mahoney { 24908748810SDavid Sterba /* 25008748810SDavid Sterba * Report first abort since mount 25108748810SDavid Sterba */ 25208748810SDavid Sterba if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, 25308748810SDavid Sterba &root->fs_info->fs_state)) { 25408748810SDavid Sterba WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n", 25508748810SDavid Sterba errno); 25608748810SDavid Sterba } 25749b25e05SJeff Mahoney trans->aborted = errno; 25849b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25949b25e05SJeff Mahoney * transaction may be able to continue. */ 26049b25e05SJeff Mahoney if (!trans->blocks_used) { 26169ce977aSMiao Xie const char *errstr; 26269ce977aSMiao Xie 26308748810SDavid Sterba errstr = btrfs_decode_error(errno); 264c2cf52ebSSimon Kirby btrfs_warn(root->fs_info, 265c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26669ce977aSMiao Xie function, line, errstr); 26749b25e05SJeff Mahoney return; 26849b25e05SJeff Mahoney } 2698d25a086SMiao Xie ACCESS_ONCE(trans->transaction->aborted) = errno; 270501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 271501407aaSJosef Bacik wake_up(&root->fs_info->transaction_wait); 272501407aaSJosef Bacik wake_up(&root->fs_info->transaction_blocked_wait); 27349b25e05SJeff Mahoney __btrfs_std_error(root->fs_info, function, line, errno, NULL); 27449b25e05SJeff Mahoney } 27549b25e05SJeff Mahoney /* 2768c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2778c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2788c342930SJeff Mahoney */ 2798c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2808c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2818c342930SJeff Mahoney { 2828c342930SJeff Mahoney char *s_id = "<unknown>"; 2838c342930SJeff Mahoney const char *errstr; 2848c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2858c342930SJeff Mahoney va_list args; 2868c342930SJeff Mahoney 2878c342930SJeff Mahoney if (fs_info) 2888c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2898c342930SJeff Mahoney 2908c342930SJeff Mahoney va_start(args, fmt); 2918c342930SJeff Mahoney vaf.va = &args; 2928c342930SJeff Mahoney 29308748810SDavid Sterba errstr = btrfs_decode_error(errno); 294aa43a17cSEric Sandeen if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR)) 29508748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29608748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2978c342930SJeff Mahoney 29808748810SDavid Sterba printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29908748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 3008c342930SJeff Mahoney va_end(args); 3018c342930SJeff Mahoney /* Caller calls BUG() */ 3028c342930SJeff Mahoney } 303e20d96d6SChris Mason 304e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 305e20d96d6SChris Mason { 306815745cfSAl Viro (void)close_ctree(btrfs_sb(sb)->tree_root); 307aea52e19SAl Viro /* FIXME: need to fix VFS to return error? */ 308aea52e19SAl Viro /* AV: return it _where_? ->put_super() can be triggered by any number 309aea52e19SAl Viro * of async events, up to and including delivery of SIGKILL to the 310aea52e19SAl Viro * last process that kept it busy. Or segfault in the aforementioned 311aea52e19SAl Viro * process... Whom would you report that to? 312aea52e19SAl Viro */ 313e20d96d6SChris Mason } 3142e635a27SChris Mason 31595e05289SChris Mason enum { 31673f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 317287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 318287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 319261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 320261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 32191435650SChris Mason Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 3229555c6c1SIlya Dryomov Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache, 3239555c6c1SIlya Dryomov Opt_no_space_cache, Opt_recovery, Opt_skip_balance, 32421adbd5cSStefan Behrens Opt_check_integrity, Opt_check_integrity_including_extent_data, 325f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 3268b87dc17SDavid Sterba Opt_commit_interval, 3279555c6c1SIlya Dryomov Opt_err, 32895e05289SChris Mason }; 32995e05289SChris Mason 33095e05289SChris Mason static match_table_t tokens = { 331dfe25020SChris Mason {Opt_degraded, "degraded"}, 33295e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3331493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33443e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 335b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 336be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 33721ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 3386f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3398f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3404543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 341c8b97818SChris Mason {Opt_compress, "compress"}, 342261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 343a555f810SChris Mason {Opt_compress_force, "compress-force"}, 344261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 345e18e4809SChris Mason {Opt_ssd, "ssd"}, 346451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3473b30c22fSChris Mason {Opt_nossd, "nossd"}, 34833268eafSJosef Bacik {Opt_noacl, "noacl"}, 3493a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 350dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 35197e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 352e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 3530af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 35488c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3554260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 35691435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 357e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3584cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 3594b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3608965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 361af31f5e5SChris Mason {Opt_recovery, "recovery"}, 3629555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 36321adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 36421adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 36521adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 366f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3678c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3688b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 36933268eafSJosef Bacik {Opt_err, NULL}, 37095e05289SChris Mason }; 37195e05289SChris Mason 372edf24abeSChristoph Hellwig /* 373edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 374edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 37549b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 376edf24abeSChristoph Hellwig */ 377edf24abeSChristoph Hellwig int btrfs_parse_options(struct btrfs_root *root, char *options) 37895e05289SChris Mason { 379edf24abeSChristoph Hellwig struct btrfs_fs_info *info = root->fs_info; 38095e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 38173bc1876SJosef Bacik char *p, *num, *orig = NULL; 38273bc1876SJosef Bacik u64 cache_gen; 3834543df7eSChris Mason int intarg; 384a7a3f7caSSage Weil int ret = 0; 385261507a0SLi Zefan char *compress_type; 386261507a0SLi Zefan bool compress_force = false; 387b6cda9bcSChris Mason 3886c41761fSDavid Sterba cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 38973bc1876SJosef Bacik if (cache_gen) 39073bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 39173bc1876SJosef Bacik 39295e05289SChris Mason if (!options) 39373bc1876SJosef Bacik goto out; 39495e05289SChris Mason 395be20aa9dSChris Mason /* 396be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 397be20aa9dSChris Mason * gets called twice 398be20aa9dSChris Mason */ 399be20aa9dSChris Mason options = kstrdup(options, GFP_NOFS); 400be20aa9dSChris Mason if (!options) 401be20aa9dSChris Mason return -ENOMEM; 402be20aa9dSChris Mason 403da495eccSJosef Bacik orig = options; 404be20aa9dSChris Mason 40595e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 40695e05289SChris Mason int token; 40795e05289SChris Mason if (!*p) 40895e05289SChris Mason continue; 40995e05289SChris Mason 41095e05289SChris Mason token = match_token(p, tokens, args); 41195e05289SChris Mason switch (token) { 412dfe25020SChris Mason case Opt_degraded: 413edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: allowing degraded mounts\n"); 414dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 415dfe25020SChris Mason break; 41695e05289SChris Mason case Opt_subvol: 41773f73415SJosef Bacik case Opt_subvolid: 418e15d0542SXin Zhong case Opt_subvolrootid: 41943e570b0SChristoph Hellwig case Opt_device: 420edf24abeSChristoph Hellwig /* 42143e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 422edf24abeSChristoph Hellwig * and can be happily ignored here. 423edf24abeSChristoph Hellwig */ 42495e05289SChris Mason break; 425b6cda9bcSChris Mason case Opt_nodatasum: 426067c28adSChris Mason printk(KERN_INFO "btrfs: setting nodatasum\n"); 427b6cda9bcSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 428be20aa9dSChris Mason break; 429be20aa9dSChris Mason case Opt_nodatacow: 430bedb2ccaSAndrei Popa if (!btrfs_test_opt(root, COMPRESS) || 431bedb2ccaSAndrei Popa !btrfs_test_opt(root, FORCE_COMPRESS)) { 432bedb2ccaSAndrei Popa printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n"); 433bedb2ccaSAndrei Popa } else { 434edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: setting nodatacow\n"); 435bedb2ccaSAndrei Popa } 436bedb2ccaSAndrei Popa info->compress_type = BTRFS_COMPRESS_NONE; 437bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 438bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 439be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 440be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 441b6cda9bcSChris Mason break; 442a555f810SChris Mason case Opt_compress_force: 443261507a0SLi Zefan case Opt_compress_force_type: 444261507a0SLi Zefan compress_force = true; 4451c697d4aSEric Sandeen /* Fallthrough */ 446261507a0SLi Zefan case Opt_compress: 447261507a0SLi Zefan case Opt_compress_type: 448261507a0SLi Zefan if (token == Opt_compress || 449261507a0SLi Zefan token == Opt_compress_force || 450261507a0SLi Zefan strcmp(args[0].from, "zlib") == 0) { 451261507a0SLi Zefan compress_type = "zlib"; 452261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 453063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 454bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 455bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 456a6fa6faeSLi Zefan } else if (strcmp(args[0].from, "lzo") == 0) { 457a6fa6faeSLi Zefan compress_type = "lzo"; 458a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 459063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 460bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 461bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 4622b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 463063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 464063849eaSArnd Hannemann compress_type = "no"; 465063849eaSArnd Hannemann info->compress_type = BTRFS_COMPRESS_NONE; 466063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 467063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 468063849eaSArnd Hannemann compress_force = false; 469261507a0SLi Zefan } else { 470261507a0SLi Zefan ret = -EINVAL; 471261507a0SLi Zefan goto out; 472261507a0SLi Zefan } 473261507a0SLi Zefan 474261507a0SLi Zefan if (compress_force) { 475261507a0SLi Zefan btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 476261507a0SLi Zefan pr_info("btrfs: force %s compression\n", 477261507a0SLi Zefan compress_type); 478261507a0SLi Zefan } else 479261507a0SLi Zefan pr_info("btrfs: use %s compression\n", 480261507a0SLi Zefan compress_type); 481a555f810SChris Mason break; 482e18e4809SChris Mason case Opt_ssd: 483edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: use ssd allocation scheme\n"); 484e18e4809SChris Mason btrfs_set_opt(info->mount_opt, SSD); 485e18e4809SChris Mason break; 486451d7585SChris Mason case Opt_ssd_spread: 487451d7585SChris Mason printk(KERN_INFO "btrfs: use spread ssd " 488451d7585SChris Mason "allocation scheme\n"); 489451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD); 490451d7585SChris Mason btrfs_set_opt(info->mount_opt, SSD_SPREAD); 491451d7585SChris Mason break; 4923b30c22fSChris Mason case Opt_nossd: 493451d7585SChris Mason printk(KERN_INFO "btrfs: not using ssd allocation " 494451d7585SChris Mason "scheme\n"); 495c289811cSChris Mason btrfs_set_opt(info->mount_opt, NOSSD); 4963b30c22fSChris Mason btrfs_clear_opt(info->mount_opt, SSD); 497451d7585SChris Mason btrfs_clear_opt(info->mount_opt, SSD_SPREAD); 4983b30c22fSChris Mason break; 49921ad10cfSChris Mason case Opt_nobarrier: 500edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: turning off barriers\n"); 50121ad10cfSChris Mason btrfs_set_opt(info->mount_opt, NOBARRIER); 50221ad10cfSChris Mason break; 5034543df7eSChris Mason case Opt_thread_pool: 5042c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5052c334e87SWang Shilong if (ret) { 5062c334e87SWang Shilong goto out; 5072c334e87SWang Shilong } else if (intarg > 0) { 5084543df7eSChris Mason info->thread_pool_size = intarg; 5092c334e87SWang Shilong } else { 5102c334e87SWang Shilong ret = -EINVAL; 5112c334e87SWang Shilong goto out; 5122c334e87SWang Shilong } 5134543df7eSChris Mason break; 5146f568d35SChris Mason case Opt_max_inline: 515edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5166f568d35SChris Mason if (num) { 51791748467SAkinobu Mita info->max_inline = memparse(num, NULL); 5186f568d35SChris Mason kfree(num); 5196f568d35SChris Mason 52015ada040SChris Mason if (info->max_inline) { 5216f568d35SChris Mason info->max_inline = max_t(u64, 52215ada040SChris Mason info->max_inline, 52315ada040SChris Mason root->sectorsize); 52415ada040SChris Mason } 525edf24abeSChristoph Hellwig printk(KERN_INFO "btrfs: max_inline at %llu\n", 526c1c9ff7cSGeert Uytterhoeven info->max_inline); 5272c334e87SWang Shilong } else { 5282c334e87SWang Shilong ret = -ENOMEM; 5292c334e87SWang Shilong goto out; 5306f568d35SChris Mason } 5316f568d35SChris Mason break; 5328f662a76SChris Mason case Opt_alloc_start: 533edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 5348f662a76SChris Mason if (num) { 535c018daecSMiao Xie mutex_lock(&info->chunk_mutex); 53691748467SAkinobu Mita info->alloc_start = memparse(num, NULL); 537c018daecSMiao Xie mutex_unlock(&info->chunk_mutex); 5388f662a76SChris Mason kfree(num); 539edf24abeSChristoph Hellwig printk(KERN_INFO 540edf24abeSChristoph Hellwig "btrfs: allocations start at %llu\n", 541c1c9ff7cSGeert Uytterhoeven info->alloc_start); 5422c334e87SWang Shilong } else { 5432c334e87SWang Shilong ret = -ENOMEM; 5442c334e87SWang Shilong goto out; 5458f662a76SChris Mason } 5468f662a76SChris Mason break; 54733268eafSJosef Bacik case Opt_noacl: 54833268eafSJosef Bacik root->fs_info->sb->s_flags &= ~MS_POSIXACL; 54933268eafSJosef Bacik break; 5503a5e1404SSage Weil case Opt_notreelog: 5513a5e1404SSage Weil printk(KERN_INFO "btrfs: disabling tree log\n"); 5523a5e1404SSage Weil btrfs_set_opt(info->mount_opt, NOTREELOG); 5533a5e1404SSage Weil break; 554dccae999SSage Weil case Opt_flushoncommit: 555dccae999SSage Weil printk(KERN_INFO "btrfs: turning on flush-on-commit\n"); 556dccae999SSage Weil btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT); 557dccae999SSage Weil break; 55897e728d4SJosef Bacik case Opt_ratio: 5592c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5602c334e87SWang Shilong if (ret) { 5612c334e87SWang Shilong goto out; 5622c334e87SWang Shilong } else if (intarg >= 0) { 56397e728d4SJosef Bacik info->metadata_ratio = intarg; 56497e728d4SJosef Bacik printk(KERN_INFO "btrfs: metadata ratio %d\n", 56597e728d4SJosef Bacik info->metadata_ratio); 5662c334e87SWang Shilong } else { 5672c334e87SWang Shilong ret = -EINVAL; 5682c334e87SWang Shilong goto out; 56997e728d4SJosef Bacik } 57097e728d4SJosef Bacik break; 571e244a0aeSChristoph Hellwig case Opt_discard: 572e244a0aeSChristoph Hellwig btrfs_set_opt(info->mount_opt, DISCARD); 573e244a0aeSChristoph Hellwig break; 5740af3d00bSJosef Bacik case Opt_space_cache: 5750af3d00bSJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 5760de90876SJosef Bacik break; 577f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 578f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 579f420ee1eSStefan Behrens break; 58073bc1876SJosef Bacik case Opt_no_space_cache: 58173bc1876SJosef Bacik printk(KERN_INFO "btrfs: disabling disk space caching\n"); 58273bc1876SJosef Bacik btrfs_clear_opt(info->mount_opt, SPACE_CACHE); 58373bc1876SJosef Bacik break; 5844b9465cbSChris Mason case Opt_inode_cache: 5854b9465cbSChris Mason printk(KERN_INFO "btrfs: enabling inode map caching\n"); 5864b9465cbSChris Mason btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 5874b9465cbSChris Mason break; 58888c2ba3bSJosef Bacik case Opt_clear_cache: 58988c2ba3bSJosef Bacik printk(KERN_INFO "btrfs: force clearing of disk cache\n"); 59088c2ba3bSJosef Bacik btrfs_set_opt(info->mount_opt, CLEAR_CACHE); 5910af3d00bSJosef Bacik break; 5924260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 5934260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 5944260f7c7SSage Weil break; 59591435650SChris Mason case Opt_enospc_debug: 59691435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 59791435650SChris Mason break; 5984cb5300bSChris Mason case Opt_defrag: 59948940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto defrag\n"); 6004cb5300bSChris Mason btrfs_set_opt(info->mount_opt, AUTO_DEFRAG); 6014cb5300bSChris Mason break; 602af31f5e5SChris Mason case Opt_recovery: 60348940662SDaniel J Blueman printk(KERN_INFO "btrfs: enabling auto recovery\n"); 604af31f5e5SChris Mason btrfs_set_opt(info->mount_opt, RECOVERY); 605af31f5e5SChris Mason break; 6069555c6c1SIlya Dryomov case Opt_skip_balance: 6079555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 6089555c6c1SIlya Dryomov break; 60921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 61021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 61121adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity" 61221adbd5cSStefan Behrens " including extent data\n"); 61321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 61421adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 61521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 61621adbd5cSStefan Behrens break; 61721adbd5cSStefan Behrens case Opt_check_integrity: 61821adbd5cSStefan Behrens printk(KERN_INFO "btrfs: enabling check integrity\n"); 61921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 62021adbd5cSStefan Behrens break; 62121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 6222c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6232c334e87SWang Shilong if (ret) { 6242c334e87SWang Shilong goto out; 6252c334e87SWang Shilong } else if (intarg >= 0) { 62621adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 62721adbd5cSStefan Behrens printk(KERN_INFO "btrfs:" 62821adbd5cSStefan Behrens " check_integrity_print_mask 0x%x\n", 62921adbd5cSStefan Behrens info->check_integrity_print_mask); 6302c334e87SWang Shilong } else { 6312c334e87SWang Shilong ret = -EINVAL; 6322c334e87SWang Shilong goto out; 63321adbd5cSStefan Behrens } 63421adbd5cSStefan Behrens break; 63521adbd5cSStefan Behrens #else 63621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 63721adbd5cSStefan Behrens case Opt_check_integrity: 63821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 63921adbd5cSStefan Behrens printk(KERN_ERR "btrfs: support for check_integrity*" 64021adbd5cSStefan Behrens " not compiled in!\n"); 64121adbd5cSStefan Behrens ret = -EINVAL; 64221adbd5cSStefan Behrens goto out; 64321adbd5cSStefan Behrens #endif 6448c342930SJeff Mahoney case Opt_fatal_errors: 6458c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 6468c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 6478c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6488c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 6498c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 6508c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 6518c342930SJeff Mahoney else { 6528c342930SJeff Mahoney ret = -EINVAL; 6538c342930SJeff Mahoney goto out; 6548c342930SJeff Mahoney } 6558c342930SJeff Mahoney break; 6568b87dc17SDavid Sterba case Opt_commit_interval: 6578b87dc17SDavid Sterba intarg = 0; 6588b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 6598b87dc17SDavid Sterba if (ret < 0) { 6608b87dc17SDavid Sterba printk(KERN_ERR 6618b87dc17SDavid Sterba "btrfs: invalid commit interval\n"); 6628b87dc17SDavid Sterba ret = -EINVAL; 6638b87dc17SDavid Sterba goto out; 6648b87dc17SDavid Sterba } 6658b87dc17SDavid Sterba if (intarg > 0) { 6668b87dc17SDavid Sterba if (intarg > 300) { 6678b87dc17SDavid Sterba printk(KERN_WARNING 6688b87dc17SDavid Sterba "btrfs: excessive commit interval %d\n", 6698b87dc17SDavid Sterba intarg); 6708b87dc17SDavid Sterba } 6718b87dc17SDavid Sterba info->commit_interval = intarg; 6728b87dc17SDavid Sterba } else { 6738b87dc17SDavid Sterba printk(KERN_INFO 6748b87dc17SDavid Sterba "btrfs: using default commit interval %ds\n", 6758b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 6768b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 6778b87dc17SDavid Sterba } 6788b87dc17SDavid Sterba break; 679a7a3f7caSSage Weil case Opt_err: 680a7a3f7caSSage Weil printk(KERN_INFO "btrfs: unrecognized mount option " 681a7a3f7caSSage Weil "'%s'\n", p); 682a7a3f7caSSage Weil ret = -EINVAL; 683a7a3f7caSSage Weil goto out; 68495e05289SChris Mason default: 685be20aa9dSChris Mason break; 68695e05289SChris Mason } 68795e05289SChris Mason } 688a7a3f7caSSage Weil out: 68973bc1876SJosef Bacik if (!ret && btrfs_test_opt(root, SPACE_CACHE)) 69073bc1876SJosef Bacik printk(KERN_INFO "btrfs: disk space caching is enabled\n"); 691da495eccSJosef Bacik kfree(orig); 692a7a3f7caSSage Weil return ret; 693edf24abeSChristoph Hellwig } 694edf24abeSChristoph Hellwig 695edf24abeSChristoph Hellwig /* 696edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 697edf24abeSChristoph Hellwig * 698edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 699edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 700edf24abeSChristoph Hellwig */ 70197288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 70273f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 7035e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 704edf24abeSChristoph Hellwig { 705edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 70683c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 7071493381fSWang Shilong char *num = NULL; 708edf24abeSChristoph Hellwig int error = 0; 709edf24abeSChristoph Hellwig 710edf24abeSChristoph Hellwig if (!options) 711830c4adbSJosef Bacik return 0; 712edf24abeSChristoph Hellwig 713edf24abeSChristoph Hellwig /* 714edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 715edf24abeSChristoph Hellwig * gets called twice 716edf24abeSChristoph Hellwig */ 717edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 718edf24abeSChristoph Hellwig if (!opts) 719edf24abeSChristoph Hellwig return -ENOMEM; 7203f3d0bc0STero Roponen orig = opts; 721edf24abeSChristoph Hellwig 722edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 723edf24abeSChristoph Hellwig int token; 724edf24abeSChristoph Hellwig if (!*p) 725edf24abeSChristoph Hellwig continue; 726edf24abeSChristoph Hellwig 727edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 728edf24abeSChristoph Hellwig switch (token) { 729edf24abeSChristoph Hellwig case Opt_subvol: 730a90e8b6fSIlya Dryomov kfree(*subvol_name); 731edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 7322c334e87SWang Shilong if (!*subvol_name) { 7332c334e87SWang Shilong error = -ENOMEM; 7342c334e87SWang Shilong goto out; 7352c334e87SWang Shilong } 736edf24abeSChristoph Hellwig break; 73773f73415SJosef Bacik case Opt_subvolid: 7381493381fSWang Shilong num = match_strdup(&args[0]); 7391493381fSWang Shilong if (num) { 7401493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 7411493381fSWang Shilong kfree(num); 7424849f01dSJosef Bacik /* we want the original fs_tree */ 7431493381fSWang Shilong if (!*subvol_objectid) 7444849f01dSJosef Bacik *subvol_objectid = 7454849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 7462c334e87SWang Shilong } else { 7472c334e87SWang Shilong error = -EINVAL; 7482c334e87SWang Shilong goto out; 7494849f01dSJosef Bacik } 75073f73415SJosef Bacik break; 751e15d0542SXin Zhong case Opt_subvolrootid: 7525e2a4b25SDavid Sterba printk(KERN_WARNING 7535e2a4b25SDavid Sterba "btrfs: 'subvolrootid' mount option is deprecated and has no effect\n"); 754e15d0542SXin Zhong break; 75543e570b0SChristoph Hellwig case Opt_device: 75683c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 75783c8c9bdSJeff Liu if (!device_name) { 75883c8c9bdSJeff Liu error = -ENOMEM; 75983c8c9bdSJeff Liu goto out; 76083c8c9bdSJeff Liu } 76183c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 76243e570b0SChristoph Hellwig flags, holder, fs_devices); 76383c8c9bdSJeff Liu kfree(device_name); 76443e570b0SChristoph Hellwig if (error) 765830c4adbSJosef Bacik goto out; 76643e570b0SChristoph Hellwig break; 767edf24abeSChristoph Hellwig default: 768edf24abeSChristoph Hellwig break; 769edf24abeSChristoph Hellwig } 770edf24abeSChristoph Hellwig } 771edf24abeSChristoph Hellwig 772edf24abeSChristoph Hellwig out: 773830c4adbSJosef Bacik kfree(orig); 774edf24abeSChristoph Hellwig return error; 77595e05289SChris Mason } 77695e05289SChris Mason 77773f73415SJosef Bacik static struct dentry *get_default_root(struct super_block *sb, 77873f73415SJosef Bacik u64 subvol_objectid) 77973f73415SJosef Bacik { 780815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 781815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 78273f73415SJosef Bacik struct btrfs_root *new_root; 78373f73415SJosef Bacik struct btrfs_dir_item *di; 78473f73415SJosef Bacik struct btrfs_path *path; 78573f73415SJosef Bacik struct btrfs_key location; 78673f73415SJosef Bacik struct inode *inode; 78773f73415SJosef Bacik u64 dir_id; 78873f73415SJosef Bacik int new = 0; 78973f73415SJosef Bacik 79073f73415SJosef Bacik /* 79173f73415SJosef Bacik * We have a specific subvol we want to mount, just setup location and 79273f73415SJosef Bacik * go look up the root. 79373f73415SJosef Bacik */ 79473f73415SJosef Bacik if (subvol_objectid) { 79573f73415SJosef Bacik location.objectid = subvol_objectid; 79673f73415SJosef Bacik location.type = BTRFS_ROOT_ITEM_KEY; 79773f73415SJosef Bacik location.offset = (u64)-1; 79873f73415SJosef Bacik goto find_root; 79973f73415SJosef Bacik } 80073f73415SJosef Bacik 80173f73415SJosef Bacik path = btrfs_alloc_path(); 80273f73415SJosef Bacik if (!path) 80373f73415SJosef Bacik return ERR_PTR(-ENOMEM); 80473f73415SJosef Bacik path->leave_spinning = 1; 80573f73415SJosef Bacik 80673f73415SJosef Bacik /* 80773f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 80873f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 80973f73415SJosef Bacik * to mount. 81073f73415SJosef Bacik */ 811815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 81273f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 813b0839166SJulia Lawall if (IS_ERR(di)) { 814b0839166SJulia Lawall btrfs_free_path(path); 815fb4f6f91SDan Carpenter return ERR_CAST(di); 816b0839166SJulia Lawall } 81773f73415SJosef Bacik if (!di) { 81873f73415SJosef Bacik /* 81973f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 82073f73415SJosef Bacik * it's always been there, but don't freak out, just try and 82173f73415SJosef Bacik * mount to root most subvolume. 82273f73415SJosef Bacik */ 82373f73415SJosef Bacik btrfs_free_path(path); 82473f73415SJosef Bacik dir_id = BTRFS_FIRST_FREE_OBJECTID; 825815745cfSAl Viro new_root = fs_info->fs_root; 82673f73415SJosef Bacik goto setup_root; 82773f73415SJosef Bacik } 82873f73415SJosef Bacik 82973f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 83073f73415SJosef Bacik btrfs_free_path(path); 83173f73415SJosef Bacik 83273f73415SJosef Bacik find_root: 833815745cfSAl Viro new_root = btrfs_read_fs_root_no_name(fs_info, &location); 83473f73415SJosef Bacik if (IS_ERR(new_root)) 835d0b678cbSJulia Lawall return ERR_CAST(new_root); 83673f73415SJosef Bacik 83773f73415SJosef Bacik dir_id = btrfs_root_dirid(&new_root->root_item); 83873f73415SJosef Bacik setup_root: 83973f73415SJosef Bacik location.objectid = dir_id; 84073f73415SJosef Bacik location.type = BTRFS_INODE_ITEM_KEY; 84173f73415SJosef Bacik location.offset = 0; 84273f73415SJosef Bacik 84373f73415SJosef Bacik inode = btrfs_iget(sb, &location, new_root, &new); 8444cbd1149SDan Carpenter if (IS_ERR(inode)) 8454cbd1149SDan Carpenter return ERR_CAST(inode); 84673f73415SJosef Bacik 84773f73415SJosef Bacik /* 84873f73415SJosef Bacik * If we're just mounting the root most subvol put the inode and return 84973f73415SJosef Bacik * a reference to the dentry. We will have already gotten a reference 85073f73415SJosef Bacik * to the inode in btrfs_fill_super so we're good to go. 85173f73415SJosef Bacik */ 85273f73415SJosef Bacik if (!new && sb->s_root->d_inode == inode) { 85373f73415SJosef Bacik iput(inode); 85473f73415SJosef Bacik return dget(sb->s_root); 85573f73415SJosef Bacik } 85673f73415SJosef Bacik 857ba5b8958SJosef Bacik return d_obtain_alias(inode); 85873f73415SJosef Bacik } 85973f73415SJosef Bacik 8608a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 8618a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 8628a4b83ccSChris Mason void *data, int silent) 8632e635a27SChris Mason { 8642e635a27SChris Mason struct inode *inode; 865815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 8665d4f98a2SYan Zheng struct btrfs_key key; 86739279cc3SChris Mason int err; 8682e635a27SChris Mason 8692e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 8702e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 871e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 872af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 873be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 8745103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 8752e635a27SChris Mason sb->s_time_gran = 1; 8760eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 87733268eafSJosef Bacik sb->s_flags |= MS_POSIXACL; 87849cf6f45SChris Ball #endif 8790c4d2d95SJosef Bacik sb->s_flags |= MS_I_VERSION; 880ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 881ad2b2c80SAl Viro if (err) { 882e20d96d6SChris Mason printk("btrfs: open_ctree failed\n"); 883ad2b2c80SAl Viro return err; 884e20d96d6SChris Mason } 885b888db2bSChris Mason 8865d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 8875d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 8885d4f98a2SYan Zheng key.offset = 0; 88998c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 8905d4f98a2SYan Zheng if (IS_ERR(inode)) { 8915d4f98a2SYan Zheng err = PTR_ERR(inode); 89239279cc3SChris Mason goto fail_close; 89339279cc3SChris Mason } 8942e635a27SChris Mason 89548fde701SAl Viro sb->s_root = d_make_root(inode); 89648fde701SAl Viro if (!sb->s_root) { 89739279cc3SChris Mason err = -ENOMEM; 89839279cc3SChris Mason goto fail_close; 8992e635a27SChris Mason } 90058176a96SJosef Bacik 9016885f308SChris Mason save_mount_options(sb, data); 90290a887c9SDan Magenheimer cleancache_init_fs(sb); 90359553edfSAl Viro sb->s_flags |= MS_ACTIVE; 9042e635a27SChris Mason return 0; 9052e635a27SChris Mason 90639279cc3SChris Mason fail_close: 907815745cfSAl Viro close_ctree(fs_info->tree_root); 908d5719762SChris Mason return err; 909d5719762SChris Mason } 910d5719762SChris Mason 9116bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 912d5719762SChris Mason { 913d5719762SChris Mason struct btrfs_trans_handle *trans; 914815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 915815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 916df2ce34cSChris Mason 9171abe9b8aSliubo trace_btrfs_sync_fs(wait); 9181abe9b8aSliubo 919d561c025SChris Mason if (!wait) { 920815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 921df2ce34cSChris Mason return 0; 922d561c025SChris Mason } 923771ed689SChris Mason 924f0de181cSJosef Bacik btrfs_wait_all_ordered_extents(fs_info); 925771ed689SChris Mason 926d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 92760376ce4SJosef Bacik if (IS_ERR(trans)) { 928354aa0fbSMiao Xie /* no transaction, don't bother */ 929354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 930bd7de2c9SJosef Bacik return 0; 93198d5dc13STsutomu Itoh return PTR_ERR(trans); 93260376ce4SJosef Bacik } 933bd7de2c9SJosef Bacik return btrfs_commit_transaction(trans, root); 934d5719762SChris Mason } 935d5719762SChris Mason 93634c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 937a9572a15SEric Paris { 938815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 939815745cfSAl Viro struct btrfs_root *root = info->tree_root; 940200da64eSTsutomu Itoh char *compress_type; 941a9572a15SEric Paris 942a9572a15SEric Paris if (btrfs_test_opt(root, DEGRADED)) 943a9572a15SEric Paris seq_puts(seq, ",degraded"); 944a9572a15SEric Paris if (btrfs_test_opt(root, NODATASUM)) 945a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 946a9572a15SEric Paris if (btrfs_test_opt(root, NODATACOW)) 947a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 948a9572a15SEric Paris if (btrfs_test_opt(root, NOBARRIER)) 949a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 950a9572a15SEric Paris if (info->max_inline != 8192 * 1024) 951c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 952a9572a15SEric Paris if (info->alloc_start != 0) 953c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",alloc_start=%llu", info->alloc_start); 954a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 955a9572a15SEric Paris num_online_cpus() + 2, 8)) 956a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 957200da64eSTsutomu Itoh if (btrfs_test_opt(root, COMPRESS)) { 958200da64eSTsutomu Itoh if (info->compress_type == BTRFS_COMPRESS_ZLIB) 959200da64eSTsutomu Itoh compress_type = "zlib"; 960200da64eSTsutomu Itoh else 961200da64eSTsutomu Itoh compress_type = "lzo"; 962200da64eSTsutomu Itoh if (btrfs_test_opt(root, FORCE_COMPRESS)) 963200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 964200da64eSTsutomu Itoh else 965200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 966200da64eSTsutomu Itoh } 967c289811cSChris Mason if (btrfs_test_opt(root, NOSSD)) 968c289811cSChris Mason seq_puts(seq, ",nossd"); 969451d7585SChris Mason if (btrfs_test_opt(root, SSD_SPREAD)) 970451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 971451d7585SChris Mason else if (btrfs_test_opt(root, SSD)) 972a9572a15SEric Paris seq_puts(seq, ",ssd"); 9733a5e1404SSage Weil if (btrfs_test_opt(root, NOTREELOG)) 9746b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 975dccae999SSage Weil if (btrfs_test_opt(root, FLUSHONCOMMIT)) 9766b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 97720a5239aSMatthew Wilcox if (btrfs_test_opt(root, DISCARD)) 97820a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 979a9572a15SEric Paris if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 980a9572a15SEric Paris seq_puts(seq, ",noacl"); 981200da64eSTsutomu Itoh if (btrfs_test_opt(root, SPACE_CACHE)) 982200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 98373bc1876SJosef Bacik else 9848965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 985f420ee1eSStefan Behrens if (btrfs_test_opt(root, RESCAN_UUID_TREE)) 986f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 987200da64eSTsutomu Itoh if (btrfs_test_opt(root, CLEAR_CACHE)) 988200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 989200da64eSTsutomu Itoh if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) 990200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 9910942caa3SDavid Sterba if (btrfs_test_opt(root, ENOSPC_DEBUG)) 9920942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 9930942caa3SDavid Sterba if (btrfs_test_opt(root, AUTO_DEFRAG)) 9940942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 9950942caa3SDavid Sterba if (btrfs_test_opt(root, INODE_MAP_CACHE)) 9960942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 9979555c6c1SIlya Dryomov if (btrfs_test_opt(root, SKIP_BALANCE)) 9989555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 9998507d216SWang Shilong if (btrfs_test_opt(root, RECOVERY)) 10008507d216SWang Shilong seq_puts(seq, ",recovery"); 10018507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 10028507d216SWang Shilong if (btrfs_test_opt(root, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 10038507d216SWang Shilong seq_puts(seq, ",check_int_data"); 10048507d216SWang Shilong else if (btrfs_test_opt(root, CHECK_INTEGRITY)) 10058507d216SWang Shilong seq_puts(seq, ",check_int"); 10068507d216SWang Shilong if (info->check_integrity_print_mask) 10078507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 10088507d216SWang Shilong info->check_integrity_print_mask); 10098507d216SWang Shilong #endif 10108507d216SWang Shilong if (info->metadata_ratio) 10118507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 10128507d216SWang Shilong info->metadata_ratio); 10138c342930SJeff Mahoney if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR)) 10148c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 10158b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 10168b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1017a9572a15SEric Paris return 0; 1018a9572a15SEric Paris } 1019a9572a15SEric Paris 1020a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 10212e635a27SChris Mason { 1022815745cfSAl Viro struct btrfs_fs_info *p = data; 1023815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 10244b82d6e4SYan 1025815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 10264b82d6e4SYan } 10274b82d6e4SYan 1028450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1029450ba0eaSJosef Bacik { 10306de1d09dSAl Viro int err = set_anon_super(s, data); 10316de1d09dSAl Viro if (!err) 1032450ba0eaSJosef Bacik s->s_fs_info = data; 10336de1d09dSAl Viro return err; 1034450ba0eaSJosef Bacik } 1035450ba0eaSJosef Bacik 1036830c4adbSJosef Bacik /* 1037f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1038f9d9ef62SDavid Sterba */ 1039f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1040f9d9ef62SDavid Sterba { 1041f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1042f9d9ef62SDavid Sterba return 1; 1043f9d9ef62SDavid Sterba return 0; 1044f9d9ef62SDavid Sterba } 1045f9d9ef62SDavid Sterba 1046f9d9ef62SDavid Sterba /* 1047830c4adbSJosef Bacik * This will strip out the subvol=%s argument for an argument string and add 1048830c4adbSJosef Bacik * subvolid=0 to make sure we get the actual tree root for path walking to the 1049830c4adbSJosef Bacik * subvol we want. 1050830c4adbSJosef Bacik */ 1051830c4adbSJosef Bacik static char *setup_root_args(char *args) 1052830c4adbSJosef Bacik { 1053f60d16a8SJim Meyering unsigned len = strlen(args) + 2 + 1; 1054f60d16a8SJim Meyering char *src, *dst, *buf; 1055830c4adbSJosef Bacik 1056830c4adbSJosef Bacik /* 1057f60d16a8SJim Meyering * We need the same args as before, but with this substitution: 1058f60d16a8SJim Meyering * s!subvol=[^,]+!subvolid=0! 1059830c4adbSJosef Bacik * 1060f60d16a8SJim Meyering * Since the replacement string is up to 2 bytes longer than the 1061f60d16a8SJim Meyering * original, allocate strlen(args) + 2 + 1 bytes. 1062830c4adbSJosef Bacik */ 1063830c4adbSJosef Bacik 1064f60d16a8SJim Meyering src = strstr(args, "subvol="); 1065830c4adbSJosef Bacik /* This shouldn't happen, but just in case.. */ 1066f60d16a8SJim Meyering if (!src) 1067830c4adbSJosef Bacik return NULL; 1068f60d16a8SJim Meyering 1069f60d16a8SJim Meyering buf = dst = kmalloc(len, GFP_NOFS); 1070f60d16a8SJim Meyering if (!buf) 1071f60d16a8SJim Meyering return NULL; 1072830c4adbSJosef Bacik 1073830c4adbSJosef Bacik /* 1074f60d16a8SJim Meyering * If the subvol= arg is not at the start of the string, 1075f60d16a8SJim Meyering * copy whatever precedes it into buf. 1076830c4adbSJosef Bacik */ 1077f60d16a8SJim Meyering if (src != args) { 1078f60d16a8SJim Meyering *src++ = '\0'; 1079f60d16a8SJim Meyering strcpy(buf, args); 1080f60d16a8SJim Meyering dst += strlen(args); 1081830c4adbSJosef Bacik } 1082830c4adbSJosef Bacik 1083f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1084f60d16a8SJim Meyering dst += strlen("subvolid=0"); 1085830c4adbSJosef Bacik 1086830c4adbSJosef Bacik /* 1087f60d16a8SJim Meyering * If there is a "," after the original subvol=... string, 1088f60d16a8SJim Meyering * copy that suffix into our buffer. Otherwise, we're done. 1089830c4adbSJosef Bacik */ 1090f60d16a8SJim Meyering src = strchr(src, ','); 1091f60d16a8SJim Meyering if (src) 1092f60d16a8SJim Meyering strcpy(dst, src); 1093830c4adbSJosef Bacik 1094f60d16a8SJim Meyering return buf; 1095830c4adbSJosef Bacik } 1096830c4adbSJosef Bacik 1097830c4adbSJosef Bacik static struct dentry *mount_subvol(const char *subvol_name, int flags, 1098830c4adbSJosef Bacik const char *device_name, char *data) 1099830c4adbSJosef Bacik { 1100830c4adbSJosef Bacik struct dentry *root; 1101830c4adbSJosef Bacik struct vfsmount *mnt; 1102830c4adbSJosef Bacik char *newargs; 1103830c4adbSJosef Bacik 1104830c4adbSJosef Bacik newargs = setup_root_args(data); 1105830c4adbSJosef Bacik if (!newargs) 1106830c4adbSJosef Bacik return ERR_PTR(-ENOMEM); 1107830c4adbSJosef Bacik mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name, 1108830c4adbSJosef Bacik newargs); 1109830c4adbSJosef Bacik kfree(newargs); 1110830c4adbSJosef Bacik if (IS_ERR(mnt)) 1111830c4adbSJosef Bacik return ERR_CAST(mnt); 1112830c4adbSJosef Bacik 1113ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1114830c4adbSJosef Bacik 1115ea441d11SAl Viro if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) { 1116ea441d11SAl Viro struct super_block *s = root->d_sb; 1117ea441d11SAl Viro dput(root); 1118ea441d11SAl Viro root = ERR_PTR(-EINVAL); 1119ea441d11SAl Viro deactivate_locked_super(s); 1120f9d9ef62SDavid Sterba printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n", 1121f9d9ef62SDavid Sterba subvol_name); 1122f9d9ef62SDavid Sterba } 1123f9d9ef62SDavid Sterba 1124830c4adbSJosef Bacik return root; 1125830c4adbSJosef Bacik } 1126450ba0eaSJosef Bacik 1127edf24abeSChristoph Hellwig /* 1128edf24abeSChristoph Hellwig * Find a superblock for the given device / mount point. 1129edf24abeSChristoph Hellwig * 1130edf24abeSChristoph Hellwig * Note: This is based on get_sb_bdev from fs/super.c with a few additions 1131edf24abeSChristoph Hellwig * for multiple device setup. Make sure to keep it in sync. 1132edf24abeSChristoph Hellwig */ 1133061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1134306e16ceSDavid Sterba const char *device_name, void *data) 11354b82d6e4SYan { 11364b82d6e4SYan struct block_device *bdev = NULL; 11374b82d6e4SYan struct super_block *s; 11384b82d6e4SYan struct dentry *root; 11398a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1140450ba0eaSJosef Bacik struct btrfs_fs_info *fs_info = NULL; 114197288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 114273f73415SJosef Bacik char *subvol_name = NULL; 114373f73415SJosef Bacik u64 subvol_objectid = 0; 11444b82d6e4SYan int error = 0; 11454b82d6e4SYan 114697288f2cSChristoph Hellwig if (!(flags & MS_RDONLY)) 114797288f2cSChristoph Hellwig mode |= FMODE_WRITE; 114897288f2cSChristoph Hellwig 114997288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 115073f73415SJosef Bacik &subvol_name, &subvol_objectid, 11515e2a4b25SDavid Sterba &fs_devices); 1152f23c8af8SIlya Dryomov if (error) { 1153f23c8af8SIlya Dryomov kfree(subvol_name); 1154061dbc6bSAl Viro return ERR_PTR(error); 1155f23c8af8SIlya Dryomov } 1156edf24abeSChristoph Hellwig 1157830c4adbSJosef Bacik if (subvol_name) { 1158830c4adbSJosef Bacik root = mount_subvol(subvol_name, flags, device_name, data); 1159830c4adbSJosef Bacik kfree(subvol_name); 1160830c4adbSJosef Bacik return root; 1161830c4adbSJosef Bacik } 1162830c4adbSJosef Bacik 1163306e16ceSDavid Sterba error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 11648a4b83ccSChris Mason if (error) 1165830c4adbSJosef Bacik return ERR_PTR(error); 11664b82d6e4SYan 1167450ba0eaSJosef Bacik /* 1168450ba0eaSJosef Bacik * Setup a dummy root and fs_info for test/set super. This is because 1169450ba0eaSJosef Bacik * we don't actually fill this stuff out until open_ctree, but we need 1170450ba0eaSJosef Bacik * it for searching for existing supers, so this lets us do that and 1171450ba0eaSJosef Bacik * then open_ctree will properly initialize everything later. 1172450ba0eaSJosef Bacik */ 1173450ba0eaSJosef Bacik fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS); 117404d21a24SIlya Dryomov if (!fs_info) 117504d21a24SIlya Dryomov return ERR_PTR(-ENOMEM); 117604d21a24SIlya Dryomov 1177450ba0eaSJosef Bacik fs_info->fs_devices = fs_devices; 1178450ba0eaSJosef Bacik 11796c41761fSDavid Sterba fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11806c41761fSDavid Sterba fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS); 11816c41761fSDavid Sterba if (!fs_info->super_copy || !fs_info->super_for_commit) { 11826c41761fSDavid Sterba error = -ENOMEM; 118304d21a24SIlya Dryomov goto error_fs_info; 118404d21a24SIlya Dryomov } 118504d21a24SIlya Dryomov 118604d21a24SIlya Dryomov error = btrfs_open_devices(fs_devices, mode, fs_type); 118704d21a24SIlya Dryomov if (error) 118804d21a24SIlya Dryomov goto error_fs_info; 118904d21a24SIlya Dryomov 119004d21a24SIlya Dryomov if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) { 119104d21a24SIlya Dryomov error = -EACCES; 11926c41761fSDavid Sterba goto error_close_devices; 11936c41761fSDavid Sterba } 11946c41761fSDavid Sterba 1195dfe25020SChris Mason bdev = fs_devices->latest_bdev; 11969249e17fSDavid Howells s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC, 11979249e17fSDavid Howells fs_info); 1198830c4adbSJosef Bacik if (IS_ERR(s)) { 1199830c4adbSJosef Bacik error = PTR_ERR(s); 1200830c4adbSJosef Bacik goto error_close_devices; 1201830c4adbSJosef Bacik } 12024b82d6e4SYan 12034b82d6e4SYan if (s->s_root) { 12042b82032cSYan Zheng btrfs_close_devices(fs_devices); 12056c41761fSDavid Sterba free_fs_info(fs_info); 120659553edfSAl Viro if ((flags ^ s->s_flags) & MS_RDONLY) 120759553edfSAl Viro error = -EBUSY; 12084b82d6e4SYan } else { 12094b82d6e4SYan char b[BDEVNAME_SIZE]; 12104b82d6e4SYan 12114b82d6e4SYan strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id)); 1212815745cfSAl Viro btrfs_sb(s)->bdev_holder = fs_type; 12138a4b83ccSChris Mason error = btrfs_fill_super(s, fs_devices, data, 12148a4b83ccSChris Mason flags & MS_SILENT ? 1 : 0); 12154b82d6e4SYan } 12164b82d6e4SYan 121759553edfSAl Viro root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error); 121859553edfSAl Viro if (IS_ERR(root)) 1219e15d0542SXin Zhong deactivate_locked_super(s); 12204b82d6e4SYan 1221061dbc6bSAl Viro return root; 12224b82d6e4SYan 1223c146afadSYan Zheng error_close_devices: 12248a4b83ccSChris Mason btrfs_close_devices(fs_devices); 122504d21a24SIlya Dryomov error_fs_info: 12266c41761fSDavid Sterba free_fs_info(fs_info); 1227061dbc6bSAl Viro return ERR_PTR(error); 12284b82d6e4SYan } 12292e635a27SChris Mason 12300d2450abSSergei Trofimovich static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit) 12310d2450abSSergei Trofimovich { 12320d2450abSSergei Trofimovich spin_lock_irq(&workers->lock); 12330d2450abSSergei Trofimovich workers->max_workers = new_limit; 12340d2450abSSergei Trofimovich spin_unlock_irq(&workers->lock); 12350d2450abSSergei Trofimovich } 12360d2450abSSergei Trofimovich 12370d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 12380d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 12390d2450abSSergei Trofimovich { 12400d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 12410d2450abSSergei Trofimovich return; 12420d2450abSSergei Trofimovich 12430d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 12440d2450abSSergei Trofimovich 12450d2450abSSergei Trofimovich printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n", 12460d2450abSSergei Trofimovich old_pool_size, new_pool_size); 12470d2450abSSergei Trofimovich 12480d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size); 12490d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->workers, new_pool_size); 12500d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size); 12510d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size); 12520d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size); 12530d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size); 12540d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size); 12550d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size); 12560d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size); 12570d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size); 12580d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size); 12590d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size); 12600d2450abSSergei Trofimovich btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size); 1261ff023aacSStefan Behrens btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers, 1262ff023aacSStefan Behrens new_pool_size); 12630d2450abSSergei Trofimovich } 12640d2450abSSergei Trofimovich 1265f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1266dc81cdc5SMiao Xie { 1267dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1268f42a34b2SMiao Xie } 1269dc81cdc5SMiao Xie 1270f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1271f42a34b2SMiao Xie unsigned long old_opts, int flags) 1272f42a34b2SMiao Xie { 1273dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1274dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1275dc81cdc5SMiao Xie (flags & MS_RDONLY))) { 1276dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1277dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1278dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 1279dc81cdc5SMiao Xie if (flags & MS_RDONLY) 1280dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1281dc81cdc5SMiao Xie } 1282dc81cdc5SMiao Xie } 1283dc81cdc5SMiao Xie 1284dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1285dc81cdc5SMiao Xie unsigned long old_opts) 1286dc81cdc5SMiao Xie { 1287dc81cdc5SMiao Xie /* 1288dc81cdc5SMiao Xie * We need cleanup all defragable inodes if the autodefragment is 1289dc81cdc5SMiao Xie * close or the fs is R/O. 1290dc81cdc5SMiao Xie */ 1291dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1292dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 1293dc81cdc5SMiao Xie (fs_info->sb->s_flags & MS_RDONLY))) { 1294dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1295dc81cdc5SMiao Xie } 1296dc81cdc5SMiao Xie 1297dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1298dc81cdc5SMiao Xie } 1299dc81cdc5SMiao Xie 1300c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1301c146afadSYan Zheng { 1302815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1303815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 130449b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 130549b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 130649b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 130749b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 130849b25e05SJeff Mahoney u64 old_alloc_start = fs_info->alloc_start; 130949b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 131049b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1311c146afadSYan Zheng int ret; 1312c146afadSYan Zheng 1313f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1314dc81cdc5SMiao Xie 1315b288052eSChris Mason ret = btrfs_parse_options(root, data); 131649b25e05SJeff Mahoney if (ret) { 131749b25e05SJeff Mahoney ret = -EINVAL; 131849b25e05SJeff Mahoney goto restore; 131949b25e05SJeff Mahoney } 1320b288052eSChris Mason 1321f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 13220d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 13230d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 13240d2450abSSergei Trofimovich 1325c146afadSYan Zheng if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1326dc81cdc5SMiao Xie goto out; 1327c146afadSYan Zheng 1328c146afadSYan Zheng if (*flags & MS_RDONLY) { 13298dabb742SStefan Behrens /* 13308dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 13318dabb742SStefan Behrens * the filesystem is busy. 13328dabb742SStefan Behrens */ 1333c146afadSYan Zheng sb->s_flags |= MS_RDONLY; 1334c146afadSYan Zheng 13358dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 13368dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1337061594efSMiao Xie btrfs_pause_balance(fs_info); 13388dabb742SStefan Behrens 1339c146afadSYan Zheng ret = btrfs_commit_super(root); 134049b25e05SJeff Mahoney if (ret) 134149b25e05SJeff Mahoney goto restore; 1342c146afadSYan Zheng } else { 13436ef3de9cSDavid Sterba if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 13446ef3de9cSDavid Sterba btrfs_err(fs_info, 13456ef3de9cSDavid Sterba "Remounting read-write after error is not allowed\n"); 13466ef3de9cSDavid Sterba ret = -EINVAL; 13476ef3de9cSDavid Sterba goto restore; 13486ef3de9cSDavid Sterba } 13498a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 135049b25e05SJeff Mahoney ret = -EACCES; 135149b25e05SJeff Mahoney goto restore; 13528a3db184SSergei Trofimovich } 13532b82032cSYan Zheng 1354292fd7fcSStefan Behrens if (fs_info->fs_devices->missing_devices > 1355292fd7fcSStefan Behrens fs_info->num_tolerated_disk_barrier_failures && 1356292fd7fcSStefan Behrens !(*flags & MS_RDONLY)) { 1357292fd7fcSStefan Behrens printk(KERN_WARNING 1358292fd7fcSStefan Behrens "Btrfs: too many missing devices, writeable remount is not allowed\n"); 1359292fd7fcSStefan Behrens ret = -EACCES; 1360292fd7fcSStefan Behrens goto restore; 1361292fd7fcSStefan Behrens } 1362292fd7fcSStefan Behrens 13638a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 136449b25e05SJeff Mahoney ret = -EINVAL; 136549b25e05SJeff Mahoney goto restore; 13668a3db184SSergei Trofimovich } 1367c146afadSYan Zheng 1368815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 136949b25e05SJeff Mahoney if (ret) 137049b25e05SJeff Mahoney goto restore; 1371c146afadSYan Zheng 1372d68fc57bSYan, Zheng /* recover relocation */ 1373d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 137449b25e05SJeff Mahoney if (ret) 137549b25e05SJeff Mahoney goto restore; 1376c146afadSYan Zheng 13772b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 13782b6ba629SIlya Dryomov if (ret) 13792b6ba629SIlya Dryomov goto restore; 13802b6ba629SIlya Dryomov 13818dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 13828dabb742SStefan Behrens if (ret) { 13838dabb742SStefan Behrens pr_warn("btrfs: failed to resume dev_replace\n"); 13848dabb742SStefan Behrens goto restore; 13858dabb742SStefan Behrens } 138694aebfb2SJosef Bacik 138794aebfb2SJosef Bacik if (!fs_info->uuid_root) { 138894aebfb2SJosef Bacik pr_info("btrfs: creating UUID tree\n"); 138994aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 139094aebfb2SJosef Bacik if (ret) { 139194aebfb2SJosef Bacik pr_warn("btrfs: failed to create the uuid tree" 139294aebfb2SJosef Bacik "%d\n", ret); 139394aebfb2SJosef Bacik goto restore; 139494aebfb2SJosef Bacik } 139594aebfb2SJosef Bacik } 1396c146afadSYan Zheng sb->s_flags &= ~MS_RDONLY; 1397c146afadSYan Zheng } 1398dc81cdc5SMiao Xie out: 1399dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1400c146afadSYan Zheng return 0; 140149b25e05SJeff Mahoney 140249b25e05SJeff Mahoney restore: 140349b25e05SJeff Mahoney /* We've hit an error - don't reset MS_RDONLY */ 140449b25e05SJeff Mahoney if (sb->s_flags & MS_RDONLY) 140549b25e05SJeff Mahoney old_flags |= MS_RDONLY; 140649b25e05SJeff Mahoney sb->s_flags = old_flags; 140749b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 140849b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 140949b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 1410c018daecSMiao Xie mutex_lock(&fs_info->chunk_mutex); 141149b25e05SJeff Mahoney fs_info->alloc_start = old_alloc_start; 1412c018daecSMiao Xie mutex_unlock(&fs_info->chunk_mutex); 14130d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 14140d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 141549b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1416dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 141749b25e05SJeff Mahoney return ret; 1418c146afadSYan Zheng } 1419c146afadSYan Zheng 1420bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1421bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1422bcd53741SArne Jansen const void *dev_info2) 1423bcd53741SArne Jansen { 1424bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1425bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1426bcd53741SArne Jansen return -1; 1427bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1428bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1429bcd53741SArne Jansen return 1; 1430bcd53741SArne Jansen else 1431bcd53741SArne Jansen return 0; 1432bcd53741SArne Jansen } 1433bcd53741SArne Jansen 1434bcd53741SArne Jansen /* 1435bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1436bcd53741SArne Jansen * is stored.(Descending Sort) 1437bcd53741SArne Jansen */ 1438bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1439bcd53741SArne Jansen struct btrfs_device_info *devices, 1440bcd53741SArne Jansen size_t nr_devices) 1441bcd53741SArne Jansen { 1442bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1443bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1444bcd53741SArne Jansen } 1445bcd53741SArne Jansen 14466d07bcecSMiao Xie /* 14476d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 14486d07bcecSMiao Xie * file data. 14496d07bcecSMiao Xie */ 14506d07bcecSMiao Xie static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes) 14516d07bcecSMiao Xie { 14526d07bcecSMiao Xie struct btrfs_fs_info *fs_info = root->fs_info; 14536d07bcecSMiao Xie struct btrfs_device_info *devices_info; 14546d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 14556d07bcecSMiao Xie struct btrfs_device *device; 14566d07bcecSMiao Xie u64 skip_space; 14576d07bcecSMiao Xie u64 type; 14586d07bcecSMiao Xie u64 avail_space; 14596d07bcecSMiao Xie u64 used_space; 14606d07bcecSMiao Xie u64 min_stripe_size; 146139fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 14626d07bcecSMiao Xie int i = 0, nr_devices; 14636d07bcecSMiao Xie int ret; 14646d07bcecSMiao Xie 1465b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 14666d07bcecSMiao Xie BUG_ON(!nr_devices); 14676d07bcecSMiao Xie 14686d07bcecSMiao Xie devices_info = kmalloc(sizeof(*devices_info) * nr_devices, 14696d07bcecSMiao Xie GFP_NOFS); 14706d07bcecSMiao Xie if (!devices_info) 14716d07bcecSMiao Xie return -ENOMEM; 14726d07bcecSMiao Xie 14736d07bcecSMiao Xie /* calc min stripe number for data space alloction */ 14746d07bcecSMiao Xie type = btrfs_get_alloc_profile(root, 1); 147539fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 14766d07bcecSMiao Xie min_stripes = 2; 147739fb26c3SMiao Xie num_stripes = nr_devices; 147839fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 14796d07bcecSMiao Xie min_stripes = 2; 148039fb26c3SMiao Xie num_stripes = 2; 148139fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 14826d07bcecSMiao Xie min_stripes = 4; 148339fb26c3SMiao Xie num_stripes = 4; 148439fb26c3SMiao Xie } 14856d07bcecSMiao Xie 14866d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 14876d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 14886d07bcecSMiao Xie else 14896d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 14906d07bcecSMiao Xie 1491b772a86eSLi Zefan list_for_each_entry(device, &fs_devices->devices, dev_list) { 149263a212abSStefan Behrens if (!device->in_fs_metadata || !device->bdev || 149363a212abSStefan Behrens device->is_tgtdev_for_dev_replace) 14946d07bcecSMiao Xie continue; 14956d07bcecSMiao Xie 14966d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 14976d07bcecSMiao Xie 14986d07bcecSMiao Xie /* align with stripe_len */ 14996d07bcecSMiao Xie do_div(avail_space, BTRFS_STRIPE_LEN); 15006d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 15016d07bcecSMiao Xie 15026d07bcecSMiao Xie /* 15036d07bcecSMiao Xie * In order to avoid overwritting the superblock on the drive, 15046d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 15056d07bcecSMiao Xie * allocation. 15066d07bcecSMiao Xie */ 15076d07bcecSMiao Xie skip_space = 1024 * 1024; 15086d07bcecSMiao Xie 15096d07bcecSMiao Xie /* user can set the offset in fs_info->alloc_start. */ 15106d07bcecSMiao Xie if (fs_info->alloc_start + BTRFS_STRIPE_LEN <= 15116d07bcecSMiao Xie device->total_bytes) 15126d07bcecSMiao Xie skip_space = max(fs_info->alloc_start, skip_space); 15136d07bcecSMiao Xie 15146d07bcecSMiao Xie /* 15156d07bcecSMiao Xie * btrfs can not use the free space in [0, skip_space - 1], 15166d07bcecSMiao Xie * we must subtract it from the total. In order to implement 15176d07bcecSMiao Xie * it, we account the used space in this range first. 15186d07bcecSMiao Xie */ 15196d07bcecSMiao Xie ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1, 15206d07bcecSMiao Xie &used_space); 15216d07bcecSMiao Xie if (ret) { 15226d07bcecSMiao Xie kfree(devices_info); 15236d07bcecSMiao Xie return ret; 15246d07bcecSMiao Xie } 15256d07bcecSMiao Xie 15266d07bcecSMiao Xie /* calc the free space in [0, skip_space - 1] */ 15276d07bcecSMiao Xie skip_space -= used_space; 15286d07bcecSMiao Xie 15296d07bcecSMiao Xie /* 15306d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 15316d07bcecSMiao Xie * it from the total. 15326d07bcecSMiao Xie */ 15336d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 15346d07bcecSMiao Xie avail_space -= skip_space; 15356d07bcecSMiao Xie else 15366d07bcecSMiao Xie avail_space = 0; 15376d07bcecSMiao Xie 15386d07bcecSMiao Xie if (avail_space < min_stripe_size) 15396d07bcecSMiao Xie continue; 15406d07bcecSMiao Xie 15416d07bcecSMiao Xie devices_info[i].dev = device; 15426d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 15436d07bcecSMiao Xie 15446d07bcecSMiao Xie i++; 15456d07bcecSMiao Xie } 15466d07bcecSMiao Xie 15476d07bcecSMiao Xie nr_devices = i; 15486d07bcecSMiao Xie 15496d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 15506d07bcecSMiao Xie 15516d07bcecSMiao Xie i = nr_devices - 1; 15526d07bcecSMiao Xie avail_space = 0; 15536d07bcecSMiao Xie while (nr_devices >= min_stripes) { 155439fb26c3SMiao Xie if (num_stripes > nr_devices) 155539fb26c3SMiao Xie num_stripes = nr_devices; 155639fb26c3SMiao Xie 15576d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 15586d07bcecSMiao Xie int j; 15596d07bcecSMiao Xie u64 alloc_size; 15606d07bcecSMiao Xie 156139fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 15626d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 156339fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 15646d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 15656d07bcecSMiao Xie } 15666d07bcecSMiao Xie i--; 15676d07bcecSMiao Xie nr_devices--; 15686d07bcecSMiao Xie } 15696d07bcecSMiao Xie 15706d07bcecSMiao Xie kfree(devices_info); 15716d07bcecSMiao Xie *free_bytes = avail_space; 15726d07bcecSMiao Xie return 0; 15736d07bcecSMiao Xie } 15746d07bcecSMiao Xie 15758fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 15768fd17795SChris Mason { 1577815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 1578815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 1579815745cfSAl Viro struct list_head *head = &fs_info->space_info; 1580bd4d1088SJosef Bacik struct btrfs_space_info *found; 1581bd4d1088SJosef Bacik u64 total_used = 0; 15826d07bcecSMiao Xie u64 total_free_data = 0; 1583db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 1584815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 15856d07bcecSMiao Xie int ret; 15868fd17795SChris Mason 15876d07bcecSMiao Xie /* holding chunk_muext to avoid allocating new chunks */ 1588815745cfSAl Viro mutex_lock(&fs_info->chunk_mutex); 1589bd4d1088SJosef Bacik rcu_read_lock(); 159089a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 15916d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 15926d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 15936d07bcecSMiao Xie total_free_data -= 15946d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 15956d07bcecSMiao Xie } 15966d07bcecSMiao Xie 1597b742bb82SYan, Zheng total_used += found->disk_used; 159889a55897SJosef Bacik } 1599bd4d1088SJosef Bacik rcu_read_unlock(); 1600bd4d1088SJosef Bacik 16018fd17795SChris Mason buf->f_namelen = BTRFS_NAME_LEN; 1602db94535dSChris Mason buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 1603bd4d1088SJosef Bacik buf->f_bfree = buf->f_blocks - (total_used >> bits); 16048fd17795SChris Mason buf->f_bsize = dentry->d_sb->s_blocksize; 16058fd17795SChris Mason buf->f_type = BTRFS_SUPER_MAGIC; 16066d07bcecSMiao Xie buf->f_bavail = total_free_data; 1607815745cfSAl Viro ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data); 16086d07bcecSMiao Xie if (ret) { 1609815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 16106d07bcecSMiao Xie return ret; 16116d07bcecSMiao Xie } 16126d07bcecSMiao Xie buf->f_bavail += total_free_data; 16136d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 1614815745cfSAl Viro mutex_unlock(&fs_info->chunk_mutex); 1615d397712bSChris Mason 16169d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 16179d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 16189d03632eSDavid Woodhouse on a big-endian or little-endian host */ 16199d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 16209d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 162132d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 162232d48fa1SDavid Woodhouse buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32; 162332d48fa1SDavid Woodhouse buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid; 162432d48fa1SDavid Woodhouse 16258fd17795SChris Mason return 0; 16268fd17795SChris Mason } 1627b5133862SChris Mason 1628aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 1629aea52e19SAl Viro { 1630815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1631aea52e19SAl Viro kill_anon_super(sb); 1632aea52e19SAl Viro free_fs_info(fs_info); 1633aea52e19SAl Viro } 1634aea52e19SAl Viro 16352e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 16362e635a27SChris Mason .owner = THIS_MODULE, 16372e635a27SChris Mason .name = "btrfs", 1638061dbc6bSAl Viro .mount = btrfs_mount, 1639aea52e19SAl Viro .kill_sb = btrfs_kill_super, 16402e635a27SChris Mason .fs_flags = FS_REQUIRES_DEV, 16412e635a27SChris Mason }; 16427f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 1643a9218f6bSChris Mason 1644d352ac68SChris Mason /* 1645d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 1646d352ac68SChris Mason */ 16478a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 16488a4b83ccSChris Mason unsigned long arg) 16498a4b83ccSChris Mason { 16508a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 16518a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 1652c071fcfdSChris Mason int ret = -ENOTTY; 16538a4b83ccSChris Mason 1654e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 1655e441d54dSChris Mason return -EPERM; 1656e441d54dSChris Mason 1657dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 1658dae7b665SLi Zefan if (IS_ERR(vol)) 1659dae7b665SLi Zefan return PTR_ERR(vol); 1660c071fcfdSChris Mason 16618a4b83ccSChris Mason switch (cmd) { 16628a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 166397288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 16648a4b83ccSChris Mason &btrfs_fs_type, &fs_devices); 16658a4b83ccSChris Mason break; 166602db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 166702db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 166802db0844SJosef Bacik &btrfs_fs_type, &fs_devices); 166902db0844SJosef Bacik if (ret) 167002db0844SJosef Bacik break; 167102db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 167202db0844SJosef Bacik break; 16738a4b83ccSChris Mason } 1674dae7b665SLi Zefan 16758a4b83ccSChris Mason kfree(vol); 1676f819d837SLinda Knippers return ret; 16778a4b83ccSChris Mason } 16788a4b83ccSChris Mason 16790176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 1680ed0dab6bSYan { 1681354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 1682354aa0fbSMiao Xie struct btrfs_root *root = btrfs_sb(sb)->tree_root; 1683354aa0fbSMiao Xie 1684d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 1685354aa0fbSMiao Xie if (IS_ERR(trans)) { 1686354aa0fbSMiao Xie /* no transaction, don't bother */ 1687354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 16880176260fSLinus Torvalds return 0; 1689354aa0fbSMiao Xie return PTR_ERR(trans); 1690354aa0fbSMiao Xie } 1691354aa0fbSMiao Xie return btrfs_commit_transaction(trans, root); 1692ed0dab6bSYan } 1693ed0dab6bSYan 16940176260fSLinus Torvalds static int btrfs_unfreeze(struct super_block *sb) 1695ed0dab6bSYan { 16960176260fSLinus Torvalds return 0; 1697ed0dab6bSYan } 16982e635a27SChris Mason 16999c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 17009c5085c1SJosef Bacik { 17019c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 17029c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 17039c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 17049c5085c1SJosef Bacik struct list_head *head; 17059c5085c1SJosef Bacik struct rcu_string *name; 17069c5085c1SJosef Bacik 17079c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 17089c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 17099c5085c1SJosef Bacik while (cur_devices) { 17109c5085c1SJosef Bacik head = &cur_devices->devices; 17119c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 1712aa9ddcd4SJosef Bacik if (dev->missing) 1713aa9ddcd4SJosef Bacik continue; 17149c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 17159c5085c1SJosef Bacik first_dev = dev; 17169c5085c1SJosef Bacik } 17179c5085c1SJosef Bacik cur_devices = cur_devices->seed; 17189c5085c1SJosef Bacik } 17199c5085c1SJosef Bacik 17209c5085c1SJosef Bacik if (first_dev) { 17219c5085c1SJosef Bacik rcu_read_lock(); 17229c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 17239c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 17249c5085c1SJosef Bacik rcu_read_unlock(); 17259c5085c1SJosef Bacik } else { 17269c5085c1SJosef Bacik WARN_ON(1); 17279c5085c1SJosef Bacik } 17289c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 17299c5085c1SJosef Bacik return 0; 17309c5085c1SJosef Bacik } 17319c5085c1SJosef Bacik 1732b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 173376dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 1734bd555975SAl Viro .evict_inode = btrfs_evict_inode, 1735e20d96d6SChris Mason .put_super = btrfs_put_super, 1736d5719762SChris Mason .sync_fs = btrfs_sync_fs, 1737a9572a15SEric Paris .show_options = btrfs_show_options, 17389c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 17394730a4bcSChris Mason .write_inode = btrfs_write_inode, 17402c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 17412c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 17428fd17795SChris Mason .statfs = btrfs_statfs, 1743c146afadSYan Zheng .remount_fs = btrfs_remount, 17440176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 17450176260fSLinus Torvalds .unfreeze_fs = btrfs_unfreeze, 1746e20d96d6SChris Mason }; 1747a9218f6bSChris Mason 1748a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 1749a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 1750a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 1751a9218f6bSChris Mason .owner = THIS_MODULE, 17526038f373SArnd Bergmann .llseek = noop_llseek, 1753a9218f6bSChris Mason }; 1754a9218f6bSChris Mason 1755a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 1756578454ffSKay Sievers .minor = BTRFS_MINOR, 1757a9218f6bSChris Mason .name = "btrfs-control", 1758a9218f6bSChris Mason .fops = &btrfs_ctl_fops 1759a9218f6bSChris Mason }; 1760a9218f6bSChris Mason 1761578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 1762578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 1763578454ffSKay Sievers 1764a9218f6bSChris Mason static int btrfs_interface_init(void) 1765a9218f6bSChris Mason { 1766a9218f6bSChris Mason return misc_register(&btrfs_misc); 1767a9218f6bSChris Mason } 1768a9218f6bSChris Mason 1769b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 1770a9218f6bSChris Mason { 1771a9218f6bSChris Mason if (misc_deregister(&btrfs_misc) < 0) 177248940662SDaniel J Blueman printk(KERN_INFO "btrfs: misc_deregister failed for control device\n"); 1773a9218f6bSChris Mason } 1774a9218f6bSChris Mason 177585965600SDavid Sterba static void btrfs_print_info(void) 177685965600SDavid Sterba { 177785965600SDavid Sterba printk(KERN_INFO "Btrfs loaded" 177885965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 177985965600SDavid Sterba ", debug=on" 178085965600SDavid Sterba #endif 178179556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 178279556c3dSStefan Behrens ", assert=on" 178379556c3dSStefan Behrens #endif 178485965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 178585965600SDavid Sterba ", integrity-checker=on" 178685965600SDavid Sterba #endif 178785965600SDavid Sterba "\n"); 178885965600SDavid Sterba } 178985965600SDavid Sterba 1790dc11dd5dSJosef Bacik static int btrfs_run_sanity_tests(void) 1791dc11dd5dSJosef Bacik { 1792*06ea65a3SJosef Bacik int ret; 1793*06ea65a3SJosef Bacik 1794*06ea65a3SJosef Bacik ret = btrfs_test_free_space_cache(); 1795*06ea65a3SJosef Bacik if (ret) 1796*06ea65a3SJosef Bacik return ret; 1797*06ea65a3SJosef Bacik return btrfs_test_extent_buffer_operations(); 1798dc11dd5dSJosef Bacik } 1799dc11dd5dSJosef Bacik 18002e635a27SChris Mason static int __init init_btrfs_fs(void) 18012e635a27SChris Mason { 18022c90e5d6SChris Mason int err; 180358176a96SJosef Bacik 180458176a96SJosef Bacik err = btrfs_init_sysfs(); 180558176a96SJosef Bacik if (err) 180658176a96SJosef Bacik return err; 180758176a96SJosef Bacik 1808143bede5SJeff Mahoney btrfs_init_compress(); 1809d1310b2eSChris Mason 1810261507a0SLi Zefan err = btrfs_init_cachep(); 1811261507a0SLi Zefan if (err) 1812261507a0SLi Zefan goto free_compress; 1813261507a0SLi Zefan 1814d1310b2eSChris Mason err = extent_io_init(); 18152f4cbe64SWyatt Banks if (err) 18162f4cbe64SWyatt Banks goto free_cachep; 18172f4cbe64SWyatt Banks 1818d1310b2eSChris Mason err = extent_map_init(); 1819d1310b2eSChris Mason if (err) 1820d1310b2eSChris Mason goto free_extent_io; 1821d1310b2eSChris Mason 18226352b91dSMiao Xie err = ordered_data_init(); 18232f4cbe64SWyatt Banks if (err) 18242f4cbe64SWyatt Banks goto free_extent_map; 1825c8b97818SChris Mason 18266352b91dSMiao Xie err = btrfs_delayed_inode_init(); 18276352b91dSMiao Xie if (err) 18286352b91dSMiao Xie goto free_ordered_data; 18296352b91dSMiao Xie 18309247f317SMiao Xie err = btrfs_auto_defrag_init(); 183116cdcec7SMiao Xie if (err) 183216cdcec7SMiao Xie goto free_delayed_inode; 183316cdcec7SMiao Xie 183478a6184aSMiao Xie err = btrfs_delayed_ref_init(); 18359247f317SMiao Xie if (err) 18369247f317SMiao Xie goto free_auto_defrag; 18379247f317SMiao Xie 1838b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 1839b9e9a6cbSWang Shilong if (err) 1840b9e9a6cbSWang Shilong goto free_prelim_ref; 1841b9e9a6cbSWang Shilong 184278a6184aSMiao Xie err = btrfs_interface_init(); 184378a6184aSMiao Xie if (err) 184478a6184aSMiao Xie goto free_delayed_ref; 184578a6184aSMiao Xie 1846e565d4b9SJan Schmidt btrfs_init_lockdep(); 1847e565d4b9SJan Schmidt 184885965600SDavid Sterba btrfs_print_info(); 1849dc11dd5dSJosef Bacik 1850dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 1851dc11dd5dSJosef Bacik if (err) 1852dc11dd5dSJosef Bacik goto unregister_ioctl; 1853dc11dd5dSJosef Bacik 1854dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 1855dc11dd5dSJosef Bacik if (err) 1856dc11dd5dSJosef Bacik goto unregister_ioctl; 185774255aa0SJosef Bacik 18582f4cbe64SWyatt Banks return 0; 18592f4cbe64SWyatt Banks 1860a9218f6bSChris Mason unregister_ioctl: 1861a9218f6bSChris Mason btrfs_interface_exit(); 1862b9e9a6cbSWang Shilong free_prelim_ref: 1863b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 186478a6184aSMiao Xie free_delayed_ref: 186578a6184aSMiao Xie btrfs_delayed_ref_exit(); 18669247f317SMiao Xie free_auto_defrag: 18679247f317SMiao Xie btrfs_auto_defrag_exit(); 186816cdcec7SMiao Xie free_delayed_inode: 186916cdcec7SMiao Xie btrfs_delayed_inode_exit(); 18706352b91dSMiao Xie free_ordered_data: 18716352b91dSMiao Xie ordered_data_exit(); 18722f4cbe64SWyatt Banks free_extent_map: 18732f4cbe64SWyatt Banks extent_map_exit(); 1874d1310b2eSChris Mason free_extent_io: 1875d1310b2eSChris Mason extent_io_exit(); 18762f4cbe64SWyatt Banks free_cachep: 18772f4cbe64SWyatt Banks btrfs_destroy_cachep(); 1878261507a0SLi Zefan free_compress: 1879261507a0SLi Zefan btrfs_exit_compress(); 18802f4cbe64SWyatt Banks btrfs_exit_sysfs(); 18812c90e5d6SChris Mason return err; 18822e635a27SChris Mason } 18832e635a27SChris Mason 18842e635a27SChris Mason static void __exit exit_btrfs_fs(void) 18852e635a27SChris Mason { 188639279cc3SChris Mason btrfs_destroy_cachep(); 188778a6184aSMiao Xie btrfs_delayed_ref_exit(); 18889247f317SMiao Xie btrfs_auto_defrag_exit(); 188916cdcec7SMiao Xie btrfs_delayed_inode_exit(); 1890b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 18916352b91dSMiao Xie ordered_data_exit(); 1892a52d9a80SChris Mason extent_map_exit(); 1893d1310b2eSChris Mason extent_io_exit(); 1894a9218f6bSChris Mason btrfs_interface_exit(); 18952e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 189658176a96SJosef Bacik btrfs_exit_sysfs(); 18978a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 1898261507a0SLi Zefan btrfs_exit_compress(); 18992e635a27SChris Mason } 19002e635a27SChris Mason 19012e635a27SChris Mason module_init(init_btrfs_fs) 19022e635a27SChris Mason module_exit(exit_btrfs_fs) 19032e635a27SChris Mason 19042e635a27SChris Mason MODULE_LICENSE("GPL"); 1905