xfs_mount.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. * Copyright (c) 2000-2005 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 (%ld) 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) -
  531. XFS_BMDR_SPACE_CALC(MINABTPTRS);
  532. break;
  533. case 512:
  534. case 1024:
  535. case 2048:
  536. mp->m_attroffset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
  537. break;
  538. default:
  539. ASSERT(0);
  540. }
  541. ASSERT(mp->m_attroffset < XFS_LITINO(mp));
  542. for (i = 0; i < 2; i++) {
  543. mp->m_alloc_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  544. xfs_alloc, i == 0);
  545. mp->m_alloc_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  546. xfs_alloc, i == 0);
  547. }
  548. for (i = 0; i < 2; i++) {
  549. mp->m_bmap_dmxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  550. xfs_bmbt, i == 0);
  551. mp->m_bmap_dmnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  552. xfs_bmbt, i == 0);
  553. }
  554. for (i = 0; i < 2; i++) {
  555. mp->m_inobt_mxr[i] = XFS_BTREE_BLOCK_MAXRECS(sbp->sb_blocksize,
  556. xfs_inobt, i == 0);
  557. mp->m_inobt_mnr[i] = XFS_BTREE_BLOCK_MINRECS(sbp->sb_blocksize,
  558. xfs_inobt, i == 0);
  559. }
  560. mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
  561. mp->m_ialloc_inos = (int)MAX((__uint16_t)XFS_INODES_PER_CHUNK,
  562. sbp->sb_inopblock);
  563. mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
  564. }
  565. /*
  566. * xfs_mountfs
  567. *
  568. * This function does the following on an initial mount of a file system:
  569. * - reads the superblock from disk and init the mount struct
  570. * - if we're a 32-bit kernel, do a size check on the superblock
  571. * so we don't mount terabyte filesystems
  572. * - init mount struct realtime fields
  573. * - allocate inode hash table for fs
  574. * - init directory manager
  575. * - perform recovery and init the log manager
  576. */
  577. int
  578. xfs_mountfs(
  579. vfs_t *vfsp,
  580. xfs_mount_t *mp,
  581. int mfsi_flags)
  582. {
  583. xfs_buf_t *bp;
  584. xfs_sb_t *sbp = &(mp->m_sb);
  585. xfs_inode_t *rip;
  586. vnode_t *rvp = NULL;
  587. int readio_log, writeio_log;
  588. xfs_daddr_t d;
  589. __uint64_t ret64;
  590. __int64_t update_flags;
  591. uint quotamount, quotaflags;
  592. int agno;
  593. int uuid_mounted = 0;
  594. int error = 0;
  595. if (mp->m_sb_bp == NULL) {
  596. if ((error = xfs_readsb(mp))) {
  597. return (error);
  598. }
  599. }
  600. xfs_mount_common(mp, sbp);
  601. /*
  602. * Check if sb_agblocks is aligned at stripe boundary
  603. * If sb_agblocks is NOT aligned turn off m_dalign since
  604. * allocator alignment is within an ag, therefore ag has
  605. * to be aligned at stripe boundary.
  606. */
  607. update_flags = 0LL;
  608. if (mp->m_dalign && !(mfsi_flags & XFS_MFSI_SECOND)) {
  609. /*
  610. * If stripe unit and stripe width are not multiples
  611. * of the fs blocksize turn off alignment.
  612. */
  613. if ((BBTOB(mp->m_dalign) & mp->m_blockmask) ||
  614. (BBTOB(mp->m_swidth) & mp->m_blockmask)) {
  615. if (mp->m_flags & XFS_MOUNT_RETERR) {
  616. cmn_err(CE_WARN,
  617. "XFS: alignment check 1 failed");
  618. error = XFS_ERROR(EINVAL);
  619. goto error1;
  620. }
  621. mp->m_dalign = mp->m_swidth = 0;
  622. } else {
  623. /*
  624. * Convert the stripe unit and width to FSBs.
  625. */
  626. mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
  627. if (mp->m_dalign && (sbp->sb_agblocks % mp->m_dalign)) {
  628. if (mp->m_flags & XFS_MOUNT_RETERR) {
  629. error = XFS_ERROR(EINVAL);
  630. goto error1;
  631. }
  632. xfs_fs_cmn_err(CE_WARN, mp,
  633. "stripe alignment turned off: sunit(%d)/swidth(%d) incompatible with agsize(%d)",
  634. mp->m_dalign, mp->m_swidth,
  635. sbp->sb_agblocks);
  636. mp->m_dalign = 0;
  637. mp->m_swidth = 0;
  638. } else if (mp->m_dalign) {
  639. mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
  640. } else {
  641. if (mp->m_flags & XFS_MOUNT_RETERR) {
  642. xfs_fs_cmn_err(CE_WARN, mp,
  643. "stripe alignment turned off: sunit(%d) less than bsize(%d)",
  644. mp->m_dalign,
  645. mp->m_blockmask +1);
  646. error = XFS_ERROR(EINVAL);
  647. goto error1;
  648. }
  649. mp->m_swidth = 0;
  650. }
  651. }
  652. /*
  653. * Update superblock with new values
  654. * and log changes
  655. */
  656. if (XFS_SB_VERSION_HASDALIGN(sbp)) {
  657. if (sbp->sb_unit != mp->m_dalign) {
  658. sbp->sb_unit = mp->m_dalign;
  659. update_flags |= XFS_SB_UNIT;
  660. }
  661. if (sbp->sb_width != mp->m_swidth) {
  662. sbp->sb_width = mp->m_swidth;
  663. update_flags |= XFS_SB_WIDTH;
  664. }
  665. }
  666. } else if ((mp->m_flags & XFS_MOUNT_NOALIGN) != XFS_MOUNT_NOALIGN &&
  667. XFS_SB_VERSION_HASDALIGN(&mp->m_sb)) {
  668. mp->m_dalign = sbp->sb_unit;
  669. mp->m_swidth = sbp->sb_width;
  670. }
  671. xfs_alloc_compute_maxlevels(mp);
  672. xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK);
  673. xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK);
  674. xfs_ialloc_compute_maxlevels(mp);
  675. if (sbp->sb_imax_pct) {
  676. __uint64_t icount;
  677. /* Make sure the maximum inode count is a multiple of the
  678. * units we allocate inodes in.
  679. */
  680. icount = sbp->sb_dblocks * sbp->sb_imax_pct;
  681. do_div(icount, 100);
  682. do_div(icount, mp->m_ialloc_blks);
  683. mp->m_maxicount = (icount * mp->m_ialloc_blks) <<
  684. sbp->sb_inopblog;
  685. } else
  686. mp->m_maxicount = 0;
  687. mp->m_maxioffset = xfs_max_file_offset(sbp->sb_blocklog);
  688. /*
  689. * XFS uses the uuid from the superblock as the unique
  690. * identifier for fsid. We can not use the uuid from the volume
  691. * since a single partition filesystem is identical to a single
  692. * partition volume/filesystem.
  693. */
  694. if ((mfsi_flags & XFS_MFSI_SECOND) == 0 &&
  695. (mp->m_flags & XFS_MOUNT_NOUUID) == 0) {
  696. if (xfs_uuid_mount(mp)) {
  697. error = XFS_ERROR(EINVAL);
  698. goto error1;
  699. }
  700. uuid_mounted=1;
  701. ret64 = uuid_hash64(&sbp->sb_uuid);
  702. memcpy(&vfsp->vfs_fsid, &ret64, sizeof(ret64));
  703. }
  704. /*
  705. * Set the default minimum read and write sizes unless
  706. * already specified in a mount option.
  707. * We use smaller I/O sizes when the file system
  708. * is being used for NFS service (wsync mount option).
  709. */
  710. if (!(mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)) {
  711. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  712. readio_log = XFS_WSYNC_READIO_LOG;
  713. writeio_log = XFS_WSYNC_WRITEIO_LOG;
  714. } else {
  715. readio_log = XFS_READIO_LOG_LARGE;
  716. writeio_log = XFS_WRITEIO_LOG_LARGE;
  717. }
  718. } else {
  719. readio_log = mp->m_readio_log;
  720. writeio_log = mp->m_writeio_log;
  721. }
  722. /*
  723. * Set the number of readahead buffers to use based on
  724. * physical memory size.
  725. */
  726. if (xfs_physmem <= 4096) /* <= 16MB */
  727. mp->m_nreadaheads = XFS_RW_NREADAHEAD_16MB;
  728. else if (xfs_physmem <= 8192) /* <= 32MB */
  729. mp->m_nreadaheads = XFS_RW_NREADAHEAD_32MB;
  730. else
  731. mp->m_nreadaheads = XFS_RW_NREADAHEAD_K32;
  732. if (sbp->sb_blocklog > readio_log) {
  733. mp->m_readio_log = sbp->sb_blocklog;
  734. } else {
  735. mp->m_readio_log = readio_log;
  736. }
  737. mp->m_readio_blocks = 1 << (mp->m_readio_log - sbp->sb_blocklog);
  738. if (sbp->sb_blocklog > writeio_log) {
  739. mp->m_writeio_log = sbp->sb_blocklog;
  740. } else {
  741. mp->m_writeio_log = writeio_log;
  742. }
  743. mp->m_writeio_blocks = 1 << (mp->m_writeio_log - sbp->sb_blocklog);
  744. /*
  745. * Set the inode cluster size based on the physical memory
  746. * size. This may still be overridden by the file system
  747. * block size if it is larger than the chosen cluster size.
  748. */
  749. if (xfs_physmem <= btoc(32 * 1024 * 1024)) { /* <= 32 MB */
  750. mp->m_inode_cluster_size = XFS_INODE_SMALL_CLUSTER_SIZE;
  751. } else {
  752. mp->m_inode_cluster_size = XFS_INODE_BIG_CLUSTER_SIZE;
  753. }
  754. /*
  755. * Set whether we're using inode alignment.
  756. */
  757. if (XFS_SB_VERSION_HASALIGN(&mp->m_sb) &&
  758. mp->m_sb.sb_inoalignmt >=
  759. XFS_B_TO_FSBT(mp, mp->m_inode_cluster_size))
  760. mp->m_inoalign_mask = mp->m_sb.sb_inoalignmt - 1;
  761. else
  762. mp->m_inoalign_mask = 0;
  763. /*
  764. * If we are using stripe alignment, check whether
  765. * the stripe unit is a multiple of the inode alignment
  766. */
  767. if (mp->m_dalign && mp->m_inoalign_mask &&
  768. !(mp->m_dalign & mp->m_inoalign_mask))
  769. mp->m_sinoalign = mp->m_dalign;
  770. else
  771. mp->m_sinoalign = 0;
  772. /*
  773. * Check that the data (and log if separate) are an ok size.
  774. */
  775. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
  776. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) {
  777. cmn_err(CE_WARN, "XFS: size check 1 failed");
  778. error = XFS_ERROR(E2BIG);
  779. goto error1;
  780. }
  781. error = xfs_read_buf(mp, mp->m_ddev_targp,
  782. d - XFS_FSS_TO_BB(mp, 1),
  783. XFS_FSS_TO_BB(mp, 1), 0, &bp);
  784. if (!error) {
  785. xfs_buf_relse(bp);
  786. } else {
  787. cmn_err(CE_WARN, "XFS: size check 2 failed");
  788. if (error == ENOSPC) {
  789. error = XFS_ERROR(E2BIG);
  790. }
  791. goto error1;
  792. }
  793. if (((mfsi_flags & XFS_MFSI_CLIENT) == 0) &&
  794. mp->m_logdev_targp != mp->m_ddev_targp) {
  795. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
  796. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) {
  797. cmn_err(CE_WARN, "XFS: size check 3 failed");
  798. error = XFS_ERROR(E2BIG);
  799. goto error1;
  800. }
  801. error = xfs_read_buf(mp, mp->m_logdev_targp,
  802. d - XFS_FSB_TO_BB(mp, 1),
  803. XFS_FSB_TO_BB(mp, 1), 0, &bp);
  804. if (!error) {
  805. xfs_buf_relse(bp);
  806. } else {
  807. cmn_err(CE_WARN, "XFS: size check 3 failed");
  808. if (error == ENOSPC) {
  809. error = XFS_ERROR(E2BIG);
  810. }
  811. goto error1;
  812. }
  813. }
  814. /*
  815. * Initialize realtime fields in the mount structure
  816. */
  817. if ((error = xfs_rtmount_init(mp))) {
  818. cmn_err(CE_WARN, "XFS: RT mount failed");
  819. goto error1;
  820. }
  821. /*
  822. * For client case we are done now
  823. */
  824. if (mfsi_flags & XFS_MFSI_CLIENT) {
  825. return(0);
  826. }
  827. /*
  828. * Copies the low order bits of the timestamp and the randomly
  829. * set "sequence" number out of a UUID.
  830. */
  831. uuid_getnodeuniq(&sbp->sb_uuid, mp->m_fixedfsid);
  832. /*
  833. * The vfs structure needs to have a file system independent
  834. * way of checking for the invariant file system ID. Since it
  835. * can't look at mount structures it has a pointer to the data
  836. * in the mount structure.
  837. *
  838. * File systems that don't support user level file handles (i.e.
  839. * all of them except for XFS) will leave vfs_altfsid as NULL.
  840. */
  841. vfsp->vfs_altfsid = (xfs_fsid_t *)mp->m_fixedfsid;
  842. mp->m_dmevmask = 0; /* not persistent; set after each mount */
  843. /*
  844. * Select the right directory manager.
  845. */
  846. mp->m_dirops =
  847. XFS_SB_VERSION_HASDIRV2(&mp->m_sb) ?
  848. xfsv2_dirops :
  849. xfsv1_dirops;
  850. /*
  851. * Initialize directory manager's entries.
  852. */
  853. XFS_DIR_MOUNT(mp);
  854. /*
  855. * Initialize the attribute manager's entries.
  856. */
  857. mp->m_attr_magicpct = (mp->m_sb.sb_blocksize * 37) / 100;
  858. /*
  859. * Initialize the precomputed transaction reservations values.
  860. */
  861. xfs_trans_init(mp);
  862. /*
  863. * Allocate and initialize the inode hash table for this
  864. * file system.
  865. */
  866. xfs_ihash_init(mp);
  867. xfs_chash_init(mp);
  868. /*
  869. * Allocate and initialize the per-ag data.
  870. */
  871. init_rwsem(&mp->m_peraglock);
  872. mp->m_perag =
  873. kmem_zalloc(sbp->sb_agcount * sizeof(xfs_perag_t), KM_SLEEP);
  874. mp->m_maxagi = xfs_initialize_perag(mp, sbp->sb_agcount);
  875. /*
  876. * log's mount-time initialization. Perform 1st part recovery if needed
  877. */
  878. if (likely(sbp->sb_logblocks > 0)) { /* check for volume case */
  879. error = xfs_log_mount(mp, mp->m_logdev_targp,
  880. XFS_FSB_TO_DADDR(mp, sbp->sb_logstart),
  881. XFS_FSB_TO_BB(mp, sbp->sb_logblocks));
  882. if (error) {
  883. cmn_err(CE_WARN, "XFS: log mount failed");
  884. goto error2;
  885. }
  886. } else { /* No log has been defined */
  887. cmn_err(CE_WARN, "XFS: no log defined");
  888. XFS_ERROR_REPORT("xfs_mountfs_int(1)", XFS_ERRLEVEL_LOW, mp);
  889. error = XFS_ERROR(EFSCORRUPTED);
  890. goto error2;
  891. }
  892. /*
  893. * Get and sanity-check the root inode.
  894. * Save the pointer to it in the mount structure.
  895. */
  896. error = xfs_iget(mp, NULL, sbp->sb_rootino, 0, XFS_ILOCK_EXCL, &rip, 0);
  897. if (error) {
  898. cmn_err(CE_WARN, "XFS: failed to read root inode");
  899. goto error3;
  900. }
  901. ASSERT(rip != NULL);
  902. rvp = XFS_ITOV(rip);
  903. if (unlikely((rip->i_d.di_mode & S_IFMT) != S_IFDIR)) {
  904. cmn_err(CE_WARN, "XFS: corrupted root inode");
  905. prdev("Root inode %llu is not a directory",
  906. mp->m_ddev_targp, (unsigned long long)rip->i_ino);
  907. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  908. XFS_ERROR_REPORT("xfs_mountfs_int(2)", XFS_ERRLEVEL_LOW,
  909. mp);
  910. error = XFS_ERROR(EFSCORRUPTED);
  911. goto error4;
  912. }
  913. mp->m_rootip = rip; /* save it */
  914. xfs_iunlock(rip, XFS_ILOCK_EXCL);
  915. /*
  916. * Initialize realtime inode pointers in the mount structure
  917. */
  918. if ((error = xfs_rtmount_inodes(mp))) {
  919. /*
  920. * Free up the root inode.
  921. */
  922. cmn_err(CE_WARN, "XFS: failed to read RT inodes");
  923. goto error4;
  924. }
  925. /*
  926. * If fs is not mounted readonly, then update the superblock
  927. * unit and width changes.
  928. */
  929. if (update_flags && !(vfsp->vfs_flag & VFS_RDONLY))
  930. xfs_mount_log_sbunit(mp, update_flags);
  931. /*
  932. * Initialise the XFS quota management subsystem for this mount
  933. */
  934. if ((error = XFS_QM_INIT(mp, &quotamount, &quotaflags)))
  935. goto error4;
  936. /*
  937. * Finish recovering the file system. This part needed to be
  938. * delayed until after the root and real-time bitmap inodes
  939. * were consistently read in.
  940. */
  941. error = xfs_log_mount_finish(mp, mfsi_flags);
  942. if (error) {
  943. cmn_err(CE_WARN, "XFS: log mount finish failed");
  944. goto error4;
  945. }
  946. /*
  947. * Complete the quota initialisation, post-log-replay component.
  948. */
  949. if ((error = XFS_QM_MOUNT(mp, quotamount, quotaflags, mfsi_flags)))
  950. goto error4;
  951. return 0;
  952. error4:
  953. /*
  954. * Free up the root inode.
  955. */
  956. VN_RELE(rvp);
  957. error3:
  958. xfs_log_unmount_dealloc(mp);
  959. error2:
  960. xfs_ihash_free(mp);
  961. xfs_chash_free(mp);
  962. for (agno = 0; agno < sbp->sb_agcount; agno++)
  963. if (mp->m_perag[agno].pagb_list)
  964. kmem_free(mp->m_perag[agno].pagb_list,
  965. sizeof(xfs_perag_busy_t) * XFS_PAGB_NUM_SLOTS);
  966. kmem_free(mp->m_perag, sbp->sb_agcount * sizeof(xfs_perag_t));
  967. mp->m_perag = NULL;
  968. /* FALLTHROUGH */
  969. error1:
  970. if (uuid_mounted)
  971. xfs_uuid_unmount(mp);
  972. xfs_freesb(mp);
  973. return error;
  974. }
  975. /*
  976. * xfs_unmountfs
  977. *
  978. * This flushes out the inodes,dquots and the superblock, unmounts the
  979. * log and makes sure that incore structures are freed.
  980. */
  981. int
  982. xfs_unmountfs(xfs_mount_t *mp, struct cred *cr)
  983. {
  984. struct vfs *vfsp = XFS_MTOVFS(mp);
  985. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  986. int64_t fsid;
  987. #endif
  988. xfs_iflush_all(mp);
  989. XFS_QM_DQPURGEALL(mp,
  990. XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING);
  991. /*
  992. * Flush out the log synchronously so that we know for sure
  993. * that nothing is pinned. This is important because bflush()
  994. * will skip pinned buffers.
  995. */
  996. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  997. xfs_binval(mp->m_ddev_targp);
  998. if (mp->m_rtdev_targp) {
  999. xfs_binval(mp->m_rtdev_targp);
  1000. }
  1001. xfs_unmountfs_writesb(mp);
  1002. xfs_unmountfs_wait(mp); /* wait for async bufs */
  1003. xfs_log_unmount(mp); /* Done! No more fs ops. */
  1004. xfs_freesb(mp);
  1005. /*
  1006. * All inodes from this mount point should be freed.
  1007. */
  1008. ASSERT(mp->m_inodes == NULL);
  1009. xfs_unmountfs_close(mp, cr);
  1010. if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
  1011. xfs_uuid_unmount(mp);
  1012. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  1013. /*
  1014. * clear all error tags on this filesystem
  1015. */
  1016. memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
  1017. xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
  1018. #endif
  1019. XFS_IODONE(vfsp);
  1020. xfs_mount_free(mp, 1);
  1021. return 0;
  1022. }
  1023. void
  1024. xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
  1025. {
  1026. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1027. xfs_free_buftarg(mp->m_logdev_targp, 1);
  1028. if (mp->m_rtdev_targp)
  1029. xfs_free_buftarg(mp->m_rtdev_targp, 1);
  1030. xfs_free_buftarg(mp->m_ddev_targp, 0);
  1031. }
  1032. STATIC void
  1033. xfs_unmountfs_wait(xfs_mount_t *mp)
  1034. {
  1035. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1036. xfs_wait_buftarg(mp->m_logdev_targp);
  1037. if (mp->m_rtdev_targp)
  1038. xfs_wait_buftarg(mp->m_rtdev_targp);
  1039. xfs_wait_buftarg(mp->m_ddev_targp);
  1040. }
  1041. int
  1042. xfs_unmountfs_writesb(xfs_mount_t *mp)
  1043. {
  1044. xfs_buf_t *sbp;
  1045. xfs_sb_t *sb;
  1046. int error = 0;
  1047. /*
  1048. * skip superblock write if fs is read-only, or
  1049. * if we are doing a forced umount.
  1050. */
  1051. sbp = xfs_getsb(mp, 0);
  1052. if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
  1053. XFS_FORCED_SHUTDOWN(mp))) {
  1054. /*
  1055. * mark shared-readonly if desired
  1056. */
  1057. sb = XFS_BUF_TO_SBP(sbp);
  1058. if (mp->m_mk_sharedro) {
  1059. if (!(sb->sb_flags & XFS_SBF_READONLY))
  1060. sb->sb_flags |= XFS_SBF_READONLY;
  1061. if (!XFS_SB_VERSION_HASSHARED(sb))
  1062. XFS_SB_VERSION_ADDSHARED(sb);
  1063. xfs_fs_cmn_err(CE_NOTE, mp,
  1064. "Unmounting, marking shared read-only");
  1065. }
  1066. XFS_BUF_UNDONE(sbp);
  1067. XFS_BUF_UNREAD(sbp);
  1068. XFS_BUF_UNDELAYWRITE(sbp);
  1069. XFS_BUF_WRITE(sbp);
  1070. XFS_BUF_UNASYNC(sbp);
  1071. ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
  1072. xfsbdstrat(mp, sbp);
  1073. /* Nevermind errors we might get here. */
  1074. error = xfs_iowait(sbp);
  1075. if (error)
  1076. xfs_ioerror_alert("xfs_unmountfs_writesb",
  1077. mp, sbp, XFS_BUF_ADDR(sbp));
  1078. if (error && mp->m_mk_sharedro)
  1079. xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
  1080. }
  1081. xfs_buf_relse(sbp);
  1082. return (error);
  1083. }
  1084. /*
  1085. * xfs_mod_sb() can be used to copy arbitrary changes to the
  1086. * in-core superblock into the superblock buffer to be logged.
  1087. * It does not provide the higher level of locking that is
  1088. * needed to protect the in-core superblock from concurrent
  1089. * access.
  1090. */
  1091. void
  1092. xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
  1093. {
  1094. xfs_buf_t *bp;
  1095. int first;
  1096. int last;
  1097. xfs_mount_t *mp;
  1098. xfs_sb_t *sbp;
  1099. xfs_sb_field_t f;
  1100. ASSERT(fields);
  1101. if (!fields)
  1102. return;
  1103. mp = tp->t_mountp;
  1104. bp = xfs_trans_getsb(tp, mp, 0);
  1105. sbp = XFS_BUF_TO_SBP(bp);
  1106. first = sizeof(xfs_sb_t);
  1107. last = 0;
  1108. /* translate/copy */
  1109. xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
  1110. /* find modified range */
  1111. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  1112. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1113. first = xfs_sb_info[f].offset;
  1114. f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
  1115. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1116. last = xfs_sb_info[f + 1].offset - 1;
  1117. xfs_trans_log_buf(tp, bp, first, last);
  1118. }
  1119. /*
  1120. * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
  1121. * a delta to a specified field in the in-core superblock. Simply
  1122. * switch on the field indicated and apply the delta to that field.
  1123. * Fields are not allowed to dip below zero, so if the delta would
  1124. * do this do not apply it and return EINVAL.
  1125. *
  1126. * The SB_LOCK must be held when this routine is called.
  1127. */
  1128. STATIC int
  1129. xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
  1130. int delta, int rsvd)
  1131. {
  1132. int scounter; /* short counter for 32 bit fields */
  1133. long long lcounter; /* long counter for 64 bit fields */
  1134. long long res_used, rem;
  1135. /*
  1136. * With the in-core superblock spin lock held, switch
  1137. * on the indicated field. Apply the delta to the
  1138. * proper field. If the fields value would dip below
  1139. * 0, then do not apply the delta and return EINVAL.
  1140. */
  1141. switch (field) {
  1142. case XFS_SBS_ICOUNT:
  1143. lcounter = (long long)mp->m_sb.sb_icount;
  1144. lcounter += delta;
  1145. if (lcounter < 0) {
  1146. ASSERT(0);
  1147. return (XFS_ERROR(EINVAL));
  1148. }
  1149. mp->m_sb.sb_icount = lcounter;
  1150. return (0);
  1151. case XFS_SBS_IFREE:
  1152. lcounter = (long long)mp->m_sb.sb_ifree;
  1153. lcounter += delta;
  1154. if (lcounter < 0) {
  1155. ASSERT(0);
  1156. return (XFS_ERROR(EINVAL));
  1157. }
  1158. mp->m_sb.sb_ifree = lcounter;
  1159. return (0);
  1160. case XFS_SBS_FDBLOCKS:
  1161. lcounter = (long long)mp->m_sb.sb_fdblocks;
  1162. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1163. if (delta > 0) { /* Putting blocks back */
  1164. if (res_used > delta) {
  1165. mp->m_resblks_avail += delta;
  1166. } else {
  1167. rem = delta - res_used;
  1168. mp->m_resblks_avail = mp->m_resblks;
  1169. lcounter += rem;
  1170. }
  1171. } else { /* Taking blocks away */
  1172. lcounter += delta;
  1173. /*
  1174. * If were out of blocks, use any available reserved blocks if
  1175. * were allowed to.
  1176. */
  1177. if (lcounter < 0) {
  1178. if (rsvd) {
  1179. lcounter = (long long)mp->m_resblks_avail + delta;
  1180. if (lcounter < 0) {
  1181. return (XFS_ERROR(ENOSPC));
  1182. }
  1183. mp->m_resblks_avail = lcounter;
  1184. return (0);
  1185. } else { /* not reserved */
  1186. return (XFS_ERROR(ENOSPC));
  1187. }
  1188. }
  1189. }
  1190. mp->m_sb.sb_fdblocks = lcounter;
  1191. return (0);
  1192. case XFS_SBS_FREXTENTS:
  1193. lcounter = (long long)mp->m_sb.sb_frextents;
  1194. lcounter += delta;
  1195. if (lcounter < 0) {
  1196. return (XFS_ERROR(ENOSPC));
  1197. }
  1198. mp->m_sb.sb_frextents = lcounter;
  1199. return (0);
  1200. case XFS_SBS_DBLOCKS:
  1201. lcounter = (long long)mp->m_sb.sb_dblocks;
  1202. lcounter += delta;
  1203. if (lcounter < 0) {
  1204. ASSERT(0);
  1205. return (XFS_ERROR(EINVAL));
  1206. }
  1207. mp->m_sb.sb_dblocks = lcounter;
  1208. return (0);
  1209. case XFS_SBS_AGCOUNT:
  1210. scounter = mp->m_sb.sb_agcount;
  1211. scounter += delta;
  1212. if (scounter < 0) {
  1213. ASSERT(0);
  1214. return (XFS_ERROR(EINVAL));
  1215. }
  1216. mp->m_sb.sb_agcount = scounter;
  1217. return (0);
  1218. case XFS_SBS_IMAX_PCT:
  1219. scounter = mp->m_sb.sb_imax_pct;
  1220. scounter += delta;
  1221. if (scounter < 0) {
  1222. ASSERT(0);
  1223. return (XFS_ERROR(EINVAL));
  1224. }
  1225. mp->m_sb.sb_imax_pct = scounter;
  1226. return (0);
  1227. case XFS_SBS_REXTSIZE:
  1228. scounter = mp->m_sb.sb_rextsize;
  1229. scounter += delta;
  1230. if (scounter < 0) {
  1231. ASSERT(0);
  1232. return (XFS_ERROR(EINVAL));
  1233. }
  1234. mp->m_sb.sb_rextsize = scounter;
  1235. return (0);
  1236. case XFS_SBS_RBMBLOCKS:
  1237. scounter = mp->m_sb.sb_rbmblocks;
  1238. scounter += delta;
  1239. if (scounter < 0) {
  1240. ASSERT(0);
  1241. return (XFS_ERROR(EINVAL));
  1242. }
  1243. mp->m_sb.sb_rbmblocks = scounter;
  1244. return (0);
  1245. case XFS_SBS_RBLOCKS:
  1246. lcounter = (long long)mp->m_sb.sb_rblocks;
  1247. lcounter += delta;
  1248. if (lcounter < 0) {
  1249. ASSERT(0);
  1250. return (XFS_ERROR(EINVAL));
  1251. }
  1252. mp->m_sb.sb_rblocks = lcounter;
  1253. return (0);
  1254. case XFS_SBS_REXTENTS:
  1255. lcounter = (long long)mp->m_sb.sb_rextents;
  1256. lcounter += delta;
  1257. if (lcounter < 0) {
  1258. ASSERT(0);
  1259. return (XFS_ERROR(EINVAL));
  1260. }
  1261. mp->m_sb.sb_rextents = lcounter;
  1262. return (0);
  1263. case XFS_SBS_REXTSLOG:
  1264. scounter = mp->m_sb.sb_rextslog;
  1265. scounter += delta;
  1266. if (scounter < 0) {
  1267. ASSERT(0);
  1268. return (XFS_ERROR(EINVAL));
  1269. }
  1270. mp->m_sb.sb_rextslog = scounter;
  1271. return (0);
  1272. default:
  1273. ASSERT(0);
  1274. return (XFS_ERROR(EINVAL));
  1275. }
  1276. }
  1277. /*
  1278. * xfs_mod_incore_sb() is used to change a field in the in-core
  1279. * superblock structure by the specified delta. This modification
  1280. * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
  1281. * routine to do the work.
  1282. */
  1283. int
  1284. xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
  1285. {
  1286. unsigned long s;
  1287. int status;
  1288. s = XFS_SB_LOCK(mp);
  1289. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1290. XFS_SB_UNLOCK(mp, s);
  1291. return (status);
  1292. }
  1293. /*
  1294. * xfs_mod_incore_sb_batch() is used to change more than one field
  1295. * in the in-core superblock structure at a time. This modification
  1296. * is protected by a lock internal to this module. The fields and
  1297. * changes to those fields are specified in the array of xfs_mod_sb
  1298. * structures passed in.
  1299. *
  1300. * Either all of the specified deltas will be applied or none of
  1301. * them will. If any modified field dips below 0, then all modifications
  1302. * will be backed out and EINVAL will be returned.
  1303. */
  1304. int
  1305. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1306. {
  1307. unsigned long s;
  1308. int status=0;
  1309. xfs_mod_sb_t *msbp;
  1310. /*
  1311. * Loop through the array of mod structures and apply each
  1312. * individually. If any fail, then back out all those
  1313. * which have already been applied. Do all of this within
  1314. * the scope of the SB_LOCK so that all of the changes will
  1315. * be atomic.
  1316. */
  1317. s = XFS_SB_LOCK(mp);
  1318. msbp = &msb[0];
  1319. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1320. /*
  1321. * Apply the delta at index n. If it fails, break
  1322. * from the loop so we'll fall into the undo loop
  1323. * below.
  1324. */
  1325. status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1326. msbp->msb_delta, rsvd);
  1327. if (status != 0) {
  1328. break;
  1329. }
  1330. }
  1331. /*
  1332. * If we didn't complete the loop above, then back out
  1333. * any changes made to the superblock. If you add code
  1334. * between the loop above and here, make sure that you
  1335. * preserve the value of status. Loop back until
  1336. * we step below the beginning of the array. Make sure
  1337. * we don't touch anything back there.
  1338. */
  1339. if (status != 0) {
  1340. msbp--;
  1341. while (msbp >= msb) {
  1342. status = xfs_mod_incore_sb_unlocked(mp,
  1343. msbp->msb_field, -(msbp->msb_delta), rsvd);
  1344. ASSERT(status == 0);
  1345. msbp--;
  1346. }
  1347. }
  1348. XFS_SB_UNLOCK(mp, s);
  1349. return (status);
  1350. }
  1351. /*
  1352. * xfs_getsb() is called to obtain the buffer for the superblock.
  1353. * The buffer is returned locked and read in from disk.
  1354. * The buffer should be released with a call to xfs_brelse().
  1355. *
  1356. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1357. * the superblock buffer if it can be locked without sleeping.
  1358. * If it can't then we'll return NULL.
  1359. */
  1360. xfs_buf_t *
  1361. xfs_getsb(
  1362. xfs_mount_t *mp,
  1363. int flags)
  1364. {
  1365. xfs_buf_t *bp;
  1366. ASSERT(mp->m_sb_bp != NULL);
  1367. bp = mp->m_sb_bp;
  1368. if (flags & XFS_BUF_TRYLOCK) {
  1369. if (!XFS_BUF_CPSEMA(bp)) {
  1370. return NULL;
  1371. }
  1372. } else {
  1373. XFS_BUF_PSEMA(bp, PRIBIO);
  1374. }
  1375. XFS_BUF_HOLD(bp);
  1376. ASSERT(XFS_BUF_ISDONE(bp));
  1377. return (bp);
  1378. }
  1379. /*
  1380. * Used to free the superblock along various error paths.
  1381. */
  1382. void
  1383. xfs_freesb(
  1384. xfs_mount_t *mp)
  1385. {
  1386. xfs_buf_t *bp;
  1387. /*
  1388. * Use xfs_getsb() so that the buffer will be locked
  1389. * when we call xfs_buf_relse().
  1390. */
  1391. bp = xfs_getsb(mp, 0);
  1392. XFS_BUF_UNMANAGE(bp);
  1393. xfs_buf_relse(bp);
  1394. mp->m_sb_bp = NULL;
  1395. }
  1396. /*
  1397. * See if the UUID is unique among mounted XFS filesystems.
  1398. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1399. */
  1400. STATIC int
  1401. xfs_uuid_mount(
  1402. xfs_mount_t *mp)
  1403. {
  1404. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1405. cmn_err(CE_WARN,
  1406. "XFS: Filesystem %s has nil UUID - can't mount",
  1407. mp->m_fsname);
  1408. return -1;
  1409. }
  1410. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1411. cmn_err(CE_WARN,
  1412. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1413. mp->m_fsname);
  1414. return -1;
  1415. }
  1416. return 0;
  1417. }
  1418. /*
  1419. * Remove filesystem from the UUID table.
  1420. */
  1421. STATIC void
  1422. xfs_uuid_unmount(
  1423. xfs_mount_t *mp)
  1424. {
  1425. uuid_table_remove(&mp->m_sb.sb_uuid);
  1426. }
  1427. /*
  1428. * Used to log changes to the superblock unit and width fields which could
  1429. * be altered by the mount options. Only the first superblock is updated.
  1430. */
  1431. STATIC void
  1432. xfs_mount_log_sbunit(
  1433. xfs_mount_t *mp,
  1434. __int64_t fields)
  1435. {
  1436. xfs_trans_t *tp;
  1437. ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
  1438. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1439. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1440. XFS_DEFAULT_LOG_COUNT)) {
  1441. xfs_trans_cancel(tp, 0);
  1442. return;
  1443. }
  1444. xfs_mod_sb(tp, fields);
  1445. xfs_trans_commit(tp, 0, NULL);
  1446. }