]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - fs/locks.c
Cleanup macros for distinguishing mandatory locks
[net-next-2.6.git] / fs / locks.c
index 671a034dc999fffdf88856401694fda0cef87147..9a3fe0d8285b178403aa76ce2d34358c7ec6c9d6 100644 (file)
@@ -203,9 +203,6 @@ static void init_once(void *foo, struct kmem_cache *cache, unsigned long flags)
 {
        struct file_lock *lock = (struct file_lock *) foo;
 
-       if (!(flags & SLAB_CTOR_CONSTRUCTOR))
-               return;
-
        locks_init_lock(lock);
 }
 
@@ -461,22 +458,20 @@ static int lease_init(struct file *filp, int type, struct file_lock *fl)
 }
 
 /* Allocate a file_lock initialised to this type of lease */
-static int lease_alloc(struct file *filp, int type, struct file_lock **flp)
+static struct file_lock *lease_alloc(struct file *filp, int type)
 {
        struct file_lock *fl = locks_alloc_lock();
        int error = -ENOMEM;
 
        if (fl == NULL)
-               goto out;
+               return ERR_PTR(error);
 
        error = lease_init(filp, type, fl);
        if (error) {
                locks_free_lock(fl);
-               fl = NULL;
+               return ERR_PTR(error);
        }
-out:
-       *flp = fl;
-       return error;
+       return fl;
 }
 
 /* Check if two locks overlap each other.
@@ -539,7 +534,9 @@ static void locks_insert_block(struct file_lock *blocker,
 static void locks_wake_up_blocks(struct file_lock *blocker)
 {
        while (!list_empty(&blocker->fl_block)) {
-               struct file_lock *waiter = list_entry(blocker->fl_block.next,
+               struct file_lock *waiter;
+
+               waiter = list_first_entry(&blocker->fl_block,
                                struct file_lock, fl_block);
                __locks_delete_block(waiter);
                if (waiter->fl_lmops && waiter->fl_lmops->fl_notify)
@@ -664,26 +661,24 @@ static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *w
        return result;
 }
 
-int
+void
 posix_test_lock(struct file *filp, struct file_lock *fl)
 {
        struct file_lock *cfl;
 
-       fl->fl_type = F_UNLCK;
        lock_kernel();
        for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
                if (!IS_POSIX(cfl))
                        continue;
-               if (posix_locks_conflict(cfl, fl))
+               if (posix_locks_conflict(fl, cfl))
                        break;
        }
-       if (cfl) {
+       if (cfl)
                __locks_copy_lock(fl, cfl);
-               unlock_kernel();
-               return 1;
-       }
+       else
+               fl->fl_type = F_UNLCK;
        unlock_kernel();
-       return 0;
+       return;
 }
 
 EXPORT_SYMBOL(posix_test_lock);
@@ -722,8 +717,7 @@ next_task:
 }
 
 /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
- * at the head of the list, but that's secret knowledge known only to
- * flock_lock_file and posix_lock_file.
+ * after any leases, but before any posix locks.
  *
  * Note that if called with an FL_EXISTS argument, the caller may determine
  * whether or not a lock was successfully freed by testing the return
@@ -740,6 +734,15 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
        lock_kernel();
        if (request->fl_flags & FL_ACCESS)
                goto find_conflict;
+
+       if (request->fl_type != F_UNLCK) {
+               error = -ENOMEM;
+               new_fl = locks_alloc_lock();
+               if (new_fl == NULL)
+                       goto out;
+               error = 0;
+       }
+
        for_each_lock(inode, before) {
                struct file_lock *fl = *before;
                if (IS_POSIX(fl))
@@ -761,10 +764,6 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
                goto out;
        }
 
-       error = -ENOMEM;
-       new_fl = locks_alloc_lock();
-       if (new_fl == NULL)
-               goto out;
        /*
         * If a higher-priority process was blocked on the old file lock,
         * give it the opportunity to lock the file.
@@ -789,7 +788,7 @@ find_conflict:
        if (request->fl_flags & FL_ACCESS)
                goto out;
        locks_copy_lock(new_fl, request);
-       locks_insert_lock(&inode->i_flock, new_fl);
+       locks_insert_lock(before, new_fl);
        new_fl = NULL;
        error = 0;
 
@@ -826,7 +825,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
        lock_kernel();
        if (request->fl_type != F_UNLCK) {
                for_each_lock(inode, before) {
-                       struct file_lock *fl = *before;
+                       fl = *before;
                        if (!IS_POSIX(fl))
                                continue;
                        if (!posix_locks_conflict(request, fl))
@@ -1120,7 +1119,7 @@ int locks_mandatory_area(int read_write, struct inode *inode,
                         * If we've been sleeping someone might have
                         * changed the permissions behind our back.
                         */
