xref: /openbmc/linux/fs/ceph/locks.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
340819f6fSGreg Farnum 
440819f6fSGreg Farnum #include <linux/file.h>
540819f6fSGreg Farnum #include <linux/namei.h>
6eb13e832SYan, Zheng #include <linux/random.h>
740819f6fSGreg Farnum 
840819f6fSGreg Farnum #include "super.h"
940819f6fSGreg Farnum #include "mds_client.h"
10*5970e15dSJeff Layton #include <linux/filelock.h>
113d14c5d2SYehuda Sadeh #include <linux/ceph/pagelist.h>
1240819f6fSGreg Farnum 
13eb13e832SYan, Zheng static u64 lock_secret;
149280be24SYan, Zheng static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
159280be24SYan, Zheng                                          struct ceph_mds_request *req);
16eb13e832SYan, Zheng 
secure_addr(void * addr)17eb13e832SYan, Zheng static inline u64 secure_addr(void *addr)
18eb13e832SYan, Zheng {
19eb13e832SYan, Zheng 	u64 v = lock_secret ^ (u64)(unsigned long)addr;
20eb13e832SYan, Zheng 	/*
21eb13e832SYan, Zheng 	 * Set the most significant bit, so that MDS knows the 'owner'
22eb13e832SYan, Zheng 	 * is sufficient to identify the owner of lock. (old code uses
23eb13e832SYan, Zheng 	 * both 'owner' and 'pid')
24eb13e832SYan, Zheng 	 */
25eb13e832SYan, Zheng 	v |= (1ULL << 63);
26eb13e832SYan, Zheng 	return v;
27eb13e832SYan, Zheng }
28eb13e832SYan, Zheng 
ceph_flock_init(void)29eb13e832SYan, Zheng void __init ceph_flock_init(void)
30eb13e832SYan, Zheng {
31eb13e832SYan, Zheng 	get_random_bytes(&lock_secret, sizeof(lock_secret));
32eb13e832SYan, Zheng }
33eb13e832SYan, Zheng 
ceph_fl_copy_lock(struct file_lock * dst,struct file_lock * src)3489aa5930SYan, Zheng static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
3589aa5930SYan, Zheng {
36ff5d913dSYan, Zheng 	struct inode *inode = file_inode(dst->fl_file);
3789aa5930SYan, Zheng 	atomic_inc(&ceph_inode(inode)->i_filelock_ref);
388e185871SXiubo Li 	dst->fl_u.ceph.inode = igrab(inode);
3989aa5930SYan, Zheng }
4089aa5930SYan, Zheng 
418e185871SXiubo Li /*
428e185871SXiubo Li  * Do not use the 'fl->fl_file' in release function, which
438e185871SXiubo Li  * is possibly already released by another thread.
448e185871SXiubo Li  */
ceph_fl_release_lock(struct file_lock * fl)4589aa5930SYan, Zheng static void ceph_fl_release_lock(struct file_lock *fl)
4689aa5930SYan, Zheng {
478e185871SXiubo Li 	struct inode *inode = fl->fl_u.ceph.inode;
488e185871SXiubo Li 	struct ceph_inode_info *ci;
498e185871SXiubo Li 
508e185871SXiubo Li 	/*
518e185871SXiubo Li 	 * If inode is NULL it should be a request file_lock,
528e185871SXiubo Li 	 * nothing we can do.
538e185871SXiubo Li 	 */
548e185871SXiubo Li 	if (!inode)
558e185871SXiubo Li 		return;
568e185871SXiubo Li 
578e185871SXiubo Li 	ci = ceph_inode(inode);
58b3f8d68fSYan, Zheng 	if (atomic_dec_and_test(&ci->i_filelock_ref)) {
59b3f8d68fSYan, Zheng 		/* clear error when all locks are released */
60b3f8d68fSYan, Zheng 		spin_lock(&ci->i_ceph_lock);
61b3f8d68fSYan, Zheng 		ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
62b3f8d68fSYan, Zheng 		spin_unlock(&ci->i_ceph_lock);
63b3f8d68fSYan, Zheng 	}
648e185871SXiubo Li 	fl->fl_u.ceph.inode = NULL;
658e185871SXiubo Li 	iput(inode);
6689aa5930SYan, Zheng }
6789aa5930SYan, Zheng 
6889aa5930SYan, Zheng static const struct file_lock_operations ceph_fl_lock_ops = {
6989aa5930SYan, Zheng 	.fl_copy_lock = ceph_fl_copy_lock,
7089aa5930SYan, Zheng 	.fl_release_private = ceph_fl_release_lock,
7189aa5930SYan, Zheng };
7289aa5930SYan, Zheng 
7306a1ad43SJeff Layton /*
7440819f6fSGreg Farnum  * Implement fcntl and flock locking functions.
7540819f6fSGreg Farnum  */
ceph_lock_message(u8 lock_type,u16 operation,struct inode * inode,int cmd,u8 wait,struct file_lock * fl)7689aa5930SYan, Zheng static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
77637ae8d5SHerb Shiu 			     int cmd, u8 wait, struct file_lock *fl)
7840819f6fSGreg Farnum {
792678da88SXiubo Li 	struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
8040819f6fSGreg Farnum 	struct ceph_mds_request *req;
8140819f6fSGreg Farnum 	int err;
82637ae8d5SHerb Shiu 	u64 length = 0;
83eb13e832SYan, Zheng 	u64 owner;
8440819f6fSGreg Farnum 
8589aa5930SYan, Zheng 	if (operation == CEPH_MDS_OP_SETFILELOCK) {
8689aa5930SYan, Zheng 		/*
8789aa5930SYan, Zheng 		 * increasing i_filelock_ref closes race window between
8889aa5930SYan, Zheng 		 * handling request reply and adding file_lock struct to
8989aa5930SYan, Zheng 		 * inode. Otherwise, auth caps may get trimmed in the
9089aa5930SYan, Zheng 		 * window. Caller function will decrease the counter.
9189aa5930SYan, Zheng 		 */
9289aa5930SYan, Zheng 		fl->fl_ops = &ceph_fl_lock_ops;
93ff5d913dSYan, Zheng 		fl->fl_ops->fl_copy_lock(fl, NULL);
9489aa5930SYan, Zheng 	}
9589aa5930SYan, Zheng 
969280be24SYan, Zheng 	if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
979280be24SYan, Zheng 		wait = 0;
989280be24SYan, Zheng 
9940819f6fSGreg Farnum 	req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
10040819f6fSGreg Farnum 	if (IS_ERR(req))
10140819f6fSGreg Farnum 		return PTR_ERR(req);
10270b666c3SSage Weil 	req->r_inode = inode;
10370b666c3SSage Weil 	ihold(inode);
1043bd58143SYan, Zheng 	req->r_num_caps = 1;
10540819f6fSGreg Farnum 
106637ae8d5SHerb Shiu 	/* mds requires start and length rather than start and end */
107637ae8d5SHerb Shiu 	if (LLONG_MAX == fl->fl_end)
108637ae8d5SHerb Shiu 		length = 0;
109637ae8d5SHerb Shiu 	else
110637ae8d5SHerb Shiu 		length = fl->fl_end - fl->fl_start + 1;
111637ae8d5SHerb Shiu 
112eb13e832SYan, Zheng 	owner = secure_addr(fl->fl_owner);
113eb13e832SYan, Zheng 
114eb13e832SYan, Zheng 	dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
1154c069a58SChengguang Xu 	     "start: %llu, length: %llu, wait: %d, type: %d\n", (int)lock_type,
116eb13e832SYan, Zheng 	     (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
117eb13e832SYan, Zheng 	     wait, fl->fl_type);
118637ae8d5SHerb Shiu 
11940819f6fSGreg Farnum 	req->r_args.filelock_change.rule = lock_type;
12040819f6fSGreg Farnum 	req->r_args.filelock_change.type = cmd;
121eb13e832SYan, Zheng 	req->r_args.filelock_change.owner = cpu_to_le64(owner);
122637ae8d5SHerb Shiu 	req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
123637ae8d5SHerb Shiu 	req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
12440819f6fSGreg Farnum 	req->r_args.filelock_change.length = cpu_to_le64(length);
12540819f6fSGreg Farnum 	req->r_args.filelock_change.wait = wait;
12640819f6fSGreg Farnum 
1279eaa7b79SJeff Layton 	err = ceph_mdsc_submit_request(mdsc, inode, req);
1289eaa7b79SJeff Layton 	if (!err)
1299eaa7b79SJeff Layton 		err = ceph_mdsc_wait_request(mdsc, req, wait ?
1309eaa7b79SJeff Layton 					ceph_lock_wait_for_completion : NULL);
13128a28261SJeff Layton 	if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
1329d5b86acSBenjamin Coddington 		fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
133a5b10629SHerb Shiu 		if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
134a5b10629SHerb Shiu 			fl->fl_type = F_RDLCK;
135a5b10629SHerb Shiu 		else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
136a5b10629SHerb Shiu 			fl->fl_type = F_WRLCK;
137a5b10629SHerb Shiu 		else
138a5b10629SHerb Shiu 			fl->fl_type = F_UNLCK;
139a5b10629SHerb Shiu 
140a5b10629SHerb Shiu 		fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
141a5b10629SHerb Shiu 		length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
142a5b10629SHerb Shiu 						 le64_to_cpu(req->r_reply_info.filelock_reply->length);
143a5b10629SHerb Shiu 		if (length >= 1)
144a5b10629SHerb Shiu 			fl->fl_end = length -1;
145a5b10629SHerb Shiu 		else
146a5b10629SHerb Shiu 			fl->fl_end = 0;
147a5b10629SHerb Shiu 
148a5b10629SHerb Shiu 	}
14940819f6fSGreg Farnum 	ceph_mdsc_put_request(req);
15040819f6fSGreg Farnum 	dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
1514c069a58SChengguang Xu 	     "length: %llu, wait: %d, type: %d, err code %d\n", (int)lock_type,
152637ae8d5SHerb Shiu 	     (int)operation, (u64)fl->fl_pid, fl->fl_start,
153637ae8d5SHerb Shiu 	     length, wait, fl->fl_type, err);
15440819f6fSGreg Farnum 	return err;
15540819f6fSGreg Farnum }
15640819f6fSGreg Farnum 
ceph_lock_wait_for_completion(struct ceph_mds_client * mdsc,struct ceph_mds_request * req)1579280be24SYan, Zheng static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
1589280be24SYan, Zheng                                          struct ceph_mds_request *req)
1599280be24SYan, Zheng {
1609280be24SYan, Zheng 	struct ceph_mds_request *intr_req;
1619280be24SYan, Zheng 	struct inode *inode = req->r_inode;
1629280be24SYan, Zheng 	int err, lock_type;
1639280be24SYan, Zheng 
1649280be24SYan, Zheng 	BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
1659280be24SYan, Zheng 	if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
1669280be24SYan, Zheng 		lock_type = CEPH_LOCK_FCNTL_INTR;
1679280be24SYan, Zheng 	else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
1689280be24SYan, Zheng 		lock_type = CEPH_LOCK_FLOCK_INTR;
1699280be24SYan, Zheng 	else
1709280be24SYan, Zheng 		BUG_ON(1);
1719280be24SYan, Zheng 	BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
1729280be24SYan, Zheng 
1739280be24SYan, Zheng 	err = wait_for_completion_interruptible(&req->r_completion);
1749280be24SYan, Zheng 	if (!err)
1759280be24SYan, Zheng 		return 0;
1769280be24SYan, Zheng 
1779280be24SYan, Zheng 	dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
1789280be24SYan, Zheng 	     req->r_tid);
1799280be24SYan, Zheng 
18092e57e62SYan, Zheng 	mutex_lock(&mdsc->mutex);
18192e57e62SYan, Zheng 	if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
18292e57e62SYan, Zheng 		err = 0;
18392e57e62SYan, Zheng 	} else {
18492e57e62SYan, Zheng 		/*
18592e57e62SYan, Zheng 		 * ensure we aren't running concurrently with
18692e57e62SYan, Zheng 		 * ceph_fill_trace or ceph_readdir_prepopulate, which
18792e57e62SYan, Zheng 		 * rely on locks (dir mutex) held by our caller.
18892e57e62SYan, Zheng 		 */
18992e57e62SYan, Zheng 		mutex_lock(&req->r_fill_mutex);
19092e57e62SYan, Zheng 		req->r_err = err;
19192e57e62SYan, Zheng 		set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
19292e57e62SYan, Zheng 		mutex_unlock(&req->r_fill_mutex);
19392e57e62SYan, Zheng 
19492e57e62SYan, Zheng 		if (!req->r_session) {
19592e57e62SYan, Zheng 			// haven't sent the request
19692e57e62SYan, Zheng 			err = 0;
19792e57e62SYan, Zheng 		}
19892e57e62SYan, Zheng 	}
19992e57e62SYan, Zheng 	mutex_unlock(&mdsc->mutex);
20092e57e62SYan, Zheng 	if (!err)
20192e57e62SYan, Zheng 		return 0;
20292e57e62SYan, Zheng 
2039280be24SYan, Zheng 	intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
2049280be24SYan, Zheng 					    USE_AUTH_MDS);
2059280be24SYan, Zheng 	if (IS_ERR(intr_req))
2069280be24SYan, Zheng 		return PTR_ERR(intr_req);
2079280be24SYan, Zheng 
2089280be24SYan, Zheng 	intr_req->r_inode = inode;
2099280be24SYan, Zheng 	ihold(inode);
2109280be24SYan, Zheng 	intr_req->r_num_caps = 1;
2119280be24SYan, Zheng 
2129280be24SYan, Zheng 	intr_req->r_args.filelock_change = req->r_args.filelock_change;
2139280be24SYan, Zheng 	intr_req->r_args.filelock_change.rule = lock_type;
2149280be24SYan, Zheng 	intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
2159280be24SYan, Zheng 
2169280be24SYan, Zheng 	err = ceph_mdsc_do_request(mdsc, inode, intr_req);
2179280be24SYan, Zheng 	ceph_mdsc_put_request(intr_req);
2189280be24SYan, Zheng 
2199280be24SYan, Zheng 	if (err && err != -ERESTARTSYS)
2209280be24SYan, Zheng 		return err;
2219280be24SYan, Zheng 
22292e57e62SYan, Zheng 	wait_for_completion_killable(&req->r_safe_completion);
2239280be24SYan, Zheng 	return 0;
2249280be24SYan, Zheng }
2259280be24SYan, Zheng 
try_unlock_file(struct file * file,struct file_lock * fl)226bbb480abSYan, Zheng static int try_unlock_file(struct file *file, struct file_lock *fl)
227bbb480abSYan, Zheng {
228bbb480abSYan, Zheng 	int err;
229bbb480abSYan, Zheng 	unsigned int orig_flags = fl->fl_flags;
230bbb480abSYan, Zheng 	fl->fl_flags |= FL_EXISTS;
231bbb480abSYan, Zheng 	err = locks_lock_file_wait(file, fl);
232bbb480abSYan, Zheng 	fl->fl_flags = orig_flags;
233bbb480abSYan, Zheng 	if (err == -ENOENT) {
234bbb480abSYan, Zheng 		if (!(orig_flags & FL_EXISTS))
235bbb480abSYan, Zheng 			err = 0;
236bbb480abSYan, Zheng 		return err;
237bbb480abSYan, Zheng 	}
238bbb480abSYan, Zheng 	return 1;
239bbb480abSYan, Zheng }
240bbb480abSYan, Zheng 
24106a1ad43SJeff Layton /*
24240819f6fSGreg Farnum  * Attempt to set an fcntl lock.
24340819f6fSGreg Farnum  * For now, this just goes away to the server. Later it may be more awesome.
24440819f6fSGreg Farnum  */
ceph_lock(struct file * file,int cmd,struct file_lock * fl)24540819f6fSGreg Farnum int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
24640819f6fSGreg Farnum {
24789aa5930SYan, Zheng 	struct inode *inode = file_inode(file);
248b3f8d68fSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
249b3f8d68fSYan, Zheng 	int err = 0;
25040819f6fSGreg Farnum 	u16 op = CEPH_MDS_OP_SETFILELOCK;
25189aa5930SYan, Zheng 	u8 wait = 0;
252b3f8d68fSYan, Zheng 	u8 lock_cmd;
25340819f6fSGreg Farnum 
254eb70c0ceSYan, Zheng 	if (!(fl->fl_flags & FL_POSIX))
255eb70c0ceSYan, Zheng 		return -ENOLCK;
256eb70c0ceSYan, Zheng 
2575d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
2585d6451b1SJeff Layton 		return -ESTALE;
2595d6451b1SJeff Layton 
2604c069a58SChengguang Xu 	dout("ceph_lock, fl_owner: %p\n", fl->fl_owner);
26140819f6fSGreg Farnum 
26240819f6fSGreg Farnum 	/* set wait bit as appropriate, then make command as Ceph expects it*/
2630e8e95d6SYan, Zheng 	if (IS_GETLK(cmd))
26440819f6fSGreg Farnum 		op = CEPH_MDS_OP_GETFILELOCK;
2650e8e95d6SYan, Zheng 	else if (IS_SETLKW(cmd))
2660e8e95d6SYan, Zheng 		wait = 1;
26740819f6fSGreg Farnum 
268b3f8d68fSYan, Zheng 	spin_lock(&ci->i_ceph_lock);
269b3f8d68fSYan, Zheng 	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
270b3f8d68fSYan, Zheng 		err = -EIO;
271b3f8d68fSYan, Zheng 	}
272b3f8d68fSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
273b3f8d68fSYan, Zheng 	if (err < 0) {
274b3f8d68fSYan, Zheng 		if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type)
275b3f8d68fSYan, Zheng 			posix_lock_file(file, fl, NULL);
276b3f8d68fSYan, Zheng 		return err;
27789aa5930SYan, Zheng 	}
27889aa5930SYan, Zheng 
27940819f6fSGreg Farnum 	if (F_RDLCK == fl->fl_type)
28040819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_SHARED;
28140819f6fSGreg Farnum 	else if (F_WRLCK == fl->fl_type)
28240819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_EXCL;
28340819f6fSGreg Farnum 	else
28440819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_UNLOCK;
28540819f6fSGreg Farnum 
286bbb480abSYan, Zheng 	if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
287bbb480abSYan, Zheng 		err = try_unlock_file(file, fl);
288bbb480abSYan, Zheng 		if (err <= 0)
289bbb480abSYan, Zheng 			return err;
290bbb480abSYan, Zheng 	}
291bbb480abSYan, Zheng 
29289aa5930SYan, Zheng 	err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
29340819f6fSGreg Farnum 	if (!err) {
294bbb480abSYan, Zheng 		if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
2954c069a58SChengguang Xu 			dout("mds locked, locking locally\n");
29640819f6fSGreg Farnum 			err = posix_lock_file(file, fl, NULL);
297b3f8d68fSYan, Zheng 			if (err) {
2980c1f91f2SSage Weil 				/* undo! This should only happen if
2990c1f91f2SSage Weil 				 * the kernel detects local
3000c1f91f2SSage Weil 				 * deadlock. */
30189aa5930SYan, Zheng 				ceph_lock_message(CEPH_LOCK_FCNTL, op, inode,
302637ae8d5SHerb Shiu 						  CEPH_LOCK_UNLOCK, 0, fl);
3034c069a58SChengguang Xu 				dout("got %d on posix_lock_file, undid lock\n",
3040c1f91f2SSage Weil 				     err);
30540819f6fSGreg Farnum 			}
306a5b10629SHerb Shiu 		}
30740819f6fSGreg Farnum 	}
30840819f6fSGreg Farnum 	return err;
30940819f6fSGreg Farnum }
31040819f6fSGreg Farnum 
ceph_flock(struct file * file,int cmd,struct file_lock * fl)31140819f6fSGreg Farnum int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
31240819f6fSGreg Farnum {
31389aa5930SYan, Zheng 	struct inode *inode = file_inode(file);
314b3f8d68fSYan, Zheng 	struct ceph_inode_info *ci = ceph_inode(inode);
315b3f8d68fSYan, Zheng 	int err = 0;
3160e8e95d6SYan, Zheng 	u8 wait = 0;
317b3f8d68fSYan, Zheng 	u8 lock_cmd;
31840819f6fSGreg Farnum 
319eb70c0ceSYan, Zheng 	if (!(fl->fl_flags & FL_FLOCK))
320eb70c0ceSYan, Zheng 		return -ENOLCK;
321eb70c0ceSYan, Zheng 
3225d6451b1SJeff Layton 	if (ceph_inode_is_shutdown(inode))
3235d6451b1SJeff Layton 		return -ESTALE;
3245d6451b1SJeff Layton 
3254c069a58SChengguang Xu 	dout("ceph_flock, fl_file: %p\n", fl->fl_file);
32640819f6fSGreg Farnum 
327b3f8d68fSYan, Zheng 	spin_lock(&ci->i_ceph_lock);
328b3f8d68fSYan, Zheng 	if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
329b3f8d68fSYan, Zheng 		err = -EIO;
330b3f8d68fSYan, Zheng 	}
331b3f8d68fSYan, Zheng 	spin_unlock(&ci->i_ceph_lock);
332b3f8d68fSYan, Zheng 	if (err < 0) {
333b3f8d68fSYan, Zheng 		if (F_UNLCK == fl->fl_type)
334b3f8d68fSYan, Zheng 			locks_lock_file_wait(file, fl);
335b3f8d68fSYan, Zheng 		return err;
336b3f8d68fSYan, Zheng 	}
33789aa5930SYan, Zheng 
3380e8e95d6SYan, Zheng 	if (IS_SETLKW(cmd))
3390e8e95d6SYan, Zheng 		wait = 1;
3400e8e95d6SYan, Zheng 
3410e8e95d6SYan, Zheng 	if (F_RDLCK == fl->fl_type)
34240819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_SHARED;
3430e8e95d6SYan, Zheng 	else if (F_WRLCK == fl->fl_type)
34440819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_EXCL;
34540819f6fSGreg Farnum 	else
34640819f6fSGreg Farnum 		lock_cmd = CEPH_LOCK_UNLOCK;
34740819f6fSGreg Farnum 
348bbb480abSYan, Zheng 	if (F_UNLCK == fl->fl_type) {
349bbb480abSYan, Zheng 		err = try_unlock_file(file, fl);
350bbb480abSYan, Zheng 		if (err <= 0)
351bbb480abSYan, Zheng 			return err;
352bbb480abSYan, Zheng 	}
353bbb480abSYan, Zheng 
35440819f6fSGreg Farnum 	err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
35589aa5930SYan, Zheng 				inode, lock_cmd, wait, fl);
356bbb480abSYan, Zheng 	if (!err && F_UNLCK != fl->fl_type) {
3574f656367SBenjamin Coddington 		err = locks_lock_file_wait(file, fl);
35840819f6fSGreg Farnum 		if (err) {
35940819f6fSGreg Farnum 			ceph_lock_message(CEPH_LOCK_FLOCK,
36040819f6fSGreg Farnum 					  CEPH_MDS_OP_SETFILELOCK,
36189aa5930SYan, Zheng 					  inode, CEPH_LOCK_UNLOCK, 0, fl);
3624c069a58SChengguang Xu 			dout("got %d on locks_lock_file_wait, undid lock\n", err);
36340819f6fSGreg Farnum 		}
36440819f6fSGreg Farnum 	}
36540819f6fSGreg Farnum 	return err;
36640819f6fSGreg Farnum }
36740819f6fSGreg Farnum 
3685263e31eSJeff Layton /*
3695263e31eSJeff Layton  * Fills in the passed counter variables, so you can prepare pagelist metadata
3705263e31eSJeff Layton  * before calling ceph_encode_locks.
37140819f6fSGreg Farnum  */
ceph_count_locks(struct inode * inode,int * fcntl_count,int * flock_count)37240819f6fSGreg Farnum void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
37340819f6fSGreg Farnum {
374e084c1bdSJeff Layton 	struct file_lock *lock;
3755263e31eSJeff Layton 	struct file_lock_context *ctx;
37640819f6fSGreg Farnum 
37740819f6fSGreg Farnum 	*fcntl_count = 0;
37840819f6fSGreg Farnum 	*flock_count = 0;
37940819f6fSGreg Farnum 
380d4e78663SJeff Layton 	ctx = locks_inode_context(inode);
3815263e31eSJeff Layton 	if (ctx) {
382e084c1bdSJeff Layton 		spin_lock(&ctx->flc_lock);
383e084c1bdSJeff Layton 		list_for_each_entry(lock, &ctx->flc_posix, fl_list)
384e084c1bdSJeff Layton 			++(*fcntl_count);
385e084c1bdSJeff Layton 		list_for_each_entry(lock, &ctx->flc_flock, fl_list)
386e084c1bdSJeff Layton 			++(*flock_count);
387e084c1bdSJeff Layton 		spin_unlock(&ctx->flc_lock);
388bd61e0a9SJeff Layton 	}
3894c069a58SChengguang Xu 	dout("counted %d flock locks and %d fcntl locks\n",
39040819f6fSGreg Farnum 	     *flock_count, *fcntl_count);
39140819f6fSGreg Farnum }
39240819f6fSGreg Farnum 
393c6db8472SYan, Zheng /*
394c6db8472SYan, Zheng  * Given a pointer to a lock, convert it to a ceph filelock
395c6db8472SYan, Zheng  */
lock_to_ceph_filelock(struct file_lock * lock,struct ceph_filelock * cephlock)396c6db8472SYan, Zheng static int lock_to_ceph_filelock(struct file_lock *lock,
397c6db8472SYan, Zheng 				 struct ceph_filelock *cephlock)
398c6db8472SYan, Zheng {
399c6db8472SYan, Zheng 	int err = 0;
400c6db8472SYan, Zheng 	cephlock->start = cpu_to_le64(lock->fl_start);
401c6db8472SYan, Zheng 	cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
402c6db8472SYan, Zheng 	cephlock->client = cpu_to_le64(0);
403c6db8472SYan, Zheng 	cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
404c6db8472SYan, Zheng 	cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
405c6db8472SYan, Zheng 
406c6db8472SYan, Zheng 	switch (lock->fl_type) {
407c6db8472SYan, Zheng 	case F_RDLCK:
408c6db8472SYan, Zheng 		cephlock->type = CEPH_LOCK_SHARED;
409c6db8472SYan, Zheng 		break;
410c6db8472SYan, Zheng 	case F_WRLCK:
411c6db8472SYan, Zheng 		cephlock->type = CEPH_LOCK_EXCL;
412c6db8472SYan, Zheng 		break;
413c6db8472SYan, Zheng 	case F_UNLCK:
414c6db8472SYan, Zheng 		cephlock->type = CEPH_LOCK_UNLOCK;
415c6db8472SYan, Zheng 		break;
416c6db8472SYan, Zheng 	default:
4174c069a58SChengguang Xu 		dout("Have unknown lock type %d\n", lock->fl_type);
418c6db8472SYan, Zheng 		err = -EINVAL;
419c6db8472SYan, Zheng 	}
420c6db8472SYan, Zheng 
421c6db8472SYan, Zheng 	return err;
422c6db8472SYan, Zheng }
423c6db8472SYan, Zheng 
42406a1ad43SJeff Layton /*
42539be95e9SJim Schutt  * Encode the flock and fcntl locks for the given inode into the ceph_filelock
4261c8c601aSJeff Layton  * array. Must be called with inode->i_lock already held.
42739be95e9SJim Schutt  * If we encounter more of a specific lock type than expected, return -ENOSPC.
42840819f6fSGreg Farnum  */
ceph_encode_locks_to_buffer(struct inode * inode,struct ceph_filelock * flocks,int num_fcntl_locks,int num_flock_locks)42939be95e9SJim Schutt int ceph_encode_locks_to_buffer(struct inode *inode,
43039be95e9SJim Schutt 				struct ceph_filelock *flocks,
43140819f6fSGreg Farnum 				int num_fcntl_locks, int num_flock_locks)
43240819f6fSGreg Farnum {
43340819f6fSGreg Farnum 	struct file_lock *lock;
434d4e78663SJeff Layton 	struct file_lock_context *ctx = locks_inode_context(inode);
43540819f6fSGreg Farnum 	int err = 0;
436fca4451aSGreg Farnum 	int seen_fcntl = 0;
437fca4451aSGreg Farnum 	int seen_flock = 0;
43839be95e9SJim Schutt 	int l = 0;
43940819f6fSGreg Farnum 
4404c069a58SChengguang Xu 	dout("encoding %d flock and %d fcntl locks\n", num_flock_locks,
44140819f6fSGreg Farnum 	     num_fcntl_locks);
44239be95e9SJim Schutt 
443bd61e0a9SJeff Layton 	if (!ctx)
444bd61e0a9SJeff Layton 		return 0;
445bd61e0a9SJeff Layton 
4466109c850SJeff Layton 	spin_lock(&ctx->flc_lock);
447f6762cb2SYan, Zheng 	list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
448fca4451aSGreg Farnum 		++seen_fcntl;
449fca4451aSGreg Farnum 		if (seen_fcntl > num_fcntl_locks) {
450fca4451aSGreg Farnum 			err = -ENOSPC;
451fca4451aSGreg Farnum 			goto fail;
452fca4451aSGreg Farnum 		}
45339be95e9SJim Schutt 		err = lock_to_ceph_filelock(lock, &flocks[l]);
45440819f6fSGreg Farnum 		if (err)
45540819f6fSGreg Farnum 			goto fail;
45639be95e9SJim Schutt 		++l;
45740819f6fSGreg Farnum 	}
4585263e31eSJeff Layton 	list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
459fca4451aSGreg Farnum 		++seen_flock;
460fca4451aSGreg Farnum 		if (seen_flock > num_flock_locks) {
461fca4451aSGreg Farnum 			err = -ENOSPC;
462fca4451aSGreg Farnum 			goto fail;
463fca4451aSGreg Farnum 		}
46439be95e9SJim Schutt 		err = lock_to_ceph_filelock(lock, &flocks[l]);
46540819f6fSGreg Farnum 		if (err)
46640819f6fSGreg Farnum 			goto fail;
46739be95e9SJim Schutt 		++l;
46840819f6fSGreg Farnum 	}
46940819f6fSGreg Farnum fail:
4706109c850SJeff Layton 	spin_unlock(&ctx->flc_lock);
47140819f6fSGreg Farnum 	return err;
47240819f6fSGreg Farnum }
47340819f6fSGreg Farnum 
47406a1ad43SJeff Layton /*
47539be95e9SJim Schutt  * Copy the encoded flock and fcntl locks into the pagelist.
47639be95e9SJim Schutt  * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
47739be95e9SJim Schutt  * sequential flock locks.
47839be95e9SJim Schutt  * Returns zero on success.
47939be95e9SJim Schutt  */
ceph_locks_to_pagelist(struct ceph_filelock * flocks,struct ceph_pagelist * pagelist,int num_fcntl_locks,int num_flock_locks)48039be95e9SJim Schutt int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
48139be95e9SJim Schutt 			   struct ceph_pagelist *pagelist,
48239be95e9SJim Schutt 			   int num_fcntl_locks, int num_flock_locks)
48339be95e9SJim Schutt {
48439be95e9SJim Schutt 	int err = 0;
48539be95e9SJim Schutt 	__le32 nlocks;
48639be95e9SJim Schutt 
48739be95e9SJim Schutt 	nlocks = cpu_to_le32(num_fcntl_locks);
48839be95e9SJim Schutt 	err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
48939be95e9SJim Schutt 	if (err)
49039be95e9SJim Schutt 		goto out_fail;
49139be95e9SJim Schutt 
4924deb14a2SYan, Zheng 	if (num_fcntl_locks > 0) {
49339be95e9SJim Schutt 		err = ceph_pagelist_append(pagelist, flocks,
49439be95e9SJim Schutt 					   num_fcntl_locks * sizeof(*flocks));
49539be95e9SJim Schutt 		if (err)
49639be95e9SJim Schutt 			goto out_fail;
4974deb14a2SYan, Zheng 	}
49839be95e9SJim Schutt 
49939be95e9SJim Schutt 	nlocks = cpu_to_le32(num_flock_locks);
50039be95e9SJim Schutt 	err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
50139be95e9SJim Schutt 	if (err)
50239be95e9SJim Schutt 		goto out_fail;
50339be95e9SJim Schutt 
5044deb14a2SYan, Zheng 	if (num_flock_locks > 0) {
5054deb14a2SYan, Zheng 		err = ceph_pagelist_append(pagelist, &flocks[num_fcntl_locks],
50639be95e9SJim Schutt 					   num_flock_locks * sizeof(*flocks));
5074deb14a2SYan, Zheng 	}
50839be95e9SJim Schutt out_fail:
50939be95e9SJim Schutt 	return err;
51039be95e9SJim Schutt }
511