Commit | Line | Data |
---|---|---|
e8d6c554 DH |
1 | /* AFS file locking support |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
e8d6c554 DH |
12 | #include "internal.h" |
13 | ||
14 | #define AFS_LOCK_GRANTED 0 | |
15 | #define AFS_LOCK_PENDING 1 | |
4be5975a | 16 | #define AFS_LOCK_YOUR_TRY 2 |
e8d6c554 | 17 | |
f044c884 DH |
18 | struct workqueue_struct *afs_lock_manager; |
19 | ||
4be5975a | 20 | static void afs_next_locker(struct afs_vnode *vnode, int error); |
e8d6c554 DH |
21 | static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl); |
22 | static void afs_fl_release_private(struct file_lock *fl); | |
23 | ||
6aed6285 | 24 | static const struct file_lock_operations afs_lock_ops = { |
e8d6c554 DH |
25 | .fl_copy_lock = afs_fl_copy_lock, |
26 | .fl_release_private = afs_fl_release_private, | |
27 | }; | |
28 | ||
4be5975a DH |
29 | static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state) |
30 | { | |
31 | _debug("STATE %u -> %u", vnode->lock_state, state); | |
32 | vnode->lock_state = state; | |
33 | } | |
34 | ||
e8d6c554 DH |
35 | /* |
36 | * if the callback is broken on this vnode, then the lock may now be available | |
37 | */ | |
38 | void afs_lock_may_be_available(struct afs_vnode *vnode) | |
39 | { | |
3b6492df | 40 | _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode); |
e8d6c554 | 41 | |
4be5975a DH |
42 | if (vnode->lock_state != AFS_VNODE_LOCK_WAITING_FOR_CB) |
43 | return; | |
44 | ||
45 | spin_lock(&vnode->lock); | |
46 | if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB) | |
47 | afs_next_locker(vnode, 0); | |
48 | spin_unlock(&vnode->lock); | |
e8d6c554 DH |
49 | } |
50 | ||
51 | /* | |
52 | * the lock will time out in 5 minutes unless we extend it, so schedule | |
53 | * extension in a bit less than that time | |
54 | */ | |
55 | static void afs_schedule_lock_extension(struct afs_vnode *vnode) | |
56 | { | |
a690f60a DH |
57 | ktime_t expires_at, now, duration; |
58 | u64 duration_j; | |
59 | ||
60 | expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2); | |
61 | now = ktime_get_real(); | |
62 | duration = ktime_sub(expires_at, now); | |
63 | if (duration <= 0) | |
64 | duration_j = 0; | |
65 | else | |
66 | duration_j = nsecs_to_jiffies(ktime_to_ns(duration)); | |
67 | ||
68 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j); | |
69 | } | |
70 | ||
71 | /* | |
72 | * In the case of successful completion of a lock operation, record the time | |
73 | * the reply appeared and start the lock extension timer. | |
74 | */ | |
75 | void afs_lock_op_done(struct afs_call *call) | |
76 | { | |
77 | struct afs_vnode *vnode = call->reply[0]; | |
78 | ||
79 | if (call->error == 0) { | |
80 | spin_lock(&vnode->lock); | |
81 | vnode->locked_at = call->reply_time; | |
82 | afs_schedule_lock_extension(vnode); | |
83 | spin_unlock(&vnode->lock); | |
84 | } | |
e8d6c554 DH |
85 | } |
86 | ||
ff8e210a DH |
87 | /* |
88 | * grant one or more locks (readlocks are allowed to jump the queue if the | |
89 | * first lock in the queue is itself a readlock) | |
90 | * - the caller must hold the vnode lock | |
91 | */ | |
4be5975a | 92 | static void afs_grant_locks(struct afs_vnode *vnode) |
ff8e210a DH |
93 | { |
94 | struct file_lock *p, *_p; | |
4be5975a | 95 | bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE); |
ff8e210a | 96 | |
4be5975a DH |
97 | list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) { |
98 | if (!exclusive && p->fl_type == F_WRLCK) | |
99 | continue; | |
100 | ||
101 | list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks); | |
102 | p->fl_u.afs.state = AFS_LOCK_GRANTED; | |
103 | wake_up(&p->fl_wait); | |
104 | } | |
105 | } | |
106 | ||
107 | /* | |
108 | * If an error is specified, reject every pending lock that matches the | |
109 | * authentication and type of the lock we failed to get. If there are any | |
110 | * remaining lockers, try to wake up one of them to have a go. | |
111 | */ | |
112 | static void afs_next_locker(struct afs_vnode *vnode, int error) | |
113 | { | |
114 | struct file_lock *p, *_p, *next = NULL; | |
115 | struct key *key = vnode->lock_key; | |
116 | unsigned int fl_type = F_RDLCK; | |
117 | ||
118 | _enter(""); | |
119 | ||
120 | if (vnode->lock_type == AFS_LOCK_WRITE) | |
121 | fl_type = F_WRLCK; | |
122 | ||
123 | list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) { | |
124 | if (error && | |
125 | p->fl_type == fl_type && | |
126 | afs_file_key(p->fl_file) == key) { | |
127 | list_del_init(&p->fl_u.afs.link); | |
128 | p->fl_u.afs.state = error; | |
129 | wake_up(&p->fl_wait); | |
ff8e210a | 130 | } |
4be5975a DH |
131 | |
132 | /* Select the next locker to hand off to. */ | |
133 | if (next && | |
134 | (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK)) | |
135 | continue; | |
136 | next = p; | |
ff8e210a | 137 | } |
4be5975a DH |
138 | |
139 | vnode->lock_key = NULL; | |
140 | key_put(key); | |
141 | ||
142 | if (next) { | |
143 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); | |
144 | next->fl_u.afs.state = AFS_LOCK_YOUR_TRY; | |
145 | wake_up(&next->fl_wait); | |
146 | } else { | |
147 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE); | |
148 | } | |
149 | ||
150 | _leave(""); | |
ff8e210a DH |
151 | } |
152 | ||
d2ddc776 DH |
153 | /* |
154 | * Get a lock on a file | |
155 | */ | |
156 | static int afs_set_lock(struct afs_vnode *vnode, struct key *key, | |
157 | afs_lock_type_t type) | |
158 | { | |
159 | struct afs_fs_cursor fc; | |
160 | int ret; | |
161 | ||
3b6492df | 162 | _enter("%s{%llx:%llu.%u},%x,%u", |
d2ddc776 DH |
163 | vnode->volume->name, |
164 | vnode->fid.vid, | |
165 | vnode->fid.vnode, | |
166 | vnode->fid.unique, | |
167 | key_serial(key), type); | |
168 | ||
169 | ret = -ERESTARTSYS; | |
170 | if (afs_begin_vnode_operation(&fc, vnode, key)) { | |
171 | while (afs_select_fileserver(&fc)) { | |
68251f0a | 172 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
d2ddc776 DH |
173 | afs_fs_set_lock(&fc, type); |
174 | } | |
175 | ||
176 | afs_check_for_remote_deletion(&fc, fc.vnode); | |
177 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); | |
178 | ret = afs_end_vnode_operation(&fc); | |
179 | } | |
180 | ||
181 | _leave(" = %d", ret); | |
182 | return ret; | |
183 | } | |
184 | ||
185 | /* | |
186 | * Extend a lock on a file | |
187 | */ | |
188 | static int afs_extend_lock(struct afs_vnode *vnode, struct key *key) | |
189 | { | |
190 | struct afs_fs_cursor fc; | |
191 | int ret; | |
192 | ||
3b6492df | 193 | _enter("%s{%llx:%llu.%u},%x", |
d2ddc776 DH |
194 | vnode->volume->name, |
195 | vnode->fid.vid, | |
196 | vnode->fid.vnode, | |
197 | vnode->fid.unique, | |
198 | key_serial(key)); | |
199 | ||
200 | ret = -ERESTARTSYS; | |
201 | if (afs_begin_vnode_operation(&fc, vnode, key)) { | |
202 | while (afs_select_current_fileserver(&fc)) { | |
68251f0a | 203 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
d2ddc776 DH |
204 | afs_fs_extend_lock(&fc); |
205 | } | |
206 | ||
207 | afs_check_for_remote_deletion(&fc, fc.vnode); | |
208 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); | |
209 | ret = afs_end_vnode_operation(&fc); | |
210 | } | |
211 | ||
212 | _leave(" = %d", ret); | |
213 | return ret; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Release a lock on a file | |
218 | */ | |
219 | static int afs_release_lock(struct afs_vnode *vnode, struct key *key) | |
220 | { | |
221 | struct afs_fs_cursor fc; | |
222 | int ret; | |
223 | ||
3b6492df | 224 | _enter("%s{%llx:%llu.%u},%x", |
d2ddc776 DH |
225 | vnode->volume->name, |
226 | vnode->fid.vid, | |
227 | vnode->fid.vnode, | |
228 | vnode->fid.unique, | |
229 | key_serial(key)); | |
230 | ||
231 | ret = -ERESTARTSYS; | |
232 | if (afs_begin_vnode_operation(&fc, vnode, key)) { | |
233 | while (afs_select_current_fileserver(&fc)) { | |
68251f0a | 234 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
d2ddc776 DH |
235 | afs_fs_release_lock(&fc); |
236 | } | |
237 | ||
238 | afs_check_for_remote_deletion(&fc, fc.vnode); | |
239 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); | |
240 | ret = afs_end_vnode_operation(&fc); | |
241 | } | |
242 | ||
243 | _leave(" = %d", ret); | |
244 | return ret; | |
245 | } | |
246 | ||
e8d6c554 DH |
247 | /* |
248 | * do work for a lock, including: | |
249 | * - probing for a lock we're waiting on but didn't get immediately | |
250 | * - extending a lock that's close to timing out | |
251 | */ | |
252 | void afs_lock_work(struct work_struct *work) | |
253 | { | |
254 | struct afs_vnode *vnode = | |
255 | container_of(work, struct afs_vnode, lock_work.work); | |
e8d6c554 DH |
256 | struct key *key; |
257 | int ret; | |
258 | ||
3b6492df | 259 | _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode); |
e8d6c554 DH |
260 | |
261 | spin_lock(&vnode->lock); | |
262 | ||
0fafdc9f DH |
263 | again: |
264 | _debug("wstate %u for %p", vnode->lock_state, vnode); | |
265 | switch (vnode->lock_state) { | |
266 | case AFS_VNODE_LOCK_NEED_UNLOCK: | |
e8d6c554 | 267 | _debug("unlock"); |
4be5975a | 268 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING); |
e8d6c554 DH |
269 | spin_unlock(&vnode->lock); |
270 | ||
271 | /* attempt to release the server lock; if it fails, we just | |
0fafdc9f DH |
272 | * wait 5 minutes and it'll expire anyway */ |
273 | ret = afs_release_lock(vnode, vnode->lock_key); | |
e8d6c554 DH |
274 | if (ret < 0) |
275 | printk(KERN_WARNING "AFS:" | |
3b6492df | 276 | " Failed to release lock on {%llx:%llx} error %d\n", |
e8d6c554 DH |
277 | vnode->fid.vid, vnode->fid.vnode, ret); |
278 | ||
279 | spin_lock(&vnode->lock); | |
4be5975a DH |
280 | afs_next_locker(vnode, 0); |
281 | spin_unlock(&vnode->lock); | |
282 | return; | |
e8d6c554 | 283 | |
0fafdc9f DH |
284 | /* If we've already got a lock, then it must be time to extend that |
285 | * lock as AFS locks time out after 5 minutes. | |
286 | */ | |
287 | case AFS_VNODE_LOCK_GRANTED: | |
e8d6c554 DH |
288 | _debug("extend"); |
289 | ||
0fafdc9f DH |
290 | ASSERT(!list_empty(&vnode->granted_locks)); |
291 | ||
292 | key = key_get(vnode->lock_key); | |
4be5975a | 293 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING); |
e8d6c554 DH |
294 | spin_unlock(&vnode->lock); |
295 | ||
0fafdc9f | 296 | ret = afs_extend_lock(vnode, key); /* RPC */ |
e8d6c554 | 297 | key_put(key); |
0fafdc9f DH |
298 | |
299 | if (ret < 0) | |
3b6492df | 300 | pr_warning("AFS: Failed to extend lock on {%llx:%llx} error %d\n", |
0fafdc9f DH |
301 | vnode->fid.vid, vnode->fid.vnode, ret); |
302 | ||
303 | spin_lock(&vnode->lock); | |
304 | ||
305 | if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING) | |
306 | goto again; | |
4be5975a | 307 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED); |
0fafdc9f | 308 | |
4be5975a | 309 | if (ret != 0) |
e8d6c554 DH |
310 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, |
311 | HZ * 10); | |
0fafdc9f DH |
312 | spin_unlock(&vnode->lock); |
313 | _leave(" [ext]"); | |
e8d6c554 | 314 | return; |
e8d6c554 | 315 | |
4be5975a DH |
316 | /* If we're waiting for a callback to indicate lock release, we can't |
317 | * actually rely on this, so need to recheck at regular intervals. The | |
318 | * problem is that the server might not notify us if the lock just | |
319 | * expires (say because a client died) rather than being explicitly | |
320 | * released. | |
321 | */ | |
0fafdc9f | 322 | case AFS_VNODE_LOCK_WAITING_FOR_CB: |
4be5975a DH |
323 | _debug("retry"); |
324 | afs_next_locker(vnode, 0); | |
e8d6c554 | 325 | spin_unlock(&vnode->lock); |
4be5975a | 326 | return; |
0fafdc9f DH |
327 | |
328 | default: | |
329 | /* Looks like a lock request was withdrawn. */ | |
330 | spin_unlock(&vnode->lock); | |
331 | _leave(" [no]"); | |
e8d6c554 DH |
332 | return; |
333 | } | |
e8d6c554 DH |
334 | } |
335 | ||
336 | /* | |
337 | * pass responsibility for the unlocking of a vnode on the server to the | |
338 | * manager thread, lest a pending signal in the calling thread interrupt | |
339 | * AF_RXRPC | |
340 | * - the caller must hold the vnode lock | |
341 | */ | |
0fafdc9f | 342 | static void afs_defer_unlock(struct afs_vnode *vnode) |
e8d6c554 | 343 | { |
4be5975a | 344 | _enter("%u", vnode->lock_state); |
0fafdc9f | 345 | |
4be5975a DH |
346 | if (list_empty(&vnode->granted_locks) && |
347 | (vnode->lock_state == AFS_VNODE_LOCK_GRANTED || | |
348 | vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) { | |
0fafdc9f DH |
349 | cancel_delayed_work(&vnode->lock_work); |
350 | ||
4be5975a DH |
351 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK); |
352 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0); | |
0fafdc9f DH |
353 | } |
354 | } | |
355 | ||
356 | /* | |
357 | * Check that our view of the file metadata is up to date and check to see | |
358 | * whether we think that we have a locking permit. | |
359 | */ | |
360 | static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key, | |
361 | afs_lock_type_t type, bool can_sleep) | |
362 | { | |
363 | afs_access_t access; | |
364 | int ret; | |
365 | ||
366 | /* Make sure we've got a callback on this file and that our view of the | |
367 | * data version is up to date. | |
368 | */ | |
369 | ret = afs_validate(vnode, key); | |
370 | if (ret < 0) | |
371 | return ret; | |
372 | ||
373 | /* Check the permission set to see if we're actually going to be | |
374 | * allowed to get a lock on this file. | |
375 | */ | |
376 | ret = afs_check_permit(vnode, key, &access); | |
377 | if (ret < 0) | |
378 | return ret; | |
379 | ||
380 | /* At a rough estimation, you need LOCK, WRITE or INSERT perm to | |
381 | * read-lock a file and WRITE or INSERT perm to write-lock a file. | |
382 | * | |
383 | * We can't rely on the server to do this for us since if we want to | |
384 | * share a read lock that we already have, we won't go the server. | |
385 | */ | |
386 | if (type == AFS_LOCK_READ) { | |
387 | if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK))) | |
388 | return -EACCES; | |
389 | if (vnode->status.lock_count == -1 && !can_sleep) | |
390 | return -EAGAIN; /* Write locked */ | |
391 | } else { | |
392 | if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE))) | |
393 | return -EACCES; | |
394 | if (vnode->status.lock_count != 0 && !can_sleep) | |
395 | return -EAGAIN; /* Locked */ | |
396 | } | |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
e8d6c554 DH |
401 | /* |
402 | * request a lock on a file on the server | |
403 | */ | |
404 | static int afs_do_setlk(struct file *file, struct file_lock *fl) | |
405 | { | |
0fafdc9f | 406 | struct inode *inode = locks_inode(file); |
1c8c601a | 407 | struct afs_vnode *vnode = AFS_FS_I(inode); |
e8d6c554 | 408 | afs_lock_type_t type; |
215804a9 | 409 | struct key *key = afs_file_key(file); |
e8d6c554 DH |
410 | int ret; |
411 | ||
3b6492df | 412 | _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type); |
e8d6c554 | 413 | |
e8d6c554 DH |
414 | fl->fl_ops = &afs_lock_ops; |
415 | INIT_LIST_HEAD(&fl->fl_u.afs.link); | |
416 | fl->fl_u.afs.state = AFS_LOCK_PENDING; | |
417 | ||
418 | type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE; | |
419 | ||
0fafdc9f | 420 | ret = afs_do_setlk_check(vnode, key, type, fl->fl_flags & FL_SLEEP); |
e8d6c554 | 421 | if (ret < 0) |
0fafdc9f | 422 | return ret; |
e8d6c554 DH |
423 | |
424 | spin_lock(&vnode->lock); | |
4be5975a | 425 | list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks); |
e8d6c554 | 426 | |
4be5975a DH |
427 | /* If we've already got a lock on the server then try to move to having |
428 | * the VFS grant the requested lock. Note that this means that other | |
429 | * clients may get starved out. | |
0fafdc9f | 430 | */ |
4be5975a DH |
431 | _debug("try %u", vnode->lock_state); |
432 | if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) { | |
433 | if (type == AFS_LOCK_READ) { | |
434 | _debug("instant readlock"); | |
435 | list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks); | |
436 | fl->fl_u.afs.state = AFS_LOCK_GRANTED; | |
437 | goto vnode_is_locked_u; | |
438 | } | |
e8d6c554 | 439 | |
4be5975a DH |
440 | if (vnode->lock_type == AFS_LOCK_WRITE) { |
441 | _debug("instant writelock"); | |
442 | list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks); | |
443 | fl->fl_u.afs.state = AFS_LOCK_GRANTED; | |
444 | goto vnode_is_locked_u; | |
445 | } | |
446 | } | |
e8d6c554 | 447 | |
0fafdc9f DH |
448 | if (vnode->lock_state != AFS_VNODE_LOCK_NONE) |
449 | goto need_to_wait; | |
e8d6c554 | 450 | |
4be5975a | 451 | try_to_lock: |
0fafdc9f DH |
452 | /* We don't have a lock on this vnode and we aren't currently waiting |
453 | * for one either, so ask the server for a lock. | |
454 | * | |
455 | * Note that we need to be careful if we get interrupted by a signal | |
456 | * after dispatching the request as we may still get the lock, even | |
457 | * though we don't wait for the reply (it's not too bad a problem - the | |
4be5975a | 458 | * lock will expire in 5 mins anyway). |
0fafdc9f DH |
459 | */ |
460 | _debug("not locked"); | |
461 | vnode->lock_key = key_get(key); | |
462 | vnode->lock_type = type; | |
4be5975a | 463 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); |
e8d6c554 DH |
464 | spin_unlock(&vnode->lock); |
465 | ||
0fafdc9f | 466 | ret = afs_set_lock(vnode, key, type); /* RPC */ |
e8d6c554 DH |
467 | |
468 | spin_lock(&vnode->lock); | |
0fafdc9f | 469 | switch (ret) { |
4be5975a DH |
470 | case -EKEYREJECTED: |
471 | case -EKEYEXPIRED: | |
472 | case -EKEYREVOKED: | |
473 | case -EPERM: | |
474 | case -EACCES: | |
475 | fl->fl_u.afs.state = ret; | |
476 | list_del_init(&fl->fl_u.afs.link); | |
477 | afs_next_locker(vnode, ret); | |
478 | goto error_unlock; | |
479 | ||
0fafdc9f | 480 | default: |
4be5975a DH |
481 | fl->fl_u.afs.state = ret; |
482 | list_del_init(&fl->fl_u.afs.link); | |
483 | afs_next_locker(vnode, 0); | |
484 | goto error_unlock; | |
e8d6c554 | 485 | |
0fafdc9f DH |
486 | case -EWOULDBLOCK: |
487 | /* The server doesn't have a lock-waiting queue, so the client | |
488 | * will have to retry. The server will break the outstanding | |
489 | * callbacks on a file when a lock is released. | |
490 | */ | |
491 | _debug("would block"); | |
492 | ASSERT(list_empty(&vnode->granted_locks)); | |
493 | ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link); | |
4be5975a | 494 | goto lock_is_contended; |
0fafdc9f DH |
495 | |
496 | case 0: | |
497 | _debug("acquired"); | |
4be5975a DH |
498 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED); |
499 | afs_grant_locks(vnode); | |
500 | goto vnode_is_locked_u; | |
e8d6c554 | 501 | } |
e8d6c554 | 502 | |
4be5975a | 503 | vnode_is_locked_u: |
0fafdc9f | 504 | spin_unlock(&vnode->lock); |
4be5975a DH |
505 | vnode_is_locked: |
506 | /* the lock has been granted by the server... */ | |
507 | ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED); | |
0fafdc9f | 508 | |
4be5975a DH |
509 | /* ... but the VFS still needs to distribute access on this client. */ |
510 | ret = locks_lock_file_wait(file, fl); | |
e8d6c554 DH |
511 | if (ret < 0) |
512 | goto vfs_rejected_lock; | |
e8d6c554 | 513 | |
0fafdc9f | 514 | /* Again, make sure we've got a callback on this file and, again, make |
e8d6c554 | 515 | * sure that our view of the data version is up to date (we ignore |
0fafdc9f DH |
516 | * errors incurred here and deal with the consequences elsewhere). |
517 | */ | |
d2ddc776 | 518 | afs_validate(vnode, key); |
0fafdc9f DH |
519 | _leave(" = 0"); |
520 | return 0; | |
e8d6c554 | 521 | |
4be5975a DH |
522 | lock_is_contended: |
523 | if (!(fl->fl_flags & FL_SLEEP)) { | |
524 | list_del_init(&fl->fl_u.afs.link); | |
525 | afs_next_locker(vnode, 0); | |
526 | ret = -EAGAIN; | |
527 | goto error_unlock; | |
528 | } | |
529 | ||
530 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB); | |
531 | queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5); | |
532 | ||
0fafdc9f DH |
533 | need_to_wait: |
534 | /* We're going to have to wait. Either this client doesn't have a lock | |
535 | * on the server yet and we need to wait for a callback to occur, or | |
4be5975a DH |
536 | * the client does have a lock on the server, but it's shared and we |
537 | * need an exclusive lock. | |
0fafdc9f | 538 | */ |
4be5975a | 539 | spin_unlock(&vnode->lock); |
0fafdc9f | 540 | |
4be5975a DH |
541 | _debug("sleep"); |
542 | ret = wait_event_interruptible(fl->fl_wait, | |
543 | fl->fl_u.afs.state != AFS_LOCK_PENDING); | |
544 | _debug("wait = %d", ret); | |
0fafdc9f | 545 | |
4be5975a | 546 | if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) { |
0fafdc9f | 547 | spin_lock(&vnode->lock); |
0fafdc9f | 548 | |
4be5975a DH |
549 | switch (fl->fl_u.afs.state) { |
550 | case AFS_LOCK_YOUR_TRY: | |
551 | fl->fl_u.afs.state = AFS_LOCK_PENDING; | |
552 | goto try_to_lock; | |
553 | case AFS_LOCK_PENDING: | |
554 | if (ret > 0) { | |
555 | /* We need to retry the lock. We may not be | |
556 | * notified by the server if it just expired | |
557 | * rather than being released. | |
558 | */ | |
559 | ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB); | |
560 | afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING); | |
561 | fl->fl_u.afs.state = AFS_LOCK_PENDING; | |
562 | goto try_to_lock; | |
563 | } | |
564 | goto error_unlock; | |
565 | case AFS_LOCK_GRANTED: | |
566 | default: | |
567 | break; | |
568 | } | |
0fafdc9f | 569 | |
4be5975a DH |
570 | spin_unlock(&vnode->lock); |
571 | } | |
0fafdc9f | 572 | |
4be5975a DH |
573 | if (fl->fl_u.afs.state == AFS_LOCK_GRANTED) |
574 | goto vnode_is_locked; | |
575 | ret = fl->fl_u.afs.state; | |
576 | goto error; | |
e8d6c554 DH |
577 | |
578 | vfs_rejected_lock: | |
0fafdc9f DH |
579 | /* The VFS rejected the lock we just obtained, so we have to discard |
580 | * what we just got. We defer this to the lock manager work item to | |
581 | * deal with. | |
582 | */ | |
e8d6c554 | 583 | _debug("vfs refused %d", ret); |
0fafdc9f | 584 | spin_lock(&vnode->lock); |
e8d6c554 | 585 | list_del_init(&fl->fl_u.afs.link); |
4be5975a DH |
586 | afs_defer_unlock(vnode); |
587 | ||
588 | error_unlock: | |
589 | spin_unlock(&vnode->lock); | |
590 | error: | |
591 | _leave(" = %d", ret); | |
592 | return ret; | |
e8d6c554 DH |
593 | } |
594 | ||
595 | /* | |
596 | * unlock on a file on the server | |
597 | */ | |
598 | static int afs_do_unlk(struct file *file, struct file_lock *fl) | |
599 | { | |
0fafdc9f | 600 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
e8d6c554 DH |
601 | int ret; |
602 | ||
3b6492df | 603 | _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type); |
e8d6c554 | 604 | |
0fafdc9f DH |
605 | /* Flush all pending writes before doing anything with locks. */ |
606 | vfs_fsync(file, 0); | |
607 | ||
4be5975a | 608 | ret = locks_lock_file_wait(file, fl); |
0fafdc9f DH |
609 | _leave(" = %d [%u]", ret, vnode->lock_state); |
610 | return ret; | |
e8d6c554 DH |
611 | } |
612 | ||
613 | /* | |
614 | * return information about a lock we currently hold, if indeed we hold one | |
615 | */ | |
616 | static int afs_do_getlk(struct file *file, struct file_lock *fl) | |
617 | { | |
0fafdc9f | 618 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
215804a9 | 619 | struct key *key = afs_file_key(file); |
e8d6c554 DH |
620 | int ret, lock_count; |
621 | ||
622 | _enter(""); | |
623 | ||
624 | fl->fl_type = F_UNLCK; | |
625 | ||
e8d6c554 | 626 | /* check local lock records first */ |
275afcac AM |
627 | posix_test_lock(file, fl); |
628 | if (fl->fl_type == F_UNLCK) { | |
e8d6c554 | 629 | /* no local locks; consult the server */ |
0c3a5ac2 | 630 | ret = afs_fetch_status(vnode, key, false); |
e8d6c554 DH |
631 | if (ret < 0) |
632 | goto error; | |
0fafdc9f DH |
633 | |
634 | lock_count = READ_ONCE(vnode->status.lock_count); | |
68ce801f DH |
635 | if (lock_count != 0) { |
636 | if (lock_count > 0) | |
637 | fl->fl_type = F_RDLCK; | |
638 | else | |
639 | fl->fl_type = F_WRLCK; | |
640 | fl->fl_start = 0; | |
641 | fl->fl_end = OFFSET_MAX; | |
642 | fl->fl_pid = 0; | |
643 | } | |
e8d6c554 DH |
644 | } |
645 | ||
0fafdc9f | 646 | ret = 0; |
e8d6c554 | 647 | error: |
e8d6c554 DH |
648 | _leave(" = %d [%hd]", ret, fl->fl_type); |
649 | return ret; | |
650 | } | |
651 | ||
652 | /* | |
653 | * manage POSIX locks on a file | |
654 | */ | |
655 | int afs_lock(struct file *file, int cmd, struct file_lock *fl) | |
656 | { | |
0fafdc9f | 657 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
e8d6c554 | 658 | |
3b6492df | 659 | _enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}", |
e8d6c554 DH |
660 | vnode->fid.vid, vnode->fid.vnode, cmd, |
661 | fl->fl_type, fl->fl_flags, | |
662 | (long long) fl->fl_start, (long long) fl->fl_end); | |
663 | ||
664 | /* AFS doesn't support mandatory locks */ | |
fc5846e5 | 665 | if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK) |
e8d6c554 DH |
666 | return -ENOLCK; |
667 | ||
668 | if (IS_GETLK(cmd)) | |
669 | return afs_do_getlk(file, fl); | |
670 | if (fl->fl_type == F_UNLCK) | |
671 | return afs_do_unlk(file, fl); | |
672 | return afs_do_setlk(file, fl); | |
673 | } | |
674 | ||
675 | /* | |
676 | * manage FLOCK locks on a file | |
677 | */ | |
678 | int afs_flock(struct file *file, int cmd, struct file_lock *fl) | |
679 | { | |
0fafdc9f | 680 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(file)); |
e8d6c554 | 681 | |
3b6492df | 682 | _enter("{%llx:%llu},%d,{t=%x,fl=%x}", |
e8d6c554 DH |
683 | vnode->fid.vid, vnode->fid.vnode, cmd, |
684 | fl->fl_type, fl->fl_flags); | |
685 | ||
686 | /* | |
687 | * No BSD flocks over NFS allowed. | |
688 | * Note: we could try to fake a POSIX lock request here by | |
689 | * using ((u32) filp | 0x80000000) or some such as the pid. | |
690 | * Not sure whether that would be unique, though, or whether | |
691 | * that would break in other places. | |
692 | */ | |
693 | if (!(fl->fl_flags & FL_FLOCK)) | |
694 | return -ENOLCK; | |
695 | ||
696 | /* we're simulating flock() locks using posix locks on the server */ | |
e8d6c554 DH |
697 | if (fl->fl_type == F_UNLCK) |
698 | return afs_do_unlk(file, fl); | |
699 | return afs_do_setlk(file, fl); | |
700 | } | |
701 | ||
702 | /* | |
703 | * the POSIX lock management core VFS code copies the lock record and adds the | |
704 | * copy into its own list, so we need to add that copy to the vnode's lock | |
705 | * queue in the same place as the original (which will be deleted shortly | |
706 | * after) | |
707 | */ | |
708 | static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl) | |
709 | { | |
0fafdc9f DH |
710 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file)); |
711 | ||
e8d6c554 DH |
712 | _enter(""); |
713 | ||
0fafdc9f | 714 | spin_lock(&vnode->lock); |
e8d6c554 | 715 | list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link); |
0fafdc9f | 716 | spin_unlock(&vnode->lock); |
e8d6c554 DH |
717 | } |
718 | ||
719 | /* | |
720 | * need to remove this lock from the vnode queue when it's removed from the | |
721 | * VFS's list | |
722 | */ | |
723 | static void afs_fl_release_private(struct file_lock *fl) | |
724 | { | |
0fafdc9f DH |
725 | struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file)); |
726 | ||
e8d6c554 DH |
727 | _enter(""); |
728 | ||
0fafdc9f | 729 | spin_lock(&vnode->lock); |
4be5975a DH |
730 | |
731 | list_del_init(&fl->fl_u.afs.link); | |
732 | if (list_empty(&vnode->granted_locks)) | |
733 | afs_defer_unlock(vnode); | |
734 | ||
0fafdc9f DH |
735 | _debug("state %u for %p", vnode->lock_state, vnode); |
736 | spin_unlock(&vnode->lock); | |
e8d6c554 | 737 | } |