xfs_mount.c 63 KB

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