Skip to content

Commit 2332c44

Browse files
AstralBobswhiteho
authored andcommitted
[GFS2] assertion failure after writing to journaled file, umount
This patch passes all my nasty tests that were causing the code to fail under one circumstance or another. Here is a complete summary of all changes from today's git tree, in order of appearance: 1. There are now separate variables for metadata buffer accounting. 2. Variable sd_log_num_hdrs is no longer needed, since the header accounting is taken care of by the reserve/refund sequence. 3. Fixed a tiny grammatical problem in a comment. 4. Added a new function "calc_reserved" to calculate the reserved log space. This isn't entirely necessary, but it has two benefits: First, it simplifies the gfs2_log_refund function greatly. Second, it allows for easier debugging because I could sprinkle the code with calls to this function to make sure the accounting is proper (by adding asserts and printks) at strategic point of the code. 5. In log_pull_tail there apparently was a kludge to fix up the accounting based on a "pull" parameter. The buffer accounting is now done properly, so the kludge was removed. 6. File sync operations were making a call to gfs2_log_flush that writes another journal header. Since that header was unplanned for (reserved) by the reserve/refund sequence, the free space had to be decremented so that when log_pull_tail gets called, the free space is be adjusted properly. (Did I hear you call that a kludge? well, maybe, but a lot more justifiable than the one I removed). 7. In the gfs2_log_shutdown code, it optionally syncs the log by specifying the PULL parameter to log_write_header. I'm not sure this is necessary anymore. It just seems to me there could be cases where shutdown is called while there are outstanding log buffers. 8. In the (data)buf_lo_before_commit functions, I changed some offset values from being calculated on the fly to being constants. That simplified some code and we might as well let the compiler do the calculation once rather than redoing those cycles at run time. 9. This version has my rewritten databuf_lo_add function. This version is much more like its predecessor, buf_lo_add, which makes it easier to understand. Again, this might not be necessary, but it seems as if this one works as well as the previous one, maybe even better, so I decided to leave it in. 10. In databuf_lo_before_commit, a previous data corruption problem was caused by going off the end of the buffer. The proper solution is to have the proper limit in place, rather than stopping earlier. (Thus my previous attempt to fix it is wrong). If you don't wrap the buffer, you're stopping too early and that causes more log buffer accounting problems. 11. In lops.h there are two new (previously mentioned) constants for figuring out the data offset for the journal buffers. 12. There are also two new functions, buf_limit and databuf_limit to calculate how many entries will fit in the buffer. 13. In function gfs2_meta_wipe, it needs to distinguish between pinned metadata buffers and journaled data buffers for proper journal buffer accounting. It can't use the JDATA gfs2_inode flag because it's sometimes passed the "real" inode and sometimes the "metadata inode" and the inode flags will be random bits in a metadata gfs2_inode. It needs to base its decision on which was passed in. Signed-off-by: Bob Peterson <rpeterso@redhat.com> Signed-off-by: Steven Whitehouse <swhiteho@redhat.com>
1 parent 2840501 commit 2332c44

5 files changed

Lines changed: 132 additions & 56 deletions

File tree

