super.c 22 KB

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