xfs_mount.c 66 KB

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