jfs_mount.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. /* map further access of per fileset inodes by the fileset inode */
  157. sbi->ipimap = ipimap;
  158. /* initialize fileset inode allocation map */
  159. if ((rc = diMount(ipimap))) {
  160. jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
  161. goto err_ipimap;
  162. }
  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. diUnmount(sbi->ipimap, 1);
  212. if ((rc = diMount(sbi->ipimap))) {
  213. jfs_err("jfs_mount_rw: diMount failed!");
  214. return rc;
  215. }
  216. dbUnmount(sbi->ipbmap, 1);
  217. if ((rc = dbMount(sbi->ipbmap))) {
  218. jfs_err("jfs_mount_rw: dbMount failed!");
  219. return rc;
  220. }
  221. }
  222. /*
  223. * open/initialize log
  224. */
  225. if ((rc = lmLogOpen(sb)))
  226. return rc;
  227. /*
  228. * update file system superblock;
  229. */
  230. if ((rc = updateSuper(sb, FM_MOUNT))) {
  231. jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
  232. lmLogClose(sb);
  233. return rc;
  234. }
  235. /*
  236. * write MOUNT log record of the file system
  237. */
  238. logMOUNT(sb);
  239. return rc;
  240. }
  241. /*
  242. * chkSuper()
  243. *
  244. * validate the superblock of the file system to be mounted and
  245. * get the file system parameters.
  246. *
  247. * returns
  248. * 0 with fragsize set if check successful
  249. * error code if not successful
  250. */
  251. static int chkSuper(struct super_block *sb)
  252. {
  253. int rc = 0;
  254. struct jfs_sb_info *sbi = JFS_SBI(sb);
  255. struct jfs_superblock *j_sb;
  256. struct buffer_head *bh;
  257. int AIM_bytesize, AIT_bytesize;
  258. int expected_AIM_bytesize, expected_AIT_bytesize;
  259. s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
  260. s64 byte_addr_diff0, byte_addr_diff1;
  261. s32 bsize;
  262. if ((rc = readSuper(sb, &bh)))
  263. return rc;
  264. j_sb = (struct jfs_superblock *)bh->b_data;
  265. /*
  266. * validate superblock
  267. */
  268. /* validate fs signature */
  269. if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
  270. le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
  271. rc = -EINVAL;
  272. goto out;
  273. }
  274. bsize = le32_to_cpu(j_sb->s_bsize);
  275. if (bsize != PSIZE) {
  276. jfs_err("Only 4K block size supported!");
  277. rc = -EINVAL;
  278. goto out;
  279. }
  280. jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
  281. le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
  282. (unsigned long long) le64_to_cpu(j_sb->s_size));
  283. /* validate the descriptors for Secondary AIM and AIT */
  284. if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
  285. cpu_to_le32(JFS_BAD_SAIT)) {
  286. expected_AIM_bytesize = 2 * PSIZE;
  287. AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
  288. expected_AIT_bytesize = 4 * PSIZE;
  289. AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
  290. AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
  291. AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
  292. byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
  293. fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
  294. byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
  295. if ((AIM_bytesize != expected_AIM_bytesize) ||
  296. (AIT_bytesize != expected_AIT_bytesize) ||
  297. (byte_addr_diff0 != AIM_bytesize) ||
  298. (byte_addr_diff1 <= AIT_bytesize))
  299. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  300. }
  301. if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
  302. cpu_to_le32(JFS_GROUPCOMMIT))
  303. j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
  304. /* validate fs state */
  305. if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
  306. !sb_rdonly(sb)) {
  307. jfs_err("jfs_mount: Mount Failure: File System Dirty.");
  308. rc = -EINVAL;
  309. goto out;
  310. }
  311. sbi->state = le32_to_cpu(j_sb->s_state);
  312. sbi->mntflag = le32_to_cpu(j_sb->s_flag);
  313. /*
  314. * JFS always does I/O by 4K pages. Don't tell the buffer cache
  315. * that we use anything else (leave s_blocksize alone).
  316. */
  317. sbi->bsize = bsize;
  318. sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
  319. /* check some fields for possible corruption */
  320. if (sbi->l2bsize != ilog2((u32)bsize) ||
  321. j_sb->pad != 0 ||
  322. le32_to_cpu(j_sb->s_state) > FM_STATE_MAX) {
  323. rc = -EINVAL;
  324. jfs_err("jfs_mount: Mount Failure: superblock is corrupt!");
  325. goto out;
  326. }
  327. /*
  328. * For now, ignore s_pbsize, l2bfactor. All I/O going through buffer
  329. * cache.
  330. */
  331. sbi->nbperpage = PSIZE >> sbi->l2bsize;
  332. sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
  333. sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
  334. if (sbi->mntflag & JFS_INLINELOG)
  335. sbi->logpxd = j_sb->s_logpxd;
  336. else {
  337. sbi->logdev = new_decode_dev(le32_to_cpu(j_sb->s_logdev));
  338. uuid_copy(&sbi->uuid, &j_sb->s_uuid);
  339. uuid_copy(&sbi->loguuid, &j_sb->s_loguuid);
  340. }
  341. sbi->fsckpxd = j_sb->s_fsckpxd;
  342. sbi->ait2 = j_sb->s_ait2;
  343. out:
  344. brelse(bh);
  345. return rc;
  346. }
  347. /*
  348. * updateSuper()
  349. *
  350. * update synchronously superblock if it is mounted read-write.
  351. */
  352. int updateSuper(struct super_block *sb, uint state)
  353. {
  354. struct jfs_superblock *j_sb;
  355. struct jfs_sb_info *sbi = JFS_SBI(sb);
  356. struct buffer_head *bh;
  357. int rc;
  358. if (sbi->flag & JFS_NOINTEGRITY) {
  359. if (state == FM_DIRTY) {
  360. sbi->p_state = state;
  361. return 0;
  362. } else if (state == FM_MOUNT) {
  363. sbi->p_state = sbi->state;
  364. state = FM_DIRTY;
  365. } else if (state == FM_CLEAN) {
  366. state = sbi->p_state;
  367. } else
  368. jfs_err("updateSuper: bad state");
  369. } else if (sbi->state == FM_DIRTY)
  370. return 0;
  371. if ((rc = readSuper(sb, &bh)))
  372. return rc;
  373. j_sb = (struct jfs_superblock *)bh->b_data;
  374. j_sb->s_state = cpu_to_le32(state);
  375. sbi->state = state;
  376. if (state == FM_MOUNT) {
  377. /* record log's dev_t and mount serial number */
  378. j_sb->s_logdev = cpu_to_le32(new_encode_dev(sbi->log->bdev->bd_dev));
  379. j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
  380. } else if (state == FM_CLEAN) {
  381. /*
  382. * If this volume is shared with OS/2, OS/2 will need to
  383. * recalculate DASD usage, since we don't deal with it.
  384. */
  385. if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
  386. j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
  387. }
  388. mark_buffer_dirty(bh);
  389. sync_dirty_buffer(bh);
  390. brelse(bh);
  391. return 0;
  392. }
  393. /*
  394. * readSuper()
  395. *
  396. * read superblock by raw sector address
  397. */
  398. int readSuper(struct super_block *sb, struct buffer_head **bpp)
  399. {
  400. /* read in primary superblock */
  401. *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
  402. if (*bpp)
  403. return 0;
  404. /* read in secondary/replicated superblock */
  405. *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  406. if (*bpp)
  407. return 0;
  408. return -EIO;
  409. }
  410. /*
  411. * logMOUNT()
  412. *
  413. * function: write a MOUNT log record for file system.
  414. *
  415. * MOUNT record keeps logredo() from processing log records
  416. * for this file system past this point in log.
  417. * it is harmless if mount fails.
  418. *
  419. * note: MOUNT record is at aggregate level, not at fileset level,
  420. * since log records of previous mounts of a fileset
  421. * (e.g., AFTER record of extent allocation) have to be processed
  422. * to update block allocation map at aggregate level.
  423. */
  424. static int logMOUNT(struct super_block *sb)
  425. {
  426. struct jfs_log *log = JFS_SBI(sb)->log;
  427. struct lrd lrd;
  428. lrd.logtid = 0;
  429. lrd.backchain = 0;
  430. lrd.type = cpu_to_le16(LOG_MOUNT);
  431. lrd.length = 0;
  432. lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
  433. lmLog(log, NULL, &lrd, NULL);
  434. return 0;
  435. }