xfs_mount.c 63 KB

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