Skip to content

Commit 4da0dd9

Browse files
committed
Merge tag 'gfs2-for-7.1' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2
Pull gfs2 updates from Andreas Gruenbacher: - Fix possible data loss during inode evict - Fix a race during bufdata allocation - More careful cleaning up during a withdraw - Prevent excessive log flushing under memory pressure - Various other minor fixes and cleanups * tag 'gfs2-for-7.1' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2: gfs2: prevent NULL pointer dereference during unmount gfs2: hide error messages after withdraw gfs2: wait for withdraw earlier during unmount gfs2: inode directory consistency checks gfs2: gfs2_log_flush withdraw fixes gfs2: add some missing log locking gfs2: fix address space truncation during withdraw gfs2: drain ail under sd_log_flush_lock gfs2: bufdata allocation race gfs2: Remove trans_drain code duplication gfs2: Move gfs2_remove_from_journal to log.c gfs2: Get rid of gfs2_log_[un]lock helpers gfs2: less aggressive low-memory log flushing gfs2: Fix data loss during inode evict gfs2: minor evict_[un]linked_inode cleanup gfs2: Avoid unnecessary transactions in evict_linked_inode gfs2: Remove unnecessary check in gfs2_evict_inode gfs2: Call unlock_new_inode before d_instantiate
2 parents acf6c67 + 74b4dbb commit 4da0dd9

13 files changed

Lines changed: 237 additions & 170 deletions

File tree

fs/gfs2/aops.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ static int gfs2_writepages(struct address_space *mapping,
158158
struct writeback_control *wbc)
159159
{
160160
struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
161+
long initial_nr_to_write = wbc->nr_to_write;
161162
struct iomap_writepage_ctx wpc = {
162163
.inode = mapping->host,
163164
.wbc = wbc,
@@ -166,13 +167,13 @@ static int gfs2_writepages(struct address_space *mapping,
166167
int ret;
167168

168169
/*
169-
* Even if we didn't write enough pages here, we might still be holding
170+
* Even if we didn't write any pages here, we might still be holding
170171
* dirty pages in the ail. We forcibly flush the ail because we don't
171172
* want balance_dirty_pages() to loop indefinitely trying to write out
172173
* pages held in the ail that it can't find.
173174
*/
174175
ret = iomap_writepages(&wpc);
175-
if (ret == 0 && wbc->nr_to_write > 0)
176+
if (ret == 0 && wbc->nr_to_write == initial_nr_to_write)
176177
set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
177178
return ret;
178179
}
@@ -582,7 +583,7 @@ static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
582583
struct gfs2_bufdata *bd;
583584

584585
lock_buffer(bh);
585-
gfs2_log_lock(sdp);
586+
spin_lock(&sdp->sd_log_lock);
586587
clear_buffer_dirty(bh);
587588
bd = bh->b_private;
588589
if (bd) {
@@ -598,7 +599,7 @@ static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
598599
clear_buffer_mapped(bh);
599600
clear_buffer_req(bh);
600601
clear_buffer_new(bh);
601-
gfs2_log_unlock(sdp);
602+
spin_unlock(&sdp->sd_log_lock);
602603
unlock_buffer(bh);
603604
}
604605

@@ -666,7 +667,7 @@ bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
666667
* again.
667668
*/
668669

669-
gfs2_log_lock(sdp);
670+
spin_lock(&sdp->sd_log_lock);
670671
bh = head;
671672
do {
672673
if (atomic_read(&bh->b_count))
@@ -698,12 +699,12 @@ bool gfs2_release_folio(struct folio *folio, gfp_t gfp_mask)
698699

699700
bh = bh->b_this_page;
700701
} while (bh != head);
701-
gfs2_log_unlock(sdp);
702+
spin_unlock(&sdp->sd_log_lock);
702703

703704
return try_to_free_buffers(folio);
704705

705706
cannot_release:
706-
gfs2_log_unlock(sdp);
707+
spin_unlock(&sdp->sd_log_lock);
707708
return false;
708709
}
709710

fs/gfs2/bmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh,
15391539
revokes = jblocks_rqsted;
15401540
if (meta)
15411541
revokes += end - start;
1542-
else if (ip->i_depth)
1542+
else if (ip->i_diskflags & GFS2_DIF_EXHASH)
15431543
revokes += sdp->sd_inptrs;
15441544
ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes);
15451545
if (ret)

