super.c 24 KB

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