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