xfs_mount.c 66 KB

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