super.c 21 KB

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