0001
0002 #include <linux/ceph/ceph_debug.h>
0003
0004 #include <linux/file.h>
0005 #include <linux/namei.h>
0006 #include <linux/random.h>
0007
0008 #include "super.h"
0009 #include "mds_client.h"
0010 #include <linux/ceph/pagelist.h>
0011
0012 static u64 lock_secret;
0013 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
0014 struct ceph_mds_request *req);
0015
0016 static inline u64 secure_addr(void *addr)
0017 {
0018 u64 v = lock_secret ^ (u64)(unsigned long)addr;
0019
0020
0021
0022
0023
0024 v |= (1ULL << 63);
0025 return v;
0026 }
0027
0028 void __init ceph_flock_init(void)
0029 {
0030 get_random_bytes(&lock_secret, sizeof(lock_secret));
0031 }
0032
0033 static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
0034 {
0035 struct ceph_file_info *fi = dst->fl_file->private_data;
0036 struct inode *inode = file_inode(dst->fl_file);
0037 atomic_inc(&ceph_inode(inode)->i_filelock_ref);
0038 atomic_inc(&fi->num_locks);
0039 }
0040
0041 static void ceph_fl_release_lock(struct file_lock *fl)
0042 {
0043 struct ceph_file_info *fi = fl->fl_file->private_data;
0044 struct inode *inode = file_inode(fl->fl_file);
0045 struct ceph_inode_info *ci = ceph_inode(inode);
0046 atomic_dec(&fi->num_locks);
0047 if (atomic_dec_and_test(&ci->i_filelock_ref)) {
0048
0049 spin_lock(&ci->i_ceph_lock);
0050 ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
0051 spin_unlock(&ci->i_ceph_lock);
0052 }
0053 }
0054
0055 static const struct file_lock_operations ceph_fl_lock_ops = {
0056 .fl_copy_lock = ceph_fl_copy_lock,
0057 .fl_release_private = ceph_fl_release_lock,
0058 };
0059
0060
0061
0062
0063 static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
0064 int cmd, u8 wait, struct file_lock *fl)
0065 {
0066 struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
0067 struct ceph_mds_request *req;
0068 int err;
0069 u64 length = 0;
0070 u64 owner;
0071
0072 if (operation == CEPH_MDS_OP_SETFILELOCK) {
0073
0074
0075
0076
0077
0078
0079 fl->fl_ops = &ceph_fl_lock_ops;
0080 fl->fl_ops->fl_copy_lock(fl, NULL);
0081 }
0082
0083 if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
0084 wait = 0;
0085
0086 req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
0087 if (IS_ERR(req))
0088 return PTR_ERR(req);
0089 req->r_inode = inode;
0090 ihold(inode);
0091 req->r_num_caps = 1;
0092
0093
0094 if (LLONG_MAX == fl->fl_end)
0095 length = 0;
0096 else
0097 length = fl->fl_end - fl->fl_start + 1;
0098
0099 owner = secure_addr(fl->fl_owner);
0100
0101 dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
0102 "start: %llu, length: %llu, wait: %d, type: %d\n", (int)lock_type,
0103 (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
0104 wait, fl->fl_type);
0105
0106 req->r_args.filelock_change.rule = lock_type;
0107 req->r_args.filelock_change.type = cmd;
0108 req->r_args.filelock_change.owner = cpu_to_le64(owner);
0109 req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
0110 req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
0111 req->r_args.filelock_change.length = cpu_to_le64(length);
0112 req->r_args.filelock_change.wait = wait;
0113
0114 err = ceph_mdsc_submit_request(mdsc, inode, req);
0115 if (!err)
0116 err = ceph_mdsc_wait_request(mdsc, req, wait ?
0117 ceph_lock_wait_for_completion : NULL);
0118 if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
0119 fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
0120 if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
0121 fl->fl_type = F_RDLCK;
0122 else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
0123 fl->fl_type = F_WRLCK;
0124 else
0125 fl->fl_type = F_UNLCK;
0126
0127 fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
0128 length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
0129 le64_to_cpu(req->r_reply_info.filelock_reply->length);
0130 if (length >= 1)
0131 fl->fl_end = length -1;
0132 else
0133 fl->fl_end = 0;
0134
0135 }
0136 ceph_mdsc_put_request(req);
0137 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
0138 "length: %llu, wait: %d, type: %d, err code %d\n", (int)lock_type,
0139 (int)operation, (u64)fl->fl_pid, fl->fl_start,
0140 length, wait, fl->fl_type, err);
0141 return err;
0142 }
0143
0144 static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
0145 struct ceph_mds_request *req)
0146 {
0147 struct ceph_mds_request *intr_req;
0148 struct inode *inode = req->r_inode;
0149 int err, lock_type;
0150
0151 BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
0152 if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
0153 lock_type = CEPH_LOCK_FCNTL_INTR;
0154 else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
0155 lock_type = CEPH_LOCK_FLOCK_INTR;
0156 else
0157 BUG_ON(1);
0158 BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
0159
0160 err = wait_for_completion_interruptible(&req->r_completion);
0161 if (!err)
0162 return 0;
0163
0164 dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
0165 req->r_tid);
0166
0167 mutex_lock(&mdsc->mutex);
0168 if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
0169 err = 0;
0170 } else {
0171
0172
0173
0174
0175
0176 mutex_lock(&req->r_fill_mutex);
0177 req->r_err = err;
0178 set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
0179 mutex_unlock(&req->r_fill_mutex);
0180
0181 if (!req->r_session) {
0182
0183 err = 0;
0184 }
0185 }
0186 mutex_unlock(&mdsc->mutex);
0187 if (!err)
0188 return 0;
0189
0190 intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
0191 USE_AUTH_MDS);
0192 if (IS_ERR(intr_req))
0193 return PTR_ERR(intr_req);
0194
0195 intr_req->r_inode = inode;
0196 ihold(inode);
0197 intr_req->r_num_caps = 1;
0198
0199 intr_req->r_args.filelock_change = req->r_args.filelock_change;
0200 intr_req->r_args.filelock_change.rule = lock_type;
0201 intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
0202
0203 err = ceph_mdsc_do_request(mdsc, inode, intr_req);
0204 ceph_mdsc_put_request(intr_req);
0205
0206 if (err && err != -ERESTARTSYS)
0207 return err;
0208
0209 wait_for_completion_killable(&req->r_safe_completion);
0210 return 0;
0211 }
0212
0213 static int try_unlock_file(struct file *file, struct file_lock *fl)
0214 {
0215 int err;
0216 unsigned int orig_flags = fl->fl_flags;
0217 fl->fl_flags |= FL_EXISTS;
0218 err = locks_lock_file_wait(file, fl);
0219 fl->fl_flags = orig_flags;
0220 if (err == -ENOENT) {
0221 if (!(orig_flags & FL_EXISTS))
0222 err = 0;
0223 return err;
0224 }
0225 return 1;
0226 }
0227
0228
0229
0230
0231
0232 int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
0233 {
0234 struct inode *inode = file_inode(file);
0235 struct ceph_inode_info *ci = ceph_inode(inode);
0236 int err = 0;
0237 u16 op = CEPH_MDS_OP_SETFILELOCK;
0238 u8 wait = 0;
0239 u8 lock_cmd;
0240
0241 if (!(fl->fl_flags & FL_POSIX))
0242 return -ENOLCK;
0243
0244 if (ceph_inode_is_shutdown(inode))
0245 return -ESTALE;
0246
0247 dout("ceph_lock, fl_owner: %p\n", fl->fl_owner);
0248
0249
0250 if (IS_GETLK(cmd))
0251 op = CEPH_MDS_OP_GETFILELOCK;
0252 else if (IS_SETLKW(cmd))
0253 wait = 1;
0254
0255 spin_lock(&ci->i_ceph_lock);
0256 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
0257 err = -EIO;
0258 }
0259 spin_unlock(&ci->i_ceph_lock);
0260 if (err < 0) {
0261 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type)
0262 posix_lock_file(file, fl, NULL);
0263 return err;
0264 }
0265
0266 if (F_RDLCK == fl->fl_type)
0267 lock_cmd = CEPH_LOCK_SHARED;
0268 else if (F_WRLCK == fl->fl_type)
0269 lock_cmd = CEPH_LOCK_EXCL;
0270 else
0271 lock_cmd = CEPH_LOCK_UNLOCK;
0272
0273 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
0274 err = try_unlock_file(file, fl);
0275 if (err <= 0)
0276 return err;
0277 }
0278
0279 err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
0280 if (!err) {
0281 if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
0282 dout("mds locked, locking locally\n");
0283 err = posix_lock_file(file, fl, NULL);
0284 if (err) {
0285
0286
0287
0288 ceph_lock_message(CEPH_LOCK_FCNTL, op, inode,
0289 CEPH_LOCK_UNLOCK, 0, fl);
0290 dout("got %d on posix_lock_file, undid lock\n",
0291 err);
0292 }
0293 }
0294 }
0295 return err;
0296 }
0297
0298 int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
0299 {
0300 struct inode *inode = file_inode(file);
0301 struct ceph_inode_info *ci = ceph_inode(inode);
0302 int err = 0;
0303 u8 wait = 0;
0304 u8 lock_cmd;
0305
0306 if (!(fl->fl_flags & FL_FLOCK))
0307 return -ENOLCK;
0308
0309 if (ceph_inode_is_shutdown(inode))
0310 return -ESTALE;
0311
0312 dout("ceph_flock, fl_file: %p\n", fl->fl_file);
0313
0314 spin_lock(&ci->i_ceph_lock);
0315 if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
0316 err = -EIO;
0317 }
0318 spin_unlock(&ci->i_ceph_lock);
0319 if (err < 0) {
0320 if (F_UNLCK == fl->fl_type)
0321 locks_lock_file_wait(file, fl);
0322 return err;
0323 }
0324
0325 if (IS_SETLKW(cmd))
0326 wait = 1;
0327
0328 if (F_RDLCK == fl->fl_type)
0329 lock_cmd = CEPH_LOCK_SHARED;
0330 else if (F_WRLCK == fl->fl_type)
0331 lock_cmd = CEPH_LOCK_EXCL;
0332 else
0333 lock_cmd = CEPH_LOCK_UNLOCK;
0334
0335 if (F_UNLCK == fl->fl_type) {
0336 err = try_unlock_file(file, fl);
0337 if (err <= 0)
0338 return err;
0339 }
0340
0341 err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
0342 inode, lock_cmd, wait, fl);
0343 if (!err && F_UNLCK != fl->fl_type) {
0344 err = locks_lock_file_wait(file, fl);
0345 if (err) {
0346 ceph_lock_message(CEPH_LOCK_FLOCK,
0347 CEPH_MDS_OP_SETFILELOCK,
0348 inode, CEPH_LOCK_UNLOCK, 0, fl);
0349 dout("got %d on locks_lock_file_wait, undid lock\n", err);
0350 }
0351 }
0352 return err;
0353 }
0354
0355
0356
0357
0358
0359 void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
0360 {
0361 struct file_lock *lock;
0362 struct file_lock_context *ctx;
0363
0364 *fcntl_count = 0;
0365 *flock_count = 0;
0366
0367 ctx = inode->i_flctx;
0368 if (ctx) {
0369 spin_lock(&ctx->flc_lock);
0370 list_for_each_entry(lock, &ctx->flc_posix, fl_list)
0371 ++(*fcntl_count);
0372 list_for_each_entry(lock, &ctx->flc_flock, fl_list)
0373 ++(*flock_count);
0374 spin_unlock(&ctx->flc_lock);
0375 }
0376 dout("counted %d flock locks and %d fcntl locks\n",
0377 *flock_count, *fcntl_count);
0378 }
0379
0380
0381
0382
0383 static int lock_to_ceph_filelock(struct file_lock *lock,
0384 struct ceph_filelock *cephlock)
0385 {
0386 int err = 0;
0387 cephlock->start = cpu_to_le64(lock->fl_start);
0388 cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
0389 cephlock->client = cpu_to_le64(0);
0390 cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
0391 cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
0392
0393 switch (lock->fl_type) {
0394 case F_RDLCK:
0395 cephlock->type = CEPH_LOCK_SHARED;
0396 break;
0397 case F_WRLCK:
0398 cephlock->type = CEPH_LOCK_EXCL;
0399 break;
0400 case F_UNLCK:
0401 cephlock->type = CEPH_LOCK_UNLOCK;
0402 break;
0403 default:
0404 dout("Have unknown lock type %d\n", lock->fl_type);
0405 err = -EINVAL;
0406 }
0407
0408 return err;
0409 }
0410
0411
0412
0413
0414
0415
0416 int ceph_encode_locks_to_buffer(struct inode *inode,
0417 struct ceph_filelock *flocks,
0418 int num_fcntl_locks, int num_flock_locks)
0419 {
0420 struct file_lock *lock;
0421 struct file_lock_context *ctx = inode->i_flctx;
0422 int err = 0;
0423 int seen_fcntl = 0;
0424 int seen_flock = 0;
0425 int l = 0;
0426
0427 dout("encoding %d flock and %d fcntl locks\n", num_flock_locks,
0428 num_fcntl_locks);
0429
0430 if (!ctx)
0431 return 0;
0432
0433 spin_lock(&ctx->flc_lock);
0434 list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
0435 ++seen_fcntl;
0436 if (seen_fcntl > num_fcntl_locks) {
0437 err = -ENOSPC;
0438 goto fail;
0439 }
0440 err = lock_to_ceph_filelock(lock, &flocks[l]);
0441 if (err)
0442 goto fail;
0443 ++l;
0444 }
0445 list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
0446 ++seen_flock;
0447 if (seen_flock > num_flock_locks) {
0448 err = -ENOSPC;
0449 goto fail;
0450 }
0451 err = lock_to_ceph_filelock(lock, &flocks[l]);
0452 if (err)
0453 goto fail;
0454 ++l;
0455 }
0456 fail:
0457 spin_unlock(&ctx->flc_lock);
0458 return err;
0459 }
0460
0461
0462
0463
0464
0465
0466
0467 int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
0468 struct ceph_pagelist *pagelist,
0469 int num_fcntl_locks, int num_flock_locks)
0470 {
0471 int err = 0;
0472 __le32 nlocks;
0473
0474 nlocks = cpu_to_le32(num_fcntl_locks);
0475 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
0476 if (err)
0477 goto out_fail;
0478
0479 if (num_fcntl_locks > 0) {
0480 err = ceph_pagelist_append(pagelist, flocks,
0481 num_fcntl_locks * sizeof(*flocks));
0482 if (err)
0483 goto out_fail;
0484 }
0485
0486 nlocks = cpu_to_le32(num_flock_locks);
0487 err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
0488 if (err)
0489 goto out_fail;
0490
0491 if (num_flock_locks > 0) {
0492 err = ceph_pagelist_append(pagelist, &flocks[num_fcntl_locks],
0493 num_flock_locks * sizeof(*flocks));
0494 }
0495 out_fail:
0496 return err;
0497 }