xfs_mount.c 55 KB

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