super.c 24 KB

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