‎fs/gfs2/incore.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ struct gfs2_trans {
354354

355355
unsigned int tr_num_buf;
356356
unsigned int tr_num_buf_new;
357+
unsigned int tr_num_databuf_new;
357358
unsigned int tr_num_buf_rm;
359+
unsigned int tr_num_databuf_rm;
358360
struct list_head tr_list_buf;
359361

360362
unsigned int tr_num_revoke;
@@ -599,6 +601,7 @@ struct gfs2_sbd {
599601

600602
unsigned int sd_log_blks_reserved;
601603
unsigned int sd_log_commited_buf;
604+
unsigned int sd_log_commited_databuf;
602605
unsigned int sd_log_commited_revoke;
603606

604607
unsigned int sd_log_num_gl;
@@ -607,7 +610,6 @@ struct gfs2_sbd {
607610
unsigned int sd_log_num_rg;
608611
unsigned int sd_log_num_databuf;
609612
unsigned int sd_log_num_jdata;
610-
unsigned int sd_log_num_hdrs;
611613

612614
struct list_head sd_log_le_gl;
613615
struct list_head sd_log_le_buf;

‎fs/gfs2/log.c‎

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
276276
* @blks: The number of blocks to reserve
277277
*
278278
* Note that we never give out the last few blocks of the journal. Thats
279-
* due to the fact that there is are a small number of header blocks
279+
* due to the fact that there is a small number of header blocks
280280
* associated with each log flush. The exact number can't be known until
281281
* flush time, so we ensure that we have just enough free blocks at all
282282
* times to avoid running out during a log flush.
@@ -371,6 +371,58 @@ static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer
371371
return dist;
372372
}
373373

374+
/**
375+
* calc_reserved - Calculate the number of blocks to reserve when
376+
* refunding a transaction's unused buffers.
377+
* @sdp: The GFS2 superblock
378+
*
379+
* This is complex. We need to reserve room for all our currently used
380+
* metadata buffers (e.g. normal file I/O rewriting file time stamps) and
381+
* all our journaled data buffers for journaled files (e.g. files in the
382+
* meta_fs like rindex, or files for which chattr +j was done.)
383+
* If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
384+
* will count it as free space (sd_log_blks_free) and corruption will follow.
385+
*
386+
* We can have metadata bufs and jdata bufs in the same journal. So each
387+
* type gets its own log header, for which we need to reserve a block.
388+
* In fact, each type has the potential for needing more than one header
389+
* in cases where we have more buffers than will fit on a journal page.
390+
* Metadata journal entries take up half the space of journaled buffer entries.
391+
* Thus, metadata entries have buf_limit (502) and journaled buffers have
392+
* databuf_limit (251) before they cause a wrap around.
393+
*
394+
* Also, we need to reserve blocks for revoke journal entries and one for an
395+
* overall header for the lot.
396+
*
397+
* Returns: the number of blocks reserved
398+
*/
399+
static unsigned int calc_reserved(struct gfs2_sbd *sdp)
400+
{
401+
unsigned int reserved = 0;
402+
unsigned int mbuf_limit, metabufhdrs_needed;
403+
unsigned int dbuf_limit, databufhdrs_needed;
404+
unsigned int revokes = 0;
405+
406+
mbuf_limit = buf_limit(sdp);
407+
metabufhdrs_needed = (sdp->sd_log_commited_buf +
408+
(mbuf_limit - 1)) / mbuf_limit;
409+
dbuf_limit = databuf_limit(sdp);
410+
databufhdrs_needed = (sdp->sd_log_commited_databuf +
411+
(dbuf_limit - 1)) / dbuf_limit;
412+
413+
if (sdp->sd_log_commited_revoke)
414+
revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
415+
sizeof(u64));
416+
417+
reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
418+
sdp->sd_log_commited_databuf + databufhdrs_needed +
419+
revokes;
420+
/* One for the overall header */
421+
if (reserved)
422+
reserved++;
423+
return reserved;
424+
}
425+
374426
static unsigned int current_tail(struct gfs2_sbd *sdp)
375427
{
376428
struct gfs2_ail *ai;
@@ -461,14 +513,14 @@ struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
461513
return bh;
462514
}
463515

464-
static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
516+
static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
465517
{
466518
unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
467519

468520
ail2_empty(sdp, new_tail);
469521

470522
gfs2_log_lock(sdp);
471-
sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
523+
sdp->sd_log_blks_free += dist;
472524
gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
473525
gfs2_log_unlock(sdp);
474526

@@ -518,7 +570,7 @@ static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
518570
brelse(bh);
519571

520572
if (sdp->sd_log_tail != tail)
521-
log_pull_tail(sdp, tail, pull);
573+
log_pull_tail(sdp, tail);
522574
else
523575
gfs2_assert_withdraw(sdp, !pull);
524576

@@ -579,7 +631,10 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
579631
INIT_LIST_HEAD(&ai->ai_ail1_list);
580632
INIT_LIST_HEAD(&ai->ai_ail2_list);
581633

582-
gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf + sdp->sd_log_num_jdata == sdp->sd_log_commited_buf);
634+
gfs2_assert_withdraw(sdp,
635+
sdp->sd_log_num_buf + sdp->sd_log_num_jdata ==
636+
sdp->sd_log_commited_buf +
637+
sdp->sd_log_commited_databuf);
583638
gfs2_assert_withdraw(sdp,
584639
sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
585640

@@ -590,16 +645,19 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
590645
lops_before_commit(sdp);
591646
if (!list_empty(&sdp->sd_log_flush_list))
592647
log_flush_commit(sdp);
593-
else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
648+
else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
649+
gfs2_log_lock(sdp);
650+
sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
651+
gfs2_log_unlock(sdp);
594652
log_write_header(sdp, 0, PULL);
653+
}
595654
lops_after_commit(sdp, ai);
596655

597656
gfs2_log_lock(sdp);
598657
sdp->sd_log_head = sdp->sd_log_flush_head;
599-
sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
600658
sdp->sd_log_blks_reserved = 0;
601659
sdp->sd_log_commited_buf = 0;
602-
sdp->sd_log_num_hdrs = 0;
660+
sdp->sd_log_commited_databuf = 0;
603661
sdp->sd_log_commited_revoke = 0;
604662

605663
if (!list_empty(&ai->ai_ail1_list)) {
@@ -616,32 +674,26 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
616674

617675
static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
618676
{
619-
unsigned int reserved = 0;
677+
unsigned int reserved;
620678
unsigned int old;
621679

622680
gfs2_log_lock(sdp);
623681

624682
sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
625-
gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
683+
sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
684+
tr->tr_num_databuf_rm;
685+
gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
686+
(((int)sdp->sd_log_commited_databuf) >= 0));
626687
sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
627688
gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
628-
629-
if (sdp->sd_log_commited_buf)
630-
reserved += sdp->sd_log_commited_buf;
631-
if (sdp->sd_log_commited_revoke)
632-
reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
633-
sizeof(u64));
634-
if (reserved)
635-
reserved++;
636-
689+
reserved = calc_reserved(sdp);
637690
old = sdp->sd_log_blks_free;
638691
sdp->sd_log_blks_free += tr->tr_reserved -
639692
(reserved - sdp->sd_log_blks_reserved);
640693

641694
gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
642-
gfs2_assert_withdraw(sdp,
643-
sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
644-
sdp->sd_log_num_hdrs);
695+
gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
696+
sdp->sd_jdesc->jd_blocks);
645697

