locks.c 8.5 KB
Newer Older
1
#include <linux/ceph/ceph_debug.h>
G
Greg Farnum 已提交
2 3 4

#include <linux/file.h>
#include <linux/namei.h>
5
#include <linux/random.h>
G
Greg Farnum 已提交
6 7 8

#include "super.h"
#include "mds_client.h"
9
#include <linux/ceph/pagelist.h>
G
Greg Farnum 已提交
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
static u64 lock_secret;

static inline u64 secure_addr(void *addr)
{
	u64 v = lock_secret ^ (u64)(unsigned long)addr;
	/*
	 * Set the most significant bit, so that MDS knows the 'owner'
	 * is sufficient to identify the owner of lock. (old code uses
	 * both 'owner' and 'pid')
	 */
	v |= (1ULL << 63);
	return v;
}

void __init ceph_flock_init(void)
{
	get_random_bytes(&lock_secret, sizeof(lock_secret));
}

G
Greg Farnum 已提交
30 31 32 33
/**
 * Implement fcntl and flock locking functions.
 */
static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
34
			     int cmd, u8 wait, struct file_lock *fl)
G
Greg Farnum 已提交
35
{
A
Al Viro 已提交
36
	struct inode *inode = file_inode(file);
37
	struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
G
Greg Farnum 已提交
38 39
	struct ceph_mds_request *req;
	int err;
40
	u64 length = 0;
41
	u64 owner;
G
Greg Farnum 已提交
42 43 44 45

	req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
	if (IS_ERR(req))
		return PTR_ERR(req);
46 47
	req->r_inode = inode;
	ihold(inode);
48
	req->r_num_caps = 1;
G
Greg Farnum 已提交
49

50 51 52 53 54 55
	/* mds requires start and length rather than start and end */
	if (LLONG_MAX == fl->fl_end)
		length = 0;
	else
		length = fl->fl_end - fl->fl_start + 1;

56
	owner = secure_addr(fl->fl_owner);
57 58 59 60 61

	dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
	     "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type,
	     (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
	     wait, fl->fl_type);
62

G
Greg Farnum 已提交
63 64
	req->r_args.filelock_change.rule = lock_type;
	req->r_args.filelock_change.type = cmd;
65
	req->r_args.filelock_change.owner = cpu_to_le64(owner);
66 67
	req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
	req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
G
Greg Farnum 已提交
68 69 70 71
	req->r_args.filelock_change.length = cpu_to_le64(length);
	req->r_args.filelock_change.wait = wait;

	err = ceph_mdsc_do_request(mdsc, inode, req);
72

73
	if (operation == CEPH_MDS_OP_GETFILELOCK) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		fl->fl_pid = le64_to_cpu(req->r_reply_info.filelock_reply->pid);
		if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
			fl->fl_type = F_RDLCK;
		else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
			fl->fl_type = F_WRLCK;
		else
			fl->fl_type = F_UNLCK;

		fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
		length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
						 le64_to_cpu(req->r_reply_info.filelock_reply->length);
		if (length >= 1)
			fl->fl_end = length -1;
		else
			fl->fl_end = 0;

	}
G
Greg Farnum 已提交
91 92
	ceph_mdsc_put_request(req);
	dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
S
Sage Weil 已提交
93
	     "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type,
94 95
	     (int)operation, (u64)fl->fl_pid, fl->fl_start,
	     length, wait, fl->fl_type, err);
G
Greg Farnum 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109
	return err;
}

/**
 * Attempt to set an fcntl lock.
 * For now, this just goes away to the server. Later it may be more awesome.
 */
