xfs_super.c 56 KB

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