xfs_super.c 55 KB

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