]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/xfs/xfs_super.c
d93a63aa7ab1216a590947db92743e19dda4827b
[linux.git] / fs / xfs / xfs_super.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include "xfs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap.h"
17 #include "xfs_alloc.h"
18 #include "xfs_fsops.h"
19 #include "xfs_trans.h"
20 #include "xfs_buf_item.h"
21 #include "xfs_log.h"
22 #include "xfs_log_priv.h"
23 #include "xfs_dir2.h"
24 #include "xfs_extfree_item.h"
25 #include "xfs_mru_cache.h"
26 #include "xfs_inode_item.h"
27 #include "xfs_icache.h"
28 #include "xfs_trace.h"
29 #include "xfs_icreate_item.h"
30 #include "xfs_filestream.h"
31 #include "xfs_quota.h"
32 #include "xfs_sysfs.h"
33 #include "xfs_ondisk.h"
34 #include "xfs_rmap_item.h"
35 #include "xfs_refcount_item.h"
36 #include "xfs_bmap_item.h"
37 #include "xfs_reflink.h"
38
39 #include <linux/magic.h>
40 #include <linux/fs_context.h>
41 #include <linux/fs_parser.h>
42
43 static const struct super_operations xfs_super_operations;
44
45 static struct kset *xfs_kset;           /* top-level xfs sysfs dir */
46 #ifdef DEBUG
47 static struct xfs_kobj xfs_dbg_kobj;    /* global debug sysfs attrs */
48 #endif
49
50 /*
51  * Table driven mount option parser.
52  */
53 enum {
54         Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
55         Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
56         Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
57         Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32, Opt_ikeep,
58         Opt_noikeep, Opt_largeio, Opt_nolargeio, Opt_attr2, Opt_noattr2,
59         Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
60         Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
61         Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
62         Opt_discard, Opt_nodiscard, Opt_dax,
63 };
64
65 static const struct fs_parameter_spec xfs_param_specs[] = {
66         fsparam_u32("logbufs",          Opt_logbufs),
67         fsparam_string("logbsize",      Opt_logbsize),
68         fsparam_string("logdev",        Opt_logdev),
69         fsparam_string("rtdev",         Opt_rtdev),
70         fsparam_flag("wsync",           Opt_wsync),
71         fsparam_flag("noalign",         Opt_noalign),
72         fsparam_flag("swalloc",         Opt_swalloc),
73         fsparam_u32("sunit",            Opt_sunit),
74         fsparam_u32("swidth",           Opt_swidth),
75         fsparam_flag("nouuid",          Opt_nouuid),
76         fsparam_flag("grpid",           Opt_grpid),
77         fsparam_flag("nogrpid",         Opt_nogrpid),
78         fsparam_flag("bsdgroups",       Opt_bsdgroups),
79         fsparam_flag("sysvgroups",      Opt_sysvgroups),
80         fsparam_string("allocsize",     Opt_allocsize),
81         fsparam_flag("norecovery",      Opt_norecovery),
82         fsparam_flag("inode64",         Opt_inode64),
83         fsparam_flag("inode32",         Opt_inode32),
84         fsparam_flag("ikeep",           Opt_ikeep),
85         fsparam_flag("noikeep",         Opt_noikeep),
86         fsparam_flag("largeio",         Opt_largeio),
87         fsparam_flag("nolargeio",       Opt_nolargeio),
88         fsparam_flag("attr2",           Opt_attr2),
89         fsparam_flag("noattr2",         Opt_noattr2),
90         fsparam_flag("filestreams",     Opt_filestreams),
91         fsparam_flag("quota",           Opt_quota),
92         fsparam_flag("noquota",         Opt_noquota),
93         fsparam_flag("usrquota",        Opt_usrquota),
94         fsparam_flag("grpquota",        Opt_grpquota),
95         fsparam_flag("prjquota",        Opt_prjquota),
96         fsparam_flag("uquota",          Opt_uquota),
97         fsparam_flag("gquota",          Opt_gquota),
98         fsparam_flag("pquota",          Opt_pquota),
99         fsparam_flag("uqnoenforce",     Opt_uqnoenforce),
100         fsparam_flag("gqnoenforce",     Opt_gqnoenforce),
101         fsparam_flag("pqnoenforce",     Opt_pqnoenforce),
102         fsparam_flag("qnoenforce",      Opt_qnoenforce),
103         fsparam_flag("discard",         Opt_discard),
104         fsparam_flag("nodiscard",       Opt_nodiscard),
105         fsparam_flag("dax",             Opt_dax),
106         {}
107 };
108
109 static const struct fs_parameter_description xfs_fs_parameters = {
110         .name           = "xfs",
111         .specs          = xfs_param_specs,
112 };
113
114 static int
115 suffix_kstrtoint(
116         const char      *s,
117         unsigned int    base,
118         int             *res)
119 {
120         int             last, shift_left_factor = 0, _res;
121         char            *value;
122         int             ret = 0;
123
124         value = kstrdup(s, GFP_KERNEL);
125         if (!value)
126                 return -ENOMEM;
127
128         last = strlen(value) - 1;
129         if (value[last] == 'K' || value[last] == 'k') {
130                 shift_left_factor = 10;
131                 value[last] = '\0';
132         }
133         if (value[last] == 'M' || value[last] == 'm') {
134                 shift_left_factor = 20;
135                 value[last] = '\0';
136         }
137         if (value[last] == 'G' || value[last] == 'g') {
138                 shift_left_factor = 30;
139                 value[last] = '\0';
140         }
141
142         if (kstrtoint(value, base, &_res))
143                 ret = -EINVAL;
144         kfree(value);
145         *res = _res << shift_left_factor;
146         return ret;
147 }
148
149 /*
150  * Set mount state from a mount option.
151  *
152  * NOTE: mp->m_super is NULL here!
153  */
154 static int
155 xfs_fc_parse_param(
156         struct fs_context       *fc,
157         struct fs_parameter     *param)
158 {
159         struct xfs_mount        *mp = fc->s_fs_info;
160         struct fs_parse_result  result;
161         int                     size = 0;
162         int                     opt;
163
164         opt = fs_parse(fc, &xfs_fs_parameters, param, &result);
165         if (opt < 0)
166                 return opt;
167
168         switch (opt) {
169         case Opt_logbufs:
170                 mp->m_logbufs = result.uint_32;
171                 return 0;
172         case Opt_logbsize:
173                 if (suffix_kstrtoint(param->string, 10, &mp->m_logbsize))
174                         return -EINVAL;
175                 return 0;
176         case Opt_logdev:
177                 kfree(mp->m_logname);
178                 mp->m_logname = kstrdup(param->string, GFP_KERNEL);
179                 if (!mp->m_logname)
180                         return -ENOMEM;
181                 return 0;
182         case Opt_rtdev:
183                 kfree(mp->m_rtname);
184                 mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
185                 if (!mp->m_rtname)
186                         return -ENOMEM;
187                 return 0;
188         case Opt_allocsize:
189                 if (suffix_kstrtoint(param->string, 10, &size))
190                         return -EINVAL;
191                 mp->m_allocsize_log = ffs(size) - 1;
192                 mp->m_flags |= XFS_MOUNT_ALLOCSIZE;
193                 return 0;
194         case Opt_grpid:
195         case Opt_bsdgroups:
196                 mp->m_flags |= XFS_MOUNT_GRPID;
197                 return 0;
198         case Opt_nogrpid:
199         case Opt_sysvgroups:
200                 mp->m_flags &= ~XFS_MOUNT_GRPID;
201                 return 0;
202         case Opt_wsync:
203                 mp->m_flags |= XFS_MOUNT_WSYNC;
204                 return 0;
205         case Opt_norecovery:
206                 mp->m_flags |= XFS_MOUNT_NORECOVERY;
207                 return 0;
208         case Opt_noalign:
209                 mp->m_flags |= XFS_MOUNT_NOALIGN;
210                 return 0;
211         case Opt_swalloc:
212                 mp->m_flags |= XFS_MOUNT_SWALLOC;
213                 return 0;
214         case Opt_sunit:
215                 mp->m_dalign = result.uint_32;
216                 return 0;
217         case Opt_swidth:
218                 mp->m_swidth = result.uint_32;
219                 return 0;
220         case Opt_inode32:
221                 mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
222                 return 0;
223         case Opt_inode64:
224                 mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
225                 return 0;
226         case Opt_nouuid:
227                 mp->m_flags |= XFS_MOUNT_NOUUID;
228                 return 0;
229         case Opt_ikeep:
230                 mp->m_flags |= XFS_MOUNT_IKEEP;
231                 return 0;
232         case Opt_noikeep:
233                 mp->m_flags &= ~XFS_MOUNT_IKEEP;
234                 return 0;
235         case Opt_largeio:
236                 mp->m_flags |= XFS_MOUNT_LARGEIO;
237                 return 0;
238         case Opt_nolargeio:
239                 mp->m_flags &= ~XFS_MOUNT_LARGEIO;
240                 return 0;
241         case Opt_attr2:
242                 mp->m_flags |= XFS_MOUNT_ATTR2;
243                 return 0;
244         case Opt_noattr2:
245                 mp->m_flags &= ~XFS_MOUNT_ATTR2;
246                 mp->m_flags |= XFS_MOUNT_NOATTR2;
247                 return 0;
248         case Opt_filestreams:
249                 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
250                 return 0;
251         case Opt_noquota:
252                 mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
253                 mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
254                 mp->m_qflags &= ~XFS_ALL_QUOTA_ACTIVE;
255                 return 0;
256         case Opt_quota:
257         case Opt_uquota:
258         case Opt_usrquota:
259                 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE |
260                                  XFS_UQUOTA_ENFD);
261                 return 0;
262         case Opt_qnoenforce:
263         case Opt_uqnoenforce:
264                 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
265                 mp->m_qflags &= ~XFS_UQUOTA_ENFD;
266                 return 0;
267         case Opt_pquota:
268         case Opt_prjquota:
269                 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE |
270                                  XFS_PQUOTA_ENFD);
271                 return 0;
272         case Opt_pqnoenforce:
273                 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
274                 mp->m_qflags &= ~XFS_PQUOTA_ENFD;
275                 return 0;
276         case Opt_gquota:
277         case Opt_grpquota:
278                 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE |
279                                  XFS_GQUOTA_ENFD);
280                 return 0;
281         case Opt_gqnoenforce:
282                 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
283                 mp->m_qflags &= ~XFS_GQUOTA_ENFD;
284                 return 0;
285         case Opt_discard:
286                 mp->m_flags |= XFS_MOUNT_DISCARD;
287                 return 0;
288         case Opt_nodiscard:
289                 mp->m_flags &= ~XFS_MOUNT_DISCARD;
290                 return 0;
291 #ifdef CONFIG_FS_DAX
292         case Opt_dax:
293                 mp->m_flags |= XFS_MOUNT_DAX;
294                 return 0;
295 #endif
296         default:
297                 xfs_warn(mp, "unknown mount option [%s].", param->key);
298                 return -EINVAL;
299         }
300
301         return 0;
302 }
303
304 static int
305 xfs_fc_validate_params(
306         struct xfs_mount        *mp)
307 {
308         /*
309          * no recovery flag requires a read-only mount
310          */
311         if ((mp->m_flags & XFS_MOUNT_NORECOVERY) &&
312             !(mp->m_flags & XFS_MOUNT_RDONLY)) {
313                 xfs_warn(mp, "no-recovery mounts must be read-only.");
314                 return -EINVAL;
315         }
316
317         if ((mp->m_flags & XFS_MOUNT_NOALIGN) &&
318             (mp->m_dalign || mp->m_swidth)) {
319                 xfs_warn(mp,
320         "sunit and swidth options incompatible with the noalign option");
321                 return -EINVAL;
322         }
323
324         if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
325                 xfs_warn(mp, "quota support not available in this kernel.");
326                 return -EINVAL;
327         }
328
329         if ((mp->m_dalign && !mp->m_swidth) ||
330             (!mp->m_dalign && mp->m_swidth)) {
331                 xfs_warn(mp, "sunit and swidth must be specified together");
332                 return -EINVAL;
333         }
334
335         if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
336                 xfs_warn(mp,
337         "stripe width (%d) must be a multiple of the stripe unit (%d)",
338                         mp->m_swidth, mp->m_dalign);
339                 return -EINVAL;
340         }
341
342         if (mp->m_logbufs != -1 &&
343             mp->m_logbufs != 0 &&
344             (mp->m_logbufs < XLOG_MIN_ICLOGS ||
345              mp->m_logbufs > XLOG_MAX_ICLOGS)) {
346                 xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
347                         mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
348                 return -EINVAL;
349         }
350         if (mp->m_logbsize != -1 &&
351             mp->m_logbsize !=  0 &&
352             (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
353              mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
354              !is_power_of_2(mp->m_logbsize))) {
355                 xfs_warn(mp,
356                         "invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
357                         mp->m_logbsize);
358                 return -EINVAL;
359         }
360
361         if ((mp->m_flags & XFS_MOUNT_ALLOCSIZE) &&
362             (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
363              mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
364                 xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
365                         mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
366                 return -EINVAL;
367         }
368
369         return 0;
370 }
371
372 struct proc_xfs_info {
373         uint64_t        flag;
374         char            *str;
375 };
376
377 static int
378 xfs_fs_show_options(
379         struct seq_file         *m,
380         struct dentry           *root)
381 {
382         static struct proc_xfs_info xfs_info_set[] = {
383                 /* the few simple ones we can get from the mount struct */
384                 { XFS_MOUNT_IKEEP,              ",ikeep" },
385                 { XFS_MOUNT_WSYNC,              ",wsync" },
386                 { XFS_MOUNT_NOALIGN,            ",noalign" },
387                 { XFS_MOUNT_SWALLOC,            ",swalloc" },
388                 { XFS_MOUNT_NOUUID,             ",nouuid" },
389                 { XFS_MOUNT_NORECOVERY,         ",norecovery" },
390                 { XFS_MOUNT_ATTR2,              ",attr2" },
391                 { XFS_MOUNT_FILESTREAMS,        ",filestreams" },
392                 { XFS_MOUNT_GRPID,              ",grpid" },
393                 { XFS_MOUNT_DISCARD,            ",discard" },
394                 { XFS_MOUNT_LARGEIO,            ",largeio" },
395                 { XFS_MOUNT_DAX,                ",dax" },
396                 { 0, NULL }
397         };
398         struct xfs_mount        *mp = XFS_M(root->d_sb);
399         struct proc_xfs_info    *xfs_infop;
400
401         for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
402                 if (mp->m_flags & xfs_infop->flag)
403                         seq_puts(m, xfs_infop->str);
404         }
405
406         seq_printf(m, ",inode%d",
407                 (mp->m_flags & XFS_MOUNT_SMALL_INUMS) ? 32 : 64);
408
409         if (mp->m_flags & XFS_MOUNT_ALLOCSIZE)
410                 seq_printf(m, ",allocsize=%dk",
411                            (1 << mp->m_allocsize_log) >> 10);
412
413         if (mp->m_logbufs > 0)
414                 seq_printf(m, ",logbufs=%d", mp->m_logbufs);
415         if (mp->m_logbsize > 0)
416                 seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
417
418         if (mp->m_logname)
419                 seq_show_option(m, "logdev", mp->m_logname);
420         if (mp->m_rtname)
421                 seq_show_option(m, "rtdev", mp->m_rtname);
422
423         if (mp->m_dalign > 0)
424                 seq_printf(m, ",sunit=%d",
425                                 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
426         if (mp->m_swidth > 0)
427                 seq_printf(m, ",swidth=%d",
428                                 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
429
430         if (mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD))
431                 seq_puts(m, ",usrquota");
432         else if (mp->m_qflags & XFS_UQUOTA_ACCT)
433                 seq_puts(m, ",uqnoenforce");
434
435         if (mp->m_qflags & XFS_PQUOTA_ACCT) {
436                 if (mp->m_qflags & XFS_PQUOTA_ENFD)
437                         seq_puts(m, ",prjquota");
438                 else
439                         seq_puts(m, ",pqnoenforce");
440         }
441         if (mp->m_qflags & XFS_GQUOTA_ACCT) {
442                 if (mp->m_qflags & XFS_GQUOTA_ENFD)
443                         seq_puts(m, ",grpquota");
444                 else
445                         seq_puts(m, ",gqnoenforce");
446         }
447
448         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
449                 seq_puts(m, ",noquota");
450
451         return 0;
452 }
453
454 static uint64_t
455 xfs_max_file_offset(
456         unsigned int            blockshift)
457 {
458         unsigned int            pagefactor = 1;
459         unsigned int            bitshift = BITS_PER_LONG - 1;
460
461         /* Figure out maximum filesize, on Linux this can depend on
462          * the filesystem blocksize (on 32 bit platforms).
463          * __block_write_begin does this in an [unsigned] long long...
464          *      page->index << (PAGE_SHIFT - bbits)
465          * So, for page sized blocks (4K on 32 bit platforms),
466          * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
467          *      (((u64)PAGE_SIZE << (BITS_PER_LONG-1))-1)
468          * but for smaller blocksizes it is less (bbits = log2 bsize).
469          */
470
471 #if BITS_PER_LONG == 32
472         ASSERT(sizeof(sector_t) == 8);
473         pagefactor = PAGE_SIZE;
474         bitshift = BITS_PER_LONG;
475 #endif
476
477         return (((uint64_t)pagefactor) << bitshift) - 1;
478 }
479
480 /*
481  * Set parameters for inode allocation heuristics, taking into account
482  * filesystem size and inode32/inode64 mount options; i.e. specifically
483  * whether or not XFS_MOUNT_SMALL_INUMS is set.
484  *
485  * Inode allocation patterns are altered only if inode32 is requested
486  * (XFS_MOUNT_SMALL_INUMS), and the filesystem is sufficiently large.
487  * If altered, XFS_MOUNT_32BITINODES is set as well.
488  *
489  * An agcount independent of that in the mount structure is provided
490  * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
491  * to the potentially higher ag count.
492  *
493  * Returns the maximum AG index which may contain inodes.
494  */
495 xfs_agnumber_t
496 xfs_set_inode_alloc(
497         struct xfs_mount *mp,
498         xfs_agnumber_t  agcount)
499 {
500         xfs_agnumber_t  index;
501         xfs_agnumber_t  maxagi = 0;
502         xfs_sb_t        *sbp = &mp->m_sb;
503         xfs_agnumber_t  max_metadata;
504         xfs_agino_t     agino;
505         xfs_ino_t       ino;
506
507         /*
508          * Calculate how much should be reserved for inodes to meet
509          * the max inode percentage.  Used only for inode32.
510          */
511         if (M_IGEO(mp)->maxicount) {
512                 uint64_t        icount;
513
514                 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
515                 do_div(icount, 100);
516                 icount += sbp->sb_agblocks - 1;
517                 do_div(icount, sbp->sb_agblocks);
518                 max_metadata = icount;
519         } else {
520                 max_metadata = agcount;
521         }
522
523         /* Get the last possible inode in the filesystem */
524         agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
525         ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
526
527         /*
528          * If user asked for no more than 32-bit inodes, and the fs is
529          * sufficiently large, set XFS_MOUNT_32BITINODES if we must alter
530          * the allocator to accommodate the request.
531          */
532         if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
533                 mp->m_flags |= XFS_MOUNT_32BITINODES;
534         else
535                 mp->m_flags &= ~XFS_MOUNT_32BITINODES;
536
537         for (index = 0; index < agcount; index++) {
538                 struct xfs_perag        *pag;
539
540                 ino = XFS_AGINO_TO_INO(mp, index, agino);
541
542                 pag = xfs_perag_get(mp, index);
543
544                 if (mp->m_flags & XFS_MOUNT_32BITINODES) {
545                         if (ino > XFS_MAXINUMBER_32) {
546                                 pag->pagi_inodeok = 0;
547                                 pag->pagf_metadata = 0;
548                         } else {
549                                 pag->pagi_inodeok = 1;
550                                 maxagi++;
551                                 if (index < max_metadata)
552                                         pag->pagf_metadata = 1;
553                                 else
554                                         pag->pagf_metadata = 0;
555                         }
556                 } else {
557                         pag->pagi_inodeok = 1;
558                         pag->pagf_metadata = 0;
559                 }
560
561                 xfs_perag_put(pag);
562         }
563
564         return (mp->m_flags & XFS_MOUNT_32BITINODES) ? maxagi : agcount;
565 }
566
567 STATIC int
568 xfs_blkdev_get(
569         xfs_mount_t             *mp,
570         const char              *name,
571         struct block_device     **bdevp)
572 {
573         int                     error = 0;
574
575         *bdevp = blkdev_get_by_path(name, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
576                                     mp);
577         if (IS_ERR(*bdevp)) {
578                 error = PTR_ERR(*bdevp);
579                 xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
580         }
581
582         return error;
583 }
584
585 STATIC void
586 xfs_blkdev_put(
587         struct block_device     *bdev)
588 {
589         if (bdev)
590                 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
591 }
592
593 void
594 xfs_blkdev_issue_flush(
595         xfs_buftarg_t           *buftarg)
596 {
597         blkdev_issue_flush(buftarg->bt_bdev, GFP_NOFS, NULL);
598 }
599
600 STATIC void
601 xfs_close_devices(
602         struct xfs_mount        *mp)
603 {
604         struct dax_device *dax_ddev = mp->m_ddev_targp->bt_daxdev;
605
606         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
607                 struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
608                 struct dax_device *dax_logdev = mp->m_logdev_targp->bt_daxdev;
609
610                 xfs_free_buftarg(mp->m_logdev_targp);
611                 xfs_blkdev_put(logdev);
612                 fs_put_dax(dax_logdev);
613         }
614         if (mp->m_rtdev_targp) {
615                 struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
616                 struct dax_device *dax_rtdev = mp->m_rtdev_targp->bt_daxdev;
617
618                 xfs_free_buftarg(mp->m_rtdev_targp);
619                 xfs_blkdev_put(rtdev);
620                 fs_put_dax(dax_rtdev);
621         }
622         xfs_free_buftarg(mp->m_ddev_targp);
623         fs_put_dax(dax_ddev);
624 }
625
626 /*
627  * The file system configurations are:
628  *      (1) device (partition) with data and internal log
629  *      (2) logical volume with data and log subvolumes.
630  *      (3) logical volume with data, log, and realtime subvolumes.
631  *
632  * We only have to handle opening the log and realtime volumes here if
633  * they are present.  The data subvolume has already been opened by
634  * get_sb_bdev() and is stored in sb->s_bdev.
635  */
636 STATIC int
637 xfs_open_devices(
638         struct xfs_mount        *mp)
639 {
640         struct block_device     *ddev = mp->m_super->s_bdev;
641         struct dax_device       *dax_ddev = fs_dax_get_by_bdev(ddev);
642         struct dax_device       *dax_logdev = NULL, *dax_rtdev = NULL;
643         struct block_device     *logdev = NULL, *rtdev = NULL;
644         int                     error;
645
646         /*
647          * Open real time and log devices - order is important.
648          */
649         if (mp->m_logname) {
650                 error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
651                 if (error)
652                         goto out;
653                 dax_logdev = fs_dax_get_by_bdev(logdev);
654         }
655
656         if (mp->m_rtname) {
657                 error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
658                 if (error)
659                         goto out_close_logdev;
660
661                 if (rtdev == ddev || rtdev == logdev) {
662                         xfs_warn(mp,
663         "Cannot mount filesystem with identical rtdev and ddev/logdev.");
664                         error = -EINVAL;
665                         goto out_close_rtdev;
666                 }
667                 dax_rtdev = fs_dax_get_by_bdev(rtdev);
668         }
669
670         /*
671          * Setup xfs_mount buffer target pointers
672          */
673         error = -ENOMEM;
674         mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev, dax_ddev);
675         if (!mp->m_ddev_targp)
676                 goto out_close_rtdev;
677
678         if (rtdev) {
679                 mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev, dax_rtdev);
680                 if (!mp->m_rtdev_targp)
681                         goto out_free_ddev_targ;
682         }
683
684         if (logdev && logdev != ddev) {
685                 mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev, dax_logdev);
686                 if (!mp->m_logdev_targp)
687                         goto out_free_rtdev_targ;
688         } else {
689                 mp->m_logdev_targp = mp->m_ddev_targp;
690         }
691
692         return 0;
693
694  out_free_rtdev_targ:
695         if (mp->m_rtdev_targp)
696                 xfs_free_buftarg(mp->m_rtdev_targp);
697  out_free_ddev_targ:
698         xfs_free_buftarg(mp->m_ddev_targp);
699  out_close_rtdev:
700         xfs_blkdev_put(rtdev);
701         fs_put_dax(dax_rtdev);
702  out_close_logdev:
703         if (logdev && logdev != ddev) {
704                 xfs_blkdev_put(logdev);
705                 fs_put_dax(dax_logdev);
706         }
707  out:
708         fs_put_dax(dax_ddev);
709         return error;
710 }
711
712 /*
713  * Setup xfs_mount buffer target pointers based on superblock
714  */
715 STATIC int
716 xfs_setup_devices(
717         struct xfs_mount        *mp)
718 {
719         int                     error;
720
721         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
722         if (error)
723                 return error;
724
725         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
726                 unsigned int    log_sector_size = BBSIZE;
727
728                 if (xfs_sb_version_hassector(&mp->m_sb))
729                         log_sector_size = mp->m_sb.sb_logsectsize;
730                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
731                                             log_sector_size);
732                 if (error)
733                         return error;
734         }
735         if (mp->m_rtdev_targp) {
736                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
737                                             mp->m_sb.sb_sectsize);
738                 if (error)
739                         return error;
740         }
741
742         return 0;
743 }
744
745 STATIC int
746 xfs_init_mount_workqueues(
747         struct xfs_mount        *mp)
748 {
749         mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
750                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 1, mp->m_super->s_id);
751         if (!mp->m_buf_workqueue)
752                 goto out;
753
754         mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
755                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
756         if (!mp->m_unwritten_workqueue)
757                 goto out_destroy_buf;
758
759         mp->m_cil_workqueue = alloc_workqueue("xfs-cil/%s",
760                         WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND,
761                         0, mp->m_super->s_id);
762         if (!mp->m_cil_workqueue)
763                 goto out_destroy_unwritten;
764
765         mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
766                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
767         if (!mp->m_reclaim_workqueue)
768                 goto out_destroy_cil;
769
770         mp->m_eofblocks_workqueue = alloc_workqueue("xfs-eofblocks/%s",
771                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
772         if (!mp->m_eofblocks_workqueue)
773                 goto out_destroy_reclaim;
774
775         mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s", WQ_FREEZABLE, 0,
776                                                mp->m_super->s_id);
777         if (!mp->m_sync_workqueue)
778                 goto out_destroy_eofb;
779
780         return 0;
781
782 out_destroy_eofb:
783         destroy_workqueue(mp->m_eofblocks_workqueue);
784 out_destroy_reclaim:
785         destroy_workqueue(mp->m_reclaim_workqueue);
786 out_destroy_cil:
787         destroy_workqueue(mp->m_cil_workqueue);
788 out_destroy_unwritten:
789         destroy_workqueue(mp->m_unwritten_workqueue);
790 out_destroy_buf:
791         destroy_workqueue(mp->m_buf_workqueue);
792 out:
793         return -ENOMEM;
794 }
795
796 STATIC void
797 xfs_destroy_mount_workqueues(
798         struct xfs_mount        *mp)
799 {
800         destroy_workqueue(mp->m_sync_workqueue);
801         destroy_workqueue(mp->m_eofblocks_workqueue);
802         destroy_workqueue(mp->m_reclaim_workqueue);
803         destroy_workqueue(mp->m_cil_workqueue);
804         destroy_workqueue(mp->m_unwritten_workqueue);
805         destroy_workqueue(mp->m_buf_workqueue);
806 }
807
808 /*
809  * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
810  * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
811  * for IO to complete so that we effectively throttle multiple callers to the
812  * rate at which IO is completing.
813  */
814 void
815 xfs_flush_inodes(
816         struct xfs_mount        *mp)
817 {
818         struct super_block      *sb = mp->m_super;
819
820         if (down_read_trylock(&sb->s_umount)) {
821                 sync_inodes_sb(sb);
822                 up_read(&sb->s_umount);
823         }
824 }
825
826 /* Catch misguided souls that try to use this interface on XFS */
827 STATIC struct inode *
828 xfs_fs_alloc_inode(
829         struct super_block      *sb)
830 {
831         BUG();
832         return NULL;
833 }
834
835 #ifdef DEBUG
836 static void
837 xfs_check_delalloc(
838         struct xfs_inode        *ip,
839         int                     whichfork)
840 {
841         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
842         struct xfs_bmbt_irec    got;
843         struct xfs_iext_cursor  icur;
844
845         if (!ifp || !xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got))
846                 return;
847         do {
848                 if (isnullstartblock(got.br_startblock)) {
849                         xfs_warn(ip->i_mount,
850         "ino %llx %s fork has delalloc extent at [0x%llx:0x%llx]",
851                                 ip->i_ino,
852                                 whichfork == XFS_DATA_FORK ? "data" : "cow",
853                                 got.br_startoff, got.br_blockcount);
854                 }
855         } while (xfs_iext_next_extent(ifp, &icur, &got));
856 }
857 #else
858 #define xfs_check_delalloc(ip, whichfork)       do { } while (0)
859 #endif
860
861 /*
862  * Now that the generic code is guaranteed not to be accessing
863  * the linux inode, we can inactivate and reclaim the inode.
864  */
865 STATIC void
866 xfs_fs_destroy_inode(
867         struct inode            *inode)
868 {
869         struct xfs_inode        *ip = XFS_I(inode);
870
871         trace_xfs_destroy_inode(ip);
872
873         ASSERT(!rwsem_is_locked(&inode->i_rwsem));
874         XFS_STATS_INC(ip->i_mount, vn_rele);
875         XFS_STATS_INC(ip->i_mount, vn_remove);
876
877         xfs_inactive(ip);
878
879         if (!XFS_FORCED_SHUTDOWN(ip->i_mount) && ip->i_delayed_blks) {
880                 xfs_check_delalloc(ip, XFS_DATA_FORK);
881                 xfs_check_delalloc(ip, XFS_COW_FORK);
882                 ASSERT(0);
883         }
884
885         XFS_STATS_INC(ip->i_mount, vn_reclaim);
886
887         /*
888          * We should never get here with one of the reclaim flags already set.
889          */
890         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
891         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIM));
892
893         /*
894          * We always use background reclaim here because even if the
895          * inode is clean, it still may be under IO and hence we have
896          * to take the flush lock. The background reclaim path handles
897          * this more efficiently than we can here, so simply let background
898          * reclaim tear down all inodes.
899          */
900         xfs_inode_set_reclaim_tag(ip);
901 }
902
903 static void
904 xfs_fs_dirty_inode(
905         struct inode                    *inode,
906         int                             flag)
907 {
908         struct xfs_inode                *ip = XFS_I(inode);
909         struct xfs_mount                *mp = ip->i_mount;
910         struct xfs_trans                *tp;
911
912         if (!(inode->i_sb->s_flags & SB_LAZYTIME))
913                 return;
914         if (flag != I_DIRTY_SYNC || !(inode->i_state & I_DIRTY_TIME))
915                 return;
916
917         if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
918                 return;
919         xfs_ilock(ip, XFS_ILOCK_EXCL);
920         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
921         xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
922         xfs_trans_commit(tp);
923 }
924
925 /*
926  * Slab object creation initialisation for the XFS inode.
927  * This covers only the idempotent fields in the XFS inode;
928  * all other fields need to be initialised on allocation
929  * from the slab. This avoids the need to repeatedly initialise
930  * fields in the xfs inode that left in the initialise state
931  * when freeing the inode.
932  */
933 STATIC void
934 xfs_fs_inode_init_once(
935         void                    *inode)
936 {
937         struct xfs_inode        *ip = inode;
938
939         memset(ip, 0, sizeof(struct xfs_inode));
940
941         /* vfs inode */
942         inode_init_once(VFS_I(ip));
943
944         /* xfs inode */
945         atomic_set(&ip->i_pincount, 0);
946         spin_lock_init(&ip->i_flags_lock);
947
948         mrlock_init(&ip->i_mmaplock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
949                      "xfsino", ip->i_ino);
950         mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
951                      "xfsino", ip->i_ino);
952 }
953
954 /*
955  * We do an unlocked check for XFS_IDONTCACHE here because we are already
956  * serialised against cache hits here via the inode->i_lock and igrab() in
957  * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
958  * racing with us, and it avoids needing to grab a spinlock here for every inode
959  * we drop the final reference on.
960  */
961 STATIC int
962 xfs_fs_drop_inode(
963         struct inode            *inode)
964 {
965         struct xfs_inode        *ip = XFS_I(inode);
966
967         /*
968          * If this unlinked inode is in the middle of recovery, don't
969          * drop the inode just yet; log recovery will take care of
970          * that.  See the comment for this inode flag.
971          */
972         if (ip->i_flags & XFS_IRECOVERY) {
973                 ASSERT(ip->i_mount->m_log->l_flags & XLOG_RECOVERY_NEEDED);
974                 return 0;
975         }
976
977         return generic_drop_inode(inode) || (ip->i_flags & XFS_IDONTCACHE);
978 }
979
980 static void
981 xfs_mount_free(
982         struct xfs_mount        *mp)
983 {
984         kfree(mp->m_rtname);
985         kfree(mp->m_logname);
986         kmem_free(mp);
987 }
988
989 STATIC int
990 xfs_fs_sync_fs(
991         struct super_block      *sb,
992         int                     wait)
993 {
994         struct xfs_mount        *mp = XFS_M(sb);
995
996         /*
997          * Doing anything during the async pass would be counterproductive.
998          */
999         if (!wait)
1000                 return 0;
1001
1002         xfs_log_force(mp, XFS_LOG_SYNC);
1003         if (laptop_mode) {
1004                 /*
1005                  * The disk must be active because we're syncing.
1006                  * We schedule log work now (now that the disk is
1007                  * active) instead of later (when it might not be).
1008                  */
1009                 flush_delayed_work(&mp->m_log->l_work);
1010         }
1011
1012         return 0;
1013 }
1014
1015 STATIC int
1016 xfs_fs_statfs(
1017         struct dentry           *dentry,
1018         struct kstatfs          *statp)
1019 {
1020         struct xfs_mount        *mp = XFS_M(dentry->d_sb);
1021         xfs_sb_t                *sbp = &mp->m_sb;
1022         struct xfs_inode        *ip = XFS_I(d_inode(dentry));
1023         uint64_t                fakeinos, id;
1024         uint64_t                icount;
1025         uint64_t                ifree;
1026         uint64_t                fdblocks;
1027         xfs_extlen_t            lsize;
1028         int64_t                 ffree;
1029
1030         statp->f_type = XFS_SUPER_MAGIC;
1031         statp->f_namelen = MAXNAMELEN - 1;
1032
1033         id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
1034         statp->f_fsid.val[0] = (u32)id;
1035         statp->f_fsid.val[1] = (u32)(id >> 32);
1036
1037         icount = percpu_counter_sum(&mp->m_icount);
1038         ifree = percpu_counter_sum(&mp->m_ifree);
1039         fdblocks = percpu_counter_sum(&mp->m_fdblocks);
1040
1041         spin_lock(&mp->m_sb_lock);
1042         statp->f_bsize = sbp->sb_blocksize;
1043         lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
1044         statp->f_blocks = sbp->sb_dblocks - lsize;
1045         spin_unlock(&mp->m_sb_lock);
1046
1047         statp->f_bfree = fdblocks - mp->m_alloc_set_aside;
1048         statp->f_bavail = statp->f_bfree;
1049
1050         fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
1051         statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
1052         if (M_IGEO(mp)->maxicount)
1053                 statp->f_files = min_t(typeof(statp->f_files),
1054                                         statp->f_files,
1055                                         M_IGEO(mp)->maxicount);
1056
1057         /* If sb_icount overshot maxicount, report actual allocation */
1058         statp->f_files = max_t(typeof(statp->f_files),
1059                                         statp->f_files,
1060                                         sbp->sb_icount);
1061
1062         /* make sure statp->f_ffree does not underflow */
1063         ffree = statp->f_files - (icount - ifree);
1064         statp->f_ffree = max_t(int64_t, ffree, 0);
1065
1066
1067         if ((ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1068             ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
1069                               (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
1070                 xfs_qm_statvfs(ip, statp);
1071
1072         if (XFS_IS_REALTIME_MOUNT(mp) &&
1073             (ip->i_d.di_flags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
1074                 statp->f_blocks = sbp->sb_rblocks;
1075                 statp->f_bavail = statp->f_bfree =
1076                         sbp->sb_frextents * sbp->sb_rextsize;
1077         }
1078
1079         return 0;
1080 }
1081
1082 STATIC void
1083 xfs_save_resvblks(struct xfs_mount *mp)
1084 {
1085         uint64_t resblks = 0;
1086
1087         mp->m_resblks_save = mp->m_resblks;
1088         xfs_reserve_blocks(mp, &resblks, NULL);
1089 }
1090
1091 STATIC void
1092 xfs_restore_resvblks(struct xfs_mount *mp)
1093 {
1094         uint64_t resblks;
1095
1096         if (mp->m_resblks_save) {
1097                 resblks = mp->m_resblks_save;
1098                 mp->m_resblks_save = 0;
1099         } else
1100                 resblks = xfs_default_resblks(mp);
1101
1102         xfs_reserve_blocks(mp, &resblks, NULL);
1103 }
1104
1105 /*
1106  * Trigger writeback of all the dirty metadata in the file system.
1107  *
1108  * This ensures that the metadata is written to their location on disk rather
1109  * than just existing in transactions in the log. This means after a quiesce
1110  * there is no log replay required to write the inodes to disk - this is the
1111  * primary difference between a sync and a quiesce.
1112  *
1113  * Note: xfs_log_quiesce() stops background log work - the callers must ensure
1114  * it is started again when appropriate.
1115  */
1116 void
1117 xfs_quiesce_attr(
1118         struct xfs_mount        *mp)
1119 {
1120         int     error = 0;
1121
1122         /* wait for all modifications to complete */
1123         while (atomic_read(&mp->m_active_trans) > 0)
1124                 delay(100);
1125
1126         /* force the log to unpin objects from the now complete transactions */
1127         xfs_log_force(mp, XFS_LOG_SYNC);
1128
1129         /* reclaim inodes to do any IO before the freeze completes */
1130         xfs_reclaim_inodes(mp, 0);
1131         xfs_reclaim_inodes(mp, SYNC_WAIT);
1132
1133         /* Push the superblock and write an unmount record */
1134         error = xfs_log_sbcount(mp);
1135         if (error)
1136                 xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
1137                                 "Frozen image may not be consistent.");
1138         /*
1139          * Just warn here till VFS can correctly support
1140          * read-only remount without racing.
1141          */
1142         WARN_ON(atomic_read(&mp->m_active_trans) != 0);
1143
1144         xfs_log_quiesce(mp);
1145 }
1146
1147 /*
1148  * Second stage of a freeze. The data is already frozen so we only
1149  * need to take care of the metadata. Once that's done sync the superblock
1150  * to the log to dirty it in case of a crash while frozen. This ensures that we
1151  * will recover the unlinked inode lists on the next mount.
1152  */
1153 STATIC int
1154 xfs_fs_freeze(
1155         struct super_block      *sb)
1156 {
1157         struct xfs_mount        *mp = XFS_M(sb);
1158
1159         xfs_stop_block_reaping(mp);
1160         xfs_save_resvblks(mp);
1161         xfs_quiesce_attr(mp);
1162         return xfs_sync_sb(mp, true);
1163 }
1164
1165 STATIC int
1166 xfs_fs_unfreeze(
1167         struct super_block      *sb)
1168 {
1169         struct xfs_mount        *mp = XFS_M(sb);
1170
1171         xfs_restore_resvblks(mp);
1172         xfs_log_work_queue(mp);
1173         xfs_start_block_reaping(mp);
1174         return 0;
1175 }
1176
1177 /*
1178  * This function fills in xfs_mount_t fields based on mount args.
1179  * Note: the superblock _has_ now been read in.
1180  */
1181 STATIC int
1182 xfs_finish_flags(
1183         struct xfs_mount        *mp)
1184 {
1185         int                     ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
1186
1187         /* Fail a mount where the logbuf is smaller than the log stripe */
1188         if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1189                 if (mp->m_logbsize <= 0 &&
1190                     mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
1191                         mp->m_logbsize = mp->m_sb.sb_logsunit;
1192                 } else if (mp->m_logbsize > 0 &&
1193                            mp->m_logbsize < mp->m_sb.sb_logsunit) {
1194                         xfs_warn(mp,
1195                 "logbuf size must be greater than or equal to log stripe size");
1196                         return -EINVAL;
1197                 }
1198         } else {
1199                 /* Fail a mount if the logbuf is larger than 32K */
1200                 if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1201                         xfs_warn(mp,
1202                 "logbuf size for version 1 logs must be 16K or 32K");
1203                         return -EINVAL;
1204                 }
1205         }
1206
1207         /*
1208          * V5 filesystems always use attr2 format for attributes.
1209          */
1210         if (xfs_sb_version_hascrc(&mp->m_sb) &&
1211             (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1212                 xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
1213                              "attr2 is always enabled for V5 filesystems.");
1214                 return -EINVAL;
1215         }
1216
1217         /*
1218          * mkfs'ed attr2 will turn on attr2 mount unless explicitly
1219          * told by noattr2 to turn it off
1220          */
1221         if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1222             !(mp->m_flags & XFS_MOUNT_NOATTR2))
1223                 mp->m_flags |= XFS_MOUNT_ATTR2;
1224
1225         /*
1226          * prohibit r/w mounts of read-only filesystems
1227          */
1228         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
1229                 xfs_warn(mp,
1230                         "cannot mount a read-only filesystem as read-write");
1231                 return -EROFS;
1232         }
1233
1234         if ((mp->m_qflags & (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE)) &&
1235             (mp->m_qflags & (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE)) &&
1236             !xfs_sb_version_has_pquotino(&mp->m_sb)) {
1237                 xfs_warn(mp,
1238                   "Super block does not support project and group quota together");
1239                 return -EINVAL;
1240         }
1241
1242         return 0;
1243 }
1244
1245 static int
1246 xfs_init_percpu_counters(
1247         struct xfs_mount        *mp)
1248 {
1249         int             error;
1250
1251         error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
1252         if (error)
1253                 return -ENOMEM;
1254
1255         error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
1256         if (error)
1257                 goto free_icount;
1258
1259         error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
1260         if (error)
1261                 goto free_ifree;
1262
1263         error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
1264         if (error)
1265                 goto free_fdblocks;
1266
1267         return 0;
1268
1269 free_fdblocks:
1270         percpu_counter_destroy(&mp->m_fdblocks);
1271 free_ifree:
1272         percpu_counter_destroy(&mp->m_ifree);
1273 free_icount:
1274         percpu_counter_destroy(&mp->m_icount);
1275         return -ENOMEM;
1276 }
1277
1278 void
1279 xfs_reinit_percpu_counters(
1280         struct xfs_mount        *mp)
1281 {
1282         percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
1283         percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
1284         percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
1285 }
1286
1287 static void
1288 xfs_destroy_percpu_counters(
1289         struct xfs_mount        *mp)
1290 {
1291         percpu_counter_destroy(&mp->m_icount);
1292         percpu_counter_destroy(&mp->m_ifree);
1293         percpu_counter_destroy(&mp->m_fdblocks);
1294         ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
1295                percpu_counter_sum(&mp->m_delalloc_blks) == 0);
1296         percpu_counter_destroy(&mp->m_delalloc_blks);
1297 }
1298
1299 static struct xfs_mount *
1300 xfs_mount_alloc(void)
1301 {
1302         struct xfs_mount        *mp;
1303
1304         mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
1305         if (!mp)
1306                 return NULL;
1307
1308         spin_lock_init(&mp->m_sb_lock);
1309         spin_lock_init(&mp->m_agirotor_lock);
1310         INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
1311         spin_lock_init(&mp->m_perag_lock);
1312         mutex_init(&mp->m_growlock);
1313         atomic_set(&mp->m_active_trans, 0);
1314         INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
1315         INIT_DELAYED_WORK(&mp->m_eofblocks_work, xfs_eofblocks_worker);
1316         INIT_DELAYED_WORK(&mp->m_cowblocks_work, xfs_cowblocks_worker);
1317         mp->m_kobj.kobject.kset = xfs_kset;
1318         /*
1319          * We don't create the finobt per-ag space reservation until after log
1320          * recovery, so we must set this to true so that an ifree transaction
1321          * started during log recovery will not depend on space reservations
1322          * for finobt expansion.
1323          */
1324         mp->m_finobt_nores = true;
1325         return mp;
1326 }
1327
1328 static int
1329 xfs_fc_fill_super(
1330         struct super_block      *sb,
1331         struct fs_context       *fc)
1332 {
1333         struct xfs_mount        *mp = sb->s_fs_info;
1334         struct inode            *root;
1335         int                     flags = 0, error = -ENOMEM;
1336
1337         mp->m_super = sb;
1338
1339         error = xfs_fc_validate_params(mp);
1340         if (error)
1341                 goto out_free_names;
1342
1343         sb_min_blocksize(sb, BBSIZE);
1344         sb->s_xattr = xfs_xattr_handlers;
1345         sb->s_export_op = &xfs_export_operations;
1346 #ifdef CONFIG_XFS_QUOTA
1347         sb->s_qcop = &xfs_quotactl_operations;
1348         sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1349 #endif
1350         sb->s_op = &xfs_super_operations;
1351
1352         /*
1353          * Delay mount work if the debug hook is set. This is debug
1354          * instrumention to coordinate simulation of xfs mount failures with
1355          * VFS superblock operations
1356          */
1357         if (xfs_globals.mount_delay) {
1358                 xfs_notice(mp, "Delaying mount for %d seconds.",
1359                         xfs_globals.mount_delay);
1360                 msleep(xfs_globals.mount_delay * 1000);
1361         }
1362
1363         if (fc->sb_flags & SB_SILENT)
1364                 flags |= XFS_MFSI_QUIET;
1365
1366         error = xfs_open_devices(mp);
1367         if (error)
1368                 goto out_free_names;
1369
1370         error = xfs_init_mount_workqueues(mp);
1371         if (error)
1372                 goto out_close_devices;
1373
1374         error = xfs_init_percpu_counters(mp);
1375         if (error)
1376                 goto out_destroy_workqueues;
1377
1378         /* Allocate stats memory before we do operations that might use it */
1379         mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1380         if (!mp->m_stats.xs_stats) {
1381                 error = -ENOMEM;
1382                 goto out_destroy_counters;
1383         }
1384
1385         error = xfs_readsb(mp, flags);
1386         if (error)
1387                 goto out_free_stats;
1388
1389         error = xfs_finish_flags(mp);
1390         if (error)
1391                 goto out_free_sb;
1392
1393         error = xfs_setup_devices(mp);
1394         if (error)
1395                 goto out_free_sb;
1396
1397         error = xfs_filestream_mount(mp);
1398         if (error)
1399                 goto out_free_sb;
1400
1401         /*
1402          * we must configure the block size in the superblock before we run the
1403          * full mount process as the mount process can lookup and cache inodes.
1404          */
1405         sb->s_magic = XFS_SUPER_MAGIC;
1406         sb->s_blocksize = mp->m_sb.sb_blocksize;
1407         sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1408         sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
1409         sb->s_max_links = XFS_MAXLINK;
1410         sb->s_time_gran = 1;
1411         sb->s_time_min = S32_MIN;
1412         sb->s_time_max = S32_MAX;
1413         sb->s_iflags |= SB_I_CGROUPWB;
1414
1415         set_posix_acl_flag(sb);
1416
1417         /* version 5 superblocks support inode version counters. */
1418         if (XFS_SB_VERSION_NUM(&mp->m_sb) == XFS_SB_VERSION_5)
1419                 sb->s_flags |= SB_I_VERSION;
1420
1421         if (mp->m_flags & XFS_MOUNT_DAX) {
1422                 bool rtdev_is_dax = false, datadev_is_dax;
1423
1424                 xfs_warn(mp,
1425                 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
1426
1427                 datadev_is_dax = bdev_dax_supported(mp->m_ddev_targp->bt_bdev,
1428                         sb->s_blocksize);
1429                 if (mp->m_rtdev_targp)
1430                         rtdev_is_dax = bdev_dax_supported(
1431                                 mp->m_rtdev_targp->bt_bdev, sb->s_blocksize);
1432                 if (!rtdev_is_dax && !datadev_is_dax) {
1433                         xfs_alert(mp,
1434                         "DAX unsupported by block device. Turning off DAX.");
1435                         mp->m_flags &= ~XFS_MOUNT_DAX;
1436                 }
1437                 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
1438                         xfs_alert(mp,
1439                 "DAX and reflink cannot be used together!");
1440                         error = -EINVAL;
1441                         goto out_filestream_unmount;
1442                 }
1443         }
1444
1445         if (mp->m_flags & XFS_MOUNT_DISCARD) {
1446                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1447
1448                 if (!blk_queue_discard(q)) {
1449                         xfs_warn(mp, "mounting with \"discard\" option, but "
1450                                         "the device does not support discard");
1451                         mp->m_flags &= ~XFS_MOUNT_DISCARD;
1452                 }
1453         }
1454
1455         if (xfs_sb_version_hasreflink(&mp->m_sb)) {
1456                 if (mp->m_sb.sb_rblocks) {
1457                         xfs_alert(mp,
1458         "reflink not compatible with realtime device!");
1459                         error = -EINVAL;
1460                         goto out_filestream_unmount;
1461                 }
1462
1463                 if (xfs_globals.always_cow) {
1464                         xfs_info(mp, "using DEBUG-only always_cow mode.");
1465                         mp->m_always_cow = true;
1466                 }
1467         }
1468
1469         if (xfs_sb_version_hasrmapbt(&mp->m_sb) && mp->m_sb.sb_rblocks) {
1470                 xfs_alert(mp,
1471         "reverse mapping btree not compatible with realtime device!");
1472                 error = -EINVAL;
1473                 goto out_filestream_unmount;
1474         }
1475
1476         error = xfs_mountfs(mp);
1477         if (error)
1478                 goto out_filestream_unmount;
1479
1480         root = igrab(VFS_I(mp->m_rootip));
1481         if (!root) {
1482                 error = -ENOENT;
1483                 goto out_unmount;
1484         }
1485         sb->s_root = d_make_root(root);
1486         if (!sb->s_root) {
1487                 error = -ENOMEM;
1488                 goto out_unmount;
1489         }
1490
1491         return 0;
1492
1493  out_filestream_unmount:
1494         xfs_filestream_unmount(mp);
1495  out_free_sb:
1496         xfs_freesb(mp);
1497  out_free_stats:
1498         free_percpu(mp->m_stats.xs_stats);
1499  out_destroy_counters:
1500         xfs_destroy_percpu_counters(mp);
1501  out_destroy_workqueues:
1502         xfs_destroy_mount_workqueues(mp);
1503  out_close_devices:
1504         xfs_close_devices(mp);
1505  out_free_names:
1506         sb->s_fs_info = NULL;
1507         xfs_mount_free(mp);
1508         return error;
1509
1510  out_unmount:
1511         xfs_filestream_unmount(mp);
1512         xfs_unmountfs(mp);
1513         goto out_free_sb;
1514 }
1515
1516 static int
1517 xfs_fc_get_tree(
1518         struct fs_context       *fc)
1519 {
1520         return get_tree_bdev(fc, xfs_fc_fill_super);
1521 }
1522
1523 STATIC void
1524 xfs_fs_put_super(
1525         struct super_block      *sb)
1526 {
1527         struct xfs_mount        *mp = XFS_M(sb);
1528
1529         /* if ->fill_super failed, we have no mount to tear down */
1530         if (!sb->s_fs_info)
1531                 return;
1532
1533         xfs_notice(mp, "Unmounting Filesystem");
1534         xfs_filestream_unmount(mp);
1535         xfs_unmountfs(mp);
1536
1537         xfs_freesb(mp);
1538         free_percpu(mp->m_stats.xs_stats);
1539         xfs_destroy_percpu_counters(mp);
1540         xfs_destroy_mount_workqueues(mp);
1541         xfs_close_devices(mp);
1542
1543         sb->s_fs_info = NULL;
1544         xfs_mount_free(mp);
1545 }
1546
1547 static long
1548 xfs_fs_nr_cached_objects(
1549         struct super_block      *sb,
1550         struct shrink_control   *sc)
1551 {
1552         /* Paranoia: catch incorrect calls during mount setup or teardown */
1553         if (WARN_ON_ONCE(!sb->s_fs_info))
1554                 return 0;
1555         return xfs_reclaim_inodes_count(XFS_M(sb));
1556 }
1557
1558 static long
1559 xfs_fs_free_cached_objects(
1560         struct super_block      *sb,
1561         struct shrink_control   *sc)
1562 {
1563         return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
1564 }
1565
1566 static const struct super_operations xfs_super_operations = {
1567         .alloc_inode            = xfs_fs_alloc_inode,
1568         .destroy_inode          = xfs_fs_destroy_inode,
1569         .dirty_inode            = xfs_fs_dirty_inode,
1570         .drop_inode             = xfs_fs_drop_inode,
1571         .put_super              = xfs_fs_put_super,
1572         .sync_fs                = xfs_fs_sync_fs,
1573         .freeze_fs              = xfs_fs_freeze,
1574         .unfreeze_fs            = xfs_fs_unfreeze,
1575         .statfs                 = xfs_fs_statfs,
1576         .show_options           = xfs_fs_show_options,
1577         .nr_cached_objects      = xfs_fs_nr_cached_objects,
1578         .free_cached_objects    = xfs_fs_free_cached_objects,
1579 };
1580
1581 static int
1582 xfs_remount_rw(
1583         struct xfs_mount        *mp)
1584 {
1585         struct xfs_sb           *sbp = &mp->m_sb;
1586         int error;
1587
1588         if (mp->m_flags & XFS_MOUNT_NORECOVERY) {
1589                 xfs_warn(mp,
1590                         "ro->rw transition prohibited on norecovery mount");
1591                 return -EINVAL;
1592         }
1593
1594         if (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
1595             xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
1596                 xfs_warn(mp,
1597         "ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
1598                         (sbp->sb_features_ro_compat &
1599                                 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
1600                 return -EINVAL;
1601         }
1602
1603         mp->m_flags &= ~XFS_MOUNT_RDONLY;
1604
1605         /*
1606          * If this is the first remount to writeable state we might have some
1607          * superblock changes to update.
1608          */
1609         if (mp->m_update_sb) {
1610                 error = xfs_sync_sb(mp, false);
1611                 if (error) {
1612                         xfs_warn(mp, "failed to write sb changes");
1613                         return error;
1614                 }
1615                 mp->m_update_sb = false;
1616         }
1617
1618         /*
1619          * Fill out the reserve pool if it is empty. Use the stashed value if
1620          * it is non-zero, otherwise go with the default.
1621          */
1622         xfs_restore_resvblks(mp);
1623         xfs_log_work_queue(mp);
1624
1625         /* Recover any CoW blocks that never got remapped. */
1626         error = xfs_reflink_recover_cow(mp);
1627         if (error) {
1628                 xfs_err(mp,
1629                         "Error %d recovering leftover CoW allocations.", error);
1630                         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1631                 return error;
1632         }
1633         xfs_start_block_reaping(mp);
1634
1635         /* Create the per-AG metadata reservation pool .*/
1636         error = xfs_fs_reserve_ag_blocks(mp);
1637         if (error && error != -ENOSPC)
1638                 return error;
1639
1640         return 0;
1641 }
1642
1643 static int
1644 xfs_remount_ro(
1645         struct xfs_mount        *mp)
1646 {
1647         int error;
1648
1649         /*
1650          * Cancel background eofb scanning so it cannot race with the final
1651          * log force+buftarg wait and deadlock the remount.
1652          */
1653         xfs_stop_block_reaping(mp);
1654
1655         /* Get rid of any leftover CoW reservations... */
1656         error = xfs_icache_free_cowblocks(mp, NULL);
1657         if (error) {
1658                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1659                 return error;
1660         }
1661
1662         /* Free the per-AG metadata reservation pool. */
1663         error = xfs_fs_unreserve_ag_blocks(mp);
1664         if (error) {
1665                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1666                 return error;
1667         }
1668
1669         /*
1670          * Before we sync the metadata, we need to free up the reserve block
1671          * pool so that the used block count in the superblock on disk is
1672          * correct at the end of the remount. Stash the current* reserve pool
1673          * size so that if we get remounted rw, we can return it to the same
1674          * size.
1675          */
1676         xfs_save_resvblks(mp);
1677
1678         xfs_quiesce_attr(mp);
1679         mp->m_flags |= XFS_MOUNT_RDONLY;
1680
1681         return 0;
1682 }
1683
1684 /*
1685  * Logically we would return an error here to prevent users from believing
1686  * they might have changed mount options using remount which can't be changed.
1687  *
1688  * But unfortunately mount(8) adds all options from mtab and fstab to the mount
1689  * arguments in some cases so we can't blindly reject options, but have to
1690  * check for each specified option if it actually differs from the currently
1691  * set option and only reject it if that's the case.
1692  *
1693  * Until that is implemented we return success for every remount request, and
1694  * silently ignore all options that we can't actually change.
1695  */
1696 static int
1697 xfs_fc_reconfigure(
1698         struct fs_context *fc)
1699 {
1700         struct xfs_mount        *mp = XFS_M(fc->root->d_sb);
1701         struct xfs_mount        *new_mp = fc->s_fs_info;
1702         xfs_sb_t                *sbp = &mp->m_sb;
1703         int                     flags = fc->sb_flags;
1704         int                     error;
1705
1706         error = xfs_fc_validate_params(new_mp);
1707         if (error)
1708                 return error;
1709
1710         sync_filesystem(mp->m_super);
1711
1712         /* inode32 -> inode64 */
1713         if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) &&
1714             !(new_mp->m_flags & XFS_MOUNT_SMALL_INUMS)) {
1715                 mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
1716                 mp->m_maxagi = xfs_set_inode_alloc(mp, sbp->sb_agcount);
1717         }
1718
1719         /* inode64 -> inode32 */
1720         if (!(mp->m_flags & XFS_MOUNT_SMALL_INUMS) &&
1721             (new_mp->m_flags & XFS_MOUNT_SMALL_INUMS)) {
1722                 mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
1723                 mp->m_maxagi = xfs_set_inode_alloc(mp, sbp->sb_agcount);
1724         }
1725
1726         /* ro -> rw */
1727         if ((mp->m_flags & XFS_MOUNT_RDONLY) && !(flags & SB_RDONLY)) {
1728                 error = xfs_remount_rw(mp);
1729                 if (error)
1730                         return error;
1731         }
1732
1733         /* rw -> ro */
1734         if (!(mp->m_flags & XFS_MOUNT_RDONLY) && (flags & SB_RDONLY)) {
1735                 error = xfs_remount_ro(mp);
1736                 if (error)
1737                         return error;
1738         }
1739
1740         return 0;
1741 }
1742
1743 static void xfs_fc_free(
1744         struct fs_context       *fc)
1745 {
1746         struct xfs_mount        *mp = fc->s_fs_info;
1747
1748         /*
1749          * mp is stored in the fs_context when it is initialized.
1750          * mp is transferred to the superblock on a successful mount,
1751          * but if an error occurs before the transfer we have to free
1752          * it here.
1753          */
1754         if (mp)
1755                 xfs_mount_free(mp);
1756 }
1757
1758 static const struct fs_context_operations xfs_context_ops = {
1759         .parse_param = xfs_fc_parse_param,
1760         .get_tree    = xfs_fc_get_tree,
1761         .reconfigure = xfs_fc_reconfigure,
1762         .free        = xfs_fc_free,
1763 };
1764
1765 static int xfs_init_fs_context(
1766         struct fs_context       *fc)
1767 {
1768         struct xfs_mount        *mp;
1769
1770         mp = xfs_mount_alloc();
1771         if (!mp)
1772                 return -ENOMEM;
1773
1774         /*
1775          * These can be overridden by the mount option parsing.
1776          */
1777         mp->m_logbufs = -1;
1778         mp->m_logbsize = -1;
1779         mp->m_allocsize_log = 16; /* 64k */
1780
1781         /*
1782          * Copy binary VFS mount flags we are interested in.
1783          */
1784         if (fc->sb_flags & SB_RDONLY)
1785                 mp->m_flags |= XFS_MOUNT_RDONLY;
1786         if (fc->sb_flags & SB_DIRSYNC)
1787                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
1788         if (fc->sb_flags & SB_SYNCHRONOUS)
1789                 mp->m_flags |= XFS_MOUNT_WSYNC;
1790
1791         fc->s_fs_info = mp;
1792         fc->ops = &xfs_context_ops;
1793
1794         return 0;
1795 }
1796
1797 static struct file_system_type xfs_fs_type = {
1798         .owner                  = THIS_MODULE,
1799         .name                   = "xfs",
1800         .init_fs_context        = xfs_init_fs_context,
1801         .parameters             = &xfs_fs_parameters,
1802         .kill_sb                = kill_block_super,
1803         .fs_flags               = FS_REQUIRES_DEV,
1804 };
1805 MODULE_ALIAS_FS("xfs");
1806
1807 STATIC int __init
1808 xfs_init_zones(void)
1809 {
1810         xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
1811                                                 "xfs_log_ticket");
1812         if (!xfs_log_ticket_zone)
1813                 goto out;
1814
1815         xfs_bmap_free_item_zone = kmem_zone_init(
1816                         sizeof(struct xfs_extent_free_item),
1817                         "xfs_bmap_free_item");
1818         if (!xfs_bmap_free_item_zone)
1819                 goto out_destroy_log_ticket_zone;
1820
1821         xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
1822                                                 "xfs_btree_cur");
1823         if (!xfs_btree_cur_zone)
1824                 goto out_destroy_bmap_free_item_zone;
1825
1826         xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
1827                                                 "xfs_da_state");
1828         if (!xfs_da_state_zone)
1829                 goto out_destroy_btree_cur_zone;
1830
1831         xfs_ifork_zone = kmem_zone_init(sizeof(struct xfs_ifork), "xfs_ifork");
1832         if (!xfs_ifork_zone)
1833                 goto out_destroy_da_state_zone;
1834
1835         xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
1836         if (!xfs_trans_zone)
1837                 goto out_destroy_ifork_zone;
1838
1839
1840         /*
1841          * The size of the zone allocated buf log item is the maximum
1842          * size possible under XFS.  This wastes a little bit of memory,
1843          * but it is much faster.
1844          */
1845         xfs_buf_item_zone = kmem_zone_init(sizeof(struct xfs_buf_log_item),
1846                                            "xfs_buf_item");
1847         if (!xfs_buf_item_zone)
1848                 goto out_destroy_trans_zone;
1849
1850         xfs_efd_zone = kmem_zone_init((sizeof(xfs_efd_log_item_t) +
1851                         ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
1852                                  sizeof(xfs_extent_t))), "xfs_efd_item");
1853         if (!xfs_efd_zone)
1854                 goto out_destroy_buf_item_zone;
1855
1856         xfs_efi_zone = kmem_zone_init((sizeof(xfs_efi_log_item_t) +
1857                         ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
1858                                 sizeof(xfs_extent_t))), "xfs_efi_item");
1859         if (!xfs_efi_zone)
1860                 goto out_destroy_efd_zone;
1861
1862         xfs_inode_zone =
1863                 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
1864                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM | KM_ZONE_SPREAD |
1865                         KM_ZONE_ACCOUNT, xfs_fs_inode_init_once);
1866         if (!xfs_inode_zone)
1867                 goto out_destroy_efi_zone;
1868
1869         xfs_ili_zone =
1870                 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
1871                                         KM_ZONE_SPREAD, NULL);
1872         if (!xfs_ili_zone)
1873                 goto out_destroy_inode_zone;
1874         xfs_icreate_zone = kmem_zone_init(sizeof(struct xfs_icreate_item),
1875                                         "xfs_icr");
1876         if (!xfs_icreate_zone)
1877                 goto out_destroy_ili_zone;
1878
1879         xfs_rud_zone = kmem_zone_init(sizeof(struct xfs_rud_log_item),
1880                         "xfs_rud_item");
1881         if (!xfs_rud_zone)
1882                 goto out_destroy_icreate_zone;
1883
1884         xfs_rui_zone = kmem_zone_init(
1885                         xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
1886                         "xfs_rui_item");
1887         if (!xfs_rui_zone)
1888                 goto out_destroy_rud_zone;
1889
1890         xfs_cud_zone = kmem_zone_init(sizeof(struct xfs_cud_log_item),
1891                         "xfs_cud_item");
1892         if (!xfs_cud_zone)
1893                 goto out_destroy_rui_zone;
1894
1895         xfs_cui_zone = kmem_zone_init(
1896                         xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
1897                         "xfs_cui_item");
1898         if (!xfs_cui_zone)
1899                 goto out_destroy_cud_zone;
1900
1901         xfs_bud_zone = kmem_zone_init(sizeof(struct xfs_bud_log_item),
1902                         "xfs_bud_item");
1903         if (!xfs_bud_zone)
1904                 goto out_destroy_cui_zone;
1905
1906         xfs_bui_zone = kmem_zone_init(
1907                         xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
1908                         "xfs_bui_item");
1909         if (!xfs_bui_zone)
1910                 goto out_destroy_bud_zone;
1911
1912         return 0;
1913
1914  out_destroy_bud_zone:
1915         kmem_zone_destroy(xfs_bud_zone);
1916  out_destroy_cui_zone:
1917         kmem_zone_destroy(xfs_cui_zone);
1918  out_destroy_cud_zone:
1919         kmem_zone_destroy(xfs_cud_zone);
1920  out_destroy_rui_zone:
1921         kmem_zone_destroy(xfs_rui_zone);
1922  out_destroy_rud_zone:
1923         kmem_zone_destroy(xfs_rud_zone);
1924  out_destroy_icreate_zone:
1925         kmem_zone_destroy(xfs_icreate_zone);
1926  out_destroy_ili_zone:
1927         kmem_zone_destroy(xfs_ili_zone);
1928  out_destroy_inode_zone:
1929         kmem_zone_destroy(xfs_inode_zone);
1930  out_destroy_efi_zone:
1931         kmem_zone_destroy(xfs_efi_zone);
1932  out_destroy_efd_zone:
1933         kmem_zone_destroy(xfs_efd_zone);
1934  out_destroy_buf_item_zone:
1935         kmem_zone_destroy(xfs_buf_item_zone);
1936  out_destroy_trans_zone:
1937         kmem_zone_destroy(xfs_trans_zone);
1938  out_destroy_ifork_zone:
1939         kmem_zone_destroy(xfs_ifork_zone);
1940  out_destroy_da_state_zone:
1941         kmem_zone_destroy(xfs_da_state_zone);
1942  out_destroy_btree_cur_zone:
1943         kmem_zone_destroy(xfs_btree_cur_zone);
1944  out_destroy_bmap_free_item_zone:
1945         kmem_zone_destroy(xfs_bmap_free_item_zone);
1946  out_destroy_log_ticket_zone:
1947         kmem_zone_destroy(xfs_log_ticket_zone);
1948  out:
1949         return -ENOMEM;
1950 }
1951
1952 STATIC void
1953 xfs_destroy_zones(void)
1954 {
1955         /*
1956          * Make sure all delayed rcu free are flushed before we
1957          * destroy caches.
1958          */
1959         rcu_barrier();
1960         kmem_zone_destroy(xfs_bui_zone);
1961         kmem_zone_destroy(xfs_bud_zone);
1962         kmem_zone_destroy(xfs_cui_zone);
1963         kmem_zone_destroy(xfs_cud_zone);
1964         kmem_zone_destroy(xfs_rui_zone);
1965         kmem_zone_destroy(xfs_rud_zone);
1966         kmem_zone_destroy(xfs_icreate_zone);
1967         kmem_zone_destroy(xfs_ili_zone);
1968         kmem_zone_destroy(xfs_inode_zone);
1969         kmem_zone_destroy(xfs_efi_zone);
1970         kmem_zone_destroy(xfs_efd_zone);
1971         kmem_zone_destroy(xfs_buf_item_zone);
1972         kmem_zone_destroy(xfs_trans_zone);
1973         kmem_zone_destroy(xfs_ifork_zone);
1974         kmem_zone_destroy(xfs_da_state_zone);
1975         kmem_zone_destroy(xfs_btree_cur_zone);
1976         kmem_zone_destroy(xfs_bmap_free_item_zone);
1977         kmem_zone_destroy(xfs_log_ticket_zone);
1978 }
1979
1980 STATIC int __init
1981 xfs_init_workqueues(void)
1982 {
1983         /*
1984          * The allocation workqueue can be used in memory reclaim situations
1985          * (writepage path), and parallelism is only limited by the number of
1986          * AGs in all the filesystems mounted. Hence use the default large
1987          * max_active value for this workqueue.
1988          */
1989         xfs_alloc_wq = alloc_workqueue("xfsalloc",
1990                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0);
1991         if (!xfs_alloc_wq)
1992                 return -ENOMEM;
1993
1994         xfs_discard_wq = alloc_workqueue("xfsdiscard", WQ_UNBOUND, 0);
1995         if (!xfs_discard_wq)
1996                 goto out_free_alloc_wq;
1997
1998         return 0;
1999 out_free_alloc_wq:
2000         destroy_workqueue(xfs_alloc_wq);
2001         return -ENOMEM;
2002 }
2003
2004 STATIC void
2005 xfs_destroy_workqueues(void)
2006 {
2007         destroy_workqueue(xfs_discard_wq);
2008         destroy_workqueue(xfs_alloc_wq);
2009 }
2010
2011 STATIC int __init
2012 init_xfs_fs(void)
2013 {
2014         int                     error;
2015
2016         xfs_check_ondisk_structs();
2017
2018         printk(KERN_INFO XFS_VERSION_STRING " with "
2019                          XFS_BUILD_OPTIONS " enabled\n");
2020
2021         xfs_dir_startup();
2022
2023         error = xfs_init_zones();
2024         if (error)
2025                 goto out;
2026
2027         error = xfs_init_workqueues();
2028         if (error)
2029                 goto out_destroy_zones;
2030
2031         error = xfs_mru_cache_init();
2032         if (error)
2033                 goto out_destroy_wq;
2034
2035         error = xfs_buf_init();
2036         if (error)
2037                 goto out_mru_cache_uninit;
2038
2039         error = xfs_init_procfs();
2040         if (error)
2041                 goto out_buf_terminate;
2042
2043         error = xfs_sysctl_register();
2044         if (error)
2045                 goto out_cleanup_procfs;
2046
2047         xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
2048         if (!xfs_kset) {
2049                 error = -ENOMEM;
2050                 goto out_sysctl_unregister;
2051         }
2052
2053         xfsstats.xs_kobj.kobject.kset = xfs_kset;
2054
2055         xfsstats.xs_stats = alloc_percpu(struct xfsstats);
2056         if (!xfsstats.xs_stats) {
2057                 error = -ENOMEM;
2058                 goto out_kset_unregister;
2059         }
2060
2061         error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2062                                "stats");
2063         if (error)
2064                 goto out_free_stats;
2065
2066 #ifdef DEBUG
2067         xfs_dbg_kobj.kobject.kset = xfs_kset;
2068         error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2069         if (error)
2070                 goto out_remove_stats_kobj;
2071 #endif
2072
2073         error = xfs_qm_init();
2074         if (error)
2075                 goto out_remove_dbg_kobj;
2076
2077         error = register_filesystem(&xfs_fs_type);
2078         if (error)
2079                 goto out_qm_exit;
2080         return 0;
2081
2082  out_qm_exit:
2083         xfs_qm_exit();
2084  out_remove_dbg_kobj:
2085 #ifdef DEBUG
2086         xfs_sysfs_del(&xfs_dbg_kobj);
2087  out_remove_stats_kobj:
2088 #endif
2089         xfs_sysfs_del(&xfsstats.xs_kobj);
2090  out_free_stats:
2091         free_percpu(xfsstats.xs_stats);
2092  out_kset_unregister:
2093         kset_unregister(xfs_kset);
2094  out_sysctl_unregister:
2095         xfs_sysctl_unregister();
2096  out_cleanup_procfs:
2097         xfs_cleanup_procfs();
2098  out_buf_terminate:
2099         xfs_buf_terminate();
2100  out_mru_cache_uninit:
2101         xfs_mru_cache_uninit();
2102  out_destroy_wq:
2103         xfs_destroy_workqueues();
2104  out_destroy_zones:
2105         xfs_destroy_zones();
2106  out:
2107         return error;
2108 }
2109
2110 STATIC void __exit
2111 exit_xfs_fs(void)
2112 {
2113         xfs_qm_exit();
2114         unregister_filesystem(&xfs_fs_type);
2115 #ifdef DEBUG
2116         xfs_sysfs_del(&xfs_dbg_kobj);
2117 #endif
2118         xfs_sysfs_del(&xfsstats.xs_kobj);
2119         free_percpu(xfsstats.xs_stats);
2120         kset_unregister(xfs_kset);
2121         xfs_sysctl_unregister();
2122         xfs_cleanup_procfs();
2123         xfs_buf_terminate();
2124         xfs_mru_cache_uninit();
2125         xfs_destroy_workqueues();
2126         xfs_destroy_zones();
2127         xfs_uuid_table_free();
2128 }
2129
2130 module_init(init_xfs_fs);
2131 module_exit(exit_xfs_fs);
2132
2133 MODULE_AUTHOR("Silicon Graphics, Inc.");
2134 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2135 MODULE_LICENSE("GPL");