jfs_mount.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "jfs_incore.h"
  51. #include "jfs_filsys.h"
  52. #include "jfs_superblock.h"
  53. #include "jfs_dmap.h"
  54. #include "jfs_imap.h"
  55. #include "jfs_metapage.h"
  56. #include "jfs_debug.h"
  57. /*
  58. * forward references
  59. */
  60. static int chkSuper(struct super_block *);
  61. static int logMOUNT(struct super_block *sb);
  62. /*
  63. * NAME: jfs_mount(sb)
  64. *
  65. * FUNCTION: vfs_mount()
  66. *
  67. * PARAMETER: sb - super block
  68. *
  69. * RETURN: -EBUSY - device already mounted or open for write
  70. * -EBUSY - cvrdvp already mounted;
  71. * -EBUSY - mount table full
  72. * -ENOTDIR- cvrdvp not directory on a device mount
  73. * -ENXIO - device open failure
  74. */
  75. int jfs_mount(struct super_block *sb)
  76. {
  77. int rc = 0; /* Return code */
  78. struct jfs_sb_info *sbi = JFS_SBI(sb);
  79. struct inode *ipaimap = NULL;
  80. struct inode *ipaimap2 = NULL;
  81. struct inode *ipimap = NULL;
  82. struct inode *ipbmap = NULL;
  83. /*
  84. * read/validate superblock
  85. * (initialize mount inode from the superblock)
  86. */
  87. if ((rc = chkSuper(sb))) {
  88. goto errout20;
  89. }
  90. ipaimap = diReadSpecial(sb, AGGREGATE_I, 0);
  91. if (ipaimap == NULL) {
  92. jfs_err("jfs_mount: Faild to read AGGREGATE_I");
  93. rc = -EIO;
  94. goto errout20;
  95. }
  96. sbi->ipaimap = ipaimap;
  97. jfs_info("jfs_mount: ipaimap:0x%p", ipaimap);
  98. /*
  99. * initialize aggregate inode allocation map
  100. */
  101. if ((rc = diMount(ipaimap))) {
  102. jfs_err("jfs_mount: diMount(ipaimap) failed w/rc = %d", rc);
  103. goto errout21;
  104. }
  105. /*
  106. * open aggregate block allocation map
  107. */
  108. ipbmap = diReadSpecial(sb, BMAP_I, 0);
  109. if (ipbmap == NULL) {
  110. rc = -EIO;
  111. goto errout22;
  112. }
  113. jfs_info("jfs_mount: ipbmap:0x%p", ipbmap);
  114. sbi->ipbmap = ipbmap;
  115. /*
  116. * initialize aggregate block allocation map
  117. */
  118. if ((rc = dbMount(ipbmap))) {
  119. jfs_err("jfs_mount: dbMount failed w/rc = %d", rc);
  120. goto errout22;
  121. }
  122. /*
  123. * open the secondary aggregate inode allocation map
  124. *
  125. * This is a duplicate of the aggregate inode allocation map.
  126. *
  127. * hand craft a vfs in the same fashion as we did to read ipaimap.
  128. * By adding INOSPEREXT (32) to the inode number, we are telling
  129. * diReadSpecial that we are reading from the secondary aggregate
  130. * inode table. This also creates a unique entry in the inode hash
  131. * table.
  132. */
  133. if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
  134. ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
  135. if (ipaimap2 == 0) {
  136. jfs_err("jfs_mount: Faild to read AGGREGATE_I");
  137. rc = -EIO;
  138. goto errout35;
  139. }
  140. sbi->ipaimap2 = ipaimap2;
  141. jfs_info("jfs_mount: ipaimap2:0x%p", ipaimap2);
  142. /*
  143. * initialize secondary aggregate inode allocation map
  144. */
  145. if ((rc = diMount(ipaimap2))) {
  146. jfs_err("jfs_mount: diMount(ipaimap2) failed, rc = %d",
  147. rc);
  148. goto errout35;
  149. }
  150. } else
  151. /* Secondary aggregate inode table is not valid */
  152. sbi->ipaimap2 = NULL;
  153. /*
  154. * mount (the only/single) fileset
  155. */
  156. /*
  157. * open fileset inode allocation map (aka fileset inode)
  158. */
  159. ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
  160. if (ipimap == NULL) {
  161. jfs_err("jfs_mount: Failed to read FILESYSTEM_I");
  162. /* open fileset secondary inode allocation map */
  163. rc = -EIO;
  164. goto errout40;
  165. }
  166. jfs_info("jfs_mount: ipimap:0x%p", ipimap);
  167. /* map further access of per fileset inodes by the fileset inode */
  168. sbi->ipimap = ipimap;
  169. /* initialize fileset inode allocation map */
  170. if ((rc = diMount(ipimap))) {
  171. jfs_err("jfs_mount: diMount failed w/rc = %d", rc);
  172. goto errout41;
  173. }
  174. goto out;
  175. /*
  176. * unwind on error
  177. */
  178. errout41: /* close fileset inode allocation map inode */
  179. diFreeSpecial(ipimap);
  180. errout40: /* fileset closed */
  181. /* close secondary aggregate inode allocation map */
  182. if (ipaimap2) {
  183. diUnmount(ipaimap2, 1);
  184. diFreeSpecial(ipaimap2);
  185. }
  186. errout35:
  187. /* close aggregate block allocation map */
  188. dbUnmount(ipbmap, 1);
  189. diFreeSpecial(ipbmap);
  190. errout22: /* close aggregate inode allocation map */
  191. diUnmount(ipaimap, 1);
  192. errout21: /* close aggregate inodes */
  193. diFreeSpecial(ipaimap);
  194. errout20: /* aggregate closed */
  195. out:
  196. if (rc)
  197. jfs_err("Mount JFS Failure: %d", rc);
  198. return rc;
  199. }
  200. /*
  201. * NAME: jfs_mount_rw(sb, remount)
  202. *
  203. * FUNCTION: Completes read-write mount, or remounts read-only volume
  204. * as read-write
  205. */
  206. int jfs_mount_rw(struct super_block *sb, int remount)
  207. {
  208. struct jfs_sb_info *sbi = JFS_SBI(sb);
  209. int rc;
  210. /*
  211. * If we are re-mounting a previously read-only volume, we want to
  212. * re-read the inode and block maps, since fsck.jfs may have updated
  213. * them.
  214. */
  215. if (remount) {
  216. if (chkSuper(sb) || (sbi->state != FM_CLEAN))
  217. return -EINVAL;
  218. truncate_inode_pages(sbi->ipimap->i_mapping, 0);
  219. truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
  220. diUnmount(sbi->ipimap, 1);
  221. if ((rc = diMount(sbi->ipimap))) {
  222. jfs_err("jfs_mount_rw: diMount failed!");
  223. return rc;
  224. }
  225. dbUnmount(sbi->ipbmap, 1);
  226. if ((rc = dbMount(sbi->ipbmap))) {
  227. jfs_err("jfs_mount_rw: dbMount failed!");
  228. return rc;
  229. }
  230. }
  231. /*
  232. * open/initialize log
  233. */
  234. if ((rc = lmLogOpen(sb)))
  235. return rc;
  236. /*
  237. * update file system superblock;
  238. */
  239. if ((rc = updateSuper(sb, FM_MOUNT))) {
  240. jfs_err("jfs_mount: updateSuper failed w/rc = %d", rc);
  241. lmLogClose(sb);
  242. return rc;
  243. }
  244. /*
  245. * write MOUNT log record of the file system
  246. */
  247. logMOUNT(sb);
  248. return rc;
  249. }
  250. /*
  251. * chkSuper()
  252. *
  253. * validate the superblock of the file system to be mounted and
  254. * get the file system parameters.
  255. *
  256. * returns
  257. * 0 with fragsize set if check successful
  258. * error code if not successful
  259. */
  260. static int chkSuper(struct super_block *sb)
  261. {
  262. int rc = 0;
  263. struct jfs_sb_info *sbi = JFS_SBI(sb);
  264. struct jfs_superblock *j_sb;
  265. struct buffer_head *bh;
  266. int AIM_bytesize, AIT_bytesize;
  267. int expected_AIM_bytesize, expected_AIT_bytesize;
  268. s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
  269. s64 byte_addr_diff0, byte_addr_diff1;
  270. s32 bsize;
  271. if ((rc = readSuper(sb, &bh)))
  272. return rc;
  273. j_sb = (struct jfs_superblock *)bh->b_data;
  274. /*
  275. * validate superblock
  276. */
  277. /* validate fs signature */
  278. if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
  279. le32_to_cpu(j_sb->s_version) > JFS_VERSION) {
  280. rc = -EINVAL;
  281. goto out;
  282. }
  283. bsize = le32_to_cpu(j_sb->s_bsize);
  284. #ifdef _JFS_4K
  285. if (bsize != PSIZE) {
  286. jfs_err("Currently only 4K block size supported!");
  287. rc = -EINVAL;
  288. goto out;
  289. }
  290. #endif /* _JFS_4K */
  291. jfs_info("superblock: flag:0x%08x state:0x%08x size:0x%Lx",
  292. le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
  293. (unsigned long long) le64_to_cpu(j_sb->s_size));
  294. /* validate the descriptors for Secondary AIM and AIT */
  295. if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
  296. cpu_to_le32(JFS_BAD_SAIT)) {
  297. expected_AIM_bytesize = 2 * PSIZE;
  298. AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
  299. expected_AIT_bytesize = 4 * PSIZE;
  300. AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
  301. AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
  302. AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
  303. byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
  304. fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
  305. byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
  306. if ((AIM_bytesize != expected_AIM_bytesize) ||
  307. (AIT_bytesize != expected_AIT_bytesize) ||
  308. (byte_addr_diff0 != AIM_bytesize) ||
  309. (byte_addr_diff1 <= AIT_bytesize))
  310. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  311. }
  312. if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
  313. cpu_to_le32(JFS_GROUPCOMMIT))
  314. j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
  315. /* validate fs state */
  316. if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
  317. !(sb->s_flags & MS_RDONLY)) {
  318. jfs_err("jfs_mount: Mount Failure: File System Dirty.");
  319. rc = -EINVAL;
  320. goto out;
  321. }
  322. sbi->state = le32_to_cpu(j_sb->s_state);
  323. sbi->mntflag = le32_to_cpu(j_sb->s_flag);
  324. /*
  325. * JFS always does I/O by 4K pages. Don't tell the buffer cache
  326. * that we use anything else (leave s_blocksize alone).
  327. */
  328. sbi->bsize = bsize;
  329. sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
  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. memcpy(sbi->uuid, j_sb->s_uuid, sizeof(sbi->uuid));
  342. memcpy(sbi->loguuid, j_sb->s_loguuid, sizeof(sbi->uuid));
  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(new_encode_dev(sbi->log->bdev->bd_dev));
  382. j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
  383. } else if (state == FM_CLEAN) {
  384. /*
  385. * If this volume is shared with OS/2, OS/2 will need to
  386. * recalculate DASD usage, since we don't deal with it.
  387. */
  388. if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
  389. j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
  390. }
  391. mark_buffer_dirty(bh);
  392. sync_dirty_buffer(bh);
  393. brelse(bh);
  394. return 0;
  395. }
  396. /*
  397. * readSuper()
  398. *
  399. * read superblock by raw sector address
  400. */
  401. int readSuper(struct super_block *sb, struct buffer_head **bpp)
  402. {
  403. /* read in primary superblock */
  404. *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
  405. if (*bpp)
  406. return 0;
  407. /* read in secondary/replicated superblock */
  408. *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  409. if (*bpp)
  410. return 0;
  411. return -EIO;
  412. }
  413. /*
  414. * logMOUNT()
  415. *
  416. * function: write a MOUNT log record for file system.
  417. *
  418. * MOUNT record keeps logredo() from processing log records
  419. * for this file system past this point in log.
  420. * it is harmless if mount fails.
  421. *
  422. * note: MOUNT record is at aggregate level, not at fileset level,
  423. * since log records of previous mounts of a fileset
  424. * (e.g., AFTER record of extent allocation) have to be processed
  425. * to update block allocation map at aggregate level.
  426. */
  427. static int logMOUNT(struct super_block *sb)
  428. {
  429. struct jfs_log *log = JFS_SBI(sb)->log;
  430. struct lrd lrd;
  431. lrd.logtid = 0;
  432. lrd.backchain = 0;
  433. lrd.type = cpu_to_le16(LOG_MOUNT);
  434. lrd.length = 0;
  435. lrd.aggregate = cpu_to_le32(new_encode_dev(sb->s_bdev->bd_dev));
  436. lmLog(log, NULL, &lrd, NULL);
  437. return 0;
  438. }