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
);
50 /* mds requires start and length rather than start and end */
51 if (LLONG_MAX
== fl
->fl_end
)
54 length
= fl
->fl_end
- fl
->fl_start
+ 1;
56 owner
= secure_addr(fl
->fl_owner
);
58 dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
59 "start: %llu, length: %llu, wait: %d, type: %d", (int)lock_type
,
60 (int)operation
, owner
, (u64
)fl
->fl_pid
, fl
->fl_start
, length
,
63 req
->r_args
.filelock_change
.rule
= lock_type
;
64 req
->r_args
.filelock_change
.type
= cmd
;
65 req
->r_args
.filelock_change
.owner
= cpu_to_le64(owner
);
66 req
->r_args
.filelock_change
.pid
= cpu_to_le64((u64
)fl
->fl_pid
);
67 req
->r_args
.filelock_change
.start
= cpu_to_le64(fl
->fl_start
);
68 req
->r_args
.filelock_change
.length
= cpu_to_le64(length
);
69 req
->r_args
.filelock_change
.wait
= wait
;
71 err
= ceph_mdsc_do_request(mdsc
, inode
, req
);
73 if (operation
== CEPH_MDS_OP_GETFILELOCK
) {
74 fl
->fl_pid
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->pid
);
75 if (CEPH_LOCK_SHARED
== req
->r_reply_info
.filelock_reply
->type
)
76 fl
->fl_type
= F_RDLCK
;
77 else if (CEPH_LOCK_EXCL
== req
->r_reply_info
.filelock_reply
->type
)
78 fl
->fl_type
= F_WRLCK
;
80 fl
->fl_type
= F_UNLCK
;
82 fl
->fl_start
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->start
);
83 length
= le64_to_cpu(req
->r_reply_info
.filelock_reply
->start
) +
84 le64_to_cpu(req
->r_reply_info
.filelock_reply
->length
);
86 fl
->fl_end
= length
-1;
91 ceph_mdsc_put_request(req
);
92 dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
93 "length: %llu, wait: %d, type: %d, err code %d", (int)lock_type
,
94 (int)operation
, (u64
)fl
->fl_pid
, fl
->fl_start
,
95 length
, wait
, fl
->fl_type
, err
);
100 * Attempt to set an fcntl lock.
101 * For now, this just goes away to the server. Later it may be more awesome.
103 int ceph_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
108 u16 op
= CEPH_MDS_OP_SETFILELOCK
;
110 if (!(fl
->fl_flags
& FL_POSIX
))
112 /* No mandatory locks */
113 if (__mandatory_lock(file
->f_mapping
->host
) && fl
->fl_type
!= F_UNLCK
)
116 dout("ceph_lock, fl_owner: %p", fl
->fl_owner
);
118 /* set wait bit as appropriate, then make command as Ceph expects it*/
120 op
= CEPH_MDS_OP_GETFILELOCK
;
121 else if (IS_SETLKW(cmd
))
124 if (F_RDLCK
== fl
->fl_type
)
125 lock_cmd
= CEPH_LOCK_SHARED
;
126 else if (F_WRLCK
== fl
->fl_type
)
127 lock_cmd
= CEPH_LOCK_EXCL
;
129 lock_cmd
= CEPH_LOCK_UNLOCK
;
131 err
= ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
, lock_cmd
, wait
, fl
);
133 if (op
!= CEPH_MDS_OP_GETFILELOCK
) {
134 dout("mds locked, locking locally");
135 err
= posix_lock_file(file
, fl
, NULL
);
136 if (err
&& (CEPH_MDS_OP_SETFILELOCK
== op
)) {
137 /* undo! This should only happen if
138 * the kernel detects local
140 ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
,
141 CEPH_LOCK_UNLOCK
, 0, fl
);
142 dout("got %d on posix_lock_file, undid lock",
147 } else if (err
== -ERESTARTSYS
) {
148 dout("undoing lock\n");
149 ceph_lock_message(CEPH_LOCK_FCNTL
, op
, file
,
150 CEPH_LOCK_UNLOCK
, 0, fl
);
155 int ceph_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
161 if (!(fl
->fl_flags
& FL_FLOCK
))
163 /* No mandatory locks */
164 if (__mandatory_lock(file
->f_mapping
->host
) && fl
->fl_type
!= F_UNLCK
)
167 dout("ceph_flock, fl_file: %p", fl
->fl_file
);
172 if (F_RDLCK
== fl
->fl_type
)
173 lock_cmd
= CEPH_LOCK_SHARED
;
174 else if (F_WRLCK
== fl
->fl_type
)
175 lock_cmd
= CEPH_LOCK_EXCL
;
177 lock_cmd
= CEPH_LOCK_UNLOCK
;
179 err
= ceph_lock_message(CEPH_LOCK_FLOCK
, CEPH_MDS_OP_SETFILELOCK
,
180 file
, lock_cmd
, wait
, fl
);
182 err
= flock_lock_file_wait(file
, fl
);
184 ceph_lock_message(CEPH_LOCK_FLOCK
,
185 CEPH_MDS_OP_SETFILELOCK
,
186 file
, CEPH_LOCK_UNLOCK
, 0, fl
);
187 dout("got %d on flock_lock_file_wait, undid lock", err
);
189 } else if (err
== -ERESTARTSYS
) {
190 dout("undoing lock\n");
191 ceph_lock_message(CEPH_LOCK_FLOCK
,
192 CEPH_MDS_OP_SETFILELOCK
,
193 file
, CEPH_LOCK_UNLOCK
, 0, fl
);
199 * Must be called with lock_flocks() already held. Fills in the passed
200 * counter variables, so you can prepare pagelist metadata before calling
203 void ceph_count_locks(struct inode
*inode
, int *fcntl_count
, int *flock_count
)
205 struct file_lock
*lock
;
210 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
211 if (lock
->fl_flags
& FL_POSIX
)
213 else if (lock
->fl_flags
& FL_FLOCK
)
216 dout("counted %d flock locks and %d fcntl locks",
217 *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.
225 int ceph_encode_locks_to_buffer(struct inode
*inode
,
226 struct ceph_filelock
*flocks
,
227 int num_fcntl_locks
, int num_flock_locks
)
229 struct file_lock
*lock
;
235 dout("encoding %d flock and %d fcntl locks", num_flock_locks
,
238 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
239 if (lock
->fl_flags
& FL_POSIX
) {
241 if (seen_fcntl
> num_fcntl_locks
) {
245 err
= lock_to_ceph_filelock(lock
, &flocks
[l
]);
251 for (lock
= inode
->i_flock
; lock
!= NULL
; lock
= lock
->fl_next
) {
252 if (lock
->fl_flags
& FL_FLOCK
) {
254 if (seen_flock
> num_flock_locks
) {
258 err
= lock_to_ceph_filelock(lock
, &flocks
[l
]);
269 * Copy the encoded flock and fcntl locks into the pagelist.
270 * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
271 * sequential flock locks.
272 * Returns zero on success.
274 int ceph_locks_to_pagelist(struct ceph_filelock
*flocks
,
275 struct ceph_pagelist
*pagelist
,
276 int num_fcntl_locks
, int num_flock_locks
)
281 nlocks
= cpu_to_le32(num_fcntl_locks
);
282 err
= ceph_pagelist_append(pagelist
, &nlocks
, sizeof(nlocks
));
286 err
= ceph_pagelist_append(pagelist
, flocks
,
287 num_fcntl_locks
* sizeof(*flocks
));
291 nlocks
= cpu_to_le32(num_flock_locks
);
292 err
= ceph_pagelist_append(pagelist
, &nlocks
, sizeof(nlocks
));
296 err
= ceph_pagelist_append(pagelist
,
297 &flocks
[num_fcntl_locks
],
298 num_flock_locks
* sizeof(*flocks
));
304 * Given a pointer to a lock, convert it to a ceph filelock
306 int lock_to_ceph_filelock(struct file_lock
*lock
,
307 struct ceph_filelock
*cephlock
)
310 cephlock
->start
= cpu_to_le64(lock
->fl_start
);
311 cephlock
->length
= cpu_to_le64(lock
->fl_end
- lock
->fl_start
+ 1);
312 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
));
316 switch (lock
->fl_type
) {
318 cephlock
->type
= CEPH_LOCK_SHARED
;
321 cephlock
->type
= CEPH_LOCK_EXCL
;
324 cephlock
->type
= CEPH_LOCK_UNLOCK
;
327 dout("Have unknown lock type %d", lock
->fl_type
);