We shouldn't really be using a read-only file descriptor to take a write lock. Most filesystems will put up with it. But NFS, for example, won't.
Unified · Split
| @@ -40,13 +40,15 @@ nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, · 39 unmodified lines | ||
| 40 | 40 | |
| 41 | 41 | /* Obtain file pointer. Not used by FREE_ALL call. */ |
| 42 | 42 | if (filp != NULL) { |
| 43 | + int mode = lock_to_openmode(&lock->fl); | |
| 44 | + | |
| 43 | 45 | error = nlm_lookup_file(rqstp, &file, lock); |
| 44 | 46 | if (error) |
| 45 | 47 | goto no_locks; |
| 46 | 48 | *filp = file; |
| 47 | 49 | |
| 48 | 50 | /* Set up the missing parts of the file_lock structure */ |
| 49 | − lock->fl.fl_file = file->f_file; | |
| 51 | + lock->fl.fl_file = file->f_file[mode]; | |
| 50 | 52 | lock->fl.fl_pid = current->tgid; |
| 51 | 53 | lock->fl.fl_lmops = &nlmsvc_lock_operations; |
| 52 | 54 | nlmsvc_locks_init_private(&lock->fl, host, (pid_t)lock->svid); |
| @@ -471,6 +471,7 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · 470 unmodified lines | ||
| 471 | 471 | { |
| 472 | 472 | struct nlm_block *block = NULL; |
| 473 | 473 | int error; |
| 474 | + int mode; | |
| 474 | 475 | __be32 ret; |
| 475 | 476 | |
| 476 | 477 | dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n", |
| @@ -524,7 +525,8 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, · 47 unmodified lines | ||
| 524 | 525 | |
| 525 | 526 | if (!wait) |
| 526 | 527 | lock->fl.fl_flags &= ~FL_SLEEP; |
| 527 | − error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | |
| 528 | + mode = lock_to_openmode(&lock->fl); | |
| 529 | + error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL); | |
| 528 | 530 | lock->fl.fl_flags &= ~FL_SLEEP; |
| 529 | 531 | |
| 530 | 532 | dprintk("lockd: vfs_lock_file returned %d\n", error); |
| @@ -577,6 +579,7 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · 46 unmodified lines | ||
| 577 | 579 | struct nlm_lock *conflock, struct nlm_cookie *cookie) |
| 578 | 580 | { |
| 579 | 581 | int error; |
| 582 | + int mode; | |
| 580 | 583 | __be32 ret; |
| 581 | 584 | struct nlm_lockowner *test_owner; |
| 582 | 585 | |
| @@ -595,7 +598,8 @@ nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, · 12 unmodified lines | ||
| 595 | 598 | /* If there's a conflicting lock, remember to clean up the test lock */ |
| 596 | 599 | test_owner = (struct nlm_lockowner *)lock->fl.fl_owner; |
| 597 | 600 | |
| 598 | − error = vfs_test_lock(file->f_file, &lock->fl); | |
| 601 | + mode = lock_to_openmode(&lock->fl); | |
| 602 | + error = vfs_test_lock(file->f_file[mode], &lock->fl); | |
| 599 | 603 | if (error) { |
| 600 | 604 | /* We can't currently deal with deferred test requests */ |
| 601 | 605 | if (error == FILE_LOCK_DEFERRED) |
| @@ -641,7 +645,7 @@ out: · 39 unmodified lines | ||
| 641 | 645 | __be32 |
| 642 | 646 | nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock) |
| 643 | 647 | { |
| 644 | − int error; | |
| 648 | + int error = 0; | |
| 645 | 649 | |
| 646 | 650 | dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n", |
| 647 | 651 | nlmsvc_file_inode(file)->i_sb->s_id, |
| @@ -654,7 +658,12 @@ nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock) · 6 unmodified lines | ||
| 654 | 658 | nlmsvc_cancel_blocked(net, file, lock); |
| 655 | 659 | |
| 656 | 660 | lock->fl.fl_type = F_UNLCK; |
| 657 | − error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | |
| 661 | + if (file->f_file[O_RDONLY]) | |
| 662 | + error = vfs_lock_file(file->f_file[O_RDONLY], F_SETLK, | |
| 663 | + &lock->fl, NULL); | |
| 664 | + if (file->f_file[O_WRONLY]) | |
| 665 | + error = vfs_lock_file(file->f_file[O_WRONLY], F_SETLK, | |
| 666 | + &lock->fl, NULL); | |
| 658 | 667 | |
| 659 | 668 | return (error < 0)? nlm_lck_denied_nolocks : nlm_granted; |
| 660 | 669 | } |
| @@ -671,6 +680,7 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l · 10 unmodified lines | ||
| 671 | 680 | { |
| 672 | 681 | struct nlm_block *block; |
| 673 | 682 | int status = 0; |
| 683 | + int mode; | |
| 674 | 684 | |
| 675 | 685 | dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n", |
| 676 | 686 | nlmsvc_file_inode(file)->i_sb->s_id, |
| @@ -686,7 +696,8 @@ nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *l · 9 unmodified lines | ||
| 686 | 696 | block = nlmsvc_lookup_block(file, lock); |
| 687 | 697 | mutex_unlock(&file->f_mutex); |
| 688 | 698 | if (block != NULL) { |
| 689 | − vfs_cancel_lock(block->b_file->f_file, | |
| 699 | + mode = lock_to_openmode(&lock->fl); | |
| 700 | + vfs_cancel_lock(block->b_file->f_file[mode], | |
| 690 | 701 | &block->b_call->a_args.lock.fl); |
| 691 | 702 | status = nlmsvc_unlink_block(block); |
| 692 | 703 | nlmsvc_release_block(block); |
| @@ -803,6 +814,7 @@ nlmsvc_grant_blocked(struct nlm_block *block) · 110 unmodified lines | ||
| 803 | 814 | { |
| 804 | 815 | struct nlm_file *file = block->b_file; |
| 805 | 816 | struct nlm_lock *lock = &block->b_call->a_args.lock; |
| 817 | + int mode; | |
| 806 | 818 | int error; |
| 807 | 819 | loff_t fl_start, fl_end; |
| 808 | 820 | |
| @@ -828,7 +840,8 @@ nlmsvc_grant_blocked(struct nlm_block *block) · 19 unmodified lines | ||
| 828 | 840 | lock->fl.fl_flags |= FL_SLEEP; |
| 829 | 841 | fl_start = lock->fl.fl_start; |
| 830 | 842 | fl_end = lock->fl.fl_end; |
| 831 | − error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | |
| 843 | + mode = lock_to_openmode(&lock->fl); | |
| 844 | + error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL); | |
| 832 | 845 | lock->fl.fl_flags &= ~FL_SLEEP; |
| 833 | 846 | lock->fl.fl_start = fl_start; |
| 834 | 847 | lock->fl.fl_end = fl_end; |
| @@ -55,6 +55,7 @@ nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, · 54 unmodified lines | ||
| 55 | 55 | struct nlm_host *host = NULL; |
| 56 | 56 | struct nlm_file *file = NULL; |
| 57 | 57 | struct nlm_lock *lock = &argp->lock; |
| 58 | + int mode; | |
| 58 | 59 | __be32 error = 0; |
| 59 | 60 | |
| 60 | 61 | /* nfsd callbacks must have been installed for this procedure */ |
| @@ -75,7 +76,8 @@ nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp, · 14 unmodified lines | ||
| 75 | 76 | *filp = file; |
| 76 | 77 | |
| 77 | 78 | /* Set up the missing parts of the file_lock structure */ |
| 78 | − lock->fl.fl_file = file->f_file; | |
| 79 | + mode = lock_to_openmode(&lock->fl); | |
| 80 | + lock->fl.fl_file = file->f_file[mode]; | |
| 79 | 81 | lock->fl.fl_pid = current->tgid; |
| 80 | 82 | lock->fl.fl_lmops = &nlmsvc_lock_operations; |
| 81 | 83 | nlmsvc_locks_init_private(&lock->fl, host, (pid_t)lock->svid); |
| @@ -71,14 +71,35 @@ static inline unsigned int file_hash(struct nfs_fh *f) · 70 unmodified lines | ||
| 71 | 71 | return tmp & (FILE_NRHASH - 1); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | +int lock_to_openmode(struct file_lock *lock) | |
| 75 | +{ | |
| 76 | + return (lock->fl_type == F_WRLCK) ? O_WRONLY : O_RDONLY; | |
| 77 | +} | |
| 78 | + | |
| 79 | +/* | |
| 80 | + * Open the file. Note that if we're reexporting, for example, | |
| 81 | + * this could block the lockd thread for a while. | |
| 82 | + * | |
| 83 | + * We have to make sure we have the right credential to open | |
| 84 | + * the file. | |
| 85 | + */ | |
| 86 | +static __be32 nlm_do_fopen(struct svc_rqst *rqstp, | |
| 87 | + struct nlm_file *file, int mode) | |
| 88 | +{ | |
| 89 | + struct file **fp = &file->f_file[mode]; | |
| 90 | + __be32 nfserr; | |
| 91 | + | |
| 92 | + if (*fp) | |
| 93 | + return 0; | |
| 94 | + nfserr = nlmsvc_ops->fopen(rqstp, &file->f_handle, fp, mode); | |
| 95 | + if (nfserr) | |
| 96 | + dprintk("lockd: open failed (error %d)\n", nfserr); | |
| 97 | + return nfserr; | |
| 98 | +} | |
| 99 | + | |
| 74 | 100 | /* |
| 75 | 101 | * Lookup file info. If it doesn't exist, create a file info struct |
| 76 | 102 | * and open a (VFS) file for the given inode. |
| 77 | − * | |
| 78 | − * FIXME: | |
| 79 | − * Note that we open the file O_RDONLY even when creating write locks. | |
| 80 | − * This is not quite right, but for now, we assume the client performs | |
| 81 | − * the proper R/W checking. | |
| 82 | 103 | */ |
| 83 | 104 | __be32 |
| 84 | 105 | nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result, |
| @@ -87,42 +108,38 @@ nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result, · 2 unmodified lines | ||
| 87 | 108 | struct nlm_file *file; |
| 88 | 109 | unsigned int hash; |
| 89 | 110 | __be32 nfserr; |
| 111 | + int mode; | |
| 90 | 112 | |
| 91 | 113 | nlm_debug_print_fh("nlm_lookup_file", &lock->fh); |
| 92 | 114 | |
| 93 | 115 | hash = file_hash(&lock->fh); |
| 116 | + mode = lock_to_openmode(&lock->fl); | |
| 94 | 117 | |
| 95 | 118 | /* Lock file table */ |
| 96 | 119 | mutex_lock(&nlm_file_mutex); |
| 97 | 120 | |
| 98 | 121 | hlist_for_each_entry(file, &nlm_files[hash], f_list) |
| 99 | − if (!nfs_compare_fh(&file->f_handle, &lock->fh)) | |
| 122 | + if (!nfs_compare_fh(&file->f_handle, &lock->fh)) { | |
| 123 | + mutex_lock(&file->f_mutex); | |
| 124 | + nfserr = nlm_do_fopen(rqstp, file, mode); | |
| 125 | + mutex_unlock(&file->f_mutex); | |
| 100 | 126 | goto found; |
| 101 | − | |
| 127 | + } | |
| 102 | 128 | nlm_debug_print_fh("creating file for", &lock->fh); |
| 103 | 129 | |
| 104 | 130 | nfserr = nlm_lck_denied_nolocks; |
| 105 | 131 | file = kzalloc(sizeof(*file), GFP_KERNEL); |
| 106 | 132 | if (!file) |
| 107 | − goto out_unlock; | |
| 133 | + goto out_free; | |
| 108 | 134 | |
| 109 | 135 | memcpy(&file->f_handle, &lock->fh, sizeof(struct nfs_fh)); |
| 110 | 136 | mutex_init(&file->f_mutex); |
| 111 | 137 | INIT_HLIST_NODE(&file->f_list); |
| 112 | 138 | INIT_LIST_HEAD(&file->f_blocks); |
| 113 | 139 | |
| 114 | − /* | |
| 115 | − * Open the file. Note that if we're reexporting, for example, | |
| 116 | − * this could block the lockd thread for a while. | |
| 117 | − * | |
| 118 | − * We have to make sure we have the right credential to open | |
| 119 | − * the file. | |
| 120 | − */ | |
| 121 | − nfserr = nlmsvc_ops->fopen(rqstp, &lock->fh, &file->f_file); | |
| 122 | − if (nfserr) { | |
| 123 | − dprintk("lockd: open failed (error %d)\n", nfserr); | |
| 124 | − goto out_free; | |
| 125 | − } | |
| 140 | + nfserr = nlm_do_fopen(rqstp, file, mode); | |
| 141 | + if (nfserr) | |
| 142 | + goto out_unlock; | |
| 126 | 143 | |
| 127 | 144 | hlist_add_head(&file->f_list, &nlm_files[hash]); |
| 128 | 145 | |
| @@ -130,7 +147,6 @@ found: · 1 unmodified line | ||
| 130 | 147 | dprintk("lockd: found file %p (count %d)\n", file, file->f_count); |
| 131 | 148 | *result = file; |
| 132 | 149 | file->f_count++; |
| 133 | − nfserr = 0; | |
| 134 | 150 | |
| 135 | 151 | out_unlock: |
| 136 | 152 | mutex_unlock(&nlm_file_mutex); |
| @@ -150,13 +166,34 @@ nlm_delete_file(struct nlm_file *file) · 13 unmodified lines | ||
| 150 | 166 | nlm_debug_print_file("closing file", file); |
| 151 | 167 | if (!hlist_unhashed(&file->f_list)) { |
| 152 | 168 | hlist_del(&file->f_list); |
| 153 | − nlmsvc_ops->fclose(file->f_file); | |
| 169 | + if (file->f_file[O_RDONLY]) | |
| 170 | + nlmsvc_ops->fclose(file->f_file[O_RDONLY]); | |
| 171 | + if (file->f_file[O_WRONLY]) | |
| 172 | + nlmsvc_ops->fclose(file->f_file[O_WRONLY]); | |
| 154 | 173 | kfree(file); |
| 155 | 174 | } else { |
| 156 | 175 | printk(KERN_WARNING "lockd: attempt to release unknown file!\n"); |
| 157 | 176 | } |
| 158 | 177 | } |
| 159 | 178 | |
| 179 | +static int nlm_unlock_files(struct nlm_file *file) | |
| 180 | +{ | |
| 181 | + struct file_lock lock; | |
| 182 | + struct file *f; | |
| 183 | + | |
| 184 | + lock.fl_type = F_UNLCK; | |
| 185 | + lock.fl_start = 0; | |
| 186 | + lock.fl_end = OFFSET_MAX; | |
| 187 | + for (f = file->f_file[0]; f <= file->f_file[1]; f++) { | |
| 188 | + if (f && vfs_lock_file(f, F_SETLK, &lock, NULL) < 0) { | |
| 189 | + pr_warn("lockd: unlock failure in %s:%d\n", | |
| 190 | + __FILE__, __LINE__); | |
| 191 | + return 1; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + return 0; | |
| 195 | +} | |
| 196 | + | |
| 160 | 197 | /* |
| 161 | 198 | * Loop over all locks on the given file and perform the specified |
| 162 | 199 | * action. |
| @@ -184,17 +221,10 @@ again: · 21 unmodified lines | ||
| 184 | 221 | |
| 185 | 222 | lockhost = ((struct nlm_lockowner *)fl->fl_owner)->host; |
| 186 | 223 | if (match(lockhost, host)) { |
| 187 | − struct file_lock lock = *fl; | |
| 188 | 224 | |
| 189 | 225 | spin_unlock(&flctx->flc_lock); |
| 190 | − lock.fl_type = F_UNLCK; | |
| 191 | − lock.fl_start = 0; | |
| 192 | − lock.fl_end = OFFSET_MAX; | |
| 193 | − if (vfs_lock_file(file->f_file, F_SETLK, &lock, NULL) < 0) { | |
| 194 | − printk("lockd: unlock failure in %s:%d\n", | |
| 195 | − __FILE__, __LINE__); | |
| 226 | + if (nlm_unlock_files(file)) | |
| 196 | 227 | return 1; |
| 197 | − } | |
| 198 | 228 | goto again; |
| 199 | 229 | } |
| 200 | 230 | } |
| @@ -248,6 +278,15 @@ nlm_file_inuse(struct nlm_file *file) · 47 unmodified lines | ||
| 248 | 278 | return 0; |
| 249 | 279 | } |
| 250 | 280 | |
| 281 | +static void nlm_close_files(struct nlm_file *file) | |
| 282 | +{ | |
| 283 | + struct file *f; | |
| 284 | + | |
| 285 | + for (f = file->f_file[0]; f <= file->f_file[1]; f++) | |
| 286 | + if (f) | |
| 287 | + nlmsvc_ops->fclose(f); | |
| 288 | +} | |
| 289 | + | |
| 251 | 290 | /* |
| 252 | 291 | * Loop over all files in the file table. |
| 253 | 292 | */ |
| @@ -278,7 +317,7 @@ nlm_traverse_files(void *data, nlm_host_match_fn_t match, · 24 unmodified lines | ||
| 278 | 317 | if (list_empty(&file->f_blocks) && !file->f_locks |
| 279 | 318 | && !file->f_shares && !file->f_count) { |
| 280 | 319 | hlist_del(&file->f_list); |
| 281 | − nlmsvc_ops->fclose(file->f_file); | |
| 320 | + nlm_close_files(file); | |
| 282 | 321 | kfree(file); |
| 283 | 322 | } |
| 284 | 323 | } |
| @@ -412,6 +451,7 @@ nlmsvc_invalidate_all(void) · 127 unmodified lines | ||
| 412 | 451 | nlm_traverse_files(NULL, nlmsvc_is_client, NULL); |
| 413 | 452 | } |
| 414 | 453 | |
| 454 | + | |
| 415 | 455 | static int |
| 416 | 456 | nlmsvc_match_sb(void *datap, struct nlm_file *file) |
| 417 | 457 | { |
| @@ -25,9 +25,11 @@ · 24 unmodified lines | ||
| 25 | 25 | * Note: we hold the dentry use count while the file is open. |
| 26 | 26 | */ |
| 27 | 27 | static __be32 |
| 28 | −nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file **filp) | |
| 28 | +nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file **filp, | |
| 29 | + int mode) | |
| 29 | 30 | { |
| 30 | 31 | __be32 nfserr; |
| 32 | + int access; | |
| 31 | 33 | struct svc_fh fh; |
| 32 | 34 | |
| 33 | 35 | /* must initialize before using! but maxsize doesn't matter */ |
| @@ -36,7 +38,9 @@ nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file **filp) · 2 unmodified lines | ||
| 36 | 38 | memcpy((char*)&fh.fh_handle.fh_base, f->data, f->size); |
| 37 | 39 | fh.fh_export = NULL; |
| 38 | 40 | |
| 39 | − nfserr = nfsd_open(rqstp, &fh, S_IFREG, NFSD_MAY_LOCK, filp); | |
| 41 | + access = (mode == O_WRONLY) ? NFSD_MAY_WRITE : NFSD_MAY_READ; | |
| 42 | + access |= NFSD_MAY_LOCK; | |
| 43 | + nfserr = nfsd_open(rqstp, &fh, S_IFREG, access, filp); | |
| 40 | 44 | fh_put(&fh); |
| 41 | 45 | /* We return nlm error codes as nlm doesn't know |
| 42 | 46 | * about nfsd, but nfsd does know about nlm.. |
| @@ -27,7 +27,8 @@ struct rpc_task; · 26 unmodified lines | ||
| 27 | 27 | struct nlmsvc_binding { |
| 28 | 28 | __be32 (*fopen)(struct svc_rqst *, |
| 29 | 29 | struct nfs_fh *, |
| 30 | − struct file **); | |
| 30 | + struct file **, | |
| 31 | + int mode); | |
| 31 | 32 | void (*fclose)(struct file *); |
| 32 | 33 | }; |
| 33 | 34 | |
| @@ -10,6 +10,8 @@ · 9 unmodified lines | ||
| 10 | 10 | #ifndef LINUX_LOCKD_LOCKD_H |
| 11 | 11 | #define LINUX_LOCKD_LOCKD_H |
| 12 | 12 | |
| 13 | +/* XXX: a lot of this should really be under fs/lockd. */ | |
| 14 | + | |
| 13 | 15 | #include <linux/in.h> |
| 14 | 16 | #include <linux/in6.h> |
| 15 | 17 | #include <net/ipv6.h> |
| @@ -154,7 +156,8 @@ struct nlm_rqst { · 138 unmodified lines | ||
| 154 | 156 | struct nlm_file { |
| 155 | 157 | struct hlist_node f_list; /* linked list */ |
| 156 | 158 | struct nfs_fh f_handle; /* NFS file handle */ |
| 157 | − struct file * f_file; /* VFS file pointer */ | |
| 159 | + struct file * f_file[2]; /* VFS file pointers, | |
| 160 | + indexed by O_ flags */ | |
| 158 | 161 | struct nlm_share * f_shares; /* DOS shares */ |
| 159 | 162 | struct list_head f_blocks; /* blocked locks */ |
| 160 | 163 | unsigned int f_locks; /* guesstimate # of locks */ |
| @@ -267,6 +270,7 @@ typedef int (*nlm_host_match_fn_t)(void *cur, struct nlm_host *ref); · 106 unmodified lines | ||
| 267 | 270 | /* |
| 268 | 271 | * Server-side lock handling |
| 269 | 272 | */ |
| 273 | +int lock_to_openmode(struct file_lock *); | |
| 270 | 274 | __be32 nlmsvc_lock(struct svc_rqst *, struct nlm_file *, |
| 271 | 275 | struct nlm_host *, struct nlm_lock *, int, |
| 272 | 276 | struct nlm_cookie *, int); |
| @@ -301,7 +305,8 @@ int nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr); · 28 unmodified lines | ||
| 301 | 305 | |
| 302 | 306 | static inline struct inode *nlmsvc_file_inode(struct nlm_file *file) |
| 303 | 307 | { |
| 304 | − return locks_inode(file->f_file); | |
| 308 | + return locks_inode(file->f_file[O_RDONLY] ? | |
| 309 | + file->f_file[O_RDONLY] : file->f_file[O_WRONLY]); | |
| 305 | 310 | } |
| 306 | 311 | |
| 307 | 312 | static inline int __nlm_privileged_request4(const struct sockaddr *sap) |