fs/gfs2/glops.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
6464
struct buffer_head *bh;
6565
const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
6666

67-
gfs2_log_lock(sdp);
67+
spin_lock(&sdp->sd_log_lock);
6868
spin_lock(&sdp->sd_ail_lock);
6969
list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
7070
if (nr_revokes == 0)
@@ -80,7 +80,7 @@ static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
8080
}
8181
GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
8282
spin_unlock(&sdp->sd_ail_lock);
83-
gfs2_log_unlock(sdp);
83+
spin_unlock(&sdp->sd_log_lock);
8484
}
8585

8686

@@ -109,10 +109,10 @@ static int gfs2_ail_empty_gl(struct gfs2_glock *gl)
109109
* If none of these conditions are true, our revokes are all
110110
* flushed and we can return.
111111
*/
112-
gfs2_log_lock(sdp);
112+
spin_lock(&sdp->sd_log_lock);
113113
have_revokes = !list_empty(&sdp->sd_log_revokes);
114114
log_in_flight = atomic_read(&sdp->sd_log_in_flight);
115-
gfs2_log_unlock(sdp);
115+
spin_unlock(&sdp->sd_log_lock);
116116
if (have_revokes)
117117
goto flush;
118118
if (log_in_flight)
@@ -457,6 +457,11 @@ static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
457457
ip->i_depth = (u8)depth;
458458
ip->i_entries = be32_to_cpu(str->di_entries);
459459