-                       if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
+                       if (__mandatory_lock(inode))
                                continue;
                }
 
@@ -1172,9 +1171,9 @@ static void time_out_leases(struct inode *inode)
  *     @inode: the inode of the file to return
  *     @mode: the open mode (read or write)
  *
- *     break_lease (inlined for speed) has checked there already
- *     is a lease on this file.  Leases are broken on a call to open()
- *     or truncate().  This function can sleep unless you
+ *     break_lease (inlined for speed) has checked there already is at least
+ *     some kind of lock (maybe a lease) on this file.  Leases are broken on
+ *     a call to open() or truncate().  This function can sleep unless you
  *     specified %O_NONBLOCK to your open().
  */
 int __break_lease(struct inode *inode, unsigned int mode)
@@ -1182,12 +1181,10 @@ int __break_lease(struct inode *inode, unsigned int mode)
        int error = 0, future;
        struct file_lock *new_fl, *flock;
        struct file_lock *fl;
-       int alloc_err;
        unsigned long break_time;
        int i_have_this_lease = 0;
 
-       alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK,
-                       &new_fl);
+       new_fl = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK);
 
        lock_kernel();
 
@@ -1215,8 +1212,9 @@ int __break_lease(struct inode *inode, unsigned int mode)
                goto out;
        }
 
-       if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) {
-               error = alloc_err;
+       if (IS_ERR(new_fl) && !i_have_this_lease
+                       && ((mode & O_NONBLOCK) == 0)) {
+               error = PTR_ERR(new_fl);
                goto out;
        }
 
@@ -1263,7 +1261,7 @@ restart:
 
 out:
        unlock_kernel();
-       if (!alloc_err)
+       if (!IS_ERR(new_fl))
                locks_free_lock(new_fl);
        return error;
 }
@@ -1332,7 +1330,7 @@ int fcntl_getlease(struct file *filp)
 }
 
 /**
- *     __setlease      -       sets a lease on an open file
+ *     generic_setlease        -       sets a lease on an open file
  *     @filp: file pointer
  *     @arg: type of lease to obtain
  *     @flp: input - file_lock to use, output - file_lock inserted
@@ -1342,18 +1340,25 @@ int fcntl_getlease(struct file *filp)
  *
  *     Called with kernel lock held.
  */
-static int __setlease(struct file *filp, long arg, struct file_lock **flp)
+int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
 {
        struct file_lock *fl, **before, **my_before = NULL, *lease;
+       struct file_lock *new_fl = NULL;
        struct dentry *dentry = filp->f_path.dentry;
        struct inode *inode = dentry->d_inode;
        int error, rdlease_count = 0, wrlease_count = 0;
 
+       if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
+               return -EACCES;
+       if (!S_ISREG(inode->i_mode))
+               return -EINVAL;
+       error = security_file_lock(filp, arg);
+       if (error)
+               return error;
+
        time_out_leases(inode);
 
-       error = -EINVAL;
-       if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break)
-               goto out;
+       BUG_ON(!(*flp)->fl_lmops->fl_break);
 
        lease = *flp;
 
@@ -1365,6 +1370,11 @@ static int __setlease(struct file *filp, long arg, struct file_lock **flp)
                || (atomic_read(&inode->i_count) > 1)))
                goto out;
 
+       error = -ENOMEM;
+       new_fl = locks_alloc_lock();
+       if (new_fl == NULL)
+               goto out;
+
        /*
         * At this point, we know that if there is an exclusive
         * lease on this file, then we hold it on this filp
@@ -1407,53 +1417,60 @@ static int __setlease(struct file *filp, long arg, struct file_lock **flp)
        if (!leases_enable)
                goto out;
 
-       error = -ENOMEM;
-       fl = locks_alloc_lock();
-       if (fl == NULL)
-               goto out;
+       locks_copy_lock(new_fl, lease);
+       locks_insert_lock(before, new_fl);
 
-       locks_copy_lock(fl, lease);
-
-       locks_insert_lock(before, fl);
+       *flp = new_fl;
+       return 0;
 
-       *flp = fl;
-       error = 0;
 out:
+       if (new_fl != NULL)
+               locks_free_lock(new_fl);
        return error;
 }
+EXPORT_SYMBOL(generic_setlease);
 
  /**
- *     setlease        -       sets a lease on an open file
+ *     vfs_setlease        -       sets a lease on an open file
  *     @filp: file pointer
  *     @arg: type of lease to obtain
  *     @lease: file_lock to use
  *
  *     Call this to establish a lease on the file.
- *     The fl_lmops fl_break function is required by break_lease
+ *     The (*lease)->fl_lmops->fl_break operation must be set; if not,
+ *     break_lease will oops!
+ *
+ *     This will call the filesystem's setlease file method, if
+ *     defined.  Note that there is no getlease method; instead, the
+ *     filesystem setlease method should call back to setlease() to
+ *     add a lease to the inode's lease list, where fcntl_getlease() can
+ *     find it.  Since fcntl_getlease() only reports whether the current
+ *     task holds a lease, a cluster filesystem need only do this for
+ *     leases held by processes on this node.
+ *
+ *     There is also no break_lease method; filesystems that
+ *     handle their own leases shoud break leases themselves from the
+ *     filesystem's open, create, and (on truncate) setattr methods.
+ *
+ *     Warning: the only current setlease methods exist only to disable
+ *     leases in certain cases.  More vfs changes may be required to
+ *     allow a full filesystem lease implementation.
  */
 
