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