460+
if (!S_ISDIR(inode->i_mode) && (ip->i_diskflags & GFS2_DIF_EXHASH)) {
461+
gfs2_consist_inode(ip);
462+
return -EIO;
463+
}
464+
460465
if (gfs2_is_stuffed(ip) && inode->i_size > gfs2_max_stuffed_size(ip)) {
461466
gfs2_consist_inode(ip);
462467
return -EIO;

fs/gfs2/inode.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
892892
goto fail_gunlock4;
893893

894894
mark_inode_dirty(inode);
895-
d_instantiate(dentry, inode);
895+
d_instantiate_new(dentry, inode);
896896
/* After instantiate, errors should result in evict which will destroy
897897
* both inode and iopen glocks properly. */
898898
if (file) {
@@ -904,7 +904,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
904904
gfs2_glock_dq_uninit(&gh);
905905
gfs2_glock_put(io_gl);
906906
gfs2_qa_put(dip);
907-
unlock_new_inode(inode);
908907
return error;
909908

910909
fail_gunlock4:

fs/gfs2/log.c

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
7272
*
7373
*/
7474

75-
void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
75+
static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
7676
{
7777
bd->bd_tr = NULL;
7878
list_del_init(&bd->bd_ail_st_list);
@@ -467,8 +467,9 @@ void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
467467
{
468468
atomic_add(blks, &sdp->sd_log_blks_free);
469469
trace_gfs2_log_blocks(sdp, blks);
470-
gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
471-
sdp->sd_jdesc->jd_blocks);
470+
gfs2_assert_withdraw(sdp, !sdp->sd_jdesc ||
471+
atomic_read(&sdp->sd_log_blks_free) <=
472+
sdp->sd_jdesc->jd_blocks);
472473
if (atomic_read(&sdp->sd_log_blks_needed))
473474
wake_up(&sdp->sd_log_waitq);
474475
}
@@ -800,9 +801,9 @@ void gfs2_flush_revokes(struct gfs2_sbd *sdp)
800801
/* number of revokes we still have room for */
801802
unsigned int max_revokes = atomic_read(&sdp->sd_log_revokes_available);
802803

803-
gfs2_log_lock(sdp);
804+
spin_lock(&sdp->sd_log_lock);
804805
gfs2_ail1_empty(sdp, max_revokes);
805-
gfs2_log_unlock(sdp);
806+
spin_unlock(&sdp->sd_log_lock);
806807
}
807808

808809
/**
@@ -983,57 +984,97 @@ static void empty_ail1_list(struct gfs2_sbd *sdp)
983984
}
984985
}
985986

987+
static void gfs2_trans_drain_list(struct gfs2_sbd *sdp, struct list_head *list)
988+
{
989+
struct gfs2_bufdata *bd;
990+
991+
while (!list_empty(list)) {
992+
bd = list_first_entry(list, struct gfs2_bufdata, bd_list);
993+
struct buffer_head *bh = bd->bd_bh;
994+
995+
WARN_ON_ONCE(!buffer_pinned(bh));
996+
clear_buffer_pinned(bh);
997+
trace_gfs2_pin(bd, 0);
998+
atomic_dec(&sdp->sd_log_pinned);
999+
list_del_init(&bd->bd_list);
1000+
brelse(bh);
1001+
}
1002+
}
1003+
9861004
/**
987-
* trans_drain - drain the buf and databuf queue for a failed transaction
1005+
* gfs2_trans_drain - drain the buf and databuf queue for a failed transaction
1006+
* @sdp: the filesystem
9881007
* @tr: the transaction to drain
9891008
*
9901009
* When this is called, we're taking an error exit for a log write that failed
9911010
* but since we bypassed the after_commit functions, we need to remove the
9921011
* items from the buf and databuf queue.
9931012
*/
994-
static void trans_drain(struct gfs2_trans *tr)
1013+
static void gfs2_trans_drain(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
9951014
{
996-
struct gfs2_bufdata *bd;
997-
struct list_head *head;
998-
9991015
if (!tr)
10001016
return;
1017+
gfs2_trans_drain_list(sdp, &tr->tr_buf);
1018+
gfs2_trans_drain_list(sdp, &tr->tr_databuf);
1019+
}
10011020

1002-
head = &tr->tr_buf;
1003-
while (!list_empty(head)) {
1004-
bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
1021+
void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
1022+
{
1023+
struct address_space *mapping = bh->b_folio->mapping;
1024+
struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1025+
struct gfs2_bufdata *bd = bh->b_private;
1026+
struct gfs2_trans *tr = current->journal_info;
1027+
int was_pinned = 0;
1028+
1029+
if (test_clear_buffer_pinned(bh)) {
1030+
trace_gfs2_pin(bd, 0);
1031+
atomic_dec(&sdp->sd_log_pinned);
10051032
list_del_init(&bd->bd_list);
1006-
if (!list_empty(&bd->bd_ail_st_list))
1007-
gfs2_remove_from_ail(bd);
1008-
kmem_cache_free(gfs2_bufdata_cachep, bd);
1033+
if (tr) {
1034+
if (meta == REMOVE_META)
1035+
tr->tr_num_buf_rm++;
1036+
else
1037+
tr->tr_num_databuf_rm++;
1038+
set_bit(TR_TOUCHED, &tr->tr_flags);
1039+
}
1040+
was_pinned = 1;
1041+
brelse(bh);
10091042
}
1010-
head = &tr->tr_databuf;
1011-
while (!list_empty(head)) {
1012-
bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
1013-
list_del_init(&bd->bd_list);
1014-
if (!list_empty(&bd->bd_ail_st_list))
1043+
if (bd) {
1044+
if (bd->bd_tr) {
1045+
if (tr)
1046+
gfs2_trans_add_revoke(sdp, bd);
1047+
else
1048+
gfs2_remove_from_ail(bd);
1049+
} else if (was_pinned) {
1050+
bh->b_private = NULL;
1051+
kmem_cache_free(gfs2_bufdata_cachep, bd);
1052+
} else if (!list_empty(&bd->bd_ail_st_list) &&
1053+
!list_empty(&bd->bd_ail_gl_list)) {
10151054
gfs2_remove_from_ail(bd);
1016-
kmem_cache_free(gfs2_bufdata_cachep, bd);
1055+
}
10171056
}
1057+
clear_buffer_dirty(bh);
1058+
clear_buffer_uptodate(bh);
10181059
}
10191060

10201061
/**
1021-
* gfs2_log_flush - flush incore transaction(s)
1062+
* __gfs2_log_flush - flush incore transaction(s)
10221063
* @sdp: The filesystem
10231064
* @gl: The glock structure to flush. If NULL, flush the whole incore log
10241065
* @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
10251066
*
10261067
*/
10271068

1028-
void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
1069+
static void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl,
1070+
u32 flags)
10291071
{
10301072
struct gfs2_trans *tr = NULL;
10311073
unsigned int reserved_blocks = 0, used_blocks = 0;
10321074
bool frozen = test_bit(SDF_FROZEN, &sdp->sd_flags);
10331075
unsigned int first_log_head;
10341076
unsigned int reserved_revokes = 0;
10351077

1036-
down_write(&sdp->sd_log_flush_lock);
10371078
trace_gfs2_log_flush(sdp, 1, flags);
10381079

10391080
repeat:
@@ -1110,7 +1151,7 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
11101151
goto out_withdraw;
11111152
lops_after_commit(sdp, tr);
11121153

1113-
gfs2_log_lock(sdp);
1154+
spin_lock(&sdp->sd_log_lock);
11141155
sdp->sd_log_blks_reserved = 0;
11151156

11161157
spin_lock(&sdp->sd_ail_lock);
@@ -1119,7 +1160,7 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
11191160
tr = NULL;
11201161
}
11211162
spin_unlock(&sdp->sd_ail_lock);
1122-
gfs2_log_unlock(sdp);
1163+
spin_unlock(&sdp->sd_log_lock);
11231164

11241165
if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
11251166
if (!sdp->sd_log_idle) {
@@ -1145,13 +1186,16 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
11451186
gfs2_assert_withdraw(sdp, used_blocks < reserved_blocks);
11461187
gfs2_log_release(sdp, reserved_blocks - used_blocks);
11471188
}
1148-
up_write(&sdp->sd_log_flush_lock);
11491189
gfs2_trans_free(sdp, tr);
11501190
trace_gfs2_log_flush(sdp, 0, flags);
11511191
return;
11521192

11531193
out_withdraw:
1154-
trans_drain(tr);
1194+
if (sdp->sd_jdesc->jd_log_bio) {
1195+
bio_io_error(sdp->sd_jdesc->jd_log_bio);
1196+
sdp->sd_jdesc->jd_log_bio = NULL;
1197+
}
1198+
gfs2_trans_drain(sdp, tr);
11551199
/**
11561200
* If the tr_list is empty, we're withdrawing during a log
11571201
* flush that targets a transaction, but the transaction was
@@ -1166,6 +1210,13 @@ void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
11661210
goto out_end;
11671211
}
11681212

1213+
void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
1214+
{
1215+
down_write(&sdp->sd_log_flush_lock);
1216+
__gfs2_log_flush(sdp, gl, flags);
1217+
up_write(&sdp->sd_log_flush_lock);
1218+
}
1219+
11691220
/**
11701221
* gfs2_merge_trans - Merge a new transaction into a cached transaction
11711222
* @sdp: the filesystem
@@ -1200,7 +1251,7 @@ static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
12001251
unsigned int unused;
12011252
unsigned int maxres;
12021253

1203-
gfs2_log_lock(sdp);
1254+
spin_lock(&sdp->sd_log_lock);
12041255

12051256
if (sdp->sd_log_tr) {
12061257
gfs2_merge_trans(sdp, tr);
@@ -1218,7 +1269,7 @@ static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
12181269
gfs2_log_release(sdp, unused);
12191270
sdp->sd_log_blks_reserved = reserved;
12201271

1221-
gfs2_log_unlock(sdp);
1272+
spin_unlock(&sdp->sd_log_lock);
12221273
}
12231274

12241275
static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
@@ -1297,19 +1348,25 @@ int gfs2_logd(void *data)
12971348
break;
12981349

12991350
if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
1351+
down_write(&sdp->sd_log_flush_lock);
13001352
gfs2_ail1_empty(sdp, 0);
1301-
gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1302-
GFS2_LFC_LOGD_JFLUSH_REQD);
1353+
__gfs2_log_flush(sdp, NULL,
1354+
GFS2_LOG_HEAD_FLUSH_NORMAL |
1355+
GFS2_LFC_LOGD_JFLUSH_REQD);
1356+
up_write(&sdp->sd_log_flush_lock);
13031357
}
13041358

13051359
if (test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
13061360
gfs2_ail_flush_reqd(sdp)) {
13071361
clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
1362+
down_write(&sdp->sd_log_flush_lock);
13081363
gfs2_ail1_start(sdp);
13091364
gfs2_ail1_wait(sdp);
13101365
gfs2_ail1_empty(sdp, 0);
1311-
gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1312-
GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1366+
__gfs2_log_flush(sdp, NULL,
1367+
GFS2_LOG_HEAD_FLUSH_NORMAL |
1368+
GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1369+
up_write(&sdp->sd_log_flush_lock);
13131370
}
13141371

13151372
t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;

0 commit comments

Comments
 (0)