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