jfs_mount.c 13 KB

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