int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
{
	u8 lock_cmd;
	int err;
	u8 wait = 0;
	u16 op = CEPH_MDS_OP_SETFILELOCK;

Y
Yan, Zheng 已提交
110 111 112 113 114 115
	if (!(fl->fl_flags & FL_POSIX))
		return -ENOLCK;
	/* No mandatory locks */
	if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
		return -ENOLCK;

116
	dout("ceph_lock, fl_owner: %p", fl->fl_owner);
G
Greg Farnum 已提交
117 118

	/* set wait bit as appropriate, then make command as Ceph expects it*/
119
	if (IS_GETLK(cmd))
G
Greg Farnum 已提交
120
		op = CEPH_MDS_OP_GETFILELOCK;
121 122
	else if (IS_SETLKW(cmd))
		wait = 1;
G
Greg Farnum 已提交
123 124 125 126 127 128 129 130

	if (F_RDLCK == fl->fl_type)
		lock_cmd = CEPH_LOCK_SHARED;
	else if (F_WRLCK == fl->fl_type)
		lock_cmd = CEPH_LOCK_EXCL;
	else
		lock_cmd = CEPH_LOCK_UNLOCK;

131
	err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
G
Greg Farnum 已提交
132
	if (!err) {
133
		if (op != CEPH_MDS_OP_GETFILELOCK) {
134 135 136
			dout("mds locked, locking locally");
			err = posix_lock_file(file, fl, NULL);
			if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
S
Sage Weil 已提交
137 138 139
				/* undo! This should only happen if
				 * the kernel detects local
				 * deadlock. */
140 141
				ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
						  CEPH_LOCK_UNLOCK, 0, fl);
S
Sage Weil 已提交
142 143
				dout("got %d on posix_lock_file, undid lock",
				     err);
144
			}
G
Greg Farnum 已提交
145
		}
146

S
Sage Weil 已提交
147 148 149 150
	} else if (err == -ERESTARTSYS) {
		dout("undoing lock\n");
		ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
				  CEPH_LOCK_UNLOCK, 0, fl);
G
Greg Farnum 已提交
151 152 153 154 155 156 157 158
	}
	return err;
}

int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
{
	u8 lock_cmd;
	int err;
159
	u8 wait = 0;
G
Greg Farnum 已提交
160

Y
Yan, Zheng 已提交
161 162 163 164 165 166
	if (!(fl->fl_flags & FL_FLOCK))
		return -ENOLCK;
	/* No mandatory locks */
	if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
		return -ENOLCK;

167
	dout("ceph_flock, fl_file: %p", fl->fl_file);
G
Greg Farnum 已提交
168

169 170 171 172
	if (IS_SETLKW(cmd))
		wait = 1;

	if (F_RDLCK == fl->fl_type)
G
Greg Farnum 已提交
173
		lock_cmd = CEPH_LOCK_SHARED;
174
	else if (F_WRLCK == fl->fl_type)
G
Greg Farnum 已提交
175 176 177 178 179
		lock_cmd = CEPH_LOCK_EXCL;
	else
		lock_cmd = CEPH_LOCK_UNLOCK;

	err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
180
				file, lock_cmd, wait, fl);
G
Greg Farnum 已提交
181 182 183 184 185
	if (!err) {
		err = flock_lock_file_wait(file, fl);
		if (err) {
			ceph_lock_message(CEPH_LOCK_FLOCK,
					  CEPH_MDS_OP_SETFILELOCK,
186
					  file, CEPH_LOCK_UNLOCK, 0, fl);
G
Greg Farnum 已提交
187 188
			dout("got %d on flock_lock_file_wait, undid lock", err);
		}
S
Sage Weil 已提交
189 190 191 192 193
	} else if (err == -ERESTARTSYS) {
		dout("undoing lock\n");
		ceph_lock_message(CEPH_LOCK_FLOCK,
				  CEPH_MDS_OP_SETFILELOCK,
				  file, CEPH_LOCK_UNLOCK, 0, fl);
G
Greg Farnum 已提交
194 195 196 197 198
	}
	return err;
}

/**
199
 * Must be called with lock_flocks() already held. Fills in the passed
G
Greg Farnum 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
 * counter variables, so you can prepare pagelist metadata before calling
 * ceph_encode_locks.
 */
