xfs_mount.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir2_sf.h"
  34. #include "xfs_attr_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_rtalloc.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_error.h"
  43. #include "xfs_rw.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_fsops.h"
  46. #include "xfs_utils.h"
  47. STATIC int xfs_uuid_mount(xfs_mount_t *);
  48. STATIC void xfs_unmountfs_wait(xfs_mount_t *);
  49. #ifdef HAVE_PERCPU_SB
  50. STATIC void xfs_icsb_balance_counter(xfs_mount_t *, xfs_sb_field_t,
  51. int);
  52. STATIC void xfs_icsb_balance_counter_locked(xfs_mount_t *, xfs_sb_field_t,
  53. int);
  54. STATIC int xfs_icsb_modify_counters(xfs_mount_t *, xfs_sb_field_t,
  55. int64_t, int);
  56. STATIC void xfs_icsb_disable_counter(xfs_mount_t *, xfs_sb_field_t);
  57. #else
  58. #define xfs_icsb_balance_counter(mp, a, b) do { } while (0)
  59. #define xfs_icsb_balance_counter_locked(mp, a, b) do { } while (0)
  60. #define xfs_icsb_modify_counters(mp, a, b, c) do { } while (0)
  61. #endif
  62. static const struct {
  63. short offset;
  64. short type; /* 0 = integer
  65. * 1 = binary / string (no translation)
  66. */
  67. } xfs_sb_info[] = {
  68. { offsetof(xfs_sb_t, sb_magicnum), 0 },
  69. { offsetof(xfs_sb_t, sb_blocksize), 0 },
  70. { offsetof(xfs_sb_t, sb_dblocks), 0 },
  71. { offsetof(xfs_sb_t, sb_rblocks), 0 },
  72. { offsetof(xfs_sb_t, sb_rextents), 0 },
  73. { offsetof(xfs_sb_t, sb_uuid), 1 },
  74. { offsetof(xfs_sb_t, sb_logstart), 0 },
  75. { offsetof(xfs_sb_t, sb_rootino), 0 },
  76. { offsetof(xfs_sb_t, sb_rbmino), 0 },
  77. { offsetof(xfs_sb_t, sb_rsumino), 0 },
  78. { offsetof(xfs_sb_t, sb_rextsize), 0 },
  79. { offsetof(xfs_sb_t, sb_agblocks), 0 },
  80. { offsetof(xfs_sb_t, sb_agcount), 0 },
  81. { offsetof(xfs_sb_t, sb_rbmblocks), 0 },
  82. { offsetof(xfs_sb_t, sb_logblocks), 0 },
  83. { offsetof(xfs_sb_t, sb_versionnum), 0 },
  84. { offsetof(xfs_sb_t, sb_sectsize), 0 },
  85. { offsetof(xfs_sb_t, sb_inodesize), 0 },
  86. { offsetof(xfs_sb_t, sb_inopblock), 0 },
  87. { offsetof(xfs_sb_t, sb_fname[0]), 1 },
  88. { offsetof(xfs_sb_t, sb_blocklog), 0 },
  89. { offsetof(xfs_sb_t, sb_sectlog), 0 },
  90. { offsetof(xfs_sb_t, sb_inodelog), 0 },
  91. { offsetof(xfs_sb_t, sb_inopblog), 0 },
  92. { offsetof(xfs_sb_t, sb_agblklog), 0 },
  93. { offsetof(xfs_sb_t, sb_rextslog), 0 },
  94. { offsetof(xfs_sb_t, sb_inprogress), 0 },
  95. { offsetof(xfs_sb_t, sb_imax_pct), 0 },
  96. { offsetof(xfs_sb_t, sb_icount), 0 },
  97. { offsetof(xfs_sb_t, sb_ifree), 0 },
  98. { offsetof(xfs_sb_t, sb_fdblocks), 0 },
  99. { offsetof(xfs_sb_t, sb_frextents), 0 },
  100. { offsetof(xfs_sb_t, sb_uquotino), 0 },
  101. { offsetof(xfs_sb_t, sb_gquotino), 0 },
  102. { offsetof(xfs_sb_t, sb_qflags), 0 },
  103. { offsetof(xfs_sb_t, sb_flags), 0 },
  104. { offsetof(xfs_sb_t, sb_shared_vn), 0 },
  105. { offsetof(xfs_sb_t, sb_inoalignmt), 0 },
  106. { offsetof(xfs_sb_t, sb_unit), 0 },
  107. { offsetof(xfs_sb_t, sb_width), 0 },
  108. { offsetof(xfs_sb_t, sb_dirblklog), 0 },
  109. { offsetof(xfs_sb_t, sb_logsectlog), 0 },
  110. { offsetof(xfs_sb_t, sb_logsectsize),0 },
  111. { offsetof(xfs_sb_t, sb_logsunit), 0 },
  112. { offsetof(xfs_sb_t, sb_features2), 0 },
  113. { offsetof(xfs_sb_t, sb_bad_features2), 0 },
  114. { sizeof(xfs_sb_t), 0 }
  115. };
  116. /*
  117. * Free up the resources associated with a mount structure. Assume that
  118. * the structure was initially zeroed, so we can tell which fields got
  119. * initialized.
  120. */
  121. STATIC void
  122. xfs_free_perag(
  123. xfs_mount_t *mp)
  124. {
  125. if (mp->m_perag) {
  126. int agno;
  127. for (agno = 0; agno < mp->m_maxagi; agno++)
  128. if (mp->m_perag[agno].pagb_list)
  129. kmem_free(mp->m_perag[agno].pagb_list);
  130. kmem_free(mp->m_perag);
  131. }
  132. }
  133. /*
  134. * Check size of device based on the (data/realtime) block count.
  135. * Note: this check is used by the growfs code as well as mount.
  136. */
  137. int
  138. xfs_sb_validate_fsb_count(
  139. xfs_sb_t *sbp,
  140. __uint64_t nblocks)
  141. {
  142. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  143. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  144. #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
  145. if (nblocks >> (PAGE_CACHE_SHIFT - sbp->sb_blocklog) > ULONG_MAX)
  146. return E2BIG;
  147. #else /* Limited by UINT_MAX of sectors */
  148. if (nblocks << (sbp->sb_blocklog - BBSHIFT) > UINT_MAX)
  149. return E2BIG;
  150. #endif
  151. return 0;
  152. }
  153. /*
  154. * Check the validity of the SB found.
  155. */
  156. STATIC int
  157. xfs_mount_validate_sb(
  158. xfs_mount_t *mp,
  159. xfs_sb_t *sbp,
  160. int flags)
  161. {
  162. /*
  163. * If the log device and data device have the
  164. * same device number, the log is internal.
  165. * Consequently, the sb_logstart should be non-zero. If
  166. * we have a zero sb_logstart in this case, we may be trying to mount
  167. * a volume filesystem in a non-volume manner.
  168. */
  169. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  170. xfs_fs_mount_cmn_err(flags, "bad magic number");
  171. return XFS_ERROR(EWRONGFS);
  172. }
  173. if (!xfs_sb_good_version(sbp)) {
  174. xfs_fs_mount_cmn_err(flags, "bad version");
  175. return XFS_ERROR(EWRONGFS);
  176. }
  177. if (unlikely(
  178. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  179. xfs_fs_mount_cmn_err(flags,
  180. "filesystem is marked as having an external log; "
  181. "specify logdev on the\nmount command line.");
  182. return XFS_ERROR(EINVAL);
  183. }
  184. if (unlikely(
  185. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  186. xfs_fs_mount_cmn_err(flags,
  187. "filesystem is marked as having an internal log; "
  188. "do not specify logdev on\nthe mount command line.");
  189. return XFS_ERROR(EINVAL);
  190. }
  191. /*
  192. * More sanity checking. These were stolen directly from
  193. * xfs_repair.
  194. */
  195. if (unlikely(
  196. sbp->sb_agcount <= 0 ||
  197. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  198. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  199. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  200. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  201. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  202. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  203. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  204. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  205. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  206. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  207. sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
  208. sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
  209. (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
  210. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  211. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  212. (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */))) {
  213. xfs_fs_mount_cmn_err(flags, "SB sanity check 1 failed");
  214. return XFS_ERROR(EFSCORRUPTED);
  215. }
  216. /*
  217. * Sanity check AG count, size fields against data size field
  218. */
  219. if (unlikely(
  220. sbp->sb_dblocks == 0 ||
  221. sbp->sb_dblocks >
  222. (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
  223. sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
  224. sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
  225. xfs_fs_mount_cmn_err(flags, "SB sanity check 2 failed");
  226. return XFS_ERROR(EFSCORRUPTED);
  227. }
  228. /*
  229. * Until this is fixed only page-sized or smaller data blocks work.
  230. */
  231. if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
  232. xfs_fs_mount_cmn_err(flags,
  233. "file system with blocksize %d bytes",
  234. sbp->sb_blocksize);
  235. xfs_fs_mount_cmn_err(flags,
  236. "only pagesize (%ld) or less will currently work.",
  237. PAGE_SIZE);
  238. return XFS_ERROR(ENOSYS);
  239. }
  240. if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
  241. xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
  242. xfs_fs_mount_cmn_err(flags,
  243. "file system too large to be mounted on this system.");
  244. return XFS_ERROR(E2BIG);
  245. }
  246. if (unlikely(sbp->sb_inprogress)) {
  247. xfs_fs_mount_cmn_err(flags, "file system busy");
  248. return XFS_ERROR(EFSCORRUPTED);
  249. }
  250. /*
  251. * Version 1 directory format has never worked on Linux.
  252. */
  253. if (unlikely(!xfs_sb_version_hasdirv2(sbp))) {
  254. xfs_fs_mount_cmn_err(flags,
  255. "file system using version 1 directory format");
  256. return XFS_ERROR(ENOSYS);
  257. }
  258. return 0;
  259. }
  260. STATIC void
  261. xfs_initialize_perag_icache(
  262. xfs_perag_t *pag)
  263. {
  264. if (!pag->pag_ici_init) {
  265. rwlock_init(&pag->pag_ici_lock);
  266. INIT_RADIX_TREE(&pag->pag_ici_root, GFP_ATOMIC);
  267. pag->pag_ici_init = 1;
  268. }
  269. }
  270. xfs_agnumber_t
  271. xfs_initialize_perag(
  272. xfs_mount_t *mp,
  273. xfs_agnumber_t agcount)
  274. {
  275. xfs_agnumber_t index, max_metadata;
  276. xfs_perag_t *pag;
  277. xfs_agino_t agino;
  278. xfs_ino_t ino;
  279. xfs_sb_t *sbp = &mp->m_sb;
  280. xfs_ino_t max_inum = XFS_MAXINUMBER_32;
  281. /* Check to see if the filesystem can overflow 32 bit inodes */
  282. agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
  283. ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
  284. /* Clear the mount flag if no inode can overflow 32 bits
  285. * on this filesystem, or if specifically requested..
  286. */
  287. if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > max_inum) {
  288. mp->m_flags |= XFS_MOUNT_32BITINODES;
  289. } else {
  290. mp->m_flags &= ~XFS_MOUNT_32BITINODES;
  291. }
  292. /* If we can overflow then setup the ag headers accordingly */
  293. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  294. /* Calculate how much should be reserved for inodes to
  295. * meet the max inode percentage.
  296. */
  297. if (mp->m_maxicount) {
  298. __uint64_t icount;
  299. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  300. do_div(icount, 100);
  301. icount += sbp->sb_agblocks - 1;
  302. do_div(icount, sbp->sb_agblocks);
  303. max_metadata = icount;
  304. } else {
  305. max_metadata = agcount;
  306. }
  307. for (index = 0; index < agcount; index++) {
  308. ino = XFS_AGINO_TO_INO(mp, index, agino);
  309. if (ino > max_inum) {
  310. index++;
  311. break;
  312. }
  313. /* This ag is preferred for inodes */
  314. pag = &mp->m_perag[index];
  315. pag->pagi_inodeok = 1;
  316. if (index < max_metadata)
  317. pag->pagf_metadata = 1;
  318. xfs_initialize_perag_icache(pag);
  319. }
  320. } else {
  321. /* Setup default behavior for smaller filesystems */
  322. for (index = 0; index < agcount; index++) {
  323. pag = &mp->m_perag[index];
  324. pag->pagi_inodeok = 1;
  325. xfs_initialize_perag_icache(pag);
  326. }
  327. }
  328. return index;
  329. }
  330. void
  331. xfs_sb_from_disk(
  332. xfs_sb_t *to,
  333. xfs_dsb_t *from)
  334. {
  335. to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
  336. to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
  337. to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
  338. to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
  339. to->sb_rextents = be64_to_cpu(from->sb_rextents);
  340. memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
  341. to->sb_logstart = be64_to_cpu(from->sb_logstart);
  342. to->sb_rootino = be64_to_cpu(from->sb_rootino);
  343. to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
  344. to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
  345. to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
  346. to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
  347. to->sb_agcount = be32_to_cpu(from->sb_agcount);
  348. to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
  349. to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
  350. to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
  351. to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
  352. to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
  353. to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
  354. memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
  355. to->sb_blocklog = from->sb_blocklog;
  356. to->sb_sectlog = from->sb_sectlog;
  357. to->sb_inodelog = from->sb_inodelog;
  358. to->sb_inopblog = from->sb_inopblog;
  359. to->sb_agblklog = from->sb_agblklog;
  360. to->sb_rextslog = from->sb_rextslog;
  361. to->sb_inprogress = from->sb_inprogress;
  362. to->sb_imax_pct = from->sb_imax_pct;
  363. to->sb_icount = be64_to_cpu(from->sb_icount);
  364. to->sb_ifree = be64_to_cpu(from->sb_ifree);
  365. to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
  366. to->sb_frextents = be64_to_cpu(from->sb_frextents);
  367. to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
  368. to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
  369. to->sb_qflags = be16_to_cpu(from->sb_qflags);
  370. to->sb_flags = from->sb_flags;
  371. to->sb_shared_vn = from->sb_shared_vn;
  372. to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
  373. to->sb_unit = be32_to_cpu(from->sb_unit);
  374. to->sb_width = be32_to_cpu(from->sb_width);
  375. to->sb_dirblklog = from->sb_dirblklog;
  376. to->sb_logsectlog = from->sb_logsectlog;
  377. to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
  378. to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
  379. to->sb_features2 = be32_to_cpu(from->sb_features2);
  380. to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
  381. }
  382. /*
  383. * Copy in core superblock to ondisk one.
  384. *
  385. * The fields argument is mask of superblock fields to copy.
  386. */
  387. void
  388. xfs_sb_to_disk(
  389. xfs_dsb_t *to,
  390. xfs_sb_t *from,
  391. __int64_t fields)
  392. {
  393. xfs_caddr_t to_ptr = (xfs_caddr_t)to;
  394. xfs_caddr_t from_ptr = (xfs_caddr_t)from;
  395. xfs_sb_field_t f;
  396. int first;
  397. int size;
  398. ASSERT(fields);
  399. if (!fields)
  400. return;
  401. while (fields) {
  402. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  403. first = xfs_sb_info[f].offset;
  404. size = xfs_sb_info[f + 1].offset - first;
  405. ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
  406. if (size == 1 || xfs_sb_info[f].type == 1) {
  407. memcpy(to_ptr + first, from_ptr + first, size);
  408. } else {
  409. switch (size) {
  410. case 2:
  411. *(__be16 *)(to_ptr + first) =
  412. cpu_to_be16(*(__u16 *)(from_ptr + first));
  413. break;
  414. case 4:
  415. *(__be32 *)(to_ptr + first) =
  416. cpu_to_be32(*(__u32 *)(from_ptr + first));
  417. break;
  418. case 8:
  419. *(__be64 *)(to_ptr + first) =
  420. cpu_to_be64(*(__u64 *)(from_ptr + first));
  421. break;
  422. default:
  423. ASSERT(0);
  424. }
  425. }
  426. fields &= ~(1LL << f);
  427. }
  428. }
  429. /*
  430. * xfs_readsb
  431. *
  432. * Does the initial read of the superblock.
  433. */
  434. int
  435. xfs_readsb(xfs_mount_t *mp, int flags)
  436. {
  437. unsigned int sector_size;
  438. unsigned int extra_flags;
  439. xfs_buf_t *bp;
  440. int error;
  441. ASSERT(mp->m_sb_bp == NULL);
  442. ASSERT(mp->m_ddev_targp != NULL);
  443. /*
  444. * Allocate a (locked) buffer to hold the superblock.
  445. * This will be kept around at all times to optimize
  446. * access to the superblock.
  447. */
  448. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  449. extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
  450. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  451. BTOBB(sector_size), extra_flags);
  452. if (!bp || XFS_BUF_ISERROR(bp)) {
  453. xfs_fs_mount_cmn_err(flags, "SB read failed");
  454. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  455. goto fail;
  456. }
  457. ASSERT(XFS_BUF_ISBUSY(bp));
  458. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  459. /*
  460. * Initialize the mount structure from the superblock.
  461. * But first do some basic consistency checking.
  462. */
  463. xfs_sb_from_disk(&mp->m_sb, XFS_BUF_TO_SBP(bp));
  464. error = xfs_mount_validate_sb(mp, &(mp->m_sb), flags);
  465. if (error) {
  466. xfs_fs_mount_cmn_err(flags, "SB validate failed");
  467. goto fail;
  468. }
  469. /*
  470. * We must be able to do sector-sized and sector-aligned IO.
  471. */
  472. if (sector_size > mp->m_sb.sb_sectsize) {
  473. xfs_fs_mount_cmn_err(flags,
  474. "device supports only %u byte sectors (not %u)",
  475. sector_size, mp->m_sb.sb_sectsize);
  476. error = ENOSYS;
  477. goto fail;
  478. }
  479. /*
  480. * If device sector size is smaller than the superblock size,
  481. * re-read the superblock so the buffer is correctly sized.
  482. */
  483. if (sector_size < mp->m_sb.sb_sectsize) {
  484. XFS_BUF_UNMANAGE(bp);
  485. xfs_buf_relse(bp);
  486. sector_size = mp->m_sb.sb_sectsize;
  487. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  488. BTOBB(sector_size), extra_flags);
  489. if (!bp || XFS_BUF_ISERROR(bp)) {
  490. xfs_fs_mount_cmn_err(flags, "SB re-read failed");
  491. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  492. goto fail;
  493. }
  494. ASSERT(XFS_BUF_ISBUSY(bp));
  495. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  496. }
  497. /* Initialize per-cpu counters */
  498. xfs_icsb_reinit_counters(mp);
  499. mp->m_sb_bp = bp;
  500. xfs_buf_relse(bp);
  501. ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
  502. return 0;
  503. fail:
  504. if (bp) {
  505. XFS_BUF_UNMANAGE(bp);
  506. xfs_buf_relse(bp);
  507. }
  508. return error;
  509. }
  510. /*
  511. * xfs_mount_common
  512. *
  513. * Mount initialization code establishing various mount
  514. * fields from the superblock associated with the given
  515. * mount structure
  516. */
  517. STATIC void
  518. xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
  519. {
  520. mp->m_agfrotor = mp->m_agirotor = 0;
  521. spin_lock_init(&mp->m_agirotor_lock);
  522. mp->m_maxagi = mp->m_sb.sb_agcount;
  523. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  524. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  525. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  526. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  527. mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
  528. mp->m_litino = sbp->sb_inodesize - sizeof(struct xfs_dinode);
  529. mp->m_blockmask = sbp->sb_blocksize - 1;
  530. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  531. mp->m_blockwmask = mp->m_blockwsize - 1;
  532. /*
  533. * Setup for attributes, in case they get created.
  534. * This value is for inodes getting attributes for the first time,
  535. * the per-inode value is for old attribute values.
  536. */
  537. ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
  538. switch (sbp->sb_inodesize) {
  539. case 256:
  540. mp->m_attroffset = XFS_LITINO(mp) -
  541. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  542. break;
  543. case 512:
  544. case 1024:
  545. case 2048:
  546. mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  547. break;
  548. default:
  549. ASSERT(0);
  550. }
  551. ASSERT(mp->m_attroffset < XFS_LITINO(mp));
  552. mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
  553. mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
  554. mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
  555. mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
  556. mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
  557. mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
  558. mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
  559. mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
  560. mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
  561. mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
  562. mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
  563. mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
  564. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  565. mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
  566. sbp->sb_inopblock);
  567. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  568. }
  569. /*
  570. * xfs_initialize_perag_data
  571. *
  572. * Read in each per-ag structure so we can count up the number of
  573. * allocated inodes, free inodes and used filesystem blocks as this
  574. * information is no longer persistent in the superblock. Once we have
  575. * this information, write it into the in-core superblock structure.
  576. */
  577. STATIC int
  578. xfs_initialize_perag_data(xfs_mount_t *mp, xfs_agnumber_t agcount)
  579. {
  580. xfs_agnumber_t index;
  581. xfs_perag_t *pag;
  582. xfs_sb_t *sbp = &mp->m_sb;
  583. uint64_t ifree = 0;
  584. uint64_t ialloc = 0;
  585. uint64_t bfree = 0;
  586. uint64_t bfreelst = 0;
  587. uint64_t btree = 0;
  588. int error;
  589. for (index = 0; index < agcount; index++) {
  590. /*
  591. * read the agf, then the agi. This gets us
  592. * all the inforamtion we need and populates the
  593. * per-ag structures for us.
  594. */
  595. error = xfs_alloc_pagf_init(mp, NULL, index, 0);
  596. if (error)
  597. return error;
  598. error = xfs_ialloc_pagi_init(mp, NULL, index);
  599. if (error)
  600. return error;
  601. pag = &mp->m_perag[index];
  602. ifree += pag->pagi_freecount;
  603. ialloc += pag->pagi_count;
  604. bfree += pag->pagf_freeblks;
  605. bfreelst += pag->pagf_flcount;
  606. btree += pag->pagf_btreeblks;
  607. }
  608. /*
  609. * Overwrite incore superblock counters with just-read data
  610. */
  611. spin_lock(&mp->m_sb_lock);
  612. sbp->sb_ifree = ifree;
  613. sbp->sb_icount = ialloc;
  614. sbp->sb_fdblocks = bfree + bfreelst + btree;
  615. spin_unlock(&mp->m_sb_lock);
  616. /* Fixup the per-cpu counters as well. */
  617. xfs_icsb_reinit_counters(mp);
  618. return 0;
  619. }
  620. /*
  621. * Update alignment values based on mount options and sb values
  622. */
  623. STATIC int
  624. xfs_update_alignment(xfs_mount_t *mp)
  625. {
  626. xfs_sb_t *sbp = &(mp->m_sb);
  627. if (mp->m_dalign) {
  628. /*
  629. * If stripe unit and stripe width are not multiples
  630. * of the fs blocksize turn off alignment.
  631. */
  632. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  633. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  634. if (mp->m_flags & XFS_MOUNT_RETERR) {
  635. cmn_err(CE_WARN,
  636. "XFS: alignment check 1 failed");
  637. return XFS_ERROR(EINVAL);
  638. }
  639. mp->m_dalign = mp->m_swidth = 0;
  640. } else {
  641. /*
  642. * Convert the stripe unit and width to FSBs.
  643. */
  644. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  645. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  646. if (mp->m_flags & XFS_MOUNT_RETERR) {
  647. return XFS_ERROR(EINVAL);
  648. }
  649. xfs_fs_cmn_err(CE_WARN, mp,
  650. "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
  651. mp->m_dalign, mp->m_swidth,
  652. sbp->sb_agblocks);
  653. mp->m_dalign = 0;
  654. mp->m_swidth = 0;
  655. } else if (mp->m_dalign) {
  656. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  657. } else {
  658. if (mp->m_flags & XFS_MOUNT_RETERR) {
  659. xfs_fs_cmn_err(CE_WARN, mp,
  660. "stripe alignment turned off: sunit(%d) less than bsize(%d)",
  661. mp->m_dalign,
  662. mp->m_blockmask +1);
  663. return XFS_ERROR(EINVAL);
  664. }
  665. mp->m_swidth = 0;
  666. }
  667. }
  668. /*
  669. * Update superblock with new values
  670. * and log changes
  671. */
  672. if (xfs_sb_version_hasdalign(sbp)) {
  673. if (sbp->sb_unit != mp->m_dalign) {
  674. sbp->sb_unit = mp->m_dalign;
  675. mp->m_update_flags |= XFS_SB_UNIT;
  676. }
  677. if (sbp->sb_width != mp->m_swidth) {
  678. sbp->sb_width = mp->m_swidth;
  679. mp->m_update_flags |= XFS_SB_WIDTH;
  680. }
  681. }
  682. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  683. xfs_sb_version_hasdalign(&mp->m_sb)) {
  684. mp->m_dalign = sbp->sb_unit;
  685. mp->m_swidth = sbp->sb_width;
  686. }
  687. return 0;
  688. }
  689. /*
  690. * Set the maximum inode count for this filesystem
  691. */
  692. STATIC void
  693. xfs_set_maxicount(xfs_mount_t *mp)
  694. {
  695. xfs_sb_t *sbp = &(mp->m_sb);
  696. __uint64_t icount;
  697. if (sbp->sb_imax_pct) {
  698. /*
  699. * Make sure the maximum inode count is a multiple
  700. * of the units we allocate inodes in.
  701. */
  702. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  703. do_div(icount, 100);
  704. do_div(icount, mp->m_ialloc_blks);
  705. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  706. sbp->sb_inopblog;
  707. } else {
  708. mp->m_maxicount = 0;
  709. }
  710. }
  711. /*
  712. * Set the default minimum read and write sizes unless
  713. * already specified in a mount option.
  714. * We use smaller I/O sizes when the file system
  715. * is being used for NFS service (wsync mount option).
  716. */
  717. STATIC void
  718. xfs_set_rw_sizes(xfs_mount_t *mp)
  719. {
  720. xfs_sb_t *sbp = &(mp->m_sb);
  721. int readio_log, writeio_log;
  722. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  723. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  724. readio_log = XFS_WSYNC_READIO_LOG;
  725. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  726. } else {
  727. readio_log = XFS_READIO_LOG_LARGE;
  728. writeio_log = XFS_WRITEIO_LOG_LARGE;
  729. }
  730. } else {
  731. readio_log = mp->m_readio_log;
  732. writeio_log = mp->m_writeio_log;
  733. }
  734. if (sbp->sb_blocklog > readio_log) {
  735. mp->m_readio_log = sbp->sb_blocklog;
  736. } else {
  737. mp->m_readio_log = readio_log;
  738. }
  739. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  740. if (sbp->sb_blocklog > writeio_log) {
  741. mp->m_writeio_log = sbp->sb_blocklog;
  742. } else {
  743. mp->m_writeio_log = writeio_log;
  744. }
  745. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  746. }
  747. /*
  748. * Set whether we're using inode alignment.
  749. */
  750. STATIC void
  751. xfs_set_inoalignment(xfs_mount_t *mp)
  752. {
  753. if (xfs_sb_version_hasalign(&mp->m_sb) &&
  754. mp->m_sb.sb_inoalignmt >=
  755. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  756. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  757. else
  758. mp->m_inoalign_mask = 0;
  759. /*
  760. * If we are using stripe alignment, check whether
  761. * the stripe unit is a multiple of the inode alignment
  762. */
  763. if (mp->m_dalign && mp->m_inoalign_mask &&
  764. !(mp->m_dalign & mp->m_inoalign_mask))
  765. mp->m_sinoalign = mp->m_dalign;
  766. else
  767. mp->m_sinoalign = 0;
  768. }
  769. /*
  770. * Check that the data (and log if separate) are an ok size.
  771. */
  772. STATIC int
  773. xfs_check_sizes(xfs_mount_t *mp)
  774. {
  775. xfs_buf_t *bp;
  776. xfs_daddr_t d;
  777. int error;
  778. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  779. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  780. cmn_err(CE_WARN, "XFS: size check 1 failed");
  781. return XFS_ERROR(E2BIG);
  782. }
  783. error = xfs_read_buf(mp, mp->m_ddev_targp,
  784. d - XFS_FSS_TO_BB(mp, 1),
  785. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  786. if (!error) {
  787. xfs_buf_relse(bp);
  788. } else {
  789. cmn_err(CE_WARN, "XFS: size check 2 failed");
  790. if (error == ENOSPC)
  791. error = XFS_ERROR(E2BIG);
  792. return error;
  793. }
  794. if (mp->m_logdev_targp != mp->m_ddev_targp) {
  795. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  796. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  797. cmn_err(CE_WARN, "XFS: size check 3 failed");
  798. return XFS_ERROR(E2BIG);
  799. }
  800. error = xfs_read_buf(mp, mp->m_logdev_targp,
  801. d - XFS_FSB_TO_BB(mp, 1),
  802. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  803. if (!error) {
  804. xfs_buf_relse(bp);
  805. } else {
  806. cmn_err(CE_WARN, "XFS: size check 3 failed");
  807. if (error == ENOSPC)
  808. error = XFS_ERROR(E2BIG);
  809. return error;
  810. }
  811. }
  812. return 0;
  813. }
  814. /*
  815. * This function does the following on an initial mount of a file system:
  816. * - reads the superblock from disk and init the mount struct
  817. * - if we're a 32-bit kernel, do a size check on the superblock
  818. * so we don't mount terabyte filesystems
  819. * - init mount struct realtime fields
  820. * - allocate inode hash table for fs
  821. * - init directory manager
  822. * - perform recovery and init the log manager
  823. */
  824. int
  825. xfs_mountfs(
  826. xfs_mount_t *mp)
  827. {
  828. xfs_sb_t *sbp = &(mp->m_sb);
  829. xfs_inode_t *rip;
  830. __uint64_t resblks;
  831. uint quotamount, quotaflags;
  832. int error = 0;
  833. xfs_mount_common(mp, sbp);
  834. /*
  835. * Check for a mismatched features2 values. Older kernels
  836. * read & wrote into the wrong sb offset for sb_features2
  837. * on some platforms due to xfs_sb_t not being 64bit size aligned
  838. * when sb_features2 was added, which made older superblock
  839. * reading/writing routines swap it as a 64-bit value.
  840. *
  841. * For backwards compatibility, we make both slots equal.
  842. *
  843. * If we detect a mismatched field, we OR the set bits into the
  844. * existing features2 field in case it has already been modified; we
  845. * don't want to lose any features. We then update the bad location
  846. * with the ORed value so that older kernels will see any features2
  847. * flags, and mark the two fields as needing updates once the
  848. * transaction subsystem is online.
  849. */
  850. if (xfs_sb_has_mismatched_features2(sbp)) {
  851. cmn_err(CE_WARN,
  852. "XFS: correcting sb_features alignment problem");
  853. sbp->sb_features2 |= sbp->sb_bad_features2;
  854. sbp->sb_bad_features2 = sbp->sb_features2;
  855. mp->m_update_flags |= XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2;
  856. /*
  857. * Re-check for ATTR2 in case it was found in bad_features2
  858. * slot.
  859. */
  860. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  861. !(mp->m_flags & XFS_MOUNT_NOATTR2))
  862. mp->m_flags |= XFS_MOUNT_ATTR2;
  863. }
  864. if (xfs_sb_version_hasattr2(&mp->m_sb) &&
  865. (mp->m_flags & XFS_MOUNT_NOATTR2)) {
  866. xfs_sb_version_removeattr2(&mp->m_sb);
  867. mp->m_update_flags |= XFS_SB_FEATURES2;
  868. /* update sb_versionnum for the clearing of the morebits */
  869. if (!sbp->sb_features2)
  870. mp->m_update_flags |= XFS_SB_VERSIONNUM;
  871. }
  872. /*
  873. * Check if sb_agblocks is aligned at stripe boundary
  874. * If sb_agblocks is NOT aligned turn off m_dalign since
  875. * allocator alignment is within an ag, therefore ag has
  876. * to be aligned at stripe boundary.
  877. */
  878. error = xfs_update_alignment(mp);
  879. if (error)
  880. goto out;
  881. xfs_alloc_compute_maxlevels(mp);
  882. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  883. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  884. xfs_ialloc_compute_maxlevels(mp);
  885. xfs_set_maxicount(mp);
  886. mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
  887. /*
  888. * XFS uses the uuid from the superblock as the unique
  889. * identifier for fsid. We can not use the uuid from the volume
  890. * since a single partition filesystem is identical to a single
  891. * partition volume/filesystem.
  892. */
  893. if (!(mp->m_flags & XFS_MOUNT_NOUUID)) {
  894. if (xfs_uuid_mount(mp)) {
  895. error = XFS_ERROR(EINVAL);
  896. goto out;
  897. }
  898. }
  899. /*
  900. * Set the minimum read and write sizes
  901. */
  902. xfs_set_rw_sizes(mp);
  903. /*
  904. * Set the inode cluster size.
  905. * This may still be overridden by the file system
  906. * block size if it is larger than the chosen cluster size.
  907. */
  908. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  909. /*
  910. * Set inode alignment fields
  911. */
  912. xfs_set_inoalignment(mp);
  913. /*
  914. * Check that the data (and log if separate) are an ok size.
  915. */
  916. error = xfs_check_sizes(mp);
  917. if (error)
  918. goto out_remove_uuid;
  919. /*
  920. * Initialize realtime fields in the mount structure
  921. */
  922. error = xfs_rtmount_init(mp);
  923. if (error) {
  924. cmn_err(CE_WARN, "XFS: RT mount failed");
  925. goto out_remove_uuid;
  926. }
  927. /*
  928. * Copies the low order bits of the timestamp and the randomly
  929. * set "sequence" number out of a UUID.
  930. */
  931. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  932. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  933. xfs_dir_mount(mp);
  934. /*
  935. * Initialize the attribute manager's entries.
  936. */
  937. mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
  938. /*
  939. * Initialize the precomputed transaction reservations values.
  940. */
  941. xfs_trans_init(mp);
  942. /*
  943. * Allocate and initialize the per-ag data.
  944. */
  945. init_rwsem(&mp->m_peraglock);
  946. mp->m_perag = kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t),
  947. KM_MAYFAIL);
  948. if (!mp->m_perag)
  949. goto out_remove_uuid;
  950. mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
  951. if (!sbp->sb_logblocks) {
  952. cmn_err(CE_WARN, "XFS: no log defined");
  953. XFS_ERROR_REPORT("xfs_mountfs", XFS_ERRLEVEL_LOW, mp);
  954. error = XFS_ERROR(EFSCORRUPTED);
  955. goto out_free_perag;
  956. }
  957. /*
  958. * log's mount-time initialization. Perform 1st part recovery if needed
  959. */
  960. error = xfs_log_mount(mp, mp->m_logdev_targp,
  961. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  962. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  963. if (error) {
  964. cmn_err(CE_WARN, "XFS: log mount failed");
  965. goto out_free_perag;
  966. }
  967. /*
  968. * Now the log is mounted, we know if it was an unclean shutdown or
  969. * not. If it was, with the first phase of recovery has completed, we
  970. * have consistent AG blocks on disk. We have not recovered EFIs yet,
  971. * but they are recovered transactionally in the second recovery phase
  972. * later.
  973. *
  974. * Hence we can safely re-initialise incore superblock counters from
  975. * the per-ag data. These may not be correct if the filesystem was not
  976. * cleanly unmounted, so we need to wait for recovery to finish before
  977. * doing this.
  978. *
  979. * If the filesystem was cleanly unmounted, then we can trust the
  980. * values in the superblock to be correct and we don't need to do
  981. * anything here.
  982. *
  983. * If we are currently making the filesystem, the initialisation will
  984. * fail as the perag data is in an undefined state.
  985. */
  986. if (xfs_sb_version_haslazysbcount(&mp->m_sb) &&
  987. !XFS_LAST_UNMOUNT_WAS_CLEAN(mp) &&
  988. !mp->m_sb.sb_inprogress) {
  989. error = xfs_initialize_perag_data(mp, sbp->sb_agcount);
  990. if (error)
  991. goto out_free_perag;
  992. }
  993. /*
  994. * Get and sanity-check the root inode.
  995. * Save the pointer to it in the mount structure.
  996. */
  997. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
  998. if (error) {
  999. cmn_err(CE_WARN, "XFS: failed to read root inode");
  1000. goto out_log_dealloc;
  1001. }
  1002. ASSERT(rip != NULL);
  1003. if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
  1004. cmn_err(CE_WARN, "XFS: corrupted root inode");
  1005. cmn_err(CE_WARN, "Device %s - root %llu is not a directory",
  1006. XFS_BUFTARG_NAME(mp->m_ddev_targp),
  1007. (unsigned long long)rip->i_ino);
  1008. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  1009. XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
  1010. mp);
  1011. error = XFS_ERROR(EFSCORRUPTED);
  1012. goto out_rele_rip;
  1013. }
  1014. mp->m_rootip = rip; /* save it */
  1015. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  1016. /*
  1017. * Initialize realtime inode pointers in the mount structure
  1018. */
  1019. error = xfs_rtmount_inodes(mp);
  1020. if (error) {
  1021. /*
  1022. * Free up the root inode.
  1023. */
  1024. cmn_err(CE_WARN, "XFS: failed to read RT inodes");
  1025. goto out_rele_rip;
  1026. }
  1027. /*
  1028. * If this is a read-only mount defer the superblock updates until
  1029. * the next remount into writeable mode. Otherwise we would never
  1030. * perform the update e.g. for the root filesystem.
  1031. */
  1032. if (mp->m_update_flags && !(mp->m_flags & XFS_MOUNT_RDONLY)) {
  1033. error = xfs_mount_log_sb(mp, mp->m_update_flags);
  1034. if (error) {
  1035. cmn_err(CE_WARN, "XFS: failed to write sb changes");
  1036. goto out_rtunmount;
  1037. }
  1038. }
  1039. /*
  1040. * Initialise the XFS quota management subsystem for this mount
  1041. */
  1042. error = XFS_QM_INIT(mp, &quotamount, &quotaflags);
  1043. if (error)
  1044. goto out_rtunmount;
  1045. /*
  1046. * Finish recovering the file system. This part needed to be
  1047. * delayed until after the root and real-time bitmap inodes
  1048. * were consistently read in.
  1049. */
  1050. error = xfs_log_mount_finish(mp);
  1051. if (error) {
  1052. cmn_err(CE_WARN, "XFS: log mount finish failed");
  1053. goto out_rtunmount;
  1054. }
  1055. /*
  1056. * Complete the quota initialisation, post-log-replay component.
  1057. */
  1058. error = XFS_QM_MOUNT(mp, quotamount, quotaflags);
  1059. if (error)
  1060. goto out_rtunmount;
  1061. /*
  1062. * Now we are mounted, reserve a small amount of unused space for
  1063. * privileged transactions. This is needed so that transaction
  1064. * space required for critical operations can dip into this pool
  1065. * when at ENOSPC. This is needed for operations like create with
  1066. * attr, unwritten extent conversion at ENOSPC, etc. Data allocations
  1067. * are not allowed to use this reserved space.
  1068. *
  1069. * We default to 5% or 1024 fsbs of space reserved, whichever is smaller.
  1070. * This may drive us straight to ENOSPC on mount, but that implies
  1071. * we were already there on the last unmount. Warn if this occurs.
  1072. */
  1073. resblks = mp->m_sb.sb_dblocks;
  1074. do_div(resblks, 20);
  1075. resblks = min_t(__uint64_t, resblks, 1024);
  1076. error = xfs_reserve_blocks(mp, &resblks, NULL);
  1077. if (error)
  1078. cmn_err(CE_WARN, "XFS: Unable to allocate reserve blocks. "
  1079. "Continuing without a reserve pool.");
  1080. return 0;
  1081. out_rtunmount:
  1082. xfs_rtunmount_inodes(mp);
  1083. out_rele_rip:
  1084. IRELE(rip);
  1085. out_log_dealloc:
  1086. xfs_log_unmount(mp);
  1087. out_free_perag:
  1088. xfs_free_perag(mp);
  1089. out_remove_uuid:
  1090. if (!(mp->m_flags & XFS_MOUNT_NOUUID))
  1091. uuid_table_remove(&mp->m_sb.sb_uuid);
  1092. out:
  1093. return error;
  1094. }
  1095. /*
  1096. * This flushes out the inodes,dquots and the superblock, unmounts the
  1097. * log and makes sure that incore structures are freed.
  1098. */
  1099. void
  1100. xfs_unmountfs(
  1101. struct xfs_mount *mp)
  1102. {
  1103. __uint64_t resblks;
  1104. int error;
  1105. /*
  1106. * Release dquot that rootinode, rbmino and rsumino might be holding,
  1107. * and release the quota inodes.
  1108. */
  1109. XFS_QM_UNMOUNT(mp);
  1110. xfs_rtunmount_inodes(mp);
  1111. IRELE(mp->m_rootip);
  1112. /*
  1113. * We can potentially deadlock here if we have an inode cluster
  1114. * that has been freed has it's buffer still pinned in memory because
  1115. * the transaction is still sitting in a iclog. The stale inodes
  1116. * on that buffer will have their flush locks held until the
  1117. * transaction hits the disk and the callbacks run. the inode
  1118. * flush takes the flush lock unconditionally and with nothing to
  1119. * push out the iclog we will never get that unlocked. hence we
  1120. * need to force the log first.
  1121. */
  1122. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1123. xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_ASYNC);
  1124. XFS_QM_DQPURGEALL(mp, XFS_QMOPT_QUOTALL | XFS_QMOPT_UMOUNTING);
  1125. if (mp->m_quotainfo)
  1126. XFS_QM_DONE(mp);
  1127. /*
  1128. * Flush out the log synchronously so that we know for sure
  1129. * that nothing is pinned. This is important because bflush()
  1130. * will skip pinned buffers.
  1131. */
  1132. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  1133. xfs_binval(mp->m_ddev_targp);
  1134. if (mp->m_rtdev_targp) {
  1135. xfs_binval(mp->m_rtdev_targp);
  1136. }
  1137. /*
  1138. * Unreserve any blocks we have so that when we unmount we don't account
  1139. * the reserved free space as used. This is really only necessary for
  1140. * lazy superblock counting because it trusts the incore superblock
  1141. * counters to be aboslutely correct on clean unmount.
  1142. *
  1143. * We don't bother correcting this elsewhere for lazy superblock
  1144. * counting because on mount of an unclean filesystem we reconstruct the
  1145. * correct counter value and this is irrelevant.
  1146. *
  1147. * For non-lazy counter filesystems, this doesn't matter at all because
  1148. * we only every apply deltas to the superblock and hence the incore
  1149. * value does not matter....
  1150. */
  1151. resblks = 0;
  1152. error = xfs_reserve_blocks(mp, &resblks, NULL);
  1153. if (error)
  1154. cmn_err(CE_WARN, "XFS: Unable to free reserved block pool. "
  1155. "Freespace may not be correct on next mount.");
  1156. error = xfs_log_sbcount(mp, 1);
  1157. if (error)
  1158. cmn_err(CE_WARN, "XFS: Unable to update superblock counters. "
  1159. "Freespace may not be correct on next mount.");
  1160. xfs_unmountfs_writesb(mp);
  1161. xfs_unmountfs_wait(mp); /* wait for async bufs */
  1162. xfs_log_unmount_write(mp);
  1163. xfs_log_unmount(mp);
  1164. if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
  1165. uuid_table_remove(&mp->m_sb.sb_uuid);
  1166. #if defined(DEBUG)
  1167. xfs_errortag_clearall(mp, 0);
  1168. #endif
  1169. xfs_free_perag(mp);
  1170. }
  1171. STATIC void
  1172. xfs_unmountfs_wait(xfs_mount_t *mp)
  1173. {
  1174. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1175. xfs_wait_buftarg(mp->m_logdev_targp);
  1176. if (mp->m_rtdev_targp)
  1177. xfs_wait_buftarg(mp->m_rtdev_targp);
  1178. xfs_wait_buftarg(mp->m_ddev_targp);
  1179. }
  1180. int
  1181. xfs_fs_writable(xfs_mount_t *mp)
  1182. {
  1183. return !(xfs_test_for_freeze(mp) || XFS_FORCED_SHUTDOWN(mp) ||
  1184. (mp->m_flags & XFS_MOUNT_RDONLY));
  1185. }
  1186. /*
  1187. * xfs_log_sbcount
  1188. *
  1189. * Called either periodically to keep the on disk superblock values
  1190. * roughly up to date or from unmount to make sure the values are
  1191. * correct on a clean unmount.
  1192. *
  1193. * Note this code can be called during the process of freezing, so
  1194. * we may need to use the transaction allocator which does not not
  1195. * block when the transaction subsystem is in its frozen state.
  1196. */
  1197. int
  1198. xfs_log_sbcount(
  1199. xfs_mount_t *mp,
  1200. uint sync)
  1201. {
  1202. xfs_trans_t *tp;
  1203. int error;
  1204. if (!xfs_fs_writable(mp))
  1205. return 0;
  1206. xfs_icsb_sync_counters(mp, 0);
  1207. /*
  1208. * we don't need to do this if we are updating the superblock
  1209. * counters on every modification.
  1210. */
  1211. if (!xfs_sb_version_haslazysbcount(&mp->m_sb))
  1212. return 0;
  1213. tp = _xfs_trans_alloc(mp, XFS_TRANS_SB_COUNT);
  1214. error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1215. XFS_DEFAULT_LOG_COUNT);
  1216. if (error) {
  1217. xfs_trans_cancel(tp, 0);
  1218. return error;
  1219. }
  1220. xfs_mod_sb(tp, XFS_SB_IFREE | XFS_SB_ICOUNT | XFS_SB_FDBLOCKS);
  1221. if (sync)
  1222. xfs_trans_set_sync(tp);
  1223. error = xfs_trans_commit(tp, 0);
  1224. return error;
  1225. }
  1226. int
  1227. xfs_unmountfs_writesb(xfs_mount_t *mp)
  1228. {
  1229. xfs_buf_t *sbp;
  1230. int error = 0;
  1231. /*
  1232. * skip superblock write if fs is read-only, or
  1233. * if we are doing a forced umount.
  1234. */
  1235. if (!((mp->m_flags & XFS_MOUNT_RDONLY) ||
  1236. XFS_FORCED_SHUTDOWN(mp))) {
  1237. sbp = xfs_getsb(mp, 0);
  1238. XFS_BUF_UNDONE(sbp);
  1239. XFS_BUF_UNREAD(sbp);
  1240. XFS_BUF_UNDELAYWRITE(sbp);
  1241. XFS_BUF_WRITE(sbp);
  1242. XFS_BUF_UNASYNC(sbp);
  1243. ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
  1244. xfsbdstrat(mp, sbp);
  1245. error = xfs_iowait(sbp);
  1246. if (error)
  1247. xfs_ioerror_alert("xfs_unmountfs_writesb",
  1248. mp, sbp, XFS_BUF_ADDR(sbp));
  1249. xfs_buf_relse(sbp);
  1250. }
  1251. return error;
  1252. }
  1253. /*
  1254. * xfs_mod_sb() can be used to copy arbitrary changes to the
  1255. * in-core superblock into the superblock buffer to be logged.
  1256. * It does not provide the higher level of locking that is
  1257. * needed to protect the in-core superblock from concurrent
  1258. * access.
  1259. */
  1260. void
  1261. xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
  1262. {
  1263. xfs_buf_t *bp;
  1264. int first;
  1265. int last;
  1266. xfs_mount_t *mp;
  1267. xfs_sb_field_t f;
  1268. ASSERT(fields);
  1269. if (!fields)
  1270. return;
  1271. mp = tp->t_mountp;
  1272. bp = xfs_trans_getsb(tp, mp, 0);
  1273. first = sizeof(xfs_sb_t);
  1274. last = 0;
  1275. /* translate/copy */
  1276. xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb, fields);
  1277. /* find modified range */
  1278. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  1279. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1280. first = xfs_sb_info[f].offset;
  1281. f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
  1282. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1283. last = xfs_sb_info[f + 1].offset - 1;
  1284. xfs_trans_log_buf(tp, bp, first, last);
  1285. }
  1286. /*
  1287. * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
  1288. * a delta to a specified field in the in-core superblock. Simply
  1289. * switch on the field indicated and apply the delta to that field.
  1290. * Fields are not allowed to dip below zero, so if the delta would
  1291. * do this do not apply it and return EINVAL.
  1292. *
  1293. * The m_sb_lock must be held when this routine is called.
  1294. */
  1295. int
  1296. xfs_mod_incore_sb_unlocked(
  1297. xfs_mount_t *mp,
  1298. xfs_sb_field_t field,
  1299. int64_t delta,
  1300. int rsvd)
  1301. {
  1302. int scounter; /* short counter for 32 bit fields */
  1303. long long lcounter; /* long counter for 64 bit fields */
  1304. long long res_used, rem;
  1305. /*
  1306. * With the in-core superblock spin lock held, switch
  1307. * on the indicated field. Apply the delta to the
  1308. * proper field. If the fields value would dip below
  1309. * 0, then do not apply the delta and return EINVAL.
  1310. */
  1311. switch (field) {
  1312. case XFS_SBS_ICOUNT:
  1313. lcounter = (long long)mp->m_sb.sb_icount;
  1314. lcounter += delta;
  1315. if (lcounter < 0) {
  1316. ASSERT(0);
  1317. return XFS_ERROR(EINVAL);
  1318. }
  1319. mp->m_sb.sb_icount = lcounter;
  1320. return 0;
  1321. case XFS_SBS_IFREE:
  1322. lcounter = (long long)mp->m_sb.sb_ifree;
  1323. lcounter += delta;
  1324. if (lcounter < 0) {
  1325. ASSERT(0);
  1326. return XFS_ERROR(EINVAL);
  1327. }
  1328. mp->m_sb.sb_ifree = lcounter;
  1329. return 0;
  1330. case XFS_SBS_FDBLOCKS:
  1331. lcounter = (long long)
  1332. mp->m_sb.sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  1333. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1334. if (delta > 0) { /* Putting blocks back */
  1335. if (res_used > delta) {
  1336. mp->m_resblks_avail += delta;
  1337. } else {
  1338. rem = delta - res_used;
  1339. mp->m_resblks_avail = mp->m_resblks;
  1340. lcounter += rem;
  1341. }
  1342. } else { /* Taking blocks away */
  1343. lcounter += delta;
  1344. /*
  1345. * If were out of blocks, use any available reserved blocks if
  1346. * were allowed to.
  1347. */
  1348. if (lcounter < 0) {
  1349. if (rsvd) {
  1350. lcounter = (long long)mp->m_resblks_avail + delta;
  1351. if (lcounter < 0) {
  1352. return XFS_ERROR(ENOSPC);
  1353. }
  1354. mp->m_resblks_avail = lcounter;
  1355. return 0;
  1356. } else { /* not reserved */
  1357. return XFS_ERROR(ENOSPC);
  1358. }
  1359. }
  1360. }
  1361. mp->m_sb.sb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  1362. return 0;
  1363. case XFS_SBS_FREXTENTS:
  1364. lcounter = (long long)mp->m_sb.sb_frextents;
  1365. lcounter += delta;
  1366. if (lcounter < 0) {
  1367. return XFS_ERROR(ENOSPC);
  1368. }
  1369. mp->m_sb.sb_frextents = lcounter;
  1370. return 0;
  1371. case XFS_SBS_DBLOCKS:
  1372. lcounter = (long long)mp->m_sb.sb_dblocks;
  1373. lcounter += delta;
  1374. if (lcounter < 0) {
  1375. ASSERT(0);
  1376. return XFS_ERROR(EINVAL);
  1377. }
  1378. mp->m_sb.sb_dblocks = lcounter;
  1379. return 0;
  1380. case XFS_SBS_AGCOUNT:
  1381. scounter = mp->m_sb.sb_agcount;
  1382. scounter += delta;
  1383. if (scounter < 0) {
  1384. ASSERT(0);
  1385. return XFS_ERROR(EINVAL);
  1386. }
  1387. mp->m_sb.sb_agcount = scounter;
  1388. return 0;
  1389. case XFS_SBS_IMAX_PCT:
  1390. scounter = mp->m_sb.sb_imax_pct;
  1391. scounter += delta;
  1392. if (scounter < 0) {
  1393. ASSERT(0);
  1394. return XFS_ERROR(EINVAL);
  1395. }
  1396. mp->m_sb.sb_imax_pct = scounter;
  1397. return 0;
  1398. case XFS_SBS_REXTSIZE:
  1399. scounter = mp->m_sb.sb_rextsize;
  1400. scounter += delta;
  1401. if (scounter < 0) {
  1402. ASSERT(0);
  1403. return XFS_ERROR(EINVAL);
  1404. }
  1405. mp->m_sb.sb_rextsize = scounter;
  1406. return 0;
  1407. case XFS_SBS_RBMBLOCKS:
  1408. scounter = mp->m_sb.sb_rbmblocks;
  1409. scounter += delta;
  1410. if (scounter < 0) {
  1411. ASSERT(0);
  1412. return XFS_ERROR(EINVAL);
  1413. }
  1414. mp->m_sb.sb_rbmblocks = scounter;
  1415. return 0;
  1416. case XFS_SBS_RBLOCKS:
  1417. lcounter = (long long)mp->m_sb.sb_rblocks;
  1418. lcounter += delta;
  1419. if (lcounter < 0) {
  1420. ASSERT(0);
  1421. return XFS_ERROR(EINVAL);
  1422. }
  1423. mp->m_sb.sb_rblocks = lcounter;
  1424. return 0;
  1425. case XFS_SBS_REXTENTS:
  1426. lcounter = (long long)mp->m_sb.sb_rextents;
  1427. lcounter += delta;
  1428. if (lcounter < 0) {
  1429. ASSERT(0);
  1430. return XFS_ERROR(EINVAL);
  1431. }
  1432. mp->m_sb.sb_rextents = lcounter;
  1433. return 0;
  1434. case XFS_SBS_REXTSLOG:
  1435. scounter = mp->m_sb.sb_rextslog;
  1436. scounter += delta;
  1437. if (scounter < 0) {
  1438. ASSERT(0);
  1439. return XFS_ERROR(EINVAL);
  1440. }
  1441. mp->m_sb.sb_rextslog = scounter;
  1442. return 0;
  1443. default:
  1444. ASSERT(0);
  1445. return XFS_ERROR(EINVAL);
  1446. }
  1447. }
  1448. /*
  1449. * xfs_mod_incore_sb() is used to change a field in the in-core
  1450. * superblock structure by the specified delta. This modification
  1451. * is protected by the m_sb_lock. Just use the xfs_mod_incore_sb_unlocked()
  1452. * routine to do the work.
  1453. */
  1454. int
  1455. xfs_mod_incore_sb(
  1456. xfs_mount_t *mp,
  1457. xfs_sb_field_t field,
  1458. int64_t delta,
  1459. int rsvd)
  1460. {
  1461. int status;
  1462. /* check for per-cpu counters */
  1463. switch (field) {
  1464. #ifdef HAVE_PERCPU_SB
  1465. case XFS_SBS_ICOUNT:
  1466. case XFS_SBS_IFREE:
  1467. case XFS_SBS_FDBLOCKS:
  1468. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1469. status = xfs_icsb_modify_counters(mp, field,
  1470. delta, rsvd);
  1471. break;
  1472. }
  1473. /* FALLTHROUGH */
  1474. #endif
  1475. default:
  1476. spin_lock(&mp->m_sb_lock);
  1477. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1478. spin_unlock(&mp->m_sb_lock);
  1479. break;
  1480. }
  1481. return status;
  1482. }
  1483. /*
  1484. * xfs_mod_incore_sb_batch() is used to change more than one field
  1485. * in the in-core superblock structure at a time. This modification
  1486. * is protected by a lock internal to this module. The fields and
  1487. * changes to those fields are specified in the array of xfs_mod_sb
  1488. * structures passed in.
  1489. *
  1490. * Either all of the specified deltas will be applied or none of
  1491. * them will. If any modified field dips below 0, then all modifications
  1492. * will be backed out and EINVAL will be returned.
  1493. */
  1494. int
  1495. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1496. {
  1497. int status=0;
  1498. xfs_mod_sb_t *msbp;
  1499. /*
  1500. * Loop through the array of mod structures and apply each
  1501. * individually. If any fail, then back out all those
  1502. * which have already been applied. Do all of this within
  1503. * the scope of the m_sb_lock so that all of the changes will
  1504. * be atomic.
  1505. */
  1506. spin_lock(&mp->m_sb_lock);
  1507. msbp = &msb[0];
  1508. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1509. /*
  1510. * Apply the delta at index n. If it fails, break
  1511. * from the loop so we'll fall into the undo loop
  1512. * below.
  1513. */
  1514. switch (msbp->msb_field) {
  1515. #ifdef HAVE_PERCPU_SB
  1516. case XFS_SBS_ICOUNT:
  1517. case XFS_SBS_IFREE:
  1518. case XFS_SBS_FDBLOCKS:
  1519. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1520. spin_unlock(&mp->m_sb_lock);
  1521. status = xfs_icsb_modify_counters(mp,
  1522. msbp->msb_field,
  1523. msbp->msb_delta, rsvd);
  1524. spin_lock(&mp->m_sb_lock);
  1525. break;
  1526. }
  1527. /* FALLTHROUGH */
  1528. #endif
  1529. default:
  1530. status = xfs_mod_incore_sb_unlocked(mp,
  1531. msbp->msb_field,
  1532. msbp->msb_delta, rsvd);
  1533. break;
  1534. }
  1535. if (status != 0) {
  1536. break;
  1537. }
  1538. }
  1539. /*
  1540. * If we didn't complete the loop above, then back out
  1541. * any changes made to the superblock. If you add code
  1542. * between the loop above and here, make sure that you
  1543. * preserve the value of status. Loop back until
  1544. * we step below the beginning of the array. Make sure
  1545. * we don't touch anything back there.
  1546. */
  1547. if (status != 0) {
  1548. msbp--;
  1549. while (msbp >= msb) {
  1550. switch (msbp->msb_field) {
  1551. #ifdef HAVE_PERCPU_SB
  1552. case XFS_SBS_ICOUNT:
  1553. case XFS_SBS_IFREE:
  1554. case XFS_SBS_FDBLOCKS:
  1555. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1556. spin_unlock(&mp->m_sb_lock);
  1557. status = xfs_icsb_modify_counters(mp,
  1558. msbp->msb_field,
  1559. -(msbp->msb_delta),
  1560. rsvd);
  1561. spin_lock(&mp->m_sb_lock);
  1562. break;
  1563. }
  1564. /* FALLTHROUGH */
  1565. #endif
  1566. default:
  1567. status = xfs_mod_incore_sb_unlocked(mp,
  1568. msbp->msb_field,
  1569. -(msbp->msb_delta),
  1570. rsvd);
  1571. break;
  1572. }
  1573. ASSERT(status == 0);
  1574. msbp--;
  1575. }
  1576. }
  1577. spin_unlock(&mp->m_sb_lock);
  1578. return status;
  1579. }
  1580. /*
  1581. * xfs_getsb() is called to obtain the buffer for the superblock.
  1582. * The buffer is returned locked and read in from disk.
  1583. * The buffer should be released with a call to xfs_brelse().
  1584. *
  1585. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1586. * the superblock buffer if it can be locked without sleeping.
  1587. * If it can't then we'll return NULL.
  1588. */
  1589. xfs_buf_t *
  1590. xfs_getsb(
  1591. xfs_mount_t *mp,
  1592. int flags)
  1593. {
  1594. xfs_buf_t *bp;
  1595. ASSERT(mp->m_sb_bp != NULL);
  1596. bp = mp->m_sb_bp;
  1597. if (flags & XFS_BUF_TRYLOCK) {
  1598. if (!XFS_BUF_CPSEMA(bp)) {
  1599. return NULL;
  1600. }
  1601. } else {
  1602. XFS_BUF_PSEMA(bp, PRIBIO);
  1603. }
  1604. XFS_BUF_HOLD(bp);
  1605. ASSERT(XFS_BUF_ISDONE(bp));
  1606. return bp;
  1607. }
  1608. /*
  1609. * Used to free the superblock along various error paths.
  1610. */
  1611. void
  1612. xfs_freesb(
  1613. xfs_mount_t *mp)
  1614. {
  1615. xfs_buf_t *bp;
  1616. /*
  1617. * Use xfs_getsb() so that the buffer will be locked
  1618. * when we call xfs_buf_relse().
  1619. */
  1620. bp = xfs_getsb(mp, 0);
  1621. XFS_BUF_UNMANAGE(bp);
  1622. xfs_buf_relse(bp);
  1623. mp->m_sb_bp = NULL;
  1624. }
  1625. /*
  1626. * See if the UUID is unique among mounted XFS filesystems.
  1627. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1628. */
  1629. STATIC int
  1630. xfs_uuid_mount(
  1631. xfs_mount_t *mp)
  1632. {
  1633. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1634. cmn_err(CE_WARN,
  1635. "XFS: Filesystem %s has nil UUID - can't mount",
  1636. mp->m_fsname);
  1637. return -1;
  1638. }
  1639. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1640. cmn_err(CE_WARN,
  1641. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1642. mp->m_fsname);
  1643. return -1;
  1644. }
  1645. return 0;
  1646. }
  1647. /*
  1648. * Used to log changes to the superblock unit and width fields which could
  1649. * be altered by the mount options, as well as any potential sb_features2
  1650. * fixup. Only the first superblock is updated.
  1651. */
  1652. int
  1653. xfs_mount_log_sb(
  1654. xfs_mount_t *mp,
  1655. __int64_t fields)
  1656. {
  1657. xfs_trans_t *tp;
  1658. int error;
  1659. ASSERT(fields & (XFS_SB_UNIT | XFS_SB_WIDTH | XFS_SB_UUID |
  1660. XFS_SB_FEATURES2 | XFS_SB_BAD_FEATURES2 |
  1661. XFS_SB_VERSIONNUM));
  1662. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1663. error = xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1664. XFS_DEFAULT_LOG_COUNT);
  1665. if (error) {
  1666. xfs_trans_cancel(tp, 0);
  1667. return error;
  1668. }
  1669. xfs_mod_sb(tp, fields);
  1670. error = xfs_trans_commit(tp, 0);
  1671. return error;
  1672. }
  1673. #ifdef HAVE_PERCPU_SB
  1674. /*
  1675. * Per-cpu incore superblock counters
  1676. *
  1677. * Simple concept, difficult implementation
  1678. *
  1679. * Basically, replace the incore superblock counters with a distributed per cpu
  1680. * counter for contended fields (e.g. free block count).
  1681. *
  1682. * Difficulties arise in that the incore sb is used for ENOSPC checking, and
  1683. * hence needs to be accurately read when we are running low on space. Hence
  1684. * there is a method to enable and disable the per-cpu counters based on how
  1685. * much "stuff" is available in them.
  1686. *
  1687. * Basically, a counter is enabled if there is enough free resource to justify
  1688. * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
  1689. * ENOSPC), then we disable the counters to synchronise all callers and
  1690. * re-distribute the available resources.
  1691. *
  1692. * If, once we redistributed the available resources, we still get a failure,
  1693. * we disable the per-cpu counter and go through the slow path.
  1694. *
  1695. * The slow path is the current xfs_mod_incore_sb() function. This means that
  1696. * when we disable a per-cpu counter, we need to drain it's resources back to
  1697. * the global superblock. We do this after disabling the counter to prevent
  1698. * more threads from queueing up on the counter.
  1699. *
  1700. * Essentially, this means that we still need a lock in the fast path to enable
  1701. * synchronisation between the global counters and the per-cpu counters. This
  1702. * is not a problem because the lock will be local to a CPU almost all the time
  1703. * and have little contention except when we get to ENOSPC conditions.
  1704. *
  1705. * Basically, this lock becomes a barrier that enables us to lock out the fast
  1706. * path while we do things like enabling and disabling counters and
  1707. * synchronising the counters.
  1708. *
  1709. * Locking rules:
  1710. *
  1711. * 1. m_sb_lock before picking up per-cpu locks
  1712. * 2. per-cpu locks always picked up via for_each_online_cpu() order
  1713. * 3. accurate counter sync requires m_sb_lock + per cpu locks
  1714. * 4. modifying per-cpu counters requires holding per-cpu lock
  1715. * 5. modifying global counters requires holding m_sb_lock
  1716. * 6. enabling or disabling a counter requires holding the m_sb_lock
  1717. * and _none_ of the per-cpu locks.
  1718. *
  1719. * Disabled counters are only ever re-enabled by a balance operation
  1720. * that results in more free resources per CPU than a given threshold.
  1721. * To ensure counters don't remain disabled, they are rebalanced when
  1722. * the global resource goes above a higher threshold (i.e. some hysteresis
  1723. * is present to prevent thrashing).
  1724. */
  1725. #ifdef CONFIG_HOTPLUG_CPU
  1726. /*
  1727. * hot-plug CPU notifier support.
  1728. *
  1729. * We need a notifier per filesystem as we need to be able to identify
  1730. * the filesystem to balance the counters out. This is achieved by
  1731. * having a notifier block embedded in the xfs_mount_t and doing pointer
  1732. * magic to get the mount pointer from the notifier block address.
  1733. */
  1734. STATIC int
  1735. xfs_icsb_cpu_notify(
  1736. struct notifier_block *nfb,
  1737. unsigned long action,
  1738. void *hcpu)
  1739. {
  1740. xfs_icsb_cnts_t *cntp;
  1741. xfs_mount_t *mp;
  1742. mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
  1743. cntp = (xfs_icsb_cnts_t *)
  1744. per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
  1745. switch (action) {
  1746. case CPU_UP_PREPARE:
  1747. case CPU_UP_PREPARE_FROZEN:
  1748. /* Easy Case - initialize the area and locks, and
  1749. * then rebalance when online does everything else for us. */
  1750. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1751. break;
  1752. case CPU_ONLINE:
  1753. case CPU_ONLINE_FROZEN:
  1754. xfs_icsb_lock(mp);
  1755. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1756. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1757. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1758. xfs_icsb_unlock(mp);
  1759. break;
  1760. case CPU_DEAD:
  1761. case CPU_DEAD_FROZEN:
  1762. /* Disable all the counters, then fold the dead cpu's
  1763. * count into the total on the global superblock and
  1764. * re-enable the counters. */
  1765. xfs_icsb_lock(mp);
  1766. spin_lock(&mp->m_sb_lock);
  1767. xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
  1768. xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
  1769. xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
  1770. mp->m_sb.sb_icount += cntp->icsb_icount;
  1771. mp->m_sb.sb_ifree += cntp->icsb_ifree;
  1772. mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
  1773. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1774. xfs_icsb_balance_counter_locked(mp, XFS_SBS_ICOUNT, 0);
  1775. xfs_icsb_balance_counter_locked(mp, XFS_SBS_IFREE, 0);
  1776. xfs_icsb_balance_counter_locked(mp, XFS_SBS_FDBLOCKS, 0);
  1777. spin_unlock(&mp->m_sb_lock);
  1778. xfs_icsb_unlock(mp);
  1779. break;
  1780. }
  1781. return NOTIFY_OK;
  1782. }
  1783. #endif /* CONFIG_HOTPLUG_CPU */
  1784. int
  1785. xfs_icsb_init_counters(
  1786. xfs_mount_t *mp)
  1787. {
  1788. xfs_icsb_cnts_t *cntp;
  1789. int i;
  1790. mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
  1791. if (mp->m_sb_cnts == NULL)
  1792. return -ENOMEM;
  1793. #ifdef CONFIG_HOTPLUG_CPU
  1794. mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
  1795. mp->m_icsb_notifier.priority = 0;
  1796. register_hotcpu_notifier(&mp->m_icsb_notifier);
  1797. #endif /* CONFIG_HOTPLUG_CPU */
  1798. for_each_online_cpu(i) {
  1799. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1800. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1801. }
  1802. mutex_init(&mp->m_icsb_mutex);
  1803. /*
  1804. * start with all counters disabled so that the
  1805. * initial balance kicks us off correctly
  1806. */
  1807. mp->m_icsb_counters = -1;
  1808. return 0;
  1809. }
  1810. void
  1811. xfs_icsb_reinit_counters(
  1812. xfs_mount_t *mp)
  1813. {
  1814. xfs_icsb_lock(mp);
  1815. /*
  1816. * start with all counters disabled so that the
  1817. * initial balance kicks us off correctly
  1818. */
  1819. mp->m_icsb_counters = -1;
  1820. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1821. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1822. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1823. xfs_icsb_unlock(mp);
  1824. }
  1825. void
  1826. xfs_icsb_destroy_counters(
  1827. xfs_mount_t *mp)
  1828. {
  1829. if (mp->m_sb_cnts) {
  1830. unregister_hotcpu_notifier(&mp->m_icsb_notifier);
  1831. free_percpu(mp->m_sb_cnts);
  1832. }
  1833. mutex_destroy(&mp->m_icsb_mutex);
  1834. }
  1835. STATIC_INLINE void
  1836. xfs_icsb_lock_cntr(
  1837. xfs_icsb_cnts_t *icsbp)
  1838. {
  1839. while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
  1840. ndelay(1000);
  1841. }
  1842. }
  1843. STATIC_INLINE void
  1844. xfs_icsb_unlock_cntr(
  1845. xfs_icsb_cnts_t *icsbp)
  1846. {
  1847. clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
  1848. }
  1849. STATIC_INLINE void
  1850. xfs_icsb_lock_all_counters(
  1851. xfs_mount_t *mp)
  1852. {
  1853. xfs_icsb_cnts_t *cntp;
  1854. int i;
  1855. for_each_online_cpu(i) {
  1856. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1857. xfs_icsb_lock_cntr(cntp);
  1858. }
  1859. }
  1860. STATIC_INLINE void
  1861. xfs_icsb_unlock_all_counters(
  1862. xfs_mount_t *mp)
  1863. {
  1864. xfs_icsb_cnts_t *cntp;
  1865. int i;
  1866. for_each_online_cpu(i) {
  1867. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1868. xfs_icsb_unlock_cntr(cntp);
  1869. }
  1870. }
  1871. STATIC void
  1872. xfs_icsb_count(
  1873. xfs_mount_t *mp,
  1874. xfs_icsb_cnts_t *cnt,
  1875. int flags)
  1876. {
  1877. xfs_icsb_cnts_t *cntp;
  1878. int i;
  1879. memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
  1880. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1881. xfs_icsb_lock_all_counters(mp);
  1882. for_each_online_cpu(i) {
  1883. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1884. cnt->icsb_icount += cntp->icsb_icount;
  1885. cnt->icsb_ifree += cntp->icsb_ifree;
  1886. cnt->icsb_fdblocks += cntp->icsb_fdblocks;
  1887. }
  1888. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1889. xfs_icsb_unlock_all_counters(mp);
  1890. }
  1891. STATIC int
  1892. xfs_icsb_counter_disabled(
  1893. xfs_mount_t *mp,
  1894. xfs_sb_field_t field)
  1895. {
  1896. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1897. return test_bit(field, &mp->m_icsb_counters);
  1898. }
  1899. STATIC void
  1900. xfs_icsb_disable_counter(
  1901. xfs_mount_t *mp,
  1902. xfs_sb_field_t field)
  1903. {
  1904. xfs_icsb_cnts_t cnt;
  1905. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1906. /*
  1907. * If we are already disabled, then there is nothing to do
  1908. * here. We check before locking all the counters to avoid
  1909. * the expensive lock operation when being called in the
  1910. * slow path and the counter is already disabled. This is
  1911. * safe because the only time we set or clear this state is under
  1912. * the m_icsb_mutex.
  1913. */
  1914. if (xfs_icsb_counter_disabled(mp, field))
  1915. return;
  1916. xfs_icsb_lock_all_counters(mp);
  1917. if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
  1918. /* drain back to superblock */
  1919. xfs_icsb_count(mp, &cnt, XFS_ICSB_LAZY_COUNT);
  1920. switch(field) {
  1921. case XFS_SBS_ICOUNT:
  1922. mp->m_sb.sb_icount = cnt.icsb_icount;
  1923. break;
  1924. case XFS_SBS_IFREE:
  1925. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1926. break;
  1927. case XFS_SBS_FDBLOCKS:
  1928. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1929. break;
  1930. default:
  1931. BUG();
  1932. }
  1933. }
  1934. xfs_icsb_unlock_all_counters(mp);
  1935. }
  1936. STATIC void
  1937. xfs_icsb_enable_counter(
  1938. xfs_mount_t *mp,
  1939. xfs_sb_field_t field,
  1940. uint64_t count,
  1941. uint64_t resid)
  1942. {
  1943. xfs_icsb_cnts_t *cntp;
  1944. int i;
  1945. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1946. xfs_icsb_lock_all_counters(mp);
  1947. for_each_online_cpu(i) {
  1948. cntp = per_cpu_ptr(mp->m_sb_cnts, i);
  1949. switch (field) {
  1950. case XFS_SBS_ICOUNT:
  1951. cntp->icsb_icount = count + resid;
  1952. break;
  1953. case XFS_SBS_IFREE:
  1954. cntp->icsb_ifree = count + resid;
  1955. break;
  1956. case XFS_SBS_FDBLOCKS:
  1957. cntp->icsb_fdblocks = count + resid;
  1958. break;
  1959. default:
  1960. BUG();
  1961. break;
  1962. }
  1963. resid = 0;
  1964. }
  1965. clear_bit(field, &mp->m_icsb_counters);
  1966. xfs_icsb_unlock_all_counters(mp);
  1967. }
  1968. void
  1969. xfs_icsb_sync_counters_locked(
  1970. xfs_mount_t *mp,
  1971. int flags)
  1972. {
  1973. xfs_icsb_cnts_t cnt;
  1974. xfs_icsb_count(mp, &cnt, flags);
  1975. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
  1976. mp->m_sb.sb_icount = cnt.icsb_icount;
  1977. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
  1978. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1979. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
  1980. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1981. }
  1982. /*
  1983. * Accurate update of per-cpu counters to incore superblock
  1984. */
  1985. void
  1986. xfs_icsb_sync_counters(
  1987. xfs_mount_t *mp,
  1988. int flags)
  1989. {
  1990. spin_lock(&mp->m_sb_lock);
  1991. xfs_icsb_sync_counters_locked(mp, flags);
  1992. spin_unlock(&mp->m_sb_lock);
  1993. }
  1994. /*
  1995. * Balance and enable/disable counters as necessary.
  1996. *
  1997. * Thresholds for re-enabling counters are somewhat magic. inode counts are
  1998. * chosen to be the same number as single on disk allocation chunk per CPU, and
  1999. * free blocks is something far enough zero that we aren't going thrash when we
  2000. * get near ENOSPC. We also need to supply a minimum we require per cpu to
  2001. * prevent looping endlessly when xfs_alloc_space asks for more than will
  2002. * be distributed to a single CPU but each CPU has enough blocks to be
  2003. * reenabled.
  2004. *
  2005. * Note that we can be called when counters are already disabled.
  2006. * xfs_icsb_disable_counter() optimises the counter locking in this case to
  2007. * prevent locking every per-cpu counter needlessly.
  2008. */
  2009. #define XFS_ICSB_INO_CNTR_REENABLE (uint64_t)64
  2010. #define XFS_ICSB_FDBLK_CNTR_REENABLE(mp) \
  2011. (uint64_t)(512 + XFS_ALLOC_SET_ASIDE(mp))
  2012. STATIC void
  2013. xfs_icsb_balance_counter_locked(
  2014. xfs_mount_t *mp,
  2015. xfs_sb_field_t field,
  2016. int min_per_cpu)
  2017. {
  2018. uint64_t count, resid;
  2019. int weight = num_online_cpus();
  2020. uint64_t min = (uint64_t)min_per_cpu;
  2021. /* disable counter and sync counter */
  2022. xfs_icsb_disable_counter(mp, field);
  2023. /* update counters - first CPU gets residual*/
  2024. switch (field) {
  2025. case XFS_SBS_ICOUNT:
  2026. count = mp->m_sb.sb_icount;
  2027. resid = do_div(count, weight);
  2028. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  2029. return;
  2030. break;
  2031. case XFS_SBS_IFREE:
  2032. count = mp->m_sb.sb_ifree;
  2033. resid = do_div(count, weight);
  2034. if (count < max(min, XFS_ICSB_INO_CNTR_REENABLE))
  2035. return;
  2036. break;
  2037. case XFS_SBS_FDBLOCKS:
  2038. count = mp->m_sb.sb_fdblocks;
  2039. resid = do_div(count, weight);
  2040. if (count < max(min, XFS_ICSB_FDBLK_CNTR_REENABLE(mp)))
  2041. return;
  2042. break;
  2043. default:
  2044. BUG();
  2045. count = resid = 0; /* quiet, gcc */
  2046. break;
  2047. }
  2048. xfs_icsb_enable_counter(mp, field, count, resid);
  2049. }
  2050. STATIC void
  2051. xfs_icsb_balance_counter(
  2052. xfs_mount_t *mp,
  2053. xfs_sb_field_t fields,
  2054. int min_per_cpu)
  2055. {
  2056. spin_lock(&mp->m_sb_lock);
  2057. xfs_icsb_balance_counter_locked(mp, fields, min_per_cpu);
  2058. spin_unlock(&mp->m_sb_lock);
  2059. }
  2060. STATIC int
  2061. xfs_icsb_modify_counters(
  2062. xfs_mount_t *mp,
  2063. xfs_sb_field_t field,
  2064. int64_t delta,
  2065. int rsvd)
  2066. {
  2067. xfs_icsb_cnts_t *icsbp;
  2068. long long lcounter; /* long counter for 64 bit fields */
  2069. int cpu, ret = 0;
  2070. might_sleep();
  2071. again:
  2072. cpu = get_cpu();
  2073. icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu);
  2074. /*
  2075. * if the counter is disabled, go to slow path
  2076. */
  2077. if (unlikely(xfs_icsb_counter_disabled(mp, field)))
  2078. goto slow_path;
  2079. xfs_icsb_lock_cntr(icsbp);
  2080. if (unlikely(xfs_icsb_counter_disabled(mp, field))) {
  2081. xfs_icsb_unlock_cntr(icsbp);
  2082. goto slow_path;
  2083. }
  2084. switch (field) {
  2085. case XFS_SBS_ICOUNT:
  2086. lcounter = icsbp->icsb_icount;
  2087. lcounter += delta;
  2088. if (unlikely(lcounter < 0))
  2089. goto balance_counter;
  2090. icsbp->icsb_icount = lcounter;
  2091. break;
  2092. case XFS_SBS_IFREE:
  2093. lcounter = icsbp->icsb_ifree;
  2094. lcounter += delta;
  2095. if (unlikely(lcounter < 0))
  2096. goto balance_counter;
  2097. icsbp->icsb_ifree = lcounter;
  2098. break;
  2099. case XFS_SBS_FDBLOCKS:
  2100. BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
  2101. lcounter = icsbp->icsb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
  2102. lcounter += delta;
  2103. if (unlikely(lcounter < 0))
  2104. goto balance_counter;
  2105. icsbp->icsb_fdblocks = lcounter + XFS_ALLOC_SET_ASIDE(mp);
  2106. break;
  2107. default:
  2108. BUG();
  2109. break;
  2110. }
  2111. xfs_icsb_unlock_cntr(icsbp);
  2112. put_cpu();
  2113. return 0;
  2114. slow_path:
  2115. put_cpu();
  2116. /*
  2117. * serialise with a mutex so we don't burn lots of cpu on
  2118. * the superblock lock. We still need to hold the superblock
  2119. * lock, however, when we modify the global structures.
  2120. */
  2121. xfs_icsb_lock(mp);
  2122. /*
  2123. * Now running atomically.
  2124. *
  2125. * If the counter is enabled, someone has beaten us to rebalancing.
  2126. * Drop the lock and try again in the fast path....
  2127. */
  2128. if (!(xfs_icsb_counter_disabled(mp, field))) {
  2129. xfs_icsb_unlock(mp);
  2130. goto again;
  2131. }
  2132. /*
  2133. * The counter is currently disabled. Because we are
  2134. * running atomically here, we know a rebalance cannot
  2135. * be in progress. Hence we can go straight to operating
  2136. * on the global superblock. We do not call xfs_mod_incore_sb()
  2137. * here even though we need to get the m_sb_lock. Doing so
  2138. * will cause us to re-enter this function and deadlock.
  2139. * Hence we get the m_sb_lock ourselves and then call
  2140. * xfs_mod_incore_sb_unlocked() as the unlocked path operates
  2141. * directly on the global counters.
  2142. */
  2143. spin_lock(&mp->m_sb_lock);
  2144. ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  2145. spin_unlock(&mp->m_sb_lock);
  2146. /*
  2147. * Now that we've modified the global superblock, we
  2148. * may be able to re-enable the distributed counters
  2149. * (e.g. lots of space just got freed). After that
  2150. * we are done.
  2151. */
  2152. if (ret != ENOSPC)
  2153. xfs_icsb_balance_counter(mp, field, 0);
  2154. xfs_icsb_unlock(mp);
  2155. return ret;
  2156. balance_counter:
  2157. xfs_icsb_unlock_cntr(icsbp);
  2158. put_cpu();
  2159. /*
  2160. * We may have multiple threads here if multiple per-cpu
  2161. * counters run dry at the same time. This will mean we can
  2162. * do more balances than strictly necessary but it is not
  2163. * the common slowpath case.
  2164. */
  2165. xfs_icsb_lock(mp);
  2166. /*
  2167. * running atomically.
  2168. *
  2169. * This will leave the counter in the correct state for future
  2170. * accesses. After the rebalance, we simply try again and our retry
  2171. * will either succeed through the fast path or slow path without
  2172. * another balance operation being required.
  2173. */
  2174. xfs_icsb_balance_counter(mp, field, delta);
  2175. xfs_icsb_unlock(mp);
  2176. goto again;
  2177. }
  2178. #endif