super.c 24 KB

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