jfs_mount.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) International Business Machines Corp., 2000-2004
  4. */
  5. /*
  6. * Module: jfs_mount.c
  7. *
  8. * note: file system in transition to aggregate/fileset:
  9. *
  10. * file system mount is interpreted as the mount of aggregate,
  11. * if not already mounted, and mount of the single/only fileset in
  12. * the aggregate;
  13. *
  14. * a file system/aggregate is represented by an internal inode
  15. * (aka mount inode) initialized with aggregate superblock;
  16. * each vfs represents a fileset, and points to its "fileset inode
  17. * allocation map inode" (aka fileset inode):
  18. * (an aggregate itself is structured recursively as a filset:
  19. * an internal vfs is constructed and points to its "fileset inode
  20. * allocation map inode" (aka aggregate inode) where each inode
  21. * represents a fileset inode) so that inode number is mapped to
  22. * on-disk inode in uniform way at both aggregate and fileset level;
  23. *
  24. * each vnode/inode of a fileset is linked to its vfs (to facilitate
  25. * per fileset inode operations, e.g., unmount of a fileset, etc.);
  26. * each inode points to the mount inode (to facilitate access to
  27. * per aggregate information, e.g., block size, etc.) as well as
  28. * its file set inode.
  29. *
  30. * aggregate
  31. * ipmnt
  32. * mntvfs -> fileset ipimap+ -> aggregate ipbmap -> aggregate ipaimap;
  33. * fileset vfs -> vp(1) <-> ... <-> vp(n) <->vproot;
  34. */
  35. #include <linux/fs.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/log2.h>
  39. #include "jfs_incore.h"
  40. #include "jfs_filsys.h"
  41. #include "jfs_superblock.h"
  42. #include "jfs_dmap.h"
  43. #include "jfs_imap.h"
  44. #include "jfs_metapage.h"
  45. #include "jfs_debug.h"
  46. /*
  47. * forward references
  48. */
  49. static int chkSuper(struct super_block *);
  50. static int logMOUNT(struct super_block *sb);
  51. /*
  52. * NAME: jfs_mount(sb)
  53. *
  54. * FUNCTION: vfs_mount()
  55. *
  56. * PARAMETER: sb - super block
  57. *
  58. * RETURN: -EBUSY - device already mounted or open for write
  59. * -EBUSY - cvrdvp already mounted;
  60. * -EBUSY - mount table full
  61. * -ENOTDIR- cvrdvp not directory on a device mount
  62. * -ENXIO - device open failure
  63. */
  64. int jfs_mount(struct super_block *sb)
  65. {
  66. int rc = 0; /* Return code */
  67. struct jfs_sb_info *sbi = JFS_SBI(sb);
  68. struct inode *ipaimap = NULL;
  69. struct inode *ipaimap2 = NULL;
  70. struct inode *ipimap = NULL;
  71. struct inode *ipbmap = NULL;
  72. /*
  73. * read/validate superblock
  74. * (initialize mount inode from the superblock)
  75. */
  76. if ((rc = chkSuper(sb))) {
  77. goto out;
  78. }
  79. ipaimap = diReadSpecial(sb, AGGREGATE_I, 0);
  80. if (ipaimap == NULL) {
  81. jfs_err("jfs_mount: Failed to read AGGREGATE_I");
  82. rc = -EIO;
  83. goto out;
  84. }
  85. sbi->ipaimap = ipaimap;
  86. jfs_info("jfs_mount: ipaimap:0x%p", ipaimap);
  87. /*
  88. * initialize aggregate inode allocation map
  89. */
  90. if ((rc = diMount(ipaimap))) {
  91. jfs_err("jfs_mount: diMount(ipaimap) failed w/rc = %d", rc);
  92. goto err_ipaimap;
  93. }
  94. /*
  95. * open aggregate block allocation map
  96. */
  97. ipbmap = diReadSpecial(sb, BMAP_I, 0);
  98. if (ipbmap == NULL) {
  99. rc = -EIO;
  100. goto err_umount_ipaimap;
  101. }
  102. jfs_info("jfs_mount: ipbmap:0x%p", ipbmap);
  103. sbi->ipbmap = ipbmap;
  104. /*
  105. * initialize aggregate block allocation map
  106. */
  107. if ((rc = dbMount(ipbmap))) {
  108. jfs_err("jfs_mount: dbMount failed w/rc = %d", rc);
  109. goto err_ipbmap;
  110. }
  111. /*
  112. * open the secondary aggregate inode allocation map
  113. *
  114. * This is a duplicate of the aggregate inode allocation map.
  115. *
  116. * hand craft a vfs in the same fashion as we did to read ipaimap.
  117. * By adding INOSPEREXT (32) to the inode number, we are telling
  118. * diReadSpecial that we are reading from the secondary aggregate
  119. * inode table. This also creates a unique entry in the inode hash
  120. * table.
  121. */
  122. if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
  123. ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
  124. if (!ipaimap2) {
  125. jfs_err("jfs_mount: Failed to read AGGREGATE_I");
  126. rc = -EIO;
  127. goto err_umount_ipbmap;
  128. }
  129. sbi->ipaimap2 = ipaimap2;
  130. jfs_info("jfs_mount: ipaimap2:0x%p", ipaimap2);
  131. /*
  132. * initialize secondary aggregate inode allocation map
  133. */
  134. if ((rc = diMount(ipaimap2))) {
  135. jfs_err("jfs_mount: diMount(ipaimap2) failed, rc = %d",
  136. rc);
  137. goto err_ipaimap2;
  138. }
  139. } else
  140. /* Secondary aggregate inode table is not valid */
  141. sbi->ipaimap2 = NULL;
  142. /*
  143. * mount (the only/single) fileset
  144. */
  145. /*
  146. * open fileset inode allocation map (aka fileset inode)
  147. */
  148. ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
  149. if (ipimap == NULL) {
  150. jfs_err("jfs_mount: Failed to read FILESYSTEM_I");
  151. /* open fileset secondary inode allocation map */
  152. rc = -EIO;
  153. goto err_umount_ipaimap2;
  154. }
  155. jfs_info("jfs_mount: ipimap:0x%p", ipimap);
  156. /* initialize fileset inode allocation map */
  157. if ((rc = diMount(ipimap))) {
  158. jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
  159. goto err_ipimap;
  160. }
  161. /* map further access of per fileset inodes by the fileset inode */
  162. sbi->ipimap = ipimap;
  163. return rc;
  164. /*
  165. * unwind on error
  166. */
  167. err_ipimap:
  168. /* close fileset inode allocation map inode */
  169. diFreeSpecial(ipimap);
  170. err_umount_ipaimap2:
  171. /* close secondary aggregate inode allocation map */
  172. if (ipaimap2)
  173. diUnmount(ipaimap2, 1);
  174. err_ipaimap2:
  175. /* close aggregate inodes */
  176. if (ipaimap2)
  177. diFreeSpecial(ipaimap2);
  178. err_umount_ipbmap: /* close aggregate block allocation map */
  179. dbUnmount(ipbmap, 1);
  180. err_ipbmap: /* close aggregate inodes */
  181. diFreeSpecial(ipbmap);
  182. err_umount_ipaimap: /* close aggregate inode allocation map */
  183. diUnmount(ipaimap, 1);
  184. err_ipaimap: /* close aggregate inodes */
  185. diFreeSpecial(ipaimap);
  186. out:
  187. if (rc)
  188. jfs_err("Mount JFS Failure: %d", rc);
  189. return rc;
  190. }
  191. /*
  192. * NAME: jfs_mount_rw(sb, remount)
  193. *
  194. * FUNCTION: Completes read-write mount, or remounts read-only volume
  195. * as read-write
  196. */
  197. int jfs_mount_rw(struct super_block *sb, int remount)
  198. {
  199. struct jfs_sb_info *sbi = JFS_SBI(sb);
  200. int rc;
  201. /*
  202. * If we are re-mounting a previously read-only volume, we want to
  203. * re-read the inode and block maps, since fsck.jfs may have updated
  204. * them.
  205. */
  206. if (remount) {
  207. if (chkSuper(sb) || (sbi->state != FM_CLEAN))
  208. return -EINVAL;
  209. truncate_inode_pages(sbi->ipimap->i_mapping, 0);
  210. truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
  211. IWRITE_LOCK(sbi->ipimap, RDWRLOCK_IMAP);
  212. diUnmount(sbi->ipimap, 1);
  213. if ((rc = diMount(sbi->ipimap))) {
  214. IWRITE_UNLOCK(sbi->ipimap);
  215. jfs_err("jfs_mount_rw: diMount failed!");
  216. return rc;
  217. }
  218. IWRITE_UNLOCK(sbi->ipimap);
  219. dbUnmount(sbi->ipbmap, 1);
  220. if ((rc = dbMount(sbi->ipbmap))) {
  221. jfs_err("jfs_mount_rw: dbMount failed!");
  222. return rc;
  223. }
  224. }
  225. /*
  226. * open/initialize log
  227. */
  228. if ((rc = lmLogOpen(sb)))
  229. return rc;
  230. /*
  231. * update file system superblock;
  232. */
  233. if ((rc = updateSuper(sb, FM_MOUNT))) {
  234. jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
  235. lmLogClose(sb);
  236. return rc;
  237. }
  238. /*
  239. * write MOUNT log record of the file system
  240. */
  241. logMOUNT(sb);
  242. return rc;
  243. }
  244. /*
  245. * chkSuper()
  246. *
  247. * validate the superblock of the file system to be mounted and
  248. * get the file system parameters.
  249. *
  250. * returns
  251. * 0 with fragsize set if check successful
  252. * error code if not successful
  253. */
  254. static int chkSuper(struct super_block *sb)
  255. {
  256. int rc = 0;
  257. struct jfs_sb_info *sbi = JFS_SBI(sb);
  258. struct jfs_superblock *j_sb;
  259. struct buffer_head *bh;
  260. int AIM_bytesize, AIT_bytesize;
  261. int expected_AIM_bytesize, expected_AIT_bytesize;
  262. s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
  263. s64 byte_addr_diff0, byte_addr_diff1;
  264. s32 bsize;
  265. if ((rc = readSuper(sb, &bh)))
  266. return rc;
  267. j_sb = (struct jfs_superblock *)bh->b_data;
  268. /*
  269. * validate superblock
  270. */
  271. /* validate fs signature */
  272. if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
  273. le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
  274. rc = -EINVAL;
  275. goto out;
  276. }
  277. bsize = le32_to_cpu(j_sb->s_bsize);
  278. if (bsize != PSIZE) {
  279. jfs_err("Only 4K block size supported!");
  280. rc = -EINVAL;
  281. goto out;
  282. }
  283. jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
  284. le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
  285. (unsigned long long) le64_to_cpu(j_sb->s_size));
  286. /* validate the descriptors for Secondary AIM and AIT */
  287. if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
  288. cpu_to_le32(JFS_BAD_SAIT)) {
  289. expected_AIM_bytesize = 2 * PSIZE;
  290. AIM_bytesize = lengthPXD(&j_sb->s_aim2) * bsize;
  291. expected_AIT_bytesize = 4 * PSIZE;
  292. AIT_bytesize = lengthPXD(&j_sb->s_ait2) * bsize;
  293. AIM_byte_addr = addressPXD(&j_sb->s_aim2) * bsize;
  294. AIT_byte_addr = addressPXD(&j_sb->s_ait2) * bsize;
  295. byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
  296. fsckwsp_addr = addressPXD(&j_sb->s_fsckpxd) * bsize;
  297. byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
  298. if ((AIM_bytesize != expected_AIM_bytesize) ||
  299. (AIT_bytesize != expected_AIT_bytesize) ||
  300. (byte_addr_diff0 != AIM_bytesize) ||
  301. (byte_addr_diff1 <= AIT_bytesize))
  302. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  303. }
  304. if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
  305. cpu_to_le32(JFS_GROUPCOMMIT))
  306. j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
  307. /* validate fs state */
  308. if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
  309. !sb_rdonly(sb)) {
  310. jfs_err("jfs_mount: Mount Failure: File System Dirty.");
  311. rc = -EINVAL;
  312. goto out;
  313. }
  314. sbi->state = le32_to_cpu(j_sb->s_state);
  315. sbi->mntflag = le32_to_cpu(j_sb->s_flag);
  316. /*
  317. * JFS always does I/O by 4K pages. Don't tell the buffer cache
  318. * that we use anything else (leave s_blocksize alone).
  319. */
  320. sbi->bsize = bsize;
  321. sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
  322. /* check some fields for possible corruption */
  323. if (sbi->l2bsize != ilog2((u32)bsize) ||
  324. j_sb->pad != 0 ||
  325. le32_to_cpu(j_sb->s_state) > FM_STATE_MAX) {
  326. rc = -EINVAL;
  327. jfs_err("jfs_mount: Mount Failure: superblock is corrupt!");
  328. goto out;
  329. }
  330. /*
  331. * For now, ignore s_pbsize, l2bfactor. All I/O going through buffer
  332. * cache.
  333. */
  334. sbi->nbperpage = PSIZE >> sbi->l2bsize;
  335. sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
  336. sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
  337. if (sbi->mntflag & JFS_INLINELOG)
  338. sbi->logpxd = j_sb->s_logpxd;
  339. else {
  340. sbi->logdev = new_decode_dev(le32_to_cpu(j_sb->s_logdev));
  341. uuid_copy(&sbi->uuid, &j_sb->s_uuid);
  342. uuid_copy(&sbi->loguuid, &j_sb->s_loguuid);
  343. }
  344. sbi->fsckpxd = j_sb->s_fsckpxd;
  345. sbi->ait2 = j_sb->s_ait2;
  346. out:
  347. brelse(bh);
  348. return rc;
  349. }
  350. /*
  351. * updateSuper()
  352. *
  353. * update synchronously superblock if it is mounted read-write.
  354. */
  355. int updateSuper(struct super_block *sb, uint state)
  356. {
  357. struct jfs_superblock *j_sb;
  358. struct jfs_sb_info *sbi = JFS_SBI(sb);
  359. struct buffer_head *bh;
  360. int rc;
  361. if (sbi->flag & JFS_NOINTEGRITY) {
  362. if (state == FM_DIRTY) {
  363. sbi->p_state = state;
  364. return 0;
  365. } else if (state == FM_MOUNT) {
  366. sbi->p_state = sbi->state;
  367. state = FM_DIRTY;
  368. } else if (state == FM_CLEAN) {
  369. state = sbi->p_state;
  370. } else
  371. jfs_err("updateSuper: bad state");
  372. } else if (sbi->state == FM_DIRTY)
  373. return 0;
  374. if ((rc = readSuper(sb, &bh)))
  375. return rc;
  376. j_sb = (struct jfs_superblock *)bh->b_data;
  377. j_sb->s_state = cpu_to_le32(state);
  378. sbi->state = state;
  379. if (state == FM_MOUNT) {
  380. /* record log's dev_t and mount serial number */
  381. j_sb->s_logdev = cpu_to_le32(
  382. new_encode_dev(file_bdev(sbi->log->bdev_file)->bd_dev));
  383. j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
  384. } else if (state == FM_CLEAN) {
  385. /*
  386. * If this volume is shared with OS/2, OS/2 will need to
  387. * recalculate DASD usage, since we don't deal with it.
  388. */
  389. if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
  390. j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
  391. }
  392. mark_buffer_dirty(bh);
  393. sync_dirty_buffer(bh);
  394. brelse(bh);
  395. return 0;
  396. }
  397. /*
  398. * readSuper()
  399. *
  400. * read superblock by raw sector address
  401. */
  402. int readSuper(struct super_block *sb, struct buffer_head **bpp)
  403. {
  404. /* read in primary superblock */
  405. *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
  406. if (*bpp)
  407. return 0;
  408. /* read in secondary/replicated superblock */
  409. *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  410. if (*bpp)
  411. return 0;
  412. return -EIO;
  413. }
  414. /*
  415. * logMOUNT()
  416. *
  417. * function: write a MOUNT log record for file system.
  418. *
  419. * MOUNT record keeps logredo() from processing log records
  420. * for this file system past this point in log.
  421. * it is harmless if mount fails.
  422. *
  423. * note: MOUNT record is at aggregate level, not at fileset level,
  424. * since log records of previous mounts of a fileset
  425. * (e.g., AFTER record of extent allocation) have to be processed
  426. * to update block allocation map at aggregate level.
  427. */
  428. static int logMOUNT(struct super_block *sb)
  429. {
  430. struct jfs_log *log = JFS_SBI(sb)->log;
  431. struct lrd lrd;
  432. lrd.logtid = 0;
  433. lrd.backchain = 0;
  434. lrd.type = cpu_to_le16(LOG_MOUNT);
  435. lrd.length = 0;
  436. lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
  437. lmLog(log, NULL, &lrd, NULL);
  438. return 0;
  439. }