1
2
3
4
5
6
7
8
9
10
11
12#include "internal.h"
13
14#define AFS_LOCK_GRANTED 0
15#define AFS_LOCK_PENDING 1
16
17static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
18static void afs_fl_release_private(struct file_lock *fl);
19
20static struct workqueue_struct *afs_lock_manager;
21static DEFINE_MUTEX(afs_lock_manager_mutex);
22
23static const struct file_lock_operations afs_lock_ops = {
24 .fl_copy_lock = afs_fl_copy_lock,
25 .fl_release_private = afs_fl_release_private,
26};
27
28
29
30
31static int afs_init_lock_manager(void)
32{
33 int ret;
34
35 ret = 0;
36 if (!afs_lock_manager) {
37 mutex_lock(&afs_lock_manager_mutex);
38 if (!afs_lock_manager) {
39 afs_lock_manager =
40 create_singlethread_workqueue("kafs_lockd");
41 if (!afs_lock_manager)
42 ret = -ENOMEM;
43 }
44 mutex_unlock(&afs_lock_manager_mutex);
45 }
46 return ret;
47}
48
49
50
51
52void __exit afs_kill_lock_manager(void)
53{
54 if (afs_lock_manager)
55 destroy_workqueue(afs_lock_manager);
56}
57
58
59
60
61void afs_lock_may_be_available(struct afs_vnode *vnode)
62{
63 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
64
65 queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
66}
67
68
69
70
71
72static void afs_schedule_lock_extension(struct afs_vnode *vnode)
73{
74 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
75 AFS_LOCKWAIT * HZ / 2);
76}
77
78
79
80
81
82
83static void afs_grant_locks(struct afs_vnode *vnode, struct file_lock *fl)
84{
85 struct file_lock *p, *_p;
86
87 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
88 if (fl->fl_type == F_RDLCK) {
89 list_for_each_entry_safe(p, _p, &vnode->pending_locks,
90 fl_u.afs.link) {
91 if (p->fl_type == F_RDLCK) {
92 p->fl_u.afs.state = AFS_LOCK_GRANTED;
93 list_move_tail(&p->fl_u.afs.link,
94 &vnode->granted_locks);
95 wake_up(&p->fl_wait);
96 }
97 }
98 }
99}
100
101
102
103
104
105
106void afs_lock_work(struct work_struct *work)
107{
108 struct afs_vnode *vnode =
109 container_of(work, struct afs_vnode, lock_work.work);
110 struct file_lock *fl;
111 afs_lock_type_t type;
112 struct key *key;
113 int ret;
114
115 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
116
117 spin_lock(&vnode->lock);
118
119 if (test_bit(AFS_VNODE_UNLOCKING, &vnode->flags)) {
120 _debug("unlock");
121 spin_unlock(&vnode->lock);
122
123
124
125 ret = afs_vnode_release_lock(vnode, vnode->unlock_key);
126 if (ret < 0)
127 printk(KERN_WARNING "AFS:"
128 " Failed to release lock on {%x:%x} error %d\n",
129 vnode->fid.vid, vnode->fid.vnode, ret);
130
131 spin_lock(&vnode->lock);
132 key_put(vnode->unlock_key);
133 vnode->unlock_key = NULL;
134 clear_bit(AFS_VNODE_UNLOCKING, &vnode->flags);
135 }
136
137
138
139 if (!list_empty(&vnode->granted_locks)) {
140 _debug("extend");
141
142 if (test_and_set_bit(AFS_VNODE_LOCKING, &vnode->flags))
143 BUG();
144 fl = list_entry(vnode->granted_locks.next,
145 struct file_lock, fl_u.afs.link);
146 key = key_get(fl->fl_file->private_data);
147 spin_unlock(&vnode->lock);
148
149 ret = afs_vnode_extend_lock(vnode, key);
150 clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
151 key_put(key);
152 switch (ret) {
153 case 0:
154 afs_schedule_lock_extension(vnode);
155 break;
156 default:
157
158
159 printk(KERN_WARNING "AFS:"
160 " Failed to extend lock on {%x:%x} error %d\n",
161 vnode->fid.vid, vnode->fid.vnode, ret);
162 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
163 HZ * 10);
164 break;
165 }
166 _leave(" [extend]");
167 return;
168 }
169
170
171
172
173 if (!list_empty(&vnode->pending_locks)) {
174 _debug("get");
175
176 if (test_and_set_bit(AFS_VNODE_LOCKING, &vnode->flags))
177 BUG();
178 fl = list_entry(vnode->pending_locks.next,
179 struct file_lock, fl_u.afs.link);
180 key = key_get(fl->fl_file->private_data);
181 type = (fl->fl_type == F_RDLCK) ?
182 AFS_LOCK_READ : AFS_LOCK_WRITE;
183 spin_unlock(&vnode->lock);
184
185 ret = afs_vnode_set_lock(vnode, key, type);
186 clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
187 switch (ret) {
188 case -EWOULDBLOCK:
189 _debug("blocked");
190 break;
191 case 0:
192 _debug("acquired");
193 if (type == AFS_LOCK_READ)
194 set_bit(AFS_VNODE_READLOCKED, &vnode->flags);
195 else
196 set_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
197 ret = AFS_LOCK_GRANTED;
198 default:
199 spin_lock(&vnode->lock);
200
201
202 if (list_entry(vnode->pending_locks.next,
203 struct file_lock, fl_u.afs.link) == fl) {
204 fl->fl_u.afs.state = ret;
205 if (ret == AFS_LOCK_GRANTED)
206 afs_grant_locks(vnode, fl);
207 else
208 list_del_init(&fl->fl_u.afs.link);
209 wake_up(&fl->fl_wait);
210 spin_unlock(&vnode->lock);
211 } else {
212 _debug("withdrawn");
213 clear_bit(AFS_VNODE_READLOCKED, &vnode->flags);
214 clear_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
215 spin_unlock(&vnode->lock);
216 afs_vnode_release_lock(vnode, key);
217 if (!list_empty(&vnode->pending_locks))
218 afs_lock_may_be_available(vnode);
219 }
220 break;
221 }
222 key_put(key);
223 _leave(" [pend]");
224 return;
225 }
226
227
228 spin_unlock(&vnode->lock);
229 _leave(" [no locks]");
230}
231
232
233
234
235
236
237
238static void afs_defer_unlock(struct afs_vnode *vnode, struct key *key)
239{
240 cancel_delayed_work(&vnode->lock_work);
241 if (!test_and_clear_bit(AFS_VNODE_READLOCKED, &vnode->flags) &&
242 !test_and_clear_bit(AFS_VNODE_WRITELOCKED, &vnode->flags))
243 BUG();
244 if (test_and_set_bit(AFS_VNODE_UNLOCKING, &vnode->flags))
245 BUG();
246 vnode->unlock_key = key_get(key);
247 afs_lock_may_be_available(vnode);
248}
249
250
251
252
253static int afs_do_setlk(struct file *file, struct file_lock *fl)
254{
255 struct inode *inode = file_inode(file);
256 struct afs_vnode *vnode = AFS_FS_I(inode);
257 afs_lock_type_t type;
258 struct key *key = file->private_data;
259 int ret;
260
261 _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
262
263
264 if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
265 return -EINVAL;
266
267 ret = afs_init_lock_manager();
268 if (ret < 0)
269 return ret;
270
271 fl->fl_ops = &afs_lock_ops;
272 INIT_LIST_HEAD(&fl->fl_u.afs.link);
273 fl->fl_u.afs.state = AFS_LOCK_PENDING;
274
275 type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
276
277 spin_lock(&inode->i_lock);
278
279
280
281 ret = afs_vnode_fetch_status(vnode, NULL, key);
282 if (ret < 0)
283 goto error;
284
285 if (vnode->status.lock_count != 0 && !(fl->fl_flags & FL_SLEEP)) {
286 ret = -EAGAIN;
287 goto error;
288 }
289
290 spin_lock(&vnode->lock);
291
292
293
294
295 if (type == AFS_LOCK_READ &&
296 vnode->flags & (1 << AFS_VNODE_READLOCKED)) {
297 _debug("instant readlock");
298 ASSERTCMP(vnode->flags &
299 ((1 << AFS_VNODE_LOCKING) |
300 (1 << AFS_VNODE_WRITELOCKED)), ==, 0);
301 ASSERT(!list_empty(&vnode->granted_locks));
302 goto sharing_existing_lock;
303 }
304
305
306
307 if (list_empty(&vnode->pending_locks) &&
308 list_empty(&vnode->granted_locks)) {
309 _debug("not locked");
310 ASSERTCMP(vnode->flags &
311 ((1 << AFS_VNODE_LOCKING) |
312 (1 << AFS_VNODE_READLOCKED) |
313 (1 << AFS_VNODE_WRITELOCKED)), ==, 0);
314 list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
315 set_bit(AFS_VNODE_LOCKING, &vnode->flags);
316 spin_unlock(&vnode->lock);
317
318 ret = afs_vnode_set_lock(vnode, key, type);
319 clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
320 switch (ret) {
321 case 0:
322 _debug("acquired");
323 goto acquired_server_lock;
324 case -EWOULDBLOCK:
325 _debug("would block");
326 spin_lock(&vnode->lock);
327 ASSERT(list_empty(&vnode->granted_locks));
328 ASSERTCMP(vnode->pending_locks.next, ==,
329 &fl->fl_u.afs.link);
330 goto wait;
331 default:
332 spin_lock(&vnode->lock);
333 list_del_init(&fl->fl_u.afs.link);
334 spin_unlock(&vnode->lock);
335 goto error;
336 }
337 }
338
339
340 _debug("wait local");
341 list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
342wait:
343 if (!(fl->fl_flags & FL_SLEEP)) {
344 _debug("noblock");
345 ret = -EAGAIN;
346 goto abort_attempt;
347 }
348 spin_unlock(&vnode->lock);
349
350
351
352 _debug("sleep");
353 ret = wait_event_interruptible(fl->fl_wait,
354 fl->fl_u.afs.state <= AFS_LOCK_GRANTED);
355 if (fl->fl_u.afs.state <= AFS_LOCK_GRANTED) {
356 ret = fl->fl_u.afs.state;
357 if (ret < 0)
358 goto error;
359 spin_lock(&vnode->lock);
360 goto given_lock;
361 }
362
363
364
365 _debug("intr");
366 ASSERTCMP(ret, ==, -ERESTARTSYS);
367
368 spin_lock(&vnode->lock);
369 if (fl->fl_u.afs.state <= AFS_LOCK_GRANTED) {
370 ret = fl->fl_u.afs.state;
371 if (ret < 0) {
372 spin_unlock(&vnode->lock);
373 goto error;
374 }
375 goto given_lock;
376 }
377
378abort_attempt:
379
380
381 _debug("abort");
382 if (list_empty(&vnode->granted_locks) &&
383 vnode->pending_locks.next == &fl->fl_u.afs.link) {
384 if (vnode->pending_locks.prev != &fl->fl_u.afs.link) {
385
386 list_del_init(&fl->fl_u.afs.link);
387 afs_lock_may_be_available(vnode);
388 }
389 } else {
390 list_del_init(&fl->fl_u.afs.link);
391 }
392 spin_unlock(&vnode->lock);
393 goto error;
394
395acquired_server_lock:
396
397
398 spin_lock(&vnode->lock);
399 afs_schedule_lock_extension(vnode);
400 if (type == AFS_LOCK_READ)
401 set_bit(AFS_VNODE_READLOCKED, &vnode->flags);
402 else
403 set_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
404sharing_existing_lock:
405
406 fl->fl_u.afs.state = AFS_LOCK_GRANTED;
407 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
408given_lock:
409
410 ASSERT(!(vnode->flags & (1 << AFS_VNODE_LOCKING)));
411 ASSERT((vnode->flags & ((1 << AFS_VNODE_READLOCKED) |
412 (1 << AFS_VNODE_WRITELOCKED))) != 0);
413 ret = posix_lock_file(file, fl, NULL);
414 if (ret < 0)
415 goto vfs_rejected_lock;
416 spin_unlock(&vnode->lock);
417
418
419
420
421 afs_vnode_fetch_status(vnode, NULL, key);
422
423error:
424 spin_unlock(&inode->i_lock);
425 _leave(" = %d", ret);
426 return ret;
427
428vfs_rejected_lock:
429
430
431 _debug("vfs refused %d", ret);
432 list_del_init(&fl->fl_u.afs.link);
433 if (list_empty(&vnode->granted_locks))
434 afs_defer_unlock(vnode, key);
435 goto abort_attempt;
436}
437
438
439
440
441static int afs_do_unlk(struct file *file, struct file_lock *fl)
442{
443 struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host);
444 struct key *key = file->private_data;
445 int ret;
446
447 _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
448
449
450 if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
451 return -EINVAL;
452
453 fl->fl_ops = &afs_lock_ops;
454 INIT_LIST_HEAD(&fl->fl_u.afs.link);
455 fl->fl_u.afs.state = AFS_LOCK_PENDING;
456
457 spin_lock(&vnode->lock);
458 ret = posix_lock_file(file, fl, NULL);
459 if (ret < 0) {
460 spin_unlock(&vnode->lock);
461 _leave(" = %d [vfs]", ret);
462 return ret;
463 }
464
465
466 if (list_empty(&vnode->granted_locks))
467 afs_defer_unlock(vnode, key);
468 spin_unlock(&vnode->lock);
469 _leave(" = 0");
470 return 0;
471}
472
473
474
475
476static int afs_do_getlk(struct file *file, struct file_lock *fl)
477{
478 struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host);
479 struct key *key = file->private_data;
480 int ret, lock_count;
481
482 _enter("");
483
484 fl->fl_type = F_UNLCK;
485
486 mutex_lock(&vnode->vfs_inode.i_mutex);
487
488
489 ret = 0;
490 posix_test_lock(file, fl);
491 if (fl->fl_type == F_UNLCK) {
492
493 ret = afs_vnode_fetch_status(vnode, NULL, key);
494 if (ret < 0)
495 goto error;
496 lock_count = vnode->status.lock_count;
497 if (lock_count) {
498 if (lock_count > 0)
499 fl->fl_type = F_RDLCK;
500 else
501 fl->fl_type = F_WRLCK;
502 fl->fl_start = 0;
503 fl->fl_end = OFFSET_MAX;
504 }
505 }
506
507error:
508 mutex_unlock(&vnode->vfs_inode.i_mutex);
509 _leave(" = %d [%hd]", ret, fl->fl_type);
510 return ret;
511}
512
513
514
515
516int afs_lock(struct file *file, int cmd, struct file_lock *fl)
517{
518 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
519
520 _enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
521 vnode->fid.vid, vnode->fid.vnode, cmd,
522 fl->fl_type, fl->fl_flags,
523 (long long) fl->fl_start, (long long) fl->fl_end);
524
525
526 if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
527 return -ENOLCK;
528
529 if (IS_GETLK(cmd))
530 return afs_do_getlk(file, fl);
531 if (fl->fl_type == F_UNLCK)
532 return afs_do_unlk(file, fl);
533 return afs_do_setlk(file, fl);
534}
535
536
537
538
539int afs_flock(struct file *file, int cmd, struct file_lock *fl)
540{
541 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
542
543 _enter("{%x:%u},%d,{t=%x,fl=%x}",
544 vnode->fid.vid, vnode->fid.vnode, cmd,
545 fl->fl_type, fl->fl_flags);
546
547
548
549
550
551
552
553
554 if (!(fl->fl_flags & FL_FLOCK))
555 return -ENOLCK;
556
557
558 if (fl->fl_type == F_UNLCK)
559 return afs_do_unlk(file, fl);
560 return afs_do_setlk(file, fl);
561}
562
563
564
565
566
567
568
569static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
570{
571 _enter("");
572
573 list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
574}
575
576
577
578
579
580static void afs_fl_release_private(struct file_lock *fl)
581{
582 _enter("");
583
584 list_del_init(&fl->fl_u.afs.link);
585}
586