super.c 22 KB

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