This source file includes following definitions.
- locks_free_lock
- locks_insert_block
- locks_delete_block
- sys_flock
- fcntl_getlk
- fcntl_setlk
- locks_remove_locks
- posix_make_lock
- flock_make_lock
- posix_locks_conflict
- flock_locks_conflict
- locks_conflict
- locks_overlap
- posix_locks_deadlock
- flock_lock_file
- posix_lock_file
- locks_alloc_lock
- locks_insert_lock
- locks_delete_lock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 #include <asm/segment.h>
65
66 #include <linux/malloc.h>
67 #include <linux/sched.h>
68 #include <linux/kernel.h>
69 #include <linux/errno.h>
70 #include <linux/stat.h>
71 #include <linux/fcntl.h>
72
73
74 #define OFFSET_MAX ((off_t)0x7fffffff)
75
76 static int flock_make_lock(struct file *filp, struct file_lock *fl,
77 unsigned int cmd);
78 static int posix_make_lock(struct file *filp, struct file_lock *fl,
79 struct flock *l);
80 static int flock_locks_conflict(struct file_lock *caller_fl,
81 struct file_lock *sys_fl);
82 static int posix_locks_conflict(struct file_lock *caller_fl,
83 struct file_lock *sys_fl);
84 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
85 static int flock_lock_file(struct file *filp, struct file_lock *caller,
86 unsigned int wait);
87 static int posix_lock_file(struct file *filp, struct file_lock *caller,
88 unsigned int wait);
89 static int posix_locks_deadlock(struct task_struct *my_task,
90 struct task_struct *blocked_task);
91 static int locks_overlap(struct file_lock *fl1, struct file_lock *fl2);
92
93 static struct file_lock *locks_alloc_lock(struct file_lock *fl);
94 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
95 static void locks_delete_lock(struct file_lock **fl, unsigned int wait);
96
97 static struct file_lock *file_lock_table = NULL;
98
99
100 static inline void locks_free_lock(struct file_lock **fl)
101 {
102 kfree(*fl);
103 *fl = NULL;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 static inline void locks_insert_block(struct file_lock **block,
121 struct file_lock *fl)
122 {
123 struct file_lock *bfl;
124
125 while ((bfl = *block) != NULL) {
126 block = &bfl->fl_block;
127 }
128
129 *block = fl;
130 fl->fl_block = NULL;
131
132 return;
133 }
134
135 static inline void locks_delete_block(struct file_lock **block,
136 struct file_lock *fl)
137 {
138 struct file_lock *bfl;
139
140 while ((bfl = *block) != NULL) {
141 if (bfl == fl) {
142 *block = fl->fl_block;
143 fl->fl_block = NULL;
144 return;
145 }
146 block = &bfl->fl_block;
147 }
148 }
149
150
151
152
153 asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
154 {
155 struct file_lock file_lock;
156 struct file *filp;
157
158 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
159 return (-EBADF);
160
161 if (!flock_make_lock(filp, &file_lock, cmd))
162 return (-EINVAL);
163
164 if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
165 return (-EBADF);
166
167 return (flock_lock_file(filp, &file_lock, cmd & LOCK_UN ? 0 : cmd & LOCK_NB ? 0 : 1));
168 }
169
170
171
172
173 int fcntl_getlk(unsigned int fd, struct flock *l)
174 {
175 int error;
176 struct flock flock;
177 struct file *filp;
178 struct file_lock *fl,file_lock;
179
180 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
181 return (-EBADF);
182 error = verify_area(VERIFY_WRITE, l, sizeof(*l));
183 if (error)
184 return (error);
185
186 memcpy_fromfs(&flock, l, sizeof(flock));
187 if ((flock.l_type == F_UNLCK) || (flock.l_type == F_EXLCK) ||
188 (flock.l_type == F_SHLCK))
189 return (-EINVAL);
190
191 if (!posix_make_lock(filp, &file_lock, &flock))
192 return (-EINVAL);
193
194 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
195 if (posix_locks_conflict(&file_lock, fl)) {
196 flock.l_pid = fl->fl_owner->pid;
197 flock.l_start = fl->fl_start;
198 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
199 fl->fl_end - fl->fl_start + 1;
200 flock.l_whence = 0;
201 flock.l_type = fl->fl_type;
202 memcpy_tofs(l, &flock, sizeof(flock));
203 return (0);
204 }
205 }
206
207 flock.l_type = F_UNLCK;
208 memcpy_tofs(l, &flock, sizeof(flock));
209 return (0);
210 }
211
212
213
214
215
216 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
217 {
218 int error;
219 struct file *filp;
220 struct file_lock file_lock;
221 struct flock flock;
222
223
224
225
226
227 if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
228 return (-EBADF);
229
230 error = verify_area(VERIFY_READ, l, sizeof(*l));
231 if (error)
232 return (error);
233
234 memcpy_fromfs(&flock, l, sizeof(flock));
235 if (!posix_make_lock(filp, &file_lock, &flock))
236 return (-EINVAL);
237
238 switch (flock.l_type) {
239 case F_RDLCK :
240 if (!(filp->f_mode & 1))
241 return -EBADF;
242 break;
243 case F_WRLCK :
244 if (!(filp->f_mode & 2))
245 return -EBADF;
246 break;
247 case F_SHLCK :
248 case F_EXLCK :
249 if (!(filp->f_mode & 3))
250 return -EBADF;
251 break;
252 case F_UNLCK :
253 break;
254 }
255
256 return (posix_lock_file(filp, &file_lock, cmd == F_SETLKW));
257 }
258
259
260
261 void locks_remove_locks(struct task_struct *task, struct file *filp)
262 {
263 struct file_lock *fl;
264 struct file_lock **before;
265
266
267
268
269
270 before = &filp->f_inode->i_flock;
271 while ((fl = *before) != NULL) {
272 if (((fl->fl_flags == F_POSIX) && (fl->fl_owner == task)) ||
273 ((fl->fl_flags == F_FLOCK) && (fl->fl_file == filp) &&
274 (filp->f_count == 1)))
275 locks_delete_lock(before, 0);
276 else
277 before = &fl->fl_next;
278 }
279
280 return;
281 }
282
283
284
285
286 static int posix_make_lock(struct file *filp, struct file_lock *fl,
287 struct flock *l)
288 {
289 off_t start;
290
291 if (!filp->f_inode)
292 return (0);
293
294 switch (l->l_type) {
295 case F_RDLCK :
296 case F_WRLCK :
297 case F_UNLCK :
298 fl->fl_type = l->l_type;
299 break;
300 case F_SHLCK :
301 fl->fl_type = F_RDLCK;
302 break;
303 case F_EXLCK :
304 fl->fl_type = F_WRLCK;
305 break;
306 default :
307 return (0);
308 }
309
310 switch (l->l_whence) {
311 case 0 :
312 start = 0;
313 break;
314 case 1 :
315 start = filp->f_pos;
316 break;
317 case 2 :
318 start = filp->f_inode->i_size;
319 break;
320 default :
321 return (0);
322 }
323
324 if (((start += l->l_start) < 0) || (l->l_len < 0))
325 return (0);
326 fl->fl_start = start;
327 if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
328 fl->fl_end = OFFSET_MAX;
329
330 fl->fl_flags = F_POSIX;
331 fl->fl_file = filp;
332 fl->fl_owner = current;
333 fl->fl_wait = NULL;
334
335 return (1);
336 }
337
338
339
340
341 static int flock_make_lock(struct file *filp, struct file_lock *fl,
342 unsigned int cmd)
343 {
344 if (!filp->f_inode)
345 return (0);
346
347 switch (cmd & ~LOCK_NB) {
348 case LOCK_SH :
349 fl->fl_type = F_RDLCK;
350 break;
351 case LOCK_EX :
352 fl->fl_type = F_WRLCK;
353 break;
354 case LOCK_UN :
355 fl->fl_type = F_UNLCK;
356 break;
357 default :
358 return (0);
359 }
360
361 fl->fl_flags = F_FLOCK;
362 fl->fl_start = 0;
363 fl->fl_end = OFFSET_MAX;
364 fl->fl_file = filp;
365 fl->fl_owner = current;
366 fl->fl_wait = NULL;
367
368 return (1);
369 }
370
371
372
373
374 static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
375 {
376
377
378
379 if ((sys_fl->fl_flags == F_POSIX) &&
380 (caller_fl->fl_owner == sys_fl->fl_owner))
381 return (0);
382
383 return (locks_conflict(caller_fl, sys_fl));
384 }
385
386
387
388
389 static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
390 {
391
392
393
394 if ((sys_fl->fl_flags == F_FLOCK) &&
395 (caller_fl->fl_file == sys_fl->fl_file))
396 return (0);
397
398 return (locks_conflict(caller_fl, sys_fl));
399 }
400
401
402
403
404 static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
405 {
406 if (!locks_overlap(caller_fl, sys_fl))
407 return (0);
408
409 switch (caller_fl->fl_type) {
410 case F_RDLCK :
411 return (sys_fl->fl_type == F_WRLCK);
412
413 case F_WRLCK :
414 return (1);
415
416 default:
417 printk("locks_conflict(): impossible lock type - %d\n",
418 caller_fl->fl_type);
419 break;
420 }
421 return (0);
422 }
423
424
425
426 static int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
427 {
428 return ((fl1->fl_end >= fl2->fl_start) &&
429 (fl2->fl_end >= fl1->fl_start));
430 }
431
432
433
434
435
436
437
438
439
440
441
442 static int posix_locks_deadlock(struct task_struct *my_task,
443 struct task_struct *blocked_task)
444 {
445 struct wait_queue *dlock_wait;
446 struct file_lock *fl;
447
448 for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
449 if (fl->fl_owner == NULL)
450 continue;
451 if (fl->fl_owner != my_task)
452 continue;
453 if (fl->fl_wait == NULL)
454 continue;
455 dlock_wait = fl->fl_wait;
456 do {
457 if (dlock_wait->task != NULL) {
458 if (dlock_wait->task == blocked_task)
459 return (-EDEADLOCK);
460 if (posix_locks_deadlock(dlock_wait->task, blocked_task))
461 return (-EDEADLOCK);
462 }
463 dlock_wait = dlock_wait->next;
464 } while (dlock_wait != fl->fl_wait);
465 }
466 return (0);
467 }
468
469
470
471
472
473 static int flock_lock_file(struct file *filp, struct file_lock *caller,
474 unsigned int wait)
475 {
476 struct file_lock *fl;
477 struct file_lock *new_fl;
478 struct file_lock **before;
479 int change = 0;
480
481
482
483
484 before = &filp->f_inode->i_flock;
485 while ((fl = *before) && (fl->fl_flags == F_FLOCK)) {
486 if (caller->fl_file == fl->fl_file) {
487 if (caller->fl_type == fl->fl_type)
488 return (0);
489 change = 1;
490 break;
491 }
492 before = &fl->fl_next;
493 }
494
495
496
497 if (change)
498 locks_delete_lock(before, caller->fl_type != F_UNLCK);
499 if (caller->fl_type == F_UNLCK)
500 return (0);
501 if ((new_fl = locks_alloc_lock(caller)) == NULL)
502 return (-ENOLCK);
503 repeat:
504 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
505 if (!flock_locks_conflict(new_fl, fl))
506 continue;
507
508 if (wait) {
509 if (current->signal & ~current->blocked) {
510
511
512
513
514
515 locks_free_lock(&new_fl);
516 return (-ERESTARTSYS);
517 }
518 locks_insert_block(&fl->fl_block, new_fl);
519 interruptible_sleep_on(&new_fl->fl_wait);
520 wake_up(&new_fl->fl_wait);
521 if (current->signal & ~current->blocked) {
522
523
524
525
526
527
528
529 locks_delete_block(&fl->fl_block, new_fl);
530 locks_free_lock(&new_fl);
531 return (-ERESTARTSYS);
532 }
533 goto repeat;
534 }
535
536 locks_free_lock(&new_fl);
537 return (-EAGAIN);
538 }
539 locks_insert_lock(&filp->f_inode->i_flock, new_fl);
540 return (0);
541 }
542
543
544
545
546
547
548
549
550
551
552
553
554
555 static int posix_lock_file(struct file *filp, struct file_lock *caller,
556 unsigned int wait)
557 {
558 struct file_lock *fl;
559 struct file_lock *new_fl;
560 struct file_lock *left = NULL;
561 struct file_lock *right = NULL;
562 struct file_lock **before;
563 int added = 0;
564
565 if (caller->fl_type != F_UNLCK) {
566 repeat:
567 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
568 if (!posix_locks_conflict(caller, fl))
569 continue;
570 if (wait) {
571 if (current->signal & ~current->blocked)
572 return (-ERESTARTSYS);
573 if (fl->fl_flags == F_POSIX)
574 if (posix_locks_deadlock(caller->fl_owner, fl->fl_owner))
575 return (-EDEADLOCK);
576 interruptible_sleep_on(&fl->fl_wait);
577 if (current->signal & ~current->blocked)
578 return (-ERESTARTSYS);
579 goto repeat;
580 }
581 return (-EAGAIN);
582 }
583 }
584
585
586
587
588 before = &filp->f_inode->i_flock;
589
590
591
592 while ((fl = *before) && ((fl->fl_flags == F_FLOCK) ||
593 (caller->fl_owner != fl->fl_owner))) {
594 before = &fl->fl_next;
595 }
596
597
598
599
600 while ((fl = *before) && (caller->fl_owner == fl->fl_owner)) {
601
602
603 if (caller->fl_type == fl->fl_type) {
604 if (fl->fl_end < caller->fl_start - 1)
605 goto next_lock;
606
607
608
609 if (fl->fl_start > caller->fl_end + 1)
610 break;
611
612
613
614
615
616
617 if (fl->fl_start > caller->fl_start)
618 fl->fl_start = caller->fl_start;
619 else
620 caller->fl_start = fl->fl_start;
621 if (fl->fl_end < caller->fl_end)
622 fl->fl_end = caller->fl_end;
623 else
624 caller->fl_end = fl->fl_end;
625 if (added) {
626 locks_delete_lock(before, 0);
627 continue;
628 }
629 caller = fl;
630 added = 1;
631 goto next_lock;
632 }
633
634
635 if (fl->fl_end < caller->fl_start)
636 goto next_lock;
637 if (fl->fl_start > caller->fl_end)
638 break;
639 if (caller->fl_type == F_UNLCK)
640 added = 1;
641 if (fl->fl_start < caller->fl_start)
642 left = fl;
643
644
645
646 if (fl->fl_end > caller->fl_end) {
647 right = fl;
648 break;
649 }
650 if (fl->fl_start >= caller->fl_start) {
651
652
653
654 if (added) {
655 locks_delete_lock(before, 0);
656 continue;
657 }
658
659
660
661
662 wake_up(&fl->fl_wait);
663 fl->fl_start = caller->fl_start;
664 fl->fl_end = caller->fl_end;
665 fl->fl_type = caller->fl_type;
666 caller = fl;
667 added = 1;
668 }
669
670
671 next_lock:
672 before = &(*before)->fl_next;
673 }
674
675
676
677
678
679
680
681
682
683
684
685
686
687 if (!added) {
688 if (caller->fl_type == F_UNLCK)
689 return (0);
690 if ((new_fl = locks_alloc_lock(caller)) == NULL)
691 return (-ENOLCK);
692 locks_insert_lock(before, new_fl);
693
694 }
695 if (right) {
696 if (left == right) {
697
698
699
700
701 if ((left = locks_alloc_lock(right)) == NULL) {
702 if (!added)
703 locks_delete_lock(before, 0);
704 return (-ENOLCK);
705 }
706 locks_insert_lock(before, left);
707 }
708 right->fl_start = caller->fl_end + 1;
709 }
710 if (left)
711 left->fl_end = caller->fl_start - 1;
712 return (0);
713 }
714
715
716
717
718
719
720 static struct file_lock *locks_alloc_lock(struct file_lock *fl)
721 {
722 struct file_lock *tmp;
723
724
725 if ((tmp = (struct file_lock *)kmalloc(sizeof(struct file_lock),
726 GFP_KERNEL)) == NULL)
727 return (tmp);
728
729 tmp->fl_nextlink = NULL;
730 tmp->fl_prevlink = NULL;
731 tmp->fl_next = NULL;
732 tmp->fl_block = NULL;
733 tmp->fl_flags = fl->fl_flags;
734 tmp->fl_owner = fl->fl_owner;
735 tmp->fl_file = fl->fl_file;
736 tmp->fl_wait = NULL;
737 tmp->fl_type = fl->fl_type;
738 tmp->fl_start = fl->fl_start;
739 tmp->fl_end = fl->fl_end;
740
741 return (tmp);
742 }
743
744
745
746
747
748 static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
749 {
750 fl->fl_nextlink = file_lock_table;
751 fl->fl_prevlink = NULL;
752 if (file_lock_table != NULL)
753 file_lock_table->fl_prevlink = fl;
754 file_lock_table = fl;
755 fl->fl_next = *pos;
756 *pos = fl;
757
758 return;
759 }
760
761
762
763
764
765
766
767
768
769 static void locks_delete_lock(struct file_lock **fl_p, unsigned int wait)
770 {
771 struct file_lock *fl;
772 struct file_lock *bfl;
773
774 fl = *fl_p;
775 *fl_p = (*fl_p)->fl_next;
776
777 if (fl->fl_nextlink != NULL)
778 fl->fl_nextlink->fl_prevlink = fl->fl_prevlink;
779
780 if (fl->fl_prevlink != NULL)
781 fl->fl_prevlink->fl_nextlink = fl->fl_nextlink;
782 else {
783 file_lock_table = fl->fl_nextlink;
784 }
785
786 while ((bfl = fl->fl_block) != NULL) {
787 fl->fl_block = bfl->fl_block;
788 bfl->fl_block = NULL;
789 wake_up(&bfl->fl_wait);
790 if (wait)
791 sleep_on(&bfl->fl_wait);
792 }
793
794 wake_up(&fl->fl_wait);
795 kfree(fl);
796
797 return;
798 }