gfs2: Check for log write errors before telling dlm to unlock
[linux-2.6-block.git] / fs / gfs2 / glops.c
CommitLineData
7336d0e6 1// SPDX-License-Identifier: GPL-2.0-only
b3b94faa
DT
2/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
cf45b752 4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
b3b94faa
DT
5 */
6
b3b94faa
DT
7#include <linux/spinlock.h>
8#include <linux/completion.h>
9#include <linux/buffer_head.h>
5c676f6d 10#include <linux/gfs2_ondisk.h>
6802e340 11#include <linux/bio.h>
c65f7fb5 12#include <linux/posix_acl.h>
f39814f6 13#include <linux/security.h>
b3b94faa
DT
14
15#include "gfs2.h"
5c676f6d 16#include "incore.h"
b3b94faa
DT
17#include "bmap.h"
18#include "glock.h"
19#include "glops.h"
20#include "inode.h"
21#include "log.h"
22#include "meta_io.h"
b3b94faa
DT
23#include "recovery.h"
24#include "rgrp.h"
5c676f6d 25#include "util.h"
ddacfaf7 26#include "trans.h"
17d539f0 27#include "dir.h"
f4686c26 28#include "lops.h"
b3b94faa 29
2e60d768
BM
30struct workqueue_struct *gfs2_freeze_wq;
31
601ef0d5
BP
32extern struct workqueue_struct *gfs2_control_wq;
33
75549186
SW
34static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
35{
15562c43
BP
36 fs_err(gl->gl_name.ln_sbd,
37 "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
38 "state 0x%lx\n",
75549186
SW
39 bh, (unsigned long long)bh->b_blocknr, bh->b_state,
40 bh->b_page->mapping, bh->b_page->flags);
15562c43 41 fs_err(gl->gl_name.ln_sbd, "AIL glock %u:%llu mapping %p\n",
75549186
SW
42 gl->gl_name.ln_type, gl->gl_name.ln_number,
43 gfs2_glock2aspace(gl));
badb55ec
AG
44 gfs2_lm(gl->gl_name.ln_sbd, "AIL error\n");
45 gfs2_withdraw(gl->gl_name.ln_sbd);
75549186
SW
46}
47
ddacfaf7 48/**
dba898b0 49 * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
ddacfaf7 50 * @gl: the glock
b5b24d7a 51 * @fsync: set when called from fsync (not all buffers will be clean)
ddacfaf7
SW
52 *
53 * None of the buffers should be dirty, locked, or pinned.
54 */
55
1bc333f4
BM
56static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
57 unsigned int nr_revokes)
ddacfaf7 58{
15562c43 59 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
ddacfaf7 60 struct list_head *head = &gl->gl_ail_list;
b5b24d7a 61 struct gfs2_bufdata *bd, *tmp;
ddacfaf7 62 struct buffer_head *bh;
b5b24d7a 63 const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
d8348de0 64
b5b24d7a 65 gfs2_log_lock(sdp);
d6a079e8 66 spin_lock(&sdp->sd_ail_lock);
1bc333f4
BM
67 list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
68 if (nr_revokes == 0)
69 break;
ddacfaf7 70 bh = bd->bd_bh;
b5b24d7a
SW
71 if (bh->b_state & b_state) {
72 if (fsync)
73 continue;
75549186 74 gfs2_ail_error(gl, bh);
b5b24d7a 75 }
1ad38c43 76 gfs2_trans_add_revoke(sdp, bd);
1bc333f4 77 nr_revokes--;
ddacfaf7 78 }
8eae1ca0 79 GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
d6a079e8 80 spin_unlock(&sdp->sd_ail_lock);
b5b24d7a 81 gfs2_log_unlock(sdp);
dba898b0
SW
82}
83
84
85static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
86{
15562c43 87 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
dba898b0
SW
88 struct gfs2_trans tr;
89
90 memset(&tr, 0, sizeof(tr));
d69a3c65
SW
91 INIT_LIST_HEAD(&tr.tr_buf);
92 INIT_LIST_HEAD(&tr.tr_databuf);
dba898b0
SW
93 tr.tr_revokes = atomic_read(&gl->gl_ail_count);
94
95 if (!tr.tr_revokes)
96 return;
97
24972557
BM
98 /* A shortened, inline version of gfs2_trans_begin()
99 * tr->alloced is not set since the transaction structure is
100 * on the stack */
2e9eeaa1 101 tr.tr_reserved = 1 + gfs2_struct2blk(sdp, tr.tr_revokes);
d29c0afe 102 tr.tr_ip = _RET_IP_;
2e60d768 103 if (gfs2_log_reserve(sdp, tr.tr_reserved) < 0)
24972557 104 return;
8eae1ca0 105 WARN_ON_ONCE(current->journal_info);
dba898b0
SW
106 current->journal_info = &tr;
107
1bc333f4 108 __gfs2_ail_flush(gl, 0, tr.tr_revokes);
dba898b0
SW
109
110 gfs2_trans_end(sdp);
805c0907
BP
111 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
112 GFS2_LFC_AIL_EMPTY_GL);
dba898b0 113}
ddacfaf7 114
b5b24d7a 115void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
dba898b0 116{
15562c43 117 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
dba898b0 118 unsigned int revokes = atomic_read(&gl->gl_ail_count);
1bc333f4 119 unsigned int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
dba898b0
SW
120 int ret;
121
122 if (!revokes)
123 return;
124
1bc333f4
BM
125 while (revokes > max_revokes)
126 max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
127
128 ret = gfs2_trans_begin(sdp, 0, max_revokes);
dba898b0
SW
129 if (ret)
130 return;
1bc333f4 131 __gfs2_ail_flush(gl, fsync, max_revokes);
ddacfaf7 132 gfs2_trans_end(sdp);
805c0907
BP
133 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
134 GFS2_LFC_AIL_FLUSH);
ddacfaf7 135}
ba7f7290
SW
136
137/**
6bac243f 138 * rgrp_go_sync - sync out the metadata for this glock
b3b94faa 139 * @gl: the glock
b3b94faa
DT
140 *
141 * Called when demoting or unlocking an EX glock. We must flush
142 * to disk all dirty buffers/pages relating to this glock, and must not
6f6597ba 143 * return to caller to demote/unlock the glock until I/O is complete.
b3b94faa
DT
144 */
145
6bac243f 146static void rgrp_go_sync(struct gfs2_glock *gl)
b3b94faa 147{
15562c43 148 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
70d4ee94 149 struct address_space *mapping = &sdp->sd_aspace;
b3422cac 150 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
6bac243f
SW
151 int error;
152
153 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
b5d32bea 154 return;
8eae1ca0 155 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
b5d32bea 156
805c0907
BP
157 gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
158 GFS2_LFC_RGRP_GO_SYNC);
70d4ee94
SW
159 filemap_fdatawrite_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
160 error = filemap_fdatawait_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
161 mapping_set_error(mapping, error);
6bac243f 162 gfs2_ail_empty_gl(gl);
7c9ca621 163
f3dd1649 164 spin_lock(&gl->gl_lockref.lock);
8339ee54
SW
165 rgd = gl->gl_object;
166 if (rgd)
167 gfs2_free_clones(rgd);
f3dd1649 168 spin_unlock(&gl->gl_lockref.lock);
b3b94faa
DT
169}
170
171/**
6bac243f 172 * rgrp_go_inval - invalidate the metadata for this glock
b3b94faa
DT
173 * @gl: the glock
174 * @flags:
175 *
6bac243f
SW
176 * We never used LM_ST_DEFERRED with resource groups, so that we
177 * should always see the metadata flag set here.
178 *
b3b94faa
DT
179 */
180
6bac243f 181static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
b3b94faa 182{
15562c43 183 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
70d4ee94 184 struct address_space *mapping = &sdp->sd_aspace;
6f6597ba 185 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
39b0f1e9
BP
186
187 if (rgd)
188 gfs2_rgrp_brelse(rgd);
b3b94faa 189
8eae1ca0 190 WARN_ON_ONCE(!(flags & DIO_METADATA));
7005c3e4 191 truncate_inode_pages_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
cf45b752 192
39b0f1e9 193 if (rgd)
cf45b752 194 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
b3b94faa
DT
195}
196
4fd1a579
AG
197static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
198{
199 struct gfs2_inode *ip;
200
201 spin_lock(&gl->gl_lockref.lock);
202 ip = gl->gl_object;
203 if (ip)
204 set_bit(GIF_GLOP_PENDING, &ip->i_flags);
205 spin_unlock(&gl->gl_lockref.lock);
206 return ip;
207}
208
6f6597ba
AG
209struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
210{
211 struct gfs2_rgrpd *rgd;
212
213 spin_lock(&gl->gl_lockref.lock);
214 rgd = gl->gl_object;
215 spin_unlock(&gl->gl_lockref.lock);
216
217 return rgd;
218}
219
4fd1a579
AG
220static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
221{
222 if (!ip)
223 return;
224
225 clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
226 wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
227}
228
b5d32bea
SW
229/**
230 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
231 * @gl: the glock protecting the inode
232 *
233 */
234
235static void inode_go_sync(struct gfs2_glock *gl)
236{
4fd1a579
AG
237 struct gfs2_inode *ip = gfs2_glock2inode(gl);
238 int isreg = ip && S_ISREG(ip->i_inode.i_mode);
009d8518 239 struct address_space *metamapping = gfs2_glock2aspace(gl);
3042a2cc
SW
240 int error;
241
4fd1a579 242 if (isreg) {
582d2f7a
SW
243 if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
244 unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
245 inode_dio_wait(&ip->i_inode);
246 }
6bac243f 247 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
4fd1a579 248 goto out;
b5d32bea 249
8eae1ca0 250 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
6bac243f 251
805c0907
BP
252 gfs2_log_flush(gl->gl_name.ln_sbd, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
253 GFS2_LFC_INODE_GO_SYNC);
6bac243f 254 filemap_fdatawrite(metamapping);
4fd1a579 255 if (isreg) {
6bac243f
SW
256 struct address_space *mapping = ip->i_inode.i_mapping;
257 filemap_fdatawrite(mapping);
258 error = filemap_fdatawait(mapping);
259 mapping_set_error(mapping, error);
b5d32bea 260 }
6bac243f
SW
261 error = filemap_fdatawait(metamapping);
262 mapping_set_error(metamapping, error);
263 gfs2_ail_empty_gl(gl);
52fcd11c
SW
264 /*
265 * Writeback of the data mapping may cause the dirty flag to be set
266 * so we have to clear it again here.
267 */
4e857c58 268 smp_mb__before_atomic();
52fcd11c 269 clear_bit(GLF_DIRTY, &gl->gl_flags);
4fd1a579
AG
270
271out:
272 gfs2_clear_glop_pending(ip);
b5d32bea
SW
273}
274
b3b94faa
DT
275/**
276 * inode_go_inval - prepare a inode glock to be released
277 * @gl: the glock
278 * @flags:
6b49d1d9
GU
279 *
280 * Normally we invalidate everything, but if we are moving into
6bac243f
SW
281 * LM_ST_DEFERRED from LM_ST_SHARED or LM_ST_EXCLUSIVE then we
282 * can keep hold of the metadata, since it won't have changed.
b3b94faa
DT
283 *
284 */
285
286static void inode_go_inval(struct gfs2_glock *gl, int flags)
287{
4fd1a579 288 struct gfs2_inode *ip = gfs2_glock2inode(gl);
b3b94faa 289
6bac243f 290 if (flags & DIO_METADATA) {
009d8518 291 struct address_space *mapping = gfs2_glock2aspace(gl);
6bac243f 292 truncate_inode_pages(mapping, 0);
c65f7fb5 293 if (ip) {
b004157a 294 set_bit(GIF_INVALID, &ip->i_flags);
c65f7fb5 295 forget_all_cached_acls(&ip->i_inode);
f39814f6 296 security_inode_invalidate_secctx(&ip->i_inode);
17d539f0 297 gfs2_dir_hash_inval(ip);
c65f7fb5 298 }
b004157a
SW
299 }
300
15562c43 301 if (ip == GFS2_I(gl->gl_name.ln_sbd->sd_rindex)) {
c1696fb8 302 gfs2_log_flush(gl->gl_name.ln_sbd, NULL,
805c0907
BP
303 GFS2_LOG_HEAD_FLUSH_NORMAL |
304 GFS2_LFC_INODE_GO_INVAL);
15562c43 305 gl->gl_name.ln_sbd->sd_rindex_uptodate = 0;
1ce53368 306 }
3cc3f710 307 if (ip && S_ISREG(ip->i_inode.i_mode))
b004157a 308 truncate_inode_pages(ip->i_inode.i_mapping, 0);
4fd1a579
AG
309
310 gfs2_clear_glop_pending(ip);
b3b94faa
DT
311}
312
313/**
314 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
315 * @gl: the glock
316 *
317 * Returns: 1 if it's ok
318 */
319
97cc1025 320static int inode_go_demote_ok(const struct gfs2_glock *gl)
b3b94faa 321{
15562c43 322 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
bc015cb8 323
97cc1025
SW
324 if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
325 return 0;
bc015cb8 326
97cc1025 327 return 1;
b3b94faa
DT
328}
329
d4b2cf1b
SW
330static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
331{
332 const struct gfs2_dinode *str = buf;
95582b00 333 struct timespec64 atime;
d4b2cf1b
SW
334 u16 height, depth;
335
336 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
337 goto corrupt;
338 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
339 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
340 ip->i_inode.i_rdev = 0;
341 switch (ip->i_inode.i_mode & S_IFMT) {
342 case S_IFBLK:
343 case S_IFCHR:
344 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
345 be32_to_cpu(str->di_minor));
346 break;
098b9c14 347 }
d4b2cf1b 348
d0546426
EB
349 i_uid_write(&ip->i_inode, be32_to_cpu(str->di_uid));
350 i_gid_write(&ip->i_inode, be32_to_cpu(str->di_gid));
eebd2e81 351 set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
d4b2cf1b
SW
352 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
353 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
354 atime.tv_sec = be64_to_cpu(str->di_atime);
355 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
95582b00 356 if (timespec64_compare(&ip->i_inode.i_atime, &atime) < 0)
d4b2cf1b
SW
357 ip->i_inode.i_atime = atime;
358 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
359 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
360 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
361 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
362
363 ip->i_goal = be64_to_cpu(str->di_goal_meta);
364 ip->i_generation = be64_to_cpu(str->di_generation);
365
366 ip->i_diskflags = be32_to_cpu(str->di_flags);
9964afbb
SW
367 ip->i_eattr = be64_to_cpu(str->di_eattr);
368 /* i_diskflags and i_eattr must be set before gfs2_set_inode_flags() */
d4b2cf1b
SW
369 gfs2_set_inode_flags(&ip->i_inode);
370 height = be16_to_cpu(str->di_height);
371 if (unlikely(height > GFS2_MAX_META_HEIGHT))
372 goto corrupt;
373 ip->i_height = (u8)height;
374
375 depth = be16_to_cpu(str->di_depth);
376 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
377 goto corrupt;
378 ip->i_depth = (u8)depth;
379 ip->i_entries = be32_to_cpu(str->di_entries);
380
d4b2cf1b
SW
381 if (S_ISREG(ip->i_inode.i_mode))
382 gfs2_set_aops(&ip->i_inode);
383
384 return 0;
385corrupt:
386 gfs2_consist_inode(ip);
387 return -EIO;
388}
389
390/**
391 * gfs2_inode_refresh - Refresh the incore copy of the dinode
392 * @ip: The GFS2 inode
393 *
394 * Returns: errno
395 */
396
397int gfs2_inode_refresh(struct gfs2_inode *ip)
398{
399 struct buffer_head *dibh;
400 int error;
401
402 error = gfs2_meta_inode_buffer(ip, &dibh);
403 if (error)
404 return error;
405
d4b2cf1b
SW
406 error = gfs2_dinode_in(ip, dibh->b_data);
407 brelse(dibh);
408 clear_bit(GIF_INVALID, &ip->i_flags);
409
410 return error;
411}
412
b3b94faa
DT
413/**
414 * inode_go_lock - operation done after an inode lock is locked by a process
415 * @gl: the glock
416 * @flags:
417 *
418 * Returns: errno
419 */
420
421static int inode_go_lock(struct gfs2_holder *gh)
422{
423 struct gfs2_glock *gl = gh->gh_gl;
15562c43 424 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
5c676f6d 425 struct gfs2_inode *ip = gl->gl_object;
b3b94faa
DT
426 int error = 0;
427
091806ed 428 if (!ip || (gh->gh_flags & GL_SKIP))
b3b94faa
DT
429 return 0;
430
bfded27b 431 if (test_bit(GIF_INVALID, &ip->i_flags)) {
b3b94faa
DT
432 error = gfs2_inode_refresh(ip);
433 if (error)
434 return error;
b3b94faa
DT
435 }
436
582d2f7a
SW
437 if (gh->gh_state != LM_ST_DEFERRED)
438 inode_dio_wait(&ip->i_inode);
439
383f01fb 440 if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
b3b94faa 441 (gl->gl_state == LM_ST_EXCLUSIVE) &&
813e0c46
SW
442 (gh->gh_state == LM_ST_EXCLUSIVE)) {
443 spin_lock(&sdp->sd_trunc_lock);
444 if (list_empty(&ip->i_trunc_list))
e7cb550d 445 list_add(&ip->i_trunc_list, &sdp->sd_trunc_list);
813e0c46
SW
446 spin_unlock(&sdp->sd_trunc_lock);
447 wake_up(&sdp->sd_quota_wait);
448 return 1;
449 }
b3b94faa
DT
450
451 return error;
452}
453
6802e340
SW
454/**
455 * inode_go_dump - print information about an inode
456 * @seq: The iterator
457 * @ip: the inode
3792ce97 458 * @fs_id_buf: file system id (may be empty)
6802e340 459 *
6802e340
SW
460 */
461
3792ce97
BP
462static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
463 const char *fs_id_buf)
6802e340 464{
27a2660f
BP
465 struct gfs2_inode *ip = gl->gl_object;
466 struct inode *inode = &ip->i_inode;
467 unsigned long nrpages;
468
6802e340 469 if (ip == NULL)
ac3beb6a 470 return;
27a2660f
BP
471
472 xa_lock_irq(&inode->i_data.i_pages);
473 nrpages = inode->i_data.nrpages;
474 xa_unlock_irq(&inode->i_data.i_pages);
475
3792ce97
BP
476 gfs2_print_dbg(seq, "%s I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu "
477 "p:%lu\n", fs_id_buf,
6802e340
SW
478 (unsigned long long)ip->i_no_formal_ino,
479 (unsigned long long)ip->i_no_addr,
fa75cedc
SW
480 IF2DT(ip->i_inode.i_mode), ip->i_flags,
481 (unsigned int)ip->i_diskflags,
27a2660f 482 (unsigned long long)i_size_read(inode), nrpages);
6802e340
SW
483}
484
b3b94faa 485/**
24972557 486 * freeze_go_sync - promote/demote the freeze glock
b3b94faa
DT
487 * @gl: the glock
488 * @state: the requested state
489 * @flags:
490 *
491 */
492
24972557 493static void freeze_go_sync(struct gfs2_glock *gl)
b3b94faa 494{
2e60d768 495 int error = 0;
15562c43 496 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
b3b94faa 497
601ef0d5 498 if (gl->gl_state == LM_ST_SHARED && !gfs2_withdrawn(sdp) &&
b3b94faa 499 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
2e60d768
BM
500 atomic_set(&sdp->sd_freeze_state, SFS_STARTING_FREEZE);
501 error = freeze_super(sdp->sd_vfs);
502 if (error) {
f29e62ee
BP
503 fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
504 error);
601ef0d5
BP
505 if (gfs2_withdrawn(sdp)) {
506 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
507 return;
508 }
2e60d768
BM
509 gfs2_assert_withdraw(sdp, 0);
510 }
511 queue_work(gfs2_freeze_wq, &sdp->sd_freeze_work);
805c0907
BP
512 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
513 GFS2_LFC_FREEZE_GO_SYNC);
b3b94faa 514 }
b3b94faa
DT
515}
516
517/**
24972557 518 * freeze_go_xmote_bh - After promoting/demoting the freeze glock
b3b94faa
DT
519 * @gl: the glock
520 *
521 */
522
24972557 523static int freeze_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
b3b94faa 524{
15562c43 525 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
feaa7bba 526 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
5c676f6d 527 struct gfs2_glock *j_gl = ip->i_gl;
55167622 528 struct gfs2_log_header_host head;
b3b94faa
DT
529 int error;
530
6802e340 531 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
1a14d3a6 532 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
b3b94faa 533
f4686c26 534 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
b3b94faa
DT
535 if (error)
536 gfs2_consist(sdp);
537 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
538 gfs2_consist(sdp);
539
540 /* Initialize some head of the log stuff */
eb43e660 541 if (!gfs2_withdrawn(sdp)) {
b3b94faa
DT
542 sdp->sd_log_sequence = head.lh_sequence + 1;
543 gfs2_log_pointers_init(sdp, head.lh_blkno);
544 }
545 }
6802e340 546 return 0;
b3b94faa
DT
547}
548
97cc1025
SW
549/**
550 * trans_go_demote_ok
551 * @gl: the glock
552 *
553 * Always returns 0
554 */
555
24972557 556static int freeze_go_demote_ok(const struct gfs2_glock *gl)
97cc1025
SW
557{
558 return 0;
559}
560
b94a170e
BM
561/**
562 * iopen_go_callback - schedule the dcache entry for the inode to be deleted
563 * @gl: the glock
564 *
f3dd1649 565 * gl_lockref.lock lock is held while calling this
b94a170e 566 */
81ffbf65 567static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
b94a170e 568{
6f6597ba 569 struct gfs2_inode *ip = gl->gl_object;
15562c43 570 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
001e8e8d 571
bc98a42c 572 if (!remote || sb_rdonly(sdp->sd_vfs))
001e8e8d 573 return;
b94a170e
BM
574
575 if (gl->gl_demote_state == LM_ST_UNLOCKED &&
009d8518 576 gl->gl_state == LM_ST_SHARED && ip) {
e66cf161 577 gl->gl_lockref.count++;
b94a170e 578 if (queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
e66cf161 579 gl->gl_lockref.count--;
b94a170e
BM
580 }
581}
582
601ef0d5
BP
583/**
584 * inode_go_free - wake up anyone waiting for dlm's unlock ast to free it
585 * @gl: glock being freed
586 *
587 * For now, this is only used for the journal inode glock. In withdraw
588 * situations, we need to wait for the glock to be freed so that we know
589 * other nodes may proceed with recovery / journal replay.
590 */
591static void inode_go_free(struct gfs2_glock *gl)
592{
593 /* Note that we cannot reference gl_object because it's already set
594 * to NULL by this point in its lifecycle. */
595 if (!test_bit(GLF_FREEING, &gl->gl_flags))
596 return;
597 clear_bit_unlock(GLF_FREEING, &gl->gl_flags);
598 wake_up_bit(&gl->gl_flags, GLF_FREEING);
599}
600
601/**
602 * nondisk_go_callback - used to signal when a node did a withdraw
603 * @gl: the nondisk glock
604 * @remote: true if this came from a different cluster node
605 *
606 */
607static void nondisk_go_callback(struct gfs2_glock *gl, bool remote)
608{
609 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
610
611 /* Ignore the callback unless it's from another node, and it's the
612 live lock. */
613 if (!remote || gl->gl_name.ln_number != GFS2_LIVE_LOCK)
614 return;
615
616 /* First order of business is to cancel the demote request. We don't
617 * really want to demote a nondisk glock. At best it's just to inform
618 * us of another node's withdraw. We'll keep it in SH mode. */
619 clear_bit(GLF_DEMOTE, &gl->gl_flags);
620 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
621
622 /* Ignore the unlock if we're withdrawn, unmounting, or in recovery. */
623 if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) ||
624 test_bit(SDF_WITHDRAWN, &sdp->sd_flags) ||
625 test_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags))
626 return;
627
628 /* We only care when a node wants us to unlock, because that means
629 * they want a journal recovered. */
630 if (gl->gl_demote_state != LM_ST_UNLOCKED)
631 return;
632
633 if (sdp->sd_args.ar_spectator) {
634 fs_warn(sdp, "Spectator node cannot recover journals.\n");
635 return;
636 }
637
638 fs_warn(sdp, "Some node has withdrawn; checking for recovery.\n");
639 set_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags);
640 /*
641 * We can't call remote_withdraw directly here or gfs2_recover_journal
642 * because this is called from the glock unlock function and the
643 * remote_withdraw needs to enqueue and dequeue the same "live" glock
644 * we were called from. So we queue it to the control work queue in
645 * lock_dlm.
646 */
647 queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
648}
649
8fb4b536 650const struct gfs2_glock_operations gfs2_meta_glops = {
ea67eedb 651 .go_type = LM_TYPE_META,
a72d2401 652 .go_flags = GLOF_NONDISK,
b3b94faa
DT
653};
654
8fb4b536 655const struct gfs2_glock_operations gfs2_inode_glops = {
06dfc306 656 .go_sync = inode_go_sync,
b3b94faa
DT
657 .go_inval = inode_go_inval,
658 .go_demote_ok = inode_go_demote_ok,
659 .go_lock = inode_go_lock,
6802e340 660 .go_dump = inode_go_dump,
ea67eedb 661 .go_type = LM_TYPE_INODE,
e7ccaf5f 662 .go_flags = GLOF_ASPACE | GLOF_LRU,
601ef0d5 663 .go_free = inode_go_free,
b3b94faa
DT
664};
665
8fb4b536 666const struct gfs2_glock_operations gfs2_rgrp_glops = {
06dfc306 667 .go_sync = rgrp_go_sync,
6bac243f 668 .go_inval = rgrp_go_inval,
7c9ca621 669 .go_lock = gfs2_rgrp_go_lock,
09010978 670 .go_dump = gfs2_rgrp_dump,
ea67eedb 671 .go_type = LM_TYPE_RGRP,
70d4ee94 672 .go_flags = GLOF_LVB,
b3b94faa
DT
673};
674
24972557
BM
675const struct gfs2_glock_operations gfs2_freeze_glops = {
676 .go_sync = freeze_go_sync,
677 .go_xmote_bh = freeze_go_xmote_bh,
678 .go_demote_ok = freeze_go_demote_ok,
ea67eedb 679 .go_type = LM_TYPE_NONDISK,
a72d2401 680 .go_flags = GLOF_NONDISK,
b3b94faa
DT
681};
682
8fb4b536 683const struct gfs2_glock_operations gfs2_iopen_glops = {
ea67eedb 684 .go_type = LM_TYPE_IOPEN,
b94a170e 685 .go_callback = iopen_go_callback,
a72d2401 686 .go_flags = GLOF_LRU | GLOF_NONDISK,
b3b94faa
DT
687};
688
8fb4b536 689const struct gfs2_glock_operations gfs2_flock_glops = {
ea67eedb 690 .go_type = LM_TYPE_FLOCK,
a72d2401 691 .go_flags = GLOF_LRU | GLOF_NONDISK,
b3b94faa
DT
692};
693
8fb4b536 694const struct gfs2_glock_operations gfs2_nondisk_glops = {
ea67eedb 695 .go_type = LM_TYPE_NONDISK,
a72d2401 696 .go_flags = GLOF_NONDISK,
601ef0d5 697 .go_callback = nondisk_go_callback,
b3b94faa
DT
698};
699
8fb4b536 700const struct gfs2_glock_operations gfs2_quota_glops = {
ea67eedb 701 .go_type = LM_TYPE_QUOTA,
a72d2401 702 .go_flags = GLOF_LVB | GLOF_LRU | GLOF_NONDISK,
b3b94faa
DT
703};
704
8fb4b536 705const struct gfs2_glock_operations gfs2_journal_glops = {
ea67eedb 706 .go_type = LM_TYPE_JOURNAL,
a72d2401 707 .go_flags = GLOF_NONDISK,
b3b94faa
DT
708};
709
64d576ba
SW
710const struct gfs2_glock_operations *gfs2_glops_list[] = {
711 [LM_TYPE_META] = &gfs2_meta_glops,
712 [LM_TYPE_INODE] = &gfs2_inode_glops,
713 [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
64d576ba
SW
714 [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
715 [LM_TYPE_FLOCK] = &gfs2_flock_glops,
716 [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
717 [LM_TYPE_QUOTA] = &gfs2_quota_glops,
718 [LM_TYPE_JOURNAL] = &gfs2_journal_glops,
719};
720