xref: /linux/fs/gfs2/super.c (revision a098f26d3b80e99740ea155a7012008d12a46010)
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 enum evict_behavior {
48 	EVICT_SHOULD_DELETE,
49 	EVICT_SHOULD_SKIP_DELETE,
50 	EVICT_SHOULD_DEFER_DELETE,
51 };
52 
53 /**
54  * gfs2_jindex_free - Clear all the journal index information
55  * @sdp: The GFS2 superblock
56  *
57  */
58 
59 void gfs2_jindex_free(struct gfs2_sbd *sdp)
60 {
61 	struct list_head list;
62 	struct gfs2_jdesc *jd;
63 
64 	spin_lock(&sdp->sd_jindex_spin);
65 	list_add(&list, &sdp->sd_jindex_list);
66 	list_del_init(&sdp->sd_jindex_list);
67 	sdp->sd_journals = 0;
68 	spin_unlock(&sdp->sd_jindex_spin);
69 
70 	down_write(&sdp->sd_log_flush_lock);
71 	sdp->sd_jdesc = NULL;
72 	up_write(&sdp->sd_log_flush_lock);
73 
74 	while (!list_empty(&list)) {
75 		jd = list_first_entry(&list, struct gfs2_jdesc, jd_list);
76 		BUG_ON(jd->jd_log_bio);
77 		gfs2_free_journal_extents(jd);
78 		list_del(&jd->jd_list);
79 		iput(jd->jd_inode);
80 		jd->jd_inode = NULL;
81 		kfree(jd);
82 	}
83 }
84 
85 static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
86 {
87 	struct gfs2_jdesc *jd;
88 
89 	list_for_each_entry(jd, head, jd_list) {
90 		if (jd->jd_jid == jid)
91 			return jd;
92 	}
93 	return NULL;
94 }
95 
96 struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
97 {
98 	struct gfs2_jdesc *jd;
99 
100 	spin_lock(&sdp->sd_jindex_spin);
101 	jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
102 	spin_unlock(&sdp->sd_jindex_spin);
103 
104 	return jd;
105 }
106 
107 int gfs2_jdesc_check(struct gfs2_jdesc *jd)
108 {
109 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
110 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
111 	u64 size = i_size_read(jd->jd_inode);
112 
113 	if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
114 		return -EIO;
115 
116 	jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
117 
118 	if (gfs2_write_alloc_required(ip, 0, size)) {
119 		gfs2_consist_inode(ip);
120 		return -EIO;
121 	}
122 
123 	return 0;
124 }
125 
126 /**
127  * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
128  * @sdp: the filesystem
129  *
130  * Returns: errno
131  */
132 
133 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
134 {
135 	struct gfs2_glock *j_gl = gfs2_inode_glock(sdp->sd_jdesc->jd_inode);
136 	int error;
137 
138 	j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
139 	if (gfs2_withdrawn(sdp))
140 		return -EIO;
141 
142 	if (sdp->sd_log_sequence == 0) {
143 		fs_err(sdp, "unknown status of our own journal jid %d",
144 		       sdp->sd_lockstruct.ls_jid);
145 		return -EIO;
146 	}
147 
148 	error = gfs2_quota_init(sdp);
149 	if (!error && gfs2_withdrawn(sdp)) {
150 		gfs2_quota_cleanup(sdp);
151 		error = -EIO;
152 	}
153 	if (!error)
154 		set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
155 	return error;
156 }
157 
158 void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
159 {
160 	const struct gfs2_statfs_change *str = buf;
161 
162 	sc->sc_total = be64_to_cpu(str->sc_total);
163 	sc->sc_free = be64_to_cpu(str->sc_free);
164 	sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
165 }
166 
167 void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
168 {
169 	struct gfs2_statfs_change *str = buf;
170 
171 	str->sc_total = cpu_to_be64(sc->sc_total);
172 	str->sc_free = cpu_to_be64(sc->sc_free);
173 	str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
174 }
175 
176 int gfs2_statfs_init(struct gfs2_sbd *sdp)
177 {
178 	struct gfs2_glock *gl = gfs2_inode_glock(sdp->sd_statfs_inode);
179 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
180 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
181 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
182 	struct buffer_head *m_bh;
183 	struct gfs2_holder gh;
184 	int error;
185 
186 	error = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
187 				   &gh);
188 	if (error)
189 		return error;
190 
191 	error = gfs2_meta_inode_buffer(m_ip, &m_bh);
192 	if (error)
193 		goto out;
194 
195 	if (sdp->sd_args.ar_spectator) {
196 		spin_lock(&sdp->sd_statfs_spin);
197 		gfs2_statfs_change_in(m_sc, m_bh->b_data +
198 				      sizeof(struct gfs2_dinode));
199 		spin_unlock(&sdp->sd_statfs_spin);
200 	} else {
201 		spin_lock(&sdp->sd_statfs_spin);
202 		gfs2_statfs_change_in(m_sc, m_bh->b_data +
203 				      sizeof(struct gfs2_dinode));
204 		gfs2_statfs_change_in(l_sc, sdp->sd_sc_bh->b_data +
205 				      sizeof(struct gfs2_dinode));
206 		spin_unlock(&sdp->sd_statfs_spin);
207 
208 	}
209 
210 	brelse(m_bh);
211 out:
212 	gfs2_glock_dq_uninit(&gh);
213 	return 0;
214 }
215 
216 void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
217 			s64 dinodes)
218 {
219 	struct gfs2_glock *gl = gfs2_inode_glock(sdp->sd_sc_inode);
220 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
221 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
222 	s64 x, y;
223 	int need_sync = 0;
224 
225 	gfs2_trans_add_meta(gl, sdp->sd_sc_bh);
226 
227 	spin_lock(&sdp->sd_statfs_spin);
228 	l_sc->sc_total += total;
229 	l_sc->sc_free += free;
230 	l_sc->sc_dinodes += dinodes;
231 	gfs2_statfs_change_out(l_sc, sdp->sd_sc_bh->b_data +
232 			       sizeof(struct gfs2_dinode));
233 	if (sdp->sd_args.ar_statfs_percent) {
234 		x = 100 * l_sc->sc_free;
235 		y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
236 		if (x >= y || x <= -y)
237 			need_sync = 1;
238 	}
239 	spin_unlock(&sdp->sd_statfs_spin);
240 
241 	if (need_sync)
242 		gfs2_wake_up_statfs(sdp);
243 }
244 
245 void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh)
246 {
247 	struct gfs2_glock *m_gl = gfs2_inode_glock(sdp->sd_statfs_inode);
248 	struct gfs2_glock *l_gl = gfs2_inode_glock(sdp->sd_sc_inode);
249 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
250 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
251 
252 	gfs2_trans_add_meta(l_gl, sdp->sd_sc_bh);
253 	gfs2_trans_add_meta(m_gl, m_bh);
254 
255 	spin_lock(&sdp->sd_statfs_spin);
256 	m_sc->sc_total += l_sc->sc_total;
257 	m_sc->sc_free += l_sc->sc_free;
258 	m_sc->sc_dinodes += l_sc->sc_dinodes;
259 	memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
260 	memset(sdp->sd_sc_bh->b_data + sizeof(struct gfs2_dinode),
261 	       0, sizeof(struct gfs2_statfs_change));
262 	gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
263 	spin_unlock(&sdp->sd_statfs_spin);
264 }
265 
266 int gfs2_statfs_sync(struct super_block *sb, int type)
267 {
268 	struct gfs2_sbd *sdp = sb->s_fs_info;
269 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
270 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
271 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
272 	struct gfs2_holder gh;
273 	struct buffer_head *m_bh;
274 	int error;
275 
276 	error = gfs2_glock_nq_init(gfs2_inode_glock(&m_ip->i_inode),
277 				   LM_ST_EXCLUSIVE, GL_NOCACHE, &gh);
278 	if (error)
279 		goto out;
280 
281 	error = gfs2_meta_inode_buffer(m_ip, &m_bh);
282 	if (error)
283 		goto out_unlock;
284 
285 	spin_lock(&sdp->sd_statfs_spin);
286 	gfs2_statfs_change_in(m_sc, m_bh->b_data +
287 			      sizeof(struct gfs2_dinode));
288 	if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
289 		spin_unlock(&sdp->sd_statfs_spin);
290 		goto out_bh;
291 	}
292 	spin_unlock(&sdp->sd_statfs_spin);
293 
294 	error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
295 	if (error)
296 		goto out_bh;
297 
298 	update_statfs(sdp, m_bh);
299 	sdp->sd_statfs_force_sync = 0;
300 
301 	gfs2_trans_end(sdp);
302 
303 out_bh:
304 	brelse(m_bh);
305 out_unlock:
306 	gfs2_glock_dq_uninit(&gh);
307 out:
308 	return error;
309 }
310 
311 struct lfcc {
312 	struct list_head list;
313 	struct gfs2_holder gh;
314 };
315 
316 /**
317  * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
318  *                            journals are clean
319  * @sdp: the file system
320  *
321  * Returns: errno
322  */
323 
324 static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
325 {
326 	struct gfs2_jdesc *jd;
327 	struct lfcc *lfcc;
328 	LIST_HEAD(list);
329 	struct gfs2_log_header_host lh;
330 	int error, error2;
331 
332 	/*
333 	 * Grab all the journal glocks in SH mode.  We are *probably* doing
334 	 * that to prevent recovery.
335 	 */
336 
337 	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
338 		struct gfs2_glock *gl = gfs2_inode_glock(jd->jd_inode);
339 
340 		lfcc = kmalloc_obj(struct lfcc);
341 		if (!lfcc) {
342 			error = -ENOMEM;
343 			goto out;
344 		}
345 		error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &lfcc->gh);
346 		if (error) {
347 			kfree(lfcc);
348 			goto out;
349 		}
350 		list_add(&lfcc->list, &list);
351 	}
352 
353 	gfs2_freeze_unlock(sdp);
354 
355 	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
356 				   LM_FLAG_RECOVER | GL_NOPID,
357 				   &sdp->sd_freeze_gh);
358 	if (error)
359 		goto relock_shared;
360 
361 	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
362 		error = gfs2_jdesc_check(jd);
363 		if (error)
364 			break;
365 		error = gfs2_find_jhead(jd, &lh);
366 		if (error)
367 			break;
368 		if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
369 			error = -EBUSY;
370 			break;
371 		}
372 	}
373 
374 	if (!error)
375 		goto out;  /* success */
376 
377 	gfs2_freeze_unlock(sdp);
378 
379 relock_shared:
380 	error2 = gfs2_freeze_lock_shared(sdp);
381 	gfs2_assert_withdraw(sdp, !error2);
382 
383 out:
384 	while (!list_empty(&list)) {
385 		lfcc = list_first_entry(&list, struct lfcc, list);
386 		list_del(&lfcc->list);
387 		gfs2_glock_dq_uninit(&lfcc->gh);
388 		kfree(lfcc);
389 	}
390 	return error;
391 }
392 
393 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
394 {
395 	const struct inode *inode = &ip->i_inode;
396 	struct gfs2_dinode *str = buf;
397 
398 	str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
399 	str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
400 	str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
401 	str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
402 	str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
403 	str->di_mode = cpu_to_be32(inode->i_mode);
404 	str->di_uid = cpu_to_be32(i_uid_read(inode));
405 	str->di_gid = cpu_to_be32(i_gid_read(inode));
406 	str->di_nlink = cpu_to_be32(inode->i_nlink);
407 	str->di_size = cpu_to_be64(i_size_read(inode));
408 	str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(inode));
409 	str->di_atime = cpu_to_be64(inode_get_atime_sec(inode));
410 	str->di_mtime = cpu_to_be64(inode_get_mtime_sec(inode));
411 	str->di_ctime = cpu_to_be64(inode_get_ctime_sec(inode));
412 
413 	str->di_goal_meta = cpu_to_be64(ip->i_goal);
414 	str->di_goal_data = cpu_to_be64(ip->i_goal);
415 	str->di_generation = cpu_to_be64(ip->i_generation);
416 
417 	str->di_flags = cpu_to_be32(ip->i_diskflags);
418 	str->di_height = cpu_to_be16(ip->i_height);
419 	str->di_payload_format = cpu_to_be32(S_ISDIR(inode->i_mode) &&
420 					     !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
421 					     GFS2_FORMAT_DE : 0);
422 	str->di_depth = cpu_to_be16(ip->i_depth);
423 	str->di_entries = cpu_to_be32(ip->i_entries);
424 
425 	str->di_eattr = cpu_to_be64(ip->i_eattr);
426 	str->di_atime_nsec = cpu_to_be32(inode_get_atime_nsec(inode));
427 	str->di_mtime_nsec = cpu_to_be32(inode_get_mtime_nsec(inode));
428 	str->di_ctime_nsec = cpu_to_be32(inode_get_ctime_nsec(inode));
429 }
430 
431 /**
432  * gfs2_write_inode - Make sure the inode is stable on the disk
433  * @inode: The inode
434  * @wbc: The writeback control structure
435  *
436  * Returns: errno
437  */
438 
439 static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
440 {
441 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
442 	struct gfs2_inode *ip = GFS2_I(inode);
443 	struct gfs2_sbd *sdp = GFS2_SB(inode);
444 	struct address_space *metamapping = gfs2_glock2aspace(gl);
445 	struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
446 	int ret = 0;
447 	bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
448 
449 	if (flush_all)
450 		gfs2_log_flush(GFS2_SB(inode), gl,
451 			       GFS2_LOG_HEAD_FLUSH_NORMAL |
452 			       GFS2_LFC_WRITE_INODE);
453 	if (bdi_wb_dirty_exceeded(bdi))
454 		gfs2_ail1_flush(sdp, wbc);
455 	else
456 		filemap_fdatawrite(metamapping);
457 	if (flush_all)
458 		ret = filemap_fdatawait(metamapping);
459 	if (ret)
460 		mark_inode_dirty_sync(inode);
461 	else {
462 		spin_lock(&inode->i_lock);
463 		if (!(inode->i_flags & I_DIRTY))
464 			gfs2_ordered_del_inode(ip);
465 		spin_unlock(&inode->i_lock);
466 	}
467 	return ret;
468 }
469 
470 /**
471  * gfs2_dirty_inode - check for atime updates
472  * @inode: The inode in question
473  * @flags: The type of dirty
474  *
475  * Unfortunately it can be called under any combination of inode
476  * glock and freeze glock, so we have to check carefully.
477  *
478  * At the moment this deals only with atime - it should be possible
479  * to expand that role in future, once a review of the locking has
480  * been carried out.
481  */
482 
483 static void gfs2_dirty_inode(struct inode *inode, int flags)
484 {
485 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
486 	struct gfs2_inode *ip = GFS2_I(inode);
487 	struct gfs2_sbd *sdp = GFS2_SB(inode);
488 	struct buffer_head *bh;
489 	struct gfs2_holder gh;
490 	int need_unlock = 0;
491 	int need_endtrans = 0;
492 	int ret;
493 
494 	/* This can only happen during incomplete inode creation. */
495 	if (unlikely(!gl))
496 		return;
497 
498 	if (gfs2_withdrawn(sdp))
499 		return;
500 	if (!gfs2_glock_is_locked_by_me(gl)) {
501 		ret = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, 0, &gh);
502 		if (ret) {
503 			fs_err(sdp, "dirty_inode: glock %d\n", ret);
504 			gfs2_dump_glock(NULL, gl, true);
505 			return;
506 		}
507 		need_unlock = 1;
508 	} else if (WARN_ON_ONCE(gl->gl_state != LM_ST_EXCLUSIVE))
509 		return;
510 
511 	if (current->journal_info == NULL) {
512 		ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
513 		if (ret) {
514 			fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
515 			goto out;
516 		}
517 		need_endtrans = 1;
518 	}
519 
520 	ret = gfs2_meta_inode_buffer(ip, &bh);
521 	if (ret == 0) {
522 		gfs2_trans_add_meta(gl, bh);
523 		gfs2_dinode_out(ip, bh->b_data);
524 		brelse(bh);
525 	}
526 
527 	if (need_endtrans)
528 		gfs2_trans_end(sdp);
529 out:
530 	if (need_unlock)
531 		gfs2_glock_dq_uninit(&gh);
532 }
533 
534 /**
535  * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
536  * @sdp: the filesystem
537  *
538  * Returns: errno
539  */
540 
541 void gfs2_make_fs_ro(struct gfs2_sbd *sdp)
542 {
543 	int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
544 
545 	if (!test_bit(SDF_KILL, &sdp->sd_flags))
546 		gfs2_flush_delete_work(sdp);
547 
548 	gfs2_destroy_threads(sdp);
549 
550 	if (log_write_allowed) {
551 		gfs2_quota_sync(sdp->sd_vfs, 0);
552 		gfs2_statfs_sync(sdp->sd_vfs, 0);
553 
554 		/* We do two log flushes here. The first one commits dirty inodes
555 		 * and rgrps to the journal, but queues up revokes to the ail list.
556 		 * The second flush writes out and removes the revokes.
557 		 *
558 		 * The first must be done before the FLUSH_SHUTDOWN code
559 		 * clears the LIVE flag, otherwise it will not be able to start
560 		 * a transaction to write its revokes, and the error will cause
561 		 * a withdraw of the file system. */
562 		gfs2_log_flush(sdp, NULL, GFS2_LFC_MAKE_FS_RO);
563 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
564 			       GFS2_LFC_MAKE_FS_RO);
565 		wait_event_timeout(sdp->sd_log_waitq,
566 				   gfs2_log_is_empty(sdp),
567 				   HZ * 5);
568 		gfs2_assert_warn(sdp, gfs2_log_is_empty(sdp));
569 	}
570 	gfs2_quota_cleanup(sdp);
571 }
572 
573 /**
574  * gfs2_put_super - Unmount the filesystem
575  * @sb: The VFS superblock
576  *
577  */
578 
579 static void gfs2_put_super(struct super_block *sb)
580 {
581 	struct gfs2_sbd *sdp = sb->s_fs_info;
582 	struct gfs2_jdesc *jd;
583 
584 	/* No more recovery requests */
585 	set_bit(SDF_NORECOVERY, &sdp->sd_flags);
586 	smp_mb();
587 
588 	/* Wait on outstanding recovery */
589 restart:
590 	spin_lock(&sdp->sd_jindex_spin);
591 	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
592 		if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
593 			continue;
594 		spin_unlock(&sdp->sd_jindex_spin);
595 		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
596 			    TASK_UNINTERRUPTIBLE);
597 		goto restart;
598 	}
599 	spin_unlock(&sdp->sd_jindex_spin);
600 
601 	/* Wait for withdraw to complete */
602 	flush_work(&sdp->sd_withdraw_work);
603 
604 	if (!sb_rdonly(sb))
605 		gfs2_make_fs_ro(sdp);
606 	else {
607 		if (gfs2_withdrawn(sdp))
608 			gfs2_destroy_threads(sdp);
609 
610 		gfs2_quota_cleanup(sdp);
611 	}
612 
613 	/*  At this point, we're through modifying the disk  */
614 
615 	/*  Release stuff  */
616 
617 	gfs2_freeze_unlock(sdp);
618 
619 	iput(sdp->sd_jindex);
620 	iput(sdp->sd_statfs_inode);
621 	iput(sdp->sd_rindex);
622 	iput(sdp->sd_quota_inode);
623 
624 	gfs2_glock_put(sdp->sd_rename_gl);
625 	gfs2_glock_put(sdp->sd_freeze_gl);
626 
627 	if (!sdp->sd_args.ar_spectator) {
628 		if (gfs2_holder_initialized(&sdp->sd_journal_gh))
629 			gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
630 		if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
631 			gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
632 		brelse(sdp->sd_sc_bh);
633 		gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
634 		gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
635 		free_local_statfs_inodes(sdp);
636 		iput(sdp->sd_qc_inode);
637 	}
638 
639 	gfs2_glock_dq_uninit(&sdp->sd_live_gh);
640 	gfs2_clear_rgrpd(sdp);
641 	gfs2_jindex_free(sdp);
642 	/*  Take apart glock structures and buffer lists  */
643 	gfs2_wait_glocks(sdp);
644 	iput(sdp->sd_inode);
645 	gfs2_delete_debugfs_file(sdp);
646 
647 	gfs2_sys_fs_del(sdp);
648 	rcu_barrier();
649 	free_sbd(sdp);
650 }
651 
652 /**
653  * gfs2_sync_fs - sync the filesystem
654  * @sb: the superblock
655  * @wait: true to wait for completion
656  *
657  * Flushes the log to disk.
658  */
659 
660 static int gfs2_sync_fs(struct super_block *sb, int wait)
661 {
662 	struct gfs2_sbd *sdp = sb->s_fs_info;
663 
664 	gfs2_quota_sync(sb, -1);
665 	if (wait)
666 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
667 			       GFS2_LFC_SYNC_FS);
668 	return sdp->sd_log_error;
669 }
670 
671 static int gfs2_do_thaw(struct gfs2_sbd *sdp, enum freeze_holder who, const void *freeze_owner)
672 {
673 	struct super_block *sb = sdp->sd_vfs;
674 	int error;
675 
676 	error = gfs2_freeze_lock_shared(sdp);
677 	if (error)
678 		goto fail;
679 	error = thaw_super(sb, who, freeze_owner);
680 	if (!error)
681 		return 0;
682 
683 fail:
684 	fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n", error);
685 	gfs2_assert_withdraw(sdp, 0);
686 	return error;
687 }
688 
689 void gfs2_freeze_func(struct work_struct *work)
690 {
691 	struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
692 	struct super_block *sb = sdp->sd_vfs;
693 	int error;
694 
695 	mutex_lock(&sdp->sd_freeze_mutex);
696 	error = -EBUSY;
697 	if (test_bit(SDF_FROZEN, &sdp->sd_flags))
698 		goto freeze_failed;
699 
700 	error = freeze_super(sb, FREEZE_HOLDER_USERSPACE, NULL);
701 	if (error)
702 		goto freeze_failed;
703 
704 	gfs2_freeze_unlock(sdp);
705 	set_bit(SDF_FROZEN, &sdp->sd_flags);
706 
707 	error = gfs2_do_thaw(sdp, FREEZE_HOLDER_USERSPACE, NULL);
708 	if (error)
709 		goto out;
710 
711 	clear_bit(SDF_FROZEN, &sdp->sd_flags);
712 	goto out;
713 
714 freeze_failed:
715 	fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n", error);
716 
717 out:
718 	mutex_unlock(&sdp->sd_freeze_mutex);
719 	deactivate_super(sb);
720 }
721 
722 /**
723  * gfs2_freeze_super - prevent further writes to the filesystem
724  * @sb: the VFS structure for the filesystem
725  * @who: freeze flags
726  * @freeze_owner: owner of the freeze
727  *
728  */
729 
730 static int gfs2_freeze_super(struct super_block *sb, enum freeze_holder who,
731 			     const void *freeze_owner)
732 {
733 	struct gfs2_sbd *sdp = sb->s_fs_info;
734 	int error;
735 
736 	if (!mutex_trylock(&sdp->sd_freeze_mutex))
737 		return -EBUSY;
738 	if (test_bit(SDF_FROZEN, &sdp->sd_flags)) {
739 		mutex_unlock(&sdp->sd_freeze_mutex);
740 		return -EBUSY;
741 	}
742 
743 	for (;;) {
744 		error = freeze_super(sb, who, freeze_owner);
745 		if (error) {
746 			fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
747 				error);
748 			goto out;
749 		}
750 
751 		error = gfs2_lock_fs_check_clean(sdp);
752 		if (!error) {
753 			set_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
754 			set_bit(SDF_FROZEN, &sdp->sd_flags);
755 			break;
756 		}
757 
758 		(void)gfs2_do_thaw(sdp, who, freeze_owner);
759 
760 		if (error == -EBUSY)
761 			fs_err(sdp, "waiting for recovery before freeze\n");
762 		else if (error == -EIO) {
763 			fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
764 			       "to recovery error.\n");
765 			goto out;
766 		} else {
767 			fs_err(sdp, "error freezing FS: %d\n", error);
768 		}
769 		fs_err(sdp, "retrying...\n");
770 		msleep(1000);
771 	}
772 
773 out:
774 	mutex_unlock(&sdp->sd_freeze_mutex);
775 	return error;
776 }
777 
778 static int gfs2_freeze_fs(struct super_block *sb)
779 {
780 	struct gfs2_sbd *sdp = sb->s_fs_info;
781 
782 	if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
783 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
784 			       GFS2_LFC_FREEZE_GO_SYNC);
785 		if (gfs2_withdrawn(sdp))
786 			return -EIO;
787 	}
788 	return 0;
789 }
790 
791 /**
792  * gfs2_thaw_super - reallow writes to the filesystem
793  * @sb: the VFS structure for the filesystem
794  * @who: freeze flags
795  * @freeze_owner: owner of the freeze
796  *
797  */
798 
799 static int gfs2_thaw_super(struct super_block *sb, enum freeze_holder who,
800 			   const void *freeze_owner)
801 {
802 	struct gfs2_sbd *sdp = sb->s_fs_info;
803 	int error;
804 
805 	if (!mutex_trylock(&sdp->sd_freeze_mutex))
806 		return -EBUSY;
807 	if (!test_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags)) {
808 		mutex_unlock(&sdp->sd_freeze_mutex);
809 		return -EINVAL;
810 	}
811 
812 	atomic_inc(&sb->s_active);
813 	gfs2_freeze_unlock(sdp);
814 
815 	error = gfs2_do_thaw(sdp, who, freeze_owner);
816 
817 	if (!error) {
818 		clear_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
819 		clear_bit(SDF_FROZEN, &sdp->sd_flags);
820 	}
821 	mutex_unlock(&sdp->sd_freeze_mutex);
822 	deactivate_super(sb);
823 	return error;
824 }
825 
826 /**
827  * statfs_slow_fill - fill in the sg for a given RG
828  * @rgd: the RG
829  * @sc: the sc structure
830  *
831  * Returns: 0 on success, -ESTALE if the LVB is invalid
832  */
833 
834 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
835 			    struct gfs2_statfs_change_host *sc)
836 {
837 	gfs2_rgrp_verify(rgd);
838 	sc->sc_total += rgd->rd_data;
839 	sc->sc_free += rgd->rd_free;
840 	sc->sc_dinodes += rgd->rd_dinodes;
841 	return 0;
842 }
843 
844 /**
845  * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
846  * @sdp: the filesystem
847  * @sc: the sc info that will be returned
848  *
849  * Any error (other than a signal) will cause this routine to fall back
850  * to the synchronous version.
851  *
852  * FIXME: This really shouldn't busy wait like this.
853  *
854  * Returns: errno
855  */
856 
857 static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
858 {
859 	struct gfs2_rgrpd *rgd_next;
860 	struct gfs2_holder *gha, *gh;
861 	unsigned int slots = 64;
862 	unsigned int x;
863 	int done;
864 	int error = 0, err;
865 
866 	memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
867 	gha = kmalloc_objs(struct gfs2_holder, slots);
868 	if (!gha)
869 		return -ENOMEM;
870 	for (x = 0; x < slots; x++)
871 		gfs2_holder_mark_uninitialized(gha + x);
872 
873 	rgd_next = gfs2_rgrpd_get_first(sdp);
874 
875 	for (;;) {
876 		done = 1;
877 
878 		for (x = 0; x < slots; x++) {
879 			gh = gha + x;
880 
881 			if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
882 				err = gfs2_glock_wait(gh);
883 				if (err) {
884 					gfs2_holder_uninit(gh);
885 					error = err;
886 				} else {
887 					if (!error) {
888 						struct gfs2_rgrpd *rgd =
889 							gfs2_glock2rgrp(gh->gh_gl);
890 
891 						error = statfs_slow_fill(rgd, sc);
892 					}
893 					gfs2_glock_dq_uninit(gh);
894 				}
895 			}
896 
897 			if (gfs2_holder_initialized(gh))
898 				done = 0;
899 			else if (rgd_next && !error) {
900 				error = gfs2_glock_nq_init(rgd_next->rd_gl,
901 							   LM_ST_SHARED,
902 							   GL_ASYNC,
903 							   gh);
904 				rgd_next = gfs2_rgrpd_get_next(rgd_next);
905 				done = 0;
906 			}
907 
908 			if (signal_pending(current))
909 				error = -ERESTARTSYS;
910 		}
911 
912 		if (done)
913 			break;
914 
915 		yield();
916 	}
917 
918 	kfree(gha);
919 	return error;
920 }
921 
922 /**
923  * gfs2_statfs_i - Do a statfs
924  * @sdp: the filesystem
925  * @sc: the sc structure
926  *
927  * Returns: errno
928  */
929 
930 static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
931 {
932 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
933 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
934 
935 	spin_lock(&sdp->sd_statfs_spin);
936 
937 	*sc = *m_sc;
938 	sc->sc_total += l_sc->sc_total;
939 	sc->sc_free += l_sc->sc_free;
940 	sc->sc_dinodes += l_sc->sc_dinodes;
941 
942 	spin_unlock(&sdp->sd_statfs_spin);
943 
944 	if (sc->sc_free < 0)
945 		sc->sc_free = 0;
946 	if (sc->sc_free > sc->sc_total)
947 		sc->sc_free = sc->sc_total;
948 	if (sc->sc_dinodes < 0)
949 		sc->sc_dinodes = 0;
950 
951 	return 0;
952 }
953 
954 /**
955  * gfs2_statfs - Gather and return stats about the filesystem
956  * @dentry: The name of the link
957  * @buf: The buffer
958  *
959  * Returns: 0 on success or error code
960  */
961 
962 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
963 {
964 	struct super_block *sb = dentry->d_sb;
965 	struct gfs2_sbd *sdp = sb->s_fs_info;
966 	struct gfs2_statfs_change_host sc;
967 	int error;
968 
969 	error = gfs2_rindex_update(sdp);
970 	if (error)
971 		return error;
972 
973 	if (gfs2_tune_get(sdp, gt_statfs_slow))
974 		error = gfs2_statfs_slow(sdp, &sc);
975 	else
976 		error = gfs2_statfs_i(sdp, &sc);
977 
978 	if (error)
979 		return error;
980 
981 	buf->f_type = GFS2_MAGIC;
982 	buf->f_bsize = sdp->sd_sb.sb_bsize;
983 	buf->f_blocks = sc.sc_total;
984 	buf->f_bfree = sc.sc_free;
985 	buf->f_bavail = sc.sc_free;
986 	buf->f_files = sc.sc_dinodes + sc.sc_free;
987 	buf->f_ffree = sc.sc_free;
988 	buf->f_namelen = GFS2_FNAMESIZE;
989 	buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
990 
991 	return 0;
992 }
993 
994 /**
995  * gfs2_drop_inode - Drop an inode (test for remote unlink)
996  * @inode: The inode to drop
997  *
998  * If we've received a callback on an iopen lock then it's because a
999  * remote node tried to deallocate the inode but failed due to this node
1000  * still having the inode open. Here we mark the link count zero
1001  * since we know that it must have reached zero if the GLF_DEMOTE flag
1002  * is set on the iopen glock. If we didn't do a disk read since the
1003  * remote node removed the final link then we might otherwise miss
1004  * this event. This check ensures that this node will deallocate the
1005  * inode's blocks, or alternatively pass the baton on to another
1006  * node for later deallocation.
1007  */
1008 
1009 static int gfs2_drop_inode(struct inode *inode)
1010 {
1011 	struct gfs2_inode *ip = GFS2_I(inode);
1012 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1013 
1014 	if (inode->i_nlink &&
1015 	    gfs2_holder_initialized(&ip->i_iopen_gh)) {
1016 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1017 		if (glock_needs_demote(gl))
1018 			clear_nlink(inode);
1019 	}
1020 
1021 	/*
1022 	 * When under memory pressure when an inode's link count has dropped to
1023 	 * zero, defer deleting the inode to the delete workqueue.  This avoids
1024 	 * calling into DLM under memory pressure, which can deadlock.
1025 	 */
1026 	if (!inode->i_nlink &&
1027 	    unlikely(current->flags & PF_MEMALLOC) &&
1028 	    gfs2_holder_initialized(&ip->i_iopen_gh)) {
1029 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1030 
1031 		gfs2_glock_hold(gl);
1032 		if (!gfs2_queue_verify_delete(gl, true))
1033 			gfs2_glock_put_async(gl);
1034 		return 0;
1035 	}
1036 
1037 	/*
1038 	 * No longer cache inodes when trying to evict them all.
1039 	 */
1040 	if (test_bit(SDF_EVICTING, &sdp->sd_flags))
1041 		return 1;
1042 
1043 	return inode_generic_drop(inode);
1044 }
1045 
1046 /**
1047  * gfs2_show_options - Show mount options for /proc/mounts
1048  * @s: seq_file structure
1049  * @root: root of this (sub)tree
1050  *
1051  * Returns: 0 on success or error code
1052  */
1053 
1054 static int gfs2_show_options(struct seq_file *s, struct dentry *root)
1055 {
1056 	struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
1057 	struct gfs2_args *args = &sdp->sd_args;
1058 	unsigned int logd_secs, statfs_slow, statfs_quantum, quota_quantum;
1059 
1060 	spin_lock(&sdp->sd_tune.gt_spin);
1061 	logd_secs = sdp->sd_tune.gt_logd_secs;
1062 	quota_quantum = sdp->sd_tune.gt_quota_quantum;
1063 	statfs_quantum = sdp->sd_tune.gt_statfs_quantum;
1064 	statfs_slow = sdp->sd_tune.gt_statfs_slow;
1065 	spin_unlock(&sdp->sd_tune.gt_spin);
1066 
1067 	if (is_subdir(root, sdp->sd_master_dir))
1068 		seq_puts(s, ",meta");
1069 	if (args->ar_lockproto[0])
1070 		seq_show_option(s, "lockproto", args->ar_lockproto);
1071 	if (args->ar_locktable[0])
1072 		seq_show_option(s, "locktable", args->ar_locktable);
1073 	if (args->ar_hostdata[0])
1074 		seq_show_option(s, "hostdata", args->ar_hostdata);
1075 	if (args->ar_spectator)
1076 		seq_puts(s, ",spectator");
1077 	if (args->ar_localflocks)
1078 		seq_puts(s, ",localflocks");
1079 	if (args->ar_debug)
1080 		seq_puts(s, ",debug");
1081 	if (args->ar_posix_acl)
1082 		seq_puts(s, ",acl");
1083 	if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1084 		char *state;
1085 		switch (args->ar_quota) {
1086 		case GFS2_QUOTA_OFF:
1087 			state = "off";
1088 			break;
1089 		case GFS2_QUOTA_ACCOUNT:
1090 			state = "account";
1091 			break;
1092 		case GFS2_QUOTA_ON:
1093 			state = "on";
1094 			break;
1095 		case GFS2_QUOTA_QUIET:
1096 			state = "quiet";
1097 			break;
1098 		default:
1099 			state = "unknown";
1100 			break;
1101 		}
1102 		seq_printf(s, ",quota=%s", state);
1103 	}
1104 	if (args->ar_suiddir)
1105 		seq_puts(s, ",suiddir");
1106 	if (args->ar_data != GFS2_DATA_DEFAULT) {
1107 		char *state;
1108 		switch (args->ar_data) {
1109 		case GFS2_DATA_WRITEBACK:
1110 			state = "writeback";
1111 			break;
1112 		case GFS2_DATA_ORDERED:
1113 			state = "ordered";
1114 			break;
1115 		default:
1116 			state = "unknown";
1117 			break;
1118 		}
1119 		seq_printf(s, ",data=%s", state);
1120 	}
1121 	if (args->ar_discard)
1122 		seq_puts(s, ",discard");
1123 	if (logd_secs != 30)
1124 		seq_printf(s, ",commit=%d", logd_secs);
1125 	if (statfs_quantum != 30)
1126 		seq_printf(s, ",statfs_quantum=%d", statfs_quantum);
1127 	else if (statfs_slow)
1128 		seq_puts(s, ",statfs_quantum=0");
1129 	if (quota_quantum != 60)
1130 		seq_printf(s, ",quota_quantum=%d", quota_quantum);
1131 	if (args->ar_statfs_percent)
1132 		seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
1133 	if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1134 		const char *state;
1135 
1136 		switch (args->ar_errors) {
1137 		case GFS2_ERRORS_WITHDRAW:
1138 			state = "withdraw";
1139 			break;
1140 		case GFS2_ERRORS_DEACTIVATE:
1141 			state = "deactivate";
1142 			break;
1143 		case GFS2_ERRORS_PANIC:
1144 			state = "panic";
1145 			break;
1146 		default:
1147 			state = "unknown";
1148 			break;
1149 		}
1150 		seq_printf(s, ",errors=%s", state);
1151 	}
1152 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
1153 		seq_puts(s, ",nobarrier");
1154 	if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
1155 		seq_puts(s, ",demote_interface_used");
1156 	if (args->ar_rgrplvb)
1157 		seq_puts(s, ",rgrplvb");
1158 	if (args->ar_loccookie)
1159 		seq_puts(s, ",loccookie");
1160 	return 0;
1161 }
1162 
1163 /**
1164  * gfs2_glock_put_eventually
1165  * @gl:	The glock to put
1166  *
1167  * When under memory pressure, trigger a deferred glock put to make sure we
1168  * won't call into DLM and deadlock.  Otherwise, put the glock directly.
1169  */
1170 
1171 static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1172 {
1173 	if (current->flags & PF_MEMALLOC)
1174 		gfs2_glock_put_async(gl);
1175 	else
1176 		gfs2_glock_put(gl);
1177 }
1178 
1179 static enum evict_behavior gfs2_upgrade_iopen_glock(struct inode *inode)
1180 {
1181 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1182 	struct gfs2_inode *ip = GFS2_I(inode);
1183 	struct gfs2_holder *gh = &ip->i_iopen_gh;
1184 	struct wait_queue_head *holder_waitq, *glock_waitq;
1185 	struct wait_queue_entry holder_wait, glock_wait;
1186 	long ret = 5 * HZ;
1187 	int error;
1188 
1189 	gh->gh_flags |= GL_NOCACHE;
1190 	gfs2_glock_dq_wait(gh);
1191 
1192 	/*
1193 	 * If there are no other lock holders, we will immediately get
1194 	 * exclusive access to the iopen glock here.
1195 	 *
1196 	 * Otherwise, the other nodes holding the lock will be notified about
1197 	 * our locking request (see iopen_go_callback()).  If they do not have
1198 	 * the inode open, they are expected to evict the cached inode and
1199 	 * release the lock, allowing us to proceed.
1200 	 *
1201 	 * Otherwise, if they cannot evict the inode, they are expected to poke
1202 	 * the inode glock (note: not the iopen glock).  We will notice that
1203 	 * and stop waiting for the iopen glock immediately.  The other node(s)
1204 	 * are then expected to take care of deleting the inode when they no
1205 	 * longer use it.
1206 	 *
1207 	 * As a last resort, if another node keeps holding the iopen glock
1208 	 * without showing any activity on the inode glock, we will eventually
1209 	 * time out and fail the iopen glock upgrade.
1210 	 */
1211 
1212 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1213 	error = gfs2_glock_nq(gh);
1214 	if (error)
1215 		return EVICT_SHOULD_SKIP_DELETE;
1216 
1217 	holder_waitq = bit_waitqueue(&gh->gh_iflags, HIF_WAIT);
1218 	glock_waitq = bit_waitqueue(&gl->gl_flags, GLF_DEMOTE);
1219 	init_wait(&holder_wait);
1220 	init_wait(&glock_wait);
1221 	for (;;) {
1222 		prepare_to_wait(holder_waitq, &holder_wait, TASK_INTERRUPTIBLE);
1223 		prepare_to_wait(glock_waitq, &glock_wait, TASK_INTERRUPTIBLE);
1224 		if (gfs2_glock_poll(gh) || glock_needs_demote(gl))
1225 			break;
1226 		if (signal_pending(current))
1227 			break;
1228 		ret = schedule_timeout(ret);
1229 		if (gfs2_glock_poll(gh) || glock_needs_demote(gl))
1230 			break;
1231 		if (!ret)
1232 			break;
1233 		if (signal_pending(current))
1234 			break;
1235 	}
1236 	finish_wait(holder_waitq, &holder_wait);
1237 	finish_wait(glock_waitq, &glock_wait);
1238 
1239 	if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1240 		gfs2_glock_dq(gh);
1241 		if (glock_needs_demote(gl))
1242 			return EVICT_SHOULD_SKIP_DELETE;
1243 		return EVICT_SHOULD_DEFER_DELETE;
1244 	}
1245 	error = gfs2_glock_holder_ready(gh);
1246 	if (error)
1247 		return EVICT_SHOULD_SKIP_DELETE;
1248 	return EVICT_SHOULD_DELETE;
1249 }
1250 
1251 /**
1252  * evict_should_delete - determine whether the inode is eligible for deletion
1253  * @inode: The inode to evict
1254  * @gh: The glock holder structure
1255  *
1256  * This function determines whether the evicted inode is eligible to be deleted
1257  * and locks the inode glock.
1258  *
1259  * Returns: the fate of the dinode
1260  */
1261 static enum evict_behavior evict_should_delete(struct inode *inode,
1262 					       struct gfs2_holder *gh)
1263 {
1264 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1265 	struct gfs2_inode *ip = GFS2_I(inode);
1266 	struct super_block *sb = inode->i_sb;
1267 	struct gfs2_sbd *sdp = sb->s_fs_info;
1268 	int ret;
1269 
1270 	if (inode->i_nlink)
1271 		return EVICT_SHOULD_SKIP_DELETE;
1272 
1273 	if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1274 	    test_bit(GLF_DEFER_DELETE, &ip->i_iopen_gh.gh_gl->gl_flags))
1275 		return EVICT_SHOULD_DEFER_DELETE;
1276 
1277 	/* Deletes should never happen under memory pressure anymore.  */
1278 	if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1279 		return EVICT_SHOULD_DEFER_DELETE;
1280 
1281 	/* Must not read inode block until block type has been verified */
1282 	ret = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
1283 	if (unlikely(ret))
1284 		return EVICT_SHOULD_SKIP_DELETE;
1285 
1286 	if (gfs2_inode_already_deleted(gl, ip->i_no_formal_ino))
1287 		return EVICT_SHOULD_SKIP_DELETE;
1288 	ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1289 	if (ret)
1290 		return EVICT_SHOULD_SKIP_DELETE;
1291 
1292 	ret = gfs2_instantiate(gh);
1293 	if (ret)
1294 		return EVICT_SHOULD_SKIP_DELETE;
1295 
1296 	/*
1297 	 * The inode may have been recreated in the meantime.
1298 	 */
1299 	if (inode->i_nlink)
1300 		return EVICT_SHOULD_SKIP_DELETE;
1301 
1302 	if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1303 	    test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
1304 		return gfs2_upgrade_iopen_glock(inode);
1305 	return EVICT_SHOULD_DELETE;
1306 }
1307 
1308 /**
1309  * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
1310  * @inode: The inode to evict
1311  * @gh: The glock holder structure
1312  */
1313 static int evict_unlinked_inode(struct inode *inode, struct gfs2_holder *gh)
1314 {
1315 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1316 	struct gfs2_inode *ip = GFS2_I(inode);
1317 	int ret;
1318 
1319 	/* The inode glock must be held exclusively and be instantiated. */
1320 	BUG_ON(!gfs2_holder_initialized(gh) ||
1321 	       test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags));
1322 
1323 	if (S_ISDIR(inode->i_mode) &&
1324 	    (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1325 		ret = gfs2_dir_exhash_dealloc(ip);
1326 		if (ret)
1327 			goto out;
1328 	}
1329 
1330 	if (ip->i_eattr) {
1331 		ret = gfs2_ea_dealloc(ip, true);
1332 		if (ret)
1333 			goto out;
1334 	}
1335 
1336 	if (!gfs2_is_stuffed(ip)) {
1337 		ret = gfs2_file_dealloc(ip);
1338 		if (ret)
1339 			goto out;
1340 	}
1341 
1342 	/*
1343 	 * As soon as we clear the bitmap for the dinode, gfs2_create_inode()
1344 	 * can get called to recreate it, or even gfs2_inode_lookup() if the
1345 	 * inode was recreated on another node in the meantime.
1346 	 *
1347 	 * However, inserting the new inode into the inode hash table will not
1348 	 * succeed until the old inode is removed, and that only happens after
1349 	 * ->evict_inode() returns.  The new inode is attached to its inode and
1350 	 *  iopen glocks after inserting it into the inode hash table, so at
1351 	 *  that point we can be sure that both glocks are unused.
1352 	 */
1353 
1354 	ret = gfs2_dinode_dealloc(ip);
1355 	if (!ret)
1356 		gfs2_inode_remember_delete(gl, ip->i_no_formal_ino);
1357 
1358 out:
1359 	return ret;
1360 }
1361 
1362 static int gfs2_truncate_inode_pages(struct inode *inode)
1363 {
1364 	struct gfs2_inode *ip = GFS2_I(inode);
1365 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1366 	struct address_space *mapping = &inode->i_data;
1367 	bool need_trans = gfs2_is_jdata(ip) && mapping->nrpages;
1368 	int ret = 0;
1369 
1370 	/*
1371 	 * Truncating a jdata inode address space may create revokes in
1372 	 * truncate_inode_pages() -> gfs2_invalidate_folio() -> ... ->
1373 	 * gfs2_remove_from_journal(), so we need a transaction here.
1374 	 *
1375 	 * During a withdraw, no new transactions can be created.  We still
1376 	 * take the log flush lock to prevent truncate from racing with
1377 	 * gfs2_log_flush().
1378 	 */
1379 	if (need_trans) {
1380 		ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1381 		if (ret)
1382 			down_read(&sdp->sd_log_flush_lock);
1383 	}
1384 	truncate_inode_pages(mapping, 0);
1385 	if (need_trans) {
1386 		if (ret)
1387 			up_read(&sdp->sd_log_flush_lock);
1388 		else
1389 			gfs2_trans_end(sdp);
1390 	}
1391 	return ret;
1392 }
1393 
1394 static void gfs2_truncate_inode_pages_final(struct inode *inode)
1395 {
1396 	struct gfs2_inode *ip = GFS2_I(inode);
1397 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1398 	struct address_space *mapping = &inode->i_data;
1399 	bool need_lock = gfs2_is_jdata(ip) && mapping->nrpages;
1400 
1401 	if (need_lock)
1402 		down_read(&sdp->sd_log_flush_lock);
1403 	truncate_inode_pages_final(mapping);
1404 	if (need_lock)
1405 		up_read(&sdp->sd_log_flush_lock);
1406 }
1407 
1408 /*
1409  * evict_linked_inode - evict an inode whose dinode has not been unlinked
1410  * @inode: The inode to evict
1411  * @gh: The glock holder structure
1412  */
1413 static int evict_linked_inode(struct inode *inode, struct gfs2_holder *gh)
1414 {
1415 	struct super_block *sb = inode->i_sb;
1416 	struct gfs2_sbd *sdp = sb->s_fs_info;
1417 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1418 	struct address_space *metamapping = gfs2_glock2aspace(gl);
1419 	int ret;
1420 
1421 	if (!(test_bit(GLF_DIRTY, &gl->gl_flags) || inode->i_flags & I_DIRTY))
1422 		goto clean;
1423 
1424 	/* The inode glock must be held exclusively and be instantiated. */
1425 	if (!gfs2_holder_initialized(gh))
1426 		ret = gfs2_glock_nq_init(gl, LM_ST_EXCLUSIVE, 0, gh);
1427 	else
1428 		ret = gfs2_instantiate(gh);
1429 	if (ret)
1430 		return ret;
1431 
1432 	gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1433 		       GFS2_LFC_EVICT_INODE);
1434 	if (test_bit(GLF_DIRTY, &gl->gl_flags)) {
1435 		filemap_fdatawrite(metamapping);
1436 		filemap_fdatawait(metamapping);
1437 	}
1438 	write_inode_now(inode, 1);
1439 	gfs2_ail_flush(gl, 0);
1440 
1441 clean:
1442 	ret = gfs2_truncate_inode_pages(inode);
1443 	truncate_inode_pages(metamapping, 0);
1444 	return ret;
1445 }
1446 
1447 /**
1448  * gfs2_evict_inode - Remove an inode from cache
1449  * @inode: The inode to evict
1450  *
1451  * There are three cases to consider:
1452  * 1. i_nlink == 0, we are final opener (and must deallocate)
1453  * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1454  * 3. i_nlink > 0
1455  *
1456  * If the fs is read only, then we have to treat all cases as per #3
1457  * since we are unable to do any deallocation. The inode will be
1458  * deallocated by the next read/write node to attempt an allocation
1459  * in the same resource group
1460  *
1461  * We have to (at the moment) hold the inodes main lock to cover
1462  * the gap between unlocking the shared lock on the iopen lock and
1463  * taking the exclusive lock. I'd rather do a shared -> exclusive
1464  * conversion on the iopen lock, but we can change that later. This
1465  * is safe, just less efficient.
1466  */
1467 
1468 static void gfs2_evict_inode(struct inode *inode)
1469 {
1470 	struct super_block *sb = inode->i_sb;
1471 	struct gfs2_sbd *sdp = sb->s_fs_info;
1472 	struct gfs2_glock *gl = gfs2_inode_glock(inode);
1473 	struct gfs2_inode *ip = GFS2_I(inode);
1474 	struct gfs2_holder gh;
1475 	enum evict_behavior behavior;
1476 	int ret;
1477 
1478 	gfs2_holder_mark_uninitialized(&gh);
1479 	if (sb_rdonly(sb) || !ip->i_no_addr || !gl)
1480 		goto out;
1481 
1482 	/*
1483 	 * In case of an incomplete mount, gfs2_evict_inode() may be called for
1484 	 * system files without having an active journal to write to.  In that
1485 	 * case, skip the filesystem evict.
1486 	 */
1487 	if (!sdp->sd_jdesc)
1488 		goto out;
1489 
1490 	behavior = evict_should_delete(inode, &gh);
1491 	if (behavior == EVICT_SHOULD_DEFER_DELETE &&
1492 	    !test_bit(SDF_KILL, &sdp->sd_flags)) {
1493 		struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
1494 
1495 		if (io_gl) {
1496 			gfs2_glock_hold(io_gl);
1497 			if (!gfs2_queue_verify_delete(io_gl, true))
1498 				gfs2_glock_put(io_gl);
1499 			goto out;
1500 		}
1501 		behavior = EVICT_SHOULD_SKIP_DELETE;
1502 	}
1503 	if (behavior == EVICT_SHOULD_DELETE)
1504 		ret = evict_unlinked_inode(inode, &gh);
1505 	else
1506 		ret = evict_linked_inode(inode, &gh);
1507 
1508 	if (gfs2_rs_active(&ip->i_res))
1509 		gfs2_rs_deltree(&ip->i_res);
1510 
1511 	if (ret && !gfs2_withdrawn(sdp) && ret != -EROFS)
1512 		fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
1513 out:
1514 	if (gfs2_holder_initialized(&gh))
1515 		gfs2_glock_dq_uninit(&gh);
1516 	gfs2_truncate_inode_pages_final(inode);
1517 	if (ip->i_qadata)
1518 		gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
1519 	gfs2_rs_deltree(&ip->i_res);
1520 	gfs2_ordered_del_inode(ip);
1521 	clear_inode(inode);
1522 	gfs2_dir_hash_inval(ip);
1523 	if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
1524 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1525 
1526 		glock_clear_object(gl, ip);
1527 		gfs2_glock_hold(gl);
1528 		ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1529 		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1530 		gfs2_glock_put_eventually(gl);
1531 	}
1532 	if (gl) {
1533 		glock_clear_object(gl, ip);
1534 		wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1535 		gfs2_glock_put_eventually(gl);
1536 		rcu_assign_pointer(ip->i_gl, NULL);
1537 	}
1538 }
1539 
1540 static struct inode *gfs2_alloc_inode(struct super_block *sb)
1541 {
1542 	struct gfs2_inode *ip;
1543 
1544 	ip = alloc_inode_sb(sb, gfs2_inode_cachep, GFP_KERNEL);
1545 	if (!ip)
1546 		return NULL;
1547 	ip->i_no_addr = 0;
1548 	ip->i_no_formal_ino = 0;
1549 	ip->i_flags = 0;
1550 	ip->i_gl = NULL;
1551 	gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
1552 	memset(&ip->i_res, 0, sizeof(ip->i_res));
1553 	RB_CLEAR_NODE(&ip->i_res.rs_node);
1554 	ip->i_diskflags = 0;
1555 	ip->i_rahead = 0;
1556 	return &ip->i_inode;
1557 }
1558 
1559 static void gfs2_free_inode(struct inode *inode)
1560 {
1561 	kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
1562 }
1563 
1564 void free_local_statfs_inodes(struct gfs2_sbd *sdp)
1565 {
1566 	struct local_statfs_inode *lsi, *safe;
1567 
1568 	/* Run through the statfs inodes list to iput and free memory */
1569 	list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
1570 		if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
1571 			sdp->sd_sc_inode = NULL; /* belongs to this node */
1572 		if (lsi->si_sc_inode)
1573 			iput(lsi->si_sc_inode);
1574 		list_del(&lsi->si_list);
1575 		kfree(lsi);
1576 	}
1577 }
1578 
1579 struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
1580 				      unsigned int index)
1581 {
1582 	struct local_statfs_inode *lsi;
1583 
1584 	/* Return the local (per node) statfs inode in the
1585 	 * sdp->sd_sc_inodes_list corresponding to the 'index'. */
1586 	list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
1587 		if (lsi->si_jid == index)
1588 			return lsi->si_sc_inode;
1589 	}
1590 	return NULL;
1591 }
1592 
1593 const struct super_operations gfs2_super_ops = {
1594 	.alloc_inode		= gfs2_alloc_inode,
1595 	.free_inode		= gfs2_free_inode,
1596 	.write_inode		= gfs2_write_inode,
1597 	.dirty_inode		= gfs2_dirty_inode,
1598 	.evict_inode		= gfs2_evict_inode,
1599 	.put_super		= gfs2_put_super,
1600 	.sync_fs		= gfs2_sync_fs,
1601 	.freeze_super		= gfs2_freeze_super,
1602 	.freeze_fs		= gfs2_freeze_fs,
1603 	.thaw_super		= gfs2_thaw_super,
1604 	.statfs			= gfs2_statfs,
1605 	.drop_inode		= gfs2_drop_inode,
1606 	.show_options		= gfs2_show_options,
1607 };
1608 
1609