xfs_mount.c 66 KB

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