OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [locks.c] - Blame information for rev 1627

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1627 jcastillo
/*
2
 *  linux/fs/locks.c
3
 *
4
 *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
5
 *  Doug Evans (dje@spiff.uucp), August 07, 1992
6
 *
7
 *  Deadlock detection added.
8
 *  FIXME: one thing isn't handled yet:
9
 *      - mandatory locks (requires lots of changes elsewhere)
10
 *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
11
 *
12
 *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
13
 *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
14
 *
15
 *  Converted file_lock_table to a linked list from an array, which eliminates
16
 *  the limits on how many active file locks are open.
17
 *  Chad Page (pageone@netcom.com), November 27, 1994
18
 *
19
 *  Removed dependency on file descriptors. dup()'ed file descriptors now
20
 *  get the same locks as the original file descriptors, and a close() on
21
 *  any file descriptor removes ALL the locks on the file for the current
22
 *  process. Since locks still depend on the process id, locks are inherited
23
 *  after an exec() but not after a fork(). This agrees with POSIX, and both
24
 *  BSD and SVR4 practice.
25
 *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
26
 *
27
 *  Scrapped free list which is redundant now that we allocate locks
28
 *  dynamically with kmalloc()/kfree().
29
 *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
30
 *
31
 *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
32
 *
33
 *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
34
 *  fcntl() system call. They have the semantics described above.
35
 *
36
 *  FL_FLOCK locks are created with calls to flock(), through the flock()
37
 *  system call, which is new. Old C libraries implement flock() via fcntl()
38
 *  and will continue to use the old, broken implementation.
39
 *
40
 *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
41
 *  with a file pointer (filp). As a result they can be shared by a parent
42
 *  process and its children after a fork(). They are removed when the last
43
 *  file descriptor referring to the file pointer is closed (unless explicitly
44
 *  unlocked).
45
 *
46
 *  FL_FLOCK locks never deadlock, an existing lock is always removed before
47
 *  upgrading from shared to exclusive (or vice versa). When this happens
48
 *  any processes blocked by the current lock are woken up and allowed to
49
 *  run before the new lock is applied.
50
 *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
51
 *
52
 *  Removed some race conditions in flock_lock_file(), marked other possible
53
 *  races. Just grep for FIXME to see them.
54
 *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
55
 *
56
 *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
57
 *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
58
 *  once we've checked for blocking and deadlocking.
59
 *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
60
 *
61
 *  Initial implementation of mandatory locks. SunOS turned out to be
62
 *  a rotten model, so I implemented the "obvious" semantics.
63
 *  See 'linux/Documentation/mandatory.txt' for details.
64
 *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
65
 *
66
 *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
67
 *  check if a file has mandatory locks, used by mmap(), open() and creat() to
68
 *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
69
 *  Manual, Section 2.
70
 *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
71
 *
72
 *  Tidied up block list handling. Added '/proc/locks' interface.
73
 *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
74
 *
75
 *  Fixed deadlock condition for pathological code that mixes calls to
76
 *  flock() and fcntl().
77
 *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
78
 *
79
 *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
80
 *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
81
 *  guarantee sensible behaviour in the case where file system modules might
82
 *  be compiled with different options than the kernel itself.
83
 *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
84
 *
85
 *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
86
 *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
87
 *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
88
 *
89
 *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
90
 *  locks. Changed process synchronisation to avoid dereferencing locks that
91
 *  have already been freed.
92
 *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
93
 *
94
 *  Made the block list a circular list to minimise searching in the list.
95
 *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
96
 *
97
 *  Made mandatory locking a mount option. Default is not to allow mandatory
98
 *  locking.
99
 *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
100
 *
101
 *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
102
 *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
103
 */
104
 
105
/*
106
 * uClinux revisions for NO_MM
107
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
108
 *                     The Silver Hammer Group, Ltd.
109
 */
110
 
111
#include <linux/malloc.h>
112
#include <linux/sched.h>
113
#include <linux/kernel.h>
114
#include <linux/errno.h>
115
#include <linux/stat.h>
116
#include <linux/file.h>
117
#include <linux/fcntl.h>
118
 
119
#include <asm/segment.h>
120
 
121
#define OFFSET_MAX      ((off_t)0x7fffffff)     /* FIXME: move elsewhere? */
122
 
123
static int flock_make_lock(struct file *filp, struct file_lock *fl,
124
                               unsigned int cmd);
125
static int posix_make_lock(struct file *filp, struct file_lock *fl,
126
                               struct flock *l);
127
static int flock_locks_conflict(struct file_lock *caller_fl,
128
                                struct file_lock *sys_fl);
129
static int posix_locks_conflict(struct file_lock *caller_fl,
130
                                struct file_lock *sys_fl);
131
static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
132
static int flock_lock_file(struct file *filp, struct file_lock *caller,
133
                           unsigned int wait);
134
static int posix_lock_file(struct file *filp, struct file_lock *caller,
135
                           unsigned int wait);
136
static int posix_locks_deadlock(struct task_struct *my_task,
137
                                struct task_struct *blocked_task);
