forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/fs/ceph/locks.c
....@@ -32,14 +32,18 @@
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);
....@@ -59,7 +63,7 @@
5963 static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
6064 int cmd, u8 wait, struct file_lock *fl)
6165 {
62
- struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
66
+ struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
6367 struct ceph_mds_request *req;
6468 int err;
6569 u64 length = 0;
....@@ -73,7 +77,7 @@
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)
....@@ -206,6 +210,21 @@
206210 return 0;
207211 }
208212
213
+static int try_unlock_file(struct file *file, struct file_lock *fl)
214
+{
215
+ int err;
216
+ unsigned int orig_flags = fl->fl_flags;
217
+ fl->fl_flags |= FL_EXISTS;
218
+ err = locks_lock_file_wait(file, fl);
219
+ fl->fl_flags = orig_flags;
220
+ if (err == -ENOENT) {
221
+ if (!(orig_flags & FL_EXISTS))
222
+ err = 0;
223
+ return err;
224
+ }
225
+ return 1;
226
+}
227
+
209228 /**
210229 * Attempt to set an fcntl lock.
211230 * For now, this just goes away to the server. Later it may be more awesome.
....@@ -236,15 +255,6 @@
236255 spin_lock(&ci->i_ceph_lock);
237256 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
238257 err = -EIO;
239
- } else if (op == CEPH_MDS_OP_SETFILELOCK) {
240
- /*
241
- * increasing i_filelock_ref closes race window between
242
- * handling request reply and adding file_lock struct to
243
- * inode. Otherwise, i_auth_cap may get trimmed in the
244
- * window. Caller function will decrease the counter.
245
- */
246
- fl->fl_ops = &ceph_fl_lock_ops;
247
- atomic_inc(&ci->i_filelock_ref);
248258 }
249259 spin_unlock(&ci->i_ceph_lock);
250260 if (err < 0) {
....@@ -260,9 +270,15 @@
260270 else
261271 lock_cmd = CEPH_LOCK_UNLOCK;
262272
273
+ if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
274
+ err = try_unlock_file(file, fl);
275
+ if (err <= 0)
276
+ return err;
277
+ }
278
+
263279 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
264280 if (!err) {
265
- if (op == CEPH_MDS_OP_SETFILELOCK) {
281
+ if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
266282 dout("mds locked, locking locally\n");
267283 err = posix_lock_file(file, fl, NULL);
268284 if (err) {
....@@ -298,10 +314,6 @@
298314 spin_lock(&ci->i_ceph_lock);
299315 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
300316 err = -EIO;
301
- } else {
302
- /* see comment in ceph_lock */
303
- fl->fl_ops = &ceph_fl_lock_ops;
304
- atomic_inc(&ci->i_filelock_ref);
305317 }
306318 spin_unlock(&ci->i_ceph_lock);
307319 if (err < 0) {
....@@ -320,9 +332,15 @@
320332 else
321333 lock_cmd = CEPH_LOCK_UNLOCK;
322334
335
+ if (F_UNLCK == fl->fl_type) {
336
+ err = try_unlock_file(file, fl);
337
+ if (err <= 0)
338
+ return err;
339
+ }
340
+
323341 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
324342 inode, lock_cmd, wait, fl);
325
- if (!err) {
343
+ if (!err && F_UNLCK != fl->fl_type) {
326344 err = locks_lock_file_wait(file, fl);
327345 if (err) {
328346 ceph_lock_message(CEPH_LOCK_FLOCK,