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