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. 74312c89fbSMisono, Tomohiro * 75312c89fbSMisono, 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 /* 11134d97007SAnand Jain * __btrfs_handle_fs_error decodes expected errors from the caller and 112acce952bSliubo * invokes the approciate error response. 113acce952bSliubo */ 114c0d19e2bSDavid Sterba __cold 11534d97007SAnand Jain void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function, 1164da35113SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 117acce952bSliubo { 118acce952bSliubo struct super_block *sb = fs_info->sb; 11957d816a1SAnand Jain #ifdef CONFIG_PRINTK 120acce952bSliubo const char *errstr; 12157d816a1SAnand Jain #endif 122acce952bSliubo 123acce952bSliubo /* 124acce952bSliubo * Special case: if the error is EROFS, and we're already 1251751e8a6SLinus Torvalds * under SB_RDONLY, then it is safe here. 126acce952bSliubo */ 127bc98a42cSDavid Howells if (errno == -EROFS && sb_rdonly(sb)) 128acce952bSliubo return; 129acce952bSliubo 13057d816a1SAnand Jain #ifdef CONFIG_PRINTK 13108748810SDavid Sterba errstr = btrfs_decode_error(errno); 1324da35113SJeff Mahoney if (fmt) { 13337252a66SEric Sandeen struct va_format vaf; 13437252a66SEric Sandeen va_list args; 13537252a66SEric Sandeen 13637252a66SEric Sandeen va_start(args, fmt); 13737252a66SEric Sandeen vaf.fmt = fmt; 13837252a66SEric Sandeen vaf.va = &args; 1394da35113SJeff Mahoney 14062e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s (%pV)\n", 14108748810SDavid Sterba sb->s_id, function, line, errno, errstr, &vaf); 14237252a66SEric Sandeen va_end(args); 1434da35113SJeff Mahoney } else { 14462e85577SJeff Mahoney pr_crit("BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 14508748810SDavid Sterba sb->s_id, function, line, errno, errstr); 1464da35113SJeff Mahoney } 14757d816a1SAnand Jain #endif 148acce952bSliubo 1490713d90cSAnand Jain /* 1500713d90cSAnand Jain * Today we only save the error info to memory. Long term we'll 1510713d90cSAnand Jain * also send it down to the disk 1520713d90cSAnand Jain */ 1530713d90cSAnand Jain set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state); 1540713d90cSAnand Jain 1554da35113SJeff Mahoney /* Don't go through full error handling during mount */ 156922ea899SAnand Jain if (!(sb->s_flags & SB_BORN)) 157922ea899SAnand Jain return; 158922ea899SAnand Jain 159922ea899SAnand Jain if (sb_rdonly(sb)) 160922ea899SAnand Jain return; 161922ea899SAnand Jain 162922ea899SAnand Jain /* btrfs handle error by forcing the filesystem readonly */ 163922ea899SAnand Jain sb->s_flags |= SB_RDONLY; 164922ea899SAnand Jain btrfs_info(fs_info, "forced readonly"); 165922ea899SAnand Jain /* 166922ea899SAnand Jain * Note that a running device replace operation is not canceled here 167922ea899SAnand Jain * although there is no way to update the progress. It would add the 168922ea899SAnand Jain * risk of a deadlock, therefore the canceling is omitted. The only 169922ea899SAnand Jain * penalty is that some I/O remains active until the procedure 170922ea899SAnand Jain * completes. The next time when the filesystem is mounted writeable 171922ea899SAnand Jain * again, the device replace operation continues. 172922ea899SAnand Jain */ 173acce952bSliubo } 1744da35113SJeff Mahoney 17557d816a1SAnand Jain #ifdef CONFIG_PRINTK 176533574c6SJoe Perches static const char * const logtypes[] = { 1774da35113SJeff Mahoney "emergency", 1784da35113SJeff Mahoney "alert", 1794da35113SJeff Mahoney "critical", 1804da35113SJeff Mahoney "error", 1814da35113SJeff Mahoney "warning", 1824da35113SJeff Mahoney "notice", 1834da35113SJeff Mahoney "info", 1844da35113SJeff Mahoney "debug", 1854da35113SJeff Mahoney }; 1864da35113SJeff Mahoney 18735f4e5e6SNikolay Borisov 18835f4e5e6SNikolay Borisov /* 18935f4e5e6SNikolay Borisov * Use one ratelimit state per log level so that a flood of less important 19035f4e5e6SNikolay Borisov * messages doesn't cause more important ones to be dropped. 19135f4e5e6SNikolay Borisov */ 19235f4e5e6SNikolay Borisov static struct ratelimit_state printk_limits[] = { 19335f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[0], DEFAULT_RATELIMIT_INTERVAL, 100), 19435f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[1], DEFAULT_RATELIMIT_INTERVAL, 100), 19535f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[2], DEFAULT_RATELIMIT_INTERVAL, 100), 19635f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[3], DEFAULT_RATELIMIT_INTERVAL, 100), 19735f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[4], DEFAULT_RATELIMIT_INTERVAL, 100), 19835f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[5], DEFAULT_RATELIMIT_INTERVAL, 100), 19935f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[6], DEFAULT_RATELIMIT_INTERVAL, 100), 20035f4e5e6SNikolay Borisov RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100), 20135f4e5e6SNikolay Borisov }; 20235f4e5e6SNikolay Borisov 203c2cf52ebSSimon Kirby void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...) 2044da35113SJeff Mahoney { 20540f7828bSPetr Mladek char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0"; 2064da35113SJeff Mahoney struct va_format vaf; 2074da35113SJeff Mahoney va_list args; 208533574c6SJoe Perches int kern_level; 20940f7828bSPetr Mladek const char *type = logtypes[4]; 21040f7828bSPetr Mladek struct ratelimit_state *ratelimit = &printk_limits[4]; 2114da35113SJeff Mahoney 2124da35113SJeff Mahoney va_start(args, fmt); 2134da35113SJeff Mahoney 214262c5e86SPetr Mladek while ((kern_level = printk_get_level(fmt)) != 0) { 215533574c6SJoe Perches size_t size = printk_skip_level(fmt) - fmt; 216262c5e86SPetr Mladek 217262c5e86SPetr Mladek if (kern_level >= '0' && kern_level <= '7') { 218533574c6SJoe Perches memcpy(lvl, fmt, size); 219533574c6SJoe Perches lvl[size] = '\0'; 220533574c6SJoe Perches type = logtypes[kern_level - '0']; 22135f4e5e6SNikolay Borisov ratelimit = &printk_limits[kern_level - '0']; 222262c5e86SPetr Mladek } 223262c5e86SPetr Mladek fmt += size; 224262c5e86SPetr Mladek } 225262c5e86SPetr Mladek 2264da35113SJeff Mahoney vaf.fmt = fmt; 2274da35113SJeff Mahoney vaf.va = &args; 228533574c6SJoe Perches 22935f4e5e6SNikolay Borisov if (__ratelimit(ratelimit)) 2303993b112SColin Ian King printk("%sBTRFS %s (device %s): %pV\n", lvl, type, 2313993b112SColin Ian King fs_info ? fs_info->sb->s_id : "<unknown>", &vaf); 232533574c6SJoe Perches 233533574c6SJoe Perches va_end(args); 2344da35113SJeff Mahoney } 235533574c6SJoe Perches #endif 236533574c6SJoe Perches 2378c342930SJeff Mahoney /* 23849b25e05SJeff Mahoney * We only mark the transaction aborted and then set the file system read-only. 23949b25e05SJeff Mahoney * This will prevent new transactions from starting or trying to join this 24049b25e05SJeff Mahoney * one. 24149b25e05SJeff Mahoney * 24249b25e05SJeff Mahoney * This means that error recovery at the call site is limited to freeing 24349b25e05SJeff Mahoney * any local memory allocations and passing the error code up without 24449b25e05SJeff Mahoney * further cleanup. The transaction should complete as it normally would 24549b25e05SJeff Mahoney * in the call path but will return -EIO. 24649b25e05SJeff Mahoney * 24749b25e05SJeff Mahoney * We'll complete the cleanup in btrfs_end_transaction and 24849b25e05SJeff Mahoney * btrfs_commit_transaction. 24949b25e05SJeff Mahoney */ 250c0d19e2bSDavid Sterba __cold 25149b25e05SJeff Mahoney void __btrfs_abort_transaction(struct btrfs_trans_handle *trans, 25266642832SJeff Mahoney const char *function, 25349b25e05SJeff Mahoney unsigned int line, int errno) 25449b25e05SJeff Mahoney { 25566642832SJeff Mahoney struct btrfs_fs_info *fs_info = trans->fs_info; 25666642832SJeff Mahoney 25749b25e05SJeff Mahoney trans->aborted = errno; 25849b25e05SJeff Mahoney /* Nothing used. The other threads that have joined this 25949b25e05SJeff Mahoney * transaction may be able to continue. */ 26064c12921SJeff Mahoney if (!trans->dirty && list_empty(&trans->new_bgs)) { 26169ce977aSMiao Xie const char *errstr; 26269ce977aSMiao Xie 26308748810SDavid Sterba errstr = btrfs_decode_error(errno); 26466642832SJeff Mahoney btrfs_warn(fs_info, 265c2cf52ebSSimon Kirby "%s:%d: Aborting unused transaction(%s).", 26669ce977aSMiao Xie function, line, errstr); 26749b25e05SJeff Mahoney return; 26849b25e05SJeff Mahoney } 26920c7bcecSSeraphime Kirkovski WRITE_ONCE(trans->transaction->aborted, errno); 270501407aaSJosef Bacik /* Wake up anybody who may be waiting on this transaction */ 27166642832SJeff Mahoney wake_up(&fs_info->transaction_wait); 27266642832SJeff Mahoney wake_up(&fs_info->transaction_blocked_wait); 27366642832SJeff Mahoney __btrfs_handle_fs_error(fs_info, function, line, errno, NULL); 27449b25e05SJeff Mahoney } 27549b25e05SJeff Mahoney /* 2768c342930SJeff Mahoney * __btrfs_panic decodes unexpected, fatal errors from the caller, 2778c342930SJeff Mahoney * issues an alert, and either panics or BUGs, depending on mount options. 2788c342930SJeff Mahoney */ 279c0d19e2bSDavid Sterba __cold 2808c342930SJeff Mahoney void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function, 2818c342930SJeff Mahoney unsigned int line, int errno, const char *fmt, ...) 2828c342930SJeff Mahoney { 2838c342930SJeff Mahoney char *s_id = "<unknown>"; 2848c342930SJeff Mahoney const char *errstr; 2858c342930SJeff Mahoney struct va_format vaf = { .fmt = fmt }; 2868c342930SJeff Mahoney va_list args; 2878c342930SJeff Mahoney 2888c342930SJeff Mahoney if (fs_info) 2898c342930SJeff Mahoney s_id = fs_info->sb->s_id; 2908c342930SJeff Mahoney 2918c342930SJeff Mahoney va_start(args, fmt); 2928c342930SJeff Mahoney vaf.va = &args; 2938c342930SJeff Mahoney 29408748810SDavid Sterba errstr = btrfs_decode_error(errno); 295d8953d69SSatoru Takeuchi if (fs_info && (btrfs_test_opt(fs_info, PANIC_ON_FATAL_ERROR))) 29608748810SDavid Sterba panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n", 29708748810SDavid Sterba s_id, function, line, &vaf, errno, errstr); 2988c342930SJeff Mahoney 299efe120a0SFrank Holton btrfs_crit(fs_info, "panic in %s:%d: %pV (errno=%d %s)", 300efe120a0SFrank Holton function, line, &vaf, errno, errstr); 3018c342930SJeff Mahoney va_end(args); 3028c342930SJeff Mahoney /* Caller calls BUG() */ 3038c342930SJeff Mahoney } 304acce952bSliubo 305e20d96d6SChris Mason static void btrfs_put_super(struct super_block *sb) 306e20d96d6SChris Mason { 3076bccf3abSJeff Mahoney close_ctree(btrfs_sb(sb)); 308e20d96d6SChris Mason } 3092e635a27SChris Mason 31095e05289SChris Mason enum { 31173f73415SJosef Bacik Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 312287a0ab9SJosef Bacik Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd, 313287a0ab9SJosef Bacik Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress, 314261507a0SLi Zefan Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 315261507a0SLi Zefan Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 31670f6d82eSOmar Sandoval Opt_space_cache, Opt_space_cache_version, Opt_clear_cache, 31770f6d82eSOmar Sandoval Opt_user_subvol_rm_allowed, Opt_enospc_debug, Opt_subvolrootid, 31870f6d82eSOmar Sandoval Opt_defrag, Opt_inode_cache, Opt_no_space_cache, Opt_recovery, 31970f6d82eSOmar Sandoval Opt_skip_balance, Opt_check_integrity, 32070f6d82eSOmar Sandoval Opt_check_integrity_including_extent_data, 321f420ee1eSStefan Behrens Opt_check_integrity_print_mask, Opt_fatal_errors, Opt_rescan_uuid_tree, 322e07a2adeSQu Wenruo Opt_commit_interval, Opt_barrier, Opt_nodefrag, Opt_nodiscard, 323a258af7aSQu Wenruo Opt_noenospc_debug, Opt_noflushoncommit, Opt_acl, Opt_datacow, 3248dcddfa0SQu Wenruo Opt_datasum, Opt_treelog, Opt_noinode_cache, Opt_usebackuproot, 325fed8f166SQu Wenruo Opt_nologreplay, Opt_norecovery, 326d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 327d0bd4560SJosef Bacik Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all, 328d0bd4560SJosef Bacik #endif 329fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 330fb592373SJosef Bacik Opt_ref_verify, 331fb592373SJosef Bacik #endif 3329555c6c1SIlya Dryomov Opt_err, 33395e05289SChris Mason }; 33495e05289SChris Mason 3354d4ab6d6SDavid Sterba static const match_table_t tokens = { 336dfe25020SChris Mason {Opt_degraded, "degraded"}, 33795e05289SChris Mason {Opt_subvol, "subvol=%s"}, 3381493381fSWang Shilong {Opt_subvolid, "subvolid=%s"}, 33943e570b0SChristoph Hellwig {Opt_device, "device=%s"}, 340b6cda9bcSChris Mason {Opt_nodatasum, "nodatasum"}, 341d399167dSQu Wenruo {Opt_datasum, "datasum"}, 342be20aa9dSChris Mason {Opt_nodatacow, "nodatacow"}, 343a258af7aSQu Wenruo {Opt_datacow, "datacow"}, 34421ad10cfSChris Mason {Opt_nobarrier, "nobarrier"}, 345842bef58SQu Wenruo {Opt_barrier, "barrier"}, 3466f568d35SChris Mason {Opt_max_inline, "max_inline=%s"}, 3478f662a76SChris Mason {Opt_alloc_start, "alloc_start=%s"}, 348*f7b885beSAnand Jain {Opt_thread_pool, "thread_pool=%u"}, 349c8b97818SChris Mason {Opt_compress, "compress"}, 350261507a0SLi Zefan {Opt_compress_type, "compress=%s"}, 351a555f810SChris Mason {Opt_compress_force, "compress-force"}, 352261507a0SLi Zefan {Opt_compress_force_type, "compress-force=%s"}, 353e18e4809SChris Mason {Opt_ssd, "ssd"}, 354451d7585SChris Mason {Opt_ssd_spread, "ssd_spread"}, 3553b30c22fSChris Mason {Opt_nossd, "nossd"}, 356bd0330adSQu Wenruo {Opt_acl, "acl"}, 35733268eafSJosef Bacik {Opt_noacl, "noacl"}, 3583a5e1404SSage Weil {Opt_notreelog, "notreelog"}, 359a88998f2SQu Wenruo {Opt_treelog, "treelog"}, 36096da0919SQu Wenruo {Opt_nologreplay, "nologreplay"}, 361fed8f166SQu Wenruo {Opt_norecovery, "norecovery"}, 362dccae999SSage Weil {Opt_flushoncommit, "flushoncommit"}, 3632c9ee856SQu Wenruo {Opt_noflushoncommit, "noflushoncommit"}, 36497e728d4SJosef Bacik {Opt_ratio, "metadata_ratio=%d"}, 365e244a0aeSChristoph Hellwig {Opt_discard, "discard"}, 366e07a2adeSQu Wenruo {Opt_nodiscard, "nodiscard"}, 3670af3d00bSJosef Bacik {Opt_space_cache, "space_cache"}, 36870f6d82eSOmar Sandoval {Opt_space_cache_version, "space_cache=%s"}, 36988c2ba3bSJosef Bacik {Opt_clear_cache, "clear_cache"}, 3704260f7c7SSage Weil {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"}, 37191435650SChris Mason {Opt_enospc_debug, "enospc_debug"}, 37253036293SQu Wenruo {Opt_noenospc_debug, "noenospc_debug"}, 373e15d0542SXin Zhong {Opt_subvolrootid, "subvolrootid=%d"}, 3744cb5300bSChris Mason {Opt_defrag, "autodefrag"}, 375fc0ca9afSQu Wenruo {Opt_nodefrag, "noautodefrag"}, 3764b9465cbSChris Mason {Opt_inode_cache, "inode_cache"}, 3773818aea2SQu Wenruo {Opt_noinode_cache, "noinode_cache"}, 3788965593eSDavid Sterba {Opt_no_space_cache, "nospace_cache"}, 3798dcddfa0SQu Wenruo {Opt_recovery, "recovery"}, /* deprecated */ 3808dcddfa0SQu Wenruo {Opt_usebackuproot, "usebackuproot"}, 3819555c6c1SIlya Dryomov {Opt_skip_balance, "skip_balance"}, 38221adbd5cSStefan Behrens {Opt_check_integrity, "check_int"}, 38321adbd5cSStefan Behrens {Opt_check_integrity_including_extent_data, "check_int_data"}, 38421adbd5cSStefan Behrens {Opt_check_integrity_print_mask, "check_int_print_mask=%d"}, 385f420ee1eSStefan Behrens {Opt_rescan_uuid_tree, "rescan_uuid_tree"}, 3868c342930SJeff Mahoney {Opt_fatal_errors, "fatal_errors=%s"}, 3878b87dc17SDavid Sterba {Opt_commit_interval, "commit=%d"}, 388d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 389d0bd4560SJosef Bacik {Opt_fragment_data, "fragment=data"}, 390d0bd4560SJosef Bacik {Opt_fragment_metadata, "fragment=metadata"}, 391d0bd4560SJosef Bacik {Opt_fragment_all, "fragment=all"}, 392d0bd4560SJosef Bacik #endif 393fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 394fb592373SJosef Bacik {Opt_ref_verify, "ref_verify"}, 395fb592373SJosef Bacik #endif 39633268eafSJosef Bacik {Opt_err, NULL}, 39795e05289SChris Mason }; 39895e05289SChris Mason 399edf24abeSChristoph Hellwig /* 400edf24abeSChristoph Hellwig * Regular mount options parser. Everything that is needed only when 401edf24abeSChristoph Hellwig * reading in a new superblock is parsed here. 40249b25e05SJeff Mahoney * XXX JDM: This needs to be cleaned up for remount. 403edf24abeSChristoph Hellwig */ 4042ff7e61eSJeff Mahoney int btrfs_parse_options(struct btrfs_fs_info *info, char *options, 40596da0919SQu Wenruo unsigned long new_flags) 40695e05289SChris Mason { 40795e05289SChris Mason substring_t args[MAX_OPT_ARGS]; 408e215772cSMisono, Tomohiro char *p, *num; 40973bc1876SJosef Bacik u64 cache_gen; 4104543df7eSChris Mason int intarg; 411a7a3f7caSSage Weil int ret = 0; 412261507a0SLi Zefan char *compress_type; 413261507a0SLi Zefan bool compress_force = false; 414b7c47bbbSTsutomu Itoh enum btrfs_compression_type saved_compress_type; 415b7c47bbbSTsutomu Itoh bool saved_compress_force; 416b7c47bbbSTsutomu Itoh int no_compress = 0; 417b6cda9bcSChris Mason 4180b246afaSJeff Mahoney cache_gen = btrfs_super_cache_generation(info->super_copy); 4190b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) 42070f6d82eSOmar Sandoval btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 42170f6d82eSOmar Sandoval else if (cache_gen) 42273bc1876SJosef Bacik btrfs_set_opt(info->mount_opt, SPACE_CACHE); 42373bc1876SJosef Bacik 42496da0919SQu Wenruo /* 42596da0919SQu Wenruo * Even the options are empty, we still need to do extra check 42696da0919SQu Wenruo * against new flags 42796da0919SQu Wenruo */ 42895e05289SChris Mason if (!options) 42996da0919SQu Wenruo goto check; 43095e05289SChris Mason 43195e05289SChris Mason while ((p = strsep(&options, ",")) != NULL) { 43295e05289SChris Mason int token; 43395e05289SChris Mason if (!*p) 43495e05289SChris Mason continue; 43595e05289SChris Mason 43695e05289SChris Mason token = match_token(p, tokens, args); 43795e05289SChris Mason switch (token) { 438dfe25020SChris Mason case Opt_degraded: 4390b246afaSJeff Mahoney btrfs_info(info, "allowing degraded mounts"); 440dfe25020SChris Mason btrfs_set_opt(info->mount_opt, DEGRADED); 441dfe25020SChris Mason break; 44295e05289SChris Mason case Opt_subvol: 44373f73415SJosef Bacik case Opt_subvolid: 444e15d0542SXin Zhong case Opt_subvolrootid: 44543e570b0SChristoph Hellwig case Opt_device: 446edf24abeSChristoph Hellwig /* 447d7407606SMisono, Tomohiro * These are parsed by btrfs_parse_subvol_options 448d7407606SMisono, Tomohiro * and btrfs_parse_early_options 449edf24abeSChristoph Hellwig * and can be happily ignored here. 450edf24abeSChristoph Hellwig */ 45195e05289SChris Mason break; 452b6cda9bcSChris Mason case Opt_nodatasum: 4533cdde224SJeff Mahoney btrfs_set_and_info(info, NODATASUM, 45407802534SQu Wenruo "setting nodatasum"); 455be20aa9dSChris Mason break; 456d399167dSQu Wenruo case Opt_datasum: 4573cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) { 4583cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 4590b246afaSJeff Mahoney btrfs_info(info, 4605d163e0eSJeff Mahoney "setting datasum, datacow enabled"); 461d399167dSQu Wenruo else 4620b246afaSJeff Mahoney btrfs_info(info, "setting datasum"); 46307802534SQu Wenruo } 464d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATACOW); 465d399167dSQu Wenruo btrfs_clear_opt(info->mount_opt, NODATASUM); 466d399167dSQu Wenruo break; 467be20aa9dSChris Mason case Opt_nodatacow: 4683cdde224SJeff Mahoney if (!btrfs_test_opt(info, NODATACOW)) { 4693cdde224SJeff Mahoney if (!btrfs_test_opt(info, COMPRESS) || 4703cdde224SJeff Mahoney !btrfs_test_opt(info, FORCE_COMPRESS)) { 4710b246afaSJeff Mahoney btrfs_info(info, 472efe120a0SFrank Holton "setting nodatacow, compression disabled"); 473bedb2ccaSAndrei Popa } else { 4740b246afaSJeff Mahoney btrfs_info(info, "setting nodatacow"); 475bedb2ccaSAndrei Popa } 47607802534SQu Wenruo } 477bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, COMPRESS); 478bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 479be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATACOW); 480be20aa9dSChris Mason btrfs_set_opt(info->mount_opt, NODATASUM); 481b6cda9bcSChris Mason break; 482a258af7aSQu Wenruo case Opt_datacow: 4833cdde224SJeff Mahoney btrfs_clear_and_info(info, NODATACOW, 48407802534SQu Wenruo "setting datacow"); 485a258af7aSQu Wenruo break; 486a555f810SChris Mason case Opt_compress_force: 487261507a0SLi Zefan case Opt_compress_force_type: 488261507a0SLi Zefan compress_force = true; 4891c697d4aSEric Sandeen /* Fallthrough */ 490261507a0SLi Zefan case Opt_compress: 491261507a0SLi Zefan case Opt_compress_type: 4923cdde224SJeff Mahoney saved_compress_type = btrfs_test_opt(info, 4933cdde224SJeff Mahoney COMPRESS) ? 494b7c47bbbSTsutomu Itoh info->compress_type : BTRFS_COMPRESS_NONE; 495b7c47bbbSTsutomu Itoh saved_compress_force = 4963cdde224SJeff Mahoney btrfs_test_opt(info, FORCE_COMPRESS); 497261507a0SLi Zefan if (token == Opt_compress || 498261507a0SLi Zefan token == Opt_compress_force || 499a7164fa4SDavid Sterba strncmp(args[0].from, "zlib", 4) == 0) { 500261507a0SLi Zefan compress_type = "zlib"; 501eae8d825SQu Wenruo 502261507a0SLi Zefan info->compress_type = BTRFS_COMPRESS_ZLIB; 503eae8d825SQu Wenruo info->compress_level = BTRFS_ZLIB_DEFAULT_LEVEL; 504eae8d825SQu Wenruo /* 505eae8d825SQu Wenruo * args[0] contains uninitialized data since 506eae8d825SQu Wenruo * for these tokens we don't expect any 507eae8d825SQu Wenruo * parameter. 508eae8d825SQu Wenruo */ 509eae8d825SQu Wenruo if (token != Opt_compress && 510eae8d825SQu Wenruo token != Opt_compress_force) 511f51d2b59SDavid Sterba info->compress_level = 512f51d2b59SDavid Sterba btrfs_compress_str2level(args[0].from); 513063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 514bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 515bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 516b7c47bbbSTsutomu Itoh no_compress = 0; 517a7164fa4SDavid Sterba } else if (strncmp(args[0].from, "lzo", 3) == 0) { 518a6fa6faeSLi Zefan compress_type = "lzo"; 519a6fa6faeSLi Zefan info->compress_type = BTRFS_COMPRESS_LZO; 520063849eaSArnd Hannemann btrfs_set_opt(info->mount_opt, COMPRESS); 521bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATACOW); 522bedb2ccaSAndrei Popa btrfs_clear_opt(info->mount_opt, NODATASUM); 5232b0ce2c2SMitch Harder btrfs_set_fs_incompat(info, COMPRESS_LZO); 524b7c47bbbSTsutomu Itoh no_compress = 0; 5255c1aab1dSNick Terrell } else if (strcmp(args[0].from, "zstd") == 0) { 5265c1aab1dSNick Terrell compress_type = "zstd"; 5275c1aab1dSNick Terrell info->compress_type = BTRFS_COMPRESS_ZSTD; 5285c1aab1dSNick Terrell btrfs_set_opt(info->mount_opt, COMPRESS); 5295c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATACOW); 5305c1aab1dSNick Terrell btrfs_clear_opt(info->mount_opt, NODATASUM); 5315c1aab1dSNick Terrell btrfs_set_fs_incompat(info, COMPRESS_ZSTD); 5325c1aab1dSNick Terrell no_compress = 0; 533063849eaSArnd Hannemann } else if (strncmp(args[0].from, "no", 2) == 0) { 534063849eaSArnd Hannemann compress_type = "no"; 535063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, COMPRESS); 536063849eaSArnd Hannemann btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 537063849eaSArnd Hannemann compress_force = false; 538b7c47bbbSTsutomu Itoh no_compress++; 539261507a0SLi Zefan } else { 540261507a0SLi Zefan ret = -EINVAL; 541261507a0SLi Zefan goto out; 542261507a0SLi Zefan } 543261507a0SLi Zefan 544261507a0SLi Zefan if (compress_force) { 545b7c47bbbSTsutomu Itoh btrfs_set_opt(info->mount_opt, FORCE_COMPRESS); 546143f3636SDavid Sterba } else { 5474027e0f4SWang Shilong /* 5484027e0f4SWang Shilong * If we remount from compress-force=xxx to 5494027e0f4SWang Shilong * compress=xxx, we need clear FORCE_COMPRESS 5504027e0f4SWang Shilong * flag, otherwise, there is no way for users 5514027e0f4SWang Shilong * to disable forcible compression separately. 5524027e0f4SWang Shilong */ 5534027e0f4SWang Shilong btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 554a7e252afSMiao Xie } 5553cdde224SJeff Mahoney if ((btrfs_test_opt(info, COMPRESS) && 556b7c47bbbSTsutomu Itoh (info->compress_type != saved_compress_type || 557b7c47bbbSTsutomu Itoh compress_force != saved_compress_force)) || 5583cdde224SJeff Mahoney (!btrfs_test_opt(info, COMPRESS) && 559b7c47bbbSTsutomu Itoh no_compress == 1)) { 560f51d2b59SDavid Sterba btrfs_info(info, "%s %s compression, level %d", 561b7c47bbbSTsutomu Itoh (compress_force) ? "force" : "use", 562f51d2b59SDavid Sterba compress_type, info->compress_level); 563b7c47bbbSTsutomu Itoh } 564b7c47bbbSTsutomu Itoh compress_force = false; 565a555f810SChris Mason break; 566e18e4809SChris Mason case Opt_ssd: 5673cdde224SJeff Mahoney btrfs_set_and_info(info, SSD, 568583b7231SHans van Kranenburg "enabling ssd optimizations"); 569951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 570e18e4809SChris Mason break; 571451d7585SChris Mason case Opt_ssd_spread: 572583b7231SHans van Kranenburg btrfs_set_and_info(info, SSD, 573583b7231SHans van Kranenburg "enabling ssd optimizations"); 5743cdde224SJeff Mahoney btrfs_set_and_info(info, SSD_SPREAD, 575583b7231SHans van Kranenburg "using spread ssd allocation scheme"); 576951e7966SAdam Borowski btrfs_clear_opt(info->mount_opt, NOSSD); 577451d7585SChris Mason break; 5783b30c22fSChris Mason case Opt_nossd: 579583b7231SHans van Kranenburg btrfs_set_opt(info->mount_opt, NOSSD); 580583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD, 581583b7231SHans van Kranenburg "not using ssd optimizations"); 582583b7231SHans van Kranenburg btrfs_clear_and_info(info, SSD_SPREAD, 583583b7231SHans van Kranenburg "not using spread ssd allocation scheme"); 5843b30c22fSChris Mason break; 585842bef58SQu Wenruo case Opt_barrier: 5863cdde224SJeff Mahoney btrfs_clear_and_info(info, NOBARRIER, 58707802534SQu Wenruo "turning on barriers"); 588842bef58SQu Wenruo break; 58921ad10cfSChris Mason case Opt_nobarrier: 5903cdde224SJeff Mahoney btrfs_set_and_info(info, NOBARRIER, 59107802534SQu Wenruo "turning off barriers"); 59221ad10cfSChris Mason break; 5934543df7eSChris Mason case Opt_thread_pool: 5942c334e87SWang Shilong ret = match_int(&args[0], &intarg); 5952c334e87SWang Shilong if (ret) { 5962c334e87SWang Shilong goto out; 597*f7b885beSAnand Jain } else if (intarg == 0) { 5982c334e87SWang Shilong ret = -EINVAL; 5992c334e87SWang Shilong goto out; 6002c334e87SWang Shilong } 601*f7b885beSAnand Jain info->thread_pool_size = intarg; 6024543df7eSChris Mason break; 6036f568d35SChris Mason case Opt_max_inline: 604edf24abeSChristoph Hellwig num = match_strdup(&args[0]); 6056f568d35SChris Mason if (num) { 60691748467SAkinobu Mita info->max_inline = memparse(num, NULL); 6076f568d35SChris Mason kfree(num); 6086f568d35SChris Mason 60915ada040SChris Mason if (info->max_inline) { 610feb5f965SMitch Harder info->max_inline = min_t(u64, 61115ada040SChris Mason info->max_inline, 6120b246afaSJeff Mahoney info->sectorsize); 61315ada040SChris Mason } 6140b246afaSJeff Mahoney btrfs_info(info, "max_inline at %llu", 615c1c9ff7cSGeert Uytterhoeven info->max_inline); 6162c334e87SWang Shilong } else { 6172c334e87SWang Shilong ret = -ENOMEM; 6182c334e87SWang Shilong goto out; 6196f568d35SChris Mason } 6206f568d35SChris Mason break; 6218f662a76SChris Mason case Opt_alloc_start: 6220d0c71b3SDavid Sterba btrfs_info(info, 6230d0c71b3SDavid Sterba "option alloc_start is obsolete, ignored"); 6248f662a76SChris Mason break; 625bd0330adSQu Wenruo case Opt_acl: 62645ff35d6SGuangliang Zhao #ifdef CONFIG_BTRFS_FS_POSIX_ACL 6271751e8a6SLinus Torvalds info->sb->s_flags |= SB_POSIXACL; 628bd0330adSQu Wenruo break; 62945ff35d6SGuangliang Zhao #else 6300b246afaSJeff Mahoney btrfs_err(info, "support for ACL not compiled in!"); 63145ff35d6SGuangliang Zhao ret = -EINVAL; 63245ff35d6SGuangliang Zhao goto out; 63345ff35d6SGuangliang Zhao #endif 63433268eafSJosef Bacik case Opt_noacl: 6351751e8a6SLinus Torvalds info->sb->s_flags &= ~SB_POSIXACL; 63633268eafSJosef Bacik break; 6373a5e1404SSage Weil case Opt_notreelog: 6383cdde224SJeff Mahoney btrfs_set_and_info(info, NOTREELOG, 63907802534SQu Wenruo "disabling tree log"); 6403a5e1404SSage Weil break; 641a88998f2SQu Wenruo case Opt_treelog: 6423cdde224SJeff Mahoney btrfs_clear_and_info(info, NOTREELOG, 64307802534SQu Wenruo "enabling tree log"); 644a88998f2SQu Wenruo break; 645fed8f166SQu Wenruo case Opt_norecovery: 64696da0919SQu Wenruo case Opt_nologreplay: 6473cdde224SJeff Mahoney btrfs_set_and_info(info, NOLOGREPLAY, 64896da0919SQu Wenruo "disabling log replay at mount time"); 64996da0919SQu Wenruo break; 650dccae999SSage Weil case Opt_flushoncommit: 6513cdde224SJeff Mahoney btrfs_set_and_info(info, FLUSHONCOMMIT, 65207802534SQu Wenruo "turning on flush-on-commit"); 653dccae999SSage Weil break; 6542c9ee856SQu Wenruo case Opt_noflushoncommit: 6553cdde224SJeff Mahoney btrfs_clear_and_info(info, FLUSHONCOMMIT, 65607802534SQu Wenruo "turning off flush-on-commit"); 6572c9ee856SQu Wenruo break; 65897e728d4SJosef Bacik case Opt_ratio: 6592c334e87SWang Shilong ret = match_int(&args[0], &intarg); 6602c334e87SWang Shilong if (ret) { 6612c334e87SWang Shilong goto out; 6622c334e87SWang Shilong } else if (intarg >= 0) { 66397e728d4SJosef Bacik info->metadata_ratio = intarg; 6640b246afaSJeff Mahoney btrfs_info(info, "metadata ratio %d", 66597e728d4SJosef Bacik info->metadata_ratio); 6662c334e87SWang Shilong } else { 6672c334e87SWang Shilong ret = -EINVAL; 6682c334e87SWang Shilong goto out; 66997e728d4SJosef Bacik } 67097e728d4SJosef Bacik break; 671e244a0aeSChristoph Hellwig case Opt_discard: 6723cdde224SJeff Mahoney btrfs_set_and_info(info, DISCARD, 67307802534SQu Wenruo "turning on discard"); 674e244a0aeSChristoph Hellwig break; 675e07a2adeSQu Wenruo case Opt_nodiscard: 6763cdde224SJeff Mahoney btrfs_clear_and_info(info, DISCARD, 67707802534SQu Wenruo "turning off discard"); 678e07a2adeSQu Wenruo break; 6790af3d00bSJosef Bacik case Opt_space_cache: 68070f6d82eSOmar Sandoval case Opt_space_cache_version: 68170f6d82eSOmar Sandoval if (token == Opt_space_cache || 68270f6d82eSOmar Sandoval strcmp(args[0].from, "v1") == 0) { 6830b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68470f6d82eSOmar Sandoval FREE_SPACE_TREE); 6853cdde224SJeff Mahoney btrfs_set_and_info(info, SPACE_CACHE, 68607802534SQu Wenruo "enabling disk space caching"); 68770f6d82eSOmar Sandoval } else if (strcmp(args[0].from, "v2") == 0) { 6880b246afaSJeff Mahoney btrfs_clear_opt(info->mount_opt, 68970f6d82eSOmar Sandoval SPACE_CACHE); 6900b246afaSJeff Mahoney btrfs_set_and_info(info, FREE_SPACE_TREE, 69170f6d82eSOmar Sandoval "enabling free space tree"); 69270f6d82eSOmar Sandoval } else { 69370f6d82eSOmar Sandoval ret = -EINVAL; 69470f6d82eSOmar Sandoval goto out; 69570f6d82eSOmar Sandoval } 6960de90876SJosef Bacik break; 697f420ee1eSStefan Behrens case Opt_rescan_uuid_tree: 698f420ee1eSStefan Behrens btrfs_set_opt(info->mount_opt, RESCAN_UUID_TREE); 699f420ee1eSStefan Behrens break; 70073bc1876SJosef Bacik case Opt_no_space_cache: 7013cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) { 7020b246afaSJeff Mahoney btrfs_clear_and_info(info, SPACE_CACHE, 70307802534SQu Wenruo "disabling disk space caching"); 70470f6d82eSOmar Sandoval } 7053cdde224SJeff Mahoney if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 7060b246afaSJeff Mahoney btrfs_clear_and_info(info, FREE_SPACE_TREE, 70770f6d82eSOmar Sandoval "disabling free space tree"); 70870f6d82eSOmar Sandoval } 70973bc1876SJosef Bacik break; 7104b9465cbSChris Mason case Opt_inode_cache: 7117e1876acSDavid Sterba btrfs_set_pending_and_info(info, INODE_MAP_CACHE, 71207802534SQu Wenruo "enabling inode map caching"); 7133818aea2SQu Wenruo break; 7143818aea2SQu Wenruo case Opt_noinode_cache: 7157e1876acSDavid Sterba btrfs_clear_pending_and_info(info, INODE_MAP_CACHE, 71607802534SQu Wenruo "disabling inode map caching"); 7174b9465cbSChris Mason break; 71888c2ba3bSJosef Bacik case Opt_clear_cache: 7193cdde224SJeff Mahoney btrfs_set_and_info(info, CLEAR_CACHE, 72007802534SQu Wenruo "force clearing of disk cache"); 7210af3d00bSJosef Bacik break; 7224260f7c7SSage Weil case Opt_user_subvol_rm_allowed: 7234260f7c7SSage Weil btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED); 7244260f7c7SSage Weil break; 72591435650SChris Mason case Opt_enospc_debug: 72691435650SChris Mason btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG); 72791435650SChris Mason break; 72853036293SQu Wenruo case Opt_noenospc_debug: 72953036293SQu Wenruo btrfs_clear_opt(info->mount_opt, ENOSPC_DEBUG); 73053036293SQu Wenruo break; 7314cb5300bSChris Mason case Opt_defrag: 7323cdde224SJeff Mahoney btrfs_set_and_info(info, AUTO_DEFRAG, 73307802534SQu Wenruo "enabling auto defrag"); 7344cb5300bSChris Mason break; 735fc0ca9afSQu Wenruo case Opt_nodefrag: 7363cdde224SJeff Mahoney btrfs_clear_and_info(info, AUTO_DEFRAG, 73707802534SQu Wenruo "disabling auto defrag"); 738fc0ca9afSQu Wenruo break; 739af31f5e5SChris Mason case Opt_recovery: 7400b246afaSJeff Mahoney btrfs_warn(info, 7418dcddfa0SQu Wenruo "'recovery' is deprecated, use 'usebackuproot' instead"); 7428dcddfa0SQu Wenruo case Opt_usebackuproot: 7430b246afaSJeff Mahoney btrfs_info(info, 7448dcddfa0SQu Wenruo "trying to use backup root at mount time"); 7458dcddfa0SQu Wenruo btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 746af31f5e5SChris Mason break; 7479555c6c1SIlya Dryomov case Opt_skip_balance: 7489555c6c1SIlya Dryomov btrfs_set_opt(info->mount_opt, SKIP_BALANCE); 7499555c6c1SIlya Dryomov break; 75021adbd5cSStefan Behrens #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 75121adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 7520b246afaSJeff Mahoney btrfs_info(info, 753efe120a0SFrank Holton "enabling check integrity including extent data"); 75421adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, 75521adbd5cSStefan Behrens CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 75621adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 75721adbd5cSStefan Behrens break; 75821adbd5cSStefan Behrens case Opt_check_integrity: 7590b246afaSJeff Mahoney btrfs_info(info, "enabling check integrity"); 76021adbd5cSStefan Behrens btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 76121adbd5cSStefan Behrens break; 76221adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7632c334e87SWang Shilong ret = match_int(&args[0], &intarg); 7642c334e87SWang Shilong if (ret) { 7652c334e87SWang Shilong goto out; 7662c334e87SWang Shilong } else if (intarg >= 0) { 76721adbd5cSStefan Behrens info->check_integrity_print_mask = intarg; 7680b246afaSJeff Mahoney btrfs_info(info, 7695d163e0eSJeff Mahoney "check_integrity_print_mask 0x%x", 77021adbd5cSStefan Behrens info->check_integrity_print_mask); 7712c334e87SWang Shilong } else { 7722c334e87SWang Shilong ret = -EINVAL; 7732c334e87SWang Shilong goto out; 77421adbd5cSStefan Behrens } 77521adbd5cSStefan Behrens break; 77621adbd5cSStefan Behrens #else 77721adbd5cSStefan Behrens case Opt_check_integrity_including_extent_data: 77821adbd5cSStefan Behrens case Opt_check_integrity: 77921adbd5cSStefan Behrens case Opt_check_integrity_print_mask: 7800b246afaSJeff Mahoney btrfs_err(info, 781efe120a0SFrank Holton "support for check_integrity* not compiled in!"); 78221adbd5cSStefan Behrens ret = -EINVAL; 78321adbd5cSStefan Behrens goto out; 78421adbd5cSStefan Behrens #endif 7858c342930SJeff Mahoney case Opt_fatal_errors: 7868c342930SJeff Mahoney if (strcmp(args[0].from, "panic") == 0) 7878c342930SJeff Mahoney btrfs_set_opt(info->mount_opt, 7888c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7898c342930SJeff Mahoney else if (strcmp(args[0].from, "bug") == 0) 7908c342930SJeff Mahoney btrfs_clear_opt(info->mount_opt, 7918c342930SJeff Mahoney PANIC_ON_FATAL_ERROR); 7928c342930SJeff Mahoney else { 7938c342930SJeff Mahoney ret = -EINVAL; 7948c342930SJeff Mahoney goto out; 7958c342930SJeff Mahoney } 7968c342930SJeff Mahoney break; 7978b87dc17SDavid Sterba case Opt_commit_interval: 7988b87dc17SDavid Sterba intarg = 0; 7998b87dc17SDavid Sterba ret = match_int(&args[0], &intarg); 8008b87dc17SDavid Sterba if (ret < 0) { 8010b246afaSJeff Mahoney btrfs_err(info, "invalid commit interval"); 8028b87dc17SDavid Sterba ret = -EINVAL; 8038b87dc17SDavid Sterba goto out; 8048b87dc17SDavid Sterba } 8058b87dc17SDavid Sterba if (intarg > 0) { 8068b87dc17SDavid Sterba if (intarg > 300) { 8070b246afaSJeff Mahoney btrfs_warn(info, 8085d163e0eSJeff Mahoney "excessive commit interval %d", 8098b87dc17SDavid Sterba intarg); 8108b87dc17SDavid Sterba } 8118b87dc17SDavid Sterba info->commit_interval = intarg; 8128b87dc17SDavid Sterba } else { 8130b246afaSJeff Mahoney btrfs_info(info, 8145d163e0eSJeff Mahoney "using default commit interval %ds", 8158b87dc17SDavid Sterba BTRFS_DEFAULT_COMMIT_INTERVAL); 8168b87dc17SDavid Sterba info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 8178b87dc17SDavid Sterba } 8188b87dc17SDavid Sterba break; 819d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 820d0bd4560SJosef Bacik case Opt_fragment_all: 8210b246afaSJeff Mahoney btrfs_info(info, "fragmenting all space"); 822d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 823d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 824d0bd4560SJosef Bacik break; 825d0bd4560SJosef Bacik case Opt_fragment_metadata: 8260b246afaSJeff Mahoney btrfs_info(info, "fragmenting metadata"); 827d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, 828d0bd4560SJosef Bacik FRAGMENT_METADATA); 829d0bd4560SJosef Bacik break; 830d0bd4560SJosef Bacik case Opt_fragment_data: 8310b246afaSJeff Mahoney btrfs_info(info, "fragmenting data"); 832d0bd4560SJosef Bacik btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 833d0bd4560SJosef Bacik break; 834d0bd4560SJosef Bacik #endif 835fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 836fb592373SJosef Bacik case Opt_ref_verify: 837fb592373SJosef Bacik btrfs_info(info, "doing ref verification"); 838fb592373SJosef Bacik btrfs_set_opt(info->mount_opt, REF_VERIFY); 839fb592373SJosef Bacik break; 840fb592373SJosef Bacik #endif 841a7a3f7caSSage Weil case Opt_err: 8420b246afaSJeff Mahoney btrfs_info(info, "unrecognized mount option '%s'", p); 843a7a3f7caSSage Weil ret = -EINVAL; 844a7a3f7caSSage Weil goto out; 84595e05289SChris Mason default: 846be20aa9dSChris Mason break; 84795e05289SChris Mason } 84895e05289SChris Mason } 84996da0919SQu Wenruo check: 85096da0919SQu Wenruo /* 85196da0919SQu Wenruo * Extra check for current option against current flag 85296da0919SQu Wenruo */ 8531751e8a6SLinus Torvalds if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & SB_RDONLY)) { 8540b246afaSJeff Mahoney btrfs_err(info, 85596da0919SQu Wenruo "nologreplay must be used with ro mount option"); 85696da0919SQu Wenruo ret = -EINVAL; 85796da0919SQu Wenruo } 858a7a3f7caSSage Weil out: 8590b246afaSJeff Mahoney if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) && 8603cdde224SJeff Mahoney !btrfs_test_opt(info, FREE_SPACE_TREE) && 8613cdde224SJeff Mahoney !btrfs_test_opt(info, CLEAR_CACHE)) { 8620b246afaSJeff Mahoney btrfs_err(info, "cannot disable free space tree"); 86370f6d82eSOmar Sandoval ret = -EINVAL; 86470f6d82eSOmar Sandoval 86570f6d82eSOmar Sandoval } 8663cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 8670b246afaSJeff Mahoney btrfs_info(info, "disk space caching is enabled"); 8683cdde224SJeff Mahoney if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 8690b246afaSJeff Mahoney btrfs_info(info, "using free space tree"); 870a7a3f7caSSage Weil return ret; 871edf24abeSChristoph Hellwig } 872edf24abeSChristoph Hellwig 873edf24abeSChristoph Hellwig /* 874edf24abeSChristoph Hellwig * Parse mount options that are required early in the mount process. 875edf24abeSChristoph Hellwig * 876edf24abeSChristoph Hellwig * All other options will be parsed on much later in the mount process and 877edf24abeSChristoph Hellwig * only when we need to allocate a new super block. 878edf24abeSChristoph Hellwig */ 87997288f2cSChristoph Hellwig static int btrfs_parse_early_options(const char *options, fmode_t flags, 880d7407606SMisono, Tomohiro void *holder, struct btrfs_fs_devices **fs_devices) 881edf24abeSChristoph Hellwig { 882edf24abeSChristoph Hellwig substring_t args[MAX_OPT_ARGS]; 88383c8c9bdSJeff Liu char *device_name, *opts, *orig, *p; 884d7407606SMisono, Tomohiro int error = 0; 885d7407606SMisono, Tomohiro 886d7407606SMisono, Tomohiro if (!options) 887d7407606SMisono, Tomohiro return 0; 888d7407606SMisono, Tomohiro 889d7407606SMisono, Tomohiro /* 890d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because btrfs_parse_options 891d7407606SMisono, Tomohiro * gets called later 892d7407606SMisono, Tomohiro */ 893d7407606SMisono, Tomohiro opts = kstrdup(options, GFP_KERNEL); 894d7407606SMisono, Tomohiro if (!opts) 895d7407606SMisono, Tomohiro return -ENOMEM; 896d7407606SMisono, Tomohiro orig = opts; 897d7407606SMisono, Tomohiro 898d7407606SMisono, Tomohiro while ((p = strsep(&opts, ",")) != NULL) { 899d7407606SMisono, Tomohiro int token; 900d7407606SMisono, Tomohiro 901d7407606SMisono, Tomohiro if (!*p) 902d7407606SMisono, Tomohiro continue; 903d7407606SMisono, Tomohiro 904d7407606SMisono, Tomohiro token = match_token(p, tokens, args); 905d7407606SMisono, Tomohiro if (token == Opt_device) { 906d7407606SMisono, Tomohiro device_name = match_strdup(&args[0]); 907d7407606SMisono, Tomohiro if (!device_name) { 908d7407606SMisono, Tomohiro error = -ENOMEM; 909d7407606SMisono, Tomohiro goto out; 910d7407606SMisono, Tomohiro } 911d7407606SMisono, Tomohiro error = btrfs_scan_one_device(device_name, 912d7407606SMisono, Tomohiro flags, holder, fs_devices); 913d7407606SMisono, Tomohiro kfree(device_name); 914d7407606SMisono, Tomohiro if (error) 915d7407606SMisono, Tomohiro goto out; 916d7407606SMisono, Tomohiro } 917d7407606SMisono, Tomohiro } 918d7407606SMisono, Tomohiro 919d7407606SMisono, Tomohiro out: 920d7407606SMisono, Tomohiro kfree(orig); 921d7407606SMisono, Tomohiro return error; 922d7407606SMisono, Tomohiro } 923d7407606SMisono, Tomohiro 924d7407606SMisono, Tomohiro /* 925d7407606SMisono, Tomohiro * Parse mount options that are related to subvolume id 926d7407606SMisono, Tomohiro * 927d7407606SMisono, Tomohiro * The value is later passed to mount_subvol() 928d7407606SMisono, Tomohiro */ 929d7407606SMisono, Tomohiro static int btrfs_parse_subvol_options(const char *options, fmode_t flags, 93078f6beacSMisono, Tomohiro char **subvol_name, u64 *subvol_objectid) 931d7407606SMisono, Tomohiro { 932d7407606SMisono, Tomohiro substring_t args[MAX_OPT_ARGS]; 933d7407606SMisono, Tomohiro char *opts, *orig, *p; 9341493381fSWang Shilong char *num = NULL; 935edf24abeSChristoph Hellwig int error = 0; 936edf24abeSChristoph Hellwig 937edf24abeSChristoph Hellwig if (!options) 938830c4adbSJosef Bacik return 0; 939edf24abeSChristoph Hellwig 940edf24abeSChristoph Hellwig /* 941d7407606SMisono, Tomohiro * strsep changes the string, duplicate it because 942d7407606SMisono, Tomohiro * btrfs_parse_early_options gets called later 943edf24abeSChristoph Hellwig */ 944edf24abeSChristoph Hellwig opts = kstrdup(options, GFP_KERNEL); 945edf24abeSChristoph Hellwig if (!opts) 946edf24abeSChristoph Hellwig return -ENOMEM; 9473f3d0bc0STero Roponen orig = opts; 948edf24abeSChristoph Hellwig 949edf24abeSChristoph Hellwig while ((p = strsep(&opts, ",")) != NULL) { 950edf24abeSChristoph Hellwig int token; 951edf24abeSChristoph Hellwig if (!*p) 952edf24abeSChristoph Hellwig continue; 953edf24abeSChristoph Hellwig 954edf24abeSChristoph Hellwig token = match_token(p, tokens, args); 955edf24abeSChristoph Hellwig switch (token) { 956edf24abeSChristoph Hellwig case Opt_subvol: 957a90e8b6fSIlya Dryomov kfree(*subvol_name); 958edf24abeSChristoph Hellwig *subvol_name = match_strdup(&args[0]); 9592c334e87SWang Shilong if (!*subvol_name) { 9602c334e87SWang Shilong error = -ENOMEM; 9612c334e87SWang Shilong goto out; 9622c334e87SWang Shilong } 963edf24abeSChristoph Hellwig break; 96473f73415SJosef Bacik case Opt_subvolid: 9651493381fSWang Shilong num = match_strdup(&args[0]); 9661493381fSWang Shilong if (num) { 9671493381fSWang Shilong *subvol_objectid = memparse(num, NULL); 9681493381fSWang Shilong kfree(num); 9694849f01dSJosef Bacik /* we want the original fs_tree */ 9701493381fSWang Shilong if (!*subvol_objectid) 9714849f01dSJosef Bacik *subvol_objectid = 9724849f01dSJosef Bacik BTRFS_FS_TREE_OBJECTID; 9732c334e87SWang Shilong } else { 9742c334e87SWang Shilong error = -EINVAL; 9752c334e87SWang Shilong goto out; 9764849f01dSJosef Bacik } 97773f73415SJosef Bacik break; 978e15d0542SXin Zhong case Opt_subvolrootid: 97962e85577SJeff Mahoney pr_warn("BTRFS: 'subvolrootid' mount option is deprecated and has no effect\n"); 980e15d0542SXin Zhong break; 981edf24abeSChristoph Hellwig default: 982edf24abeSChristoph Hellwig break; 983edf24abeSChristoph Hellwig } 984edf24abeSChristoph Hellwig } 985edf24abeSChristoph Hellwig 986edf24abeSChristoph Hellwig out: 987830c4adbSJosef Bacik kfree(orig); 988edf24abeSChristoph Hellwig return error; 98995e05289SChris Mason } 99095e05289SChris Mason 99105dbe683SOmar Sandoval static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info, 99273f73415SJosef Bacik u64 subvol_objectid) 99373f73415SJosef Bacik { 994815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 99505dbe683SOmar Sandoval struct btrfs_root *fs_root; 99605dbe683SOmar Sandoval struct btrfs_root_ref *root_ref; 99705dbe683SOmar Sandoval struct btrfs_inode_ref *inode_ref; 99805dbe683SOmar Sandoval struct btrfs_key key; 99905dbe683SOmar Sandoval struct btrfs_path *path = NULL; 100005dbe683SOmar Sandoval char *name = NULL, *ptr; 100105dbe683SOmar Sandoval u64 dirid; 100205dbe683SOmar Sandoval int len; 100305dbe683SOmar Sandoval int ret; 100405dbe683SOmar Sandoval 100505dbe683SOmar Sandoval path = btrfs_alloc_path(); 100605dbe683SOmar Sandoval if (!path) { 100705dbe683SOmar Sandoval ret = -ENOMEM; 100805dbe683SOmar Sandoval goto err; 100905dbe683SOmar Sandoval } 101005dbe683SOmar Sandoval path->leave_spinning = 1; 101105dbe683SOmar Sandoval 10123ec83621SDavid Sterba name = kmalloc(PATH_MAX, GFP_KERNEL); 101305dbe683SOmar Sandoval if (!name) { 101405dbe683SOmar Sandoval ret = -ENOMEM; 101505dbe683SOmar Sandoval goto err; 101605dbe683SOmar Sandoval } 101705dbe683SOmar Sandoval ptr = name + PATH_MAX - 1; 101805dbe683SOmar Sandoval ptr[0] = '\0'; 101905dbe683SOmar Sandoval 102005dbe683SOmar Sandoval /* 102105dbe683SOmar Sandoval * Walk up the subvolume trees in the tree of tree roots by root 102205dbe683SOmar Sandoval * backrefs until we hit the top-level subvolume. 102305dbe683SOmar Sandoval */ 102405dbe683SOmar Sandoval while (subvol_objectid != BTRFS_FS_TREE_OBJECTID) { 102505dbe683SOmar Sandoval key.objectid = subvol_objectid; 102605dbe683SOmar Sandoval key.type = BTRFS_ROOT_BACKREF_KEY; 102705dbe683SOmar Sandoval key.offset = (u64)-1; 102805dbe683SOmar Sandoval 102905dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 103005dbe683SOmar Sandoval if (ret < 0) { 103105dbe683SOmar Sandoval goto err; 103205dbe683SOmar Sandoval } else if (ret > 0) { 103305dbe683SOmar Sandoval ret = btrfs_previous_item(root, path, subvol_objectid, 103405dbe683SOmar Sandoval BTRFS_ROOT_BACKREF_KEY); 103505dbe683SOmar Sandoval if (ret < 0) { 103605dbe683SOmar Sandoval goto err; 103705dbe683SOmar Sandoval } else if (ret > 0) { 103805dbe683SOmar Sandoval ret = -ENOENT; 103905dbe683SOmar Sandoval goto err; 104005dbe683SOmar Sandoval } 104105dbe683SOmar Sandoval } 104205dbe683SOmar Sandoval 104305dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 104405dbe683SOmar Sandoval subvol_objectid = key.offset; 104505dbe683SOmar Sandoval 104605dbe683SOmar Sandoval root_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 104705dbe683SOmar Sandoval struct btrfs_root_ref); 104805dbe683SOmar Sandoval len = btrfs_root_ref_name_len(path->nodes[0], root_ref); 104905dbe683SOmar Sandoval ptr -= len + 1; 105005dbe683SOmar Sandoval if (ptr < name) { 105105dbe683SOmar Sandoval ret = -ENAMETOOLONG; 105205dbe683SOmar Sandoval goto err; 105305dbe683SOmar Sandoval } 105405dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 105505dbe683SOmar Sandoval (unsigned long)(root_ref + 1), len); 105605dbe683SOmar Sandoval ptr[0] = '/'; 105705dbe683SOmar Sandoval dirid = btrfs_root_ref_dirid(path->nodes[0], root_ref); 105805dbe683SOmar Sandoval btrfs_release_path(path); 105905dbe683SOmar Sandoval 106005dbe683SOmar Sandoval key.objectid = subvol_objectid; 106105dbe683SOmar Sandoval key.type = BTRFS_ROOT_ITEM_KEY; 106205dbe683SOmar Sandoval key.offset = (u64)-1; 106305dbe683SOmar Sandoval fs_root = btrfs_read_fs_root_no_name(fs_info, &key); 106405dbe683SOmar Sandoval if (IS_ERR(fs_root)) { 106505dbe683SOmar Sandoval ret = PTR_ERR(fs_root); 106605dbe683SOmar Sandoval goto err; 106705dbe683SOmar Sandoval } 106805dbe683SOmar Sandoval 106905dbe683SOmar Sandoval /* 107005dbe683SOmar Sandoval * Walk up the filesystem tree by inode refs until we hit the 107105dbe683SOmar Sandoval * root directory. 107205dbe683SOmar Sandoval */ 107305dbe683SOmar Sandoval while (dirid != BTRFS_FIRST_FREE_OBJECTID) { 107405dbe683SOmar Sandoval key.objectid = dirid; 107505dbe683SOmar Sandoval key.type = BTRFS_INODE_REF_KEY; 107605dbe683SOmar Sandoval key.offset = (u64)-1; 107705dbe683SOmar Sandoval 107805dbe683SOmar Sandoval ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); 107905dbe683SOmar Sandoval if (ret < 0) { 108005dbe683SOmar Sandoval goto err; 108105dbe683SOmar Sandoval } else if (ret > 0) { 108205dbe683SOmar Sandoval ret = btrfs_previous_item(fs_root, path, dirid, 108305dbe683SOmar Sandoval BTRFS_INODE_REF_KEY); 108405dbe683SOmar Sandoval if (ret < 0) { 108505dbe683SOmar Sandoval goto err; 108605dbe683SOmar Sandoval } else if (ret > 0) { 108705dbe683SOmar Sandoval ret = -ENOENT; 108805dbe683SOmar Sandoval goto err; 108905dbe683SOmar Sandoval } 109005dbe683SOmar Sandoval } 109105dbe683SOmar Sandoval 109205dbe683SOmar Sandoval btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); 109305dbe683SOmar Sandoval dirid = key.offset; 109405dbe683SOmar Sandoval 109505dbe683SOmar Sandoval inode_ref = btrfs_item_ptr(path->nodes[0], 109605dbe683SOmar Sandoval path->slots[0], 109705dbe683SOmar Sandoval struct btrfs_inode_ref); 109805dbe683SOmar Sandoval len = btrfs_inode_ref_name_len(path->nodes[0], 109905dbe683SOmar Sandoval inode_ref); 110005dbe683SOmar Sandoval ptr -= len + 1; 110105dbe683SOmar Sandoval if (ptr < name) { 110205dbe683SOmar Sandoval ret = -ENAMETOOLONG; 110305dbe683SOmar Sandoval goto err; 110405dbe683SOmar Sandoval } 110505dbe683SOmar Sandoval read_extent_buffer(path->nodes[0], ptr + 1, 110605dbe683SOmar Sandoval (unsigned long)(inode_ref + 1), len); 110705dbe683SOmar Sandoval ptr[0] = '/'; 110805dbe683SOmar Sandoval btrfs_release_path(path); 110905dbe683SOmar Sandoval } 111005dbe683SOmar Sandoval } 111105dbe683SOmar Sandoval 111205dbe683SOmar Sandoval btrfs_free_path(path); 111305dbe683SOmar Sandoval if (ptr == name + PATH_MAX - 1) { 111405dbe683SOmar Sandoval name[0] = '/'; 111505dbe683SOmar Sandoval name[1] = '\0'; 111605dbe683SOmar Sandoval } else { 111705dbe683SOmar Sandoval memmove(name, ptr, name + PATH_MAX - ptr); 111805dbe683SOmar Sandoval } 111905dbe683SOmar Sandoval return name; 112005dbe683SOmar Sandoval 112105dbe683SOmar Sandoval err: 112205dbe683SOmar Sandoval btrfs_free_path(path); 112305dbe683SOmar Sandoval kfree(name); 112405dbe683SOmar Sandoval return ERR_PTR(ret); 112505dbe683SOmar Sandoval } 112605dbe683SOmar Sandoval 112705dbe683SOmar Sandoval static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objectid) 112805dbe683SOmar Sandoval { 112905dbe683SOmar Sandoval struct btrfs_root *root = fs_info->tree_root; 113073f73415SJosef Bacik struct btrfs_dir_item *di; 113173f73415SJosef Bacik struct btrfs_path *path; 113273f73415SJosef Bacik struct btrfs_key location; 113373f73415SJosef Bacik u64 dir_id; 113473f73415SJosef Bacik 113573f73415SJosef Bacik path = btrfs_alloc_path(); 113673f73415SJosef Bacik if (!path) 113705dbe683SOmar Sandoval return -ENOMEM; 113873f73415SJosef Bacik path->leave_spinning = 1; 113973f73415SJosef Bacik 114073f73415SJosef Bacik /* 114173f73415SJosef Bacik * Find the "default" dir item which points to the root item that we 114273f73415SJosef Bacik * will mount by default if we haven't been given a specific subvolume 114373f73415SJosef Bacik * to mount. 114473f73415SJosef Bacik */ 1145815745cfSAl Viro dir_id = btrfs_super_root_dir(fs_info->super_copy); 114673f73415SJosef Bacik di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 1147b0839166SJulia Lawall if (IS_ERR(di)) { 1148b0839166SJulia Lawall btrfs_free_path(path); 114905dbe683SOmar Sandoval return PTR_ERR(di); 1150b0839166SJulia Lawall } 115173f73415SJosef Bacik if (!di) { 115273f73415SJosef Bacik /* 115373f73415SJosef Bacik * Ok the default dir item isn't there. This is weird since 115473f73415SJosef Bacik * it's always been there, but don't freak out, just try and 115505dbe683SOmar Sandoval * mount the top-level subvolume. 115673f73415SJosef Bacik */ 115773f73415SJosef Bacik btrfs_free_path(path); 115805dbe683SOmar Sandoval *objectid = BTRFS_FS_TREE_OBJECTID; 115905dbe683SOmar Sandoval return 0; 116073f73415SJosef Bacik } 116173f73415SJosef Bacik 116273f73415SJosef Bacik btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 116373f73415SJosef Bacik btrfs_free_path(path); 116405dbe683SOmar Sandoval *objectid = location.objectid; 116505dbe683SOmar Sandoval return 0; 116673f73415SJosef Bacik } 116773f73415SJosef Bacik 11688a4b83ccSChris Mason static int btrfs_fill_super(struct super_block *sb, 11698a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices, 117056e033a7SDavid Sterba void *data) 11712e635a27SChris Mason { 11722e635a27SChris Mason struct inode *inode; 1173815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 11745d4f98a2SYan Zheng struct btrfs_key key; 117539279cc3SChris Mason int err; 11762e635a27SChris Mason 11772e635a27SChris Mason sb->s_maxbytes = MAX_LFS_FILESIZE; 11782e635a27SChris Mason sb->s_magic = BTRFS_SUPER_MAGIC; 1179e20d96d6SChris Mason sb->s_op = &btrfs_super_ops; 1180af53d29aSAl Viro sb->s_d_op = &btrfs_dentry_operations; 1181be6e8dc0SBalaji Rao sb->s_export_op = &btrfs_export_ops; 11825103e947SJosef Bacik sb->s_xattr = btrfs_xattr_handlers; 11832e635a27SChris Mason sb->s_time_gran = 1; 11840eda294dSChris Mason #ifdef CONFIG_BTRFS_FS_POSIX_ACL 11851751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 118649cf6f45SChris Ball #endif 1187357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 1188da2f0f74SChris Mason sb->s_iflags |= SB_I_CGROUPWB; 11899e11ceeeSJan Kara 11909e11ceeeSJan Kara err = super_setup_bdi(sb); 11919e11ceeeSJan Kara if (err) { 11929e11ceeeSJan Kara btrfs_err(fs_info, "super_setup_bdi failed"); 11939e11ceeeSJan Kara return err; 11949e11ceeeSJan Kara } 11959e11ceeeSJan Kara 1196ad2b2c80SAl Viro err = open_ctree(sb, fs_devices, (char *)data); 1197ad2b2c80SAl Viro if (err) { 1198ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "open_ctree failed"); 1199ad2b2c80SAl Viro return err; 1200e20d96d6SChris Mason } 1201b888db2bSChris Mason 12025d4f98a2SYan Zheng key.objectid = BTRFS_FIRST_FREE_OBJECTID; 12035d4f98a2SYan Zheng key.type = BTRFS_INODE_ITEM_KEY; 12045d4f98a2SYan Zheng key.offset = 0; 120598c7089cSAl Viro inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL); 12065d4f98a2SYan Zheng if (IS_ERR(inode)) { 12075d4f98a2SYan Zheng err = PTR_ERR(inode); 120839279cc3SChris Mason goto fail_close; 120939279cc3SChris Mason } 12102e635a27SChris Mason 121148fde701SAl Viro sb->s_root = d_make_root(inode); 121248fde701SAl Viro if (!sb->s_root) { 121339279cc3SChris Mason err = -ENOMEM; 121439279cc3SChris Mason goto fail_close; 12152e635a27SChris Mason } 121658176a96SJosef Bacik 121790a887c9SDan Magenheimer cleancache_init_fs(sb); 12181751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 12192e635a27SChris Mason return 0; 12202e635a27SChris Mason 122139279cc3SChris Mason fail_close: 12226bccf3abSJeff Mahoney close_ctree(fs_info); 1223d5719762SChris Mason return err; 1224d5719762SChris Mason } 1225d5719762SChris Mason 12266bf13c0cSSage Weil int btrfs_sync_fs(struct super_block *sb, int wait) 1227d5719762SChris Mason { 1228d5719762SChris Mason struct btrfs_trans_handle *trans; 1229815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1230815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 1231df2ce34cSChris Mason 1232bc074524SJeff Mahoney trace_btrfs_sync_fs(fs_info, wait); 12331abe9b8aSliubo 1234d561c025SChris Mason if (!wait) { 1235815745cfSAl Viro filemap_flush(fs_info->btree_inode->i_mapping); 1236df2ce34cSChris Mason return 0; 1237d561c025SChris Mason } 1238771ed689SChris Mason 12396374e57aSChris Mason btrfs_wait_ordered_roots(fs_info, U64_MAX, 0, (u64)-1); 1240771ed689SChris Mason 1241d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 124260376ce4SJosef Bacik if (IS_ERR(trans)) { 1243354aa0fbSMiao Xie /* no transaction, don't bother */ 12446b5fe46dSDavid Sterba if (PTR_ERR(trans) == -ENOENT) { 12456b5fe46dSDavid Sterba /* 12466b5fe46dSDavid Sterba * Exit unless we have some pending changes 12476b5fe46dSDavid Sterba * that need to go through commit 12486b5fe46dSDavid Sterba */ 12496b5fe46dSDavid Sterba if (fs_info->pending_changes == 0) 1250bd7de2c9SJosef Bacik return 0; 1251a53f4f8eSQu Wenruo /* 1252a53f4f8eSQu Wenruo * A non-blocking test if the fs is frozen. We must not 1253a53f4f8eSQu Wenruo * start a new transaction here otherwise a deadlock 1254a53f4f8eSQu Wenruo * happens. The pending operations are delayed to the 1255a53f4f8eSQu Wenruo * next commit after thawing. 1256a53f4f8eSQu Wenruo */ 1257a7e3c5f2SRakesh Pandit if (sb_start_write_trylock(sb)) 1258a7e3c5f2SRakesh Pandit sb_end_write(sb); 1259a53f4f8eSQu Wenruo else 1260a53f4f8eSQu Wenruo return 0; 12616b5fe46dSDavid Sterba trans = btrfs_start_transaction(root, 0); 126260376ce4SJosef Bacik } 126398bd5c54SDavid Sterba if (IS_ERR(trans)) 126498bd5c54SDavid Sterba return PTR_ERR(trans); 12656b5fe46dSDavid Sterba } 12663a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 1267d5719762SChris Mason } 1268d5719762SChris Mason 126934c80b1dSAl Viro static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1270a9572a15SEric Paris { 1271815745cfSAl Viro struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 12720f628c63SDavid Sterba const char *compress_type; 1273a9572a15SEric Paris 12743cdde224SJeff Mahoney if (btrfs_test_opt(info, DEGRADED)) 1275a9572a15SEric Paris seq_puts(seq, ",degraded"); 12763cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATASUM)) 1277a9572a15SEric Paris seq_puts(seq, ",nodatasum"); 12783cdde224SJeff Mahoney if (btrfs_test_opt(info, NODATACOW)) 1279a9572a15SEric Paris seq_puts(seq, ",nodatacow"); 12803cdde224SJeff Mahoney if (btrfs_test_opt(info, NOBARRIER)) 1281a9572a15SEric Paris seq_puts(seq, ",nobarrier"); 128295ac567aSFilipe David Borba Manana if (info->max_inline != BTRFS_DEFAULT_MAX_INLINE) 1283c1c9ff7cSGeert Uytterhoeven seq_printf(seq, ",max_inline=%llu", info->max_inline); 1284a9572a15SEric Paris if (info->thread_pool_size != min_t(unsigned long, 1285a9572a15SEric Paris num_online_cpus() + 2, 8)) 1286*f7b885beSAnand Jain seq_printf(seq, ",thread_pool=%u", info->thread_pool_size); 12873cdde224SJeff Mahoney if (btrfs_test_opt(info, COMPRESS)) { 12880f628c63SDavid Sterba compress_type = btrfs_compress_type2str(info->compress_type); 12893cdde224SJeff Mahoney if (btrfs_test_opt(info, FORCE_COMPRESS)) 1290200da64eSTsutomu Itoh seq_printf(seq, ",compress-force=%s", compress_type); 1291200da64eSTsutomu Itoh else 1292200da64eSTsutomu Itoh seq_printf(seq, ",compress=%s", compress_type); 1293f51d2b59SDavid Sterba if (info->compress_level) 1294fa4d885aSAdam Borowski seq_printf(seq, ":%d", info->compress_level); 1295200da64eSTsutomu Itoh } 12963cdde224SJeff Mahoney if (btrfs_test_opt(info, NOSSD)) 1297c289811cSChris Mason seq_puts(seq, ",nossd"); 12983cdde224SJeff Mahoney if (btrfs_test_opt(info, SSD_SPREAD)) 1299451d7585SChris Mason seq_puts(seq, ",ssd_spread"); 13003cdde224SJeff Mahoney else if (btrfs_test_opt(info, SSD)) 1301a9572a15SEric Paris seq_puts(seq, ",ssd"); 13023cdde224SJeff Mahoney if (btrfs_test_opt(info, NOTREELOG)) 13036b65c5c6SSage Weil seq_puts(seq, ",notreelog"); 13043cdde224SJeff Mahoney if (btrfs_test_opt(info, NOLOGREPLAY)) 130596da0919SQu Wenruo seq_puts(seq, ",nologreplay"); 13063cdde224SJeff Mahoney if (btrfs_test_opt(info, FLUSHONCOMMIT)) 13076b65c5c6SSage Weil seq_puts(seq, ",flushoncommit"); 13083cdde224SJeff Mahoney if (btrfs_test_opt(info, DISCARD)) 130920a5239aSMatthew Wilcox seq_puts(seq, ",discard"); 13101751e8a6SLinus Torvalds if (!(info->sb->s_flags & SB_POSIXACL)) 1311a9572a15SEric Paris seq_puts(seq, ",noacl"); 13123cdde224SJeff Mahoney if (btrfs_test_opt(info, SPACE_CACHE)) 1313200da64eSTsutomu Itoh seq_puts(seq, ",space_cache"); 13143cdde224SJeff Mahoney else if (btrfs_test_opt(info, FREE_SPACE_TREE)) 131570f6d82eSOmar Sandoval seq_puts(seq, ",space_cache=v2"); 131673bc1876SJosef Bacik else 13178965593eSDavid Sterba seq_puts(seq, ",nospace_cache"); 13183cdde224SJeff Mahoney if (btrfs_test_opt(info, RESCAN_UUID_TREE)) 1319f420ee1eSStefan Behrens seq_puts(seq, ",rescan_uuid_tree"); 13203cdde224SJeff Mahoney if (btrfs_test_opt(info, CLEAR_CACHE)) 1321200da64eSTsutomu Itoh seq_puts(seq, ",clear_cache"); 13223cdde224SJeff Mahoney if (btrfs_test_opt(info, USER_SUBVOL_RM_ALLOWED)) 1323200da64eSTsutomu Itoh seq_puts(seq, ",user_subvol_rm_allowed"); 13243cdde224SJeff Mahoney if (btrfs_test_opt(info, ENOSPC_DEBUG)) 13250942caa3SDavid Sterba seq_puts(seq, ",enospc_debug"); 13263cdde224SJeff Mahoney if (btrfs_test_opt(info, AUTO_DEFRAG)) 13270942caa3SDavid Sterba seq_puts(seq, ",autodefrag"); 13283cdde224SJeff Mahoney if (btrfs_test_opt(info, INODE_MAP_CACHE)) 13290942caa3SDavid Sterba seq_puts(seq, ",inode_cache"); 13303cdde224SJeff Mahoney if (btrfs_test_opt(info, SKIP_BALANCE)) 13319555c6c1SIlya Dryomov seq_puts(seq, ",skip_balance"); 13328507d216SWang Shilong #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 13333cdde224SJeff Mahoney if (btrfs_test_opt(info, CHECK_INTEGRITY_INCLUDING_EXTENT_DATA)) 13348507d216SWang Shilong seq_puts(seq, ",check_int_data"); 13353cdde224SJeff Mahoney else if (btrfs_test_opt(info, CHECK_INTEGRITY)) 13368507d216SWang Shilong seq_puts(seq, ",check_int"); 13378507d216SWang Shilong if (info->check_integrity_print_mask) 13388507d216SWang Shilong seq_printf(seq, ",check_int_print_mask=%d", 13398507d216SWang Shilong info->check_integrity_print_mask); 13408507d216SWang Shilong #endif 13418507d216SWang Shilong if (info->metadata_ratio) 13428507d216SWang Shilong seq_printf(seq, ",metadata_ratio=%d", 13438507d216SWang Shilong info->metadata_ratio); 13443cdde224SJeff Mahoney if (btrfs_test_opt(info, PANIC_ON_FATAL_ERROR)) 13458c342930SJeff Mahoney seq_puts(seq, ",fatal_errors=panic"); 13468b87dc17SDavid Sterba if (info->commit_interval != BTRFS_DEFAULT_COMMIT_INTERVAL) 13478b87dc17SDavid Sterba seq_printf(seq, ",commit=%d", info->commit_interval); 1348d0bd4560SJosef Bacik #ifdef CONFIG_BTRFS_DEBUG 13493cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_DATA)) 1350d0bd4560SJosef Bacik seq_puts(seq, ",fragment=data"); 13513cdde224SJeff Mahoney if (btrfs_test_opt(info, FRAGMENT_METADATA)) 1352d0bd4560SJosef Bacik seq_puts(seq, ",fragment=metadata"); 1353d0bd4560SJosef Bacik #endif 1354fb592373SJosef Bacik if (btrfs_test_opt(info, REF_VERIFY)) 1355fb592373SJosef Bacik seq_puts(seq, ",ref_verify"); 1356c8d3fe02SOmar Sandoval seq_printf(seq, ",subvolid=%llu", 1357c8d3fe02SOmar Sandoval BTRFS_I(d_inode(dentry))->root->root_key.objectid); 1358c8d3fe02SOmar Sandoval seq_puts(seq, ",subvol="); 1359c8d3fe02SOmar Sandoval seq_dentry(seq, dentry, " \t\n\\"); 1360a9572a15SEric Paris return 0; 1361a9572a15SEric Paris } 1362a9572a15SEric Paris 1363a061fc8dSChris Mason static int btrfs_test_super(struct super_block *s, void *data) 13642e635a27SChris Mason { 1365815745cfSAl Viro struct btrfs_fs_info *p = data; 1366815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(s); 13674b82d6e4SYan 1368815745cfSAl Viro return fs_info->fs_devices == p->fs_devices; 13694b82d6e4SYan } 13704b82d6e4SYan 1371450ba0eaSJosef Bacik static int btrfs_set_super(struct super_block *s, void *data) 1372450ba0eaSJosef Bacik { 13736de1d09dSAl Viro int err = set_anon_super(s, data); 13746de1d09dSAl Viro if (!err) 1375450ba0eaSJosef Bacik s->s_fs_info = data; 13766de1d09dSAl Viro return err; 1377450ba0eaSJosef Bacik } 1378450ba0eaSJosef Bacik 1379830c4adbSJosef Bacik /* 1380f9d9ef62SDavid Sterba * subvolumes are identified by ino 256 1381f9d9ef62SDavid Sterba */ 1382f9d9ef62SDavid Sterba static inline int is_subvolume_inode(struct inode *inode) 1383f9d9ef62SDavid Sterba { 1384f9d9ef62SDavid Sterba if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 1385f9d9ef62SDavid Sterba return 1; 1386f9d9ef62SDavid Sterba return 0; 1387f9d9ef62SDavid Sterba } 1388f9d9ef62SDavid Sterba 1389bb289b7bSOmar Sandoval static struct dentry *mount_subvol(const char *subvol_name, u64 subvol_objectid, 13905bedc48aSDavid Sterba const char *device_name, struct vfsmount *mnt) 1391830c4adbSJosef Bacik { 1392830c4adbSJosef Bacik struct dentry *root; 1393fa330659SOmar Sandoval int ret; 1394830c4adbSJosef Bacik 139505dbe683SOmar Sandoval if (!subvol_name) { 139605dbe683SOmar Sandoval if (!subvol_objectid) { 139705dbe683SOmar Sandoval ret = get_default_subvol_objectid(btrfs_sb(mnt->mnt_sb), 139805dbe683SOmar Sandoval &subvol_objectid); 139905dbe683SOmar Sandoval if (ret) { 140005dbe683SOmar Sandoval root = ERR_PTR(ret); 140105dbe683SOmar Sandoval goto out; 140205dbe683SOmar Sandoval } 140305dbe683SOmar Sandoval } 140405dbe683SOmar Sandoval subvol_name = get_subvol_name_from_objectid(btrfs_sb(mnt->mnt_sb), 140505dbe683SOmar Sandoval subvol_objectid); 140605dbe683SOmar Sandoval if (IS_ERR(subvol_name)) { 140705dbe683SOmar Sandoval root = ERR_CAST(subvol_name); 140805dbe683SOmar Sandoval subvol_name = NULL; 140905dbe683SOmar Sandoval goto out; 141005dbe683SOmar Sandoval } 141105dbe683SOmar Sandoval 141205dbe683SOmar Sandoval } 141305dbe683SOmar Sandoval 1414ea441d11SAl Viro root = mount_subtree(mnt, subvol_name); 1415fa330659SOmar Sandoval /* mount_subtree() drops our reference on the vfsmount. */ 1416fa330659SOmar Sandoval mnt = NULL; 1417830c4adbSJosef Bacik 1418bb289b7bSOmar Sandoval if (!IS_ERR(root)) { 1419ea441d11SAl Viro struct super_block *s = root->d_sb; 1420ab8d0fc4SJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(s); 1421bb289b7bSOmar Sandoval struct inode *root_inode = d_inode(root); 1422bb289b7bSOmar Sandoval u64 root_objectid = BTRFS_I(root_inode)->root->root_key.objectid; 1423bb289b7bSOmar Sandoval 1424bb289b7bSOmar Sandoval ret = 0; 1425bb289b7bSOmar Sandoval if (!is_subvolume_inode(root_inode)) { 1426ab8d0fc4SJeff Mahoney btrfs_err(fs_info, "'%s' is not a valid subvolume", 1427bb289b7bSOmar Sandoval subvol_name); 1428bb289b7bSOmar Sandoval ret = -EINVAL; 1429bb289b7bSOmar Sandoval } 1430bb289b7bSOmar Sandoval if (subvol_objectid && root_objectid != subvol_objectid) { 143105dbe683SOmar Sandoval /* 143205dbe683SOmar Sandoval * This will also catch a race condition where a 143305dbe683SOmar Sandoval * subvolume which was passed by ID is renamed and 143405dbe683SOmar Sandoval * another subvolume is renamed over the old location. 143505dbe683SOmar Sandoval */ 1436ab8d0fc4SJeff Mahoney btrfs_err(fs_info, 1437ab8d0fc4SJeff Mahoney "subvol '%s' does not match subvolid %llu", 1438bb289b7bSOmar Sandoval subvol_name, subvol_objectid); 1439bb289b7bSOmar Sandoval ret = -EINVAL; 1440bb289b7bSOmar Sandoval } 1441bb289b7bSOmar Sandoval if (ret) { 1442ea441d11SAl Viro dput(root); 1443bb289b7bSOmar Sandoval root = ERR_PTR(ret); 1444ea441d11SAl Viro deactivate_locked_super(s); 1445bb289b7bSOmar Sandoval } 1446f9d9ef62SDavid Sterba } 1447f9d9ef62SDavid Sterba 1448fa330659SOmar Sandoval out: 1449fa330659SOmar Sandoval mntput(mnt); 1450fa330659SOmar Sandoval kfree(subvol_name); 1451830c4adbSJosef Bacik return root; 1452830c4adbSJosef Bacik } 1453450ba0eaSJosef Bacik 1454f667aef6SQu Wenruo static int parse_security_options(char *orig_opts, 1455f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1456f667aef6SQu Wenruo { 1457f667aef6SQu Wenruo char *secdata = NULL; 1458f667aef6SQu Wenruo int ret = 0; 1459f667aef6SQu Wenruo 1460f667aef6SQu Wenruo secdata = alloc_secdata(); 1461f667aef6SQu Wenruo if (!secdata) 1462f667aef6SQu Wenruo return -ENOMEM; 1463f667aef6SQu Wenruo ret = security_sb_copy_data(orig_opts, secdata); 1464f667aef6SQu Wenruo if (ret) { 1465f667aef6SQu Wenruo free_secdata(secdata); 1466f667aef6SQu Wenruo return ret; 1467f667aef6SQu Wenruo } 1468f667aef6SQu Wenruo ret = security_sb_parse_opts_str(secdata, sec_opts); 1469f667aef6SQu Wenruo free_secdata(secdata); 1470f667aef6SQu Wenruo return ret; 1471f667aef6SQu Wenruo } 1472f667aef6SQu Wenruo 1473f667aef6SQu Wenruo static int setup_security_options(struct btrfs_fs_info *fs_info, 1474f667aef6SQu Wenruo struct super_block *sb, 1475f667aef6SQu Wenruo struct security_mnt_opts *sec_opts) 1476f667aef6SQu Wenruo { 1477f667aef6SQu Wenruo int ret = 0; 1478f667aef6SQu Wenruo 1479f667aef6SQu Wenruo /* 1480f667aef6SQu Wenruo * Call security_sb_set_mnt_opts() to check whether new sec_opts 1481f667aef6SQu Wenruo * is valid. 1482f667aef6SQu Wenruo */ 1483f667aef6SQu Wenruo ret = security_sb_set_mnt_opts(sb, sec_opts, 0, NULL); 1484f667aef6SQu Wenruo if (ret) 1485f667aef6SQu Wenruo return ret; 1486f667aef6SQu Wenruo 1487a43bb39bSQu Wenruo #ifdef CONFIG_SECURITY 1488f667aef6SQu Wenruo if (!fs_info->security_opts.num_mnt_opts) { 1489f667aef6SQu Wenruo /* first time security setup, copy sec_opts to fs_info */ 1490f667aef6SQu Wenruo memcpy(&fs_info->security_opts, sec_opts, sizeof(*sec_opts)); 1491f667aef6SQu Wenruo } else { 1492f667aef6SQu Wenruo /* 1493180e4d47SLuis de Bethencourt * Since SELinux (the only one supporting security_mnt_opts) 1494180e4d47SLuis de Bethencourt * does NOT support changing context during remount/mount of 1495180e4d47SLuis de Bethencourt * the same sb, this must be the same or part of the same 1496180e4d47SLuis de Bethencourt * security options, just free it. 1497f667aef6SQu Wenruo */ 1498f667aef6SQu Wenruo security_free_mnt_opts(sec_opts); 1499f667aef6SQu Wenruo } 1500a43bb39bSQu Wenruo #endif 1501f667aef6SQu Wenruo return ret; 1502f667aef6SQu Wenruo } 1503f667aef6SQu Wenruo 1504312c89fbSMisono, Tomohiro /* 1505312c89fbSMisono, Tomohiro * Find a superblock for the given device / mount point. 1506312c89fbSMisono, Tomohiro * 1507312c89fbSMisono, Tomohiro * Note: This is based on mount_bdev from fs/super.c with a few additions 1508312c89fbSMisono, Tomohiro * for multiple device setup. Make sure to keep it in sync. 1509312c89fbSMisono, Tomohiro */ 151072fa39f5SMisono, Tomohiro static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, 151172fa39f5SMisono, Tomohiro int flags, const char *device_name, void *data) 151272fa39f5SMisono, Tomohiro { 151372fa39f5SMisono, Tomohiro struct block_device *bdev = NULL; 151472fa39f5SMisono, Tomohiro struct super_block *s; 151572fa39f5SMisono, Tomohiro struct btrfs_fs_devices *fs_devices = NULL; 151672fa39f5SMisono, Tomohiro struct btrfs_fs_info *fs_info = NULL; 151772fa39f5SMisono, Tomohiro struct security_mnt_opts new_sec_opts; 151872fa39f5SMisono, Tomohiro fmode_t mode = FMODE_READ; 151972fa39f5SMisono, Tomohiro int error = 0; 152072fa39f5SMisono, Tomohiro 152172fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY)) 152272fa39f5SMisono, Tomohiro mode |= FMODE_WRITE; 152372fa39f5SMisono, Tomohiro 152472fa39f5SMisono, Tomohiro error = btrfs_parse_early_options(data, mode, fs_type, 152572fa39f5SMisono, Tomohiro &fs_devices); 152672fa39f5SMisono, Tomohiro if (error) { 152772fa39f5SMisono, Tomohiro return ERR_PTR(error); 152872fa39f5SMisono, Tomohiro } 152972fa39f5SMisono, Tomohiro 153072fa39f5SMisono, Tomohiro security_init_mnt_opts(&new_sec_opts); 153172fa39f5SMisono, Tomohiro if (data) { 153272fa39f5SMisono, Tomohiro error = parse_security_options(data, &new_sec_opts); 153372fa39f5SMisono, Tomohiro if (error) 153472fa39f5SMisono, Tomohiro return ERR_PTR(error); 153572fa39f5SMisono, Tomohiro } 153672fa39f5SMisono, Tomohiro 153772fa39f5SMisono, Tomohiro error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices); 153872fa39f5SMisono, Tomohiro if (error) 153972fa39f5SMisono, Tomohiro goto error_sec_opts; 154072fa39f5SMisono, Tomohiro 154172fa39f5SMisono, Tomohiro /* 154272fa39f5SMisono, Tomohiro * Setup a dummy root and fs_info for test/set super. This is because 154372fa39f5SMisono, Tomohiro * we don't actually fill this stuff out until open_ctree, but we need 154472fa39f5SMisono, Tomohiro * it for searching for existing supers, so this lets us do that and 154572fa39f5SMisono, Tomohiro * then open_ctree will properly initialize everything later. 154672fa39f5SMisono, Tomohiro */ 1547a8fd1f71SJeff Mahoney fs_info = kvzalloc(sizeof(struct btrfs_fs_info), GFP_KERNEL); 154872fa39f5SMisono, Tomohiro if (!fs_info) { 154972fa39f5SMisono, Tomohiro error = -ENOMEM; 155072fa39f5SMisono, Tomohiro goto error_sec_opts; 155172fa39f5SMisono, Tomohiro } 155272fa39f5SMisono, Tomohiro 155372fa39f5SMisono, Tomohiro fs_info->fs_devices = fs_devices; 155472fa39f5SMisono, Tomohiro 155572fa39f5SMisono, Tomohiro fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 155672fa39f5SMisono, Tomohiro fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_KERNEL); 155772fa39f5SMisono, Tomohiro security_init_mnt_opts(&fs_info->security_opts); 155872fa39f5SMisono, Tomohiro if (!fs_info->super_copy || !fs_info->super_for_commit) { 155972fa39f5SMisono, Tomohiro error = -ENOMEM; 156072fa39f5SMisono, Tomohiro goto error_fs_info; 156172fa39f5SMisono, Tomohiro } 156272fa39f5SMisono, Tomohiro 156372fa39f5SMisono, Tomohiro error = btrfs_open_devices(fs_devices, mode, fs_type); 156472fa39f5SMisono, Tomohiro if (error) 156572fa39f5SMisono, Tomohiro goto error_fs_info; 156672fa39f5SMisono, Tomohiro 156772fa39f5SMisono, Tomohiro if (!(flags & SB_RDONLY) && fs_devices->rw_devices == 0) { 156872fa39f5SMisono, Tomohiro error = -EACCES; 156972fa39f5SMisono, Tomohiro goto error_close_devices; 157072fa39f5SMisono, Tomohiro } 157172fa39f5SMisono, Tomohiro 157272fa39f5SMisono, Tomohiro bdev = fs_devices->latest_bdev; 157372fa39f5SMisono, Tomohiro s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | SB_NOSEC, 157472fa39f5SMisono, Tomohiro fs_info); 157572fa39f5SMisono, Tomohiro if (IS_ERR(s)) { 157672fa39f5SMisono, Tomohiro error = PTR_ERR(s); 157772fa39f5SMisono, Tomohiro goto error_close_devices; 157872fa39f5SMisono, Tomohiro } 157972fa39f5SMisono, Tomohiro 158072fa39f5SMisono, Tomohiro if (s->s_root) { 158172fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 158272fa39f5SMisono, Tomohiro free_fs_info(fs_info); 158372fa39f5SMisono, Tomohiro if ((flags ^ s->s_flags) & SB_RDONLY) 158472fa39f5SMisono, Tomohiro error = -EBUSY; 158572fa39f5SMisono, Tomohiro } else { 158672fa39f5SMisono, Tomohiro snprintf(s->s_id, sizeof(s->s_id), "%pg", bdev); 158772fa39f5SMisono, Tomohiro btrfs_sb(s)->bdev_holder = fs_type; 158872fa39f5SMisono, Tomohiro error = btrfs_fill_super(s, fs_devices, data); 158972fa39f5SMisono, Tomohiro } 159072fa39f5SMisono, Tomohiro if (error) { 159172fa39f5SMisono, Tomohiro deactivate_locked_super(s); 159272fa39f5SMisono, Tomohiro goto error_sec_opts; 159372fa39f5SMisono, Tomohiro } 159472fa39f5SMisono, Tomohiro 159572fa39f5SMisono, Tomohiro fs_info = btrfs_sb(s); 159672fa39f5SMisono, Tomohiro error = setup_security_options(fs_info, s, &new_sec_opts); 159772fa39f5SMisono, Tomohiro if (error) { 159872fa39f5SMisono, Tomohiro deactivate_locked_super(s); 159972fa39f5SMisono, Tomohiro goto error_sec_opts; 160072fa39f5SMisono, Tomohiro } 160172fa39f5SMisono, Tomohiro 160272fa39f5SMisono, Tomohiro return dget(s->s_root); 160372fa39f5SMisono, Tomohiro 160472fa39f5SMisono, Tomohiro error_close_devices: 160572fa39f5SMisono, Tomohiro btrfs_close_devices(fs_devices); 160672fa39f5SMisono, Tomohiro error_fs_info: 160772fa39f5SMisono, Tomohiro free_fs_info(fs_info); 160872fa39f5SMisono, Tomohiro error_sec_opts: 160972fa39f5SMisono, Tomohiro security_free_mnt_opts(&new_sec_opts); 161072fa39f5SMisono, Tomohiro return ERR_PTR(error); 161172fa39f5SMisono, Tomohiro } 1612312c89fbSMisono, Tomohiro 1613edf24abeSChristoph Hellwig /* 1614312c89fbSMisono, Tomohiro * Mount function which is called by VFS layer. 1615edf24abeSChristoph Hellwig * 1616312c89fbSMisono, Tomohiro * In order to allow mounting a subvolume directly, btrfs uses mount_subtree() 1617312c89fbSMisono, Tomohiro * which needs vfsmount* of device's root (/). This means device's root has to 1618312c89fbSMisono, Tomohiro * be mounted internally in any case. 1619312c89fbSMisono, Tomohiro * 1620312c89fbSMisono, Tomohiro * Operation flow: 1621312c89fbSMisono, Tomohiro * 1. Parse subvol id related options for later use in mount_subvol(). 1622312c89fbSMisono, Tomohiro * 1623312c89fbSMisono, Tomohiro * 2. Mount device's root (/) by calling vfs_kern_mount(). 1624312c89fbSMisono, Tomohiro * 1625312c89fbSMisono, Tomohiro * NOTE: vfs_kern_mount() is used by VFS to call btrfs_mount() in the 1626312c89fbSMisono, Tomohiro * first place. In order to avoid calling btrfs_mount() again, we use 1627312c89fbSMisono, Tomohiro * different file_system_type which is not registered to VFS by 1628312c89fbSMisono, Tomohiro * register_filesystem() (btrfs_root_fs_type). As a result, 1629312c89fbSMisono, Tomohiro * btrfs_mount_root() is called. The return value will be used by 1630312c89fbSMisono, Tomohiro * mount_subtree() in mount_subvol(). 1631312c89fbSMisono, Tomohiro * 1632312c89fbSMisono, Tomohiro * 3. Call mount_subvol() to get the dentry of subvolume. Since there is 1633312c89fbSMisono, Tomohiro * "btrfs subvolume set-default", mount_subvol() is called always. 1634edf24abeSChristoph Hellwig */ 1635061dbc6bSAl Viro static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags, 1636306e16ceSDavid Sterba const char *device_name, void *data) 16374b82d6e4SYan { 1638312c89fbSMisono, Tomohiro struct vfsmount *mnt_root; 1639312c89fbSMisono, Tomohiro struct dentry *root; 164097288f2cSChristoph Hellwig fmode_t mode = FMODE_READ; 164173f73415SJosef Bacik char *subvol_name = NULL; 164273f73415SJosef Bacik u64 subvol_objectid = 0; 16434b82d6e4SYan int error = 0; 16444b82d6e4SYan 16451751e8a6SLinus Torvalds if (!(flags & SB_RDONLY)) 164697288f2cSChristoph Hellwig mode |= FMODE_WRITE; 164797288f2cSChristoph Hellwig 164878f6beacSMisono, Tomohiro error = btrfs_parse_subvol_options(data, mode, 1649d7407606SMisono, Tomohiro &subvol_name, &subvol_objectid); 1650f23c8af8SIlya Dryomov if (error) { 1651f23c8af8SIlya Dryomov kfree(subvol_name); 1652061dbc6bSAl Viro return ERR_PTR(error); 1653f23c8af8SIlya Dryomov } 1654edf24abeSChristoph Hellwig 1655312c89fbSMisono, Tomohiro /* mount device's root (/) */ 1656312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, flags, device_name, data); 1657312c89fbSMisono, Tomohiro if (PTR_ERR_OR_ZERO(mnt_root) == -EBUSY) { 1658312c89fbSMisono, Tomohiro if (flags & SB_RDONLY) { 1659312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1660312c89fbSMisono, Tomohiro flags & ~SB_RDONLY, device_name, data); 16614b82d6e4SYan } else { 1662312c89fbSMisono, Tomohiro mnt_root = vfs_kern_mount(&btrfs_root_fs_type, 1663312c89fbSMisono, Tomohiro flags | SB_RDONLY, device_name, data); 1664312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1665312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1666312c89fbSMisono, Tomohiro goto out; 1667f667aef6SQu Wenruo } 1668f667aef6SQu Wenruo 1669312c89fbSMisono, Tomohiro down_write(&mnt_root->mnt_sb->s_umount); 1670312c89fbSMisono, Tomohiro error = btrfs_remount(mnt_root->mnt_sb, &flags, NULL); 1671312c89fbSMisono, Tomohiro up_write(&mnt_root->mnt_sb->s_umount); 1672312c89fbSMisono, Tomohiro if (error < 0) { 1673312c89fbSMisono, Tomohiro root = ERR_PTR(error); 1674312c89fbSMisono, Tomohiro mntput(mnt_root); 1675312c89fbSMisono, Tomohiro goto out; 1676312c89fbSMisono, Tomohiro } 1677312c89fbSMisono, Tomohiro } 1678312c89fbSMisono, Tomohiro } 1679312c89fbSMisono, Tomohiro if (IS_ERR(mnt_root)) { 1680312c89fbSMisono, Tomohiro root = ERR_CAST(mnt_root); 1681312c89fbSMisono, Tomohiro goto out; 1682f667aef6SQu Wenruo } 16834b82d6e4SYan 1684312c89fbSMisono, Tomohiro /* mount_subvol() will free subvol_name and mnt_root */ 16855bedc48aSDavid Sterba root = mount_subvol(subvol_name, subvol_objectid, device_name, mnt_root); 16864b82d6e4SYan 1687312c89fbSMisono, Tomohiro out: 1688312c89fbSMisono, Tomohiro return root; 16894b82d6e4SYan } 16902e635a27SChris Mason 16910d2450abSSergei Trofimovich static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info, 1692*f7b885beSAnand Jain u32 new_pool_size, u32 old_pool_size) 16930d2450abSSergei Trofimovich { 16940d2450abSSergei Trofimovich if (new_pool_size == old_pool_size) 16950d2450abSSergei Trofimovich return; 16960d2450abSSergei Trofimovich 16970d2450abSSergei Trofimovich fs_info->thread_pool_size = new_pool_size; 16980d2450abSSergei Trofimovich 1699efe120a0SFrank Holton btrfs_info(fs_info, "resize thread pool %d -> %d", 17000d2450abSSergei Trofimovich old_pool_size, new_pool_size); 17010d2450abSSergei Trofimovich 17025cdc7ad3SQu Wenruo btrfs_workqueue_set_max(fs_info->workers, new_pool_size); 1703afe3d242SQu Wenruo btrfs_workqueue_set_max(fs_info->delalloc_workers, new_pool_size); 1704a8c93d4eSQu Wenruo btrfs_workqueue_set_max(fs_info->submit_workers, new_pool_size); 1705e66f0bb1SQu Wenruo btrfs_workqueue_set_max(fs_info->caching_workers, new_pool_size); 1706fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_workers, new_pool_size); 1707fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_workers, new_pool_size); 1708fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_meta_write_workers, 1709fccb5d86SQu Wenruo new_pool_size); 1710fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_write_workers, new_pool_size); 1711fccb5d86SQu Wenruo btrfs_workqueue_set_max(fs_info->endio_freespace_worker, new_pool_size); 17125b3bc44eSQu Wenruo btrfs_workqueue_set_max(fs_info->delayed_workers, new_pool_size); 1713736cfa15SQu Wenruo btrfs_workqueue_set_max(fs_info->readahead_workers, new_pool_size); 17140339ef2fSQu Wenruo btrfs_workqueue_set_max(fs_info->scrub_wr_completion_workers, 1715ff023aacSStefan Behrens new_pool_size); 17160d2450abSSergei Trofimovich } 17170d2450abSSergei Trofimovich 1718f42a34b2SMiao Xie static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info) 1719dc81cdc5SMiao Xie { 1720dc81cdc5SMiao Xie set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1721f42a34b2SMiao Xie } 1722dc81cdc5SMiao Xie 1723f42a34b2SMiao Xie static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info, 1724f42a34b2SMiao Xie unsigned long old_opts, int flags) 1725f42a34b2SMiao Xie { 1726dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1727dc81cdc5SMiao Xie (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || 17281751e8a6SLinus Torvalds (flags & SB_RDONLY))) { 1729dc81cdc5SMiao Xie /* wait for any defraggers to finish */ 1730dc81cdc5SMiao Xie wait_event(fs_info->transaction_wait, 1731dc81cdc5SMiao Xie (atomic_read(&fs_info->defrag_running) == 0)); 17321751e8a6SLinus Torvalds if (flags & SB_RDONLY) 1733dc81cdc5SMiao Xie sync_filesystem(fs_info->sb); 1734dc81cdc5SMiao Xie } 1735dc81cdc5SMiao Xie } 1736dc81cdc5SMiao Xie 1737dc81cdc5SMiao Xie static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info, 1738dc81cdc5SMiao Xie unsigned long old_opts) 1739dc81cdc5SMiao Xie { 1740dc81cdc5SMiao Xie /* 1741180e4d47SLuis de Bethencourt * We need to cleanup all defragable inodes if the autodefragment is 1742180e4d47SLuis de Bethencourt * close or the filesystem is read only. 1743dc81cdc5SMiao Xie */ 1744dc81cdc5SMiao Xie if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) && 1745bc98a42cSDavid Howells (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) || sb_rdonly(fs_info->sb))) { 1746dc81cdc5SMiao Xie btrfs_cleanup_defrag_inodes(fs_info); 1747dc81cdc5SMiao Xie } 1748dc81cdc5SMiao Xie 1749dc81cdc5SMiao Xie clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state); 1750dc81cdc5SMiao Xie } 1751dc81cdc5SMiao Xie 1752c146afadSYan Zheng static int btrfs_remount(struct super_block *sb, int *flags, char *data) 1753c146afadSYan Zheng { 1754815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 1755815745cfSAl Viro struct btrfs_root *root = fs_info->tree_root; 175649b25e05SJeff Mahoney unsigned old_flags = sb->s_flags; 175749b25e05SJeff Mahoney unsigned long old_opts = fs_info->mount_opt; 175849b25e05SJeff Mahoney unsigned long old_compress_type = fs_info->compress_type; 175949b25e05SJeff Mahoney u64 old_max_inline = fs_info->max_inline; 1760*f7b885beSAnand Jain u32 old_thread_pool_size = fs_info->thread_pool_size; 176149b25e05SJeff Mahoney unsigned int old_metadata_ratio = fs_info->metadata_ratio; 1762c146afadSYan Zheng int ret; 1763c146afadSYan Zheng 176402b9984dSTheodore Ts'o sync_filesystem(sb); 1765f42a34b2SMiao Xie btrfs_remount_prepare(fs_info); 1766dc81cdc5SMiao Xie 1767f667aef6SQu Wenruo if (data) { 1768f667aef6SQu Wenruo struct security_mnt_opts new_sec_opts; 1769f667aef6SQu Wenruo 1770f667aef6SQu Wenruo security_init_mnt_opts(&new_sec_opts); 1771f667aef6SQu Wenruo ret = parse_security_options(data, &new_sec_opts); 1772f667aef6SQu Wenruo if (ret) 1773f667aef6SQu Wenruo goto restore; 1774f667aef6SQu Wenruo ret = setup_security_options(fs_info, sb, 1775f667aef6SQu Wenruo &new_sec_opts); 1776f667aef6SQu Wenruo if (ret) { 1777f667aef6SQu Wenruo security_free_mnt_opts(&new_sec_opts); 1778f667aef6SQu Wenruo goto restore; 1779f667aef6SQu Wenruo } 1780f667aef6SQu Wenruo } 1781f667aef6SQu Wenruo 17822ff7e61eSJeff Mahoney ret = btrfs_parse_options(fs_info, data, *flags); 178349b25e05SJeff Mahoney if (ret) { 178449b25e05SJeff Mahoney ret = -EINVAL; 178549b25e05SJeff Mahoney goto restore; 178649b25e05SJeff Mahoney } 1787b288052eSChris Mason 1788f42a34b2SMiao Xie btrfs_remount_begin(fs_info, old_opts, *flags); 17890d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 17900d2450abSSergei Trofimovich fs_info->thread_pool_size, old_thread_pool_size); 17910d2450abSSergei Trofimovich 17921751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) 1793dc81cdc5SMiao Xie goto out; 1794c146afadSYan Zheng 17951751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 17968dabb742SStefan Behrens /* 17978dabb742SStefan Behrens * this also happens on 'umount -rf' or on shutdown, when 17988dabb742SStefan Behrens * the filesystem is busy. 17998dabb742SStefan Behrens */ 180021c7e756SMiao Xie cancel_work_sync(&fs_info->async_reclaim_work); 1801361c093dSStefan Behrens 1802361c093dSStefan Behrens /* wait for the uuid_scan task to finish */ 1803361c093dSStefan Behrens down(&fs_info->uuid_tree_rescan_sem); 1804361c093dSStefan Behrens /* avoid complains from lockdep et al. */ 1805361c093dSStefan Behrens up(&fs_info->uuid_tree_rescan_sem); 1806361c093dSStefan Behrens 18071751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 1808c146afadSYan Zheng 1809e44163e1SJeff Mahoney /* 18101751e8a6SLinus Torvalds * Setting SB_RDONLY will put the cleaner thread to 1811e44163e1SJeff Mahoney * sleep at the next loop if it's already active. 1812e44163e1SJeff Mahoney * If it's already asleep, we'll leave unused block 1813e44163e1SJeff Mahoney * groups on disk until we're mounted read-write again 1814e44163e1SJeff Mahoney * unless we clean them up here. 1815e44163e1SJeff Mahoney */ 1816e44163e1SJeff Mahoney btrfs_delete_unused_bgs(fs_info); 1817e44163e1SJeff Mahoney 18188dabb742SStefan Behrens btrfs_dev_replace_suspend_for_unmount(fs_info); 18198dabb742SStefan Behrens btrfs_scrub_cancel(fs_info); 1820061594efSMiao Xie btrfs_pause_balance(fs_info); 18218dabb742SStefan Behrens 18226bccf3abSJeff Mahoney ret = btrfs_commit_super(fs_info); 182349b25e05SJeff Mahoney if (ret) 182449b25e05SJeff Mahoney goto restore; 1825c146afadSYan Zheng } else { 18260b246afaSJeff Mahoney if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 18276ef3de9cSDavid Sterba btrfs_err(fs_info, 1828efe120a0SFrank Holton "Remounting read-write after error is not allowed"); 18296ef3de9cSDavid Sterba ret = -EINVAL; 18306ef3de9cSDavid Sterba goto restore; 18316ef3de9cSDavid Sterba } 18328a3db184SSergei Trofimovich if (fs_info->fs_devices->rw_devices == 0) { 183349b25e05SJeff Mahoney ret = -EACCES; 183449b25e05SJeff Mahoney goto restore; 18358a3db184SSergei Trofimovich } 18362b82032cSYan Zheng 18376528b99dSAnand Jain if (!btrfs_check_rw_degradable(fs_info, NULL)) { 1838efe120a0SFrank Holton btrfs_warn(fs_info, 1839efe120a0SFrank Holton "too many missing devices, writeable remount is not allowed"); 1840292fd7fcSStefan Behrens ret = -EACCES; 1841292fd7fcSStefan Behrens goto restore; 1842292fd7fcSStefan Behrens } 1843292fd7fcSStefan Behrens 18448a3db184SSergei Trofimovich if (btrfs_super_log_root(fs_info->super_copy) != 0) { 184549b25e05SJeff Mahoney ret = -EINVAL; 184649b25e05SJeff Mahoney goto restore; 18478a3db184SSergei Trofimovich } 1848c146afadSYan Zheng 1849815745cfSAl Viro ret = btrfs_cleanup_fs_roots(fs_info); 185049b25e05SJeff Mahoney if (ret) 185149b25e05SJeff Mahoney goto restore; 1852c146afadSYan Zheng 1853d68fc57bSYan, Zheng /* recover relocation */ 18545f316481SWang Shilong mutex_lock(&fs_info->cleaner_mutex); 1855d68fc57bSYan, Zheng ret = btrfs_recover_relocation(root); 18565f316481SWang Shilong mutex_unlock(&fs_info->cleaner_mutex); 185749b25e05SJeff Mahoney if (ret) 185849b25e05SJeff Mahoney goto restore; 1859c146afadSYan Zheng 18602b6ba629SIlya Dryomov ret = btrfs_resume_balance_async(fs_info); 18612b6ba629SIlya Dryomov if (ret) 18622b6ba629SIlya Dryomov goto restore; 18632b6ba629SIlya Dryomov 18648dabb742SStefan Behrens ret = btrfs_resume_dev_replace_async(fs_info); 18658dabb742SStefan Behrens if (ret) { 1866efe120a0SFrank Holton btrfs_warn(fs_info, "failed to resume dev_replace"); 18678dabb742SStefan Behrens goto restore; 18688dabb742SStefan Behrens } 186994aebfb2SJosef Bacik 18706c6b5a39SAleksa Sarai btrfs_qgroup_rescan_resume(fs_info); 18716c6b5a39SAleksa Sarai 187294aebfb2SJosef Bacik if (!fs_info->uuid_root) { 1873efe120a0SFrank Holton btrfs_info(fs_info, "creating UUID tree"); 187494aebfb2SJosef Bacik ret = btrfs_create_uuid_tree(fs_info); 187594aebfb2SJosef Bacik if (ret) { 18765d163e0eSJeff Mahoney btrfs_warn(fs_info, 18775d163e0eSJeff Mahoney "failed to create the UUID tree %d", 18785d163e0eSJeff Mahoney ret); 187994aebfb2SJosef Bacik goto restore; 188094aebfb2SJosef Bacik } 188194aebfb2SJosef Bacik } 18821751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 188390c711abSZygo Blaxell 1884afcdd129SJosef Bacik set_bit(BTRFS_FS_OPEN, &fs_info->flags); 1885c146afadSYan Zheng } 1886dc81cdc5SMiao Xie out: 18872c6a92b0SJustin Maggard wake_up_process(fs_info->transaction_kthread); 1888dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 1889c146afadSYan Zheng return 0; 189049b25e05SJeff Mahoney 189149b25e05SJeff Mahoney restore: 18921751e8a6SLinus Torvalds /* We've hit an error - don't reset SB_RDONLY */ 1893bc98a42cSDavid Howells if (sb_rdonly(sb)) 18941751e8a6SLinus Torvalds old_flags |= SB_RDONLY; 189549b25e05SJeff Mahoney sb->s_flags = old_flags; 189649b25e05SJeff Mahoney fs_info->mount_opt = old_opts; 189749b25e05SJeff Mahoney fs_info->compress_type = old_compress_type; 189849b25e05SJeff Mahoney fs_info->max_inline = old_max_inline; 18990d2450abSSergei Trofimovich btrfs_resize_thread_pool(fs_info, 19000d2450abSSergei Trofimovich old_thread_pool_size, fs_info->thread_pool_size); 190149b25e05SJeff Mahoney fs_info->metadata_ratio = old_metadata_ratio; 1902dc81cdc5SMiao Xie btrfs_remount_cleanup(fs_info, old_opts); 190349b25e05SJeff Mahoney return ret; 1904c146afadSYan Zheng } 1905c146afadSYan Zheng 1906bcd53741SArne Jansen /* Used to sort the devices by max_avail(descending sort) */ 1907bcd53741SArne Jansen static int btrfs_cmp_device_free_bytes(const void *dev_info1, 1908bcd53741SArne Jansen const void *dev_info2) 1909bcd53741SArne Jansen { 1910bcd53741SArne Jansen if (((struct btrfs_device_info *)dev_info1)->max_avail > 1911bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1912bcd53741SArne Jansen return -1; 1913bcd53741SArne Jansen else if (((struct btrfs_device_info *)dev_info1)->max_avail < 1914bcd53741SArne Jansen ((struct btrfs_device_info *)dev_info2)->max_avail) 1915bcd53741SArne Jansen return 1; 1916bcd53741SArne Jansen else 1917bcd53741SArne Jansen return 0; 1918bcd53741SArne Jansen } 1919bcd53741SArne Jansen 1920bcd53741SArne Jansen /* 1921bcd53741SArne Jansen * sort the devices by max_avail, in which max free extent size of each device 1922bcd53741SArne Jansen * is stored.(Descending Sort) 1923bcd53741SArne Jansen */ 1924bcd53741SArne Jansen static inline void btrfs_descending_sort_devices( 1925bcd53741SArne Jansen struct btrfs_device_info *devices, 1926bcd53741SArne Jansen size_t nr_devices) 1927bcd53741SArne Jansen { 1928bcd53741SArne Jansen sort(devices, nr_devices, sizeof(struct btrfs_device_info), 1929bcd53741SArne Jansen btrfs_cmp_device_free_bytes, NULL); 1930bcd53741SArne Jansen } 1931bcd53741SArne Jansen 19326d07bcecSMiao Xie /* 19336d07bcecSMiao Xie * The helper to calc the free space on the devices that can be used to store 19346d07bcecSMiao Xie * file data. 19356d07bcecSMiao Xie */ 19366bccf3abSJeff Mahoney static int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, 19376bccf3abSJeff Mahoney u64 *free_bytes) 19386d07bcecSMiao Xie { 19396d07bcecSMiao Xie struct btrfs_device_info *devices_info; 19406d07bcecSMiao Xie struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 19416d07bcecSMiao Xie struct btrfs_device *device; 19426d07bcecSMiao Xie u64 skip_space; 19436d07bcecSMiao Xie u64 type; 19446d07bcecSMiao Xie u64 avail_space; 19456d07bcecSMiao Xie u64 min_stripe_size; 194639fb26c3SMiao Xie int min_stripes = 1, num_stripes = 1; 19476d07bcecSMiao Xie int i = 0, nr_devices; 19486d07bcecSMiao Xie 19497e33fd99SJosef Bacik /* 195001327610SNicholas D Steeves * We aren't under the device list lock, so this is racy-ish, but good 19517e33fd99SJosef Bacik * enough for our purposes. 19527e33fd99SJosef Bacik */ 1953b772a86eSLi Zefan nr_devices = fs_info->fs_devices->open_devices; 19547e33fd99SJosef Bacik if (!nr_devices) { 19557e33fd99SJosef Bacik smp_mb(); 19567e33fd99SJosef Bacik nr_devices = fs_info->fs_devices->open_devices; 19577e33fd99SJosef Bacik ASSERT(nr_devices); 19587e33fd99SJosef Bacik if (!nr_devices) { 19597e33fd99SJosef Bacik *free_bytes = 0; 19607e33fd99SJosef Bacik return 0; 19617e33fd99SJosef Bacik } 19627e33fd99SJosef Bacik } 19636d07bcecSMiao Xie 1964d9b0d9baSDulshani Gunawardhana devices_info = kmalloc_array(nr_devices, sizeof(*devices_info), 19656a44517dSDavid Sterba GFP_KERNEL); 19666d07bcecSMiao Xie if (!devices_info) 19676d07bcecSMiao Xie return -ENOMEM; 19686d07bcecSMiao Xie 196901327610SNicholas D Steeves /* calc min stripe number for data space allocation */ 19701b86826dSJeff Mahoney type = btrfs_data_alloc_profile(fs_info); 197139fb26c3SMiao Xie if (type & BTRFS_BLOCK_GROUP_RAID0) { 19726d07bcecSMiao Xie min_stripes = 2; 197339fb26c3SMiao Xie num_stripes = nr_devices; 197439fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID1) { 19756d07bcecSMiao Xie min_stripes = 2; 197639fb26c3SMiao Xie num_stripes = 2; 197739fb26c3SMiao Xie } else if (type & BTRFS_BLOCK_GROUP_RAID10) { 19786d07bcecSMiao Xie min_stripes = 4; 197939fb26c3SMiao Xie num_stripes = 4; 198039fb26c3SMiao Xie } 19816d07bcecSMiao Xie 19826d07bcecSMiao Xie if (type & BTRFS_BLOCK_GROUP_DUP) 19836d07bcecSMiao Xie min_stripe_size = 2 * BTRFS_STRIPE_LEN; 19846d07bcecSMiao Xie else 19856d07bcecSMiao Xie min_stripe_size = BTRFS_STRIPE_LEN; 19866d07bcecSMiao Xie 19877e33fd99SJosef Bacik rcu_read_lock(); 19887e33fd99SJosef Bacik list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { 1989e12c9621SAnand Jain if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, 1990e12c9621SAnand Jain &device->dev_state) || 1991401e29c1SAnand Jain !device->bdev || 1992401e29c1SAnand Jain test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) 19936d07bcecSMiao Xie continue; 19946d07bcecSMiao Xie 19957e33fd99SJosef Bacik if (i >= nr_devices) 19967e33fd99SJosef Bacik break; 19977e33fd99SJosef Bacik 19986d07bcecSMiao Xie avail_space = device->total_bytes - device->bytes_used; 19996d07bcecSMiao Xie 20006d07bcecSMiao Xie /* align with stripe_len */ 2001f8c269d7SDavid Sterba avail_space = div_u64(avail_space, BTRFS_STRIPE_LEN); 20026d07bcecSMiao Xie avail_space *= BTRFS_STRIPE_LEN; 20036d07bcecSMiao Xie 20046d07bcecSMiao Xie /* 200501327610SNicholas D Steeves * In order to avoid overwriting the superblock on the drive, 20066d07bcecSMiao Xie * btrfs starts at an offset of at least 1MB when doing chunk 20076d07bcecSMiao Xie * allocation. 20086d07bcecSMiao Xie */ 2009ee22184bSByongho Lee skip_space = SZ_1M; 20106d07bcecSMiao Xie 20116d07bcecSMiao Xie /* 20126d07bcecSMiao Xie * we can use the free space in [0, skip_space - 1], subtract 20136d07bcecSMiao Xie * it from the total. 20146d07bcecSMiao Xie */ 20156d07bcecSMiao Xie if (avail_space && avail_space >= skip_space) 20166d07bcecSMiao Xie avail_space -= skip_space; 20176d07bcecSMiao Xie else 20186d07bcecSMiao Xie avail_space = 0; 20196d07bcecSMiao Xie 20206d07bcecSMiao Xie if (avail_space < min_stripe_size) 20216d07bcecSMiao Xie continue; 20226d07bcecSMiao Xie 20236d07bcecSMiao Xie devices_info[i].dev = device; 20246d07bcecSMiao Xie devices_info[i].max_avail = avail_space; 20256d07bcecSMiao Xie 20266d07bcecSMiao Xie i++; 20276d07bcecSMiao Xie } 20287e33fd99SJosef Bacik rcu_read_unlock(); 20296d07bcecSMiao Xie 20306d07bcecSMiao Xie nr_devices = i; 20316d07bcecSMiao Xie 20326d07bcecSMiao Xie btrfs_descending_sort_devices(devices_info, nr_devices); 20336d07bcecSMiao Xie 20346d07bcecSMiao Xie i = nr_devices - 1; 20356d07bcecSMiao Xie avail_space = 0; 20366d07bcecSMiao Xie while (nr_devices >= min_stripes) { 203739fb26c3SMiao Xie if (num_stripes > nr_devices) 203839fb26c3SMiao Xie num_stripes = nr_devices; 203939fb26c3SMiao Xie 20406d07bcecSMiao Xie if (devices_info[i].max_avail >= min_stripe_size) { 20416d07bcecSMiao Xie int j; 20426d07bcecSMiao Xie u64 alloc_size; 20436d07bcecSMiao Xie 204439fb26c3SMiao Xie avail_space += devices_info[i].max_avail * num_stripes; 20456d07bcecSMiao Xie alloc_size = devices_info[i].max_avail; 204639fb26c3SMiao Xie for (j = i + 1 - num_stripes; j <= i; j++) 20476d07bcecSMiao Xie devices_info[j].max_avail -= alloc_size; 20486d07bcecSMiao Xie } 20496d07bcecSMiao Xie i--; 20506d07bcecSMiao Xie nr_devices--; 20516d07bcecSMiao Xie } 20526d07bcecSMiao Xie 20536d07bcecSMiao Xie kfree(devices_info); 20546d07bcecSMiao Xie *free_bytes = avail_space; 20556d07bcecSMiao Xie return 0; 20566d07bcecSMiao Xie } 20576d07bcecSMiao Xie 2058ba7b6e62SDavid Sterba /* 2059ba7b6e62SDavid Sterba * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. 2060ba7b6e62SDavid Sterba * 2061ba7b6e62SDavid Sterba * If there's a redundant raid level at DATA block groups, use the respective 2062ba7b6e62SDavid Sterba * multiplier to scale the sizes. 2063ba7b6e62SDavid Sterba * 2064ba7b6e62SDavid Sterba * Unused device space usage is based on simulating the chunk allocator 20650d0c71b3SDavid Sterba * algorithm that respects the device sizes and order of allocations. This is 20660d0c71b3SDavid Sterba * a close approximation of the actual use but there are other factors that may 20670d0c71b3SDavid Sterba * change the result (like a new metadata chunk). 2068ba7b6e62SDavid Sterba * 2069ca8a51b3SDavid Sterba * If metadata is exhausted, f_bavail will be 0. 2070ba7b6e62SDavid Sterba */ 20718fd17795SChris Mason static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) 20728fd17795SChris Mason { 2073815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb); 2074815745cfSAl Viro struct btrfs_super_block *disk_super = fs_info->super_copy; 2075815745cfSAl Viro struct list_head *head = &fs_info->space_info; 2076bd4d1088SJosef Bacik struct btrfs_space_info *found; 2077bd4d1088SJosef Bacik u64 total_used = 0; 20786d07bcecSMiao Xie u64 total_free_data = 0; 2079ca8a51b3SDavid Sterba u64 total_free_meta = 0; 2080db94535dSChris Mason int bits = dentry->d_sb->s_blocksize_bits; 2081815745cfSAl Viro __be32 *fsid = (__be32 *)fs_info->fsid; 2082ba7b6e62SDavid Sterba unsigned factor = 1; 2083ba7b6e62SDavid Sterba struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; 20846d07bcecSMiao Xie int ret; 2085ca8a51b3SDavid Sterba u64 thresh = 0; 2086ae02d1bdSLuis de Bethencourt int mixed = 0; 20878fd17795SChris Mason 2088bd4d1088SJosef Bacik rcu_read_lock(); 208989a55897SJosef Bacik list_for_each_entry_rcu(found, head, list) { 20906d07bcecSMiao Xie if (found->flags & BTRFS_BLOCK_GROUP_DATA) { 2091ba7b6e62SDavid Sterba int i; 2092ba7b6e62SDavid Sterba 20936d07bcecSMiao Xie total_free_data += found->disk_total - found->disk_used; 20946d07bcecSMiao Xie total_free_data -= 20956d07bcecSMiao Xie btrfs_account_ro_block_groups_free_space(found); 2096ba7b6e62SDavid Sterba 2097ba7b6e62SDavid Sterba for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { 2098ba7b6e62SDavid Sterba if (!list_empty(&found->block_groups[i])) { 2099ba7b6e62SDavid Sterba switch (i) { 2100ba7b6e62SDavid Sterba case BTRFS_RAID_DUP: 2101ba7b6e62SDavid Sterba case BTRFS_RAID_RAID1: 2102ba7b6e62SDavid Sterba case BTRFS_RAID_RAID10: 2103ba7b6e62SDavid Sterba factor = 2; 2104ba7b6e62SDavid Sterba } 2105ba7b6e62SDavid Sterba } 2106ba7b6e62SDavid Sterba } 21076d07bcecSMiao Xie } 2108ae02d1bdSLuis de Bethencourt 2109ae02d1bdSLuis de Bethencourt /* 2110ae02d1bdSLuis de Bethencourt * Metadata in mixed block goup profiles are accounted in data 2111ae02d1bdSLuis de Bethencourt */ 2112ae02d1bdSLuis de Bethencourt if (!mixed && found->flags & BTRFS_BLOCK_GROUP_METADATA) { 2113ae02d1bdSLuis de Bethencourt if (found->flags & BTRFS_BLOCK_GROUP_DATA) 2114ae02d1bdSLuis de Bethencourt mixed = 1; 2115ae02d1bdSLuis de Bethencourt else 2116ae02d1bdSLuis de Bethencourt total_free_meta += found->disk_total - 2117ae02d1bdSLuis de Bethencourt found->disk_used; 2118ae02d1bdSLuis de Bethencourt } 21196d07bcecSMiao Xie 2120b742bb82SYan, Zheng total_used += found->disk_used; 212189a55897SJosef Bacik } 2122ba7b6e62SDavid Sterba 2123bd4d1088SJosef Bacik rcu_read_unlock(); 2124bd4d1088SJosef Bacik 2125ba7b6e62SDavid Sterba buf->f_blocks = div_u64(btrfs_super_total_bytes(disk_super), factor); 2126ba7b6e62SDavid Sterba buf->f_blocks >>= bits; 2127ba7b6e62SDavid Sterba buf->f_bfree = buf->f_blocks - (div_u64(total_used, factor) >> bits); 2128ba7b6e62SDavid Sterba 2129ba7b6e62SDavid Sterba /* Account global block reserve as used, it's in logical size already */ 2130ba7b6e62SDavid Sterba spin_lock(&block_rsv->lock); 213141b34accSLuis de Bethencourt /* Mixed block groups accounting is not byte-accurate, avoid overflow */ 213241b34accSLuis de Bethencourt if (buf->f_bfree >= block_rsv->size >> bits) 2133ba7b6e62SDavid Sterba buf->f_bfree -= block_rsv->size >> bits; 213441b34accSLuis de Bethencourt else 213541b34accSLuis de Bethencourt buf->f_bfree = 0; 2136ba7b6e62SDavid Sterba spin_unlock(&block_rsv->lock); 2137ba7b6e62SDavid Sterba 21380d95c1beSDavid Sterba buf->f_bavail = div_u64(total_free_data, factor); 21396bccf3abSJeff Mahoney ret = btrfs_calc_avail_data_space(fs_info, &total_free_data); 21407e33fd99SJosef Bacik if (ret) 21416d07bcecSMiao Xie return ret; 2142ba7b6e62SDavid Sterba buf->f_bavail += div_u64(total_free_data, factor); 21436d07bcecSMiao Xie buf->f_bavail = buf->f_bavail >> bits; 2144d397712bSChris Mason 2145ca8a51b3SDavid Sterba /* 2146ca8a51b3SDavid Sterba * We calculate the remaining metadata space minus global reserve. If 2147ca8a51b3SDavid Sterba * this is (supposedly) smaller than zero, there's no space. But this 2148ca8a51b3SDavid Sterba * does not hold in practice, the exhausted state happens where's still 2149ca8a51b3SDavid Sterba * some positive delta. So we apply some guesswork and compare the 2150ca8a51b3SDavid Sterba * delta to a 4M threshold. (Practically observed delta was ~2M.) 2151ca8a51b3SDavid Sterba * 2152ca8a51b3SDavid Sterba * We probably cannot calculate the exact threshold value because this 2153ca8a51b3SDavid Sterba * depends on the internal reservations requested by various 2154ca8a51b3SDavid Sterba * operations, so some operations that consume a few metadata will 2155ca8a51b3SDavid Sterba * succeed even if the Avail is zero. But this is better than the other 2156ca8a51b3SDavid Sterba * way around. 2157ca8a51b3SDavid Sterba */ 2158d4417e22SNikolay Borisov thresh = SZ_4M; 2159ca8a51b3SDavid Sterba 2160ae02d1bdSLuis de Bethencourt if (!mixed && total_free_meta - thresh < block_rsv->size) 2161ca8a51b3SDavid Sterba buf->f_bavail = 0; 2162ca8a51b3SDavid Sterba 2163ba7b6e62SDavid Sterba buf->f_type = BTRFS_SUPER_MAGIC; 2164ba7b6e62SDavid Sterba buf->f_bsize = dentry->d_sb->s_blocksize; 2165ba7b6e62SDavid Sterba buf->f_namelen = BTRFS_NAME_LEN; 2166ba7b6e62SDavid Sterba 21679d03632eSDavid Woodhouse /* We treat it as constant endianness (it doesn't matter _which_) 21689d03632eSDavid Woodhouse because we want the fsid to come out the same whether mounted 21699d03632eSDavid Woodhouse on a big-endian or little-endian host */ 21709d03632eSDavid Woodhouse buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); 21719d03632eSDavid Woodhouse buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); 217232d48fa1SDavid Woodhouse /* Mask in the root object ID too, to disambiguate subvols */ 21732b0143b5SDavid Howells buf->f_fsid.val[0] ^= BTRFS_I(d_inode(dentry))->root->objectid >> 32; 21742b0143b5SDavid Howells buf->f_fsid.val[1] ^= BTRFS_I(d_inode(dentry))->root->objectid; 217532d48fa1SDavid Woodhouse 21768fd17795SChris Mason return 0; 21778fd17795SChris Mason } 2178b5133862SChris Mason 2179aea52e19SAl Viro static void btrfs_kill_super(struct super_block *sb) 2180aea52e19SAl Viro { 2181815745cfSAl Viro struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2182aea52e19SAl Viro kill_anon_super(sb); 2183aea52e19SAl Viro free_fs_info(fs_info); 2184aea52e19SAl Viro } 2185aea52e19SAl Viro 21862e635a27SChris Mason static struct file_system_type btrfs_fs_type = { 21872e635a27SChris Mason .owner = THIS_MODULE, 21882e635a27SChris Mason .name = "btrfs", 2189061dbc6bSAl Viro .mount = btrfs_mount, 2190aea52e19SAl Viro .kill_sb = btrfs_kill_super, 2191f667aef6SQu Wenruo .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 21922e635a27SChris Mason }; 219372fa39f5SMisono, Tomohiro 219472fa39f5SMisono, Tomohiro static struct file_system_type btrfs_root_fs_type = { 219572fa39f5SMisono, Tomohiro .owner = THIS_MODULE, 219672fa39f5SMisono, Tomohiro .name = "btrfs", 219772fa39f5SMisono, Tomohiro .mount = btrfs_mount_root, 219872fa39f5SMisono, Tomohiro .kill_sb = btrfs_kill_super, 219972fa39f5SMisono, Tomohiro .fs_flags = FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA, 220072fa39f5SMisono, Tomohiro }; 220172fa39f5SMisono, Tomohiro 22027f78e035SEric W. Biederman MODULE_ALIAS_FS("btrfs"); 2203a9218f6bSChris Mason 2204d8620958STom Van Braeckel static int btrfs_control_open(struct inode *inode, struct file *file) 2205d8620958STom Van Braeckel { 2206d8620958STom Van Braeckel /* 2207d8620958STom Van Braeckel * The control file's private_data is used to hold the 2208d8620958STom Van Braeckel * transaction when it is started and is used to keep 2209d8620958STom Van Braeckel * track of whether a transaction is already in progress. 2210d8620958STom Van Braeckel */ 2211d8620958STom Van Braeckel file->private_data = NULL; 2212d8620958STom Van Braeckel return 0; 2213d8620958STom Van Braeckel } 2214d8620958STom Van Braeckel 2215d352ac68SChris Mason /* 2216d352ac68SChris Mason * used by btrfsctl to scan devices when no FS is mounted 2217d352ac68SChris Mason */ 22188a4b83ccSChris Mason static long btrfs_control_ioctl(struct file *file, unsigned int cmd, 22198a4b83ccSChris Mason unsigned long arg) 22208a4b83ccSChris Mason { 22218a4b83ccSChris Mason struct btrfs_ioctl_vol_args *vol; 22228a4b83ccSChris Mason struct btrfs_fs_devices *fs_devices; 2223c071fcfdSChris Mason int ret = -ENOTTY; 22248a4b83ccSChris Mason 2225e441d54dSChris Mason if (!capable(CAP_SYS_ADMIN)) 2226e441d54dSChris Mason return -EPERM; 2227e441d54dSChris Mason 2228dae7b665SLi Zefan vol = memdup_user((void __user *)arg, sizeof(*vol)); 2229dae7b665SLi Zefan if (IS_ERR(vol)) 2230dae7b665SLi Zefan return PTR_ERR(vol); 2231c071fcfdSChris Mason 22328a4b83ccSChris Mason switch (cmd) { 22338a4b83ccSChris Mason case BTRFS_IOC_SCAN_DEV: 223497288f2cSChristoph Hellwig ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2235312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 22368a4b83ccSChris Mason break; 223702db0844SJosef Bacik case BTRFS_IOC_DEVICES_READY: 223802db0844SJosef Bacik ret = btrfs_scan_one_device(vol->name, FMODE_READ, 2239312c89fbSMisono, Tomohiro &btrfs_root_fs_type, &fs_devices); 224002db0844SJosef Bacik if (ret) 224102db0844SJosef Bacik break; 224202db0844SJosef Bacik ret = !(fs_devices->num_devices == fs_devices->total_devices); 224302db0844SJosef Bacik break; 2244c5868f83SDavid Sterba case BTRFS_IOC_GET_SUPPORTED_FEATURES: 2245d5131b65SDavid Sterba ret = btrfs_ioctl_get_supported_features((void __user*)arg); 2246c5868f83SDavid Sterba break; 22478a4b83ccSChris Mason } 2248dae7b665SLi Zefan 22498a4b83ccSChris Mason kfree(vol); 2250f819d837SLinda Knippers return ret; 22518a4b83ccSChris Mason } 22528a4b83ccSChris Mason 22530176260fSLinus Torvalds static int btrfs_freeze(struct super_block *sb) 2254ed0dab6bSYan { 2255354aa0fbSMiao Xie struct btrfs_trans_handle *trans; 22560b246afaSJeff Mahoney struct btrfs_fs_info *fs_info = btrfs_sb(sb); 22570b246afaSJeff Mahoney struct btrfs_root *root = fs_info->tree_root; 2258354aa0fbSMiao Xie 2259fac03c8dSDavid Sterba set_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22609e7cc91aSWang Xiaoguang /* 22619e7cc91aSWang Xiaoguang * We don't need a barrier here, we'll wait for any transaction that 22629e7cc91aSWang Xiaoguang * could be in progress on other threads (and do delayed iputs that 22639e7cc91aSWang Xiaoguang * we want to avoid on a frozen filesystem), or do the commit 22649e7cc91aSWang Xiaoguang * ourselves. 22659e7cc91aSWang Xiaoguang */ 2266d4edf39bSMiao Xie trans = btrfs_attach_transaction_barrier(root); 2267354aa0fbSMiao Xie if (IS_ERR(trans)) { 2268354aa0fbSMiao Xie /* no transaction, don't bother */ 2269354aa0fbSMiao Xie if (PTR_ERR(trans) == -ENOENT) 22700176260fSLinus Torvalds return 0; 2271354aa0fbSMiao Xie return PTR_ERR(trans); 2272354aa0fbSMiao Xie } 22733a45bb20SJeff Mahoney return btrfs_commit_transaction(trans); 2274ed0dab6bSYan } 2275ed0dab6bSYan 22769e7cc91aSWang Xiaoguang static int btrfs_unfreeze(struct super_block *sb) 22779e7cc91aSWang Xiaoguang { 2278fac03c8dSDavid Sterba struct btrfs_fs_info *fs_info = btrfs_sb(sb); 2279fac03c8dSDavid Sterba 2280fac03c8dSDavid Sterba clear_bit(BTRFS_FS_FROZEN, &fs_info->flags); 22819e7cc91aSWang Xiaoguang return 0; 22829e7cc91aSWang Xiaoguang } 22839e7cc91aSWang Xiaoguang 22849c5085c1SJosef Bacik static int btrfs_show_devname(struct seq_file *m, struct dentry *root) 22859c5085c1SJosef Bacik { 22869c5085c1SJosef Bacik struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb); 22879c5085c1SJosef Bacik struct btrfs_fs_devices *cur_devices; 22889c5085c1SJosef Bacik struct btrfs_device *dev, *first_dev = NULL; 22899c5085c1SJosef Bacik struct list_head *head; 22909c5085c1SJosef Bacik struct rcu_string *name; 22919c5085c1SJosef Bacik 22929c5085c1SJosef Bacik mutex_lock(&fs_info->fs_devices->device_list_mutex); 22939c5085c1SJosef Bacik cur_devices = fs_info->fs_devices; 22949c5085c1SJosef Bacik while (cur_devices) { 22959c5085c1SJosef Bacik head = &cur_devices->devices; 22969c5085c1SJosef Bacik list_for_each_entry(dev, head, dev_list) { 2297e6e674bdSAnand Jain if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state)) 2298aa9ddcd4SJosef Bacik continue; 22990aeb8a6eSAnand Jain if (!dev->name) 23000aeb8a6eSAnand Jain continue; 23019c5085c1SJosef Bacik if (!first_dev || dev->devid < first_dev->devid) 23029c5085c1SJosef Bacik first_dev = dev; 23039c5085c1SJosef Bacik } 23049c5085c1SJosef Bacik cur_devices = cur_devices->seed; 23059c5085c1SJosef Bacik } 23069c5085c1SJosef Bacik 23079c5085c1SJosef Bacik if (first_dev) { 23089c5085c1SJosef Bacik rcu_read_lock(); 23099c5085c1SJosef Bacik name = rcu_dereference(first_dev->name); 23109c5085c1SJosef Bacik seq_escape(m, name->str, " \t\n\\"); 23119c5085c1SJosef Bacik rcu_read_unlock(); 23129c5085c1SJosef Bacik } else { 23139c5085c1SJosef Bacik WARN_ON(1); 23149c5085c1SJosef Bacik } 23159c5085c1SJosef Bacik mutex_unlock(&fs_info->fs_devices->device_list_mutex); 23169c5085c1SJosef Bacik return 0; 23179c5085c1SJosef Bacik } 23189c5085c1SJosef Bacik 2319b87221deSAlexey Dobriyan static const struct super_operations btrfs_super_ops = { 232076dda93cSYan, Zheng .drop_inode = btrfs_drop_inode, 2321bd555975SAl Viro .evict_inode = btrfs_evict_inode, 2322e20d96d6SChris Mason .put_super = btrfs_put_super, 2323d5719762SChris Mason .sync_fs = btrfs_sync_fs, 2324a9572a15SEric Paris .show_options = btrfs_show_options, 23259c5085c1SJosef Bacik .show_devname = btrfs_show_devname, 23264730a4bcSChris Mason .write_inode = btrfs_write_inode, 23272c90e5d6SChris Mason .alloc_inode = btrfs_alloc_inode, 23282c90e5d6SChris Mason .destroy_inode = btrfs_destroy_inode, 23298fd17795SChris Mason .statfs = btrfs_statfs, 2330c146afadSYan Zheng .remount_fs = btrfs_remount, 23310176260fSLinus Torvalds .freeze_fs = btrfs_freeze, 23329e7cc91aSWang Xiaoguang .unfreeze_fs = btrfs_unfreeze, 2333e20d96d6SChris Mason }; 2334a9218f6bSChris Mason 2335a9218f6bSChris Mason static const struct file_operations btrfs_ctl_fops = { 2336d8620958STom Van Braeckel .open = btrfs_control_open, 2337a9218f6bSChris Mason .unlocked_ioctl = btrfs_control_ioctl, 2338a9218f6bSChris Mason .compat_ioctl = btrfs_control_ioctl, 2339a9218f6bSChris Mason .owner = THIS_MODULE, 23406038f373SArnd Bergmann .llseek = noop_llseek, 2341a9218f6bSChris Mason }; 2342a9218f6bSChris Mason 2343a9218f6bSChris Mason static struct miscdevice btrfs_misc = { 2344578454ffSKay Sievers .minor = BTRFS_MINOR, 2345a9218f6bSChris Mason .name = "btrfs-control", 2346a9218f6bSChris Mason .fops = &btrfs_ctl_fops 2347a9218f6bSChris Mason }; 2348a9218f6bSChris Mason 2349578454ffSKay Sievers MODULE_ALIAS_MISCDEV(BTRFS_MINOR); 2350578454ffSKay Sievers MODULE_ALIAS("devname:btrfs-control"); 2351578454ffSKay Sievers 2352f5c29bd9SLiu Bo static int __init btrfs_interface_init(void) 2353a9218f6bSChris Mason { 2354a9218f6bSChris Mason return misc_register(&btrfs_misc); 2355a9218f6bSChris Mason } 2356a9218f6bSChris Mason 2357b2950863SChristoph Hellwig static void btrfs_interface_exit(void) 2358a9218f6bSChris Mason { 2359f368ed60SGreg Kroah-Hartman misc_deregister(&btrfs_misc); 2360a9218f6bSChris Mason } 2361a9218f6bSChris Mason 2362f5c29bd9SLiu Bo static void __init btrfs_print_mod_info(void) 236385965600SDavid Sterba { 236462e85577SJeff Mahoney pr_info("Btrfs loaded, crc32c=%s" 236585965600SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG 236685965600SDavid Sterba ", debug=on" 236785965600SDavid Sterba #endif 236879556c3dSStefan Behrens #ifdef CONFIG_BTRFS_ASSERT 236979556c3dSStefan Behrens ", assert=on" 237079556c3dSStefan Behrens #endif 237185965600SDavid Sterba #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 237285965600SDavid Sterba ", integrity-checker=on" 237385965600SDavid Sterba #endif 2374fb592373SJosef Bacik #ifdef CONFIG_BTRFS_FS_REF_VERIFY 2375fb592373SJosef Bacik ", ref-verify=on" 2376fb592373SJosef Bacik #endif 23775f9e1059SJeff Mahoney "\n", 23785f9e1059SJeff Mahoney btrfs_crc32c_impl()); 237985965600SDavid Sterba } 238085965600SDavid Sterba 23812e635a27SChris Mason static int __init init_btrfs_fs(void) 23822e635a27SChris Mason { 23832c90e5d6SChris Mason int err; 238458176a96SJosef Bacik 238514a958e6SFilipe David Borba Manana err = btrfs_hash_init(); 238614a958e6SFilipe David Borba Manana if (err) 238714a958e6SFilipe David Borba Manana return err; 238814a958e6SFilipe David Borba Manana 238963541927SFilipe David Borba Manana btrfs_props_init(); 239063541927SFilipe David Borba Manana 239158176a96SJosef Bacik err = btrfs_init_sysfs(); 239258176a96SJosef Bacik if (err) 239314a958e6SFilipe David Borba Manana goto free_hash; 239458176a96SJosef Bacik 2395143bede5SJeff Mahoney btrfs_init_compress(); 2396d1310b2eSChris Mason 2397261507a0SLi Zefan err = btrfs_init_cachep(); 2398261507a0SLi Zefan if (err) 2399261507a0SLi Zefan goto free_compress; 2400261507a0SLi Zefan 2401d1310b2eSChris Mason err = extent_io_init(); 24022f4cbe64SWyatt Banks if (err) 24032f4cbe64SWyatt Banks goto free_cachep; 24042f4cbe64SWyatt Banks 2405d1310b2eSChris Mason err = extent_map_init(); 2406d1310b2eSChris Mason if (err) 2407d1310b2eSChris Mason goto free_extent_io; 2408d1310b2eSChris Mason 24096352b91dSMiao Xie err = ordered_data_init(); 24102f4cbe64SWyatt Banks if (err) 24112f4cbe64SWyatt Banks goto free_extent_map; 2412c8b97818SChris Mason 24136352b91dSMiao Xie err = btrfs_delayed_inode_init(); 24146352b91dSMiao Xie if (err) 24156352b91dSMiao Xie goto free_ordered_data; 24166352b91dSMiao Xie 24179247f317SMiao Xie err = btrfs_auto_defrag_init(); 241816cdcec7SMiao Xie if (err) 241916cdcec7SMiao Xie goto free_delayed_inode; 242016cdcec7SMiao Xie 242178a6184aSMiao Xie err = btrfs_delayed_ref_init(); 24229247f317SMiao Xie if (err) 24239247f317SMiao Xie goto free_auto_defrag; 24249247f317SMiao Xie 2425b9e9a6cbSWang Shilong err = btrfs_prelim_ref_init(); 2426b9e9a6cbSWang Shilong if (err) 2427af13b492SDavid Sterba goto free_delayed_ref; 2428b9e9a6cbSWang Shilong 242997eb6b69SDavid Sterba err = btrfs_end_io_wq_init(); 243078a6184aSMiao Xie if (err) 2431af13b492SDavid Sterba goto free_prelim_ref; 243278a6184aSMiao Xie 243397eb6b69SDavid Sterba err = btrfs_interface_init(); 243497eb6b69SDavid Sterba if (err) 243597eb6b69SDavid Sterba goto free_end_io_wq; 243697eb6b69SDavid Sterba 2437e565d4b9SJan Schmidt btrfs_init_lockdep(); 2438e565d4b9SJan Schmidt 24398ae1af3cSAnand Jain btrfs_print_mod_info(); 2440dc11dd5dSJosef Bacik 2441dc11dd5dSJosef Bacik err = btrfs_run_sanity_tests(); 2442dc11dd5dSJosef Bacik if (err) 2443dc11dd5dSJosef Bacik goto unregister_ioctl; 2444dc11dd5dSJosef Bacik 2445dc11dd5dSJosef Bacik err = register_filesystem(&btrfs_fs_type); 2446dc11dd5dSJosef Bacik if (err) 2447dc11dd5dSJosef Bacik goto unregister_ioctl; 244874255aa0SJosef Bacik 24492f4cbe64SWyatt Banks return 0; 24502f4cbe64SWyatt Banks 2451a9218f6bSChris Mason unregister_ioctl: 2452a9218f6bSChris Mason btrfs_interface_exit(); 245397eb6b69SDavid Sterba free_end_io_wq: 245497eb6b69SDavid Sterba btrfs_end_io_wq_exit(); 2455b9e9a6cbSWang Shilong free_prelim_ref: 2456b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 245778a6184aSMiao Xie free_delayed_ref: 245878a6184aSMiao Xie btrfs_delayed_ref_exit(); 24599247f317SMiao Xie free_auto_defrag: 24609247f317SMiao Xie btrfs_auto_defrag_exit(); 246116cdcec7SMiao Xie free_delayed_inode: 246216cdcec7SMiao Xie btrfs_delayed_inode_exit(); 24636352b91dSMiao Xie free_ordered_data: 24646352b91dSMiao Xie ordered_data_exit(); 24652f4cbe64SWyatt Banks free_extent_map: 24662f4cbe64SWyatt Banks extent_map_exit(); 2467d1310b2eSChris Mason free_extent_io: 2468d1310b2eSChris Mason extent_io_exit(); 24692f4cbe64SWyatt Banks free_cachep: 24702f4cbe64SWyatt Banks btrfs_destroy_cachep(); 2471261507a0SLi Zefan free_compress: 2472261507a0SLi Zefan btrfs_exit_compress(); 24732f4cbe64SWyatt Banks btrfs_exit_sysfs(); 247414a958e6SFilipe David Borba Manana free_hash: 247514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24762c90e5d6SChris Mason return err; 24772e635a27SChris Mason } 24782e635a27SChris Mason 24792e635a27SChris Mason static void __exit exit_btrfs_fs(void) 24802e635a27SChris Mason { 248139279cc3SChris Mason btrfs_destroy_cachep(); 248278a6184aSMiao Xie btrfs_delayed_ref_exit(); 24839247f317SMiao Xie btrfs_auto_defrag_exit(); 248416cdcec7SMiao Xie btrfs_delayed_inode_exit(); 2485b9e9a6cbSWang Shilong btrfs_prelim_ref_exit(); 24866352b91dSMiao Xie ordered_data_exit(); 2487a52d9a80SChris Mason extent_map_exit(); 2488d1310b2eSChris Mason extent_io_exit(); 2489a9218f6bSChris Mason btrfs_interface_exit(); 24905ed5f588SJosef Bacik btrfs_end_io_wq_exit(); 24912e635a27SChris Mason unregister_filesystem(&btrfs_fs_type); 249258176a96SJosef Bacik btrfs_exit_sysfs(); 24938a4b83ccSChris Mason btrfs_cleanup_fs_uuids(); 2494261507a0SLi Zefan btrfs_exit_compress(); 249514a958e6SFilipe David Borba Manana btrfs_hash_exit(); 24962e635a27SChris Mason } 24972e635a27SChris Mason 249860efa5ebSFilipe David Borba Manana late_initcall(init_btrfs_fs); 24992e635a27SChris Mason module_exit(exit_btrfs_fs) 25002e635a27SChris Mason 25012e635a27SChris Mason MODULE_LICENSE("GPL"); 2502