138
static void posix_remove_locks(struct file_lock **before, struct task_struct *task);
139
static void flock_remove_locks(struct file_lock **before, struct file *filp);
140
static struct file_lock *locks_empty_lock(void);
141
static struct file_lock *locks_init_lock(struct file_lock *,
142
                                         struct file_lock *);
143
static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
144
static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait);
145
static char *lock_get_status(struct file_lock *fl, int id, char *pfx);
146
 
147
static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter);
148
static void locks_delete_block(struct file_lock *blocker, struct file_lock *waiter);
149
static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait);
150
 
151
static struct file_lock *file_lock_table = NULL;
152
 
153
/* Allocate a new lock, and initialize its fields from fl.
154
 * The lock is not inserted into any lists until locks_insert_lock() or
155
 * locks_insert_block() are called.
156
 */
157
static inline struct file_lock *locks_alloc_lock(struct file_lock *fl)
158
{
159
        return locks_init_lock(locks_empty_lock(), fl);
160
}
161
 
162
/* Free lock not inserted in any queue.
163
 */
164
static inline void locks_free_lock(struct file_lock *fl)
165
{
166
        if (waitqueue_active(&fl->fl_wait))
167
                panic("Aarggh: attempting to free lock with active wait queue - shoot Andy");
168
 
169
        if (fl->fl_nextblock != NULL || fl->fl_prevblock != NULL)
170
                panic("Aarggh: attempting to free lock with active block list - shoot Andy");
171
 
172
        kfree(fl);
173
        return;
174
}
175
 
176
/* Check if two locks overlap each other.
177
 */
178
static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
179
{
180
        return ((fl1->fl_end >= fl2->fl_start) &&
181
                (fl2->fl_end >= fl1->fl_start));
182
}
183
 
184
/* Insert waiter into blocker's block list.
185
 * We use a circular list so that processes can be easily woken up in
186
 * the order they blocked. The documentation doesn't require this but
187
 * it seems like the reasonable thing to do.
188
 */
189
static void locks_insert_block(struct file_lock *blocker,
190
                               struct file_lock *waiter)
191
{
192
        struct file_lock *prevblock;
193
 
194
        if (blocker->fl_prevblock == NULL)
195
                /* No previous waiters - list is empty */
196
                prevblock = blocker;
197
        else
198
                /* Previous waiters exist - add to end of list */
199
                prevblock = blocker->fl_prevblock;
200
 
201
        prevblock->fl_nextblock = waiter;
202
        blocker->fl_prevblock = waiter;
203
        waiter->fl_nextblock = blocker;
204
        waiter->fl_prevblock = prevblock;
205
 
206
        return;
207
}
208
 
209
/* Remove waiter from blocker's block list.
210
 * When blocker ends up pointing to itself then the list is empty.
211
 */
212
static void locks_delete_block(struct file_lock *blocker,
213
                               struct file_lock *waiter)
214
{
215
        struct file_lock *nextblock;
216
        struct file_lock *prevblock;
217
 
218
        nextblock = waiter->fl_nextblock;
219
        prevblock = waiter->fl_prevblock;
220
 
221
        if (nextblock == NULL)
222
                return;
223
 
224
        nextblock->fl_prevblock = prevblock;
225
        prevblock->fl_nextblock = nextblock;
226
 
227
        waiter->fl_prevblock = waiter->fl_nextblock = NULL;
228
        if (blocker->fl_nextblock == blocker)
229
                /* No more locks on blocker's blocked list */
230
                blocker->fl_prevblock = blocker->fl_nextblock = NULL;
231
        return;
232
}
233
 
234
/* Wake up processes blocked waiting for blocker.
235
 * If told to wait then schedule the processes until the block list
236
 * is empty, otherwise empty the block list ourselves.
237
 */
238
static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
239
{
240
        struct file_lock *waiter;
241
 
242
        while ((waiter = blocker->fl_nextblock) != NULL) {
243
                wake_up(&waiter->fl_wait);
244
                if (wait)
245
                        /* Let the blocked process remove waiter from the
246
                         * block list when it gets scheduled.
247
                         */
248
                        schedule();
249
                else
250
                        /* Remove waiter from the block list, because by the
251
                         * time it wakes up blocker won't exist any more.
252
                         */
253
                        locks_delete_block(blocker, waiter);
254
        }
255
        return;
256
}
257
 
258
/* flock() system call entry point. Apply a FL_FLOCK style lock to
259
 * an open file descriptor.
260
 */
