xfs_mount.c 41 KB

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