xfs_mount.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  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 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, MUTEX_DEFAULT, "xfs_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,
  983. XFS_QMOPT_UQUOTA | XFS_QMOPT_GQUOTA | XFS_QMOPT_UMOUNTING);
  984. /*
  985. * Flush out the log synchronously so that we know for sure
  986. * that nothing is pinned. This is important because bflush()
  987. * will skip pinned buffers.
  988. */
  989. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE | XFS_LOG_SYNC);
  990. xfs_binval(mp->m_ddev_targp);
  991. if (mp->m_rtdev_targp) {
  992. xfs_binval(mp->m_rtdev_targp);
  993. }
  994. xfs_unmountfs_writesb(mp);
  995. xfs_unmountfs_wait(mp); /* wait for async bufs */
  996. xfs_log_unmount(mp); /* Done! No more fs ops. */
  997. xfs_freesb(mp);
  998. /*
  999. * All inodes from this mount point should be freed.
  1000. */
  1001. ASSERT(mp->m_inodes == NULL);
  1002. xfs_unmountfs_close(mp, cr);
  1003. if ((mp->m_flags & XFS_MOUNT_NOUUID) == 0)
  1004. xfs_uuid_unmount(mp);
  1005. #if defined(DEBUG) || defined(INDUCE_IO_ERROR)
  1006. /*
  1007. * clear all error tags on this filesystem
  1008. */
  1009. memcpy(&fsid, &vfsp->vfs_fsid, sizeof(int64_t));
  1010. xfs_errortag_clearall_umount(fsid, mp->m_fsname, 0);
  1011. #endif
  1012. XFS_IODONE(vfsp);
  1013. xfs_mount_free(mp, 1);
  1014. return 0;
  1015. }
  1016. void
  1017. xfs_unmountfs_close(xfs_mount_t *mp, struct cred *cr)
  1018. {
  1019. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1020. xfs_free_buftarg(mp->m_logdev_targp, 1);
  1021. if (mp->m_rtdev_targp)
  1022. xfs_free_buftarg(mp->m_rtdev_targp, 1);
  1023. xfs_free_buftarg(mp->m_ddev_targp, 0);
  1024. }
  1025. STATIC void
  1026. xfs_unmountfs_wait(xfs_mount_t *mp)
  1027. {
  1028. if (mp->m_logdev_targp != mp->m_ddev_targp)
  1029. xfs_wait_buftarg(mp->m_logdev_targp);
  1030. if (mp->m_rtdev_targp)
  1031. xfs_wait_buftarg(mp->m_rtdev_targp);
  1032. xfs_wait_buftarg(mp->m_ddev_targp);
  1033. }
  1034. int
  1035. xfs_unmountfs_writesb(xfs_mount_t *mp)
  1036. {
  1037. xfs_buf_t *sbp;
  1038. xfs_sb_t *sb;
  1039. int error = 0;
  1040. /*
  1041. * skip superblock write if fs is read-only, or
  1042. * if we are doing a forced umount.
  1043. */
  1044. sbp = xfs_getsb(mp, 0);
  1045. if (!(XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY ||
  1046. XFS_FORCED_SHUTDOWN(mp))) {
  1047. /*
  1048. * mark shared-readonly if desired
  1049. */
  1050. sb = XFS_BUF_TO_SBP(sbp);
  1051. if (mp->m_mk_sharedro) {
  1052. if (!(sb->sb_flags & XFS_SBF_READONLY))
  1053. sb->sb_flags |= XFS_SBF_READONLY;
  1054. if (!XFS_SB_VERSION_HASSHARED(sb))
  1055. XFS_SB_VERSION_ADDSHARED(sb);
  1056. xfs_fs_cmn_err(CE_NOTE, mp,
  1057. "Unmounting, marking shared read-only");
  1058. }
  1059. XFS_BUF_UNDONE(sbp);
  1060. XFS_BUF_UNREAD(sbp);
  1061. XFS_BUF_UNDELAYWRITE(sbp);
  1062. XFS_BUF_WRITE(sbp);
  1063. XFS_BUF_UNASYNC(sbp);
  1064. ASSERT(XFS_BUF_TARGET(sbp) == mp->m_ddev_targp);
  1065. xfsbdstrat(mp, sbp);
  1066. /* Nevermind errors we might get here. */
  1067. error = xfs_iowait(sbp);
  1068. if (error)
  1069. xfs_ioerror_alert("xfs_unmountfs_writesb",
  1070. mp, sbp, XFS_BUF_ADDR(sbp));
  1071. if (error && mp->m_mk_sharedro)
  1072. xfs_fs_cmn_err(CE_ALERT, mp, "Superblock write error detected while unmounting. Filesystem may not be marked shared readonly");
  1073. }
  1074. xfs_buf_relse(sbp);
  1075. return (error);
  1076. }
  1077. /*
  1078. * xfs_mod_sb() can be used to copy arbitrary changes to the
  1079. * in-core superblock into the superblock buffer to be logged.
  1080. * It does not provide the higher level of locking that is
  1081. * needed to protect the in-core superblock from concurrent
  1082. * access.
  1083. */
  1084. void
  1085. xfs_mod_sb(xfs_trans_t *tp, __int64_t fields)
  1086. {
  1087. xfs_buf_t *bp;
  1088. int first;
  1089. int last;
  1090. xfs_mount_t *mp;
  1091. xfs_sb_t *sbp;
  1092. xfs_sb_field_t f;
  1093. ASSERT(fields);
  1094. if (!fields)
  1095. return;
  1096. mp = tp->t_mountp;
  1097. bp = xfs_trans_getsb(tp, mp, 0);
  1098. sbp = XFS_BUF_TO_SBP(bp);
  1099. first = sizeof(xfs_sb_t);
  1100. last = 0;
  1101. /* translate/copy */
  1102. xfs_xlatesb(XFS_BUF_PTR(bp), &(mp->m_sb), -1, fields);
  1103. /* find modified range */
  1104. f = (xfs_sb_field_t)xfs_lowbit64((__uint64_t)fields);
  1105. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1106. first = xfs_sb_info[f].offset;
  1107. f = (xfs_sb_field_t)xfs_highbit64((__uint64_t)fields);
  1108. ASSERT((1LL << f) & XFS_SB_MOD_BITS);
  1109. last = xfs_sb_info[f + 1].offset - 1;
  1110. xfs_trans_log_buf(tp, bp, first, last);
  1111. }
  1112. /*
  1113. * xfs_mod_incore_sb_unlocked() is a utility routine common used to apply
  1114. * a delta to a specified field in the in-core superblock. Simply
  1115. * switch on the field indicated and apply the delta to that field.
  1116. * Fields are not allowed to dip below zero, so if the delta would
  1117. * do this do not apply it and return EINVAL.
  1118. *
  1119. * The SB_LOCK must be held when this routine is called.
  1120. */
  1121. STATIC int
  1122. xfs_mod_incore_sb_unlocked(xfs_mount_t *mp, xfs_sb_field_t field,
  1123. int delta, int rsvd)
  1124. {
  1125. int scounter; /* short counter for 32 bit fields */
  1126. long long lcounter; /* long counter for 64 bit fields */
  1127. long long res_used, rem;
  1128. /*
  1129. * With the in-core superblock spin lock held, switch
  1130. * on the indicated field. Apply the delta to the
  1131. * proper field. If the fields value would dip below
  1132. * 0, then do not apply the delta and return EINVAL.
  1133. */
  1134. switch (field) {
  1135. case XFS_SBS_ICOUNT:
  1136. lcounter = (long long)mp->m_sb.sb_icount;
  1137. lcounter += delta;
  1138. if (lcounter < 0) {
  1139. ASSERT(0);
  1140. return (XFS_ERROR(EINVAL));
  1141. }
  1142. mp->m_sb.sb_icount = lcounter;
  1143. return (0);
  1144. case XFS_SBS_IFREE:
  1145. lcounter = (long long)mp->m_sb.sb_ifree;
  1146. lcounter += delta;
  1147. if (lcounter < 0) {
  1148. ASSERT(0);
  1149. return (XFS_ERROR(EINVAL));
  1150. }
  1151. mp->m_sb.sb_ifree = lcounter;
  1152. return (0);
  1153. case XFS_SBS_FDBLOCKS:
  1154. lcounter = (long long)mp->m_sb.sb_fdblocks;
  1155. res_used = (long long)(mp->m_resblks - mp->m_resblks_avail);
  1156. if (delta > 0) { /* Putting blocks back */
  1157. if (res_used > delta) {
  1158. mp->m_resblks_avail += delta;
  1159. } else {
  1160. rem = delta - res_used;
  1161. mp->m_resblks_avail = mp->m_resblks;
  1162. lcounter += rem;
  1163. }
  1164. } else { /* Taking blocks away */
  1165. lcounter += delta;
  1166. /*
  1167. * If were out of blocks, use any available reserved blocks if
  1168. * were allowed to.
  1169. */
  1170. if (lcounter < 0) {
  1171. if (rsvd) {
  1172. lcounter = (long long)mp->m_resblks_avail + delta;
  1173. if (lcounter < 0) {
  1174. return (XFS_ERROR(ENOSPC));
  1175. }
  1176. mp->m_resblks_avail = lcounter;
  1177. return (0);
  1178. } else { /* not reserved */
  1179. return (XFS_ERROR(ENOSPC));
  1180. }
  1181. }
  1182. }
  1183. mp->m_sb.sb_fdblocks = lcounter;
  1184. return (0);
  1185. case XFS_SBS_FREXTENTS:
  1186. lcounter = (long long)mp->m_sb.sb_frextents;
  1187. lcounter += delta;
  1188. if (lcounter < 0) {
  1189. return (XFS_ERROR(ENOSPC));
  1190. }
  1191. mp->m_sb.sb_frextents = lcounter;
  1192. return (0);
  1193. case XFS_SBS_DBLOCKS:
  1194. lcounter = (long long)mp->m_sb.sb_dblocks;
  1195. lcounter += delta;
  1196. if (lcounter < 0) {
  1197. ASSERT(0);
  1198. return (XFS_ERROR(EINVAL));
  1199. }
  1200. mp->m_sb.sb_dblocks = lcounter;
  1201. return (0);
  1202. case XFS_SBS_AGCOUNT:
  1203. scounter = mp->m_sb.sb_agcount;
  1204. scounter += delta;
  1205. if (scounter < 0) {
  1206. ASSERT(0);
  1207. return (XFS_ERROR(EINVAL));
  1208. }
  1209. mp->m_sb.sb_agcount = scounter;
  1210. return (0);
  1211. case XFS_SBS_IMAX_PCT:
  1212. scounter = mp->m_sb.sb_imax_pct;
  1213. scounter += delta;
  1214. if (scounter < 0) {
  1215. ASSERT(0);
  1216. return (XFS_ERROR(EINVAL));
  1217. }
  1218. mp->m_sb.sb_imax_pct = scounter;
  1219. return (0);
  1220. case XFS_SBS_REXTSIZE:
  1221. scounter = mp->m_sb.sb_rextsize;
  1222. scounter += delta;
  1223. if (scounter < 0) {
  1224. ASSERT(0);
  1225. return (XFS_ERROR(EINVAL));
  1226. }
  1227. mp->m_sb.sb_rextsize = scounter;
  1228. return (0);
  1229. case XFS_SBS_RBMBLOCKS:
  1230. scounter = mp->m_sb.sb_rbmblocks;
  1231. scounter += delta;
  1232. if (scounter < 0) {
  1233. ASSERT(0);
  1234. return (XFS_ERROR(EINVAL));
  1235. }
  1236. mp->m_sb.sb_rbmblocks = scounter;
  1237. return (0);
  1238. case XFS_SBS_RBLOCKS:
  1239. lcounter = (long long)mp->m_sb.sb_rblocks;
  1240. lcounter += delta;
  1241. if (lcounter < 0) {
  1242. ASSERT(0);
  1243. return (XFS_ERROR(EINVAL));
  1244. }
  1245. mp->m_sb.sb_rblocks = lcounter;
  1246. return (0);
  1247. case XFS_SBS_REXTENTS:
  1248. lcounter = (long long)mp->m_sb.sb_rextents;
  1249. lcounter += delta;
  1250. if (lcounter < 0) {
  1251. ASSERT(0);
  1252. return (XFS_ERROR(EINVAL));
  1253. }
  1254. mp->m_sb.sb_rextents = lcounter;
  1255. return (0);
  1256. case XFS_SBS_REXTSLOG:
  1257. scounter = mp->m_sb.sb_rextslog;
  1258. scounter += delta;
  1259. if (scounter < 0) {
  1260. ASSERT(0);
  1261. return (XFS_ERROR(EINVAL));
  1262. }
  1263. mp->m_sb.sb_rextslog = scounter;
  1264. return (0);
  1265. default:
  1266. ASSERT(0);
  1267. return (XFS_ERROR(EINVAL));
  1268. }
  1269. }
  1270. /*
  1271. * xfs_mod_incore_sb() is used to change a field in the in-core
  1272. * superblock structure by the specified delta. This modification
  1273. * is protected by the SB_LOCK. Just use the xfs_mod_incore_sb_unlocked()
  1274. * routine to do the work.
  1275. */
  1276. int
  1277. xfs_mod_incore_sb(xfs_mount_t *mp, xfs_sb_field_t field, int delta, int rsvd)
  1278. {
  1279. unsigned long s;
  1280. int status;
  1281. s = XFS_SB_LOCK(mp);
  1282. status = xfs_mod_incore_sb_unlocked(mp, field, delta, rsvd);
  1283. XFS_SB_UNLOCK(mp, s);
  1284. return (status);
  1285. }
  1286. /*
  1287. * xfs_mod_incore_sb_batch() is used to change more than one field
  1288. * in the in-core superblock structure at a time. This modification
  1289. * is protected by a lock internal to this module. The fields and
  1290. * changes to those fields are specified in the array of xfs_mod_sb
  1291. * structures passed in.
  1292. *
  1293. * Either all of the specified deltas will be applied or none of
  1294. * them will. If any modified field dips below 0, then all modifications
  1295. * will be backed out and EINVAL will be returned.
  1296. */
  1297. int
  1298. xfs_mod_incore_sb_batch(xfs_mount_t *mp, xfs_mod_sb_t *msb, uint nmsb, int rsvd)
  1299. {
  1300. unsigned long s;
  1301. int status=0;
  1302. xfs_mod_sb_t *msbp;
  1303. /*
  1304. * Loop through the array of mod structures and apply each
  1305. * individually. If any fail, then back out all those
  1306. * which have already been applied. Do all of this within
  1307. * the scope of the SB_LOCK so that all of the changes will
  1308. * be atomic.
  1309. */
  1310. s = XFS_SB_LOCK(mp);
  1311. msbp = &msb[0];
  1312. for (msbp = &msbp[0]; msbp < (msb + nmsb); msbp++) {
  1313. /*
  1314. * Apply the delta at index n. If it fails, break
  1315. * from the loop so we'll fall into the undo loop
  1316. * below.
  1317. */
  1318. status = xfs_mod_incore_sb_unlocked(mp, msbp->msb_field,
  1319. msbp->msb_delta, rsvd);
  1320. if (status != 0) {
  1321. break;
  1322. }
  1323. }
  1324. /*
  1325. * If we didn't complete the loop above, then back out
  1326. * any changes made to the superblock. If you add code
  1327. * between the loop above and here, make sure that you
  1328. * preserve the value of status. Loop back until
  1329. * we step below the beginning of the array. Make sure
  1330. * we don't touch anything back there.
  1331. */
  1332. if (status != 0) {
  1333. msbp--;
  1334. while (msbp >= msb) {
  1335. status = xfs_mod_incore_sb_unlocked(mp,
  1336. msbp->msb_field, -(msbp->msb_delta), rsvd);
  1337. ASSERT(status == 0);
  1338. msbp--;
  1339. }
  1340. }
  1341. XFS_SB_UNLOCK(mp, s);
  1342. return (status);
  1343. }
  1344. /*
  1345. * xfs_getsb() is called to obtain the buffer for the superblock.
  1346. * The buffer is returned locked and read in from disk.
  1347. * The buffer should be released with a call to xfs_brelse().
  1348. *
  1349. * If the flags parameter is BUF_TRYLOCK, then we'll only return
  1350. * the superblock buffer if it can be locked without sleeping.
  1351. * If it can't then we'll return NULL.
  1352. */
  1353. xfs_buf_t *
  1354. xfs_getsb(
  1355. xfs_mount_t *mp,
  1356. int flags)
  1357. {
  1358. xfs_buf_t *bp;
  1359. ASSERT(mp->m_sb_bp != NULL);
  1360. bp = mp->m_sb_bp;
  1361. if (flags & XFS_BUF_TRYLOCK) {
  1362. if (!XFS_BUF_CPSEMA(bp)) {
  1363. return NULL;
  1364. }
  1365. } else {
  1366. XFS_BUF_PSEMA(bp, PRIBIO);
  1367. }
  1368. XFS_BUF_HOLD(bp);
  1369. ASSERT(XFS_BUF_ISDONE(bp));
  1370. return (bp);
  1371. }
  1372. /*
  1373. * Used to free the superblock along various error paths.
  1374. */
  1375. void
  1376. xfs_freesb(
  1377. xfs_mount_t *mp)
  1378. {
  1379. xfs_buf_t *bp;
  1380. /*
  1381. * Use xfs_getsb() so that the buffer will be locked
  1382. * when we call xfs_buf_relse().
  1383. */
  1384. bp = xfs_getsb(mp, 0);
  1385. XFS_BUF_UNMANAGE(bp);
  1386. xfs_buf_relse(bp);
  1387. mp->m_sb_bp = NULL;
  1388. }
  1389. /*
  1390. * See if the UUID is unique among mounted XFS filesystems.
  1391. * Mount fails if UUID is nil or a FS with the same UUID is already mounted.
  1392. */
  1393. STATIC int
  1394. xfs_uuid_mount(
  1395. xfs_mount_t *mp)
  1396. {
  1397. if (uuid_is_nil(&mp->m_sb.sb_uuid)) {
  1398. cmn_err(CE_WARN,
  1399. "XFS: Filesystem %s has nil UUID - can't mount",
  1400. mp->m_fsname);
  1401. return -1;
  1402. }
  1403. if (!uuid_table_insert(&mp->m_sb.sb_uuid)) {
  1404. cmn_err(CE_WARN,
  1405. "XFS: Filesystem %s has duplicate UUID - can't mount",
  1406. mp->m_fsname);
  1407. return -1;
  1408. }
  1409. return 0;
  1410. }
  1411. /*
  1412. * Remove filesystem from the UUID table.
  1413. */
  1414. STATIC void
  1415. xfs_uuid_unmount(
  1416. xfs_mount_t *mp)
  1417. {
  1418. uuid_table_remove(&mp->m_sb.sb_uuid);
  1419. }
  1420. /*
  1421. * Used to log changes to the superblock unit and width fields which could
  1422. * be altered by the mount options. Only the first superblock is updated.
  1423. */
  1424. STATIC void
  1425. xfs_mount_log_sbunit(
  1426. xfs_mount_t *mp,
  1427. __int64_t fields)
  1428. {
  1429. xfs_trans_t *tp;
  1430. ASSERT(fields & (XFS_SB_UNIT|XFS_SB_WIDTH|XFS_SB_UUID));
  1431. tp = xfs_trans_alloc(mp, XFS_TRANS_SB_UNIT);
  1432. if (xfs_trans_reserve(tp, 0, mp->m_sb.sb_sectsize + 128, 0, 0,
  1433. XFS_DEFAULT_LOG_COUNT)) {
  1434. xfs_trans_cancel(tp, 0);
  1435. return;
  1436. }
  1437. xfs_mod_sb(tp, fields);
  1438. xfs_trans_commit(tp, 0, NULL);
  1439. }