xfs_mount.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. /*
  2. * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_macros.h"
  34. #include "xfs_types.h"
  35. #include "xfs_inum.h"
  36. #include "xfs_log.h"
  37. #include "xfs_trans.h"
  38. #include "xfs_sb.h"
  39. #include "xfs_ag.h"
  40. #include "xfs_dir.h"
  41. #include "xfs_dir2.h"
  42. #include "xfs_dmapi.h"
  43. #include "xfs_mount.h"
  44. #include "xfs_alloc_btree.h"
  45. #include "xfs_bmap_btree.h"
  46. #include "xfs_ialloc_btree.h"
  47. #include "xfs_btree.h"
  48. #include "xfs_ialloc.h"
  49. #include "xfs_attr_sf.h"
  50. #include "xfs_dir_sf.h"
  51. #include "xfs_dir2_sf.h"
  52. #include "xfs_dinode.h"
  53. #include "xfs_inode.h"
  54. #include "xfs_alloc.h"
  55. #include "xfs_rtalloc.h"
  56. #include "xfs_bmap.h"
  57. #include "xfs_error.h"
  58. #include "xfs_bit.h"
  59. #include "xfs_rw.h"
  60. #include "xfs_quota.h"
  61. #include "xfs_fsops.h"
  62. STATIC void xfs_mount_log_sbunit(xfs_mount_t *, __int64_t);
  63. STATIC int xfs_uuid_mount(xfs_mount_t *);
  64. STATIC void xfs_uuid_unmount(xfs_mount_t *mp);
  65. STATIC void xfs_unmountfs_wait(xfs_mount_t *);
  66. static 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(*mp), KM_SLEEP);
  127. AIL_LOCKINIT(&mp->m_ail_lock, "xfs_ail");
  128. spinlock_init(&mp->m_sb_lock, "xfs_sb");
  129. mutex_init(&mp->m_ilock, MUTEX_DEFAULT, "xfs_ilock");
  130. initnsema(&mp->m_growlock, 1, "xfs_grow");
  131. /*
  132. * Initialize the AIL.
  133. */
  134. xfs_trans_ail_init(mp);
  135. atomic_set(&mp->m_active_trans, 0);
  136. return mp;
  137. }
  138. /*
  139. * Free up the resources associated with a mount structure. Assume that
  140. * the structure was initially zeroed, so we can tell which fields got
  141. * initialized.
  142. */
  143. void
  144. xfs_mount_free(
  145. xfs_mount_t *mp,
  146. int remove_bhv)
  147. {
  148. if (mp->m_ihash)
  149. xfs_ihash_free(mp);
  150. if (mp->m_chash)
  151. xfs_chash_free(mp);
  152. if (mp->m_perag) {
  153. int agno;
  154. for (agno = 0; agno < mp->m_maxagi; agno++)
  155. if (mp->m_perag[agno].pagb_list)
  156. kmem_free(mp->m_perag[agno].pagb_list,
  157. sizeof(xfs_perag_busy_t) *
  158. XFS_PAGB_NUM_SLOTS);
  159. kmem_free(mp->m_perag,
  160. sizeof(xfs_perag_t) * mp->m_sb.sb_agcount);
  161. }
  162. AIL_LOCK_DESTROY(&mp->m_ail_lock);
  163. spinlock_destroy(&mp->m_sb_lock);
  164. mutex_destroy(&mp->m_ilock);
  165. freesema(&mp->m_growlock);
  166. if (mp->m_quotainfo)
  167. XFS_QM_DONE(mp);
  168. if (mp->m_fsname != NULL)
  169. kmem_free(mp->m_fsname, mp->m_fsname_len);
  170. if (remove_bhv) {
  171. struct vfs *vfsp = XFS_MTOVFS(mp);
  172. bhv_remove_all_vfsops(vfsp, 0);
  173. VFS_REMOVEBHV(vfsp, &mp->m_bhv);
  174. }
  175. kmem_free(mp, sizeof(xfs_mount_t));
  176. }
  177. /*
  178. * Check the validity of the SB found.
  179. */
  180. STATIC int
  181. xfs_mount_validate_sb(
  182. xfs_mount_t *mp,
  183. xfs_sb_t *sbp)
  184. {
  185. /*
  186. * If the log device and data device have the
  187. * same device number, the log is internal.
  188. * Consequently, the sb_logstart should be non-zero. If
  189. * we have a zero sb_logstart in this case, we may be trying to mount
  190. * a volume filesystem in a non-volume manner.
  191. */
  192. if (sbp->sb_magicnum != XFS_SB_MAGIC) {
  193. cmn_err(CE_WARN, "XFS: bad magic number");
  194. return XFS_ERROR(EWRONGFS);
  195. }
  196. if (!XFS_SB_GOOD_VERSION(sbp)) {
  197. cmn_err(CE_WARN, "XFS: bad version");
  198. return XFS_ERROR(EWRONGFS);
  199. }
  200. if (unlikely(
  201. sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
  202. cmn_err(CE_WARN,
  203. "XFS: filesystem is marked as having an external log; "
  204. "specify logdev on the\nmount command line.");
  205. XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(1)",
  206. XFS_ERRLEVEL_HIGH, mp, sbp);
  207. return XFS_ERROR(EFSCORRUPTED);
  208. }
  209. if (unlikely(
  210. sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
  211. cmn_err(CE_WARN,
  212. "XFS: filesystem is marked as having an internal log; "
  213. "don't specify logdev on\nthe mount command line.");
  214. XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(2)",
  215. XFS_ERRLEVEL_HIGH, mp, sbp);
  216. return XFS_ERROR(EFSCORRUPTED);
  217. }
  218. /*
  219. * More sanity checking. These were stolen directly from
  220. * xfs_repair.
  221. */
  222. if (unlikely(
  223. sbp->sb_agcount <= 0 ||
  224. sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
  225. sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
  226. sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
  227. sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
  228. sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
  229. sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
  230. sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
  231. sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
  232. sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
  233. sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
  234. (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
  235. (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
  236. sbp->sb_imax_pct > 100)) {
  237. cmn_err(CE_WARN, "XFS: SB sanity check 1 failed");
  238. XFS_CORRUPTION_ERROR("xfs_mount_validate_sb(3)",
  239. XFS_ERRLEVEL_LOW, mp, sbp);
  240. return XFS_ERROR(EFSCORRUPTED);
  241. }
  242. /*
  243. * Sanity check AG count, size fields against data size field
  244. */
  245. if (unlikely(
  246. sbp->sb_dblocks == 0 ||
  247. sbp->sb_dblocks >
  248. (xfs_drfsbno_t)sbp->sb_agcount * sbp->sb_agblocks ||
  249. sbp->sb_dblocks < (xfs_drfsbno_t)(sbp->sb_agcount - 1) *
  250. sbp->sb_agblocks + XFS_MIN_AG_BLOCKS)) {
  251. cmn_err(CE_WARN, "XFS: SB sanity check 2 failed");
  252. XFS_ERROR_REPORT("xfs_mount_validate_sb(4)",
  253. XFS_ERRLEVEL_LOW, mp);
  254. return XFS_ERROR(EFSCORRUPTED);
  255. }
  256. ASSERT(PAGE_SHIFT >= sbp->sb_blocklog);
  257. ASSERT(sbp->sb_blocklog >= BBSHIFT);
  258. #if XFS_BIG_BLKNOS /* Limited by ULONG_MAX of page cache index */
  259. if (unlikely(
  260. (sbp->sb_dblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX ||
  261. (sbp->sb_rblocks >> (PAGE_SHIFT - sbp->sb_blocklog)) > ULONG_MAX)) {
  262. #else /* Limited by UINT_MAX of sectors */
  263. if (unlikely(
  264. (sbp->sb_dblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX ||
  265. (sbp->sb_rblocks << (sbp->sb_blocklog - BBSHIFT)) > UINT_MAX)) {
  266. #endif
  267. cmn_err(CE_WARN,
  268. "XFS: File system is too large to be mounted on this system.");
  269. return XFS_ERROR(E2BIG);
  270. }
  271. if (unlikely(sbp->sb_inprogress)) {
  272. cmn_err(CE_WARN, "XFS: file system busy");
  273. XFS_ERROR_REPORT("xfs_mount_validate_sb(5)",
  274. XFS_ERRLEVEL_LOW, mp);
  275. return XFS_ERROR(EFSCORRUPTED);
  276. }
  277. /*
  278. * Version 1 directory format has never worked on Linux.
  279. */
  280. if (unlikely(!XFS_SB_VERSION_HASDIRV2(sbp))) {
  281. cmn_err(CE_WARN,
  282. "XFS: Attempted to mount file system using version 1 directory format");
  283. return XFS_ERROR(ENOSYS);
  284. }
  285. /*
  286. * Until this is fixed only page-sized or smaller data blocks work.
  287. */
  288. if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
  289. cmn_err(CE_WARN,
  290. "XFS: Attempted to mount file system with blocksize %d bytes",
  291. sbp->sb_blocksize);
  292. cmn_err(CE_WARN,
  293. "XFS: Only page-sized (%d) or less blocksizes currently work.",
  294. PAGE_SIZE);
  295. return XFS_ERROR(ENOSYS);
  296. }
  297. return 0;
  298. }
  299. xfs_agnumber_t
  300. xfs_initialize_perag(xfs_mount_t *mp, xfs_agnumber_t agcount)
  301. {
  302. xfs_agnumber_t index, max_metadata;
  303. xfs_perag_t *pag;
  304. xfs_agino_t agino;
  305. xfs_ino_t ino;
  306. xfs_sb_t *sbp = &mp->m_sb;
  307. xfs_ino_t max_inum = XFS_MAXINUMBER_32;
  308. /* Check to see if the filesystem can overflow 32 bit inodes */
  309. agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
  310. ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
  311. /* Clear the mount flag if no inode can overflow 32 bits
  312. * on this filesystem, or if specifically requested..
  313. */
  314. if ((mp->m_flags & XFS_MOUNT_32BITINOOPT) && ino > max_inum) {
  315. mp->m_flags |= XFS_MOUNT_32BITINODES;
  316. } else {
  317. mp->m_flags &= ~XFS_MOUNT_32BITINODES;
  318. }
  319. /* If we can overflow then setup the ag headers accordingly */
  320. if (mp->m_flags & XFS_MOUNT_32BITINODES) {
  321. /* Calculate how much should be reserved for inodes to
  322. * meet the max inode percentage.
  323. */
  324. if (mp->m_maxicount) {
  325. __uint64_t icount;
  326. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  327. do_div(icount, 100);
  328. icount += sbp->sb_agblocks - 1;
  329. do_div(icount, mp->m_ialloc_blks);
  330. max_metadata = icount;
  331. } else {
  332. max_metadata = agcount;
  333. }
  334. for (index = 0; index < agcount; index++) {
  335. ino = XFS_AGINO_TO_INO(mp, index, agino);
  336. if (ino > max_inum) {
  337. index++;
  338. break;
  339. }
  340. /* This ag is prefered for inodes */
  341. pag = &mp->m_perag[index];
  342. pag->pagi_inodeok = 1;
  343. if (index < max_metadata)
  344. pag->pagf_metadata = 1;
  345. }
  346. } else {
  347. /* Setup default behavior for smaller filesystems */
  348. for (index = 0; index < agcount; index++) {
  349. pag = &mp->m_perag[index];
  350. pag->pagi_inodeok = 1;
  351. }
  352. }
  353. return index;
  354. }
  355. /*
  356. * xfs_xlatesb
  357. *
  358. * data - on disk version of sb
  359. * sb - a superblock
  360. * dir - conversion direction: <0 - convert sb to buf
  361. * >0 - convert buf to sb
  362. * fields - which fields to copy (bitmask)
  363. */
  364. void
  365. xfs_xlatesb(
  366. void *data,
  367. xfs_sb_t *sb,
  368. int dir,
  369. __int64_t fields)
  370. {
  371. xfs_caddr_t buf_ptr;
  372. xfs_caddr_t mem_ptr;
  373. xfs_sb_field_t f;
  374. int first;
  375. int size;
  376. ASSERT(dir);
  377. ASSERT(fields);
  378. if (!fields)
  379. return;
  380. buf_ptr = (xfs_caddr_t)data;
  381. mem_ptr = (xfs_caddr_t)sb;
  382. while (fields) {
  383. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  384. first = xfs_sb_info[f].offset;
  385. size = xfs_sb_info[f + 1].offset - first;
  386. ASSERT(xfs_sb_info[f].type == 0 || xfs_sb_info[f].type == 1);
  387. if (size == 1 || xfs_sb_info[f].type == 1) {
  388. if (dir > 0) {
  389. memcpy(mem_ptr + first, buf_ptr + first, size);
  390. } else {
  391. memcpy(buf_ptr + first, mem_ptr + first, size);
  392. }
  393. } else {
  394. switch (size) {
  395. case 2:
  396. INT_XLATE(*(__uint16_t*)(buf_ptr+first),
  397. *(__uint16_t*)(mem_ptr+first),
  398. dir, ARCH_CONVERT);
  399. break;
  400. case 4:
  401. INT_XLATE(*(__uint32_t*)(buf_ptr+first),
  402. *(__uint32_t*)(mem_ptr+first),
  403. dir, ARCH_CONVERT);
  404. break;
  405. case 8:
  406. INT_XLATE(*(__uint64_t*)(buf_ptr+first),
  407. *(__uint64_t*)(mem_ptr+first), dir, ARCH_CONVERT);
  408. break;
  409. default:
  410. ASSERT(0);
  411. }
  412. }
  413. fields &= ~(1LL << f);
  414. }
  415. }
  416. /*
  417. * xfs_readsb
  418. *
  419. * Does the initial read of the superblock.
  420. */
  421. int
  422. xfs_readsb(xfs_mount_t *mp)
  423. {
  424. unsigned int sector_size;
  425. unsigned int extra_flags;
  426. xfs_buf_t *bp;
  427. xfs_sb_t *sbp;
  428. int error;
  429. ASSERT(mp->m_sb_bp == NULL);
  430. ASSERT(mp->m_ddev_targp != NULL);
  431. /*
  432. * Allocate a (locked) buffer to hold the superblock.
  433. * This will be kept around at all times to optimize
  434. * access to the superblock.
  435. */
  436. sector_size = xfs_getsize_buftarg(mp->m_ddev_targp);
  437. extra_flags = XFS_BUF_LOCK | XFS_BUF_MANAGE | XFS_BUF_MAPPED;
  438. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  439. BTOBB(sector_size), extra_flags);
  440. if (!bp || XFS_BUF_ISERROR(bp)) {
  441. cmn_err(CE_WARN, "XFS: SB read failed");
  442. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  443. goto fail;
  444. }
  445. ASSERT(XFS_BUF_ISBUSY(bp));
  446. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  447. /*
  448. * Initialize the mount structure from the superblock.
  449. * But first do some basic consistency checking.
  450. */
  451. sbp = XFS_BUF_TO_SBP(bp);
  452. xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), 1, XFS_SB_ALL_BITS);
  453. error = xfs_mount_validate_sb(mp, &(mp->m_sb));
  454. if (error) {
  455. cmn_err(CE_WARN, "XFS: SB validate failed");
  456. goto fail;
  457. }
  458. /*
  459. * We must be able to do sector-sized and sector-aligned IO.
  460. */
  461. if (sector_size > mp->m_sb.sb_sectsize) {
  462. cmn_err(CE_WARN,
  463. "XFS: device supports only %u byte sectors (not %u)",
  464. sector_size, mp->m_sb.sb_sectsize);
  465. error = ENOSYS;
  466. goto fail;
  467. }
  468. /*
  469. * If device sector size is smaller than the superblock size,
  470. * re-read the superblock so the buffer is correctly sized.
  471. */
  472. if (sector_size < mp->m_sb.sb_sectsize) {
  473. XFS_BUF_UNMANAGE(bp);
  474. xfs_buf_relse(bp);
  475. sector_size = mp->m_sb.sb_sectsize;
  476. bp = xfs_buf_read_flags(mp->m_ddev_targp, XFS_SB_DADDR,
  477. BTOBB(sector_size), extra_flags);
  478. if (!bp || XFS_BUF_ISERROR(bp)) {
  479. cmn_err(CE_WARN, "XFS: SB re-read failed");
  480. error = bp ? XFS_BUF_GETERROR(bp) : ENOMEM;
  481. goto fail;
  482. }
  483. ASSERT(XFS_BUF_ISBUSY(bp));
  484. ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
  485. }
  486. mp->m_sb_bp = bp;
  487. xfs_buf_relse(bp);
  488. ASSERT(XFS_BUF_VALUSEMA(bp) > 0);
  489. return 0;
  490. fail:
  491. if (bp) {
  492. XFS_BUF_UNMANAGE(bp);
  493. xfs_buf_relse(bp);
  494. }
  495. return error;
  496. }
  497. /*
  498. * xfs_mount_common
  499. *
  500. * Mount initialization code establishing various mount
  501. * fields from the superblock associated with the given
  502. * mount structure
  503. */
  504. STATIC void
  505. xfs_mount_common(xfs_mount_t *mp, xfs_sb_t *sbp)
  506. {
  507. int i;
  508. mp->m_agfrotor = mp->m_agirotor = 0;
  509. spinlock_init(&mp->m_agirotor_lock, "m_agirotor_lock");
  510. mp->m_maxagi = mp->m_sb.sb_agcount;
  511. mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
  512. mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
  513. mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
  514. mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
  515. mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
  516. mp->m_litino = sbp->sb_inodesize -
  517. ((uint)sizeof(xfs_dinode_core_t) + (uint)sizeof(xfs_agino_t));
  518. mp->m_blockmask = sbp->sb_blocksize - 1;
  519. mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
  520. mp->m_blockwmask = mp->m_blockwsize - 1;
  521. INIT_LIST_HEAD(&mp->m_del_inodes);
  522. /*
  523. * Setup for attributes, in case they get created.
  524. * This value is for inodes getting attributes for the first time,
  525. * the per-inode value is for old attribute values.
  526. */
  527. ASSERT(sbp->sb_inodesize >= 256 && sbp->sb_inodesize <= 2048);
  528. switch (sbp->sb_inodesize) {
  529. case 256:
  530. mp->m_attroffset = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(2);
  531. break;
  532. case 512:
  533. case 1024:
  534. case 2048:
  535. mp->m_attroffset = XFS_BMDR_SPACE_CALC(12);
  536. break;
  537. default:
  538. ASSERT(0);
  539. }
  540. ASSERT(mp->m_attroffset < XFS_LITINO(mp));
  541. for (i = 0; i < 2; i++) {
  542. mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  543. xfs_alloc, i == 0);
  544. mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  545. xfs_alloc, i == 0);
  546. }
  547. for (i = 0; i < 2; i++) {
  548. mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  549. xfs_bmbt, i == 0);
  550. mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  551. xfs_bmbt, i == 0);
  552. }
  553. for (i = 0; i < 2; i++) {
  554. mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  555. xfs_inobt, i == 0);
  556. mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  557. xfs_inobt, i == 0);
  558. }
  559. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  560. mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
  561. sbp->sb_inopblock);
  562. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  563. }
  564. /*
  565. * xfs_mountfs
  566. *
  567. * This function does the following on an initial mount of a file system:
  568. * - reads the superblock from disk and init the mount struct
  569. * - if we're a 32-bit kernel, do a size check on the superblock
  570. * so we don't mount terabyte filesystems
  571. * - init mount struct realtime fields
  572. * - allocate inode hash table for fs
  573. * - init directory manager
  574. * - perform recovery and init the log manager
  575. */
  576. int
  577. xfs_mountfs(
  578. vfs_t *vfsp,
  579. xfs_mount_t *mp,
  580. int mfsi_flags)
  581. {
  582. xfs_buf_t *bp;
  583. xfs_sb_t *sbp = &(mp->m_sb);
  584. xfs_inode_t *rip;
  585. vnode_t *rvp = NULL;
  586. int readio_log, writeio_log;
  587. xfs_daddr_t d;
  588. __uint64_t ret64;
  589. __int64_t update_flags;
  590. uint quotamount, quotaflags;
  591. int agno;
  592. int uuid_mounted = 0;
  593. int error = 0;
  594. if (mp->m_sb_bp == NULL) {
  595. if ((error = xfs_readsb(mp))) {
  596. return (error);
  597. }
  598. }
  599. xfs_mount_common(mp, sbp);
  600. /*
  601. * Check if sb_agblocks is aligned at stripe boundary
  602. * If sb_agblocks is NOT aligned turn off m_dalign since
  603. * allocator alignment is within an ag, therefore ag has
  604. * to be aligned at stripe boundary.
  605. */
  606. update_flags = 0LL;
  607. if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
  608. /*
  609. * If stripe unit and stripe width are not multiples
  610. * of the fs blocksize turn off alignment.
  611. */
  612. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  613. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  614. if (mp->m_flags & XFS_MOUNT_RETERR) {
  615. cmn_err(CE_WARN,
  616. "XFS: alignment check 1 failed");
  617. error = XFS_ERROR(EINVAL);
  618. goto error1;
  619. }
  620. mp->m_dalign = mp->m_swidth = 0;
  621. } else {
  622. /*
  623. * Convert the stripe unit and width to FSBs.
  624. */
  625. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  626. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  627. if (mp->m_flags & XFS_MOUNT_RETERR) {
  628. error = XFS_ERROR(EINVAL);
  629. goto error1;
  630. }
  631. xfs_fs_cmn_err(CE_WARN, mp,
  632. "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
  633. mp->m_dalign, mp->m_swidth,
  634. sbp->sb_agblocks);
  635. mp->m_dalign = 0;
  636. mp->m_swidth = 0;
  637. } else if (mp->m_dalign) {
  638. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  639. } else {
  640. if (mp->m_flags & XFS_MOUNT_RETERR) {
  641. xfs_fs_cmn_err(CE_WARN, mp,
  642. "stripe alignment turned off: sunit(%d) less than bsize(%d)",
  643. mp->m_dalign,
  644. mp->m_blockmask +1);
  645. error = XFS_ERROR(EINVAL);
  646. goto error1;
  647. }
  648. mp->m_swidth = 0;
  649. }
  650. }
  651. /*
  652. * Update superblock with new values
  653. * and log changes
  654. */
  655. if (XFS_SB_VERSION_HASDALIGN(sbp)) {
  656. if (sbp->sb_unit != mp->m_dalign) {
  657. sbp->sb_unit = mp->m_dalign;
  658. update_flags |= XFS_SB_UNIT;
  659. }
  660. if (sbp->sb_width != mp->m_swidth) {
  661. sbp->sb_width = mp->m_swidth;
  662. update_flags |= XFS_SB_WIDTH;
  663. }
  664. }
  665. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  666. XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
  667. mp->m_dalign = sbp->sb_unit;
  668. mp->m_swidth = sbp->sb_width;
  669. }
  670. xfs_alloc_compute_maxlevels(mp);
  671. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  672. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  673. xfs_ialloc_compute_maxlevels(mp);
  674. if (sbp->sb_imax_pct) {
  675. __uint64_t icount;
  676. /* Make sure the maximum inode count is a multiple of the
  677. * units we allocate inodes in.
  678. */
  679. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  680. do_div(icount, 100);
  681. do_div(icount, mp->m_ialloc_blks);
  682. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  683. sbp->sb_inopblog;
  684. } else
  685. mp->m_maxicount = 0;
  686. mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
  687. /*
  688. * XFS uses the uuid from the superblock as the unique
  689. * identifier for fsid. We can not use the uuid from the volume
  690. * since a single partition filesystem is identical to a single
  691. * partition volume/filesystem.
  692. */
  693. if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
  694. (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
  695. if (xfs_uuid_mount(mp)) {
  696. error = XFS_ERROR(EINVAL);
  697. goto error1;
  698. }
  699. uuid_mounted=1;
  700. ret64 = uuid_hash64(&sbp->sb_uuid);
  701. memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
  702. }
  703. /*
  704. * Set the default minimum read and write sizes unless
  705. * already specified in a mount option.
  706. * We use smaller I/O sizes when the file system
  707. * is being used for NFS service (wsync mount option).
  708. */
  709. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  710. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  711. readio_log = XFS_WSYNC_READIO_LOG;
  712. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  713. } else {
  714. readio_log = XFS_READIO_LOG_LARGE;
  715. writeio_log = XFS_WRITEIO_LOG_LARGE;
  716. }
  717. } else {
  718. readio_log = mp->m_readio_log;
  719. writeio_log = mp->m_writeio_log;
  720. }
  721. /*
  722. * Set the number of readahead buffers to use based on
  723. * physical memory size.
  724. */
  725. if (xfs_physmem <= 4096) /* <= 16MB */
  726. mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
  727. else if (xfs_physmem <= 8192) /* <= 32MB */
  728. mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
  729. else
  730. mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
  731. if (sbp->sb_blocklog > readio_log) {
  732. mp->m_readio_log = sbp->sb_blocklog;
  733. } else {
  734. mp->m_readio_log = readio_log;
  735. }
  736. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  737. if (sbp->sb_blocklog > writeio_log) {
  738. mp->m_writeio_log = sbp->sb_blocklog;
  739. } else {
  740. mp->m_writeio_log = writeio_log;
  741. }
  742. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  743. /*
  744. * Set the inode cluster size based on the physical memory
  745. * size. This may still be overridden by the file system
  746. * block size if it is larger than the chosen cluster size.
  747. */
  748. if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
  749. mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
  750. } else {
  751. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  752. }
  753. /*
  754. * Set whether we're using inode alignment.
  755. */
  756. if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
  757. mp->m_sb.sb_inoalignmt >=
  758. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  759. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  760. else
  761. mp->m_inoalign_mask = 0;
  762. /*
  763. * If we are using stripe alignment, check whether
  764. * the stripe unit is a multiple of the inode alignment
  765. */
  766. if (mp->m_dalign && mp->m_inoalign_mask &&
  767. !(mp->m_dalign & mp->m_inoalign_mask))
  768. mp->m_sinoalign = mp->m_dalign;
  769. else
  770. mp->m_sinoalign = 0;
  771. /*
  772. * Check that the data (and log if separate) are an ok size.
  773. */
  774. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  775. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  776. cmn_err(CE_WARN, "XFS: size check 1 failed");
  777. error = XFS_ERROR(E2BIG);
  778. goto error1;
  779. }
  780. error = xfs_read_buf(mp, mp->m_ddev_targp,
  781. d - XFS_FSS_TO_BB(mp, 1),
  782. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  783. if (!error) {
  784. xfs_buf_relse(bp);
  785. } else {
  786. cmn_err(CE_WARN, "XFS: size check 2 failed");
  787. if (error == ENOSPC) {
  788. error = XFS_ERROR(E2BIG);
  789. }
  790. goto error1;
  791. }
  792. if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
  793. mp->m_logdev_targp != mp->m_ddev_targp) {
  794. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  795. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  796. cmn_err(CE_WARN, "XFS: size check 3 failed");
  797. error = XFS_ERROR(E2BIG);
  798. goto error1;
  799. }
  800. error = xfs_read_buf(mp, mp->m_logdev_targp,
  801. d - XFS_FSB_TO_BB(mp, 1),
  802. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  803. if (!error) {
  804. xfs_buf_relse(bp);
  805. } else {
  806. cmn_err(CE_WARN, "XFS: size check 3 failed");
  807. if (error == ENOSPC) {
  808. error = XFS_ERROR(E2BIG);
  809. }
  810. goto error1;
  811. }
  812. }
  813. /*
  814. * Initialize realtime fields in the mount structure
  815. */
  816. if ((error = xfs_rtmount_init(mp))) {
  817. cmn_err(CE_WARN, "XFS: RT mount failed");
  818. goto error1;
  819. }
  820. /*
  821. * For client case we are done now
  822. */
  823. if (mfsi_flags & XFS_MFSI_CLIENT) {
  824. return(0);
  825. }
  826. /*
  827. * Copies the low order bits of the timestamp and the randomly
  828. * set "sequence" number out of a UUID.
  829. */
  830. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  831. /*
  832. * The vfs structure needs to have a file system independent
  833. * way of checking for the invariant file system ID. Since it
  834. * can't look at mount structures it has a pointer to the data
  835. * in the mount structure.
  836. *
  837. * File systems that don't support user level file handles (i.e.
  838. * all of them except for XFS) will leave vfs_altfsid as NULL.
  839. */
  840. vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
  841. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  842. /*
  843. * Select the right directory manager.
  844. */
  845. mp->m_dirops =
  846. XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
  847. xfsv2_dirops :
  848. xfsv1_dirops;
  849. /*
  850. * Initialize directory manager's entries.
  851. */
  852. XFS_DIR_MOUNT(mp);
  853. /*
  854. * Initialize the attribute manager's entries.
  855. */
  856. mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
  857. /*
  858. * Initialize the precomputed transaction reservations values.
  859. */
  860. xfs_trans_init(mp);
  861. /*
  862. * Allocate and initialize the inode hash table for this
  863. * file system.
  864. */
  865. xfs_ihash_init(mp);
  866. xfs_chash_init(mp);
  867. /*
  868. * Allocate and initialize the per-ag data.
  869. */
  870. init_rwsem(&mp->m_peraglock);
  871. mp->m_perag =
  872. kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
  873. mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
  874. /*
  875. * log's mount-time initialization. Perform 1st part recovery if needed
  876. */
  877. if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
  878. error = xfs_log_mount(mp, mp->m_logdev_targp,
  879. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  880. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  881. if (error) {
  882. cmn_err(CE_WARN, "XFS: log mount failed");
  883. goto error2;
  884. }
  885. } else { /* No log has been defined */
  886. cmn_err(CE_WARN, "XFS: no log defined");
  887. XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
  888. error = XFS_ERROR(EFSCORRUPTED);
  889. goto error2;
  890. }
  891. /*
  892. * Get and sanity-check the root inode.
  893. * Save the pointer to it in the mount structure.
  894. */
  895. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
  896. if (error) {
  897. cmn_err(CE_WARN, "XFS: failed to read root inode");
  898. goto error3;
  899. }
  900. ASSERT(rip != NULL);
  901. rvp = XFS_ITOV(rip);
  902. if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
  903. cmn_err(CE_WARN, "XFS: corrupted root inode");
  904. prdev("Root inode %llu is not a directory",
  905. mp->m_ddev_targp, (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 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,
  989. XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING);
  990. /*
  991. * Flush out the log synchronously so that we know for sure
  992. * that nothing is pinned. This is important because bflush()
  993. * will skip pinned buffers.
  994. */
  995. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  996. xfs_binval(mp->m_ddev_targp);
  997. if (mp->m_rtdev_targp) {
  998. xfs_binval(mp->m_rtdev_targp);
  999. }
  1000. xfs_unmountfs_writesb(mp);
  1001. xfs_unmountfs_wait(mp); /* wait for async bufs */
  1002. xfs_log_unmount(mp); /* Done! No more fs ops. */
  1003. xfs_freesb(mp);
  1004. /*
  1005. * All inodes from this mount point should be freed.
  1006. */
  1007. ASSERT(mp->m_inodes == NULL);
  1008. xfs_unmountfs_close(mp, cr);
  1009. if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
  1010. xfs_uuid_unmount(mp);
  1011. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  1012. /*
  1013. * clear all error tags on this filesystem
  1014. */
  1015. memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
  1016. xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
  1017. #endif
  1018. XFS_IODONE(vfsp);
  1019. xfs_mount_free(mp, 1);
  1020. return 0;
  1021. }
  1022. void
  1023. xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
  1024. {
  1025. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1026. xfs_free_buftarg(mp->m_logdev_targp, 1);
  1027. if (mp->m_rtdev_targp)
  1028. xfs_free_buftarg(mp->m_rtdev_targp, 1);
  1029. xfs_free_buftarg(mp->m_ddev_targp, 0);
  1030. }
  1031. STATIC void
  1032. xfs_unmountfs_wait(xfs_mount_t *mp)
  1033. {
  1034. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1035. xfs_wait_buftarg(mp->m_logdev_targp);
  1036. if (mp->m_rtdev_targp)
  1037. xfs_wait_buftarg(mp->m_rtdev_targp);
  1038. xfs_wait_buftarg(mp->m_ddev_targp);
  1039. }
  1040. int
  1041. xfs_unmountfs_writesb(xfs_mount_t *mp)
  1042. {
  1043. xfs_buf_t *sbp;
  1044. xfs_sb_t *sb;
  1045. int error = 0;
  1046. /*
  1047. * skip superblock write if fs is read-only, or
  1048. * if we are doing a forced umount.
  1049. */
  1050. sbp = xfs_getsb(mp, 0);
  1051. if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
  1052. XFS_FORCED_SHUTDOWN(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. STATIC 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)mp->m_sb.sb_fdblocks;
  1161. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1162. if (delta > 0) { /* Putting blocks back */
  1163. if (res_used > delta) {
  1164. mp->m_resblks_avail += delta;
  1165. } else {
  1166. rem = delta - res_used;
  1167. mp->m_resblks_avail = mp->m_resblks;
  1168. lcounter += rem;
  1169. }
  1170. } else { /* Taking blocks away */
  1171. lcounter += delta;
  1172. /*
  1173. * If were out of blocks, use any available reserved blocks if
  1174. * were allowed to.
  1175. */
  1176. if (lcounter < 0) {
  1177. if (rsvd) {
  1178. lcounter = (long long)mp->m_resblks_avail + delta;
  1179. if (lcounter < 0) {
  1180. return (XFS_ERROR(ENOSPC));
  1181. }
  1182. mp->m_resblks_avail = lcounter;
  1183. return (0);
  1184. } else { /* not reserved */
  1185. return (XFS_ERROR(ENOSPC));
  1186. }
  1187. }
  1188. }
  1189. mp->m_sb.sb_fdblocks = lcounter;
  1190. return (0);
  1191. case XFS_SBS_FREXTENTS:
  1192. lcounter = (long long)mp->m_sb.sb_frextents;
  1193. lcounter += delta;
  1194. if (lcounter < 0) {
  1195. return (XFS_ERROR(ENOSPC));
  1196. }
  1197. mp->m_sb.sb_frextents = lcounter;
  1198. return (0);
  1199. case XFS_SBS_DBLOCKS:
  1200. lcounter = (long long)mp->m_sb.sb_dblocks;
  1201. lcounter += delta;
  1202. if (lcounter < 0) {
  1203. ASSERT(0);
  1204. return (XFS_ERROR(EINVAL));
  1205. }
  1206. mp->m_sb.sb_dblocks = lcounter;
  1207. return (0);
  1208. case XFS_SBS_AGCOUNT:
  1209. scounter = mp->m_sb.sb_agcount;
  1210. scounter += delta;
  1211. if (scounter < 0) {
  1212. ASSERT(0);
  1213. return (XFS_ERROR(EINVAL));
  1214. }
  1215. mp->m_sb.sb_agcount = scounter;
  1216. return (0);
  1217. case XFS_SBS_IMAX_PCT:
  1218. scounter = mp->m_sb.sb_imax_pct;
  1219. scounter += delta;
  1220. if (scounter < 0) {
  1221. ASSERT(0);
  1222. return (XFS_ERROR(EINVAL));
  1223. }
  1224. mp->m_sb.sb_imax_pct = scounter;
  1225. return (0);
  1226. case XFS_SBS_REXTSIZE:
  1227. scounter = mp->m_sb.sb_rextsize;
  1228. scounter += delta;
  1229. if (scounter < 0) {
  1230. ASSERT(0);
  1231. return (XFS_ERROR(EINVAL));
  1232. }
  1233. mp->m_sb.sb_rextsize = scounter;
  1234. return (0);
  1235. case XFS_SBS_RBMBLOCKS:
  1236. scounter = mp->m_sb.sb_rbmblocks;
  1237. scounter += delta;
  1238. if (scounter < 0) {
  1239. ASSERT(0);
  1240. return (XFS_ERROR(EINVAL));
  1241. }
  1242. mp->m_sb.sb_rbmblocks = scounter;
  1243. return (0);
  1244. case XFS_SBS_RBLOCKS:
  1245. lcounter = (long long)mp->m_sb.sb_rblocks;
  1246. lcounter += delta;
  1247. if (lcounter < 0) {
  1248. ASSERT(0);
  1249. return (XFS_ERROR(EINVAL));
  1250. }
  1251. mp->m_sb.sb_rblocks = lcounter;
  1252. return (0);
  1253. case XFS_SBS_REXTENTS:
  1254. lcounter = (long long)mp->m_sb.sb_rextents;
  1255. lcounter += delta;
  1256. if (lcounter < 0) {
  1257. ASSERT(0);
  1258. return (XFS_ERROR(EINVAL));
  1259. }
  1260. mp->m_sb.sb_rextents = lcounter;
  1261. return (0);
  1262. case XFS_SBS_REXTSLOG:
  1263. scounter = mp->m_sb.sb_rextslog;
  1264. scounter += delta;
  1265. if (scounter < 0) {
  1266. ASSERT(0);
  1267. return (XFS_ERROR(EINVAL));
  1268. }
  1269. mp->m_sb.sb_rextslog = scounter;
  1270. return (0);
  1271. default:
  1272. ASSERT(0);
  1273. return (XFS_ERROR(EINVAL));
  1274. }
  1275. }
  1276. /*
  1277. * xfs_mod_incore_sb() is used to change a field in the in-core
  1278. * superblock structure by the specified delta. This modification
  1279. * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
  1280. * routine to do the work.
  1281. */
  1282. int
  1283. xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
  1284. {
  1285. unsigned long s;
  1286. int status;
  1287. s = XFS_SB_LOCK(mp);
  1288. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1289. XFS_SB_UNLOCK(mp, s);
  1290. return (status);
  1291. }
  1292. /*
  1293. * xfs_mod_incore_sb_batch() is used to change more than one field
  1294. * in the in-core superblock structure at a time. This modification
  1295. * is protected by a lock internal to this module. The fields and
  1296. * changes to those fields are specified in the array of xfs_mod_sb
  1297. * structures passed in.
  1298. *
  1299. * Either all of the specified deltas will be applied or none of
  1300. * them will. If any modified field dips below 0, then all modifications
  1301. * will be backed out and EINVAL will be returned.
  1302. */
  1303. int
  1304. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1305. {
  1306. unsigned long s;
  1307. int status=0;
  1308. xfs_mod_sb_t *msbp;
  1309. /*
  1310. * Loop through the array of mod structures and apply each
  1311. * individually. If any fail, then back out all those
  1312. * which have already been applied. Do all of this within
  1313. * the scope of the SB_LOCK so that all of the changes will
  1314. * be atomic.
  1315. */
  1316. s = XFS_SB_LOCK(mp);
  1317. msbp = &msb[0];
  1318. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1319. /*
  1320. * Apply the delta at index n. If it fails, break
  1321. * from the loop so we'll fall into the undo loop
  1322. * below.
  1323. */
  1324. status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1325. msbp->msb_delta, rsvd);
  1326. if (status != 0) {
  1327. break;
  1328. }
  1329. }
  1330. /*
  1331. * If we didn't complete the loop above, then back out
  1332. * any changes made to the superblock. If you add code
  1333. * between the loop above and here, make sure that you
  1334. * preserve the value of status. Loop back until
  1335. * we step below the beginning of the array. Make sure
  1336. * we don't touch anything back there.
  1337. */
  1338. if (status != 0) {
  1339. msbp--;
  1340. while (msbp >= msb) {
  1341. status = xfs_mod_incore_sb_unlocked(mp,
  1342. msbp->msb_field, -(msbp->msb_delta), rsvd);
  1343. ASSERT(status == 0);
  1344. msbp--;
  1345. }
  1346. }
  1347. XFS_SB_UNLOCK(mp, s);
  1348. return (status);
  1349. }
  1350. /*
  1351. * xfs_getsb() is called to obtain the buffer for the superblock.
  1352. * The buffer is returned locked and read in from disk.
  1353. * The buffer should be released with a call to xfs_brelse().
  1354. *
  1355. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1356. * the superblock buffer if it can be locked without sleeping.
  1357. * If it can't then we'll return NULL.
  1358. */
  1359. xfs_buf_t *
  1360. xfs_getsb(
  1361. xfs_mount_t *mp,
  1362. int flags)
  1363. {
  1364. xfs_buf_t *bp;
  1365. ASSERT(mp->m_sb_bp != NULL);
  1366. bp = mp->m_sb_bp;
  1367. if (flags & XFS_BUF_TRYLOCK) {
  1368. if (!XFS_BUF_CPSEMA(bp)) {
  1369. return NULL;
  1370. }
  1371. } else {
  1372. XFS_BUF_PSEMA(bp, PRIBIO);
  1373. }
  1374. XFS_BUF_HOLD(bp);
  1375. ASSERT(XFS_BUF_ISDONE(bp));
  1376. return (bp);
  1377. }
  1378. /*
  1379. * Used to free the superblock along various error paths.
  1380. */
  1381. void
  1382. xfs_freesb(
  1383. xfs_mount_t *mp)
  1384. {
  1385. xfs_buf_t *bp;
  1386. /*
  1387. * Use xfs_getsb() so that the buffer will be locked
  1388. * when we call xfs_buf_relse().
  1389. */
  1390. bp = xfs_getsb(mp, 0);
  1391. XFS_BUF_UNMANAGE(bp);
  1392. xfs_buf_relse(bp);
  1393. mp->m_sb_bp = NULL;
  1394. }
  1395. /*
  1396. * See if the UUID is unique among mounted XFS filesystems.
  1397. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1398. */
  1399. STATIC int
  1400. xfs_uuid_mount(
  1401. xfs_mount_t *mp)
  1402. {
  1403. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1404. cmn_err(CE_WARN,
  1405. "XFS: Filesystem %s has nil UUID - can't mount",
  1406. mp->m_fsname);
  1407. return -1;
  1408. }
  1409. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1410. cmn_err(CE_WARN,
  1411. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1412. mp->m_fsname);
  1413. return -1;
  1414. }
  1415. return 0;
  1416. }
  1417. /*
  1418. * Remove filesystem from the UUID table.
  1419. */
  1420. STATIC void
  1421. xfs_uuid_unmount(
  1422. xfs_mount_t *mp)
  1423. {
  1424. uuid_table_remove(&mp->m_sb.sb_uuid);
  1425. }
  1426. /*
  1427. * Used to log changes to the superblock unit and width fields which could
  1428. * be altered by the mount options. Only the first superblock is updated.
  1429. */
  1430. STATIC void
  1431. xfs_mount_log_sbunit(
  1432. xfs_mount_t *mp,
  1433. __int64_t fields)
  1434. {
  1435. xfs_trans_t *tp;
  1436. ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
  1437. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1438. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1439. XFS_DEFAULT_LOG_COUNT)) {
  1440. xfs_trans_cancel(tp, 0);
  1441. return;
  1442. }
  1443. xfs_mod_sb(tp, fields);
  1444. xfs_trans_commit(tp, 0, NULL);
  1445. }