Skip to content

Commit fca4451

Browse files
Greg Farnumliewegas
authored andcommitted
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: Greg Farnum <gregf@hq.newdream.net> Signed-off-by: Sage Weil <sage@newdream.net>
1 parent ac0b74d commit fca4451

2 files changed

Lines changed: 44 additions & 15 deletions

File tree

‎fs/ceph/locks.c‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,18 @@ void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
181181
* Encode the flock and fcntl locks for the given inode into the pagelist.
182182
* Format is: #fcntl locks, sequential fcntl locks, #flock locks,
183183
* sequential flock locks.
184-
* Must be called with BLK already held, and the lock numbers should have
185-
* been gathered under the same lock holding window.
184+
* Must be called with lock_flocks() already held.
185+
* If we encounter more of a specific lock type than expected,
186+
* we return the value 1.
186187
*/
187188
int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
188189
int num_fcntl_locks, int num_flock_locks)
189190
{
190191
struct file_lock *lock;
191192
struct ceph_filelock cephlock;
192193
int err = 0;
194+
int seen_fcntl = 0;
195+
int seen_flock = 0;
193196

194197
dout("encoding %d flock and %d fcntl locks", num_flock_locks,
195198
num_fcntl_locks);
@@ -198,6 +201,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
198201
goto fail;
199202
for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
200203
if (lock->fl_flags & FL_POSIX) {
204+
++seen_fcntl;
205+
if (seen_fcntl > num_fcntl_locks) {
206+
err = -ENOSPC;
207+
goto fail;
208+
}
201209
err = lock_to_ceph_filelock(lock, &cephlock);
202210
if (err)
203211
goto fail;
@@ -213,6 +221,11 @@ int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
213221
goto fail;
214222
for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
215223
if (lock->fl_flags & FL_FLOCK) {
224+
++seen_flock;
225+
if (seen_flock > num_flock_locks) {
226+
err = -ENOSPC;
227+
goto fail;
228+
}
216229
err = lock_to_ceph_filelock(lock, &cephlock);
217230
if (err)
218231
goto fail;

‎fs/ceph/mds_client.c‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,19 +2365,35 @@ static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap,
23652365

23662366
if (recon_state->flock) {
23672367
int num_fcntl_locks, num_flock_locks;
2368-
2369-
lock_kernel();
2370-
ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
2371-
rec.v2.flock_len = (2*sizeof(u32) +
2372-
(num_fcntl_locks+num_flock_locks) *
2373-
sizeof(struct ceph_filelock));
2374-
2375-
err = ceph_pagelist_append(pagelist, &rec, reclen);
2376-
if (!err)
2377-
err = ceph_encode_locks(inode, pagelist,
2378-
num_fcntl_locks,
2379-
num_flock_locks);
2380-
unlock_kernel();
2368+
struct ceph_pagelist_cursor trunc_point;
2369+
2370+
ceph_pagelist_set_cursor(pagelist, &trunc_point);
2371+
do {
2372+
lock_kernel();
2373+
ceph_count_locks(inode, &num_fcntl_locks,
2374+
&num_flock_locks);
2375+
rec.v2.flock_len = (2*sizeof(u32) +
2376+
(num_fcntl_locks+num_flock_locks) *
2377+
sizeof(struct ceph_filelock));
2378+
unlock_kernel();
2379+
2380+
/* pre-alloc pagelist */
2381+
ceph_pagelist_truncate(pagelist, &trunc_point);
2382+
err = ceph_pagelist_append(pagelist, &rec, reclen);
2383+
if (!err)
2384+
err = ceph_pagelist_reserve(pagelist,
2385+
rec.v2.flock_len);
2386+
2387+
/* encode locks */
2388+
if (!err) {
2389+
lock_kernel();
2390+
err = ceph_encode_locks(inode,
2391+
pagelist,
2392+
num_fcntl_locks,
2393+
num_flock_locks);
2394+
unlock_kernel();
2395+
}
2396+
} while (err == -ENOSPC);
23812397
} else {
23822398
err = ceph_pagelist_append(pagelist, &rec, reclen);
23832399
}

0 commit comments

Comments
 (0)