xfs_super.c 56 KB

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