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> 4516cdcec7SMiao Xie #include "delayed-inode.h" 462e635a27SChris Mason #include "ctree.h" 47e20d96d6SChris Mason #include "disk-io.h" 48d5719762SChris Mason #include "transaction.h" 492c90e5d6SChris Mason #include "btrfs_inode.h" 503a686375SChris Mason #include "print-tree.h" 5114a958e6SFilipe David Borba Manana #include "hash.h" 5263541927SFilipe David Borba Manana #include "props.h" 535103e947SJosef Bacik #include "xattr.h" 548a4b83ccSChris Mason #include "volumes.h" 55be6e8dc0SBalaji Rao #include "export.h" 56c8b97818SChris Mason #include "compression.h" 579c5085c1SJosef Bacik #include "rcu-string.h" 588dabb742SStefan Behrens #include "dev-replace.h" 5974255aa0SJosef Bacik #include "free-space-cache.h" 60b9e9a6cbSWang Shilong #include "backref.h" 61dc11dd5dSJosef Bacik #include "tests/btrfs-tests.h" 622e635a27SChris Mason 63d3982100SMark Fasheh #include "qgroup.h" 641abe9b8aSliubo #define CREATE_TRACE_POINTS 651abe9b8aSliubo #include <trace/events/btrfs.h> 661abe9b8aSliubo 67b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops; 6872fa39f5SMisono, Tomohiro 6972fa39f5SMisono, Tomohiro /* 7072fa39f5SMisono, Tomohiro * Types for mounting the default subvolume and a subvolume explicitly 7172fa39f5SMisono, Tomohiro * requested by subvol=/path. That way the callchain is straightforward and we 7272fa39f5SMisono, Tomohiro * don't have to play tricks with the mount options and recursive calls to 7372fa39f5SMisono, Tomohiro * btrfs_mount. 74*312c89fbSMisono, Tomohiro * 75*312c89fbSMisono, Tomohiro * The new btrfs_root_fs_type also servers as a tag for the bdev_holder. 7672fa39f5SMisono, Tomohiro */ 77830c4adbSJosef Bacik static struct file_system_type btrfs_fs_type; 7872fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type; 79e20d96d6SChris Mason 800723a047SHarald Hoyer static int btrfs_remount(struct super_block *sb, int *flags, char *data); 810723a047SHarald Hoyer 82e33e17eeSJeff Mahoney const char *btrfs_decode_error(int errno) 83acce952bSliubo { 8408748810SDavid Sterba char *errstr = "unknown"; 85acce952bSliubo 86acce952bSliubo switch (errno) { 87acce952bSliubo case -EIO: 88acce952bSliubo errstr = "IO failure"; 89acce952bSliubo break; 90acce952bSliubo case -ENOMEM: 91acce952bSliubo errstr = "Out of memory"; 92acce952bSliubo break; 93acce952bSliubo case -EROFS: 94acce952bSliubo errstr = "Readonly filesystem"; 95acce952bSliubo break; 968c342930SJeff Mahoney case -EEXIST: 978c342930SJeff Mahoney errstr = "Object already exists"; 988c342930SJeff Mahoney break; 9994ef7280SDavid Sterba case -ENOSPC: 10094ef7280SDavid Sterba errstr = "No space left"; 10194ef7280SDavid Sterba break; 10294ef7280SDavid Sterba case -ENOENT: 10394ef7280SDavid Sterba errstr = "No such entry"; 10494ef7280SDavid Sterba break; 105acce952bSliubo } 106acce952bSliubo 107acce952bSliubo return errstr; 108acce952bSliubo } 109acce952bSliubo 110acce952bSliubo /* btrfs handle error by forcing the filesystem readonly */ 111acce952bSliubo static void btrfs_handle_error(struct btrfs_fs_info *fs_info) 112acce952bSliubo { 113acce952bSliubo struct super_block *sb = fs_info->sb; 114acce952bSliubo 115bc98a42cSDavid Howells if (sb_rdonly(sb)) 116acce952bSliubo return; 117acce952bSliubo 11887533c47SMiao Xie if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 1191751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 120c2cf52ebSSimon Kirby btrfs_info(fs_info, "forced readonly"); 1211acd6831SStefan Behrens /* 1221acd6831SStefan Behrens * Note that a running device replace operation is not 1231acd6831SStefan Behrens * canceled here although there is no way to update 1241acd6831SStefan Behrens * the progress. It would add the risk of a deadlock, 12501327610SNicholas D Steeves * therefore the canceling is omitted. The only penalty 1261acd6831SStefan Behrens * is that some I/O remains active until the procedure 1271acd6831SStefan Behrens * completes. The next time when the filesystem is 1281acd6831SStefan Behrens * mounted writeable again, the device replace 1291acd6831SStefan Behrens * operation continues. 1301acd6831SStefan Behrens */ 131acce952bSliubo } 132acce952bSliubo } 133acce952bSliubo 134acce952bSliubo /* 13534d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 136acce952bSliubo * invokes the approciate error response. 137acce952bSliubo */ 138c0d19e2bSDavid Sterba __cold 13934d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1404da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 141acce952bSliubo { 142acce952bSliubo struct super_block *sb = fs_info->sb; 14357d816a1SAnand Jain #ifdef CONFIG_PRINTK 144acce952bSliubo const char *errstr; 14557d816a1SAnand Jain #endif 146acce952bSliubo 147acce952bSliubo /* 148acce952bSliubo * Special case: if the error is EROFS, and we're already 1491751e8a6SLinus Torvalds * under SB_RDONLY, then it is safe here. 150acce952bSliubo */ 151bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 152acce952bSliubo return; 153acce952bSliubo 15457d816a1SAnand Jain #ifdef CONFIG_PRINTK 15508748810SDavid Sterba errstr = btrfs_decode_error(errno); 1564da35113SJeff Mahoney if (fmt) { 15737252a66SEric Sandeen struct va_format vaf; 15837252a66SEric Sandeen va_list args; 15937252a66SEric Sandeen 16037252a66SEric Sandeen va_start(args, fmt); 16137252a66SEric Sandeen vaf.fmt = fmt; 16237252a66SEric Sandeen vaf.va = &args; 1634da35113SJeff Mahoney 16462e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 16508748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 16637252a66SEric Sandeen va_end(args); 1674da35113SJeff Mahoney } else { 16862e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 16908748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1704da35113SJeff Mahoney } 17157d816a1SAnand Jain #endif 172acce952bSliubo 1730713d90cSAnand Jain /* 1740713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1750713d90cSAnand Jain * also send it down to the disk 1760713d90cSAnand Jain */ 1770713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1780713d90cSAnand Jain 1794da35113SJeff Mahoney /* Don't go through full error handling during mount */ 1801751e8a6SLinus Torvalds if (sb->s_flags & SB_BORN) 181acce952bSliubo btrfs_handle_error(fs_info); 182acce952bSliubo } 1834da35113SJeff Mahoney 18457d816a1SAnand Jain #ifdef CONFIG_PRINTK 185533574c6SJoe Perches static const char * const logtypes[] = { 1864da35113SJeff Mahoney "emergency", 1874da35113SJeff Mahoney "alert", 1884da35113SJeff Mahoney "critical", 1894da35113SJeff Mahoney "error", 1904da35113SJeff Mahoney "warning", 1914da35113SJeff Mahoney "notice", 1924da35113SJeff Mahoney "info", 1934da35113SJeff Mahoney "debug", 1944da35113SJeff Mahoney }; 1954da35113SJeff Mahoney 19635f4e5e6SNikolay Borisov 19735f4e5e6SNikolay Borisov /* 19835f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 19935f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 20035f4e5e6SNikolay Borisov */ 20135f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 20235f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 20335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 20435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 20535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 20635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 20735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 20835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 20935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 21035f4e5e6SNikolay Borisov }; 21135f4e5e6SNikolay Borisov 212c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2134da35113SJeff Mahoney { 21440f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 2154da35113SJeff Mahoney struct va_format vaf; 2164da35113SJeff Mahoney va_list args; 217533574c6SJoe Perches int kern_level; 21840f7828bSPetr Mladek const char *type = logtypes[4]; 21940f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 2204da35113SJeff Mahoney 2214da35113SJeff Mahoney va_start(args, fmt); 2224da35113SJeff Mahoney 223262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 224533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 225262c5e86SPetr Mladek 226262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 227533574c6SJoe Perches memcpy(lvl, fmt, size); 228533574c6SJoe Perches lvl[size] = '\0'; 229533574c6SJoe Perches type = logtypes[kern_level - '0']; 23035f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 231262c5e86SPetr Mladek } 232262c5e86SPetr Mladek fmt += size; 233262c5e86SPetr Mladek } 234262c5e86SPetr Mladek 2354da35113SJeff Mahoney vaf.fmt = fmt; 2364da35113SJeff Mahoney vaf.va = &args; 237533574c6SJoe Perches 23835f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 2393993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 2403993b112SColin Ian King fs_info ? fs_info->sb->s_id : "<unknown>", &vaf); 241533574c6SJoe Perches 242533574c6SJoe Perches va_end(args); 2434da35113SJeff Mahoney } 244533574c6SJoe Perches #endif 245533574c6SJoe Perches 2468c342930SJeff Mahoney /* 24749b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 24849b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24949b25e05SJeff Mahoney * one. 25049b25e05SJeff Mahoney * 25149b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 25249b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 25349b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 25449b25e05SJeff Mahoney * in the call path but will return -EIO. 25549b25e05SJeff Mahoney * 25649b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 25749b25e05SJeff Mahoney * btrfs_commit_transaction. 25849b25e05SJeff Mahoney */ 259c0d19e2bSDavid Sterba __cold 26049b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 26166642832SJeff Mahoney const char *function, 26249b25e05SJeff Mahoney unsigned int line, int errno) 26349b25e05SJeff Mahoney { 26466642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 26566642832SJeff Mahoney 26649b25e05SJeff Mahoney trans->aborted = errno; 26749b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 26849b25e05SJeff Mahoney * transaction may be able to continue. */ 26964c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 27069ce977aSMiao Xie const char *errstr; 27169ce977aSMiao Xie 27208748810SDavid Sterba errstr = btrfs_decode_error(errno); 27366642832SJeff Mahoney btrfs_warn(fs_info, 274c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 27569ce977aSMiao Xie function, line, errstr); 27649b25e05SJeff Mahoney return; 27749b25e05SJeff Mahoney } 27820c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 279501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 28066642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 28166642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 28266642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 28349b25e05SJeff Mahoney } 28449b25e05SJeff Mahoney /* 2858c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2868c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2878c342930SJeff Mahoney */ 288c0d19e2bSDavid Sterba __cold 2898c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2908c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2918c342930SJeff Mahoney { 2928c342930SJeff Mahoney char *s_id = "<unknown>"; 2938c342930SJeff Mahoney const char *errstr; 2948c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2958c342930SJeff Mahoney va_list args; 2968c342930SJeff Mahoney 2978c342930SJeff Mahoney if (fs_info) 2988c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2998c342930SJeff Mahoney 3008c342930SJeff Mahoney va_start(args, fmt); 3018c342930SJeff Mahoney vaf.va = &args; 3028c342930SJeff Mahoney 30308748810SDavid Sterba errstr = btrfs_decode_error(errno); 304d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 30508748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 30608748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 3078c342930SJeff Mahoney 308efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 309efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3108c342930SJeff Mahoney va_end(args); 3118c342930SJeff Mahoney /* Caller calls BUG() */ 3128c342930SJeff Mahoney } 313acce952bSliubo 314e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 315e20d96d6SChris Mason { 3166bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 317e20d96d6SChris Mason } 3182e635a27SChris Mason 31995e05289SChris Mason enum { 32073f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 321287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 322287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 323261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 324261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 32570f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 32670f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 32770f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 32870f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 32970f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 330f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 331e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 332a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3338dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 334fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 335d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 336d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 337d0bd4560SJosef Bacik #endif 338fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 339fb592373SJosef Bacik Opt_ref_verify, 340fb592373SJosef Bacik #endif 3419555c6c1SIlya Dryomov Opt_err, 34295e05289SChris Mason }; 34395e05289SChris Mason 3444d4ab6d6SDavid Sterba static const match_table_t tokens = { 345dfe25020SChris Mason {Opt_degraded, "degraded"}, 34695e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3471493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 34843e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 349b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 350d399167dSQu Wenruo {Opt_datasum, "datasum"}, 351be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 352a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 35321ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 354842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3556f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3568f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 3574543df7eSChris Mason {Opt_thread_pool, "thread_pool=%d"}, 358c8b97818SChris Mason {Opt_compress, "compress"}, 359261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 360a555f810SChris Mason {Opt_compress_force, "compress-force"}, 361261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 362e18e4809SChris Mason {Opt_ssd, "ssd"}, 363451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3643b30c22fSChris Mason {Opt_nossd, "nossd"}, 365bd0330adSQu Wenruo {Opt_acl, "acl"}, 36633268eafSJosef Bacik {Opt_noacl, "noacl"}, 3673a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 368a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 36996da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 370fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 371dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3722c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 37397e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 374e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 375e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3760af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 37770f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 37888c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3794260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 38091435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 38153036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 382e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3834cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 384fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3854b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3863818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3878965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3888dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3898dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3909555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 39121adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 39221adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 39321adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 394f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3958c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3968b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 397d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 398d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 399d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 400d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 401d0bd4560SJosef Bacik #endif 402fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 403fb592373SJosef Bacik {Opt_ref_verify, "ref_verify"}, 404fb592373SJosef Bacik #endif 40533268eafSJosef Bacik {Opt_err, NULL}, 40695e05289SChris Mason }; 40795e05289SChris Mason 408edf24abeSChristoph Hellwig /* 409edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 410edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 41149b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 412edf24abeSChristoph Hellwig */ 4132ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 41496da0919SQu Wenruo unsigned long new_flags) 41595e05289SChris Mason { 41695e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 41773bc1876SJosef Bacik char *p, *num, *orig = NULL; 41873bc1876SJosef Bacik u64 cache_gen; 4194543df7eSChris Mason int intarg; 420a7a3f7caSSage Weil int ret = 0; 421261507a0SLi Zefan char *compress_type; 422261507a0SLi Zefan bool compress_force = false; 423b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 424b7c47bbbSTsutomu Itoh bool saved_compress_force; 425b7c47bbbSTsutomu Itoh int no_compress = 0; 426b6cda9bcSChris Mason 4270b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4280b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 42970f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 43070f6d82eSOmar Sandoval else if (cache_gen) 43173bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 43273bc1876SJosef Bacik 43396da0919SQu Wenruo /* 43496da0919SQu Wenruo * Even the options are empty, we still need to do extra check 43596da0919SQu Wenruo * against new flags 43696da0919SQu Wenruo */ 43795e05289SChris Mason if (!options) 43896da0919SQu Wenruo goto check; 43995e05289SChris Mason 440be20aa9dSChris Mason /* 441be20aa9dSChris Mason * strsep changes the string, duplicate it because parse_options 442be20aa9dSChris Mason * gets called twice 443be20aa9dSChris Mason */ 4443ec83621SDavid Sterba options = kstrdup(options, GFP_KERNEL); 445be20aa9dSChris Mason if (!options) 446be20aa9dSChris Mason return -ENOMEM; 447be20aa9dSChris Mason 448da495eccSJosef Bacik orig = options; 449be20aa9dSChris Mason 45095e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 45195e05289SChris Mason int token; 45295e05289SChris Mason if (!*p) 45395e05289SChris Mason continue; 45495e05289SChris Mason 45595e05289SChris Mason token = match_token(p, tokens, args); 45695e05289SChris Mason switch (token) { 457dfe25020SChris Mason case Opt_degraded: 4580b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 459dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 460dfe25020SChris Mason break; 46195e05289SChris Mason case Opt_subvol: 46273f73415SJosef Bacik case Opt_subvolid: 463e15d0542SXin Zhong case Opt_subvolrootid: 46443e570b0SChristoph Hellwig case Opt_device: 465edf24abeSChristoph Hellwig /* 46643e570b0SChristoph Hellwig * These are parsed by btrfs_parse_early_options 467edf24abeSChristoph Hellwig * and can be happily ignored here. 468edf24abeSChristoph Hellwig */ 46995e05289SChris Mason break; 470b6cda9bcSChris Mason case Opt_nodatasum: 4713cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 47207802534SQu Wenruo "setting nodatasum"); 473be20aa9dSChris Mason break; 474d399167dSQu Wenruo case Opt_datasum: 4753cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4763cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4770b246afaSJeff Mahoney btrfs_info(info, 4785d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 479d399167dSQu Wenruo else 4800b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 48107802534SQu Wenruo } 482d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 483d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 484d399167dSQu Wenruo break; 485be20aa9dSChris Mason case Opt_nodatacow: 4863cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4873cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4883cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4890b246afaSJeff Mahoney btrfs_info(info, 490efe120a0SFrank Holton "setting nodatacow, compression disabled"); 491bedb2ccaSAndrei Popa } else { 4920b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 493bedb2ccaSAndrei Popa } 49407802534SQu Wenruo } 495bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 496bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 497be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 498be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 499b6cda9bcSChris Mason break; 500a258af7aSQu Wenruo case Opt_datacow: 5013cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 50207802534SQu Wenruo "setting datacow"); 503a258af7aSQu Wenruo break; 504a555f810SChris Mason case Opt_compress_force: 505261507a0SLi Zefan case Opt_compress_force_type: 506261507a0SLi Zefan compress_force = true; 5071c697d4aSEric Sandeen /* Fallthrough */ 508261507a0SLi Zefan case Opt_compress: 509261507a0SLi Zefan case Opt_compress_type: 5103cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 5113cdde224SJeff Mahoney COMPRESS) ? 512b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 513b7c47bbbSTsutomu Itoh saved_compress_force = 5143cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 515261507a0SLi Zefan if (token == Opt_compress || 516261507a0SLi Zefan token == Opt_compress_force || 517a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 518261507a0SLi Zefan compress_type = "zlib"; 519eae8d825SQu Wenruo 520261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 521eae8d825SQu Wenruo info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 522eae8d825SQu Wenruo /* 523eae8d825SQu Wenruo * args[0] contains uninitialized data since 524eae8d825SQu Wenruo * for these tokens we don't expect any 525eae8d825SQu Wenruo * parameter. 526eae8d825SQu Wenruo */ 527eae8d825SQu Wenruo if (token != Opt_compress && 528eae8d825SQu Wenruo token != Opt_compress_force) 529f51d2b59SDavid Sterba info->compress_level = 530f51d2b59SDavid Sterba btrfs_compress_str2level(args[0].from); 531063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 532bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 533bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 534b7c47bbbSTsutomu Itoh no_compress = 0; 535a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 536a6fa6faeSLi Zefan compress_type = "lzo"; 537a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 538063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 539bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 540bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5412b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 542b7c47bbbSTsutomu Itoh no_compress = 0; 5435c1aab1dSNick Terrell } else if (strcmp(args[0].from, "zstd") == 0) { 5445c1aab1dSNick Terrell compress_type = "zstd"; 5455c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 5465c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 5475c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 5485c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 5495c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 5505c1aab1dSNick Terrell no_compress = 0; 551063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 552063849eaSArnd Hannemann compress_type = "no"; 553063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 554063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 555063849eaSArnd Hannemann compress_force = false; 556b7c47bbbSTsutomu Itoh no_compress++; 557261507a0SLi Zefan } else { 558261507a0SLi Zefan ret = -EINVAL; 559261507a0SLi Zefan goto out; 560261507a0SLi Zefan } 561261507a0SLi Zefan 562261507a0SLi Zefan if (compress_force) { 563b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 564143f3636SDavid Sterba } else { 5654027e0f4SWang Shilong /* 5664027e0f4SWang Shilong * If we remount from compress-force=xxx to 5674027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5684027e0f4SWang Shilong * flag, otherwise, there is no way for users 5694027e0f4SWang Shilong * to disable forcible compression separately. 5704027e0f4SWang Shilong */ 5714027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 572a7e252afSMiao Xie } 5733cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 574b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 575b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5763cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 577b7c47bbbSTsutomu Itoh no_compress == 1)) { 578f51d2b59SDavid Sterba btrfs_info(info, "%s %s compression, level %d", 579b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 580f51d2b59SDavid Sterba compress_type, info->compress_level); 581b7c47bbbSTsutomu Itoh } 582b7c47bbbSTsutomu Itoh compress_force = false; 583a555f810SChris Mason break; 584e18e4809SChris Mason case Opt_ssd: 5853cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 586583b7231SHans van Kranenburg "enabling ssd optimizations"); 587951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 588e18e4809SChris Mason break; 589451d7585SChris Mason case Opt_ssd_spread: 590583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 591583b7231SHans van Kranenburg "enabling ssd optimizations"); 5923cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 593583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 594951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 595451d7585SChris Mason break; 5963b30c22fSChris Mason case Opt_nossd: 597583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 598583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 599583b7231SHans van Kranenburg "not using ssd optimizations"); 600583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 601583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 6023b30c22fSChris Mason break; 603842bef58SQu Wenruo case Opt_barrier: 6043cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 60507802534SQu Wenruo "turning on barriers"); 606842bef58SQu Wenruo break; 60721ad10cfSChris Mason case Opt_nobarrier: 6083cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 60907802534SQu Wenruo "turning off barriers"); 61021ad10cfSChris Mason break; 6114543df7eSChris Mason case Opt_thread_pool: 6122c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6132c334e87SWang Shilong if (ret) { 6142c334e87SWang Shilong goto out; 6152c334e87SWang Shilong } else if (intarg > 0) { 6164543df7eSChris Mason info->thread_pool_size = intarg; 6172c334e87SWang Shilong } else { 6182c334e87SWang Shilong ret = -EINVAL; 6192c334e87SWang Shilong goto out; 6202c334e87SWang Shilong } 6214543df7eSChris Mason break; 6226f568d35SChris Mason case Opt_max_inline: 623edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6246f568d35SChris Mason if (num) { 62591748467SAkinobu Mita info->max_inline = memparse(num, NULL); 6266f568d35SChris Mason kfree(num); 6276f568d35SChris Mason 62815ada040SChris Mason if (info->max_inline) { 629feb5f965SMitch Harder info->max_inline = min_t(u64, 63015ada040SChris Mason info->max_inline, 6310b246afaSJeff Mahoney info->sectorsize); 63215ada040SChris Mason } 6330b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 634c1c9ff7cSGeert Uytterhoeven info->max_inline); 6352c334e87SWang Shilong } else { 6362c334e87SWang Shilong ret = -ENOMEM; 6372c334e87SWang Shilong goto out; 6386f568d35SChris Mason } 6396f568d35SChris Mason break; 6408f662a76SChris Mason case Opt_alloc_start: 6410d0c71b3SDavid Sterba btrfs_info(info, 6420d0c71b3SDavid Sterba "option alloc_start is obsolete, ignored"); 6438f662a76SChris Mason break; 644bd0330adSQu Wenruo case Opt_acl: 64545ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6461751e8a6SLinus Torvalds info->sb->s_flags |= SB_POSIXACL; 647bd0330adSQu Wenruo break; 64845ff35d6SGuangliang Zhao #else 6490b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 65045ff35d6SGuangliang Zhao ret = -EINVAL; 65145ff35d6SGuangliang Zhao goto out; 65245ff35d6SGuangliang Zhao #endif 65333268eafSJosef Bacik case Opt_noacl: 6541751e8a6SLinus Torvalds info->sb->s_flags &= ~SB_POSIXACL; 65533268eafSJosef Bacik break; 6563a5e1404SSage Weil case Opt_notreelog: 6573cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 65807802534SQu Wenruo "disabling tree log"); 6593a5e1404SSage Weil break; 660a88998f2SQu Wenruo case Opt_treelog: 6613cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 66207802534SQu Wenruo "enabling tree log"); 663a88998f2SQu Wenruo break; 664fed8f166SQu Wenruo case Opt_norecovery: 66596da0919SQu Wenruo case Opt_nologreplay: 6663cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 66796da0919SQu Wenruo "disabling log replay at mount time"); 66896da0919SQu Wenruo break; 669dccae999SSage Weil case Opt_flushoncommit: 6703cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 67107802534SQu Wenruo "turning on flush-on-commit"); 672dccae999SSage Weil break; 6732c9ee856SQu Wenruo case Opt_noflushoncommit: 6743cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 67507802534SQu Wenruo "turning off flush-on-commit"); 6762c9ee856SQu Wenruo break; 67797e728d4SJosef Bacik case Opt_ratio: 6782c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6792c334e87SWang Shilong if (ret) { 6802c334e87SWang Shilong goto out; 6812c334e87SWang Shilong } else if (intarg >= 0) { 68297e728d4SJosef Bacik info->metadata_ratio = intarg; 6830b246afaSJeff Mahoney btrfs_info(info, "metadata ratio %d", 68497e728d4SJosef Bacik info->metadata_ratio); 6852c334e87SWang Shilong } else { 6862c334e87SWang Shilong ret = -EINVAL; 6872c334e87SWang Shilong goto out; 68897e728d4SJosef Bacik } 68997e728d4SJosef Bacik break; 690e244a0aeSChristoph Hellwig case Opt_discard: 6913cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 69207802534SQu Wenruo "turning on discard"); 693e244a0aeSChristoph Hellwig break; 694e07a2adeSQu Wenruo case Opt_nodiscard: 6953cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 69607802534SQu Wenruo "turning off discard"); 697e07a2adeSQu Wenruo break; 6980af3d00bSJosef Bacik case Opt_space_cache: 69970f6d82eSOmar Sandoval case Opt_space_cache_version: 70070f6d82eSOmar Sandoval if (token == Opt_space_cache || 70170f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 7020b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 70370f6d82eSOmar Sandoval FREE_SPACE_TREE); 7043cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 70507802534SQu Wenruo "enabling disk space caching"); 70670f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 7070b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 70870f6d82eSOmar Sandoval SPACE_CACHE); 7090b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 71070f6d82eSOmar Sandoval "enabling free space tree"); 71170f6d82eSOmar Sandoval } else { 71270f6d82eSOmar Sandoval ret = -EINVAL; 71370f6d82eSOmar Sandoval goto out; 71470f6d82eSOmar Sandoval } 7150de90876SJosef Bacik break; 716f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 717f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 718f420ee1eSStefan Behrens break; 71973bc1876SJosef Bacik case Opt_no_space_cache: 7203cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 7210b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 72207802534SQu Wenruo "disabling disk space caching"); 72370f6d82eSOmar Sandoval } 7243cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 7250b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 72670f6d82eSOmar Sandoval "disabling free space tree"); 72770f6d82eSOmar Sandoval } 72873bc1876SJosef Bacik break; 7294b9465cbSChris Mason case Opt_inode_cache: 7307e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 73107802534SQu Wenruo "enabling inode map caching"); 7323818aea2SQu Wenruo break; 7333818aea2SQu Wenruo case Opt_noinode_cache: 7347e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 73507802534SQu Wenruo "disabling inode map caching"); 7364b9465cbSChris Mason break; 73788c2ba3bSJosef Bacik case Opt_clear_cache: 7383cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 73907802534SQu Wenruo "force clearing of disk cache"); 7400af3d00bSJosef Bacik break; 7414260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7424260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7434260f7c7SSage Weil break; 74491435650SChris Mason case Opt_enospc_debug: 74591435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 74691435650SChris Mason break; 74753036293SQu Wenruo case Opt_noenospc_debug: 74853036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 74953036293SQu Wenruo break; 7504cb5300bSChris Mason case Opt_defrag: 7513cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 75207802534SQu Wenruo "enabling auto defrag"); 7534cb5300bSChris Mason break; 754fc0ca9afSQu Wenruo case Opt_nodefrag: 7553cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 75607802534SQu Wenruo "disabling auto defrag"); 757fc0ca9afSQu Wenruo break; 758af31f5e5SChris Mason case Opt_recovery: 7590b246afaSJeff Mahoney btrfs_warn(info, 7608dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7618dcddfa0SQu Wenruo case Opt_usebackuproot: 7620b246afaSJeff Mahoney btrfs_info(info, 7638dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7648dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 765af31f5e5SChris Mason break; 7669555c6c1SIlya Dryomov case Opt_skip_balance: 7679555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7689555c6c1SIlya Dryomov break; 76921adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 77021adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7710b246afaSJeff Mahoney btrfs_info(info, 772efe120a0SFrank Holton "enabling check integrity including extent data"); 77321adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 77421adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 77521adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 77621adbd5cSStefan Behrens break; 77721adbd5cSStefan Behrens case Opt_check_integrity: 7780b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 77921adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 78021adbd5cSStefan Behrens break; 78121adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7822c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7832c334e87SWang Shilong if (ret) { 7842c334e87SWang Shilong goto out; 7852c334e87SWang Shilong } else if (intarg >= 0) { 78621adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 7870b246afaSJeff Mahoney btrfs_info(info, 7885d163e0eSJeff Mahoney "check_integrity_print_mask 0x%x", 78921adbd5cSStefan Behrens info->check_integrity_print_mask); 7902c334e87SWang Shilong } else { 7912c334e87SWang Shilong ret = -EINVAL; 7922c334e87SWang Shilong goto out; 79321adbd5cSStefan Behrens } 79421adbd5cSStefan Behrens break; 79521adbd5cSStefan Behrens #else 79621adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 79721adbd5cSStefan Behrens case Opt_check_integrity: 79821adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7990b246afaSJeff Mahoney btrfs_err(info, 800efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 80121adbd5cSStefan Behrens ret = -EINVAL; 80221adbd5cSStefan Behrens goto out; 80321adbd5cSStefan Behrens #endif 8048c342930SJeff Mahoney case Opt_fatal_errors: 8058c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 8068c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 8078c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 8088c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 8098c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 8108c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 8118c342930SJeff Mahoney else { 8128c342930SJeff Mahoney ret = -EINVAL; 8138c342930SJeff Mahoney goto out; 8148c342930SJeff Mahoney } 8158c342930SJeff Mahoney break; 8168b87dc17SDavid Sterba case Opt_commit_interval: 8178b87dc17SDavid Sterba intarg = 0; 8188b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 8198b87dc17SDavid Sterba if (ret < 0) { 8200b246afaSJeff Mahoney btrfs_err(info, "invalid commit interval"); 8218b87dc17SDavid Sterba ret = -EINVAL; 8228b87dc17SDavid Sterba goto out; 8238b87dc17SDavid Sterba } 8248b87dc17SDavid Sterba if (intarg > 0) { 8258b87dc17SDavid Sterba if (intarg > 300) { 8260b246afaSJeff Mahoney btrfs_warn(info, 8275d163e0eSJeff Mahoney "excessive commit interval %d", 8288b87dc17SDavid Sterba intarg); 8298b87dc17SDavid Sterba } 8308b87dc17SDavid Sterba info->commit_interval = intarg; 8318b87dc17SDavid Sterba } else { 8320b246afaSJeff Mahoney btrfs_info(info, 8335d163e0eSJeff Mahoney "using default commit interval %ds", 8348b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 8358b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 8368b87dc17SDavid Sterba } 8378b87dc17SDavid Sterba break; 838d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 839d0bd4560SJosef Bacik case Opt_fragment_all: 8400b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 841d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 842d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 843d0bd4560SJosef Bacik break; 844d0bd4560SJosef Bacik case Opt_fragment_metadata: 8450b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 846d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 847d0bd4560SJosef Bacik FRAGMENT_METADATA); 848d0bd4560SJosef Bacik break; 849d0bd4560SJosef Bacik case Opt_fragment_data: 8500b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 851d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 852d0bd4560SJosef Bacik break; 853d0bd4560SJosef Bacik #endif 854fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 855fb592373SJosef Bacik case Opt_ref_verify: 856fb592373SJosef Bacik btrfs_info(info, "doing ref verification"); 857fb592373SJosef Bacik btrfs_set_opt(info->mount_opt, REF_VERIFY); 858fb592373SJosef Bacik break; 859fb592373SJosef Bacik #endif 860a7a3f7caSSage Weil case Opt_err: 8610b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 862a7a3f7caSSage Weil ret = -EINVAL; 863a7a3f7caSSage Weil goto out; 86495e05289SChris Mason default: 865be20aa9dSChris Mason break; 86695e05289SChris Mason } 86795e05289SChris Mason } 86896da0919SQu Wenruo check: 86996da0919SQu Wenruo /* 87096da0919SQu Wenruo * Extra check for current option against current flag 87196da0919SQu Wenruo */ 8721751e8a6SLinus Torvalds if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) { 8730b246afaSJeff Mahoney btrfs_err(info, 87496da0919SQu Wenruo "nologreplay must be used with ro mount option"); 87596da0919SQu Wenruo ret = -EINVAL; 87696da0919SQu Wenruo } 877a7a3f7caSSage Weil out: 8780b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8793cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8803cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8810b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 88270f6d82eSOmar Sandoval ret = -EINVAL; 88370f6d82eSOmar Sandoval 88470f6d82eSOmar Sandoval } 8853cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8860b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8873cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8880b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 889da495eccSJosef Bacik kfree(orig); 890a7a3f7caSSage Weil return ret; 891edf24abeSChristoph Hellwig } 892edf24abeSChristoph Hellwig 893edf24abeSChristoph Hellwig /* 894edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 895edf24abeSChristoph Hellwig * 896edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 897edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 898edf24abeSChristoph Hellwig */ 89997288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 90073f73415SJosef Bacik void *holder, char **subvol_name, u64 *subvol_objectid, 9015e2a4b25SDavid Sterba struct btrfs_fs_devices **fs_devices) 902edf24abeSChristoph Hellwig { 903edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 90483c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 9051493381fSWang Shilong char *num = NULL; 906edf24abeSChristoph Hellwig int error = 0; 907edf24abeSChristoph Hellwig 908edf24abeSChristoph Hellwig if (!options) 909830c4adbSJosef Bacik return 0; 910edf24abeSChristoph Hellwig 911edf24abeSChristoph Hellwig /* 912edf24abeSChristoph Hellwig * strsep changes the string, duplicate it because parse_options 913edf24abeSChristoph Hellwig * gets called twice 914edf24abeSChristoph Hellwig */ 915edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 916edf24abeSChristoph Hellwig if (!opts) 917edf24abeSChristoph Hellwig return -ENOMEM; 9183f3d0bc0STero Roponen orig = opts; 919edf24abeSChristoph Hellwig 920edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 921edf24abeSChristoph Hellwig int token; 922edf24abeSChristoph Hellwig if (!*p) 923edf24abeSChristoph Hellwig continue; 924edf24abeSChristoph Hellwig 925edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 926edf24abeSChristoph Hellwig switch (token) { 927edf24abeSChristoph Hellwig case Opt_subvol: 928a90e8b6fSIlya Dryomov kfree(*subvol_name); 929edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 9302c334e87SWang Shilong if (!*subvol_name) { 9312c334e87SWang Shilong error = -ENOMEM; 9322c334e87SWang Shilong goto out; 9332c334e87SWang Shilong } 934edf24abeSChristoph Hellwig break; 93573f73415SJosef Bacik case Opt_subvolid: 9361493381fSWang Shilong num = match_strdup(&args[0]); 9371493381fSWang Shilong if (num) { 9381493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 9391493381fSWang Shilong kfree(num); 9404849f01dSJosef Bacik /* we want the original fs_tree */ 9411493381fSWang Shilong if (!*subvol_objectid) 9424849f01dSJosef Bacik *subvol_objectid = 9434849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 9442c334e87SWang Shilong } else { 9452c334e87SWang Shilong error = -EINVAL; 9462c334e87SWang Shilong goto out; 9474849f01dSJosef Bacik } 94873f73415SJosef Bacik break; 949e15d0542SXin Zhong case Opt_subvolrootid: 95062e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 951e15d0542SXin Zhong break; 95243e570b0SChristoph Hellwig case Opt_device: 95383c8c9bdSJeff Liu device_name = match_strdup(&args[0]); 95483c8c9bdSJeff Liu if (!device_name) { 95583c8c9bdSJeff Liu error = -ENOMEM; 95683c8c9bdSJeff Liu goto out; 95783c8c9bdSJeff Liu } 95883c8c9bdSJeff Liu error = btrfs_scan_one_device(device_name, 95943e570b0SChristoph Hellwig flags, holder, fs_devices); 96083c8c9bdSJeff Liu kfree(device_name); 96143e570b0SChristoph Hellwig if (error) 962830c4adbSJosef Bacik goto out; 96343e570b0SChristoph Hellwig break; 964edf24abeSChristoph Hellwig default: 965edf24abeSChristoph Hellwig break; 966edf24abeSChristoph Hellwig } 967edf24abeSChristoph Hellwig } 968edf24abeSChristoph Hellwig 969edf24abeSChristoph Hellwig out: 970830c4adbSJosef Bacik kfree(orig); 971edf24abeSChristoph Hellwig return error; 97295e05289SChris Mason } 97395e05289SChris Mason 97405dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 97573f73415SJosef Bacik u64 subvol_objectid) 97673f73415SJosef Bacik { 977815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 97805dbe683SOmar Sandoval struct btrfs_root *fs_root; 97905dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 98005dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 98105dbe683SOmar Sandoval struct btrfs_key key; 98205dbe683SOmar Sandoval struct btrfs_path *path = NULL; 98305dbe683SOmar Sandoval char *name = NULL, *ptr; 98405dbe683SOmar Sandoval u64 dirid; 98505dbe683SOmar Sandoval int len; 98605dbe683SOmar Sandoval int ret; 98705dbe683SOmar Sandoval 98805dbe683SOmar Sandoval path = btrfs_alloc_path(); 98905dbe683SOmar Sandoval if (!path) { 99005dbe683SOmar Sandoval ret = -ENOMEM; 99105dbe683SOmar Sandoval goto err; 99205dbe683SOmar Sandoval } 99305dbe683SOmar Sandoval path->leave_spinning = 1; 99405dbe683SOmar Sandoval 9953ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 99605dbe683SOmar Sandoval if (!name) { 99705dbe683SOmar Sandoval ret = -ENOMEM; 99805dbe683SOmar Sandoval goto err; 99905dbe683SOmar Sandoval } 100005dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 100105dbe683SOmar Sandoval ptr[0] = '\0'; 100205dbe683SOmar Sandoval 100305dbe683SOmar Sandoval /* 100405dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 100505dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 100605dbe683SOmar Sandoval */ 100705dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 100805dbe683SOmar Sandoval key.objectid = subvol_objectid; 100905dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 101005dbe683SOmar Sandoval key.offset = (u64)-1; 101105dbe683SOmar Sandoval 101205dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 101305dbe683SOmar Sandoval if (ret < 0) { 101405dbe683SOmar Sandoval goto err; 101505dbe683SOmar Sandoval } else if (ret > 0) { 101605dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 101705dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 101805dbe683SOmar Sandoval if (ret < 0) { 101905dbe683SOmar Sandoval goto err; 102005dbe683SOmar Sandoval } else if (ret > 0) { 102105dbe683SOmar Sandoval ret = -ENOENT; 102205dbe683SOmar Sandoval goto err; 102305dbe683SOmar Sandoval } 102405dbe683SOmar Sandoval } 102505dbe683SOmar Sandoval 102605dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 102705dbe683SOmar Sandoval subvol_objectid = key.offset; 102805dbe683SOmar Sandoval 102905dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 103005dbe683SOmar Sandoval struct btrfs_root_ref); 103105dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 103205dbe683SOmar Sandoval ptr -= len + 1; 103305dbe683SOmar Sandoval if (ptr < name) { 103405dbe683SOmar Sandoval ret = -ENAMETOOLONG; 103505dbe683SOmar Sandoval goto err; 103605dbe683SOmar Sandoval } 103705dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 103805dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 103905dbe683SOmar Sandoval ptr[0] = '/'; 104005dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 104105dbe683SOmar Sandoval btrfs_release_path(path); 104205dbe683SOmar Sandoval 104305dbe683SOmar Sandoval key.objectid = subvol_objectid; 104405dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 104505dbe683SOmar Sandoval key.offset = (u64)-1; 104605dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 104705dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 104805dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 104905dbe683SOmar Sandoval goto err; 105005dbe683SOmar Sandoval } 105105dbe683SOmar Sandoval 105205dbe683SOmar Sandoval /* 105305dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 105405dbe683SOmar Sandoval * root directory. 105505dbe683SOmar Sandoval */ 105605dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 105705dbe683SOmar Sandoval key.objectid = dirid; 105805dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 105905dbe683SOmar Sandoval key.offset = (u64)-1; 106005dbe683SOmar Sandoval 106105dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 106205dbe683SOmar Sandoval if (ret < 0) { 106305dbe683SOmar Sandoval goto err; 106405dbe683SOmar Sandoval } else if (ret > 0) { 106505dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 106605dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 106705dbe683SOmar Sandoval if (ret < 0) { 106805dbe683SOmar Sandoval goto err; 106905dbe683SOmar Sandoval } else if (ret > 0) { 107005dbe683SOmar Sandoval ret = -ENOENT; 107105dbe683SOmar Sandoval goto err; 107205dbe683SOmar Sandoval } 107305dbe683SOmar Sandoval } 107405dbe683SOmar Sandoval 107505dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 107605dbe683SOmar Sandoval dirid = key.offset; 107705dbe683SOmar Sandoval 107805dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 107905dbe683SOmar Sandoval path->slots[0], 108005dbe683SOmar Sandoval struct btrfs_inode_ref); 108105dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 108205dbe683SOmar Sandoval inode_ref); 108305dbe683SOmar Sandoval ptr -= len + 1; 108405dbe683SOmar Sandoval if (ptr < name) { 108505dbe683SOmar Sandoval ret = -ENAMETOOLONG; 108605dbe683SOmar Sandoval goto err; 108705dbe683SOmar Sandoval } 108805dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 108905dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 109005dbe683SOmar Sandoval ptr[0] = '/'; 109105dbe683SOmar Sandoval btrfs_release_path(path); 109205dbe683SOmar Sandoval } 109305dbe683SOmar Sandoval } 109405dbe683SOmar Sandoval 109505dbe683SOmar Sandoval btrfs_free_path(path); 109605dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 109705dbe683SOmar Sandoval name[0] = '/'; 109805dbe683SOmar Sandoval name[1] = '\0'; 109905dbe683SOmar Sandoval } else { 110005dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 110105dbe683SOmar Sandoval } 110205dbe683SOmar Sandoval return name; 110305dbe683SOmar Sandoval 110405dbe683SOmar Sandoval err: 110505dbe683SOmar Sandoval btrfs_free_path(path); 110605dbe683SOmar Sandoval kfree(name); 110705dbe683SOmar Sandoval return ERR_PTR(ret); 110805dbe683SOmar Sandoval } 110905dbe683SOmar Sandoval 111005dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 111105dbe683SOmar Sandoval { 111205dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 111373f73415SJosef Bacik struct btrfs_dir_item *di; 111473f73415SJosef Bacik struct btrfs_path *path; 111573f73415SJosef Bacik struct btrfs_key location; 111673f73415SJosef Bacik u64 dir_id; 111773f73415SJosef Bacik 111873f73415SJosef Bacik path = btrfs_alloc_path(); 111973f73415SJosef Bacik if (!path) 112005dbe683SOmar Sandoval return -ENOMEM; 112173f73415SJosef Bacik path->leave_spinning = 1; 112273f73415SJosef Bacik 112373f73415SJosef Bacik /* 112473f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 112573f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 112673f73415SJosef Bacik * to mount. 112773f73415SJosef Bacik */ 1128815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 112973f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1130b0839166SJulia Lawall if (IS_ERR(di)) { 1131b0839166SJulia Lawall btrfs_free_path(path); 113205dbe683SOmar Sandoval return PTR_ERR(di); 1133b0839166SJulia Lawall } 113473f73415SJosef Bacik if (!di) { 113573f73415SJosef Bacik /* 113673f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 113773f73415SJosef Bacik * it's always been there, but don't freak out, just try and 113805dbe683SOmar Sandoval * mount the top-level subvolume. 113973f73415SJosef Bacik */ 114073f73415SJosef Bacik btrfs_free_path(path); 114105dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 114205dbe683SOmar Sandoval return 0; 114373f73415SJosef Bacik } 114473f73415SJosef Bacik 114573f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 114673f73415SJosef Bacik btrfs_free_path(path); 114705dbe683SOmar Sandoval *objectid = location.objectid; 114805dbe683SOmar Sandoval return 0; 114973f73415SJosef Bacik } 115073f73415SJosef Bacik 11518a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11528a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 115356e033a7SDavid Sterba void *data) 11542e635a27SChris Mason { 11552e635a27SChris Mason struct inode *inode; 1156815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11575d4f98a2SYan Zheng struct btrfs_key key; 115839279cc3SChris Mason int err; 11592e635a27SChris Mason 11602e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11612e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1162e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1163af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1164be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11655103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11662e635a27SChris Mason sb->s_time_gran = 1; 11670eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 11681751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 116949cf6f45SChris Ball #endif 1170357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1171da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 11729e11ceeeSJan Kara 11739e11ceeeSJan Kara err = super_setup_bdi(sb); 11749e11ceeeSJan Kara if (err) { 11759e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 11769e11ceeeSJan Kara return err; 11779e11ceeeSJan Kara } 11789e11ceeeSJan Kara 1179ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1180ad2b2c80SAl Viro if (err) { 1181ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1182ad2b2c80SAl Viro return err; 1183e20d96d6SChris Mason } 1184b888db2bSChris Mason 11855d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 11865d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 11875d4f98a2SYan Zheng key.offset = 0; 118898c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 11895d4f98a2SYan Zheng if (IS_ERR(inode)) { 11905d4f98a2SYan Zheng err = PTR_ERR(inode); 119139279cc3SChris Mason goto fail_close; 119239279cc3SChris Mason } 11932e635a27SChris Mason 119448fde701SAl Viro sb->s_root = d_make_root(inode); 119548fde701SAl Viro if (!sb->s_root) { 119639279cc3SChris Mason err = -ENOMEM; 119739279cc3SChris Mason goto fail_close; 11982e635a27SChris Mason } 119958176a96SJosef Bacik 120090a887c9SDan Magenheimer cleancache_init_fs(sb); 12011751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 12022e635a27SChris Mason return 0; 12032e635a27SChris Mason 120439279cc3SChris Mason fail_close: 12056bccf3abSJeff Mahoney close_ctree(fs_info); 1206d5719762SChris Mason return err; 1207d5719762SChris Mason } 1208d5719762SChris Mason 12096bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1210d5719762SChris Mason { 1211d5719762SChris Mason struct btrfs_trans_handle *trans; 1212815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1213815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1214df2ce34cSChris Mason 1215bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 12161abe9b8aSliubo 1217d561c025SChris Mason if (!wait) { 1218815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1219df2ce34cSChris Mason return 0; 1220d561c025SChris Mason } 1221771ed689SChris Mason 12226374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1223771ed689SChris Mason 1224d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 122560376ce4SJosef Bacik if (IS_ERR(trans)) { 1226354aa0fbSMiao Xie /* no transaction, don't bother */ 12276b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 12286b5fe46dSDavid Sterba /* 12296b5fe46dSDavid Sterba * Exit unless we have some pending changes 12306b5fe46dSDavid Sterba * that need to go through commit 12316b5fe46dSDavid Sterba */ 12326b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1233bd7de2c9SJosef Bacik return 0; 1234a53f4f8eSQu Wenruo /* 1235a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1236a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1237a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1238a53f4f8eSQu Wenruo * next commit after thawing. 1239a53f4f8eSQu Wenruo */ 1240a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1241a7e3c5f2SRakesh Pandit sb_end_write(sb); 1242a53f4f8eSQu Wenruo else 1243a53f4f8eSQu Wenruo return 0; 12446b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 124560376ce4SJosef Bacik } 124698bd5c54SDavid Sterba if (IS_ERR(trans)) 124798bd5c54SDavid Sterba return PTR_ERR(trans); 12486b5fe46dSDavid Sterba } 12493a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1250d5719762SChris Mason } 1251d5719762SChris Mason 125234c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1253a9572a15SEric Paris { 1254815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 12550f628c63SDavid Sterba const char *compress_type; 1256a9572a15SEric Paris 12573cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1258a9572a15SEric Paris seq_puts(seq, ",degraded"); 12593cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1260a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12613cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1262a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12633cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1264a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 126595ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1266c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1267a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1268a9572a15SEric Paris num_online_cpus() + 2, 8)) 1269a9572a15SEric Paris seq_printf(seq, ",thread_pool=%d", info->thread_pool_size); 12703cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 12710f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 12723cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1273200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1274200da64eSTsutomu Itoh else 1275200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1276f51d2b59SDavid Sterba if (info->compress_level) 1277fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1278200da64eSTsutomu Itoh } 12793cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1280c289811cSChris Mason seq_puts(seq, ",nossd"); 12813cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1282451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 12833cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1284a9572a15SEric Paris seq_puts(seq, ",ssd"); 12853cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 12866b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 12873cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 128896da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 12893cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 12906b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 12913cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 129220a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 12931751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1294a9572a15SEric Paris seq_puts(seq, ",noacl"); 12953cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1296200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 12973cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 129870f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 129973bc1876SJosef Bacik else 13008965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 13013cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1302f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 13033cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1304200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 13053cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1306200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 13073cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 13080942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 13093cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 13100942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 13113cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 13120942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 13133cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 13149555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 13158507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 13163cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 13178507d216SWang Shilong seq_puts(seq, ",check_int_data"); 13183cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 13198507d216SWang Shilong seq_puts(seq, ",check_int"); 13208507d216SWang Shilong if (info->check_integrity_print_mask) 13218507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 13228507d216SWang Shilong info->check_integrity_print_mask); 13238507d216SWang Shilong #endif 13248507d216SWang Shilong if (info->metadata_ratio) 13258507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 13268507d216SWang Shilong info->metadata_ratio); 13273cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 13288c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 13298b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 13308b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1331d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 13323cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1333d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 13343cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1335d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1336d0bd4560SJosef Bacik #endif 1337fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1338fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1339c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1340c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1341c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1342c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1343a9572a15SEric Paris return 0; 1344a9572a15SEric Paris } 1345a9572a15SEric Paris 1346a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13472e635a27SChris Mason { 1348815745cfSAl Viro struct btrfs_fs_info *p = data; 1349815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13504b82d6e4SYan 1351815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13524b82d6e4SYan } 13534b82d6e4SYan 1354450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1355450ba0eaSJosef Bacik { 13566de1d09dSAl Viro int err = set_anon_super(s, data); 13576de1d09dSAl Viro if (!err) 1358450ba0eaSJosef Bacik s->s_fs_info = data; 13596de1d09dSAl Viro return err; 1360450ba0eaSJosef Bacik } 1361450ba0eaSJosef Bacik 1362830c4adbSJosef Bacik /* 1363f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1364f9d9ef62SDavid Sterba */ 1365f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1366f9d9ef62SDavid Sterba { 1367f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1368f9d9ef62SDavid Sterba return 1; 1369f9d9ef62SDavid Sterba return 0; 1370f9d9ef62SDavid Sterba } 1371f9d9ef62SDavid Sterba 1372f9d9ef62SDavid Sterba /* 1373e6e4dbe8SOmar Sandoval * This will add subvolid=0 to the argument string while removing any subvol= 1374e6e4dbe8SOmar Sandoval * and subvolid= arguments to make sure we get the top-level root for path 1375e6e4dbe8SOmar Sandoval * walking to the subvol we want. 1376830c4adbSJosef Bacik */ 1377830c4adbSJosef Bacik static char *setup_root_args(char *args) 1378830c4adbSJosef Bacik { 1379e6e4dbe8SOmar Sandoval char *buf, *dst, *sep; 1380830c4adbSJosef Bacik 1381e6e4dbe8SOmar Sandoval if (!args) 13823ec83621SDavid Sterba return kstrdup("subvolid=0", GFP_KERNEL); 1383830c4adbSJosef Bacik 1384e6e4dbe8SOmar Sandoval /* The worst case is that we add ",subvolid=0" to the end. */ 13853ec83621SDavid Sterba buf = dst = kmalloc(strlen(args) + strlen(",subvolid=0") + 1, 13863ec83621SDavid Sterba GFP_KERNEL); 1387f60d16a8SJim Meyering if (!buf) 1388f60d16a8SJim Meyering return NULL; 1389830c4adbSJosef Bacik 1390e6e4dbe8SOmar Sandoval while (1) { 1391e6e4dbe8SOmar Sandoval sep = strchrnul(args, ','); 1392e6e4dbe8SOmar Sandoval if (!strstarts(args, "subvol=") && 1393e6e4dbe8SOmar Sandoval !strstarts(args, "subvolid=")) { 1394e6e4dbe8SOmar Sandoval memcpy(dst, args, sep - args); 1395e6e4dbe8SOmar Sandoval dst += sep - args; 1396e6e4dbe8SOmar Sandoval *dst++ = ','; 1397830c4adbSJosef Bacik } 1398e6e4dbe8SOmar Sandoval if (*sep) 1399e6e4dbe8SOmar Sandoval args = sep + 1; 1400e6e4dbe8SOmar Sandoval else 1401e6e4dbe8SOmar Sandoval break; 1402e6e4dbe8SOmar Sandoval } 1403f60d16a8SJim Meyering strcpy(dst, "subvolid=0"); 1404830c4adbSJosef Bacik 1405f60d16a8SJim Meyering return buf; 1406830c4adbSJosef Bacik } 1407830c4adbSJosef Bacik 1408bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 1409bb289b7bSOmar Sandoval int flags, const char *device_name, 1410*312c89fbSMisono, Tomohiro char *data, struct vfsmount *mnt) 1411830c4adbSJosef Bacik { 1412830c4adbSJosef Bacik struct dentry *root; 1413fa330659SOmar Sandoval int ret; 1414830c4adbSJosef Bacik 141505dbe683SOmar Sandoval if (!subvol_name) { 141605dbe683SOmar Sandoval if (!subvol_objectid) { 141705dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 141805dbe683SOmar Sandoval &subvol_objectid); 141905dbe683SOmar Sandoval if (ret) { 142005dbe683SOmar Sandoval root = ERR_PTR(ret); 142105dbe683SOmar Sandoval goto out; 142205dbe683SOmar Sandoval } 142305dbe683SOmar Sandoval } 142405dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 142505dbe683SOmar Sandoval subvol_objectid); 142605dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 142705dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 142805dbe683SOmar Sandoval subvol_name = NULL; 142905dbe683SOmar Sandoval goto out; 143005dbe683SOmar Sandoval } 143105dbe683SOmar Sandoval 143205dbe683SOmar Sandoval } 143305dbe683SOmar Sandoval 1434ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1435fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1436fa330659SOmar Sandoval mnt = NULL; 1437830c4adbSJosef Bacik 1438bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1439ea441d11SAl Viro struct super_block *s = root->d_sb; 1440ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1441bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1442bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1443bb289b7bSOmar Sandoval 1444bb289b7bSOmar Sandoval ret = 0; 1445bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1446ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1447bb289b7bSOmar Sandoval subvol_name); 1448bb289b7bSOmar Sandoval ret = -EINVAL; 1449bb289b7bSOmar Sandoval } 1450bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 145105dbe683SOmar Sandoval /* 145205dbe683SOmar Sandoval * This will also catch a race condition where a 145305dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 145405dbe683SOmar Sandoval * another subvolume is renamed over the old location. 145505dbe683SOmar Sandoval */ 1456ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1457ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1458bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1459bb289b7bSOmar Sandoval ret = -EINVAL; 1460bb289b7bSOmar Sandoval } 1461bb289b7bSOmar Sandoval if (ret) { 1462ea441d11SAl Viro dput(root); 1463bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1464ea441d11SAl Viro deactivate_locked_super(s); 1465bb289b7bSOmar Sandoval } 1466f9d9ef62SDavid Sterba } 1467f9d9ef62SDavid Sterba 1468fa330659SOmar Sandoval out: 1469fa330659SOmar Sandoval mntput(mnt); 1470fa330659SOmar Sandoval kfree(subvol_name); 1471830c4adbSJosef Bacik return root; 1472830c4adbSJosef Bacik } 1473450ba0eaSJosef Bacik 1474f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1475f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1476f667aef6SQu Wenruo { 1477f667aef6SQu Wenruo char *secdata = NULL; 1478f667aef6SQu Wenruo int ret = 0; 1479f667aef6SQu Wenruo 1480f667aef6SQu Wenruo secdata = alloc_secdata(); 1481f667aef6SQu Wenruo if (!secdata) 1482f667aef6SQu Wenruo return -ENOMEM; 1483f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1484f667aef6SQu Wenruo if (ret) { 1485f667aef6SQu Wenruo free_secdata(secdata); 1486f667aef6SQu Wenruo return ret; 1487f667aef6SQu Wenruo } 1488f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1489f667aef6SQu Wenruo free_secdata(secdata); 1490f667aef6SQu Wenruo return ret; 1491f667aef6SQu Wenruo } 1492f667aef6SQu Wenruo 1493f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1494f667aef6SQu Wenruo struct super_block *sb, 1495f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1496f667aef6SQu Wenruo { 1497f667aef6SQu Wenruo int ret = 0; 1498f667aef6SQu Wenruo 1499f667aef6SQu Wenruo /* 1500f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1501f667aef6SQu Wenruo * is valid. 1502f667aef6SQu Wenruo */ 1503f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1504f667aef6SQu Wenruo if (ret) 1505f667aef6SQu Wenruo return ret; 1506f667aef6SQu Wenruo 1507a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1508f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1509f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1510f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1511f667aef6SQu Wenruo } else { 1512f667aef6SQu Wenruo /* 1513180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1514180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1515180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1516180e4d47SLuis de Bethencourt * security options, just free it. 1517f667aef6SQu Wenruo */ 1518f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1519f667aef6SQu Wenruo } 1520a43bb39bSQu Wenruo #endif 1521f667aef6SQu Wenruo return ret; 1522f667aef6SQu Wenruo } 1523f667aef6SQu Wenruo 1524*312c89fbSMisono, Tomohiro /* 1525*312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1526*312c89fbSMisono, Tomohiro * 1527*312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1528*312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1529*312c89fbSMisono, Tomohiro */ 153072fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 153172fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 153272fa39f5SMisono, Tomohiro { 153372fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 153472fa39f5SMisono, Tomohiro struct super_block *s; 153572fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 153672fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 153772fa39f5SMisono, Tomohiro struct security_mnt_opts new_sec_opts; 153872fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 153972fa39f5SMisono, Tomohiro char *subvol_name = NULL; 154072fa39f5SMisono, Tomohiro u64 subvol_objectid = 0; 154172fa39f5SMisono, Tomohiro int error = 0; 154272fa39f5SMisono, Tomohiro 154372fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 154472fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 154572fa39f5SMisono, Tomohiro 154672fa39f5SMisono, Tomohiro error = btrfs_parse_early_options(data, mode, fs_type, 154772fa39f5SMisono, Tomohiro &subvol_name, &subvol_objectid, 154872fa39f5SMisono, Tomohiro &fs_devices); 154972fa39f5SMisono, Tomohiro if (error) { 155072fa39f5SMisono, Tomohiro kfree(subvol_name); 155172fa39f5SMisono, Tomohiro return ERR_PTR(error); 155272fa39f5SMisono, Tomohiro } 155372fa39f5SMisono, Tomohiro 155472fa39f5SMisono, Tomohiro security_init_mnt_opts(&new_sec_opts); 155572fa39f5SMisono, Tomohiro if (data) { 155672fa39f5SMisono, Tomohiro error = parse_security_options(data, &new_sec_opts); 155772fa39f5SMisono, Tomohiro if (error) 155872fa39f5SMisono, Tomohiro return ERR_PTR(error); 155972fa39f5SMisono, Tomohiro } 156072fa39f5SMisono, Tomohiro 156172fa39f5SMisono, Tomohiro error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 156272fa39f5SMisono, Tomohiro if (error) 156372fa39f5SMisono, Tomohiro goto error_sec_opts; 156472fa39f5SMisono, Tomohiro 156572fa39f5SMisono, Tomohiro /* 156672fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 156772fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 156872fa39f5SMisono, Tomohiro * it for searching for existing supers, so this lets us do that and 156972fa39f5SMisono, Tomohiro * then open_ctree will properly initialize everything later. 157072fa39f5SMisono, Tomohiro */ 157172fa39f5SMisono, Tomohiro fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 157272fa39f5SMisono, Tomohiro if (!fs_info) { 157372fa39f5SMisono, Tomohiro error = -ENOMEM; 157472fa39f5SMisono, Tomohiro goto error_sec_opts; 157572fa39f5SMisono, Tomohiro } 157672fa39f5SMisono, Tomohiro 157772fa39f5SMisono, Tomohiro fs_info->fs_devices = fs_devices; 157872fa39f5SMisono, Tomohiro 157972fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 158072fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 158172fa39f5SMisono, Tomohiro security_init_mnt_opts(&fs_info->security_opts); 158272fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 158372fa39f5SMisono, Tomohiro error = -ENOMEM; 158472fa39f5SMisono, Tomohiro goto error_fs_info; 158572fa39f5SMisono, Tomohiro } 158672fa39f5SMisono, Tomohiro 158772fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 158872fa39f5SMisono, Tomohiro if (error) 158972fa39f5SMisono, Tomohiro goto error_fs_info; 159072fa39f5SMisono, Tomohiro 159172fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 159272fa39f5SMisono, Tomohiro error = -EACCES; 159372fa39f5SMisono, Tomohiro goto error_close_devices; 159472fa39f5SMisono, Tomohiro } 159572fa39f5SMisono, Tomohiro 159672fa39f5SMisono, Tomohiro bdev = fs_devices->latest_bdev; 159772fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 159872fa39f5SMisono, Tomohiro fs_info); 159972fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 160072fa39f5SMisono, Tomohiro error = PTR_ERR(s); 160172fa39f5SMisono, Tomohiro goto error_close_devices; 160272fa39f5SMisono, Tomohiro } 160372fa39f5SMisono, Tomohiro 160472fa39f5SMisono, Tomohiro if (s->s_root) { 160572fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 160672fa39f5SMisono, Tomohiro free_fs_info(fs_info); 160772fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 160872fa39f5SMisono, Tomohiro error = -EBUSY; 160972fa39f5SMisono, Tomohiro } else { 161072fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 161172fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 161272fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 161372fa39f5SMisono, Tomohiro } 161472fa39f5SMisono, Tomohiro if (error) { 161572fa39f5SMisono, Tomohiro deactivate_locked_super(s); 161672fa39f5SMisono, Tomohiro goto error_sec_opts; 161772fa39f5SMisono, Tomohiro } 161872fa39f5SMisono, Tomohiro 161972fa39f5SMisono, Tomohiro fs_info = btrfs_sb(s); 162072fa39f5SMisono, Tomohiro error = setup_security_options(fs_info, s, &new_sec_opts); 162172fa39f5SMisono, Tomohiro if (error) { 162272fa39f5SMisono, Tomohiro deactivate_locked_super(s); 162372fa39f5SMisono, Tomohiro goto error_sec_opts; 162472fa39f5SMisono, Tomohiro } 162572fa39f5SMisono, Tomohiro 162672fa39f5SMisono, Tomohiro return dget(s->s_root); 162772fa39f5SMisono, Tomohiro 162872fa39f5SMisono, Tomohiro error_close_devices: 162972fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 163072fa39f5SMisono, Tomohiro error_fs_info: 163172fa39f5SMisono, Tomohiro free_fs_info(fs_info); 163272fa39f5SMisono, Tomohiro error_sec_opts: 163372fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 163472fa39f5SMisono, Tomohiro return ERR_PTR(error); 163572fa39f5SMisono, Tomohiro } 1636*312c89fbSMisono, Tomohiro 1637edf24abeSChristoph Hellwig /* 1638*312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1639edf24abeSChristoph Hellwig * 1640*312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1641*312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1642*312c89fbSMisono, Tomohiro * be mounted internally in any case. 1643*312c89fbSMisono, Tomohiro * 1644*312c89fbSMisono, Tomohiro * Operation flow: 1645*312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1646*312c89fbSMisono, Tomohiro * 1647*312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1648*312c89fbSMisono, Tomohiro * 1649*312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1650*312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1651*312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1652*312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1653*312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1654*312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1655*312c89fbSMisono, Tomohiro * 1656*312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1657*312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1658edf24abeSChristoph Hellwig */ 1659061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1660306e16ceSDavid Sterba const char *device_name, void *data) 16614b82d6e4SYan { 16628a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices = NULL; 1663*312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1664*312c89fbSMisono, Tomohiro struct dentry *root; 166597288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 166673f73415SJosef Bacik char *subvol_name = NULL; 166773f73415SJosef Bacik u64 subvol_objectid = 0; 16684b82d6e4SYan int error = 0; 16694b82d6e4SYan 16701751e8a6SLinus Torvalds if (!(flags & SB_RDONLY)) 167197288f2cSChristoph Hellwig mode |= FMODE_WRITE; 167297288f2cSChristoph Hellwig 167397288f2cSChristoph Hellwig error = btrfs_parse_early_options(data, mode, fs_type, 167473f73415SJosef Bacik &subvol_name, &subvol_objectid, 16755e2a4b25SDavid Sterba &fs_devices); 1676f23c8af8SIlya Dryomov if (error) { 1677f23c8af8SIlya Dryomov kfree(subvol_name); 1678061dbc6bSAl Viro return ERR_PTR(error); 1679f23c8af8SIlya Dryomov } 1680edf24abeSChristoph Hellwig 1681*312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1682*312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1683*312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1684*312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1685*312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1686*312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 16874b82d6e4SYan } else { 1688*312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1689*312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1690*312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1691*312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1692*312c89fbSMisono, Tomohiro goto out; 1693f667aef6SQu Wenruo } 1694f667aef6SQu Wenruo 1695*312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1696*312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1697*312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1698*312c89fbSMisono, Tomohiro if (error < 0) { 1699*312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1700*312c89fbSMisono, Tomohiro mntput(mnt_root); 1701*312c89fbSMisono, Tomohiro goto out; 1702*312c89fbSMisono, Tomohiro } 1703*312c89fbSMisono, Tomohiro } 1704*312c89fbSMisono, Tomohiro } 1705*312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1706*312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1707*312c89fbSMisono, Tomohiro goto out; 1708f667aef6SQu Wenruo } 17094b82d6e4SYan 1710*312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 1711*312c89fbSMisono, Tomohiro root = mount_subvol(subvol_name, subvol_objectid, flags, device_name, 1712*312c89fbSMisono, Tomohiro data, mnt_root); 17134b82d6e4SYan 1714*312c89fbSMisono, Tomohiro out: 1715*312c89fbSMisono, Tomohiro return root; 17164b82d6e4SYan } 17172e635a27SChris Mason 17180d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 17190d2450abSSergei Trofimovich int new_pool_size, int old_pool_size) 17200d2450abSSergei Trofimovich { 17210d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 17220d2450abSSergei Trofimovich return; 17230d2450abSSergei Trofimovich 17240d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 17250d2450abSSergei Trofimovich 1726efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 17270d2450abSSergei Trofimovich old_pool_size, new_pool_size); 17280d2450abSSergei Trofimovich 17295cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1730afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1731a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1732e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1733fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1734fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1735fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1736fccb5d86SQu Wenruo new_pool_size); 1737fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1738fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 17395b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1740736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 17410339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1742ff023aacSStefan Behrens new_pool_size); 17430d2450abSSergei Trofimovich } 17440d2450abSSergei Trofimovich 1745f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1746dc81cdc5SMiao Xie { 1747dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1748f42a34b2SMiao Xie } 1749dc81cdc5SMiao Xie 1750f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1751f42a34b2SMiao Xie unsigned long old_opts, int flags) 1752f42a34b2SMiao Xie { 1753dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1754dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 17551751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1756dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1757dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1758dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 17591751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1760dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1761dc81cdc5SMiao Xie } 1762dc81cdc5SMiao Xie } 1763dc81cdc5SMiao Xie 1764dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1765dc81cdc5SMiao Xie unsigned long old_opts) 1766dc81cdc5SMiao Xie { 1767dc81cdc5SMiao Xie /* 1768180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1769180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1770dc81cdc5SMiao Xie */ 1771dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1772bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1773dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1774dc81cdc5SMiao Xie } 1775dc81cdc5SMiao Xie 1776dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1777dc81cdc5SMiao Xie } 1778dc81cdc5SMiao Xie 1779c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1780c146afadSYan Zheng { 1781815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1782815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 178349b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 178449b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 178549b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 178649b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 178749b25e05SJeff Mahoney int old_thread_pool_size = fs_info->thread_pool_size; 178849b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1789c146afadSYan Zheng int ret; 1790c146afadSYan Zheng 179102b9984dSTheodore Ts'o sync_filesystem(sb); 1792f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1793dc81cdc5SMiao Xie 1794f667aef6SQu Wenruo if (data) { 1795f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1796f667aef6SQu Wenruo 1797f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1798f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1799f667aef6SQu Wenruo if (ret) 1800f667aef6SQu Wenruo goto restore; 1801f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1802f667aef6SQu Wenruo &new_sec_opts); 1803f667aef6SQu Wenruo if (ret) { 1804f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1805f667aef6SQu Wenruo goto restore; 1806f667aef6SQu Wenruo } 1807f667aef6SQu Wenruo } 1808f667aef6SQu Wenruo 18092ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 181049b25e05SJeff Mahoney if (ret) { 181149b25e05SJeff Mahoney ret = -EINVAL; 181249b25e05SJeff Mahoney goto restore; 181349b25e05SJeff Mahoney } 1814b288052eSChris Mason 1815f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 18160d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 18170d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 18180d2450abSSergei Trofimovich 18191751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1820dc81cdc5SMiao Xie goto out; 1821c146afadSYan Zheng 18221751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 18238dabb742SStefan Behrens /* 18248dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 18258dabb742SStefan Behrens * the filesystem is busy. 18268dabb742SStefan Behrens */ 182721c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1828361c093dSStefan Behrens 1829361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1830361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1831361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1832361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1833361c093dSStefan Behrens 18341751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 1835c146afadSYan Zheng 1836e44163e1SJeff Mahoney /* 18371751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1838e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1839e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1840e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1841e44163e1SJeff Mahoney * unless we clean them up here. 1842e44163e1SJeff Mahoney */ 1843e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1844e44163e1SJeff Mahoney 18458dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 18468dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1847061594efSMiao Xie btrfs_pause_balance(fs_info); 18488dabb742SStefan Behrens 18496bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 185049b25e05SJeff Mahoney if (ret) 185149b25e05SJeff Mahoney goto restore; 1852c146afadSYan Zheng } else { 18530b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 18546ef3de9cSDavid Sterba btrfs_err(fs_info, 1855efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 18566ef3de9cSDavid Sterba ret = -EINVAL; 18576ef3de9cSDavid Sterba goto restore; 18586ef3de9cSDavid Sterba } 18598a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 186049b25e05SJeff Mahoney ret = -EACCES; 186149b25e05SJeff Mahoney goto restore; 18628a3db184SSergei Trofimovich } 18632b82032cSYan Zheng 1864b382cfe8SQu Wenruo if (!btrfs_check_rw_degradable(fs_info)) { 1865efe120a0SFrank Holton btrfs_warn(fs_info, 1866efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1867292fd7fcSStefan Behrens ret = -EACCES; 1868292fd7fcSStefan Behrens goto restore; 1869292fd7fcSStefan Behrens } 1870292fd7fcSStefan Behrens 18718a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 187249b25e05SJeff Mahoney ret = -EINVAL; 187349b25e05SJeff Mahoney goto restore; 18748a3db184SSergei Trofimovich } 1875c146afadSYan Zheng 1876815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 187749b25e05SJeff Mahoney if (ret) 187849b25e05SJeff Mahoney goto restore; 1879c146afadSYan Zheng 1880d68fc57bSYan, Zheng /* recover relocation */ 18815f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1882d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18835f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 188449b25e05SJeff Mahoney if (ret) 188549b25e05SJeff Mahoney goto restore; 1886c146afadSYan Zheng 18872b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18882b6ba629SIlya Dryomov if (ret) 18892b6ba629SIlya Dryomov goto restore; 18902b6ba629SIlya Dryomov 18918dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18928dabb742SStefan Behrens if (ret) { 1893efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18948dabb742SStefan Behrens goto restore; 18958dabb742SStefan Behrens } 189694aebfb2SJosef Bacik 18976c6b5a39SAleksa Sarai btrfs_qgroup_rescan_resume(fs_info); 18986c6b5a39SAleksa Sarai 189994aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1900efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 190194aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 190294aebfb2SJosef Bacik if (ret) { 19035d163e0eSJeff Mahoney btrfs_warn(fs_info, 19045d163e0eSJeff Mahoney "failed to create the UUID tree %d", 19055d163e0eSJeff Mahoney ret); 190694aebfb2SJosef Bacik goto restore; 190794aebfb2SJosef Bacik } 190894aebfb2SJosef Bacik } 19091751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 191090c711abSZygo Blaxell 1911afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1912c146afadSYan Zheng } 1913dc81cdc5SMiao Xie out: 19142c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1915dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1916c146afadSYan Zheng return 0; 191749b25e05SJeff Mahoney 191849b25e05SJeff Mahoney restore: 19191751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 1920bc98a42cSDavid Howells if (sb_rdonly(sb)) 19211751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 192249b25e05SJeff Mahoney sb->s_flags = old_flags; 192349b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 192449b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 192549b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 19260d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 19270d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 192849b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1929dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 193049b25e05SJeff Mahoney return ret; 1931c146afadSYan Zheng } 1932c146afadSYan Zheng 1933bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1934bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1935bcd53741SArne Jansen const void *dev_info2) 1936bcd53741SArne Jansen { 1937bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1938bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1939bcd53741SArne Jansen return -1; 1940bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1941bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1942bcd53741SArne Jansen return 1; 1943bcd53741SArne Jansen else 1944bcd53741SArne Jansen return 0; 1945bcd53741SArne Jansen } 1946bcd53741SArne Jansen 1947bcd53741SArne Jansen /* 1948bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1949bcd53741SArne Jansen * is stored.(Descending Sort) 1950bcd53741SArne Jansen */ 1951bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1952bcd53741SArne Jansen struct btrfs_device_info *devices, 1953bcd53741SArne Jansen size_t nr_devices) 1954bcd53741SArne Jansen { 1955bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1956bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1957bcd53741SArne Jansen } 1958bcd53741SArne Jansen 19596d07bcecSMiao Xie /* 19606d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 19616d07bcecSMiao Xie * file data. 19626d07bcecSMiao Xie */ 19636bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 19646bccf3abSJeff Mahoney u64 *free_bytes) 19656d07bcecSMiao Xie { 19666d07bcecSMiao Xie struct btrfs_device_info *devices_info; 19676d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 19686d07bcecSMiao Xie struct btrfs_device *device; 19696d07bcecSMiao Xie u64 skip_space; 19706d07bcecSMiao Xie u64 type; 19716d07bcecSMiao Xie u64 avail_space; 19726d07bcecSMiao Xie u64 min_stripe_size; 197339fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19746d07bcecSMiao Xie int i = 0, nr_devices; 19756d07bcecSMiao Xie 19767e33fd99SJosef Bacik /* 197701327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19787e33fd99SJosef Bacik * enough for our purposes. 19797e33fd99SJosef Bacik */ 1980b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19817e33fd99SJosef Bacik if (!nr_devices) { 19827e33fd99SJosef Bacik smp_mb(); 19837e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19847e33fd99SJosef Bacik ASSERT(nr_devices); 19857e33fd99SJosef Bacik if (!nr_devices) { 19867e33fd99SJosef Bacik *free_bytes = 0; 19877e33fd99SJosef Bacik return 0; 19887e33fd99SJosef Bacik } 19897e33fd99SJosef Bacik } 19906d07bcecSMiao Xie 1991d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19926a44517dSDavid Sterba GFP_KERNEL); 19936d07bcecSMiao Xie if (!devices_info) 19946d07bcecSMiao Xie return -ENOMEM; 19956d07bcecSMiao Xie 199601327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19971b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 199839fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 19996d07bcecSMiao Xie min_stripes = 2; 200039fb26c3SMiao Xie num_stripes = nr_devices; 200139fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 20026d07bcecSMiao Xie min_stripes = 2; 200339fb26c3SMiao Xie num_stripes = 2; 200439fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 20056d07bcecSMiao Xie min_stripes = 4; 200639fb26c3SMiao Xie num_stripes = 4; 200739fb26c3SMiao Xie } 20086d07bcecSMiao Xie 20096d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 20106d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 20116d07bcecSMiao Xie else 20126d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 20136d07bcecSMiao Xie 20147e33fd99SJosef Bacik rcu_read_lock(); 20157e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 2016e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 2017e12c9621SAnand Jain &device->dev_state) || 2018401e29c1SAnand Jain !device->bdev || 2019401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 20206d07bcecSMiao Xie continue; 20216d07bcecSMiao Xie 20227e33fd99SJosef Bacik if (i >= nr_devices) 20237e33fd99SJosef Bacik break; 20247e33fd99SJosef Bacik 20256d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 20266d07bcecSMiao Xie 20276d07bcecSMiao Xie /* align with stripe_len */ 2028f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 20296d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 20306d07bcecSMiao Xie 20316d07bcecSMiao Xie /* 203201327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 20336d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 20346d07bcecSMiao Xie * allocation. 20356d07bcecSMiao Xie */ 2036ee22184bSByongho Lee skip_space = SZ_1M; 20376d07bcecSMiao Xie 20386d07bcecSMiao Xie /* 20396d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 20406d07bcecSMiao Xie * it from the total. 20416d07bcecSMiao Xie */ 20426d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 20436d07bcecSMiao Xie avail_space -= skip_space; 20446d07bcecSMiao Xie else 20456d07bcecSMiao Xie avail_space = 0; 20466d07bcecSMiao Xie 20476d07bcecSMiao Xie if (avail_space < min_stripe_size) 20486d07bcecSMiao Xie continue; 20496d07bcecSMiao Xie 20506d07bcecSMiao Xie devices_info[i].dev = device; 20516d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 20526d07bcecSMiao Xie 20536d07bcecSMiao Xie i++; 20546d07bcecSMiao Xie } 20557e33fd99SJosef Bacik rcu_read_unlock(); 20566d07bcecSMiao Xie 20576d07bcecSMiao Xie nr_devices = i; 20586d07bcecSMiao Xie 20596d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 20606d07bcecSMiao Xie 20616d07bcecSMiao Xie i = nr_devices - 1; 20626d07bcecSMiao Xie avail_space = 0; 20636d07bcecSMiao Xie while (nr_devices >= min_stripes) { 206439fb26c3SMiao Xie if (num_stripes > nr_devices) 206539fb26c3SMiao Xie num_stripes = nr_devices; 206639fb26c3SMiao Xie 20676d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 20686d07bcecSMiao Xie int j; 20696d07bcecSMiao Xie u64 alloc_size; 20706d07bcecSMiao Xie 207139fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20726d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 207339fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20746d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20756d07bcecSMiao Xie } 20766d07bcecSMiao Xie i--; 20776d07bcecSMiao Xie nr_devices--; 20786d07bcecSMiao Xie } 20796d07bcecSMiao Xie 20806d07bcecSMiao Xie kfree(devices_info); 20816d07bcecSMiao Xie *free_bytes = avail_space; 20826d07bcecSMiao Xie return 0; 20836d07bcecSMiao Xie } 20846d07bcecSMiao Xie 2085ba7b6e62SDavid Sterba /* 2086ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2087ba7b6e62SDavid Sterba * 2088ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2089ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2090ba7b6e62SDavid Sterba * 2091ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 20920d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 20930d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 20940d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2095ba7b6e62SDavid Sterba * 2096ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2097ba7b6e62SDavid Sterba */ 20988fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20998fd17795SChris Mason { 2100815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2101815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2102815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2103bd4d1088SJosef Bacik struct btrfs_space_info *found; 2104bd4d1088SJosef Bacik u64 total_used = 0; 21056d07bcecSMiao Xie u64 total_free_data = 0; 2106ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2107db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2108815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2109ba7b6e62SDavid Sterba unsigned factor = 1; 2110ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 21116d07bcecSMiao Xie int ret; 2112ca8a51b3SDavid Sterba u64 thresh = 0; 2113ae02d1bdSLuis de Bethencourt int mixed = 0; 21148fd17795SChris Mason 2115bd4d1088SJosef Bacik rcu_read_lock(); 211689a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 21176d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2118ba7b6e62SDavid Sterba int i; 2119ba7b6e62SDavid Sterba 21206d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 21216d07bcecSMiao Xie total_free_data -= 21226d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2123ba7b6e62SDavid Sterba 2124ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2125ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2126ba7b6e62SDavid Sterba switch (i) { 2127ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2128ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2129ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2130ba7b6e62SDavid Sterba factor = 2; 2131ba7b6e62SDavid Sterba } 2132ba7b6e62SDavid Sterba } 2133ba7b6e62SDavid Sterba } 21346d07bcecSMiao Xie } 2135ae02d1bdSLuis de Bethencourt 2136ae02d1bdSLuis de Bethencourt /* 2137ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2138ae02d1bdSLuis de Bethencourt */ 2139ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2140ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2141ae02d1bdSLuis de Bethencourt mixed = 1; 2142ae02d1bdSLuis de Bethencourt else 2143ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2144ae02d1bdSLuis de Bethencourt found->disk_used; 2145ae02d1bdSLuis de Bethencourt } 21466d07bcecSMiao Xie 2147b742bb82SYan, Zheng total_used += found->disk_used; 214889a55897SJosef Bacik } 2149ba7b6e62SDavid Sterba 2150bd4d1088SJosef Bacik rcu_read_unlock(); 2151bd4d1088SJosef Bacik 2152ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2153ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2154ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2155ba7b6e62SDavid Sterba 2156ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2157ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 215841b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 215941b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2160ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 216141b34accSLuis de Bethencourt else 216241b34accSLuis de Bethencourt buf->f_bfree = 0; 2163ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2164ba7b6e62SDavid Sterba 21650d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 21666bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 21677e33fd99SJosef Bacik if (ret) 21686d07bcecSMiao Xie return ret; 2169ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21706d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2171d397712bSChris Mason 2172ca8a51b3SDavid Sterba /* 2173ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2174ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2175ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2176ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2177ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2178ca8a51b3SDavid Sterba * 2179ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2180ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2181ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2182ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2183ca8a51b3SDavid Sterba * way around. 2184ca8a51b3SDavid Sterba */ 2185d4417e22SNikolay Borisov thresh = SZ_4M; 2186ca8a51b3SDavid Sterba 2187ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2188ca8a51b3SDavid Sterba buf->f_bavail = 0; 2189ca8a51b3SDavid Sterba 2190ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2191ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2192ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2193ba7b6e62SDavid Sterba 21949d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21959d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21969d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21979d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21989d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 219932d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 22002b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 22012b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 220232d48fa1SDavid Woodhouse 22038fd17795SChris Mason return 0; 22048fd17795SChris Mason } 2205b5133862SChris Mason 2206aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2207aea52e19SAl Viro { 2208815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2209aea52e19SAl Viro kill_anon_super(sb); 2210aea52e19SAl Viro free_fs_info(fs_info); 2211aea52e19SAl Viro } 2212aea52e19SAl Viro 22132e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 22142e635a27SChris Mason .owner = THIS_MODULE, 22152e635a27SChris Mason .name = "btrfs", 2216061dbc6bSAl Viro .mount = btrfs_mount, 2217aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2218f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 22192e635a27SChris Mason }; 222072fa39f5SMisono, Tomohiro 222172fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 222272fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 222372fa39f5SMisono, Tomohiro .name = "btrfs", 222472fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 222572fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 222672fa39f5SMisono, Tomohiro .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 222772fa39f5SMisono, Tomohiro }; 222872fa39f5SMisono, Tomohiro 22297f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2230a9218f6bSChris Mason 2231d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2232d8620958STom Van Braeckel { 2233d8620958STom Van Braeckel /* 2234d8620958STom Van Braeckel * The control file's private_data is used to hold the 2235d8620958STom Van Braeckel * transaction when it is started and is used to keep 2236d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2237d8620958STom Van Braeckel */ 2238d8620958STom Van Braeckel file->private_data = NULL; 2239d8620958STom Van Braeckel return 0; 2240d8620958STom Van Braeckel } 2241d8620958STom Van Braeckel 2242d352ac68SChris Mason /* 2243d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2244d352ac68SChris Mason */ 22458a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 22468a4b83ccSChris Mason unsigned long arg) 22478a4b83ccSChris Mason { 22488a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 22498a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2250c071fcfdSChris Mason int ret = -ENOTTY; 22518a4b83ccSChris Mason 2252e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2253e441d54dSChris Mason return -EPERM; 2254e441d54dSChris Mason 2255dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2256dae7b665SLi Zefan if (IS_ERR(vol)) 2257dae7b665SLi Zefan return PTR_ERR(vol); 2258c071fcfdSChris Mason 22598a4b83ccSChris Mason switch (cmd) { 22608a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 226197288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2262*312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 22638a4b83ccSChris Mason break; 226402db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 226502db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2266*312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 226702db0844SJosef Bacik if (ret) 226802db0844SJosef Bacik break; 226902db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 227002db0844SJosef Bacik break; 2271c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2272d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2273c5868f83SDavid Sterba break; 22748a4b83ccSChris Mason } 2275dae7b665SLi Zefan 22768a4b83ccSChris Mason kfree(vol); 2277f819d837SLinda Knippers return ret; 22788a4b83ccSChris Mason } 22798a4b83ccSChris Mason 22800176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2281ed0dab6bSYan { 2282354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22830b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22840b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2285354aa0fbSMiao Xie 2286fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22879e7cc91aSWang Xiaoguang /* 22889e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22899e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22909e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22919e7cc91aSWang Xiaoguang * ourselves. 22929e7cc91aSWang Xiaoguang */ 2293d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2294354aa0fbSMiao Xie if (IS_ERR(trans)) { 2295354aa0fbSMiao Xie /* no transaction, don't bother */ 2296354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22970176260fSLinus Torvalds return 0; 2298354aa0fbSMiao Xie return PTR_ERR(trans); 2299354aa0fbSMiao Xie } 23003a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2301ed0dab6bSYan } 2302ed0dab6bSYan 23039e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 23049e7cc91aSWang Xiaoguang { 2305fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2306fac03c8dSDavid Sterba 2307fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 23089e7cc91aSWang Xiaoguang return 0; 23099e7cc91aSWang Xiaoguang } 23109e7cc91aSWang Xiaoguang 23119c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 23129c5085c1SJosef Bacik { 23139c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 23149c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 23159c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 23169c5085c1SJosef Bacik struct list_head *head; 23179c5085c1SJosef Bacik struct rcu_string *name; 23189c5085c1SJosef Bacik 23199c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 23209c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 23219c5085c1SJosef Bacik while (cur_devices) { 23229c5085c1SJosef Bacik head = &cur_devices->devices; 23239c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2324e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 2325aa9ddcd4SJosef Bacik continue; 23260aeb8a6eSAnand Jain if (!dev->name) 23270aeb8a6eSAnand Jain continue; 23289c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 23299c5085c1SJosef Bacik first_dev = dev; 23309c5085c1SJosef Bacik } 23319c5085c1SJosef Bacik cur_devices = cur_devices->seed; 23329c5085c1SJosef Bacik } 23339c5085c1SJosef Bacik 23349c5085c1SJosef Bacik if (first_dev) { 23359c5085c1SJosef Bacik rcu_read_lock(); 23369c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 23379c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 23389c5085c1SJosef Bacik rcu_read_unlock(); 23399c5085c1SJosef Bacik } else { 23409c5085c1SJosef Bacik WARN_ON(1); 23419c5085c1SJosef Bacik } 23429c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 23439c5085c1SJosef Bacik return 0; 23449c5085c1SJosef Bacik } 23459c5085c1SJosef Bacik 2346b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 234776dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2348bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2349e20d96d6SChris Mason .put_super = btrfs_put_super, 2350d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2351a9572a15SEric Paris .show_options = btrfs_show_options, 23529c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 23534730a4bcSChris Mason .write_inode = btrfs_write_inode, 23542c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 23552c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 23568fd17795SChris Mason .statfs = btrfs_statfs, 2357c146afadSYan Zheng .remount_fs = btrfs_remount, 23580176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 23599e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2360e20d96d6SChris Mason }; 2361a9218f6bSChris Mason 2362a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2363d8620958STom Van Braeckel .open = btrfs_control_open, 2364a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2365a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2366a9218f6bSChris Mason .owner = THIS_MODULE, 23676038f373SArnd Bergmann .llseek = noop_llseek, 2368a9218f6bSChris Mason }; 2369a9218f6bSChris Mason 2370a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2371578454ffSKay Sievers .minor = BTRFS_MINOR, 2372a9218f6bSChris Mason .name = "btrfs-control", 2373a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2374a9218f6bSChris Mason }; 2375a9218f6bSChris Mason 2376578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2377578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2378578454ffSKay Sievers 2379f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2380a9218f6bSChris Mason { 2381a9218f6bSChris Mason return misc_register(&btrfs_misc); 2382a9218f6bSChris Mason } 2383a9218f6bSChris Mason 2384b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2385a9218f6bSChris Mason { 2386f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2387a9218f6bSChris Mason } 2388a9218f6bSChris Mason 2389f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 239085965600SDavid Sterba { 239162e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 239285965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 239385965600SDavid Sterba ", debug=on" 239485965600SDavid Sterba #endif 239579556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 239679556c3dSStefan Behrens ", assert=on" 239779556c3dSStefan Behrens #endif 239885965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 239985965600SDavid Sterba ", integrity-checker=on" 240085965600SDavid Sterba #endif 2401fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2402fb592373SJosef Bacik ", ref-verify=on" 2403fb592373SJosef Bacik #endif 24045f9e1059SJeff Mahoney "\n", 24055f9e1059SJeff Mahoney btrfs_crc32c_impl()); 240685965600SDavid Sterba } 240785965600SDavid Sterba 24082e635a27SChris Mason static int __init init_btrfs_fs(void) 24092e635a27SChris Mason { 24102c90e5d6SChris Mason int err; 241158176a96SJosef Bacik 241214a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 241314a958e6SFilipe David Borba Manana if (err) 241414a958e6SFilipe David Borba Manana return err; 241514a958e6SFilipe David Borba Manana 241663541927SFilipe David Borba Manana btrfs_props_init(); 241763541927SFilipe David Borba Manana 241858176a96SJosef Bacik err = btrfs_init_sysfs(); 241958176a96SJosef Bacik if (err) 242014a958e6SFilipe David Borba Manana goto free_hash; 242158176a96SJosef Bacik 2422143bede5SJeff Mahoney btrfs_init_compress(); 2423d1310b2eSChris Mason 2424261507a0SLi Zefan err = btrfs_init_cachep(); 2425261507a0SLi Zefan if (err) 2426261507a0SLi Zefan goto free_compress; 2427261507a0SLi Zefan 2428d1310b2eSChris Mason err = extent_io_init(); 24292f4cbe64SWyatt Banks if (err) 24302f4cbe64SWyatt Banks goto free_cachep; 24312f4cbe64SWyatt Banks 2432d1310b2eSChris Mason err = extent_map_init(); 2433d1310b2eSChris Mason if (err) 2434d1310b2eSChris Mason goto free_extent_io; 2435d1310b2eSChris Mason 24366352b91dSMiao Xie err = ordered_data_init(); 24372f4cbe64SWyatt Banks if (err) 24382f4cbe64SWyatt Banks goto free_extent_map; 2439c8b97818SChris Mason 24406352b91dSMiao Xie err = btrfs_delayed_inode_init(); 24416352b91dSMiao Xie if (err) 24426352b91dSMiao Xie goto free_ordered_data; 24436352b91dSMiao Xie 24449247f317SMiao Xie err = btrfs_auto_defrag_init(); 244516cdcec7SMiao Xie if (err) 244616cdcec7SMiao Xie goto free_delayed_inode; 244716cdcec7SMiao Xie 244878a6184aSMiao Xie err = btrfs_delayed_ref_init(); 24499247f317SMiao Xie if (err) 24509247f317SMiao Xie goto free_auto_defrag; 24519247f317SMiao Xie 2452b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2453b9e9a6cbSWang Shilong if (err) 2454af13b492SDavid Sterba goto free_delayed_ref; 2455b9e9a6cbSWang Shilong 245697eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 245778a6184aSMiao Xie if (err) 2458af13b492SDavid Sterba goto free_prelim_ref; 245978a6184aSMiao Xie 246097eb6b69SDavid Sterba err = btrfs_interface_init(); 246197eb6b69SDavid Sterba if (err) 246297eb6b69SDavid Sterba goto free_end_io_wq; 246397eb6b69SDavid Sterba 2464e565d4b9SJan Schmidt btrfs_init_lockdep(); 2465e565d4b9SJan Schmidt 24668ae1af3cSAnand Jain btrfs_print_mod_info(); 2467dc11dd5dSJosef Bacik 2468dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2469dc11dd5dSJosef Bacik if (err) 2470dc11dd5dSJosef Bacik goto unregister_ioctl; 2471dc11dd5dSJosef Bacik 2472dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2473dc11dd5dSJosef Bacik if (err) 2474dc11dd5dSJosef Bacik goto unregister_ioctl; 247574255aa0SJosef Bacik 24762f4cbe64SWyatt Banks return 0; 24772f4cbe64SWyatt Banks 2478a9218f6bSChris Mason unregister_ioctl: 2479a9218f6bSChris Mason btrfs_interface_exit(); 248097eb6b69SDavid Sterba free_end_io_wq: 248197eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2482b9e9a6cbSWang Shilong free_prelim_ref: 2483b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 248478a6184aSMiao Xie free_delayed_ref: 248578a6184aSMiao Xie btrfs_delayed_ref_exit(); 24869247f317SMiao Xie free_auto_defrag: 24879247f317SMiao Xie btrfs_auto_defrag_exit(); 248816cdcec7SMiao Xie free_delayed_inode: 248916cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24906352b91dSMiao Xie free_ordered_data: 24916352b91dSMiao Xie ordered_data_exit(); 24922f4cbe64SWyatt Banks free_extent_map: 24932f4cbe64SWyatt Banks extent_map_exit(); 2494d1310b2eSChris Mason free_extent_io: 2495d1310b2eSChris Mason extent_io_exit(); 24962f4cbe64SWyatt Banks free_cachep: 24972f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2498261507a0SLi Zefan free_compress: 2499261507a0SLi Zefan btrfs_exit_compress(); 25002f4cbe64SWyatt Banks btrfs_exit_sysfs(); 250114a958e6SFilipe David Borba Manana free_hash: 250214a958e6SFilipe David Borba Manana btrfs_hash_exit(); 25032c90e5d6SChris Mason return err; 25042e635a27SChris Mason } 25052e635a27SChris Mason 25062e635a27SChris Mason static void __exit exit_btrfs_fs(void) 25072e635a27SChris Mason { 250839279cc3SChris Mason btrfs_destroy_cachep(); 250978a6184aSMiao Xie btrfs_delayed_ref_exit(); 25109247f317SMiao Xie btrfs_auto_defrag_exit(); 251116cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2512b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 25136352b91dSMiao Xie ordered_data_exit(); 2514a52d9a80SChris Mason extent_map_exit(); 2515d1310b2eSChris Mason extent_io_exit(); 2516a9218f6bSChris Mason btrfs_interface_exit(); 25175ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 25182e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 251958176a96SJosef Bacik btrfs_exit_sysfs(); 25208a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2521261507a0SLi Zefan btrfs_exit_compress(); 252214a958e6SFilipe David Borba Manana btrfs_hash_exit(); 25232e635a27SChris Mason } 25242e635a27SChris Mason 252560efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 25262e635a27SChris Mason module_exit(exit_btrfs_fs) 25272e635a27SChris Mason 25282e635a27SChris Mason MODULE_LICENSE("GPL"); 2529