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