super.c 23 KB

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