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