xfs_mount.c 41 KB

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