646698
sdp->sd_log_blks_reserved = reserved;
647699

@@ -687,13 +739,13 @@ void gfs2_log_shutdown(struct gfs2_sbd *sdp)
687739
gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
688740
gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
689741
gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
690-
gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
691742
gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
692743

693744
sdp->sd_log_flush_head = sdp->sd_log_head;
694745
sdp->sd_log_flush_wrapped = 0;
695746

696-
log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
747+
log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
748+
(sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
697749

698750
gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
699751
gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);

‎fs/gfs2/lops.c‎

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "gfs2.h"
1919
#include "incore.h"
20+
#include "inode.h"
2021
#include "glock.h"
2122
#include "log.h"
2223
#include "lops.h"
@@ -117,15 +118,13 @@ static void buf_lo_before_commit(struct gfs2_sbd *sdp)
117118
struct gfs2_log_descriptor *ld;
118119
struct gfs2_bufdata *bd1 = NULL, *bd2;
119120
unsigned int total = sdp->sd_log_num_buf;
120-
unsigned int offset = sizeof(struct gfs2_log_descriptor);
121+
unsigned int offset = BUF_OFFSET;
121122
unsigned int limit;
122123
unsigned int num;
123124
unsigned n;
124125
__be64 *ptr;
125126

126-
offset += sizeof(__be64) - 1;
127-
offset &= ~(sizeof(__be64) - 1);
128-
limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
127+
limit = buf_limit(sdp);
129128
/* for 4k blocks, limit = 503 */
130129

131130
bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
@@ -134,7 +133,6 @@ static void buf_lo_before_commit(struct gfs2_sbd *sdp)
134133
if (total > limit)
135134
num = limit;
136135
bh = gfs2_log_get_buf(sdp);
137-
sdp->sd_log_num_hdrs++;
138136
ld = (struct gfs2_log_descriptor *)bh->b_data;
139137
ptr = (__be64 *)(bh->b_data + offset);
140138
ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
@@ -469,27 +467,26 @@ static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
469467
struct gfs2_inode *ip = GFS2_I(mapping->host);
470468

