blob: 4d2e87462ac4a19499ad45ea17f2246379beb577 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scotta805bad2006-06-19 08:40:27 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +10009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110015#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_bmap.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110017#include "xfs_alloc.h"
Christoph Hellwig9909c4a2007-10-11 18:11:14 +100018#include "xfs_fsops.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_buf_item.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log.h"
David Chinnera67d7c52007-11-23 16:29:32 +110022#include "xfs_log_priv.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100023#include "xfs_dir2.h"
Christoph Hellwig9f8868f2008-07-18 17:11:46 +100024#include "xfs_extfree_item.h"
25#include "xfs_mru_cache.h"
26#include "xfs_inode_item.h"
Dave Chinner6d8b79c2012-10-08 21:56:09 +110027#include "xfs_icache.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000028#include "xfs_trace.h"
Dave Chinner3ebe7d22013-06-27 16:04:53 +100029#include "xfs_icreate_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_filestream.h"
31#include "xfs_quota.h"
Brian Foster65b65732014-09-09 11:52:42 +100032#include "xfs_sysfs.h"
Darrick J. Wong30cbc592016-03-09 08:15:14 +110033#include "xfs_ondisk.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100034#include "xfs_rmap_item.h"
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -070035#include "xfs_refcount_item.h"
Darrick J. Wong6413a012016-10-03 09:11:25 -070036#include "xfs_bmap_item.h"
Darrick J. Wong5e7e6052016-10-03 09:11:38 -070037#include "xfs_reflink.h"
Darrick J. Wong894ecac2021-01-22 16:48:44 -080038#include "xfs_pwork.h"
Dave Chinner9bbafc712021-06-02 10:48:24 +100039#include "xfs_ag.h"
Darrick J. Wongf3c799c2021-10-12 14:11:01 -070040#include "xfs_defer.h"
Darrick J. Wong4136e382022-05-22 15:59:48 +100041#include "xfs_attr_item.h"
Darrick J. Wongd9c61cc2022-05-27 10:33:29 +100042#include "xfs_xattr.h"
Dave Chinner784eb7d2022-07-14 11:47:42 +100043#include "xfs_iunlink_item.h"
Darrick J. Wong3cfb9292023-03-16 09:31:20 -070044#include "xfs_dahash_test.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Adam Borowskidddde682018-10-18 17:20:19 +110046#include <linux/magic.h>
Ian Kent73e5fff2019-11-04 13:58:46 -080047#include <linux/fs_context.h>
48#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Alexey Dobriyanb87221d2009-09-21 17:01:09 -070050static const struct super_operations xfs_super_operations;
Brian Foster65b65732014-09-09 11:52:42 +100051
Dave Chinnere3aed1a2014-09-29 10:46:08 +100052static struct kset *xfs_kset; /* top-level xfs sysfs dir */
Brian Foster65b65732014-09-09 11:52:42 +100053#ifdef DEBUG
54static struct xfs_kobj xfs_dbg_kobj; /* global debug sysfs attrs */
55#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Dave Chinner0ed17f02021-08-06 11:05:38 -070057#ifdef CONFIG_HOTPLUG_CPU
58static LIST_HEAD(xfs_mount_list);
59static DEFINE_SPINLOCK(xfs_mount_list_lock);
60
61static inline void xfs_mount_list_add(struct xfs_mount *mp)
62{
63 spin_lock(&xfs_mount_list_lock);
64 list_add(&mp->m_mount_list, &xfs_mount_list);
65 spin_unlock(&xfs_mount_list_lock);
66}
67
68static inline void xfs_mount_list_del(struct xfs_mount *mp)
69{
70 spin_lock(&xfs_mount_list_lock);
71 list_del(&mp->m_mount_list);
72 spin_unlock(&xfs_mount_list_lock);
73}
74#else /* !CONFIG_HOTPLUG_CPU */
75static inline void xfs_mount_list_add(struct xfs_mount *mp) {}
76static inline void xfs_mount_list_del(struct xfs_mount *mp) {}
77#endif
78
Ira Weiny8d6c3442020-05-04 09:02:42 -070079enum xfs_dax_mode {
80 XFS_DAX_INODE = 0,
81 XFS_DAX_ALWAYS = 1,
82 XFS_DAX_NEVER = 2,
83};
84
85static void
86xfs_mount_set_dax_mode(
87 struct xfs_mount *mp,
88 enum xfs_dax_mode mode)
89{
90 switch (mode) {
91 case XFS_DAX_INODE:
Dave Chinner0560f312021-08-18 18:46:52 -070092 mp->m_features &= ~(XFS_FEAT_DAX_ALWAYS | XFS_FEAT_DAX_NEVER);
Ira Weiny8d6c3442020-05-04 09:02:42 -070093 break;
94 case XFS_DAX_ALWAYS:
Dave Chinner0560f312021-08-18 18:46:52 -070095 mp->m_features |= XFS_FEAT_DAX_ALWAYS;
96 mp->m_features &= ~XFS_FEAT_DAX_NEVER;
Ira Weiny8d6c3442020-05-04 09:02:42 -070097 break;
98 case XFS_DAX_NEVER:
Dave Chinner0560f312021-08-18 18:46:52 -070099 mp->m_features |= XFS_FEAT_DAX_NEVER;
100 mp->m_features &= ~XFS_FEAT_DAX_ALWAYS;
Ira Weiny8d6c3442020-05-04 09:02:42 -0700101 break;
102 }
103}
104
105static const struct constant_table dax_param_enums[] = {
106 {"inode", XFS_DAX_INODE },
107 {"always", XFS_DAX_ALWAYS },
108 {"never", XFS_DAX_NEVER },
109 {}
110};
111
Christoph Hellwig62a877e2008-07-18 17:12:36 +1000112/*
113 * Table driven mount option parser.
Christoph Hellwig62a877e2008-07-18 17:12:36 +1000114 */
115enum {
Ian Kent8da57c52019-10-28 08:41:42 -0700116 Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
Eric Sandeen2e74af02016-03-02 09:55:38 +1100117 Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
Christoph Hellwig94079282019-04-28 08:32:52 -0700118 Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
Eric Sandeen1c02d502018-07-26 09:11:27 -0700119 Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32, Opt_ikeep,
120 Opt_noikeep, Opt_largeio, Opt_nolargeio, Opt_attr2, Opt_noattr2,
121 Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
122 Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
Eric Sandeen2e74af02016-03-02 09:55:38 +1100123 Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
Ira Weiny8d6c3442020-05-04 09:02:42 -0700124 Opt_discard, Opt_nodiscard, Opt_dax, Opt_dax_enum,
Christoph Hellwig62a877e2008-07-18 17:12:36 +1000125};
126
Al Virod7167b12019-09-07 07:23:15 -0400127static const struct fs_parameter_spec xfs_fs_parameters[] = {
Ian Kent73e5fff2019-11-04 13:58:46 -0800128 fsparam_u32("logbufs", Opt_logbufs),
129 fsparam_string("logbsize", Opt_logbsize),
130 fsparam_string("logdev", Opt_logdev),
131 fsparam_string("rtdev", Opt_rtdev),
132 fsparam_flag("wsync", Opt_wsync),
133 fsparam_flag("noalign", Opt_noalign),
134 fsparam_flag("swalloc", Opt_swalloc),
135 fsparam_u32("sunit", Opt_sunit),
136 fsparam_u32("swidth", Opt_swidth),
137 fsparam_flag("nouuid", Opt_nouuid),
138 fsparam_flag("grpid", Opt_grpid),
139 fsparam_flag("nogrpid", Opt_nogrpid),
140 fsparam_flag("bsdgroups", Opt_bsdgroups),
141 fsparam_flag("sysvgroups", Opt_sysvgroups),
142 fsparam_string("allocsize", Opt_allocsize),
143 fsparam_flag("norecovery", Opt_norecovery),
144 fsparam_flag("inode64", Opt_inode64),
145 fsparam_flag("inode32", Opt_inode32),
146 fsparam_flag("ikeep", Opt_ikeep),
147 fsparam_flag("noikeep", Opt_noikeep),
148 fsparam_flag("largeio", Opt_largeio),
149 fsparam_flag("nolargeio", Opt_nolargeio),
150 fsparam_flag("attr2", Opt_attr2),
151 fsparam_flag("noattr2", Opt_noattr2),
152 fsparam_flag("filestreams", Opt_filestreams),
153 fsparam_flag("quota", Opt_quota),
154 fsparam_flag("noquota", Opt_noquota),
155 fsparam_flag("usrquota", Opt_usrquota),
156 fsparam_flag("grpquota", Opt_grpquota),
157 fsparam_flag("prjquota", Opt_prjquota),
158 fsparam_flag("uquota", Opt_uquota),
159 fsparam_flag("gquota", Opt_gquota),
160 fsparam_flag("pquota", Opt_pquota),
161 fsparam_flag("uqnoenforce", Opt_uqnoenforce),
162 fsparam_flag("gqnoenforce", Opt_gqnoenforce),
163 fsparam_flag("pqnoenforce", Opt_pqnoenforce),
164 fsparam_flag("qnoenforce", Opt_qnoenforce),
165 fsparam_flag("discard", Opt_discard),
166 fsparam_flag("nodiscard", Opt_nodiscard),
167 fsparam_flag("dax", Opt_dax),
Ira Weiny8d6c3442020-05-04 09:02:42 -0700168 fsparam_enum("dax", Opt_dax_enum, dax_param_enums),
Ian Kent73e5fff2019-11-04 13:58:46 -0800169 {}
Christoph Hellwig62a877e2008-07-18 17:12:36 +1000170};
171
David Chinnera67d7c52007-11-23 16:29:32 +1100172struct proc_xfs_info {
Dave Chinnercbe4dab2015-06-04 09:19:18 +1000173 uint64_t flag;
174 char *str;
David Chinnera67d7c52007-11-23 16:29:32 +1100175};
176
Christoph Hellwig21f55992019-10-28 08:41:47 -0700177static int
178xfs_fs_show_options(
179 struct seq_file *m,
180 struct dentry *root)
David Chinnera67d7c52007-11-23 16:29:32 +1100181{
182 static struct proc_xfs_info xfs_info_set[] = {
183 /* the few simple ones we can get from the mount struct */
Dave Chinner0560f312021-08-18 18:46:52 -0700184 { XFS_FEAT_IKEEP, ",ikeep" },
185 { XFS_FEAT_WSYNC, ",wsync" },
186 { XFS_FEAT_NOALIGN, ",noalign" },
187 { XFS_FEAT_SWALLOC, ",swalloc" },
188 { XFS_FEAT_NOUUID, ",nouuid" },
189 { XFS_FEAT_NORECOVERY, ",norecovery" },
190 { XFS_FEAT_ATTR2, ",attr2" },
191 { XFS_FEAT_FILESTREAMS, ",filestreams" },
192 { XFS_FEAT_GRPID, ",grpid" },
193 { XFS_FEAT_DISCARD, ",discard" },
194 { XFS_FEAT_LARGE_IOSIZE, ",largeio" },
195 { XFS_FEAT_DAX_ALWAYS, ",dax=always" },
196 { XFS_FEAT_DAX_NEVER, ",dax=never" },
David Chinnera67d7c52007-11-23 16:29:32 +1100197 { 0, NULL }
198 };
Christoph Hellwig21f55992019-10-28 08:41:47 -0700199 struct xfs_mount *mp = XFS_M(root->d_sb);
David Chinnera67d7c52007-11-23 16:29:32 +1100200 struct proc_xfs_info *xfs_infop;
201
202 for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
Dave Chinner0560f312021-08-18 18:46:52 -0700203 if (mp->m_features & xfs_infop->flag)
David Chinnera67d7c52007-11-23 16:29:32 +1100204 seq_puts(m, xfs_infop->str);
205 }
Christoph Hellwig1775c502019-10-28 08:41:47 -0700206
Dave Chinner0560f312021-08-18 18:46:52 -0700207 seq_printf(m, ",inode%d", xfs_has_small_inums(mp) ? 32 : 64);
David Chinnera67d7c52007-11-23 16:29:32 +1100208
Dave Chinner0560f312021-08-18 18:46:52 -0700209 if (xfs_has_allocsize(mp))
Eric Sandeen2e74af02016-03-02 09:55:38 +1100210 seq_printf(m, ",allocsize=%dk",
Christoph Hellwigaa58d442019-10-28 08:41:46 -0700211 (1 << mp->m_allocsize_log) >> 10);
David Chinnera67d7c52007-11-23 16:29:32 +1100212
213 if (mp->m_logbufs > 0)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100214 seq_printf(m, ",logbufs=%d", mp->m_logbufs);
David Chinnera67d7c52007-11-23 16:29:32 +1100215 if (mp->m_logbsize > 0)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100216 seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
David Chinnera67d7c52007-11-23 16:29:32 +1100217
218 if (mp->m_logname)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100219 seq_show_option(m, "logdev", mp->m_logname);
David Chinnera67d7c52007-11-23 16:29:32 +1100220 if (mp->m_rtname)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100221 seq_show_option(m, "rtdev", mp->m_rtname);
David Chinnera67d7c52007-11-23 16:29:32 +1100222
223 if (mp->m_dalign > 0)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100224 seq_printf(m, ",sunit=%d",
David Chinnera67d7c52007-11-23 16:29:32 +1100225 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
226 if (mp->m_swidth > 0)
Eric Sandeen2e74af02016-03-02 09:55:38 +1100227 seq_printf(m, ",swidth=%d",
David Chinnera67d7c52007-11-23 16:29:32 +1100228 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
229
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700230 if (mp->m_qflags & XFS_UQUOTA_ENFD)
231 seq_puts(m, ",usrquota");
232 else if (mp->m_qflags & XFS_UQUOTA_ACCT)
233 seq_puts(m, ",uqnoenforce");
David Chinnera67d7c52007-11-23 16:29:32 +1100234
Christoph Hellwig149e53a2021-08-06 11:05:37 -0700235 if (mp->m_qflags & XFS_PQUOTA_ENFD)
236 seq_puts(m, ",prjquota");
237 else if (mp->m_qflags & XFS_PQUOTA_ACCT)
238 seq_puts(m, ",pqnoenforce");
239
240 if (mp->m_qflags & XFS_GQUOTA_ENFD)
241 seq_puts(m, ",grpquota");
242 else if (mp->m_qflags & XFS_GQUOTA_ACCT)
243 seq_puts(m, ",gqnoenforce");
David Chinnera67d7c52007-11-23 16:29:32 +1100244
245 if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
Eric Sandeen2e74af02016-03-02 09:55:38 +1100246 seq_puts(m, ",noquota");
Christoph Hellwig21f55992019-10-28 08:41:47 -0700247
248 return 0;
David Chinnera67d7c52007-11-23 16:29:32 +1100249}
Eric Sandeen91083262019-05-01 20:26:30 -0700250
Dave Chinner7ac2ff82023-02-13 09:14:52 +1100251static bool
252xfs_set_inode_alloc_perag(
253 struct xfs_perag *pag,
254 xfs_ino_t ino,
255 xfs_agnumber_t max_metadata)
256{
257 if (!xfs_is_inode32(pag->pag_mount)) {
258 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
259 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
260 return false;
261 }
262
263 if (ino > XFS_MAXINUMBER_32) {
264 clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
265 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
266 return false;
267 }
268
269 set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
270 if (pag->pag_agno < max_metadata)
271 set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
272 else
273 clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
274 return true;
275}
276
Eric Sandeen9de67c32014-07-24 20:51:54 +1000277/*
Eric Sandeen12c3f052016-03-02 09:58:09 +1100278 * Set parameters for inode allocation heuristics, taking into account
279 * filesystem size and inode32/inode64 mount options; i.e. specifically
Dave Chinner0560f312021-08-18 18:46:52 -0700280 * whether or not XFS_FEAT_SMALL_INUMS is set.
Eric Sandeen12c3f052016-03-02 09:58:09 +1100281 *
282 * Inode allocation patterns are altered only if inode32 is requested
Dave Chinner0560f312021-08-18 18:46:52 -0700283 * (XFS_FEAT_SMALL_INUMS), and the filesystem is sufficiently large.
Dave Chinner2e973b22021-08-18 18:46:52 -0700284 * If altered, XFS_OPSTATE_INODE32 is set as well.
Eric Sandeen12c3f052016-03-02 09:58:09 +1100285 *
286 * An agcount independent of that in the mount structure is provided
287 * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
288 * to the potentially higher ag count.
289 *
290 * Returns the maximum AG index which may contain inodes.
Eric Sandeen9de67c32014-07-24 20:51:54 +1000291 */
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300292xfs_agnumber_t
Eric Sandeen12c3f052016-03-02 09:58:09 +1100293xfs_set_inode_alloc(
294 struct xfs_mount *mp,
295 xfs_agnumber_t agcount)
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300296{
Eric Sandeen12c3f052016-03-02 09:58:09 +1100297 xfs_agnumber_t index;
Carlos Maiolino4056c1d2012-09-20 10:32:40 -0300298 xfs_agnumber_t maxagi = 0;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300299 xfs_sb_t *sbp = &mp->m_sb;
300 xfs_agnumber_t max_metadata;
Eric Sandeen54aa61f2014-07-24 20:53:10 +1000301 xfs_agino_t agino;
302 xfs_ino_t ino;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300303
Eric Sandeen12c3f052016-03-02 09:58:09 +1100304 /*
305 * Calculate how much should be reserved for inodes to meet
306 * the max inode percentage. Used only for inode32.
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300307 */
Darrick J. Wongef325952019-06-05 11:19:34 -0700308 if (M_IGEO(mp)->maxicount) {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700309 uint64_t icount;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300310
311 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
312 do_div(icount, 100);
313 icount += sbp->sb_agblocks - 1;
314 do_div(icount, sbp->sb_agblocks);
315 max_metadata = icount;
316 } else {
Eric Sandeen9de67c32014-07-24 20:51:54 +1000317 max_metadata = agcount;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300318 }
319
Eric Sandeen12c3f052016-03-02 09:58:09 +1100320 /* Get the last possible inode in the filesystem */
Darrick J. Wong43004b22018-12-12 08:46:24 -0800321 agino = XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
Eric Sandeen12c3f052016-03-02 09:58:09 +1100322 ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
Eric Sandeen54aa61f2014-07-24 20:53:10 +1000323
Eric Sandeen12c3f052016-03-02 09:58:09 +1100324 /*
325 * If user asked for no more than 32-bit inodes, and the fs is
Dave Chinner2e973b22021-08-18 18:46:52 -0700326 * sufficiently large, set XFS_OPSTATE_INODE32 if we must alter
Eric Sandeen12c3f052016-03-02 09:58:09 +1100327 * the allocator to accommodate the request.
328 */
Dave Chinner0560f312021-08-18 18:46:52 -0700329 if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
Dave Chinner2e973b22021-08-18 18:46:52 -0700330 set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
Eric Sandeen12c3f052016-03-02 09:58:09 +1100331 else
Dave Chinner2e973b22021-08-18 18:46:52 -0700332 clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300333
Eric Sandeen9de67c32014-07-24 20:51:54 +1000334 for (index = 0; index < agcount; index++) {
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300335 struct xfs_perag *pag;
336
Eric Sandeen12c3f052016-03-02 09:58:09 +1100337 ino = XFS_AGINO_TO_INO(mp, index, agino);
338
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300339 pag = xfs_perag_get(mp, index);
Dave Chinner7ac2ff82023-02-13 09:14:52 +1100340 if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
341 maxagi++;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300342 xfs_perag_put(pag);
343 }
344
Dave Chinner2e973b22021-08-18 18:46:52 -0700345 return xfs_is_inode32(mp) ? maxagi : agcount;
Carlos Maiolino2d2194f2012-09-20 10:32:38 -0300346}
347
Christoph Hellwig679a9942021-11-29 11:21:41 +0100348static int
349xfs_setup_dax_always(
350 struct xfs_mount *mp)
Christoph Hellwiga384f082021-08-26 15:55:09 +0200351{
Christoph Hellwig7b0800d2021-11-29 11:21:42 +0100352 if (!mp->m_ddev_targp->bt_daxdev &&
353 (!mp->m_rtdev_targp || !mp->m_rtdev_targp->bt_daxdev)) {
Christoph Hellwig679a9942021-11-29 11:21:41 +0100354 xfs_alert(mp,
355 "DAX unsupported by block device. Turning off DAX.");
356 goto disable_dax;
357 }
358
Christoph Hellwig7b0800d2021-11-29 11:21:42 +0100359 if (mp->m_super->s_blocksize != PAGE_SIZE) {
360 xfs_alert(mp,
361 "DAX not supported for blocksize. Turning off DAX.");
362 goto disable_dax;
363 }
364
Shiyang Ruan35fcd752022-06-09 22:34:35 +0800365 if (xfs_has_reflink(mp) &&
366 bdev_is_partition(mp->m_ddev_targp->bt_bdev)) {
367 xfs_alert(mp,
368 "DAX and reflink cannot work with multi-partitions!");
Christoph Hellwig679a9942021-11-29 11:21:41 +0100369 return -EINVAL;
370 }
371
372 xfs_warn(mp, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
373 return 0;
374
375disable_dax:
376 xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
377 return 0;
Christoph Hellwiga384f082021-08-26 15:55:09 +0200378}
379
Hannes Eder3180e662009-03-04 19:34:10 +0100380STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381xfs_blkdev_get(
382 xfs_mount_t *mp,
383 const char *name,
384 struct block_device **bdevp)
385{
386 int error = 0;
387
Tejun Heod4d77622010-11-13 11:55:18 +0100388 *bdevp = blkdev_get_by_path(name, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
389 mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (IS_ERR(*bdevp)) {
391 error = PTR_ERR(*bdevp);
Eric Sandeen77af5742014-12-24 09:47:27 +1100392 xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Dave Chinner24513372014-06-25 14:58:08 +1000395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Hannes Eder3180e662009-03-04 19:34:10 +0100398STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399xfs_blkdev_put(
400 struct block_device *bdev)
401{
402 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +0100403 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000406STATIC void
407xfs_close_devices(
408 struct xfs_mount *mp)
409{
410 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
Lachlan McIlroyc032bfc2008-07-18 17:13:12 +1000411 struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
Dan Williams486aff52017-08-24 15:12:50 -0700412
Eric Sandeena1f69412018-04-06 10:09:42 -0700413 xfs_free_buftarg(mp->m_logdev_targp);
Lachlan McIlroyc032bfc2008-07-18 17:13:12 +1000414 xfs_blkdev_put(logdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000415 }
416 if (mp->m_rtdev_targp) {
Lachlan McIlroyc032bfc2008-07-18 17:13:12 +1000417 struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
Dan Williams486aff52017-08-24 15:12:50 -0700418
Eric Sandeena1f69412018-04-06 10:09:42 -0700419 xfs_free_buftarg(mp->m_rtdev_targp);
Lachlan McIlroyc032bfc2008-07-18 17:13:12 +1000420 xfs_blkdev_put(rtdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000421 }
Eric Sandeena1f69412018-04-06 10:09:42 -0700422 xfs_free_buftarg(mp->m_ddev_targp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000423}
424
425/*
426 * The file system configurations are:
427 * (1) device (partition) with data and internal log
428 * (2) logical volume with data and log subvolumes.
429 * (3) logical volume with data, log, and realtime subvolumes.
430 *
431 * We only have to handle opening the log and realtime volumes here if
432 * they are present. The data subvolume has already been opened by
433 * get_sb_bdev() and is stored in sb->s_bdev.
434 */
435STATIC int
436xfs_open_devices(
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100437 struct xfs_mount *mp)
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000438{
439 struct block_device *ddev = mp->m_super->s_bdev;
440 struct block_device *logdev = NULL, *rtdev = NULL;
441 int error;
442
443 /*
444 * Open real time and log devices - order is important.
445 */
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100446 if (mp->m_logname) {
447 error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000448 if (error)
Christoph Hellwig5b5abbe2021-11-29 11:21:55 +0100449 return error;
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000450 }
451
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100452 if (mp->m_rtname) {
453 error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000454 if (error)
455 goto out_close_logdev;
456
457 if (rtdev == ddev || rtdev == logdev) {
Dave Chinner4f107002011-03-07 10:00:35 +1100458 xfs_warn(mp,
459 "Cannot mount filesystem with identical rtdev and ddev/logdev.");
Dave Chinner24513372014-06-25 14:58:08 +1000460 error = -EINVAL;
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000461 goto out_close_rtdev;
462 }
463 }
464
465 /*
466 * Setup xfs_mount buffer target pointers
467 */
Dave Chinner24513372014-06-25 14:58:08 +1000468 error = -ENOMEM;
Christoph Hellwig5b5abbe2021-11-29 11:21:55 +0100469 mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000470 if (!mp->m_ddev_targp)
471 goto out_close_rtdev;
472
473 if (rtdev) {
Christoph Hellwig5b5abbe2021-11-29 11:21:55 +0100474 mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000475 if (!mp->m_rtdev_targp)
476 goto out_free_ddev_targ;
477 }
478
479 if (logdev && logdev != ddev) {
Christoph Hellwig5b5abbe2021-11-29 11:21:55 +0100480 mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000481 if (!mp->m_logdev_targp)
482 goto out_free_rtdev_targ;
483 } else {
484 mp->m_logdev_targp = mp->m_ddev_targp;
485 }
486
487 return 0;
488
489 out_free_rtdev_targ:
490 if (mp->m_rtdev_targp)
Eric Sandeena1f69412018-04-06 10:09:42 -0700491 xfs_free_buftarg(mp->m_rtdev_targp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000492 out_free_ddev_targ:
Eric Sandeena1f69412018-04-06 10:09:42 -0700493 xfs_free_buftarg(mp->m_ddev_targp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000494 out_close_rtdev:
Markus Elfringd2a5e3c2014-12-01 08:24:20 +1100495 xfs_blkdev_put(rtdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000496 out_close_logdev:
Christoph Hellwig5b5abbe2021-11-29 11:21:55 +0100497 if (logdev && logdev != ddev)
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000498 xfs_blkdev_put(logdev);
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000499 return error;
500}
501
Christoph Hellwige34b5622008-05-20 15:10:36 +1000502/*
503 * Setup xfs_mount buffer target pointers based on superblock
504 */
505STATIC int
506xfs_setup_devices(
507 struct xfs_mount *mp)
508{
509 int error;
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000510
Eric Sandeena96c4152014-04-14 19:00:29 +1000511 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
Christoph Hellwige34b5622008-05-20 15:10:36 +1000512 if (error)
513 return error;
514
515 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
516 unsigned int log_sector_size = BBSIZE;
517
Dave Chinner38c26bf2021-08-18 18:46:37 -0700518 if (xfs_has_sector(mp))
Christoph Hellwige34b5622008-05-20 15:10:36 +1000519 log_sector_size = mp->m_sb.sb_logsectsize;
520 error = xfs_setsize_buftarg(mp->m_logdev_targp,
Christoph Hellwige34b5622008-05-20 15:10:36 +1000521 log_sector_size);
522 if (error)
523 return error;
524 }
525 if (mp->m_rtdev_targp) {
526 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
Christoph Hellwige34b5622008-05-20 15:10:36 +1000527 mp->m_sb.sb_sectsize);
528 if (error)
529 return error;
530 }
531
532 return 0;
533}
Christoph Hellwig19f354d2008-05-20 11:31:13 +1000534
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000535STATIC int
536xfs_init_mount_workqueues(
537 struct xfs_mount *mp)
538{
Brian Foster78c931b2014-11-28 13:59:58 +1100539 mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
Darrick J. Wong05a302a2021-01-22 16:48:42 -0800540 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
541 1, mp->m_super->s_id);
Brian Foster78c931b2014-11-28 13:59:58 +1100542 if (!mp->m_buf_workqueue)
543 goto out;
544
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000545 mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
Darrick J. Wong05a302a2021-01-22 16:48:42 -0800546 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
547 0, mp->m_super->s_id);
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000548 if (!mp->m_unwritten_workqueue)
Darrick J. Wong28408242019-04-15 13:13:21 -0700549 goto out_destroy_buf;
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000550
Dave Chinner58896082012-10-08 21:56:05 +1100551 mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
Darrick J. Wong05a302a2021-01-22 16:48:42 -0800552 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
553 0, mp->m_super->s_id);
Dave Chinner58896082012-10-08 21:56:05 +1100554 if (!mp->m_reclaim_workqueue)
Dave Chinner33c0dd72021-08-10 18:00:45 -0700555 goto out_destroy_unwritten;
Dave Chinner58896082012-10-08 21:56:05 +1100556
Dave Chinnerab23a772021-08-06 11:05:39 -0700557 mp->m_blockgc_wq = alloc_workqueue("xfs-blockgc/%s",
558 XFS_WQFLAGS(WQ_UNBOUND | WQ_FREEZABLE | WQ_MEM_RECLAIM),
Darrick J. Wong05a302a2021-01-22 16:48:42 -0800559 0, mp->m_super->s_id);
Dave Chinnerab23a772021-08-06 11:05:39 -0700560 if (!mp->m_blockgc_wq)
Christoph Hellwig1058d0f2019-06-28 19:27:25 -0700561 goto out_destroy_reclaim;
Brian Foster579b62f2012-11-06 09:50:47 -0500562
Dave Chinnerab23a772021-08-06 11:05:39 -0700563 mp->m_inodegc_wq = alloc_workqueue("xfs-inodegc/%s",
564 XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
565 1, mp->m_super->s_id);
566 if (!mp->m_inodegc_wq)
567 goto out_destroy_blockgc;
568
Darrick J. Wong05a302a2021-01-22 16:48:42 -0800569 mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s",
570 XFS_WQFLAGS(WQ_FREEZABLE), 0, mp->m_super->s_id);
Brian Foster696a5622017-03-28 14:51:44 -0700571 if (!mp->m_sync_workqueue)
Dave Chinnerab23a772021-08-06 11:05:39 -0700572 goto out_destroy_inodegc;
Brian Foster696a5622017-03-28 14:51:44 -0700573
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000574 return 0;
575
Dave Chinnerab23a772021-08-06 11:05:39 -0700576out_destroy_inodegc:
577 destroy_workqueue(mp->m_inodegc_wq);
578out_destroy_blockgc:
579 destroy_workqueue(mp->m_blockgc_wq);
Dave Chinner58896082012-10-08 21:56:05 +1100580out_destroy_reclaim:
581 destroy_workqueue(mp->m_reclaim_workqueue);
Dave Chinner4c2d5422012-04-23 17:54:32 +1000582out_destroy_unwritten:
583 destroy_workqueue(mp->m_unwritten_workqueue);
Brian Foster78c931b2014-11-28 13:59:58 +1100584out_destroy_buf:
585 destroy_workqueue(mp->m_buf_workqueue);
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000586out:
587 return -ENOMEM;
588}
589
590STATIC void
591xfs_destroy_mount_workqueues(
592 struct xfs_mount *mp)
593{
Brian Foster696a5622017-03-28 14:51:44 -0700594 destroy_workqueue(mp->m_sync_workqueue);
Dave Chinnerab23a772021-08-06 11:05:39 -0700595 destroy_workqueue(mp->m_blockgc_wq);
596 destroy_workqueue(mp->m_inodegc_wq);
Dave Chinner58896082012-10-08 21:56:05 +1100597 destroy_workqueue(mp->m_reclaim_workqueue);
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000598 destroy_workqueue(mp->m_unwritten_workqueue);
Brian Foster78c931b2014-11-28 13:59:58 +1100599 destroy_workqueue(mp->m_buf_workqueue);
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000600}
601
Darrick J. Wongf0f7a672020-04-12 13:11:10 -0700602static void
603xfs_flush_inodes_worker(
604 struct work_struct *work)
605{
606 struct xfs_mount *mp = container_of(work, struct xfs_mount,
607 m_flush_inodes_work);
608 struct super_block *sb = mp->m_super;
609
610 if (down_read_trylock(&sb->s_umount)) {
611 sync_inodes_sb(sb);
612 up_read(&sb->s_umount);
613 }
614}
615
Dave Chinner9aa05002012-10-08 21:56:04 +1100616/*
617 * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
618 * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
619 * for IO to complete so that we effectively throttle multiple callers to the
620 * rate at which IO is completing.
621 */
622void
623xfs_flush_inodes(
624 struct xfs_mount *mp)
625{
Darrick J. Wongf0f7a672020-04-12 13:11:10 -0700626 /*
627 * If flush_work() returns true then that means we waited for a flush
628 * which was already in progress. Don't bother running another scan.
629 */
630 if (flush_work(&mp->m_flush_inodes_work))
Darrick J. Wongc6425702020-03-27 08:49:44 -0700631 return;
632
Darrick J. Wongf0f7a672020-04-12 13:11:10 -0700633 queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
634 flush_work(&mp->m_flush_inodes_work);
Dave Chinner9aa05002012-10-08 21:56:04 +1100635}
636
David Chinnerbf904242008-10-30 17:36:14 +1100637/* Catch misguided souls that try to use this interface on XFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638STATIC struct inode *
Nathan Scotta50cd262006-03-14 14:06:18 +1100639xfs_fs_alloc_inode(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct super_block *sb)
641{
David Chinnerbf904242008-10-30 17:36:14 +1100642 BUG();
Lachlan McIlroy493dca62008-10-30 17:36:52 +1100643 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
David Chinnerbf904242008-10-30 17:36:14 +1100646/*
David Chinner99fa8cb2008-10-30 17:36:40 +1100647 * Now that the generic code is guaranteed not to be accessing
Dave Chinner8179c032016-05-18 13:52:42 +1000648 * the linux inode, we can inactivate and reclaim the inode.
David Chinnerbf904242008-10-30 17:36:14 +1100649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650STATIC void
Nathan Scotta50cd262006-03-14 14:06:18 +1100651xfs_fs_destroy_inode(
Christoph Hellwig848ce8f2009-09-29 13:48:56 +0000652 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Christoph Hellwig848ce8f2009-09-29 13:48:56 +0000654 struct xfs_inode *ip = XFS_I(inode);
655
Christoph Hellwigcca28fb2010-06-24 11:57:09 +1000656 trace_xfs_destroy_inode(ip);
David Chinner99fa8cb2008-10-30 17:36:40 +1100657
Christoph Hellwig65523212016-11-30 14:33:25 +1100658 ASSERT(!rwsem_is_locked(&inode->i_rwsem));
Dave Chinner8179c032016-05-18 13:52:42 +1000659 XFS_STATS_INC(ip->i_mount, vn_rele);
660 XFS_STATS_INC(ip->i_mount, vn_remove);
Darrick J. Wongc076ae72021-05-31 11:32:02 -0700661 xfs_inode_mark_reclaimable(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Christoph Hellwigc3b1b132018-03-06 17:04:00 -0800664static void
665xfs_fs_dirty_inode(
666 struct inode *inode,
Lukas Czernercbfecb92022-08-25 12:06:57 +0200667 int flags)
Christoph Hellwigc3b1b132018-03-06 17:04:00 -0800668{
669 struct xfs_inode *ip = XFS_I(inode);
670 struct xfs_mount *mp = ip->i_mount;
671 struct xfs_trans *tp;
672
673 if (!(inode->i_sb->s_flags & SB_LAZYTIME))
674 return;
Lukas Czernercbfecb92022-08-25 12:06:57 +0200675
676 /*
677 * Only do the timestamp update if the inode is dirty (I_DIRTY_SYNC)
678 * and has dirty timestamp (I_DIRTY_TIME). I_DIRTY_TIME can be passed
679 * in flags possibly together with I_DIRTY_SYNC.
680 */
681 if ((flags & ~I_DIRTY_TIME) != I_DIRTY_SYNC || !(flags & I_DIRTY_TIME))
Christoph Hellwigc3b1b132018-03-06 17:04:00 -0800682 return;
683
684 if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
685 return;
686 xfs_ilock(ip, XFS_ILOCK_EXCL);
687 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
688 xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
689 xfs_trans_commit(tp);
690}
691
David Chinner07c8f672008-10-30 16:11:59 +1100692/*
693 * Slab object creation initialisation for the XFS inode.
694 * This covers only the idempotent fields in the XFS inode;
695 * all other fields need to be initialised on allocation
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400696 * from the slab. This avoids the need to repeatedly initialise
David Chinner07c8f672008-10-30 16:11:59 +1100697 * fields in the xfs inode that left in the initialise state
698 * when freeing the inode.
699 */
David Chinnerbf904242008-10-30 17:36:14 +1100700STATIC void
701xfs_fs_inode_init_once(
David Chinner07c8f672008-10-30 16:11:59 +1100702 void *inode)
703{
704 struct xfs_inode *ip = inode;
705
706 memset(ip, 0, sizeof(struct xfs_inode));
David Chinnerbf904242008-10-30 17:36:14 +1100707
708 /* vfs inode */
709 inode_init_once(VFS_I(ip));
710
711 /* xfs inode */
David Chinner07c8f672008-10-30 16:11:59 +1100712 atomic_set(&ip->i_pincount, 0);
713 spin_lock_init(&ip->i_flags_lock);
David Chinner07c8f672008-10-30 16:11:59 +1100714
715 mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
716 "xfsino", ip->i_ino);
David Chinner07c8f672008-10-30 16:11:59 +1100717}
718
Dave Chinner5132ba82012-03-22 05:15:10 +0000719/*
720 * We do an unlocked check for XFS_IDONTCACHE here because we are already
721 * serialised against cache hits here via the inode->i_lock and igrab() in
722 * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
723 * racing with us, and it avoids needing to grab a spinlock here for every inode
724 * we drop the final reference on.
725 */
726STATIC int
727xfs_fs_drop_inode(
728 struct inode *inode)
729{
730 struct xfs_inode *ip = XFS_I(inode);
731
Darrick J. Wong17c12bc2016-10-03 09:11:29 -0700732 /*
733 * If this unlinked inode is in the middle of recovery, don't
734 * drop the inode just yet; log recovery will take care of
735 * that. See the comment for this inode flag.
736 */
737 if (ip->i_flags & XFS_IRECOVERY) {
Dave Chinnere1d06e52021-08-10 17:59:02 -0700738 ASSERT(xlog_recovery_needed(ip->i_mount->m_log));
Darrick J. Wong17c12bc2016-10-03 09:11:29 -0700739 return 0;
740 }
741
Ira Weinydae2f8e2020-04-30 07:41:37 -0700742 return generic_drop_inode(inode);
Dave Chinner5132ba82012-03-22 05:15:10 +0000743}
744
Ian Kenta943f372019-11-04 13:58:42 -0800745static void
746xfs_mount_free(
Christoph Hellwiga7381592008-08-13 16:04:05 +1000747 struct xfs_mount *mp)
748{
Christoph Hellwiga7381592008-08-13 16:04:05 +1000749 kfree(mp->m_rtname);
750 kfree(mp->m_logname);
Ian Kenta943f372019-11-04 13:58:42 -0800751 kmem_free(mp);
Christoph Hellwiga7381592008-08-13 16:04:05 +1000752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754STATIC int
Christoph Hellwig69961a22009-10-06 20:29:28 +0000755xfs_fs_sync_fs(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct super_block *sb,
757 int wait)
758{
Christoph Hellwig745f6912007-08-30 17:20:39 +1000759 struct xfs_mount *mp = XFS_M(sb);
Darrick J. Wong2d862932022-01-30 08:53:17 -0800760 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Chinnerab23a772021-08-06 11:05:39 -0700762 trace_xfs_fs_sync_fs(mp, __return_address);
763
Lachlan McIlroye893bff2007-10-12 11:13:35 +1000764 /*
Christoph Hellwig34625c62011-12-06 21:58:12 +0000765 * Doing anything during the async pass would be counterproductive.
Lachlan McIlroye893bff2007-10-12 11:13:35 +1000766 */
Christoph Hellwig34625c62011-12-06 21:58:12 +0000767 if (!wait)
Christoph Hellwig69961a22009-10-06 20:29:28 +0000768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Darrick J. Wong2d862932022-01-30 08:53:17 -0800770 error = xfs_log_force(mp, XFS_LOG_SYNC);
771 if (error)
772 return error;
773
Christoph Hellwig69961a22009-10-06 20:29:28 +0000774 if (laptop_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * The disk must be active because we're syncing.
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100777 * We schedule log work now (now that the disk is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * active) instead of later (when it might not be).
779 */
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100780 flush_delayed_work(&mp->m_log->l_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
Dave Chinnerab23a772021-08-06 11:05:39 -0700783 /*
784 * If we are called with page faults frozen out, it means we are about
785 * to freeze the transaction subsystem. Take the opportunity to shut
786 * down inodegc because once SB_FREEZE_FS is set it's too late to
787 * prevent inactivation races with freeze. The fs doesn't get called
788 * again by the freezing process until after SB_FREEZE_FS has been set,
Darrick J. Wong6f649092021-08-06 11:05:42 -0700789 * so it's now or never. Same logic applies to speculative allocation
790 * garbage collection.
Dave Chinnerab23a772021-08-06 11:05:39 -0700791 *
792 * We don't care if this is a normal syncfs call that does this or
793 * freeze that does this - we can run this multiple times without issue
794 * and we won't race with a restart because a restart can only occur
795 * when the state is either SB_FREEZE_FS or SB_FREEZE_COMPLETE.
796 */
Darrick J. Wong6f649092021-08-06 11:05:42 -0700797 if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
Dave Chinnerab23a772021-08-06 11:05:39 -0700798 xfs_inodegc_stop(mp);
Darrick J. Wong6f649092021-08-06 11:05:42 -0700799 xfs_blockgc_stop(mp);
800 }
Dave Chinnerab23a772021-08-06 11:05:39 -0700801
Christoph Hellwig69961a22009-10-06 20:29:28 +0000802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +1100806xfs_fs_statfs(
David Howells726c3342006-06-23 02:02:58 -0700807 struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct kstatfs *statp)
809{
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000810 struct xfs_mount *mp = XFS_M(dentry->d_sb);
811 xfs_sb_t *sbp = &mp->m_sb;
David Howells2b0143b2015-03-17 22:25:59 +0000812 struct xfs_inode *ip = XFS_I(d_inode(dentry));
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700813 uint64_t fakeinos, id;
814 uint64_t icount;
815 uint64_t ifree;
816 uint64_t fdblocks;
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000817 xfs_extlen_t lsize;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700818 int64_t ffree;
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000819
Dave Chinner5e672cd2022-06-16 07:44:32 -0700820 /*
821 * Expedite background inodegc but don't wait. We do not want to block
822 * here waiting hours for a billion extent file to be truncated.
823 */
824 xfs_inodegc_push(mp);
Darrick J. Wong01e8f372021-08-06 11:05:42 -0700825
Adam Borowskidddde682018-10-18 17:20:19 +1100826 statp->f_type = XFS_SUPER_MAGIC;
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000827 statp->f_namelen = MAXNAMELEN - 1;
828
829 id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
Al Viro6d1349c2020-09-18 16:45:50 -0400830 statp->f_fsid = u64_to_fsid(id);
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000831
Dave Chinner501ab322015-02-23 21:19:28 +1100832 icount = percpu_counter_sum(&mp->m_icount);
Dave Chinnere88b64e2015-02-23 21:19:53 +1100833 ifree = percpu_counter_sum(&mp->m_ifree);
Dave Chinner0d485ad2015-02-23 21:22:03 +1100834 fdblocks = percpu_counter_sum(&mp->m_fdblocks);
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000835
836 spin_lock(&mp->m_sb_lock);
837 statp->f_bsize = sbp->sb_blocksize;
838 lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
839 statp->f_blocks = sbp->sb_dblocks - lsize;
Dave Chinner0d485ad2015-02-23 21:22:03 +1100840 spin_unlock(&mp->m_sb_lock);
841
Zheng Bin237aac42020-05-12 11:48:35 -0700842 /* make sure statp->f_bfree does not underflow */
Darrick J. Wong85bcfa22022-03-16 13:38:43 -0700843 statp->f_bfree = max_t(int64_t, 0,
844 fdblocks - xfs_fdblocks_unavailable(mp));
Dave Chinner0d485ad2015-02-23 21:22:03 +1100845 statp->f_bavail = statp->f_bfree;
846
Darrick J. Wong43004b22018-12-12 08:46:24 -0800847 fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
Dave Chinner9bb54cb2018-06-07 07:54:02 -0700848 statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
Darrick J. Wongef325952019-06-05 11:19:34 -0700849 if (M_IGEO(mp)->maxicount)
Christoph Hellwiga19d9f82009-03-29 09:51:08 +0200850 statp->f_files = min_t(typeof(statp->f_files),
851 statp->f_files,
Darrick J. Wongef325952019-06-05 11:19:34 -0700852 M_IGEO(mp)->maxicount);
Stuart Brodsky2fe33662010-08-24 11:46:05 +1000853
Eric Sandeen01f98822015-02-06 09:53:02 +1100854 /* If sb_icount overshot maxicount, report actual allocation */
855 statp->f_files = max_t(typeof(statp->f_files),
856 statp->f_files,
857 sbp->sb_icount);
858
Stuart Brodsky2fe33662010-08-24 11:46:05 +1000859 /* make sure statp->f_ffree does not underflow */
Dave Chinnere88b64e2015-02-23 21:19:53 +1100860 ffree = statp->f_files - (icount - ifree);
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700861 statp->f_ffree = max_t(int64_t, ffree, 0);
Stuart Brodsky2fe33662010-08-24 11:46:05 +1000862
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000863
Christoph Hellwigdb073492021-03-29 11:11:44 -0700864 if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
Chandra Seetharaman83e782e2013-06-27 17:25:10 -0500865 ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
866 (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
Christoph Hellwig7d095252009-06-08 15:33:32 +0200867 xfs_qm_statvfs(ip, statp);
Richard Wareinga0158312018-01-08 10:41:33 -0800868
869 if (XFS_IS_REALTIME_MOUNT(mp) &&
Christoph Hellwigdb073492021-03-29 11:11:44 -0700870 (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
Darrick J. Wong22292762022-04-12 06:49:42 +1000871 s64 freertx;
872
Richard Wareinga0158312018-01-08 10:41:33 -0800873 statp->f_blocks = sbp->sb_rblocks;
Darrick J. Wong22292762022-04-12 06:49:42 +1000874 freertx = percpu_counter_sum_positive(&mp->m_frextents);
875 statp->f_bavail = statp->f_bfree = freertx * sbp->sb_rextsize;
Richard Wareinga0158312018-01-08 10:41:33 -0800876 }
877
Christoph Hellwig4ca488e2007-10-11 18:09:40 +1000878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Eric Sandeend5db0f92010-02-05 22:59:53 +0000881STATIC void
882xfs_save_resvblks(struct xfs_mount *mp)
883{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700884 uint64_t resblks = 0;
Eric Sandeend5db0f92010-02-05 22:59:53 +0000885
886 mp->m_resblks_save = mp->m_resblks;
887 xfs_reserve_blocks(mp, &resblks, NULL);
888}
889
890STATIC void
891xfs_restore_resvblks(struct xfs_mount *mp)
892{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700893 uint64_t resblks;
Eric Sandeend5db0f92010-02-05 22:59:53 +0000894
895 if (mp->m_resblks_save) {
896 resblks = mp->m_resblks_save;
897 mp->m_resblks_save = 0;
898 } else
899 resblks = xfs_default_resblks(mp);
900
901 xfs_reserve_blocks(mp, &resblks, NULL);
902}
903
Dave Chinnerc7eea6f2012-10-08 21:56:07 +1100904/*
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000905 * Second stage of a freeze. The data is already frozen so we only
Dave Chinner61e63ec2015-01-22 09:10:31 +1100906 * need to take care of the metadata. Once that's done sync the superblock
907 * to the log to dirty it in case of a crash while frozen. This ensures that we
908 * will recover the unlinked inode lists on the next mount.
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000909 */
Takashi Satoc4be0c12009-01-09 16:40:58 -0800910STATIC int
911xfs_fs_freeze(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct super_block *sb)
913{
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000914 struct xfs_mount *mp = XFS_M(sb);
Waiman Longc3f23752020-07-08 10:21:44 -0700915 unsigned int flags;
916 int ret;
Christoph Hellwig9909c4a2007-10-11 18:11:14 +1000917
Waiman Longc3f23752020-07-08 10:21:44 -0700918 /*
919 * The filesystem is now frozen far enough that memory reclaim
920 * cannot safely operate on the filesystem. Hence we need to
921 * set a GFP_NOFS context here to avoid recursion deadlocks.
922 */
923 flags = memalloc_nofs_save();
Eric Sandeend5db0f92010-02-05 22:59:53 +0000924 xfs_save_resvblks(mp);
Brian Foster5b0ad7c2021-01-22 16:48:24 -0800925 ret = xfs_log_quiesce(mp);
Waiman Longc3f23752020-07-08 10:21:44 -0700926 memalloc_nofs_restore(flags);
Dave Chinnerab23a772021-08-06 11:05:39 -0700927
928 /*
929 * For read-write filesystems, we need to restart the inodegc on error
930 * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
931 * going to be run to restart it now. We are at SB_FREEZE_FS level
932 * here, so we can restart safely without racing with a stop in
933 * xfs_fs_sync_fs().
934 */
Dave Chinner2e973b22021-08-18 18:46:52 -0700935 if (ret && !xfs_is_readonly(mp)) {
Darrick J. Wong6f649092021-08-06 11:05:42 -0700936 xfs_blockgc_start(mp);
Dave Chinnerab23a772021-08-06 11:05:39 -0700937 xfs_inodegc_start(mp);
Darrick J. Wong6f649092021-08-06 11:05:42 -0700938 }
Dave Chinnerab23a772021-08-06 11:05:39 -0700939
Waiman Longc3f23752020-07-08 10:21:44 -0700940 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943STATIC int
Eric Sandeend5db0f92010-02-05 22:59:53 +0000944xfs_fs_unfreeze(
945 struct super_block *sb)
946{
947 struct xfs_mount *mp = XFS_M(sb);
948
949 xfs_restore_resvblks(mp);
Dave Chinnerf661f1e2012-10-08 21:56:02 +1100950 xfs_log_work_queue(mp);
Dave Chinnerab23a772021-08-06 11:05:39 -0700951
952 /*
953 * Don't reactivate the inodegc worker on a readonly filesystem because
Darrick J. Wong6f649092021-08-06 11:05:42 -0700954 * inodes are sent directly to reclaim. Don't reactivate the blockgc
955 * worker because there are no speculative preallocations on a readonly
956 * filesystem.
Dave Chinnerab23a772021-08-06 11:05:39 -0700957 */
Dave Chinner2e973b22021-08-18 18:46:52 -0700958 if (!xfs_is_readonly(mp)) {
Darrick J. Wong6f649092021-08-06 11:05:42 -0700959 xfs_blockgc_start(mp);
Dave Chinnerab23a772021-08-06 11:05:39 -0700960 xfs_inodegc_start(mp);
Darrick J. Wong6f649092021-08-06 11:05:42 -0700961 }
Dave Chinnerab23a772021-08-06 11:05:39 -0700962
Eric Sandeend5db0f92010-02-05 22:59:53 +0000963 return 0;
964}
965
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000966/*
967 * This function fills in xfs_mount_t fields based on mount args.
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000968 * Note: the superblock _has_ now been read in.
969 */
970STATIC int
971xfs_finish_flags(
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000972 struct xfs_mount *mp)
973{
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200974 /* Fail a mount where the logbuf is smaller than the log stripe */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700975 if (xfs_has_logv2(mp)) {
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100976 if (mp->m_logbsize <= 0 &&
977 mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000978 mp->m_logbsize = mp->m_sb.sb_logsunit;
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100979 } else if (mp->m_logbsize > 0 &&
980 mp->m_logbsize < mp->m_sb.sb_logsunit) {
Dave Chinner4f107002011-03-07 10:00:35 +1100981 xfs_warn(mp,
982 "logbuf size must be greater than or equal to log stripe size");
Dave Chinner24513372014-06-25 14:58:08 +1000983 return -EINVAL;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000984 }
985 } else {
986 /* Fail a mount if the logbuf is larger than 32K */
Christoph Hellwig9d565ff2008-10-30 17:53:24 +1100987 if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
Dave Chinner4f107002011-03-07 10:00:35 +1100988 xfs_warn(mp,
989 "logbuf size for version 1 logs must be 16K or 32K");
Dave Chinner24513372014-06-25 14:58:08 +1000990 return -EINVAL;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +1000991 }
992 }
993
994 /*
Dave Chinnerd3eaace2013-06-05 12:09:09 +1000995 * V5 filesystems always use attr2 format for attributes.
996 */
Dave Chinner0560f312021-08-18 18:46:52 -0700997 if (xfs_has_crc(mp) && xfs_has_noattr2(mp)) {
Eric Sandeen2e74af02016-03-02 09:55:38 +1100998 xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
999 "attr2 is always enabled for V5 filesystems.");
Dave Chinner24513372014-06-25 14:58:08 +10001000 return -EINVAL;
Dave Chinnerd3eaace2013-06-05 12:09:09 +10001001 }
1002
1003 /*
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001004 * prohibit r/w mounts of read-only filesystems
1005 */
Dave Chinner2e973b22021-08-18 18:46:52 -07001006 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001007 xfs_warn(mp,
1008 "cannot mount a read-only filesystem as read-write");
Dave Chinner24513372014-06-25 14:58:08 +10001009 return -EROFS;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001010 }
1011
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001012 if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
1013 (mp->m_qflags & XFS_PQUOTA_ACCT) &&
Dave Chinner38c26bf2021-08-18 18:46:37 -07001014 !xfs_has_pquotino(mp)) {
Chandra Seetharamand892d582013-07-19 17:36:02 -05001015 xfs_warn(mp,
1016 "Super block does not support project and group quota together");
Dave Chinner24513372014-06-25 14:58:08 +10001017 return -EINVAL;
Chandra Seetharamand892d582013-07-19 17:36:02 -05001018 }
1019
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001020 return 0;
1021}
1022
Dave Chinner5681ca42015-02-23 21:22:31 +11001023static int
1024xfs_init_percpu_counters(
1025 struct xfs_mount *mp)
1026{
1027 int error;
1028
1029 error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
1030 if (error)
Joe Perches5e9383f2015-03-25 15:00:24 +11001031 return -ENOMEM;
Dave Chinner5681ca42015-02-23 21:22:31 +11001032
1033 error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
1034 if (error)
1035 goto free_icount;
1036
1037 error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
1038 if (error)
1039 goto free_ifree;
1040
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001041 error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
1042 if (error)
1043 goto free_fdblocks;
1044
Darrick J. Wong22292762022-04-12 06:49:42 +10001045 error = percpu_counter_init(&mp->m_frextents, 0, GFP_KERNEL);
1046 if (error)
1047 goto free_delalloc;
1048
Dave Chinner5681ca42015-02-23 21:22:31 +11001049 return 0;
1050
Darrick J. Wong22292762022-04-12 06:49:42 +10001051free_delalloc:
1052 percpu_counter_destroy(&mp->m_delalloc_blks);
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001053free_fdblocks:
1054 percpu_counter_destroy(&mp->m_fdblocks);
Dave Chinner5681ca42015-02-23 21:22:31 +11001055free_ifree:
1056 percpu_counter_destroy(&mp->m_ifree);
1057free_icount:
1058 percpu_counter_destroy(&mp->m_icount);
1059 return -ENOMEM;
1060}
1061
1062void
1063xfs_reinit_percpu_counters(
1064 struct xfs_mount *mp)
1065{
1066 percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
1067 percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
1068 percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
Darrick J. Wong22292762022-04-12 06:49:42 +10001069 percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
Dave Chinner5681ca42015-02-23 21:22:31 +11001070}
1071
1072static void
1073xfs_destroy_percpu_counters(
1074 struct xfs_mount *mp)
1075{
1076 percpu_counter_destroy(&mp->m_icount);
1077 percpu_counter_destroy(&mp->m_ifree);
1078 percpu_counter_destroy(&mp->m_fdblocks);
Dave Chinner75c8c50f2021-08-18 18:46:53 -07001079 ASSERT(xfs_is_shutdown(mp) ||
Darrick J. Wong9fe82b82019-04-25 18:26:22 -07001080 percpu_counter_sum(&mp->m_delalloc_blks) == 0);
1081 percpu_counter_destroy(&mp->m_delalloc_blks);
Darrick J. Wong22292762022-04-12 06:49:42 +10001082 percpu_counter_destroy(&mp->m_frextents);
Dave Chinner5681ca42015-02-23 21:22:31 +11001083}
1084
Dave Chinnerab23a772021-08-06 11:05:39 -07001085static int
1086xfs_inodegc_init_percpu(
1087 struct xfs_mount *mp)
1088{
1089 struct xfs_inodegc *gc;
1090 int cpu;
1091
1092 mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
1093 if (!mp->m_inodegc)
1094 return -ENOMEM;
1095
1096 for_each_possible_cpu(cpu) {
1097 gc = per_cpu_ptr(mp->m_inodegc, cpu);
1098 init_llist_head(&gc->list);
1099 gc->items = 0;
Dave Chinner7cf2b0f2022-06-16 07:44:31 -07001100 INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
Dave Chinnerab23a772021-08-06 11:05:39 -07001101 }
1102 return 0;
1103}
1104
1105static void
1106xfs_inodegc_free_percpu(
1107 struct xfs_mount *mp)
1108{
1109 if (!mp->m_inodegc)
1110 return;
1111 free_percpu(mp->m_inodegc);
1112}
1113
Ian Kent2f8d66b2019-11-04 13:58:47 -08001114static void
1115xfs_fs_put_super(
1116 struct super_block *sb)
1117{
1118 struct xfs_mount *mp = XFS_M(sb);
1119
1120 /* if ->fill_super failed, we have no mount to tear down */
1121 if (!sb->s_fs_info)
1122 return;
1123
Lukas Herbolt64c80df2022-11-16 19:20:21 -08001124 xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
Ian Kent2f8d66b2019-11-04 13:58:47 -08001125 xfs_filestream_unmount(mp);
1126 xfs_unmountfs(mp);
1127
1128 xfs_freesb(mp);
1129 free_percpu(mp->m_stats.xs_stats);
Dave Chinner0ed17f02021-08-06 11:05:38 -07001130 xfs_mount_list_del(mp);
Dave Chinnerab23a772021-08-06 11:05:39 -07001131 xfs_inodegc_free_percpu(mp);
Ian Kent2f8d66b2019-11-04 13:58:47 -08001132 xfs_destroy_percpu_counters(mp);
1133 xfs_destroy_mount_workqueues(mp);
1134 xfs_close_devices(mp);
1135
1136 sb->s_fs_info = NULL;
1137 xfs_mount_free(mp);
1138}
1139
1140static long
1141xfs_fs_nr_cached_objects(
1142 struct super_block *sb,
1143 struct shrink_control *sc)
1144{
1145 /* Paranoia: catch incorrect calls during mount setup or teardown */
1146 if (WARN_ON_ONCE(!sb->s_fs_info))
1147 return 0;
1148 return xfs_reclaim_inodes_count(XFS_M(sb));
1149}
1150
1151static long
1152xfs_fs_free_cached_objects(
1153 struct super_block *sb,
1154 struct shrink_control *sc)
1155{
1156 return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
1157}
1158
1159static const struct super_operations xfs_super_operations = {
1160 .alloc_inode = xfs_fs_alloc_inode,
1161 .destroy_inode = xfs_fs_destroy_inode,
1162 .dirty_inode = xfs_fs_dirty_inode,
1163 .drop_inode = xfs_fs_drop_inode,
1164 .put_super = xfs_fs_put_super,
1165 .sync_fs = xfs_fs_sync_fs,
1166 .freeze_fs = xfs_fs_freeze,
1167 .unfreeze_fs = xfs_fs_unfreeze,
1168 .statfs = xfs_fs_statfs,
1169 .show_options = xfs_fs_show_options,
1170 .nr_cached_objects = xfs_fs_nr_cached_objects,
1171 .free_cached_objects = xfs_fs_free_cached_objects,
1172};
1173
Ian Kent73e5fff2019-11-04 13:58:46 -08001174static int
Ian Kent8757c382019-11-04 13:58:48 -08001175suffix_kstrtoint(
1176 const char *s,
1177 unsigned int base,
1178 int *res)
1179{
1180 int last, shift_left_factor = 0, _res;
1181 char *value;
1182 int ret = 0;
1183
1184 value = kstrdup(s, GFP_KERNEL);
1185 if (!value)
1186 return -ENOMEM;
1187
1188 last = strlen(value) - 1;
1189 if (value[last] == 'K' || value[last] == 'k') {
1190 shift_left_factor = 10;
1191 value[last] = '\0';
1192 }
1193 if (value[last] == 'M' || value[last] == 'm') {
1194 shift_left_factor = 20;
1195 value[last] = '\0';
1196 }
1197 if (value[last] == 'G' || value[last] == 'g') {
1198 shift_left_factor = 30;
1199 value[last] = '\0';
1200 }
1201
1202 if (kstrtoint(value, base, &_res))
1203 ret = -EINVAL;
1204 kfree(value);
1205 *res = _res << shift_left_factor;
1206 return ret;
1207}
1208
Pavel Reichl92cf7d32021-03-22 09:52:02 -07001209static inline void
1210xfs_fs_warn_deprecated(
1211 struct fs_context *fc,
1212 struct fs_parameter *param,
1213 uint64_t flag,
1214 bool value)
1215{
1216 /* Don't print the warning if reconfiguring and current mount point
1217 * already had the flag set
1218 */
1219 if ((fc->purpose & FS_CONTEXT_FOR_RECONFIGURE) &&
Dave Chinner0560f312021-08-18 18:46:52 -07001220 !!(XFS_M(fc->root->d_sb)->m_features & flag) == value)
Pavel Reichl92cf7d32021-03-22 09:52:02 -07001221 return;
1222 xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
1223}
1224
Ian Kent8757c382019-11-04 13:58:48 -08001225/*
1226 * Set mount state from a mount option.
1227 *
1228 * NOTE: mp->m_super is NULL here!
1229 */
1230static int
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001231xfs_fs_parse_param(
Ian Kent8757c382019-11-04 13:58:48 -08001232 struct fs_context *fc,
1233 struct fs_parameter *param)
1234{
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001235 struct xfs_mount *parsing_mp = fc->s_fs_info;
Ian Kent8757c382019-11-04 13:58:48 -08001236 struct fs_parse_result result;
1237 int size = 0;
1238 int opt;
1239
Al Virod7167b12019-09-07 07:23:15 -04001240 opt = fs_parse(fc, xfs_fs_parameters, param, &result);
Ian Kent8757c382019-11-04 13:58:48 -08001241 if (opt < 0)
1242 return opt;
1243
1244 switch (opt) {
1245 case Opt_logbufs:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001246 parsing_mp->m_logbufs = result.uint_32;
Ian Kent8757c382019-11-04 13:58:48 -08001247 return 0;
1248 case Opt_logbsize:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001249 if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
Ian Kent8757c382019-11-04 13:58:48 -08001250 return -EINVAL;
1251 return 0;
1252 case Opt_logdev:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001253 kfree(parsing_mp->m_logname);
1254 parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
1255 if (!parsing_mp->m_logname)
Ian Kent8757c382019-11-04 13:58:48 -08001256 return -ENOMEM;
1257 return 0;
1258 case Opt_rtdev:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001259 kfree(parsing_mp->m_rtname);
1260 parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
1261 if (!parsing_mp->m_rtname)
Ian Kent8757c382019-11-04 13:58:48 -08001262 return -ENOMEM;
1263 return 0;
1264 case Opt_allocsize:
1265 if (suffix_kstrtoint(param->string, 10, &size))
1266 return -EINVAL;
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001267 parsing_mp->m_allocsize_log = ffs(size) - 1;
Dave Chinner0560f312021-08-18 18:46:52 -07001268 parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
Ian Kent8757c382019-11-04 13:58:48 -08001269 return 0;
1270 case Opt_grpid:
1271 case Opt_bsdgroups:
Dave Chinner0560f312021-08-18 18:46:52 -07001272 parsing_mp->m_features |= XFS_FEAT_GRPID;
Ian Kent8757c382019-11-04 13:58:48 -08001273 return 0;
1274 case Opt_nogrpid:
1275 case Opt_sysvgroups:
Dave Chinner0560f312021-08-18 18:46:52 -07001276 parsing_mp->m_features &= ~XFS_FEAT_GRPID;
Ian Kent8757c382019-11-04 13:58:48 -08001277 return 0;
1278 case Opt_wsync:
Dave Chinner0560f312021-08-18 18:46:52 -07001279 parsing_mp->m_features |= XFS_FEAT_WSYNC;
Ian Kent8757c382019-11-04 13:58:48 -08001280 return 0;
1281 case Opt_norecovery:
Dave Chinner0560f312021-08-18 18:46:52 -07001282 parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
Ian Kent8757c382019-11-04 13:58:48 -08001283 return 0;
1284 case Opt_noalign:
Dave Chinner0560f312021-08-18 18:46:52 -07001285 parsing_mp->m_features |= XFS_FEAT_NOALIGN;
Ian Kent8757c382019-11-04 13:58:48 -08001286 return 0;
1287 case Opt_swalloc:
Dave Chinner0560f312021-08-18 18:46:52 -07001288 parsing_mp->m_features |= XFS_FEAT_SWALLOC;
Ian Kent8757c382019-11-04 13:58:48 -08001289 return 0;
1290 case Opt_sunit:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001291 parsing_mp->m_dalign = result.uint_32;
Ian Kent8757c382019-11-04 13:58:48 -08001292 return 0;
1293 case Opt_swidth:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001294 parsing_mp->m_swidth = result.uint_32;
Ian Kent8757c382019-11-04 13:58:48 -08001295 return 0;
1296 case Opt_inode32:
Dave Chinner0560f312021-08-18 18:46:52 -07001297 parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
Ian Kent8757c382019-11-04 13:58:48 -08001298 return 0;
1299 case Opt_inode64:
Dave Chinner0560f312021-08-18 18:46:52 -07001300 parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
Ian Kent8757c382019-11-04 13:58:48 -08001301 return 0;
1302 case Opt_nouuid:
Dave Chinner0560f312021-08-18 18:46:52 -07001303 parsing_mp->m_features |= XFS_FEAT_NOUUID;
Ian Kent8757c382019-11-04 13:58:48 -08001304 return 0;
Ian Kent8757c382019-11-04 13:58:48 -08001305 case Opt_largeio:
Dave Chinner0560f312021-08-18 18:46:52 -07001306 parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
Ian Kent8757c382019-11-04 13:58:48 -08001307 return 0;
1308 case Opt_nolargeio:
Dave Chinner0560f312021-08-18 18:46:52 -07001309 parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
Ian Kent8757c382019-11-04 13:58:48 -08001310 return 0;
Ian Kent8757c382019-11-04 13:58:48 -08001311 case Opt_filestreams:
Dave Chinner0560f312021-08-18 18:46:52 -07001312 parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
Ian Kent8757c382019-11-04 13:58:48 -08001313 return 0;
1314 case Opt_noquota:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001315 parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
1316 parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
Ian Kent8757c382019-11-04 13:58:48 -08001317 return 0;
1318 case Opt_quota:
1319 case Opt_uquota:
1320 case Opt_usrquota:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001321 parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
Ian Kent8757c382019-11-04 13:58:48 -08001322 return 0;
1323 case Opt_qnoenforce:
1324 case Opt_uqnoenforce:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001325 parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001326 parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
Ian Kent8757c382019-11-04 13:58:48 -08001327 return 0;
1328 case Opt_pquota:
1329 case Opt_prjquota:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001330 parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
Ian Kent8757c382019-11-04 13:58:48 -08001331 return 0;
1332 case Opt_pqnoenforce:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001333 parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001334 parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
Ian Kent8757c382019-11-04 13:58:48 -08001335 return 0;
1336 case Opt_gquota:
1337 case Opt_grpquota:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001338 parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
Ian Kent8757c382019-11-04 13:58:48 -08001339 return 0;
1340 case Opt_gqnoenforce:
Christoph Hellwig149e53a2021-08-06 11:05:37 -07001341 parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001342 parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
Ian Kent8757c382019-11-04 13:58:48 -08001343 return 0;
1344 case Opt_discard:
Dave Chinner0560f312021-08-18 18:46:52 -07001345 parsing_mp->m_features |= XFS_FEAT_DISCARD;
Ian Kent8757c382019-11-04 13:58:48 -08001346 return 0;
1347 case Opt_nodiscard:
Dave Chinner0560f312021-08-18 18:46:52 -07001348 parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
Ian Kent8757c382019-11-04 13:58:48 -08001349 return 0;
1350#ifdef CONFIG_FS_DAX
1351 case Opt_dax:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001352 xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
Ira Weiny8d6c3442020-05-04 09:02:42 -07001353 return 0;
1354 case Opt_dax_enum:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001355 xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
Ian Kent8757c382019-11-04 13:58:48 -08001356 return 0;
1357#endif
Pavel Reichlc23c3932020-09-25 11:10:29 -07001358 /* Following mount options will be removed in September 2025 */
1359 case Opt_ikeep:
Dave Chinner0560f312021-08-18 18:46:52 -07001360 xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, true);
1361 parsing_mp->m_features |= XFS_FEAT_IKEEP;
Pavel Reichlc23c3932020-09-25 11:10:29 -07001362 return 0;
1363 case Opt_noikeep:
Dave Chinner0560f312021-08-18 18:46:52 -07001364 xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, false);
1365 parsing_mp->m_features &= ~XFS_FEAT_IKEEP;
Pavel Reichlc23c3932020-09-25 11:10:29 -07001366 return 0;
1367 case Opt_attr2:
Dave Chinner0560f312021-08-18 18:46:52 -07001368 xfs_fs_warn_deprecated(fc, param, XFS_FEAT_ATTR2, true);
1369 parsing_mp->m_features |= XFS_FEAT_ATTR2;
Pavel Reichlc23c3932020-09-25 11:10:29 -07001370 return 0;
1371 case Opt_noattr2:
Dave Chinner0560f312021-08-18 18:46:52 -07001372 xfs_fs_warn_deprecated(fc, param, XFS_FEAT_NOATTR2, true);
1373 parsing_mp->m_features |= XFS_FEAT_NOATTR2;
Pavel Reichlc23c3932020-09-25 11:10:29 -07001374 return 0;
Ian Kent8757c382019-11-04 13:58:48 -08001375 default:
Pavel Reichl0f98b4e2021-03-22 09:52:01 -07001376 xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
Ian Kent8757c382019-11-04 13:58:48 -08001377 return -EINVAL;
1378 }
1379
1380 return 0;
1381}
1382
1383static int
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001384xfs_fs_validate_params(
Ian Kent8757c382019-11-04 13:58:48 -08001385 struct xfs_mount *mp)
1386{
Dave Chinner0560f312021-08-18 18:46:52 -07001387 /* No recovery flag requires a read-only mount */
Dave Chinner2e973b22021-08-18 18:46:52 -07001388 if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
Ian Kent8757c382019-11-04 13:58:48 -08001389 xfs_warn(mp, "no-recovery mounts must be read-only.");
1390 return -EINVAL;
1391 }
1392
Dave Chinner0560f312021-08-18 18:46:52 -07001393 /*
1394 * We have not read the superblock at this point, so only the attr2
1395 * mount option can set the attr2 feature by this stage.
1396 */
1397 if (xfs_has_attr2(mp) && xfs_has_noattr2(mp)) {
Dave Chinnere23b55d2021-08-18 18:46:25 -07001398 xfs_warn(mp, "attr2 and noattr2 cannot both be specified.");
1399 return -EINVAL;
1400 }
1401
1402
Dave Chinner0560f312021-08-18 18:46:52 -07001403 if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
Ian Kent8757c382019-11-04 13:58:48 -08001404 xfs_warn(mp,
1405 "sunit and swidth options incompatible with the noalign option");
1406 return -EINVAL;
1407 }
1408
1409 if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
1410 xfs_warn(mp, "quota support not available in this kernel.");
1411 return -EINVAL;
1412 }
1413
1414 if ((mp->m_dalign && !mp->m_swidth) ||
1415 (!mp->m_dalign && mp->m_swidth)) {
1416 xfs_warn(mp, "sunit and swidth must be specified together");
1417 return -EINVAL;
1418 }
1419
1420 if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
1421 xfs_warn(mp,
1422 "stripe width (%d) must be a multiple of the stripe unit (%d)",
1423 mp->m_swidth, mp->m_dalign);
1424 return -EINVAL;
1425 }
1426
1427 if (mp->m_logbufs != -1 &&
1428 mp->m_logbufs != 0 &&
1429 (mp->m_logbufs < XLOG_MIN_ICLOGS ||
1430 mp->m_logbufs > XLOG_MAX_ICLOGS)) {
1431 xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
1432 mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
1433 return -EINVAL;
1434 }
1435
1436 if (mp->m_logbsize != -1 &&
1437 mp->m_logbsize != 0 &&
1438 (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
1439 mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
1440 !is_power_of_2(mp->m_logbsize))) {
1441 xfs_warn(mp,
1442 "invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
1443 mp->m_logbsize);
1444 return -EINVAL;
1445 }
1446
Dave Chinner0560f312021-08-18 18:46:52 -07001447 if (xfs_has_allocsize(mp) &&
Ian Kent8757c382019-11-04 13:58:48 -08001448 (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
1449 mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
1450 xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
1451 mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
1452 return -EINVAL;
1453 }
1454
1455 return 0;
1456}
1457
1458static int
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001459xfs_fs_fill_super(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct super_block *sb,
Ian Kent73e5fff2019-11-04 13:58:46 -08001461 struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Ian Kent73e5fff2019-11-04 13:58:46 -08001463 struct xfs_mount *mp = sb->s_fs_info;
Christoph Hellwigf3dcc132008-03-27 18:00:54 +11001464 struct inode *root;
Colin Ian King0279c712019-11-06 08:07:46 -08001465 int flags = 0, error;
Christoph Hellwigbdd907b2008-05-20 15:10:44 +10001466
Ian Kent7c89fcb2019-11-04 13:58:46 -08001467 mp->m_super = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001469 error = xfs_fs_validate_params(mp);
Christoph Hellwig745f6912007-08-30 17:20:39 +10001470 if (error)
Ian Kente1d3d212019-11-04 13:58:40 -08001471 goto out_free_names;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 sb_min_blocksize(sb, BBSIZE);
Lachlan McIlroy0ec58512008-06-23 13:23:01 +10001474 sb->s_xattr = xfs_xattr_handlers;
Nathan Scotta50cd262006-03-14 14:06:18 +11001475 sb->s_export_op = &xfs_export_operations;
Christoph Hellwigfcafb712009-02-09 08:47:34 +01001476#ifdef CONFIG_XFS_QUOTA
Nathan Scotta50cd262006-03-14 14:06:18 +11001477 sb->s_qcop = &xfs_quotactl_operations;
Jan Kara17ef4fd2014-09-30 22:35:33 +02001478 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Christoph Hellwigfcafb712009-02-09 08:47:34 +01001479#endif
Nathan Scotta50cd262006-03-14 14:06:18 +11001480 sb->s_op = &xfs_super_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Dave Chinnerdae5cd82018-05-10 21:50:23 -07001482 /*
1483 * Delay mount work if the debug hook is set. This is debug
1484 * instrumention to coordinate simulation of xfs mount failures with
1485 * VFS superblock operations
1486 */
1487 if (xfs_globals.mount_delay) {
1488 xfs_notice(mp, "Delaying mount for %d seconds.",
1489 xfs_globals.mount_delay);
1490 msleep(xfs_globals.mount_delay * 1000);
1491 }
1492
Ian Kent73e5fff2019-11-04 13:58:46 -08001493 if (fc->sb_flags & SB_SILENT)
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001494 flags |= XFS_MFSI_QUIET;
1495
Christoph Hellwig9d565ff2008-10-30 17:53:24 +11001496 error = xfs_open_devices(mp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +10001497 if (error)
Ian Kente1d3d212019-11-04 13:58:40 -08001498 goto out_free_names;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001499
Dave Chinner24513372014-06-25 14:58:08 +10001500 error = xfs_init_mount_workqueues(mp);
Christoph Hellwig61ba35d2010-09-30 02:25:54 +00001501 if (error)
1502 goto out_close_devices;
Christoph Hellwigc962fb72008-05-20 15:10:52 +10001503
Dave Chinner5681ca42015-02-23 21:22:31 +11001504 error = xfs_init_percpu_counters(mp);
Christoph Hellwigaa6bf012012-02-29 09:53:48 +00001505 if (error)
1506 goto out_destroy_workqueues;
1507
Dave Chinnerab23a772021-08-06 11:05:39 -07001508 error = xfs_inodegc_init_percpu(mp);
1509 if (error)
1510 goto out_destroy_counters;
1511
Dave Chinner0ed17f02021-08-06 11:05:38 -07001512 /*
1513 * All percpu data structures requiring cleanup when a cpu goes offline
1514 * must be allocated before adding this @mp to the cpu-dead handler's
1515 * mount list.
1516 */
1517 xfs_mount_list_add(mp);
1518
Bill O'Donnell225e4632015-10-12 18:21:19 +11001519 /* Allocate stats memory before we do operations that might use it */
1520 mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1521 if (!mp->m_stats.xs_stats) {
Dan Carpenterf9d460b2015-10-19 08:42:47 +11001522 error = -ENOMEM;
Dave Chinnerab23a772021-08-06 11:05:39 -07001523 goto out_destroy_inodegc;
Bill O'Donnell225e4632015-10-12 18:21:19 +11001524 }
1525
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001526 error = xfs_readsb(mp, flags);
1527 if (error)
Bill O'Donnell225e4632015-10-12 18:21:19 +11001528 goto out_free_stats;
Christoph Hellwig9d565ff2008-10-30 17:53:24 +11001529
1530 error = xfs_finish_flags(mp);
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001531 if (error)
Christoph Hellwigeffa2ed2008-05-20 15:11:05 +10001532 goto out_free_sb;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001533
Christoph Hellwige34b5622008-05-20 15:10:36 +10001534 error = xfs_setup_devices(mp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +10001535 if (error)
Christoph Hellwigeffa2ed2008-05-20 15:11:05 +10001536 goto out_free_sb;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001537
Darrick J. Wongb96cb832020-09-10 10:57:17 -07001538 /* V4 support is undergoing deprecation. */
Dave Chinner38c26bf2021-08-18 18:46:37 -07001539 if (!xfs_has_crc(mp)) {
Darrick J. Wongb96cb832020-09-10 10:57:17 -07001540#ifdef CONFIG_XFS_SUPPORT_V4
1541 xfs_warn_once(mp,
1542 "Deprecated V4 format (crc=0) will not be supported after September 2030.");
1543#else
1544 xfs_warn(mp,
1545 "Deprecated V4 format (crc=0) not supported by kernel.");
1546 error = -EINVAL;
1547 goto out_free_sb;
1548#endif
1549 }
1550
Darrick J. Wong7ba83852023-04-11 19:05:19 -07001551 /* ASCII case insensitivity is undergoing deprecation. */
1552 if (xfs_has_asciici(mp)) {
1553#ifdef CONFIG_XFS_SUPPORT_ASCII_CI
1554 xfs_warn_once(mp,
1555 "Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
1556#else
1557 xfs_warn(mp,
1558 "Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
1559 error = -EINVAL;
1560 goto out_free_sb;
1561#endif
1562 }
1563
Darrick J. Wong80c720b2020-11-24 11:45:55 -08001564 /* Filesystem claims it needs repair, so refuse the mount. */
Dave Chinnerebd90272021-08-18 18:46:55 -07001565 if (xfs_has_needsrepair(mp)) {
Darrick J. Wong80c720b2020-11-24 11:45:55 -08001566 xfs_warn(mp, "Filesystem needs repair. Please run xfs_repair.");
1567 error = -EFSCORRUPTED;
1568 goto out_free_sb;
1569 }
1570
Darrick J. Wong932befe2020-01-02 13:20:13 -08001571 /*
Darrick J. Wong3945ae02020-11-24 11:45:54 -08001572 * Don't touch the filesystem if a user tool thinks it owns the primary
1573 * superblock. mkfs doesn't clear the flag from secondary supers, so
1574 * we don't check them at all.
1575 */
1576 if (mp->m_sb.sb_inprogress) {
1577 xfs_warn(mp, "Offline file system operation in progress!");
1578 error = -EFSCORRUPTED;
1579 goto out_free_sb;
1580 }
1581
1582 /*
1583 * Until this is fixed only page-sized or smaller data blocks work.
1584 */
1585 if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
1586 xfs_warn(mp,
1587 "File system with blocksize %d bytes. "
1588 "Only pagesize (%ld) or less will currently work.",
1589 mp->m_sb.sb_blocksize, PAGE_SIZE);
1590 error = -ENOSYS;
1591 goto out_free_sb;
1592 }
1593
1594 /* Ensure this filesystem fits in the page cache limits */
1595 if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
1596 xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
1597 xfs_warn(mp,
1598 "file system too large to be mounted on this system.");
1599 error = -EFBIG;
1600 goto out_free_sb;
1601 }
1602
1603 /*
Darrick J. Wong932befe2020-01-02 13:20:13 -08001604 * XFS block mappings use 54 bits to store the logical block offset.
1605 * This should suffice to handle the maximum file size that the VFS
1606 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1607 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1608 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1609 * to check this assertion.
1610 *
1611 * Avoid integer overflow by comparing the maximum bmbt offset to the
1612 * maximum pagecache offset in units of fs blocks.
1613 */
Darrick J. Wong33005fd2020-12-04 13:28:35 -08001614 if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
Darrick J. Wong932befe2020-01-02 13:20:13 -08001615 xfs_warn(mp,
1616"MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1617 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1618 XFS_MAX_FILEOFF);
1619 error = -EINVAL;
1620 goto out_free_sb;
1621 }
1622
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001623 error = xfs_filestream_mount(mp);
1624 if (error)
Christoph Hellwigeffa2ed2008-05-20 15:11:05 +10001625 goto out_free_sb;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001626
Dave Chinner704b2902011-03-26 09:14:57 +11001627 /*
1628 * we must configure the block size in the superblock before we run the
1629 * full mount process as the mount process can lookup and cache inodes.
Dave Chinner704b2902011-03-26 09:14:57 +11001630 */
Adam Borowskidddde682018-10-18 17:20:19 +11001631 sb->s_magic = XFS_SUPER_MAGIC;
Christoph Hellwig4ca488e2007-10-11 18:09:40 +10001632 sb->s_blocksize = mp->m_sb.sb_blocksize;
1633 sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
Darrick J. Wong932befe2020-01-02 13:20:13 -08001634 sb->s_maxbytes = MAX_LFS_FILESIZE;
Al Viro8de52772012-02-06 12:45:27 -05001635 sb->s_max_links = XFS_MAXLINK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 sb->s_time_gran = 1;
Dave Chinner38c26bf2021-08-18 18:46:37 -07001637 if (xfs_has_bigtime(mp)) {
Darrick J. Wongf93e54362020-08-17 09:59:07 -07001638 sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1639 sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1640 } else {
1641 sb->s_time_min = XFS_LEGACY_TIME_MIN;
1642 sb->s_time_max = XFS_LEGACY_TIME_MAX;
1643 }
Darrick J. Wong06dbf822020-08-24 11:58:01 -07001644 trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
Christoph Hellwigadfb5fb2019-06-28 19:30:22 -07001645 sb->s_iflags |= SB_I_CGROUPWB;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 set_posix_acl_flag(sb);
1648
Dave Chinnerdc037ad2013-06-27 16:04:59 +10001649 /* version 5 superblocks support inode version counters. */
Dave Chinnerd6837c12021-08-18 18:46:56 -07001650 if (xfs_has_crc(mp))
Matthew Garrett357fdad2017-10-18 13:56:26 -07001651 sb->s_flags |= SB_I_VERSION;
Dave Chinnerdc037ad2013-06-27 16:04:59 +10001652
Dave Chinner0560f312021-08-18 18:46:52 -07001653 if (xfs_has_dax_always(mp)) {
Christoph Hellwig679a9942021-11-29 11:21:41 +01001654 error = xfs_setup_dax_always(mp);
1655 if (error)
Darrick J. Wongb6e03c12018-01-31 14:21:56 -08001656 goto out_filestream_unmount;
Dave Chinnercbe4dab2015-06-04 09:19:18 +10001657 }
1658
Christoph Hellwig70200572022-04-15 06:52:55 +02001659 if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
1660 xfs_warn(mp,
1661 "mounting with \"discard\" option, but the device does not support discard");
1662 mp->m_features &= ~XFS_FEAT_DISCARD;
Kenjiro Nakayama1e6fa682017-09-18 12:03:56 -07001663 }
1664
Dave Chinner38c26bf2021-08-18 18:46:37 -07001665 if (xfs_has_reflink(mp)) {
Christoph Hellwig66ae56a2019-02-18 09:38:49 -08001666 if (mp->m_sb.sb_rblocks) {
1667 xfs_alert(mp,
Darrick J. Wongc14632d2018-01-31 16:38:18 -08001668 "reflink not compatible with realtime device!");
Christoph Hellwig66ae56a2019-02-18 09:38:49 -08001669 error = -EINVAL;
1670 goto out_filestream_unmount;
1671 }
1672
1673 if (xfs_globals.always_cow) {
1674 xfs_info(mp, "using DEBUG-only always_cow mode.");
1675 mp->m_always_cow = true;
1676 }
Darrick J. Wongc14632d2018-01-31 16:38:18 -08001677 }
1678
Dave Chinner38c26bf2021-08-18 18:46:37 -07001679 if (xfs_has_rmapbt(mp) && mp->m_sb.sb_rblocks) {
Darrick J. Wong1c0607a2016-08-03 12:20:57 +10001680 xfs_alert(mp,
Darrick J. Wong76883f72018-01-31 09:47:25 -08001681 "reverse mapping btree not compatible with realtime device!");
1682 error = -EINVAL;
1683 goto out_filestream_unmount;
Darrick J. Wong738f57c2016-08-26 15:59:19 +10001684 }
Darrick J. Wong1c0607a2016-08-03 12:20:57 +10001685
Chandan Babu R973ac0e2021-08-11 10:33:20 +05301686 if (xfs_has_large_extent_counts(mp))
1687 xfs_warn(mp,
1688 "EXPERIMENTAL Large extent counts feature in use. Use at your own risk!");
1689
Dave Chinner8a00ebe2012-04-13 12:10:44 +00001690 error = xfs_mountfs(mp);
Christoph Hellwig2bcf6e92011-07-13 13:43:48 +02001691 if (error)
Dave Chinner7e185302012-10-08 21:56:00 +11001692 goto out_filestream_unmount;
Dave Chinner704b2902011-03-26 09:14:57 +11001693
David Chinner01651642008-08-13 15:45:15 +10001694 root = igrab(VFS_I(mp->m_rootip));
Christoph Hellwigf3dcc132008-03-27 18:00:54 +11001695 if (!root) {
Dave Chinner24513372014-06-25 14:58:08 +10001696 error = -ENOENT;
Dave Chinner8a00ebe2012-04-13 12:10:44 +00001697 goto out_unmount;
Christoph Hellwigcbc89dc2008-02-05 12:14:01 +11001698 }
Al Viro48fde702012-01-08 22:15:13 -05001699 sb->s_root = d_make_root(root);
Christoph Hellwigf3dcc132008-03-27 18:00:54 +11001700 if (!sb->s_root) {
Dave Chinner24513372014-06-25 14:58:08 +10001701 error = -ENOMEM;
Dave Chinner8a00ebe2012-04-13 12:10:44 +00001702 goto out_unmount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Christoph Hellwig74394492007-08-30 17:21:22 +10001704
Dave Chinner7e185302012-10-08 21:56:00 +11001705 return 0;
1706
1707 out_filestream_unmount:
Christoph Hellwig120226c2008-05-20 15:11:11 +10001708 xfs_filestream_unmount(mp);
Christoph Hellwigeffa2ed2008-05-20 15:11:05 +10001709 out_free_sb:
1710 xfs_freesb(mp);
Bill O'Donnell225e4632015-10-12 18:21:19 +11001711 out_free_stats:
1712 free_percpu(mp->m_stats.xs_stats);
Dave Chinnerab23a772021-08-06 11:05:39 -07001713 out_destroy_inodegc:
Dave Chinner0ed17f02021-08-06 11:05:38 -07001714 xfs_mount_list_del(mp);
Dave Chinnerab23a772021-08-06 11:05:39 -07001715 xfs_inodegc_free_percpu(mp);
Christoph Hellwig9d565ff2008-10-30 17:53:24 +11001716 out_destroy_counters:
Dave Chinner5681ca42015-02-23 21:22:31 +11001717 xfs_destroy_percpu_counters(mp);
Bill O'Donnell225e4632015-10-12 18:21:19 +11001718 out_destroy_workqueues:
Christoph Hellwigaa6bf012012-02-29 09:53:48 +00001719 xfs_destroy_mount_workqueues(mp);
Christoph Hellwig61ba35d2010-09-30 02:25:54 +00001720 out_close_devices:
Christoph Hellwig19f354d2008-05-20 11:31:13 +10001721 xfs_close_devices(mp);
Ian Kente1d3d212019-11-04 13:58:40 -08001722 out_free_names:
Dave Chinnerc9fbd7b2018-05-10 21:50:23 -07001723 sb->s_fs_info = NULL;
Ian Kenta943f372019-11-04 13:58:42 -08001724 xfs_mount_free(mp);
Dave Chinner24513372014-06-25 14:58:08 +10001725 return error;
Christoph Hellwigf8f15e42008-05-20 11:30:59 +10001726
Christoph Hellwig2bcf6e92011-07-13 13:43:48 +02001727 out_unmount:
Christoph Hellwige48ad3162008-05-20 11:30:52 +10001728 xfs_filestream_unmount(mp);
Christoph Hellwig19f354d2008-05-20 11:31:13 +10001729 xfs_unmountfs(mp);
Christoph Hellwig62033002008-08-13 16:50:21 +10001730 goto out_free_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Ian Kent73e5fff2019-11-04 13:58:46 -08001733static int
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001734xfs_fs_get_tree(
Ian Kent73e5fff2019-11-04 13:58:46 -08001735 struct fs_context *fc)
1736{
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001737 return get_tree_bdev(fc, xfs_fs_fill_super);
Ian Kent73e5fff2019-11-04 13:58:46 -08001738}
1739
Ian Kent63cd1e92019-11-04 13:58:47 -08001740static int
1741xfs_remount_rw(
1742 struct xfs_mount *mp)
1743{
1744 struct xfs_sb *sbp = &mp->m_sb;
1745 int error;
1746
Dave Chinner0560f312021-08-18 18:46:52 -07001747 if (xfs_has_norecovery(mp)) {
Ian Kent63cd1e92019-11-04 13:58:47 -08001748 xfs_warn(mp,
1749 "ro->rw transition prohibited on norecovery mount");
1750 return -EINVAL;
1751 }
1752
Dave Chinnerd6837c12021-08-18 18:46:56 -07001753 if (xfs_sb_is_v5(sbp) &&
Ian Kent63cd1e92019-11-04 13:58:47 -08001754 xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
1755 xfs_warn(mp,
1756 "ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
1757 (sbp->sb_features_ro_compat &
1758 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
1759 return -EINVAL;
1760 }
1761
Dave Chinner2e973b22021-08-18 18:46:52 -07001762 clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
Ian Kent63cd1e92019-11-04 13:58:47 -08001763
1764 /*
1765 * If this is the first remount to writeable state we might have some
1766 * superblock changes to update.
1767 */
1768 if (mp->m_update_sb) {
1769 error = xfs_sync_sb(mp, false);
1770 if (error) {
1771 xfs_warn(mp, "failed to write sb changes");
1772 return error;
1773 }
1774 mp->m_update_sb = false;
1775 }
1776
1777 /*
1778 * Fill out the reserve pool if it is empty. Use the stashed value if
1779 * it is non-zero, otherwise go with the default.
1780 */
1781 xfs_restore_resvblks(mp);
1782 xfs_log_work_queue(mp);
Darrick J. Wongc9a65262021-01-22 16:48:44 -08001783 xfs_blockgc_start(mp);
Ian Kent63cd1e92019-11-04 13:58:47 -08001784
1785 /* Create the per-AG metadata reservation pool .*/
1786 error = xfs_fs_reserve_ag_blocks(mp);
1787 if (error && error != -ENOSPC)
1788 return error;
1789
Dave Chinnerab23a772021-08-06 11:05:39 -07001790 /* Re-enable the background inode inactivation worker. */
1791 xfs_inodegc_start(mp);
1792
Ian Kent63cd1e92019-11-04 13:58:47 -08001793 return 0;
1794}
1795
1796static int
1797xfs_remount_ro(
1798 struct xfs_mount *mp)
1799{
Darrick J. Wong089558bc2021-12-06 15:38:20 -08001800 struct xfs_icwalk icw = {
1801 .icw_flags = XFS_ICWALK_FLAG_SYNC,
1802 };
1803 int error;
Ian Kent63cd1e92019-11-04 13:58:47 -08001804
Darrick J. Wongb97cca32022-02-03 08:29:21 -08001805 /* Flush all the dirty data to disk. */
1806 error = sync_filesystem(mp->m_super);
1807 if (error)
1808 return error;
1809
Ian Kent63cd1e92019-11-04 13:58:47 -08001810 /*
1811 * Cancel background eofb scanning so it cannot race with the final
1812 * log force+buftarg wait and deadlock the remount.
1813 */
Darrick J. Wongc9a65262021-01-22 16:48:44 -08001814 xfs_blockgc_stop(mp);
Ian Kent63cd1e92019-11-04 13:58:47 -08001815
Darrick J. Wong089558bc2021-12-06 15:38:20 -08001816 /*
1817 * Clear out all remaining COW staging extents and speculative post-EOF
1818 * preallocations so that we don't leave inodes requiring inactivation
1819 * cleanups during reclaim on a read-only mount. We must process every
1820 * cached inode, so this requires a synchronous cache scan.
1821 */
1822 error = xfs_blockgc_free_space(mp, &icw);
Ian Kent63cd1e92019-11-04 13:58:47 -08001823 if (error) {
1824 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1825 return error;
1826 }
1827
Dave Chinnerab23a772021-08-06 11:05:39 -07001828 /*
1829 * Stop the inodegc background worker. xfs_fs_reconfigure already
1830 * flushed all pending inodegc work when it sync'd the filesystem.
1831 * The VFS holds s_umount, so we know that inodes cannot enter
1832 * xfs_fs_destroy_inode during a remount operation. In readonly mode
1833 * we send inodes straight to reclaim, so no inodes will be queued.
1834 */
1835 xfs_inodegc_stop(mp);
1836
Ian Kent63cd1e92019-11-04 13:58:47 -08001837 /* Free the per-AG metadata reservation pool. */
1838 error = xfs_fs_unreserve_ag_blocks(mp);
1839 if (error) {
1840 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1841 return error;
1842 }
1843
1844 /*
1845 * Before we sync the metadata, we need to free up the reserve block
1846 * pool so that the used block count in the superblock on disk is
1847 * correct at the end of the remount. Stash the current* reserve pool
1848 * size so that if we get remounted rw, we can return it to the same
1849 * size.
1850 */
1851 xfs_save_resvblks(mp);
1852
Brian Fosterea2064d2021-01-22 16:48:24 -08001853 xfs_log_clean(mp);
Dave Chinner2e973b22021-08-18 18:46:52 -07001854 set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
Ian Kent63cd1e92019-11-04 13:58:47 -08001855
1856 return 0;
1857}
1858
1859/*
1860 * Logically we would return an error here to prevent users from believing
1861 * they might have changed mount options using remount which can't be changed.
1862 *
1863 * But unfortunately mount(8) adds all options from mtab and fstab to the mount
1864 * arguments in some cases so we can't blindly reject options, but have to
1865 * check for each specified option if it actually differs from the currently
1866 * set option and only reject it if that's the case.
1867 *
1868 * Until that is implemented we return success for every remount request, and
1869 * silently ignore all options that we can't actually change.
1870 */
1871static int
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001872xfs_fs_reconfigure(
Ian Kent63cd1e92019-11-04 13:58:47 -08001873 struct fs_context *fc)
1874{
1875 struct xfs_mount *mp = XFS_M(fc->root->d_sb);
1876 struct xfs_mount *new_mp = fc->s_fs_info;
Ian Kent63cd1e92019-11-04 13:58:47 -08001877 int flags = fc->sb_flags;
1878 int error;
1879
Eric Sandeen4750a172020-07-15 08:30:37 -07001880 /* version 5 superblocks always support version counters. */
Dave Chinnerd6837c12021-08-18 18:46:56 -07001881 if (xfs_has_crc(mp))
Eric Sandeen4750a172020-07-15 08:30:37 -07001882 fc->sb_flags |= SB_I_VERSION;
1883
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001884 error = xfs_fs_validate_params(new_mp);
Ian Kent63cd1e92019-11-04 13:58:47 -08001885 if (error)
1886 return error;
1887
Ian Kent63cd1e92019-11-04 13:58:47 -08001888 /* inode32 -> inode64 */
Dave Chinner0560f312021-08-18 18:46:52 -07001889 if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
1890 mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
Dave Chinnerd6837c12021-08-18 18:46:56 -07001891 mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
Ian Kent63cd1e92019-11-04 13:58:47 -08001892 }
1893
1894 /* inode64 -> inode32 */
Dave Chinner0560f312021-08-18 18:46:52 -07001895 if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
1896 mp->m_features |= XFS_FEAT_SMALL_INUMS;
Dave Chinnerd6837c12021-08-18 18:46:56 -07001897 mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
Ian Kent63cd1e92019-11-04 13:58:47 -08001898 }
1899
1900 /* ro -> rw */
Dave Chinner2e973b22021-08-18 18:46:52 -07001901 if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
Ian Kent63cd1e92019-11-04 13:58:47 -08001902 error = xfs_remount_rw(mp);
1903 if (error)
1904 return error;
1905 }
1906
1907 /* rw -> ro */
Dave Chinner2e973b22021-08-18 18:46:52 -07001908 if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
Ian Kent63cd1e92019-11-04 13:58:47 -08001909 error = xfs_remount_ro(mp);
1910 if (error)
1911 return error;
1912 }
1913
1914 return 0;
1915}
1916
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001917static void xfs_fs_free(
Ian Kent73e5fff2019-11-04 13:58:46 -08001918 struct fs_context *fc)
1919{
1920 struct xfs_mount *mp = fc->s_fs_info;
1921
1922 /*
1923 * mp is stored in the fs_context when it is initialized.
1924 * mp is transferred to the superblock on a successful mount,
1925 * but if an error occurs before the transfer we have to free
1926 * it here.
1927 */
1928 if (mp)
1929 xfs_mount_free(mp);
1930}
1931
1932static const struct fs_context_operations xfs_context_ops = {
Darrick J. Wong1e5c39d2020-12-04 15:59:39 -08001933 .parse_param = xfs_fs_parse_param,
1934 .get_tree = xfs_fs_get_tree,
1935 .reconfigure = xfs_fs_reconfigure,
1936 .free = xfs_fs_free,
Ian Kent73e5fff2019-11-04 13:58:46 -08001937};
1938
1939static int xfs_init_fs_context(
1940 struct fs_context *fc)
1941{
1942 struct xfs_mount *mp;
1943
Ian Kent50f83002019-11-04 13:58:48 -08001944 mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
Ian Kent73e5fff2019-11-04 13:58:46 -08001945 if (!mp)
1946 return -ENOMEM;
1947
Ian Kent50f83002019-11-04 13:58:48 -08001948 spin_lock_init(&mp->m_sb_lock);
Ian Kent50f83002019-11-04 13:58:48 -08001949 INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
1950 spin_lock_init(&mp->m_perag_lock);
1951 mutex_init(&mp->m_growlock);
Darrick J. Wongf0f7a672020-04-12 13:11:10 -07001952 INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
Ian Kent50f83002019-11-04 13:58:48 -08001953 INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
Ian Kent50f83002019-11-04 13:58:48 -08001954 mp->m_kobj.kobject.kset = xfs_kset;
1955 /*
1956 * We don't create the finobt per-ag space reservation until after log
1957 * recovery, so we must set this to true so that an ifree transaction
1958 * started during log recovery will not depend on space reservations
1959 * for finobt expansion.
1960 */
1961 mp->m_finobt_nores = true;
1962
Ian Kent73e5fff2019-11-04 13:58:46 -08001963 /*
1964 * These can be overridden by the mount option parsing.
1965 */
1966 mp->m_logbufs = -1;
1967 mp->m_logbsize = -1;
1968 mp->m_allocsize_log = 16; /* 64k */
1969
1970 /*
1971 * Copy binary VFS mount flags we are interested in.
1972 */
1973 if (fc->sb_flags & SB_RDONLY)
Dave Chinner2e973b22021-08-18 18:46:52 -07001974 set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
Ian Kent73e5fff2019-11-04 13:58:46 -08001975 if (fc->sb_flags & SB_DIRSYNC)
Dave Chinner0560f312021-08-18 18:46:52 -07001976 mp->m_features |= XFS_FEAT_DIRSYNC;
Ian Kent73e5fff2019-11-04 13:58:46 -08001977 if (fc->sb_flags & SB_SYNCHRONOUS)
Dave Chinner0560f312021-08-18 18:46:52 -07001978 mp->m_features |= XFS_FEAT_WSYNC;
Ian Kent73e5fff2019-11-04 13:58:46 -08001979
1980 fc->s_fs_info = mp;
1981 fc->ops = &xfs_context_ops;
1982
1983 return 0;
1984}
1985
Andrew Morton5085b602007-02-20 13:57:47 -08001986static struct file_system_type xfs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .owner = THIS_MODULE,
1988 .name = "xfs",
Ian Kent73e5fff2019-11-04 13:58:46 -08001989 .init_fs_context = xfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04001990 .parameters = xfs_fs_parameters,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 .kill_sb = kill_block_super,
Christoph Hellwigf736d932021-01-21 14:19:58 +01001992 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001994MODULE_ALIAS_FS("xfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10001996STATIC int __init
Darrick J. Wong182696f2021-10-12 11:09:23 -07001997xfs_init_caches(void)
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10001998{
Darrick J. Wong9fa47bd2021-09-23 12:21:37 -07001999 int error;
2000
Dave Chinner231f91a2022-07-18 18:20:37 -07002001 xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2002 SLAB_HWCACHE_ALIGN |
2003 SLAB_RECLAIM_ACCOUNT |
2004 SLAB_MEM_SPREAD,
2005 NULL);
2006 if (!xfs_buf_cache)
2007 goto out;
2008
Darrick J. Wong182696f2021-10-12 11:09:23 -07002009 xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002010 sizeof(struct xlog_ticket),
2011 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002012 if (!xfs_log_ticket_cache)
Dave Chinner231f91a2022-07-18 18:20:37 -07002013 goto out_destroy_buf_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002014
Darrick J. Wong9fa47bd2021-09-23 12:21:37 -07002015 error = xfs_btree_init_cur_caches();
2016 if (error)
Darrick J. Wongc201d9c2021-10-12 14:17:01 -07002017 goto out_destroy_log_ticket_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002018
Darrick J. Wongf3c799c2021-10-12 14:11:01 -07002019 error = xfs_defer_init_item_caches();
2020 if (error)
2021 goto out_destroy_btree_cur_cache;
2022
Darrick J. Wong182696f2021-10-12 11:09:23 -07002023 xfs_da_state_cache = kmem_cache_create("xfs_da_state",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002024 sizeof(struct xfs_da_state),
2025 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002026 if (!xfs_da_state_cache)
Darrick J. Wongf3c799c2021-10-12 14:11:01 -07002027 goto out_destroy_defer_item_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002028
Darrick J. Wong182696f2021-10-12 11:09:23 -07002029 xfs_ifork_cache = kmem_cache_create("xfs_ifork",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002030 sizeof(struct xfs_ifork),
2031 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002032 if (!xfs_ifork_cache)
2033 goto out_destroy_da_state_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002034
Darrick J. Wong182696f2021-10-12 11:09:23 -07002035 xfs_trans_cache = kmem_cache_create("xfs_trans",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002036 sizeof(struct xfs_trans),
2037 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002038 if (!xfs_trans_cache)
2039 goto out_destroy_ifork_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002040
Christoph Hellwige98c4142010-06-23 18:11:15 +10002041
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002042 /*
Darrick J. Wong182696f2021-10-12 11:09:23 -07002043 * The size of the cache-allocated buf log item is the maximum
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002044 * size possible under XFS. This wastes a little bit of memory,
2045 * but it is much faster.
2046 */
Darrick J. Wong182696f2021-10-12 11:09:23 -07002047 xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002048 sizeof(struct xfs_buf_log_item),
2049 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002050 if (!xfs_buf_item_cache)
2051 goto out_destroy_trans_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002052
Darrick J. Wong182696f2021-10-12 11:09:23 -07002053 xfs_efd_cache = kmem_cache_create("xfs_efd_item",
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -07002054 xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2055 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002056 if (!xfs_efd_cache)
2057 goto out_destroy_buf_item_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002058
Darrick J. Wong182696f2021-10-12 11:09:23 -07002059 xfs_efi_cache = kmem_cache_create("xfs_efi_item",
Darrick J. Wong3c5aaac2022-10-21 09:10:05 -07002060 xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2061 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002062 if (!xfs_efi_cache)
2063 goto out_destroy_efd_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002064
Darrick J. Wong182696f2021-10-12 11:09:23 -07002065 xfs_inode_cache = kmem_cache_create("xfs_inode",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002066 sizeof(struct xfs_inode), 0,
2067 (SLAB_HWCACHE_ALIGN |
2068 SLAB_RECLAIM_ACCOUNT |
2069 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
2070 xfs_fs_inode_init_once);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002071 if (!xfs_inode_cache)
2072 goto out_destroy_efi_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002073
Darrick J. Wong182696f2021-10-12 11:09:23 -07002074 xfs_ili_cache = kmem_cache_create("xfs_ili",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002075 sizeof(struct xfs_inode_log_item), 0,
Dave Chinnerd59eada2020-03-24 20:10:28 -07002076 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2077 NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002078 if (!xfs_ili_cache)
2079 goto out_destroy_inode_cache;
Carlos Maiolinob1231762019-11-14 12:43:03 -08002080
Darrick J. Wong182696f2021-10-12 11:09:23 -07002081 xfs_icreate_cache = kmem_cache_create("xfs_icr",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002082 sizeof(struct xfs_icreate_item),
2083 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002084 if (!xfs_icreate_cache)
2085 goto out_destroy_ili_cache;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002086
Darrick J. Wong182696f2021-10-12 11:09:23 -07002087 xfs_rud_cache = kmem_cache_create("xfs_rud_item",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002088 sizeof(struct xfs_rud_log_item),
2089 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002090 if (!xfs_rud_cache)
2091 goto out_destroy_icreate_cache;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10002092
Darrick J. Wong182696f2021-10-12 11:09:23 -07002093 xfs_rui_cache = kmem_cache_create("xfs_rui_item",
Darrick J. Wongcd001582016-09-19 10:24:27 +10002094 xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
Carlos Maiolinob1231762019-11-14 12:43:03 -08002095 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002096 if (!xfs_rui_cache)
2097 goto out_destroy_rud_cache;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10002098
Darrick J. Wong182696f2021-10-12 11:09:23 -07002099 xfs_cud_cache = kmem_cache_create("xfs_cud_item",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002100 sizeof(struct xfs_cud_log_item),
2101 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002102 if (!xfs_cud_cache)
2103 goto out_destroy_rui_cache;
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -07002104
Darrick J. Wong182696f2021-10-12 11:09:23 -07002105 xfs_cui_cache = kmem_cache_create("xfs_cui_item",
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -07002106 xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
Carlos Maiolinob1231762019-11-14 12:43:03 -08002107 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002108 if (!xfs_cui_cache)
2109 goto out_destroy_cud_cache;
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -07002110
Darrick J. Wong182696f2021-10-12 11:09:23 -07002111 xfs_bud_cache = kmem_cache_create("xfs_bud_item",
Carlos Maiolinob1231762019-11-14 12:43:03 -08002112 sizeof(struct xfs_bud_log_item),
2113 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002114 if (!xfs_bud_cache)
2115 goto out_destroy_cui_cache;
Darrick J. Wong6413a012016-10-03 09:11:25 -07002116
Darrick J. Wong182696f2021-10-12 11:09:23 -07002117 xfs_bui_cache = kmem_cache_create("xfs_bui_item",
Darrick J. Wong6413a012016-10-03 09:11:25 -07002118 xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
Carlos Maiolinob1231762019-11-14 12:43:03 -08002119 0, 0, NULL);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002120 if (!xfs_bui_cache)
2121 goto out_destroy_bud_cache;
Darrick J. Wong6413a012016-10-03 09:11:25 -07002122
Darrick J. Wong4136e382022-05-22 15:59:48 +10002123 xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
2124 sizeof(struct xfs_attrd_log_item),
2125 0, 0, NULL);
2126 if (!xfs_attrd_cache)
2127 goto out_destroy_bui_cache;
2128
2129 xfs_attri_cache = kmem_cache_create("xfs_attri_item",
2130 sizeof(struct xfs_attri_log_item),
2131 0, 0, NULL);
2132 if (!xfs_attri_cache)
2133 goto out_destroy_attrd_cache;
2134
Dave Chinner784eb7d2022-07-14 11:47:42 +10002135 xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2136 sizeof(struct xfs_iunlink_item),
2137 0, 0, NULL);
2138 if (!xfs_iunlink_cache)
2139 goto out_destroy_attri_cache;
2140
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002141 return 0;
2142
Dave Chinner784eb7d2022-07-14 11:47:42 +10002143 out_destroy_attri_cache:
2144 kmem_cache_destroy(xfs_attri_cache);
Darrick J. Wong4136e382022-05-22 15:59:48 +10002145 out_destroy_attrd_cache:
2146 kmem_cache_destroy(xfs_attrd_cache);
2147 out_destroy_bui_cache:
2148 kmem_cache_destroy(xfs_bui_cache);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002149 out_destroy_bud_cache:
2150 kmem_cache_destroy(xfs_bud_cache);
2151 out_destroy_cui_cache:
2152 kmem_cache_destroy(xfs_cui_cache);
2153 out_destroy_cud_cache:
2154 kmem_cache_destroy(xfs_cud_cache);
2155 out_destroy_rui_cache:
2156 kmem_cache_destroy(xfs_rui_cache);
2157 out_destroy_rud_cache:
2158 kmem_cache_destroy(xfs_rud_cache);
2159 out_destroy_icreate_cache:
2160 kmem_cache_destroy(xfs_icreate_cache);
2161 out_destroy_ili_cache:
2162 kmem_cache_destroy(xfs_ili_cache);
2163 out_destroy_inode_cache:
2164 kmem_cache_destroy(xfs_inode_cache);
2165 out_destroy_efi_cache:
2166 kmem_cache_destroy(xfs_efi_cache);
2167 out_destroy_efd_cache:
2168 kmem_cache_destroy(xfs_efd_cache);
2169 out_destroy_buf_item_cache:
2170 kmem_cache_destroy(xfs_buf_item_cache);
2171 out_destroy_trans_cache:
2172 kmem_cache_destroy(xfs_trans_cache);
2173 out_destroy_ifork_cache:
2174 kmem_cache_destroy(xfs_ifork_cache);
2175 out_destroy_da_state_cache:
2176 kmem_cache_destroy(xfs_da_state_cache);
Darrick J. Wongf3c799c2021-10-12 14:11:01 -07002177 out_destroy_defer_item_cache:
2178 xfs_defer_destroy_item_caches();
Darrick J. Wong182696f2021-10-12 11:09:23 -07002179 out_destroy_btree_cur_cache:
Darrick J. Wong9fa47bd2021-09-23 12:21:37 -07002180 xfs_btree_destroy_cur_caches();
Darrick J. Wong182696f2021-10-12 11:09:23 -07002181 out_destroy_log_ticket_cache:
2182 kmem_cache_destroy(xfs_log_ticket_cache);
Dave Chinner231f91a2022-07-18 18:20:37 -07002183 out_destroy_buf_cache:
2184 kmem_cache_destroy(xfs_buf_cache);
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002185 out:
2186 return -ENOMEM;
2187}
2188
2189STATIC void
Darrick J. Wong182696f2021-10-12 11:09:23 -07002190xfs_destroy_caches(void)
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002191{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10002192 /*
2193 * Make sure all delayed rcu free are flushed before we
2194 * destroy caches.
2195 */
2196 rcu_barrier();
Dave Chinner784eb7d2022-07-14 11:47:42 +10002197 kmem_cache_destroy(xfs_iunlink_cache);
Darrick J. Wong4136e382022-05-22 15:59:48 +10002198 kmem_cache_destroy(xfs_attri_cache);
2199 kmem_cache_destroy(xfs_attrd_cache);
Darrick J. Wong182696f2021-10-12 11:09:23 -07002200 kmem_cache_destroy(xfs_bui_cache);
2201 kmem_cache_destroy(xfs_bud_cache);
2202 kmem_cache_destroy(xfs_cui_cache);
2203 kmem_cache_destroy(xfs_cud_cache);
2204 kmem_cache_destroy(xfs_rui_cache);
2205 kmem_cache_destroy(xfs_rud_cache);
2206 kmem_cache_destroy(xfs_icreate_cache);
2207 kmem_cache_destroy(xfs_ili_cache);
2208 kmem_cache_destroy(xfs_inode_cache);
2209 kmem_cache_destroy(xfs_efi_cache);
2210 kmem_cache_destroy(xfs_efd_cache);
2211 kmem_cache_destroy(xfs_buf_item_cache);
2212 kmem_cache_destroy(xfs_trans_cache);
2213 kmem_cache_destroy(xfs_ifork_cache);
2214 kmem_cache_destroy(xfs_da_state_cache);
Darrick J. Wongf3c799c2021-10-12 14:11:01 -07002215 xfs_defer_destroy_item_caches();
Darrick J. Wong9fa47bd2021-09-23 12:21:37 -07002216 xfs_btree_destroy_cur_caches();
Darrick J. Wong182696f2021-10-12 11:09:23 -07002217 kmem_cache_destroy(xfs_log_ticket_cache);
Dave Chinner231f91a2022-07-18 18:20:37 -07002218 kmem_cache_destroy(xfs_buf_cache);
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002219}
2220
2221STATIC int __init
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002222xfs_init_workqueues(void)
2223{
2224 /*
Dave Chinnerc999a222012-03-22 05:15:07 +00002225 * The allocation workqueue can be used in memory reclaim situations
2226 * (writepage path), and parallelism is only limited by the number of
2227 * AGs in all the filesystems mounted. Hence use the default large
2228 * max_active value for this workqueue.
2229 */
Brian Foster8018ec02014-09-09 11:44:46 +10002230 xfs_alloc_wq = alloc_workqueue("xfsalloc",
Darrick J. Wong05a302a2021-01-22 16:48:42 -08002231 XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
Dave Chinnerc999a222012-03-22 05:15:07 +00002232 if (!xfs_alloc_wq)
Dave Chinner58896082012-10-08 21:56:05 +11002233 return -ENOMEM;
Dave Chinnerc999a222012-03-22 05:15:07 +00002234
Darrick J. Wong05a302a2021-01-22 16:48:42 -08002235 xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
2236 0);
Christoph Hellwig4560e782017-02-07 14:07:58 -08002237 if (!xfs_discard_wq)
2238 goto out_free_alloc_wq;
2239
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002240 return 0;
Christoph Hellwig4560e782017-02-07 14:07:58 -08002241out_free_alloc_wq:
2242 destroy_workqueue(xfs_alloc_wq);
2243 return -ENOMEM;
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002244}
2245
Luck, Tony39411f82011-04-11 12:06:12 -07002246STATIC void
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002247xfs_destroy_workqueues(void)
2248{
Christoph Hellwig4560e782017-02-07 14:07:58 -08002249 destroy_workqueue(xfs_discard_wq);
Dave Chinnerc999a222012-03-22 05:15:07 +00002250 destroy_workqueue(xfs_alloc_wq);
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002251}
2252
Dave Chinnerf1653c22021-08-06 11:05:37 -07002253#ifdef CONFIG_HOTPLUG_CPU
2254static int
2255xfs_cpu_dead(
2256 unsigned int cpu)
2257{
Dave Chinner0ed17f02021-08-06 11:05:38 -07002258 struct xfs_mount *mp, *n;
2259
2260 spin_lock(&xfs_mount_list_lock);
2261 list_for_each_entry_safe(mp, n, &xfs_mount_list, m_mount_list) {
2262 spin_unlock(&xfs_mount_list_lock);
Dave Chinnerab23a772021-08-06 11:05:39 -07002263 xfs_inodegc_cpu_dead(mp, cpu);
Dave Chinneraf1c2142022-07-02 02:13:52 +10002264 xlog_cil_pcp_dead(mp->m_log, cpu);
Dave Chinner0ed17f02021-08-06 11:05:38 -07002265 spin_lock(&xfs_mount_list_lock);
2266 }
2267 spin_unlock(&xfs_mount_list_lock);
Dave Chinnerf1653c22021-08-06 11:05:37 -07002268 return 0;
2269}
2270
2271static int __init
2272xfs_cpu_hotplug_init(void)
2273{
2274 int error;
2275
2276 error = cpuhp_setup_state_nocalls(CPUHP_XFS_DEAD, "xfs:dead", NULL,
2277 xfs_cpu_dead);
2278 if (error < 0)
2279 xfs_alert(NULL,
2280"Failed to initialise CPU hotplug, error %d. XFS is non-functional.",
2281 error);
2282 return error;
2283}
2284
2285static void
2286xfs_cpu_hotplug_destroy(void)
2287{
2288 cpuhp_remove_state_nocalls(CPUHP_XFS_DEAD);
2289}
2290
2291#else /* !CONFIG_HOTPLUG_CPU */
2292static inline int xfs_cpu_hotplug_init(void) { return 0; }
2293static inline void xfs_cpu_hotplug_destroy(void) {}
2294#endif
2295
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002296STATIC int __init
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002297init_xfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Darrick J. Wong30cbc592016-03-09 08:15:14 +11002301 xfs_check_ondisk_structs();
2302
Darrick J. Wong3cfb9292023-03-16 09:31:20 -07002303 error = xfs_dahash_test();
2304 if (error)
2305 return error;
2306
Christoph Hellwig65795912008-11-28 14:23:33 +11002307 printk(KERN_INFO XFS_VERSION_STRING " with "
2308 XFS_BUILD_OPTIONS " enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002310 xfs_dir_startup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Dave Chinnerf1653c22021-08-06 11:05:37 -07002312 error = xfs_cpu_hotplug_init();
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002313 if (error)
2314 goto out;
2315
Darrick J. Wong182696f2021-10-12 11:09:23 -07002316 error = xfs_init_caches();
Dave Chinnerf1653c22021-08-06 11:05:37 -07002317 if (error)
2318 goto out_destroy_hp;
2319
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002320 error = xfs_init_workqueues();
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002321 if (error)
Darrick J. Wong182696f2021-10-12 11:09:23 -07002322 goto out_destroy_caches;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002323
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002324 error = xfs_mru_cache_init();
2325 if (error)
2326 goto out_destroy_wq;
2327
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002328 error = xfs_init_procfs();
2329 if (error)
Dave Chinner231f91a2022-07-18 18:20:37 -07002330 goto out_mru_cache_uninit;
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002331
2332 error = xfs_sysctl_register();
2333 if (error)
2334 goto out_cleanup_procfs;
2335
Brian Foster3d871222014-07-15 07:41:37 +10002336 xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
2337 if (!xfs_kset) {
2338 error = -ENOMEM;
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002339 goto out_sysctl_unregister;
Brian Foster3d871222014-07-15 07:41:37 +10002340 }
2341
Bill O'Donnell80529c42015-10-12 05:19:45 +11002342 xfsstats.xs_kobj.kobject.kset = xfs_kset;
2343
2344 xfsstats.xs_stats = alloc_percpu(struct xfsstats);
2345 if (!xfsstats.xs_stats) {
2346 error = -ENOMEM;
2347 goto out_kset_unregister;
2348 }
2349
2350 error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002351 "stats");
2352 if (error)
Bill O'Donnell80529c42015-10-12 05:19:45 +11002353 goto out_free_stats;
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002354
Brian Foster65b65732014-09-09 11:52:42 +10002355#ifdef DEBUG
2356 xfs_dbg_kobj.kobject.kset = xfs_kset;
2357 error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
Christoph Hellwiga05931c2012-03-13 08:52:37 +00002358 if (error)
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002359 goto out_remove_stats_kobj;
Brian Foster65b65732014-09-09 11:52:42 +10002360#endif
2361
2362 error = xfs_qm_init();
2363 if (error)
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002364 goto out_remove_dbg_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 error = register_filesystem(&xfs_fs_type);
2367 if (error)
Christoph Hellwiga05931c2012-03-13 08:52:37 +00002368 goto out_qm_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return 0;
2370
Christoph Hellwiga05931c2012-03-13 08:52:37 +00002371 out_qm_exit:
2372 xfs_qm_exit();
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002373 out_remove_dbg_kobj:
Brian Foster65b65732014-09-09 11:52:42 +10002374#ifdef DEBUG
2375 xfs_sysfs_del(&xfs_dbg_kobj);
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002376 out_remove_stats_kobj:
Brian Foster65b65732014-09-09 11:52:42 +10002377#endif
Bill O'Donnell80529c42015-10-12 05:19:45 +11002378 xfs_sysfs_del(&xfsstats.xs_kobj);
2379 out_free_stats:
2380 free_percpu(xfsstats.xs_stats);
Bill O'Donnellbb230c12015-10-12 05:15:45 +11002381 out_kset_unregister:
Brian Foster3d871222014-07-15 07:41:37 +10002382 kset_unregister(xfs_kset);
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002383 out_sysctl_unregister:
2384 xfs_sysctl_unregister();
2385 out_cleanup_procfs:
2386 xfs_cleanup_procfs();
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002387 out_mru_cache_uninit:
2388 xfs_mru_cache_uninit();
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002389 out_destroy_wq:
2390 xfs_destroy_workqueues();
Darrick J. Wong182696f2021-10-12 11:09:23 -07002391 out_destroy_caches:
2392 xfs_destroy_caches();
Dave Chinnerf1653c22021-08-06 11:05:37 -07002393 out_destroy_hp:
2394 xfs_cpu_hotplug_destroy();
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002395 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return error;
2397}
2398
2399STATIC void __exit
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002400exit_xfs_fs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Christoph Hellwiga05931c2012-03-13 08:52:37 +00002402 xfs_qm_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 unregister_filesystem(&xfs_fs_type);
Brian Foster65b65732014-09-09 11:52:42 +10002404#ifdef DEBUG
2405 xfs_sysfs_del(&xfs_dbg_kobj);
2406#endif
Bill O'Donnell80529c42015-10-12 05:19:45 +11002407 xfs_sysfs_del(&xfsstats.xs_kobj);
2408 free_percpu(xfsstats.xs_stats);
Brian Foster3d871222014-07-15 07:41:37 +10002409 kset_unregister(xfs_kset);
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002410 xfs_sysctl_unregister();
2411 xfs_cleanup_procfs();
Christoph Hellwig9f8868f2008-07-18 17:11:46 +10002412 xfs_mru_cache_uninit();
Dave Chinner0bf6a5b2011-04-08 12:45:07 +10002413 xfs_destroy_workqueues();
Darrick J. Wong182696f2021-10-12 11:09:23 -07002414 xfs_destroy_caches();
Darrick J. Wongaf3b6382015-11-03 13:06:34 +11002415 xfs_uuid_table_free();
Dave Chinnerf1653c22021-08-06 11:05:37 -07002416 xfs_cpu_hotplug_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419module_init(init_xfs_fs);
2420module_exit(exit_xfs_fs);
2421
2422MODULE_AUTHOR("Silicon Graphics, Inc.");
2423MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2424MODULE_LICENSE("GPL");