super.c 24 KB

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