ops_fstype.c 35 KB

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