Merge tag 'sound-4.2' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[linux-2.6-block.git] / fs / gfs2 / super.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
da6dd40d 3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
b3b94faa
DT
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
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
d77d1b58
JP
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
9e6e0a12 12#include <linux/bio.h>
b3b94faa
DT
13#include <linux/sched.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/completion.h>
17#include <linux/buffer_head.h>
9e6e0a12
SW
18#include <linux/statfs.h>
19#include <linux/seq_file.h>
20#include <linux/mount.h>
21#include <linux/kthread.h>
22#include <linux/delay.h>
5c676f6d 23#include <linux/gfs2_ondisk.h>
9e6e0a12
SW
24#include <linux/crc32.h>
25#include <linux/time.h>
e402746a 26#include <linux/wait.h>
a9185b41 27#include <linux/writeback.h>
4667a0ec 28#include <linux/backing-dev.h>
2e60d768 29#include <linux/kernel.h>
b3b94faa
DT
30
31#include "gfs2.h"
5c676f6d 32#include "incore.h"
b3b94faa
DT
33#include "bmap.h"
34#include "dir.h"
b3b94faa
DT
35#include "glock.h"
36#include "glops.h"
37#include "inode.h"
38#include "log.h"
39#include "meta_io.h"
40#include "quota.h"
41#include "recovery.h"
42#include "rgrp.h"
43#include "super.h"
44#include "trans.h"
5c676f6d 45#include "util.h"
9e6e0a12 46#include "sys.h"
307cf6e6 47#include "xattr.h"
9e6e0a12
SW
48
49#define args_neq(a1, a2, x) ((a1)->ar_##x != (a2)->ar_##x)
50
51enum {
52 Opt_lockproto,
53 Opt_locktable,
54 Opt_hostdata,
55 Opt_spectator,
56 Opt_ignore_local_fs,
57 Opt_localflocks,
58 Opt_localcaching,
59 Opt_debug,
60 Opt_nodebug,
61 Opt_upgrade,
62 Opt_acl,
63 Opt_noacl,
64 Opt_quota_off,
65 Opt_quota_account,
66 Opt_quota_on,
67 Opt_quota,
68 Opt_noquota,
69 Opt_suiddir,
70 Opt_nosuiddir,
71 Opt_data_writeback,
72 Opt_data_ordered,
73 Opt_meta,
74 Opt_discard,
75 Opt_nodiscard,
76 Opt_commit,
d34843d0
BP
77 Opt_err_withdraw,
78 Opt_err_panic,
3d3c10f2
BM
79 Opt_statfs_quantum,
80 Opt_statfs_percent,
81 Opt_quota_quantum,
f25934c5
CH
82 Opt_barrier,
83 Opt_nobarrier,
90306c41
BM
84 Opt_rgrplvb,
85 Opt_norgrplvb,
9e6e0a12
SW
86 Opt_error,
87};
88
89static const match_table_t tokens = {
90 {Opt_lockproto, "lockproto=%s"},
91 {Opt_locktable, "locktable=%s"},
92 {Opt_hostdata, "hostdata=%s"},
93 {Opt_spectator, "spectator"},
43f74c19 94 {Opt_spectator, "norecovery"},
9e6e0a12
SW
95 {Opt_ignore_local_fs, "ignore_local_fs"},
96 {Opt_localflocks, "localflocks"},
97 {Opt_localcaching, "localcaching"},
98 {Opt_debug, "debug"},
99 {Opt_nodebug, "nodebug"},
100 {Opt_upgrade, "upgrade"},
101 {Opt_acl, "acl"},
102 {Opt_noacl, "noacl"},
103 {Opt_quota_off, "quota=off"},
104 {Opt_quota_account, "quota=account"},
105 {Opt_quota_on, "quota=on"},
106 {Opt_quota, "quota"},
107 {Opt_noquota, "noquota"},
108 {Opt_suiddir, "suiddir"},
109 {Opt_nosuiddir, "nosuiddir"},
110 {Opt_data_writeback, "data=writeback"},
111 {Opt_data_ordered, "data=ordered"},
112 {Opt_meta, "meta"},
113 {Opt_discard, "discard"},
114 {Opt_nodiscard, "nodiscard"},
115 {Opt_commit, "commit=%d"},
d34843d0
BP
116 {Opt_err_withdraw, "errors=withdraw"},
117 {Opt_err_panic, "errors=panic"},
3d3c10f2
BM
118 {Opt_statfs_quantum, "statfs_quantum=%d"},
119 {Opt_statfs_percent, "statfs_percent=%d"},
120 {Opt_quota_quantum, "quota_quantum=%d"},
f25934c5
CH
121 {Opt_barrier, "barrier"},
122 {Opt_nobarrier, "nobarrier"},
90306c41
BM
123 {Opt_rgrplvb, "rgrplvb"},
124 {Opt_norgrplvb, "norgrplvb"},
9e6e0a12
SW
125 {Opt_error, NULL}
126};
127
128/**
129 * gfs2_mount_args - Parse mount options
f55073ff
SW
130 * @args: The structure into which the parsed options will be written
131 * @options: The options to parse
9e6e0a12
SW
132 *
133 * Return: errno
134 */
135
f55073ff 136int gfs2_mount_args(struct gfs2_args *args, char *options)
9e6e0a12
SW
137{
138 char *o;
139 int token;
140 substring_t tmp[MAX_OPT_ARGS];
141 int rv;
142
143 /* Split the options into tokens with the "," character and
144 process them */
145
146 while (1) {
147 o = strsep(&options, ",");
148 if (o == NULL)
149 break;
150 if (*o == '\0')
151 continue;
152
153 token = match_token(o, tokens, tmp);
154 switch (token) {
155 case Opt_lockproto:
156 match_strlcpy(args->ar_lockproto, &tmp[0],
157 GFS2_LOCKNAME_LEN);
158 break;
159 case Opt_locktable:
160 match_strlcpy(args->ar_locktable, &tmp[0],
161 GFS2_LOCKNAME_LEN);
162 break;
163 case Opt_hostdata:
164 match_strlcpy(args->ar_hostdata, &tmp[0],
165 GFS2_LOCKNAME_LEN);
166 break;
167 case Opt_spectator:
168 args->ar_spectator = 1;
169 break;
170 case Opt_ignore_local_fs:
f57a024e 171 /* Retained for backwards compat only */
9e6e0a12
SW
172 break;
173 case Opt_localflocks:
174 args->ar_localflocks = 1;
175 break;
176 case Opt_localcaching:
c2048b00 177 /* Retained for backwards compat only */
9e6e0a12
SW
178 break;
179 case Opt_debug:
d34843d0 180 if (args->ar_errors == GFS2_ERRORS_PANIC) {
d77d1b58 181 pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
d34843d0
BP
182 return -EINVAL;
183 }
9e6e0a12
SW
184 args->ar_debug = 1;
185 break;
186 case Opt_nodebug:
187 args->ar_debug = 0;
188 break;
189 case Opt_upgrade:
c80dbb58 190 /* Retained for backwards compat only */
9e6e0a12
SW
191 break;
192 case Opt_acl:
193 args->ar_posix_acl = 1;
194 break;
195 case Opt_noacl:
196 args->ar_posix_acl = 0;
197 break;
198 case Opt_quota_off:
199 case Opt_noquota:
200 args->ar_quota = GFS2_QUOTA_OFF;
201 break;
202 case Opt_quota_account:
203 args->ar_quota = GFS2_QUOTA_ACCOUNT;
204 break;
205 case Opt_quota_on:
206 case Opt_quota:
207 args->ar_quota = GFS2_QUOTA_ON;
208 break;
209 case Opt_suiddir:
210 args->ar_suiddir = 1;
211 break;
212 case Opt_nosuiddir:
213 args->ar_suiddir = 0;
214 break;
215 case Opt_data_writeback:
216 args->ar_data = GFS2_DATA_WRITEBACK;
217 break;
218 case Opt_data_ordered:
219 args->ar_data = GFS2_DATA_ORDERED;
220 break;
221 case Opt_meta:
222 args->ar_meta = 1;
223 break;
224 case Opt_discard:
225 args->ar_discard = 1;
226 break;
227 case Opt_nodiscard:
228 args->ar_discard = 0;
229 break;
230 case Opt_commit:
231 rv = match_int(&tmp[0], &args->ar_commit);
232 if (rv || args->ar_commit <= 0) {
d77d1b58 233 pr_warn("commit mount option requires a positive numeric argument\n");
9e6e0a12
SW
234 return rv ? rv : -EINVAL;
235 }
236 break;
3d3c10f2
BM
237 case Opt_statfs_quantum:
238 rv = match_int(&tmp[0], &args->ar_statfs_quantum);
239 if (rv || args->ar_statfs_quantum < 0) {
d77d1b58 240 pr_warn("statfs_quantum mount option requires a non-negative numeric argument\n");
3d3c10f2
BM
241 return rv ? rv : -EINVAL;
242 }
243 break;
244 case Opt_quota_quantum:
245 rv = match_int(&tmp[0], &args->ar_quota_quantum);
246 if (rv || args->ar_quota_quantum <= 0) {
d77d1b58 247 pr_warn("quota_quantum mount option requires a positive numeric argument\n");
3d3c10f2
BM
248 return rv ? rv : -EINVAL;
249 }
250 break;
251 case Opt_statfs_percent:
252 rv = match_int(&tmp[0], &args->ar_statfs_percent);
253 if (rv || args->ar_statfs_percent < 0 ||
254 args->ar_statfs_percent > 100) {
fc554ed3 255 pr_warn("statfs_percent mount option requires a numeric argument between 0 and 100\n");
3d3c10f2
BM
256 return rv ? rv : -EINVAL;
257 }
258 break;
d34843d0
BP
259 case Opt_err_withdraw:
260 args->ar_errors = GFS2_ERRORS_WITHDRAW;
261 break;
262 case Opt_err_panic:
263 if (args->ar_debug) {
d77d1b58 264 pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
d34843d0
BP
265 return -EINVAL;
266 }
267 args->ar_errors = GFS2_ERRORS_PANIC;
268 break;
f25934c5
CH
269 case Opt_barrier:
270 args->ar_nobarrier = 0;
271 break;
272 case Opt_nobarrier:
273 args->ar_nobarrier = 1;
274 break;
90306c41
BM
275 case Opt_rgrplvb:
276 args->ar_rgrplvb = 1;
277 break;
278 case Opt_norgrplvb:
279 args->ar_rgrplvb = 0;
280 break;
9e6e0a12
SW
281 case Opt_error:
282 default:
d77d1b58 283 pr_warn("invalid mount option: %s\n", o);
9e6e0a12
SW
284 return -EINVAL;
285 }
286 }
287
288 return 0;
289}
b3b94faa 290
fefc03bf
SW
291/**
292 * gfs2_jindex_free - Clear all the journal index information
293 * @sdp: The GFS2 superblock
294 *
295 */
296
297void gfs2_jindex_free(struct gfs2_sbd *sdp)
298{
b50f227b 299 struct list_head list;
fefc03bf 300 struct gfs2_jdesc *jd;
fefc03bf
SW
301
302 spin_lock(&sdp->sd_jindex_spin);
303 list_add(&list, &sdp->sd_jindex_list);
304 list_del_init(&sdp->sd_jindex_list);
305 sdp->sd_journals = 0;
306 spin_unlock(&sdp->sd_jindex_spin);
307
308 while (!list_empty(&list)) {
309 jd = list_entry(list.next, struct gfs2_jdesc, jd_list);
b50f227b 310 gfs2_free_journal_extents(jd);
fefc03bf
SW
311 list_del(&jd->jd_list);
312 iput(jd->jd_inode);
313 kfree(jd);
314 }
315}
316
b3b94faa
DT
317static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
318{
319 struct gfs2_jdesc *jd;
320 int found = 0;
321
322 list_for_each_entry(jd, head, jd_list) {
323 if (jd->jd_jid == jid) {
324 found = 1;
325 break;
326 }
327 }
328
329 if (!found)
330 jd = NULL;
331
332 return jd;
333}
334
335struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
336{
337 struct gfs2_jdesc *jd;
338
339 spin_lock(&sdp->sd_jindex_spin);
340 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
341 spin_unlock(&sdp->sd_jindex_spin);
342
343 return jd;
344}
345
b3b94faa
DT
346int gfs2_jdesc_check(struct gfs2_jdesc *jd)
347{
feaa7bba
SW
348 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
349 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
a2e0f799 350 u64 size = i_size_read(jd->jd_inode);
b3b94faa 351
a2e0f799 352 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, 1 << 30))
b3b94faa 353 return -EIO;
b3b94faa 354
a2e0f799
SW
355 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
356
357 if (gfs2_write_alloc_required(ip, 0, size)) {
b3b94faa 358 gfs2_consist_inode(ip);
461cb419 359 return -EIO;
b3b94faa
DT
360 }
361
461cb419 362 return 0;
b3b94faa
DT
363}
364
8ad151c2
SW
365static int init_threads(struct gfs2_sbd *sdp)
366{
367 struct task_struct *p;
368 int error = 0;
369
370 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
371 if (IS_ERR(p)) {
372 error = PTR_ERR(p);
373 fs_err(sdp, "can't start logd thread: %d\n", error);
374 return error;
375 }
376 sdp->sd_logd_process = p;
377
378 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
379 if (IS_ERR(p)) {
380 error = PTR_ERR(p);
381 fs_err(sdp, "can't start quotad thread: %d\n", error);
382 goto fail;
383 }
384 sdp->sd_quotad_process = p;
385 return 0;
386
387fail:
388 kthread_stop(sdp->sd_logd_process);
389 return error;
390}
391
b3b94faa
DT
392/**
393 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
394 * @sdp: the filesystem
395 *
396 * Returns: errno
397 */
398
399int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
400{
feaa7bba 401 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
5c676f6d 402 struct gfs2_glock *j_gl = ip->i_gl;
2e60d768 403 struct gfs2_holder freeze_gh;
55167622 404 struct gfs2_log_header_host head;
b3b94faa
DT
405 int error;
406
8ad151c2 407 error = init_threads(sdp);
b3b94faa
DT
408 if (error)
409 return error;
410
24972557 411 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
2e60d768 412 &freeze_gh);
8ad151c2
SW
413 if (error)
414 goto fail_threads;
415
1a14d3a6 416 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
b3b94faa
DT
417
418 error = gfs2_find_jhead(sdp->sd_jdesc, &head);
419 if (error)
420 goto fail;
421
422 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
423 gfs2_consist(sdp);
424 error = -EIO;
425 goto fail;
426 }
427
428 /* Initialize some head of the log stuff */
429 sdp->sd_log_sequence = head.lh_sequence + 1;
430 gfs2_log_pointers_init(sdp, head.lh_blkno);
431
b3b94faa
DT
432 error = gfs2_quota_init(sdp);
433 if (error)
a91ea69f 434 goto fail;
b3b94faa
DT
435
436 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
437
2e60d768 438 gfs2_glock_dq_uninit(&freeze_gh);
b3b94faa
DT
439
440 return 0;
441
a91ea69f 442fail:
2e60d768
BM
443 freeze_gh.gh_flags |= GL_NOCACHE;
444 gfs2_glock_dq_uninit(&freeze_gh);
8ad151c2
SW
445fail_threads:
446 kthread_stop(sdp->sd_quotad_process);
447 kthread_stop(sdp->sd_logd_process);
b3b94faa
DT
448 return error;
449}
450
1946f70a 451void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
bb8d8a6f
SW
452{
453 const struct gfs2_statfs_change *str = buf;
454
455 sc->sc_total = be64_to_cpu(str->sc_total);
456 sc->sc_free = be64_to_cpu(str->sc_free);
457 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
458}
459
460static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
461{
462 struct gfs2_statfs_change *str = buf;
463
464 str->sc_total = cpu_to_be64(sc->sc_total);
465 str->sc_free = cpu_to_be64(sc->sc_free);
466 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
467}
468
b3b94faa
DT
469int gfs2_statfs_init(struct gfs2_sbd *sdp)
470{
feaa7bba 471 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
bd209cc0 472 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
feaa7bba 473 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0 474 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
b3b94faa
DT
475 struct buffer_head *m_bh, *l_bh;
476 struct gfs2_holder gh;
477 int error;
478
479 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
480 &gh);
481 if (error)
482 return error;
483
484 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
485 if (error)
486 goto out;
487
488 if (sdp->sd_args.ar_spectator) {
489 spin_lock(&sdp->sd_statfs_spin);
490 gfs2_statfs_change_in(m_sc, m_bh->b_data +
491 sizeof(struct gfs2_dinode));
492 spin_unlock(&sdp->sd_statfs_spin);
493 } else {
494 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
495 if (error)
496 goto out_m_bh;
497
498 spin_lock(&sdp->sd_statfs_spin);
499 gfs2_statfs_change_in(m_sc, m_bh->b_data +
500 sizeof(struct gfs2_dinode));
501 gfs2_statfs_change_in(l_sc, l_bh->b_data +
502 sizeof(struct gfs2_dinode));
503 spin_unlock(&sdp->sd_statfs_spin);
504
505 brelse(l_bh);
506 }
507
a91ea69f 508out_m_bh:
b3b94faa 509 brelse(m_bh);
a91ea69f 510out:
b3b94faa 511 gfs2_glock_dq_uninit(&gh);
b3b94faa
DT
512 return 0;
513}
514
cd915493
SW
515void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
516 s64 dinodes)
b3b94faa 517{
feaa7bba 518 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0 519 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
3d3c10f2 520 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
b3b94faa 521 struct buffer_head *l_bh;
c14f5735
BM
522 s64 x, y;
523 int need_sync = 0;
b3b94faa
DT
524 int error;
525
526 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
527 if (error)
528 return;
529
350a9b0a 530 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
b3b94faa
DT
531
532 spin_lock(&sdp->sd_statfs_spin);
533 l_sc->sc_total += total;
534 l_sc->sc_free += free;
535 l_sc->sc_dinodes += dinodes;
907b9bce 536 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
c14f5735
BM
537 if (sdp->sd_args.ar_statfs_percent) {
538 x = 100 * l_sc->sc_free;
539 y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
540 if (x >= y || x <= -y)
541 need_sync = 1;
542 }
b3b94faa
DT
543 spin_unlock(&sdp->sd_statfs_spin);
544
545 brelse(l_bh);
c14f5735 546 if (need_sync)
3d3c10f2 547 gfs2_wake_up_statfs(sdp);
b3b94faa
DT
548}
549
1946f70a
BM
550void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh,
551 struct buffer_head *l_bh)
552{
553 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
554 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
555 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
556 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
557
350a9b0a 558 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
1946f70a
BM
559
560 spin_lock(&sdp->sd_statfs_spin);
561 m_sc->sc_total += l_sc->sc_total;
562 m_sc->sc_free += l_sc->sc_free;
563 m_sc->sc_dinodes += l_sc->sc_dinodes;
564 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
565 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
566 0, sizeof(struct gfs2_statfs_change));
567 spin_unlock(&sdp->sd_statfs_spin);
568
350a9b0a 569 gfs2_trans_add_meta(m_ip->i_gl, m_bh);
1946f70a
BM
570 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
571}
572
8c42d637 573int gfs2_statfs_sync(struct super_block *sb, int type)
b3b94faa 574{
8c42d637 575 struct gfs2_sbd *sdp = sb->s_fs_info;
feaa7bba
SW
576 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
577 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
bd209cc0
AV
578 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
579 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
b3b94faa
DT
580 struct gfs2_holder gh;
581 struct buffer_head *m_bh, *l_bh;
582 int error;
583
2e60d768 584 sb_start_write(sb);
b3b94faa
DT
585 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
586 &gh);
587 if (error)
2e60d768 588 goto out;
b3b94faa
DT
589
590 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
591 if (error)
2e60d768 592 goto out_unlock;
b3b94faa
DT
593
594 spin_lock(&sdp->sd_statfs_spin);
595 gfs2_statfs_change_in(m_sc, m_bh->b_data +
907b9bce 596 sizeof(struct gfs2_dinode));
b3b94faa
DT
597 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
598 spin_unlock(&sdp->sd_statfs_spin);
599 goto out_bh;
600 }
601 spin_unlock(&sdp->sd_statfs_spin);
602
603 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
604 if (error)
605 goto out_bh;
606
607 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
608 if (error)
609 goto out_bh2;
610
1946f70a 611 update_statfs(sdp, m_bh, l_bh);
3d3c10f2 612 sdp->sd_statfs_force_sync = 0;
b3b94faa
DT
613
614 gfs2_trans_end(sdp);
615
a91ea69f 616out_bh2:
b3b94faa 617 brelse(l_bh);
a91ea69f 618out_bh:
b3b94faa 619 brelse(m_bh);
2e60d768 620out_unlock:
b3b94faa 621 gfs2_glock_dq_uninit(&gh);
2e60d768
BM
622out:
623 sb_end_write(sb);
b3b94faa
DT
624 return error;
625}
626
b3b94faa
DT
627struct lfcc {
628 struct list_head list;
629 struct gfs2_holder gh;
630};
631
632/**
633 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
634 * journals are clean
635 * @sdp: the file system
636 * @state: the state to put the transaction lock into
637 * @t_gh: the hold on the transaction lock
638 *
639 * Returns: errno
640 */
641
08bc2dbc 642static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
24972557 643 struct gfs2_holder *freeze_gh)
b3b94faa 644{
5c676f6d 645 struct gfs2_inode *ip;
b3b94faa
DT
646 struct gfs2_jdesc *jd;
647 struct lfcc *lfcc;
648 LIST_HEAD(list);
55167622 649 struct gfs2_log_header_host lh;
b3b94faa
DT
650 int error;
651
b3b94faa
DT
652 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
653 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
654 if (!lfcc) {
655 error = -ENOMEM;
656 goto out;
657 }
feaa7bba
SW
658 ip = GFS2_I(jd->jd_inode);
659 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
b3b94faa
DT
660 if (error) {
661 kfree(lfcc);
662 goto out;
663 }
664 list_add(&lfcc->list, &list);
665 }
666
24972557
BM
667 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
668 GL_NOCACHE, freeze_gh);
b3b94faa
DT
669
670 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
671 error = gfs2_jdesc_check(jd);
672 if (error)
673 break;
674 error = gfs2_find_jhead(jd, &lh);
675 if (error)
676 break;
677 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
678 error = -EBUSY;
679 break;
680 }
681 }
682
683 if (error)
24972557 684 gfs2_glock_dq_uninit(freeze_gh);
b3b94faa 685
a91ea69f 686out:
b3b94faa
DT
687 while (!list_empty(&list)) {
688 lfcc = list_entry(list.next, struct lfcc, list);
689 list_del(&lfcc->list);
690 gfs2_glock_dq_uninit(&lfcc->gh);
691 kfree(lfcc);
692 }
b3b94faa
DT
693 return error;
694}
695
9eed04cd
SW
696void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
697{
698 struct gfs2_dinode *str = buf;
699
700 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
701 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
702 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
703 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
704 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
705 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
d0546426
EB
706 str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
707 str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
9eed04cd
SW
708 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
709 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
710 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
711 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
712 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
713 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
714
715 str->di_goal_meta = cpu_to_be64(ip->i_goal);
716 str->di_goal_data = cpu_to_be64(ip->i_goal);
717 str->di_generation = cpu_to_be64(ip->i_generation);
718
719 str->di_flags = cpu_to_be32(ip->i_diskflags);
720 str->di_height = cpu_to_be16(ip->i_height);
721 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
722 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
723 GFS2_FORMAT_DE : 0);
724 str->di_depth = cpu_to_be16(ip->i_depth);
725 str->di_entries = cpu_to_be32(ip->i_entries);
726
727 str->di_eattr = cpu_to_be64(ip->i_eattr);
728 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
729 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
730 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
731}
9e6e0a12
SW
732
733/**
734 * gfs2_write_inode - Make sure the inode is stable on the disk
735 * @inode: The inode
1027efaa 736 * @wbc: The writeback control structure
9e6e0a12
SW
737 *
738 * Returns: errno
739 */
740
a9185b41 741static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
9e6e0a12
SW
742{
743 struct gfs2_inode *ip = GFS2_I(inode);
744 struct gfs2_sbd *sdp = GFS2_SB(inode);
1027efaa 745 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
de1414a6 746 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
ab9bbda0
SW
747 int ret = 0;
748
a9185b41 749 if (wbc->sync_mode == WB_SYNC_ALL)
24972557 750 gfs2_log_flush(GFS2_SB(inode), ip->i_gl, NORMAL_FLUSH);
a88a341a 751 if (bdi->wb.dirty_exceeded)
4667a0ec 752 gfs2_ail1_flush(sdp, wbc);
1d4ec642
SW
753 else
754 filemap_fdatawrite(metamapping);
ab9bbda0 755 if (wbc->sync_mode == WB_SYNC_ALL)
1027efaa
SW
756 ret = filemap_fdatawait(metamapping);
757 if (ret)
758 mark_inode_dirty_sync(inode);
9e6e0a12
SW
759 return ret;
760}
761
ab9bbda0
SW
762/**
763 * gfs2_dirty_inode - check for atime updates
764 * @inode: The inode in question
765 * @flags: The type of dirty
766 *
767 * Unfortunately it can be called under any combination of inode
768 * glock and transaction lock, so we have to check carefully.
769 *
770 * At the moment this deals only with atime - it should be possible
771 * to expand that role in future, once a review of the locking has
772 * been carried out.
773 */
774
775static void gfs2_dirty_inode(struct inode *inode, int flags)
776{
777 struct gfs2_inode *ip = GFS2_I(inode);
778 struct gfs2_sbd *sdp = GFS2_SB(inode);
779 struct buffer_head *bh;
780 struct gfs2_holder gh;
781 int need_unlock = 0;
782 int need_endtrans = 0;
783 int ret;
784
785 if (!(flags & (I_DIRTY_DATASYNC|I_DIRTY_SYNC)))
786 return;
787
788 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
789 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
790 if (ret) {
791 fs_err(sdp, "dirty_inode: glock %d\n", ret);
792 return;
793 }
794 need_unlock = 1;
3d162688
BM
795 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
796 return;
ab9bbda0
SW
797
798 if (current->journal_info == NULL) {
799 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
800 if (ret) {
801 fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
802 goto out;
803 }
804 need_endtrans = 1;
805 }
806
807 ret = gfs2_meta_inode_buffer(ip, &bh);
808 if (ret == 0) {
350a9b0a 809 gfs2_trans_add_meta(ip->i_gl, bh);
ab9bbda0
SW
810 gfs2_dinode_out(ip, bh->b_data);
811 brelse(bh);
812 }
813
814 if (need_endtrans)
815 gfs2_trans_end(sdp);
816out:
817 if (need_unlock)
818 gfs2_glock_dq_uninit(&gh);
819}
820
9e6e0a12
SW
821/**
822 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
823 * @sdp: the filesystem
824 *
825 * Returns: errno
826 */
827
828static int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
829{
2e60d768 830 struct gfs2_holder freeze_gh;
9e6e0a12
SW
831 int error;
832
24972557 833 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_NOCACHE,
2e60d768 834 &freeze_gh);
24972557
BM
835 if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
836 return error;
837
8ad151c2
SW
838 kthread_stop(sdp->sd_quotad_process);
839 kthread_stop(sdp->sd_logd_process);
840
b94a170e 841 flush_workqueue(gfs2_delete_workqueue);
ceed1723 842 gfs2_quota_sync(sdp->sd_vfs, 0);
8c42d637 843 gfs2_statfs_sync(sdp->sd_vfs, 0);
9e6e0a12 844
2e60d768
BM
845 down_write(&sdp->sd_log_flush_lock);
846 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
847 up_write(&sdp->sd_log_flush_lock);
848
24972557 849 gfs2_log_flush(sdp, NULL, SHUTDOWN_FLUSH);
2e60d768 850 wait_event(sdp->sd_reserving_log_wait, atomic_read(&sdp->sd_reserving_log) == 0);
24972557 851 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
9e6e0a12 852
2e60d768
BM
853 if (freeze_gh.gh_gl)
854 gfs2_glock_dq_uninit(&freeze_gh);
9e6e0a12
SW
855
856 gfs2_quota_cleanup(sdp);
857
858 return error;
859}
860
9e6e0a12
SW
861/**
862 * gfs2_put_super - Unmount the filesystem
863 * @sb: The VFS superblock
864 *
865 */
866
867static void gfs2_put_super(struct super_block *sb)
868{
869 struct gfs2_sbd *sdp = sb->s_fs_info;
870 int error;
871 struct gfs2_jdesc *jd;
872
9e6e0a12
SW
873 /* No more recovery requests */
874 set_bit(SDF_NORECOVERY, &sdp->sd_flags);
875 smp_mb();
876
877 /* Wait on outstanding recovery */
878restart:
879 spin_lock(&sdp->sd_jindex_spin);
880 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
881 if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
882 continue;
883 spin_unlock(&sdp->sd_jindex_spin);
884 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
74316201 885 TASK_UNINTERRUPTIBLE);
9e6e0a12
SW
886 goto restart;
887 }
888 spin_unlock(&sdp->sd_jindex_spin);
889
9e6e0a12
SW
890 if (!(sb->s_flags & MS_RDONLY)) {
891 error = gfs2_make_fs_ro(sdp);
892 if (error)
893 gfs2_io_error(sdp);
894 }
895 /* At this point, we're through modifying the disk */
896
897 /* Release stuff */
898
899 iput(sdp->sd_jindex);
9e6e0a12
SW
900 iput(sdp->sd_statfs_inode);
901 iput(sdp->sd_rindex);
902 iput(sdp->sd_quota_inode);
903
904 gfs2_glock_put(sdp->sd_rename_gl);
24972557 905 gfs2_glock_put(sdp->sd_freeze_gl);
9e6e0a12
SW
906
907 if (!sdp->sd_args.ar_spectator) {
908 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
909 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
9e6e0a12
SW
910 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
911 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
9e6e0a12
SW
912 iput(sdp->sd_sc_inode);
913 iput(sdp->sd_qc_inode);
914 }
915
916 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
917 gfs2_clear_rgrpd(sdp);
918 gfs2_jindex_free(sdp);
919 /* Take apart glock structures and buffer lists */
920 gfs2_gl_hash_clear(sdp);
921 /* Unmount the locking protocol */
922 gfs2_lm_unmount(sdp);
923
924 /* At this point, we're through participating in the lockspace */
925 gfs2_sys_fs_del(sdp);
926}
927
9e6e0a12
SW
928/**
929 * gfs2_sync_fs - sync the filesystem
930 * @sb: the superblock
931 *
932 * Flushes the log to disk.
933 */
934
935static int gfs2_sync_fs(struct super_block *sb, int wait)
936{
1027efaa 937 struct gfs2_sbd *sdp = sb->s_fs_info;
a1177825
JK
938
939 gfs2_quota_sync(sb, -1);
2e60d768 940 if (wait && sdp)
24972557 941 gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
9e6e0a12
SW
942 return 0;
943}
944
2e60d768
BM
945void gfs2_freeze_func(struct work_struct *work)
946{
947 int error;
948 struct gfs2_holder freeze_gh;
949 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
950 struct super_block *sb = sdp->sd_vfs;
951
952 atomic_inc(&sb->s_active);
953 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
954 &freeze_gh);
955 if (error) {
956 printk(KERN_INFO "GFS2: couln't get freeze lock : %d\n", error);
957 gfs2_assert_withdraw(sdp, 0);
958 }
959 else {
960 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
961 error = thaw_super(sb);
962 if (error) {
963 printk(KERN_INFO "GFS2: couldn't thaw filesystem: %d\n",
964 error);
965 gfs2_assert_withdraw(sdp, 0);
966 }
967 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
968 freeze_gh.gh_flags |= GL_NOCACHE;
969 gfs2_glock_dq_uninit(&freeze_gh);
970 }
971 deactivate_super(sb);
972 return;
973}
974
9e6e0a12
SW
975/**
976 * gfs2_freeze - prevent further writes to the filesystem
977 * @sb: the VFS structure for the filesystem
978 *
979 */
980
981static int gfs2_freeze(struct super_block *sb)
982{
983 struct gfs2_sbd *sdp = sb->s_fs_info;
2e60d768 984 int error = 0;
9e6e0a12 985
2e60d768
BM
986 mutex_lock(&sdp->sd_freeze_mutex);
987 if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN)
988 goto out;
989
990 if (test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
991 error = -EINVAL;
992 goto out;
993 }
9e6e0a12
SW
994
995 for (;;) {
d564053f 996 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
9e6e0a12
SW
997 if (!error)
998 break;
999
1000 switch (error) {
1001 case -EBUSY:
1002 fs_err(sdp, "waiting for recovery before freeze\n");
1003 break;
1004
1005 default:
1006 fs_err(sdp, "error freezing FS: %d\n", error);
1007 break;
1008 }
1009
1010 fs_err(sdp, "retrying...\n");
1011 msleep(1000);
1012 }
2e60d768
BM
1013 error = 0;
1014out:
1015 mutex_unlock(&sdp->sd_freeze_mutex);
1016 return error;
9e6e0a12
SW
1017}
1018
1019/**
1020 * gfs2_unfreeze - reallow writes to the filesystem
1021 * @sb: the VFS structure for the filesystem
1022 *
1023 */
1024
1025static int gfs2_unfreeze(struct super_block *sb)
1026{
d564053f
SW
1027 struct gfs2_sbd *sdp = sb->s_fs_info;
1028
2e60d768
BM
1029 mutex_lock(&sdp->sd_freeze_mutex);
1030 if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
1031 sdp->sd_freeze_gh.gh_gl == NULL) {
1032 mutex_unlock(&sdp->sd_freeze_mutex);
1033 return 0;
1034 }
1035
d564053f 1036 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
2e60d768 1037 mutex_unlock(&sdp->sd_freeze_mutex);
9e6e0a12
SW
1038 return 0;
1039}
1040
1041/**
1042 * statfs_fill - fill in the sg for a given RG
1043 * @rgd: the RG
1044 * @sc: the sc structure
1045 *
1046 * Returns: 0 on success, -ESTALE if the LVB is invalid
1047 */
1048
1049static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
1050 struct gfs2_statfs_change_host *sc)
1051{
1052 gfs2_rgrp_verify(rgd);
1053 sc->sc_total += rgd->rd_data;
1054 sc->sc_free += rgd->rd_free;
1055 sc->sc_dinodes += rgd->rd_dinodes;
1056 return 0;
1057}
1058
1059/**
1060 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
1061 * @sdp: the filesystem
1062 * @sc: the sc info that will be returned
1063 *
1064 * Any error (other than a signal) will cause this routine to fall back
1065 * to the synchronous version.
1066 *
1067 * FIXME: This really shouldn't busy wait like this.
1068 *
1069 * Returns: errno
1070 */
1071
1072static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
1073{
9e6e0a12
SW
1074 struct gfs2_rgrpd *rgd_next;
1075 struct gfs2_holder *gha, *gh;
1076 unsigned int slots = 64;
1077 unsigned int x;
1078 int done;
1079 int error = 0, err;
1080
1081 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
1082 gha = kcalloc(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
1083 if (!gha)
1084 return -ENOMEM;
1085
9e6e0a12
SW
1086 rgd_next = gfs2_rgrpd_get_first(sdp);
1087
1088 for (;;) {
1089 done = 1;
1090
1091 for (x = 0; x < slots; x++) {
1092 gh = gha + x;
1093
1094 if (gh->gh_gl && gfs2_glock_poll(gh)) {
1095 err = gfs2_glock_wait(gh);
1096 if (err) {
1097 gfs2_holder_uninit(gh);
1098 error = err;
1099 } else {
1100 if (!error)
1101 error = statfs_slow_fill(
1102 gh->gh_gl->gl_object, sc);
1103 gfs2_glock_dq_uninit(gh);
1104 }
1105 }
1106
1107 if (gh->gh_gl)
1108 done = 0;
1109 else if (rgd_next && !error) {
1110 error = gfs2_glock_nq_init(rgd_next->rd_gl,
1111 LM_ST_SHARED,
1112 GL_ASYNC,
1113 gh);
1114 rgd_next = gfs2_rgrpd_get_next(rgd_next);
1115 done = 0;
1116 }
1117
1118 if (signal_pending(current))
1119 error = -ERESTARTSYS;
1120 }
1121
1122 if (done)
1123 break;
1124
1125 yield();
1126 }
1127
9e6e0a12
SW
1128 kfree(gha);
1129 return error;
1130}
1131
1132/**
1133 * gfs2_statfs_i - Do a statfs
1134 * @sdp: the filesystem
1135 * @sg: the sg structure
1136 *
1137 * Returns: errno
1138 */
1139
1140static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
1141{
1142 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
1143 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
1144
1145 spin_lock(&sdp->sd_statfs_spin);
1146
1147 *sc = *m_sc;
1148 sc->sc_total += l_sc->sc_total;
1149 sc->sc_free += l_sc->sc_free;
1150 sc->sc_dinodes += l_sc->sc_dinodes;
1151
1152 spin_unlock(&sdp->sd_statfs_spin);
1153
1154 if (sc->sc_free < 0)
1155 sc->sc_free = 0;
1156 if (sc->sc_free > sc->sc_total)
1157 sc->sc_free = sc->sc_total;
1158 if (sc->sc_dinodes < 0)
1159 sc->sc_dinodes = 0;
1160
1161 return 0;
1162}
1163
1164/**
1165 * gfs2_statfs - Gather and return stats about the filesystem
1166 * @sb: The superblock
1167 * @statfsbuf: The buffer
1168 *
1169 * Returns: 0 on success or error code
1170 */
1171
1172static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
1173{
2b0143b5 1174 struct super_block *sb = d_inode(dentry)->i_sb;
9e6e0a12
SW
1175 struct gfs2_sbd *sdp = sb->s_fs_info;
1176 struct gfs2_statfs_change_host sc;
1177 int error;
1178
8339ee54
SW
1179 error = gfs2_rindex_update(sdp);
1180 if (error)
1181 return error;
1182
9e6e0a12
SW
1183 if (gfs2_tune_get(sdp, gt_statfs_slow))
1184 error = gfs2_statfs_slow(sdp, &sc);
1185 else
1186 error = gfs2_statfs_i(sdp, &sc);
1187
1188 if (error)
1189 return error;
1190
1191 buf->f_type = GFS2_MAGIC;
1192 buf->f_bsize = sdp->sd_sb.sb_bsize;
1193 buf->f_blocks = sc.sc_total;
1194 buf->f_bfree = sc.sc_free;
1195 buf->f_bavail = sc.sc_free;
1196 buf->f_files = sc.sc_dinodes + sc.sc_free;
1197 buf->f_ffree = sc.sc_free;
1198 buf->f_namelen = GFS2_FNAMESIZE;
1199
1200 return 0;
1201}
1202
1203/**
1204 * gfs2_remount_fs - called when the FS is remounted
1205 * @sb: the filesystem
1206 * @flags: the remount flags
1207 * @data: extra data passed in (not used right now)
1208 *
1209 * Returns: errno
1210 */
1211
1212static int gfs2_remount_fs(struct super_block *sb, int *flags, char *data)
1213{
1214 struct gfs2_sbd *sdp = sb->s_fs_info;
1215 struct gfs2_args args = sdp->sd_args; /* Default to current settings */
1216 struct gfs2_tune *gt = &sdp->sd_tune;
1217 int error;
1218
02b9984d
TT
1219 sync_filesystem(sb);
1220
9e6e0a12 1221 spin_lock(&gt->gt_spin);
5e687eac 1222 args.ar_commit = gt->gt_logd_secs;
3d3c10f2
BM
1223 args.ar_quota_quantum = gt->gt_quota_quantum;
1224 if (gt->gt_statfs_slow)
1225 args.ar_statfs_quantum = 0;
1226 else
1227 args.ar_statfs_quantum = gt->gt_statfs_quantum;
9e6e0a12 1228 spin_unlock(&gt->gt_spin);
f55073ff 1229 error = gfs2_mount_args(&args, data);
9e6e0a12
SW
1230 if (error)
1231 return error;
1232
1233 /* Not allowed to change locking details */
1234 if (strcmp(args.ar_lockproto, sdp->sd_args.ar_lockproto) ||
1235 strcmp(args.ar_locktable, sdp->sd_args.ar_locktable) ||
1236 strcmp(args.ar_hostdata, sdp->sd_args.ar_hostdata))
1237 return -EINVAL;
1238
1239 /* Some flags must not be changed */
1240 if (args_neq(&args, &sdp->sd_args, spectator) ||
9e6e0a12 1241 args_neq(&args, &sdp->sd_args, localflocks) ||
9e6e0a12
SW
1242 args_neq(&args, &sdp->sd_args, meta))
1243 return -EINVAL;
1244
1245 if (sdp->sd_args.ar_spectator)
1246 *flags |= MS_RDONLY;
1247
1248 if ((sb->s_flags ^ *flags) & MS_RDONLY) {
1249 if (*flags & MS_RDONLY)
1250 error = gfs2_make_fs_ro(sdp);
1251 else
1252 error = gfs2_make_fs_rw(sdp);
1253 if (error)
1254 return error;
1255 }
1256
1257 sdp->sd_args = args;
1258 if (sdp->sd_args.ar_posix_acl)
1259 sb->s_flags |= MS_POSIXACL;
1260 else
1261 sb->s_flags &= ~MS_POSIXACL;
f25934c5
CH
1262 if (sdp->sd_args.ar_nobarrier)
1263 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1264 else
1265 clear_bit(SDF_NOBARRIERS, &sdp->sd_flags);
9e6e0a12 1266 spin_lock(&gt->gt_spin);
5e687eac 1267 gt->gt_logd_secs = args.ar_commit;
3d3c10f2
BM
1268 gt->gt_quota_quantum = args.ar_quota_quantum;
1269 if (args.ar_statfs_quantum) {
1270 gt->gt_statfs_slow = 0;
1271 gt->gt_statfs_quantum = args.ar_statfs_quantum;
1272 }
1273 else {
1274 gt->gt_statfs_slow = 1;
1275 gt->gt_statfs_quantum = 30;
1276 }
9e6e0a12
SW
1277 spin_unlock(&gt->gt_spin);
1278
8633ecfa 1279 gfs2_online_uevent(sdp);
9e6e0a12
SW
1280 return 0;
1281}
1282
1283/**
1284 * gfs2_drop_inode - Drop an inode (test for remote unlink)
1285 * @inode: The inode to drop
1286 *
1287 * If we've received a callback on an iopen lock then its because a
1288 * remote node tried to deallocate the inode but failed due to this node
1289 * still having the inode open. Here we mark the link count zero
1290 * since we know that it must have reached zero if the GLF_DEMOTE flag
1291 * is set on the iopen glock. If we didn't do a disk read since the
1292 * remote node removed the final link then we might otherwise miss
1293 * this event. This check ensures that this node will deallocate the
1294 * inode's blocks, or alternatively pass the baton on to another
1295 * node for later deallocation.
1296 */
1297
45321ac5 1298static int gfs2_drop_inode(struct inode *inode)
9e6e0a12
SW
1299{
1300 struct gfs2_inode *ip = GFS2_I(inode);
1301
05978803 1302 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && inode->i_nlink) {
9e6e0a12
SW
1303 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1304 if (gl && test_bit(GLF_DEMOTE, &gl->gl_flags))
1305 clear_nlink(inode);
1306 }
45321ac5 1307 return generic_drop_inode(inode);
9e6e0a12
SW
1308}
1309
1310static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
1311{
1312 do {
1313 if (d1 == d2)
1314 return 1;
1315 d1 = d1->d_parent;
1316 } while (!IS_ROOT(d1));
1317 return 0;
1318}
1319
1320/**
1321 * gfs2_show_options - Show mount options for /proc/mounts
1322 * @s: seq_file structure
34c80b1d 1323 * @root: root of this (sub)tree
9e6e0a12
SW
1324 *
1325 * Returns: 0 on success or error code
1326 */
1327
34c80b1d 1328static int gfs2_show_options(struct seq_file *s, struct dentry *root)
9e6e0a12 1329{
34c80b1d 1330 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
9e6e0a12 1331 struct gfs2_args *args = &sdp->sd_args;
3d3c10f2 1332 int val;
9e6e0a12 1333
34c80b1d 1334 if (is_ancestor(root, sdp->sd_master_dir))
eaebdedc 1335 seq_puts(s, ",meta");
9e6e0a12
SW
1336 if (args->ar_lockproto[0])
1337 seq_printf(s, ",lockproto=%s", args->ar_lockproto);
1338 if (args->ar_locktable[0])
1339 seq_printf(s, ",locktable=%s", args->ar_locktable);
1340 if (args->ar_hostdata[0])
1341 seq_printf(s, ",hostdata=%s", args->ar_hostdata);
1342 if (args->ar_spectator)
eaebdedc 1343 seq_puts(s, ",spectator");
9e6e0a12 1344 if (args->ar_localflocks)
eaebdedc 1345 seq_puts(s, ",localflocks");
9e6e0a12 1346 if (args->ar_debug)
eaebdedc 1347 seq_puts(s, ",debug");
9e6e0a12 1348 if (args->ar_posix_acl)
eaebdedc 1349 seq_puts(s, ",acl");
9e6e0a12
SW
1350 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1351 char *state;
1352 switch (args->ar_quota) {
1353 case GFS2_QUOTA_OFF:
1354 state = "off";
1355 break;
1356 case GFS2_QUOTA_ACCOUNT:
1357 state = "account";
1358 break;
1359 case GFS2_QUOTA_ON:
1360 state = "on";
1361 break;
1362 default:
1363 state = "unknown";
1364 break;
1365 }
1366 seq_printf(s, ",quota=%s", state);
1367 }
1368 if (args->ar_suiddir)
eaebdedc 1369 seq_puts(s, ",suiddir");
9e6e0a12
SW
1370 if (args->ar_data != GFS2_DATA_DEFAULT) {
1371 char *state;
1372 switch (args->ar_data) {
1373 case GFS2_DATA_WRITEBACK:
1374 state = "writeback";
1375 break;
1376 case GFS2_DATA_ORDERED:
1377 state = "ordered";
1378 break;
1379 default:
1380 state = "unknown";
1381 break;
1382 }
1383 seq_printf(s, ",data=%s", state);
1384 }
1385 if (args->ar_discard)
eaebdedc 1386 seq_puts(s, ",discard");
5e687eac
BM
1387 val = sdp->sd_tune.gt_logd_secs;
1388 if (val != 30)
3d3c10f2
BM
1389 seq_printf(s, ",commit=%d", val);
1390 val = sdp->sd_tune.gt_statfs_quantum;
1391 if (val != 30)
1392 seq_printf(s, ",statfs_quantum=%d", val);
2b9731e8
SW
1393 else if (sdp->sd_tune.gt_statfs_slow)
1394 seq_puts(s, ",statfs_quantum=0");
3d3c10f2
BM
1395 val = sdp->sd_tune.gt_quota_quantum;
1396 if (val != 60)
1397 seq_printf(s, ",quota_quantum=%d", val);
1398 if (args->ar_statfs_percent)
1399 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
d34843d0
BP
1400 if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1401 const char *state;
1402
1403 switch (args->ar_errors) {
1404 case GFS2_ERRORS_WITHDRAW:
1405 state = "withdraw";
1406 break;
1407 case GFS2_ERRORS_PANIC:
1408 state = "panic";
1409 break;
1410 default:
1411 state = "unknown";
1412 break;
1413 }
1414 seq_printf(s, ",errors=%s", state);
1415 }
cdcfde62 1416 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
eaebdedc 1417 seq_puts(s, ",nobarrier");
913a71d2 1418 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
eaebdedc 1419 seq_puts(s, ",demote_interface_used");
90306c41 1420 if (args->ar_rgrplvb)
eaebdedc 1421 seq_puts(s, ",rgrplvb");
9e6e0a12
SW
1422 return 0;
1423}
1424
f42ab085
SW
1425static void gfs2_final_release_pages(struct gfs2_inode *ip)
1426{
1427 struct inode *inode = &ip->i_inode;
1428 struct gfs2_glock *gl = ip->i_gl;
1429
1430 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1431 truncate_inode_pages(&inode->i_data, 0);
1432
1433 if (atomic_read(&gl->gl_revokes) == 0) {
1434 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1435 clear_bit(GLF_DIRTY, &gl->gl_flags);
1436 }
1437}
1438
1439static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1440{
1441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
f42ab085 1442 struct gfs2_rgrpd *rgd;
564e12b1 1443 struct gfs2_holder gh;
f42ab085
SW
1444 int error;
1445
1446 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
94fb763b 1447 gfs2_consist_inode(ip);
f42ab085
SW
1448 return -EIO;
1449 }
1450
8e2e0047
BP
1451 error = gfs2_rindex_update(sdp);
1452 if (error)
1453 return error;
f42ab085 1454
f4108a60 1455 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
f42ab085 1456 if (error)
5407e242 1457 return error;
f42ab085 1458
66fc061b 1459 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
f42ab085
SW
1460 if (!rgd) {
1461 gfs2_consist_inode(ip);
1462 error = -EIO;
8339ee54 1463 goto out_qs;
f42ab085
SW
1464 }
1465
564e12b1 1466 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
f42ab085 1467 if (error)
8339ee54 1468 goto out_qs;
f42ab085 1469
4667a0ec
SW
1470 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1471 sdp->sd_jdesc->jd_blocks);
f42ab085
SW
1472 if (error)
1473 goto out_rg_gunlock;
1474
1475 gfs2_free_di(rgd, ip);
1476
1477 gfs2_final_release_pages(ip);
1478
1479 gfs2_trans_end(sdp);
1480
1481out_rg_gunlock:
564e12b1 1482 gfs2_glock_dq_uninit(&gh);
f42ab085
SW
1483out_qs:
1484 gfs2_quota_unhold(ip);
f42ab085
SW
1485 return error;
1486}
1487
380f7c65
SW
1488/**
1489 * gfs2_evict_inode - Remove an inode from cache
1490 * @inode: The inode to evict
1491 *
1492 * There are three cases to consider:
1493 * 1. i_nlink == 0, we are final opener (and must deallocate)
1494 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1495 * 3. i_nlink > 0
1496 *
1497 * If the fs is read only, then we have to treat all cases as per #3
1498 * since we are unable to do any deallocation. The inode will be
1499 * deallocated by the next read/write node to attempt an allocation
1500 * in the same resource group
1501 *
9e6e0a12
SW
1502 * We have to (at the moment) hold the inodes main lock to cover
1503 * the gap between unlocking the shared lock on the iopen lock and
1504 * taking the exclusive lock. I'd rather do a shared -> exclusive
1505 * conversion on the iopen lock, but we can change that later. This
1506 * is safe, just less efficient.
1507 */
1508
d5c1515c 1509static void gfs2_evict_inode(struct inode *inode)
9e6e0a12 1510{
001e8e8d
SW
1511 struct super_block *sb = inode->i_sb;
1512 struct gfs2_sbd *sdp = sb->s_fs_info;
9e6e0a12
SW
1513 struct gfs2_inode *ip = GFS2_I(inode);
1514 struct gfs2_holder gh;
1515 int error;
1516
05978803
AD
1517 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1518 clear_inode(inode);
1519 return;
1520 }
1521
001e8e8d 1522 if (inode->i_nlink || (sb->s_flags & MS_RDONLY))
d5c1515c
AV
1523 goto out;
1524
44ad37d6
BP
1525 /* Must not read inode block until block type has been verified */
1526 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
9e6e0a12 1527 if (unlikely(error)) {
a6a4d98b 1528 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
9e6e0a12
SW
1529 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1530 goto out;
1531 }
1532
40ac218f
SW
1533 if (!test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1534 error = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1535 if (error)
1536 goto out_truncate;
1537 }
acf7e244 1538
44ad37d6
BP
1539 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1540 error = gfs2_inode_refresh(ip);
1541 if (error)
1542 goto out_truncate;
1543 }
1544
23c30108 1545 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
9e6e0a12
SW
1546 gfs2_glock_dq_wait(&ip->i_iopen_gh);
1547 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, &ip->i_iopen_gh);
1548 error = gfs2_glock_nq(&ip->i_iopen_gh);
1549 if (error)
1550 goto out_truncate;
1551
380f7c65
SW
1552 /* Case 1 starts here */
1553
9e6e0a12
SW
1554 if (S_ISDIR(inode->i_mode) &&
1555 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1556 error = gfs2_dir_exhash_dealloc(ip);
1557 if (error)
1558 goto out_unlock;
1559 }
1560
1561 if (ip->i_eattr) {
1562 error = gfs2_ea_dealloc(ip);
1563 if (error)
1564 goto out_unlock;
1565 }
1566
1567 if (!gfs2_is_stuffed(ip)) {
1568 error = gfs2_file_dealloc(ip);
1569 if (error)
1570 goto out_unlock;
1571 }
1572
1573 error = gfs2_dinode_dealloc(ip);
f42ab085 1574 goto out_unlock;
9e6e0a12
SW
1575
1576out_truncate:
24972557 1577 gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
2216db70
BM
1578 if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1579 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
1580 filemap_fdatawrite(metamapping);
1581 filemap_fdatawait(metamapping);
1582 }
40ac218f 1583 write_inode_now(inode, 1);
b5b24d7a 1584 gfs2_ail_flush(ip->i_gl, 0);
40ac218f 1585
380f7c65 1586 /* Case 2 starts here */
9e6e0a12
SW
1587 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1588 if (error)
1589 goto out_unlock;
380f7c65
SW
1590 /* Needs to be done before glock release & also in a transaction */
1591 truncate_inode_pages(&inode->i_data, 0);
9e6e0a12
SW
1592 gfs2_trans_end(sdp);
1593
1594out_unlock:
380f7c65 1595 /* Error path for case 1 */
8e2e0047 1596 if (gfs2_rs_active(ip->i_res))
20095218 1597 gfs2_rs_deltree(ip->i_res);
8e2e0047 1598
a6a4d98b
BP
1599 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1600 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
9e6e0a12 1601 gfs2_glock_dq(&ip->i_iopen_gh);
a6a4d98b 1602 }
9e6e0a12
SW
1603 gfs2_holder_uninit(&ip->i_iopen_gh);
1604 gfs2_glock_dq_uninit(&gh);
1605 if (error && error != GLR_TRYFAILED && error != -EROFS)
d5c1515c 1606 fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
9e6e0a12 1607out:
380f7c65 1608 /* Case 3 starts here */
91b0abe3 1609 truncate_inode_pages_final(&inode->i_data);
af5c2697 1610 gfs2_rs_delete(ip, NULL);
45138990 1611 gfs2_ordered_del_inode(ip);
dbd5768f 1612 clear_inode(inode);
17d539f0 1613 gfs2_dir_hash_inval(ip);
d5c1515c 1614 ip->i_gl->gl_object = NULL;
43829731 1615 flush_delayed_work(&ip->i_gl->gl_work);
29687a2a 1616 gfs2_glock_add_to_lru(ip->i_gl);
d5c1515c
AV
1617 gfs2_glock_put(ip->i_gl);
1618 ip->i_gl = NULL;
1619 if (ip->i_iopen_gh.gh_gl) {
1620 ip->i_iopen_gh.gh_gl->gl_object = NULL;
a6a4d98b 1621 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
d5c1515c
AV
1622 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1623 }
9e6e0a12
SW
1624}
1625
1626static struct inode *gfs2_alloc_inode(struct super_block *sb)
1627{
1628 struct gfs2_inode *ip;
1629
1630 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
1631 if (ip) {
1632 ip->i_flags = 0;
1633 ip->i_gl = NULL;
54335b1f 1634 ip->i_rgd = NULL;
0a305e49 1635 ip->i_res = NULL;
9e6e0a12
SW
1636 }
1637 return &ip->i_inode;
1638}
1639
fa0d7e3d 1640static void gfs2_i_callback(struct rcu_head *head)
9e6e0a12 1641{
fa0d7e3d 1642 struct inode *inode = container_of(head, struct inode, i_rcu);
9e6e0a12
SW
1643 kmem_cache_free(gfs2_inode_cachep, inode);
1644}
1645
fa0d7e3d
NP
1646static void gfs2_destroy_inode(struct inode *inode)
1647{
1648 call_rcu(&inode->i_rcu, gfs2_i_callback);
1649}
1650
9e6e0a12
SW
1651const struct super_operations gfs2_super_ops = {
1652 .alloc_inode = gfs2_alloc_inode,
1653 .destroy_inode = gfs2_destroy_inode,
1654 .write_inode = gfs2_write_inode,
ab9bbda0 1655 .dirty_inode = gfs2_dirty_inode,
d5c1515c 1656 .evict_inode = gfs2_evict_inode,
9e6e0a12 1657 .put_super = gfs2_put_super,
9e6e0a12 1658 .sync_fs = gfs2_sync_fs,
2e60d768
BM
1659 .freeze_super = gfs2_freeze,
1660 .thaw_super = gfs2_unfreeze,
9e6e0a12
SW
1661 .statfs = gfs2_statfs,
1662 .remount_fs = gfs2_remount_fs,
9e6e0a12
SW
1663 .drop_inode = gfs2_drop_inode,
1664 .show_options = gfs2_show_options,
1665};
1666