-int setlease(struct file *filp, long arg, struct file_lock **lease)
+int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
 {
-       struct dentry *dentry = filp->f_path.dentry;
-       struct inode *inode = dentry->d_inode;
        int error;
 
-       if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
-               return -EACCES;
-       if (!S_ISREG(inode->i_mode))
-               return -EINVAL;
-       error = security_file_lock(filp, arg);
-       if (error)
-               return error;
-
        lock_kernel();
-       error = __setlease(filp, arg, lease);
+       if (filp->f_op && filp->f_op->setlease)
+               error = filp->f_op->setlease(filp, arg, lease);
+       else
+               error = generic_setlease(filp, arg, lease);
        unlock_kernel();
 
        return error;
 }
-
-EXPORT_SYMBOL(setlease);
+EXPORT_SYMBOL_GPL(vfs_setlease);
 
 /**
  *     fcntl_setlease  -       sets a lease on an open file
@@ -1472,14 +1489,6 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
        struct inode *inode = dentry->d_inode;
        int error;
 
-       if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
-               return -EACCES;
-       if (!S_ISREG(inode->i_mode))
-               return -EINVAL;
-       error = security_file_lock(filp, arg);
-       if (error)
-               return error;
-
        locks_init_lock(&fl);
        error = lease_init(filp, arg, &fl);
        if (error)
@@ -1487,15 +1496,15 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
 
        lock_kernel();
 
-       error = __setlease(filp, arg, &flp);
+       error = vfs_setlease(filp, arg, &flp);
        if (error || arg == F_UNLCK)
                goto out_unlock;
 
        error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
        if (error < 0) {
-               /* remove lease just inserted by __setlease */
+               /* remove lease just inserted by setlease */
                flp->fl_type = F_UNLCK | F_INPROGRESS;
-               flp->fl_break_time = jiffies- 10;
+               flp->fl_break_time = jiffies - 10;
                time_out_leases(inode);
                goto out_unlock;
        }
@@ -1600,8 +1609,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
 /**
  * vfs_test_lock - test file byte range lock
  * @filp: The file to test lock for
- * @fl: The lock to test
- * @conf: Place to return a copy of the conflicting lock, if found
+ * @fl: The lock to test; also used to hold result
  *
  * Returns -ERRNO on failure.  Indicates presence of conflicting lock by
  * setting conf->fl_type to something other than F_UNLCK.
@@ -1632,6 +1640,7 @@ static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
        flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
                fl->fl_end - fl->fl_start + 1;
        flock->l_whence = 0;
+       flock->l_type = fl->fl_type;
        return 0;
 }
 
@@ -1752,9 +1761,7 @@ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
        /* Don't allow mandatory locks on files that may be memory mapped
         * and shared.
         */
-       if (IS_MANDLOCK(inode) &&
-           (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
-           mapping_writably_mapped(filp->f_mapping)) {
+       if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
                error = -EAGAIN;
                goto out;
        }
@@ -1878,9 +1885,7 @@ int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
        /* Don't allow mandatory locks on files that may be memory mapped
         * and shared.
         */
-       if (IS_MANDLOCK(inode) &&
-           (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
-           mapping_writably_mapped(filp->f_mapping)) {
+       if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
                error = -EAGAIN;
                goto out;
        }
@@ -2074,9 +2079,7 @@ static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
                out += sprintf(out, "%6s %s ",
                             (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
                             (inode == NULL) ? "*NOINODE*" :
-                            (IS_MANDLOCK(inode) &&
-                             (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
-                            "MANDATORY" : "ADVISORY ");
+                            mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
        } else if (IS_FLOCK(fl)) {
                if (fl->fl_type & LOCK_MAND) {
                        out += sprintf(out, "FLOCK  MSNFS     ");
@@ -2276,7 +2279,7 @@ static int __init filelock_init(void)
 {
        filelock_cache = kmem_cache_create("file_lock_cache",
                        sizeof(struct file_lock), 0, SLAB_PANIC,
-                       init_once, NULL);
+                       init_once);
        return 0;
 }