Skip to content

Commit 3d198e4

Browse files
committed
Merge tag 'gfs2-v5.17-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2
Pull gfs2 fixes from Andreas Gruenbacher: - To avoid deadlocks, actively cancel dlm locking requests when we give up on them. Further dlm operations on the same lock will return -EBUSY until the cancel has been completed, so in that case, wait and repeat. (This is rare.) - Lock inversion fixes in gfs2_inode_lookup() and gfs2_create_inode(). - Some more fallout from the gfs2 mmap + page fault deadlock fixes (merged in commit c03098d: "Merge tag 'gfs2-v5.15-rc5-mmap-fault'"). - Various other minor bug fixes and cleanups. * tag 'gfs2-v5.17-rc4-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2: gfs2: Make sure FITRIM minlen is rounded up to fs block size gfs2: Make sure not to return short direct writes gfs2: Remove dead code in gfs2_file_read_iter gfs2: Fix gfs2_file_buffered_write endless loop workaround gfs2: Minor retry logic cleanup gfs2: Disable page faults during lockless buffered reads gfs2: Fix should_fault_in_pages() logic gfs2: Remove return value for gfs2_indirect_init gfs2: Initialize gh_error in gfs2_glock_nq gfs2: Make use of list_is_first gfs2: Switch lock order of inode and iopen glock gfs2: cancel timed-out glock requests gfs2: Expect -EBUSY after canceling dlm locking requests gfs2: gfs2_setattr_size error path fix gfs2: assign rgrp glock before compute_bitstructs
2 parents f008b1d + 27ca827 commit 3d198e4

File tree

8 files changed

+104
-76
lines changed

8 files changed

+104
-76
lines changed

fs/gfs2/bmap.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,9 @@ static int gfs2_hole_size(struct inode *inode, sector_t lblock, u64 len,
606606
return ret;
607607
}
608608

