super.c 24 KB

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