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