Line data Source code
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 : #include "xfs_pwork.h"
39 : #include "xfs_ag.h"
40 : #include "xfs_defer.h"
41 : #include "xfs_attr_item.h"
42 : #include "xfs_xattr.h"
43 : #include "xfs_iunlink_item.h"
44 : #include "xfs_dahash_test.h"
45 : #include "xfs_swapext_item.h"
46 : #include "xfs_parent.h"
47 : #include "xfs_rtbitmap.h"
48 : #include "xfs_rtalloc.h"
49 : #include "scrub/stats.h"
50 : #include "scrub/rcbag_btree.h"
51 :
52 : #include <linux/magic.h>
53 : #include <linux/fs_context.h>
54 : #include <linux/fs_parser.h>
55 :
56 : static const struct super_operations xfs_super_operations;
57 :
58 : static struct dentry *xfs_debugfs; /* top-level xfs debugfs dir */
59 : static struct kset *xfs_kset; /* top-level xfs sysfs dir */
60 : #ifdef DEBUG
61 : static struct xfs_kobj xfs_dbg_kobj; /* global debug sysfs attrs */
62 : #endif
63 :
64 : #ifdef CONFIG_HOTPLUG_CPU
65 : static LIST_HEAD(xfs_mount_list);
66 : static DEFINE_SPINLOCK(xfs_mount_list_lock);
67 :
68 67694 : static inline void xfs_mount_list_add(struct xfs_mount *mp)
69 : {
70 67694 : spin_lock(&xfs_mount_list_lock);
71 67694 : list_add(&mp->m_mount_list, &xfs_mount_list);
72 67694 : spin_unlock(&xfs_mount_list_lock);
73 67694 : }
74 :
75 67705 : static inline void xfs_mount_list_del(struct xfs_mount *mp)
76 : {
77 67705 : spin_lock(&xfs_mount_list_lock);
78 67705 : list_del(&mp->m_mount_list);
79 67705 : spin_unlock(&xfs_mount_list_lock);
80 67705 : }
81 : #else /* !CONFIG_HOTPLUG_CPU */
82 : static inline void xfs_mount_list_add(struct xfs_mount *mp) {}
83 : static inline void xfs_mount_list_del(struct xfs_mount *mp) {}
84 : #endif
85 :
86 : enum xfs_dax_mode {
87 : XFS_DAX_INODE = 0,
88 : XFS_DAX_ALWAYS = 1,
89 : XFS_DAX_NEVER = 2,
90 : };
91 :
92 : static void
93 44 : xfs_mount_set_dax_mode(
94 : struct xfs_mount *mp,
95 : enum xfs_dax_mode mode)
96 : {
97 44 : switch (mode) {
98 0 : case XFS_DAX_INODE:
99 0 : mp->m_features &= ~(XFS_FEAT_DAX_ALWAYS | XFS_FEAT_DAX_NEVER);
100 0 : break;
101 113 : case XFS_DAX_ALWAYS:
102 113 : mp->m_features |= XFS_FEAT_DAX_ALWAYS;
103 113 : mp->m_features &= ~XFS_FEAT_DAX_NEVER;
104 44 : break;
105 0 : case XFS_DAX_NEVER:
106 113 : mp->m_features |= XFS_FEAT_DAX_NEVER;
107 113 : mp->m_features &= ~XFS_FEAT_DAX_ALWAYS;
108 0 : break;
109 : }
110 44 : }
111 :
112 : static const struct constant_table dax_param_enums[] = {
113 : {"inode", XFS_DAX_INODE },
114 : {"always", XFS_DAX_ALWAYS },
115 : {"never", XFS_DAX_NEVER },
116 : {}
117 : };
118 :
119 : /*
120 : * Table driven mount option parser.
121 : */
122 : enum {
123 : Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
124 : Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
125 : Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
126 : Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32, Opt_ikeep,
127 : Opt_noikeep, Opt_largeio, Opt_nolargeio, Opt_attr2, Opt_noattr2,
128 : Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
129 : Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
130 : Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
131 : Opt_discard, Opt_nodiscard, Opt_dax, Opt_dax_enum,
132 : };
133 :
134 : static const struct fs_parameter_spec xfs_fs_parameters[] = {
135 : fsparam_u32("logbufs", Opt_logbufs),
136 : fsparam_string("logbsize", Opt_logbsize),
137 : fsparam_string("logdev", Opt_logdev),
138 : fsparam_string("rtdev", Opt_rtdev),
139 : fsparam_flag("wsync", Opt_wsync),
140 : fsparam_flag("noalign", Opt_noalign),
141 : fsparam_flag("swalloc", Opt_swalloc),
142 : fsparam_u32("sunit", Opt_sunit),
143 : fsparam_u32("swidth", Opt_swidth),
144 : fsparam_flag("nouuid", Opt_nouuid),
145 : fsparam_flag("grpid", Opt_grpid),
146 : fsparam_flag("nogrpid", Opt_nogrpid),
147 : fsparam_flag("bsdgroups", Opt_bsdgroups),
148 : fsparam_flag("sysvgroups", Opt_sysvgroups),
149 : fsparam_string("allocsize", Opt_allocsize),
150 : fsparam_flag("norecovery", Opt_norecovery),
151 : fsparam_flag("inode64", Opt_inode64),
152 : fsparam_flag("inode32", Opt_inode32),
153 : fsparam_flag("ikeep", Opt_ikeep),
154 : fsparam_flag("noikeep", Opt_noikeep),
155 : fsparam_flag("largeio", Opt_largeio),
156 : fsparam_flag("nolargeio", Opt_nolargeio),
157 : fsparam_flag("attr2", Opt_attr2),
158 : fsparam_flag("noattr2", Opt_noattr2),
159 : fsparam_flag("filestreams", Opt_filestreams),
160 : fsparam_flag("quota", Opt_quota),
161 : fsparam_flag("noquota", Opt_noquota),
162 : fsparam_flag("usrquota", Opt_usrquota),
163 : fsparam_flag("grpquota", Opt_grpquota),
164 : fsparam_flag("prjquota", Opt_prjquota),
165 : fsparam_flag("uquota", Opt_uquota),
166 : fsparam_flag("gquota", Opt_gquota),
167 : fsparam_flag("pquota", Opt_pquota),
168 : fsparam_flag("uqnoenforce", Opt_uqnoenforce),
169 : fsparam_flag("gqnoenforce", Opt_gqnoenforce),
170 : fsparam_flag("pqnoenforce", Opt_pqnoenforce),
171 : fsparam_flag("qnoenforce", Opt_qnoenforce),
172 : fsparam_flag("discard", Opt_discard),
173 : fsparam_flag("nodiscard", Opt_nodiscard),
174 : fsparam_flag("dax", Opt_dax),
175 : fsparam_enum("dax", Opt_dax_enum, dax_param_enums),
176 : {}
177 : };
178 :
179 : struct proc_xfs_info {
180 : uint64_t flag;
181 : char *str;
182 : };
183 :
184 : static int
185 16161069 : xfs_fs_show_options(
186 : struct seq_file *m,
187 : struct dentry *root)
188 : {
189 16161069 : static struct proc_xfs_info xfs_info_set[] = {
190 : /* the few simple ones we can get from the mount struct */
191 : { XFS_FEAT_IKEEP, ",ikeep" },
192 : { XFS_FEAT_WSYNC, ",wsync" },
193 : { XFS_FEAT_NOALIGN, ",noalign" },
194 : { XFS_FEAT_SWALLOC, ",swalloc" },
195 : { XFS_FEAT_NOUUID, ",nouuid" },
196 : { XFS_FEAT_NORECOVERY, ",norecovery" },
197 : { XFS_FEAT_ATTR2, ",attr2" },
198 : { XFS_FEAT_FILESTREAMS, ",filestreams" },
199 : { XFS_FEAT_GRPID, ",grpid" },
200 : { XFS_FEAT_DISCARD, ",discard" },
201 : { XFS_FEAT_LARGE_IOSIZE, ",largeio" },
202 : { XFS_FEAT_DAX_ALWAYS, ",dax=always" },
203 : { XFS_FEAT_DAX_NEVER, ",dax=never" },
204 : { 0, NULL }
205 : };
206 16161069 : struct xfs_mount *mp = XFS_M(root->d_sb);
207 16161069 : struct proc_xfs_info *xfs_infop;
208 :
209 225133744 : for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
210 208996275 : if (mp->m_features & xfs_infop->flag)
211 16195655 : seq_puts(m, xfs_infop->str);
212 : }
213 :
214 32274759 : seq_printf(m, ",inode%d", xfs_has_small_inums(mp) ? 32 : 64);
215 :
216 16140154 : if (xfs_has_allocsize(mp))
217 428 : seq_printf(m, ",allocsize=%dk",
218 214 : (1 << mp->m_allocsize_log) >> 10);
219 :
220 16151411 : if (mp->m_logbufs > 0)
221 16145246 : seq_printf(m, ",logbufs=%d", mp->m_logbufs);
222 16125289 : if (mp->m_logbsize > 0)
223 16121893 : seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
224 :
225 16121461 : if (mp->m_logname)
226 6243293 : seq_show_option(m, "logdev", mp->m_logname);
227 16123325 : if (mp->m_rtname)
228 7282181 : seq_show_option(m, "rtdev", mp->m_rtname);
229 :
230 16126453 : if (mp->m_dalign > 0)
231 2456426 : seq_printf(m, ",sunit=%d",
232 1228213 : (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
233 16124541 : if (mp->m_swidth > 0)
234 2455300 : seq_printf(m, ",swidth=%d",
235 1227650 : (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
236 :
237 16125624 : if (mp->m_qflags & XFS_UQUOTA_ENFD)
238 14840638 : seq_puts(m, ",usrquota");
239 1284986 : else if (mp->m_qflags & XFS_UQUOTA_ACCT)
240 2123 : seq_puts(m, ",uqnoenforce");
241 :
242 16118840 : if (mp->m_qflags & XFS_PQUOTA_ENFD)
243 14810711 : seq_puts(m, ",prjquota");
244 1308130 : else if (mp->m_qflags & XFS_PQUOTA_ACCT)
245 1978 : seq_puts(m, ",pqnoenforce");
246 :
247 16127132 : if (mp->m_qflags & XFS_GQUOTA_ENFD)
248 14830816 : seq_puts(m, ",grpquota");
249 1296316 : else if (mp->m_qflags & XFS_GQUOTA_ACCT)
250 2155 : seq_puts(m, ",gqnoenforce");
251 :
252 16114601 : if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
253 1273459 : seq_puts(m, ",noquota");
254 :
255 16114296 : return 0;
256 : }
257 :
258 : static bool
259 589370 : xfs_set_inode_alloc_perag(
260 : struct xfs_perag *pag,
261 : xfs_ino_t ino,
262 : xfs_agnumber_t max_metadata)
263 : {
264 1178740 : if (!xfs_is_inode32(pag->pag_mount)) {
265 589370 : set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
266 589370 : clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
267 589370 : return false;
268 : }
269 :
270 0 : if (ino > XFS_MAXINUMBER_32) {
271 0 : clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
272 0 : clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
273 0 : return false;
274 : }
275 :
276 0 : set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
277 0 : if (pag->pag_agno < max_metadata)
278 0 : set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
279 : else
280 0 : clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
281 : return true;
282 : }
283 :
284 : /*
285 : * Set parameters for inode allocation heuristics, taking into account
286 : * filesystem size and inode32/inode64 mount options; i.e. specifically
287 : * whether or not XFS_FEAT_SMALL_INUMS is set.
288 : *
289 : * Inode allocation patterns are altered only if inode32 is requested
290 : * (XFS_FEAT_SMALL_INUMS), and the filesystem is sufficiently large.
291 : * If altered, XFS_OPSTATE_INODE32 is set as well.
292 : *
293 : * An agcount independent of that in the mount structure is provided
294 : * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
295 : * to the potentially higher ag count.
296 : *
297 : * Returns the maximum AG index which may contain inodes.
298 : */
299 : xfs_agnumber_t
300 80854 : xfs_set_inode_alloc(
301 : struct xfs_mount *mp,
302 : xfs_agnumber_t agcount)
303 : {
304 80854 : xfs_agnumber_t index;
305 80854 : xfs_agnumber_t maxagi = 0;
306 80854 : xfs_sb_t *sbp = &mp->m_sb;
307 80854 : xfs_agnumber_t max_metadata;
308 80854 : xfs_agino_t agino;
309 80854 : xfs_ino_t ino;
310 :
311 : /*
312 : * Calculate how much should be reserved for inodes to meet
313 : * the max inode percentage. Used only for inode32.
314 : */
315 80854 : if (M_IGEO(mp)->maxicount) {
316 80854 : uint64_t icount;
317 :
318 80854 : icount = sbp->sb_dblocks * sbp->sb_imax_pct;
319 80854 : do_div(icount, 100);
320 80854 : icount += sbp->sb_agblocks - 1;
321 80854 : do_div(icount, sbp->sb_agblocks);
322 80854 : max_metadata = icount;
323 : } else {
324 : max_metadata = agcount;
325 : }
326 :
327 : /* Get the last possible inode in the filesystem */
328 80854 : agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
329 80854 : ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
330 :
331 : /*
332 : * If user asked for no more than 32-bit inodes, and the fs is
333 : * sufficiently large, set XFS_OPSTATE_INODE32 if we must alter
334 : * the allocator to accommodate the request.
335 : */
336 80854 : if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
337 0 : set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
338 : else
339 80854 : clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
340 :
341 670224 : for (index = 0; index < agcount; index++) {
342 589370 : struct xfs_perag *pag;
343 :
344 589370 : ino = XFS_AGINO_TO_INO(mp, index, agino);
345 :
346 589370 : pag = xfs_perag_get(mp, index);
347 589370 : if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
348 0 : maxagi++;
349 589370 : clear_bit(XFS_AGSTATE_NOALLOC, &pag->pag_opstate);
350 589370 : xfs_perag_put(pag);
351 : }
352 :
353 161708 : return xfs_is_inode32(mp) ? maxagi : agcount;
354 : }
355 :
356 : static int
357 113 : xfs_setup_dax_always(
358 : struct xfs_mount *mp)
359 : {
360 113 : if (!mp->m_ddev_targp->bt_daxdev &&
361 113 : (!mp->m_rtdev_targp || !mp->m_rtdev_targp->bt_daxdev)) {
362 113 : xfs_alert(mp,
363 : "DAX unsupported by block device. Turning off DAX.");
364 113 : goto disable_dax;
365 : }
366 :
367 0 : if (mp->m_super->s_blocksize != PAGE_SIZE) {
368 0 : xfs_alert(mp,
369 : "DAX not supported for blocksize. Turning off DAX.");
370 0 : goto disable_dax;
371 : }
372 :
373 0 : if (xfs_has_reflink(mp) &&
374 0 : bdev_is_partition(mp->m_ddev_targp->bt_bdev)) {
375 0 : xfs_alert(mp,
376 : "DAX and reflink cannot work with multi-partitions!");
377 0 : return -EINVAL;
378 : }
379 :
380 0 : xfs_warn(mp, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
381 0 : return 0;
382 :
383 113 : disable_dax:
384 113 : xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
385 113 : return 0;
386 : }
387 :
388 : static void
389 0 : xfs_bdev_mark_dead(
390 : struct block_device *bdev)
391 : {
392 0 : xfs_force_shutdown(bdev->bd_holder, SHUTDOWN_DEVICE_REMOVED);
393 0 : }
394 :
395 : static const struct blk_holder_ops xfs_holder_ops = {
396 : .mark_dead = xfs_bdev_mark_dead,
397 : };
398 :
399 : STATIC int
400 47513 : xfs_blkdev_get(
401 : xfs_mount_t *mp,
402 : const char *name,
403 : struct block_device **bdevp)
404 : {
405 47513 : int error = 0;
406 :
407 47513 : *bdevp = blkdev_get_by_path(name, BLK_OPEN_READ | BLK_OPEN_WRITE, mp,
408 : &xfs_holder_ops);
409 47513 : if (IS_ERR(*bdevp)) {
410 0 : error = PTR_ERR(*bdevp);
411 0 : xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
412 : }
413 :
414 47513 : return error;
415 : }
416 :
417 : STATIC void
418 47521 : xfs_blkdev_put(
419 : struct xfs_mount *mp,
420 : struct block_device *bdev)
421 : {
422 47521 : if (bdev)
423 47521 : blkdev_put(bdev, mp);
424 47521 : }
425 :
426 : STATIC void
427 67705 : xfs_close_devices(
428 : struct xfs_mount *mp)
429 : {
430 67705 : if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
431 24521 : struct block_device *logdev = xfs_buftarg_bdev(mp->m_logdev_targp);
432 :
433 24521 : xfs_free_buftarg(mp->m_logdev_targp);
434 24521 : xfs_blkdev_put(mp, logdev);
435 : }
436 67705 : if (mp->m_rtdev_targp) {
437 23000 : struct block_device *rtdev = xfs_buftarg_bdev(mp->m_rtdev_targp);
438 :
439 23000 : xfs_free_buftarg(mp->m_rtdev_targp);
440 23000 : xfs_blkdev_put(mp, rtdev);
441 : }
442 67705 : xfs_free_buftarg(mp->m_ddev_targp);
443 67705 : }
444 :
445 : /*
446 : * The file system configurations are:
447 : * (1) device (partition) with data and internal log
448 : * (2) logical volume with data and log subvolumes.
449 : * (3) logical volume with data, log, and realtime subvolumes.
450 : *
451 : * We only have to handle opening the log and realtime volumes here if
452 : * they are present. The data subvolume has already been opened by
453 : * get_sb_bdev() and is stored in sb->s_bdev.
454 : */
455 : STATIC int
456 67694 : xfs_open_devices(
457 : struct xfs_mount *mp)
458 : {
459 67694 : struct block_device *ddev = mp->m_super->s_bdev;
460 67694 : struct block_device *logdev = NULL, *rtdev = NULL;
461 67694 : int error;
462 :
463 : /*
464 : * Open real time and log devices - order is important.
465 : */
466 67694 : if (mp->m_logname) {
467 24518 : error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
468 24518 : if (error)
469 : return error;
470 : }
471 :
472 67694 : if (mp->m_rtname) {
473 22995 : error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
474 22995 : if (error)
475 0 : goto out_close_logdev;
476 :
477 22995 : if (rtdev == ddev || rtdev == logdev) {
478 0 : xfs_warn(mp,
479 : "Cannot mount filesystem with identical rtdev and ddev/logdev.");
480 0 : error = -EINVAL;
481 0 : goto out_close_rtdev;
482 : }
483 : }
484 :
485 : /*
486 : * Setup xfs_mount buffer target pointers
487 : */
488 67694 : error = -ENOMEM;
489 67694 : mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev);
490 67694 : if (!mp->m_ddev_targp)
491 0 : goto out_close_rtdev;
492 :
493 67694 : if (rtdev) {
494 22995 : mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev);
495 22995 : if (!mp->m_rtdev_targp)
496 0 : goto out_free_ddev_targ;
497 : }
498 :
499 67694 : if (logdev && logdev != ddev) {
500 24518 : mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev);
501 24518 : if (!mp->m_logdev_targp)
502 0 : goto out_free_rtdev_targ;
503 : } else {
504 43176 : mp->m_logdev_targp = mp->m_ddev_targp;
505 : }
506 :
507 : return 0;
508 :
509 : out_free_rtdev_targ:
510 0 : if (mp->m_rtdev_targp)
511 0 : xfs_free_buftarg(mp->m_rtdev_targp);
512 0 : out_free_ddev_targ:
513 0 : xfs_free_buftarg(mp->m_ddev_targp);
514 0 : out_close_rtdev:
515 0 : xfs_blkdev_put(mp, rtdev);
516 0 : out_close_logdev:
517 0 : if (logdev && logdev != ddev)
518 0 : xfs_blkdev_put(mp, logdev);
519 : return error;
520 : }
521 :
522 : /*
523 : * Setup xfs_mount buffer target pointers based on superblock
524 : */
525 : STATIC int
526 67049 : xfs_setup_devices(
527 : struct xfs_mount *mp)
528 : {
529 67049 : int error;
530 :
531 67049 : error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
532 67049 : if (error)
533 : return error;
534 :
535 67049 : if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
536 24467 : unsigned int log_sector_size = BBSIZE;
537 :
538 24467 : if (xfs_has_sector(mp))
539 24122 : log_sector_size = mp->m_sb.sb_logsectsize;
540 24467 : error = xfs_setsize_buftarg(mp->m_logdev_targp,
541 : log_sector_size);
542 24467 : if (error)
543 : return error;
544 : }
545 67049 : if (mp->m_rtdev_targp) {
546 22955 : error = xfs_setsize_buftarg(mp->m_rtdev_targp,
547 22955 : mp->m_sb.sb_sectsize);
548 22955 : if (error)
549 0 : return error;
550 : }
551 :
552 : return 0;
553 : }
554 :
555 : STATIC int
556 67694 : xfs_init_mount_workqueues(
557 : struct xfs_mount *mp)
558 : {
559 135388 : mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
560 : XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
561 67694 : 1, mp->m_super->s_id);
562 67694 : if (!mp->m_buf_workqueue)
563 0 : goto out;
564 :
565 135388 : mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
566 : XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
567 67694 : 0, mp->m_super->s_id);
568 67694 : if (!mp->m_unwritten_workqueue)
569 0 : goto out_destroy_buf;
570 :
571 135388 : mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
572 : XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
573 67694 : 0, mp->m_super->s_id);
574 67694 : if (!mp->m_reclaim_workqueue)
575 0 : goto out_destroy_unwritten;
576 :
577 135388 : mp->m_blockgc_wq = alloc_workqueue("xfs-blockgc/%s",
578 : XFS_WQFLAGS(WQ_UNBOUND | WQ_FREEZABLE | WQ_MEM_RECLAIM),
579 67694 : 0, mp->m_super->s_id);
580 67694 : if (!mp->m_blockgc_wq)
581 0 : goto out_destroy_reclaim;
582 :
583 135388 : mp->m_inodegc_wq = alloc_workqueue("xfs-inodegc/%s",
584 : XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
585 67694 : 1, mp->m_super->s_id);
586 67694 : if (!mp->m_inodegc_wq)
587 0 : goto out_destroy_blockgc;
588 :
589 135388 : mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s",
590 67694 : XFS_WQFLAGS(WQ_FREEZABLE), 0, mp->m_super->s_id);
591 67694 : if (!mp->m_sync_workqueue)
592 0 : goto out_destroy_inodegc;
593 :
594 : return 0;
595 :
596 : out_destroy_inodegc:
597 0 : destroy_workqueue(mp->m_inodegc_wq);
598 0 : out_destroy_blockgc:
599 0 : destroy_workqueue(mp->m_blockgc_wq);
600 0 : out_destroy_reclaim:
601 0 : destroy_workqueue(mp->m_reclaim_workqueue);
602 0 : out_destroy_unwritten:
603 0 : destroy_workqueue(mp->m_unwritten_workqueue);
604 0 : out_destroy_buf:
605 0 : destroy_workqueue(mp->m_buf_workqueue);
606 : out:
607 : return -ENOMEM;
608 : }
609 :
610 : STATIC void
611 67705 : xfs_destroy_mount_workqueues(
612 : struct xfs_mount *mp)
613 : {
614 67705 : destroy_workqueue(mp->m_sync_workqueue);
615 67705 : destroy_workqueue(mp->m_blockgc_wq);
616 67705 : destroy_workqueue(mp->m_inodegc_wq);
617 67705 : destroy_workqueue(mp->m_reclaim_workqueue);
618 67705 : destroy_workqueue(mp->m_unwritten_workqueue);
619 67705 : destroy_workqueue(mp->m_buf_workqueue);
620 67705 : }
621 :
622 : static void
623 1133372 : xfs_flush_inodes_worker(
624 : struct work_struct *work)
625 : {
626 1133372 : struct xfs_mount *mp = container_of(work, struct xfs_mount,
627 : m_flush_inodes_work);
628 1133372 : struct super_block *sb = mp->m_super;
629 :
630 1133372 : if (down_read_trylock(&sb->s_umount)) {
631 1133372 : sync_inodes_sb(sb);
632 1133372 : up_read(&sb->s_umount);
633 : }
634 1133372 : }
635 :
636 : /*
637 : * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
638 : * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
639 : * for IO to complete so that we effectively throttle multiple callers to the
640 : * rate at which IO is completing.
641 : */
642 : void
643 2337153 : xfs_flush_inodes(
644 : struct xfs_mount *mp)
645 : {
646 : /*
647 : * If flush_work() returns true then that means we waited for a flush
648 : * which was already in progress. Don't bother running another scan.
649 : */
650 2337153 : if (flush_work(&mp->m_flush_inodes_work))
651 : return;
652 :
653 1135055 : queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
654 1135082 : flush_work(&mp->m_flush_inodes_work);
655 : }
656 :
657 : /* Catch misguided souls that try to use this interface on XFS */
658 : STATIC struct inode *
659 0 : xfs_fs_alloc_inode(
660 : struct super_block *sb)
661 : {
662 0 : BUG();
663 : return NULL;
664 : }
665 :
666 : /*
667 : * Now that the generic code is guaranteed not to be accessing
668 : * the linux inode, we can inactivate and reclaim the inode.
669 : */
670 : STATIC void
671 879550877 : xfs_fs_destroy_inode(
672 : struct inode *inode)
673 : {
674 879550877 : struct xfs_inode *ip = XFS_I(inode);
675 :
676 879550877 : trace_xfs_destroy_inode(ip);
677 :
678 879489502 : ASSERT(!rwsem_is_locked(&inode->i_rwsem));
679 879489502 : XFS_STATS_INC(ip->i_mount, vn_rele);
680 879658344 : XFS_STATS_INC(ip->i_mount, vn_remove);
681 879689178 : xfs_inode_mark_reclaimable(ip);
682 879667968 : }
683 :
684 : static void
685 7813697 : xfs_fs_dirty_inode(
686 : struct inode *inode,
687 : int flags)
688 : {
689 7813697 : struct xfs_inode *ip = XFS_I(inode);
690 7813697 : struct xfs_mount *mp = ip->i_mount;
691 7813697 : struct xfs_trans *tp;
692 :
693 7813697 : if (!(inode->i_sb->s_flags & SB_LAZYTIME))
694 7813590 : return;
695 :
696 : /*
697 : * Only do the timestamp update if the inode is dirty (I_DIRTY_SYNC)
698 : * and has dirty timestamp (I_DIRTY_TIME). I_DIRTY_TIME can be passed
699 : * in flags possibly together with I_DIRTY_SYNC.
700 : */
701 107 : if ((flags & ~I_DIRTY_TIME) != I_DIRTY_SYNC || !(flags & I_DIRTY_TIME))
702 : return;
703 :
704 107 : if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
705 : return;
706 107 : xfs_ilock(ip, XFS_ILOCK_EXCL);
707 107 : xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
708 107 : xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
709 107 : xfs_trans_commit(tp);
710 : }
711 :
712 : /*
713 : * Slab object creation initialisation for the XFS inode.
714 : * This covers only the idempotent fields in the XFS inode;
715 : * all other fields need to be initialised on allocation
716 : * from the slab. This avoids the need to repeatedly initialise
717 : * fields in the xfs inode that left in the initialise state
718 : * when freeing the inode.
719 : */
720 : STATIC void
721 423775818 : xfs_fs_inode_init_once(
722 : void *inode)
723 : {
724 423775818 : struct xfs_inode *ip = inode;
725 :
726 423775818 : memset(ip, 0, sizeof(struct xfs_inode));
727 :
728 : /* vfs inode */
729 423775818 : inode_init_once(VFS_I(ip));
730 :
731 : /* xfs inode */
732 423777481 : atomic_set(&ip->i_pincount, 0);
733 423777481 : spin_lock_init(&ip->i_flags_lock);
734 :
735 423778052 : mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
736 : "xfsino", ip->i_ino);
737 423775607 : }
738 :
739 : /*
740 : * We do an unlocked check for XFS_IDONTCACHE here because we are already
741 : * serialised against cache hits here via the inode->i_lock and igrab() in
742 : * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
743 : * racing with us, and it avoids needing to grab a spinlock here for every inode
744 : * we drop the final reference on.
745 : */
746 : STATIC int
747 80442618422 : xfs_fs_drop_inode(
748 : struct inode *inode)
749 : {
750 80442618422 : struct xfs_inode *ip = XFS_I(inode);
751 :
752 : /*
753 : * If this unlinked inode is in the middle of recovery, don't
754 : * drop the inode just yet; log recovery will take care of
755 : * that. See the comment for this inode flag.
756 : */
757 80442618422 : if (ip->i_flags & XFS_IRECOVERY) {
758 92 : ASSERT(xlog_recovery_needed(ip->i_mount->m_log));
759 46 : return 0;
760 : }
761 :
762 80442618376 : return generic_drop_inode(inode);
763 : }
764 :
765 : static void
766 83061 : xfs_mount_free(
767 : struct xfs_mount *mp)
768 : {
769 83061 : debugfs_remove(mp->m_debugfs);
770 83061 : kfree(mp->m_rtname);
771 83061 : kfree(mp->m_logname);
772 83061 : kmem_free(mp);
773 83061 : }
774 :
775 : STATIC int
776 8456918 : xfs_fs_sync_fs(
777 : struct super_block *sb,
778 : int wait)
779 : {
780 8456918 : struct xfs_mount *mp = XFS_M(sb);
781 8456918 : int error;
782 :
783 8456918 : trace_xfs_fs_sync_fs(mp, __return_address);
784 :
785 : /*
786 : * Doing anything during the async pass would be counterproductive.
787 : */
788 8456560 : if (!wait)
789 : return 0;
790 :
791 4228805 : error = xfs_log_force(mp, XFS_LOG_SYNC);
792 4226497 : if (error)
793 : return error;
794 :
795 4207072 : if (laptop_mode) {
796 : /*
797 : * The disk must be active because we're syncing.
798 : * We schedule log work now (now that the disk is
799 : * active) instead of later (when it might not be).
800 : */
801 0 : flush_delayed_work(&mp->m_log->l_work);
802 : }
803 :
804 : /*
805 : * If we are called with page faults frozen out, it means we are about
806 : * to freeze the transaction subsystem. Take the opportunity to shut
807 : * down inodegc because once SB_FREEZE_FS is set it's too late to
808 : * prevent inactivation races with freeze. The fs doesn't get called
809 : * again by the freezing process until after SB_FREEZE_FS has been set,
810 : * so it's now or never. Same logic applies to speculative allocation
811 : * garbage collection.
812 : *
813 : * We don't care if this is a normal syncfs call that does this or
814 : * freeze that does this - we can run this multiple times without issue
815 : * and we won't race with a restart because a restart can only occur
816 : * when the state is either SB_FREEZE_FS or SB_FREEZE_COMPLETE.
817 : */
818 4207072 : if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
819 69667 : xfs_inodegc_stop(mp);
820 69667 : xfs_blockgc_stop(mp);
821 : }
822 :
823 : return 0;
824 : }
825 :
826 : STATIC int
827 29731540 : xfs_fs_statfs(
828 : struct dentry *dentry,
829 : struct kstatfs *statp)
830 : {
831 29731540 : struct xfs_mount *mp = XFS_M(dentry->d_sb);
832 29731540 : xfs_sb_t *sbp = &mp->m_sb;
833 29731540 : struct xfs_inode *ip = XFS_I(d_inode(dentry));
834 29731540 : uint64_t fakeinos, id;
835 29731540 : uint64_t icount;
836 29731540 : uint64_t ifree;
837 29731540 : uint64_t fdblocks;
838 29731540 : xfs_extlen_t lsize;
839 29731540 : int64_t ffree;
840 :
841 : /*
842 : * Expedite background inodegc but don't wait. We do not want to block
843 : * here waiting hours for a billion extent file to be truncated.
844 : */
845 29731540 : xfs_inodegc_push(mp);
846 :
847 29695303 : statp->f_type = XFS_SUPER_MAGIC;
848 29695303 : statp->f_namelen = MAXNAMELEN - 1;
849 :
850 29695303 : id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
851 29695303 : statp->f_fsid = u64_to_fsid(id);
852 :
853 29695303 : icount = percpu_counter_sum(&mp->m_icount);
854 29762504 : ifree = percpu_counter_sum(&mp->m_ifree);
855 29760818 : fdblocks = percpu_counter_sum(&mp->m_fdblocks);
856 :
857 29759915 : spin_lock(&mp->m_sb_lock);
858 29762547 : statp->f_bsize = sbp->sb_blocksize;
859 29762547 : lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
860 29762547 : statp->f_blocks = sbp->sb_dblocks - lsize;
861 29762547 : spin_unlock(&mp->m_sb_lock);
862 :
863 : /* make sure statp->f_bfree does not underflow */
864 29757457 : statp->f_bfree = max_t(int64_t, 0,
865 : fdblocks - xfs_fdblocks_unavailable(mp));
866 29757457 : statp->f_bavail = statp->f_bfree;
867 :
868 29757457 : fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
869 29757457 : statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
870 29757457 : if (M_IGEO(mp)->maxicount)
871 29757858 : statp->f_files = min_t(typeof(statp->f_files),
872 : statp->f_files,
873 : M_IGEO(mp)->maxicount);
874 :
875 : /* If sb_icount overshot maxicount, report actual allocation */
876 29757457 : statp->f_files = max_t(typeof(statp->f_files),
877 : statp->f_files,
878 : sbp->sb_icount);
879 :
880 : /* make sure statp->f_ffree does not underflow */
881 29757457 : ffree = statp->f_files - (icount - ifree);
882 29757457 : statp->f_ffree = max_t(int64_t, ffree, 0);
883 :
884 29757457 : if (XFS_IS_REALTIME_MOUNT(mp) &&
885 13320670 : (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
886 13115069 : s64 freertx;
887 :
888 13115069 : statp->f_blocks = sbp->sb_rblocks;
889 13115069 : freertx = percpu_counter_sum_positive(&mp->m_frextents);
890 13116825 : statp->f_bavail = statp->f_bfree = xfs_rtx_to_rtb(mp, freertx);
891 : }
892 :
893 29755366 : if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
894 4609 : ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
895 : (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
896 3355 : xfs_qm_statvfs(ip, statp);
897 :
898 29755366 : return 0;
899 : }
900 :
901 : STATIC void
902 70077 : xfs_save_resvblks(struct xfs_mount *mp)
903 : {
904 70077 : uint64_t resblks = 0;
905 :
906 70077 : mp->m_resblks_save = mp->m_resblks;
907 70077 : xfs_reserve_blocks(mp, &resblks, NULL);
908 70077 : }
909 :
910 : STATIC void
911 70028 : xfs_restore_resvblks(struct xfs_mount *mp)
912 : {
913 70028 : uint64_t resblks;
914 :
915 70028 : if (mp->m_resblks_save) {
916 70006 : resblks = mp->m_resblks_save;
917 70006 : mp->m_resblks_save = 0;
918 : } else
919 22 : resblks = xfs_default_resblks(mp);
920 :
921 70028 : xfs_reserve_blocks(mp, &resblks, NULL);
922 70028 : }
923 :
924 : /*
925 : * Second stage of a freeze. The data is already frozen so we only
926 : * need to take care of the metadata. Once that's done sync the superblock
927 : * to the log to dirty it in case of a crash while frozen. This ensures that we
928 : * will recover the unlinked inode lists on the next mount.
929 : */
930 : STATIC int
931 69667 : xfs_fs_freeze(
932 : struct super_block *sb)
933 : {
934 69667 : struct xfs_mount *mp = XFS_M(sb);
935 69667 : unsigned int flags;
936 69667 : int ret;
937 :
938 : /*
939 : * The filesystem is now frozen far enough that memory reclaim
940 : * cannot safely operate on the filesystem. Hence we need to
941 : * set a GFP_NOFS context here to avoid recursion deadlocks.
942 : */
943 69667 : flags = memalloc_nofs_save();
944 69667 : xfs_save_resvblks(mp);
945 69667 : ret = xfs_log_quiesce(mp);
946 69667 : memalloc_nofs_restore(flags);
947 :
948 : /*
949 : * For read-write filesystems, we need to restart the inodegc on error
950 : * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
951 : * going to be run to restart it now. We are at SB_FREEZE_FS level
952 : * here, so we can restart safely without racing with a stop in
953 : * xfs_fs_sync_fs().
954 : */
955 69667 : if (ret && !xfs_is_readonly(mp)) {
956 0 : xfs_blockgc_start(mp);
957 0 : xfs_inodegc_start(mp);
958 : }
959 :
960 69667 : return ret;
961 : }
962 :
963 : STATIC int
964 69667 : xfs_fs_unfreeze(
965 : struct super_block *sb)
966 : {
967 69667 : struct xfs_mount *mp = XFS_M(sb);
968 :
969 69667 : xfs_restore_resvblks(mp);
970 69667 : xfs_log_work_queue(mp);
971 :
972 : /*
973 : * Don't reactivate the inodegc worker on a readonly filesystem because
974 : * inodes are sent directly to reclaim. Don't reactivate the blockgc
975 : * worker because there are no speculative preallocations on a readonly
976 : * filesystem.
977 : */
978 139334 : if (!xfs_is_readonly(mp)) {
979 69667 : xfs_blockgc_start(mp);
980 69667 : xfs_inodegc_start(mp);
981 : }
982 :
983 69667 : return 0;
984 : }
985 :
986 : /*
987 : * This function fills in xfs_mount_t fields based on mount args.
988 : * Note: the superblock _has_ now been read in.
989 : */
990 : STATIC int
991 67108 : xfs_finish_flags(
992 : struct xfs_mount *mp)
993 : {
994 : /* Fail a mount where the logbuf is smaller than the log stripe */
995 67108 : if (xfs_has_logv2(mp)) {
996 67057 : if (mp->m_logbsize <= 0 &&
997 66249 : mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
998 345 : mp->m_logbsize = mp->m_sb.sb_logsunit;
999 66712 : } else if (mp->m_logbsize > 0 &&
1000 808 : mp->m_logbsize < mp->m_sb.sb_logsunit) {
1001 0 : xfs_warn(mp,
1002 : "logbuf size must be greater than or equal to log stripe size");
1003 0 : return -EINVAL;
1004 : }
1005 : } else {
1006 : /* Fail a mount if the logbuf is larger than 32K */
1007 51 : if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1008 21 : xfs_warn(mp,
1009 : "logbuf size for version 1 logs must be 16K or 32K");
1010 21 : return -EINVAL;
1011 : }
1012 : }
1013 :
1014 : /*
1015 : * V5 filesystems always use attr2 format for attributes.
1016 : */
1017 67087 : if (xfs_has_crc(mp) && xfs_has_noattr2(mp)) {
1018 16 : xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
1019 : "attr2 is always enabled for V5 filesystems.");
1020 16 : return -EINVAL;
1021 : }
1022 :
1023 : /*
1024 : * prohibit r/w mounts of read-only filesystems
1025 : */
1026 67071 : if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
1027 0 : xfs_warn(mp,
1028 : "cannot mount a read-only filesystem as read-write");
1029 0 : return -EROFS;
1030 : }
1031 :
1032 67071 : if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
1033 55486 : (mp->m_qflags & XFS_PQUOTA_ACCT) &&
1034 : !xfs_has_pquotino(mp)) {
1035 22 : xfs_warn(mp,
1036 : "Super block does not support project and group quota together");
1037 22 : return -EINVAL;
1038 : }
1039 :
1040 : return 0;
1041 : }
1042 :
1043 : static int
1044 67694 : xfs_init_percpu_counters(
1045 : struct xfs_mount *mp)
1046 : {
1047 67694 : int error;
1048 :
1049 67694 : error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
1050 67694 : if (error)
1051 : return -ENOMEM;
1052 :
1053 67694 : error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
1054 67694 : if (error)
1055 0 : goto free_icount;
1056 :
1057 67694 : error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
1058 67694 : if (error)
1059 0 : goto free_ifree;
1060 :
1061 67694 : error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
1062 67694 : if (error)
1063 0 : goto free_fdblocks;
1064 :
1065 67694 : error = percpu_counter_init(&mp->m_frextents, 0, GFP_KERNEL);
1066 67694 : if (error)
1067 0 : goto free_delalloc;
1068 :
1069 : return 0;
1070 :
1071 : free_delalloc:
1072 0 : percpu_counter_destroy(&mp->m_delalloc_blks);
1073 0 : free_fdblocks:
1074 0 : percpu_counter_destroy(&mp->m_fdblocks);
1075 0 : free_ifree:
1076 0 : percpu_counter_destroy(&mp->m_ifree);
1077 0 : free_icount:
1078 0 : percpu_counter_destroy(&mp->m_icount);
1079 0 : return -ENOMEM;
1080 : }
1081 :
1082 : void
1083 94346 : xfs_reinit_percpu_counters(
1084 : struct xfs_mount *mp)
1085 : {
1086 94346 : percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
1087 94346 : percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
1088 94346 : percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
1089 94346 : percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1090 94346 : }
1091 :
1092 : static void
1093 67705 : xfs_destroy_percpu_counters(
1094 : struct xfs_mount *mp)
1095 : {
1096 67705 : percpu_counter_destroy(&mp->m_icount);
1097 67705 : percpu_counter_destroy(&mp->m_ifree);
1098 67705 : percpu_counter_destroy(&mp->m_fdblocks);
1099 135410 : ASSERT(xfs_is_shutdown(mp) ||
1100 : percpu_counter_sum(&mp->m_delalloc_blks) == 0);
1101 67705 : percpu_counter_destroy(&mp->m_delalloc_blks);
1102 67705 : percpu_counter_destroy(&mp->m_frextents);
1103 67705 : }
1104 :
1105 : static int
1106 67694 : xfs_inodegc_init_percpu(
1107 : struct xfs_mount *mp)
1108 : {
1109 67694 : struct xfs_inodegc *gc;
1110 67694 : int cpu;
1111 :
1112 67694 : mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
1113 67694 : if (!mp->m_inodegc)
1114 : return -ENOMEM;
1115 :
1116 338470 : for_each_possible_cpu(cpu) {
1117 270776 : gc = per_cpu_ptr(mp->m_inodegc, cpu);
1118 : #if defined(DEBUG) || defined(XFS_WARN)
1119 270776 : gc->cpu = cpu;
1120 : #endif
1121 270776 : init_llist_head(&gc->list);
1122 270776 : gc->items = 0;
1123 270776 : gc->error = 0;
1124 270776 : INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
1125 : }
1126 : return 0;
1127 : }
1128 :
1129 : static void
1130 : xfs_inodegc_free_percpu(
1131 : struct xfs_mount *mp)
1132 : {
1133 67705 : if (!mp->m_inodegc)
1134 : return;
1135 67705 : free_percpu(mp->m_inodegc);
1136 : }
1137 :
1138 : static void
1139 66647 : xfs_fs_put_super(
1140 : struct super_block *sb)
1141 : {
1142 66647 : struct xfs_mount *mp = XFS_M(sb);
1143 :
1144 : /* if ->fill_super failed, we have no mount to tear down */
1145 66647 : if (!sb->s_fs_info)
1146 : return;
1147 :
1148 66647 : xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
1149 66647 : xfs_filestream_unmount(mp);
1150 66647 : xfs_unmountfs(mp);
1151 :
1152 66647 : xfs_rtmount_freesb(mp);
1153 66647 : xfs_freesb(mp);
1154 66647 : xchk_mount_stats_free(mp);
1155 66647 : free_percpu(mp->m_stats.xs_stats);
1156 66647 : xfs_mount_list_del(mp);
1157 66647 : xfs_inodegc_free_percpu(mp);
1158 66647 : xfs_destroy_percpu_counters(mp);
1159 66647 : xfs_destroy_mount_workqueues(mp);
1160 66647 : xfs_close_devices(mp);
1161 :
1162 66647 : sb->s_fs_info = NULL;
1163 66647 : xfs_mount_free(mp);
1164 : }
1165 :
1166 : static long
1167 386465 : xfs_fs_nr_cached_objects(
1168 : struct super_block *sb,
1169 : struct shrink_control *sc)
1170 : {
1171 : /* Paranoia: catch incorrect calls during mount setup or teardown */
1172 386465 : if (WARN_ON_ONCE(!sb->s_fs_info))
1173 : return 0;
1174 386465 : return xfs_reclaim_inodes_count(XFS_M(sb));
1175 : }
1176 :
1177 : static long
1178 40085 : xfs_fs_free_cached_objects(
1179 : struct super_block *sb,
1180 : struct shrink_control *sc)
1181 : {
1182 40085 : return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
1183 : }
1184 :
1185 : static void
1186 0 : xfs_fs_shutdown(
1187 : struct super_block *sb)
1188 : {
1189 0 : xfs_force_shutdown(XFS_M(sb), SHUTDOWN_DEVICE_REMOVED);
1190 0 : }
1191 :
1192 : static const struct super_operations xfs_super_operations = {
1193 : .alloc_inode = xfs_fs_alloc_inode,
1194 : .destroy_inode = xfs_fs_destroy_inode,
1195 : .dirty_inode = xfs_fs_dirty_inode,
1196 : .drop_inode = xfs_fs_drop_inode,
1197 : .put_super = xfs_fs_put_super,
1198 : .sync_fs = xfs_fs_sync_fs,
1199 : .freeze_fs = xfs_fs_freeze,
1200 : .unfreeze_fs = xfs_fs_unfreeze,
1201 : .statfs = xfs_fs_statfs,
1202 : .show_options = xfs_fs_show_options,
1203 : .nr_cached_objects = xfs_fs_nr_cached_objects,
1204 : .free_cached_objects = xfs_fs_free_cached_objects,
1205 : .shutdown = xfs_fs_shutdown,
1206 : };
1207 :
1208 : static int
1209 963 : suffix_kstrtoint(
1210 : const char *s,
1211 : unsigned int base,
1212 : int *res)
1213 : {
1214 963 : int last, shift_left_factor = 0, _res;
1215 963 : char *value;
1216 963 : int ret = 0;
1217 :
1218 963 : value = kstrdup(s, GFP_KERNEL);
1219 963 : if (!value)
1220 : return -ENOMEM;
1221 :
1222 963 : last = strlen(value) - 1;
1223 963 : if (value[last] == 'K' || value[last] == 'k') {
1224 923 : shift_left_factor = 10;
1225 923 : value[last] = '\0';
1226 : }
1227 963 : if (value[last] == 'M' || value[last] == 'm') {
1228 0 : shift_left_factor = 20;
1229 0 : value[last] = '\0';
1230 : }
1231 963 : if (value[last] == 'G' || value[last] == 'g') {
1232 10 : shift_left_factor = 30;
1233 10 : value[last] = '\0';
1234 : }
1235 :
1236 963 : if (kstrtoint(value, base, &_res))
1237 0 : ret = -EINVAL;
1238 963 : kfree(value);
1239 963 : *res = _res << shift_left_factor;
1240 963 : return ret;
1241 : }
1242 :
1243 : static inline void
1244 132 : xfs_fs_warn_deprecated(
1245 : struct fs_context *fc,
1246 : struct fs_parameter *param,
1247 : uint64_t flag,
1248 : bool value)
1249 : {
1250 : /* Don't print the warning if reconfiguring and current mount point
1251 : * already had the flag set
1252 : */
1253 132 : if ((fc->purpose & FS_CONTEXT_FOR_RECONFIGURE) &&
1254 55 : !!(XFS_M(fc->root->d_sb)->m_features & flag) == value)
1255 : return;
1256 99 : xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
1257 : }
1258 :
1259 : /*
1260 : * Set mount state from a mount option.
1261 : *
1262 : * NOTE: mp->m_super is NULL here!
1263 : */
1264 : static int
1265 354580 : xfs_fs_parse_param(
1266 : struct fs_context *fc,
1267 : struct fs_parameter *param)
1268 : {
1269 354580 : struct xfs_mount *parsing_mp = fc->s_fs_info;
1270 354580 : struct fs_parse_result result;
1271 354580 : int size = 0;
1272 354580 : int opt;
1273 :
1274 354580 : opt = fs_parse(fc, xfs_fs_parameters, param, &result);
1275 354580 : if (opt < 0)
1276 : return opt;
1277 :
1278 283901 : switch (opt) {
1279 51 : case Opt_logbufs:
1280 51 : parsing_mp->m_logbufs = result.uint_32;
1281 51 : return 0;
1282 912 : case Opt_logbsize:
1283 912 : if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
1284 0 : return -EINVAL;
1285 : return 0;
1286 26388 : case Opt_logdev:
1287 26388 : kfree(parsing_mp->m_logname);
1288 26388 : parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
1289 26388 : if (!parsing_mp->m_logname)
1290 0 : return -ENOMEM;
1291 : return 0;
1292 29659 : case Opt_rtdev:
1293 29659 : kfree(parsing_mp->m_rtname);
1294 29659 : parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
1295 29659 : if (!parsing_mp->m_rtname)
1296 0 : return -ENOMEM;
1297 : return 0;
1298 51 : case Opt_allocsize:
1299 51 : if (suffix_kstrtoint(param->string, 10, &size))
1300 : return -EINVAL;
1301 51 : parsing_mp->m_allocsize_log = ffs(size) - 1;
1302 51 : parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
1303 51 : return 0;
1304 20 : case Opt_grpid:
1305 : case Opt_bsdgroups:
1306 20 : parsing_mp->m_features |= XFS_FEAT_GRPID;
1307 20 : return 0;
1308 20 : case Opt_nogrpid:
1309 : case Opt_sysvgroups:
1310 20 : parsing_mp->m_features &= ~XFS_FEAT_GRPID;
1311 20 : return 0;
1312 40 : case Opt_wsync:
1313 40 : parsing_mp->m_features |= XFS_FEAT_WSYNC;
1314 40 : return 0;
1315 75 : case Opt_norecovery:
1316 75 : parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
1317 75 : return 0;
1318 10 : case Opt_noalign:
1319 10 : parsing_mp->m_features |= XFS_FEAT_NOALIGN;
1320 10 : return 0;
1321 10 : case Opt_swalloc:
1322 10 : parsing_mp->m_features |= XFS_FEAT_SWALLOC;
1323 10 : return 0;
1324 157 : case Opt_sunit:
1325 157 : parsing_mp->m_dalign = result.uint_32;
1326 157 : return 0;
1327 157 : case Opt_swidth:
1328 157 : parsing_mp->m_swidth = result.uint_32;
1329 157 : return 0;
1330 10 : case Opt_inode32:
1331 10 : parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
1332 10 : return 0;
1333 32 : case Opt_inode64:
1334 32 : parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1335 32 : return 0;
1336 15 : case Opt_nouuid:
1337 15 : parsing_mp->m_features |= XFS_FEAT_NOUUID;
1338 15 : return 0;
1339 10 : case Opt_largeio:
1340 10 : parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
1341 10 : return 0;
1342 10 : case Opt_nolargeio:
1343 10 : parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
1344 10 : return 0;
1345 97 : case Opt_filestreams:
1346 97 : parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
1347 97 : return 0;
1348 2430 : case Opt_noquota:
1349 2430 : parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
1350 2430 : parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
1351 2430 : return 0;
1352 72978 : case Opt_quota:
1353 : case Opt_uquota:
1354 : case Opt_usrquota:
1355 72978 : parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
1356 72978 : return 0;
1357 2392 : case Opt_qnoenforce:
1358 : case Opt_uqnoenforce:
1359 2392 : parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
1360 2392 : parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
1361 2392 : return 0;
1362 71408 : case Opt_pquota:
1363 : case Opt_prjquota:
1364 71408 : parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
1365 71408 : return 0;
1366 2232 : case Opt_pqnoenforce:
1367 2232 : parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
1368 2232 : parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
1369 2232 : return 0;
1370 71978 : case Opt_gquota:
1371 : case Opt_grpquota:
1372 71978 : parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
1373 71978 : return 0;
1374 2492 : case Opt_gqnoenforce:
1375 2492 : parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
1376 2492 : parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
1377 2492 : return 0;
1378 12 : case Opt_discard:
1379 12 : parsing_mp->m_features |= XFS_FEAT_DISCARD;
1380 12 : return 0;
1381 10 : case Opt_nodiscard:
1382 10 : parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
1383 10 : return 0;
1384 : #ifdef CONFIG_FS_DAX
1385 : case Opt_dax:
1386 69 : xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
1387 69 : return 0;
1388 44 : case Opt_dax_enum:
1389 44 : xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
1390 44 : return 0;
1391 : #endif
1392 : /* Following mount options will be removed in September 2025 */
1393 21 : case Opt_ikeep:
1394 21 : xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, true);
1395 21 : parsing_mp->m_features |= XFS_FEAT_IKEEP;
1396 21 : return 0;
1397 21 : case Opt_noikeep:
1398 21 : xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, false);
1399 21 : parsing_mp->m_features &= ~XFS_FEAT_IKEEP;
1400 21 : return 0;
1401 53 : case Opt_attr2:
1402 53 : xfs_fs_warn_deprecated(fc, param, XFS_FEAT_ATTR2, true);
1403 53 : parsing_mp->m_features |= XFS_FEAT_ATTR2;
1404 53 : return 0;
1405 37 : case Opt_noattr2:
1406 37 : xfs_fs_warn_deprecated(fc, param, XFS_FEAT_NOATTR2, true);
1407 37 : parsing_mp->m_features |= XFS_FEAT_NOATTR2;
1408 37 : return 0;
1409 0 : default:
1410 0 : xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
1411 0 : return -EINVAL;
1412 : }
1413 :
1414 : return 0;
1415 : }
1416 :
1417 : static int
1418 68664 : xfs_fs_validate_params(
1419 : struct xfs_mount *mp)
1420 : {
1421 : /* No recovery flag requires a read-only mount */
1422 68728 : if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
1423 10 : xfs_warn(mp, "no-recovery mounts must be read-only.");
1424 10 : return -EINVAL;
1425 : }
1426 :
1427 : /*
1428 : * We have not read the superblock at this point, so only the attr2
1429 : * mount option can set the attr2 feature by this stage.
1430 : */
1431 68654 : if (xfs_has_attr2(mp) && xfs_has_noattr2(mp)) {
1432 0 : xfs_warn(mp, "attr2 and noattr2 cannot both be specified.");
1433 0 : return -EINVAL;
1434 : }
1435 :
1436 :
1437 68654 : if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
1438 0 : xfs_warn(mp,
1439 : "sunit and swidth options incompatible with the noalign option");
1440 0 : return -EINVAL;
1441 : }
1442 :
1443 68654 : if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
1444 : xfs_warn(mp, "quota support not available in this kernel.");
1445 : return -EINVAL;
1446 : }
1447 :
1448 68654 : if ((mp->m_dalign && !mp->m_swidth) ||
1449 68537 : (!mp->m_dalign && mp->m_swidth)) {
1450 0 : xfs_warn(mp, "sunit and swidth must be specified together");
1451 0 : return -EINVAL;
1452 : }
1453 :
1454 68654 : if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
1455 0 : xfs_warn(mp,
1456 : "stripe width (%d) must be a multiple of the stripe unit (%d)",
1457 : mp->m_swidth, mp->m_dalign);
1458 0 : return -EINVAL;
1459 : }
1460 :
1461 68654 : if (mp->m_logbufs != -1 &&
1462 51 : mp->m_logbufs != 0 &&
1463 51 : (mp->m_logbufs < XLOG_MIN_ICLOGS ||
1464 : mp->m_logbufs > XLOG_MAX_ICLOGS)) {
1465 20 : xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
1466 : mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
1467 20 : return -EINVAL;
1468 : }
1469 :
1470 68634 : if (mp->m_logbsize != -1 &&
1471 890 : mp->m_logbsize != 0 &&
1472 890 : (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
1473 870 : mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
1474 870 : !is_power_of_2(mp->m_logbsize))) {
1475 20 : xfs_warn(mp,
1476 : "invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
1477 : mp->m_logbsize);
1478 20 : return -EINVAL;
1479 : }
1480 :
1481 68614 : if (xfs_has_allocsize(mp) &&
1482 51 : (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
1483 : mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
1484 20 : xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
1485 : mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
1486 20 : return -EINVAL;
1487 : }
1488 :
1489 : return 0;
1490 : }
1491 :
1492 : struct dentry *
1493 67031 : xfs_debugfs_mkdir(
1494 : const char *name,
1495 : struct dentry *parent)
1496 : {
1497 134784 : struct dentry *child;
1498 :
1499 : /* Apparently we're expected to ignore error returns?? */
1500 67031 : child = debugfs_create_dir(name, parent);
1501 134784 : if (IS_ERR(child))
1502 0 : return NULL;
1503 :
1504 : return child;
1505 : }
1506 :
1507 : static int
1508 67764 : xfs_fs_fill_super(
1509 : struct super_block *sb,
1510 : struct fs_context *fc)
1511 : {
1512 67764 : struct xfs_mount *mp = sb->s_fs_info;
1513 67764 : struct inode *root;
1514 67764 : int flags = 0, error;
1515 :
1516 67764 : mp->m_super = sb;
1517 :
1518 67764 : error = xfs_fs_validate_params(mp);
1519 67764 : if (error)
1520 70 : goto out_free_names;
1521 :
1522 67694 : sb_min_blocksize(sb, BBSIZE);
1523 67694 : sb->s_xattr = xfs_xattr_handlers;
1524 67694 : sb->s_export_op = &xfs_export_operations;
1525 : #ifdef CONFIG_XFS_QUOTA
1526 67694 : sb->s_qcop = &xfs_quotactl_operations;
1527 67694 : sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1528 : #endif
1529 67694 : sb->s_op = &xfs_super_operations;
1530 :
1531 : /*
1532 : * Delay mount work if the debug hook is set. This is debug
1533 : * instrumention to coordinate simulation of xfs mount failures with
1534 : * VFS superblock operations
1535 : */
1536 67694 : if (xfs_globals.mount_delay) {
1537 11 : xfs_notice(mp, "Delaying mount for %d seconds.",
1538 : xfs_globals.mount_delay);
1539 11 : msleep(xfs_globals.mount_delay * 1000);
1540 : }
1541 :
1542 67694 : if (fc->sb_flags & SB_SILENT)
1543 0 : flags |= XFS_MFSI_QUIET;
1544 :
1545 67694 : error = xfs_open_devices(mp);
1546 67694 : if (error)
1547 0 : goto out_free_names;
1548 :
1549 67694 : if (xfs_debugfs) {
1550 135388 : mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
1551 : xfs_debugfs);
1552 : } else {
1553 0 : mp->m_debugfs = NULL;
1554 : }
1555 :
1556 67694 : error = xfs_init_mount_workqueues(mp);
1557 67694 : if (error)
1558 0 : goto out_close_devices;
1559 :
1560 67694 : error = xfs_init_percpu_counters(mp);
1561 67694 : if (error)
1562 0 : goto out_destroy_workqueues;
1563 :
1564 67694 : error = xfs_inodegc_init_percpu(mp);
1565 67694 : if (error)
1566 0 : goto out_destroy_counters;
1567 :
1568 : /*
1569 : * All percpu data structures requiring cleanup when a cpu goes offline
1570 : * must be allocated before adding this @mp to the cpu-dead handler's
1571 : * mount list.
1572 : */
1573 67694 : xfs_mount_list_add(mp);
1574 :
1575 : /* Allocate stats memory before we do operations that might use it */
1576 67694 : mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1577 67694 : if (!mp->m_stats.xs_stats) {
1578 0 : error = -ENOMEM;
1579 0 : goto out_destroy_inodegc;
1580 : }
1581 :
1582 67694 : error = xchk_mount_stats_alloc(mp);
1583 67694 : if (error)
1584 0 : goto out_free_stats;
1585 :
1586 67694 : error = xfs_readsb(mp, flags);
1587 67694 : if (error)
1588 586 : goto out_free_scrub_stats;
1589 :
1590 67108 : error = xfs_finish_flags(mp);
1591 67108 : if (error)
1592 59 : goto out_free_sb;
1593 :
1594 67049 : error = xfs_setup_devices(mp);
1595 67049 : if (error)
1596 0 : goto out_free_sb;
1597 :
1598 : /* V4 support is undergoing deprecation. */
1599 67049 : if (!xfs_has_crc(mp)) {
1600 : #ifdef CONFIG_XFS_SUPPORT_V4
1601 236 : xfs_warn_once(mp,
1602 : "Deprecated V4 format (crc=0) will not be supported after September 2030.");
1603 : #else
1604 : xfs_warn(mp,
1605 : "Deprecated V4 format (crc=0) not supported by kernel.");
1606 : error = -EINVAL;
1607 : goto out_free_sb;
1608 : #endif
1609 : }
1610 :
1611 : /* ASCII case insensitivity is undergoing deprecation. */
1612 67049 : if (xfs_has_asciici(mp)) {
1613 : #ifdef CONFIG_XFS_SUPPORT_ASCII_CI
1614 155 : xfs_warn_once(mp,
1615 : "Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
1616 : #else
1617 : xfs_warn(mp,
1618 : "Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
1619 : error = -EINVAL;
1620 : goto out_free_sb;
1621 : #endif
1622 : }
1623 :
1624 : /* Filesystem claims it needs repair, so refuse the mount. */
1625 67049 : if (xfs_has_needsrepair(mp)) {
1626 24 : xfs_warn(mp, "Filesystem needs repair. Please run xfs_repair.");
1627 24 : error = -EFSCORRUPTED;
1628 24 : goto out_free_sb;
1629 : }
1630 :
1631 : /*
1632 : * Don't touch the filesystem if a user tool thinks it owns the primary
1633 : * superblock. mkfs doesn't clear the flag from secondary supers, so
1634 : * we don't check them at all.
1635 : */
1636 67025 : if (mp->m_sb.sb_inprogress) {
1637 0 : xfs_warn(mp, "Offline file system operation in progress!");
1638 0 : error = -EFSCORRUPTED;
1639 0 : goto out_free_sb;
1640 : }
1641 :
1642 : /*
1643 : * Until this is fixed only page-sized or smaller data blocks work.
1644 : */
1645 67025 : if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
1646 43 : xfs_warn(mp,
1647 : "File system with blocksize %d bytes. "
1648 : "Only pagesize (%ld) or less will currently work.",
1649 : mp->m_sb.sb_blocksize, PAGE_SIZE);
1650 43 : error = -ENOSYS;
1651 43 : goto out_free_sb;
1652 : }
1653 :
1654 : /* Ensure this filesystem fits in the page cache limits */
1655 133964 : if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
1656 66982 : xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
1657 0 : xfs_warn(mp,
1658 : "file system too large to be mounted on this system.");
1659 0 : error = -EFBIG;
1660 0 : goto out_free_sb;
1661 : }
1662 :
1663 : /*
1664 : * XFS block mappings use 54 bits to store the logical block offset.
1665 : * This should suffice to handle the maximum file size that the VFS
1666 : * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1667 : * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1668 : * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1669 : * to check this assertion.
1670 : *
1671 : * Avoid integer overflow by comparing the maximum bmbt offset to the
1672 : * maximum pagecache offset in units of fs blocks.
1673 : */
1674 66982 : if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1675 0 : xfs_warn(mp,
1676 : "MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1677 : XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1678 : XFS_MAX_FILEOFF);
1679 0 : error = -EINVAL;
1680 0 : goto out_free_sb;
1681 : }
1682 :
1683 66982 : error = xfs_rtmount_readsb(mp);
1684 66982 : if (error)
1685 0 : goto out_free_sb;
1686 :
1687 66982 : error = xfs_filestream_mount(mp);
1688 66982 : if (error)
1689 0 : goto out_free_rtsb;
1690 :
1691 : /*
1692 : * we must configure the block size in the superblock before we run the
1693 : * full mount process as the mount process can lookup and cache inodes.
1694 : */
1695 66982 : sb->s_magic = XFS_SUPER_MAGIC;
1696 66982 : sb->s_blocksize = mp->m_sb.sb_blocksize;
1697 66982 : sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1698 66982 : sb->s_maxbytes = MAX_LFS_FILESIZE;
1699 66982 : sb->s_max_links = XFS_MAXLINK;
1700 66982 : sb->s_time_gran = 1;
1701 66982 : if (xfs_has_bigtime(mp)) {
1702 66565 : sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1703 66565 : sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1704 : } else {
1705 417 : sb->s_time_min = XFS_LEGACY_TIME_MIN;
1706 417 : sb->s_time_max = XFS_LEGACY_TIME_MAX;
1707 : }
1708 66982 : trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1709 66982 : sb->s_iflags |= SB_I_CGROUPWB;
1710 :
1711 66982 : set_posix_acl_flag(sb);
1712 :
1713 : /* version 5 superblocks support inode version counters. */
1714 66982 : if (xfs_has_crc(mp))
1715 66746 : sb->s_flags |= SB_I_VERSION;
1716 :
1717 66982 : if (xfs_has_dax_always(mp)) {
1718 113 : error = xfs_setup_dax_always(mp);
1719 113 : if (error)
1720 0 : goto out_filestream_unmount;
1721 : }
1722 :
1723 66982 : if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
1724 0 : xfs_warn(mp,
1725 : "mounting with \"discard\" option, but the device does not support discard");
1726 0 : mp->m_features &= ~XFS_FEAT_DISCARD;
1727 : }
1728 :
1729 66982 : if (xfs_has_metadir(mp))
1730 60461 : xfs_warn(mp,
1731 : "EXPERIMENTAL metadata directory feature in use. Use at your own risk!");
1732 :
1733 66982 : if (xfs_has_rtgroups(mp))
1734 60441 : xfs_warn(mp,
1735 : "EXPERIMENTAL realtime allocation group feature in use. Use at your own risk!");
1736 :
1737 66982 : if (xfs_has_reflink(mp)) {
1738 : /*
1739 : * Reflink doesn't support pagecache pages that span multiple
1740 : * realtime extents because iomap doesn't track subpage dirty
1741 : * state. This means that we cannot dirty all the pages
1742 : * backing an rt extent without dirtying the adjoining rt
1743 : * extents. If those rt extents are shared and extend into
1744 : * other pages, this leads to crazy write amplification.
1745 : *
1746 : * Hence we only support rt extent sizes that are an integer
1747 : * power of two or an integer multiple of the page size because
1748 : * we know those will align with the page size.
1749 : */
1750 80992 : if (xfs_has_realtime(mp) &&
1751 18675 : !is_power_of_2(mp->m_sb.sb_rextsize) &&
1752 4045 : (XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize) & ~PAGE_MASK)) {
1753 0 : xfs_alert(mp,
1754 : "reflink not compatible with realtime extent size %u!",
1755 : mp->m_sb.sb_rextsize);
1756 0 : error = -EINVAL;
1757 0 : goto out_filestream_unmount;
1758 : }
1759 :
1760 : /*
1761 : * always-cow mode is not supported on filesystems with rt
1762 : * extent sizes larger than a single block because we'd have
1763 : * to perform write-around for unaligned writes because remap
1764 : * requests must be aligned to an rt extent.
1765 : */
1766 62317 : if (xfs_globals.always_cow &&
1767 10 : (!xfs_has_realtime(mp) || mp->m_sb.sb_rextsize == 1)) {
1768 3831 : xfs_info(mp, "using DEBUG-only always_cow mode.");
1769 3831 : mp->m_always_cow = true;
1770 : }
1771 : }
1772 :
1773 :
1774 66982 : if (xfs_has_parent(mp))
1775 60705 : xfs_warn(mp,
1776 : "EXPERIMENTAL parent pointer feature enabled. Use at your own risk!");
1777 :
1778 66982 : error = xfs_mountfs(mp);
1779 66982 : if (error)
1780 346 : goto out_filestream_unmount;
1781 :
1782 66636 : root = igrab(VFS_I(mp->m_rootip));
1783 66636 : if (!root) {
1784 0 : error = -ENOENT;
1785 0 : goto out_unmount;
1786 : }
1787 66636 : sb->s_root = d_make_root(root);
1788 66636 : if (!sb->s_root) {
1789 0 : error = -ENOMEM;
1790 0 : goto out_unmount;
1791 : }
1792 :
1793 : return 0;
1794 :
1795 346 : out_filestream_unmount:
1796 346 : xfs_filestream_unmount(mp);
1797 346 : out_free_rtsb:
1798 346 : xfs_rtmount_freesb(mp);
1799 472 : out_free_sb:
1800 472 : xfs_freesb(mp);
1801 1058 : out_free_scrub_stats:
1802 1058 : xchk_mount_stats_free(mp);
1803 1058 : out_free_stats:
1804 1058 : free_percpu(mp->m_stats.xs_stats);
1805 1058 : out_destroy_inodegc:
1806 1058 : xfs_mount_list_del(mp);
1807 1058 : xfs_inodegc_free_percpu(mp);
1808 1058 : out_destroy_counters:
1809 1058 : xfs_destroy_percpu_counters(mp);
1810 1058 : out_destroy_workqueues:
1811 1058 : xfs_destroy_mount_workqueues(mp);
1812 1058 : out_close_devices:
1813 1058 : xfs_close_devices(mp);
1814 1128 : out_free_names:
1815 1128 : sb->s_fs_info = NULL;
1816 1128 : xfs_mount_free(mp);
1817 1128 : return error;
1818 :
1819 0 : out_unmount:
1820 0 : xfs_filestream_unmount(mp);
1821 0 : xfs_unmountfs(mp);
1822 0 : goto out_free_rtsb;
1823 : }
1824 :
1825 : static int
1826 70604 : xfs_fs_get_tree(
1827 : struct fs_context *fc)
1828 : {
1829 70604 : return get_tree_bdev(fc, xfs_fs_fill_super);
1830 : }
1831 :
1832 : static int
1833 383 : xfs_remount_rw(
1834 : struct xfs_mount *mp)
1835 : {
1836 383 : struct xfs_sb *sbp = &mp->m_sb;
1837 383 : int error;
1838 :
1839 383 : if (xfs_has_norecovery(mp)) {
1840 11 : xfs_warn(mp,
1841 : "ro->rw transition prohibited on norecovery mount");
1842 11 : return -EINVAL;
1843 : }
1844 :
1845 372 : if (xfs_sb_is_v5(sbp) &&
1846 : xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
1847 11 : xfs_warn(mp,
1848 : "ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
1849 : (sbp->sb_features_ro_compat &
1850 : XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
1851 11 : return -EINVAL;
1852 : }
1853 :
1854 361 : clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1855 :
1856 : /*
1857 : * If this is the first remount to writeable state we might have some
1858 : * superblock changes to update.
1859 : */
1860 361 : if (mp->m_update_sb) {
1861 11 : error = xfs_sync_sb(mp, false);
1862 11 : if (error) {
1863 0 : xfs_warn(mp, "failed to write sb changes");
1864 0 : return error;
1865 : }
1866 11 : mp->m_update_sb = false;
1867 : }
1868 :
1869 : /*
1870 : * Fill out the reserve pool if it is empty. Use the stashed value if
1871 : * it is non-zero, otherwise go with the default.
1872 : */
1873 361 : xfs_restore_resvblks(mp);
1874 361 : xfs_log_work_queue(mp);
1875 361 : xfs_blockgc_start(mp);
1876 :
1877 : /* Create the per-AG metadata reservation pool .*/
1878 361 : error = xfs_fs_reserve_ag_blocks(mp);
1879 361 : if (error && error != -ENOSPC)
1880 : return error;
1881 :
1882 : /* Re-enable the background inode inactivation worker. */
1883 361 : xfs_inodegc_start(mp);
1884 :
1885 361 : return 0;
1886 : }
1887 :
1888 : static int
1889 410 : xfs_remount_ro(
1890 : struct xfs_mount *mp)
1891 : {
1892 410 : struct xfs_icwalk icw = {
1893 : .icw_flags = XFS_ICWALK_FLAG_SYNC,
1894 : };
1895 410 : int error;
1896 :
1897 : /* Flush all the dirty data to disk. */
1898 410 : error = sync_filesystem(mp->m_super);
1899 410 : if (error)
1900 : return error;
1901 :
1902 : /*
1903 : * Cancel background eofb scanning so it cannot race with the final
1904 : * log force+buftarg wait and deadlock the remount.
1905 : */
1906 410 : xfs_blockgc_stop(mp);
1907 :
1908 : /*
1909 : * Clear out all remaining COW staging extents and speculative post-EOF
1910 : * preallocations so that we don't leave inodes requiring inactivation
1911 : * cleanups during reclaim on a read-only mount. We must process every
1912 : * cached inode, so this requires a synchronous cache scan.
1913 : */
1914 410 : error = xfs_blockgc_free_space(mp, &icw);
1915 410 : if (error) {
1916 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1917 0 : return error;
1918 : }
1919 :
1920 : /*
1921 : * Stop the inodegc background worker. xfs_fs_reconfigure already
1922 : * flushed all pending inodegc work when it sync'd the filesystem.
1923 : * The VFS holds s_umount, so we know that inodes cannot enter
1924 : * xfs_fs_destroy_inode during a remount operation. In readonly mode
1925 : * we send inodes straight to reclaim, so no inodes will be queued.
1926 : */
1927 410 : xfs_inodegc_stop(mp);
1928 :
1929 : /* Free the per-AG metadata reservation pool. */
1930 410 : xfs_fs_unreserve_ag_blocks(mp);
1931 :
1932 : /*
1933 : * Before we sync the metadata, we need to free up the reserve block
1934 : * pool so that the used block count in the superblock on disk is
1935 : * correct at the end of the remount. Stash the current* reserve pool
1936 : * size so that if we get remounted rw, we can return it to the same
1937 : * size.
1938 : */
1939 410 : xfs_save_resvblks(mp);
1940 :
1941 410 : xfs_log_clean(mp);
1942 410 : set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1943 :
1944 410 : return 0;
1945 : }
1946 :
1947 : /*
1948 : * Logically we would return an error here to prevent users from believing
1949 : * they might have changed mount options using remount which can't be changed.
1950 : *
1951 : * But unfortunately mount(8) adds all options from mtab and fstab to the mount
1952 : * arguments in some cases so we can't blindly reject options, but have to
1953 : * check for each specified option if it actually differs from the currently
1954 : * set option and only reject it if that's the case.
1955 : *
1956 : * Until that is implemented we return success for every remount request, and
1957 : * silently ignore all options that we can't actually change.
1958 : */
1959 : static int
1960 900 : xfs_fs_reconfigure(
1961 : struct fs_context *fc)
1962 : {
1963 900 : struct xfs_mount *mp = XFS_M(fc->root->d_sb);
1964 900 : struct xfs_mount *new_mp = fc->s_fs_info;
1965 900 : int flags = fc->sb_flags;
1966 900 : int error;
1967 :
1968 : /* version 5 superblocks always support version counters. */
1969 900 : if (xfs_has_crc(mp))
1970 889 : fc->sb_flags |= SB_I_VERSION;
1971 :
1972 900 : error = xfs_fs_validate_params(new_mp);
1973 900 : if (error)
1974 : return error;
1975 :
1976 : /* inode32 -> inode64 */
1977 900 : if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
1978 0 : mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1979 0 : mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1980 : }
1981 :
1982 : /* inode64 -> inode32 */
1983 900 : if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
1984 0 : mp->m_features |= XFS_FEAT_SMALL_INUMS;
1985 0 : mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1986 : }
1987 :
1988 : /* ro -> rw */
1989 1800 : if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
1990 383 : error = xfs_remount_rw(mp);
1991 383 : if (error)
1992 : return error;
1993 : }
1994 :
1995 : /* rw -> ro */
1996 1756 : if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
1997 410 : error = xfs_remount_ro(mp);
1998 410 : if (error)
1999 0 : return error;
2000 : }
2001 :
2002 : return 0;
2003 : }
2004 :
2005 83050 : static void xfs_fs_free(
2006 : struct fs_context *fc)
2007 : {
2008 83050 : struct xfs_mount *mp = fc->s_fs_info;
2009 :
2010 : /*
2011 : * mp is stored in the fs_context when it is initialized.
2012 : * mp is transferred to the superblock on a successful mount,
2013 : * but if an error occurs before the transfer we have to free
2014 : * it here.
2015 : */
2016 83050 : if (mp)
2017 15286 : xfs_mount_free(mp);
2018 83050 : }
2019 :
2020 : static const struct fs_context_operations xfs_context_ops = {
2021 : .parse_param = xfs_fs_parse_param,
2022 : .get_tree = xfs_fs_get_tree,
2023 : .reconfigure = xfs_fs_reconfigure,
2024 : .free = xfs_fs_free,
2025 : };
2026 :
2027 83050 : static int xfs_init_fs_context(
2028 : struct fs_context *fc)
2029 : {
2030 83050 : struct xfs_mount *mp;
2031 :
2032 83050 : mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
2033 83050 : if (!mp)
2034 : return -ENOMEM;
2035 :
2036 83050 : spin_lock_init(&mp->m_sb_lock);
2037 83050 : INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
2038 83050 : spin_lock_init(&mp->m_perag_lock);
2039 83050 : INIT_RADIX_TREE(&mp->m_rtgroup_tree, GFP_ATOMIC);
2040 83050 : spin_lock_init(&mp->m_rtgroup_lock);
2041 83050 : mutex_init(&mp->m_growlock);
2042 83050 : INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
2043 83050 : INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
2044 83050 : mp->m_kobj.kobject.kset = xfs_kset;
2045 : /*
2046 : * We don't create the finobt per-ag space reservation until after log
2047 : * recovery, so we must set this to true so that an ifree transaction
2048 : * started during log recovery will not depend on space reservations
2049 : * for finobt expansion.
2050 : */
2051 83050 : mp->m_finobt_nores = true;
2052 :
2053 : /*
2054 : * These can be overridden by the mount option parsing.
2055 : */
2056 83050 : mp->m_logbufs = -1;
2057 83050 : mp->m_logbsize = -1;
2058 83050 : mp->m_allocsize_log = 16; /* 64k */
2059 :
2060 83050 : xfs_hooks_init(&mp->m_dir_update_hooks);
2061 :
2062 : /*
2063 : * Copy binary VFS mount flags we are interested in.
2064 : */
2065 83050 : if (fc->sb_flags & SB_RDONLY)
2066 14044 : set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
2067 83050 : if (fc->sb_flags & SB_DIRSYNC)
2068 0 : mp->m_features |= XFS_FEAT_DIRSYNC;
2069 83050 : if (fc->sb_flags & SB_SYNCHRONOUS)
2070 352 : mp->m_features |= XFS_FEAT_WSYNC;
2071 :
2072 83050 : fc->s_fs_info = mp;
2073 83050 : fc->ops = &xfs_context_ops;
2074 :
2075 83050 : return 0;
2076 : }
2077 :
2078 : static struct file_system_type xfs_fs_type = {
2079 : .owner = THIS_MODULE,
2080 : .name = "xfs",
2081 : .init_fs_context = xfs_init_fs_context,
2082 : .parameters = xfs_fs_parameters,
2083 : .kill_sb = kill_block_super,
2084 : .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
2085 : };
2086 : MODULE_ALIAS_FS("xfs");
2087 :
2088 : STATIC int __init
2089 59 : xfs_init_caches(void)
2090 : {
2091 59 : int error;
2092 :
2093 59 : xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2094 : SLAB_HWCACHE_ALIGN |
2095 : SLAB_RECLAIM_ACCOUNT |
2096 : SLAB_MEM_SPREAD,
2097 : NULL);
2098 59 : if (!xfs_buf_cache)
2099 0 : goto out;
2100 :
2101 59 : xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2102 : sizeof(struct xlog_ticket),
2103 : 0, 0, NULL);
2104 59 : if (!xfs_log_ticket_cache)
2105 0 : goto out_destroy_buf_cache;
2106 :
2107 59 : error = xfs_btree_init_cur_caches();
2108 59 : if (error)
2109 0 : goto out_destroy_log_ticket_cache;
2110 :
2111 59 : error = rcbagbt_init_cur_cache();
2112 59 : if (error)
2113 0 : goto out_destroy_btree_cur_cache;
2114 :
2115 59 : error = xfs_defer_init_item_caches();
2116 59 : if (error)
2117 0 : goto out_destroy_rcbagbt_cur_cache;
2118 :
2119 59 : xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2120 : sizeof(struct xfs_da_state),
2121 : 0, 0, NULL);
2122 59 : if (!xfs_da_state_cache)
2123 0 : goto out_destroy_defer_item_cache;
2124 :
2125 59 : xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2126 : sizeof(struct xfs_ifork),
2127 : 0, 0, NULL);
2128 59 : if (!xfs_ifork_cache)
2129 0 : goto out_destroy_da_state_cache;
2130 :
2131 59 : xfs_trans_cache = kmem_cache_create("xfs_trans",
2132 : sizeof(struct xfs_trans),
2133 : 0, 0, NULL);
2134 59 : if (!xfs_trans_cache)
2135 0 : goto out_destroy_ifork_cache;
2136 :
2137 :
2138 : /*
2139 : * The size of the cache-allocated buf log item is the maximum
2140 : * size possible under XFS. This wastes a little bit of memory,
2141 : * but it is much faster.
2142 : */
2143 59 : xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2144 : sizeof(struct xfs_buf_log_item),
2145 : 0, 0, NULL);
2146 59 : if (!xfs_buf_item_cache)
2147 0 : goto out_destroy_trans_cache;
2148 :
2149 59 : xfs_efd_cache = kmem_cache_create("xfs_efd_item",
2150 : xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2151 : 0, 0, NULL);
2152 59 : if (!xfs_efd_cache)
2153 0 : goto out_destroy_buf_item_cache;
2154 :
2155 59 : xfs_efi_cache = kmem_cache_create("xfs_efi_item",
2156 : xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2157 : 0, 0, NULL);
2158 59 : if (!xfs_efi_cache)
2159 0 : goto out_destroy_efd_cache;
2160 :
2161 59 : xfs_inode_cache = kmem_cache_create("xfs_inode",
2162 : sizeof(struct xfs_inode), 0,
2163 : (SLAB_HWCACHE_ALIGN |
2164 : SLAB_RECLAIM_ACCOUNT |
2165 : SLAB_MEM_SPREAD | SLAB_ACCOUNT),
2166 : xfs_fs_inode_init_once);
2167 59 : if (!xfs_inode_cache)
2168 0 : goto out_destroy_efi_cache;
2169 :
2170 59 : xfs_ili_cache = kmem_cache_create("xfs_ili",
2171 : sizeof(struct xfs_inode_log_item), 0,
2172 : SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2173 : NULL);
2174 59 : if (!xfs_ili_cache)
2175 0 : goto out_destroy_inode_cache;
2176 :
2177 59 : xfs_icreate_cache = kmem_cache_create("xfs_icr",
2178 : sizeof(struct xfs_icreate_item),
2179 : 0, 0, NULL);
2180 59 : if (!xfs_icreate_cache)
2181 0 : goto out_destroy_ili_cache;
2182 :
2183 59 : xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2184 : sizeof(struct xfs_rud_log_item),
2185 : 0, 0, NULL);
2186 59 : if (!xfs_rud_cache)
2187 0 : goto out_destroy_icreate_cache;
2188 :
2189 59 : xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2190 : xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2191 : 0, 0, NULL);
2192 59 : if (!xfs_rui_cache)
2193 0 : goto out_destroy_rud_cache;
2194 :
2195 59 : xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2196 : sizeof(struct xfs_cud_log_item),
2197 : 0, 0, NULL);
2198 59 : if (!xfs_cud_cache)
2199 0 : goto out_destroy_rui_cache;
2200 :
2201 59 : xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2202 : xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2203 : 0, 0, NULL);
2204 59 : if (!xfs_cui_cache)
2205 0 : goto out_destroy_cud_cache;
2206 :
2207 59 : xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2208 : sizeof(struct xfs_bud_log_item),
2209 : 0, 0, NULL);
2210 59 : if (!xfs_bud_cache)
2211 0 : goto out_destroy_cui_cache;
2212 :
2213 59 : xfs_bui_cache = kmem_cache_create("xfs_bui_item",
2214 : xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2215 : 0, 0, NULL);
2216 59 : if (!xfs_bui_cache)
2217 0 : goto out_destroy_bud_cache;
2218 :
2219 59 : xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
2220 : sizeof(struct xfs_attrd_log_item),
2221 : 0, 0, NULL);
2222 59 : if (!xfs_attrd_cache)
2223 0 : goto out_destroy_bui_cache;
2224 :
2225 59 : xfs_attri_cache = kmem_cache_create("xfs_attri_item",
2226 : sizeof(struct xfs_attri_log_item),
2227 : 0, 0, NULL);
2228 59 : if (!xfs_attri_cache)
2229 0 : goto out_destroy_attrd_cache;
2230 :
2231 59 : xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2232 : sizeof(struct xfs_iunlink_item),
2233 : 0, 0, NULL);
2234 59 : if (!xfs_iunlink_cache)
2235 0 : goto out_destroy_attri_cache;
2236 :
2237 59 : xfs_sxd_cache = kmem_cache_create("xfs_sxd_item",
2238 : sizeof(struct xfs_sxd_log_item),
2239 : 0, 0, NULL);
2240 59 : if (!xfs_sxd_cache)
2241 0 : goto out_destroy_iul_cache;
2242 :
2243 59 : xfs_sxi_cache = kmem_cache_create("xfs_sxi_item",
2244 : sizeof(struct xfs_sxi_log_item),
2245 : 0, 0, NULL);
2246 59 : if (!xfs_sxi_cache)
2247 0 : goto out_destroy_sxd_cache;
2248 :
2249 59 : xfs_parent_intent_cache = kmem_cache_create("xfs_parent_intent",
2250 : sizeof(struct xfs_parent_defer),
2251 : 0, 0, NULL);
2252 59 : if (!xfs_parent_intent_cache)
2253 0 : goto out_destroy_sxi_cache;
2254 :
2255 : return 0;
2256 :
2257 : out_destroy_sxi_cache:
2258 0 : kmem_cache_destroy(xfs_sxi_cache);
2259 0 : out_destroy_sxd_cache:
2260 0 : kmem_cache_destroy(xfs_sxd_cache);
2261 0 : out_destroy_iul_cache:
2262 0 : kmem_cache_destroy(xfs_iunlink_cache);
2263 0 : out_destroy_attri_cache:
2264 0 : kmem_cache_destroy(xfs_attri_cache);
2265 0 : out_destroy_attrd_cache:
2266 0 : kmem_cache_destroy(xfs_attrd_cache);
2267 0 : out_destroy_bui_cache:
2268 0 : kmem_cache_destroy(xfs_bui_cache);
2269 0 : out_destroy_bud_cache:
2270 0 : kmem_cache_destroy(xfs_bud_cache);
2271 0 : out_destroy_cui_cache:
2272 0 : kmem_cache_destroy(xfs_cui_cache);
2273 0 : out_destroy_cud_cache:
2274 0 : kmem_cache_destroy(xfs_cud_cache);
2275 0 : out_destroy_rui_cache:
2276 0 : kmem_cache_destroy(xfs_rui_cache);
2277 0 : out_destroy_rud_cache:
2278 0 : kmem_cache_destroy(xfs_rud_cache);
2279 0 : out_destroy_icreate_cache:
2280 0 : kmem_cache_destroy(xfs_icreate_cache);
2281 0 : out_destroy_ili_cache:
2282 0 : kmem_cache_destroy(xfs_ili_cache);
2283 0 : out_destroy_inode_cache:
2284 0 : kmem_cache_destroy(xfs_inode_cache);
2285 0 : out_destroy_efi_cache:
2286 0 : kmem_cache_destroy(xfs_efi_cache);
2287 0 : out_destroy_efd_cache:
2288 0 : kmem_cache_destroy(xfs_efd_cache);
2289 0 : out_destroy_buf_item_cache:
2290 0 : kmem_cache_destroy(xfs_buf_item_cache);
2291 0 : out_destroy_trans_cache:
2292 0 : kmem_cache_destroy(xfs_trans_cache);
2293 0 : out_destroy_ifork_cache:
2294 0 : kmem_cache_destroy(xfs_ifork_cache);
2295 0 : out_destroy_da_state_cache:
2296 0 : kmem_cache_destroy(xfs_da_state_cache);
2297 0 : out_destroy_defer_item_cache:
2298 0 : xfs_defer_destroy_item_caches();
2299 0 : out_destroy_rcbagbt_cur_cache:
2300 0 : rcbagbt_destroy_cur_cache();
2301 0 : out_destroy_btree_cur_cache:
2302 0 : xfs_btree_destroy_cur_caches();
2303 0 : out_destroy_log_ticket_cache:
2304 0 : kmem_cache_destroy(xfs_log_ticket_cache);
2305 0 : out_destroy_buf_cache:
2306 0 : kmem_cache_destroy(xfs_buf_cache);
2307 : out:
2308 : return -ENOMEM;
2309 : }
2310 :
2311 : STATIC void
2312 58 : xfs_destroy_caches(void)
2313 : {
2314 : /*
2315 : * Make sure all delayed rcu free are flushed before we
2316 : * destroy caches.
2317 : */
2318 58 : rcu_barrier();
2319 58 : kmem_cache_destroy(xfs_parent_intent_cache);
2320 58 : kmem_cache_destroy(xfs_sxd_cache);
2321 58 : kmem_cache_destroy(xfs_sxi_cache);
2322 58 : kmem_cache_destroy(xfs_iunlink_cache);
2323 58 : kmem_cache_destroy(xfs_attri_cache);
2324 58 : kmem_cache_destroy(xfs_attrd_cache);
2325 58 : kmem_cache_destroy(xfs_bui_cache);
2326 58 : kmem_cache_destroy(xfs_bud_cache);
2327 58 : kmem_cache_destroy(xfs_cui_cache);
2328 58 : kmem_cache_destroy(xfs_cud_cache);
2329 58 : kmem_cache_destroy(xfs_rui_cache);
2330 58 : kmem_cache_destroy(xfs_rud_cache);
2331 58 : kmem_cache_destroy(xfs_icreate_cache);
2332 58 : kmem_cache_destroy(xfs_ili_cache);
2333 58 : kmem_cache_destroy(xfs_inode_cache);
2334 58 : kmem_cache_destroy(xfs_efi_cache);
2335 58 : kmem_cache_destroy(xfs_efd_cache);
2336 58 : kmem_cache_destroy(xfs_buf_item_cache);
2337 58 : kmem_cache_destroy(xfs_trans_cache);
2338 58 : kmem_cache_destroy(xfs_ifork_cache);
2339 58 : kmem_cache_destroy(xfs_da_state_cache);
2340 58 : xfs_defer_destroy_item_caches();
2341 58 : rcbagbt_destroy_cur_cache();
2342 58 : xfs_btree_destroy_cur_caches();
2343 58 : kmem_cache_destroy(xfs_log_ticket_cache);
2344 58 : kmem_cache_destroy(xfs_buf_cache);
2345 58 : }
2346 :
2347 : STATIC int __init
2348 59 : xfs_init_workqueues(void)
2349 : {
2350 : /*
2351 : * The allocation workqueue can be used in memory reclaim situations
2352 : * (writepage path), and parallelism is only limited by the number of
2353 : * AGs in all the filesystems mounted. Hence use the default large
2354 : * max_active value for this workqueue.
2355 : */
2356 59 : xfs_alloc_wq = alloc_workqueue("xfsalloc",
2357 : XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
2358 59 : if (!xfs_alloc_wq)
2359 : return -ENOMEM;
2360 :
2361 59 : xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
2362 : 0);
2363 59 : if (!xfs_discard_wq)
2364 0 : goto out_free_alloc_wq;
2365 :
2366 : return 0;
2367 : out_free_alloc_wq:
2368 0 : destroy_workqueue(xfs_alloc_wq);
2369 0 : return -ENOMEM;
2370 : }
2371 :
2372 : STATIC void
2373 58 : xfs_destroy_workqueues(void)
2374 : {
2375 58 : destroy_workqueue(xfs_discard_wq);
2376 58 : destroy_workqueue(xfs_alloc_wq);
2377 58 : }
2378 :
2379 : #ifdef CONFIG_HOTPLUG_CPU
2380 : static int
2381 158 : xfs_cpu_dead(
2382 : unsigned int cpu)
2383 : {
2384 158 : struct xfs_mount *mp, *n;
2385 :
2386 158 : spin_lock(&xfs_mount_list_lock);
2387 316 : list_for_each_entry_safe(mp, n, &xfs_mount_list, m_mount_list) {
2388 158 : spin_unlock(&xfs_mount_list_lock);
2389 158 : xfs_inodegc_cpu_dead(mp, cpu);
2390 158 : xlog_cil_pcp_dead(mp->m_log, cpu);
2391 158 : spin_lock(&xfs_mount_list_lock);
2392 : }
2393 158 : spin_unlock(&xfs_mount_list_lock);
2394 158 : return 0;
2395 : }
2396 :
2397 : static int __init
2398 59 : xfs_cpu_hotplug_init(void)
2399 : {
2400 59 : int error;
2401 :
2402 59 : error = cpuhp_setup_state_nocalls(CPUHP_XFS_DEAD, "xfs:dead", NULL,
2403 : xfs_cpu_dead);
2404 59 : if (error < 0)
2405 0 : xfs_alert(NULL,
2406 : "Failed to initialise CPU hotplug, error %d. XFS is non-functional.",
2407 : error);
2408 59 : return error;
2409 : }
2410 :
2411 : static void
2412 : xfs_cpu_hotplug_destroy(void)
2413 : {
2414 58 : cpuhp_remove_state_nocalls(CPUHP_XFS_DEAD);
2415 0 : }
2416 :
2417 : #else /* !CONFIG_HOTPLUG_CPU */
2418 : static inline int xfs_cpu_hotplug_init(void) { return 0; }
2419 : static inline void xfs_cpu_hotplug_destroy(void) {}
2420 : #endif
2421 :
2422 : STATIC int __init
2423 59 : init_xfs_fs(void)
2424 : {
2425 59 : int error;
2426 :
2427 59 : xfs_check_ondisk_structs();
2428 :
2429 59 : error = xfs_dahash_test();
2430 59 : if (error)
2431 : return error;
2432 :
2433 59 : printk(KERN_INFO XFS_VERSION_STRING " with "
2434 : XFS_BUILD_OPTIONS " enabled\n");
2435 :
2436 59 : xfs_dir_startup();
2437 :
2438 59 : error = xfs_cpu_hotplug_init();
2439 59 : if (error)
2440 0 : goto out;
2441 :
2442 59 : error = xfs_init_caches();
2443 59 : if (error)
2444 0 : goto out_destroy_hp;
2445 :
2446 59 : error = xfs_init_workqueues();
2447 59 : if (error)
2448 0 : goto out_destroy_caches;
2449 :
2450 59 : error = xfs_mru_cache_init();
2451 59 : if (error)
2452 0 : goto out_destroy_wq;
2453 :
2454 59 : error = xfs_init_procfs();
2455 59 : if (error)
2456 0 : goto out_mru_cache_uninit;
2457 :
2458 59 : error = xfs_sysctl_register();
2459 59 : if (error)
2460 0 : goto out_cleanup_procfs;
2461 :
2462 59 : xfs_debugfs = xfs_debugfs_mkdir("xfs", NULL);
2463 :
2464 59 : xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
2465 59 : if (!xfs_kset) {
2466 0 : error = -ENOMEM;
2467 0 : goto out_debugfs_unregister;
2468 : }
2469 :
2470 59 : xfsstats.xs_kobj.kobject.kset = xfs_kset;
2471 :
2472 59 : xfsstats.xs_stats = alloc_percpu(struct xfsstats);
2473 59 : if (!xfsstats.xs_stats) {
2474 0 : error = -ENOMEM;
2475 0 : goto out_kset_unregister;
2476 : }
2477 :
2478 59 : error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2479 : "stats");
2480 59 : if (error)
2481 0 : goto out_free_stats;
2482 :
2483 59 : error = xchk_global_stats_setup(xfs_debugfs);
2484 59 : if (error)
2485 0 : goto out_remove_stats_kobj;
2486 :
2487 : #ifdef DEBUG
2488 59 : xfs_dbg_kobj.kobject.kset = xfs_kset;
2489 59 : error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2490 59 : if (error)
2491 0 : goto out_remove_scrub_stats;
2492 : #endif
2493 :
2494 59 : error = xfs_qm_init();
2495 59 : if (error)
2496 0 : goto out_remove_dbg_kobj;
2497 :
2498 59 : error = register_filesystem(&xfs_fs_type);
2499 59 : if (error)
2500 0 : goto out_qm_exit;
2501 : return 0;
2502 :
2503 : out_qm_exit:
2504 0 : xfs_qm_exit();
2505 0 : out_remove_dbg_kobj:
2506 : #ifdef DEBUG
2507 0 : xfs_sysfs_del(&xfs_dbg_kobj);
2508 0 : out_remove_scrub_stats:
2509 : #endif
2510 0 : xchk_global_stats_teardown();
2511 0 : out_remove_stats_kobj:
2512 0 : xfs_sysfs_del(&xfsstats.xs_kobj);
2513 0 : out_free_stats:
2514 0 : free_percpu(xfsstats.xs_stats);
2515 0 : out_kset_unregister:
2516 0 : kset_unregister(xfs_kset);
2517 0 : out_debugfs_unregister:
2518 0 : debugfs_remove(xfs_debugfs);
2519 0 : xfs_sysctl_unregister();
2520 0 : out_cleanup_procfs:
2521 0 : xfs_cleanup_procfs();
2522 0 : out_mru_cache_uninit:
2523 0 : xfs_mru_cache_uninit();
2524 0 : out_destroy_wq:
2525 0 : xfs_destroy_workqueues();
2526 0 : out_destroy_caches:
2527 0 : xfs_destroy_caches();
2528 0 : out_destroy_hp:
2529 0 : xfs_cpu_hotplug_destroy();
2530 : out:
2531 : return error;
2532 : }
2533 :
2534 : STATIC void __exit
2535 58 : exit_xfs_fs(void)
2536 : {
2537 58 : xfs_qm_exit();
2538 58 : unregister_filesystem(&xfs_fs_type);
2539 : #ifdef DEBUG
2540 58 : xfs_sysfs_del(&xfs_dbg_kobj);
2541 : #endif
2542 58 : xchk_global_stats_teardown();
2543 58 : xfs_sysfs_del(&xfsstats.xs_kobj);
2544 58 : free_percpu(xfsstats.xs_stats);
2545 58 : kset_unregister(xfs_kset);
2546 58 : debugfs_remove(xfs_debugfs);
2547 58 : xfs_sysctl_unregister();
2548 58 : xfs_cleanup_procfs();
2549 58 : xfs_mru_cache_uninit();
2550 58 : xfs_destroy_workqueues();
2551 58 : xfs_destroy_caches();
2552 58 : xfs_uuid_table_free();
2553 58 : xfs_cpu_hotplug_destroy();
2554 58 : }
2555 :
2556 : module_init(init_xfs_fs);
2557 : module_exit(exit_xfs_fs);
2558 :
2559 : MODULE_AUTHOR("Silicon Graphics, Inc.");
2560 : MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2561 : MODULE_LICENSE("GPL");
|