Skip to content

Commit ff5d913

Browse files
ukernelidryomov
authored andcommitted
ceph: return -EIO if read/write against filp that lost file locks
After mds evicts session, file locks get lost sliently. It's not safe to let programs continue to do read/write. Signed-off-by: "Yan, Zheng" <zyan@redhat.com> Reviewed-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent d468e72 commit ff5d913

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎fs/ceph/caps.c‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,8 +2570,13 @@ static void __take_cap_refs(struct ceph_inode_info *ci, int got,
25702570
*
25712571
* FIXME: how does a 0 return differ from -EAGAIN?
25722572
*/
2573+
enum {
2574+
NON_BLOCKING = 1,
2575+
CHECK_FILELOCK = 2,
2576+
};
2577+
25732578
static int try_get_cap_refs(struct inode *inode, int need, int want,
2574-
loff_t endoff, bool nonblock, int *got)
2579+
loff_t endoff, int flags, int *got)
25752580
{
25762581
struct ceph_inode_info *ci = ceph_inode(inode);
25772582
struct ceph_mds_client *mdsc = ceph_inode_to_client(inode)->mdsc;
@@ -2586,6 +2591,13 @@ static int try_get_cap_refs(struct inode *inode, int need, int want,
25862591
again:
25872592
spin_lock(&ci->i_ceph_lock);
25882593

2594+
if ((flags & CHECK_FILELOCK) &&
2595+
(ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK)) {
2596+
dout("try_get_cap_refs %p error filelock\n", inode);
2597+
ret = -EIO;
2598+
goto out_unlock;
2599+
}
2600+
25892601
/* make sure file is actually open */
25902602
file_wanted = __ceph_caps_file_wanted(ci);
25912603
if ((file_wanted & need) != need) {
@@ -2647,7 +2659,7 @@ static int try_get_cap_refs(struct inode *inode, int need, int want,
26472659
* we can not call down_read() when
26482660
* task isn't in TASK_RUNNING state
26492661
*/
2650-
if (nonblock) {
2662+
if (flags & NON_BLOCKING) {
26512663
ret = -EAGAIN;
26522664
goto out_unlock;
26532665
}
@@ -2752,7 +2764,8 @@ int ceph_try_get_caps(struct inode *inode, int need, int want,
27522764
if (ret < 0)
27532765
return ret;
27542766

2755-
ret = try_get_cap_refs(inode, need, want, 0, nonblock, got);
2767+
ret = try_get_cap_refs(inode, need, want, 0,
2768+
(nonblock ? NON_BLOCKING : 0), got);
27562769
return ret == -EAGAIN ? 0 : ret;
27572770
}
27582771

@@ -2764,9 +2777,10 @@ int ceph_try_get_caps(struct inode *inode, int need, int want,
27642777
int ceph_get_caps(struct file *filp, int need, int want,
27652778
loff_t endoff, int *got, struct page **pinned_page)
27662779
{
2780+
struct ceph_file_info *fi = filp->private_data;
27672781
struct inode *inode = file_inode(filp);
27682782
struct ceph_inode_info *ci = ceph_inode(inode);
2769-
int _got, ret;
2783+
int ret, _got, flags;
27702784

27712785
ret = ceph_pool_perm_check(inode, need);
27722786
if (ret < 0)
@@ -2776,17 +2790,19 @@ int ceph_get_caps(struct file *filp, int need, int want,
27762790
if (endoff > 0)
27772791
check_max_size(inode, endoff);
27782792

2793+
flags = atomic_read(&fi->num_locks) ? CHECK_FILELOCK : 0;
27792794
_got = 0;
27802795
ret = try_get_cap_refs(inode, need, want, endoff,
2781-
false, &_got);
2796+
flags, &_got);
27822797
if (ret == -EAGAIN)
27832798
continue;
27842799
if (!ret) {
27852800
DEFINE_WAIT_FUNC(wait, woken_wake_function);
27862801
add_wait_queue(&ci->i_cap_wq, &wait);
27872802

2803+
flags |= NON_BLOCKING;
27882804
while (!(ret = try_get_cap_refs(inode, need, want,
2789-
endoff, true, &_got))) {
2805+
endoff, flags, &_got))) {
27902806
if (signal_pending(current)) {
27912807
ret = -ERESTARTSYS;
27922808
break;

‎fs/ceph/locks.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ void __init ceph_flock_init(void)
3232

3333
static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
3434
{
35-
struct inode *inode = file_inode(src->fl_file);
35+
struct ceph_file_info *fi = dst->fl_file->private_data;
36+
struct inode *inode = file_inode(dst->fl_file);
3637
atomic_inc(&ceph_inode(inode)->i_filelock_ref);
38+
atomic_inc(&fi->num_locks);
3739
}
3840

3941
static void ceph_fl_release_lock(struct file_lock *fl)
4042
{
43+
struct ceph_file_info *fi = fl->fl_file->private_data;
4144
struct inode *inode = file_inode(fl->fl_file);
4245
struct ceph_inode_info *ci = ceph_inode(inode);
46+
atomic_dec(&fi->num_locks);
4347
if (atomic_dec_and_test(&ci->i_filelock_ref)) {
4448
/* clear error when all locks are released */
4549
spin_lock(&ci->i_ceph_lock);
@@ -73,7 +77,7 @@ static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
7377
* window. Caller function will decrease the counter.
7478
*/
7579
fl->fl_ops = &ceph_fl_lock_ops;
76-
atomic_inc(&ceph_inode(inode)->i_filelock_ref);
80+
fl->fl_ops->fl_copy_lock(fl, NULL);
7781
}
7882

7983
if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)

‎fs/ceph/super.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ struct ceph_file_info {
707707
struct list_head rw_contexts;
708708

709709
errseq_t meta_err;
710+
atomic_t num_locks;
710711
};
711712

712713
struct ceph_dir_file_info {

0 commit comments

Comments
 (0)