jfs_mount.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *   Copyright (c) International Business Machines Corp., 2000-2002
  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/locks.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. jFYI(1, ("nMount JFSn"));
  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. jERROR(1, ("jfs_mount: Faild to read AGGREGATE_In"));
  94. rc = EIO;
  95. goto errout20;
  96. }
  97. sbi->ipaimap = ipaimap;
  98. jFYI(1, ("jfs_mount: ipaimap:0x%pn", ipaimap));
  99. /*
  100.  * initialize aggregate inode allocation map
  101.  */
  102. if ((rc = diMount(ipaimap))) {
  103. jERROR(1,
  104.        ("jfs_mount: diMount(ipaimap) failed w/rc = %dn",
  105. rc));
  106. goto errout21;
  107. }
  108. /*
  109.  * open aggregate block allocation map
  110.  */
  111. ipbmap = diReadSpecial(sb, BMAP_I, 0);
  112. if (ipbmap == NULL) {
  113. rc = EIO;
  114. goto errout22;
  115. }
  116. jFYI(1, ("jfs_mount: ipbmap:0x%pn", ipbmap));
  117. sbi->ipbmap = ipbmap;
  118. /*
  119.  * initialize aggregate block allocation map
  120.  */
  121. if ((rc = dbMount(ipbmap))) {
  122. jERROR(1, ("jfs_mount: dbMount failed w/rc = %dn", rc));
  123. goto errout22;
  124. }
  125. /*
  126.  * open the secondary aggregate inode allocation map
  127.  *
  128.  * This is a duplicate of the aggregate inode allocation map.
  129.  *
  130.  * hand craft a vfs in the same fashion as we did to read ipaimap.
  131.  * By adding INOSPEREXT (32) to the inode number, we are telling
  132.  * diReadSpecial that we are reading from the secondary aggregate
  133.  * inode table.  This also creates a unique entry in the inode hash
  134.  * table.
  135.  */
  136. if ((sbi->mntflag & JFS_BAD_SAIT) == 0) {
  137. ipaimap2 = diReadSpecial(sb, AGGREGATE_I, 1);
  138. if (ipaimap2 == 0) {
  139. jERROR(1,
  140.        ("jfs_mount: Faild to read AGGREGATE_In"));
  141. rc = EIO;
  142. goto errout35;
  143. }
  144. sbi->ipaimap2 = ipaimap2;
  145. jFYI(1, ("jfs_mount: ipaimap2:0x%pn", ipaimap2));
  146. /*
  147.  * initialize secondary aggregate inode allocation map
  148.  */
  149. if ((rc = diMount(ipaimap2))) {
  150. jERROR(1,
  151.        ("jfs_mount: diMount(ipaimap2) failed, rc = %dn",
  152. rc));
  153. goto errout35;
  154. }
  155. } else
  156. /* Secondary aggregate inode table is not valid */
  157. sbi->ipaimap2 = 0;
  158. /*
  159.  *      mount (the only/single) fileset
  160.  */
  161. /*
  162.  * open fileset inode allocation map (aka fileset inode)
  163.  */
  164. ipimap = diReadSpecial(sb, FILESYSTEM_I, 0);
  165. if (ipimap == NULL) {
  166. jERROR(1, ("jfs_mount: Failed to read FILESYSTEM_In"));
  167. /* open fileset secondary inode allocation map */
  168. rc = EIO;
  169. goto errout40;
  170. }
  171. jFYI(1, ("jfs_mount: ipimap:0x%pn", ipimap));
  172. /* map further access of per fileset inodes by the fileset inode */
  173. sbi->ipimap = ipimap;
  174. /* initialize fileset inode allocation map */
  175. if ((rc = diMount(ipimap))) {
  176. jERROR(1, ("jfs_mount: diMount failed w/rc = %dn", rc));
  177. goto errout41;
  178. }
  179. jFYI(1, ("Mount JFS Complete.n"));
  180. goto out;
  181. /*
  182.  *      unwind on error
  183.  */
  184. //errout42: /* close fileset inode allocation map */
  185. diUnmount(ipimap, 1);
  186.       errout41: /* close fileset inode allocation map inode */
  187. diFreeSpecial(ipimap);
  188.       errout40: /* fileset closed */
  189. /* close secondary aggregate inode allocation map */
  190. if (ipaimap2) {
  191. diUnmount(ipaimap2, 1);
  192. diFreeSpecial(ipaimap2);
  193. }
  194.       errout35:
  195. /* close aggregate block allocation map */
  196. dbUnmount(ipbmap, 1);
  197. diFreeSpecial(ipbmap);
  198.       errout22: /* close aggregate inode allocation map */
  199. diUnmount(ipaimap, 1);
  200.       errout21: /* close aggregate inodes */
  201. diFreeSpecial(ipaimap);
  202.       errout20: /* aggregate closed */
  203.       out:
  204. if (rc) {
  205. jERROR(1, ("Mount JFS Failure: %dn", rc));
  206. }
  207. return rc;
  208. }
  209. /*
  210.  * NAME: jfs_mount_rw(sb, remount)
  211.  *
  212.  * FUNCTION: Completes read-write mount, or remounts read-only volume
  213.  * as read-write
  214.  */
  215. int jfs_mount_rw(struct super_block *sb, int remount)
  216. {
  217. struct jfs_sb_info *sbi = JFS_SBI(sb);  
  218. struct jfs_log *log;
  219. int rc;
  220. /*
  221.  * If we are re-mounting a previously read-only volume, we want to
  222.  * re-read the inode and block maps, since fsck.jfs may have updated
  223.  * them.
  224.  */
  225. if (remount) {
  226. if (chkSuper(sb) || (sbi->state != FM_CLEAN))
  227. return -EINVAL;
  228. truncate_inode_pages(sbi->ipimap->i_mapping, 0);
  229. truncate_inode_pages(sbi->ipbmap->i_mapping, 0);
  230. diUnmount(sbi->ipimap, 1);
  231. if ((rc = diMount(sbi->ipimap))) {
  232. jERROR(1,("jfs_mount_rw: diMount failed!n"));
  233. return rc;
  234. }
  235. dbUnmount(sbi->ipbmap, 1);
  236. if ((rc = dbMount(sbi->ipbmap))) {
  237. jERROR(1,("jfs_mount_rw: dbMount failed!n"));
  238. return rc;
  239. }
  240. }
  241. /*
  242.  * open/initialize log
  243.  */
  244. if ((rc = lmLogOpen(sb, &log)))
  245. return rc;
  246. JFS_SBI(sb)->log = log;
  247. /*
  248.  * update file system superblock;
  249.  */
  250. if ((rc = updateSuper(sb, FM_MOUNT))) {
  251. jERROR(1,
  252.        ("jfs_mount: updateSuper failed w/rc = %dn", rc));
  253. lmLogClose(sb, log);
  254. JFS_SBI(sb)->log = 0;
  255. return rc;
  256. }
  257. /*
  258.  * write MOUNT log record of the file system
  259.  */
  260. logMOUNT(sb);
  261. return rc;
  262. }
  263. /*
  264.  * chkSuper()
  265.  *
  266.  * validate the superblock of the file system to be mounted and 
  267.  * get the file system parameters.
  268.  *
  269.  * returns
  270.  * 0 with fragsize set if check successful
  271.  * error code if not successful
  272.  */
  273. static int chkSuper(struct super_block *sb)
  274. {
  275. int rc = 0;
  276. struct jfs_sb_info *sbi = JFS_SBI(sb);
  277. struct jfs_superblock *j_sb;
  278. struct buffer_head *bh;
  279. int AIM_bytesize, AIT_bytesize;
  280. int expected_AIM_bytesize, expected_AIT_bytesize;
  281. s64 AIM_byte_addr, AIT_byte_addr, fsckwsp_addr;
  282. s64 byte_addr_diff0, byte_addr_diff1;
  283. s32 bsize;
  284. if ((rc = readSuper(sb, &bh)))
  285. return rc;
  286. j_sb = (struct jfs_superblock *)bh->b_data;
  287. /*
  288.  * validate superblock
  289.  */
  290. /* validate fs signature */
  291. if (strncmp(j_sb->s_magic, JFS_MAGIC, 4) ||
  292.     j_sb->s_version > cpu_to_le32(JFS_VERSION)) {
  293. //rc = EFORMAT;
  294. rc = EINVAL;
  295. goto out;
  296. }
  297. bsize = le32_to_cpu(j_sb->s_bsize);
  298. #ifdef _JFS_4K
  299. if (bsize != PSIZE) {
  300. jERROR(1, ("Currently only 4K block size supported!n"));
  301. rc = EINVAL;
  302. goto out;
  303. }
  304. #endif /* _JFS_4K */
  305. jFYI(1, ("superblock: flag:0x%08x state:0x%08x size:0x%Lxn",
  306.  le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
  307.  (unsigned long long) le64_to_cpu(j_sb->s_size)));
  308. /* validate the descriptors for Secondary AIM and AIT */
  309. if ((j_sb->s_flag & cpu_to_le32(JFS_BAD_SAIT)) !=
  310.     cpu_to_le32(JFS_BAD_SAIT)) {
  311. expected_AIM_bytesize = 2 * PSIZE;
  312. AIM_bytesize = lengthPXD(&(j_sb->s_aim2)) * bsize;
  313. expected_AIT_bytesize = 4 * PSIZE;
  314. AIT_bytesize = lengthPXD(&(j_sb->s_ait2)) * bsize;
  315. AIM_byte_addr = addressPXD(&(j_sb->s_aim2)) * bsize;
  316. AIT_byte_addr = addressPXD(&(j_sb->s_ait2)) * bsize;
  317. byte_addr_diff0 = AIT_byte_addr - AIM_byte_addr;
  318. fsckwsp_addr = addressPXD(&(j_sb->s_fsckpxd)) * bsize;
  319. byte_addr_diff1 = fsckwsp_addr - AIT_byte_addr;
  320. if ((AIM_bytesize != expected_AIM_bytesize) ||
  321.     (AIT_bytesize != expected_AIT_bytesize) ||
  322.     (byte_addr_diff0 != AIM_bytesize) ||
  323.     (byte_addr_diff1 <= AIT_bytesize))
  324. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  325. }
  326. if ((j_sb->s_flag & cpu_to_le32(JFS_GROUPCOMMIT)) !=
  327.     cpu_to_le32(JFS_GROUPCOMMIT))
  328. j_sb->s_flag |= cpu_to_le32(JFS_GROUPCOMMIT);
  329. jFYI(0, ("superblock: flag:0x%08x state:0x%08x size:0x%Lxn",
  330.  le32_to_cpu(j_sb->s_flag), le32_to_cpu(j_sb->s_state),
  331.  (unsigned long long) le64_to_cpu(j_sb->s_size)));
  332. /* validate fs state */
  333. if (j_sb->s_state != cpu_to_le32(FM_CLEAN) &&
  334.     !(sb->s_flags & MS_RDONLY)) {
  335. jERROR(1,
  336.        ("jfs_mount: Mount Failure: File System Dirty.n"));
  337. rc = EINVAL;
  338. goto out;
  339. }
  340. sbi->state = le32_to_cpu(j_sb->s_state);
  341. sbi->mntflag = le32_to_cpu(j_sb->s_flag);
  342. /*
  343.  * JFS always does I/O by 4K pages.  Don't tell the buffer cache
  344.  * that we use anything else (leave s_blocksize alone).
  345.  */
  346. sbi->bsize = bsize;
  347. sbi->l2bsize = le16_to_cpu(j_sb->s_l2bsize);
  348. /*
  349.  * For now, ignore s_pbsize, l2bfactor.  All I/O going through buffer
  350.  * cache.
  351.  */
  352. sbi->nbperpage = PSIZE >> sbi->l2bsize;
  353. sbi->l2nbperpage = L2PSIZE - sbi->l2bsize;
  354. sbi->l2niperblk = sbi->l2bsize - L2DISIZE;
  355. if (sbi->mntflag & JFS_INLINELOG)
  356. sbi->logpxd = j_sb->s_logpxd;
  357. else {
  358. sbi->logdev = to_kdev_t(le32_to_cpu(j_sb->s_logdev));
  359. memcpy(sbi->uuid, j_sb->s_uuid, sizeof(sbi->uuid));
  360. memcpy(sbi->loguuid, j_sb->s_loguuid, sizeof(sbi->uuid));
  361. }
  362. sbi->fsckpxd = j_sb->s_fsckpxd;
  363. sbi->ait2 = j_sb->s_ait2;
  364.       out:
  365. brelse(bh);
  366. return rc;
  367. }
  368. /*
  369.  * updateSuper()
  370.  *
  371.  * update synchronously superblock if it is mounted read-write.
  372.  */
  373. int updateSuper(struct super_block *sb, uint state)
  374. {
  375. struct jfs_superblock *j_sb;
  376. struct jfs_sb_info *sbi = JFS_SBI(sb);
  377. struct buffer_head *bh;
  378. int rc;
  379. /*
  380.  * Only fsck can fix dirty state
  381.  */
  382. if (sbi->state == FM_DIRTY)
  383. return 0;
  384. if ((rc = readSuper(sb, &bh)))
  385. return rc;
  386. j_sb = (struct jfs_superblock *)bh->b_data;
  387. j_sb->s_state = cpu_to_le32(state);
  388. sbi->state = state;
  389. if (state == FM_MOUNT) {
  390. /* record log's dev_t and mount serial number */
  391. j_sb->s_logdev = cpu_to_le32(sbi->log->bdev->bd_dev);
  392. j_sb->s_logserial = cpu_to_le32(sbi->log->serial);
  393. } else if (state == FM_CLEAN) {
  394. /*
  395.  * If this volume is shared with OS/2, OS/2 will need to
  396.  * recalculate DASD usage, since we don't deal with it.
  397.  */
  398. if (j_sb->s_flag & cpu_to_le32(JFS_DASD_ENABLED))
  399. j_sb->s_flag |= cpu_to_le32(JFS_DASD_PRIME);
  400. }
  401. mark_buffer_dirty(bh);
  402. ll_rw_block(WRITE, 1, &bh);
  403. wait_on_buffer(bh);
  404. brelse(bh);
  405. return 0;
  406. }
  407. /*
  408.  * readSuper()
  409.  *
  410.  * read superblock by raw sector address
  411.  */
  412. int readSuper(struct super_block *sb, struct buffer_head **bpp)
  413. {
  414. /* read in primary superblock */
  415. *bpp = sb_bread(sb, SUPER1_OFF >> sb->s_blocksize_bits);
  416. if (*bpp)
  417. return 0;
  418. /* read in secondary/replicated superblock */
  419. *bpp = sb_bread(sb, SUPER2_OFF >> sb->s_blocksize_bits);
  420. if (*bpp)
  421. return 0;
  422. return -EIO;
  423. }
  424. /*
  425.  * logMOUNT()
  426.  *
  427.  * function: write a MOUNT log record for file system.
  428.  *
  429.  * MOUNT record keeps logredo() from processing log records
  430.  * for this file system past this point in log.
  431.  * it is harmless if mount fails.
  432.  *
  433.  * note: MOUNT record is at aggregate level, not at fileset level, 
  434.  * since log records of previous mounts of a fileset
  435.  * (e.g., AFTER record of extent allocation) have to be processed 
  436.  * to update block allocation map at aggregate level.
  437.  */
  438. static int logMOUNT(struct super_block *sb)
  439. {
  440. struct jfs_log *log = JFS_SBI(sb)->log;
  441. struct lrd lrd;
  442. lrd.logtid = 0;
  443. lrd.backchain = 0;
  444. lrd.type = cpu_to_le16(LOG_MOUNT);
  445. lrd.length = 0;
  446. lrd.aggregate = cpu_to_le32(kdev_t_to_nr(sb->s_dev));
  447. lmLog(log, NULL, &lrd, NULL);
  448. return 0;
  449. }