Commit fca4451a authored by Greg Farnum's avatar Greg Farnum Committed by Sage Weil

ceph: preallocate flock state without locks held

When the lock_kernel() turns into lock_flocks() and a spinlock, we won't
be able to do allocations with the lock held.  Preallocate space without
the lock, and retry if the lock state changes out from underneath us.
Signed-off-by: default avatarGreg Farnum <gregf@hq.newdream.net>
Signed-off-by: default avatarSage Weil <sage@newdream.net>
parent ac0b74d8
...@@ -181,8 +181,9 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count) ...@@ -181,8 +181,9 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
* Encode the flock and fcntl locks for the given inode into the pagelist. * Encode the flock and fcntl locks for the given inode into the pagelist.
* Format is: #fcntl locks, sequential fcntl locks, #flock locks, * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
* sequential flock locks. * sequential flock locks.
* Must be called with BLK already held, and the lock numbers should have * Must be called with lock_flocks() already held.
* been gathered under the same lock holding window. * If we encounter more of a specific lock type than expected,
* we return the value 1.
*/ */
int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist, int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
int num_fcntl_locks, int num_flock_locks) int num_fcntl_locks, int num_flock_locks)
...@@ -190,6 +191,8 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist, ...@@ -190,6 +191,8 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
struct file_lock *lock; struct file_lock *lock;
struct ceph_filelock cephlock; struct ceph_filelock cephlock;
int err = 0; int err = 0;
int seen_fcntl = 0;
int seen_flock = 0;
dout("encoding %d flock and %d fcntl locks", num_flock_locks, dout("encoding %d flock and %d fcntl locks", num_flock_locks,
num_fcntl_locks); num_fcntl_locks);
...@@ -198,6 +201,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist, ...@@ -198,6 +201,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
goto fail; goto fail;
for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
if (lock->fl_flags & FL_POSIX) { if (lock->fl_flags & FL_POSIX) {
++seen_fcntl;
if (seen_fcntl > num_fcntl_locks) {
err = -ENOSPC;
goto fail;
}
err = lock_to_ceph_filelock(lock, &cephlock); err = lock_to_ceph_filelock(lock, &cephlock);
if (err) if (err)
goto fail; goto fail;
...@@ -213,6 +221,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist, ...@@ -213,6 +221,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
goto fail; goto fail;
for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
if (lock->fl_flags & FL_FLOCK) { if (lock->fl_flags & FL_FLOCK) {
++seen_flock;
if (seen_flock > num_flock_locks) {
err = -ENOSPC;
goto fail;
}
err = lock_to_ceph_filelock(lock, &cephlock); err = lock_to_ceph_filelock(lock, &cephlock);
if (err) if (err)
goto fail; goto fail;
......
...@@ -2365,19 +2365,35 @@ static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, ...@@ -2365,19 +2365,35 @@ static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
if (recon_state->flock) { if (recon_state->flock) {
int num_fcntl_locks, num_flock_locks; int num_fcntl_locks, num_flock_locks;
struct ceph_pagelist_cursor trunc_point;
lock_kernel();
ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks); ceph_pagelist_set_cursor(pagelist, &trunc_point);
rec.v2.flock_len = (2*sizeof(u32) + do {
(num_fcntl_locks+num_flock_locks) * lock_kernel();
sizeof(struct ceph_filelock)); ceph_count_locks(inode, &num_fcntl_locks,
&num_flock_locks);
err = ceph_pagelist_append(pagelist, &rec, reclen); rec.v2.flock_len = (2*sizeof(u32) +
if (!err) (num_fcntl_locks+num_flock_locks) *
err = ceph_encode_locks(inode, pagelist, sizeof(struct ceph_filelock));
num_fcntl_locks, unlock_kernel();
num_flock_locks);
unlock_kernel(); /* pre-alloc pagelist */
ceph_pagelist_truncate(pagelist, &trunc_point);
err = ceph_pagelist_append(pagelist, &rec, reclen);
if (!err)
err = ceph_pagelist_reserve(pagelist,
rec.v2.flock_len);
/* encode locks */
if (!err) {
lock_kernel();
err = ceph_encode_locks(inode,
pagelist,
num_fcntl_locks,
num_flock_locks);
unlock_kernel();
}
} while (err == -ENOSPC);
} else { } else {
err = ceph_pagelist_append(pagelist, &rec, reclen); err = ceph_pagelist_append(pagelist, &rec, reclen);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment