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