ops_fstype.c 35 KB

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