ops_fstype.c 35 KB

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