1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
49 SHOULD_NOT_DELETE_DINODE,
50 SHOULD_DEFER_EVICTION,
54 * gfs2_jindex_free - Clear all the journal index information
55 * @sdp: The GFS2 superblock
59 void gfs2_jindex_free(struct gfs2_sbd *sdp)
61 struct list_head list;
62 struct gfs2_jdesc *jd;
64 spin_lock(&sdp->sd_jindex_spin);
65 list_add(&list, &sdp->sd_jindex_list);
66 list_del_init(&sdp->sd_jindex_list);
68 spin_unlock(&sdp->sd_jindex_spin);
71 while (!list_empty(&list)) {
72 jd = list_first_entry(&list, struct gfs2_jdesc, jd_list);
73 gfs2_free_journal_extents(jd);
74 list_del(&jd->jd_list);
81 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
83 struct gfs2_jdesc *jd;
85 list_for_each_entry(jd, head, jd_list) {
86 if (jd->jd_jid == jid)
92 struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
94 struct gfs2_jdesc *jd;
96 spin_lock(&sdp->sd_jindex_spin);
97 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
98 spin_unlock(&sdp->sd_jindex_spin);
103 int gfs2_jdesc_check(struct gfs2_jdesc *jd)
105 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
106 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
107 u64 size = i_size_read(jd->jd_inode);
109 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
112 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
114 if (gfs2_write_alloc_required(ip, 0, size)) {
115 gfs2_consist_inode(ip);
123 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
124 * @sdp: the filesystem
129 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
131 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
132 struct gfs2_glock *j_gl = ip->i_gl;
133 struct gfs2_log_header_host head;
136 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
137 if (gfs2_withdrawn(sdp))
140 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
146 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
151 /* Initialize some head of the log stuff */
152 sdp->sd_log_sequence = head.lh_sequence + 1;
153 gfs2_log_pointers_init(sdp, head.lh_blkno);
155 error = gfs2_quota_init(sdp);
156 if (!error && gfs2_withdrawn(sdp))
159 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
163 void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
165 const struct gfs2_statfs_change *str = buf;
167 sc->sc_total = be64_to_cpu(str->sc_total);
168 sc->sc_free = be64_to_cpu(str->sc_free);
169 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
172 void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
174 struct gfs2_statfs_change *str = buf;
176 str->sc_total = cpu_to_be64(sc->sc_total);
177 str->sc_free = cpu_to_be64(sc->sc_free);
178 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
181 int gfs2_statfs_init(struct gfs2_sbd *sdp)
183 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
184 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
185 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
186 struct buffer_head *m_bh;
187 struct gfs2_holder gh;
190 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
195 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
199 if (sdp->sd_args.ar_spectator) {
200 spin_lock(&sdp->sd_statfs_spin);
201 gfs2_statfs_change_in(m_sc, m_bh->b_data +
202 sizeof(struct gfs2_dinode));
203 spin_unlock(&sdp->sd_statfs_spin);
205 spin_lock(&sdp->sd_statfs_spin);
206 gfs2_statfs_change_in(m_sc, m_bh->b_data +
207 sizeof(struct gfs2_dinode));
208 gfs2_statfs_change_in(l_sc, sdp->sd_sc_bh->b_data +
209 sizeof(struct gfs2_dinode));
210 spin_unlock(&sdp->sd_statfs_spin);
216 gfs2_glock_dq_uninit(&gh);
220 void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
223 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
224 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
225 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
229 gfs2_trans_add_meta(l_ip->i_gl, sdp->sd_sc_bh);
231 spin_lock(&sdp->sd_statfs_spin);
232 l_sc->sc_total += total;
233 l_sc->sc_free += free;
234 l_sc->sc_dinodes += dinodes;
235 gfs2_statfs_change_out(l_sc, sdp->sd_sc_bh->b_data +
236 sizeof(struct gfs2_dinode));
237 if (sdp->sd_args.ar_statfs_percent) {
238 x = 100 * l_sc->sc_free;
239 y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
240 if (x >= y || x <= -y)
243 spin_unlock(&sdp->sd_statfs_spin);
246 gfs2_wake_up_statfs(sdp);
249 void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh)
251 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
252 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
253 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
254 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
256 gfs2_trans_add_meta(l_ip->i_gl, sdp->sd_sc_bh);
257 gfs2_trans_add_meta(m_ip->i_gl, m_bh);
259 spin_lock(&sdp->sd_statfs_spin);
260 m_sc->sc_total += l_sc->sc_total;
261 m_sc->sc_free += l_sc->sc_free;
262 m_sc->sc_dinodes += l_sc->sc_dinodes;
263 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
264 memset(sdp->sd_sc_bh->b_data + sizeof(struct gfs2_dinode),
265 0, sizeof(struct gfs2_statfs_change));
266 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
267 spin_unlock(&sdp->sd_statfs_spin);
270 int gfs2_statfs_sync(struct super_block *sb, int type)
272 struct gfs2_sbd *sdp = sb->s_fs_info;
273 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
274 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
275 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
276 struct gfs2_holder gh;
277 struct buffer_head *m_bh;
280 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
285 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
289 spin_lock(&sdp->sd_statfs_spin);
290 gfs2_statfs_change_in(m_sc, m_bh->b_data +
291 sizeof(struct gfs2_dinode));
292 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
293 spin_unlock(&sdp->sd_statfs_spin);
296 spin_unlock(&sdp->sd_statfs_spin);
298 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
302 update_statfs(sdp, m_bh);
303 sdp->sd_statfs_force_sync = 0;
310 gfs2_glock_dq_uninit(&gh);
316 struct list_head list;
317 struct gfs2_holder gh;
321 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
323 * @sdp: the file system
328 static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
330 struct gfs2_inode *ip;
331 struct gfs2_jdesc *jd;
334 struct gfs2_log_header_host lh;
337 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
338 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
343 ip = GFS2_I(jd->jd_inode);
344 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
349 list_add(&lfcc->list, &list);
352 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
353 LM_FLAG_NOEXP | GL_NOPID,
358 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
359 error = gfs2_jdesc_check(jd);
362 error = gfs2_find_jhead(jd, &lh, false);
365 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
372 gfs2_freeze_unlock(&sdp->sd_freeze_gh);
375 while (!list_empty(&list)) {
376 lfcc = list_first_entry(&list, struct lfcc, list);
377 list_del(&lfcc->list);
378 gfs2_glock_dq_uninit(&lfcc->gh);
384 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
386 const struct inode *inode = &ip->i_inode;
387 struct gfs2_dinode *str = buf;
389 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
390 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
391 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
392 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
393 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
394 str->di_mode = cpu_to_be32(inode->i_mode);
395 str->di_uid = cpu_to_be32(i_uid_read(inode));
396 str->di_gid = cpu_to_be32(i_gid_read(inode));
397 str->di_nlink = cpu_to_be32(inode->i_nlink);
398 str->di_size = cpu_to_be64(i_size_read(inode));
399 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(inode));
400 str->di_atime = cpu_to_be64(inode->i_atime.tv_sec);
401 str->di_mtime = cpu_to_be64(inode->i_mtime.tv_sec);
402 str->di_ctime = cpu_to_be64(inode->i_ctime.tv_sec);
404 str->di_goal_meta = cpu_to_be64(ip->i_goal);
405 str->di_goal_data = cpu_to_be64(ip->i_goal);
406 str->di_generation = cpu_to_be64(ip->i_generation);
408 str->di_flags = cpu_to_be32(ip->i_diskflags);
409 str->di_height = cpu_to_be16(ip->i_height);
410 str->di_payload_format = cpu_to_be32(S_ISDIR(inode->i_mode) &&
411 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
413 str->di_depth = cpu_to_be16(ip->i_depth);
414 str->di_entries = cpu_to_be32(ip->i_entries);
416 str->di_eattr = cpu_to_be64(ip->i_eattr);
417 str->di_atime_nsec = cpu_to_be32(inode->i_atime.tv_nsec);
418 str->di_mtime_nsec = cpu_to_be32(inode->i_mtime.tv_nsec);
419 str->di_ctime_nsec = cpu_to_be32(inode->i_ctime.tv_nsec);
423 * gfs2_write_inode - Make sure the inode is stable on the disk
425 * @wbc: The writeback control structure
430 static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
432 struct gfs2_inode *ip = GFS2_I(inode);
433 struct gfs2_sbd *sdp = GFS2_SB(inode);
434 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
435 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
437 bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
440 gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
441 GFS2_LOG_HEAD_FLUSH_NORMAL |
442 GFS2_LFC_WRITE_INODE);
443 if (bdi->wb.dirty_exceeded)
444 gfs2_ail1_flush(sdp, wbc);
446 filemap_fdatawrite(metamapping);
448 ret = filemap_fdatawait(metamapping);
450 mark_inode_dirty_sync(inode);
452 spin_lock(&inode->i_lock);
453 if (!(inode->i_flags & I_DIRTY))
454 gfs2_ordered_del_inode(ip);
455 spin_unlock(&inode->i_lock);
461 * gfs2_dirty_inode - check for atime updates
462 * @inode: The inode in question
463 * @flags: The type of dirty
465 * Unfortunately it can be called under any combination of inode
466 * glock and transaction lock, so we have to check carefully.
468 * At the moment this deals only with atime - it should be possible
469 * to expand that role in future, once a review of the locking has
473 static void gfs2_dirty_inode(struct inode *inode, int flags)
475 struct gfs2_inode *ip = GFS2_I(inode);
476 struct gfs2_sbd *sdp = GFS2_SB(inode);
477 struct buffer_head *bh;
478 struct gfs2_holder gh;
480 int need_endtrans = 0;
483 if (unlikely(gfs2_withdrawn(sdp)))
485 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
486 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
488 fs_err(sdp, "dirty_inode: glock %d\n", ret);
489 gfs2_dump_glock(NULL, ip->i_gl, true);
493 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
496 if (current->journal_info == NULL) {
497 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
499 fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
505 ret = gfs2_meta_inode_buffer(ip, &bh);
507 gfs2_trans_add_meta(ip->i_gl, bh);
508 gfs2_dinode_out(ip, bh->b_data);
516 gfs2_glock_dq_uninit(&gh);
520 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
521 * @sdp: the filesystem
526 void gfs2_make_fs_ro(struct gfs2_sbd *sdp)
528 int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
530 gfs2_flush_delete_work(sdp);
531 if (!log_write_allowed && current == sdp->sd_quotad_process)
532 fs_warn(sdp, "The quotad daemon is withdrawing.\n");
533 else if (sdp->sd_quotad_process)
534 kthread_stop(sdp->sd_quotad_process);
535 sdp->sd_quotad_process = NULL;
537 if (!log_write_allowed && current == sdp->sd_logd_process)
538 fs_warn(sdp, "The logd daemon is withdrawing.\n");
539 else if (sdp->sd_logd_process)
540 kthread_stop(sdp->sd_logd_process);
541 sdp->sd_logd_process = NULL;
543 if (log_write_allowed) {
544 gfs2_quota_sync(sdp->sd_vfs, 0);
545 gfs2_statfs_sync(sdp->sd_vfs, 0);
547 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
548 GFS2_LFC_MAKE_FS_RO);
549 wait_event_timeout(sdp->sd_log_waitq,
550 gfs2_log_is_empty(sdp),
552 gfs2_assert_warn(sdp, gfs2_log_is_empty(sdp));
554 wait_event_timeout(sdp->sd_log_waitq,
555 gfs2_log_is_empty(sdp),
558 gfs2_quota_cleanup(sdp);
560 if (!log_write_allowed)
561 sdp->sd_vfs->s_flags |= SB_RDONLY;
565 * gfs2_put_super - Unmount the filesystem
566 * @sb: The VFS superblock
570 static void gfs2_put_super(struct super_block *sb)
572 struct gfs2_sbd *sdp = sb->s_fs_info;
573 struct gfs2_jdesc *jd;
575 /* No more recovery requests */
576 set_bit(SDF_NORECOVERY, &sdp->sd_flags);
579 /* Wait on outstanding recovery */
581 spin_lock(&sdp->sd_jindex_spin);
582 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
583 if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
585 spin_unlock(&sdp->sd_jindex_spin);
586 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
587 TASK_UNINTERRUPTIBLE);
590 spin_unlock(&sdp->sd_jindex_spin);
592 if (!sb_rdonly(sb)) {
593 gfs2_make_fs_ro(sdp);
595 WARN_ON(gfs2_withdrawing(sdp));
597 /* At this point, we're through modifying the disk */
601 iput(sdp->sd_jindex);
602 iput(sdp->sd_statfs_inode);
603 iput(sdp->sd_rindex);
604 iput(sdp->sd_quota_inode);
606 gfs2_glock_put(sdp->sd_rename_gl);
607 gfs2_glock_put(sdp->sd_freeze_gl);
609 if (!sdp->sd_args.ar_spectator) {
610 if (gfs2_holder_initialized(&sdp->sd_journal_gh))
611 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
612 if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
613 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
614 brelse(sdp->sd_sc_bh);
615 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
616 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
617 free_local_statfs_inodes(sdp);
618 iput(sdp->sd_qc_inode);
621 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
622 gfs2_clear_rgrpd(sdp);
623 gfs2_jindex_free(sdp);
624 /* Take apart glock structures and buffer lists */
625 gfs2_gl_hash_clear(sdp);
626 truncate_inode_pages_final(&sdp->sd_aspace);
627 gfs2_delete_debugfs_file(sdp);
628 /* Unmount the locking protocol */
629 gfs2_lm_unmount(sdp);
631 /* At this point, we're through participating in the lockspace */
632 gfs2_sys_fs_del(sdp);
637 * gfs2_sync_fs - sync the filesystem
638 * @sb: the superblock
639 * @wait: true to wait for completion
641 * Flushes the log to disk.
644 static int gfs2_sync_fs(struct super_block *sb, int wait)
646 struct gfs2_sbd *sdp = sb->s_fs_info;
648 gfs2_quota_sync(sb, -1);
650 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
652 return sdp->sd_log_error;
655 void gfs2_freeze_func(struct work_struct *work)
658 struct gfs2_holder freeze_gh;
659 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
660 struct super_block *sb = sdp->sd_vfs;
662 atomic_inc(&sb->s_active);
663 error = gfs2_freeze_lock(sdp, &freeze_gh, 0);
665 gfs2_assert_withdraw(sdp, 0);
667 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
668 error = thaw_super(sb);
670 fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n",
672 gfs2_assert_withdraw(sdp, 0);
674 gfs2_freeze_unlock(&freeze_gh);
676 deactivate_super(sb);
677 clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags);
678 wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN);
683 * gfs2_freeze - prevent further writes to the filesystem
684 * @sb: the VFS structure for the filesystem
688 static int gfs2_freeze(struct super_block *sb)
690 struct gfs2_sbd *sdp = sb->s_fs_info;
693 mutex_lock(&sdp->sd_freeze_mutex);
694 if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN) {
700 if (gfs2_withdrawn(sdp)) {
705 error = gfs2_lock_fs_check_clean(sdp);
710 fs_err(sdp, "waiting for recovery before freeze\n");
711 else if (error == -EIO) {
712 fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
713 "to recovery error.\n");
716 fs_err(sdp, "error freezing FS: %d\n", error);
718 fs_err(sdp, "retrying...\n");
721 set_bit(SDF_FS_FROZEN, &sdp->sd_flags);
723 mutex_unlock(&sdp->sd_freeze_mutex);
728 * gfs2_unfreeze - reallow writes to the filesystem
729 * @sb: the VFS structure for the filesystem
733 static int gfs2_unfreeze(struct super_block *sb)
735 struct gfs2_sbd *sdp = sb->s_fs_info;
737 mutex_lock(&sdp->sd_freeze_mutex);
738 if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
739 !gfs2_holder_initialized(&sdp->sd_freeze_gh)) {
740 mutex_unlock(&sdp->sd_freeze_mutex);
744 gfs2_freeze_unlock(&sdp->sd_freeze_gh);
745 mutex_unlock(&sdp->sd_freeze_mutex);
746 return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE);
750 * statfs_slow_fill - fill in the sg for a given RG
752 * @sc: the sc structure
754 * Returns: 0 on success, -ESTALE if the LVB is invalid
757 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
758 struct gfs2_statfs_change_host *sc)
760 gfs2_rgrp_verify(rgd);
761 sc->sc_total += rgd->rd_data;
762 sc->sc_free += rgd->rd_free;
763 sc->sc_dinodes += rgd->rd_dinodes;
768 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
769 * @sdp: the filesystem
770 * @sc: the sc info that will be returned
772 * Any error (other than a signal) will cause this routine to fall back
773 * to the synchronous version.
775 * FIXME: This really shouldn't busy wait like this.
780 static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
782 struct gfs2_rgrpd *rgd_next;
783 struct gfs2_holder *gha, *gh;
784 unsigned int slots = 64;
789 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
790 gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
793 for (x = 0; x < slots; x++)
794 gfs2_holder_mark_uninitialized(gha + x);
796 rgd_next = gfs2_rgrpd_get_first(sdp);
801 for (x = 0; x < slots; x++) {
804 if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
805 err = gfs2_glock_wait(gh);
807 gfs2_holder_uninit(gh);
811 struct gfs2_rgrpd *rgd =
812 gfs2_glock2rgrp(gh->gh_gl);
814 error = statfs_slow_fill(rgd, sc);
816 gfs2_glock_dq_uninit(gh);
820 if (gfs2_holder_initialized(gh))
822 else if (rgd_next && !error) {
823 error = gfs2_glock_nq_init(rgd_next->rd_gl,
827 rgd_next = gfs2_rgrpd_get_next(rgd_next);
831 if (signal_pending(current))
832 error = -ERESTARTSYS;
846 * gfs2_statfs_i - Do a statfs
847 * @sdp: the filesystem
848 * @sc: the sc structure
853 static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
855 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
856 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
858 spin_lock(&sdp->sd_statfs_spin);
861 sc->sc_total += l_sc->sc_total;
862 sc->sc_free += l_sc->sc_free;
863 sc->sc_dinodes += l_sc->sc_dinodes;
865 spin_unlock(&sdp->sd_statfs_spin);
869 if (sc->sc_free > sc->sc_total)
870 sc->sc_free = sc->sc_total;
871 if (sc->sc_dinodes < 0)
878 * gfs2_statfs - Gather and return stats about the filesystem
879 * @dentry: The name of the link
882 * Returns: 0 on success or error code
885 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
887 struct super_block *sb = dentry->d_sb;
888 struct gfs2_sbd *sdp = sb->s_fs_info;
889 struct gfs2_statfs_change_host sc;
892 error = gfs2_rindex_update(sdp);
896 if (gfs2_tune_get(sdp, gt_statfs_slow))
897 error = gfs2_statfs_slow(sdp, &sc);
899 error = gfs2_statfs_i(sdp, &sc);
904 buf->f_type = GFS2_MAGIC;
905 buf->f_bsize = sdp->sd_sb.sb_bsize;
906 buf->f_blocks = sc.sc_total;
907 buf->f_bfree = sc.sc_free;
908 buf->f_bavail = sc.sc_free;
909 buf->f_files = sc.sc_dinodes + sc.sc_free;
910 buf->f_ffree = sc.sc_free;
911 buf->f_namelen = GFS2_FNAMESIZE;
917 * gfs2_drop_inode - Drop an inode (test for remote unlink)
918 * @inode: The inode to drop
920 * If we've received a callback on an iopen lock then it's because a
921 * remote node tried to deallocate the inode but failed due to this node
922 * still having the inode open. Here we mark the link count zero
923 * since we know that it must have reached zero if the GLF_DEMOTE flag
924 * is set on the iopen glock. If we didn't do a disk read since the
925 * remote node removed the final link then we might otherwise miss
926 * this event. This check ensures that this node will deallocate the
927 * inode's blocks, or alternatively pass the baton on to another
928 * node for later deallocation.
931 static int gfs2_drop_inode(struct inode *inode)
933 struct gfs2_inode *ip = GFS2_I(inode);
935 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) &&
937 gfs2_holder_initialized(&ip->i_iopen_gh)) {
938 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
939 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
944 * When under memory pressure when an inode's link count has dropped to
945 * zero, defer deleting the inode to the delete workqueue. This avoids
946 * calling into DLM under memory pressure, which can deadlock.
948 if (!inode->i_nlink &&
949 unlikely(current->flags & PF_MEMALLOC) &&
950 gfs2_holder_initialized(&ip->i_iopen_gh)) {
951 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
954 if (!gfs2_queue_delete_work(gl, 0))
955 gfs2_glock_queue_put(gl);
959 return generic_drop_inode(inode);
962 static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
968 } while (!IS_ROOT(d1));
973 * gfs2_show_options - Show mount options for /proc/mounts
974 * @s: seq_file structure
975 * @root: root of this (sub)tree
977 * Returns: 0 on success or error code
980 static int gfs2_show_options(struct seq_file *s, struct dentry *root)
982 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
983 struct gfs2_args *args = &sdp->sd_args;
984 unsigned int logd_secs, statfs_slow, statfs_quantum, quota_quantum;
986 spin_lock(&sdp->sd_tune.gt_spin);
987 logd_secs = sdp->sd_tune.gt_logd_secs;
988 quota_quantum = sdp->sd_tune.gt_quota_quantum;
989 statfs_quantum = sdp->sd_tune.gt_statfs_quantum;
990 statfs_slow = sdp->sd_tune.gt_statfs_slow;
991 spin_unlock(&sdp->sd_tune.gt_spin);
993 if (is_ancestor(root, sdp->sd_master_dir))
994 seq_puts(s, ",meta");
995 if (args->ar_lockproto[0])
996 seq_show_option(s, "lockproto", args->ar_lockproto);
997 if (args->ar_locktable[0])
998 seq_show_option(s, "locktable", args->ar_locktable);
999 if (args->ar_hostdata[0])
1000 seq_show_option(s, "hostdata", args->ar_hostdata);
1001 if (args->ar_spectator)
1002 seq_puts(s, ",spectator");
1003 if (args->ar_localflocks)
1004 seq_puts(s, ",localflocks");
1006 seq_puts(s, ",debug");
1007 if (args->ar_posix_acl)
1008 seq_puts(s, ",acl");
1009 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1011 switch (args->ar_quota) {
1012 case GFS2_QUOTA_OFF:
1015 case GFS2_QUOTA_ACCOUNT:
1025 seq_printf(s, ",quota=%s", state);
1027 if (args->ar_suiddir)
1028 seq_puts(s, ",suiddir");
1029 if (args->ar_data != GFS2_DATA_DEFAULT) {
1031 switch (args->ar_data) {
1032 case GFS2_DATA_WRITEBACK:
1033 state = "writeback";
1035 case GFS2_DATA_ORDERED:
1042 seq_printf(s, ",data=%s", state);
1044 if (args->ar_discard)
1045 seq_puts(s, ",discard");
1046 if (logd_secs != 30)
1047 seq_printf(s, ",commit=%d", logd_secs);
1048 if (statfs_quantum != 30)
1049 seq_printf(s, ",statfs_quantum=%d", statfs_quantum);
1050 else if (statfs_slow)
1051 seq_puts(s, ",statfs_quantum=0");
1052 if (quota_quantum != 60)
1053 seq_printf(s, ",quota_quantum=%d", quota_quantum);
1054 if (args->ar_statfs_percent)
1055 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
1056 if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1059 switch (args->ar_errors) {
1060 case GFS2_ERRORS_WITHDRAW:
1063 case GFS2_ERRORS_PANIC:
1070 seq_printf(s, ",errors=%s", state);
1072 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
1073 seq_puts(s, ",nobarrier");
1074 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
1075 seq_puts(s, ",demote_interface_used");
1076 if (args->ar_rgrplvb)
1077 seq_puts(s, ",rgrplvb");
1078 if (args->ar_loccookie)
1079 seq_puts(s, ",loccookie");
1083 static void gfs2_final_release_pages(struct gfs2_inode *ip)
1085 struct inode *inode = &ip->i_inode;
1086 struct gfs2_glock *gl = ip->i_gl;
1088 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1089 truncate_inode_pages(&inode->i_data, 0);
1091 if (atomic_read(&gl->gl_revokes) == 0) {
1092 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1093 clear_bit(GLF_DIRTY, &gl->gl_flags);
1097 static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1099 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1100 struct gfs2_rgrpd *rgd;
1101 struct gfs2_holder gh;
1104 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
1105 gfs2_consist_inode(ip);
1109 error = gfs2_rindex_update(sdp);
1113 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1117 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1119 gfs2_consist_inode(ip);
1124 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
1125 LM_FLAG_NODE_SCOPE, &gh);
1129 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1130 sdp->sd_jdesc->jd_blocks);
1132 goto out_rg_gunlock;
1134 gfs2_free_di(rgd, ip);
1136 gfs2_final_release_pages(ip);
1138 gfs2_trans_end(sdp);
1141 gfs2_glock_dq_uninit(&gh);
1143 gfs2_quota_unhold(ip);
1148 * gfs2_glock_put_eventually
1149 * @gl: The glock to put
1151 * When under memory pressure, trigger a deferred glock put to make sure we
1152 * won't call into DLM and deadlock. Otherwise, put the glock directly.
1155 static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1157 if (current->flags & PF_MEMALLOC)
1158 gfs2_glock_queue_put(gl);
1163 static bool gfs2_upgrade_iopen_glock(struct inode *inode)
1165 struct gfs2_inode *ip = GFS2_I(inode);
1166 struct gfs2_sbd *sdp = GFS2_SB(inode);
1167 struct gfs2_holder *gh = &ip->i_iopen_gh;
1168 long timeout = 5 * HZ;
1171 gh->gh_flags |= GL_NOCACHE;
1172 gfs2_glock_dq_wait(gh);
1175 * If there are no other lock holders, we'll get the lock immediately.
1176 * Otherwise, the other nodes holding the lock will be notified about
1177 * our locking request. If they don't have the inode open, they'll
1178 * evict the cached inode and release the lock. Otherwise, if they
1179 * poke the inode glock, we'll take this as an indication that they
1180 * still need the iopen glock and that they'll take care of deleting
1181 * the inode when they're done. As a last resort, if another node
1182 * keeps holding the iopen glock without showing any activity on the
1183 * inode glock, we'll eventually time out.
1185 * Note that we're passing the LM_FLAG_TRY_1CB flag to the first
1186 * locking request as an optimization to notify lock holders as soon as
1187 * possible. Without that flag, they'd be notified implicitly by the
1188 * second locking request.
1191 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, gh);
1192 error = gfs2_glock_nq(gh);
1193 if (error != GLR_TRYFAILED)
1196 gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1197 error = gfs2_glock_nq(gh);
1201 timeout = wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
1202 !test_bit(HIF_WAIT, &gh->gh_iflags) ||
1203 test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags),
1205 if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1209 return gfs2_glock_holder_ready(gh) == 0;
1213 * evict_should_delete - determine whether the inode is eligible for deletion
1214 * @inode: The inode to evict
1215 * @gh: The glock holder structure
1217 * This function determines whether the evicted inode is eligible to be deleted
1218 * and locks the inode glock.
1220 * Returns: the fate of the dinode
1222 static enum dinode_demise evict_should_delete(struct inode *inode,
1223 struct gfs2_holder *gh)
1225 struct gfs2_inode *ip = GFS2_I(inode);
1226 struct super_block *sb = inode->i_sb;
1227 struct gfs2_sbd *sdp = sb->s_fs_info;
1230 if (test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1231 BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));
1235 if (test_bit(GIF_DEFERRED_DELETE, &ip->i_flags))
1236 return SHOULD_DEFER_EVICTION;
1238 /* Deletes should never happen under memory pressure anymore. */
1239 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1240 return SHOULD_DEFER_EVICTION;
1242 /* Must not read inode block until block type has been verified */
1243 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
1244 if (unlikely(ret)) {
1245 glock_clear_object(ip->i_iopen_gh.gh_gl, ip);
1246 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1247 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1248 return SHOULD_DEFER_EVICTION;
1251 if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
1252 return SHOULD_NOT_DELETE_DINODE;
1253 ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1255 return SHOULD_NOT_DELETE_DINODE;
1257 ret = gfs2_instantiate(gh);
1259 return SHOULD_NOT_DELETE_DINODE;
1262 * The inode may have been recreated in the meantime.
1265 return SHOULD_NOT_DELETE_DINODE;
1268 if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1269 test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1270 if (!gfs2_upgrade_iopen_glock(inode)) {
1271 gfs2_holder_uninit(&ip->i_iopen_gh);
1272 return SHOULD_NOT_DELETE_DINODE;
1275 return SHOULD_DELETE_DINODE;
1279 * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
1280 * @inode: The inode to evict
1282 static int evict_unlinked_inode(struct inode *inode)
1284 struct gfs2_inode *ip = GFS2_I(inode);
1287 if (S_ISDIR(inode->i_mode) &&
1288 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1289 ret = gfs2_dir_exhash_dealloc(ip);
1295 ret = gfs2_ea_dealloc(ip);
1300 if (!gfs2_is_stuffed(ip)) {
1301 ret = gfs2_file_dealloc(ip);
1306 /* We're about to clear the bitmap for the dinode, but as soon as we
1307 do, gfs2_create_inode can create another inode at the same block
1308 location and try to set gl_object again. We clear gl_object here so
1309 that subsequent inode creates don't see an old gl_object. */
1310 glock_clear_object(ip->i_gl, ip);
1311 ret = gfs2_dinode_dealloc(ip);
1312 gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
1318 * evict_linked_inode - evict an inode whose dinode has not been unlinked
1319 * @inode: The inode to evict
1321 static int evict_linked_inode(struct inode *inode)
1323 struct super_block *sb = inode->i_sb;
1324 struct gfs2_sbd *sdp = sb->s_fs_info;
1325 struct gfs2_inode *ip = GFS2_I(inode);
1326 struct address_space *metamapping;
1329 gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1330 GFS2_LFC_EVICT_INODE);
1331 metamapping = gfs2_glock2aspace(ip->i_gl);
1332 if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1333 filemap_fdatawrite(metamapping);
1334 filemap_fdatawait(metamapping);
1336 write_inode_now(inode, 1);
1337 gfs2_ail_flush(ip->i_gl, 0);
1339 ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1343 /* Needs to be done before glock release & also in a transaction */
1344 truncate_inode_pages(&inode->i_data, 0);
1345 truncate_inode_pages(metamapping, 0);
1346 gfs2_trans_end(sdp);
1351 * gfs2_evict_inode - Remove an inode from cache
1352 * @inode: The inode to evict
1354 * There are three cases to consider:
1355 * 1. i_nlink == 0, we are final opener (and must deallocate)
1356 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1359 * If the fs is read only, then we have to treat all cases as per #3
1360 * since we are unable to do any deallocation. The inode will be
1361 * deallocated by the next read/write node to attempt an allocation
1362 * in the same resource group
1364 * We have to (at the moment) hold the inodes main lock to cover
1365 * the gap between unlocking the shared lock on the iopen lock and
1366 * taking the exclusive lock. I'd rather do a shared -> exclusive
1367 * conversion on the iopen lock, but we can change that later. This
1368 * is safe, just less efficient.
1371 static void gfs2_evict_inode(struct inode *inode)
1373 struct super_block *sb = inode->i_sb;
1374 struct gfs2_sbd *sdp = sb->s_fs_info;
1375 struct gfs2_inode *ip = GFS2_I(inode);
1376 struct gfs2_holder gh;
1379 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1384 if (inode->i_nlink || sb_rdonly(sb))
1388 * In case of an incomplete mount, gfs2_evict_inode() may be called for
1389 * system files without having an active journal to write to. In that
1390 * case, skip the filesystem evict.
1395 gfs2_holder_mark_uninitialized(&gh);
1396 ret = evict_should_delete(inode, &gh);
1397 if (ret == SHOULD_DEFER_EVICTION)
1399 if (ret == SHOULD_DELETE_DINODE)
1400 ret = evict_unlinked_inode(inode);
1402 ret = evict_linked_inode(inode);
1404 if (gfs2_rs_active(&ip->i_res))
1405 gfs2_rs_deltree(&ip->i_res);
1407 if (gfs2_holder_initialized(&gh)) {
1408 glock_clear_object(ip->i_gl, ip);
1409 gfs2_glock_dq_uninit(&gh);
1411 if (ret && ret != GLR_TRYFAILED && ret != -EROFS)
1412 fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
1414 truncate_inode_pages_final(&inode->i_data);
1416 gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
1417 gfs2_rs_deltree(&ip->i_res);
1418 gfs2_ordered_del_inode(ip);
1420 gfs2_dir_hash_inval(ip);
1421 if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
1422 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1424 glock_clear_object(gl, ip);
1425 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1426 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1427 gfs2_glock_dq(&ip->i_iopen_gh);
1429 gfs2_glock_hold(gl);
1430 gfs2_holder_uninit(&ip->i_iopen_gh);
1431 gfs2_glock_put_eventually(gl);
1434 glock_clear_object(ip->i_gl, ip);
1435 wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1436 gfs2_glock_add_to_lru(ip->i_gl);
1437 gfs2_glock_put_eventually(ip->i_gl);
1438 rcu_assign_pointer(ip->i_gl, NULL);
1442 static struct inode *gfs2_alloc_inode(struct super_block *sb)
1444 struct gfs2_inode *ip;
1446 ip = alloc_inode_sb(sb, gfs2_inode_cachep, GFP_KERNEL);
1451 gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
1452 memset(&ip->i_res, 0, sizeof(ip->i_res));
1453 RB_CLEAR_NODE(&ip->i_res.rs_node);
1455 return &ip->i_inode;
1458 static void gfs2_free_inode(struct inode *inode)
1460 kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
1463 extern void free_local_statfs_inodes(struct gfs2_sbd *sdp)
1465 struct local_statfs_inode *lsi, *safe;
1467 /* Run through the statfs inodes list to iput and free memory */
1468 list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
1469 if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
1470 sdp->sd_sc_inode = NULL; /* belongs to this node */
1471 if (lsi->si_sc_inode)
1472 iput(lsi->si_sc_inode);
1473 list_del(&lsi->si_list);
1478 extern struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
1481 struct local_statfs_inode *lsi;
1483 /* Return the local (per node) statfs inode in the
1484 * sdp->sd_sc_inodes_list corresponding to the 'index'. */
1485 list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
1486 if (lsi->si_jid == index)
1487 return lsi->si_sc_inode;
1492 const struct super_operations gfs2_super_ops = {
1493 .alloc_inode = gfs2_alloc_inode,
1494 .free_inode = gfs2_free_inode,
1495 .write_inode = gfs2_write_inode,
1496 .dirty_inode = gfs2_dirty_inode,
1497 .evict_inode = gfs2_evict_inode,
1498 .put_super = gfs2_put_super,
1499 .sync_fs = gfs2_sync_fs,
1500 .freeze_super = gfs2_freeze,
1501 .thaw_super = gfs2_unfreeze,
1502 .statfs = gfs2_statfs,
1503 .drop_inode = gfs2_drop_inode,
1504 .show_options = gfs2_show_options,