xfs_mount.c 41 KB

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