PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/gfs2/ops_fstype.c

https://bitbucket.org/digetx/picasso-kernel
C | 1435 lines | 1080 code | 209 blank | 146 comment | 161 complexity | d4d8b4755307ac95f5cfc04b30d8de58 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/kthread.h>
  16. #include <linux/export.h>
  17. #include <linux/namei.h>
  18. #include <linux/mount.h>
  19. #include <linux/gfs2_ondisk.h>
  20. #include <linux/quotaops.h>
  21. #include <linux/lockdep.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "bmap.h"
  25. #include "glock.h"
  26. #include "glops.h"
  27. #include "inode.h"
  28. #include "recovery.h"
  29. #include "rgrp.h"
  30. #include "super.h"
  31. #include "sys.h"
  32. #include "util.h"
  33. #include "log.h"
  34. #include "quota.h"
  35. #include "dir.h"
  36. #include "trace_gfs2.h"
  37. #define DO 0
  38. #define UNDO 1
  39. /**
  40. * gfs2_tune_init - Fill a gfs2_tune structure with default values
  41. * @gt: tune
  42. *
  43. */
  44. static void gfs2_tune_init(struct gfs2_tune *gt)
  45. {
  46. spin_lock_init(&gt->gt_spin);
  47. gt->gt_quota_simul_sync = 64;
  48. gt->gt_quota_warn_period = 10;
  49. gt->gt_quota_scale_num = 1;
  50. gt->gt_quota_scale_den = 1;
  51. gt->gt_new_files_jdata = 0;
  52. gt->gt_max_readahead = 1 << 18;
  53. gt->gt_complain_secs = 10;
  54. }
  55. static struct gfs2_sbd *init_sbd(struct super_block *sb)
  56. {
  57. struct gfs2_sbd *sdp;
  58. sdp = kzalloc(sizeof(struct gfs2_sbd), GFP_KERNEL);
  59. if (!sdp)
  60. return NULL;
  61. sb->s_fs_info = sdp;
  62. sdp->sd_vfs = sb;
  63. sdp->sd_lkstats = alloc_percpu(struct gfs2_pcpu_lkstats);
  64. if (!sdp->sd_lkstats) {
  65. kfree(sdp);
  66. return NULL;
  67. }
  68. set_bit(SDF_NOJOURNALID, &sdp->sd_flags);
  69. gfs2_tune_init(&sdp->sd_tune);
  70. init_waitqueue_head(&sdp->sd_glock_wait);
  71. atomic_set(&sdp->sd_glock_disposal, 0);
  72. init_completion(&sdp->sd_locking_init);
  73. spin_lock_init(&sdp->sd_statfs_spin);
  74. spin_lock_init(&sdp->sd_rindex_spin);
  75. sdp->sd_rindex_tree.rb_node = NULL;
  76. INIT_LIST_HEAD(&sdp->sd_jindex_list);
  77. spin_lock_init(&sdp->sd_jindex_spin);
  78. mutex_init(&sdp->sd_jindex_mutex);
  79. INIT_LIST_HEAD(&sdp->sd_quota_list);
  80. mutex_init(&sdp->sd_quota_mutex);
  81. init_waitqueue_head(&sdp->sd_quota_wait);
  82. INIT_LIST_HEAD(&sdp->sd_trunc_list);
  83. spin_lock_init(&sdp->sd_trunc_lock);
  84. spin_lock_init(&sdp->sd_log_lock);
  85. atomic_set(&sdp->sd_log_pinned, 0);
  86. INIT_LIST_HEAD(&sdp->sd_log_le_buf);
  87. INIT_LIST_HEAD(&sdp->sd_log_le_revoke);
  88. INIT_LIST_HEAD(&sdp->sd_log_le_databuf);
  89. INIT_LIST_HEAD(&sdp->sd_log_le_ordered);
  90. init_waitqueue_head(&sdp->sd_log_waitq);
  91. init_waitqueue_head(&sdp->sd_logd_waitq);
  92. spin_lock_init(&sdp->sd_ail_lock);
  93. INIT_LIST_HEAD(&sdp->sd_ail1_list);
  94. INIT_LIST_HEAD(&sdp->sd_ail2_list);
  95. init_rwsem(&sdp->sd_log_flush_lock);
  96. atomic_set(&sdp->sd_log_in_flight, 0);
  97. init_waitqueue_head(&sdp->sd_log_flush_wait);
  98. INIT_LIST_HEAD(&sdp->sd_revoke_list);
  99. mutex_init(&sdp->sd_freeze_lock);
  100. return sdp;
  101. }
  102. /**
  103. * gfs2_check_sb - Check superblock
  104. * @sdp: the filesystem
  105. * @sb: The superblock
  106. * @silent: Don't print a message if the check fails
  107. *
  108. * Checks the version code of the FS is one that we understand how to
  109. * read and that the sizes of the various on-disk structures have not
  110. * changed.
  111. */
  112. static int gfs2_check_sb(struct gfs2_sbd *sdp, int silent)
  113. {
  114. struct gfs2_sb_host *sb = &sdp->sd_sb;
  115. if (sb->sb_magic != GFS2_MAGIC ||
  116. sb->sb_type != GFS2_METATYPE_SB) {
  117. if (!silent)
  118. printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
  119. return -EINVAL;
  120. }
  121. /* If format numbers match exactly, we're done. */
  122. if (sb->sb_fs_format == GFS2_FORMAT_FS &&
  123. sb->sb_multihost_format == GFS2_FORMAT_MULTI)
  124. return 0;
  125. fs_warn(sdp, "Unknown on-disk format, unable to mount\n");
  126. return -EINVAL;
  127. }
  128. static void end_bio_io_page(struct bio *bio, int error)
  129. {
  130. struct page *page = bio->bi_private;
  131. if (!error)
  132. SetPageUptodate(page);
  133. else
  134. printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
  135. unlock_page(page);
  136. }
  137. static void gfs2_sb_in(struct gfs2_sbd *sdp, const void *buf)
  138. {
  139. struct gfs2_sb_host *sb = &sdp->sd_sb;
  140. struct super_block *s = sdp->sd_vfs;
  141. const struct gfs2_sb *str = buf;
  142. sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
  143. sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
  144. sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
  145. sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
  146. sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
  147. sb->sb_bsize = be32_to_cpu(str->sb_bsize);
  148. sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
  149. sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
  150. sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
  151. sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
  152. sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
  153. memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
  154. memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
  155. memcpy(s->s_uuid, str->sb_uuid, 16);
  156. }
  157. /**
  158. * gfs2_read_super - Read the gfs2 super block from disk
  159. * @sdp: The GFS2 super block
  160. * @sector: The location of the super block
  161. * @error: The error code to return
  162. *
  163. * This uses the bio functions to read the super block from disk
  164. * because we want to be 100% sure that we never read cached data.
  165. * A super block is read twice only during each GFS2 mount and is
  166. * never written to by the filesystem. The first time its read no
  167. * locks are held, and the only details which are looked at are those
  168. * relating to the locking protocol. Once locking is up and working,
  169. * the sb is read again under the lock to establish the location of
  170. * the master directory (contains pointers to journals etc) and the
  171. * root directory.
  172. *
  173. * Returns: 0 on success or error
  174. */
  175. static int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector, int silent)
  176. {
  177. struct super_block *sb = sdp->sd_vfs;
  178. struct gfs2_sb *p;
  179. struct page *page;
  180. struct bio *bio;
  181. page = alloc_page(GFP_NOFS);
  182. if (unlikely(!page))
  183. return -ENOBUFS;
  184. ClearPageUptodate(page);
  185. ClearPageDirty(page);
  186. lock_page(page);
  187. bio = bio_alloc(GFP_NOFS, 1);
  188. bio->bi_sector = sector * (sb->s_blocksize >> 9);
  189. bio->bi_bdev = sb->s_bdev;
  190. bio_add_page(bio, page, PAGE_SIZE, 0);
  191. bio->bi_end_io = end_bio_io_page;
  192. bio->bi_private = page;
  193. submit_bio(READ_SYNC | REQ_META, bio);
  194. wait_on_page_locked(page);
  195. bio_put(bio);
  196. if (!PageUptodate(page)) {
  197. __free_page(page);
  198. return -EIO;
  199. }
  200. p = kmap(page);
  201. gfs2_sb_in(sdp, p);
  202. kunmap(page);
  203. __free_page(page);
  204. return gfs2_check_sb(sdp, silent);
  205. }
  206. /**
  207. * gfs2_read_sb - Read super block
  208. * @sdp: The GFS2 superblock
  209. * @silent: Don't print message if mount fails
  210. *
  211. */
  212. static int gfs2_read_sb(struct gfs2_sbd *sdp, int silent)
  213. {
  214. u32 hash_blocks, ind_blocks, leaf_blocks;
  215. u32 tmp_blocks;
  216. unsigned int x;
  217. int error;
  218. error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
  219. if (error) {
  220. if (!silent)
  221. fs_err(sdp, "can't read superblock\n");
  222. return error;
  223. }
  224. sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
  225. GFS2_BASIC_BLOCK_SHIFT;
  226. sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
  227. sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
  228. sizeof(struct gfs2_dinode)) / sizeof(u64);
  229. sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
  230. sizeof(struct gfs2_meta_header)) / sizeof(u64);
  231. sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
  232. sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
  233. sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
  234. sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
  235. sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
  236. sizeof(struct gfs2_meta_header)) /
  237. sizeof(struct gfs2_quota_change);
  238. sdp->sd_blocks_per_bitmap = (sdp->sd_sb.sb_bsize -
  239. sizeof(struct gfs2_meta_header))
  240. * GFS2_NBBY; /* not the rgrp bitmap, subsequent bitmaps only */
  241. /* Compute maximum reservation required to add a entry to a directory */
  242. hash_blocks = DIV_ROUND_UP(sizeof(u64) * (1 << GFS2_DIR_MAX_DEPTH),
  243. sdp->sd_jbsize);
  244. ind_blocks = 0;
  245. for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
  246. tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
  247. ind_blocks += tmp_blocks;
  248. }
  249. leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
  250. sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
  251. sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
  252. sizeof(struct gfs2_dinode);
  253. sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
  254. for (x = 2;; x++) {
  255. u64 space, d;
  256. u32 m;
  257. space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
  258. d = space;
  259. m = do_div(d, sdp->sd_inptrs);
  260. if (d != sdp->sd_heightsize[x - 1] || m)
  261. break;
  262. sdp->sd_heightsize[x] = space;
  263. }
  264. sdp->sd_max_height = x;
  265. sdp->sd_heightsize[x] = ~0;
  266. gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
  267. sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
  268. sizeof(struct gfs2_dinode);
  269. sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
  270. for (x = 2;; x++) {
  271. u64 space, d;
  272. u32 m;
  273. space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
  274. d = space;
  275. m = do_div(d, sdp->sd_inptrs);
  276. if (d != sdp->sd_jheightsize[x - 1] || m)
  277. break;
  278. sdp->sd_jheightsize[x] = space;
  279. }
  280. sdp->sd_max_jheight = x;
  281. sdp->sd_jheightsize[x] = ~0;
  282. gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
  283. return 0;
  284. }
  285. static int init_names(struct gfs2_sbd *sdp, int silent)
  286. {
  287. char *proto, *table;
  288. int error = 0;
  289. proto = sdp->sd_args.ar_lockproto;
  290. table = sdp->sd_args.ar_locktable;
  291. /* Try to autodetect */
  292. if (!proto[0] || !table[0]) {
  293. error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift, silent);
  294. if (error)
  295. return error;
  296. if (!proto[0])
  297. proto = sdp->sd_sb.sb_lockproto;
  298. if (!table[0])
  299. table = sdp->sd_sb.sb_locktable;
  300. }
  301. if (!table[0])
  302. table = sdp->sd_vfs->s_id;
  303. strlcpy(sdp->sd_proto_name, proto, GFS2_FSNAME_LEN);
  304. strlcpy(sdp->sd_table_name, table, GFS2_FSNAME_LEN);
  305. table = sdp->sd_table_name;
  306. while ((table = strchr(table, '/')))
  307. *table = '_';
  308. return error;
  309. }
  310. static int init_locking(struct gfs2_sbd *sdp, struct gfs2_holder *mount_gh,
  311. int undo)
  312. {
  313. int error = 0;
  314. if (undo)
  315. goto fail_trans;
  316. error = gfs2_glock_nq_num(sdp,
  317. GFS2_MOUNT_LOCK, &gfs2_nondisk_glops,
  318. LM_ST_EXCLUSIVE, LM_FLAG_NOEXP | GL_NOCACHE,
  319. mount_gh);
  320. if (error) {
  321. fs_err(sdp, "can't acquire mount glock: %d\n", error);
  322. goto fail;
  323. }
  324. error = gfs2_glock_nq_num(sdp,
  325. GFS2_LIVE_LOCK, &gfs2_nondisk_glops,
  326. LM_ST_SHARED,
  327. LM_FLAG_NOEXP | GL_EXACT,
  328. &sdp->sd_live_gh);
  329. if (error) {
  330. fs_err(sdp, "can't acquire live glock: %d\n", error);
  331. goto fail_mount;
  332. }
  333. error = gfs2_glock_get(sdp, GFS2_RENAME_LOCK, &gfs2_nondisk_glops,
  334. CREATE, &sdp->sd_rename_gl);
  335. if (error) {
  336. fs_err(sdp, "can't create rename glock: %d\n", error);
  337. goto fail_live;
  338. }
  339. error = gfs2_glock_get(sdp, GFS2_TRANS_LOCK, &gfs2_trans_glops,
  340. CREATE, &sdp->sd_trans_gl);
  341. if (error) {
  342. fs_err(sdp, "can't create transaction glock: %d\n", error);
  343. goto fail_rename;
  344. }
  345. return 0;
  346. fail_trans:
  347. gfs2_glock_put(sdp->sd_trans_gl);
  348. fail_rename:
  349. gfs2_glock_put(sdp->sd_rename_gl);
  350. fail_live:
  351. gfs2_glock_dq_uninit(&sdp->sd_live_gh);
  352. fail_mount:
  353. gfs2_glock_dq_uninit(mount_gh);
  354. fail:
  355. return error;
  356. }
  357. static int gfs2_lookup_root(struct super_block *sb, struct dentry **dptr,
  358. u64 no_addr, const char *name)
  359. {
  360. struct gfs2_sbd *sdp = sb->s_fs_info;
  361. struct dentry *dentry;
  362. struct inode *inode;
  363. inode = gfs2_inode_lookup(sb, DT_DIR, no_addr, 0, 0);
  364. if (IS_ERR(inode)) {
  365. fs_err(sdp, "can't read in %s inode: %ld\n", name, PTR_ERR(inode));
  366. return PTR_ERR(inode);
  367. }
  368. dentry = d_make_root(inode);
  369. if (!dentry) {
  370. fs_err(sdp, "can't alloc %s dentry\n", name);
  371. return -ENOMEM;
  372. }
  373. *dptr = dentry;
  374. return 0;
  375. }
  376. static int init_sb(struct gfs2_sbd *sdp, int silent)
  377. {
  378. struct super_block *sb = sdp->sd_vfs;
  379. struct gfs2_holder sb_gh;
  380. u64 no_addr;
  381. int ret;
  382. ret = gfs2_glock_nq_num(sdp, GFS2_SB_LOCK, &gfs2_meta_glops,
  383. LM_ST_SHARED, 0, &sb_gh);
  384. if (ret) {
  385. fs_err(sdp, "can't acquire superblock glock: %d\n", ret);
  386. return ret;
  387. }
  388. ret = gfs2_read_sb(sdp, silent);
  389. if (ret) {
  390. fs_err(sdp, "can't read superblock: %d\n", ret);
  391. goto out;
  392. }
  393. /* Set up the buffer cache and SB for real */
  394. if (sdp->sd_sb.sb_bsize < bdev_logical_block_size(sb->s_bdev)) {
  395. ret = -EINVAL;
  396. fs_err(sdp, "FS block size (%u) is too small for device "
  397. "block size (%u)\n",
  398. sdp->sd_sb.sb_bsize, bdev_logical_block_size(sb->s_bdev));
  399. goto out;
  400. }
  401. if (sdp->sd_sb.sb_bsize > PAGE_SIZE) {
  402. ret = -EINVAL;
  403. fs_err(sdp, "FS block size (%u) is too big for machine "
  404. "page size (%u)\n",
  405. sdp->sd_sb.sb_bsize, (unsigned int)PAGE_SIZE);
  406. goto out;
  407. }
  408. sb_set_blocksize(sb, sdp->sd_sb.sb_bsize);
  409. /* Get the root inode */
  410. no_addr = sdp->sd_sb.sb_root_dir.no_addr;
  411. ret = gfs2_lookup_root(sb, &sdp->sd_root_dir, no_addr, "root");
  412. if (ret)
  413. goto out;
  414. /* Get the master inode */
  415. no_addr = sdp->sd_sb.sb_master_dir.no_addr;
  416. ret = gfs2_lookup_root(sb, &sdp->sd_master_dir, no_addr, "master");
  417. if (ret) {
  418. dput(sdp->sd_root_dir);
  419. goto out;
  420. }
  421. sb->s_root = dget(sdp->sd_args.ar_meta ? sdp->sd_master_dir : sdp->sd_root_dir);
  422. out:
  423. gfs2_glock_dq_uninit(&sb_gh);
  424. return ret;
  425. }
  426. /**
  427. * map_journal_extents - create a reusable "extent" mapping from all logical
  428. * blocks to all physical blocks for the given journal. This will save
  429. * us time when writing journal blocks. Most journals will have only one
  430. * extent that maps all their logical blocks. That's because gfs2.mkfs
  431. * arranges the journal blocks sequentially to maximize performance.
  432. * So the extent would map the first block for the entire file length.
  433. * However, gfs2_jadd can happen while file activity is happening, so
  434. * those journals may not be sequential. Less likely is the case where
  435. * the users created their own journals by mounting the metafs and
  436. * laying it out. But it's still possible. These journals might have
  437. * several extents.
  438. *
  439. * TODO: This should be done in bigger chunks rather than one block at a time,
  440. * but since it's only done at mount time, I'm not worried about the
  441. * time it takes.
  442. */
  443. static int map_journal_extents(struct gfs2_sbd *sdp)
  444. {
  445. struct gfs2_jdesc *jd = sdp->sd_jdesc;
  446. unsigned int lb;
  447. u64 db, prev_db; /* logical block, disk block, prev disk block */
  448. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  449. struct gfs2_journal_extent *jext = NULL;
  450. struct buffer_head bh;
  451. int rc = 0;
  452. prev_db = 0;
  453. for (lb = 0; lb < i_size_read(jd->jd_inode) >> sdp->sd_sb.sb_bsize_shift; lb++) {
  454. bh.b_state = 0;
  455. bh.b_blocknr = 0;
  456. bh.b_size = 1 << ip->i_inode.i_blkbits;
  457. rc = gfs2_block_map(jd->jd_inode, lb, &bh, 0);
  458. db = bh.b_blocknr;
  459. if (rc || !db) {
  460. printk(KERN_INFO "GFS2 journal mapping error %d: lb="
  461. "%u db=%llu\n", rc, lb, (unsigned long long)db);
  462. break;
  463. }
  464. if (!prev_db || db != prev_db + 1) {
  465. jext = kzalloc(sizeof(struct gfs2_journal_extent),
  466. GFP_KERNEL);
  467. if (!jext) {
  468. printk(KERN_INFO "GFS2 error: out of memory "
  469. "mapping journal extents.\n");
  470. rc = -ENOMEM;
  471. break;
  472. }
  473. jext->dblock = db;
  474. jext->lblock = lb;
  475. jext->blocks = 1;
  476. list_add_tail(&jext->extent_list, &jd->extent_list);
  477. } else {
  478. jext->blocks++;
  479. }
  480. prev_db = db;
  481. }
  482. return rc;
  483. }
  484. static void gfs2_others_may_mount(struct gfs2_sbd *sdp)
  485. {
  486. char *message = "FIRSTMOUNT=Done";
  487. char *envp[] = { message, NULL };
  488. fs_info(sdp, "first mount done, others may mount\n");
  489. if (sdp->sd_lockstruct.ls_ops->lm_first_done)
  490. sdp->sd_lockstruct.ls_ops->lm_first_done(sdp);
  491. kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
  492. }
  493. /**
  494. * gfs2_jindex_hold - Grab a lock on the jindex
  495. * @sdp: The GFS2 superblock
  496. * @ji_gh: the holder for the jindex glock
  497. *
  498. * Returns: errno
  499. */
  500. static int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
  501. {
  502. struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
  503. struct qstr name;
  504. char buf[20];
  505. struct gfs2_jdesc *jd;
  506. int error;
  507. name.name = buf;
  508. mutex_lock(&sdp->sd_jindex_mutex);
  509. for (;;) {
  510. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
  511. if (error)
  512. break;
  513. name.len = sprintf(buf, "journal%u", sdp->sd_journals);
  514. name.hash = gfs2_disk_hash(name.name, name.len);
  515. error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
  516. if (error == -ENOENT) {
  517. error = 0;
  518. break;
  519. }
  520. gfs2_glock_dq_uninit(ji_gh);
  521. if (error)
  522. break;
  523. error = -ENOMEM;
  524. jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
  525. if (!jd)
  526. break;
  527. INIT_LIST_HEAD(&jd->extent_list);
  528. INIT_WORK(&jd->jd_work, gfs2_recover_func);
  529. jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1);
  530. if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
  531. if (!jd->jd_inode)
  532. error = -ENOENT;
  533. else
  534. error = PTR_ERR(jd->jd_inode);
  535. kfree(jd);
  536. break;
  537. }
  538. spin_lock(&sdp->sd_jindex_spin);
  539. jd->jd_jid = sdp->sd_journals++;
  540. list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
  541. spin_unlock(&sdp->sd_jindex_spin);
  542. }
  543. mutex_unlock(&sdp->sd_jindex_mutex);
  544. return error;
  545. }
  546. static int init_journal(struct gfs2_sbd *sdp, int undo)
  547. {
  548. struct inode *master = sdp->sd_master_dir->d_inode;
  549. struct gfs2_holder ji_gh;
  550. struct gfs2_inode *ip;
  551. int jindex = 1;
  552. int error = 0;
  553. if (undo) {
  554. jindex = 0;
  555. goto fail_jinode_gh;
  556. }
  557. sdp->sd_jindex = gfs2_lookup_simple(master, "jindex");
  558. if (IS_ERR(sdp->sd_jindex)) {
  559. fs_err(sdp, "can't lookup journal index: %d\n", error);
  560. return PTR_ERR(sdp->sd_jindex);
  561. }
  562. /* Load in the journal index special file */
  563. error = gfs2_jindex_hold(sdp, &ji_gh);
  564. if (error) {
  565. fs_err(sdp, "can't read journal index: %d\n", error);
  566. goto fail;
  567. }
  568. error = -EUSERS;
  569. if (!gfs2_jindex_size(sdp)) {
  570. fs_err(sdp, "no journals!\n");
  571. goto fail_jindex;
  572. }
  573. if (sdp->sd_args.ar_spectator) {
  574. sdp->sd_jdesc = gfs2_jdesc_find(sdp, 0);
  575. atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
  576. atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
  577. atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
  578. } else {
  579. if (sdp->sd_lockstruct.ls_jid >= gfs2_jindex_size(sdp)) {
  580. fs_err(sdp, "can't mount journal #%u\n",
  581. sdp->sd_lockstruct.ls_jid);
  582. fs_err(sdp, "there are only %u journals (0 - %u)\n",
  583. gfs2_jindex_size(sdp),
  584. gfs2_jindex_size(sdp) - 1);
  585. goto fail_jindex;
  586. }
  587. sdp->sd_jdesc = gfs2_jdesc_find(sdp, sdp->sd_lockstruct.ls_jid);
  588. error = gfs2_glock_nq_num(sdp, sdp->sd_lockstruct.ls_jid,
  589. &gfs2_journal_glops,
  590. LM_ST_EXCLUSIVE, LM_FLAG_NOEXP,
  591. &sdp->sd_journal_gh);
  592. if (error) {
  593. fs_err(sdp, "can't acquire journal glock: %d\n", error);
  594. goto fail_jindex;
  595. }
  596. ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  597. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
  598. LM_FLAG_NOEXP | GL_EXACT | GL_NOCACHE,
  599. &sdp->sd_jinode_gh);
  600. if (error) {
  601. fs_err(sdp, "can't acquire journal inode glock: %d\n",
  602. error);
  603. goto fail_journal_gh;
  604. }
  605. error = gfs2_jdesc_check(sdp->sd_jdesc);
  606. if (error) {
  607. fs_err(sdp, "my journal (%u) is bad: %d\n",
  608. sdp->sd_jdesc->jd_jid, error);
  609. goto fail_jinode_gh;
  610. }
  611. atomic_set(&sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks);
  612. atomic_set(&sdp->sd_log_thresh1, 2*sdp->sd_jdesc->jd_blocks/5);
  613. atomic_set(&sdp->sd_log_thresh2, 4*sdp->sd_jdesc->jd_blocks/5);
  614. /* Map the extents for this journal's blocks */
  615. map_journal_extents(sdp);
  616. }
  617. trace_gfs2_log_blocks(sdp, atomic_read(&sdp->sd_log_blks_free));
  618. if (sdp->sd_lockstruct.ls_first) {
  619. unsigned int x;
  620. for (x = 0; x < sdp->sd_journals; x++) {
  621. error = gfs2_recover_journal(gfs2_jdesc_find(sdp, x),
  622. true);
  623. if (error) {
  624. fs_err(sdp, "error recovering journal %u: %d\n",
  625. x, error);
  626. goto fail_jinode_gh;
  627. }
  628. }
  629. gfs2_others_may_mount(sdp);
  630. } else if (!sdp->sd_args.ar_spectator) {
  631. error = gfs2_recover_journal(sdp->sd_jdesc, true);
  632. if (error) {
  633. fs_err(sdp, "error recovering my journal: %d\n", error);
  634. goto fail_jinode_gh;
  635. }
  636. }
  637. set_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags);
  638. gfs2_glock_dq_uninit(&ji_gh);
  639. jindex = 0;
  640. return 0;
  641. fail_jinode_gh:
  642. if (!sdp->sd_args.ar_spectator)
  643. gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
  644. fail_journal_gh:
  645. if (!sdp->sd_args.ar_spectator)
  646. gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
  647. fail_jindex:
  648. gfs2_jindex_free(sdp);
  649. if (jindex)
  650. gfs2_glock_dq_uninit(&ji_gh);
  651. fail:
  652. iput(sdp->sd_jindex);
  653. return error;
  654. }
  655. static struct lock_class_key gfs2_quota_imutex_key;
  656. static int init_inodes(struct gfs2_sbd *sdp, int undo)
  657. {
  658. int error = 0;
  659. struct inode *master = sdp->sd_master_dir->d_inode;
  660. if (undo)
  661. goto fail_qinode;
  662. error = init_journal(sdp, undo);
  663. if (error)
  664. goto fail;
  665. /* Read in the master statfs inode */
  666. sdp->sd_statfs_inode = gfs2_lookup_simple(master, "statfs");
  667. if (IS_ERR(sdp->sd_statfs_inode)) {
  668. error = PTR_ERR(sdp->sd_statfs_inode);
  669. fs_err(sdp, "can't read in statfs inode: %d\n", error);
  670. goto fail_journal;
  671. }
  672. /* Read in the resource index inode */
  673. sdp->sd_rindex = gfs2_lookup_simple(master, "rindex");
  674. if (IS_ERR(sdp->sd_rindex)) {
  675. error = PTR_ERR(sdp->sd_rindex);
  676. fs_err(sdp, "can't get resource index inode: %d\n", error);
  677. goto fail_statfs;
  678. }
  679. sdp->sd_rindex_uptodate = 0;
  680. /* Read in the quota inode */
  681. sdp->sd_quota_inode = gfs2_lookup_simple(master, "quota");
  682. if (IS_ERR(sdp->sd_quota_inode)) {
  683. error = PTR_ERR(sdp->sd_quota_inode);
  684. fs_err(sdp, "can't get quota file inode: %d\n", error);
  685. goto fail_rindex;
  686. }
  687. /*
  688. * i_mutex on quota files is special. Since this inode is hidden system
  689. * file, we are safe to define locking ourselves.
  690. */
  691. lockdep_set_class(&sdp->sd_quota_inode->i_mutex,
  692. &gfs2_quota_imutex_key);
  693. error = gfs2_rindex_update(sdp);
  694. if (error)
  695. goto fail_qinode;
  696. return 0;
  697. fail_qinode:
  698. iput(sdp->sd_quota_inode);
  699. fail_rindex:
  700. gfs2_clear_rgrpd(sdp);
  701. iput(sdp->sd_rindex);
  702. fail_statfs:
  703. iput(sdp->sd_statfs_inode);
  704. fail_journal:
  705. init_journal(sdp, UNDO);
  706. fail:
  707. return error;
  708. }
  709. static int init_per_node(struct gfs2_sbd *sdp, int undo)
  710. {
  711. struct inode *pn = NULL;
  712. char buf[30];
  713. int error = 0;
  714. struct gfs2_inode *ip;
  715. struct inode *master = sdp->sd_master_dir->d_inode;
  716. if (sdp->sd_args.ar_spectator)
  717. return 0;
  718. if (undo)
  719. goto fail_qc_gh;
  720. pn = gfs2_lookup_simple(master, "per_node");
  721. if (IS_ERR(pn)) {
  722. error = PTR_ERR(pn);
  723. fs_err(sdp, "can't find per_node directory: %d\n", error);
  724. return error;
  725. }
  726. sprintf(buf, "statfs_change%u", sdp->sd_jdesc->jd_jid);
  727. sdp->sd_sc_inode = gfs2_lookup_simple(pn, buf);
  728. if (IS_ERR(sdp->sd_sc_inode)) {
  729. error = PTR_ERR(sdp->sd_sc_inode);
  730. fs_err(sdp, "can't find local \"sc\" file: %d\n", error);
  731. goto fail;
  732. }
  733. sprintf(buf, "quota_change%u", sdp->sd_jdesc->jd_jid);
  734. sdp->sd_qc_inode = gfs2_lookup_simple(pn, buf);
  735. if (IS_ERR(sdp->sd_qc_inode)) {
  736. error = PTR_ERR(sdp->sd_qc_inode);
  737. fs_err(sdp, "can't find local \"qc\" file: %d\n", error);
  738. goto fail_ut_i;
  739. }
  740. iput(pn);
  741. pn = NULL;
  742. ip = GFS2_I(sdp->sd_sc_inode);
  743. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
  744. &sdp->sd_sc_gh);
  745. if (error) {
  746. fs_err(sdp, "can't lock local \"sc\" file: %d\n", error);
  747. goto fail_qc_i;
  748. }
  749. ip = GFS2_I(sdp->sd_qc_inode);
  750. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0,
  751. &sdp->sd_qc_gh);
  752. if (error) {
  753. fs_err(sdp, "can't lock local \"qc\" file: %d\n", error);
  754. goto fail_ut_gh;
  755. }
  756. return 0;
  757. fail_qc_gh:
  758. gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
  759. fail_ut_gh:
  760. gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
  761. fail_qc_i:
  762. iput(sdp->sd_qc_inode);
  763. fail_ut_i:
  764. iput(sdp->sd_sc_inode);
  765. fail:
  766. if (pn)
  767. iput(pn);
  768. return error;
  769. }
  770. static int init_threads(struct gfs2_sbd *sdp, int undo)
  771. {
  772. struct task_struct *p;
  773. int error = 0;
  774. if (undo)
  775. goto fail_quotad;
  776. p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
  777. error = IS_ERR(p);
  778. if (error) {
  779. fs_err(sdp, "can't start logd thread: %d\n", error);
  780. return error;
  781. }
  782. sdp->sd_logd_process = p;
  783. p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
  784. error = IS_ERR(p);
  785. if (error) {
  786. fs_err(sdp, "can't start quotad thread: %d\n", error);
  787. goto fail;
  788. }
  789. sdp->sd_quotad_process = p;
  790. return 0;
  791. fail_quotad:
  792. kthread_stop(sdp->sd_quotad_process);
  793. fail:
  794. kthread_stop(sdp->sd_logd_process);
  795. return error;
  796. }
  797. static const match_table_t nolock_tokens = {
  798. { Opt_jid, "jid=%d\n", },
  799. { Opt_err, NULL },
  800. };
  801. static const struct lm_lockops nolock_ops = {
  802. .lm_proto_name = "lock_nolock",
  803. .lm_put_lock = gfs2_glock_free,
  804. .lm_tokens = &nolock_tokens,
  805. };
  806. /**
  807. * gfs2_lm_mount - mount a locking protocol
  808. * @sdp: the filesystem
  809. * @args: mount arguments
  810. * @silent: if 1, don't complain if the FS isn't a GFS2 fs
  811. *
  812. * Returns: errno
  813. */
  814. static int gfs2_lm_mount(struct gfs2_sbd *sdp, int silent)
  815. {
  816. const struct lm_lockops *lm;
  817. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  818. struct gfs2_args *args = &sdp->sd_args;
  819. const char *proto = sdp->sd_proto_name;
  820. const char *table = sdp->sd_table_name;
  821. char *o, *options;
  822. int ret;
  823. if (!strcmp("lock_nolock", proto)) {
  824. lm = &nolock_ops;
  825. sdp->sd_args.ar_localflocks = 1;
  826. #ifdef CONFIG_GFS2_FS_LOCKING_DLM
  827. } else if (!strcmp("lock_dlm", proto)) {
  828. lm = &gfs2_dlm_ops;
  829. #endif
  830. } else {
  831. printk(KERN_INFO "GFS2: can't find protocol %s\n", proto);
  832. return -ENOENT;
  833. }
  834. fs_info(sdp, "Trying to join cluster \"%s\", \"%s\"\n", proto, table);
  835. ls->ls_ops = lm;
  836. ls->ls_first = 1;
  837. for (options = args->ar_hostdata; (o = strsep(&options, ":")); ) {
  838. substring_t tmp[MAX_OPT_ARGS];
  839. int token, option;
  840. if (!o || !*o)
  841. continue;
  842. token = match_token(o, *lm->lm_tokens, tmp);
  843. switch (token) {
  844. case Opt_jid:
  845. ret = match_int(&tmp[0], &option);
  846. if (ret || option < 0)
  847. goto hostdata_error;
  848. if (test_and_clear_bit(SDF_NOJOURNALID, &sdp->sd_flags))
  849. ls->ls_jid = option;
  850. break;
  851. case Opt_id:
  852. case Opt_nodir:
  853. /* Obsolete, but left for backward compat purposes */
  854. break;
  855. case Opt_first:
  856. ret = match_int(&tmp[0], &option);
  857. if (ret || (option != 0 && option != 1))
  858. goto hostdata_error;
  859. ls->ls_first = option;
  860. break;
  861. case Opt_err:
  862. default:
  863. hostdata_error:
  864. fs_info(sdp, "unknown hostdata (%s)\n", o);
  865. return -EINVAL;
  866. }
  867. }
  868. if (lm->lm_mount == NULL) {
  869. fs_info(sdp, "Now mounting FS...\n");
  870. complete_all(&sdp->sd_locking_init);
  871. return 0;
  872. }
  873. ret = lm->lm_mount(sdp, table);
  874. if (ret == 0)
  875. fs_info(sdp, "Joined cluster. Now mounting FS...\n");
  876. complete_all(&sdp->sd_locking_init);
  877. return ret;
  878. }
  879. void gfs2_lm_unmount(struct gfs2_sbd *sdp)
  880. {
  881. const struct lm_lockops *lm = sdp->sd_lockstruct.ls_ops;
  882. if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) &&
  883. lm->lm_unmount)
  884. lm->lm_unmount(sdp);
  885. }
  886. static int gfs2_journalid_wait(void *word)
  887. {
  888. if (signal_pending(current))
  889. return -EINTR;
  890. schedule();
  891. return 0;
  892. }
  893. static int wait_on_journal(struct gfs2_sbd *sdp)
  894. {
  895. if (sdp->sd_lockstruct.ls_ops->lm_mount == NULL)
  896. return 0;
  897. return wait_on_bit(&sdp->sd_flags, SDF_NOJOURNALID, gfs2_journalid_wait, TASK_INTERRUPTIBLE);
  898. }
  899. void gfs2_online_uevent(struct gfs2_sbd *sdp)
  900. {
  901. struct super_block *sb = sdp->sd_vfs;
  902. char ro[20];
  903. char spectator[20];
  904. char *envp[] = { ro, spectator, NULL };
  905. sprintf(ro, "RDONLY=%d", (sb->s_flags & MS_RDONLY) ? 1 : 0);
  906. sprintf(spectator, "SPECTATOR=%d", sdp->sd_args.ar_spectator ? 1 : 0);
  907. kobject_uevent_env(&sdp->sd_kobj, KOBJ_ONLINE, envp);
  908. }
  909. /**
  910. * fill_super - Read in superblock
  911. * @sb: The VFS superblock
  912. * @data: Mount options
  913. * @silent: Don't complain if it's not a GFS2 filesystem
  914. *
  915. * Returns: errno
  916. */
  917. static int fill_super(struct super_block *sb, struct gfs2_args *args, int silent)
  918. {
  919. struct gfs2_sbd *sdp;
  920. struct gfs2_holder mount_gh;
  921. int error;
  922. sdp = init_sbd(sb);
  923. if (!sdp) {
  924. printk(KERN_WARNING "GFS2: can't alloc struct gfs2_sbd\n");
  925. return -ENOMEM;
  926. }
  927. sdp->sd_args = *args;
  928. if (sdp->sd_args.ar_spectator) {
  929. sb->s_flags |= MS_RDONLY;
  930. set_bit(SDF_RORECOVERY, &sdp->sd_flags);
  931. }
  932. if (sdp->sd_args.ar_posix_acl)
  933. sb->s_flags |= MS_POSIXACL;
  934. if (sdp->sd_args.ar_nobarrier)
  935. set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
  936. sb->s_flags |= MS_NOSEC;
  937. sb->s_magic = GFS2_MAGIC;
  938. sb->s_op = &gfs2_super_ops;
  939. sb->s_d_op = &gfs2_dops;
  940. sb->s_export_op = &gfs2_export_ops;
  941. sb->s_xattr = gfs2_xattr_handlers;
  942. sb->s_qcop = &gfs2_quotactl_ops;
  943. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
  944. sb->s_time_gran = 1;
  945. sb->s_maxbytes = MAX_LFS_FILESIZE;
  946. /* Set up the buffer cache and fill in some fake block size values
  947. to allow us to read-in the on-disk superblock. */
  948. sdp->sd_sb.sb_bsize = sb_min_blocksize(sb, GFS2_BASIC_BLOCK);
  949. sdp->sd_sb.sb_bsize_shift = sb->s_blocksize_bits;
  950. sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
  951. GFS2_BASIC_BLOCK_SHIFT;
  952. sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
  953. sdp->sd_tune.gt_logd_secs = sdp->sd_args.ar_commit;
  954. sdp->sd_tune.gt_quota_quantum = sdp->sd_args.ar_quota_quantum;
  955. if (sdp->sd_args.ar_statfs_quantum) {
  956. sdp->sd_tune.gt_statfs_slow = 0;
  957. sdp->sd_tune.gt_statfs_quantum = sdp->sd_args.ar_statfs_quantum;
  958. } else {
  959. sdp->sd_tune.gt_statfs_slow = 1;
  960. sdp->sd_tune.gt_statfs_quantum = 30;
  961. }
  962. error = init_names(sdp, silent);
  963. if (error) {
  964. /* In this case, we haven't initialized sysfs, so we have to
  965. manually free the sdp. */
  966. free_percpu(sdp->sd_lkstats);
  967. kfree(sdp);
  968. sb->s_fs_info = NULL;
  969. return error;
  970. }
  971. snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s", sdp->sd_table_name);
  972. error = gfs2_sys_fs_add(sdp);
  973. /*
  974. * If we hit an error here, gfs2_sys_fs_add will have called function
  975. * kobject_put which causes the sysfs usage count to go to zero, which
  976. * causes sysfs to call function gfs2_sbd_release, which frees sdp.
  977. * Subsequent error paths here will call gfs2_sys_fs_del, which also
  978. * kobject_put to free sdp.
  979. */
  980. if (error)
  981. return error;
  982. gfs2_create_debugfs_file(sdp);
  983. error = gfs2_lm_mount(sdp, silent);
  984. if (error)
  985. goto fail_debug;
  986. error = init_locking(sdp, &mount_gh, DO);
  987. if (error)
  988. goto fail_lm;
  989. error = init_sb(sdp, silent);
  990. if (error)
  991. goto fail_locking;
  992. error = wait_on_journal(sdp);
  993. if (error)
  994. goto fail_sb;
  995. /*
  996. * If user space has failed to join the cluster or some similar
  997. * failure has occurred, then the journal id will contain a
  998. * negative (error) number. This will then be returned to the
  999. * caller (of the mount syscall). We do this even for spectator
  1000. * mounts (which just write a jid of 0 to indicate "ok" even though
  1001. * the jid is unused in the spectator case)
  1002. */
  1003. if (sdp->sd_lockstruct.ls_jid < 0) {
  1004. error = sdp->sd_lockstruct.ls_jid;
  1005. sdp->sd_lockstruct.ls_jid = 0;
  1006. goto fail_sb;
  1007. }
  1008. if (sdp->sd_args.ar_spectator)
  1009. snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.s",
  1010. sdp->sd_table_name);
  1011. else
  1012. snprintf(sdp->sd_fsname, GFS2_FSNAME_LEN, "%s.%u",
  1013. sdp->sd_table_name, sdp->sd_lockstruct.ls_jid);
  1014. error = init_inodes(sdp, DO);
  1015. if (error)
  1016. goto fail_sb;
  1017. error = init_per_node(sdp, DO);
  1018. if (error)
  1019. goto fail_inodes;
  1020. error = gfs2_statfs_init(sdp);
  1021. if (error) {
  1022. fs_err(sdp, "can't initialize statfs subsystem: %d\n", error);
  1023. goto fail_per_node;
  1024. }
  1025. error = init_threads(sdp, DO);
  1026. if (error)
  1027. goto fail_per_node;
  1028. if (!(sb->s_flags & MS_RDONLY)) {
  1029. error = gfs2_make_fs_rw(sdp);
  1030. if (error) {
  1031. fs_err(sdp, "can't make FS RW: %d\n", error);
  1032. goto fail_threads;
  1033. }
  1034. }
  1035. gfs2_glock_dq_uninit(&mount_gh);
  1036. gfs2_online_uevent(sdp);
  1037. return 0;
  1038. fail_threads:
  1039. init_threads(sdp, UNDO);
  1040. fail_per_node:
  1041. init_per_node(sdp, UNDO);
  1042. fail_inodes:
  1043. init_inodes(sdp, UNDO);
  1044. fail_sb:
  1045. if (sdp->sd_root_dir)
  1046. dput(sdp->sd_root_dir);
  1047. if (sdp->sd_master_dir)
  1048. dput(sdp->sd_master_dir);
  1049. if (sb->s_root)
  1050. dput(sb->s_root);
  1051. sb->s_root = NULL;
  1052. fail_locking:
  1053. init_locking(sdp, &mount_gh, UNDO);
  1054. fail_lm:
  1055. gfs2_gl_hash_clear(sdp);
  1056. gfs2_lm_unmount(sdp);
  1057. fail_debug:
  1058. gfs2_delete_debugfs_file(sdp);
  1059. free_percpu(sdp->sd_lkstats);
  1060. /* gfs2_sys_fs_del must be the last thing we do, since it causes
  1061. * sysfs to call function gfs2_sbd_release, which frees sdp. */
  1062. gfs2_sys_fs_del(sdp);
  1063. sb->s_fs_info = NULL;
  1064. return error;
  1065. }
  1066. static int set_gfs2_super(struct super_block *s, void *data)
  1067. {
  1068. s->s_bdev = data;
  1069. s->s_dev = s->s_bdev->bd_dev;
  1070. /*
  1071. * We set the bdi here to the queue backing, file systems can
  1072. * overwrite this in ->fill_super()
  1073. */
  1074. s->s_bdi = &bdev_get_queue(s->s_bdev)->backing_dev_info;
  1075. return 0;
  1076. }
  1077. static int test_gfs2_super(struct super_block *s, void *ptr)
  1078. {
  1079. struct block_device *bdev = ptr;
  1080. return (bdev == s->s_bdev);
  1081. }
  1082. /**
  1083. * gfs2_mount - Get the GFS2 superblock
  1084. * @fs_type: The GFS2 filesystem type
  1085. * @flags: Mount flags
  1086. * @dev_name: The name of the device
  1087. * @data: The mount arguments
  1088. *
  1089. * Q. Why not use get_sb_bdev() ?
  1090. * A. We need to select one of two root directories to mount, independent
  1091. * of whether this is the initial, or subsequent, mount of this sb
  1092. *
  1093. * Returns: 0 or -ve on error
  1094. */
  1095. static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
  1096. const char *dev_name, void *data)
  1097. {
  1098. struct block_device *bdev;
  1099. struct super_block *s;
  1100. fmode_t mode = FMODE_READ | FMODE_EXCL;
  1101. int error;
  1102. struct gfs2_args args;
  1103. struct gfs2_sbd *sdp;
  1104. if (!(flags & MS_RDONLY))
  1105. mode |= FMODE_WRITE;
  1106. bdev = blkdev_get_by_path(dev_name, mode, fs_type);
  1107. if (IS_ERR(bdev))
  1108. return ERR_CAST(bdev);
  1109. /*
  1110. * once the super is inserted into the list by sget, s_umount
  1111. * will protect the lockfs code from trying to start a snapshot
  1112. * while we are mounting
  1113. */
  1114. mutex_lock(&bdev->bd_fsfreeze_mutex);
  1115. if (bdev->bd_fsfreeze_count > 0) {
  1116. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  1117. error = -EBUSY;
  1118. goto error_bdev;
  1119. }
  1120. s = sget(fs_type, test_gfs2_super, set_gfs2_super, flags, bdev);
  1121. mutex_unlock(&bdev->bd_fsfreeze_mutex);
  1122. error = PTR_ERR(s);
  1123. if (IS_ERR(s))
  1124. goto error_bdev;
  1125. if (s->s_root)
  1126. blkdev_put(bdev, mode);
  1127. memset(&args, 0, sizeof(args));
  1128. args.ar_quota = GFS2_QUOTA_DEFAULT;
  1129. args.ar_data = GFS2_DATA_DEFAULT;
  1130. args.ar_commit = 30;
  1131. args.ar_statfs_quantum = 30;
  1132. args.ar_quota_quantum = 60;
  1133. args.ar_errors = GFS2_ERRORS_DEFAULT;
  1134. error = gfs2_mount_args(&args, data);
  1135. if (error) {
  1136. printk(KERN_WARNING "GFS2: can't parse mount arguments\n");
  1137. goto error_super;
  1138. }
  1139. if (s->s_root) {
  1140. error = -EBUSY;
  1141. if ((flags ^ s->s_flags) & MS_RDONLY)
  1142. goto error_super;
  1143. } else {
  1144. char b[BDEVNAME_SIZE];
  1145. s->s_mode = mode;
  1146. strlcpy(s->s_id, bdevname(bdev, b), sizeof(s->s_id));
  1147. sb_set_blocksize(s, block_size(bdev));
  1148. error = fill_super(s, &args, flags & MS_SILENT ? 1 : 0);
  1149. if (error)
  1150. goto error_super;
  1151. s->s_flags |= MS_ACTIVE;
  1152. bdev->bd_super = s;
  1153. }
  1154. sdp = s->s_fs_info;
  1155. if (args.ar_meta)
  1156. return dget(sdp->sd_master_dir);
  1157. else
  1158. return dget(sdp->sd_root_dir);
  1159. error_super:
  1160. deactivate_locked_super(s);
  1161. return ERR_PTR(error);
  1162. error_bdev:
  1163. blkdev_put(bdev, mode);
  1164. return ERR_PTR(error);
  1165. }
  1166. static int set_meta_super(struct super_block *s, void *ptr)
  1167. {
  1168. return -EINVAL;
  1169. }
  1170. static struct dentry *gfs2_mount_meta(struct file_system_type *fs_type,
  1171. int flags, const char *dev_name, void *data)
  1172. {
  1173. struct super_block *s;
  1174. struct gfs2_sbd *sdp;
  1175. struct path path;
  1176. int error;
  1177. error = kern_path(dev_name, LOOKUP_FOLLOW, &path);
  1178. if (error) {
  1179. printk(KERN_WARNING "GFS2: path_lookup on %s returned error %d\n",
  1180. dev_name, error);
  1181. return ERR_PTR(error);
  1182. }
  1183. s = sget(&gfs2_fs_type, test_gfs2_super, set_meta_super, flags,
  1184. path.dentry->d_inode->i_sb->s_bdev);
  1185. path_put(&path);
  1186. if (IS_ERR(s)) {
  1187. printk(KERN_WARNING "GFS2: gfs2 mount does not exist\n");
  1188. return ERR_CAST(s);
  1189. }
  1190. if ((flags ^ s->s_flags) & MS_RDONLY) {
  1191. deactivate_locked_super(s);
  1192. return ERR_PTR(-EBUSY);
  1193. }
  1194. sdp = s->s_fs_info;
  1195. return dget(sdp->sd_master_dir);
  1196. }
  1197. static void gfs2_kill_sb(struct super_block *sb)
  1198. {
  1199. struct gfs2_sbd *sdp = sb->s_fs_info;
  1200. if (sdp == NULL) {
  1201. kill_block_super(sb);
  1202. return;
  1203. }
  1204. gfs2_meta_syncfs(sdp);
  1205. dput(sdp->sd_root_dir);
  1206. dput(sdp->sd_master_dir);
  1207. sdp->sd_root_dir = NULL;
  1208. sdp->sd_master_dir = NULL;
  1209. shrink_dcache_sb(sb);
  1210. gfs2_delete_debugfs_file(sdp);
  1211. free_percpu(sdp->sd_lkstats);
  1212. kill_block_super(sb);
  1213. }
  1214. struct file_system_type gfs2_fs_type = {
  1215. .name = "gfs2",
  1216. .fs_flags = FS_REQUIRES_DEV,
  1217. .mount = gfs2_mount,
  1218. .kill_sb = gfs2_kill_sb,
  1219. .owner = THIS_MODULE,
  1220. };
  1221. struct file_system_type gfs2meta_fs_type = {
  1222. .name = "gfs2meta",
  1223. .fs_flags = FS_REQUIRES_DEV,
  1224. .mount = gfs2_mount_meta,
  1225. .owner = THIS_MODULE,
  1226. };