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
17struct workqueue_struct *afs_lock_manager;
18
19static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
20static void afs_fl_release_private(struct file_lock *fl);
21
22static const struct file_lock_operations afs_lock_ops = {
23 .fl_copy_lock = afs_fl_copy_lock,
24 .fl_release_private = afs_fl_release_private,
25};
26
27
28
29
30void afs_lock_may_be_available(struct afs_vnode *vnode)
31{
32 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
33
34 queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
35}
36
37
38
39
40
41static void afs_schedule_lock_extension(struct afs_vnode *vnode)
42{
43 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
44 AFS_LOCKWAIT * HZ / 2);
45}
46
47
48
49
50
51
52static void afs_grant_locks(struct afs_vnode *vnode, struct file_lock *fl)
53{
54 struct file_lock *p, *_p;
55
56 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
57 if (fl->fl_type == F_RDLCK) {
58 list_for_each_entry_safe(p, _p, &vnode->pending_locks,
59 fl_u.afs.link) {
60 if (p->fl_type == F_RDLCK) {
61 p->fl_u.afs.state = AFS_LOCK_GRANTED;
62 list_move_tail(&p->fl_u.afs.link,
63 &vnode->granted_locks);
64 wake_up(&p->fl_wait);
65 }
66 }
67 }
68}
69
70
71
72
73static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
74 afs_lock_type_t type)
75{
76 struct afs_fs_cursor fc;
77 int ret;
78
79 _enter("%s{%x:%u.%u},%x,%u",
80 vnode->volume->name,
81 vnode->fid.vid,
82 vnode->fid.vnode,
83 vnode->fid.unique,
84 key_serial(key), type);
85
86 ret = -ERESTARTSYS;
87 if (afs_begin_vnode_operation(&fc, vnode, key)) {
88 while (afs_select_fileserver(&fc)) {
89 fc.cb_break = afs_calc_vnode_cb_break(vnode);
90 afs_fs_set_lock(&fc, type);
91 }
92
93 afs_check_for_remote_deletion(&fc, fc.vnode);
94 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
95 ret = afs_end_vnode_operation(&fc);
96 }
97
98 _leave(" = %d", ret);
99 return ret;
100}
101
102
103
104
105static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
106{
107 struct afs_fs_cursor fc;
108 int ret;
109
110 _enter("%s{%x:%u.%u},%x",
111 vnode->volume->name,
112 vnode->fid.vid,
113 vnode->fid.vnode,
114 vnode->fid.unique,
115 key_serial(key));
116
117 ret = -ERESTARTSYS;
118 if (afs_begin_vnode_operation(&fc, vnode, key)) {
119 while (afs_select_current_fileserver(&fc)) {
120 fc.cb_break = afs_calc_vnode_cb_break(vnode);
121 afs_fs_extend_lock(&fc);
122 }
123
124 afs_check_for_remote_deletion(&fc, fc.vnode);
125 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
126 ret = afs_end_vnode_operation(&fc);
127 }
128
129 _leave(" = %d", ret);
130 return ret;
131}
132
133
134
135
136static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
137{
138 struct afs_fs_cursor fc;
139 int ret;
140
141 _enter("%s{%x:%u.%u},%x",
142 vnode->volume->name,
143 vnode->fid.vid,
144 vnode->fid.vnode,
145 vnode->fid.unique,
146 key_serial(key));
147
148 ret = -ERESTARTSYS;
149 if (afs_begin_vnode_operation(&fc, vnode, key)) {
150 while (afs_select_current_fileserver(&fc)) {
151 fc.cb_break = afs_calc_vnode_cb_break(vnode);
152 afs_fs_release_lock(&fc);
153 }
154
155 afs_check_for_remote_deletion(&fc, fc.vnode);
156 afs_vnode_commit_status(&fc, vnode, fc.cb_break);
157 ret = afs_end_vnode_operation(&fc);
158 }
159
160 _leave(" = %d", ret);
161 return ret;
162}
163
164
165
166
167
168
169void afs_lock_work(struct work_struct *work)
170{
171 struct afs_vnode *vnode =
172 container_of(work, struct afs_vnode, lock_work.work);
173 struct file_lock *fl, *next;
174 afs_lock_type_t type;
175 struct key *key;
176 int ret;
177
178 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
179
180 spin_lock(&vnode->lock);
181
182again:
183 _debug("wstate %u for %p", vnode->lock_state, vnode);
184 switch (vnode->lock_state) {
185 case AFS_VNODE_LOCK_NEED_UNLOCK:
186 _debug("unlock");
187 vnode->lock_state = AFS_VNODE_LOCK_UNLOCKING;
188 spin_unlock(&vnode->lock);
189
190
191
192 ret = afs_release_lock(vnode, vnode->lock_key);
193 if (ret < 0)
194 printk(KERN_WARNING "AFS:"
195 " Failed to release lock on {%x:%x} error %d\n",
196 vnode->fid.vid, vnode->fid.vnode, ret);
197
198 spin_lock(&vnode->lock);
199 key_put(vnode->lock_key);
200 vnode->lock_key = NULL;
201 vnode->lock_state = AFS_VNODE_LOCK_NONE;
202
203 if (list_empty(&vnode->pending_locks)) {
204 spin_unlock(&vnode->lock);
205 return;
206 }
207
208
209 next = list_entry(vnode->pending_locks.next,
210 struct file_lock, fl_u.afs.link);
211 vnode->lock_key = afs_file_key(next->fl_file);
212 vnode->lock_type = (next->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
213 vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
214 goto again;
215
216
217
218
219 case AFS_VNODE_LOCK_GRANTED:
220 _debug("extend");
221
222 ASSERT(!list_empty(&vnode->granted_locks));
223
224 key = key_get(vnode->lock_key);
225 vnode->lock_state = AFS_VNODE_LOCK_EXTENDING;
226 spin_unlock(&vnode->lock);
227
228 ret = afs_extend_lock(vnode, key);
229 key_put(key);
230
231 if (ret < 0)
232 pr_warning("AFS: Failed to extend lock on {%x:%x} error %d\n",
233 vnode->fid.vid, vnode->fid.vnode, ret);
234
235 spin_lock(&vnode->lock);
236
237 if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
238 goto again;
239 vnode->lock_state = AFS_VNODE_LOCK_GRANTED;
240
241 if (ret == 0)
242 afs_schedule_lock_extension(vnode);
243 else
244 queue_delayed_work(afs_lock_manager, &vnode->lock_work,
245 HZ * 10);
246 spin_unlock(&vnode->lock);
247 _leave(" [ext]");
248 return;
249
250
251
252
253
254 case AFS_VNODE_LOCK_WAITING_FOR_CB:
255 _debug("get");
256
257 key = key_get(vnode->lock_key);
258 type = vnode->lock_type;
259 vnode->lock_state = AFS_VNODE_LOCK_SETTING;
260 spin_unlock(&vnode->lock);
261
262 ret = afs_set_lock(vnode, key, type);
263 key_put(key);
264
265 spin_lock(&vnode->lock);
266 switch (ret) {
267 case -EWOULDBLOCK:
268 _debug("blocked");
269 break;
270 case 0:
271 _debug("acquired");
272 vnode->lock_state = AFS_VNODE_LOCK_GRANTED;
273
274 default:
275
276
277
278
279
280 if (list_empty(&vnode->pending_locks)) {
281 _debug("withdrawn");
282 vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
283 goto again;
284 }
285
286 fl = list_entry(vnode->pending_locks.next,
287 struct file_lock, fl_u.afs.link);
288 type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
289 if (vnode->lock_type != type) {
290 _debug("changed");
291 vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
292 goto again;
293 }
294
295 fl->fl_u.afs.state = ret;
296 if (ret == 0)
297 afs_grant_locks(vnode, fl);
298 else
299 list_del_init(&fl->fl_u.afs.link);
300 wake_up(&fl->fl_wait);
301 spin_unlock(&vnode->lock);
302 _leave(" [granted]");
303 return;
304 }
305
306 default:
307
308 spin_unlock(&vnode->lock);
309 _leave(" [no]");
310 return;
311 }
312}
313
314
315
316
317
318
319
320static void afs_defer_unlock(struct afs_vnode *vnode)
321{
322 _enter("");
323
324 if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
325 vnode->lock_state == AFS_VNODE_LOCK_EXTENDING) {
326 cancel_delayed_work(&vnode->lock_work);
327
328 vnode->lock_state = AFS_VNODE_LOCK_NEED_UNLOCK;
329 afs_lock_may_be_available(vnode);
330 }
331}
332
333
334
335
336
337static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
338 afs_lock_type_t type, bool can_sleep)
339{
340 afs_access_t access;
341 int ret;
342
343
344
345
346 ret = afs_validate(vnode, key);
347 if (ret < 0)
348 return ret;
349
350
351
352
353 ret = afs_check_permit(vnode, key, &access);
354 if (ret < 0)
355 return ret;
356
357
358
359
360
361
362
363 if (type == AFS_LOCK_READ) {
364 if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
365 return -EACCES;
366 if (vnode->status.lock_count == -1 && !can_sleep)
367 return -EAGAIN;
368 } else {
369 if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
370 return -EACCES;
371 if (vnode->status.lock_count != 0 && !can_sleep)
372 return -EAGAIN;
373 }
374
375 return 0;
376}
377
378
379
380
381
382static void afs_dequeue_lock(struct afs_vnode *vnode, struct file_lock *fl)
383{
384 struct file_lock *next;
385
386 _enter("");
387
388
389
390
391
392 if (vnode->granted_locks.next == &fl->fl_u.afs.link &&
393 vnode->granted_locks.prev == &fl->fl_u.afs.link) {
394 list_del_init(&fl->fl_u.afs.link);
395 afs_defer_unlock(vnode);
396 return;
397 }
398
399 if (!list_empty(&vnode->granted_locks) ||
400 vnode->pending_locks.next != &fl->fl_u.afs.link) {
401 list_del_init(&fl->fl_u.afs.link);
402 return;
403 }
404
405 list_del_init(&fl->fl_u.afs.link);
406 key_put(vnode->lock_key);
407 vnode->lock_key = NULL;
408 vnode->lock_state = AFS_VNODE_LOCK_NONE;
409
410 if (list_empty(&vnode->pending_locks))
411 return;
412
413
414 next = list_entry(vnode->pending_locks.next,
415 struct file_lock, fl_u.afs.link);
416 vnode->lock_key = afs_file_key(next->fl_file);
417 vnode->lock_type = (next->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
418 vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
419 afs_lock_may_be_available(vnode);
420}
421
422
423
424
425static int afs_do_setlk(struct file *file, struct file_lock *fl)
426{
427 struct inode *inode = locks_inode(file);
428 struct afs_vnode *vnode = AFS_FS_I(inode);
429 afs_lock_type_t type;
430 struct key *key = afs_file_key(file);
431 int ret;
432
433 _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
434
435
436 if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
437 return -EINVAL;
438
439 fl->fl_ops = &afs_lock_ops;
440 INIT_LIST_HEAD(&fl->fl_u.afs.link);
441 fl->fl_u.afs.state = AFS_LOCK_PENDING;
442
443 type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
444
445 ret = afs_do_setlk_check(vnode, key, type, fl->fl_flags & FL_SLEEP);
446 if (ret < 0)
447 return ret;
448
449 spin_lock(&vnode->lock);
450
451
452
453
454
455 if (type == AFS_LOCK_READ &&
456 vnode->lock_state == AFS_VNODE_LOCK_GRANTED &&
457 vnode->lock_type == AFS_LOCK_READ) {
458 _debug("instant readlock");
459 ASSERT(!list_empty(&vnode->granted_locks));
460 goto share_existing_lock;
461 }
462
463 list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
464
465 if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
466 goto need_to_wait;
467
468
469
470
471
472
473
474
475
476 _debug("not locked");
477 vnode->lock_key = key_get(key);
478 vnode->lock_type = type;
479 vnode->lock_state = AFS_VNODE_LOCK_SETTING;
480 spin_unlock(&vnode->lock);
481
482 ret = afs_set_lock(vnode, key, type);
483
484 spin_lock(&vnode->lock);
485 switch (ret) {
486 default:
487 goto abort_attempt;
488
489 case -EWOULDBLOCK:
490
491
492
493
494 _debug("would block");
495 ASSERT(list_empty(&vnode->granted_locks));
496 ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
497 vnode->lock_state = AFS_VNODE_LOCK_WAITING_FOR_CB;
498 goto need_to_wait;
499
500 case 0:
501 _debug("acquired");
502 break;
503 }
504
505
506
507 vnode->lock_state = AFS_VNODE_LOCK_GRANTED;
508 afs_schedule_lock_extension(vnode);
509
510share_existing_lock:
511
512 fl->fl_u.afs.state = AFS_LOCK_GRANTED;
513 list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
514
515given_lock:
516
517 spin_unlock(&vnode->lock);
518
519 ret = posix_lock_file(file, fl, NULL);
520 if (ret < 0)
521 goto vfs_rejected_lock;
522
523
524
525
526
527 afs_validate(vnode, key);
528 _leave(" = 0");
529 return 0;
530
531need_to_wait:
532
533
534
535
536
537 ret = -EAGAIN;
538 if (fl->fl_flags & FL_SLEEP) {
539 spin_unlock(&vnode->lock);
540
541 _debug("sleep");
542 ret = wait_event_interruptible(fl->fl_wait,
543 fl->fl_u.afs.state != AFS_LOCK_PENDING);
544
545 spin_lock(&vnode->lock);
546 }
547
548 if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
549 goto given_lock;
550 if (fl->fl_u.afs.state < 0)
551 ret = fl->fl_u.afs.state;
552
553abort_attempt:
554
555
556 _debug("abort");
557 afs_dequeue_lock(vnode, fl);
558
559error_unlock:
560 spin_unlock(&vnode->lock);
561 _leave(" = %d", ret);
562 return ret;
563
564vfs_rejected_lock:
565
566
567
568
569 _debug("vfs refused %d", ret);
570 spin_lock(&vnode->lock);
571 list_del_init(&fl->fl_u.afs.link);
572 if (list_empty(&vnode->granted_locks))
573 afs_defer_unlock(vnode);
574 goto error_unlock;
575}
576
577
578
579
580static int afs_do_unlk(struct file *file, struct file_lock *fl)
581{
582 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
583 int ret;
584
585 _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
586
587
588 vfs_fsync(file, 0);
589
590
591 if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
592 return -EINVAL;
593
594 ret = posix_lock_file(file, fl, NULL);
595 _leave(" = %d [%u]", ret, vnode->lock_state);
596 return ret;
597}
598
599
600
601
602static int afs_do_getlk(struct file *file, struct file_lock *fl)
603{
604 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
605 struct key *key = afs_file_key(file);
606 int ret, lock_count;
607
608 _enter("");
609
610 fl->fl_type = F_UNLCK;
611
612
613 posix_test_lock(file, fl);
614 if (fl->fl_type == F_UNLCK) {
615
616 ret = afs_fetch_status(vnode, key, false);
617 if (ret < 0)
618 goto error;
619
620 lock_count = READ_ONCE(vnode->status.lock_count);
621 if (lock_count > 0)
622 fl->fl_type = F_RDLCK;
623 else
624 fl->fl_type = F_WRLCK;
625 fl->fl_start = 0;
626 fl->fl_end = OFFSET_MAX;
627 }
628
629 ret = 0;
630error:
631 _leave(" = %d [%hd]", ret, fl->fl_type);
632 return ret;
633}
634
635
636
637
638int afs_lock(struct file *file, int cmd, struct file_lock *fl)
639{
640 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
641
642 _enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
643 vnode->fid.vid, vnode->fid.vnode, cmd,
644 fl->fl_type, fl->fl_flags,
645 (long long) fl->fl_start, (long long) fl->fl_end);
646
647
648 if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
649 return -ENOLCK;
650
651 if (IS_GETLK(cmd))
652 return afs_do_getlk(file, fl);
653 if (fl->fl_type == F_UNLCK)
654 return afs_do_unlk(file, fl);
655 return afs_do_setlk(file, fl);
656}
657
658
659
660
661int afs_flock(struct file *file, int cmd, struct file_lock *fl)
662{
663 struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
664
665 _enter("{%x:%u},%d,{t=%x,fl=%x}",
666 vnode->fid.vid, vnode->fid.vnode, cmd,
667 fl->fl_type, fl->fl_flags);
668
669
670
671
672
673
674
675
676 if (!(fl->fl_flags & FL_FLOCK))
677 return -ENOLCK;
678
679
680 if (fl->fl_type == F_UNLCK)
681 return afs_do_unlk(file, fl);
682 return afs_do_setlk(file, fl);
683}
684
685
686
687
688
689
690
691static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
692{
693 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
694
695 _enter("");
696
697 spin_lock(&vnode->lock);
698 list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
699 spin_unlock(&vnode->lock);
700}
701
702
703
704
705
706static void afs_fl_release_private(struct file_lock *fl)
707{
708 struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
709
710 _enter("");
711
712 spin_lock(&vnode->lock);
713 afs_dequeue_lock(vnode, fl);
714 _debug("state %u for %p", vnode->lock_state, vnode);
715 spin_unlock(&vnode->lock);
716}
717