blob: 1666382b198db6463a75446578a7ee358ef66933 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersonda6dd402007-12-11 18:49:21 -06003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
Joe Perchesd77d1b52014-03-06 12:10:45 -080010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010012#include <linux/bio.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000013#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>
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010018#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>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include <linux/gfs2_ondisk.h>
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010024#include <linux/crc32.h>
25#include <linux/time.h>
Steven Whitehousee4027462010-01-25 11:20:19 +000026#include <linux/wait.h>
Christoph Hellwiga9185b42010-03-05 09:21:37 +010027#include <linux/writeback.h>
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010028#include <linux/backing-dev.h>
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060029#include <linux/kernel.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
31#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050032#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "bmap.h"
34#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000035#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"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050045#include "util.h"
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010046#include "sys.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010047#include "xattr.h"
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010048
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,
Bob Petersond34843d2009-08-24 10:44:18 +010077 Opt_err_withdraw,
78 Opt_err_panic,
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -050079 Opt_statfs_quantum,
80 Opt_statfs_percent,
81 Opt_quota_quantum,
Christoph Hellwigf25934c2009-10-30 08:03:27 +010082 Opt_barrier,
83 Opt_nobarrier,
Benjamin Marzinski90306c42012-05-29 23:01:09 -050084 Opt_rgrplvb,
85 Opt_norgrplvb,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010086 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"},
Steven Whitehouse43f74c12010-09-29 14:24:41 +010094 {Opt_spectator, "norecovery"},
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010095 {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"},
Bob Petersond34843d2009-08-24 10:44:18 +0100116 {Opt_err_withdraw, "errors=withdraw"},
117 {Opt_err_panic, "errors=panic"},
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500118 {Opt_statfs_quantum, "statfs_quantum=%d"},
119 {Opt_statfs_percent, "statfs_percent=%d"},
120 {Opt_quota_quantum, "quota_quantum=%d"},
Christoph Hellwigf25934c2009-10-30 08:03:27 +0100121 {Opt_barrier, "barrier"},
122 {Opt_nobarrier, "nobarrier"},
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500123 {Opt_rgrplvb, "rgrplvb"},
124 {Opt_norgrplvb, "norgrplvb"},
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100125 {Opt_error, NULL}
126};
127
128/**
129 * gfs2_mount_args - Parse mount options
Steven Whitehousef55073f2009-09-28 10:30:49 +0100130 * @args: The structure into which the parsed options will be written
131 * @options: The options to parse
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100132 *
133 * Return: errno
134 */
135
Steven Whitehousef55073f2009-09-28 10:30:49 +0100136int gfs2_mount_args(struct gfs2_args *args, char *options)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100137{
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:
Steven Whitehousef57a0242010-09-23 13:41:42 +0100171 /* Retained for backwards compat only */
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100172 break;
173 case Opt_localflocks:
174 args->ar_localflocks = 1;
175 break;
176 case Opt_localcaching:
Steven Whitehousec2048b002010-09-23 14:00:31 +0100177 /* Retained for backwards compat only */
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100178 break;
179 case Opt_debug:
Bob Petersond34843d2009-08-24 10:44:18 +0100180 if (args->ar_errors == GFS2_ERRORS_PANIC) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800181 pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
Bob Petersond34843d2009-08-24 10:44:18 +0100182 return -EINVAL;
183 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100184 args->ar_debug = 1;
185 break;
186 case Opt_nodebug:
187 args->ar_debug = 0;
188 break;
189 case Opt_upgrade:
Steven Whitehousec80dbb52010-09-24 09:55:07 +0100190 /* Retained for backwards compat only */
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100191 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) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800233 pr_warn("commit mount option requires a positive numeric argument\n");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100234 return rv ? rv : -EINVAL;
235 }
236 break;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500237 case Opt_statfs_quantum:
238 rv = match_int(&tmp[0], &args->ar_statfs_quantum);
239 if (rv || args->ar_statfs_quantum < 0) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800240 pr_warn("statfs_quantum mount option requires a non-negative numeric argument\n");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500241 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) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800247 pr_warn("quota_quantum mount option requires a positive numeric argument\n");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500248 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) {
Fabian Frederickfc554ed2014-03-05 22:06:42 +0800255 pr_warn("statfs_percent mount option requires a numeric argument between 0 and 100\n");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500256 return rv ? rv : -EINVAL;
257 }
258 break;
Bob Petersond34843d2009-08-24 10:44:18 +0100259 case Opt_err_withdraw:
260 args->ar_errors = GFS2_ERRORS_WITHDRAW;
261 break;
262 case Opt_err_panic:
263 if (args->ar_debug) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800264 pr_warn("-o debug and -o errors=panic are mutually exclusive\n");
Bob Petersond34843d2009-08-24 10:44:18 +0100265 return -EINVAL;
266 }
267 args->ar_errors = GFS2_ERRORS_PANIC;
268 break;
Christoph Hellwigf25934c2009-10-30 08:03:27 +0100269 case Opt_barrier:
270 args->ar_nobarrier = 0;
271 break;
272 case Opt_nobarrier:
273 args->ar_nobarrier = 1;
274 break;
Benjamin Marzinski90306c42012-05-29 23:01:09 -0500275 case Opt_rgrplvb:
276 args->ar_rgrplvb = 1;
277 break;
278 case Opt_norgrplvb:
279 args->ar_rgrplvb = 0;
280 break;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100281 case Opt_error:
282 default:
Joe Perchesd77d1b52014-03-06 12:10:45 -0800283 pr_warn("invalid mount option: %s\n", o);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100284 return -EINVAL;
285 }
286 }
287
288 return 0;
289}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290
Steven Whitehousefefc03b2008-12-19 15:32:06 +0000291/**
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{
Steven Whitehouseb50f2272014-03-03 13:35:57 +0000299 struct list_head list;
Steven Whitehousefefc03b2008-12-19 15:32:06 +0000300 struct gfs2_jdesc *jd;
Steven Whitehousefefc03b2008-12-19 15:32:06 +0000301
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);
Steven Whitehouseb50f2272014-03-03 13:35:57 +0000310 gfs2_free_journal_extents(jd);
Steven Whitehousefefc03b2008-12-19 15:32:06 +0000311 list_del(&jd->jd_list);
312 iput(jd->jd_inode);
313 kfree(jd);
314 }
315}
316
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317static 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000346int gfs2_jdesc_check(struct gfs2_jdesc *jd)
347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
349 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100350 u64 size = i_size_read(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000351
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100352 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, 1 << 30))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100355 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
356
357 if (gfs2_write_alloc_required(ip, 0, size)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358 gfs2_consist_inode(ip);
Bob Peterson461cb412010-06-24 19:21:20 -0400359 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000360 }
361
Bob Peterson461cb412010-06-24 19:21:20 -0400362 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363}
364
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000365static 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392/**
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{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400401 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500402 struct gfs2_glock *j_gl = ip->i_gl;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600403 struct gfs2_holder freeze_gh;
Al Viro55167622006-10-13 21:47:13 -0400404 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000405 int error;
406
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000407 error = init_threads(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 if (error)
409 return error;
410
Benjamin Marzinski24972552014-05-01 22:26:55 -0500411 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, 0,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600412 &freeze_gh);
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000413 if (error)
414 goto fail_threads;
415
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500416 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000417
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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 error = gfs2_quota_init(sdp);
433 if (error)
Steven Whitehousea91ea692006-09-04 12:04:26 -0400434 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
436 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
437
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600438 gfs2_glock_dq_uninit(&freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439
440 return 0;
441
Steven Whitehousea91ea692006-09-04 12:04:26 -0400442fail:
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600443 freeze_gh.gh_flags |= GL_NOCACHE;
444 gfs2_glock_dq_uninit(&freeze_gh);
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000445fail_threads:
446 kthread_stop(sdp->sd_quotad_process);
447 kthread_stop(sdp->sd_logd_process);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000448 return error;
449}
450
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500451void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100452{
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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469int gfs2_statfs_init(struct gfs2_sbd *sdp)
470{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400471 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400472 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400473 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400474 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 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
Steven Whitehousea91ea692006-09-04 12:04:26 -0400508out_m_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 brelse(m_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400510out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 return 0;
513}
514
Steven Whitehousecd915492006-09-04 12:49:07 -0400515void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
516 s64 dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400518 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400519 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500520 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 struct buffer_head *l_bh;
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500522 s64 x, y;
523 int need_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 int error;
525
526 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
527 if (error)
528 return;
529
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000530 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531
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;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400536 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500537 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 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000543 spin_unlock(&sdp->sd_statfs_spin);
544
545 brelse(l_bh);
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500546 if (need_sync)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500547 gfs2_wake_up_statfs(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548}
549
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500550void 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
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000558 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500559
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
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000569 gfs2_trans_add_meta(m_ip->i_gl, m_bh);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500570 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
571}
572
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100573int gfs2_statfs_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574{
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100575 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400576 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
577 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400578 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
579 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000580 struct gfs2_holder gh;
581 struct buffer_head *m_bh, *l_bh;
582 int error;
583
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600584 sb_start_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
586 &gh);
587 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600588 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
590 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
591 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600592 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000593
594 spin_lock(&sdp->sd_statfs_spin);
595 gfs2_statfs_change_in(m_sc, m_bh->b_data +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400596 sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597 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
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500611 update_statfs(sdp, m_bh, l_bh);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500612 sdp->sd_statfs_force_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000613
614 gfs2_trans_end(sdp);
615
Steven Whitehousea91ea692006-09-04 12:04:26 -0400616out_bh2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 brelse(l_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400618out_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 brelse(m_bh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600620out_unlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621 gfs2_glock_dq_uninit(&gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600622out:
623 sb_end_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000624 return error;
625}
626
David Teiglandb3b94fa2006-01-16 16:50:04 +0000627struct 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
Adrian Bunk08bc2db2006-04-28 10:59:12 -0400642static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp,
Benjamin Marzinski24972552014-05-01 22:26:55 -0500643 struct gfs2_holder *freeze_gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000644{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500645 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 struct gfs2_jdesc *jd;
647 struct lfcc *lfcc;
648 LIST_HEAD(list);
Al Viro55167622006-10-13 21:47:13 -0400649 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650 int error;
651
David Teiglandb3b94fa2006-01-16 16:50:04 +0000652 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 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400658 ip = GFS2_I(jd->jd_inode);
659 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000660 if (error) {
661 kfree(lfcc);
662 goto out;
663 }
664 list_add(&lfcc->list, &list);
665 }
666
Benjamin Marzinski24972552014-05-01 22:26:55 -0500667 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
668 GL_NOCACHE, freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000669
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)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500684 gfs2_glock_dq_uninit(freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000685
Steven Whitehousea91ea692006-09-04 12:04:26 -0400686out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000687 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 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693 return error;
694}
695
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100696void 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);
Eric W. Biedermand0546422013-01-31 22:08:10 -0800706 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));
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100708 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}
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100732
733/**
734 * gfs2_write_inode - Make sure the inode is stable on the disk
735 * @inode: The inode
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100736 * @wbc: The writeback control structure
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100737 *
738 * Returns: errno
739 */
740
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100741static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100742{
743 struct gfs2_inode *ip = GFS2_I(inode);
744 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100745 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100746 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100747 int ret = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100748
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100749 if (wbc->sync_mode == WB_SYNC_ALL)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500750 gfs2_log_flush(GFS2_SB(inode), ip->i_gl, NORMAL_FLUSH);
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100751 if (bdi->dirty_exceeded)
752 gfs2_ail1_flush(sdp, wbc);
Steven Whitehouse1d4ec642011-08-02 13:13:20 +0100753 else
754 filemap_fdatawrite(metamapping);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100755 if (wbc->sync_mode == WB_SYNC_ALL)
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100756 ret = filemap_fdatawait(metamapping);
757 if (ret)
758 mark_inode_dirty_sync(inode);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100759 return ret;
760}
761
762/**
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100763 * 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;
Benjamin Marzinski3d162682012-11-06 00:49:28 -0600795 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
796 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100797
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) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000809 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100810 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
821/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100822 * 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{
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600830 struct gfs2_holder freeze_gh;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100831 int error;
832
Benjamin Marzinski24972552014-05-01 22:26:55 -0500833 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_NOCACHE,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600834 &freeze_gh);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500835 if (error && !test_bit(SDF_SHUTDOWN, &sdp->sd_flags))
836 return error;
837
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000838 kthread_stop(sdp->sd_quotad_process);
839 kthread_stop(sdp->sd_logd_process);
840
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500841 flush_workqueue(gfs2_delete_workqueue);
Jan Karaceed1722012-07-03 16:45:28 +0200842 gfs2_quota_sync(sdp->sd_vfs, 0);
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100843 gfs2_statfs_sync(sdp->sd_vfs, 0);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100844
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600845 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
Benjamin Marzinski24972552014-05-01 22:26:55 -0500849 gfs2_log_flush(sdp, NULL, SHUTDOWN_FLUSH);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600850 wait_event(sdp->sd_reserving_log_wait, atomic_read(&sdp->sd_reserving_log) == 0);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500851 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100852
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600853 if (freeze_gh.gh_gl)
854 gfs2_glock_dq_uninit(&freeze_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100855
856 gfs2_quota_cleanup(sdp);
857
858 return error;
859}
860
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100861/**
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
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100873 /* 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,
NeilBrown74316202014-07-07 15:16:04 +1000885 TASK_UNINTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100886 goto restart;
887 }
888 spin_unlock(&sdp->sd_jindex_spin);
889
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100890 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);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100900 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);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500905 gfs2_glock_put(sdp->sd_freeze_gl);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100906
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);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100910 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
911 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100912 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
928/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100929 * 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{
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100937 struct gfs2_sbd *sdp = sb->s_fs_info;
Jan Karaa1177822012-07-03 16:45:29 +0200938
939 gfs2_quota_sync(sb, -1);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600940 if (wait && sdp)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500941 gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100942 return 0;
943}
944
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600945void 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
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100975/**
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;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600984 int error = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100985
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600986 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 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100994
995 for (;;) {
Steven Whitehoused564053f2013-01-11 10:49:34 +0000996 error = gfs2_lock_fs_check_clean(sdp, &sdp->sd_freeze_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100997 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 }
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001013 error = 0;
1014out:
1015 mutex_unlock(&sdp->sd_freeze_mutex);
1016 return error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001017}
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{
Steven Whitehoused564053f2013-01-11 10:49:34 +00001027 struct gfs2_sbd *sdp = sb->s_fs_info;
1028
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001029 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
Steven Whitehoused564053f2013-01-11 10:49:34 +00001036 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001037 mutex_unlock(&sdp->sd_freeze_mutex);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001038 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{
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001074 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
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001086 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
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001128 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{
1174 struct super_block *sb = dentry->d_inode->i_sb;
1175 struct gfs2_sbd *sdp = sb->s_fs_info;
1176 struct gfs2_statfs_change_host sc;
1177 int error;
1178
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001179 error = gfs2_rindex_update(sdp);
1180 if (error)
1181 return error;
1182
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001183 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
Theodore Ts'o02b99842014-03-13 10:14:33 -04001219 sync_filesystem(sb);
1220
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001221 spin_lock(&gt->gt_spin);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001222 args.ar_commit = gt->gt_logd_secs;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001223 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;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001228 spin_unlock(&gt->gt_spin);
Steven Whitehousef55073f2009-09-28 10:30:49 +01001229 error = gfs2_mount_args(&args, data);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001230 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) ||
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001241 args_neq(&args, &sdp->sd_args, localflocks) ||
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001242 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;
Christoph Hellwigf25934c2009-10-30 08:03:27 +01001262 if (sdp->sd_args.ar_nobarrier)
1263 set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
1264 else
1265 clear_bit(SDF_NOBARRIERS, &sdp->sd_flags);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001266 spin_lock(&gt->gt_spin);
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001267 gt->gt_logd_secs = args.ar_commit;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001268 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 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001277 spin_unlock(&gt->gt_spin);
1278
Steven Whitehouse8633ecf2009-07-31 11:07:29 +01001279 gfs2_online_uevent(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001280 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
Al Viro45321ac2010-06-07 13:43:19 -04001298static int gfs2_drop_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001299{
1300 struct gfs2_inode *ip = GFS2_I(inode);
1301
Abhi Das05978802014-03-31 10:33:17 -05001302 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && inode->i_nlink) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001303 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 }
Al Viro45321ac2010-06-07 13:43:19 -04001307 return generic_drop_inode(inode);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001308}
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
Al Viro34c80b12011-12-08 21:32:45 -05001323 * @root: root of this (sub)tree
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001324 *
1325 * Returns: 0 on success or error code
1326 */
1327
Al Viro34c80b12011-12-08 21:32:45 -05001328static int gfs2_show_options(struct seq_file *s, struct dentry *root)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001329{
Al Viro34c80b12011-12-08 21:32:45 -05001330 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001331 struct gfs2_args *args = &sdp->sd_args;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001332 int val;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001333
Al Viro34c80b12011-12-08 21:32:45 -05001334 if (is_ancestor(root, sdp->sd_master_dir))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001335 seq_puts(s, ",meta");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001336 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)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001343 seq_puts(s, ",spectator");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001344 if (args->ar_localflocks)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001345 seq_puts(s, ",localflocks");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001346 if (args->ar_debug)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001347 seq_puts(s, ",debug");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001348 if (args->ar_posix_acl)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001349 seq_puts(s, ",acl");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001350 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)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001369 seq_puts(s, ",suiddir");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001370 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)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001386 seq_puts(s, ",discard");
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001387 val = sdp->sd_tune.gt_logd_secs;
1388 if (val != 30)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001389 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);
Steven Whitehouse2b9731e2012-08-20 17:07:49 +01001393 else if (sdp->sd_tune.gt_statfs_slow)
1394 seq_puts(s, ",statfs_quantum=0");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001395 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);
Bob Petersond34843d2009-08-24 10:44:18 +01001400 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 }
Steven Whitehousecdcfde62009-10-30 10:48:53 +00001416 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001417 seq_puts(s, ",nobarrier");
Steven Whitehouse913a71d2010-05-06 11:03:29 +01001418 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001419 seq_puts(s, ",demote_interface_used");
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001420 if (args->ar_rgrplvb)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001421 seq_puts(s, ",rgrplvb");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001422 return 0;
1423}
1424
Steven Whitehousef42ab082011-04-14 16:50:31 +01001425static 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);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001442 struct gfs2_rgrpd *rgd;
Bob Peterson564e12b2011-11-21 13:36:17 -05001443 struct gfs2_holder gh;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001444 int error;
1445
1446 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001447 gfs2_consist_inode(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001448 return -EIO;
1449 }
1450
Bob Peterson8e2e0042012-07-19 08:12:40 -04001451 error = gfs2_rindex_update(sdp);
1452 if (error)
1453 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001454
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001455 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001456 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001457 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001458
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001459 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001460 if (!rgd) {
1461 gfs2_consist_inode(ip);
1462 error = -EIO;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001463 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001464 }
1465
Bob Peterson564e12b2011-11-21 13:36:17 -05001466 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001467 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001468 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001469
Steven Whitehouse4667a0e2011-04-18 14:18:09 +01001470 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1471 sdp->sd_jdesc->jd_blocks);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001472 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:
Bob Peterson564e12b2011-11-21 13:36:17 -05001482 gfs2_glock_dq_uninit(&gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001483out_qs:
1484 gfs2_quota_unhold(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001485 return error;
1486}
1487
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001488/**
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 *
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001502 * 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
Al Virod5c15152010-06-07 11:05:19 -04001509static void gfs2_evict_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001510{
Steven Whitehouse001e8e82011-03-30 14:17:51 +01001511 struct super_block *sb = inode->i_sb;
1512 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001513 struct gfs2_inode *ip = GFS2_I(inode);
1514 struct gfs2_holder gh;
1515 int error;
1516
Abhi Das05978802014-03-31 10:33:17 -05001517 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1518 clear_inode(inode);
1519 return;
1520 }
1521
Steven Whitehouse001e8e82011-03-30 14:17:51 +01001522 if (inode->i_nlink || (sb->s_flags & MS_RDONLY))
Al Virod5c15152010-06-07 11:05:19 -04001523 goto out;
1524
Bob Peterson44ad37d2011-03-17 16:19:58 -04001525 /* 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);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001527 if (unlikely(error)) {
Bob Petersona6a4d982013-05-29 11:51:52 -04001528 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001529 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1530 goto out;
1531 }
1532
Steven Whitehouse40ac2182011-08-02 13:17:27 +01001533 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 }
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001538
Bob Peterson44ad37d2011-03-17 16:19:58 -04001539 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1540 error = gfs2_inode_refresh(ip);
1541 if (error)
1542 goto out_truncate;
1543 }
1544
Benjamin Marzinski23c30102011-01-14 22:39:16 -06001545 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001546 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
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001552 /* Case 1 starts here */
1553
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001554 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);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001574 goto out_unlock;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001575
1576out_truncate:
Benjamin Marzinski24972552014-05-01 22:26:55 -05001577 gfs2_log_flush(sdp, ip->i_gl, NORMAL_FLUSH);
Benjamin Marzinski2216db72012-09-20 09:52:58 -05001578 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 }
Steven Whitehouse40ac2182011-08-02 13:17:27 +01001583 write_inode_now(inode, 1);
Steven Whitehouseb5b24d72011-09-07 10:33:25 +01001584 gfs2_ail_flush(ip->i_gl, 0);
Steven Whitehouse40ac2182011-08-02 13:17:27 +01001585
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001586 /* Case 2 starts here */
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001587 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1588 if (error)
1589 goto out_unlock;
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001590 /* Needs to be done before glock release & also in a transaction */
1591 truncate_inode_pages(&inode->i_data, 0);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001592 gfs2_trans_end(sdp);
1593
1594out_unlock:
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001595 /* Error path for case 1 */
Bob Peterson8e2e0042012-07-19 08:12:40 -04001596 if (gfs2_rs_active(ip->i_res))
Bob Peterson20095212013-03-13 10:26:38 -04001597 gfs2_rs_deltree(ip->i_res);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001598
Bob Petersona6a4d982013-05-29 11:51:52 -04001599 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1600 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001601 gfs2_glock_dq(&ip->i_iopen_gh);
Bob Petersona6a4d982013-05-29 11:51:52 -04001602 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001603 gfs2_holder_uninit(&ip->i_iopen_gh);
1604 gfs2_glock_dq_uninit(&gh);
1605 if (error && error != GLR_TRYFAILED && error != -EROFS)
Al Virod5c15152010-06-07 11:05:19 -04001606 fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001607out:
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001608 /* Case 3 starts here */
Johannes Weiner91b0abe2014-04-03 14:47:49 -07001609 truncate_inode_pages_final(&inode->i_data);
Steven Whitehouseaf5c2692013-09-27 12:49:33 +01001610 gfs2_rs_delete(ip, NULL);
Steven Whitehouse45138992013-01-28 09:30:07 +00001611 gfs2_ordered_del_inode(ip);
Jan Karadbd57682012-05-03 14:48:02 +02001612 clear_inode(inode);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001613 gfs2_dir_hash_inval(ip);
Al Virod5c15152010-06-07 11:05:19 -04001614 ip->i_gl->gl_object = NULL;
Tejun Heo43829732012-08-20 14:51:24 -07001615 flush_delayed_work(&ip->i_gl->gl_work);
Steven Whitehouse29687a22011-03-30 16:33:25 +01001616 gfs2_glock_add_to_lru(ip->i_gl);
Al Virod5c15152010-06-07 11:05:19 -04001617 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;
Bob Petersona6a4d982013-05-29 11:51:52 -04001621 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Al Virod5c15152010-06-07 11:05:19 -04001622 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1623 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001624}
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;
Steven Whitehouse54335b12011-09-01 13:31:59 +01001634 ip->i_rgd = NULL;
Bob Peterson0a305e42012-06-06 11:17:59 +01001635 ip->i_res = NULL;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001636 }
1637 return &ip->i_inode;
1638}
1639
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001640static void gfs2_i_callback(struct rcu_head *head)
1641{
1642 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001643 kmem_cache_free(gfs2_inode_cachep, inode);
1644}
1645
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001646static void gfs2_destroy_inode(struct inode *inode)
1647{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001648 call_rcu(&inode->i_rcu, gfs2_i_callback);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001649}
1650
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,
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001655 .dirty_inode = gfs2_dirty_inode,
Al Virod5c15152010-06-07 11:05:19 -04001656 .evict_inode = gfs2_evict_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001657 .put_super = gfs2_put_super,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001658 .sync_fs = gfs2_sync_fs,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001659 .freeze_super = gfs2_freeze,
1660 .thaw_super = gfs2_unfreeze,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001661 .statfs = gfs2_statfs,
1662 .remount_fs = gfs2_remount_fs,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001663 .drop_inode = gfs2_drop_inode,
1664 .show_options = gfs2_show_options,
1665};
1666