261
asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
262
{
263
        struct file_lock file_lock;
264
        struct file *filp;
265
        int err = -EINVAL;
266
 
267
        filp = fget(fd);
268
        if(filp==NULL)
269
                return -EBADF;
270
 
271
 
272
        if (!flock_make_lock(filp, &file_lock, cmd))
273
                goto out;
274
 
275
        if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
276
        {
277
                err = -EBADF;
278
                goto out;
279
        }
280
        err=flock_lock_file(filp, &file_lock, (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
281
out:
282
        fput(filp, filp->f_inode);
283
        return err;
284
}
285
 
286
/* Report the first existing lock that would conflict with l.
287
 * This implements the F_GETLK command of fcntl().
288
 */
289
int fcntl_getlk(unsigned int fd, struct flock *l)
290
{
291
        int error;
292
        struct flock flock;
293
        struct file *filp;
294
        struct file_lock *fl,file_lock;
295
 
296
        error = verify_area(VERIFY_WRITE, l, sizeof(*l));
297
        if (error)
298
                return (error);
299
 
300
        filp = fget(fd);
301
        if(filp==NULL)
302
                return -EBADF;
303
 
304
        memcpy_fromfs(&flock, l, sizeof(flock));
305
        if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
306
        {
307
                error = -EINVAL;
308
                goto out;
309
        }
310
 
311
        if (!filp->f_inode || !posix_make_lock(filp, &file_lock, &flock))
312
        {
313
                error = -EINVAL;
314
                goto out;
315
        }
316
 
317
        flock.l_type = F_UNLCK;
318
        for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
319
                if (!(fl->fl_flags & FL_POSIX))
320
                        break;
321
                if (posix_locks_conflict(&file_lock, fl)) {
322
                        flock.l_pid = fl->fl_owner->pid;
323
                        flock.l_start = fl->fl_start;
324
                        flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
325
                                fl->fl_end - fl->fl_start + 1;
326
                        flock.l_whence = 0;
327
                        flock.l_type = fl->fl_type;
328
                        break;
329
                }
330
        }
331
 
332
        memcpy_tofs(l, &flock, sizeof(flock));
333
out:
334
        fput(filp, filp->f_inode);
335
        return error;
336
}
337
 
338
/* Apply the lock described by l to an open file descriptor.
339
 * This implements both the F_SETLK and F_SETLKW commands of fcntl().
340
 * It also emulates flock() in a pretty broken way for older C
341
 * libraries.
342
 */
343
int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
344
{
345
        int error;
346
        struct file *filp;
347
        struct file_lock file_lock;
348
        struct flock flock;
349
        struct inode *inode;
350
 
351
        /* Get arguments and validate them ...
352
         */
353
 
354
        error = verify_area(VERIFY_READ, l, sizeof(*l));
355
        if (error)
356
                return (error);
357
 
358
        filp = fget(fd);
359
        if(filp==NULL)
360
                return -EBADF;
361
 
362
        inode = filp->f_inode;
363
 
364
        /*
365
         * This might block, so we do it before checking the inode.
366
         */
367
 
368
        memcpy_fromfs(&flock, l, sizeof(flock));
369
 
370
        /* Don't allow mandatory locks on files that may be memory mapped
371
         * and shared.
372
         */
373
#ifndef NO_MM
374
        if (IS_MANDLOCK(inode) &&
375
            (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
376
            inode->i_mmap) {
377
                struct vm_area_struct *vma = inode->i_mmap;
378
                do {
379
                        if (vma->vm_flags & VM_MAYSHARE)
380
                        {
381
                                error = -EAGAIN;
382
                                goto out;
383
                        }
384
                        vma = vma->vm_next_share;
385
                } while (vma != inode->i_mmap);
386
        }
387
#endif /* !NO_MM */
388
 
389
        if (!posix_make_lock(filp, &file_lock, &flock))
390
        {
391
                error = -EINVAL;
392
                goto out;
393
        }
394
 
395
        switch (flock.l_type) {
396
        case F_RDLCK:
397
                if (!(filp->f_mode & 1))
398
                {
399
                        error = -EBADF;
400
                        goto out;
401
                }
402
                break;
403
        case F_WRLCK:
404
                if (!(filp->f_mode & 2))
405
                {
406
                        error = -EBADF;
407
                        goto out;
408
                }
409
                break;
410
        case F_UNLCK:
411
                break;
412
        case F_SHLCK:
413
        case F_EXLCK:
414
#if 1
415
/* warn a bit for now, but don't overdo it */
416
{
417
        static int count = 0;
418
        if (!count) {
419
                count=1;
420
                printk(KERN_WARNING
421
                       "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
422
                       current->pid, current->comm);
423
        }
424
}
425
#endif
426
                if (!(filp->f_mode & 3))
427
                {
428
                        error = -EBADF;
429
                        goto out;
430
                }
431
                break;
432
        default:
433
                return (-EINVAL);
434
        }
435
 
436
        error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
437
out:
438
        fput(filp, filp->f_inode);
439
        return error;
440
}
441
 
442
/* This function is called when the file is closed.
443
 */
444
void locks_remove_locks(struct task_struct *task, struct file *filp)
445
{
446
        struct file_lock *fl;
447
 
448
        /* For POSIX locks we free all locks on this file for the given task.
449
         * For FLOCK we only free locks on this *open* file if it is the last
450
         * close on that file.
451
         */
452
        if ((fl = filp->f_inode->i_flock) != NULL) {
453
                if (fl->fl_flags & FL_POSIX)
454
                        posix_remove_locks(&filp->f_inode->i_flock, task);
455
                else
456
                        flock_remove_locks(&filp->f_inode->i_flock, filp);
457
        }
458
 
459
        return;
460
}
461
 
462
static void posix_remove_locks(struct file_lock **before, struct task_struct *task)
463
{
464
        struct file_lock *fl;
465
 
466
        while ((fl = *before) != NULL) {
467
                if (fl->fl_owner == task)
468
                        locks_delete_lock(before, 0);
469
                else
470
                        before = &fl->fl_next;
471
        }
472
 
473
        return;
474
}
475
 
476
static void flock_remove_locks(struct file_lock **before, struct file *filp)
477
{
478
        struct file_lock *fl;
479
 
480
        while ((fl = *before) != NULL) {
481
                if ((fl->fl_file == filp) && (filp->f_count == 1))
482
                        locks_delete_lock(before, 0);
483
                else
484
                        before = &fl->fl_next;
485
        }
486
 
487
        return;
488
}
489
 
490
int locks_verify_locked(struct inode *inode)
491
{
492
        /* Candidates for mandatory locking have the setgid bit set
493
         * but no group execute bit -  an otherwise meaningless combination.
494
         */
495
        if (IS_MANDLOCK(inode) &&
496
            (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
497
                return (locks_mandatory_locked(inode));
498
        return (0);
499
}
500
 
501
int locks_verify_area(int read_write, struct inode *inode, struct file *filp,
502
                      unsigned int offset, unsigned int count)
503
{
504
        /* Candidates for mandatory locking have the setgid bit set
505
         * but no group execute bit -  an otherwise meaningless combination.
506
         */
507
        if (IS_MANDLOCK(inode) &&
508
            (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
509
                return (locks_mandatory_area(read_write, inode, filp, offset,
510
                                             count));
511
        return (0);
512
}
513
 
514
int locks_mandatory_locked(struct inode *inode)
515
{
516
        struct file_lock *fl;
517
 
518
        /* If there are no FL_POSIX locks then go ahead. */
519
        if (!(fl = inode->i_flock) || !(fl->fl_flags & FL_POSIX))
520
                return (0);
521
 
522
        /* Search the lock list for this inode for any POSIX locks.
523
         */
524
        while (fl != NULL) {
525
                if (fl->fl_owner != current)
526
                        return (-EAGAIN);
527
                fl = fl->fl_next;
528
        }
529
        return (0);
530
}
531
 
532
int locks_mandatory_area(int read_write, struct inode *inode,
533
                         struct file *filp, unsigned int offset,
534
                         unsigned int count)
535
{
536
        struct file_lock *fl;
537
        struct file_lock tfl;
538
 
539
        memset(&tfl, 0, sizeof(tfl));
540
 
541
        tfl.fl_file = filp;
542
        tfl.fl_flags = FL_POSIX | FL_ACCESS;
543
        tfl.fl_owner = current;
544
        tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
545
        tfl.fl_start = offset;
546
        tfl.fl_end = offset + count - 1;
547
 
548
repeat:
549
        /* If there are no FL_POSIX locks then go ahead. */
550
        if (!(fl = inode->i_flock) || !(fl->fl_flags & FL_POSIX))
551
                return (0);
552
 
553
        /* Search the lock list for this inode for locks that conflict with
554
         * the proposed read/write.
555
         */
556
        while (fl != NULL) {
557
                /* Block for writes against a "read" lock,
558
                 * and both reads and writes against a "write" lock.
559
                 */
560
                if (posix_locks_conflict(fl, &tfl)) {
561
                        if (filp && (filp->f_flags & O_NONBLOCK))
562
                                return (-EAGAIN);
563
                        if (current->signal & ~current->blocked)
564
                                return (-ERESTARTSYS);
565
                        if (posix_locks_deadlock(current, fl->fl_owner))
566
                                return (-EDEADLK);
567
 
568
                        locks_insert_block(fl, &tfl);
569
                        interruptible_sleep_on(&tfl.fl_wait);
570
                        locks_delete_block(fl, &tfl);
571
 
572
                        if (current->signal & ~current->blocked)
573
                                return (-ERESTARTSYS);
574
                        /* If we've been sleeping someone might have
575
                         * changed the permissions behind our back.
576
                         */
577
                        if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
578
                                break;
579
                        goto repeat;
580
                }
581
                fl = fl->fl_next;
582
        }
583
        return (0);
584
}
585
 
586
/* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
587
 * style lock.
588
 */
589
static int posix_make_lock(struct file *filp, struct file_lock *fl,
590
                           struct flock *l)
591
{
592
        off_t start;
593
 
594
        memset(fl, 0, sizeof(*fl));
595
 
596
        fl->fl_flags = FL_POSIX;
597
 
598
        switch (l->l_type) {
599
        case F_RDLCK:
600
        case F_WRLCK:
601
        case F_UNLCK:
602
                fl->fl_type = l->l_type;
603
                break;
604
        case F_SHLCK :
605
                fl->fl_type = F_RDLCK;
606
                fl->fl_flags |= FL_BROKEN;
607
                break;
608
        case F_EXLCK :
609
                fl->fl_type = F_WRLCK;
610
                fl->fl_flags |= FL_BROKEN;
611
                break;
612
        default:
613
                return (0);
614
        }
615
 
616
        switch (l->l_whence) {
617
        case 0: /*SEEK_SET*/
618
                start = 0;
619
                break;
620
        case 1: /*SEEK_CUR*/
621
                start = filp->f_pos;
622
                break;
623
        case 2: /*SEEK_END*/
624
                start = filp->f_inode->i_size;
625
                break;
626
        default:
627
                return (0);
628
        }
629
 
630
        if (((start += l->l_start) < 0) || (l->l_len < 0))
631
                return (0);
632
        fl->fl_start = start;   /* we record the absolute position */
633
        if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
634
                fl->fl_end = OFFSET_MAX;
635
 
636
        fl->fl_file = filp;
637
        fl->fl_owner = current;
638
 
639
        return (1);
640
}
641
 
642
/* Verify a call to flock() and fill in a file_lock structure with
643
 * an appropriate FLOCK lock.
644
 */
645
static int flock_make_lock(struct file *filp, struct file_lock *fl,
646
                           unsigned int cmd)
647
{
648
        memset(fl, 0, sizeof(*fl));
649
 
650
        if (!filp->f_inode)     /* just in case */
651
                return (0);
652
 
653
        switch (cmd & ~LOCK_NB) {
654
        case LOCK_SH:
655
                fl->fl_type = F_RDLCK;
656
                break;
657
        case LOCK_EX:
658
                fl->fl_type = F_WRLCK;
659
                break;
660
        case LOCK_UN:
661
                fl->fl_type = F_UNLCK;
662
                break;
663
        default:
664
                return (0);
665
        }
666
 
667
        fl->fl_flags = FL_FLOCK;
668
        fl->fl_start = 0;
669
        fl->fl_end = OFFSET_MAX;
670
        fl->fl_file = filp;
671
        fl->fl_owner = NULL;
672
 
673
        return (1);
674
}
675
 
676
/* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
677
 * checking before calling the locks_conflict().
678
 */
679
static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
680
{
681
        /* POSIX locks owned by the same process do not conflict with
682
         * each other.
683
         */
684
        if (caller_fl->fl_owner == sys_fl->fl_owner)
685
                return (0);
686
 
687
        return (locks_conflict(caller_fl, sys_fl));
688
}
689
 
690
/* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
691
 * checking before calling the locks_conflict().
692
 */
693
static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
694
{
695
        /* FLOCK locks referring to the same filp do not conflict with
696
         * each other.
697
         */
698
        if (caller_fl->fl_file == sys_fl->fl_file)
699
                return (0);
700
 
701
        return (locks_conflict(caller_fl, sys_fl));
702
}
703
 
704
/* Determine if lock sys_fl blocks lock caller_fl. Common functionality
705
 * checks for overlapping locks and shared/exclusive status.
706
 */
707
static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
708
{
709
        if (!locks_overlap(caller_fl, sys_fl))
710
                return (0);
711
 
712
        switch (caller_fl->fl_type) {
713
        case F_RDLCK:
714
                return (sys_fl->fl_type == F_WRLCK);
715
 
716
        case F_WRLCK:
717
                return (1);
718
 
719
        default:
720
                printk("locks_conflict(): impossible lock type - %d\n",
721
                       caller_fl->fl_type);
722
                break;
723
        }
724
        return (0);      /* This should never happen */
725
}
726
 
727
/* This function tests for deadlock condition before putting a process to
728
 * sleep. The detection scheme is no longer recursive. Recursive was neat,
729
 * but dangerous - we risked stack corruption if the lock data was bad, or
730
 * if the recursion was too deep for any other reason.
731
 *
732
 * We rely on the fact that a task can only be on one lock's wait queue
733
 * at a time. When we find blocked_task on a wait queue we can re-search
734
 * with blocked_task equal to that queue's owner, until either blocked_task
735
 * isn't found, or blocked_task is found on a queue owned by my_task.
736
 */
737
static int posix_locks_deadlock(struct task_struct *my_task,
738
                                struct task_struct *blocked_task)
739
{
740
        struct file_lock *fl;
741
        struct file_lock *bfl;
742
 
743
next_task:
744
        if (my_task == blocked_task)
745
                return (1);
746
        for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
747
                if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
748
                        continue;
749
                for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
750
                        if (bfl->fl_owner == blocked_task) {
751
                                if (fl->fl_owner == my_task) {
752
                                        return (1);
753
                                }
754
                                blocked_task = fl->fl_owner;
755
                                goto next_task;
756
                        }
757
                }
758
        }
759
        return (0);
760
}
761
 
762
/* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
763
 * the head of the list, but that's secret knowledge known only to the next
764
 * two functions.
765
 */
766
static int flock_lock_file(struct file *filp, struct file_lock *caller,
767
                           unsigned int wait)
768
{
769
        struct file_lock *fl;
770
        struct file_lock *new_fl = NULL;
771
        struct file_lock **before;
772
        int error;
773
        int change;
774
        int unlock = (caller->fl_type == F_UNLCK);
775
 
776
        /*
777
         * If we need a new lock, get it in advance to avoid races.
778
         */
779
        if (!unlock) {
780
                error = -ENOLCK;
781
                new_fl = locks_alloc_lock(caller);
782
                if (!new_fl)
783
                        goto out;
784
        }
785
 
786
 
787
        error = 0;
788
search:
789
        change = 0;
790
 
791
        before = &filp->f_inode->i_flock;
792
 
793
        if ((fl = *before) && (fl->fl_flags & FL_POSIX)) {
794
                error = -EBUSY;
795
                goto out;
796
        }
797
 
798
        while ((fl = *before) != NULL) {
799
                if (caller->fl_file == fl->fl_file) {
800
                        if (caller->fl_type == fl->fl_type)
801
                                goto out;
802
                        change = 1;
803
                        break;
804
                }
805
                before = &fl->fl_next;
806
        }
807
        /* change means that we are changing the type of an existing lock, or
808
         * or else unlocking it.
809
         */
810
        if (change) {
811
                /* N.B. What if the wait argument is false? */
812
                locks_delete_lock(before, !unlock);
813
                /*
814
                 * If we waited, another lock may have been added ...
815
                 */
816
                if (!unlock)
817
                        goto search;
818
        }
819
        if (unlock)
820
                goto out;
821
 
822
repeat:
823
        /* Check signals each time we start */
824
        error = -ERESTARTSYS;
825
        if (current->signal & ~current->blocked)
826
                goto out;
827
        error = -EBUSY;
828
        if ((fl = filp->f_inode->i_flock) && (fl->fl_flags & FL_POSIX))
829
                goto out;
830
 
831
        while (fl != NULL) {
832
                if (!flock_locks_conflict(new_fl, fl)) {
833
                        fl = fl->fl_next;
834
                        continue;
835
                }
836
                error = -EAGAIN;
837
                if (!wait)
838
                        goto out;
839
                locks_insert_block(fl, new_fl);
840
                interruptible_sleep_on(&new_fl->fl_wait);
841
                locks_delete_block(fl, new_fl);
842
                goto repeat;
843
        }
844
        locks_insert_lock(&filp->f_inode->i_flock, new_fl);
845
        new_fl = NULL;
846
        error = 0;
847
 
848
out:
849
        if (new_fl)
850
                locks_free_lock(new_fl);
851
        return (error);
852
}
853
 
854
/* Add a POSIX style lock to a file.
855
 * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
856
 * task, then by starting address
857
 *
858
 * Kai Petzke writes:
859
 * To make freeing a lock much faster, we keep a pointer to the lock before the
860
 * actual one. But the real gain of the new coding was, that lock_it() and
861
 * unlock_it() became one function.
862
 *
863
 * To all purists: Yes, I use a few goto's. Just pass on to the next function.
864
 */
865
 
866
static int posix_lock_file(struct file *filp, struct file_lock *caller,
867
                           unsigned int wait)
868
{
869
        struct file_lock *fl;
870
        struct file_lock *new_fl, *new_fl2;
871
        struct file_lock *left = NULL;
872
        struct file_lock *right = NULL;
873
        struct file_lock **before;
874
        int error;
875
        int added = 0;
876
 
877
        /*
878
         * We may need two file_lock structures for this operation,
879
         * so we get them in advance to avoid races.
880
         */
881
        new_fl  = locks_empty_lock();
882
        new_fl2 = locks_empty_lock();
883
        error = -ENOLCK; /* "no luck" */
884
        if (!(new_fl && new_fl2))
885
                goto out;
886
 
887
        if (caller->fl_type != F_UNLCK) {
888
  repeat:
889
                error = -EBUSY;
890
                if ((fl = filp->f_inode->i_flock) && (fl->fl_flags & FL_FLOCK))
891
                        goto out;
892
 
893
                while (fl != NULL) {
894
                        if (!posix_locks_conflict(caller, fl)) {
895
                                fl = fl->fl_next;
896
                                continue;
897
                        }
898
                        error = -EAGAIN;
899
                        if (!wait)
900
                                goto out;
901
                        error = -EDEADLK;
902
                        if (posix_locks_deadlock(caller->fl_owner, fl->fl_owner))
903
                                goto out;
904
                        error = -ERESTARTSYS;
905
                        if (current->signal & ~current->blocked)
906
                                goto out;
907
                        locks_insert_block(fl, caller);
908
                        interruptible_sleep_on(&caller->fl_wait);
909
                        locks_delete_block(fl, caller);
910
                        goto repeat;
911
                }
912
        }
913
 
914
        /*
915
         * We've allocated the new locks in advance, so there are no
916
         * errors possible (and no blocking operations) from here on.
917
         *
918
         * Find the first old lock with the same owner as the new lock.
919
         */
920
 
921
        before = &filp->f_inode->i_flock;
922
 
923
        error = -EBUSY;
924
        if ((*before != NULL) && ((*before)->fl_flags & FL_FLOCK))
925
                goto out;
926
 
927
        /* First skip locks owned by other processes.
928
         */
929
        while ((fl = *before) && (caller->fl_owner != fl->fl_owner)) {
930
                before = &fl->fl_next;
931
        }
932
 
933
        /* Process locks with this owner.
934
         */
935
        while ((fl = *before) && (caller->fl_owner == fl->fl_owner)) {
936
                /* Detect adjacent or overlapping regions (if same lock type)
937
                 */
938
                if (caller->fl_type == fl->fl_type) {
939
                        if (fl->fl_end < caller->fl_start - 1)
940
                                goto next_lock;
941
                        /* If the next lock in the list has entirely bigger
942
                         * addresses than the new one, insert the lock here.
943
                         */
944
                        if (fl->fl_start > caller->fl_end + 1)
945
                                break;
946
 
947
                        /* If we come here, the new and old lock are of the
948
                         * same type and adjacent or overlapping. Make one
949
                         * lock yielding from the lower start address of both
950
                         * locks to the higher end address.
951
                         */
952
                        if (fl->fl_start > caller->fl_start)
953
                                fl->fl_start = caller->fl_start;
954
                        else
955
                                caller->fl_start = fl->fl_start;
956
                        if (fl->fl_end < caller->fl_end)
957
                                fl->fl_end = caller->fl_end;
958
                        else
959
                                caller->fl_end = fl->fl_end;
960
                        if (added) {
961
                                locks_delete_lock(before, 0);
962
                                continue;
963
                        }
964
                        caller = fl;
965
                        added = 1;
966
                }
967
                else {
968
                        /* Processing for different lock types is a bit
969
                         * more complex.
970
                         */
971
                        if (fl->fl_end < caller->fl_start)
972
                                goto next_lock;
973
                        if (fl->fl_start > caller->fl_end)
974
                                break;
975
                        if (caller->fl_type == F_UNLCK)
976
                                added = 1;
977
                        if (fl->fl_start < caller->fl_start)
978
                                left = fl;
979
                        /* If the next lock in the list has a higher end
980
                         * address than the new one, insert the new one here.
981
                         */
982
                        if (fl->fl_end > caller->fl_end) {
983
                                right = fl;
984
                                break;
985
                        }
986
                        if (fl->fl_start >= caller->fl_start) {
987
                                /* The new lock completely replaces an old
988
                                 * one (This may happen several times).
989
                                 */
990
                                if (added) {
991
                                        locks_delete_lock(before, 0);
992
                                        continue;
993
                                }
994
                                /* Replace the old lock with the new one.
995
                                 * Wake up anybody waiting for the old one,
996
                                 * as the change in lock type might satisfy
997
                                 * their needs.
998
                                 */
999
                                locks_wake_up_blocks(fl, 0);
1000
                                fl->fl_start = caller->fl_start;
1001
                                fl->fl_end = caller->fl_end;
1002
                                fl->fl_type = caller->fl_type;
1003
                                caller = fl;
1004
                                added = 1;
1005
                        }
1006
                }
1007
                /* Go on to next lock.
1008
                 */
1009
        next_lock:
1010
                before = &fl->fl_next;
1011
        }
1012
 
1013
        error = 0;
1014
        if (!added) {
1015
                if (caller->fl_type == F_UNLCK)
1016
                        goto out;
1017
                locks_init_lock(new_fl, caller);
1018
                locks_insert_lock(before, new_fl);
1019
                new_fl = NULL;
1020
        }
1021
        if (right) {
1022
                if (left == right) {
1023
                        /* The new lock breaks the old one in two pieces,
1024
                         * so we have to use the second new lock (in this
1025
                         * case, even F_UNLCK may fail!).
1026
                         */
1027
                        left = locks_init_lock(new_fl2, right);
1028
                        locks_insert_lock(before, left);
1029
                        new_fl2 = NULL;
1030
                }
1031
                right->fl_start = caller->fl_end + 1;
1032
                locks_wake_up_blocks(right, 0);
1033
        }
1034
        if (left) {
1035
                left->fl_end = caller->fl_start - 1;
1036
                locks_wake_up_blocks(left, 0);
1037
        }
1038
out:
1039
        /*
1040
         * Free any unused locks.  (They haven't
1041
         * ever been used, so we use kfree().)
1042
         */
1043
        if (new_fl)
1044
                kfree(new_fl);
1045
        if (new_fl2)
1046
                kfree(new_fl2);
1047
        return error;
1048
}
1049
 
1050
/*
1051
 * Allocate an empty lock structure. We can use GFP_KERNEL now that
1052
 * all allocations are done in advance.
1053
 */
1054
static struct file_lock *locks_empty_lock(void)
1055
{
1056
        return ((struct file_lock *) kmalloc(sizeof(struct file_lock),
1057
                                                GFP_KERNEL));
1058
}
1059
 
1060
/*
1061
 * Initialize a new lock from an existing file_lock structure.
1062
 */
1063
static struct file_lock *locks_init_lock(struct file_lock *new,
1064
                                         struct file_lock *fl)
1065
{
1066
        if (new) {
1067
                memset(new, 0, sizeof(*new));
1068
                new->fl_owner = fl->fl_owner;
1069
                new->fl_file = fl->fl_file;
1070
                new->fl_flags = fl->fl_flags;
1071
                new->fl_type = fl->fl_type;
1072
                new->fl_start = fl->fl_start;
1073
                new->fl_end = fl->fl_end;
1074
        }
1075
        return new;
1076
}
1077
 
1078
/* Insert file lock fl into an inode's lock list at the position indicated
1079
 * by pos. At the same time add the lock to the global file lock list.
1080
 */
1081
static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
1082
{
1083
        fl->fl_nextlink = file_lock_table;
1084
        fl->fl_prevlink = NULL;
1085
        if (file_lock_table != NULL)
1086
                file_lock_table->fl_prevlink = fl;
1087
        file_lock_table = fl;
1088
        fl->fl_next = *pos;     /* insert into file's list */
1089
        *pos = fl;
1090
 
1091
        return;
1092
}
1093
 
1094
/* Delete a lock and free it.
1095
 * First remove our lock from the active lock lists. Then call
1096
 * locks_wake_up_blocks() to wake up processes that are blocked
1097
 * waiting for this lock. Finally free the lock structure.
1098
 */
1099
static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
1100
{
1101
        struct file_lock *thisfl;
1102
        struct file_lock *prevfl;
1103
        struct file_lock *nextfl;
1104
 
1105
        thisfl = *thisfl_p;
1106
        *thisfl_p = thisfl->fl_next;
1107
 
1108
        prevfl = thisfl->fl_prevlink;
1109
        nextfl = thisfl->fl_nextlink;
1110
 
1111
        if (nextfl != NULL)
1112
                nextfl->fl_prevlink = prevfl;
1113
 
1114
        if (prevfl != NULL)
1115
                prevfl->fl_nextlink = nextfl;
1116
        else
1117
                file_lock_table = nextfl;
1118
 
1119
        locks_wake_up_blocks(thisfl, wait);
1120
        locks_free_lock(thisfl);
1121
 
1122
        return;
1123
}
1124
 
1125
 
1126
static char *lock_get_status(struct file_lock *fl, int id, char *pfx)
1127
{
1128
        static char temp[129];
1129
        char *p = temp;
1130
        struct inode *inode;
1131
 
1132
        inode = fl->fl_file->f_inode;
1133
 
1134
        p += sprintf(p, "%d:%s ", id, pfx);
1135
        if (fl->fl_flags & FL_POSIX) {
1136
                p += sprintf(p, "%6s %s ",
1137
                             (fl->fl_flags & FL_BROKEN) ? "BROKEN" :
1138
                             (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
1139
                             (IS_MANDLOCK(inode) &&
1140
                              (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
1141
                             "MANDATORY" : "ADVISORY ");
1142
        }
1143
        else {
1144
                p += sprintf(p, "FLOCK  ADVISORY  ");
1145
        }
1146
        p += sprintf(p, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
1147
        p += sprintf(p, "%d %s:%ld %ld %ld ",
1148
                     fl->fl_owner ? fl->fl_owner->pid : 0,
1149
                     kdevname(inode->i_dev), inode->i_ino, fl->fl_start,
1150
                     fl->fl_end);
1151
        sprintf(p, "%08lx %08lx %08lx %08lx %08lx\n",
1152
                (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
1153
                (long)fl->fl_next, (long)fl->fl_nextblock);
1154
        return (temp);
1155
}
1156
 
1157
static inline int copy_lock_status(char *p, char **q, off_t pos, int len,
1158
                                   off_t offset, int length)
1159
{
1160
        int i;
1161
 
1162
        i = pos - offset;
1163
        if (i > 0) {
1164
                if (i >= length) {
1165
                        i = len + length - i;
1166
                        memcpy(*q, p, i);
1167
                        *q += i;
1168
                        return (0);
1169
                }
1170
                if (i < len) {
1171
                        p += len - i;
1172
                }
1173
                else
1174
                        i = len;
1175
                memcpy(*q, p, i);
1176
                *q += i;
1177
        }
1178
 
1179
        return (1);
1180
}
1181
 
1182
int get_locks_status(char *buffer, char **start, off_t offset, int length)
1183
{
1184
        struct file_lock *fl;
1185
        struct file_lock *bfl;
1186
        char *p;
1187
        char *q = buffer;
1188
        int i;
1189
        int len;
1190
        off_t pos = 0;
1191
 
1192
        for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
1193
                p = lock_get_status(fl, i, "");
1194
                len = strlen(p);
1195
                pos += len;
1196
                if (!copy_lock_status(p, &q, pos, len, offset, length))
1197
                        goto done;
1198
                if ((bfl = fl->fl_nextblock) == NULL)
1199
                        continue;
1200
                do {
1201
                        p = lock_get_status(bfl, i, " ->");
1202
                        len = strlen(p);
1203
                        pos += len;
1204
                        if (!copy_lock_status(p, &q, pos, len, offset, length))
1205
                                goto done;
1206
                } while ((bfl = bfl->fl_nextblock) != fl);
1207
        }
1208
done:
1209
        if (q != buffer)
1210
                *start = buffer;
1211
        return (q - buffer);
1212
}
1213
 
1214
 
1215
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.