~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/btrfs/super.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2007 Oracle.  All rights reserved.
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU General Public
  6  * License v2 as published by the Free Software Foundation.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU General Public
 14  * License along with this program; if not, write to the
 15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 16  * Boston, MA 021110-1307, USA.
 17  */
 18 
 19 #include <linux/blkdev.h>
 20 #include <linux/module.h>
 21 #include <linux/buffer_head.h>
 22 #include <linux/fs.h>
 23 #include <linux/pagemap.h>
 24 #include <linux/highmem.h>
 25 #include <linux/time.h>
 26 #include <linux/init.h>
 27 #include <linux/seq_file.h>
 28 #include <linux/string.h>
 29 #include <linux/backing-dev.h>
 30 #include <linux/mount.h>
 31 #include <linux/mpage.h>
 32 #include <linux/swap.h>
 33 #include <linux/writeback.h>
 34 #include <linux/statfs.h>
 35 #include <linux/compat.h>
 36 #include <linux/parser.h>
 37 #include <linux/ctype.h>
 38 #include <linux/namei.h>
 39 #include <linux/miscdevice.h>
 40 #include <linux/magic.h>
 41 #include <linux/slab.h>
 42 #include <linux/cleancache.h>
 43 #include <linux/ratelimit.h>
 44 #include <linux/btrfs.h>
 45 #include "compat.h"
 46 #include "delayed-inode.h"
 47 #include "ctree.h"
 48 #include "disk-io.h"
 49 #include "transaction.h"
 50 #include "btrfs_inode.h"
 51 #include "print-tree.h"
 52 #include "xattr.h"
 53 #include "volumes.h"
 54 #include "version.h"
 55 #include "export.h"
 56 #include "compression.h"
 57 #include "rcu-string.h"
 58 #include "dev-replace.h"
 59 #include "free-space-cache.h"
 60 
 61 #define CREATE_TRACE_POINTS
 62 #include <trace/events/btrfs.h>
 63 
 64 static const struct super_operations btrfs_super_ops;
 65 static struct file_system_type btrfs_fs_type;
 66 
 67 static const char *btrfs_decode_error(int errno)
 68 {
 69         char *errstr = "unknown";
 70 
 71         switch (errno) {
 72         case -EIO:
 73                 errstr = "IO failure";
 74                 break;
 75         case -ENOMEM:
 76                 errstr = "Out of memory";
 77                 break;
 78         case -EROFS:
 79                 errstr = "Readonly filesystem";
 80                 break;
 81         case -EEXIST:
 82                 errstr = "Object already exists";
 83                 break;
 84         case -ENOSPC:
 85                 errstr = "No space left";
 86                 break;
 87         case -ENOENT:
 88                 errstr = "No such entry";
 89                 break;
 90         }
 91 
 92         return errstr;
 93 }
 94 
 95 static void save_error_info(struct btrfs_fs_info *fs_info)
 96 {
 97         /*
 98          * today we only save the error info into ram.  Long term we'll
 99          * also send it down to the disk
100          */
101         set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
102 }
103 
104 /* btrfs handle error by forcing the filesystem readonly */
105 static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
106 {
107         struct super_block *sb = fs_info->sb;
108 
109         if (sb->s_flags & MS_RDONLY)
110                 return;
111 
112         if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
113                 sb->s_flags |= MS_RDONLY;
114                 btrfs_info(fs_info, "forced readonly");
115                 /*
116                  * Note that a running device replace operation is not
117                  * canceled here although there is no way to update
118                  * the progress. It would add the risk of a deadlock,
119                  * therefore the canceling is ommited. The only penalty
120                  * is that some I/O remains active until the procedure
121                  * completes. The next time when the filesystem is
122                  * mounted writeable again, the device replace
123                  * operation continues.
124                  */
125         }
126 }
127 
128 #ifdef CONFIG_PRINTK
129 /*
130  * __btrfs_std_error decodes expected errors from the caller and
131  * invokes the approciate error response.
132  */
133 void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
134                        unsigned int line, int errno, const char *fmt, ...)
135 {
136         struct super_block *sb = fs_info->sb;
137         const char *errstr;
138 
139         /*
140          * Special case: if the error is EROFS, and we're already
141          * under MS_RDONLY, then it is safe here.
142          */
143         if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
144                 return;
145 
146         errstr = btrfs_decode_error(errno);
147         if (fmt) {
148                 struct va_format vaf;
149                 va_list args;
150 
151                 va_start(args, fmt);
152                 vaf.fmt = fmt;
153                 vaf.va = &args;
154 
155                 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s (%pV)\n",
156                         sb->s_id, function, line, errno, errstr, &vaf);
157                 va_end(args);
158         } else {
159                 printk(KERN_CRIT "BTRFS error (device %s) in %s:%d: errno=%d %s\n",
160                         sb->s_id, function, line, errno, errstr);
161         }
162 
163         /* Don't go through full error handling during mount */
164         save_error_info(fs_info);
165         if (sb->s_flags & MS_BORN)
166                 btrfs_handle_error(fs_info);
167 }
168 
169 static const char * const logtypes[] = {
170         "emergency",
171         "alert",
172         "critical",
173         "error",
174         "warning",
175         "notice",
176         "info",
177         "debug",
178 };
179 
180 void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
181 {
182         struct super_block *sb = fs_info->sb;
183         char lvl[4];
184         struct va_format vaf;
185         va_list args;
186         const char *type = logtypes[4];
187         int kern_level;
188 
189         va_start(args, fmt);
190 
191         kern_level = printk_get_level(fmt);
192         if (kern_level) {
193                 size_t size = printk_skip_level(fmt) - fmt;
194                 memcpy(lvl, fmt,  size);
195                 lvl[size] = '\0';
196                 fmt += size;
197                 type = logtypes[kern_level - ''];
198         } else
199                 *lvl = '\0';
200 
201         vaf.fmt = fmt;
202         vaf.va = &args;
203 
204         printk("%sBTRFS %s (device %s): %pV\n", lvl, type, sb->s_id, &vaf);
205 
206         va_end(args);
207 }
208 
209 #else
210 
211 void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
212                        unsigned int line, int errno, const char *fmt, ...)
213 {
214         struct super_block *sb = fs_info->sb;
215 
216         /*
217          * Special case: if the error is EROFS, and we're already
218          * under MS_RDONLY, then it is safe here.
219          */
220         if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
221                 return;
222 
223         /* Don't go through full error handling during mount */
224         if (sb->s_flags & MS_BORN) {
225                 save_error_info(fs_info);
226                 btrfs_handle_error(fs_info);
227         }
228 }
229 #endif
230 
231 /*
232  * We only mark the transaction aborted and then set the file system read-only.
233  * This will prevent new transactions from starting or trying to join this
234  * one.
235  *
236  * This means that error recovery at the call site is limited to freeing
237  * any local memory allocations and passing the error code up without
238  * further cleanup. The transaction should complete as it normally would
239  * in the call path but will return -EIO.
240  *
241  * We'll complete the cleanup in btrfs_end_transaction and
242  * btrfs_commit_transaction.
243  */
244 void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
245                                struct btrfs_root *root, const char *function,
246                                unsigned int line, int errno)
247 {
248         /*
249          * Report first abort since mount
250          */
251         if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED,
252                                 &root->fs_info->fs_state)) {
253                 WARN(1, KERN_DEBUG "btrfs: Transaction aborted (error %d)\n",
254                                 errno);
255         }
256         trans->aborted = errno;
257         /* Nothing used. The other threads that have joined this
258          * transaction may be able to continue. */
259         if (!trans->blocks_used) {
260                 const char *errstr;
261 
262                 errstr = btrfs_decode_error(errno);
263                 btrfs_warn(root->fs_info,
264                            "%s:%d: Aborting unused transaction(%s).",
265                            function, line, errstr);
266                 return;
267         }
268         ACCESS_ONCE(trans->transaction->aborted) = errno;
269         __btrfs_std_error(root->fs_info, function, line, errno, NULL);
270 }
271 /*
272  * __btrfs_panic decodes unexpected, fatal errors from the caller,
273  * issues an alert, and either panics or BUGs, depending on mount options.
274  */
275 void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
276                    unsigned int line, int errno, const char *fmt, ...)
277 {
278         char *s_id = "<unknown>";
279         const char *errstr;
280         struct va_format vaf = { .fmt = fmt };
281         va_list args;
282 
283         if (fs_info)
284                 s_id = fs_info->sb->s_id;
285 
286         va_start(args, fmt);
287         vaf.va = &args;
288 
289         errstr = btrfs_decode_error(errno);
290         if (fs_info && (fs_info->mount_opt & BTRFS_MOUNT_PANIC_ON_FATAL_ERROR))
291                 panic(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
292                         s_id, function, line, &vaf, errno, errstr);
293 
294         printk(KERN_CRIT "BTRFS panic (device %s) in %s:%d: %pV (errno=%d %s)\n",
295                s_id, function, line, &vaf, errno, errstr);
296         va_end(args);
297         /* Caller calls BUG() */
298 }
299 
300 static void btrfs_put_super(struct super_block *sb)
301 {
302         (void)close_ctree(btrfs_sb(sb)->tree_root);
303         /* FIXME: need to fix VFS to return error? */
304         /* AV: return it _where_?  ->put_super() can be triggered by any number
305          * of async events, up to and including delivery of SIGKILL to the
306          * last process that kept it busy.  Or segfault in the aforementioned
307          * process...  Whom would you report that to?
308          */
309 }
310 
311 enum {
312         Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum,
313         Opt_nodatacow, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, Opt_ssd,
314         Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, Opt_compress,
315         Opt_compress_type, Opt_compress_force, Opt_compress_force_type,
316         Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard,
317         Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed,
318         Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_inode_cache,
319         Opt_no_space_cache, Opt_recovery, Opt_skip_balance,
320         Opt_check_integrity, Opt_check_integrity_including_extent_data,
321         Opt_check_integrity_print_mask, Opt_fatal_errors,
322         Opt_err,
323 };
324 
325 static match_table_t tokens = {
326         {Opt_degraded, "degraded"},
327         {Opt_subvol, "subvol=%s"},
328         {Opt_subvolid, "subvolid=%d"},
329         {Opt_device, "device=%s"},
330         {Opt_nodatasum, "nodatasum"},
331         {Opt_nodatacow, "nodatacow"},
332         {Opt_nobarrier, "nobarrier"},
333         {Opt_max_inline, "max_inline=%s"},
334         {Opt_alloc_start, "alloc_start=%s"},
335         {Opt_thread_pool, "thread_pool=%d"},
336         {Opt_compress, "compress"},
337         {Opt_compress_type, "compress=%s"},
338         {Opt_compress_force, "compress-force"},
339         {Opt_compress_force_type, "compress-force=%s"},
340         {Opt_ssd, "ssd"},
341         {Opt_ssd_spread, "ssd_spread"},
342         {Opt_nossd, "nossd"},
343         {Opt_noacl, "noacl"},
344         {Opt_notreelog, "notreelog"},
345         {Opt_flushoncommit, "flushoncommit"},
346         {Opt_ratio, "metadata_ratio=%d"},
347         {Opt_discard, "discard"},
348         {Opt_space_cache, "space_cache"},
349         {Opt_clear_cache, "clear_cache"},
350         {Opt_user_subvol_rm_allowed, "user_subvol_rm_allowed"},
351         {Opt_enospc_debug, "enospc_debug"},
352         {Opt_subvolrootid, "subvolrootid=%d"},
353         {Opt_defrag, "autodefrag"},
354         {Opt_inode_cache, "inode_cache"},
355         {Opt_no_space_cache, "nospace_cache"},
356         {Opt_recovery, "recovery"},
357         {Opt_skip_balance, "skip_balance"},
358         {Opt_check_integrity, "check_int"},
359         {Opt_check_integrity_including_extent_data, "check_int_data"},
360         {Opt_check_integrity_print_mask, "check_int_print_mask=%d"},
361         {Opt_fatal_errors, "fatal_errors=%s"},
362         {Opt_err, NULL},
363 };
364 
365 /*
366  * Regular mount options parser.  Everything that is needed only when
367  * reading in a new superblock is parsed here.
368  * XXX JDM: This needs to be cleaned up for remount.
369  */
370 int btrfs_parse_options(struct btrfs_root *root, char *options)
371 {
372         struct btrfs_fs_info *info = root->fs_info;
373         substring_t args[MAX_OPT_ARGS];
374         char *p, *num, *orig = NULL;
375         u64 cache_gen;
376         int intarg;
377         int ret = 0;
378         char *compress_type;
379         bool compress_force = false;
380 
381         cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
382         if (cache_gen)
383                 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
384 
385         if (!options)
386                 goto out;
387 
388         /*
389          * strsep changes the string, duplicate it because parse_options
390          * gets called twice
391          */
392         options = kstrdup(options, GFP_NOFS);
393         if (!options)
394                 return -ENOMEM;
395 
396         orig = options;
397 
398         while ((p = strsep(&options, ",")) != NULL) {
399                 int token;
400                 if (!*p)
401                         continue;
402 
403                 token = match_token(p, tokens, args);
404                 switch (token) {
405                 case Opt_degraded:
406                         printk(KERN_INFO "btrfs: allowing degraded mounts\n");
407                         btrfs_set_opt(info->mount_opt, DEGRADED);
408                         break;
409                 case Opt_subvol:
410                 case Opt_subvolid:
411                 case Opt_subvolrootid:
412                 case Opt_device:
413                         /*
414                          * These are parsed by btrfs_parse_early_options
415                          * and can be happily ignored here.
416                          */
417                         break;
418                 case Opt_nodatasum:
419                         printk(KERN_INFO "btrfs: setting nodatasum\n");
420                         btrfs_set_opt(info->mount_opt, NODATASUM);
421                         break;
422                 case Opt_nodatacow:
423                         if (!btrfs_test_opt(root, COMPRESS) ||
424                                 !btrfs_test_opt(root, FORCE_COMPRESS)) {
425                                         printk(KERN_INFO "btrfs: setting nodatacow, compression disabled\n");
426                         } else {
427                                 printk(KERN_INFO "btrfs: setting nodatacow\n");
428                         }
429                         info->compress_type = BTRFS_COMPRESS_NONE;
430                         btrfs_clear_opt(info->mount_opt, COMPRESS);
431                         btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
432                         btrfs_set_opt(info->mount_opt, NODATACOW);
433                         btrfs_set_opt(info->mount_opt, NODATASUM);
434                         break;
435                 case Opt_compress_force:
436                 case Opt_compress_force_type:
437                         compress_force = true;
438                         /* Fallthrough */
439                 case Opt_compress:
440                 case Opt_compress_type:
441                         if (token == Opt_compress ||
442                             token == Opt_compress_force ||
443                             strcmp(args[0].from, "zlib") == 0) {
444                                 compress_type = "zlib";
445                                 info->compress_type = BTRFS_COMPRESS_ZLIB;
446                                 btrfs_set_opt(info->mount_opt, COMPRESS);
447                                 btrfs_clear_opt(info->mount_opt, NODATACOW);
448                                 btrfs_clear_opt(info->mount_opt, NODATASUM);
449                         } else if (strcmp(args[0].from, "lzo") == 0) {
450                                 compress_type = "lzo";
451                                 info->compress_type = BTRFS_COMPRESS_LZO;
452                                 btrfs_set_opt(info->mount_opt, COMPRESS);
453                                 btrfs_clear_opt(info->mount_opt, NODATACOW);
454                                 btrfs_clear_opt(info->mount_opt, NODATASUM);
455                                 btrfs_set_fs_incompat(info, COMPRESS_LZO);
456                         } else if (strncmp(args[0].from, "no", 2) == 0) {
457                                 compress_type = "no";
458                                 info->compress_type = BTRFS_COMPRESS_NONE;
459                                 btrfs_clear_opt(info->mount_opt, COMPRESS);
460                                 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
461                                 compress_force = false;
462                         } else {
463                                 ret = -EINVAL;
464                                 goto out;
465                         }
466 
467                         if (compress_force) {
468                                 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
469                                 pr_info("btrfs: force %s compression\n",
470                                         compress_type);
471                         } else
472                                 pr_info("btrfs: use %s compression\n",
473                                         compress_type);
474                         break;
475                 case Opt_ssd:
476                         printk(KERN_INFO "btrfs: use ssd allocation scheme\n");
477                         btrfs_set_opt(info->mount_opt, SSD);
478                         break;
479                 case Opt_ssd_spread:
480                         printk(KERN_INFO "btrfs: use spread ssd "
481                                "allocation scheme\n");
482                         btrfs_set_opt(info->mount_opt, SSD);
483                         btrfs_set_opt(info->mount_opt, SSD_SPREAD);
484                         break;
485                 case Opt_nossd:
486                         printk(KERN_INFO "btrfs: not using ssd allocation "
487                                "scheme\n");
488                         btrfs_set_opt(info->mount_opt, NOSSD);
489                         btrfs_clear_opt(info->mount_opt, SSD);
490                         btrfs_clear_opt(info->mount_opt, SSD_SPREAD);
491                         break;
492                 case Opt_nobarrier:
493                         printk(KERN_INFO "btrfs: turning off barriers\n");
494                         btrfs_set_opt(info->mount_opt, NOBARRIER);
495                         break;
496                 case Opt_thread_pool:
497                         intarg = 0;
498                         match_int(&args[0], &intarg);
499                         if (intarg)
500                                 info->thread_pool_size = intarg;
501                         break;
502                 case Opt_max_inline:
503                         num = match_strdup(&args[0]);
504                         if (num) {
505                                 info->max_inline = memparse(num, NULL);
506                                 kfree(num);
507 
508                                 if (info->max_inline) {
509                                         info->max_inline = max_t(u64,
510                                                 info->max_inline,
511                                                 root->sectorsize);
512                                 }
513                                 printk(KERN_INFO "btrfs: max_inline at %llu\n",
514                                         (unsigned long long)info->max_inline);
515                         }
516                         break;
517                 case Opt_alloc_start:
518                         num = match_strdup(&args[0]);
519                         if (num) {
520                                 mutex_lock(&info->chunk_mutex);
521                                 info->alloc_start = memparse(num, NULL);
522                                 mutex_unlock(&info->chunk_mutex);
523                                 kfree(num);
524                                 printk(KERN_INFO
525                                         "btrfs: allocations start at %llu\n",
526                                         (unsigned long long)info->alloc_start);
527                         }
528                         break;
529                 case Opt_noacl:
530                         root->fs_info->sb->s_flags &= ~MS_POSIXACL;
531                         break;
532                 case Opt_notreelog:
533                         printk(KERN_INFO "btrfs: disabling tree log\n");
534                         btrfs_set_opt(info->mount_opt, NOTREELOG);
535                         break;
536                 case Opt_flushoncommit:
537                         printk(KERN_INFO "btrfs: turning on flush-on-commit\n");
538                         btrfs_set_opt(info->mount_opt, FLUSHONCOMMIT);
539                         break;
540                 case Opt_ratio:
541                         intarg = 0;
542                         match_int(&args[0], &intarg);
543                         if (intarg) {
544                                 info->metadata_ratio = intarg;
545                                 printk(KERN_INFO "btrfs: metadata ratio %d\n",
546                                        info->metadata_ratio);
547                         }
548                         break;
549                 case Opt_discard:
550                         btrfs_set_opt(info->mount_opt, DISCARD);
551                         break;
552                 case Opt_space_cache:
553                         btrfs_set_opt(info->mount_opt, SPACE_CACHE);
554                         break;
555                 case Opt_no_space_cache:
556                         printk(KERN_INFO "btrfs: disabling disk space caching\n");
557                         btrfs_clear_opt(info->mount_opt, SPACE_CACHE);
558                         break;
559                 case Opt_inode_cache:
560                         printk(KERN_INFO "btrfs: enabling inode map caching\n");
561                         btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE);
562                         break;
563                 case Opt_clear_cache:
564                         printk(KERN_INFO "btrfs: force clearing of disk cache\n");
565                         btrfs_set_opt(info->mount_opt, CLEAR_CACHE);
566                         break;
567                 case Opt_user_subvol_rm_allowed:
568                         btrfs_set_opt(info->mount_opt, USER_SUBVOL_RM_ALLOWED);
569                         break;
570                 case Opt_enospc_debug:
571                         btrfs_set_opt(info->mount_opt, ENOSPC_DEBUG);
572                         break;
573                 case Opt_defrag:
574                         printk(KERN_INFO "btrfs: enabling auto defrag\n");
575                         btrfs_set_opt(info->mount_opt, AUTO_DEFRAG);
576                         break;
577                 case Opt_recovery:
578                         printk(KERN_INFO "btrfs: enabling auto recovery\n");
579                         btrfs_set_opt(info->mount_opt, RECOVERY);
580                         break;
581                 case Opt_skip_balance:
582                         btrfs_set_opt(info->mount_opt, SKIP_BALANCE);
583                         break;
584 #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
585                 case Opt_check_integrity_including_extent_data:
586                         printk(KERN_INFO "btrfs: enabling check integrity"
587                                " including extent data\n");
588                         btrfs_set_opt(info->mount_opt,
589                                       CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
590                         btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
591                         break;
592                 case Opt_check_integrity:
593                         printk(KERN_INFO "btrfs: enabling check integrity\n");
594                         btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
595                         break;
596                 case Opt_check_integrity_print_mask:
597                         intarg = 0;
598                         match_int(&args[0], &intarg);
599                         if (intarg) {
600                                 info->check_integrity_print_mask = intarg;
601                                 printk(KERN_INFO "btrfs:"
602                                        " check_integrity_print_mask 0x%x\n",
603                                        info->check_integrity_print_mask);
604                         }
605                         break;
606 #else
607                 case Opt_check_integrity_including_extent_data:
608                 case Opt_check_integrity:
609                 case Opt_check_integrity_print_mask:
610                         printk(KERN_ERR "btrfs: support for check_integrity*"
611                                " not compiled in!\n");
612                         ret = -EINVAL;
613                         goto out;
614 #endif
615                 case Opt_fatal_errors:
616                         if (strcmp(args[0].from, "panic") == 0)
617                                 btrfs_set_opt(info->mount_opt,
618                                               PANIC_ON_FATAL_ERROR);
619                         else if (strcmp(args[0].from, "bug") == 0)
620                                 btrfs_clear_opt(info->mount_opt,
621                                               PANIC_ON_FATAL_ERROR);
622                         else {
623                                 ret = -EINVAL;
624                                 goto out;
625                         }
626                         break;
627                 case Opt_err:
628                         printk(KERN_INFO "btrfs: unrecognized mount option "
629                                "'%s'\n", p);
630                         ret = -EINVAL;
631                         goto out;
632                 default:
633                         break;
634                 }
635         }
636 out:
637         if (!ret && btrfs_test_opt(root, SPACE_CACHE))
638                 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
639         kfree(orig);
640         return ret;
641 }
642 
643 /*
644  * Parse mount options that are required early in the mount process.
645  *
646  * All other options will be parsed on much later in the mount process and
647  * only when we need to allocate a new super block.
648  */
649 static int btrfs_parse_early_options(const char *options, fmode_t flags,
650                 void *holder, char **subvol_name, u64 *subvol_objectid,
651                 struct btrfs_fs_devices **fs_devices)
652 {
653         substring_t args[MAX_OPT_ARGS];
654         char *device_name, *opts, *orig, *p;
655         int error = 0;
656         int intarg;
657 
658         if (!options)
659                 return 0;
660 
661         /*
662          * strsep changes the string, duplicate it because parse_options
663          * gets called twice
664          */
665         opts = kstrdup(options, GFP_KERNEL);
666         if (!opts)
667                 return -ENOMEM;
668         orig = opts;
669 
670         while ((p = strsep(&opts, ",")) != NULL) {
671                 int token;
672                 if (!*p)
673                         continue;
674 
675                 token = match_token(p, tokens, args);
676                 switch (token) {
677                 case Opt_subvol:
678                         kfree(*subvol_name);
679                         *subvol_name = match_strdup(&args[0]);
680                         break;
681                 case Opt_subvolid:
682                         intarg = 0;
683                         error = match_int(&args[0], &intarg);
684                         if (!error) {
685                                 /* we want the original fs_tree */
686                                 if (!intarg)
687                                         *subvol_objectid =
688                                                 BTRFS_FS_TREE_OBJECTID;
689                                 else
690                                         *subvol_objectid = intarg;
691                         }
692                         break;
693                 case Opt_subvolrootid:
694                         printk(KERN_WARNING
695                                 "btrfs: 'subvolrootid' mount option is deprecated and has no effect\n");
696                         break;
697                 case Opt_device:
698                         device_name = match_strdup(&args[0]);
699                         if (!device_name) {
700                                 error = -ENOMEM;
701                                 goto out;
702                         }
703                         error = btrfs_scan_one_device(device_name,
704                                         flags, holder, fs_devices);
705                         kfree(device_name);
706                         if (error)
707                                 goto out;
708                         break;
709                 default:
710                         break;
711                 }
712         }
713 
714 out:
715         kfree(orig);
716         return error;
717 }
718 
719 static struct dentry *get_default_root(struct super_block *sb,
720                                        u64 subvol_objectid)
721 {
722         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
723         struct btrfs_root *root = fs_info->tree_root;
724         struct btrfs_root *new_root;
725         struct btrfs_dir_item *di;
726         struct btrfs_path *path;
727         struct btrfs_key location;
728         struct inode *inode;
729         u64 dir_id;
730         int new = 0;
731 
732         /*
733          * We have a specific subvol we want to mount, just setup location and
734          * go look up the root.
735          */
736         if (subvol_objectid) {
737                 location.objectid = subvol_objectid;
738                 location.type = BTRFS_ROOT_ITEM_KEY;
739                 location.offset = (u64)-1;
740                 goto find_root;
741         }
742 
743         path = btrfs_alloc_path();
744         if (!path)
745                 return ERR_PTR(-ENOMEM);
746         path->leave_spinning = 1;
747 
748         /*
749          * Find the "default" dir item which points to the root item that we
750          * will mount by default if we haven't been given a specific subvolume
751          * to mount.
752          */
753         dir_id = btrfs_super_root_dir(fs_info->super_copy);
754         di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0);
755         if (IS_ERR(di)) {
756                 btrfs_free_path(path);
757                 return ERR_CAST(di);
758         }
759         if (!di) {
760                 /*
761                  * Ok the default dir item isn't there.  This is weird since
762                  * it's always been there, but don't freak out, just try and
763                  * mount to root most subvolume.
764                  */
765                 btrfs_free_path(path);
766                 dir_id = BTRFS_FIRST_FREE_OBJECTID;
767                 new_root = fs_info->fs_root;
768                 goto setup_root;
769         }
770 
771         btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
772         btrfs_free_path(path);
773 
774 find_root:
775         new_root = btrfs_read_fs_root_no_name(fs_info, &location);
776         if (IS_ERR(new_root))
777                 return ERR_CAST(new_root);
778 
779         if (btrfs_root_refs(&new_root->root_item) == 0)
780                 return ERR_PTR(-ENOENT);
781 
782         if (!(sb->s_flags & MS_RDONLY)) {
783                 int ret;
784                 down_read(&fs_info->cleanup_work_sem);
785                 ret = btrfs_orphan_cleanup(new_root);
786                 up_read(&fs_info->cleanup_work_sem);
787                 if (ret)
788                         return ERR_PTR(ret);
789         }
790 
791         dir_id = btrfs_root_dirid(&new_root->root_item);
792 setup_root:
793         location.objectid = dir_id;
794         location.type = BTRFS_INODE_ITEM_KEY;
795         location.offset = 0;
796 
797         inode = btrfs_iget(sb, &location, new_root, &new);
798         if (IS_ERR(inode))
799                 return ERR_CAST(inode);
800 
801         /*
802          * If we're just mounting the root most subvol put the inode and return
803          * a reference to the dentry.  We will have already gotten a reference
804          * to the inode in btrfs_fill_super so we're good to go.
805          */
806         if (!new && sb->s_root->d_inode == inode) {
807                 iput(inode);
808                 return dget(sb->s_root);
809         }
810 
811         return d_obtain_alias(inode);
812 }
813 
814 static int btrfs_fill_super(struct super_block *sb,
815                             struct btrfs_fs_devices *fs_devices,
816                             void *data, int silent)
817 {
818         struct inode *inode;
819         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
820         struct btrfs_key key;
821         int err;
822 
823         sb->s_maxbytes = MAX_LFS_FILESIZE;
824         sb->s_magic = BTRFS_SUPER_MAGIC;
825         sb->s_op = &btrfs_super_ops;
826         sb->s_d_op = &btrfs_dentry_operations;
827         sb->s_export_op = &btrfs_export_ops;
828         sb->s_xattr = btrfs_xattr_handlers;
829         sb->s_time_gran = 1;
830 #ifdef CONFIG_BTRFS_FS_POSIX_ACL
831         sb->s_flags |= MS_POSIXACL;
832 #endif
833         sb->s_flags |= MS_I_VERSION;
834         err = open_ctree(sb, fs_devices, (char *)data);
835         if (err) {
836                 printk("btrfs: open_ctree failed\n");
837                 return err;
838         }
839 
840         key.objectid = BTRFS_FIRST_FREE_OBJECTID;
841         key.type = BTRFS_INODE_ITEM_KEY;
842         key.offset = 0;
843         inode = btrfs_iget(sb, &key, fs_info->fs_root, NULL);
844         if (IS_ERR(inode)) {
845                 err = PTR_ERR(inode);
846                 goto fail_close;
847         }
848 
849         sb->s_root = d_make_root(inode);
850         if (!sb->s_root) {
851                 err = -ENOMEM;
852                 goto fail_close;
853         }
854 
855         save_mount_options(sb, data);
856         cleancache_init_fs(sb);
857         sb->s_flags |= MS_ACTIVE;
858         return 0;
859 
860 fail_close:
861         close_ctree(fs_info->tree_root);
862         return err;
863 }
864 
865 int btrfs_sync_fs(struct super_block *sb, int wait)
866 {
867         struct btrfs_trans_handle *trans;
868         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
869         struct btrfs_root *root = fs_info->tree_root;
870 
871         trace_btrfs_sync_fs(wait);
872 
873         if (!wait) {
874                 filemap_flush(fs_info->btree_inode->i_mapping);
875                 return 0;
876         }
877 
878         btrfs_wait_ordered_extents(root, 1);
879 
880         trans = btrfs_attach_transaction_barrier(root);
881         if (IS_ERR(trans)) {
882                 /* no transaction, don't bother */
883                 if (PTR_ERR(trans) == -ENOENT)
884                         return 0;
885                 return PTR_ERR(trans);
886         }
887         return btrfs_commit_transaction(trans, root);
888 }
889 
890 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
891 {
892         struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
893         struct btrfs_root *root = info->tree_root;
894         char *compress_type;
895 
896         if (btrfs_test_opt(root, DEGRADED))
897                 seq_puts(seq, ",degraded");
898         if (btrfs_test_opt(root, NODATASUM))
899                 seq_puts(seq, ",nodatasum");
900         if (btrfs_test_opt(root, NODATACOW))
901                 seq_puts(seq, ",nodatacow");
902         if (btrfs_test_opt(root, NOBARRIER))
903                 seq_puts(seq, ",nobarrier");
904         if (info->max_inline != 8192 * 1024)
905                 seq_printf(seq, ",max_inline=%llu",
906                            (unsigned long long)info->max_inline);
907         if (info->alloc_start != 0)
908                 seq_printf(seq, ",alloc_start=%llu",
909                            (unsigned long long)info->alloc_start);
910         if (info->thread_pool_size !=  min_t(unsigned long,
911                                              num_online_cpus() + 2, 8))
912                 seq_printf(seq, ",thread_pool=%d", info->thread_pool_size);
913         if (btrfs_test_opt(root, COMPRESS)) {
914                 if (info->compress_type == BTRFS_COMPRESS_ZLIB)
915                         compress_type = "zlib";
916                 else
917                         compress_type = "lzo";
918                 if (btrfs_test_opt(root, FORCE_COMPRESS))
919                         seq_printf(seq, ",compress-force=%s", compress_type);
920                 else
921                         seq_printf(seq, ",compress=%s", compress_type);
922         }
923         if (btrfs_test_opt(root, NOSSD))
924                 seq_puts(seq, ",nossd");
925         if (btrfs_test_opt(root, SSD_SPREAD))
926                 seq_puts(seq, ",ssd_spread");
927         else if (btrfs_test_opt(root, SSD))
928                 seq_puts(seq, ",ssd");
929         if (btrfs_test_opt(root, NOTREELOG))
930                 seq_puts(seq, ",notreelog");
931         if (btrfs_test_opt(root, FLUSHONCOMMIT))
932                 seq_puts(seq, ",flushoncommit");
933         if (btrfs_test_opt(root, DISCARD))
934                 seq_puts(seq, ",discard");
935         if (!(root->fs_info->sb->s_flags & MS_POSIXACL))
936                 seq_puts(seq, ",noacl");
937         if (btrfs_test_opt(root, SPACE_CACHE))
938                 seq_puts(seq, ",space_cache");
939         else
940                 seq_puts(seq, ",nospace_cache");
941         if (btrfs_test_opt(root, CLEAR_CACHE))
942                 seq_puts(seq, ",clear_cache");
943         if (btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
944                 seq_puts(seq, ",user_subvol_rm_allowed");
945         if (btrfs_test_opt(root, ENOSPC_DEBUG))
946                 seq_puts(seq, ",enospc_debug");
947         if (btrfs_test_opt(root, AUTO_DEFRAG))
948                 seq_puts(seq, ",autodefrag");
949         if (btrfs_test_opt(root, INODE_MAP_CACHE))
950                 seq_puts(seq, ",inode_cache");
951         if (btrfs_test_opt(root, SKIP_BALANCE))
952                 seq_puts(seq, ",skip_balance");
953         if (btrfs_test_opt(root, PANIC_ON_FATAL_ERROR))
954                 seq_puts(seq, ",fatal_errors=panic");
955         return 0;
956 }
957 
958 static int btrfs_test_super(struct super_block *s, void *data)
959 {
960         struct btrfs_fs_info *p = data;
961         struct btrfs_fs_info *fs_info = btrfs_sb(s);
962 
963         return fs_info->fs_devices == p->fs_devices;
964 }
965 
966 static int btrfs_set_super(struct super_block *s, void *data)
967 {
968         int err = set_anon_super(s, data);
969         if (!err)
970                 s->s_fs_info = data;
971         return err;
972 }
973 
974 /*
975  * subvolumes are identified by ino 256
976  */
977 static inline int is_subvolume_inode(struct inode *inode)
978 {
979         if (inode && inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
980                 return 1;
981         return 0;
982 }
983 
984 /*
985  * This will strip out the subvol=%s argument for an argument string and add
986  * subvolid=0 to make sure we get the actual tree root for path walking to the
987  * subvol we want.
988  */
989 static char *setup_root_args(char *args)
990 {
991         unsigned len = strlen(args) + 2 + 1;
992         char *src, *dst, *buf;
993 
994         /*
995          * We need the same args as before, but with this substitution:
996          * s!subvol=[^,]+!subvolid=0!
997          *
998          * Since the replacement string is up to 2 bytes longer than the
999          * original, allocate strlen(args) + 2 + 1 bytes.
1000          */
1001 
1002         src = strstr(args, "subvol=");
1003         /* This shouldn't happen, but just in case.. */
1004         if (!src)
1005                 return NULL;
1006 
1007         buf = dst = kmalloc(len, GFP_NOFS);
1008         if (!buf)
1009                 return NULL;
1010 
1011         /*
1012          * If the subvol= arg is not at the start of the string,
1013          * copy whatever precedes it into buf.
1014          */
1015         if (src != args) {
1016                 *src++ = '\0';
1017                 strcpy(buf, args);
1018                 dst += strlen(args);
1019         }
1020 
1021         strcpy(dst, "subvolid=0");
1022         dst += strlen("subvolid=0");
1023 
1024         /*
1025          * If there is a "," after the original subvol=... string,
1026          * copy that suffix into our buffer.  Otherwise, we're done.
1027          */
1028         src = strchr(src, ',');
1029         if (src)
1030                 strcpy(dst, src);
1031 
1032         return buf;
1033 }
1034 
1035 static struct dentry *mount_subvol(const char *subvol_name, int flags,
1036                                    const char *device_name, char *data)
1037 {
1038         struct dentry *root;
1039         struct vfsmount *mnt;
1040         char *newargs;
1041 
1042         newargs = setup_root_args(data);
1043         if (!newargs)
1044                 return ERR_PTR(-ENOMEM);
1045         mnt = vfs_kern_mount(&btrfs_fs_type, flags, device_name,
1046                              newargs);
1047         kfree(newargs);
1048         if (IS_ERR(mnt))
1049                 return ERR_CAST(mnt);
1050 
1051         root = mount_subtree(mnt, subvol_name);
1052 
1053         if (!IS_ERR(root) && !is_subvolume_inode(root->d_inode)) {
1054                 struct super_block *s = root->d_sb;
1055                 dput(root);
1056                 root = ERR_PTR(-EINVAL);
1057                 deactivate_locked_super(s);
1058                 printk(KERN_ERR "btrfs: '%s' is not a valid subvolume\n",
1059                                 subvol_name);
1060         }
1061 
1062         return root;
1063 }
1064 
1065 /*
1066  * Find a superblock for the given device / mount point.
1067  *
1068  * Note:  This is based on get_sb_bdev from fs/super.c with a few additions
1069  *        for multiple device setup.  Make sure to keep it in sync.
1070  */
1071 static struct dentry *btrfs_mount(struct file_system_type *fs_type, int flags,
1072                 const char *device_name, void *data)
1073 {
1074         struct block_device *bdev = NULL;
1075         struct super_block *s;
1076         struct dentry *root;
1077         struct btrfs_fs_devices *fs_devices = NULL;
1078         struct btrfs_fs_info *fs_info = NULL;
1079         fmode_t mode = FMODE_READ;
1080         char *subvol_name = NULL;
1081         u64 subvol_objectid = 0;
1082         int error = 0;
1083 
1084         if (!(flags & MS_RDONLY))
1085                 mode |= FMODE_WRITE;
1086 
1087         error = btrfs_parse_early_options(data, mode, fs_type,
1088                                           &subvol_name, &subvol_objectid,
1089                                           &fs_devices);
1090         if (error) {
1091                 kfree(subvol_name);
1092                 return ERR_PTR(error);
1093         }
1094 
1095         if (subvol_name) {
1096                 root = mount_subvol(subvol_name, flags, device_name, data);
1097                 kfree(subvol_name);
1098                 return root;
1099         }
1100 
1101         error = btrfs_scan_one_device(device_name, mode, fs_type, &fs_devices);
1102         if (error)
1103                 return ERR_PTR(error);
1104 
1105         /*
1106          * Setup a dummy root and fs_info for test/set super.  This is because
1107          * we don't actually fill this stuff out until open_ctree, but we need
1108          * it for searching for existing supers, so this lets us do that and
1109          * then open_ctree will properly initialize everything later.
1110          */
1111         fs_info = kzalloc(sizeof(struct btrfs_fs_info), GFP_NOFS);
1112         if (!fs_info)
1113                 return ERR_PTR(-ENOMEM);
1114 
1115         fs_info->fs_devices = fs_devices;
1116 
1117         fs_info->super_copy = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1118         fs_info->super_for_commit = kzalloc(BTRFS_SUPER_INFO_SIZE, GFP_NOFS);
1119         if (!fs_info->super_copy || !fs_info->super_for_commit) {
1120                 error = -ENOMEM;
1121                 goto error_fs_info;
1122         }
1123 
1124         error = btrfs_open_devices(fs_devices, mode, fs_type);
1125         if (error)
1126                 goto error_fs_info;
1127 
1128         if (!(flags & MS_RDONLY) && fs_devices->rw_devices == 0) {
1129                 error = -EACCES;
1130                 goto error_close_devices;
1131         }
1132 
1133         bdev = fs_devices->latest_bdev;
1134         s = sget(fs_type, btrfs_test_super, btrfs_set_super, flags | MS_NOSEC,
1135                  fs_info);
1136         if (IS_ERR(s)) {
1137                 error = PTR_ERR(s);
1138                 goto error_close_devices;
1139         }
1140 
1141         if (s->s_root) {
1142                 btrfs_close_devices(fs_devices);
1143                 free_fs_info(fs_info);
1144                 if ((flags ^ s->s_flags) & MS_RDONLY)
1145                         error = -EBUSY;
1146         } else {
1147                 char b[BDEVNAME_SIZE];
1148 
1149                 strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
1150                 btrfs_sb(s)->bdev_holder = fs_type;
1151                 error = btrfs_fill_super(s, fs_devices, data,
1152                                          flags & MS_SILENT ? 1 : 0);
1153         }
1154 
1155         root = !error ? get_default_root(s, subvol_objectid) : ERR_PTR(error);
1156         if (IS_ERR(root))
1157                 deactivate_locked_super(s);
1158 
1159         return root;
1160 
1161 error_close_devices:
1162         btrfs_close_devices(fs_devices);
1163 error_fs_info:
1164         free_fs_info(fs_info);
1165         return ERR_PTR(error);
1166 }
1167 
1168 static void btrfs_set_max_workers(struct btrfs_workers *workers, int new_limit)
1169 {
1170         spin_lock_irq(&workers->lock);
1171         workers->max_workers = new_limit;
1172         spin_unlock_irq(&workers->lock);
1173 }
1174 
1175 static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
1176                                      int new_pool_size, int old_pool_size)
1177 {
1178         if (new_pool_size == old_pool_size)
1179                 return;
1180 
1181         fs_info->thread_pool_size = new_pool_size;
1182 
1183         printk(KERN_INFO "btrfs: resize thread pool %d -> %d\n",
1184                old_pool_size, new_pool_size);
1185 
1186         btrfs_set_max_workers(&fs_info->generic_worker, new_pool_size);
1187         btrfs_set_max_workers(&fs_info->workers, new_pool_size);
1188         btrfs_set_max_workers(&fs_info->delalloc_workers, new_pool_size);
1189         btrfs_set_max_workers(&fs_info->submit_workers, new_pool_size);
1190         btrfs_set_max_workers(&fs_info->caching_workers, new_pool_size);
1191         btrfs_set_max_workers(&fs_info->fixup_workers, new_pool_size);
1192         btrfs_set_max_workers(&fs_info->endio_workers, new_pool_size);
1193         btrfs_set_max_workers(&fs_info->endio_meta_workers, new_pool_size);
1194         btrfs_set_max_workers(&fs_info->endio_meta_write_workers, new_pool_size);
1195         btrfs_set_max_workers(&fs_info->endio_write_workers, new_pool_size);
1196         btrfs_set_max_workers(&fs_info->endio_freespace_worker, new_pool_size);
1197         btrfs_set_max_workers(&fs_info->delayed_workers, new_pool_size);
1198         btrfs_set_max_workers(&fs_info->readahead_workers, new_pool_size);
1199         btrfs_set_max_workers(&fs_info->scrub_wr_completion_workers,
1200                               new_pool_size);
1201 }
1202 
1203 static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info)
1204 {
1205         set_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1206 }
1207 
1208 static inline void btrfs_remount_begin(struct btrfs_fs_info *fs_info,
1209                                        unsigned long old_opts, int flags)
1210 {
1211         if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1212             (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1213              (flags & MS_RDONLY))) {
1214                 /* wait for any defraggers to finish */
1215                 wait_event(fs_info->transaction_wait,
1216                            (atomic_read(&fs_info->defrag_running) == 0));
1217                 if (flags & MS_RDONLY)
1218                         sync_filesystem(fs_info->sb);
1219         }
1220 }
1221 
1222 static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
1223                                          unsigned long old_opts)
1224 {
1225         /*
1226          * We need cleanup all defragable inodes if the autodefragment is
1227          * close or the fs is R/O.
1228          */
1229         if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
1230             (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
1231              (fs_info->sb->s_flags & MS_RDONLY))) {
1232                 btrfs_cleanup_defrag_inodes(fs_info);
1233         }
1234 
1235         clear_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state);
1236 }
1237 
1238 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1239 {
1240         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1241         struct btrfs_root *root = fs_info->tree_root;
1242         unsigned old_flags = sb->s_flags;
1243         unsigned long old_opts = fs_info->mount_opt;
1244         unsigned long old_compress_type = fs_info->compress_type;
1245         u64 old_max_inline = fs_info->max_inline;
1246         u64 old_alloc_start = fs_info->alloc_start;
1247         int old_thread_pool_size = fs_info->thread_pool_size;
1248         unsigned int old_metadata_ratio = fs_info->metadata_ratio;
1249         int ret;
1250 
1251         btrfs_remount_prepare(fs_info);
1252 
1253         ret = btrfs_parse_options(root, data);
1254         if (ret) {
1255                 ret = -EINVAL;
1256                 goto restore;
1257         }
1258 
1259         btrfs_remount_begin(fs_info, old_opts, *flags);
1260         btrfs_resize_thread_pool(fs_info,
1261                 fs_info->thread_pool_size, old_thread_pool_size);
1262 
1263         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1264                 goto out;
1265 
1266         if (*flags & MS_RDONLY) {
1267                 /*
1268                  * this also happens on 'umount -rf' or on shutdown, when
1269                  * the filesystem is busy.
1270                  */
1271                 sb->s_flags |= MS_RDONLY;
1272 
1273                 btrfs_dev_replace_suspend_for_unmount(fs_info);
1274                 btrfs_scrub_cancel(fs_info);
1275                 btrfs_pause_balance(fs_info);
1276 
1277                 ret = btrfs_commit_super(root);
1278                 if (ret)
1279                         goto restore;
1280         } else {
1281                 if (fs_info->fs_devices->rw_devices == 0) {
1282                         ret = -EACCES;
1283                         goto restore;
1284                 }
1285 
1286                 if (fs_info->fs_devices->missing_devices >
1287                      fs_info->num_tolerated_disk_barrier_failures &&
1288                     !(*flags & MS_RDONLY)) {
1289                         printk(KERN_WARNING
1290                                "Btrfs: too many missing devices, writeable remount is not allowed\n");
1291                         ret = -EACCES;
1292                         goto restore;
1293                 }
1294 
1295                 if (btrfs_super_log_root(fs_info->super_copy) != 0) {
1296                         ret = -EINVAL;
1297                         goto restore;
1298                 }
1299 
1300                 ret = btrfs_cleanup_fs_roots(fs_info);
1301                 if (ret)
1302                         goto restore;
1303 
1304                 /* recover relocation */
1305                 ret = btrfs_recover_relocation(root);
1306                 if (ret)
1307                         goto restore;
1308 
1309                 ret = btrfs_resume_balance_async(fs_info);
1310                 if (ret)
1311                         goto restore;
1312 
1313                 ret = btrfs_resume_dev_replace_async(fs_info);
1314                 if (ret) {
1315                         pr_warn("btrfs: failed to resume dev_replace\n");
1316                         goto restore;
1317                 }
1318                 sb->s_flags &= ~MS_RDONLY;
1319         }
1320 out:
1321         btrfs_remount_cleanup(fs_info, old_opts);
1322         return 0;
1323 
1324 restore:
1325         /* We've hit an error - don't reset MS_RDONLY */
1326         if (sb->s_flags & MS_RDONLY)
1327                 old_flags |= MS_RDONLY;
1328         sb->s_flags = old_flags;
1329         fs_info->mount_opt = old_opts;
1330         fs_info->compress_type = old_compress_type;
1331         fs_info->max_inline = old_max_inline;
1332         mutex_lock(&fs_info->chunk_mutex);
1333         fs_info->alloc_start = old_alloc_start;
1334         mutex_unlock(&fs_info->chunk_mutex);
1335         btrfs_resize_thread_pool(fs_info,
1336                 old_thread_pool_size, fs_info->thread_pool_size);
1337         fs_info->metadata_ratio = old_metadata_ratio;
1338         btrfs_remount_cleanup(fs_info, old_opts);
1339         return ret;
1340 }
1341 
1342 /* Used to sort the devices by max_avail(descending sort) */
1343 static int btrfs_cmp_device_free_bytes(const void *dev_info1,
1344                                        const void *dev_info2)
1345 {
1346         if (((struct btrfs_device_info *)dev_info1)->max_avail >
1347             ((struct btrfs_device_info *)dev_info2)->max_avail)
1348                 return -1;
1349         else if (((struct btrfs_device_info *)dev_info1)->max_avail <
1350                  ((struct btrfs_device_info *)dev_info2)->max_avail)
1351                 return 1;
1352         else
1353         return 0;
1354 }
1355 
1356 /*
1357  * sort the devices by max_avail, in which max free extent size of each device
1358  * is stored.(Descending Sort)
1359  */
1360 static inline void btrfs_descending_sort_devices(
1361                                         struct btrfs_device_info *devices,
1362                                         size_t nr_devices)
1363 {
1364         sort(devices, nr_devices, sizeof(struct btrfs_device_info),
1365              btrfs_cmp_device_free_bytes, NULL);
1366 }
1367 
1368 /*
1369  * The helper to calc the free space on the devices that can be used to store
1370  * file data.
1371  */
1372 static int btrfs_calc_avail_data_space(struct btrfs_root *root, u64 *free_bytes)
1373 {
1374         struct btrfs_fs_info *fs_info = root->fs_info;
1375         struct btrfs_device_info *devices_info;
1376         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1377         struct btrfs_device *device;
1378         u64 skip_space;
1379         u64 type;
1380         u64 avail_space;
1381         u64 used_space;
1382         u64 min_stripe_size;
1383         int min_stripes = 1, num_stripes = 1;
1384         int i = 0, nr_devices;
1385         int ret;
1386 
1387         nr_devices = fs_info->fs_devices->open_devices;
1388         BUG_ON(!nr_devices);
1389 
1390         devices_info = kmalloc(sizeof(*devices_info) * nr_devices,
1391                                GFP_NOFS);
1392         if (!devices_info)
1393                 return -ENOMEM;
1394 
1395         /* calc min stripe number for data space alloction */
1396         type = btrfs_get_alloc_profile(root, 1);
1397         if (type & BTRFS_BLOCK_GROUP_RAID0) {
1398                 min_stripes = 2;
1399                 num_stripes = nr_devices;
1400         } else if (type & BTRFS_BLOCK_GROUP_RAID1) {
1401                 min_stripes = 2;
1402                 num_stripes = 2;
1403         } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
1404                 min_stripes = 4;
1405                 num_stripes = 4;
1406         }
1407 
1408         if (type & BTRFS_BLOCK_GROUP_DUP)
1409                 min_stripe_size = 2 * BTRFS_STRIPE_LEN;
1410         else
1411                 min_stripe_size = BTRFS_STRIPE_LEN;
1412 
1413         list_for_each_entry(device, &fs_devices->devices, dev_list) {
1414                 if (!device->in_fs_metadata || !device->bdev ||
1415                     device->is_tgtdev_for_dev_replace)
1416                         continue;
1417 
1418                 avail_space = device->total_bytes - device->bytes_used;
1419 
1420                 /* align with stripe_len */
1421                 do_div(avail_space, BTRFS_STRIPE_LEN);
1422                 avail_space *= BTRFS_STRIPE_LEN;
1423 
1424                 /*
1425                  * In order to avoid overwritting the superblock on the drive,
1426                  * btrfs starts at an offset of at least 1MB when doing chunk
1427                  * allocation.
1428                  */
1429                 skip_space = 1024 * 1024;
1430 
1431                 /* user can set the offset in fs_info->alloc_start. */
1432                 if (fs_info->alloc_start + BTRFS_STRIPE_LEN <=
1433                     device->total_bytes)
1434                         skip_space = max(fs_info->alloc_start, skip_space);
1435 
1436                 /*
1437                  * btrfs can not use the free space in [0, skip_space - 1],
1438                  * we must subtract it from the total. In order to implement
1439                  * it, we account the used space in this range first.
1440                  */
1441                 ret = btrfs_account_dev_extents_size(device, 0, skip_space - 1,
1442                                                      &used_space);
1443                 if (ret) {
1444                         kfree(devices_info);
1445                         return ret;
1446                 }
1447 
1448                 /* calc the free space in [0, skip_space - 1] */
1449                 skip_space -= used_space;
1450 
1451                 /*
1452                  * we can use the free space in [0, skip_space - 1], subtract
1453                  * it from the total.
1454                  */
1455                 if (avail_space && avail_space >= skip_space)
1456                         avail_space -= skip_space;
1457                 else
1458                         avail_space = 0;
1459 
1460                 if (avail_space < min_stripe_size)
1461                         continue;
1462 
1463                 devices_info[i].dev = device;
1464                 devices_info[i].max_avail = avail_space;
1465 
1466                 i++;
1467         }
1468 
1469         nr_devices = i;
1470 
1471         btrfs_descending_sort_devices(devices_info, nr_devices);
1472 
1473         i = nr_devices - 1;
1474         avail_space = 0;
1475         while (nr_devices >= min_stripes) {
1476                 if (num_stripes > nr_devices)
1477                         num_stripes = nr_devices;
1478 
1479                 if (devices_info[i].max_avail >= min_stripe_size) {
1480                         int j;
1481                         u64 alloc_size;
1482 
1483                         avail_space += devices_info[i].max_avail * num_stripes;
1484                         alloc_size = devices_info[i].max_avail;
1485                         for (j = i + 1 - num_stripes; j <= i; j++)
1486                                 devices_info[j].max_avail -= alloc_size;
1487                 }
1488                 i--;
1489                 nr_devices--;
1490         }
1491 
1492         kfree(devices_info);
1493         *free_bytes = avail_space;
1494         return 0;
1495 }
1496 
1497 static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1498 {
1499         struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
1500         struct btrfs_super_block *disk_super = fs_info->super_copy;
1501         struct list_head *head = &fs_info->space_info;
1502         struct btrfs_space_info *found;
1503         u64 total_used = 0;
1504         u64 total_free_data = 0;
1505         int bits = dentry->d_sb->s_blocksize_bits;
1506         __be32 *fsid = (__be32 *)fs_info->fsid;
1507         int ret;
1508 
1509         /* holding chunk_muext to avoid allocating new chunks */
1510         mutex_lock(&fs_info->chunk_mutex);
1511         rcu_read_lock();
1512         list_for_each_entry_rcu(found, head, list) {
1513                 if (found->flags & BTRFS_BLOCK_GROUP_DATA) {
1514                         total_free_data += found->disk_total - found->disk_used;
1515                         total_free_data -=
1516                                 btrfs_account_ro_block_groups_free_space(found);
1517                 }
1518 
1519                 total_used += found->disk_used;
1520         }
1521         rcu_read_unlock();
1522 
1523         buf->f_namelen = BTRFS_NAME_LEN;
1524         buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits;
1525         buf->f_bfree = buf->f_blocks - (total_used >> bits);
1526         buf->f_bsize = dentry->d_sb->s_blocksize;
1527         buf->f_type = BTRFS_SUPER_MAGIC;
1528         buf->f_bavail = total_free_data;
1529         ret = btrfs_calc_avail_data_space(fs_info->tree_root, &total_free_data);
1530         if (ret) {
1531                 mutex_unlock(&fs_info->chunk_mutex);
1532                 return ret;
1533         }
1534         buf->f_bavail += total_free_data;
1535         buf->f_bavail = buf->f_bavail >> bits;
1536         mutex_unlock(&fs_info->chunk_mutex);
1537 
1538         /* We treat it as constant endianness (it doesn't matter _which_)
1539            because we want the fsid to come out the same whether mounted
1540            on a big-endian or little-endian host */
1541         buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]);
1542         buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]);
1543         /* Mask in the root object ID too, to disambiguate subvols */
1544         buf->f_fsid.val[0] ^= BTRFS_I(dentry->d_inode)->root->objectid >> 32;
1545         buf->f_fsid.val[1] ^= BTRFS_I(dentry->d_inode)->root->objectid;
1546 
1547         return 0;
1548 }
1549 
1550 static void btrfs_kill_super(struct super_block *sb)
1551 {
1552         struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1553         kill_anon_super(sb);
1554         free_fs_info(fs_info);
1555 }
1556 
1557 static struct file_system_type btrfs_fs_type = {
1558         .owner          = THIS_MODULE,
1559         .name           = "btrfs",
1560         .mount          = btrfs_mount,
1561         .kill_sb        = btrfs_kill_super,
1562         .fs_flags       = FS_REQUIRES_DEV,
1563 };
1564 MODULE_ALIAS_FS("btrfs");
1565 
1566 /*
1567  * used by btrfsctl to scan devices when no FS is mounted
1568  */
1569 static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
1570                                 unsigned long arg)
1571 {
1572         struct btrfs_ioctl_vol_args *vol;
1573         struct btrfs_fs_devices *fs_devices;
1574         int ret = -ENOTTY;
1575 
1576         if (!capable(CAP_SYS_ADMIN))
1577                 return -EPERM;
1578 
1579         vol = memdup_user((void __user *)arg, sizeof(*vol));
1580         if (IS_ERR(vol))
1581                 return PTR_ERR(vol);
1582 
1583         switch (cmd) {
1584         case BTRFS_IOC_SCAN_DEV:
1585                 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
1586                                             &btrfs_fs_type, &fs_devices);
1587                 break;
1588         case BTRFS_IOC_DEVICES_READY:
1589                 ret = btrfs_scan_one_device(vol->name, FMODE_READ,
1590                                             &btrfs_fs_type, &fs_devices);
1591                 if (ret)
1592                         break;
1593                 ret = !(fs_devices->num_devices == fs_devices->total_devices);
1594                 break;
1595         }
1596 
1597         kfree(vol);
1598         return ret;
1599 }
1600 
1601 static int btrfs_freeze(struct super_block *sb)
1602 {
1603         struct btrfs_trans_handle *trans;
1604         struct btrfs_root *root = btrfs_sb(sb)->tree_root;
1605 
1606         trans = btrfs_attach_transaction_barrier(root);
1607         if (IS_ERR(trans)) {
1608                 /* no transaction, don't bother */
1609                 if (PTR_ERR(trans) == -ENOENT)
1610                         return 0;
1611                 return PTR_ERR(trans);
1612         }
1613         return btrfs_commit_transaction(trans, root);
1614 }
1615 
1616 static int btrfs_unfreeze(struct super_block *sb)
1617 {
1618         return 0;
1619 }
1620 
1621 static int btrfs_show_devname(struct seq_file *m, struct dentry *root)
1622 {
1623         struct btrfs_fs_info *fs_info = btrfs_sb(root->d_sb);
1624         struct btrfs_fs_devices *cur_devices;
1625         struct btrfs_device *dev, *first_dev = NULL;
1626         struct list_head *head;
1627         struct rcu_string *name;
1628 
1629         mutex_lock(&fs_info->fs_devices->device_list_mutex);
1630         cur_devices = fs_info->fs_devices;
1631         while (cur_devices) {
1632                 head = &cur_devices->devices;
1633                 list_for_each_entry(dev, head, dev_list) {
1634                         if (dev->missing)
1635                                 continue;
1636                         if (!first_dev || dev->devid < first_dev->devid)
1637                                 first_dev = dev;
1638                 }
1639                 cur_devices = cur_devices->seed;
1640         }
1641 
1642         if (first_dev) {
1643                 rcu_read_lock();
1644                 name = rcu_dereference(first_dev->name);
1645                 seq_escape(m, name->str, " \t\n\\");
1646                 rcu_read_unlock();
1647         } else {
1648                 WARN_ON(1);
1649         }
1650         mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1651         return 0;
1652 }
1653 
1654 static const struct super_operations btrfs_super_ops = {
1655         .drop_inode     = btrfs_drop_inode,
1656         .evict_inode    = btrfs_evict_inode,
1657         .put_super      = btrfs_put_super,
1658         .sync_fs        = btrfs_sync_fs,
1659         .show_options   = btrfs_show_options,
1660         .show_devname   = btrfs_show_devname,
1661         .write_inode    = btrfs_write_inode,
1662         .alloc_inode    = btrfs_alloc_inode,
1663         .destroy_inode  = btrfs_destroy_inode,
1664         .statfs         = btrfs_statfs,
1665         .remount_fs     = btrfs_remount,
1666         .freeze_fs      = btrfs_freeze,
1667         .unfreeze_fs    = btrfs_unfreeze,
1668 };
1669 
1670 static const struct file_operations btrfs_ctl_fops = {
1671         .unlocked_ioctl  = btrfs_control_ioctl,
1672         .compat_ioctl = btrfs_control_ioctl,
1673         .owner   = THIS_MODULE,
1674         .llseek = noop_llseek,
1675 };
1676 
1677 static struct miscdevice btrfs_misc = {
1678         .minor          = BTRFS_MINOR,
1679         .name           = "btrfs-control",
1680         .fops           = &btrfs_ctl_fops
1681 };
1682 
1683 MODULE_ALIAS_MISCDEV(BTRFS_MINOR);
1684 MODULE_ALIAS("devname:btrfs-control");
1685 
1686 static int btrfs_interface_init(void)
1687 {
1688         return misc_register(&btrfs_misc);
1689 }
1690 
1691 static void btrfs_interface_exit(void)
1692 {
1693         if (misc_deregister(&btrfs_misc) < 0)
1694                 printk(KERN_INFO "btrfs: misc_deregister failed for control device\n");
1695 }
1696 
1697 static int __init init_btrfs_fs(void)
1698 {
1699         int err;
1700 
1701         err = btrfs_init_sysfs();
1702         if (err)
1703                 return err;
1704 
1705         btrfs_init_compress();
1706 
1707         err = btrfs_init_cachep();
1708         if (err)
1709                 goto free_compress;
1710 
1711         err = extent_io_init();
1712         if (err)
1713                 goto free_cachep;
1714 
1715         err = extent_map_init();
1716         if (err)
1717                 goto free_extent_io;
1718 
1719         err = ordered_data_init();
1720         if (err)
1721                 goto free_extent_map;
1722 
1723         err = btrfs_delayed_inode_init();
1724         if (err)
1725                 goto free_ordered_data;
1726 
1727         err = btrfs_auto_defrag_init();
1728         if (err)
1729                 goto free_delayed_inode;
1730 
1731         err = btrfs_delayed_ref_init();
1732         if (err)
1733                 goto free_auto_defrag;
1734 
1735         err = btrfs_interface_init();
1736         if (err)
1737                 goto free_delayed_ref;
1738 
1739         err = register_filesystem(&btrfs_fs_type);
1740         if (err)
1741                 goto unregister_ioctl;
1742 
1743         btrfs_init_lockdep();
1744 
1745 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
1746         btrfs_test_free_space_cache();
1747 #endif
1748 
1749         printk(KERN_INFO "%s loaded\n", BTRFS_BUILD_VERSION);
1750         return 0;
1751 
1752 unregister_ioctl:
1753         btrfs_interface_exit();
1754 free_delayed_ref:
1755         btrfs_delayed_ref_exit();
1756 free_auto_defrag:
1757         btrfs_auto_defrag_exit();
1758 free_delayed_inode:
1759         btrfs_delayed_inode_exit();
1760 free_ordered_data:
1761         ordered_data_exit();
1762 free_extent_map:
1763         extent_map_exit();
1764 free_extent_io:
1765         extent_io_exit();
1766 free_cachep:
1767         btrfs_destroy_cachep();
1768 free_compress:
1769         btrfs_exit_compress();
1770         btrfs_exit_sysfs();
1771         return err;
1772 }
1773 
1774 static void __exit exit_btrfs_fs(void)
1775 {
1776         btrfs_destroy_cachep();
1777         btrfs_delayed_ref_exit();
1778         btrfs_auto_defrag_exit();
1779         btrfs_delayed_inode_exit();
1780         ordered_data_exit();
1781         extent_map_exit();
1782         extent_io_exit();
1783         btrfs_interface_exit();
1784         unregister_filesystem(&btrfs_fs_type);
1785         btrfs_exit_sysfs();
1786         btrfs_cleanup_fs_uuids();
1787         btrfs_exit_compress();
1788 }
1789 
1790 module_init(init_btrfs_fs)
1791 module_exit(exit_btrfs_fs)
1792 
1793 MODULE_LICENSE("GPL");
1794 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp