super.c 22 KB

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