blob: 2c1c6aa1c077e8518e2b2805b182baaf559eae08 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 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 v.2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <asm/semaphore.h>
16
17#include "gfs2.h"
18#include "bmap.h"
19#include "dir.h"
20#include "format.h"
21#include "glock.h"
22#include "glops.h"
23#include "inode.h"
24#include "log.h"
25#include "meta_io.h"
26#include "quota.h"
27#include "recovery.h"
28#include "rgrp.h"
29#include "super.h"
30#include "trans.h"
31#include "unlinked.h"
32
33/**
34 * gfs2_tune_init - Fill a gfs2_tune structure with default values
35 * @gt: tune
36 *
37 */
38
39void gfs2_tune_init(struct gfs2_tune *gt)
40{
41 spin_lock_init(&gt->gt_spin);
42
43 gt->gt_ilimit = 100;
44 gt->gt_ilimit_tries = 3;
45 gt->gt_ilimit_min = 1;
46 gt->gt_demote_secs = 300;
47 gt->gt_incore_log_blocks = 1024;
48 gt->gt_log_flush_secs = 60;
49 gt->gt_jindex_refresh_secs = 60;
50 gt->gt_scand_secs = 15;
51 gt->gt_recoverd_secs = 60;
52 gt->gt_logd_secs = 1;
53 gt->gt_quotad_secs = 5;
54 gt->gt_inoded_secs = 15;
55 gt->gt_quota_simul_sync = 64;
56 gt->gt_quota_warn_period = 10;
57 gt->gt_quota_scale_num = 1;
58 gt->gt_quota_scale_den = 1;
59 gt->gt_quota_cache_secs = 300;
60 gt->gt_quota_quantum = 60;
61 gt->gt_atime_quantum = 3600;
62 gt->gt_new_files_jdata = 0;
63 gt->gt_new_files_directio = 0;
64 gt->gt_max_atomic_write = 4 << 20;
65 gt->gt_max_readahead = 1 << 18;
66 gt->gt_lockdump_size = 131072;
67 gt->gt_stall_secs = 600;
68 gt->gt_complain_secs = 10;
69 gt->gt_reclaim_limit = 5000;
70 gt->gt_entries_per_readdir = 32;
71 gt->gt_prefetch_secs = 10;
72 gt->gt_greedy_default = HZ / 10;
73 gt->gt_greedy_quantum = HZ / 40;
74 gt->gt_greedy_max = HZ / 4;
75 gt->gt_statfs_quantum = 30;
76 gt->gt_statfs_slow = 0;
77}
78
79/**
80 * gfs2_check_sb - Check superblock
81 * @sdp: the filesystem
82 * @sb: The superblock
83 * @silent: Don't print a message if the check fails
84 *
85 * Checks the version code of the FS is one that we understand how to
86 * read and that the sizes of the various on-disk structures have not
87 * changed.
88 */
89
90int gfs2_check_sb(struct gfs2_sbd *sdp, struct gfs2_sb *sb, int silent)
91{
92 unsigned int x;
93
94 if (sb->sb_header.mh_magic != GFS2_MAGIC ||
95 sb->sb_header.mh_type != GFS2_METATYPE_SB) {
96 if (!silent)
Steven Whitehoused92a8d42006-02-27 10:57:14 -050097 printk(KERN_WARNING "GFS2: not a GFS2 filesystem\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +000098 return -EINVAL;
99 }
100
101 /* If format numbers match exactly, we're done. */
102
103 if (sb->sb_fs_format == GFS2_FORMAT_FS &&
104 sb->sb_multihost_format == GFS2_FORMAT_MULTI)
105 return 0;
106
107 if (sb->sb_fs_format != GFS2_FORMAT_FS) {
108 for (x = 0; gfs2_old_fs_formats[x]; x++)
109 if (gfs2_old_fs_formats[x] == sb->sb_fs_format)
110 break;
111
112 if (!gfs2_old_fs_formats[x]) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500113 printk(KERN_WARNING
114 "GFS2: code version (%u, %u) is incompatible "
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 "with ondisk format (%u, %u)\n",
116 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
117 sb->sb_fs_format, sb->sb_multihost_format);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500118 printk(KERN_WARNING
119 "GFS2: I don't know how to upgrade this FS\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120 return -EINVAL;
121 }
122 }
123
124 if (sb->sb_multihost_format != GFS2_FORMAT_MULTI) {
125 for (x = 0; gfs2_old_multihost_formats[x]; x++)
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500126 if (gfs2_old_multihost_formats[x] ==
127 sb->sb_multihost_format)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000128 break;
129
130 if (!gfs2_old_multihost_formats[x]) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500131 printk(KERN_WARNING
132 "GFS2: code version (%u, %u) is incompatible "
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 "with ondisk format (%u, %u)\n",
134 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
135 sb->sb_fs_format, sb->sb_multihost_format);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500136 printk(KERN_WARNING
137 "GFS2: I don't know how to upgrade this FS\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000138 return -EINVAL;
139 }
140 }
141
142 if (!sdp->sd_args.ar_upgrade) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500143 printk(KERN_WARNING
144 "GFS2: code version (%u, %u) is incompatible "
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 "with ondisk format (%u, %u)\n",
146 GFS2_FORMAT_FS, GFS2_FORMAT_MULTI,
147 sb->sb_fs_format, sb->sb_multihost_format);
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500148 printk(KERN_INFO
149 "GFS2: Use the \"upgrade\" mount option to upgrade "
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 "the FS\n");
Steven Whitehoused92a8d42006-02-27 10:57:14 -0500151 printk(KERN_INFO "GFS2: See the manual for more details\n");
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 return -EINVAL;
153 }
154
155 return 0;
156}
157
158/**
159 * gfs2_read_sb - Read super block
160 * @sdp: The GFS2 superblock
161 * @gl: the glock for the superblock (assumed to be held)
162 * @silent: Don't print message if mount fails
163 *
164 */
165
166int gfs2_read_sb(struct gfs2_sbd *sdp, struct gfs2_glock *gl, int silent)
167{
168 struct buffer_head *bh;
169 uint32_t hash_blocks, ind_blocks, leaf_blocks;
170 uint32_t tmp_blocks;
171 unsigned int x;
172 int error;
173
174 error = gfs2_meta_read(gl, GFS2_SB_ADDR >> sdp->sd_fsb2bb_shift,
175 DIO_FORCE | DIO_START | DIO_WAIT, &bh);
176 if (error) {
177 if (!silent)
178 fs_err(sdp, "can't read superblock\n");
179 return error;
180 }
181
182 gfs2_assert(sdp, sizeof(struct gfs2_sb) <= bh->b_size);
183 gfs2_sb_in(&sdp->sd_sb, bh->b_data);
184 brelse(bh);
185
186 error = gfs2_check_sb(sdp, &sdp->sd_sb, silent);
187 if (error)
188 return error;
189
190 sdp->sd_fsb2bb_shift = sdp->sd_sb.sb_bsize_shift -
191 GFS2_BASIC_BLOCK_SHIFT;
192 sdp->sd_fsb2bb = 1 << sdp->sd_fsb2bb_shift;
193 sdp->sd_diptrs = (sdp->sd_sb.sb_bsize -
194 sizeof(struct gfs2_dinode)) / sizeof(uint64_t);
195 sdp->sd_inptrs = (sdp->sd_sb.sb_bsize -
196 sizeof(struct gfs2_meta_header)) / sizeof(uint64_t);
197 sdp->sd_jbsize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header);
198 sdp->sd_hash_bsize = sdp->sd_sb.sb_bsize / 2;
199 sdp->sd_hash_bsize_shift = sdp->sd_sb.sb_bsize_shift - 1;
200 sdp->sd_hash_ptrs = sdp->sd_hash_bsize / sizeof(uint64_t);
201 sdp->sd_ut_per_block = (sdp->sd_sb.sb_bsize -
202 sizeof(struct gfs2_meta_header)) /
203 sizeof(struct gfs2_unlinked_tag);
204 sdp->sd_qc_per_block = (sdp->sd_sb.sb_bsize -
205 sizeof(struct gfs2_meta_header)) /
206 sizeof(struct gfs2_quota_change);
207
208 /* Compute maximum reservation required to add a entry to a directory */
209
210 hash_blocks = DIV_RU(sizeof(uint64_t) * (1 << GFS2_DIR_MAX_DEPTH),
211 sdp->sd_jbsize);
212
213 ind_blocks = 0;
214 for (tmp_blocks = hash_blocks; tmp_blocks > sdp->sd_diptrs;) {
215 tmp_blocks = DIV_RU(tmp_blocks, sdp->sd_inptrs);
216 ind_blocks += tmp_blocks;
217 }
218
219 leaf_blocks = 2 + GFS2_DIR_MAX_DEPTH;
220
221 sdp->sd_max_dirres = hash_blocks + ind_blocks + leaf_blocks;
222
223 sdp->sd_heightsize[0] = sdp->sd_sb.sb_bsize -
224 sizeof(struct gfs2_dinode);
225 sdp->sd_heightsize[1] = sdp->sd_sb.sb_bsize * sdp->sd_diptrs;
226 for (x = 2;; x++) {
227 uint64_t space, d;
228 uint32_t m;
229
230 space = sdp->sd_heightsize[x - 1] * sdp->sd_inptrs;
231 d = space;
232 m = do_div(d, sdp->sd_inptrs);
233
234 if (d != sdp->sd_heightsize[x - 1] || m)
235 break;
236 sdp->sd_heightsize[x] = space;
237 }
238 sdp->sd_max_height = x;
239 gfs2_assert(sdp, sdp->sd_max_height <= GFS2_MAX_META_HEIGHT);
240
241 sdp->sd_jheightsize[0] = sdp->sd_sb.sb_bsize -
242 sizeof(struct gfs2_dinode);
243 sdp->sd_jheightsize[1] = sdp->sd_jbsize * sdp->sd_diptrs;
244 for (x = 2;; x++) {
245 uint64_t space, d;
246 uint32_t m;
247
248 space = sdp->sd_jheightsize[x - 1] * sdp->sd_inptrs;
249 d = space;
250 m = do_div(d, sdp->sd_inptrs);
251
252 if (d != sdp->sd_jheightsize[x - 1] || m)
253 break;
254 sdp->sd_jheightsize[x] = space;
255 }
256 sdp->sd_max_jheight = x;
257 gfs2_assert(sdp, sdp->sd_max_jheight <= GFS2_MAX_META_HEIGHT);
258
259 return 0;
260}
261
262int gfs2_do_upgrade(struct gfs2_sbd *sdp, struct gfs2_glock *sb_gl)
263{
264 return 0;
265}
266
267/**
268 * gfs2_jindex_hold - Grab a lock on the jindex
269 * @sdp: The GFS2 superblock
270 * @ji_gh: the holder for the jindex glock
271 *
272 * This is very similar to the gfs2_rindex_hold() function, except that
273 * in general we hold the jindex lock for longer periods of time and
274 * we grab it far less frequently (in general) then the rgrp lock.
275 *
276 * Returns: errno
277 */
278
279int gfs2_jindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ji_gh)
280{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000281 struct gfs2_inode *dip = get_v2ip(sdp->sd_jindex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 struct qstr name;
283 char buf[20];
284 struct gfs2_jdesc *jd;
285 int error;
286
287 name.name = buf;
288
Steven Whitehousef55ab262006-02-21 12:51:39 +0000289 mutex_lock(&sdp->sd_jindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290
291 for (;;) {
292 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED,
293 GL_LOCAL_EXCL, ji_gh);
294 if (error)
295 break;
296
297 name.len = sprintf(buf, "journal%u", sdp->sd_journals);
298
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500299 error = gfs2_dir_search(get_v2ip(sdp->sd_jindex),
300 &name, NULL, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301 if (error == -ENOENT) {
302 error = 0;
303 break;
304 }
305
306 gfs2_glock_dq_uninit(ji_gh);
307
308 if (error)
309 break;
310
311 error = -ENOMEM;
312 jd = kzalloc(sizeof(struct gfs2_jdesc), GFP_KERNEL);
313 if (!jd)
314 break;
315
Steven Whitehouse7359a192006-02-13 12:27:43 +0000316 error = gfs2_lookupi(sdp->sd_jindex, &name, 1, &jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317 if (error) {
318 kfree(jd);
319 break;
320 }
321
322 spin_lock(&sdp->sd_jindex_spin);
323 jd->jd_jid = sdp->sd_journals++;
324 list_add_tail(&jd->jd_list, &sdp->sd_jindex_list);
325 spin_unlock(&sdp->sd_jindex_spin);
326 }
327
Steven Whitehousef55ab262006-02-21 12:51:39 +0000328 mutex_unlock(&sdp->sd_jindex_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000329
330 return error;
331}
332
333/**
334 * gfs2_jindex_free - Clear all the journal index information
335 * @sdp: The GFS2 superblock
336 *
337 */
338
339void gfs2_jindex_free(struct gfs2_sbd *sdp)
340{
341 struct list_head list;
342 struct gfs2_jdesc *jd;
343
344 spin_lock(&sdp->sd_jindex_spin);
345 list_add(&list, &sdp->sd_jindex_list);
346 list_del_init(&sdp->sd_jindex_list);
347 sdp->sd_journals = 0;
348 spin_unlock(&sdp->sd_jindex_spin);
349
350 while (!list_empty(&list)) {
351 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
352 list_del(&jd->jd_list);
Steven Whitehouse7359a192006-02-13 12:27:43 +0000353 iput(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354 kfree(jd);
355 }
356}
357
358static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
359{
360 struct gfs2_jdesc *jd;
361 int found = 0;
362
363 list_for_each_entry(jd, head, jd_list) {
364 if (jd->jd_jid == jid) {
365 found = 1;
366 break;
367 }
368 }
369
370 if (!found)
371 jd = NULL;
372
373 return jd;
374}
375
376struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
377{
378 struct gfs2_jdesc *jd;
379
380 spin_lock(&sdp->sd_jindex_spin);
381 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
382 spin_unlock(&sdp->sd_jindex_spin);
383
384 return jd;
385}
386
387void gfs2_jdesc_make_dirty(struct gfs2_sbd *sdp, unsigned int jid)
388{
389 struct gfs2_jdesc *jd;
390
391 spin_lock(&sdp->sd_jindex_spin);
392 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
393 if (jd)
394 jd->jd_dirty = 1;
395 spin_unlock(&sdp->sd_jindex_spin);
396}
397
398struct gfs2_jdesc *gfs2_jdesc_find_dirty(struct gfs2_sbd *sdp)
399{
400 struct gfs2_jdesc *jd;
401 int found = 0;
402
403 spin_lock(&sdp->sd_jindex_spin);
404
405 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
406 if (jd->jd_dirty) {
407 jd->jd_dirty = 0;
408 found = 1;
409 break;
410 }
411 }
412 spin_unlock(&sdp->sd_jindex_spin);
413
414 if (!found)
415 jd = NULL;
416
417 return jd;
418}
419
420int gfs2_jdesc_check(struct gfs2_jdesc *jd)
421{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000422 struct gfs2_inode *ip = get_v2ip(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 struct gfs2_sbd *sdp = ip->i_sbd;
424 int ar;
425 int error;
426
427 if (ip->i_di.di_size < (8 << 20) ||
428 ip->i_di.di_size > (1 << 30) ||
429 (ip->i_di.di_size & (sdp->sd_sb.sb_bsize - 1))) {
430 gfs2_consist_inode(ip);
431 return -EIO;
432 }
433 jd->jd_blocks = ip->i_di.di_size >> sdp->sd_sb.sb_bsize_shift;
434
435 error = gfs2_write_alloc_required(ip,
436 0, ip->i_di.di_size,
437 &ar);
438 if (!error && ar) {
439 gfs2_consist_inode(ip);
440 error = -EIO;
441 }
442
443 return error;
444}
445
446int gfs2_lookup_master_dir(struct gfs2_sbd *sdp)
447{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000448 struct inode *inode = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449 struct gfs2_glock *gl;
450 int error;
451
452 error = gfs2_glock_get(sdp,
453 sdp->sd_sb.sb_master_dir.no_addr,
454 &gfs2_inode_glops, CREATE, &gl);
455 if (!error) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500456 error = gfs2_lookup_simple(sdp->sd_root_dir, ".gfs2_admin",
457 &inode);
Steven Whitehousef42faf42006-01-30 18:34:10 +0000458 sdp->sd_master_dir = inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 gfs2_glock_put(gl);
460 }
461
462 return error;
463}
464
465/**
466 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
467 * @sdp: the filesystem
468 *
469 * Returns: errno
470 */
471
472int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
473{
Steven Whitehouse7359a192006-02-13 12:27:43 +0000474 struct gfs2_glock *j_gl = get_v2ip(sdp->sd_jdesc->jd_inode)->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 struct gfs2_holder t_gh;
476 struct gfs2_log_header head;
477 int error;
478
479 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
480 GL_LOCAL_EXCL | GL_NEVER_RECURSE, &t_gh);
481 if (error)
482 return error;
483
Steven Whitehouse7359a192006-02-13 12:27:43 +0000484 gfs2_meta_cache_flush(get_v2ip(sdp->sd_jdesc->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA | DIO_DATA);
486
487 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
488 if (error)
489 goto fail;
490
491 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
492 gfs2_consist(sdp);
493 error = -EIO;
494 goto fail;
495 }
496
497 /* Initialize some head of the log stuff */
498 sdp->sd_log_sequence = head.lh_sequence + 1;
499 gfs2_log_pointers_init(sdp, head.lh_blkno);
500
501 error = gfs2_unlinked_init(sdp);
502 if (error)
503 goto fail;
504 error = gfs2_quota_init(sdp);
505 if (error)
506 goto fail_unlinked;
507
508 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
509
510 gfs2_glock_dq_uninit(&t_gh);
511
512 return 0;
513
514 fail_unlinked:
515 gfs2_unlinked_cleanup(sdp);
516
517 fail:
518 t_gh.gh_flags |= GL_NOCACHE;
519 gfs2_glock_dq_uninit(&t_gh);
520
521 return error;
522}
523
524/**
525 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
526 * @sdp: the filesystem
527 *
528 * Returns: errno
529 */
530
531int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
532{
533 struct gfs2_holder t_gh;
534 int error;
535
536 gfs2_unlinked_dealloc(sdp);
537 gfs2_quota_sync(sdp);
538 gfs2_statfs_sync(sdp);
539
540 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
541 GL_LOCAL_EXCL | GL_NEVER_RECURSE | GL_NOCACHE,
542 &t_gh);
543 if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
544 return error;
545
546 gfs2_meta_syncfs(sdp);
547 gfs2_log_shutdown(sdp);
548
549 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
550
551 if (t_gh.gh_gl)
552 gfs2_glock_dq_uninit(&t_gh);
553
554 gfs2_unlinked_cleanup(sdp);
555 gfs2_quota_cleanup(sdp);
556
557 return error;
558}
559
560int gfs2_statfs_init(struct gfs2_sbd *sdp)
561{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000562 struct gfs2_inode *m_ip = get_v2ip(sdp->sd_statfs_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
Steven Whitehousef42faf42006-01-30 18:34:10 +0000564 struct gfs2_inode *l_ip = get_v2ip(sdp->sd_sc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
566 struct buffer_head *m_bh, *l_bh;
567 struct gfs2_holder gh;
568 int error;
569
570 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
571 &gh);
572 if (error)
573 return error;
574
575 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
576 if (error)
577 goto out;
578
579 if (sdp->sd_args.ar_spectator) {
580 spin_lock(&sdp->sd_statfs_spin);
581 gfs2_statfs_change_in(m_sc, m_bh->b_data +
582 sizeof(struct gfs2_dinode));
583 spin_unlock(&sdp->sd_statfs_spin);
584 } else {
585 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
586 if (error)
587 goto out_m_bh;
588
589 spin_lock(&sdp->sd_statfs_spin);
590 gfs2_statfs_change_in(m_sc, m_bh->b_data +
591 sizeof(struct gfs2_dinode));
592 gfs2_statfs_change_in(l_sc, l_bh->b_data +
593 sizeof(struct gfs2_dinode));
594 spin_unlock(&sdp->sd_statfs_spin);
595
596 brelse(l_bh);
597 }
598
599 out_m_bh:
600 brelse(m_bh);
601
602 out:
603 gfs2_glock_dq_uninit(&gh);
604
605 return 0;
606}
607
608void gfs2_statfs_change(struct gfs2_sbd *sdp, int64_t total, int64_t free,
609 int64_t dinodes)
610{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000611 struct gfs2_inode *l_ip = get_v2ip(sdp->sd_sc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000612 struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
613 struct buffer_head *l_bh;
614 int error;
615
616 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
617 if (error)
618 return;
619
Steven Whitehousef55ab262006-02-21 12:51:39 +0000620 mutex_lock(&sdp->sd_statfs_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000621 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000622 mutex_unlock(&sdp->sd_statfs_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000623
624 spin_lock(&sdp->sd_statfs_spin);
625 l_sc->sc_total += total;
626 l_sc->sc_free += free;
627 l_sc->sc_dinodes += dinodes;
628 gfs2_statfs_change_out(l_sc, l_bh->b_data +
629 sizeof(struct gfs2_dinode));
630 spin_unlock(&sdp->sd_statfs_spin);
631
632 brelse(l_bh);
633}
634
635int gfs2_statfs_sync(struct gfs2_sbd *sdp)
636{
Steven Whitehousef42faf42006-01-30 18:34:10 +0000637 struct gfs2_inode *m_ip = get_v2ip(sdp->sd_statfs_inode);
638 struct gfs2_inode *l_ip = get_v2ip(sdp->sd_sc_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000639 struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
640 struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
641 struct gfs2_holder gh;
642 struct buffer_head *m_bh, *l_bh;
643 int error;
644
645 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
646 &gh);
647 if (error)
648 return error;
649
650 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
651 if (error)
652 goto out;
653
654 spin_lock(&sdp->sd_statfs_spin);
655 gfs2_statfs_change_in(m_sc, m_bh->b_data +
656 sizeof(struct gfs2_dinode));
657 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
658 spin_unlock(&sdp->sd_statfs_spin);
659 goto out_bh;
660 }
661 spin_unlock(&sdp->sd_statfs_spin);
662
663 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
664 if (error)
665 goto out_bh;
666
667 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
668 if (error)
669 goto out_bh2;
670
Steven Whitehousef55ab262006-02-21 12:51:39 +0000671 mutex_lock(&sdp->sd_statfs_mutex);
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000672 gfs2_trans_add_bh(l_ip->i_gl, l_bh, 1);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000673 mutex_unlock(&sdp->sd_statfs_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000674
675 spin_lock(&sdp->sd_statfs_spin);
676 m_sc->sc_total += l_sc->sc_total;
677 m_sc->sc_free += l_sc->sc_free;
678 m_sc->sc_dinodes += l_sc->sc_dinodes;
679 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
680 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
681 0, sizeof(struct gfs2_statfs_change));
682 spin_unlock(&sdp->sd_statfs_spin);
683
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000684 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
686
687 gfs2_trans_end(sdp);
688
689 out_bh2:
690 brelse(l_bh);
691
692 out_bh:
693 brelse(m_bh);
694
695 out:
696 gfs2_glock_dq_uninit(&gh);
697
698 return error;
699}
700
701/**
702 * gfs2_statfs_i - Do a statfs
703 * @sdp: the filesystem
704 * @sg: the sg structure
705 *
706 * Returns: errno
707 */
708
709int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
710{
711 struct gfs2_statfs_change *m_sc = &sdp->sd_statfs_master;
712 struct gfs2_statfs_change *l_sc = &sdp->sd_statfs_local;
713
714 spin_lock(&sdp->sd_statfs_spin);
715
716 *sc = *m_sc;
717 sc->sc_total += l_sc->sc_total;
718 sc->sc_free += l_sc->sc_free;
719 sc->sc_dinodes += l_sc->sc_dinodes;
720
721 spin_unlock(&sdp->sd_statfs_spin);
722
723 if (sc->sc_free < 0)
724 sc->sc_free = 0;
725 if (sc->sc_free > sc->sc_total)
726 sc->sc_free = sc->sc_total;
727 if (sc->sc_dinodes < 0)
728 sc->sc_dinodes = 0;
729
730 return 0;
731}
732
733/**
734 * statfs_fill - fill in the sg for a given RG
735 * @rgd: the RG
736 * @sc: the sc structure
737 *
738 * Returns: 0 on success, -ESTALE if the LVB is invalid
739 */
740
741static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
742 struct gfs2_statfs_change *sc)
743{
744 gfs2_rgrp_verify(rgd);
745 sc->sc_total += rgd->rd_ri.ri_data;
746 sc->sc_free += rgd->rd_rg.rg_free;
747 sc->sc_dinodes += rgd->rd_rg.rg_dinodes;
748 return 0;
749}
750
751/**
752 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
753 * @sdp: the filesystem
754 * @sc: the sc info that will be returned
755 *
756 * Any error (other than a signal) will cause this routine to fall back
757 * to the synchronous version.
758 *
759 * FIXME: This really shouldn't busy wait like this.
760 *
761 * Returns: errno
762 */
763
764int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change *sc)
765{
766 struct gfs2_holder ri_gh;
767 struct gfs2_rgrpd *rgd_next;
768 struct gfs2_holder *gha, *gh;
769 unsigned int slots = 64;
770 unsigned int x;
771 int done;
772 int error = 0, err;
773
774 memset(sc, 0, sizeof(struct gfs2_statfs_change));
775 gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
776 if (!gha)
777 return -ENOMEM;
778
779 error = gfs2_rindex_hold(sdp, &ri_gh);
780 if (error)
781 goto out;
782
783 rgd_next = gfs2_rgrpd_get_first(sdp);
784
785 for (;;) {
786 done = 1;
787
788 for (x = 0; x < slots; x++) {
789 gh = gha + x;
790
791 if (gh->gh_gl && gfs2_glock_poll(gh)) {
792 err = gfs2_glock_wait(gh);
793 if (err) {
794 gfs2_holder_uninit(gh);
795 error = err;
796 } else {
797 if (!error)
798 error = statfs_slow_fill(get_gl2rgd(gh->gh_gl), sc);
799 gfs2_glock_dq_uninit(gh);
800 }
801 }
802
803 if (gh->gh_gl)
804 done = 0;
805 else if (rgd_next && !error) {
806 error = gfs2_glock_nq_init(rgd_next->rd_gl,
807 LM_ST_SHARED,
808 GL_ASYNC,
809 gh);
810 rgd_next = gfs2_rgrpd_get_next(rgd_next);
811 done = 0;
812 }
813
814 if (signal_pending(current))
815 error = -ERESTARTSYS;
816 }
817
818 if (done)
819 break;
820
821 yield();
822 }
823
824 gfs2_glock_dq_uninit(&ri_gh);
825
826 out:
827 kfree(gha);
828
829 return error;
830}
831
832struct lfcc {
833 struct list_head list;
834 struct gfs2_holder gh;
835};
836
837/**
838 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
839 * journals are clean
840 * @sdp: the file system
841 * @state: the state to put the transaction lock into
842 * @t_gh: the hold on the transaction lock
843 *
844 * Returns: errno
845 */
846
847int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp, struct gfs2_holder *t_gh)
848{
849 struct gfs2_holder ji_gh;
850 struct gfs2_jdesc *jd;
851 struct lfcc *lfcc;
852 LIST_HEAD(list);
853 struct gfs2_log_header lh;
854 int error;
855
856 error = gfs2_jindex_hold(sdp, &ji_gh);
857 if (error)
858 return error;
859
860 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
861 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
862 if (!lfcc) {
863 error = -ENOMEM;
864 goto out;
865 }
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500866 error = gfs2_glock_nq_init(get_v2ip(jd->jd_inode)->i_gl,
867 LM_ST_SHARED, 0,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868 &lfcc->gh);
869 if (error) {
870 kfree(lfcc);
871 goto out;
872 }
873 list_add(&lfcc->list, &list);
874 }
875
876 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_DEFERRED,
877 LM_FLAG_PRIORITY | GL_NEVER_RECURSE | GL_NOCACHE,
878 t_gh);
879
880 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
881 error = gfs2_jdesc_check(jd);
882 if (error)
883 break;
884 error = gfs2_find_jhead(jd, &lh);
885 if (error)
886 break;
887 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
888 error = -EBUSY;
889 break;
890 }
891 }
892
893 if (error)
894 gfs2_glock_dq_uninit(t_gh);
895
896 out:
897 while (!list_empty(&list)) {
898 lfcc = list_entry(list.next, struct lfcc, list);
899 list_del(&lfcc->list);
900 gfs2_glock_dq_uninit(&lfcc->gh);
901 kfree(lfcc);
902 }
903 gfs2_glock_dq_uninit(&ji_gh);
904
905 return error;
906}
907
908/**
909 * gfs2_freeze_fs - freezes the file system
910 * @sdp: the file system
911 *
912 * This function flushes data and meta data for all machines by
913 * aquiring the transaction log exclusively. All journals are
914 * ensured to be in a clean state as well.
915 *
916 * Returns: errno
917 */
918
919int gfs2_freeze_fs(struct gfs2_sbd *sdp)
920{
921 int error = 0;
922
Steven Whitehousef55ab262006-02-21 12:51:39 +0000923 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000924
925 if (!sdp->sd_freeze_count++) {
926 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
927 if (error)
928 sdp->sd_freeze_count--;
929 }
930
Steven Whitehousef55ab262006-02-21 12:51:39 +0000931 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932
933 return error;
934}
935
936/**
937 * gfs2_unfreeze_fs - unfreezes the file system
938 * @sdp: the file system
939 *
940 * This function allows the file system to proceed by unlocking
941 * the exclusively held transaction lock. Other GFS2 nodes are
942 * now free to acquire the lock shared and go on with their lives.
943 *
944 */
945
946void gfs2_unfreeze_fs(struct gfs2_sbd *sdp)
947{
Steven Whitehousef55ab262006-02-21 12:51:39 +0000948 mutex_lock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000949
950 if (sdp->sd_freeze_count && !--sdp->sd_freeze_count)
951 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
952
Steven Whitehousef55ab262006-02-21 12:51:39 +0000953 mutex_unlock(&sdp->sd_freeze_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000954}
955