void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
{
	struct file_lock *lock;

	*fcntl_count = 0;
	*flock_count = 0;

	for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
		if (lock->fl_flags & FL_POSIX)
			++(*fcntl_count);
		else if (lock->fl_flags & FL_FLOCK)
			++(*flock_count);
	}
	dout("counted %d flock locks and %d fcntl locks",
	     *flock_count, *fcntl_count);
}

/**
221
 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
222
 * array. Must be called with inode->i_lock already held.
223
 * If we encounter more of a specific lock type than expected, return -ENOSPC.
G
Greg Farnum 已提交
224
 */
225 226 227
int ceph_encode_locks_to_buffer(struct inode *inode,
				struct ceph_filelock *flocks,
				int num_fcntl_locks, int num_flock_locks)
G
Greg Farnum 已提交
228 229 230
{
	struct file_lock *lock;
	int err = 0;
231 232
	int seen_fcntl = 0;
	int seen_flock = 0;
233
	int l = 0;
G
Greg Farnum 已提交
234 235 236

	dout("encoding %d flock and %d fcntl locks", num_flock_locks,
	     num_fcntl_locks);
237

G
Greg Farnum 已提交
238 239
	for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
		if (lock->fl_flags & FL_POSIX) {
240 241 242 243 244
			++seen_fcntl;
			if (seen_fcntl > num_fcntl_locks) {
				err = -ENOSPC;
				goto fail;
			}
245
			err = lock_to_ceph_filelock(lock, &flocks[l]);
G
Greg Farnum 已提交
246 247
			if (err)
				goto fail;
248
			++l;
G
Greg Farnum 已提交
249 250 251 252
		}
	}
	for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
		if (lock->fl_flags & FL_FLOCK) {
253 254 255 256 257
			++seen_flock;
			if (seen_flock > num_flock_locks) {
				err = -ENOSPC;
				goto fail;
			}
258
			err = lock_to_ceph_filelock(lock, &flocks[l]);
G
Greg Farnum 已提交
259 260
			if (err)
				goto fail;
261
			++l;
G
Greg Farnum 已提交
262 263 264 265 266 267
		}
	}
fail:
	return err;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/**
 * Copy the encoded flock and fcntl locks into the pagelist.
 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
 * sequential flock locks.
 * Returns zero on success.
 */
int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
			   struct ceph_pagelist *pagelist,
			   int num_fcntl_locks, int num_flock_locks)
{
	int err = 0;
	__le32 nlocks;

	nlocks = cpu_to_le32(num_fcntl_locks);
	err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
	if (err)
		goto out_fail;

	err = ceph_pagelist_append(pagelist, flocks,
				   num_fcntl_locks * sizeof(*flocks));
	if (err)
		goto out_fail;

	nlocks = cpu_to_le32(num_flock_locks);
	err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
	if (err)
		goto out_fail;

	err = ceph_pagelist_append(pagelist,
				   &flocks[num_fcntl_locks],
				   num_flock_locks * sizeof(*flocks));
out_fail:
	return err;
}

G
Greg Farnum 已提交
303 304 305 306 307 308 309 310 311 312
/*
 * Given a pointer to a lock, convert it to a ceph filelock
 */
int lock_to_ceph_filelock(struct file_lock *lock,
			  struct ceph_filelock *cephlock)
{
	int err = 0;
	cephlock->start = cpu_to_le64(lock->fl_start);
	cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
	cephlock->client = cpu_to_le64(0);
313
	cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
314
	cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
G
Greg Farnum 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

	switch (lock->fl_type) {
	case F_RDLCK:
		cephlock->type = CEPH_LOCK_SHARED;
		break;
	case F_WRLCK:
		cephlock->type = CEPH_LOCK_EXCL;
		break;
	case F_UNLCK:
		cephlock->type = CEPH_LOCK_UNLOCK;
		break;
	default:
		dout("Have unknown lock type %d", lock->fl_type);
		err = -EINVAL;
	}

	return err;
}