xfs_super.c 56 KB

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