609-
static inline __be64 *gfs2_indirect_init(struct metapath *mp,
610-
struct gfs2_glock *gl, unsigned int i,
611-
unsigned offset, u64 bn)
609+
static inline void gfs2_indirect_init(struct metapath *mp,
610+
struct gfs2_glock *gl, unsigned int i,
611+
unsigned offset, u64 bn)
612612
{
613613
__be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
614614
((i > 1) ? sizeof(struct gfs2_meta_header) :
@@ -621,7 +621,6 @@ static inline __be64 *gfs2_indirect_init(struct metapath *mp,
621621
gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
622622
ptr += offset;
623623
*ptr = cpu_to_be64(bn);
624-
return ptr;
625624
}
626625

627626
enum alloc_state {
@@ -2146,7 +2145,7 @@ int gfs2_setattr_size(struct inode *inode, u64 newsize)
21462145

21472146
ret = do_shrink(inode, newsize);
21482147
out:
2149-
gfs2_rs_delete(ip, NULL);
2148+
gfs2_rs_delete(ip);
21502149
gfs2_qa_put(ip);
21512150
return ret;
21522151
}

fs/gfs2/file.c

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ static int gfs2_release(struct inode *inode, struct file *file)
706706

707707
if (file->f_mode & FMODE_WRITE) {
708708
if (gfs2_rs_active(&ip->i_res))
709-
gfs2_rs_delete(ip, &inode->i_writecount);
709+
gfs2_rs_delete(ip);
710710
gfs2_qa_put(ip);
711711
}
712712
return 0;
@@ -775,8 +775,7 @@ static inline bool should_fault_in_pages(ssize_t ret, struct iov_iter *i,
775775
size_t *window_size)
776776
{
777777
size_t count = iov_iter_count(i);
778-
char __user *p;
779-
int pages = 1;
778+
size_t size, offs;
780779

781780
if (likely(!count))
782781
return false;
@@ -785,18 +784,20 @@ static inline bool should_fault_in_pages(ssize_t ret, struct iov_iter *i,
785784
if (!iter_is_iovec(i))
786785
return false;
787786

787+
size = PAGE_SIZE;
788+
offs = offset_in_page(i->iov[0].iov_base + i->iov_offset);
788789
if (*prev_count != count || !*window_size) {
789-
int pages, nr_dirtied;
790+
size_t nr_dirtied;
790791

791-
pages = min_t(int, BIO_MAX_VECS, DIV_ROUND_UP(count, PAGE_SIZE));
792+
size = ALIGN(offs + count, PAGE_SIZE);
793+
size = min_t(size_t, size, SZ_1M);
792794
nr_dirtied = max(current->nr_dirtied_pause -
793-
current->nr_dirtied, 1);
794-
pages = min(pages, nr_dirtied);
795+
current->nr_dirtied, 8);
796+
size = min(size, nr_dirtied << PAGE_SHIFT);
795797
}
796798

797799
*prev_count = count;
798-
p = i->iov[0].iov_base + i->iov_offset;
799-
*window_size = (size_t)PAGE_SIZE * pages - offset_in_page(p);
800+
*window_size = size - offs;
800801
return true;
801802
}
802803

@@ -851,9 +852,9 @@ static ssize_t gfs2_file_direct_read(struct kiocb *iocb, struct iov_iter *to,
851852
leftover = fault_in_iov_iter_writeable(to, window_size);
852853
gfs2_holder_disallow_demote(gh);
853854
if (leftover != window_size) {
854-
if (!gfs2_holder_queued(gh))
855-
goto retry;
856-
goto retry_under_glock;
855+
if (gfs2_holder_queued(gh))
856+
goto retry_under_glock;
857+
goto retry;
857858
}
858859
}
859860
if (gfs2_holder_queued(gh))
@@ -920,9 +921,9 @@ static ssize_t gfs2_file_direct_write(struct kiocb *iocb, struct iov_iter *from,
920921
leftover = fault_in_iov_iter_readable(from, window_size);
921922
gfs2_holder_disallow_demote(gh);
922923
if (leftover != window_size) {
923-
if (!gfs2_holder_queued(gh))
924-
goto retry;
925-
goto retry_under_glock;
924+
if (gfs2_holder_queued(gh))
925+
goto retry_under_glock;
926+
goto retry;
926927
}
927928
}
928929
out:
@@ -950,20 +951,19 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
950951
* and retry.
951952
*/
952953

953-
if (iocb->ki_flags & IOCB_DIRECT) {
954-
ret = gfs2_file_direct_read(iocb, to, &gh);
955-
if (likely(ret != -ENOTBLK))
956-
return ret;
957-
iocb->ki_flags &= ~IOCB_DIRECT;
958-
}
954+
if (iocb->ki_flags & IOCB_DIRECT)
955+
return gfs2_file_direct_read(iocb, to, &gh);
956+
957+
pagefault_disable();
959958
iocb->ki_flags |= IOCB_NOIO;
960959
ret = generic_file_read_iter(iocb, to);
961960
iocb->ki_flags &= ~IOCB_NOIO;
961+
pagefault_enable();
962962
if (ret >= 0) {
963963
if (!iov_iter_count(to))
964964
return ret;
965965
written = ret;
966-
} else {
966+
} else if (ret != -EFAULT) {
967967
if (ret != -EAGAIN)
968968
return ret;
969969
if (iocb->ki_flags & IOCB_NOWAIT)
@@ -989,12 +989,11 @@ static ssize_t gfs2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
989989
leftover = fault_in_iov_iter_writeable(to, window_size);
990990
gfs2_holder_disallow_demote(&gh);
991991
if (leftover != window_size) {
992-
if (!gfs2_holder_queued(&gh)) {
993-
if (written)
994-
goto out_uninit;
995-
goto retry;
996-
}
997-
goto retry_under_glock;
992+
if (gfs2_holder_queued(&gh))
993+
goto retry_under_glock;
994+
if (written)
995+
goto out_uninit;
996+
goto retry;
998997
}
999998
}
1000999
if (gfs2_holder_queued(&gh))
@@ -1068,12 +1067,11 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
10681067
gfs2_holder_disallow_demote(gh);
10691068
if (leftover != window_size) {
10701069
from->count = min(from->count, window_size - leftover);
1071-
if (!gfs2_holder_queued(gh)) {
1072-
if (read)
1073-
goto out_uninit;
1074-
goto retry;
1075-
}
1076-
goto retry_under_glock;
1070+
if (gfs2_holder_queued(gh))
1071+
goto retry_under_glock;
1072+
if (read && !(iocb->ki_flags & IOCB_DIRECT))
1073+
goto out_uninit;
1074+
goto retry;
10771075
}
10781076
}
10791077
out_unlock:
@@ -1083,6 +1081,7 @@ static ssize_t gfs2_file_buffered_write(struct kiocb *iocb,
10831081
gfs2_holder_uninit(gh);
10841082
if (statfs_gh)
10851083
kfree(statfs_gh);
1084+
from->count = orig_count - read;
10861085
return read ? read : ret;
10871086
}
10881087

@@ -1497,7 +1496,6 @@ static int do_flock(struct file *file, int cmd, struct file_lock *fl)
14971496
if (error != GLR_TRYFAILED)
14981497
break;
14991498
fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
1500-
fl_gh->gh_error = 0;
15011499
msleep(sleeptime);
15021500
}
15031501
if (error) {

fs/gfs2/glock.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ __acquires(&gl->gl_lockref.lock)
542542
* some reason. If this holder is the head of the list, it
543543
* means we have a blocked holder at the head, so return 1.
544544
*/
545-
if (gh->gh_list.prev == &gl->gl_holders)
545+
if (list_is_first(&gh->gh_list, &gl->gl_holders))
546546
return 1;
547547
do_error(gl, 0);
548548
break;
@@ -669,6 +669,8 @@ static void finish_xmote(struct gfs2_glock *gl, unsigned int ret)
669669

670670
/* Check for state != intended state */
671671
if (unlikely(state != gl->gl_target)) {
672+
if (gh && (ret & LM_OUT_CANCELED))
673+
gfs2_holder_wake(gh);
672674
if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) {
673675
/* move to back of queue and try next entry */
674676
if (ret & LM_OUT_CANCELED) {
@@ -1259,7 +1261,6 @@ void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags,
12591261
gh->gh_owner_pid = get_pid(task_pid(current));
12601262
gh->gh_state = state;
12611263
gh->gh_flags = flags;
1262-
gh->gh_error = 0;
12631264
gh->gh_iflags = 0;
12641265
gfs2_glock_hold(gl);
12651266
}
@@ -1565,6 +1566,7 @@ int gfs2_glock_nq(struct gfs2_holder *gh)
15651566
if (test_bit(GLF_LRU, &gl->gl_flags))
15661567
gfs2_glock_remove_from_lru(gl);
15671568

1569+
gh->gh_error = 0;
15681570
spin_lock(&gl->gl_lockref.lock);
15691571
add_to_queue(gh);
15701572
if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) &&
@@ -1691,6 +1693,14 @@ void gfs2_glock_dq(struct gfs2_holder *gh)
16911693
struct gfs2_glock *gl = gh->gh_gl;
16921694

16931695
spin_lock(&gl->gl_lockref.lock);
1696+
if (list_is_first(&gh->gh_list, &gl->gl_holders) &&
1697+
!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1698+
spin_unlock(&gl->gl_lockref.lock);
1699+
gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl);
1700+
wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE);
1701+
spin_lock(&gl->gl_lockref.lock);
1702+
}
1703+
16941704
__gfs2_glock_dq(gh);
16951705
spin_unlock(&gl->gl_lockref.lock);
16961706
}

