xfs_super.c 55 KB

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