xfs_mount.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193
  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. * In order to avoid ENOSPC-related deadlock caused by
  1120. * out-of-order locking of AGF buffer (PV 947395), we place
  1121. * constraints on the relationship among actual allocations for
  1122. * data blocks, freelist blocks, and potential file data bmap
  1123. * btree blocks. However, these restrictions may result in no
  1124. * actual space allocated for a delayed extent, for example, a data
  1125. * block in a certain AG is allocated but there is no additional
  1126. * block for the additional bmap btree block due to a split of the
  1127. * bmap btree of the file. The result of this may lead to an
  1128. * infinite loop in xfssyncd when the file gets flushed to disk and
  1129. * all delayed extents need to be actually allocated. To get around
  1130. * this, we explicitly set aside a few blocks which will not be
  1131. * reserved in delayed allocation. Considering the minimum number of
  1132. * needed freelist blocks is 4 fsbs, a potential split of file's bmap
  1133. * btree requires 1 fsb, so we set the number of set-aside blocks to 8.
  1134. */
  1135. #define SET_ASIDE_BLOCKS 8
  1136. /*
  1137. * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
  1138. * a delta to a specified field in the in-core superblock. Simply
  1139. * switch on the field indicated and apply the delta to that field.
  1140. * Fields are not allowed to dip below zero, so if the delta would
  1141. * do this do not apply it and return EINVAL.
  1142. *
  1143. * The SB_LOCK must be held when this routine is called.
  1144. */
  1145. int
  1146. xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
  1147. int delta, int rsvd)
  1148. {
  1149. int scounter; /* short counter for 32 bit fields */
  1150. long long lcounter; /* long counter for 64 bit fields */
  1151. long long res_used, rem;
  1152. /*
  1153. * With the in-core superblock spin lock held, switch
  1154. * on the indicated field. Apply the delta to the
  1155. * proper field. If the fields value would dip below
  1156. * 0, then do not apply the delta and return EINVAL.
  1157. */
  1158. switch (field) {
  1159. case XFS_SBS_ICOUNT:
  1160. lcounter = (long long)mp->m_sb.sb_icount;
  1161. lcounter += delta;
  1162. if (lcounter < 0) {
  1163. ASSERT(0);
  1164. return XFS_ERROR(EINVAL);
  1165. }
  1166. mp->m_sb.sb_icount = lcounter;
  1167. return 0;
  1168. case XFS_SBS_IFREE:
  1169. lcounter = (long long)mp->m_sb.sb_ifree;
  1170. lcounter += delta;
  1171. if (lcounter < 0) {
  1172. ASSERT(0);
  1173. return XFS_ERROR(EINVAL);
  1174. }
  1175. mp->m_sb.sb_ifree = lcounter;
  1176. return 0;
  1177. case XFS_SBS_FDBLOCKS:
  1178. lcounter = (long long)mp->m_sb.sb_fdblocks - SET_ASIDE_BLOCKS;
  1179. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1180. if (delta > 0) { /* Putting blocks back */
  1181. if (res_used > delta) {
  1182. mp->m_resblks_avail += delta;
  1183. } else {
  1184. rem = delta - res_used;
  1185. mp->m_resblks_avail = mp->m_resblks;
  1186. lcounter += rem;
  1187. }
  1188. } else { /* Taking blocks away */
  1189. lcounter += delta;
  1190. /*
  1191. * If were out of blocks, use any available reserved blocks if
  1192. * were allowed to.
  1193. */
  1194. if (lcounter < 0) {
  1195. if (rsvd) {
  1196. lcounter = (long long)mp->m_resblks_avail + delta;
  1197. if (lcounter < 0) {
  1198. return XFS_ERROR(ENOSPC);
  1199. }
  1200. mp->m_resblks_avail = lcounter;
  1201. return 0;
  1202. } else { /* not reserved */
  1203. return XFS_ERROR(ENOSPC);
  1204. }
  1205. }
  1206. }
  1207. mp->m_sb.sb_fdblocks = lcounter + SET_ASIDE_BLOCKS;
  1208. return 0;
  1209. case XFS_SBS_FREXTENTS:
  1210. lcounter = (long long)mp->m_sb.sb_frextents;
  1211. lcounter += delta;
  1212. if (lcounter < 0) {
  1213. return XFS_ERROR(ENOSPC);
  1214. }
  1215. mp->m_sb.sb_frextents = lcounter;
  1216. return 0;
  1217. case XFS_SBS_DBLOCKS:
  1218. lcounter = (long long)mp->m_sb.sb_dblocks;
  1219. lcounter += delta;
  1220. if (lcounter < 0) {
  1221. ASSERT(0);
  1222. return XFS_ERROR(EINVAL);
  1223. }
  1224. mp->m_sb.sb_dblocks = lcounter;
  1225. return 0;
  1226. case XFS_SBS_AGCOUNT:
  1227. scounter = mp->m_sb.sb_agcount;
  1228. scounter += delta;
  1229. if (scounter < 0) {
  1230. ASSERT(0);
  1231. return XFS_ERROR(EINVAL);
  1232. }
  1233. mp->m_sb.sb_agcount = scounter;
  1234. return 0;
  1235. case XFS_SBS_IMAX_PCT:
  1236. scounter = mp->m_sb.sb_imax_pct;
  1237. scounter += delta;
  1238. if (scounter < 0) {
  1239. ASSERT(0);
  1240. return XFS_ERROR(EINVAL);
  1241. }
  1242. mp->m_sb.sb_imax_pct = scounter;
  1243. return 0;
  1244. case XFS_SBS_REXTSIZE:
  1245. scounter = mp->m_sb.sb_rextsize;
  1246. scounter += delta;
  1247. if (scounter < 0) {
  1248. ASSERT(0);
  1249. return XFS_ERROR(EINVAL);
  1250. }
  1251. mp->m_sb.sb_rextsize = scounter;
  1252. return 0;
  1253. case XFS_SBS_RBMBLOCKS:
  1254. scounter = mp->m_sb.sb_rbmblocks;
  1255. scounter += delta;
  1256. if (scounter < 0) {
  1257. ASSERT(0);
  1258. return XFS_ERROR(EINVAL);
  1259. }
  1260. mp->m_sb.sb_rbmblocks = scounter;
  1261. return 0;
  1262. case XFS_SBS_RBLOCKS:
  1263. lcounter = (long long)mp->m_sb.sb_rblocks;
  1264. lcounter += delta;
  1265. if (lcounter < 0) {
  1266. ASSERT(0);
  1267. return XFS_ERROR(EINVAL);
  1268. }
  1269. mp->m_sb.sb_rblocks = lcounter;
  1270. return 0;
  1271. case XFS_SBS_REXTENTS:
  1272. lcounter = (long long)mp->m_sb.sb_rextents;
  1273. lcounter += delta;
  1274. if (lcounter < 0) {
  1275. ASSERT(0);
  1276. return XFS_ERROR(EINVAL);
  1277. }
  1278. mp->m_sb.sb_rextents = lcounter;
  1279. return 0;
  1280. case XFS_SBS_REXTSLOG:
  1281. scounter = mp->m_sb.sb_rextslog;
  1282. scounter += delta;
  1283. if (scounter < 0) {
  1284. ASSERT(0);
  1285. return XFS_ERROR(EINVAL);
  1286. }
  1287. mp->m_sb.sb_rextslog = scounter;
  1288. return 0;
  1289. default:
  1290. ASSERT(0);
  1291. return XFS_ERROR(EINVAL);
  1292. }
  1293. }
  1294. /*
  1295. * xfs_mod_incore_sb() is used to change a field in the in-core
  1296. * superblock structure by the specified delta. This modification
  1297. * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
  1298. * routine to do the work.
  1299. */
  1300. int
  1301. xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
  1302. {
  1303. unsigned long s;
  1304. int status;
  1305. /* check for per-cpu counters */
  1306. switch (field) {
  1307. #ifdef HAVE_PERCPU_SB
  1308. case XFS_SBS_ICOUNT:
  1309. case XFS_SBS_IFREE:
  1310. case XFS_SBS_FDBLOCKS:
  1311. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1312. status = xfs_icsb_modify_counters(mp, field,
  1313. delta, rsvd);
  1314. break;
  1315. }
  1316. /* FALLTHROUGH */
  1317. #endif
  1318. default:
  1319. s = XFS_SB_LOCK(mp);
  1320. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1321. XFS_SB_UNLOCK(mp, s);
  1322. break;
  1323. }
  1324. return status;
  1325. }
  1326. /*
  1327. * xfs_mod_incore_sb_batch() is used to change more than one field
  1328. * in the in-core superblock structure at a time. This modification
  1329. * is protected by a lock internal to this module. The fields and
  1330. * changes to those fields are specified in the array of xfs_mod_sb
  1331. * structures passed in.
  1332. *
  1333. * Either all of the specified deltas will be applied or none of
  1334. * them will. If any modified field dips below 0, then all modifications
  1335. * will be backed out and EINVAL will be returned.
  1336. */
  1337. int
  1338. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1339. {
  1340. unsigned long s;
  1341. int status=0;
  1342. xfs_mod_sb_t *msbp;
  1343. /*
  1344. * Loop through the array of mod structures and apply each
  1345. * individually. If any fail, then back out all those
  1346. * which have already been applied. Do all of this within
  1347. * the scope of the SB_LOCK so that all of the changes will
  1348. * be atomic.
  1349. */
  1350. s = XFS_SB_LOCK(mp);
  1351. msbp = &msb[0];
  1352. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1353. /*
  1354. * Apply the delta at index n. If it fails, break
  1355. * from the loop so we'll fall into the undo loop
  1356. * below.
  1357. */
  1358. switch (msbp->msb_field) {
  1359. #ifdef HAVE_PERCPU_SB
  1360. case XFS_SBS_ICOUNT:
  1361. case XFS_SBS_IFREE:
  1362. case XFS_SBS_FDBLOCKS:
  1363. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1364. status = xfs_icsb_modify_counters_locked(mp,
  1365. msbp->msb_field,
  1366. msbp->msb_delta, rsvd);
  1367. break;
  1368. }
  1369. /* FALLTHROUGH */
  1370. #endif
  1371. default:
  1372. status = xfs_mod_incore_sb_unlocked(mp,
  1373. msbp->msb_field,
  1374. msbp->msb_delta, rsvd);
  1375. break;
  1376. }
  1377. if (status != 0) {
  1378. break;
  1379. }
  1380. }
  1381. /*
  1382. * If we didn't complete the loop above, then back out
  1383. * any changes made to the superblock. If you add code
  1384. * between the loop above and here, make sure that you
  1385. * preserve the value of status. Loop back until
  1386. * we step below the beginning of the array. Make sure
  1387. * we don't touch anything back there.
  1388. */
  1389. if (status != 0) {
  1390. msbp--;
  1391. while (msbp >= msb) {
  1392. switch (msbp->msb_field) {
  1393. #ifdef HAVE_PERCPU_SB
  1394. case XFS_SBS_ICOUNT:
  1395. case XFS_SBS_IFREE:
  1396. case XFS_SBS_FDBLOCKS:
  1397. if (!(mp->m_flags & XFS_MOUNT_NO_PERCPU_SB)) {
  1398. status =
  1399. xfs_icsb_modify_counters_locked(mp,
  1400. msbp->msb_field,
  1401. -(msbp->msb_delta),
  1402. rsvd);
  1403. break;
  1404. }
  1405. /* FALLTHROUGH */
  1406. #endif
  1407. default:
  1408. status = xfs_mod_incore_sb_unlocked(mp,
  1409. msbp->msb_field,
  1410. -(msbp->msb_delta),
  1411. rsvd);
  1412. break;
  1413. }
  1414. ASSERT(status == 0);
  1415. msbp--;
  1416. }
  1417. }
  1418. XFS_SB_UNLOCK(mp, s);
  1419. return status;
  1420. }
  1421. /*
  1422. * xfs_getsb() is called to obtain the buffer for the superblock.
  1423. * The buffer is returned locked and read in from disk.
  1424. * The buffer should be released with a call to xfs_brelse().
  1425. *
  1426. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1427. * the superblock buffer if it can be locked without sleeping.
  1428. * If it can't then we'll return NULL.
  1429. */
  1430. xfs_buf_t *
  1431. xfs_getsb(
  1432. xfs_mount_t *mp,
  1433. int flags)
  1434. {
  1435. xfs_buf_t *bp;
  1436. ASSERT(mp->m_sb_bp != NULL);
  1437. bp = mp->m_sb_bp;
  1438. if (flags & XFS_BUF_TRYLOCK) {
  1439. if (!XFS_BUF_CPSEMA(bp)) {
  1440. return NULL;
  1441. }
  1442. } else {
  1443. XFS_BUF_PSEMA(bp, PRIBIO);
  1444. }
  1445. XFS_BUF_HOLD(bp);
  1446. ASSERT(XFS_BUF_ISDONE(bp));
  1447. return bp;
  1448. }
  1449. /*
  1450. * Used to free the superblock along various error paths.
  1451. */
  1452. void
  1453. xfs_freesb(
  1454. xfs_mount_t *mp)
  1455. {
  1456. xfs_buf_t *bp;
  1457. /*
  1458. * Use xfs_getsb() so that the buffer will be locked
  1459. * when we call xfs_buf_relse().
  1460. */
  1461. bp = xfs_getsb(mp, 0);
  1462. XFS_BUF_UNMANAGE(bp);
  1463. xfs_buf_relse(bp);
  1464. mp->m_sb_bp = NULL;
  1465. }
  1466. /*
  1467. * See if the UUID is unique among mounted XFS filesystems.
  1468. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1469. */
  1470. STATIC int
  1471. xfs_uuid_mount(
  1472. xfs_mount_t *mp)
  1473. {
  1474. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1475. cmn_err(CE_WARN,
  1476. "XFS: Filesystem %s has nil UUID - can't mount",
  1477. mp->m_fsname);
  1478. return -1;
  1479. }
  1480. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1481. cmn_err(CE_WARN,
  1482. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1483. mp->m_fsname);
  1484. return -1;
  1485. }
  1486. return 0;
  1487. }
  1488. /*
  1489. * Remove filesystem from the UUID table.
  1490. */
  1491. STATIC void
  1492. xfs_uuid_unmount(
  1493. xfs_mount_t *mp)
  1494. {
  1495. uuid_table_remove(&mp->m_sb.sb_uuid);
  1496. }
  1497. /*
  1498. * Used to log changes to the superblock unit and width fields which could
  1499. * be altered by the mount options. Only the first superblock is updated.
  1500. */
  1501. STATIC void
  1502. xfs_mount_log_sbunit(
  1503. xfs_mount_t *mp,
  1504. __int64_t fields)
  1505. {
  1506. xfs_trans_t *tp;
  1507. ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
  1508. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1509. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1510. XFS_DEFAULT_LOG_COUNT)) {
  1511. xfs_trans_cancel(tp, 0);
  1512. return;
  1513. }
  1514. xfs_mod_sb(tp, fields);
  1515. xfs_trans_commit(tp, 0, NULL);
  1516. }
  1517. #ifdef HAVE_PERCPU_SB
  1518. /*
  1519. * Per-cpu incore superblock counters
  1520. *
  1521. * Simple concept, difficult implementation
  1522. *
  1523. * Basically, replace the incore superblock counters with a distributed per cpu
  1524. * counter for contended fields (e.g. free block count).
  1525. *
  1526. * Difficulties arise in that the incore sb is used for ENOSPC checking, and
  1527. * hence needs to be accurately read when we are running low on space. Hence
  1528. * there is a method to enable and disable the per-cpu counters based on how
  1529. * much "stuff" is available in them.
  1530. *
  1531. * Basically, a counter is enabled if there is enough free resource to justify
  1532. * running a per-cpu fast-path. If the per-cpu counter runs out (i.e. a local
  1533. * ENOSPC), then we disable the counters to synchronise all callers and
  1534. * re-distribute the available resources.
  1535. *
  1536. * If, once we redistributed the available resources, we still get a failure,
  1537. * we disable the per-cpu counter and go through the slow path.
  1538. *
  1539. * The slow path is the current xfs_mod_incore_sb() function. This means that
  1540. * when we disable a per-cpu counter, we need to drain it's resources back to
  1541. * the global superblock. We do this after disabling the counter to prevent
  1542. * more threads from queueing up on the counter.
  1543. *
  1544. * Essentially, this means that we still need a lock in the fast path to enable
  1545. * synchronisation between the global counters and the per-cpu counters. This
  1546. * is not a problem because the lock will be local to a CPU almost all the time
  1547. * and have little contention except when we get to ENOSPC conditions.
  1548. *
  1549. * Basically, this lock becomes a barrier that enables us to lock out the fast
  1550. * path while we do things like enabling and disabling counters and
  1551. * synchronising the counters.
  1552. *
  1553. * Locking rules:
  1554. *
  1555. * 1. XFS_SB_LOCK() before picking up per-cpu locks
  1556. * 2. per-cpu locks always picked up via for_each_online_cpu() order
  1557. * 3. accurate counter sync requires XFS_SB_LOCK + per cpu locks
  1558. * 4. modifying per-cpu counters requires holding per-cpu lock
  1559. * 5. modifying global counters requires holding XFS_SB_LOCK
  1560. * 6. enabling or disabling a counter requires holding the XFS_SB_LOCK
  1561. * and _none_ of the per-cpu locks.
  1562. *
  1563. * Disabled counters are only ever re-enabled by a balance operation
  1564. * that results in more free resources per CPU than a given threshold.
  1565. * To ensure counters don't remain disabled, they are rebalanced when
  1566. * the global resource goes above a higher threshold (i.e. some hysteresis
  1567. * is present to prevent thrashing).
  1568. */
  1569. #ifdef CONFIG_HOTPLUG_CPU
  1570. /*
  1571. * hot-plug CPU notifier support.
  1572. *
  1573. * We need a notifier per filesystem as we need to be able to identify
  1574. * the filesystem to balance the counters out. This is achieved by
  1575. * having a notifier block embedded in the xfs_mount_t and doing pointer
  1576. * magic to get the mount pointer from the notifier block address.
  1577. */
  1578. STATIC int
  1579. xfs_icsb_cpu_notify(
  1580. struct notifier_block *nfb,
  1581. unsigned long action,
  1582. void *hcpu)
  1583. {
  1584. xfs_icsb_cnts_t *cntp;
  1585. xfs_mount_t *mp;
  1586. int s;
  1587. mp = (xfs_mount_t *)container_of(nfb, xfs_mount_t, m_icsb_notifier);
  1588. cntp = (xfs_icsb_cnts_t *)
  1589. per_cpu_ptr(mp->m_sb_cnts, (unsigned long)hcpu);
  1590. switch (action) {
  1591. case CPU_UP_PREPARE:
  1592. /* Easy Case - initialize the area and locks, and
  1593. * then rebalance when online does everything else for us. */
  1594. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1595. break;
  1596. case CPU_ONLINE:
  1597. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, 0);
  1598. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, 0);
  1599. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, 0);
  1600. break;
  1601. case CPU_DEAD:
  1602. /* Disable all the counters, then fold the dead cpu's
  1603. * count into the total on the global superblock and
  1604. * re-enable the counters. */
  1605. s = XFS_SB_LOCK(mp);
  1606. xfs_icsb_disable_counter(mp, XFS_SBS_ICOUNT);
  1607. xfs_icsb_disable_counter(mp, XFS_SBS_IFREE);
  1608. xfs_icsb_disable_counter(mp, XFS_SBS_FDBLOCKS);
  1609. mp->m_sb.sb_icount += cntp->icsb_icount;
  1610. mp->m_sb.sb_ifree += cntp->icsb_ifree;
  1611. mp->m_sb.sb_fdblocks += cntp->icsb_fdblocks;
  1612. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1613. xfs_icsb_balance_counter(mp, XFS_SBS_ICOUNT, XFS_ICSB_SB_LOCKED);
  1614. xfs_icsb_balance_counter(mp, XFS_SBS_IFREE, XFS_ICSB_SB_LOCKED);
  1615. xfs_icsb_balance_counter(mp, XFS_SBS_FDBLOCKS, XFS_ICSB_SB_LOCKED);
  1616. XFS_SB_UNLOCK(mp, s);
  1617. break;
  1618. }
  1619. return NOTIFY_OK;
  1620. }
  1621. #endif /* CONFIG_HOTPLUG_CPU */
  1622. int
  1623. xfs_icsb_init_counters(
  1624. xfs_mount_t *mp)
  1625. {
  1626. xfs_icsb_cnts_t *cntp;
  1627. int i;
  1628. mp->m_sb_cnts = alloc_percpu(xfs_icsb_cnts_t);
  1629. if (mp->m_sb_cnts == NULL)
  1630. return -ENOMEM;
  1631. #ifdef CONFIG_HOTPLUG_CPU
  1632. mp->m_icsb_notifier.notifier_call = xfs_icsb_cpu_notify;
  1633. mp->m_icsb_notifier.priority = 0;
  1634. register_hotcpu_notifier(&mp->m_icsb_notifier);
  1635. #endif /* CONFIG_HOTPLUG_CPU */
  1636. for_each_online_cpu(i) {
  1637. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1638. memset(cntp, 0, sizeof(xfs_icsb_cnts_t));
  1639. }
  1640. /*
  1641. * start with all counters disabled so that the
  1642. * initial balance kicks us off correctly
  1643. */
  1644. mp->m_icsb_counters = -1;
  1645. return 0;
  1646. }
  1647. STATIC void
  1648. xfs_icsb_destroy_counters(
  1649. xfs_mount_t *mp)
  1650. {
  1651. if (mp->m_sb_cnts) {
  1652. unregister_hotcpu_notifier(&mp->m_icsb_notifier);
  1653. free_percpu(mp->m_sb_cnts);
  1654. }
  1655. }
  1656. STATIC inline void
  1657. xfs_icsb_lock_cntr(
  1658. xfs_icsb_cnts_t *icsbp)
  1659. {
  1660. while (test_and_set_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags)) {
  1661. ndelay(1000);
  1662. }
  1663. }
  1664. STATIC inline void
  1665. xfs_icsb_unlock_cntr(
  1666. xfs_icsb_cnts_t *icsbp)
  1667. {
  1668. clear_bit(XFS_ICSB_FLAG_LOCK, &icsbp->icsb_flags);
  1669. }
  1670. STATIC inline void
  1671. xfs_icsb_lock_all_counters(
  1672. xfs_mount_t *mp)
  1673. {
  1674. xfs_icsb_cnts_t *cntp;
  1675. int i;
  1676. for_each_online_cpu(i) {
  1677. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1678. xfs_icsb_lock_cntr(cntp);
  1679. }
  1680. }
  1681. STATIC inline void
  1682. xfs_icsb_unlock_all_counters(
  1683. xfs_mount_t *mp)
  1684. {
  1685. xfs_icsb_cnts_t *cntp;
  1686. int i;
  1687. for_each_online_cpu(i) {
  1688. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1689. xfs_icsb_unlock_cntr(cntp);
  1690. }
  1691. }
  1692. STATIC void
  1693. xfs_icsb_count(
  1694. xfs_mount_t *mp,
  1695. xfs_icsb_cnts_t *cnt,
  1696. int flags)
  1697. {
  1698. xfs_icsb_cnts_t *cntp;
  1699. int i;
  1700. memset(cnt, 0, sizeof(xfs_icsb_cnts_t));
  1701. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1702. xfs_icsb_lock_all_counters(mp);
  1703. for_each_online_cpu(i) {
  1704. cntp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, i);
  1705. cnt->icsb_icount += cntp->icsb_icount;
  1706. cnt->icsb_ifree += cntp->icsb_ifree;
  1707. cnt->icsb_fdblocks += cntp->icsb_fdblocks;
  1708. }
  1709. if (!(flags & XFS_ICSB_LAZY_COUNT))
  1710. xfs_icsb_unlock_all_counters(mp);
  1711. }
  1712. STATIC int
  1713. xfs_icsb_counter_disabled(
  1714. xfs_mount_t *mp,
  1715. xfs_sb_field_t field)
  1716. {
  1717. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1718. return test_bit(field, &mp->m_icsb_counters);
  1719. }
  1720. STATIC int
  1721. xfs_icsb_disable_counter(
  1722. xfs_mount_t *mp,
  1723. xfs_sb_field_t field)
  1724. {
  1725. xfs_icsb_cnts_t cnt;
  1726. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1727. xfs_icsb_lock_all_counters(mp);
  1728. if (!test_and_set_bit(field, &mp->m_icsb_counters)) {
  1729. /* drain back to superblock */
  1730. xfs_icsb_count(mp, &cnt, XFS_ICSB_SB_LOCKED|XFS_ICSB_LAZY_COUNT);
  1731. switch(field) {
  1732. case XFS_SBS_ICOUNT:
  1733. mp->m_sb.sb_icount = cnt.icsb_icount;
  1734. break;
  1735. case XFS_SBS_IFREE:
  1736. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1737. break;
  1738. case XFS_SBS_FDBLOCKS:
  1739. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1740. break;
  1741. default:
  1742. BUG();
  1743. }
  1744. }
  1745. xfs_icsb_unlock_all_counters(mp);
  1746. return 0;
  1747. }
  1748. STATIC void
  1749. xfs_icsb_enable_counter(
  1750. xfs_mount_t *mp,
  1751. xfs_sb_field_t field,
  1752. uint64_t count,
  1753. uint64_t resid)
  1754. {
  1755. xfs_icsb_cnts_t *cntp;
  1756. int i;
  1757. ASSERT((field >= XFS_SBS_ICOUNT) && (field <= XFS_SBS_FDBLOCKS));
  1758. xfs_icsb_lock_all_counters(mp);
  1759. for_each_online_cpu(i) {
  1760. cntp = per_cpu_ptr(mp->m_sb_cnts, i);
  1761. switch (field) {
  1762. case XFS_SBS_ICOUNT:
  1763. cntp->icsb_icount = count + resid;
  1764. break;
  1765. case XFS_SBS_IFREE:
  1766. cntp->icsb_ifree = count + resid;
  1767. break;
  1768. case XFS_SBS_FDBLOCKS:
  1769. cntp->icsb_fdblocks = count + resid;
  1770. break;
  1771. default:
  1772. BUG();
  1773. break;
  1774. }
  1775. resid = 0;
  1776. }
  1777. clear_bit(field, &mp->m_icsb_counters);
  1778. xfs_icsb_unlock_all_counters(mp);
  1779. }
  1780. STATIC void
  1781. xfs_icsb_sync_counters_int(
  1782. xfs_mount_t *mp,
  1783. int flags)
  1784. {
  1785. xfs_icsb_cnts_t cnt;
  1786. int s;
  1787. /* Pass 1: lock all counters */
  1788. if ((flags & XFS_ICSB_SB_LOCKED) == 0)
  1789. s = XFS_SB_LOCK(mp);
  1790. xfs_icsb_count(mp, &cnt, flags);
  1791. /* Step 3: update mp->m_sb fields */
  1792. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_ICOUNT))
  1793. mp->m_sb.sb_icount = cnt.icsb_icount;
  1794. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_IFREE))
  1795. mp->m_sb.sb_ifree = cnt.icsb_ifree;
  1796. if (!xfs_icsb_counter_disabled(mp, XFS_SBS_FDBLOCKS))
  1797. mp->m_sb.sb_fdblocks = cnt.icsb_fdblocks;
  1798. if ((flags & XFS_ICSB_SB_LOCKED) == 0)
  1799. XFS_SB_UNLOCK(mp, s);
  1800. }
  1801. /*
  1802. * Accurate update of per-cpu counters to incore superblock
  1803. */
  1804. STATIC void
  1805. xfs_icsb_sync_counters(
  1806. xfs_mount_t *mp)
  1807. {
  1808. xfs_icsb_sync_counters_int(mp, 0);
  1809. }
  1810. /*
  1811. * lazy addition used for things like df, background sb syncs, etc
  1812. */
  1813. void
  1814. xfs_icsb_sync_counters_lazy(
  1815. xfs_mount_t *mp)
  1816. {
  1817. xfs_icsb_sync_counters_int(mp, XFS_ICSB_LAZY_COUNT);
  1818. }
  1819. /*
  1820. * Balance and enable/disable counters as necessary.
  1821. *
  1822. * Thresholds for re-enabling counters are somewhat magic.
  1823. * inode counts are chosen to be the same number as single
  1824. * on disk allocation chunk per CPU, and free blocks is
  1825. * something far enough zero that we aren't going thrash
  1826. * when we get near ENOSPC.
  1827. */
  1828. #define XFS_ICSB_INO_CNTR_REENABLE 64
  1829. #define XFS_ICSB_FDBLK_CNTR_REENABLE 512
  1830. STATIC void
  1831. xfs_icsb_balance_counter(
  1832. xfs_mount_t *mp,
  1833. xfs_sb_field_t field,
  1834. int flags)
  1835. {
  1836. uint64_t count, resid;
  1837. int weight = num_online_cpus();
  1838. int s;
  1839. if (!(flags & XFS_ICSB_SB_LOCKED))
  1840. s = XFS_SB_LOCK(mp);
  1841. /* disable counter and sync counter */
  1842. xfs_icsb_disable_counter(mp, field);
  1843. /* update counters - first CPU gets residual*/
  1844. switch (field) {
  1845. case XFS_SBS_ICOUNT:
  1846. count = mp->m_sb.sb_icount;
  1847. resid = do_div(count, weight);
  1848. if (count < XFS_ICSB_INO_CNTR_REENABLE)
  1849. goto out;
  1850. break;
  1851. case XFS_SBS_IFREE:
  1852. count = mp->m_sb.sb_ifree;
  1853. resid = do_div(count, weight);
  1854. if (count < XFS_ICSB_INO_CNTR_REENABLE)
  1855. goto out;
  1856. break;
  1857. case XFS_SBS_FDBLOCKS:
  1858. count = mp->m_sb.sb_fdblocks;
  1859. resid = do_div(count, weight);
  1860. if (count < XFS_ICSB_FDBLK_CNTR_REENABLE)
  1861. goto out;
  1862. break;
  1863. default:
  1864. BUG();
  1865. count = resid = 0; /* quiet, gcc */
  1866. break;
  1867. }
  1868. xfs_icsb_enable_counter(mp, field, count, resid);
  1869. out:
  1870. if (!(flags & XFS_ICSB_SB_LOCKED))
  1871. XFS_SB_UNLOCK(mp, s);
  1872. }
  1873. STATIC int
  1874. xfs_icsb_modify_counters_int(
  1875. xfs_mount_t *mp,
  1876. xfs_sb_field_t field,
  1877. int delta,
  1878. int rsvd,
  1879. int flags)
  1880. {
  1881. xfs_icsb_cnts_t *icsbp;
  1882. long long lcounter; /* long counter for 64 bit fields */
  1883. int cpu, s, locked = 0;
  1884. int ret = 0, balance_done = 0;
  1885. again:
  1886. cpu = get_cpu();
  1887. icsbp = (xfs_icsb_cnts_t *)per_cpu_ptr(mp->m_sb_cnts, cpu),
  1888. xfs_icsb_lock_cntr(icsbp);
  1889. if (unlikely(xfs_icsb_counter_disabled(mp, field)))
  1890. goto slow_path;
  1891. switch (field) {
  1892. case XFS_SBS_ICOUNT:
  1893. lcounter = icsbp->icsb_icount;
  1894. lcounter += delta;
  1895. if (unlikely(lcounter < 0))
  1896. goto slow_path;
  1897. icsbp->icsb_icount = lcounter;
  1898. break;
  1899. case XFS_SBS_IFREE:
  1900. lcounter = icsbp->icsb_ifree;
  1901. lcounter += delta;
  1902. if (unlikely(lcounter < 0))
  1903. goto slow_path;
  1904. icsbp->icsb_ifree = lcounter;
  1905. break;
  1906. case XFS_SBS_FDBLOCKS:
  1907. BUG_ON((mp->m_resblks - mp->m_resblks_avail) != 0);
  1908. lcounter = icsbp->icsb_fdblocks;
  1909. lcounter += delta;
  1910. if (unlikely(lcounter < 0))
  1911. goto slow_path;
  1912. icsbp->icsb_fdblocks = lcounter;
  1913. break;
  1914. default:
  1915. BUG();
  1916. break;
  1917. }
  1918. xfs_icsb_unlock_cntr(icsbp);
  1919. put_cpu();
  1920. if (locked)
  1921. XFS_SB_UNLOCK(mp, s);
  1922. return 0;
  1923. /*
  1924. * The slow path needs to be run with the SBLOCK
  1925. * held so that we prevent other threads from
  1926. * attempting to run this path at the same time.
  1927. * this provides exclusion for the balancing code,
  1928. * and exclusive fallback if the balance does not
  1929. * provide enough resources to continue in an unlocked
  1930. * manner.
  1931. */
  1932. slow_path:
  1933. xfs_icsb_unlock_cntr(icsbp);
  1934. put_cpu();
  1935. /* need to hold superblock incase we need
  1936. * to disable a counter */
  1937. if (!(flags & XFS_ICSB_SB_LOCKED)) {
  1938. s = XFS_SB_LOCK(mp);
  1939. locked = 1;
  1940. flags |= XFS_ICSB_SB_LOCKED;
  1941. }
  1942. if (!balance_done) {
  1943. xfs_icsb_balance_counter(mp, field, flags);
  1944. balance_done = 1;
  1945. goto again;
  1946. } else {
  1947. /*
  1948. * we might not have enough on this local
  1949. * cpu to allocate for a bulk request.
  1950. * We need to drain this field from all CPUs
  1951. * and disable the counter fastpath
  1952. */
  1953. xfs_icsb_disable_counter(mp, field);
  1954. }
  1955. ret = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1956. if (locked)
  1957. XFS_SB_UNLOCK(mp, s);
  1958. return ret;
  1959. }
  1960. STATIC int
  1961. xfs_icsb_modify_counters(
  1962. xfs_mount_t *mp,
  1963. xfs_sb_field_t field,
  1964. int delta,
  1965. int rsvd)
  1966. {
  1967. return xfs_icsb_modify_counters_int(mp, field, delta, rsvd, 0);
  1968. }
  1969. /*
  1970. * Called when superblock is already locked
  1971. */
  1972. STATIC int
  1973. xfs_icsb_modify_counters_locked(
  1974. xfs_mount_t *mp,
  1975. xfs_sb_field_t field,
  1976. int delta,
  1977. int rsvd)
  1978. {
  1979. return xfs_icsb_modify_counters_int(mp, field, delta,
  1980. rsvd, XFS_ICSB_SB_LOCKED);
  1981. }
  1982. #endif