xfs_mount.c 64 KB

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