1
2
3
4
5
6
7
8
9
10#include <linux/spinlock.h>
11#include <linux/completion.h>
12#include <linux/buffer_head.h>
13#include <linux/gfs2_ondisk.h>
14#include <linux/bio.h>
15#include <linux/posix_acl.h>
16#include <linux/security.h>
17
18#include "gfs2.h"
19#include "incore.h"
20#include "bmap.h"
21#include "glock.h"
22#include "glops.h"
23#include "inode.h"
24#include "log.h"
25#include "meta_io.h"
26#include "recovery.h"
27#include "rgrp.h"
28#include "util.h"
29#include "trans.h"
30#include "dir.h"
31#include "lops.h"
32
33static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
34{
35 fs_err(gl->gl_name.ln_sbd,
36 "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
37 "state 0x%lx\n",
38 bh, (unsigned long long)bh->b_blocknr, bh->b_state,
39 bh->b_page->mapping, bh->b_page->flags);
40 fs_err(gl->gl_name.ln_sbd, "AIL glock %u:%llu mapping %p\n",
41 gl->gl_name.ln_type, gl->gl_name.ln_number,
42 gfs2_glock2aspace(gl));
43 gfs2_lm_withdraw(gl->gl_name.ln_sbd, "AIL error\n");
44}
45
46
47
48
49
50
51
52
53
54static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
55 unsigned int nr_revokes)
56{
57 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
58 struct list_head *head = &gl->gl_ail_list;
59 struct gfs2_bufdata *bd, *tmp;
60 struct buffer_head *bh;
61 const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
62
63 gfs2_log_lock(sdp);
64 spin_lock(&sdp->sd_ail_lock);
65 list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
66 if (nr_revokes == 0)
67 break;
68 bh = bd->bd_bh;
69 if (bh->b_state & b_state) {
70 if (fsync)
71 continue;
72 gfs2_ail_error(gl, bh);
73 }
74 gfs2_trans_add_revoke(sdp, bd);
75 nr_revokes--;
76 }
77 GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
78 spin_unlock(&sdp->sd_ail_lock);
79 gfs2_log_unlock(sdp);
80}
81
82
83static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
84{
85 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
86 struct gfs2_trans tr;
87
88 memset(&tr, 0, sizeof(tr));
89 INIT_LIST_HEAD(&tr.tr_buf);
90 INIT_LIST_HEAD(&tr.tr_databuf);
91 tr.tr_revokes = atomic_read(&gl->gl_ail_count);
92
93 if (!tr.tr_revokes)
94 return;
95
96
97 tr.tr_reserved = 1 + gfs2_struct2blk(sdp, tr.tr_revokes, sizeof(u64));
98 tr.tr_ip = (unsigned long)__builtin_return_address(0);
99 sb_start_intwrite(sdp->sd_vfs);
100 gfs2_log_reserve(sdp, tr.tr_reserved);
101 WARN_ON_ONCE(current->journal_info);
102 current->journal_info = &tr;
103
104 __gfs2_ail_flush(gl, 0, tr.tr_revokes);
105
106 gfs2_trans_end(sdp);
107 gfs2_log_flush(sdp, NULL);
108}
109
110void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
111{
112 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
113 unsigned int revokes = atomic_read(&gl->gl_ail_count);
114 unsigned int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
115 int ret;
116
117 if (!revokes)
118 return;
119
120 while (revokes > max_revokes)
121 max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
122
123 ret = gfs2_trans_begin(sdp, 0, max_revokes);
124 if (ret)
125 return;
126 __gfs2_ail_flush(gl, fsync, max_revokes);
127 gfs2_trans_end(sdp);
128 gfs2_log_flush(sdp, NULL);
129}
130
131
132
133
134
135
136
137
138
139
140static void rgrp_go_sync(struct gfs2_glock *gl)
141{
142 struct address_space *metamapping = gfs2_glock2aspace(gl);
143 struct gfs2_rgrpd *rgd;
144 int error;
145
146 spin_lock(&gl->gl_lockref.lock);
147 rgd = gl->gl_object;
148 if (rgd)
149 gfs2_rgrp_brelse(rgd);
150 spin_unlock(&gl->gl_lockref.lock);
151
152 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
153 return;
154 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
155
156 gfs2_log_flush(gl->gl_name.ln_sbd, gl);
157 filemap_fdatawrite(metamapping);
158 error = filemap_fdatawait(metamapping);
159 mapping_set_error(metamapping, error);
160 gfs2_ail_empty_gl(gl);
161
162 spin_lock(&gl->gl_lockref.lock);
163 rgd = gl->gl_object;
164 if (rgd)
165 gfs2_free_clones(rgd);
166 spin_unlock(&gl->gl_lockref.lock);
167}
168
169
170
171
172
173
174
175
176
177
178
179static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
180{
181 struct address_space *mapping = gfs2_glock2aspace(gl);
182 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
183
184 if (rgd)
185 gfs2_rgrp_brelse(rgd);
186
187 WARN_ON_ONCE(!(flags & DIO_METADATA));
188 gfs2_assert_withdraw(gl->gl_name.ln_sbd, !atomic_read(&gl->gl_ail_count));
189 truncate_inode_pages(mapping, 0);
190
191 if (rgd)
192 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
193}
194
195static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
196{
197 struct gfs2_inode *ip;
198
199 spin_lock(&gl->gl_lockref.lock);
200 ip = gl->gl_object;
201 if (ip)
202 set_bit(GIF_GLOP_PENDING, &ip->i_flags);
203 spin_unlock(&gl->gl_lockref.lock);
204 return ip;
205}
206
207struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
208{
209 struct gfs2_rgrpd *rgd;
210
211 spin_lock(&gl->gl_lockref.lock);
212 rgd = gl->gl_object;
213 spin_unlock(&gl->gl_lockref.lock);
214
215 return rgd;
216}
217
218static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
219{
220 if (!ip)
221 return;
222
223 clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
224 wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
225}
226
227
228
229
230
231
232
233static void inode_go_sync(struct gfs2_glock *gl)
234{
235 struct gfs2_inode *ip = gfs2_glock2inode(gl);
236 int isreg = ip && S_ISREG(ip->i_inode.i_mode);
237 struct address_space *metamapping = gfs2_glock2aspace(gl);
238 int error;
239
240 if (isreg) {
241 if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
242 unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
243 inode_dio_wait(&ip->i_inode);
244 }
245 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
246 goto out;
247
248 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
249
250 gfs2_log_flush(gl->gl_name.ln_sbd, gl);
251 filemap_fdatawrite(metamapping);
252 if (isreg) {
253 struct address_space *mapping = ip->i_inode.i_mapping;
254 filemap_fdatawrite(mapping);
255 error = filemap_fdatawait(mapping);
256 mapping_set_error(mapping, error);
257 }
258 error = filemap_fdatawait(metamapping);
259 mapping_set_error(metamapping, error);
260 gfs2_ail_empty_gl(gl);
261
262
263
264
265 smp_mb__before_clear_bit();
266 clear_bit(GLF_DIRTY, &gl->gl_flags);
267
268out:
269 gfs2_clear_glop_pending(ip);
270}
271
272
273
274
275
276
277
278
279
280
281
282
283static void inode_go_inval(struct gfs2_glock *gl, int flags)
284{
285 struct gfs2_inode *ip = gfs2_glock2inode(gl);
286
287 gfs2_assert_withdraw(gl->gl_name.ln_sbd, !atomic_read(&gl->gl_ail_count));
288
289 if (flags & DIO_METADATA) {
290 struct address_space *mapping = gfs2_glock2aspace(gl);
291 truncate_inode_pages(mapping, 0);
292 if (ip) {
293 set_bit(GIF_INVALID, &ip->i_flags);
294 forget_all_cached_acls(&ip->i_inode);
295 security_inode_invalidate_secctx(&ip->i_inode);
296 gfs2_dir_hash_inval(ip);
297 }
298 }
299
300 if (ip == GFS2_I(gl->gl_name.ln_sbd->sd_rindex)) {
301 gfs2_log_flush(gl->gl_name.ln_sbd, NULL);
302 gl->gl_name.ln_sbd->sd_rindex_uptodate = 0;
303 }
304 if (ip && S_ISREG(ip->i_inode.i_mode))
305 truncate_inode_pages(ip->i_inode.i_mapping, 0);
306
307 gfs2_clear_glop_pending(ip);
308}
309
310
311
312
313
314
315
316
317static int inode_go_demote_ok(const struct gfs2_glock *gl)
318{
319 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
320 struct gfs2_holder *gh;
321
322 if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
323 return 0;
324
325 if (!list_empty(&gl->gl_holders)) {
326 gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list);
327 if (gh->gh_list.next != &gl->gl_holders)
328 return 0;
329 }
330
331 return 1;
332}
333
334static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
335{
336 const struct gfs2_dinode *str = buf;
337 struct timespec atime;
338 u16 height, depth;
339
340 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
341 goto corrupt;
342 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
343 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
344 ip->i_inode.i_rdev = 0;
345 switch (ip->i_inode.i_mode & S_IFMT) {
346 case S_IFBLK:
347 case S_IFCHR:
348 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
349 be32_to_cpu(str->di_minor));
350 break;
351 };
352
353 i_uid_write(&ip->i_inode, be32_to_cpu(str->di_uid));
354 i_gid_write(&ip->i_inode, be32_to_cpu(str->di_gid));
355 set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
356 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
357 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
358 atime.tv_sec = be64_to_cpu(str->di_atime);
359 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
360 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
361 ip->i_inode.i_atime = atime;
362 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
363 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
364 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
365 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
366
367 ip->i_goal = be64_to_cpu(str->di_goal_meta);
368 ip->i_generation = be64_to_cpu(str->di_generation);
369
370 ip->i_diskflags = be32_to_cpu(str->di_flags);
371 ip->i_eattr = be64_to_cpu(str->di_eattr);
372
373 gfs2_set_inode_flags(&ip->i_inode);
374 height = be16_to_cpu(str->di_height);
375 if (unlikely(height > GFS2_MAX_META_HEIGHT))
376 goto corrupt;
377 ip->i_height = (u8)height;
378
379 depth = be16_to_cpu(str->di_depth);
380 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
381 goto corrupt;
382 ip->i_depth = (u8)depth;
383 ip->i_entries = be32_to_cpu(str->di_entries);
384
385 if (S_ISREG(ip->i_inode.i_mode))
386 gfs2_set_aops(&ip->i_inode);
387
388 return 0;
389corrupt:
390 gfs2_consist_inode(ip);
391 return -EIO;
392}
393
394
395
396
397
398
399
400
401int gfs2_inode_refresh(struct gfs2_inode *ip)
402{
403 struct buffer_head *dibh;
404 int error;
405
406 error = gfs2_meta_inode_buffer(ip, &dibh);
407 if (error)
408 return error;
409
410 error = gfs2_dinode_in(ip, dibh->b_data);
411 brelse(dibh);
412 clear_bit(GIF_INVALID, &ip->i_flags);
413
414 return error;
415}
416
417
418
419
420
421
422
423
424
425static int inode_go_lock(struct gfs2_holder *gh)
426{
427 struct gfs2_glock *gl = gh->gh_gl;
428 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
429 struct gfs2_inode *ip = gl->gl_object;
430 int error = 0;
431
432 if (!ip || (gh->gh_flags & GL_SKIP))
433 return 0;
434
435 if (test_bit(GIF_INVALID, &ip->i_flags)) {
436 error = gfs2_inode_refresh(ip);
437 if (error)
438 return error;
439 }
440
441 if (gh->gh_state != LM_ST_DEFERRED)
442 inode_dio_wait(&ip->i_inode);
443
444 if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
445 (gl->gl_state == LM_ST_EXCLUSIVE) &&
446 (gh->gh_state == LM_ST_EXCLUSIVE)) {
447 spin_lock(&sdp->sd_trunc_lock);
448 if (list_empty(&ip->i_trunc_list))
449 list_add(&ip->i_trunc_list, &sdp->sd_trunc_list);
450 spin_unlock(&sdp->sd_trunc_lock);
451 wake_up(&sdp->sd_quota_wait);
452 return 1;
453 }
454
455 return error;
456}
457
458
459
460
461
462
463
464
465
466static int inode_go_dump(struct seq_file *seq, const struct gfs2_glock *gl)
467{
468 const struct gfs2_inode *ip = gl->gl_object;
469 if (ip == NULL)
470 return 0;
471 gfs2_print_dbg(seq, " I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu\n",
472 (unsigned long long)ip->i_no_formal_ino,
473 (unsigned long long)ip->i_no_addr,
474 IF2DT(ip->i_inode.i_mode), ip->i_flags,
475 (unsigned int)ip->i_diskflags,
476 (unsigned long long)i_size_read(&ip->i_inode));
477 return 0;
478}
479
480
481
482
483
484
485
486
487
488static void trans_go_sync(struct gfs2_glock *gl)
489{
490 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
491
492 if (gl->gl_state != LM_ST_UNLOCKED &&
493 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
494 gfs2_meta_syncfs(sdp);
495 gfs2_log_shutdown(sdp, 0);
496 }
497}
498
499
500
501
502
503
504
505static int trans_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
506{
507 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
508 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
509 struct gfs2_glock *j_gl = ip->i_gl;
510 struct gfs2_log_header_host head;
511 int error;
512
513 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
514 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
515
516 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
517 if (error)
518 gfs2_consist(sdp);
519 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
520 gfs2_consist(sdp);
521
522
523 if (!test_bit(SDF_SHUTDOWN, &sdp->sd_flags)) {
524 sdp->sd_log_sequence = head.lh_sequence + 1;
525 gfs2_log_pointers_init(sdp, head.lh_blkno);
526 }
527 }
528 return 0;
529}
530
531
532
533
534
535
536
537
538static int trans_go_demote_ok(const struct gfs2_glock *gl)
539{
540 return 0;
541}
542
543
544
545
546
547
548
549static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
550{
551 struct gfs2_inode *ip = gl->gl_object;
552 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
553
554 if (!remote || (sdp->sd_vfs->s_flags & MS_RDONLY))
555 return;
556
557 if (gl->gl_demote_state == LM_ST_UNLOCKED &&
558 gl->gl_state == LM_ST_SHARED && ip) {
559 gl->gl_lockref.count++;
560 if (queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
561 gl->gl_lockref.count--;
562 }
563}
564
565const struct gfs2_glock_operations gfs2_meta_glops = {
566 .go_type = LM_TYPE_META,
567};
568
569const struct gfs2_glock_operations gfs2_inode_glops = {
570 .go_sync = inode_go_sync,
571 .go_inval = inode_go_inval,
572 .go_demote_ok = inode_go_demote_ok,
573 .go_lock = inode_go_lock,
574 .go_dump = inode_go_dump,
575 .go_type = LM_TYPE_INODE,
576 .go_flags = GLOF_ASPACE | GLOF_LRU,
577};
578
579const struct gfs2_glock_operations gfs2_rgrp_glops = {
580 .go_sync = rgrp_go_sync,
581 .go_inval = rgrp_go_inval,
582 .go_lock = gfs2_rgrp_go_lock,
583 .go_unlock = gfs2_rgrp_go_unlock,
584 .go_dump = gfs2_rgrp_dump,
585 .go_type = LM_TYPE_RGRP,
586 .go_flags = GLOF_ASPACE | GLOF_LVB,
587};
588
589const struct gfs2_glock_operations gfs2_trans_glops = {
590 .go_sync = trans_go_sync,
591 .go_xmote_bh = trans_go_xmote_bh,
592 .go_demote_ok = trans_go_demote_ok,
593 .go_type = LM_TYPE_NONDISK,
594};
595
596const struct gfs2_glock_operations gfs2_iopen_glops = {
597 .go_type = LM_TYPE_IOPEN,
598 .go_callback = iopen_go_callback,
599 .go_flags = GLOF_LRU,
600};
601
602const struct gfs2_glock_operations gfs2_flock_glops = {
603 .go_type = LM_TYPE_FLOCK,
604 .go_flags = GLOF_LRU,
605};
606
607const struct gfs2_glock_operations gfs2_nondisk_glops = {
608 .go_type = LM_TYPE_NONDISK,
609};
610
611const struct gfs2_glock_operations gfs2_quota_glops = {
612 .go_type = LM_TYPE_QUOTA,
613 .go_flags = GLOF_LVB | GLOF_LRU,
614};
615
616const struct gfs2_glock_operations gfs2_journal_glops = {
617 .go_type = LM_TYPE_JOURNAL,
618};
619
620const struct gfs2_glock_operations *gfs2_glops_list[] = {
621 [LM_TYPE_META] = &gfs2_meta_glops,
622 [LM_TYPE_INODE] = &gfs2_inode_glops,
623 [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
624 [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
625 [LM_TYPE_FLOCK] = &gfs2_flock_glops,
626 [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
627 [LM_TYPE_QUOTA] = &gfs2_quota_glops,
628 [LM_TYPE_JOURNAL] = &gfs2_journal_glops,
629};
630
631