471469
gfs2_log_lock(sdp);
472-
tr->tr_touched = 1;
473-
if (list_empty(&bd->bd_list_tr) &&
474-
(ip->i_di.di_flags & GFS2_DIF_JDATA)) {
475-
tr->tr_num_buf++;
476-
list_add(&bd->bd_list_tr, &tr->tr_list_buf);
477-
gfs2_log_unlock(sdp);
478-
if (!list_empty(&le->le_list))
479-
return;
480-
gfs2_pin(sdp, bd->bd_bh);
481-
tr->tr_num_buf_new++;
482-
} else {
470+
if (!list_empty(&bd->bd_list_tr)) {
483471
gfs2_log_unlock(sdp);
472+
return;
484473
}
474+
tr->tr_touched = 1;
475+
tr->tr_num_buf++;
476+
list_add(&bd->bd_list_tr, &tr->tr_list_buf);
477+
gfs2_log_unlock(sdp);
478+
if (!list_empty(&le->le_list))
479+
return;
480+
485481
gfs2_trans_add_gl(bd->bd_gl);
486-
gfs2_log_lock(sdp);
487-
if (list_empty(&le->le_list)) {
488-
if (ip->i_di.di_flags & GFS2_DIF_JDATA)
489-
sdp->sd_log_num_jdata++;
490-
sdp->sd_log_num_databuf++;
491-
list_add(&le->le_list, &sdp->sd_log_le_databuf);
482+
if (gfs2_is_jdata(ip)) {
483+
sdp->sd_log_num_jdata++;
484+
gfs2_pin(sdp, bd->bd_bh);
485+
tr->tr_num_databuf_new++;
492486
}
487+
sdp->sd_log_num_databuf++;
488+
gfs2_log_lock(sdp);
489+
list_add(&le->le_list, &sdp->sd_log_le_databuf);
493490
gfs2_log_unlock(sdp);
494491
}
495492

@@ -522,17 +519,14 @@ static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
522519
LIST_HEAD(started);
523520
struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
524521
struct buffer_head *bh = NULL,*bh1 = NULL;
525-
unsigned int offset = sizeof(struct gfs2_log_descriptor);
526522
struct gfs2_log_descriptor *ld;
527523
unsigned int limit;
528524
unsigned int total_dbuf = sdp->sd_log_num_databuf;
529525
unsigned int total_jdata = sdp->sd_log_num_jdata;
530526
unsigned int num, n;
531527
__be64 *ptr = NULL;
532528

533-
offset += 2*sizeof(__be64) - 1;
534-
offset &= ~(2*sizeof(__be64) - 1);
535-
limit = (sdp->sd_sb.sb_bsize - offset)/sizeof(__be64);
529+
limit = databuf_limit(sdp);
536530

537531
/*
538532
* Start writing ordered buffers, write journaled buffers
@@ -583,10 +577,10 @@ static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
583577
gfs2_log_unlock(sdp);
584578
if (!bh) {
585579
bh = gfs2_log_get_buf(sdp);
586-
sdp->sd_log_num_hdrs++;
587580
ld = (struct gfs2_log_descriptor *)
588581
bh->b_data;
589-
ptr = (__be64 *)(bh->b_data + offset);
582+
ptr = (__be64 *)(bh->b_data +
583+
DATABUF_OFFSET);
590584
ld->ld_header.mh_magic =
591585
cpu_to_be32(GFS2_MAGIC);
592586
ld->ld_header.mh_type =
@@ -607,8 +601,7 @@ static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
607601
if (unlikely(magic != 0))
608602
set_buffer_escaped(bh1);
609603
gfs2_log_lock(sdp);
610-
n += 2;
611-
if (n >= num)
604+
if (++n >= num)
612605
break;
613606
} else if (!bh1) {
614607
total_dbuf--;

‎fs/gfs2/lops.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
#include <linux/list.h>
1414
#include "incore.h"
1515

16+
#define BUF_OFFSET \
17+
((sizeof(struct gfs2_log_descriptor) + sizeof(__be64) - 1) & \
18+
~(sizeof(__be64) - 1))
19+
#define DATABUF_OFFSET \
20+
((sizeof(struct gfs2_log_descriptor) + (2 * sizeof(__be64) - 1)) & \
21+
~(2 * sizeof(__be64) - 1))
22+
1623
extern const struct gfs2_log_operations gfs2_glock_lops;
1724
extern const struct gfs2_log_operations gfs2_buf_lops;
1825
extern const struct gfs2_log_operations gfs2_revoke_lops;
@@ -21,6 +28,22 @@ extern const struct gfs2_log_operations gfs2_databuf_lops;
2128

2229
extern const struct gfs2_log_operations *gfs2_log_ops[];
2330

31+
static inline unsigned int buf_limit(struct gfs2_sbd *sdp)
32+
{
33+
unsigned int limit;
34+
35+
limit = (sdp->sd_sb.sb_bsize - BUF_OFFSET) / sizeof(__be64);
36+
return limit;
37+
}
38+
39+
static inline unsigned int databuf_limit(struct gfs2_sbd *sdp)
40+
{
41+
unsigned int limit;
42+
43+
limit = (sdp->sd_sb.sb_bsize - DATABUF_OFFSET) / (2 * sizeof(__be64));
44+
return limit;
45+
}
46+
2447
static inline void lops_init_le(struct gfs2_log_element *le,
2548
const struct gfs2_log_operations *lops)
2649
{

‎fs/gfs2/meta_io.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,18 @@ void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
387387

388388
if (test_clear_buffer_pinned(bh)) {
389389
struct gfs2_trans *tr = current->journal_info;
390+
struct gfs2_inode *bh_ip =
391+
GFS2_I(bh->b_page->mapping->host);
392+
390393
gfs2_log_lock(sdp);
391394
list_del_init(&bd->bd_le.le_list);
392395
gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
393396
sdp->sd_log_num_buf--;
394397
gfs2_log_unlock(sdp);
395-
tr->tr_num_buf_rm++;
398+
if (bh_ip->i_inode.i_private != NULL)
399+
tr->tr_num_databuf_rm++;
400+
else
401+
tr->tr_num_buf_rm++;
396402
brelse(bh);
397403
}
398404
if (bd) {

0 commit comments

Comments
 (0)