]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/ceph/locks.c
1 #include <linux/ceph/ceph_debug.h>
3 #include <linux/file.h>
4 #include <linux/namei.h>
5 #include <linux/random.h>
8 #include "mds_client.h"
9 #include <linux/ceph/pagelist.h>
11 static u64 lock_secret
;
13 static inline u64
secure_addr(void *addr
)
15 u64 v
= lock_secret
^ (u64
)(unsigned long)addr
;
17 * Set the most significant bit, so that MDS knows the 'owner'
18 * is sufficient to identify the owner of lock. (old code uses
19 * both 'owner' and 'pid')
25 void __init
ceph_flock_init(void)
27 get_random_bytes(&lock_secret
, sizeof(lock_secret
));
31 * Implement fcntl and flock locking functions.
33 static int ceph_lock_message(u8 lock_type
, u16 operation
, struct file
*file
,
34 int cmd
, u8 wait
, struct file_lock
*fl
)
36 struct inode
*inode
= file_inode(file
);
37 struct ceph_mds_client
*mdsc
= ceph_sb_to_client(inode
->i_sb
)->mdsc
;
38 struct ceph_mds_request
*req
;
43 req
= ceph_mdsc_create_request(mdsc
, operation
, USE_AUTH_MDS
);
49 /* mds requires start and length rather than start and end */
50 if (LLONG_MAX
== fl
->fl_end
)
53 length
= fl
->fl_end
- fl
->fl_start
+ 1;
55 if (lock_type
== CEPH_LOCK_FCNTL
)
56 owner
= secure_addr(fl
->fl_owner
);
58 owner
= secure_addr(fl
->fl_file
);
60 dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
61 "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type
,
62 (int)operation
, owner
, (u64
)fl
->fl_pid
, fl
->fl_start
, length
,
65 req
->r_args
.filelock_change
.rule
= lock_type
;
66 req
->r_args
.filelock_change
.type
= cmd
;
67 req
->r_args
.filelock_change
.owner
= cpu_to_le64(owner
);
68 req
->r_args
.filelock_change
.pid
= cpu_to_le64((u64
)fl
->fl_pid
);
69 req
->r_args
.filelock_change
.start
= cpu_to_le64(fl
->fl_start
);
70 req
->r_args
.filelock_change
.length
= cpu_to_le64(length
);
71 req
->r_args
.filelock_change
.wait
= wait
;
73 err
= ceph_mdsc_do_request(mdsc
, inode
, req
);
75 if (operation
== CEPH_MDS_OP_GETFILELOCK
) {
76 fl
->fl_pid
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->pid
);
77 if (CEPH_LOCK_SHARED
== req
->r_reply_info
.filelock_reply
->type
)
78 fl
->fl_type
= F_RDLCK
;
79 else if (CEPH_LOCK_EXCL
== req
->r_reply_info
.filelock_reply
->type
)
80 fl
->fl_type
= F_WRLCK
;
82 fl
->fl_type
= F_UNLCK
;
84 fl
->fl_start
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->start
);
85 length
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->start
) +
86 le64_to_cpu(req
->r_reply_info
.filelock_reply
->length
);
88 fl
->fl_end
= length
-1;
93 ceph_mdsc_put_request(req
);
94 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
95 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type
,
96 (int)operation
, (u64
)fl
->fl_pid
, fl
->fl_start
,
97 length
, wait
, fl
->fl_type
, err
);
102 * Attempt to set an fcntl lock.
103 * For now, this just goes away to the server. Later it may be more awesome.
105 int ceph_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
110 u16 op
= CEPH_MDS_OP_SETFILELOCK
;
112 if (!(fl
->fl_flags
& FL_POSIX
))
114 /* No mandatory locks */
115 if (__mandatory_lock(file
->f_mapping
->host
) && fl
->fl_type
!= F_UNLCK
)
118 dout("ceph_lock, fl_owner: %p", fl
->fl_owner
);
120 /* set wait bit as appropriate, then make command as Ceph expects it*/
122 op
= CEPH_MDS_OP_GETFILELOCK
;
123 else if (IS_SETLKW(cmd
))
126 if (F_RDLCK
== fl
->fl_type
)
127 lock_cmd
= CEPH_LOCK_SHARED
;
128 else if (F_WRLCK
== fl
->fl_type
)
129 lock_cmd
= CEPH_LOCK_EXCL
;
131 lock_cmd
= CEPH_LOCK_UNLOCK
;
133 err
= ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
, lock_cmd
, wait
, fl
);
135 if (op
!= CEPH_MDS_OP_GETFILELOCK
) {
136 dout("mds locked, locking locally");
137 err
= posix_lock_file(file
, fl
, NULL
);
138 if (err
&& (CEPH_MDS_OP_SETFILELOCK
== op
)) {
139 /* undo! This should only happen if
140 * the kernel detects local
142 ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
,
143 CEPH_LOCK_UNLOCK
, 0, fl
);
144 dout("got %d on posix_lock_file, undid lock",
149 } else if (err
== -ERESTARTSYS
) {
150 dout("undoing lock\n");
151 ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
,
152 CEPH_LOCK_UNLOCK
, 0, fl
);
157 int ceph_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
163 if (!(fl
->fl_flags
& FL_FLOCK
))
165 /* No mandatory locks */
166 if (__mandatory_lock(file
->f_mapping
->host
) && fl
->fl_type
!= F_UNLCK
)
169 dout("ceph_flock, fl_file: %p", fl
->fl_file
);
174 if (F_RDLCK
== fl
->fl_type
)
175 lock_cmd
= CEPH_LOCK_SHARED
;
176 else if (F_WRLCK
== fl
->fl_type
)
177 lock_cmd
= CEPH_LOCK_EXCL
;
179 lock_cmd
= CEPH_LOCK_UNLOCK
;
181 err
= ceph_lock_message(CEPH_LOCK_FLOCK
, CEPH_MDS_OP_SETFILELOCK
,
182 file
, lock_cmd
, wait
, fl
);
184 err
= flock_lock_file_wait(file
, fl
);
186 ceph_lock_message(CEPH_LOCK_FLOCK
,
187 CEPH_MDS_OP_SETFILELOCK
,
188 file
, CEPH_LOCK_UNLOCK
, 0, fl
);
189 dout("got %d on flock_lock_file_wait, undid lock", err
);
191 } else if (err
== -ERESTARTSYS
) {
192 dout("undoing lock\n");
193 ceph_lock_message(CEPH_LOCK_FLOCK
,
194 CEPH_MDS_OP_SETFILELOCK
,
195 file
, CEPH_LOCK_UNLOCK
, 0, fl
);
201 * Must be called with lock_flocks() already held. Fills in the passed
202 * counter variables, so you can prepare pagelist metadata before calling
205 void ceph_count_locks(struct inode
*inode
, int *fcntl_count
, int *flock_count
)
207 struct file_lock
*lock
;
212 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
213 if (lock
->fl_flags
& FL_POSIX
)
215 else if (lock
->fl_flags
& FL_FLOCK
)
218 dout("counted %d flock locks and %d fcntl locks",
219 *flock_count
, *fcntl_count
);
223 * Encode the flock and fcntl locks for the given inode into the ceph_filelock
224 * array. Must be called with inode->i_lock already held.
225 * If we encounter more of a specific lock type than expected, return -ENOSPC.
227 int ceph_encode_locks_to_buffer(struct inode
*inode
,
228 struct ceph_filelock
*flocks
,
229 int num_fcntl_locks
, int num_flock_locks
)
231 struct file_lock
*lock
;
237 dout("encoding %d flock and %d fcntl locks", num_flock_locks
,
240 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
241 if (lock
->fl_flags
& FL_POSIX
) {
243 if (seen_fcntl
> num_fcntl_locks
) {
247 err
= lock_to_ceph_filelock(lock
, &flocks
[l
]);
253 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
254 if (lock
->fl_flags
& FL_FLOCK
) {
256 if (seen_flock
> num_flock_locks
) {
260 err
= lock_to_ceph_filelock(lock
, &flocks
[l
]);
271 * Copy the encoded flock and fcntl locks into the pagelist.
272 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
273 * sequential flock locks.
274 * Returns zero on success.
276 int ceph_locks_to_pagelist(struct ceph_filelock
*flocks
,
277 struct ceph_pagelist
*pagelist
,
278 int num_fcntl_locks
, int num_flock_locks
)
283 nlocks
= cpu_to_le32(num_fcntl_locks
);
284 err
= ceph_pagelist_append(pagelist
, &nlocks
, sizeof(nlocks
));
288 err
= ceph_pagelist_append(pagelist
, flocks
,
289 num_fcntl_locks
* sizeof(*flocks
));
293 nlocks
= cpu_to_le32(num_flock_locks
);
294 err
= ceph_pagelist_append(pagelist
, &nlocks
, sizeof(nlocks
));
298 err
= ceph_pagelist_append(pagelist
,
299 &flocks
[num_fcntl_locks
],
300 num_flock_locks
* sizeof(*flocks
));
306 * Given a pointer to a lock, convert it to a ceph filelock
308 int lock_to_ceph_filelock(struct file_lock
*lock
,
309 struct ceph_filelock
*cephlock
)
312 cephlock
->start
= cpu_to_le64(lock
->fl_start
);
313 cephlock
->length
= cpu_to_le64(lock
->fl_end
- lock
->fl_start
+ 1);
314 cephlock
->client
= cpu_to_le64(0);
315 cephlock
->pid
= cpu_to_le64((u64
)lock
->fl_pid
);
316 if (lock
->fl_flags
& FL_POSIX
)
317 cephlock
->owner
= cpu_to_le64(secure_addr(lock
->fl_owner
));
319 cephlock
->owner
= cpu_to_le64(secure_addr(lock
->fl_file
));
321 switch (lock
->fl_type
) {
323 cephlock
->type
= CEPH_LOCK_SHARED
;
326 cephlock
->type
= CEPH_LOCK_EXCL
;
329 cephlock
->type
= CEPH_LOCK_UNLOCK
;
332 dout("Have unknown lock type %d", lock
->fl_type
);