fs/gfs2/inode.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,21 @@ struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
131131
struct gfs2_sbd *sdp = GFS2_SB(inode);
132132
struct gfs2_glock *io_gl;
133133

134-
error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
134+
error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
135+
&ip->i_gl);
136+
if (unlikely(error))
137+
goto fail;
138+
139+
error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE,
140+
&io_gl);
141+
if (unlikely(error))
142+
goto fail;
143+
144+
if (blktype != GFS2_BLKST_UNLINKED)
145+
gfs2_cancel_delete_work(io_gl);
146+
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT,
147+
&ip->i_iopen_gh);
148+
gfs2_glock_put(io_gl);
135149
if (unlikely(error))
136150
goto fail;
137151

@@ -161,16 +175,6 @@ struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
161175

162176
set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
163177

164-
error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
165-
if (unlikely(error))
166-
goto fail;
167-
if (blktype != GFS2_BLKST_UNLINKED)
168-
gfs2_cancel_delete_work(io_gl);
169-
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
170-
gfs2_glock_put(io_gl);
171-
if (unlikely(error))
172-
goto fail;
173-
174178
/* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
175179
inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
176180
inode->i_atime.tv_nsec = 0;
@@ -716,13 +720,17 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
716720
error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
717721
BUG_ON(error);
718722

719-
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
723+
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
720724
if (error)
721725
goto fail_gunlock2;
722726

727+
error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
728+
if (error)
729+
goto fail_gunlock3;
730+
723731
error = gfs2_trans_begin(sdp, blocks, 0);
724732
if (error)
725-
goto fail_gunlock2;
733+
goto fail_gunlock3;
726734

727735
if (blocks > 1) {
728736
ip->i_eattr = ip->i_no_addr + 1;
@@ -731,10 +739,6 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
731739
init_dinode(dip, ip, symname);
732740
gfs2_trans_end(sdp);
733741

734-
error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
735-
if (error)
736-
goto fail_gunlock2;
737-
738742
glock_set_object(ip->i_gl, ip);
739743
glock_set_object(io_gl, ip);
740744
gfs2_set_iop(inode);
@@ -745,26 +749,26 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
745749
if (default_acl) {
746750
error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
747751
if (error)
748-
goto fail_gunlock3;
752+
goto fail_gunlock4;
749753
posix_acl_release(default_acl);
750754
default_acl = NULL;
751755
}
752756
if (acl) {
753757
error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
754758
if (error)
755-
goto fail_gunlock3;
759+
goto fail_gunlock4;
756760
posix_acl_release(acl);
757761
acl = NULL;
758762
}
759763

760764
error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
761765
&gfs2_initxattrs, NULL);
762766
if (error)
763-
goto fail_gunlock3;
767+
goto fail_gunlock4;
764768

765769
error = link_dinode(dip, name, ip, &da);
766770
if (error)
767-
goto fail_gunlock3;
771+
goto fail_gunlock4;
768772

769773
mark_inode_dirty(inode);
770774
d_instantiate(dentry, inode);
@@ -782,9 +786,10 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
782786
unlock_new_inode(inode);
783787
return error;
784788

785-
fail_gunlock3:
789+
fail_gunlock4:
786790
glock_clear_object(ip->i_gl, ip);
787791
glock_clear_object(io_gl, ip);
792+
fail_gunlock3:
788793
gfs2_glock_dq_uninit(&ip->i_iopen_gh);
789794
fail_gunlock2:
790795
gfs2_glock_put(io_gl);
@@ -793,7 +798,7 @@ static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
793798
if (free_vfs_inode) /* else evict will do the put for us */
794799
gfs2_glock_put(ip->i_gl);
795800
}
796-
gfs2_rs_delete(ip, NULL);
801+
gfs2_rs_deltree(&ip->i_res);
797802
gfs2_qa_put(ip);
798803
fail_free_acls:
799804
posix_acl_release(default_acl);

fs/gfs2/lock_dlm.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
261261
int req;
262262
u32 lkf;
263263
char strname[GDLM_STRNAME_BYTES] = "";
264+
int error;
264265

265266
req = make_mode(gl->gl_name.ln_sbd, req_state);
266267
lkf = make_flags(gl, flags, req);
@@ -279,8 +280,14 @@ static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
279280
* Submit the actual lock request.
280281
*/
281282

282-
return dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, strname,
283+
again:
284+
error = dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, strname,
283285
GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
286+
if (error == -EBUSY) {
287+
msleep(20);
288+
goto again;
289+
}
290+
return error;
284291
}
285292

286293
static void gdlm_put_lock(struct gfs2_glock *gl)
@@ -312,8 +319,14 @@ static void gdlm_put_lock(struct gfs2_glock *gl)
312319
return;
313320
}
314321

322+
again:
315323
error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
316324
NULL, gl);
325+
if (error == -EBUSY) {
326+
msleep(20);
327+
goto again;
328+
}
329+
317330
if (error) {
318331
fs_err(sdp, "gdlm_unlock %x,%llx err=%d\n",
319332
gl->gl_name.ln_type,

0 commit comments

Comments
 (0)