jfs_mount.c 13 KB

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