super.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. /**
  233. * gfs2_jindex_hold - Grab a lock on the jindex
  234. * @sdp: The GFS2 superblock
  235. * @ji_gh: the holder for the jindex glock
  236. *
  237. * This is very similar to the gfs2_rindex_hold() function, except that
  238. * in general we hold the jindex lock for longer periods of time and
  239. * we grab it far less frequently (in general) then the rgrp lock.
  240. *
  241. * Returns: errno
  242. */
  243. int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
  244. {
  245. struct gfs2_inode *dip = sdp->sd_jindex->u.generic_ip;
  246. struct qstr name;
  247. char buf[20];
  248. struct gfs2_jdesc *jd;
  249. int error;
  250. name.name = buf;
  251. mutex_lock(&sdp->sd_jindex_mutex);
  252. for (;;) {
  253. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED,
  254. GL_LOCAL_EXCL, ji_gh);
  255. if (error)
  256. break;
  257. name.len = sprintf(buf, "journal%u", sdp->sd_journals);
  258. name.hash = gfs2_disk_hash(name.name, name.len);
  259. error = gfs2_dir_search(sdp->sd_jindex,
  260. &name, NULL, NULL);
  261. if (error == -ENOENT) {
  262. error = 0;
  263. break;
  264. }
  265. gfs2_glock_dq_uninit(ji_gh);
  266. if (error)
  267. break;
  268. error = -ENOMEM;
  269. jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
  270. if (!jd)
  271. break;
  272. jd->jd_inode = gfs2_lookupi(sdp->sd_jindex, &name, 1, NULL);
  273. if (!jd->jd_inode || IS_ERR(jd->jd_inode)) {
  274. if (!jd->jd_inode)
  275. error = -ENOENT;
  276. else
  277. error = PTR_ERR(jd->jd_inode);
  278. kfree(jd);
  279. break;
  280. }
  281. spin_lock(&sdp->sd_jindex_spin);
  282. jd->jd_jid = sdp->sd_journals++;
  283. list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
  284. spin_unlock(&sdp->sd_jindex_spin);
  285. }
  286. mutex_unlock(&sdp->sd_jindex_mutex);
  287. return error;
  288. }
  289. /**
  290. * gfs2_jindex_free - Clear all the journal index information
  291. * @sdp: The GFS2 superblock
  292. *
  293. */
  294. void gfs2_jindex_free(struct gfs2_sbd *sdp)
  295. {
  296. struct list_head list;
  297. struct gfs2_jdesc *jd;
  298. spin_lock(&sdp->sd_jindex_spin);
  299. list_add(&list, &sdp->sd_jindex_list);
  300. list_del_init(&sdp->sd_jindex_list);
  301. sdp->sd_journals = 0;
  302. spin_unlock(&sdp->sd_jindex_spin);
  303. while (!list_empty(&list)) {
  304. jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
  305. list_del(&jd->jd_list);
  306. iput(jd->jd_inode);
  307. kfree(jd);
  308. }
  309. }
  310. static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
  311. {
  312. struct gfs2_jdesc *jd;
  313. int found = 0;
  314. list_for_each_entry(jd, head, jd_list) {
  315. if (jd->jd_jid == jid) {
  316. found = 1;
  317. break;
  318. }
  319. }
  320. if (!found)
  321. jd = NULL;
  322. return jd;
  323. }
  324. struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
  325. {
  326. struct gfs2_jdesc *jd;
  327. spin_lock(&sdp->sd_jindex_spin);
  328. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  329. spin_unlock(&sdp->sd_jindex_spin);
  330. return jd;
  331. }
  332. void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
  333. {
  334. struct gfs2_jdesc *jd;
  335. spin_lock(&sdp->sd_jindex_spin);
  336. jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
  337. if (jd)
  338. jd->jd_dirty = 1;
  339. spin_unlock(&sdp->sd_jindex_spin);
  340. }
  341. struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
  342. {
  343. struct gfs2_jdesc *jd;
  344. int found = 0;
  345. spin_lock(&sdp->sd_jindex_spin);
  346. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  347. if (jd->jd_dirty) {
  348. jd->jd_dirty = 0;
  349. found = 1;
  350. break;
  351. }
  352. }
  353. spin_unlock(&sdp->sd_jindex_spin);
  354. if (!found)
  355. jd = NULL;
  356. return jd;
  357. }
  358. int gfs2_jdesc_check(struct gfs2_jdesc *jd)
  359. {
  360. struct gfs2_inode *ip = jd->jd_inode->u.generic_ip;
  361. struct gfs2_sbd *sdp = ip->i_sbd;
  362. int ar;
  363. int error;
  364. if (ip->i_di.di_size < (8 << 20) ||
  365. ip->i_di.di_size > (1 << 30) ||
  366. (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
  367. gfs2_consist_inode(ip);
  368. return -EIO;
  369. }
  370. jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
  371. error = gfs2_write_alloc_required(ip,
  372. 0, ip->i_di.di_size,
  373. &ar);
  374. if (!error && ar) {
  375. gfs2_consist_inode(ip);
  376. error = -EIO;
  377. }
  378. return error;
  379. }
  380. /**
  381. * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
  382. * @sdp: the filesystem
  383. *
  384. * Returns: errno
  385. */
  386. int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
  387. {
  388. struct gfs2_inode *ip = sdp->sd_jdesc->jd_inode->u.generic_ip;
  389. struct gfs2_glock *j_gl = ip->i_gl;
  390. struct gfs2_holder t_gh;
  391. struct gfs2_log_header head;
  392. int error;
  393. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
  394. GL_LOCAL_EXCL, &t_gh);
  395. if (error)
  396. return error;
  397. gfs2_meta_cache_flush(ip);
  398. j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
  399. error = gfs2_find_jhead(sdp->sd_jdesc, &head);
  400. if (error)
  401. goto fail;
  402. if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  403. gfs2_consist(sdp);
  404. error = -EIO;
  405. goto fail;
  406. }
  407. /* Initialize some head of the log stuff */
  408. sdp->sd_log_sequence = head.lh_sequence + 1;
  409. gfs2_log_pointers_init(sdp, head.lh_blkno);
  410. error = gfs2_unlinked_init(sdp);
  411. if (error)
  412. goto fail;
  413. error = gfs2_quota_init(sdp);
  414. if (error)
  415. goto fail_unlinked;
  416. set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  417. gfs2_glock_dq_uninit(&t_gh);
  418. return 0;
  419. fail_unlinked:
  420. gfs2_unlinked_cleanup(sdp);
  421. fail:
  422. t_gh.gh_flags |= GL_NOCACHE;
  423. gfs2_glock_dq_uninit(&t_gh);
  424. return error;
  425. }
  426. /**
  427. * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
  428. * @sdp: the filesystem
  429. *
  430. * Returns: errno
  431. */
  432. int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
  433. {
  434. struct gfs2_holder t_gh;
  435. int error;
  436. gfs2_unlinked_dealloc(sdp);
  437. gfs2_quota_sync(sdp);
  438. gfs2_statfs_sync(sdp);
  439. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
  440. GL_LOCAL_EXCL | GL_NOCACHE,
  441. &t_gh);
  442. if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
  443. return error;
  444. gfs2_meta_syncfs(sdp);
  445. gfs2_log_shutdown(sdp);
  446. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  447. if (t_gh.gh_gl)
  448. gfs2_glock_dq_uninit(&t_gh);
  449. gfs2_unlinked_cleanup(sdp);
  450. gfs2_quota_cleanup(sdp);
  451. return error;
  452. }
  453. int gfs2_statfs_init(struct gfs2_sbd *sdp)
  454. {
  455. struct gfs2_inode *m_ip = sdp->sd_statfs_inode->u.generic_ip;
  456. struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
  457. struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
  458. struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
  459. struct buffer_head *m_bh, *l_bh;
  460. struct gfs2_holder gh;
  461. int error;
  462. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  463. &gh);
  464. if (error)
  465. return error;
  466. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  467. if (error)
  468. goto out;
  469. if (sdp->sd_args.ar_spectator) {
  470. spin_lock(&sdp->sd_statfs_spin);
  471. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  472. sizeof(struct gfs2_dinode));
  473. spin_unlock(&sdp->sd_statfs_spin);
  474. } else {
  475. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  476. if (error)
  477. goto out_m_bh;
  478. spin_lock(&sdp->sd_statfs_spin);
  479. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  480. sizeof(struct gfs2_dinode));
  481. gfs2_statfs_change_in(l_sc, l_bh->b_data +
  482. sizeof(struct gfs2_dinode));
  483. spin_unlock(&sdp->sd_statfs_spin);
  484. brelse(l_bh);
  485. }
  486. out_m_bh:
  487. brelse(m_bh);
  488. out:
  489. gfs2_glock_dq_uninit(&gh);
  490. return 0;
  491. }
  492. void gfs2_statfs_change(struct gfs2_sbd *sdp, int64_t total, int64_t free,
  493. int64_t dinodes)
  494. {
  495. struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
  496. struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
  497. struct buffer_head *l_bh;
  498. int error;
  499. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  500. if (error)
  501. return;
  502. mutex_lock(&sdp->sd_statfs_mutex);
  503. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  504. mutex_unlock(&sdp->sd_statfs_mutex);
  505. spin_lock(&sdp->sd_statfs_spin);
  506. l_sc->sc_total += total;
  507. l_sc->sc_free += free;
  508. l_sc->sc_dinodes += dinodes;
  509. gfs2_statfs_change_out(l_sc, l_bh->b_data +
  510. sizeof(struct gfs2_dinode));
  511. spin_unlock(&sdp->sd_statfs_spin);
  512. brelse(l_bh);
  513. }
  514. int gfs2_statfs_sync(struct gfs2_sbd *sdp)
  515. {
  516. struct gfs2_inode *m_ip = sdp->sd_statfs_inode->u.generic_ip;
  517. struct gfs2_inode *l_ip = sdp->sd_sc_inode->u.generic_ip;
  518. struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
  519. struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
  520. struct gfs2_holder gh;
  521. struct buffer_head *m_bh, *l_bh;
  522. int error;
  523. error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
  524. &gh);
  525. if (error)
  526. return error;
  527. error = gfs2_meta_inode_buffer(m_ip, &m_bh);
  528. if (error)
  529. goto out;
  530. spin_lock(&sdp->sd_statfs_spin);
  531. gfs2_statfs_change_in(m_sc, m_bh->b_data +
  532. sizeof(struct gfs2_dinode));
  533. if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
  534. spin_unlock(&sdp->sd_statfs_spin);
  535. goto out_bh;
  536. }
  537. spin_unlock(&sdp->sd_statfs_spin);
  538. error = gfs2_meta_inode_buffer(l_ip, &l_bh);
  539. if (error)
  540. goto out_bh;
  541. error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
  542. if (error)
  543. goto out_bh2;
  544. mutex_lock(&sdp->sd_statfs_mutex);
  545. gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
  546. mutex_unlock(&sdp->sd_statfs_mutex);
  547. spin_lock(&sdp->sd_statfs_spin);
  548. m_sc->sc_total += l_sc->sc_total;
  549. m_sc->sc_free += l_sc->sc_free;
  550. m_sc->sc_dinodes += l_sc->sc_dinodes;
  551. memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
  552. memset(l_bh->b_data + sizeof(struct gfs2_dinode),
  553. 0, sizeof(struct gfs2_statfs_change));
  554. spin_unlock(&sdp->sd_statfs_spin);
  555. gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
  556. gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
  557. gfs2_trans_end(sdp);
  558. out_bh2:
  559. brelse(l_bh);
  560. out_bh:
  561. brelse(m_bh);
  562. out:
  563. gfs2_glock_dq_uninit(&gh);
  564. return error;
  565. }
  566. /**
  567. * gfs2_statfs_i - Do a statfs
  568. * @sdp: the filesystem
  569. * @sg: the sg structure
  570. *
  571. * Returns: errno
  572. */
  573. int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
  574. {
  575. struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
  576. struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
  577. spin_lock(&sdp->sd_statfs_spin);
  578. *sc = *m_sc;
  579. sc->sc_total += l_sc->sc_total;
  580. sc->sc_free += l_sc->sc_free;
  581. sc->sc_dinodes += l_sc->sc_dinodes;
  582. spin_unlock(&sdp->sd_statfs_spin);
  583. if (sc->sc_free < 0)
  584. sc->sc_free = 0;
  585. if (sc->sc_free > sc->sc_total)
  586. sc->sc_free = sc->sc_total;
  587. if (sc->sc_dinodes < 0)
  588. sc->sc_dinodes = 0;
  589. return 0;
  590. }
  591. /**
  592. * statfs_fill - fill in the sg for a given RG
  593. * @rgd: the RG
  594. * @sc: the sc structure
  595. *
  596. * Returns: 0 on success, -ESTALE if the LVB is invalid
  597. */
  598. static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
  599. struct gfs2_statfs_change *sc)
  600. {
  601. gfs2_rgrp_verify(rgd);
  602. sc->sc_total += rgd->rd_ri.ri_data;
  603. sc->sc_free += rgd->rd_rg.rg_free;
  604. sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
  605. return 0;
  606. }
  607. /**
  608. * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
  609. * @sdp: the filesystem
  610. * @sc: the sc info that will be returned
  611. *
  612. * Any error (other than a signal) will cause this routine to fall back
  613. * to the synchronous version.
  614. *
  615. * FIXME: This really shouldn't busy wait like this.
  616. *
  617. * Returns: errno
  618. */
  619. int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
  620. {
  621. struct gfs2_holder ri_gh;
  622. struct gfs2_rgrpd *rgd_next;
  623. struct gfs2_holder *gha, *gh;
  624. unsigned int slots = 64;
  625. unsigned int x;
  626. int done;
  627. int error = 0, err;
  628. memset(sc, 0, sizeof(struct gfs2_statfs_change));
  629. gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
  630. if (!gha)
  631. return -ENOMEM;
  632. error = gfs2_rindex_hold(sdp, &ri_gh);
  633. if (error)
  634. goto out;
  635. rgd_next = gfs2_rgrpd_get_first(sdp);
  636. for (;;) {
  637. done = 1;
  638. for (x = 0; x < slots; x++) {
  639. gh = gha + x;
  640. if (gh->gh_gl && gfs2_glock_poll(gh)) {
  641. err = gfs2_glock_wait(gh);
  642. if (err) {
  643. gfs2_holder_uninit(gh);
  644. error = err;
  645. } else {
  646. if (!error)
  647. error = statfs_slow_fill(
  648. gh->gh_gl->gl_object, sc);
  649. gfs2_glock_dq_uninit(gh);
  650. }
  651. }
  652. if (gh->gh_gl)
  653. done = 0;
  654. else if (rgd_next && !error) {
  655. error = gfs2_glock_nq_init(rgd_next->rd_gl,
  656. LM_ST_SHARED,
  657. GL_ASYNC,
  658. gh);
  659. rgd_next = gfs2_rgrpd_get_next(rgd_next);
  660. done = 0;
  661. }
  662. if (signal_pending(current))
  663. error = -ERESTARTSYS;
  664. }
  665. if (done)
  666. break;
  667. yield();
  668. }
  669. gfs2_glock_dq_uninit(&ri_gh);
  670. out:
  671. kfree(gha);
  672. return error;
  673. }
  674. struct lfcc {
  675. struct list_head list;
  676. struct gfs2_holder gh;
  677. };
  678. /**
  679. * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
  680. * journals are clean
  681. * @sdp: the file system
  682. * @state: the state to put the transaction lock into
  683. * @t_gh: the hold on the transaction lock
  684. *
  685. * Returns: errno
  686. */
  687. static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
  688. struct gfs2_holder *t_gh)
  689. {
  690. struct gfs2_inode *ip;
  691. struct gfs2_holder ji_gh;
  692. struct gfs2_jdesc *jd;
  693. struct lfcc *lfcc;
  694. LIST_HEAD(list);
  695. struct gfs2_log_header lh;
  696. int error;
  697. error = gfs2_jindex_hold(sdp, &ji_gh);
  698. if (error)
  699. return error;
  700. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  701. lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
  702. if (!lfcc) {
  703. error = -ENOMEM;
  704. goto out;
  705. }
  706. ip = jd->jd_inode->u.generic_ip;
  707. error = gfs2_glock_nq_init(ip->i_gl,
  708. LM_ST_SHARED, 0,
  709. &lfcc->gh);
  710. if (error) {
  711. kfree(lfcc);
  712. goto out;
  713. }
  714. list_add(&lfcc->list, &list);
  715. }
  716. error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
  717. LM_FLAG_PRIORITY | GL_NOCACHE,
  718. t_gh);
  719. list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
  720. error = gfs2_jdesc_check(jd);
  721. if (error)
  722. break;
  723. error = gfs2_find_jhead(jd, &lh);
  724. if (error)
  725. break;
  726. if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  727. error = -EBUSY;
  728. break;
  729. }
  730. }
  731. if (error)
  732. gfs2_glock_dq_uninit(t_gh);
  733. out:
  734. while (!list_empty(&list)) {
  735. lfcc = list_entry(list.next, struct lfcc, list);
  736. list_del(&lfcc->list);
  737. gfs2_glock_dq_uninit(&lfcc->gh);
  738. kfree(lfcc);
  739. }
  740. gfs2_glock_dq_uninit(&ji_gh);
  741. return error;
  742. }
  743. /**
  744. * gfs2_freeze_fs - freezes the file system
  745. * @sdp: the file system
  746. *
  747. * This function flushes data and meta data for all machines by
  748. * aquiring the transaction log exclusively. All journals are
  749. * ensured to be in a clean state as well.
  750. *
  751. * Returns: errno
  752. */
  753. int gfs2_freeze_fs(struct gfs2_sbd *sdp)
  754. {
  755. int error = 0;
  756. mutex_lock(&sdp->sd_freeze_lock);
  757. if (!sdp->sd_freeze_count++) {
  758. error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
  759. if (error)
  760. sdp->sd_freeze_count--;
  761. }
  762. mutex_unlock(&sdp->sd_freeze_lock);
  763. return error;
  764. }
  765. /**
  766. * gfs2_unfreeze_fs - unfreezes the file system
  767. * @sdp: the file system
  768. *
  769. * This function allows the file system to proceed by unlocking
  770. * the exclusively held transaction lock. Other GFS2 nodes are
  771. * now free to acquire the lock shared and go on with their lives.
  772. *
  773. */
  774. void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
  775. {
  776. mutex_lock(&sdp->sd_freeze_lock);
  777. if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
  778. gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
  779. mutex_unlock(&sdp->sd_freeze_lock);
  780. }