PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/gfs2/ops_fstype.c

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