xref: /linux/fs/gfs2/super.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
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 
gfs2_jindex_free(struct gfs2_sbd * sdp)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 
jdesc_find_i(struct list_head * head,unsigned int jid)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 
gfs2_jdesc_find(struct gfs2_sbd * sdp,unsigned int jid)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 
gfs2_jdesc_check(struct gfs2_jdesc * jd)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 
gfs2_make_fs_rw(struct gfs2_sbd * sdp)133 int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
134 {
135 	struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
136 	struct gfs2_glock *j_gl = ip->i_gl;
137 	int error;
138 
139 	j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
140 	if (gfs2_withdrawn(sdp))
141 		return -EIO;
142 
143 	if (sdp->sd_log_sequence == 0) {
144 		fs_err(sdp, "unknown status of our own journal jid %d",
145 		       sdp->sd_lockstruct.ls_jid);
146 		return -EIO;
147 	}
148 
149 	error = gfs2_quota_init(sdp);
150 	if (!error && gfs2_withdrawn(sdp)) {
151 		gfs2_quota_cleanup(sdp);
152 		error = -EIO;
153 	}
154 	if (!error)
155 		set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
156 	return error;
157 }
158 
gfs2_statfs_change_in(struct gfs2_statfs_change_host * sc,const void * buf)159 void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
160 {
161 	const struct gfs2_statfs_change *str = buf;
162 
163 	sc->sc_total = be64_to_cpu(str->sc_total);
164 	sc->sc_free = be64_to_cpu(str->sc_free);
165 	sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
166 }
167 
gfs2_statfs_change_out(const struct gfs2_statfs_change_host * sc,void * buf)168 void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
169 {
170 	struct gfs2_statfs_change *str = buf;
171 
172 	str->sc_total = cpu_to_be64(sc->sc_total);
173 	str->sc_free = cpu_to_be64(sc->sc_free);
174 	str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
175 }
176 
gfs2_statfs_init(struct gfs2_sbd * sdp)177 int gfs2_statfs_init(struct gfs2_sbd *sdp)
178 {
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(m_ip->i_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 
gfs2_statfs_change(struct gfs2_sbd * sdp,s64 total,s64 free,s64 dinodes)216 void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
217 			s64 dinodes)
218 {
219 	struct gfs2_inode *l_ip = GFS2_I(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(l_ip->i_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 
update_statfs(struct gfs2_sbd * sdp,struct buffer_head * m_bh)245 void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh)
246 {
247 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
248 	struct gfs2_inode *l_ip = GFS2_I(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_ip->i_gl, sdp->sd_sc_bh);
253 	gfs2_trans_add_meta(m_ip->i_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 
gfs2_statfs_sync(struct super_block * sb,int type)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(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
277 				   &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 
gfs2_lock_fs_check_clean(struct gfs2_sbd * sdp)324 static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
325 {
326 	struct gfs2_inode *ip;
327 	struct gfs2_jdesc *jd;
328 	struct lfcc *lfcc;
329 	LIST_HEAD(list);
330 	struct gfs2_log_header_host lh;
331 	int error, error2;
332 
333 	/*
334 	 * Grab all the journal glocks in SH mode.  We are *probably* doing
335 	 * that to prevent recovery.
336 	 */
337 
338 	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
339 		lfcc = kmalloc_obj(struct lfcc);
340 		if (!lfcc) {
341 			error = -ENOMEM;
342 			goto out;
343 		}
344 		ip = GFS2_I(jd->jd_inode);
345 		error = gfs2_glock_nq_init(ip->i_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 
gfs2_dinode_out(const struct gfs2_inode * ip,void * buf)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 
gfs2_write_inode(struct inode * inode,struct writeback_control * wbc)439 static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
440 {
441 	struct gfs2_inode *ip = GFS2_I(inode);
442 	struct gfs2_sbd *sdp = GFS2_SB(inode);
443 	struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
444 	struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
445 	int ret = 0;
446 	bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
447 
448 	if (flush_all)
449 		gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
450 			       GFS2_LOG_HEAD_FLUSH_NORMAL |
451 			       GFS2_LFC_WRITE_INODE);
452 	if (bdi->wb.dirty_exceeded)
453 		gfs2_ail1_flush(sdp, wbc);
454 	else
455 		filemap_fdatawrite(metamapping);
456 	if (flush_all)
457 		ret = filemap_fdatawait(metamapping);
458 	if (ret)
459 		mark_inode_dirty_sync(inode);
460 	else {
461 		spin_lock(&inode->i_lock);
462 		if (!(inode->i_flags & I_DIRTY))
463 			gfs2_ordered_del_inode(ip);
464 		spin_unlock(&inode->i_lock);
465 	}
466 	return ret;
467 }
468 
469 /**
470  * gfs2_dirty_inode - check for atime updates
471  * @inode: The inode in question
472  * @flags: The type of dirty
473  *
474  * Unfortunately it can be called under any combination of inode
475  * glock and freeze glock, so we have to check carefully.
476  *
477  * At the moment this deals only with atime - it should be possible
478  * to expand that role in future, once a review of the locking has
479  * been carried out.
480  */
481 
gfs2_dirty_inode(struct inode * inode,int flags)482 static void gfs2_dirty_inode(struct inode *inode, int flags)
483 {
484 	struct gfs2_inode *ip = GFS2_I(inode);
485 	struct gfs2_sbd *sdp = GFS2_SB(inode);
486 	struct buffer_head *bh;
487 	struct gfs2_holder gh;
488 	int need_unlock = 0;
489 	int need_endtrans = 0;
490 	int ret;
491 
492 	/* This can only happen during incomplete inode creation. */
493 	if (unlikely(!ip->i_gl))
494 		return;
495 
496 	if (gfs2_withdrawn(sdp))
497 		return;
498 	if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
499 		ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
500 		if (ret) {
501 			fs_err(sdp, "dirty_inode: glock %d\n", ret);
502 			gfs2_dump_glock(NULL, ip->i_gl, true);
503 			return;
504 		}
505 		need_unlock = 1;
506 	} else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
507 		return;
508 
509 	if (current->journal_info == NULL) {
510 		ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
511 		if (ret) {
512 			fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
513 			goto out;
514 		}
515 		need_endtrans = 1;
516 	}
517 
518 	ret = gfs2_meta_inode_buffer(ip, &bh);
519 	if (ret == 0) {
520 		gfs2_trans_add_meta(ip->i_gl, bh);
521 		gfs2_dinode_out(ip, bh->b_data);
522 		brelse(bh);
523 	}
524 
525 	if (need_endtrans)
526 		gfs2_trans_end(sdp);
527 out:
528 	if (need_unlock)
529 		gfs2_glock_dq_uninit(&gh);
530 }
531 
532 /**
533  * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
534  * @sdp: the filesystem
535  *
536  * Returns: errno
537  */
538 
gfs2_make_fs_ro(struct gfs2_sbd * sdp)539 void gfs2_make_fs_ro(struct gfs2_sbd *sdp)
540 {
541 	int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
542 
543 	if (!test_bit(SDF_KILL, &sdp->sd_flags))
544 		gfs2_flush_delete_work(sdp);
545 
546 	gfs2_destroy_threads(sdp);
547 
548 	if (log_write_allowed) {
549 		gfs2_quota_sync(sdp->sd_vfs, 0);
550 		gfs2_statfs_sync(sdp->sd_vfs, 0);
551 
552 		/* We do two log flushes here. The first one commits dirty inodes
553 		 * and rgrps to the journal, but queues up revokes to the ail list.
554 		 * The second flush writes out and removes the revokes.
555 		 *
556 		 * The first must be done before the FLUSH_SHUTDOWN code
557 		 * clears the LIVE flag, otherwise it will not be able to start
558 		 * a transaction to write its revokes, and the error will cause
559 		 * a withdraw of the file system. */
560 		gfs2_log_flush(sdp, NULL, GFS2_LFC_MAKE_FS_RO);
561 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
562 			       GFS2_LFC_MAKE_FS_RO);
563 		wait_event_timeout(sdp->sd_log_waitq,
564 				   gfs2_log_is_empty(sdp),
565 				   HZ * 5);
566 		gfs2_assert_warn(sdp, gfs2_log_is_empty(sdp));
567 	}
568 	gfs2_quota_cleanup(sdp);
569 }
570 
571 /**
572  * gfs2_put_super - Unmount the filesystem
573  * @sb: The VFS superblock
574  *
575  */
576 
gfs2_put_super(struct super_block * sb)577 static void gfs2_put_super(struct super_block *sb)
578 {
579 	struct gfs2_sbd *sdp = sb->s_fs_info;
580 	struct gfs2_jdesc *jd;
581 
582 	/* No more recovery requests */
583 	set_bit(SDF_NORECOVERY, &sdp->sd_flags);
584 	smp_mb();
585 
586 	/* Wait on outstanding recovery */
587 restart:
588 	spin_lock(&sdp->sd_jindex_spin);
589 	list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
590 		if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
591 			continue;
592 		spin_unlock(&sdp->sd_jindex_spin);
593 		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
594 			    TASK_UNINTERRUPTIBLE);
595 		goto restart;
596 	}
597 	spin_unlock(&sdp->sd_jindex_spin);
598 
599 	if (!sb_rdonly(sb))
600 		gfs2_make_fs_ro(sdp);
601 	else {
602 		if (gfs2_withdrawn(sdp))
603 			gfs2_destroy_threads(sdp);
604 
605 		gfs2_quota_cleanup(sdp);
606 	}
607 
608 	flush_work(&sdp->sd_withdraw_work);
609 
610 	/*  At this point, we're through modifying the disk  */
611 
612 	/*  Release stuff  */
613 
614 	gfs2_freeze_unlock(sdp);
615 
616 	iput(sdp->sd_jindex);
617 	iput(sdp->sd_statfs_inode);
618 	iput(sdp->sd_rindex);
619 	iput(sdp->sd_quota_inode);
620 
621 	gfs2_glock_put(sdp->sd_rename_gl);
622 	gfs2_glock_put(sdp->sd_freeze_gl);
623 
624 	if (!sdp->sd_args.ar_spectator) {
625 		if (gfs2_holder_initialized(&sdp->sd_journal_gh))
626 			gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
627 		if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
628 			gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
629 		brelse(sdp->sd_sc_bh);
630 		gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
631 		gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
632 		free_local_statfs_inodes(sdp);
633 		iput(sdp->sd_qc_inode);
634 	}
635 
636 	gfs2_glock_dq_uninit(&sdp->sd_live_gh);
637 	gfs2_clear_rgrpd(sdp);
638 	gfs2_jindex_free(sdp);
639 	/*  Take apart glock structures and buffer lists  */
640 	gfs2_gl_hash_clear(sdp);
641 	iput(sdp->sd_inode);
642 	gfs2_delete_debugfs_file(sdp);
643 
644 	gfs2_sys_fs_del(sdp);
645 	free_sbd(sdp);
646 }
647 
648 /**
649  * gfs2_sync_fs - sync the filesystem
650  * @sb: the superblock
651  * @wait: true to wait for completion
652  *
653  * Flushes the log to disk.
654  */
655 
gfs2_sync_fs(struct super_block * sb,int wait)656 static int gfs2_sync_fs(struct super_block *sb, int wait)
657 {
658 	struct gfs2_sbd *sdp = sb->s_fs_info;
659 
660 	gfs2_quota_sync(sb, -1);
661 	if (wait)
662 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
663 			       GFS2_LFC_SYNC_FS);
664 	return sdp->sd_log_error;
665 }
666 
gfs2_do_thaw(struct gfs2_sbd * sdp,enum freeze_holder who,const void * freeze_owner)667 static int gfs2_do_thaw(struct gfs2_sbd *sdp, enum freeze_holder who, const void *freeze_owner)
668 {
669 	struct super_block *sb = sdp->sd_vfs;
670 	int error;
671 
672 	error = gfs2_freeze_lock_shared(sdp);
673 	if (error)
674 		goto fail;
675 	error = thaw_super(sb, who, freeze_owner);
676 	if (!error)
677 		return 0;
678 
679 fail:
680 	fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n", error);
681 	gfs2_assert_withdraw(sdp, 0);
682 	return error;
683 }
684 
gfs2_freeze_func(struct work_struct * work)685 void gfs2_freeze_func(struct work_struct *work)
686 {
687 	struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
688 	struct super_block *sb = sdp->sd_vfs;
689 	int error;
690 
691 	mutex_lock(&sdp->sd_freeze_mutex);
692 	error = -EBUSY;
693 	if (test_bit(SDF_FROZEN, &sdp->sd_flags))
694 		goto freeze_failed;
695 
696 	error = freeze_super(sb, FREEZE_HOLDER_USERSPACE, NULL);
697 	if (error)
698 		goto freeze_failed;
699 
700 	gfs2_freeze_unlock(sdp);
701 	set_bit(SDF_FROZEN, &sdp->sd_flags);
702 
703 	error = gfs2_do_thaw(sdp, FREEZE_HOLDER_USERSPACE, NULL);
704 	if (error)
705 		goto out;
706 
707 	clear_bit(SDF_FROZEN, &sdp->sd_flags);
708 	goto out;
709 
710 freeze_failed:
711 	fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n", error);
712 
713 out:
714 	mutex_unlock(&sdp->sd_freeze_mutex);
715 	deactivate_super(sb);
716 }
717 
718 /**
719  * gfs2_freeze_super - prevent further writes to the filesystem
720  * @sb: the VFS structure for the filesystem
721  * @who: freeze flags
722  * @freeze_owner: owner of the freeze
723  *
724  */
725 
gfs2_freeze_super(struct super_block * sb,enum freeze_holder who,const void * freeze_owner)726 static int gfs2_freeze_super(struct super_block *sb, enum freeze_holder who,
727 			     const void *freeze_owner)
728 {
729 	struct gfs2_sbd *sdp = sb->s_fs_info;
730 	int error;
731 
732 	if (!mutex_trylock(&sdp->sd_freeze_mutex))
733 		return -EBUSY;
734 	if (test_bit(SDF_FROZEN, &sdp->sd_flags)) {
735 		mutex_unlock(&sdp->sd_freeze_mutex);
736 		return -EBUSY;
737 	}
738 
739 	for (;;) {
740 		error = freeze_super(sb, who, freeze_owner);
741 		if (error) {
742 			fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
743 				error);
744 			goto out;
745 		}
746 
747 		error = gfs2_lock_fs_check_clean(sdp);
748 		if (!error) {
749 			set_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
750 			set_bit(SDF_FROZEN, &sdp->sd_flags);
751 			break;
752 		}
753 
754 		(void)gfs2_do_thaw(sdp, who, freeze_owner);
755 
756 		if (error == -EBUSY)
757 			fs_err(sdp, "waiting for recovery before freeze\n");
758 		else if (error == -EIO) {
759 			fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
760 			       "to recovery error.\n");
761 			goto out;
762 		} else {
763 			fs_err(sdp, "error freezing FS: %d\n", error);
764 		}
765 		fs_err(sdp, "retrying...\n");
766 		msleep(1000);
767 	}
768 
769 out:
770 	mutex_unlock(&sdp->sd_freeze_mutex);
771 	return error;
772 }
773 
gfs2_freeze_fs(struct super_block * sb)774 static int gfs2_freeze_fs(struct super_block *sb)
775 {
776 	struct gfs2_sbd *sdp = sb->s_fs_info;
777 
778 	if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
779 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
780 			       GFS2_LFC_FREEZE_GO_SYNC);
781 		if (gfs2_withdrawn(sdp))
782 			return -EIO;
783 	}
784 	return 0;
785 }
786 
787 /**
788  * gfs2_thaw_super - reallow writes to the filesystem
789  * @sb: the VFS structure for the filesystem
790  * @who: freeze flags
791  * @freeze_owner: owner of the freeze
792  *
793  */
794 
gfs2_thaw_super(struct super_block * sb,enum freeze_holder who,const void * freeze_owner)795 static int gfs2_thaw_super(struct super_block *sb, enum freeze_holder who,
796 			   const void *freeze_owner)
797 {
798 	struct gfs2_sbd *sdp = sb->s_fs_info;
799 	int error;
800 
801 	if (!mutex_trylock(&sdp->sd_freeze_mutex))
802 		return -EBUSY;
803 	if (!test_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags)) {
804 		mutex_unlock(&sdp->sd_freeze_mutex);
805 		return -EINVAL;
806 	}
807 
808 	atomic_inc(&sb->s_active);
809 	gfs2_freeze_unlock(sdp);
810 
811 	error = gfs2_do_thaw(sdp, who, freeze_owner);
812 
813 	if (!error) {
814 		clear_bit(SDF_FREEZE_INITIATOR, &sdp->sd_flags);
815 		clear_bit(SDF_FROZEN, &sdp->sd_flags);
816 	}
817 	mutex_unlock(&sdp->sd_freeze_mutex);
818 	deactivate_super(sb);
819 	return error;
820 }
821 
822 /**
823  * statfs_slow_fill - fill in the sg for a given RG
824  * @rgd: the RG
825  * @sc: the sc structure
826  *
827  * Returns: 0 on success, -ESTALE if the LVB is invalid
828  */
829 
statfs_slow_fill(struct gfs2_rgrpd * rgd,struct gfs2_statfs_change_host * sc)830 static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
831 			    struct gfs2_statfs_change_host *sc)
832 {
833 	gfs2_rgrp_verify(rgd);
834 	sc->sc_total += rgd->rd_data;
835 	sc->sc_free += rgd->rd_free;
836 	sc->sc_dinodes += rgd->rd_dinodes;
837 	return 0;
838 }
839 
840 /**
841  * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
842  * @sdp: the filesystem
843  * @sc: the sc info that will be returned
844  *
845  * Any error (other than a signal) will cause this routine to fall back
846  * to the synchronous version.
847  *
848  * FIXME: This really shouldn't busy wait like this.
849  *
850  * Returns: errno
851  */
852 
gfs2_statfs_slow(struct gfs2_sbd * sdp,struct gfs2_statfs_change_host * sc)853 static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
854 {
855 	struct gfs2_rgrpd *rgd_next;
856 	struct gfs2_holder *gha, *gh;
857 	unsigned int slots = 64;
858 	unsigned int x;
859 	int done;
860 	int error = 0, err;
861 
862 	memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
863 	gha = kmalloc_objs(struct gfs2_holder, slots);
864 	if (!gha)
865 		return -ENOMEM;
866 	for (x = 0; x < slots; x++)
867 		gfs2_holder_mark_uninitialized(gha + x);
868 
869 	rgd_next = gfs2_rgrpd_get_first(sdp);
870 
871 	for (;;) {
872 		done = 1;
873 
874 		for (x = 0; x < slots; x++) {
875 			gh = gha + x;
876 
877 			if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
878 				err = gfs2_glock_wait(gh);
879 				if (err) {
880 					gfs2_holder_uninit(gh);
881 					error = err;
882 				} else {
883 					if (!error) {
884 						struct gfs2_rgrpd *rgd =
885 							gfs2_glock2rgrp(gh->gh_gl);
886 
887 						error = statfs_slow_fill(rgd, sc);
888 					}
889 					gfs2_glock_dq_uninit(gh);
890 				}
891 			}
892 
893 			if (gfs2_holder_initialized(gh))
894 				done = 0;
895 			else if (rgd_next && !error) {
896 				error = gfs2_glock_nq_init(rgd_next->rd_gl,
897 							   LM_ST_SHARED,
898 							   GL_ASYNC,
899 							   gh);
900 				rgd_next = gfs2_rgrpd_get_next(rgd_next);
901 				done = 0;
902 			}
903 
904 			if (signal_pending(current))
905 				error = -ERESTARTSYS;
906 		}
907 
908 		if (done)
909 			break;
910 
911 		yield();
912 	}
913 
914 	kfree(gha);
915 	return error;
916 }
917 
918 /**
919  * gfs2_statfs_i - Do a statfs
920  * @sdp: the filesystem
921  * @sc: the sc structure
922  *
923  * Returns: errno
924  */
925 
gfs2_statfs_i(struct gfs2_sbd * sdp,struct gfs2_statfs_change_host * sc)926 static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
927 {
928 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
929 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
930 
931 	spin_lock(&sdp->sd_statfs_spin);
932 
933 	*sc = *m_sc;
934 	sc->sc_total += l_sc->sc_total;
935 	sc->sc_free += l_sc->sc_free;
936 	sc->sc_dinodes += l_sc->sc_dinodes;
937 
938 	spin_unlock(&sdp->sd_statfs_spin);
939 
940 	if (sc->sc_free < 0)
941 		sc->sc_free = 0;
942 	if (sc->sc_free > sc->sc_total)
943 		sc->sc_free = sc->sc_total;
944 	if (sc->sc_dinodes < 0)
945 		sc->sc_dinodes = 0;
946 
947 	return 0;
948 }
949 
950 /**
951  * gfs2_statfs - Gather and return stats about the filesystem
952  * @dentry: The name of the link
953  * @buf: The buffer
954  *
955  * Returns: 0 on success or error code
956  */
957 
gfs2_statfs(struct dentry * dentry,struct kstatfs * buf)958 static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
959 {
960 	struct super_block *sb = dentry->d_sb;
961 	struct gfs2_sbd *sdp = sb->s_fs_info;
962 	struct gfs2_statfs_change_host sc;
963 	int error;
964 
965 	error = gfs2_rindex_update(sdp);
966 	if (error)
967 		return error;
968 
969 	if (gfs2_tune_get(sdp, gt_statfs_slow))
970 		error = gfs2_statfs_slow(sdp, &sc);
971 	else
972 		error = gfs2_statfs_i(sdp, &sc);
973 
974 	if (error)
975 		return error;
976 
977 	buf->f_type = GFS2_MAGIC;
978 	buf->f_bsize = sdp->sd_sb.sb_bsize;
979 	buf->f_blocks = sc.sc_total;
980 	buf->f_bfree = sc.sc_free;
981 	buf->f_bavail = sc.sc_free;
982 	buf->f_files = sc.sc_dinodes + sc.sc_free;
983 	buf->f_ffree = sc.sc_free;
984 	buf->f_namelen = GFS2_FNAMESIZE;
985 	buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
986 
987 	return 0;
988 }
989 
990 /**
991  * gfs2_drop_inode - Drop an inode (test for remote unlink)
992  * @inode: The inode to drop
993  *
994  * If we've received a callback on an iopen lock then it's because a
995  * remote node tried to deallocate the inode but failed due to this node
996  * still having the inode open. Here we mark the link count zero
997  * since we know that it must have reached zero if the GLF_DEMOTE flag
998  * is set on the iopen glock. If we didn't do a disk read since the
999  * remote node removed the final link then we might otherwise miss
1000  * this event. This check ensures that this node will deallocate the
1001  * inode's blocks, or alternatively pass the baton on to another
1002  * node for later deallocation.
1003  */
1004 
gfs2_drop_inode(struct inode * inode)1005 static int gfs2_drop_inode(struct inode *inode)
1006 {
1007 	struct gfs2_inode *ip = GFS2_I(inode);
1008 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1009 
1010 	if (inode->i_nlink &&
1011 	    gfs2_holder_initialized(&ip->i_iopen_gh)) {
1012 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1013 		if (glock_needs_demote(gl))
1014 			clear_nlink(inode);
1015 	}
1016 
1017 	/*
1018 	 * When under memory pressure when an inode's link count has dropped to
1019 	 * zero, defer deleting the inode to the delete workqueue.  This avoids
1020 	 * calling into DLM under memory pressure, which can deadlock.
1021 	 */
1022 	if (!inode->i_nlink &&
1023 	    unlikely(current->flags & PF_MEMALLOC) &&
1024 	    gfs2_holder_initialized(&ip->i_iopen_gh)) {
1025 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1026 
1027 		gfs2_glock_hold(gl);
1028 		if (!gfs2_queue_verify_delete(gl, true))
1029 			gfs2_glock_put_async(gl);
1030 		return 0;
1031 	}
1032 
1033 	/*
1034 	 * No longer cache inodes when trying to evict them all.
1035 	 */
1036 	if (test_bit(SDF_EVICTING, &sdp->sd_flags))
1037 		return 1;
1038 
1039 	return inode_generic_drop(inode);
1040 }
1041 
1042 /**
1043  * gfs2_show_options - Show mount options for /proc/mounts
1044  * @s: seq_file structure
1045  * @root: root of this (sub)tree
1046  *
1047  * Returns: 0 on success or error code
1048  */
1049 
gfs2_show_options(struct seq_file * s,struct dentry * root)1050 static int gfs2_show_options(struct seq_file *s, struct dentry *root)
1051 {
1052 	struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
1053 	struct gfs2_args *args = &sdp->sd_args;
1054 	unsigned int logd_secs, statfs_slow, statfs_quantum, quota_quantum;
1055 
1056 	spin_lock(&sdp->sd_tune.gt_spin);
1057 	logd_secs = sdp->sd_tune.gt_logd_secs;
1058 	quota_quantum = sdp->sd_tune.gt_quota_quantum;
1059 	statfs_quantum = sdp->sd_tune.gt_statfs_quantum;
1060 	statfs_slow = sdp->sd_tune.gt_statfs_slow;
1061 	spin_unlock(&sdp->sd_tune.gt_spin);
1062 
1063 	if (is_subdir(root, sdp->sd_master_dir))
1064 		seq_puts(s, ",meta");
1065 	if (args->ar_lockproto[0])
1066 		seq_show_option(s, "lockproto", args->ar_lockproto);
1067 	if (args->ar_locktable[0])
1068 		seq_show_option(s, "locktable", args->ar_locktable);
1069 	if (args->ar_hostdata[0])
1070 		seq_show_option(s, "hostdata", args->ar_hostdata);
1071 	if (args->ar_spectator)
1072 		seq_puts(s, ",spectator");
1073 	if (args->ar_localflocks)
1074 		seq_puts(s, ",localflocks");
1075 	if (args->ar_debug)
1076 		seq_puts(s, ",debug");
1077 	if (args->ar_posix_acl)
1078 		seq_puts(s, ",acl");
1079 	if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1080 		char *state;
1081 		switch (args->ar_quota) {
1082 		case GFS2_QUOTA_OFF:
1083 			state = "off";
1084 			break;
1085 		case GFS2_QUOTA_ACCOUNT:
1086 			state = "account";
1087 			break;
1088 		case GFS2_QUOTA_ON:
1089 			state = "on";
1090 			break;
1091 		case GFS2_QUOTA_QUIET:
1092 			state = "quiet";
1093 			break;
1094 		default:
1095 			state = "unknown";
1096 			break;
1097 		}
1098 		seq_printf(s, ",quota=%s", state);
1099 	}
1100 	if (args->ar_suiddir)
1101 		seq_puts(s, ",suiddir");
1102 	if (args->ar_data != GFS2_DATA_DEFAULT) {
1103 		char *state;
1104 		switch (args->ar_data) {
1105 		case GFS2_DATA_WRITEBACK:
1106 			state = "writeback";
1107 			break;
1108 		case GFS2_DATA_ORDERED:
1109 			state = "ordered";
1110 			break;
1111 		default:
1112 			state = "unknown";
1113 			break;
1114 		}
1115 		seq_printf(s, ",data=%s", state);
1116 	}
1117 	if (args->ar_discard)
1118 		seq_puts(s, ",discard");
1119 	if (logd_secs != 30)
1120 		seq_printf(s, ",commit=%d", logd_secs);
1121 	if (statfs_quantum != 30)
1122 		seq_printf(s, ",statfs_quantum=%d", statfs_quantum);
1123 	else if (statfs_slow)
1124 		seq_puts(s, ",statfs_quantum=0");
1125 	if (quota_quantum != 60)
1126 		seq_printf(s, ",quota_quantum=%d", quota_quantum);
1127 	if (args->ar_statfs_percent)
1128 		seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
1129 	if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1130 		const char *state;
1131 
1132 		switch (args->ar_errors) {
1133 		case GFS2_ERRORS_WITHDRAW:
1134 			state = "withdraw";
1135 			break;
1136 		case GFS2_ERRORS_DEACTIVATE:
1137 			state = "deactivate";
1138 			break;
1139 		case GFS2_ERRORS_PANIC:
1140 			state = "panic";
1141 			break;
1142 		default:
1143 			state = "unknown";
1144 			break;
1145 		}
1146 		seq_printf(s, ",errors=%s", state);
1147 	}
1148 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
1149 		seq_puts(s, ",nobarrier");
1150 	if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
1151 		seq_puts(s, ",demote_interface_used");
1152 	if (args->ar_rgrplvb)
1153 		seq_puts(s, ",rgrplvb");
1154 	if (args->ar_loccookie)
1155 		seq_puts(s, ",loccookie");
1156 	return 0;
1157 }
1158 
1159 /**
1160  * gfs2_glock_put_eventually
1161  * @gl:	The glock to put
1162  *
1163  * When under memory pressure, trigger a deferred glock put to make sure we
1164  * won't call into DLM and deadlock.  Otherwise, put the glock directly.
1165  */
1166 
gfs2_glock_put_eventually(struct gfs2_glock * gl)1167 static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1168 {
1169 	if (current->flags & PF_MEMALLOC)
1170 		gfs2_glock_put_async(gl);
1171 	else
1172 		gfs2_glock_put(gl);
1173 }
1174 
gfs2_upgrade_iopen_glock(struct inode * inode)1175 static enum evict_behavior gfs2_upgrade_iopen_glock(struct inode *inode)
1176 {
1177 	struct gfs2_inode *ip = GFS2_I(inode);
1178 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1179 	struct gfs2_holder *gh = &ip->i_iopen_gh;
1180 	int error;
1181 
1182 	gh->gh_flags |= GL_NOCACHE;
1183 	gfs2_glock_dq_wait(gh);
1184 
1185 	/*
1186 	 * If there are no other lock holders, we will immediately get
1187 	 * exclusive access to the iopen glock here.
1188 	 *
1189 	 * Otherwise, the other nodes holding the lock will be notified about
1190 	 * our locking request (see iopen_go_callback()).  If they do not have
1191 	 * the inode open, they are expected to evict the cached inode and
1192 	 * release the lock, allowing us to proceed.
1193 	 *
1194 	 * Otherwise, if they cannot evict the inode, they are expected to poke
1195 	 * the inode glock (note: not the iopen glock).  We will notice that
1196 	 * and stop waiting for the iopen glock immediately.  The other node(s)
1197 	 * are then expected to take care of deleting the inode when they no
1198 	 * longer use it.
1199 	 *
1200 	 * As a last resort, if another node keeps holding the iopen glock
1201 	 * without showing any activity on the inode glock, we will eventually
1202 	 * time out and fail the iopen glock upgrade.
1203 	 */
1204 
1205 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1206 	error = gfs2_glock_nq(gh);
1207 	if (error)
1208 		return EVICT_SHOULD_SKIP_DELETE;
1209 
1210 	wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
1211 		!test_bit(HIF_WAIT, &gh->gh_iflags) ||
1212 		glock_needs_demote(ip->i_gl),
1213 		5 * HZ);
1214 	if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1215 		gfs2_glock_dq(gh);
1216 		if (glock_needs_demote(ip->i_gl))
1217 			return EVICT_SHOULD_SKIP_DELETE;
1218 		return EVICT_SHOULD_DEFER_DELETE;
1219 	}
1220 	error = gfs2_glock_holder_ready(gh);
1221 	if (error)
1222 		return EVICT_SHOULD_SKIP_DELETE;
1223 	return EVICT_SHOULD_DELETE;
1224 }
1225 
1226 /**
1227  * evict_should_delete - determine whether the inode is eligible for deletion
1228  * @inode: The inode to evict
1229  * @gh: The glock holder structure
1230  *
1231  * This function determines whether the evicted inode is eligible to be deleted
1232  * and locks the inode glock.
1233  *
1234  * Returns: the fate of the dinode
1235  */
evict_should_delete(struct inode * inode,struct gfs2_holder * gh)1236 static enum evict_behavior evict_should_delete(struct inode *inode,
1237 					       struct gfs2_holder *gh)
1238 {
1239 	struct gfs2_inode *ip = GFS2_I(inode);
1240 	struct super_block *sb = inode->i_sb;
1241 	struct gfs2_sbd *sdp = sb->s_fs_info;
1242 	int ret;
1243 
1244 	if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1245 	    test_bit(GLF_DEFER_DELETE, &ip->i_iopen_gh.gh_gl->gl_flags))
1246 		return EVICT_SHOULD_DEFER_DELETE;
1247 
1248 	/* Deletes should never happen under memory pressure anymore.  */
1249 	if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1250 		return EVICT_SHOULD_DEFER_DELETE;
1251 
1252 	/* Must not read inode block until block type has been verified */
1253 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
1254 	if (unlikely(ret))
1255 		return EVICT_SHOULD_SKIP_DELETE;
1256 
1257 	if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
1258 		return EVICT_SHOULD_SKIP_DELETE;
1259 	ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1260 	if (ret)
1261 		return EVICT_SHOULD_SKIP_DELETE;
1262 
1263 	ret = gfs2_instantiate(gh);
1264 	if (ret)
1265 		return EVICT_SHOULD_SKIP_DELETE;
1266 
1267 	/*
1268 	 * The inode may have been recreated in the meantime.
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(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags))
1275 		return gfs2_upgrade_iopen_glock(inode);
1276 	return EVICT_SHOULD_DELETE;
1277 }
1278 
1279 /**
1280  * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
1281  * @inode: The inode to evict
1282  */
evict_unlinked_inode(struct inode * inode)1283 static int evict_unlinked_inode(struct inode *inode)
1284 {
1285 	struct gfs2_inode *ip = GFS2_I(inode);
1286 	int ret;
1287 
1288 	if (S_ISDIR(inode->i_mode) &&
1289 	    (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1290 		ret = gfs2_dir_exhash_dealloc(ip);
1291 		if (ret)
1292 			goto out;
1293 	}
1294 
1295 	if (ip->i_eattr) {
1296 		ret = gfs2_ea_dealloc(ip, true);
1297 		if (ret)
1298 			goto out;
1299 	}
1300 
1301 	if (!gfs2_is_stuffed(ip)) {
1302 		ret = gfs2_file_dealloc(ip);
1303 		if (ret)
1304 			goto out;
1305 	}
1306 
1307 	/*
1308 	 * As soon as we clear the bitmap for the dinode, gfs2_create_inode()
1309 	 * can get called to recreate it, or even gfs2_inode_lookup() if the
1310 	 * inode was recreated on another node in the meantime.
1311 	 *
1312 	 * However, inserting the new inode into the inode hash table will not
1313 	 * succeed until the old inode is removed, and that only happens after
1314 	 * ->evict_inode() returns.  The new inode is attached to its inode and
1315 	 *  iopen glocks after inserting it into the inode hash table, so at
1316 	 *  that point we can be sure that both glocks are unused.
1317 	 */
1318 
1319 	ret = gfs2_dinode_dealloc(ip);
1320 	if (!ret && ip->i_gl)
1321 		gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
1322 
1323 out:
1324 	return ret;
1325 }
1326 
1327 /*
1328  * evict_linked_inode - evict an inode whose dinode has not been unlinked
1329  * @inode: The inode to evict
1330  */
evict_linked_inode(struct inode * inode)1331 static int evict_linked_inode(struct inode *inode)
1332 {
1333 	struct super_block *sb = inode->i_sb;
1334 	struct gfs2_sbd *sdp = sb->s_fs_info;
1335 	struct gfs2_inode *ip = GFS2_I(inode);
1336 	struct address_space *metamapping;
1337 	int ret;
1338 
1339 	gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1340 		       GFS2_LFC_EVICT_INODE);
1341 	metamapping = gfs2_glock2aspace(ip->i_gl);
1342 	if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1343 		filemap_fdatawrite(metamapping);
1344 		filemap_fdatawait(metamapping);
1345 	}
1346 	write_inode_now(inode, 1);
1347 	gfs2_ail_flush(ip->i_gl, 0);
1348 
1349 	ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1350 	if (ret)
1351 		return ret;
1352 
1353 	/* Needs to be done before glock release & also in a transaction */
1354 	truncate_inode_pages(&inode->i_data, 0);
1355 	truncate_inode_pages(metamapping, 0);
1356 	gfs2_trans_end(sdp);
1357 	return 0;
1358 }
1359 
1360 /**
1361  * gfs2_evict_inode - Remove an inode from cache
1362  * @inode: The inode to evict
1363  *
1364  * There are three cases to consider:
1365  * 1. i_nlink == 0, we are final opener (and must deallocate)
1366  * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1367  * 3. i_nlink > 0
1368  *
1369  * If the fs is read only, then we have to treat all cases as per #3
1370  * since we are unable to do any deallocation. The inode will be
1371  * deallocated by the next read/write node to attempt an allocation
1372  * in the same resource group
1373  *
1374  * We have to (at the moment) hold the inodes main lock to cover
1375  * the gap between unlocking the shared lock on the iopen lock and
1376  * taking the exclusive lock. I'd rather do a shared -> exclusive
1377  * conversion on the iopen lock, but we can change that later. This
1378  * is safe, just less efficient.
1379  */
1380 
gfs2_evict_inode(struct inode * inode)1381 static void gfs2_evict_inode(struct inode *inode)
1382 {
1383 	struct super_block *sb = inode->i_sb;
1384 	struct gfs2_sbd *sdp = sb->s_fs_info;
1385 	struct gfs2_inode *ip = GFS2_I(inode);
1386 	struct gfs2_holder gh;
1387 	enum evict_behavior behavior;
1388 	int ret;
1389 
1390 	gfs2_holder_mark_uninitialized(&gh);
1391 	if (inode->i_nlink || sb_rdonly(sb) || !ip->i_no_addr)
1392 		goto out;
1393 
1394 	/*
1395 	 * In case of an incomplete mount, gfs2_evict_inode() may be called for
1396 	 * system files without having an active journal to write to.  In that
1397 	 * case, skip the filesystem evict.
1398 	 */
1399 	if (!sdp->sd_jdesc)
1400 		goto out;
1401 
1402 	behavior = evict_should_delete(inode, &gh);
1403 	if (behavior == EVICT_SHOULD_DEFER_DELETE &&
1404 	    !test_bit(SDF_KILL, &sdp->sd_flags)) {
1405 		struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
1406 
1407 		if (io_gl) {
1408 			gfs2_glock_hold(io_gl);
1409 			if (!gfs2_queue_verify_delete(io_gl, true))
1410 				gfs2_glock_put(io_gl);
1411 			goto out;
1412 		}
1413 		behavior = EVICT_SHOULD_SKIP_DELETE;
1414 	}
1415 	if (behavior == EVICT_SHOULD_DELETE)
1416 		ret = evict_unlinked_inode(inode);
1417 	else
1418 		ret = evict_linked_inode(inode);
1419 
1420 	if (gfs2_rs_active(&ip->i_res))
1421 		gfs2_rs_deltree(&ip->i_res);
1422 
1423 	if (ret && ret != GLR_TRYFAILED && ret != -EROFS)
1424 		fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
1425 out:
1426 	if (gfs2_holder_initialized(&gh))
1427 		gfs2_glock_dq_uninit(&gh);
1428 	truncate_inode_pages_final(&inode->i_data);
1429 	if (ip->i_qadata)
1430 		gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
1431 	gfs2_rs_deltree(&ip->i_res);
1432 	gfs2_ordered_del_inode(ip);
1433 	clear_inode(inode);
1434 	gfs2_dir_hash_inval(ip);
1435 	if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
1436 		struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1437 
1438 		glock_clear_object(gl, ip);
1439 		gfs2_glock_hold(gl);
1440 		ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1441 		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1442 		gfs2_glock_put_eventually(gl);
1443 	}
1444 	if (ip->i_gl) {
1445 		glock_clear_object(ip->i_gl, ip);
1446 		wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1447 		gfs2_glock_put_eventually(ip->i_gl);
1448 		rcu_assign_pointer(ip->i_gl, NULL);
1449 	}
1450 }
1451 
gfs2_alloc_inode(struct super_block * sb)1452 static struct inode *gfs2_alloc_inode(struct super_block *sb)
1453 {
1454 	struct gfs2_inode *ip;
1455 
1456 	ip = alloc_inode_sb(sb, gfs2_inode_cachep, GFP_KERNEL);
1457 	if (!ip)
1458 		return NULL;
1459 	ip->i_no_addr = 0;
1460 	ip->i_no_formal_ino = 0;
1461 	ip->i_flags = 0;
1462 	ip->i_gl = NULL;
1463 	gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
1464 	memset(&ip->i_res, 0, sizeof(ip->i_res));
1465 	RB_CLEAR_NODE(&ip->i_res.rs_node);
1466 	ip->i_diskflags = 0;
1467 	ip->i_rahead = 0;
1468 	return &ip->i_inode;
1469 }
1470 
gfs2_free_inode(struct inode * inode)1471 static void gfs2_free_inode(struct inode *inode)
1472 {
1473 	kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
1474 }
1475 
free_local_statfs_inodes(struct gfs2_sbd * sdp)1476 void free_local_statfs_inodes(struct gfs2_sbd *sdp)
1477 {
1478 	struct local_statfs_inode *lsi, *safe;
1479 
1480 	/* Run through the statfs inodes list to iput and free memory */
1481 	list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
1482 		if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
1483 			sdp->sd_sc_inode = NULL; /* belongs to this node */
1484 		if (lsi->si_sc_inode)
1485 			iput(lsi->si_sc_inode);
1486 		list_del(&lsi->si_list);
1487 		kfree(lsi);
1488 	}
1489 }
1490 
find_local_statfs_inode(struct gfs2_sbd * sdp,unsigned int index)1491 struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
1492 				      unsigned int index)
1493 {
1494 	struct local_statfs_inode *lsi;
1495 
1496 	/* Return the local (per node) statfs inode in the
1497 	 * sdp->sd_sc_inodes_list corresponding to the 'index'. */
1498 	list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
1499 		if (lsi->si_jid == index)
1500 			return lsi->si_sc_inode;
1501 	}
1502 	return NULL;
1503 }
1504 
1505 const struct super_operations gfs2_super_ops = {
1506 	.alloc_inode		= gfs2_alloc_inode,
1507 	.free_inode		= gfs2_free_inode,
1508 	.write_inode		= gfs2_write_inode,
1509 	.dirty_inode		= gfs2_dirty_inode,
1510 	.evict_inode		= gfs2_evict_inode,
1511 	.put_super		= gfs2_put_super,
1512 	.sync_fs		= gfs2_sync_fs,
1513 	.freeze_super		= gfs2_freeze_super,
1514 	.freeze_fs		= gfs2_freeze_fs,
1515 	.thaw_super		= gfs2_thaw_super,
1516 	.statfs			= gfs2_statfs,
1517 	.drop_inode		= gfs2_drop_inode,
1518 	.show_options		= gfs2_show_options,
1519 };
1520 
1521