super.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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/crc32.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/bio.h>
  17. #include <linux/lm_interface.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "bmap.h"
  21. #include "dir.h"
  22. #include "glock.h"
  23. #include "glops.h"
  24. #include "inode.h"
  25. #include "log.h"
  26. #include "meta_io.h"
  27. #include "quota.h"
  28. #include "recovery.h"
  29. #include "rgrp.h"
  30. #include "super.h"
  31. #include "trans.h"
  32. #include "util.h"
  33. static const u32 gfs2_old_fs_formats[] = {
  34. 0
  35. };
  36. static const u32 gfs2_old_multihost_formats[] = {
  37. 0
  38. };
  39. /**
  40. * gfs2_tune_init - Fill a gfs2_tune structure with default values
  41. * @gt: tune
  42. *
  43. */
  44. void gfs2_tune_init(struct gfs2_tune *gt)
  45. {
  46. spin_lock_init(&gt->gt_spin);
  47. gt->gt_ilimit = 100;
  48. gt->gt_ilimit_tries = 3;
  49. gt->gt_ilimit_min = 1;
  50. gt->gt_demote_secs = 300;
  51. gt->gt_incore_log_blocks = 1024;
  52. gt->gt_log_flush_secs = 60;
  53. gt->gt_jindex_refresh_secs = 60;
  54. gt->gt_scand_secs = 15;
  55. gt->gt_recoverd_secs = 60;
  56. gt->gt_logd_secs = 1;
  57. gt->gt_quotad_secs = 5;
  58. gt->gt_quota_simul_sync = 64;
  59. gt->gt_quota_warn_period = 10;
  60. gt->gt_quota_scale_num = 1;
  61. gt->gt_quota_scale_den = 1;
  62. gt->gt_quota_cache_secs = 300;
  63. gt->gt_quota_quantum = 60;
  64. gt->gt_atime_quantum = 3600;
  65. gt->gt_new_files_jdata = 0;
  66. gt->gt_new_files_directio = 0;
  67. gt->gt_max_readahead = 1 << 18;
  68. gt->gt_lockdump_size = 131072;
  69. gt->gt_stall_secs = 600;
  70. gt->gt_complain_secs = 10;
  71. gt->gt_reclaim_limit = 5000;
  72. gt->gt_statfs_quantum = 30;
  73. gt->gt_statfs_slow = 0;
  74. }
  75. /**
  76. * gfs2_check_sb - Check superblock
  77. * @sdp: the filesystem
  78. * @sb: The superblock
  79. * @silent: Don't print a message if the check fails
  80. *
  81. * Checks the version code of the FS is one that we understand how to
  82. * read and that the sizes of the various on-disk structures have not
  83. * changed.
  84. */
  85. int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb_host *sb, int silent)
  86. {
  87. unsigned int x;
  88. if (sb->sb_magic != GFS2_MAGIC ||
  89. sb->sb_type != GFS2_METATYPE_SB) {
  90. if (!silent)
  91. printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
  92. return -EINVAL;
  93. }
  94. /* If format numbers match exactly, we're done. */
  95. if (sb->sb_fs_format == GFS2_FORMAT_FS &&
  96. sb->sb_multihost_format == GFS2_FORMAT_MULTI)
  97. return 0;
  98. if (sb->sb_fs_format != GFS2_FORMAT_FS) {
  99. for (x = 0; gfs2_old_fs_formats[x]; x++)
  100. if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
  101. break;
  102. if (!gfs2_old_fs_formats[x]) {
  103. printk(KERN_WARNING
  104. "GFS2: code version (%u, %u) is incompatible "
  105. "with ondisk format (%u, %u)\n",
  106. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  107. sb->sb_fs_format, sb->sb_multihost_format);
  108. printk(KERN_WARNING
  109. "GFS2: I don't know how to upgrade this FS\n");
  110. return -EINVAL;
  111. }
  112. }
  113. if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
  114. for (x = 0; gfs2_old_multihost_formats[x]; x++)
  115. if (gfs2_old_multihost_formats[x] ==
  116. sb->sb_multihost_format)
  117. break;
  118. if (!gfs2_old_multihost_formats[x]) {
  119. printk(KERN_WARNING
  120. "GFS2: code version (%u, %u) is incompatible "
  121. "with ondisk format (%u, %u)\n",
  122. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  123. sb->sb_fs_format, sb->sb_multihost_format);
  124. printk(KERN_WARNING
  125. "GFS2: I don't know how to upgrade this FS\n");
  126. return -EINVAL;
  127. }
  128. }
  129. if (!sdp->sd_args.ar_upgrade) {
  130. printk(KERN_WARNING
  131. "GFS2: code version (%u, %u) is incompatible "
  132. "with ondisk format (%u, %u)\n",
  133. GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
  134. sb->sb_fs_format, sb->sb_multihost_format);
  135. printk(KERN_INFO
  136. "GFS2: Use the \"upgrade\" mount option to upgrade "
  137. "the FS\n");
  138. printk(KERN_INFO "GFS2: See the manual for more details\n");
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. static void end_bio_io_page(struct bio *bio, int error)
  144. {
  145. struct page *page = bio->bi_private;
  146. if (!error)
  147. SetPageUptodate(page);
  148. else
  149. printk(KERN_WARNING "gfs2: error %d reading superblock\n", error);
  150. unlock_page(page);
  151. return 0;
  152. }
  153. static void gfs2_sb_in(struct gfs2_sb_host *sb, const void *buf)
  154. {
  155. const struct gfs2_sb *str = buf;
  156. sb->sb_magic = be32_to_cpu(str->sb_header.mh_magic);
  157. sb->sb_type = be32_to_cpu(str->sb_header.mh_type);
  158. sb->sb_format = be32_to_cpu(str->sb_header.mh_format);
  159. sb->sb_fs_format = be32_to_cpu(str->sb_fs_format);
  160. sb->sb_multihost_format = be32_to_cpu(str->sb_multihost_format);
  161. sb->sb_bsize = be32_to_cpu(str->sb_bsize);
  162. sb->sb_bsize_shift = be32_to_cpu(str->sb_bsize_shift);
  163. sb->sb_master_dir.no_addr = be64_to_cpu(str->sb_master_dir.no_addr);
  164. sb->sb_master_dir.no_formal_ino = be64_to_cpu(str->sb_master_dir.no_formal_ino);
  165. sb->sb_root_dir.no_addr = be64_to_cpu(str->sb_root_dir.no_addr);
  166. sb->sb_root_dir.no_formal_ino = be64_to_cpu(str->sb_root_dir.no_formal_ino);
  167. memcpy(sb->sb_lockproto, str->sb_lockproto, GFS2_LOCKNAME_LEN);
  168. memcpy(sb->sb_locktable, str->sb_locktable, GFS2_LOCKNAME_LEN);
  169. }
  170. /**
  171. * gfs2_read_super - Read the gfs2 super block from disk
  172. * @sdp: The GFS2 super block
  173. * @sector: The location of the super block
  174. * @error: The error code to return
  175. *
  176. * This uses the bio functions to read the super block from disk
  177. * because we want to be 100% sure that we never read cached data.
  178. * A super block is read twice only during each GFS2 mount and is
  179. * never written to by the filesystem. The first time its read no
  180. * locks are held, and the only details which are looked at are those
  181. * relating to the locking protocol. Once locking is up and working,
  182. * the sb is read again under the lock to establish the location of
  183. * the master directory (contains pointers to journals etc) and the
  184. * root directory.
  185. *
  186. * Returns: 0 on success or error
  187. */
  188. int gfs2_read_super(struct gfs2_sbd *sdp, sector_t sector)
  189. {
  190. struct super_block *sb = sdp->sd_vfs;
  191. struct gfs2_sb *p;
  192. struct page *page;
  193. struct bio *bio;
  194. page = alloc_page(GFP_KERNEL);
  195. if (unlikely(!page))
  196. return -ENOBUFS;
  197. ClearPageUptodate(page);
  198. ClearPageDirty(page);
  199. lock_page(page);
  200. bio = bio_alloc(GFP_KERNEL, 1);
  201. if (unlikely(!bio)) {
  202. __free_page(page);
  203. return -ENOBUFS;
  204. }
  205. bio->bi_sector = sector * (sb->s_blocksize >> 9);
  206. bio->bi_bdev = sb->s_bdev;
  207. bio_add_page(bio, page, PAGE_SIZE, 0);
  208. bio->bi_end_io = end_bio_io_page;
  209. bio->bi_private = page;
  210. submit_bio(READ_SYNC | (1 << BIO_RW_META), 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->sd_sb, p);
  219. kunmap(page);
  220. __free_page(page);
  221. return 0;
  222. }
  223. /**
  224. * gfs2_read_sb - Read super block
  225. * @sdp: The GFS2 superblock
  226. * @gl: the glock for the superblock (assumed to be held)
  227. * @silent: Don't print message if mount fails
  228. *
  229. */
  230. int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
  231. {
  232. u32 hash_blocks, ind_blocks, leaf_blocks;
  233. u32 tmp_blocks;
  234. unsigned int x;
  235. int error;
  236. error = gfs2_read_super(sdp, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift);
  237. if (error) {
  238. if (!silent)
  239. fs_err(sdp, "can't read superblock\n");
  240. return error;
  241. }
  242. error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
  243. if (error)
  244. return error;
  245. sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
  246. GFS2_BASIC_BLOCK_SHIFT;
  247. sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
  248. sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
  249. sizeof(struct gfs2_dinode)) / sizeof(u64);
  250. sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
  251. sizeof(struct gfs2_meta_header)) / sizeof(u64);
  252. sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
  253. sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
  254. sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
  255. sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(u64);
  256. sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
  257. sizeof(struct gfs2_meta_header)) /
  258. sizeof(struct gfs2_quota_change);
  259. /* Compute maximum reservation required to add a entry to a directory */
  260. hash_blocks = DIV_ROUND_UP(sizeof(u64) * (1 << GFS2_DIR_MAX_DEPTH),
  261. sdp->sd_jbsize);
  262. ind_blocks = 0;
  263. for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
  264. tmp_blocks = DIV_ROUND_UP(tmp_blocks, sdp->sd_inptrs);
  265. ind_blocks += tmp_blocks;
  266. }
  267. leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
  268. sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
  269. sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
  270. sizeof(struct gfs2_dinode);
  271. sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
  272. for (x = 2;; x++) {
  273. u64 space, d;
  274. u32 m;
  275. space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
  276. d = space;
  277. m = do_div(d, sdp->sd_inptrs);
  278. if (d != sdp->sd_heightsize[x - 1] || m)
  279. break;
  280. sdp->sd_heightsize[x] = space;
  281. }
  282. sdp->sd_max_height = x;
  283. 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. gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
  299. return 0;
  300. }
  301. /**
  302. * gfs2_jindex_hold - Grab a lock on the jindex
  303. * @sdp: The GFS2 superblock
  304. * @ji_gh: the holder for the jindex glock
  305. *
  306. * This is very similar to the gfs2_rindex_hold() function, except that
  307. * in general we hold the jindex lock for longer periods of time and
  308. * we grab it far less frequently (in general) then the rgrp lock.
  309. *
  310. * Returns: errno
  311. */
  312. int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
  313. {
  314. struct gfs2_inode *dip = GFS2_I(sdp->sd_jindex);
  315. struct qstr name;
  316. char buf[20];
  317. struct gfs2_jdesc *jd;
  318. int error;
  319. name.name = buf;
  320. mutex_lock(&sdp->sd_jindex_mutex);
  321. for (;;) {
  322. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, ji_gh);
  323. if (error)
  324. break;
  325. name.len = sprintf(buf, "journal%u", sdp->sd_journals);
  326. name.hash = gfs2_disk_hash(name.name, name.len);
  327. error = gfs2_dir_check(sdp->sd_jindex, &name, NULL);
  328. if (error == -ENOENT) {
  329. error = 0;
  330. break;
  331. }
  332. gfs2_glock_dq_uninit(ji_gh);
  333. if (error)
  334. break;
  335. error = -ENOMEM;
  336. jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
  337. if (!jd)
  338. break;
  339. jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
  340. if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
  341. if (!jd->jd_inode)
  342. error = -ENOENT;
  343. else
  344. error = PTR_ERR(jd->jd_inode);
  345. kfree(jd);
  346. break;
  347. }
  348. spin_lock(&sdp->sd_jindex_spin);
  349. jd->jd_jid = sdp->sd_journals++;
  350. list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
  351. spin_unlock(&sdp->sd_jindex_spin);
  352. }
  353. mutex_unlock(&sdp->sd_jindex_mutex);
  354. return error;
  355. }
  356. /**
  357. * gfs2_jindex_free - Clear all the journal index information
  358. * @sdp: The GFS2 superblock
  359. *
  360. */
  361. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  362. {
  363. struct list_head list;
  364. struct gfs2_jdesc *jd;
  365. spin_lock(&sdp->sd_jindex_spin);
  366. list_add(&list, &sdp->sd_jindex_list);
  367. list_del_init(&sdp->sd_jindex_list);
  368. sdp->sd_journals = 0;
  369. spin_unlock(&sdp->sd_jindex_spin);
  370. while (!list_empty(&list)) {
  371. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  372. list_del(&jd->jd_list);
  373. iput(jd->jd_inode);
  374. kfree(jd);
  375. }
  376. }
  377. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  378. {
  379. struct gfs2_jdesc *jd;
  380. int found = 0;
  381. list_for_each_entry(jd, head, jd_list) {
  382. if (jd->jd_jid == jid) {
  383. found = 1;
  384. break;
  385. }
  386. }
  387. if (!found)
  388. jd = NULL;
  389. return jd;
  390. }
  391. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  392. {
  393. struct gfs2_jdesc *jd;
  394. spin_lock(&sdp->sd_jindex_spin);
  395. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  396. spin_unlock(&sdp->sd_jindex_spin);
  397. return jd;
  398. }
  399. void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
  400. {
  401. struct gfs2_jdesc *jd;
  402. spin_lock(&sdp->sd_jindex_spin);
  403. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  404. if (jd)
  405. jd->jd_dirty = 1;
  406. spin_unlock(&sdp->sd_jindex_spin);
  407. }
  408. struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
  409. {
  410. struct gfs2_jdesc *jd;
  411. int found = 0;
  412. spin_lock(&sdp->sd_jindex_spin);
  413. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  414. if (jd->jd_dirty) {
  415. jd->jd_dirty = 0;
  416. found = 1;
  417. break;
  418. }
  419. }
  420. spin_unlock(&sdp->sd_jindex_spin);
  421. if (!found)
  422. jd = NULL;
  423. return jd;
  424. }
  425. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  426. {
  427. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  428. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  429. int ar;
  430. int error;
  431. if (ip->i_di.di_size < (8 << 20) || ip->i_di.di_size > (1 << 30) ||
  432. (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
  433. gfs2_consist_inode(ip);
  434. return -EIO;
  435. }
  436. jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
  437. error = gfs2_write_alloc_required(ip, 0, ip->i_di.di_size, &ar);
  438. if (!error && ar) {
  439. gfs2_consist_inode(ip);
  440. error = -EIO;
  441. }
  442. return error;
  443. }
  444. /**
  445. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  446. * @sdp: the filesystem
  447. *
  448. * Returns: errno
  449. */
  450. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  451. {
  452. struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
  453. struct gfs2_glock *j_gl = ip->i_gl;
  454. struct gfs2_holder t_gh;
  455. struct gfs2_log_header_host head;
  456. int error;
  457. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &t_gh);
  458. if (error)
  459. return error;
  460. gfs2_meta_cache_flush(ip);
  461. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
  462. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  463. if (error)
  464. goto fail;
  465. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  466. gfs2_consist(sdp);
  467. error = -EIO;
  468. goto fail;
  469. }
  470. /* Initialize some head of the log stuff */
  471. sdp->sd_log_sequence = head.lh_sequence + 1;
  472. gfs2_log_pointers_init(sdp, head.lh_blkno);
  473. error = gfs2_quota_init(sdp);
  474. if (error)
  475. goto fail;
  476. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  477. gfs2_glock_dq_uninit(&t_gh);
  478. return 0;
  479. fail:
  480. t_gh.gh_flags |= GL_NOCACHE;
  481. gfs2_glock_dq_uninit(&t_gh);
  482. return error;
  483. }
  484. /**
  485. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  486. * @sdp: the filesystem
  487. *
  488. * Returns: errno
  489. */
  490. int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  491. {
  492. struct gfs2_holder t_gh;
  493. int error;
  494. gfs2_quota_sync(sdp);
  495. gfs2_statfs_sync(sdp);
  496. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, GL_NOCACHE,
  497. &t_gh);
  498. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  499. return error;
  500. gfs2_meta_syncfs(sdp);
  501. gfs2_log_shutdown(sdp);
  502. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  503. if (t_gh.gh_gl)
  504. gfs2_glock_dq_uninit(&t_gh);
  505. gfs2_quota_cleanup(sdp);
  506. return error;
  507. }
  508. static void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
  509. {
  510. const struct gfs2_statfs_change *str = buf;
  511. sc->sc_total = be64_to_cpu(str->sc_total);
  512. sc->sc_free = be64_to_cpu(str->sc_free);
  513. sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
  514. }
  515. static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
  516. {
  517. struct gfs2_statfs_change *str = buf;
  518. str->sc_total = cpu_to_be64(sc->sc_total);
  519. str->sc_free = cpu_to_be64(sc->sc_free);
  520. str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
  521. }
  522. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  523. {
  524. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  525. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  526. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  527. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  528. struct buffer_head *m_bh, *l_bh;
  529. struct gfs2_holder gh;
  530. int error;
  531. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  532. &gh);
  533. if (error)
  534. return error;
  535. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  536. if (error)
  537. goto out;
  538. if (sdp->sd_args.ar_spectator) {
  539. spin_lock(&sdp->sd_statfs_spin);
  540. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  541. sizeof(struct gfs2_dinode));
  542. spin_unlock(&sdp->sd_statfs_spin);
  543. } else {
  544. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  545. if (error)
  546. goto out_m_bh;
  547. spin_lock(&sdp->sd_statfs_spin);
  548. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  549. sizeof(struct gfs2_dinode));
  550. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  551. sizeof(struct gfs2_dinode));
  552. spin_unlock(&sdp->sd_statfs_spin);
  553. brelse(l_bh);
  554. }
  555. out_m_bh:
  556. brelse(m_bh);
  557. out:
  558. gfs2_glock_dq_uninit(&gh);
  559. return 0;
  560. }
  561. void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
  562. s64 dinodes)
  563. {
  564. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  565. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  566. struct buffer_head *l_bh;
  567. int error;
  568. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  569. if (error)
  570. return;
  571. mutex_lock(&sdp->sd_statfs_mutex);
  572. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  573. mutex_unlock(&sdp->sd_statfs_mutex);
  574. spin_lock(&sdp->sd_statfs_spin);
  575. l_sc->sc_total += total;
  576. l_sc->sc_free += free;
  577. l_sc->sc_dinodes += dinodes;
  578. gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
  579. spin_unlock(&sdp->sd_statfs_spin);
  580. brelse(l_bh);
  581. }
  582. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  583. {
  584. struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
  585. struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
  586. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  587. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  588. struct gfs2_holder gh;
  589. struct buffer_head *m_bh, *l_bh;
  590. int error;
  591. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  592. &gh);
  593. if (error)
  594. return error;
  595. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  596. if (error)
  597. goto out;
  598. spin_lock(&sdp->sd_statfs_spin);
  599. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  600. sizeof(struct gfs2_dinode));
  601. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  602. spin_unlock(&sdp->sd_statfs_spin);
  603. goto out_bh;
  604. }
  605. spin_unlock(&sdp->sd_statfs_spin);
  606. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  607. if (error)
  608. goto out_bh;
  609. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  610. if (error)
  611. goto out_bh2;
  612. mutex_lock(&sdp->sd_statfs_mutex);
  613. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  614. mutex_unlock(&sdp->sd_statfs_mutex);
  615. spin_lock(&sdp->sd_statfs_spin);
  616. m_sc->sc_total += l_sc->sc_total;
  617. m_sc->sc_free += l_sc->sc_free;
  618. m_sc->sc_dinodes += l_sc->sc_dinodes;
  619. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  620. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  621. 0, sizeof(struct gfs2_statfs_change));
  622. spin_unlock(&sdp->sd_statfs_spin);
  623. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  624. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  625. gfs2_trans_end(sdp);
  626. out_bh2:
  627. brelse(l_bh);
  628. out_bh:
  629. brelse(m_bh);
  630. out:
  631. gfs2_glock_dq_uninit(&gh);
  632. return error;
  633. }
  634. /**
  635. * gfs2_statfs_i - Do a statfs
  636. * @sdp: the filesystem
  637. * @sg: the sg structure
  638. *
  639. * Returns: errno
  640. */
  641. int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  642. {
  643. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  644. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  645. spin_lock(&sdp->sd_statfs_spin);
  646. *sc = *m_sc;
  647. sc->sc_total += l_sc->sc_total;
  648. sc->sc_free += l_sc->sc_free;
  649. sc->sc_dinodes += l_sc->sc_dinodes;
  650. spin_unlock(&sdp->sd_statfs_spin);
  651. if (sc->sc_free < 0)
  652. sc->sc_free = 0;
  653. if (sc->sc_free > sc->sc_total)
  654. sc->sc_free = sc->sc_total;
  655. if (sc->sc_dinodes < 0)
  656. sc->sc_dinodes = 0;
  657. return 0;
  658. }
  659. /**
  660. * statfs_fill - fill in the sg for a given RG
  661. * @rgd: the RG
  662. * @sc: the sc structure
  663. *
  664. * Returns: 0 on success, -ESTALE if the LVB is invalid
  665. */
  666. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  667. struct gfs2_statfs_change_host *sc)
  668. {
  669. gfs2_rgrp_verify(rgd);
  670. sc->sc_total += rgd->rd_data;
  671. sc->sc_free += rgd->rd_rg.rg_free;
  672. sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
  673. return 0;
  674. }
  675. /**
  676. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  677. * @sdp: the filesystem
  678. * @sc: the sc info that will be returned
  679. *
  680. * Any error (other than a signal) will cause this routine to fall back
  681. * to the synchronous version.
  682. *
  683. * FIXME: This really shouldn't busy wait like this.
  684. *
  685. * Returns: errno
  686. */
  687. int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
  688. {
  689. struct gfs2_holder ri_gh;
  690. struct gfs2_rgrpd *rgd_next;
  691. struct gfs2_holder *gha, *gh;
  692. unsigned int slots = 64;
  693. unsigned int x;
  694. int done;
  695. int error = 0, err;
  696. memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
  697. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  698. if (!gha)
  699. return -ENOMEM;
  700. error = gfs2_rindex_hold(sdp, &ri_gh);
  701. if (error)
  702. goto out;
  703. rgd_next = gfs2_rgrpd_get_first(sdp);
  704. for (;;) {
  705. done = 1;
  706. for (x = 0; x < slots; x++) {
  707. gh = gha + x;
  708. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  709. err = gfs2_glock_wait(gh);
  710. if (err) {
  711. gfs2_holder_uninit(gh);
  712. error = err;
  713. } else {
  714. if (!error)
  715. error = statfs_slow_fill(
  716. gh->gh_gl->gl_object, sc);
  717. gfs2_glock_dq_uninit(gh);
  718. }
  719. }
  720. if (gh->gh_gl)
  721. done = 0;
  722. else if (rgd_next && !error) {
  723. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  724. LM_ST_SHARED,
  725. GL_ASYNC,
  726. gh);
  727. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  728. done = 0;
  729. }
  730. if (signal_pending(current))
  731. error = -ERESTARTSYS;
  732. }
  733. if (done)
  734. break;
  735. yield();
  736. }
  737. gfs2_glock_dq_uninit(&ri_gh);
  738. out:
  739. kfree(gha);
  740. return error;
  741. }
  742. struct lfcc {
  743. struct list_head list;
  744. struct gfs2_holder gh;
  745. };
  746. /**
  747. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  748. * journals are clean
  749. * @sdp: the file system
  750. * @state: the state to put the transaction lock into
  751. * @t_gh: the hold on the transaction lock
  752. *
  753. * Returns: errno
  754. */
  755. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  756. struct gfs2_holder *t_gh)
  757. {
  758. struct gfs2_inode *ip;
  759. struct gfs2_holder ji_gh;
  760. struct gfs2_jdesc *jd;
  761. struct lfcc *lfcc;
  762. LIST_HEAD(list);
  763. struct gfs2_log_header_host lh;
  764. int error;
  765. error = gfs2_jindex_hold(sdp, &ji_gh);
  766. if (error)
  767. return error;
  768. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  769. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  770. if (!lfcc) {
  771. error = -ENOMEM;
  772. goto out;
  773. }
  774. ip = GFS2_I(jd->jd_inode);
  775. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
  776. if (error) {
  777. kfree(lfcc);
  778. goto out;
  779. }
  780. list_add(&lfcc->list, &list);
  781. }
  782. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  783. LM_FLAG_PRIORITY | GL_NOCACHE,
  784. t_gh);
  785. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  786. error = gfs2_jdesc_check(jd);
  787. if (error)
  788. break;
  789. error = gfs2_find_jhead(jd, &lh);
  790. if (error)
  791. break;
  792. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  793. error = -EBUSY;
  794. break;
  795. }
  796. }
  797. if (error)
  798. gfs2_glock_dq_uninit(t_gh);
  799. out:
  800. while (!list_empty(&list)) {
  801. lfcc = list_entry(list.next, struct lfcc, list);
  802. list_del(&lfcc->list);
  803. gfs2_glock_dq_uninit(&lfcc->gh);
  804. kfree(lfcc);
  805. }
  806. gfs2_glock_dq_uninit(&ji_gh);
  807. return error;
  808. }
  809. /**
  810. * gfs2_freeze_fs - freezes the file system
  811. * @sdp: the file system
  812. *
  813. * This function flushes data and meta data for all machines by
  814. * aquiring the transaction log exclusively. All journals are
  815. * ensured to be in a clean state as well.
  816. *
  817. * Returns: errno
  818. */
  819. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  820. {
  821. int error = 0;
  822. mutex_lock(&sdp->sd_freeze_lock);
  823. if (!sdp->sd_freeze_count++) {
  824. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  825. if (error)
  826. sdp->sd_freeze_count--;
  827. }
  828. mutex_unlock(&sdp->sd_freeze_lock);
  829. return error;
  830. }
  831. /**
  832. * gfs2_unfreeze_fs - unfreezes the file system
  833. * @sdp: the file system
  834. *
  835. * This function allows the file system to proceed by unlocking
  836. * the exclusively held transaction lock. Other GFS2 nodes are
  837. * now free to acquire the lock shared and go on with their lives.
  838. *
  839. */
  840. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  841. {
  842. mutex_lock(&sdp->sd_freeze_lock);
  843. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  844. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  845. mutex_unlock(&sdp->sd_freeze_lock);
  846. }