PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/gfs2/ops_fstype.c

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