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