aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/exec.c12
-rw-r--r--fs/lockd/host.c4
-rw-r--r--fs/locks.c48
-rw-r--r--fs/namei.c95
-rw-r--r--fs/nfs/delegation.c2
-rw-r--r--fs/nfs/delegation.h16
-rw-r--r--fs/nfs/dir.c49
-rw-r--r--fs/nfs/file.c27
-rw-r--r--fs/nfs/inode.c96
-rw-r--r--fs/nfs/nfs3proc.c4
-rw-r--r--fs/nfs/nfs4_fs.h53
-rw-r--r--fs/nfs/nfs4proc.c581
-rw-r--r--fs/nfs/nfs4state.c181
-rw-r--r--fs/nfs/nfs4xdr.c65
-rw-r--r--fs/nfs/proc.c2
-rw-r--r--fs/open.c79
16 files changed, 876 insertions, 438 deletions
diff --git a/fs/exec.c b/fs/exec.c
index a04a575ad433..d2208f7c87db 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -126,8 +126,7 @@ asmlinkage long sys_uselib(const char __user * library)
126 struct nameidata nd; 126 struct nameidata nd;
127 int error; 127 int error;
128 128
129 nd.intent.open.flags = FMODE_READ; 129 error = __user_path_lookup_open(library, LOOKUP_FOLLOW, &nd, FMODE_READ);
130 error = __user_walk(library, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
131 if (error) 130 if (error)
132 goto out; 131 goto out;
133 132
@@ -139,7 +138,7 @@ asmlinkage long sys_uselib(const char __user * library)
139 if (error) 138 if (error)
140 goto exit; 139 goto exit;
141 140
142 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); 141 file = nameidata_to_filp(&nd, O_RDONLY);
143 error = PTR_ERR(file); 142 error = PTR_ERR(file);
144 if (IS_ERR(file)) 143 if (IS_ERR(file))
145 goto out; 144 goto out;
@@ -167,6 +166,7 @@ asmlinkage long sys_uselib(const char __user * library)
167out: 166out:
168 return error; 167 return error;
169exit: 168exit:
169 release_open_intent(&nd);
170 path_release(&nd); 170 path_release(&nd);
171 goto out; 171 goto out;
172} 172}
@@ -490,8 +490,7 @@ struct file *open_exec(const char *name)
490 int err; 490 int err;
491 struct file *file; 491 struct file *file;
492 492
493 nd.intent.open.flags = FMODE_READ; 493 err = path_lookup_open(name, LOOKUP_FOLLOW, &nd, FMODE_READ);
494 err = path_lookup(name, LOOKUP_FOLLOW|LOOKUP_OPEN, &nd);
495 file = ERR_PTR(err); 494 file = ERR_PTR(err);
496 495
497 if (!err) { 496 if (!err) {
@@ -504,7 +503,7 @@ struct file *open_exec(const char *name)
504 err = -EACCES; 503 err = -EACCES;
505 file = ERR_PTR(err); 504 file = ERR_PTR(err);
506 if (!err) { 505 if (!err) {
507 file = dentry_open(nd.dentry, nd.mnt, O_RDONLY); 506 file = nameidata_to_filp(&nd, O_RDONLY);
508 if (!IS_ERR(file)) { 507 if (!IS_ERR(file)) {
509 err = deny_write_access(file); 508 err = deny_write_access(file);
510 if (err) { 509 if (err) {
@@ -516,6 +515,7 @@ out:
516 return file; 515 return file;
517 } 516 }
518 } 517 }
518 release_open_intent(&nd);
519 path_release(&nd); 519 path_release(&nd);
520 } 520 }
521 goto out; 521 goto out;
diff --git a/fs/lockd/host.c b/fs/lockd/host.c
index 82c77df81c5f..c4c8601096e0 100644
--- a/fs/lockd/host.c
+++ b/fs/lockd/host.c
@@ -173,11 +173,10 @@ nlm_bind_host(struct nlm_host *host)
173 173
174 /* If we've already created an RPC client, check whether 174 /* If we've already created an RPC client, check whether
175 * RPC rebind is required 175 * RPC rebind is required
176 * Note: why keep rebinding if we're on a tcp connection?
177 */ 176 */
178 if ((clnt = host->h_rpcclnt) != NULL) { 177 if ((clnt = host->h_rpcclnt) != NULL) {
179 xprt = clnt->cl_xprt; 178 xprt = clnt->cl_xprt;
180 if (!xprt->stream && time_after_eq(jiffies, host->h_nextrebind)) { 179 if (time_after_eq(jiffies, host->h_nextrebind)) {
181 clnt->cl_port = 0; 180 clnt->cl_port = 0;
182 host->h_nextrebind = jiffies + NLM_HOST_REBIND; 181 host->h_nextrebind = jiffies + NLM_HOST_REBIND;
183 dprintk("lockd: next rebind in %ld jiffies\n", 182 dprintk("lockd: next rebind in %ld jiffies\n",
@@ -189,7 +188,6 @@ nlm_bind_host(struct nlm_host *host)
189 goto forgetit; 188 goto forgetit;
190 189
191 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout); 190 xprt_set_timeout(&xprt->timeout, 5, nlmsvc_timeout);
192 xprt->nocong = 1; /* No congestion control for NLM */
193 xprt->resvport = 1; /* NLM requires a reserved port */ 191 xprt->resvport = 1; /* NLM requires a reserved port */
194 192
195 /* Existing NLM servers accept AUTH_UNIX only */ 193 /* Existing NLM servers accept AUTH_UNIX only */
diff --git a/fs/locks.c b/fs/locks.c
index f7daa5f48949..a1e8b2248014 100644
--- a/fs/locks.c
+++ b/fs/locks.c
@@ -316,21 +316,22 @@ static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
316 /* POSIX-1996 leaves the case l->l_len < 0 undefined; 316 /* POSIX-1996 leaves the case l->l_len < 0 undefined;
317 POSIX-2001 defines it. */ 317 POSIX-2001 defines it. */
318 start += l->l_start; 318 start += l->l_start;
319 end = start + l->l_len - 1; 319 if (start < 0)
320 if (l->l_len < 0) { 320 return -EINVAL;
321 fl->fl_end = OFFSET_MAX;
322 if (l->l_len > 0) {
323 end = start + l->l_len - 1;
324 fl->fl_end = end;
325 } else if (l->l_len < 0) {
321 end = start - 1; 326 end = start - 1;
327 fl->fl_end = end;
322 start += l->l_len; 328 start += l->l_len;
329 if (start < 0)
330 return -EINVAL;
323 } 331 }
324
325 if (start < 0)
326 return -EINVAL;
327 if (l->l_len > 0 && end < 0)
328 return -EOVERFLOW;
329
330 fl->fl_start = start; /* we record the absolute position */ 332 fl->fl_start = start; /* we record the absolute position */
331 fl->fl_end = end; 333 if (fl->fl_end < fl->fl_start)
332 if (l->l_len == 0) 334 return -EOVERFLOW;
333 fl->fl_end = OFFSET_MAX;
334 335
335 fl->fl_owner = current->files; 336 fl->fl_owner = current->files;
336 fl->fl_pid = current->tgid; 337 fl->fl_pid = current->tgid;
@@ -362,14 +363,21 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
362 return -EINVAL; 363 return -EINVAL;
363 } 364 }
364 365
365 if (((start += l->l_start) < 0) || (l->l_len < 0)) 366 start += l->l_start;
367 if (start < 0)
366 return -EINVAL; 368 return -EINVAL;
367 fl->fl_end = start + l->l_len - 1; 369 fl->fl_end = OFFSET_MAX;
368 if (l->l_len > 0 && fl->fl_end < 0) 370 if (l->l_len > 0) {
369 return -EOVERFLOW; 371 fl->fl_end = start + l->l_len - 1;
372 } else if (l->l_len < 0) {
373 fl->fl_end = start - 1;
374 start += l->l_len;
375 if (start < 0)
376 return -EINVAL;
377 }
370 fl->fl_start = start; /* we record the absolute position */ 378 fl->fl_start = start; /* we record the absolute position */
371 if (l->l_len == 0) 379 if (fl->fl_end < fl->fl_start)
372 fl->fl_end = OFFSET_MAX; 380 return -EOVERFLOW;
373 381
374 fl->fl_owner = current->files; 382 fl->fl_owner = current->files;
375 fl->fl_pid = current->tgid; 383 fl->fl_pid = current->tgid;
@@ -829,12 +837,16 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request)
829 /* Detect adjacent or overlapping regions (if same lock type) 837 /* Detect adjacent or overlapping regions (if same lock type)
830 */ 838 */
831 if (request->fl_type == fl->fl_type) { 839 if (request->fl_type == fl->fl_type) {
840 /* In all comparisons of start vs end, use
841 * "start - 1" rather than "end + 1". If end
842 * is OFFSET_MAX, end + 1 will become negative.
843 */
832 if (fl->fl_end < request->fl_start - 1) 844 if (fl->fl_end < request->fl_start - 1)
833 goto next_lock; 845 goto next_lock;
834 /* If the next lock in the list has entirely bigger 846 /* If the next lock in the list has entirely bigger
835 * addresses than the new one, insert the lock here. 847 * addresses than the new one, insert the lock here.
836 */ 848 */
837 if (fl->fl_start > request->fl_end + 1) 849 if (fl->fl_start - 1 > request->fl_end)
838 break; 850 break;
839 851
840 /* If we come here, the new and old lock are of the 852 /* If we come here, the new and old lock are of the
diff --git a/fs/namei.c b/fs/namei.c
index aa62dbda93ac..aaaa81036234 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -28,6 +28,7 @@
28#include <linux/syscalls.h> 28#include <linux/syscalls.h>
29#include <linux/mount.h> 29#include <linux/mount.h>
30#include <linux/audit.h> 30#include <linux/audit.h>
31#include <linux/file.h>
31#include <asm/namei.h> 32#include <asm/namei.h>
32#include <asm/uaccess.h> 33#include <asm/uaccess.h>
33 34
@@ -317,6 +318,18 @@ void path_release_on_umount(struct nameidata *nd)
317 mntput_no_expire(nd->mnt); 318 mntput_no_expire(nd->mnt);
318} 319}
319 320
321/**
322 * release_open_intent - free up open intent resources
323 * @nd: pointer to nameidata
324 */
325void release_open_intent(struct nameidata *nd)
326{
327 if (nd->intent.open.file->f_dentry == NULL)
328 put_filp(nd->intent.open.file);
329 else
330 fput(nd->intent.open.file);
331}
332
320/* 333/*
321 * Internal lookup() using the new generic dcache. 334 * Internal lookup() using the new generic dcache.
322 * SMP-safe 335 * SMP-safe
@@ -750,6 +763,7 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
750 struct qstr this; 763 struct qstr this;
751 unsigned int c; 764 unsigned int c;
752 765
766 nd->flags |= LOOKUP_CONTINUE;
753 err = exec_permission_lite(inode, nd); 767 err = exec_permission_lite(inode, nd);
754 if (err == -EAGAIN) { 768 if (err == -EAGAIN) {
755 err = permission(inode, MAY_EXEC, nd); 769 err = permission(inode, MAY_EXEC, nd);
@@ -802,7 +816,6 @@ static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
802 if (err < 0) 816 if (err < 0)
803 break; 817 break;
804 } 818 }
805 nd->flags |= LOOKUP_CONTINUE;
806 /* This does the actual lookups.. */ 819 /* This does the actual lookups.. */
807 err = do_lookup(nd, &this, &next); 820 err = do_lookup(nd, &this, &next);
808 if (err) 821 if (err)
@@ -1052,6 +1065,70 @@ out:
1052 return retval; 1065 return retval;
1053} 1066}
1054 1067
1068static int __path_lookup_intent_open(const char *name, unsigned int lookup_flags,
1069 struct nameidata *nd, int open_flags, int create_mode)
1070{
1071 struct file *filp = get_empty_filp();
1072 int err;
1073
1074 if (filp == NULL)
1075 return -ENFILE;
1076 nd->intent.open.file = filp;
1077 nd->intent.open.flags = open_flags;
1078 nd->intent.open.create_mode = create_mode;
1079 err = path_lookup(name, lookup_flags|LOOKUP_OPEN, nd);
1080 if (IS_ERR(nd->intent.open.file)) {
1081 if (err == 0) {
1082 err = PTR_ERR(nd->intent.open.file);
1083 path_release(nd);
1084 }
1085 } else if (err != 0)
1086 release_open_intent(nd);
1087 return err;
1088}
1089
1090/**
1091 * path_lookup_open - lookup a file path with open intent
1092 * @name: pointer to file name
1093 * @lookup_flags: lookup intent flags
1094 * @nd: pointer to nameidata
1095 * @open_flags: open intent flags
1096 */
1097int path_lookup_open(const char *name, unsigned int lookup_flags,
1098 struct nameidata *nd, int open_flags)
1099{
1100 return __path_lookup_intent_open(name, lookup_flags, nd,
1101 open_flags, 0);
1102}
1103
1104/**
1105 * path_lookup_create - lookup a file path with open + create intent
1106 * @name: pointer to file name
1107 * @lookup_flags: lookup intent flags
1108 * @nd: pointer to nameidata
1109 * @open_flags: open intent flags
1110 * @create_mode: create intent flags
1111 */
1112int path_lookup_create(const char *name, unsigned int lookup_flags,
1113 struct nameidata *nd, int open_flags, int create_mode)
1114{
1115 return __path_lookup_intent_open(name, lookup_flags|LOOKUP_CREATE, nd,
1116 open_flags, create_mode);
1117}
1118
1119int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1120 struct nameidata *nd, int open_flags)
1121{
1122 char *tmp = getname(name);
1123 int err = PTR_ERR(tmp);
1124
1125 if (!IS_ERR(tmp)) {
1126 err = __path_lookup_intent_open(tmp, lookup_flags, nd, open_flags, 0);
1127 putname(tmp);
1128 }
1129 return err;
1130}
1131
1055/* 1132/*
1056 * Restricted form of lookup. Doesn't follow links, single-component only, 1133 * Restricted form of lookup. Doesn't follow links, single-component only,
1057 * needs parent already locked. Doesn't follow mounts. 1134 * needs parent already locked. Doesn't follow mounts.
@@ -1416,27 +1493,27 @@ int may_open(struct nameidata *nd, int acc_mode, int flag)
1416 */ 1493 */
1417int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd) 1494int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1418{ 1495{
1419 int acc_mode, error = 0; 1496 int acc_mode, error;
1420 struct path path; 1497 struct path path;
1421 struct dentry *dir; 1498 struct dentry *dir;
1422 int count = 0; 1499 int count = 0;
1423 1500
1424 acc_mode = ACC_MODE(flag); 1501 acc_mode = ACC_MODE(flag);
1425 1502
1503 /* O_TRUNC implies we need access checks for write permissions */
1504 if (flag & O_TRUNC)
1505 acc_mode |= MAY_WRITE;
1506
1426 /* Allow the LSM permission hook to distinguish append 1507 /* Allow the LSM permission hook to distinguish append
1427 access from general write access. */ 1508 access from general write access. */
1428 if (flag & O_APPEND) 1509 if (flag & O_APPEND)
1429 acc_mode |= MAY_APPEND; 1510 acc_mode |= MAY_APPEND;
1430 1511
1431 /* Fill in the open() intent data */
1432 nd->intent.open.flags = flag;
1433 nd->intent.open.create_mode = mode;
1434
1435 /* 1512 /*
1436 * The simplest case - just a plain lookup. 1513 * The simplest case - just a plain lookup.
1437 */ 1514 */
1438 if (!(flag & O_CREAT)) { 1515 if (!(flag & O_CREAT)) {
1439 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd); 1516 error = path_lookup_open(pathname, lookup_flags(flag), nd, flag);
1440 if (error) 1517 if (error)
1441 return error; 1518 return error;
1442 goto ok; 1519 goto ok;
@@ -1445,7 +1522,7 @@ int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1445 /* 1522 /*
1446 * Create - we need to know the parent. 1523 * Create - we need to know the parent.
1447 */ 1524 */
1448 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd); 1525 error = path_lookup_create(pathname, LOOKUP_PARENT, nd, flag, mode);
1449 if (error) 1526 if (error)
1450 return error; 1527 return error;
1451 1528
@@ -1520,6 +1597,8 @@ ok:
1520exit_dput: 1597exit_dput:
1521 dput_path(&path, nd); 1598 dput_path(&path, nd);
1522exit: 1599exit:
1600 if (!IS_ERR(nd->intent.open.file))
1601 release_open_intent(nd);
1523 path_release(nd); 1602 path_release(nd);
1524 return error; 1603 return error;
1525 1604
diff --git a/fs/nfs/delegation.c b/fs/nfs/delegation.c
index 4a36839f0bbd..44135af9894c 100644
--- a/fs/nfs/delegation.c
+++ b/fs/nfs/delegation.c
@@ -142,7 +142,7 @@ static void nfs_msync_inode(struct inode *inode)
142/* 142/*
143 * Basic procedure for returning a delegation to the server 143 * Basic procedure for returning a delegation to the server
144 */ 144 */
145int nfs_inode_return_delegation(struct inode *inode) 145int __nfs_inode_return_delegation(struct inode *inode)
146{ 146{
147 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state; 147 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
148 struct nfs_inode *nfsi = NFS_I(inode); 148 struct nfs_inode *nfsi = NFS_I(inode);
diff --git a/fs/nfs/delegation.h b/fs/nfs/delegation.h
index 3f6c45a29d6a..8017846b561f 100644
--- a/fs/nfs/delegation.h
+++ b/fs/nfs/delegation.h
@@ -25,7 +25,7 @@ struct nfs_delegation {
25 25
26int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res); 26int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res);
27void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res); 27void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res);
28int nfs_inode_return_delegation(struct inode *inode); 28int __nfs_inode_return_delegation(struct inode *inode);
29int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid); 29int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid);
30 30
31struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle); 31struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle);
@@ -47,11 +47,25 @@ static inline int nfs_have_delegation(struct inode *inode, int flags)
47 return 1; 47 return 1;
48 return 0; 48 return 0;
49} 49}
50
51static inline int nfs_inode_return_delegation(struct inode *inode)
52{
53 int err = 0;
54
55 if (NFS_I(inode)->delegation != NULL)
56 err = __nfs_inode_return_delegation(inode);
57 return err;
58}
50#else 59#else
51static inline int nfs_have_delegation(struct inode *inode, int flags) 60static inline int nfs_have_delegation(struct inode *inode, int flags)
52{ 61{
53 return 0; 62 return 0;
54} 63}
64
65static inline int nfs_inode_return_delegation(struct inode *inode)
66{
67 return 0;
68}
55#endif 69#endif
56 70
57#endif 71#endif
diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
index 2df639f143e8..eb50c19fc253 100644
--- a/fs/nfs/dir.c
+++ b/fs/nfs/dir.c
@@ -565,8 +565,6 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
565 } 565 }
566 } 566 }
567 unlock_kernel(); 567 unlock_kernel();
568 if (desc->error < 0)
569 return desc->error;
570 if (res < 0) 568 if (res < 0)
571 return res; 569 return res;
572 return 0; 570 return 0;
@@ -803,6 +801,7 @@ static int nfs_dentry_delete(struct dentry *dentry)
803 */ 801 */
804static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) 802static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
805{ 803{
804 nfs_inode_return_delegation(inode);
806 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { 805 if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
807 lock_kernel(); 806 lock_kernel();
808 inode->i_nlink--; 807 inode->i_nlink--;
@@ -916,7 +915,6 @@ static int is_atomic_open(struct inode *dir, struct nameidata *nd)
916static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 915static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
917{ 916{
918 struct dentry *res = NULL; 917 struct dentry *res = NULL;
919 struct inode *inode = NULL;
920 int error; 918 int error;
921 919
922 /* Check that we are indeed trying to open this file */ 920 /* Check that we are indeed trying to open this file */
@@ -930,8 +928,10 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
930 dentry->d_op = NFS_PROTO(dir)->dentry_ops; 928 dentry->d_op = NFS_PROTO(dir)->dentry_ops;
931 929
932 /* Let vfs_create() deal with O_EXCL */ 930 /* Let vfs_create() deal with O_EXCL */
933 if (nd->intent.open.flags & O_EXCL) 931 if (nd->intent.open.flags & O_EXCL) {
934 goto no_entry; 932 d_add(dentry, NULL);
933 goto out;
934 }
935 935
936 /* Open the file on the server */ 936 /* Open the file on the server */
937 lock_kernel(); 937 lock_kernel();
@@ -945,32 +945,30 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
945 945
946 if (nd->intent.open.flags & O_CREAT) { 946 if (nd->intent.open.flags & O_CREAT) {
947 nfs_begin_data_update(dir); 947 nfs_begin_data_update(dir);
948 inode = nfs4_atomic_open(dir, dentry, nd); 948 res = nfs4_atomic_open(dir, dentry, nd);
949 nfs_end_data_update(dir); 949 nfs_end_data_update(dir);
950 } else 950 } else
951 inode = nfs4_atomic_open(dir, dentry, nd); 951 res = nfs4_atomic_open(dir, dentry, nd);
952 unlock_kernel(); 952 unlock_kernel();
953 if (IS_ERR(inode)) { 953 if (IS_ERR(res)) {
954 error = PTR_ERR(inode); 954 error = PTR_ERR(res);
955 switch (error) { 955 switch (error) {
956 /* Make a negative dentry */ 956 /* Make a negative dentry */
957 case -ENOENT: 957 case -ENOENT:
958 inode = NULL; 958 res = NULL;
959 break; 959 goto out;
960 /* This turned out not to be a regular file */ 960 /* This turned out not to be a regular file */
961 case -EISDIR:
962 case -ENOTDIR:
963 goto no_open;
961 case -ELOOP: 964 case -ELOOP:
962 if (!(nd->intent.open.flags & O_NOFOLLOW)) 965 if (!(nd->intent.open.flags & O_NOFOLLOW))
963 goto no_open; 966 goto no_open;
964 /* case -EISDIR: */
965 /* case -EINVAL: */ 967 /* case -EINVAL: */
966 default: 968 default:
967 res = ERR_PTR(error);
968 goto out; 969 goto out;
969 } 970 }
970 } 971 } else if (res != NULL)
971no_entry:
972 res = d_add_unique(dentry, inode);
973 if (res != NULL)
974 dentry = res; 972 dentry = res;
975 nfs_renew_times(dentry); 973 nfs_renew_times(dentry);
976 nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); 974 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
@@ -1014,7 +1012,7 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
1014 */ 1012 */
1015 lock_kernel(); 1013 lock_kernel();
1016 verifier = nfs_save_change_attribute(dir); 1014 verifier = nfs_save_change_attribute(dir);
1017 ret = nfs4_open_revalidate(dir, dentry, openflags); 1015 ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
1018 if (!ret) 1016 if (!ret)
1019 nfs_set_verifier(dentry, verifier); 1017 nfs_set_verifier(dentry, verifier);
1020 unlock_kernel(); 1018 unlock_kernel();
@@ -1137,7 +1135,7 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
1137 1135
1138 lock_kernel(); 1136 lock_kernel();
1139 nfs_begin_data_update(dir); 1137 nfs_begin_data_update(dir);
1140 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); 1138 error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
1141 nfs_end_data_update(dir); 1139 nfs_end_data_update(dir);
1142 if (error != 0) 1140 if (error != 0)
1143 goto out_err; 1141 goto out_err;
@@ -1332,6 +1330,7 @@ static int nfs_safe_remove(struct dentry *dentry)
1332 1330
1333 nfs_begin_data_update(dir); 1331 nfs_begin_data_update(dir);
1334 if (inode != NULL) { 1332 if (inode != NULL) {
1333 nfs_inode_return_delegation(inode);
1335 nfs_begin_data_update(inode); 1334 nfs_begin_data_update(inode);
1336 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name); 1335 error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
1337 /* The VFS may want to delete this inode */ 1336 /* The VFS may want to delete this inode */
@@ -1512,9 +1511,11 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1512 */ 1511 */
1513 if (!new_inode) 1512 if (!new_inode)
1514 goto go_ahead; 1513 goto go_ahead;
1515 if (S_ISDIR(new_inode->i_mode)) 1514 if (S_ISDIR(new_inode->i_mode)) {
1516 goto out; 1515 error = -EISDIR;
1517 else if (atomic_read(&new_dentry->d_count) > 2) { 1516 if (!S_ISDIR(old_inode->i_mode))
1517 goto out;
1518 } else if (atomic_read(&new_dentry->d_count) > 2) {
1518 int err; 1519 int err;
1519 /* copy the target dentry's name */ 1520 /* copy the target dentry's name */
1520 dentry = d_alloc(new_dentry->d_parent, 1521 dentry = d_alloc(new_dentry->d_parent,
@@ -1539,7 +1540,8 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1539#endif 1540#endif
1540 goto out; 1541 goto out;
1541 } 1542 }
1542 } 1543 } else
1544 new_inode->i_nlink--;
1543 1545
1544go_ahead: 1546go_ahead:
1545 /* 1547 /*
@@ -1549,6 +1551,7 @@ go_ahead:
1549 nfs_wb_all(old_inode); 1551 nfs_wb_all(old_inode);
1550 shrink_dcache_parent(old_dentry); 1552 shrink_dcache_parent(old_dentry);
1551 } 1553 }
1554 nfs_inode_return_delegation(old_inode);
1552 1555
1553 if (new_inode) 1556 if (new_inode)
1554 d_delete(new_dentry); 1557 d_delete(new_dentry);
diff --git a/fs/nfs/file.c b/fs/nfs/file.c
index 6bdcfa95de94..572d8593486f 100644
--- a/fs/nfs/file.c
+++ b/fs/nfs/file.c
@@ -376,22 +376,31 @@ out_swapfile:
376 376
377static int do_getlk(struct file *filp, int cmd, struct file_lock *fl) 377static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
378{ 378{
379 struct file_lock *cfl;
379 struct inode *inode = filp->f_mapping->host; 380 struct inode *inode = filp->f_mapping->host;
380 int status = 0; 381 int status = 0;
381 382
382 lock_kernel(); 383 lock_kernel();
383 /* Use local locking if mounted with "-onolock" */ 384 /* Try local locking first */
384 if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)) 385 cfl = posix_test_lock(filp, fl);
385 status = NFS_PROTO(inode)->lock(filp, cmd, fl); 386 if (cfl != NULL) {
386 else { 387 locks_copy_lock(fl, cfl);
387 struct file_lock *cfl = posix_test_lock(filp, fl); 388 goto out;
388
389 fl->fl_type = F_UNLCK;
390 if (cfl != NULL)
391 memcpy(fl, cfl, sizeof(*fl));
392 } 389 }
390
391 if (nfs_have_delegation(inode, FMODE_READ))
392 goto out_noconflict;
393
394 if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
395 goto out_noconflict;
396
397 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
398out:
393 unlock_kernel(); 399 unlock_kernel();
394 return status; 400 return status;
401out_noconflict:
402 fl->fl_type = F_UNLCK;
403 goto out;
395} 404}
396 405
397static int do_vfs_lock(struct file *file, struct file_lock *fl) 406static int do_vfs_lock(struct file *file, struct file_lock *fl)
diff --git a/fs/nfs/inode.c b/fs/nfs/inode.c
index d4eadeea128e..65d5ab45ddc5 100644
--- a/fs/nfs/inode.c
+++ b/fs/nfs/inode.c
@@ -358,6 +358,35 @@ out_no_root:
358 return no_root_error; 358 return no_root_error;
359} 359}
360 360
361static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, unsigned int timeo, unsigned int retrans)
362{
363 to->to_initval = timeo * HZ / 10;
364 to->to_retries = retrans;
365 if (!to->to_retries)
366 to->to_retries = 2;
367
368 switch (proto) {
369 case IPPROTO_TCP:
370 if (!to->to_initval)
371 to->to_initval = 60 * HZ;
372 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
373 to->to_initval = NFS_MAX_TCP_TIMEOUT;
374 to->to_increment = to->to_initval;
375 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
376 to->to_exponential = 0;
377 break;
378 case IPPROTO_UDP:
379 default:
380 if (!to->to_initval)
381 to->to_initval = 11 * HZ / 10;
382 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
383 to->to_initval = NFS_MAX_UDP_TIMEOUT;
384 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
385 to->to_exponential = 1;
386 break;
387 }
388}
389
361/* 390/*
362 * Create an RPC client handle. 391 * Create an RPC client handle.
363 */ 392 */
@@ -367,22 +396,12 @@ nfs_create_client(struct nfs_server *server, const struct nfs_mount_data *data)
367 struct rpc_timeout timeparms; 396 struct rpc_timeout timeparms;
368 struct rpc_xprt *xprt = NULL; 397 struct rpc_xprt *xprt = NULL;
369 struct rpc_clnt *clnt = NULL; 398 struct rpc_clnt *clnt = NULL;
370 int tcp = (data->flags & NFS_MOUNT_TCP); 399 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
371 400
372 /* Initialize timeout values */ 401 nfs_init_timeout_values(&timeparms, proto, data->timeo, data->retrans);
373 timeparms.to_initval = data->timeo * HZ / 10;
374 timeparms.to_retries = data->retrans;
375 timeparms.to_maxval = tcp ? RPC_MAX_TCP_TIMEOUT : RPC_MAX_UDP_TIMEOUT;
376 timeparms.to_exponential = 1;
377
378 if (!timeparms.to_initval)
379 timeparms.to_initval = (tcp ? 600 : 11) * HZ / 10;
380 if (!timeparms.to_retries)
381 timeparms.to_retries = 5;
382 402
383 /* create transport and client */ 403 /* create transport and client */
384 xprt = xprt_create_proto(tcp ? IPPROTO_TCP : IPPROTO_UDP, 404 xprt = xprt_create_proto(proto, &server->addr, &timeparms);
385 &server->addr, &timeparms);
386 if (IS_ERR(xprt)) { 405 if (IS_ERR(xprt)) {
387 dprintk("%s: cannot create RPC transport. Error = %ld\n", 406 dprintk("%s: cannot create RPC transport. Error = %ld\n",
388 __FUNCTION__, PTR_ERR(xprt)); 407 __FUNCTION__, PTR_ERR(xprt));
@@ -576,7 +595,6 @@ static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
576 { NFS_MOUNT_SOFT, ",soft", ",hard" }, 595 { NFS_MOUNT_SOFT, ",soft", ",hard" },
577 { NFS_MOUNT_INTR, ",intr", "" }, 596 { NFS_MOUNT_INTR, ",intr", "" },
578 { NFS_MOUNT_POSIX, ",posix", "" }, 597 { NFS_MOUNT_POSIX, ",posix", "" },
579 { NFS_MOUNT_TCP, ",tcp", ",udp" },
580 { NFS_MOUNT_NOCTO, ",nocto", "" }, 598 { NFS_MOUNT_NOCTO, ",nocto", "" },
581 { NFS_MOUNT_NOAC, ",noac", "" }, 599 { NFS_MOUNT_NOAC, ",noac", "" },
582 { NFS_MOUNT_NONLM, ",nolock", ",lock" }, 600 { NFS_MOUNT_NONLM, ",nolock", ",lock" },
@@ -585,6 +603,8 @@ static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
585 }; 603 };
586 struct proc_nfs_info *nfs_infop; 604 struct proc_nfs_info *nfs_infop;
587 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb); 605 struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
606 char buf[12];
607 char *proto;
588 608
589 seq_printf(m, ",v%d", nfss->rpc_ops->version); 609 seq_printf(m, ",v%d", nfss->rpc_ops->version);
590 seq_printf(m, ",rsize=%d", nfss->rsize); 610 seq_printf(m, ",rsize=%d", nfss->rsize);
@@ -603,6 +623,18 @@ static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
603 else 623 else
604 seq_puts(m, nfs_infop->nostr); 624 seq_puts(m, nfs_infop->nostr);
605 } 625 }
626 switch (nfss->client->cl_xprt->prot) {
627 case IPPROTO_TCP:
628 proto = "tcp";
629 break;
630 case IPPROTO_UDP:
631 proto = "udp";
632 break;
633 default:
634 snprintf(buf, sizeof(buf), "%u", nfss->client->cl_xprt->prot);
635 proto = buf;
636 }
637 seq_printf(m, ",proto=%s", proto);
606 seq_puts(m, ",addr="); 638 seq_puts(m, ",addr=");
607 seq_escape(m, nfss->hostname, " \t\n\\"); 639 seq_escape(m, nfss->hostname, " \t\n\\");
608 return 0; 640 return 0;
@@ -821,6 +853,11 @@ nfs_setattr(struct dentry *dentry, struct iattr *attr)
821 filemap_fdatawait(inode->i_mapping); 853 filemap_fdatawait(inode->i_mapping);
822 nfs_wb_all(inode); 854 nfs_wb_all(inode);
823 } 855 }
856 /*
857 * Return any delegations if we're going to change ACLs
858 */
859 if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
860 nfs_inode_return_delegation(inode);
824 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr); 861 error = NFS_PROTO(inode)->setattr(dentry, &fattr, attr);
825 if (error == 0) 862 if (error == 0)
826 nfs_refresh_inode(inode, &fattr); 863 nfs_refresh_inode(inode, &fattr);
@@ -1639,8 +1676,7 @@ static void nfs4_clear_inode(struct inode *inode)
1639 struct nfs_inode *nfsi = NFS_I(inode); 1676 struct nfs_inode *nfsi = NFS_I(inode);
1640 1677
1641 /* If we are holding a delegation, return it! */ 1678 /* If we are holding a delegation, return it! */
1642 if (nfsi->delegation != NULL) 1679 nfs_inode_return_delegation(inode);
1643 nfs_inode_return_delegation(inode);
1644 /* First call standard NFS clear_inode() code */ 1680 /* First call standard NFS clear_inode() code */
1645 nfs_clear_inode(inode); 1681 nfs_clear_inode(inode);
1646 /* Now clear out any remaining state */ 1682 /* Now clear out any remaining state */
@@ -1669,7 +1705,7 @@ static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data,
1669 struct rpc_clnt *clnt = NULL; 1705 struct rpc_clnt *clnt = NULL;
1670 struct rpc_timeout timeparms; 1706 struct rpc_timeout timeparms;
1671 rpc_authflavor_t authflavour; 1707 rpc_authflavor_t authflavour;
1672 int proto, err = -EIO; 1708 int err = -EIO;
1673 1709
1674 sb->s_blocksize_bits = 0; 1710 sb->s_blocksize_bits = 0;
1675 sb->s_blocksize = 0; 1711 sb->s_blocksize = 0;
@@ -1687,30 +1723,8 @@ static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data,
1687 server->acdirmax = data->acdirmax*HZ; 1723 server->acdirmax = data->acdirmax*HZ;
1688 1724
1689 server->rpc_ops = &nfs_v4_clientops; 1725 server->rpc_ops = &nfs_v4_clientops;
1690 /* Initialize timeout values */
1691
1692 timeparms.to_initval = data->timeo * HZ / 10;
1693 timeparms.to_retries = data->retrans;
1694 timeparms.to_exponential = 1;
1695 if (!timeparms.to_retries)
1696 timeparms.to_retries = 5;
1697 1726
1698 proto = data->proto; 1727 nfs_init_timeout_values(&timeparms, data->proto, data->timeo, data->retrans);
1699 /* Which IP protocol do we use? */
1700 switch (proto) {
1701 case IPPROTO_TCP:
1702 timeparms.to_maxval = RPC_MAX_TCP_TIMEOUT;
1703 if (!timeparms.to_initval)
1704 timeparms.to_initval = 600 * HZ / 10;
1705 break;
1706 case IPPROTO_UDP:
1707 timeparms.to_maxval = RPC_MAX_UDP_TIMEOUT;
1708 if (!timeparms.to_initval)
1709 timeparms.to_initval = 11 * HZ / 10;
1710 break;
1711 default:
1712 return -EINVAL;
1713 }
1714 1728
1715 clp = nfs4_get_client(&server->addr.sin_addr); 1729 clp = nfs4_get_client(&server->addr.sin_addr);
1716 if (!clp) { 1730 if (!clp) {
@@ -1735,7 +1749,7 @@ static int nfs4_fill_super(struct super_block *sb, struct nfs4_mount_data *data,
1735 1749
1736 down_write(&clp->cl_sem); 1750 down_write(&clp->cl_sem);
1737 if (IS_ERR(clp->cl_rpcclient)) { 1751 if (IS_ERR(clp->cl_rpcclient)) {
1738 xprt = xprt_create_proto(proto, &server->addr, &timeparms); 1752 xprt = xprt_create_proto(data->proto, &server->addr, &timeparms);
1739 if (IS_ERR(xprt)) { 1753 if (IS_ERR(xprt)) {
1740 up_write(&clp->cl_sem); 1754 up_write(&clp->cl_sem);
1741 err = PTR_ERR(xprt); 1755 err = PTR_ERR(xprt);
diff --git a/fs/nfs/nfs3proc.c b/fs/nfs/nfs3proc.c
index edc95514046d..e4a1cd48195e 100644
--- a/fs/nfs/nfs3proc.c
+++ b/fs/nfs/nfs3proc.c
@@ -299,7 +299,7 @@ static int nfs3_proc_commit(struct nfs_write_data *cdata)
299 */ 299 */
300static int 300static int
301nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 301nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
302 int flags) 302 int flags, struct nameidata *nd)
303{ 303{
304 struct nfs_fh fhandle; 304 struct nfs_fh fhandle;
305 struct nfs_fattr fattr; 305 struct nfs_fattr fattr;
@@ -735,7 +735,7 @@ extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
735static void 735static void
736nfs3_read_done(struct rpc_task *task) 736nfs3_read_done(struct rpc_task *task)
737{ 737{
738 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata; 738 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
739 739
740 if (nfs3_async_handle_jukebox(task)) 740 if (nfs3_async_handle_jukebox(task))
741 return; 741 return;
diff --git a/fs/nfs/nfs4_fs.h b/fs/nfs/nfs4_fs.h
index ec1a22d7b876..78a53f5a9f18 100644
--- a/fs/nfs/nfs4_fs.h
+++ b/fs/nfs/nfs4_fs.h
@@ -93,25 +93,50 @@ struct nfs4_client {
93}; 93};
94 94
95/* 95/*
96 * struct rpc_sequence ensures that RPC calls are sent in the exact
97 * order that they appear on the list.
98 */
99struct rpc_sequence {
100 struct rpc_wait_queue wait; /* RPC call delay queue */
101 spinlock_t lock; /* Protects the list */
102 struct list_head list; /* Defines sequence of RPC calls */
103};
104
105#define NFS_SEQID_CONFIRMED 1
106struct nfs_seqid_counter {
107 struct rpc_sequence *sequence;
108 int flags;
109 u32 counter;
110};
111
112struct nfs_seqid {
113 struct nfs_seqid_counter *sequence;
114 struct list_head list;
115};
116
117static inline void nfs_confirm_seqid(struct nfs_seqid_counter *seqid, int status)
118{
119 if (seqid_mutating_err(-status))
120 seqid->flags |= NFS_SEQID_CONFIRMED;
121}
122
123/*
96 * NFS4 state_owners and lock_owners are simply labels for ordered 124 * NFS4 state_owners and lock_owners are simply labels for ordered
97 * sequences of RPC calls. Their sole purpose is to provide once-only 125 * sequences of RPC calls. Their sole purpose is to provide once-only
98 * semantics by allowing the server to identify replayed requests. 126 * semantics by allowing the server to identify replayed requests.
99 *
100 * The ->so_sema is held during all state_owner seqid-mutating operations:
101 * OPEN, OPEN_DOWNGRADE, and CLOSE. Its purpose is to properly serialize
102 * so_seqid.
103 */ 127 */
104struct nfs4_state_owner { 128struct nfs4_state_owner {
129 spinlock_t so_lock;
105 struct list_head so_list; /* per-clientid list of state_owners */ 130 struct list_head so_list; /* per-clientid list of state_owners */
106 struct nfs4_client *so_client; 131 struct nfs4_client *so_client;
107 u32 so_id; /* 32-bit identifier, unique */ 132 u32 so_id; /* 32-bit identifier, unique */
108 struct semaphore so_sema;
109 u32 so_seqid; /* protected by so_sema */
110 atomic_t so_count; 133 atomic_t so_count;
111 134
112 struct rpc_cred *so_cred; /* Associated cred */ 135 struct rpc_cred *so_cred; /* Associated cred */
113 struct list_head so_states; 136 struct list_head so_states;
114 struct list_head so_delegations; 137 struct list_head so_delegations;
138 struct nfs_seqid_counter so_seqid;
139 struct rpc_sequence so_sequence;
115}; 140};
116 141
117/* 142/*
@@ -132,7 +157,7 @@ struct nfs4_lock_state {
132 fl_owner_t ls_owner; /* POSIX lock owner */ 157 fl_owner_t ls_owner; /* POSIX lock owner */
133#define NFS_LOCK_INITIALIZED 1 158#define NFS_LOCK_INITIALIZED 1
134 int ls_flags; 159 int ls_flags;
135 u32 ls_seqid; 160 struct nfs_seqid_counter ls_seqid;
136 u32 ls_id; 161 u32 ls_id;
137 nfs4_stateid ls_stateid; 162 nfs4_stateid ls_stateid;
138 atomic_t ls_count; 163 atomic_t ls_count;
@@ -153,7 +178,6 @@ struct nfs4_state {
153 struct inode *inode; /* Pointer to the inode */ 178 struct inode *inode; /* Pointer to the inode */
154 179
155 unsigned long flags; /* Do we hold any locks? */ 180 unsigned long flags; /* Do we hold any locks? */
156 struct semaphore lock_sema; /* Serializes file locking operations */
157 spinlock_t state_lock; /* Protects the lock_states list */ 181 spinlock_t state_lock; /* Protects the lock_states list */
158 182
159 nfs4_stateid stateid; 183 nfs4_stateid stateid;
@@ -191,8 +215,8 @@ extern int nfs4_proc_setclientid_confirm(struct nfs4_client *);
191extern int nfs4_proc_async_renew(struct nfs4_client *); 215extern int nfs4_proc_async_renew(struct nfs4_client *);
192extern int nfs4_proc_renew(struct nfs4_client *); 216extern int nfs4_proc_renew(struct nfs4_client *);
193extern int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode); 217extern int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode);
194extern struct inode *nfs4_atomic_open(struct inode *, struct dentry *, struct nameidata *); 218extern struct dentry *nfs4_atomic_open(struct inode *, struct dentry *, struct nameidata *);
195extern int nfs4_open_revalidate(struct inode *, struct dentry *, int); 219extern int nfs4_open_revalidate(struct inode *, struct dentry *, int, struct nameidata *);
196 220
197extern struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops; 221extern struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops;
198extern struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops; 222extern struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops;
@@ -224,12 +248,17 @@ extern struct nfs4_state * nfs4_get_open_state(struct inode *, struct nfs4_state
224extern void nfs4_put_open_state(struct nfs4_state *); 248extern void nfs4_put_open_state(struct nfs4_state *);
225extern void nfs4_close_state(struct nfs4_state *, mode_t); 249extern void nfs4_close_state(struct nfs4_state *, mode_t);
226extern struct nfs4_state *nfs4_find_state(struct inode *, struct rpc_cred *, mode_t mode); 250extern struct nfs4_state *nfs4_find_state(struct inode *, struct rpc_cred *, mode_t mode);
227extern void nfs4_increment_seqid(int status, struct nfs4_state_owner *sp);
228extern void nfs4_schedule_state_recovery(struct nfs4_client *); 251extern void nfs4_schedule_state_recovery(struct nfs4_client *);
252extern void nfs4_put_lock_state(struct nfs4_lock_state *lsp);
229extern int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl); 253extern int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl);
230extern void nfs4_increment_lock_seqid(int status, struct nfs4_lock_state *ls);
231extern void nfs4_copy_stateid(nfs4_stateid *, struct nfs4_state *, fl_owner_t); 254extern void nfs4_copy_stateid(nfs4_stateid *, struct nfs4_state *, fl_owner_t);
232 255
256extern struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter);
257extern int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task);
258extern void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid);
259extern void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid);
260extern void nfs_free_seqid(struct nfs_seqid *seqid);
261
233extern const nfs4_stateid zero_stateid; 262extern const nfs4_stateid zero_stateid;
234 263
235/* nfs4xdr.c */ 264/* nfs4xdr.c */
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
index 9701ca8c9428..9c1da34036aa 100644
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -47,6 +47,7 @@
47#include <linux/nfs_page.h> 47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h> 48#include <linux/smp_lock.h>
49#include <linux/namei.h> 49#include <linux/namei.h>
50#include <linux/mount.h>
50 51
51#include "nfs4_fs.h" 52#include "nfs4_fs.h"
52#include "delegation.h" 53#include "delegation.h"
@@ -56,10 +57,11 @@
56#define NFS4_POLL_RETRY_MIN (1*HZ) 57#define NFS4_POLL_RETRY_MIN (1*HZ)
57#define NFS4_POLL_RETRY_MAX (15*HZ) 58#define NFS4_POLL_RETRY_MAX (15*HZ)
58 59
60static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid);
59static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); 61static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
60static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *); 62static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
61static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry); 63static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
62static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception); 64static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
63extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus); 65extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
64extern struct rpc_procinfo nfs4_procedures[]; 66extern struct rpc_procinfo nfs4_procedures[];
65 67
@@ -189,12 +191,28 @@ static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinf
189 nfsi->change_attr = cinfo->after; 191 nfsi->change_attr = cinfo->after;
190} 192}
191 193
194/* Helper for asynchronous RPC calls */
195static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
196 rpc_action tk_exit, void *calldata)
197{
198 struct rpc_task *task;
199
200 if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
201 return -ENOMEM;
202
203 task->tk_calldata = calldata;
204 task->tk_action = tk_begin;
205 rpc_execute(task);
206 return 0;
207}
208
192static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags) 209static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
193{ 210{
194 struct inode *inode = state->inode; 211 struct inode *inode = state->inode;
195 212
196 open_flags &= (FMODE_READ|FMODE_WRITE); 213 open_flags &= (FMODE_READ|FMODE_WRITE);
197 /* Protect against nfs4_find_state() */ 214 /* Protect against nfs4_find_state() */
215 spin_lock(&state->owner->so_lock);
198 spin_lock(&inode->i_lock); 216 spin_lock(&inode->i_lock);
199 state->state |= open_flags; 217 state->state |= open_flags;
200 /* NB! List reordering - see the reclaim code for why. */ 218 /* NB! List reordering - see the reclaim code for why. */
@@ -204,12 +222,12 @@ static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid,
204 state->nreaders++; 222 state->nreaders++;
205 memcpy(&state->stateid, stateid, sizeof(state->stateid)); 223 memcpy(&state->stateid, stateid, sizeof(state->stateid));
206 spin_unlock(&inode->i_lock); 224 spin_unlock(&inode->i_lock);
225 spin_unlock(&state->owner->so_lock);
207} 226}
208 227
209/* 228/*
210 * OPEN_RECLAIM: 229 * OPEN_RECLAIM:
211 * reclaim state on the server after a reboot. 230 * reclaim state on the server after a reboot.
212 * Assumes caller is holding the sp->so_sem
213 */ 231 */
214static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) 232static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
215{ 233{
@@ -218,7 +236,6 @@ static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *st
218 struct nfs_delegation *delegation = NFS_I(inode)->delegation; 236 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
219 struct nfs_openargs o_arg = { 237 struct nfs_openargs o_arg = {
220 .fh = NFS_FH(inode), 238 .fh = NFS_FH(inode),
221 .seqid = sp->so_seqid,
222 .id = sp->so_id, 239 .id = sp->so_id,
223 .open_flags = state->state, 240 .open_flags = state->state,
224 .clientid = server->nfs4_state->cl_clientid, 241 .clientid = server->nfs4_state->cl_clientid,
@@ -245,8 +262,13 @@ static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *st
245 } 262 }
246 o_arg.u.delegation_type = delegation->type; 263 o_arg.u.delegation_type = delegation->type;
247 } 264 }
265 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
266 if (o_arg.seqid == NULL)
267 return -ENOMEM;
248 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 268 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
249 nfs4_increment_seqid(status, sp); 269 /* Confirm the sequence as being established */
270 nfs_confirm_seqid(&sp->so_seqid, status);
271 nfs_increment_open_seqid(status, o_arg.seqid);
250 if (status == 0) { 272 if (status == 0) {
251 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid)); 273 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
252 if (o_res.delegation_type != 0) { 274 if (o_res.delegation_type != 0) {
@@ -256,6 +278,7 @@ static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *st
256 nfs_async_inode_return_delegation(inode, &o_res.stateid); 278 nfs_async_inode_return_delegation(inode, &o_res.stateid);
257 } 279 }
258 } 280 }
281 nfs_free_seqid(o_arg.seqid);
259 clear_bit(NFS_DELEGATED_STATE, &state->flags); 282 clear_bit(NFS_DELEGATED_STATE, &state->flags);
260 /* Ensure we update the inode attributes */ 283 /* Ensure we update the inode attributes */
261 NFS_CACHEINV(inode); 284 NFS_CACHEINV(inode);
@@ -302,23 +325,35 @@ static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state
302 }; 325 };
303 int status = 0; 326 int status = 0;
304 327
305 down(&sp->so_sema);
306 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) 328 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
307 goto out; 329 goto out;
308 if (state->state == 0) 330 if (state->state == 0)
309 goto out; 331 goto out;
310 arg.seqid = sp->so_seqid; 332 arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
333 status = -ENOMEM;
334 if (arg.seqid == NULL)
335 goto out;
311 arg.open_flags = state->state; 336 arg.open_flags = state->state;
312 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data)); 337 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
313 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 338 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
314 nfs4_increment_seqid(status, sp); 339 nfs_increment_open_seqid(status, arg.seqid);
340 if (status != 0)
341 goto out_free;
342 if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
343 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
344 sp, &res.stateid, arg.seqid);
345 if (status != 0)
346 goto out_free;
347 }
348 nfs_confirm_seqid(&sp->so_seqid, 0);
315 if (status >= 0) { 349 if (status >= 0) {
316 memcpy(state->stateid.data, res.stateid.data, 350 memcpy(state->stateid.data, res.stateid.data,
317 sizeof(state->stateid.data)); 351 sizeof(state->stateid.data));
318 clear_bit(NFS_DELEGATED_STATE, &state->flags); 352 clear_bit(NFS_DELEGATED_STATE, &state->flags);
319 } 353 }
354out_free:
355 nfs_free_seqid(arg.seqid);
320out: 356out:
321 up(&sp->so_sema);
322 dput(parent); 357 dput(parent);
323 return status; 358 return status;
324} 359}
@@ -345,11 +380,11 @@ int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
345 return err; 380 return err;
346} 381}
347 382
348static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid) 383static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
349{ 384{
350 struct nfs_open_confirmargs arg = { 385 struct nfs_open_confirmargs arg = {
351 .fh = fh, 386 .fh = fh,
352 .seqid = sp->so_seqid, 387 .seqid = seqid,
353 .stateid = *stateid, 388 .stateid = *stateid,
354 }; 389 };
355 struct nfs_open_confirmres res; 390 struct nfs_open_confirmres res;
@@ -362,7 +397,9 @@ static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nf
362 int status; 397 int status;
363 398
364 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR); 399 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
365 nfs4_increment_seqid(status, sp); 400 /* Confirm the sequence as being established */
401 nfs_confirm_seqid(&sp->so_seqid, status);
402 nfs_increment_open_seqid(status, seqid);
366 if (status >= 0) 403 if (status >= 0)
367 memcpy(stateid, &res.stateid, sizeof(*stateid)); 404 memcpy(stateid, &res.stateid, sizeof(*stateid));
368 return status; 405 return status;
@@ -380,21 +417,37 @@ static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, stru
380 int status; 417 int status;
381 418
382 /* Update sequence id. The caller must serialize! */ 419 /* Update sequence id. The caller must serialize! */
383 o_arg->seqid = sp->so_seqid;
384 o_arg->id = sp->so_id; 420 o_arg->id = sp->so_id;
385 o_arg->clientid = sp->so_client->cl_clientid; 421 o_arg->clientid = sp->so_client->cl_clientid;
386 422
387 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 423 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
388 nfs4_increment_seqid(status, sp); 424 if (status == 0) {
425 /* OPEN on anything except a regular file is disallowed in NFSv4 */
426 switch (o_res->f_attr->mode & S_IFMT) {
427 case S_IFREG:
428 break;
429 case S_IFLNK:
430 status = -ELOOP;
431 break;
432 case S_IFDIR:
433 status = -EISDIR;
434 break;
435 default:
436 status = -ENOTDIR;
437 }
438 }
439
440 nfs_increment_open_seqid(status, o_arg->seqid);
389 if (status != 0) 441 if (status != 0)
390 goto out; 442 goto out;
391 update_changeattr(dir, &o_res->cinfo); 443 update_changeattr(dir, &o_res->cinfo);
392 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { 444 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
393 status = _nfs4_proc_open_confirm(server->client, &o_res->fh, 445 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
394 sp, &o_res->stateid); 446 sp, &o_res->stateid, o_arg->seqid);
395 if (status != 0) 447 if (status != 0)
396 goto out; 448 goto out;
397 } 449 }
450 nfs_confirm_seqid(&sp->so_seqid, 0);
398 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) 451 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
399 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr); 452 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
400out: 453out:
@@ -465,6 +518,10 @@ static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *st
465 set_bit(NFS_DELEGATED_STATE, &state->flags); 518 set_bit(NFS_DELEGATED_STATE, &state->flags);
466 goto out; 519 goto out;
467 } 520 }
521 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
522 status = -ENOMEM;
523 if (o_arg.seqid == NULL)
524 goto out;
468 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res); 525 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
469 if (status != 0) 526 if (status != 0)
470 goto out_nodeleg; 527 goto out_nodeleg;
@@ -490,6 +547,7 @@ static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *st
490 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res); 547 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
491 } 548 }
492out_nodeleg: 549out_nodeleg:
550 nfs_free_seqid(o_arg.seqid);
493 clear_bit(NFS_DELEGATED_STATE, &state->flags); 551 clear_bit(NFS_DELEGATED_STATE, &state->flags);
494out: 552out:
495 dput(parent); 553 dput(parent);
@@ -564,7 +622,6 @@ static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred
564 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__); 622 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
565 goto out_err; 623 goto out_err;
566 } 624 }
567 down(&sp->so_sema);
568 state = nfs4_get_open_state(inode, sp); 625 state = nfs4_get_open_state(inode, sp);
569 if (state == NULL) 626 if (state == NULL)
570 goto out_err; 627 goto out_err;
@@ -589,7 +646,6 @@ static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred
589 set_bit(NFS_DELEGATED_STATE, &state->flags); 646 set_bit(NFS_DELEGATED_STATE, &state->flags);
590 update_open_stateid(state, &delegation->stateid, open_flags); 647 update_open_stateid(state, &delegation->stateid, open_flags);
591out_ok: 648out_ok:
592 up(&sp->so_sema);
593 nfs4_put_state_owner(sp); 649 nfs4_put_state_owner(sp);
594 up_read(&nfsi->rwsem); 650 up_read(&nfsi->rwsem);
595 up_read(&clp->cl_sem); 651 up_read(&clp->cl_sem);
@@ -600,11 +656,12 @@ out_err:
600 if (sp != NULL) { 656 if (sp != NULL) {
601 if (state != NULL) 657 if (state != NULL)
602 nfs4_put_open_state(state); 658 nfs4_put_open_state(state);
603 up(&sp->so_sema);
604 nfs4_put_state_owner(sp); 659 nfs4_put_state_owner(sp);
605 } 660 }
606 up_read(&nfsi->rwsem); 661 up_read(&nfsi->rwsem);
607 up_read(&clp->cl_sem); 662 up_read(&clp->cl_sem);
663 if (err != -EACCES)
664 nfs_inode_return_delegation(inode);
608 return err; 665 return err;
609} 666}
610 667
@@ -665,8 +722,10 @@ static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, st
665 } else 722 } else
666 o_arg.u.attrs = sattr; 723 o_arg.u.attrs = sattr;
667 /* Serialization for the sequence id */ 724 /* Serialization for the sequence id */
668 down(&sp->so_sema);
669 725
726 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
727 if (o_arg.seqid == NULL)
728 return -ENOMEM;
670 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res); 729 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
671 if (status != 0) 730 if (status != 0)
672 goto out_err; 731 goto out_err;
@@ -681,7 +740,7 @@ static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, st
681 update_open_stateid(state, &o_res.stateid, flags); 740 update_open_stateid(state, &o_res.stateid, flags);
682 if (o_res.delegation_type != 0) 741 if (o_res.delegation_type != 0)
683 nfs_inode_set_delegation(inode, cred, &o_res); 742 nfs_inode_set_delegation(inode, cred, &o_res);
684 up(&sp->so_sema); 743 nfs_free_seqid(o_arg.seqid);
685 nfs4_put_state_owner(sp); 744 nfs4_put_state_owner(sp);
686 up_read(&clp->cl_sem); 745 up_read(&clp->cl_sem);
687 *res = state; 746 *res = state;
@@ -690,7 +749,7 @@ out_err:
690 if (sp != NULL) { 749 if (sp != NULL) {
691 if (state != NULL) 750 if (state != NULL)
692 nfs4_put_open_state(state); 751 nfs4_put_open_state(state);
693 up(&sp->so_sema); 752 nfs_free_seqid(o_arg.seqid);
694 nfs4_put_state_owner(sp); 753 nfs4_put_state_owner(sp);
695 } 754 }
696 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */ 755 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
@@ -718,7 +777,7 @@ static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry,
718 * It is actually a sign of a bug on the client or on the server. 777 * It is actually a sign of a bug on the client or on the server.
719 * 778 *
720 * If we receive a BAD_SEQID error in the particular case of 779 * If we receive a BAD_SEQID error in the particular case of
721 * doing an OPEN, we assume that nfs4_increment_seqid() will 780 * doing an OPEN, we assume that nfs_increment_open_seqid() will
722 * have unhashed the old state_owner for us, and that we can 781 * have unhashed the old state_owner for us, and that we can
723 * therefore safely retry using a new one. We should still warn 782 * therefore safely retry using a new one. We should still warn
724 * the user though... 783 * the user though...
@@ -728,6 +787,16 @@ static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry,
728 exception.retry = 1; 787 exception.retry = 1;
729 continue; 788 continue;
730 } 789 }
790 /*
791 * BAD_STATEID on OPEN means that the server cancelled our
792 * state before it received the OPEN_CONFIRM.
793 * Recover by retrying the request as per the discussion
794 * on Page 181 of RFC3530.
795 */
796 if (status == -NFS4ERR_BAD_STATEID) {
797 exception.retry = 1;
798 continue;
799 }
731 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir), 800 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
732 status, &exception)); 801 status, &exception));
733 } while (exception.retry); 802 } while (exception.retry);
@@ -789,17 +858,27 @@ struct nfs4_closedata {
789 struct nfs_closeres res; 858 struct nfs_closeres res;
790}; 859};
791 860
861static void nfs4_free_closedata(struct nfs4_closedata *calldata)
862{
863 struct nfs4_state *state = calldata->state;
864 struct nfs4_state_owner *sp = state->owner;
865
866 nfs4_put_open_state(calldata->state);
867 nfs_free_seqid(calldata->arg.seqid);
868 nfs4_put_state_owner(sp);
869 kfree(calldata);
870}
871
792static void nfs4_close_done(struct rpc_task *task) 872static void nfs4_close_done(struct rpc_task *task)
793{ 873{
794 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata; 874 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
795 struct nfs4_state *state = calldata->state; 875 struct nfs4_state *state = calldata->state;
796 struct nfs4_state_owner *sp = state->owner;
797 struct nfs_server *server = NFS_SERVER(calldata->inode); 876 struct nfs_server *server = NFS_SERVER(calldata->inode);
798 877
799 /* hmm. we are done with the inode, and in the process of freeing 878 /* hmm. we are done with the inode, and in the process of freeing
800 * the state_owner. we keep this around to process errors 879 * the state_owner. we keep this around to process errors
801 */ 880 */
802 nfs4_increment_seqid(task->tk_status, sp); 881 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
803 switch (task->tk_status) { 882 switch (task->tk_status) {
804 case 0: 883 case 0:
805 memcpy(&state->stateid, &calldata->res.stateid, 884 memcpy(&state->stateid, &calldata->res.stateid,
@@ -817,24 +896,46 @@ static void nfs4_close_done(struct rpc_task *task)
817 } 896 }
818 } 897 }
819 state->state = calldata->arg.open_flags; 898 state->state = calldata->arg.open_flags;
820 nfs4_put_open_state(state); 899 nfs4_free_closedata(calldata);
821 up(&sp->so_sema);
822 nfs4_put_state_owner(sp);
823 up_read(&server->nfs4_state->cl_sem);
824 kfree(calldata);
825} 900}
826 901
827static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *calldata) 902static void nfs4_close_begin(struct rpc_task *task)
828{ 903{
904 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
905 struct nfs4_state *state = calldata->state;
829 struct rpc_message msg = { 906 struct rpc_message msg = {
830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], 907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
831 .rpc_argp = &calldata->arg, 908 .rpc_argp = &calldata->arg,
832 .rpc_resp = &calldata->res, 909 .rpc_resp = &calldata->res,
833 .rpc_cred = calldata->state->owner->so_cred, 910 .rpc_cred = state->owner->so_cred,
834 }; 911 };
835 if (calldata->arg.open_flags != 0) 912 int mode = 0;
913 int status;
914
915 status = nfs_wait_on_sequence(calldata->arg.seqid, task);
916 if (status != 0)
917 return;
918 /* Don't reorder reads */
919 smp_rmb();
920 /* Recalculate the new open mode in case someone reopened the file
921 * while we were waiting in line to be scheduled.
922 */
923 if (state->nreaders != 0)
924 mode |= FMODE_READ;
925 if (state->nwriters != 0)
926 mode |= FMODE_WRITE;
927 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
928 state->state = mode;
929 if (mode == state->state) {
930 nfs4_free_closedata(calldata);
931 task->tk_exit = NULL;
932 rpc_exit(task, 0);
933 return;
934 }
935 if (mode != 0)
836 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; 936 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
837 return rpc_call_async(clnt, &msg, 0, nfs4_close_done, calldata); 937 calldata->arg.open_flags = mode;
938 rpc_call_setup(task, &msg, 0);
838} 939}
839 940
840/* 941/*
@@ -851,39 +952,52 @@ static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *
851int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode) 952int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
852{ 953{
853 struct nfs4_closedata *calldata; 954 struct nfs4_closedata *calldata;
854 int status; 955 int status = -ENOMEM;
855 956
856 /* Tell caller we're done */ 957 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
857 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
858 state->state = mode;
859 return 0;
860 }
861 calldata = (struct nfs4_closedata *)kmalloc(sizeof(*calldata), GFP_KERNEL);
862 if (calldata == NULL) 958 if (calldata == NULL)
863 return -ENOMEM; 959 goto out;
864 calldata->inode = inode; 960 calldata->inode = inode;
865 calldata->state = state; 961 calldata->state = state;
866 calldata->arg.fh = NFS_FH(inode); 962 calldata->arg.fh = NFS_FH(inode);
963 calldata->arg.stateid = &state->stateid;
867 /* Serialization for the sequence id */ 964 /* Serialization for the sequence id */
868 calldata->arg.seqid = state->owner->so_seqid; 965 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
869 calldata->arg.open_flags = mode; 966 if (calldata->arg.seqid == NULL)
870 memcpy(&calldata->arg.stateid, &state->stateid, 967 goto out_free_calldata;
871 sizeof(calldata->arg.stateid)); 968
872 status = nfs4_close_call(NFS_SERVER(inode)->client, calldata); 969 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
873 /* 970 nfs4_close_done, calldata);
874 * Return -EINPROGRESS on success in order to indicate to the 971 if (status == 0)
875 * caller that an asynchronous RPC call has been launched, and 972 goto out;
876 * that it will release the semaphores on completion. 973
877 */ 974 nfs_free_seqid(calldata->arg.seqid);
878 return (status == 0) ? -EINPROGRESS : status; 975out_free_calldata:
976 kfree(calldata);
977out:
978 return status;
979}
980
981static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
982{
983 struct file *filp;
984
985 filp = lookup_instantiate_filp(nd, dentry, NULL);
986 if (!IS_ERR(filp)) {
987 struct nfs_open_context *ctx;
988 ctx = (struct nfs_open_context *)filp->private_data;
989 ctx->state = state;
990 } else
991 nfs4_close_state(state, nd->intent.open.flags);
879} 992}
880 993
881struct inode * 994struct dentry *
882nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 995nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
883{ 996{
884 struct iattr attr; 997 struct iattr attr;
885 struct rpc_cred *cred; 998 struct rpc_cred *cred;
886 struct nfs4_state *state; 999 struct nfs4_state *state;
1000 struct dentry *res;
887 1001
888 if (nd->flags & LOOKUP_CREATE) { 1002 if (nd->flags & LOOKUP_CREATE) {
889 attr.ia_mode = nd->intent.open.create_mode; 1003 attr.ia_mode = nd->intent.open.create_mode;
@@ -897,16 +1011,23 @@ nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
897 1011
898 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0); 1012 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
899 if (IS_ERR(cred)) 1013 if (IS_ERR(cred))
900 return (struct inode *)cred; 1014 return (struct dentry *)cred;
901 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred); 1015 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
902 put_rpccred(cred); 1016 put_rpccred(cred);
903 if (IS_ERR(state)) 1017 if (IS_ERR(state)) {
904 return (struct inode *)state; 1018 if (PTR_ERR(state) == -ENOENT)
905 return state->inode; 1019 d_add(dentry, NULL);
1020 return (struct dentry *)state;
1021 }
1022 res = d_add_unique(dentry, state->inode);
1023 if (res != NULL)
1024 dentry = res;
1025 nfs4_intent_set_file(nd, dentry, state);
1026 return res;
906} 1027}
907 1028
908int 1029int
909nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags) 1030nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
910{ 1031{
911 struct rpc_cred *cred; 1032 struct rpc_cred *cred;
912 struct nfs4_state *state; 1033 struct nfs4_state *state;
@@ -919,18 +1040,30 @@ nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
919 if (IS_ERR(state)) 1040 if (IS_ERR(state))
920 state = nfs4_do_open(dir, dentry, openflags, NULL, cred); 1041 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
921 put_rpccred(cred); 1042 put_rpccred(cred);
922 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0) 1043 if (IS_ERR(state)) {
923 return 1; 1044 switch (PTR_ERR(state)) {
924 if (IS_ERR(state)) 1045 case -EPERM:
925 return 0; 1046 case -EACCES:
1047 case -EDQUOT:
1048 case -ENOSPC:
1049 case -EROFS:
1050 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1051 return 1;
1052 case -ENOENT:
1053 if (dentry->d_inode == NULL)
1054 return 1;
1055 }
1056 goto out_drop;
1057 }
926 inode = state->inode; 1058 inode = state->inode;
1059 iput(inode);
927 if (inode == dentry->d_inode) { 1060 if (inode == dentry->d_inode) {
928 iput(inode); 1061 nfs4_intent_set_file(nd, dentry, state);
929 return 1; 1062 return 1;
930 } 1063 }
931 d_drop(dentry);
932 nfs4_close_state(state, openflags); 1064 nfs4_close_state(state, openflags);
933 iput(inode); 1065out_drop:
1066 d_drop(dentry);
934 return 0; 1067 return 0;
935} 1068}
936 1069
@@ -1431,7 +1564,7 @@ static int nfs4_proc_commit(struct nfs_write_data *cdata)
1431 1564
1432static int 1565static int
1433nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 1566nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1434 int flags) 1567 int flags, struct nameidata *nd)
1435{ 1568{
1436 struct nfs4_state *state; 1569 struct nfs4_state *state;
1437 struct rpc_cred *cred; 1570 struct rpc_cred *cred;
@@ -1453,13 +1586,13 @@ nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1453 struct nfs_fattr fattr; 1586 struct nfs_fattr fattr;
1454 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr, 1587 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1455 NFS_FH(state->inode), sattr, state); 1588 NFS_FH(state->inode), sattr, state);
1456 if (status == 0) { 1589 if (status == 0)
1457 nfs_setattr_update_inode(state->inode, sattr); 1590 nfs_setattr_update_inode(state->inode, sattr);
1458 goto out; 1591 }
1459 } 1592 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1460 } else if (flags != 0) 1593 nfs4_intent_set_file(nd, dentry, state);
1461 goto out; 1594 else
1462 nfs4_close_state(state, flags); 1595 nfs4_close_state(state, flags);
1463out: 1596out:
1464 return status; 1597 return status;
1465} 1598}
@@ -2106,65 +2239,6 @@ nfs4_proc_renew(struct nfs4_client *clp)
2106 return 0; 2239 return 0;
2107} 2240}
2108 2241
2109/*
2110 * We will need to arrange for the VFS layer to provide an atomic open.
2111 * Until then, this open method is prone to inefficiency and race conditions
2112 * due to the lookup, potential create, and open VFS calls from sys_open()
2113 * placed on the wire.
2114 */
2115static int
2116nfs4_proc_file_open(struct inode *inode, struct file *filp)
2117{
2118 struct dentry *dentry = filp->f_dentry;
2119 struct nfs_open_context *ctx;
2120 struct nfs4_state *state = NULL;
2121 struct rpc_cred *cred;
2122 int status = -ENOMEM;
2123
2124 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2125 (int)dentry->d_parent->d_name.len,
2126 dentry->d_parent->d_name.name,
2127 (int)dentry->d_name.len, dentry->d_name.name);
2128
2129
2130 /* Find our open stateid */
2131 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2132 if (IS_ERR(cred))
2133 return PTR_ERR(cred);
2134 ctx = alloc_nfs_open_context(dentry, cred);
2135 put_rpccred(cred);
2136 if (unlikely(ctx == NULL))
2137 return -ENOMEM;
2138 status = -EIO; /* ERACE actually */
2139 state = nfs4_find_state(inode, cred, filp->f_mode);
2140 if (unlikely(state == NULL))
2141 goto no_state;
2142 ctx->state = state;
2143 nfs4_close_state(state, filp->f_mode);
2144 ctx->mode = filp->f_mode;
2145 nfs_file_set_open_context(filp, ctx);
2146 put_nfs_open_context(ctx);
2147 if (filp->f_mode & FMODE_WRITE)
2148 nfs_begin_data_update(inode);
2149 return 0;
2150no_state:
2151 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2152 put_nfs_open_context(ctx);
2153 return status;
2154}
2155
2156/*
2157 * Release our state
2158 */
2159static int
2160nfs4_proc_file_release(struct inode *inode, struct file *filp)
2161{
2162 if (filp->f_mode & FMODE_WRITE)
2163 nfs_end_data_update(inode);
2164 nfs_file_clear_open_context(filp);
2165 return 0;
2166}
2167
2168static inline int nfs4_server_supports_acls(struct nfs_server *server) 2242static inline int nfs4_server_supports_acls(struct nfs_server *server)
2169{ 2243{
2170 return (server->caps & NFS_CAP_ACLS) 2244 return (server->caps & NFS_CAP_ACLS)
@@ -2285,7 +2359,7 @@ static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size
2285 return -ENOMEM; 2359 return -ENOMEM;
2286 args.acl_pages[0] = localpage; 2360 args.acl_pages[0] = localpage;
2287 args.acl_pgbase = 0; 2361 args.acl_pgbase = 0;
2288 args.acl_len = PAGE_SIZE; 2362 resp_len = args.acl_len = PAGE_SIZE;
2289 } else { 2363 } else {
2290 resp_buf = buf; 2364 resp_buf = buf;
2291 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase); 2365 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
@@ -2345,6 +2419,7 @@ static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen
2345 2419
2346 if (!nfs4_server_supports_acls(server)) 2420 if (!nfs4_server_supports_acls(server))
2347 return -EOPNOTSUPP; 2421 return -EOPNOTSUPP;
2422 nfs_inode_return_delegation(inode);
2348 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase); 2423 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2349 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0); 2424 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2350 if (ret == 0) 2425 if (ret == 0)
@@ -2353,7 +2428,7 @@ static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen
2353} 2428}
2354 2429
2355static int 2430static int
2356nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server) 2431nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2357{ 2432{
2358 struct nfs4_client *clp = server->nfs4_state; 2433 struct nfs4_client *clp = server->nfs4_state;
2359 2434
@@ -2431,7 +2506,7 @@ static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2431/* This is the error handling routine for processes that are allowed 2506/* This is the error handling routine for processes that are allowed
2432 * to sleep. 2507 * to sleep.
2433 */ 2508 */
2434int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) 2509int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2435{ 2510{
2436 struct nfs4_client *clp = server->nfs4_state; 2511 struct nfs4_client *clp = server->nfs4_state;
2437 int ret = errorcode; 2512 int ret = errorcode;
@@ -2632,7 +2707,6 @@ static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock
2632 2707
2633 down_read(&clp->cl_sem); 2708 down_read(&clp->cl_sem);
2634 nlo.clientid = clp->cl_clientid; 2709 nlo.clientid = clp->cl_clientid;
2635 down(&state->lock_sema);
2636 status = nfs4_set_lock_state(state, request); 2710 status = nfs4_set_lock_state(state, request);
2637 if (status != 0) 2711 if (status != 0)
2638 goto out; 2712 goto out;
@@ -2659,7 +2733,6 @@ static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock
2659 status = 0; 2733 status = 0;
2660 } 2734 }
2661out: 2735out:
2662 up(&state->lock_sema);
2663 up_read(&clp->cl_sem); 2736 up_read(&clp->cl_sem);
2664 return status; 2737 return status;
2665} 2738}
@@ -2696,79 +2769,149 @@ static int do_vfs_lock(struct file *file, struct file_lock *fl)
2696 return res; 2769 return res;
2697} 2770}
2698 2771
2699static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) 2772struct nfs4_unlockdata {
2773 struct nfs_lockargs arg;
2774 struct nfs_locku_opargs luargs;
2775 struct nfs_lockres res;
2776 struct nfs4_lock_state *lsp;
2777 struct nfs_open_context *ctx;
2778 atomic_t refcount;
2779 struct completion completion;
2780};
2781
2782static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
2700{ 2783{
2701 struct inode *inode = state->inode; 2784 if (atomic_dec_and_test(&calldata->refcount)) {
2702 struct nfs_server *server = NFS_SERVER(inode); 2785 nfs_free_seqid(calldata->luargs.seqid);
2703 struct nfs4_client *clp = server->nfs4_state; 2786 nfs4_put_lock_state(calldata->lsp);
2704 struct nfs_lockargs arg = { 2787 put_nfs_open_context(calldata->ctx);
2705 .fh = NFS_FH(inode), 2788 kfree(calldata);
2706 .type = nfs4_lck_type(cmd, request), 2789 }
2707 .offset = request->fl_start, 2790}
2708 .length = nfs4_lck_length(request), 2791
2709 }; 2792static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
2710 struct nfs_lockres res = { 2793{
2711 .server = server, 2794 complete(&calldata->completion);
2712 }; 2795 nfs4_locku_release_calldata(calldata);
2796}
2797
2798static void nfs4_locku_done(struct rpc_task *task)
2799{
2800 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2801
2802 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2803 switch (task->tk_status) {
2804 case 0:
2805 memcpy(calldata->lsp->ls_stateid.data,
2806 calldata->res.u.stateid.data,
2807 sizeof(calldata->lsp->ls_stateid.data));
2808 break;
2809 case -NFS4ERR_STALE_STATEID:
2810 case -NFS4ERR_EXPIRED:
2811 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2812 break;
2813 default:
2814 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2815 rpc_restart_call(task);
2816 return;
2817 }
2818 }
2819 nfs4_locku_complete(calldata);
2820}
2821
2822static void nfs4_locku_begin(struct rpc_task *task)
2823{
2824 struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2713 struct rpc_message msg = { 2825 struct rpc_message msg = {
2714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], 2826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2715 .rpc_argp = &arg, 2827 .rpc_argp = &calldata->arg,
2716 .rpc_resp = &res, 2828 .rpc_resp = &calldata->res,
2717 .rpc_cred = state->owner->so_cred, 2829 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
2718 }; 2830 };
2831 int status;
2832
2833 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2834 if (status != 0)
2835 return;
2836 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2837 nfs4_locku_complete(calldata);
2838 task->tk_exit = NULL;
2839 rpc_exit(task, 0);
2840 return;
2841 }
2842 rpc_call_setup(task, &msg, 0);
2843}
2844
2845static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2846{
2847 struct nfs4_unlockdata *calldata;
2848 struct inode *inode = state->inode;
2849 struct nfs_server *server = NFS_SERVER(inode);
2719 struct nfs4_lock_state *lsp; 2850 struct nfs4_lock_state *lsp;
2720 struct nfs_locku_opargs luargs;
2721 int status; 2851 int status;
2722 2852
2723 down_read(&clp->cl_sem);
2724 down(&state->lock_sema);
2725 status = nfs4_set_lock_state(state, request); 2853 status = nfs4_set_lock_state(state, request);
2726 if (status != 0) 2854 if (status != 0)
2727 goto out; 2855 return status;
2728 lsp = request->fl_u.nfs4_fl.owner; 2856 lsp = request->fl_u.nfs4_fl.owner;
2729 /* We might have lost the locks! */ 2857 /* We might have lost the locks! */
2730 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) 2858 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2731 goto out; 2859 return 0;
2732 luargs.seqid = lsp->ls_seqid; 2860 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2733 memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid)); 2861 if (calldata == NULL)
2734 arg.u.locku = &luargs; 2862 return -ENOMEM;
2735 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 2863 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2736 nfs4_increment_lock_seqid(status, lsp); 2864 if (calldata->luargs.seqid == NULL) {
2737 2865 kfree(calldata);
2738 if (status == 0) 2866 return -ENOMEM;
2739 memcpy(&lsp->ls_stateid, &res.u.stateid, 2867 }
2740 sizeof(lsp->ls_stateid)); 2868 calldata->luargs.stateid = &lsp->ls_stateid;
2741out: 2869 calldata->arg.fh = NFS_FH(inode);
2742 up(&state->lock_sema); 2870 calldata->arg.type = nfs4_lck_type(cmd, request);
2871 calldata->arg.offset = request->fl_start;
2872 calldata->arg.length = nfs4_lck_length(request);
2873 calldata->arg.u.locku = &calldata->luargs;
2874 calldata->res.server = server;
2875 calldata->lsp = lsp;
2876 atomic_inc(&lsp->ls_count);
2877
2878 /* Ensure we don't close file until we're done freeing locks! */
2879 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2880
2881 atomic_set(&calldata->refcount, 2);
2882 init_completion(&calldata->completion);
2883
2884 status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
2885 nfs4_locku_done, calldata);
2743 if (status == 0) 2886 if (status == 0)
2744 do_vfs_lock(request->fl_file, request); 2887 wait_for_completion_interruptible(&calldata->completion);
2745 up_read(&clp->cl_sem); 2888 do_vfs_lock(request->fl_file, request);
2889 nfs4_locku_release_calldata(calldata);
2746 return status; 2890 return status;
2747} 2891}
2748 2892
2749static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2750{
2751 struct nfs4_exception exception = { };
2752 int err;
2753
2754 do {
2755 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2756 _nfs4_proc_unlck(state, cmd, request),
2757 &exception);
2758 } while (exception.retry);
2759 return err;
2760}
2761
2762static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim) 2893static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2763{ 2894{
2764 struct inode *inode = state->inode; 2895 struct inode *inode = state->inode;
2765 struct nfs_server *server = NFS_SERVER(inode); 2896 struct nfs_server *server = NFS_SERVER(inode);
2766 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; 2897 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2898 struct nfs_lock_opargs largs = {
2899 .lock_stateid = &lsp->ls_stateid,
2900 .open_stateid = &state->stateid,
2901 .lock_owner = {
2902 .clientid = server->nfs4_state->cl_clientid,
2903 .id = lsp->ls_id,
2904 },
2905 .reclaim = reclaim,
2906 };
2767 struct nfs_lockargs arg = { 2907 struct nfs_lockargs arg = {
2768 .fh = NFS_FH(inode), 2908 .fh = NFS_FH(inode),
2769 .type = nfs4_lck_type(cmd, request), 2909 .type = nfs4_lck_type(cmd, request),
2770 .offset = request->fl_start, 2910 .offset = request->fl_start,
2771 .length = nfs4_lck_length(request), 2911 .length = nfs4_lck_length(request),
2912 .u = {
2913 .lock = &largs,
2914 },
2772 }; 2915 };
2773 struct nfs_lockres res = { 2916 struct nfs_lockres res = {
2774 .server = server, 2917 .server = server,
@@ -2779,53 +2922,39 @@ static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *r
2779 .rpc_resp = &res, 2922 .rpc_resp = &res,
2780 .rpc_cred = state->owner->so_cred, 2923 .rpc_cred = state->owner->so_cred,
2781 }; 2924 };
2782 struct nfs_lock_opargs largs = { 2925 int status = -ENOMEM;
2783 .reclaim = reclaim,
2784 .new_lock_owner = 0,
2785 };
2786 int status;
2787 2926
2788 if (!(lsp->ls_flags & NFS_LOCK_INITIALIZED)) { 2927 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2928 if (largs.lock_seqid == NULL)
2929 return -ENOMEM;
2930 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2789 struct nfs4_state_owner *owner = state->owner; 2931 struct nfs4_state_owner *owner = state->owner;
2790 struct nfs_open_to_lock otl = { 2932
2791 .lock_owner = { 2933 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2792 .clientid = server->nfs4_state->cl_clientid, 2934 if (largs.open_seqid == NULL)
2793 }, 2935 goto out;
2794 };
2795
2796 otl.lock_seqid = lsp->ls_seqid;
2797 otl.lock_owner.id = lsp->ls_id;
2798 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2799 largs.u.open_lock = &otl;
2800 largs.new_lock_owner = 1; 2936 largs.new_lock_owner = 1;
2801 arg.u.lock = &largs;
2802 down(&owner->so_sema);
2803 otl.open_seqid = owner->so_seqid;
2804 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 2937 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2805 /* increment open_owner seqid on success, and 2938 /* increment open seqid on success, and seqid mutating errors */
2806 * seqid mutating errors */ 2939 if (largs.new_lock_owner != 0) {
2807 nfs4_increment_seqid(status, owner); 2940 nfs_increment_open_seqid(status, largs.open_seqid);
2808 up(&owner->so_sema); 2941 if (status == 0)
2809 if (status == 0) { 2942 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2810 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2811 lsp->ls_seqid++;
2812 } 2943 }
2813 } else { 2944 nfs_free_seqid(largs.open_seqid);
2814 struct nfs_exist_lock el = { 2945 } else
2815 .seqid = lsp->ls_seqid,
2816 };
2817 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2818 largs.u.exist_lock = &el;
2819 arg.u.lock = &largs;
2820 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR); 2946 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2821 /* increment seqid on success, and * seqid mutating errors*/ 2947 /* increment lock seqid on success, and seqid mutating errors*/
2822 nfs4_increment_lock_seqid(status, lsp); 2948 nfs_increment_lock_seqid(status, largs.lock_seqid);
2823 }
2824 /* save the returned stateid. */ 2949 /* save the returned stateid. */
2825 if (status == 0) 2950 if (status == 0) {
2826 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid)); 2951 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2827 else if (status == -NFS4ERR_DENIED) 2952 sizeof(lsp->ls_stateid.data));
2953 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2954 } else if (status == -NFS4ERR_DENIED)
2828 status = -EAGAIN; 2955 status = -EAGAIN;
2956out:
2957 nfs_free_seqid(largs.lock_seqid);
2829 return status; 2958 return status;
2830} 2959}
2831 2960
@@ -2865,11 +2994,9 @@ static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock
2865 int status; 2994 int status;
2866 2995
2867 down_read(&clp->cl_sem); 2996 down_read(&clp->cl_sem);
2868 down(&state->lock_sema);
2869 status = nfs4_set_lock_state(state, request); 2997 status = nfs4_set_lock_state(state, request);
2870 if (status == 0) 2998 if (status == 0)
2871 status = _nfs4_do_setlk(state, cmd, request, 0); 2999 status = _nfs4_do_setlk(state, cmd, request, 0);
2872 up(&state->lock_sema);
2873 if (status == 0) { 3000 if (status == 0) {
2874 /* Note: we always want to sleep here! */ 3001 /* Note: we always want to sleep here! */
2875 request->fl_flags |= FL_SLEEP; 3002 request->fl_flags |= FL_SLEEP;
@@ -3024,8 +3151,8 @@ struct nfs_rpc_ops nfs_v4_clientops = {
3024 .read_setup = nfs4_proc_read_setup, 3151 .read_setup = nfs4_proc_read_setup,
3025 .write_setup = nfs4_proc_write_setup, 3152 .write_setup = nfs4_proc_write_setup,
3026 .commit_setup = nfs4_proc_commit_setup, 3153 .commit_setup = nfs4_proc_commit_setup,
3027 .file_open = nfs4_proc_file_open, 3154 .file_open = nfs_open,
3028 .file_release = nfs4_proc_file_release, 3155 .file_release = nfs_release,
3029 .lock = nfs4_proc_lock, 3156 .lock = nfs4_proc_lock,
3030 .clear_acl_cache = nfs4_zap_acl_attr, 3157 .clear_acl_cache = nfs4_zap_acl_attr,
3031}; 3158};
diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c
index afe587d82f1e..2d5a6a2b9dec 100644
--- a/fs/nfs/nfs4state.c
+++ b/fs/nfs/nfs4state.c
@@ -264,13 +264,16 @@ nfs4_alloc_state_owner(void)
264{ 264{
265 struct nfs4_state_owner *sp; 265 struct nfs4_state_owner *sp;
266 266
267 sp = kmalloc(sizeof(*sp),GFP_KERNEL); 267 sp = kzalloc(sizeof(*sp),GFP_KERNEL);
268 if (!sp) 268 if (!sp)
269 return NULL; 269 return NULL;
270 init_MUTEX(&sp->so_sema); 270 spin_lock_init(&sp->so_lock);
271 sp->so_seqid = 0; /* arbitrary */
272 INIT_LIST_HEAD(&sp->so_states); 271 INIT_LIST_HEAD(&sp->so_states);
273 INIT_LIST_HEAD(&sp->so_delegations); 272 INIT_LIST_HEAD(&sp->so_delegations);
273 rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
274 sp->so_seqid.sequence = &sp->so_sequence;
275 spin_lock_init(&sp->so_sequence.lock);
276 INIT_LIST_HEAD(&sp->so_sequence.list);
274 atomic_set(&sp->so_count, 1); 277 atomic_set(&sp->so_count, 1);
275 return sp; 278 return sp;
276} 279}
@@ -359,7 +362,6 @@ nfs4_alloc_open_state(void)
359 memset(state->stateid.data, 0, sizeof(state->stateid.data)); 362 memset(state->stateid.data, 0, sizeof(state->stateid.data));
360 atomic_set(&state->count, 1); 363 atomic_set(&state->count, 1);
361 INIT_LIST_HEAD(&state->lock_states); 364 INIT_LIST_HEAD(&state->lock_states);
362 init_MUTEX(&state->lock_sema);
363 spin_lock_init(&state->state_lock); 365 spin_lock_init(&state->state_lock);
364 return state; 366 return state;
365} 367}
@@ -437,21 +439,23 @@ nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
437 if (state) 439 if (state)
438 goto out; 440 goto out;
439 new = nfs4_alloc_open_state(); 441 new = nfs4_alloc_open_state();
442 spin_lock(&owner->so_lock);
440 spin_lock(&inode->i_lock); 443 spin_lock(&inode->i_lock);
441 state = __nfs4_find_state_byowner(inode, owner); 444 state = __nfs4_find_state_byowner(inode, owner);
442 if (state == NULL && new != NULL) { 445 if (state == NULL && new != NULL) {
443 state = new; 446 state = new;
444 /* Caller *must* be holding owner->so_sem */
445 /* Note: The reclaim code dictates that we add stateless
446 * and read-only stateids to the end of the list */
447 list_add_tail(&state->open_states, &owner->so_states);
448 state->owner = owner; 447 state->owner = owner;
449 atomic_inc(&owner->so_count); 448 atomic_inc(&owner->so_count);
450 list_add(&state->inode_states, &nfsi->open_states); 449 list_add(&state->inode_states, &nfsi->open_states);
451 state->inode = igrab(inode); 450 state->inode = igrab(inode);
452 spin_unlock(&inode->i_lock); 451 spin_unlock(&inode->i_lock);
452 /* Note: The reclaim code dictates that we add stateless
453 * and read-only stateids to the end of the list */
454 list_add_tail(&state->open_states, &owner->so_states);
455 spin_unlock(&owner->so_lock);
453 } else { 456 } else {
454 spin_unlock(&inode->i_lock); 457 spin_unlock(&inode->i_lock);
458 spin_unlock(&owner->so_lock);
455 if (new) 459 if (new)
456 nfs4_free_open_state(new); 460 nfs4_free_open_state(new);
457 } 461 }
@@ -461,19 +465,21 @@ out:
461 465
462/* 466/*
463 * Beware! Caller must be holding exactly one 467 * Beware! Caller must be holding exactly one
464 * reference to clp->cl_sem and owner->so_sema! 468 * reference to clp->cl_sem!
465 */ 469 */
466void nfs4_put_open_state(struct nfs4_state *state) 470void nfs4_put_open_state(struct nfs4_state *state)
467{ 471{
468 struct inode *inode = state->inode; 472 struct inode *inode = state->inode;
469 struct nfs4_state_owner *owner = state->owner; 473 struct nfs4_state_owner *owner = state->owner;
470 474
471 if (!atomic_dec_and_lock(&state->count, &inode->i_lock)) 475 if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
472 return; 476 return;
477 spin_lock(&inode->i_lock);
473 if (!list_empty(&state->inode_states)) 478 if (!list_empty(&state->inode_states))
474 list_del(&state->inode_states); 479 list_del(&state->inode_states);
475 spin_unlock(&inode->i_lock);
476 list_del(&state->open_states); 480 list_del(&state->open_states);
481 spin_unlock(&inode->i_lock);
482 spin_unlock(&owner->so_lock);
477 iput(inode); 483 iput(inode);
478 BUG_ON (state->state != 0); 484 BUG_ON (state->state != 0);
479 nfs4_free_open_state(state); 485 nfs4_free_open_state(state);
@@ -481,20 +487,17 @@ void nfs4_put_open_state(struct nfs4_state *state)
481} 487}
482 488
483/* 489/*
484 * Beware! Caller must be holding no references to clp->cl_sem! 490 * Close the current file.
485 * of owner->so_sema!
486 */ 491 */
487void nfs4_close_state(struct nfs4_state *state, mode_t mode) 492void nfs4_close_state(struct nfs4_state *state, mode_t mode)
488{ 493{
489 struct inode *inode = state->inode; 494 struct inode *inode = state->inode;
490 struct nfs4_state_owner *owner = state->owner; 495 struct nfs4_state_owner *owner = state->owner;
491 struct nfs4_client *clp = owner->so_client;
492 int newstate; 496 int newstate;
493 497
494 atomic_inc(&owner->so_count); 498 atomic_inc(&owner->so_count);
495 down_read(&clp->cl_sem);
496 down(&owner->so_sema);
497 /* Protect against nfs4_find_state() */ 499 /* Protect against nfs4_find_state() */
500 spin_lock(&owner->so_lock);
498 spin_lock(&inode->i_lock); 501 spin_lock(&inode->i_lock);
499 if (mode & FMODE_READ) 502 if (mode & FMODE_READ)
500 state->nreaders--; 503 state->nreaders--;
@@ -507,6 +510,7 @@ void nfs4_close_state(struct nfs4_state *state, mode_t mode)
507 list_move_tail(&state->open_states, &owner->so_states); 510 list_move_tail(&state->open_states, &owner->so_states);
508 } 511 }
509 spin_unlock(&inode->i_lock); 512 spin_unlock(&inode->i_lock);
513 spin_unlock(&owner->so_lock);
510 newstate = 0; 514 newstate = 0;
511 if (state->state != 0) { 515 if (state->state != 0) {
512 if (state->nreaders) 516 if (state->nreaders)
@@ -515,14 +519,16 @@ void nfs4_close_state(struct nfs4_state *state, mode_t mode)
515 newstate |= FMODE_WRITE; 519 newstate |= FMODE_WRITE;
516 if (state->state == newstate) 520 if (state->state == newstate)
517 goto out; 521 goto out;
518 if (nfs4_do_close(inode, state, newstate) == -EINPROGRESS) 522 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
523 state->state = newstate;
524 goto out;
525 }
526 if (nfs4_do_close(inode, state, newstate) == 0)
519 return; 527 return;
520 } 528 }
521out: 529out:
522 nfs4_put_open_state(state); 530 nfs4_put_open_state(state);
523 up(&owner->so_sema);
524 nfs4_put_state_owner(owner); 531 nfs4_put_state_owner(owner);
525 up_read(&clp->cl_sem);
526} 532}
527 533
528/* 534/*
@@ -546,19 +552,16 @@ __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
546 * Return a compatible lock_state. If no initialized lock_state structure 552 * Return a compatible lock_state. If no initialized lock_state structure
547 * exists, return an uninitialized one. 553 * exists, return an uninitialized one.
548 * 554 *
549 * The caller must be holding state->lock_sema
550 */ 555 */
551static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner) 556static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
552{ 557{
553 struct nfs4_lock_state *lsp; 558 struct nfs4_lock_state *lsp;
554 struct nfs4_client *clp = state->owner->so_client; 559 struct nfs4_client *clp = state->owner->so_client;
555 560
556 lsp = kmalloc(sizeof(*lsp), GFP_KERNEL); 561 lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
557 if (lsp == NULL) 562 if (lsp == NULL)
558 return NULL; 563 return NULL;
559 lsp->ls_flags = 0; 564 lsp->ls_seqid.sequence = &state->owner->so_sequence;
560 lsp->ls_seqid = 0; /* arbitrary */
561 memset(lsp->ls_stateid.data, 0, sizeof(lsp->ls_stateid.data));
562 atomic_set(&lsp->ls_count, 1); 565 atomic_set(&lsp->ls_count, 1);
563 lsp->ls_owner = fl_owner; 566 lsp->ls_owner = fl_owner;
564 spin_lock(&clp->cl_lock); 567 spin_lock(&clp->cl_lock);
@@ -572,7 +575,7 @@ static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, f
572 * Return a compatible lock_state. If no initialized lock_state structure 575 * Return a compatible lock_state. If no initialized lock_state structure
573 * exists, return an uninitialized one. 576 * exists, return an uninitialized one.
574 * 577 *
575 * The caller must be holding state->lock_sema and clp->cl_sem 578 * The caller must be holding clp->cl_sem
576 */ 579 */
577static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner) 580static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
578{ 581{
@@ -605,7 +608,7 @@ static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_
605 * Release reference to lock_state, and free it if we see that 608 * Release reference to lock_state, and free it if we see that
606 * it is no longer in use 609 * it is no longer in use
607 */ 610 */
608static void nfs4_put_lock_state(struct nfs4_lock_state *lsp) 611void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
609{ 612{
610 struct nfs4_state *state; 613 struct nfs4_state *state;
611 614
@@ -673,29 +676,94 @@ void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t f
673 nfs4_put_lock_state(lsp); 676 nfs4_put_lock_state(lsp);
674} 677}
675 678
676/* 679struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
677* Called with state->lock_sema and clp->cl_sem held.
678*/
679void nfs4_increment_lock_seqid(int status, struct nfs4_lock_state *lsp)
680{ 680{
681 if (status == NFS_OK || seqid_mutating_err(-status)) 681 struct nfs_seqid *new;
682 lsp->ls_seqid++; 682
683 new = kmalloc(sizeof(*new), GFP_KERNEL);
684 if (new != NULL) {
685 new->sequence = counter;
686 INIT_LIST_HEAD(&new->list);
687 }
688 return new;
689}
690
691void nfs_free_seqid(struct nfs_seqid *seqid)
692{
693 struct rpc_sequence *sequence = seqid->sequence->sequence;
694
695 if (!list_empty(&seqid->list)) {
696 spin_lock(&sequence->lock);
697 list_del(&seqid->list);
698 spin_unlock(&sequence->lock);
699 }
700 rpc_wake_up_next(&sequence->wait);
701 kfree(seqid);
683} 702}
684 703
685/* 704/*
686* Called with sp->so_sema and clp->cl_sem held. 705 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
687* 706 * failed with a seqid incrementing error -
688* Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or 707 * see comments nfs_fs.h:seqid_mutating_error()
689* failed with a seqid incrementing error - 708 */
690* see comments nfs_fs.h:seqid_mutating_error() 709static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
691*/ 710{
692void nfs4_increment_seqid(int status, struct nfs4_state_owner *sp) 711 switch (status) {
693{ 712 case 0:
694 if (status == NFS_OK || seqid_mutating_err(-status)) 713 break;
695 sp->so_seqid++; 714 case -NFS4ERR_BAD_SEQID:
696 /* If the server returns BAD_SEQID, unhash state_owner here */ 715 case -NFS4ERR_STALE_CLIENTID:
697 if (status == -NFS4ERR_BAD_SEQID) 716 case -NFS4ERR_STALE_STATEID:
717 case -NFS4ERR_BAD_STATEID:
718 case -NFS4ERR_BADXDR:
719 case -NFS4ERR_RESOURCE:
720 case -NFS4ERR_NOFILEHANDLE:
721 /* Non-seqid mutating errors */
722 return;
723 };
724 /*
725 * Note: no locking needed as we are guaranteed to be first
726 * on the sequence list
727 */
728 seqid->sequence->counter++;
729}
730
731void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
732{
733 if (status == -NFS4ERR_BAD_SEQID) {
734 struct nfs4_state_owner *sp = container_of(seqid->sequence,
735 struct nfs4_state_owner, so_seqid);
698 nfs4_drop_state_owner(sp); 736 nfs4_drop_state_owner(sp);
737 }
738 return nfs_increment_seqid(status, seqid);
739}
740
741/*
742 * Increment the seqid if the LOCK/LOCKU succeeded, or
743 * failed with a seqid incrementing error -
744 * see comments nfs_fs.h:seqid_mutating_error()
745 */
746void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
747{
748 return nfs_increment_seqid(status, seqid);
749}
750
751int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
752{
753 struct rpc_sequence *sequence = seqid->sequence->sequence;
754 int status = 0;
755
756 if (sequence->list.next == &seqid->list)
757 goto out;
758 spin_lock(&sequence->lock);
759 if (!list_empty(&sequence->list)) {
760 rpc_sleep_on(&sequence->wait, task, NULL, NULL);
761 status = -EAGAIN;
762 } else
763 list_add(&seqid->list, &sequence->list);
764 spin_unlock(&sequence->lock);
765out:
766 return status;
699} 767}
700 768
701static int reclaimer(void *); 769static int reclaimer(void *);
@@ -791,8 +859,6 @@ static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct n
791 if (state->state == 0) 859 if (state->state == 0)
792 continue; 860 continue;
793 status = ops->recover_open(sp, state); 861 status = ops->recover_open(sp, state);
794 list_for_each_entry(lock, &state->lock_states, ls_locks)
795 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
796 if (status >= 0) { 862 if (status >= 0) {
797 status = nfs4_reclaim_locks(ops, state); 863 status = nfs4_reclaim_locks(ops, state);
798 if (status < 0) 864 if (status < 0)
@@ -831,6 +897,28 @@ out_err:
831 return status; 897 return status;
832} 898}
833 899
900static void nfs4_state_mark_reclaim(struct nfs4_client *clp)
901{
902 struct nfs4_state_owner *sp;
903 struct nfs4_state *state;
904 struct nfs4_lock_state *lock;
905
906 /* Reset all sequence ids to zero */
907 list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
908 sp->so_seqid.counter = 0;
909 sp->so_seqid.flags = 0;
910 spin_lock(&sp->so_lock);
911 list_for_each_entry(state, &sp->so_states, open_states) {
912 list_for_each_entry(lock, &state->lock_states, ls_locks) {
913 lock->ls_seqid.counter = 0;
914 lock->ls_seqid.flags = 0;
915 lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
916 }
917 }
918 spin_unlock(&sp->so_lock);
919 }
920}
921
834static int reclaimer(void *ptr) 922static int reclaimer(void *ptr)
835{ 923{
836 struct reclaimer_args *args = (struct reclaimer_args *)ptr; 924 struct reclaimer_args *args = (struct reclaimer_args *)ptr;
@@ -864,6 +952,7 @@ restart_loop:
864 default: 952 default:
865 ops = &nfs4_network_partition_recovery_ops; 953 ops = &nfs4_network_partition_recovery_ops;
866 }; 954 };
955 nfs4_state_mark_reclaim(clp);
867 status = __nfs4_init_client(clp); 956 status = __nfs4_init_client(clp);
868 if (status) 957 if (status)
869 goto out_error; 958 goto out_error;
diff --git a/fs/nfs/nfs4xdr.c b/fs/nfs/nfs4xdr.c
index 6c564ef9489e..cd762648fa9a 100644
--- a/fs/nfs/nfs4xdr.c
+++ b/fs/nfs/nfs4xdr.c
@@ -602,10 +602,10 @@ static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
602{ 602{
603 uint32_t *p; 603 uint32_t *p;
604 604
605 RESERVE_SPACE(8+sizeof(arg->stateid.data)); 605 RESERVE_SPACE(8+sizeof(arg->stateid->data));
606 WRITE32(OP_CLOSE); 606 WRITE32(OP_CLOSE);
607 WRITE32(arg->seqid); 607 WRITE32(arg->seqid->sequence->counter);
608 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data)); 608 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
609 609
610 return 0; 610 return 0;
611} 611}
@@ -729,22 +729,18 @@ static int encode_lock(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
729 WRITE64(arg->length); 729 WRITE64(arg->length);
730 WRITE32(opargs->new_lock_owner); 730 WRITE32(opargs->new_lock_owner);
731 if (opargs->new_lock_owner){ 731 if (opargs->new_lock_owner){
732 struct nfs_open_to_lock *ol = opargs->u.open_lock;
733
734 RESERVE_SPACE(40); 732 RESERVE_SPACE(40);
735 WRITE32(ol->open_seqid); 733 WRITE32(opargs->open_seqid->sequence->counter);
736 WRITEMEM(&ol->open_stateid, sizeof(ol->open_stateid)); 734 WRITEMEM(opargs->open_stateid->data, sizeof(opargs->open_stateid->data));
737 WRITE32(ol->lock_seqid); 735 WRITE32(opargs->lock_seqid->sequence->counter);
738 WRITE64(ol->lock_owner.clientid); 736 WRITE64(opargs->lock_owner.clientid);
739 WRITE32(4); 737 WRITE32(4);
740 WRITE32(ol->lock_owner.id); 738 WRITE32(opargs->lock_owner.id);
741 } 739 }
742 else { 740 else {
743 struct nfs_exist_lock *el = opargs->u.exist_lock;
744
745 RESERVE_SPACE(20); 741 RESERVE_SPACE(20);
746 WRITEMEM(&el->stateid, sizeof(el->stateid)); 742 WRITEMEM(opargs->lock_stateid->data, sizeof(opargs->lock_stateid->data));
747 WRITE32(el->seqid); 743 WRITE32(opargs->lock_seqid->sequence->counter);
748 } 744 }
749 745
750 return 0; 746 return 0;
@@ -775,8 +771,8 @@ static int encode_locku(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
775 RESERVE_SPACE(44); 771 RESERVE_SPACE(44);
776 WRITE32(OP_LOCKU); 772 WRITE32(OP_LOCKU);
777 WRITE32(arg->type); 773 WRITE32(arg->type);
778 WRITE32(opargs->seqid); 774 WRITE32(opargs->seqid->sequence->counter);
779 WRITEMEM(&opargs->stateid, sizeof(opargs->stateid)); 775 WRITEMEM(opargs->stateid->data, sizeof(opargs->stateid->data));
780 WRITE64(arg->offset); 776 WRITE64(arg->offset);
781 WRITE64(arg->length); 777 WRITE64(arg->length);
782 778
@@ -826,7 +822,7 @@ static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_opena
826 */ 822 */
827 RESERVE_SPACE(8); 823 RESERVE_SPACE(8);
828 WRITE32(OP_OPEN); 824 WRITE32(OP_OPEN);
829 WRITE32(arg->seqid); 825 WRITE32(arg->seqid->sequence->counter);
830 encode_share_access(xdr, arg->open_flags); 826 encode_share_access(xdr, arg->open_flags);
831 RESERVE_SPACE(16); 827 RESERVE_SPACE(16);
832 WRITE64(arg->clientid); 828 WRITE64(arg->clientid);
@@ -941,7 +937,7 @@ static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_con
941 RESERVE_SPACE(8+sizeof(arg->stateid.data)); 937 RESERVE_SPACE(8+sizeof(arg->stateid.data));
942 WRITE32(OP_OPEN_CONFIRM); 938 WRITE32(OP_OPEN_CONFIRM);
943 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data)); 939 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
944 WRITE32(arg->seqid); 940 WRITE32(arg->seqid->sequence->counter);
945 941
946 return 0; 942 return 0;
947} 943}
@@ -950,10 +946,10 @@ static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closea
950{ 946{
951 uint32_t *p; 947 uint32_t *p;
952 948
953 RESERVE_SPACE(8+sizeof(arg->stateid.data)); 949 RESERVE_SPACE(8+sizeof(arg->stateid->data));
954 WRITE32(OP_OPEN_DOWNGRADE); 950 WRITE32(OP_OPEN_DOWNGRADE);
955 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data)); 951 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
956 WRITE32(arg->seqid); 952 WRITE32(arg->seqid->sequence->counter);
957 encode_share_access(xdr, arg->open_flags); 953 encode_share_access(xdr, arg->open_flags);
958 return 0; 954 return 0;
959} 955}
@@ -1437,6 +1433,9 @@ static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_opena
1437 }; 1433 };
1438 int status; 1434 int status;
1439 1435
1436 status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1437 if (status != 0)
1438 goto out;
1440 xdr_init_encode(&xdr, &req->rq_snd_buf, p); 1439 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1441 encode_compound_hdr(&xdr, &hdr); 1440 encode_compound_hdr(&xdr, &hdr);
1442 status = encode_putfh(&xdr, args->fh); 1441 status = encode_putfh(&xdr, args->fh);
@@ -1464,6 +1463,9 @@ static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct n
1464 }; 1463 };
1465 int status; 1464 int status;
1466 1465
1466 status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1467 if (status != 0)
1468 goto out;
1467 xdr_init_encode(&xdr, &req->rq_snd_buf, p); 1469 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1468 encode_compound_hdr(&xdr, &hdr); 1470 encode_compound_hdr(&xdr, &hdr);
1469 status = encode_putfh(&xdr, args->fh); 1471 status = encode_putfh(&xdr, args->fh);
@@ -1485,6 +1487,9 @@ static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nf
1485 }; 1487 };
1486 int status; 1488 int status;
1487 1489
1490 status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1491 if (status != 0)
1492 goto out;
1488 xdr_init_encode(&xdr, &req->rq_snd_buf, p); 1493 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1489 encode_compound_hdr(&xdr, &hdr); 1494 encode_compound_hdr(&xdr, &hdr);
1490 status = encode_putfh(&xdr, args->fh); 1495 status = encode_putfh(&xdr, args->fh);
@@ -1525,8 +1530,15 @@ static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_locka
1525 struct compound_hdr hdr = { 1530 struct compound_hdr hdr = {
1526 .nops = 2, 1531 .nops = 2,
1527 }; 1532 };
1533 struct nfs_lock_opargs *opargs = args->u.lock;
1528 int status; 1534 int status;
1529 1535
1536 status = nfs_wait_on_sequence(opargs->lock_seqid, req->rq_task);
1537 if (status != 0)
1538 goto out;
1539 /* Do we need to do an open_to_lock_owner? */
1540 if (opargs->lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1541 opargs->new_lock_owner = 0;
1530 xdr_init_encode(&xdr, &req->rq_snd_buf, p); 1542 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1531 encode_compound_hdr(&xdr, &hdr); 1543 encode_compound_hdr(&xdr, &hdr);
1532 status = encode_putfh(&xdr, args->fh); 1544 status = encode_putfh(&xdr, args->fh);
@@ -2890,8 +2902,8 @@ static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
2890 2902
2891 status = decode_op_hdr(xdr, OP_LOCK); 2903 status = decode_op_hdr(xdr, OP_LOCK);
2892 if (status == 0) { 2904 if (status == 0) {
2893 READ_BUF(sizeof(nfs4_stateid)); 2905 READ_BUF(sizeof(res->u.stateid.data));
2894 COPYMEM(&res->u.stateid, sizeof(res->u.stateid)); 2906 COPYMEM(res->u.stateid.data, sizeof(res->u.stateid.data));
2895 } else if (status == -NFS4ERR_DENIED) 2907 } else if (status == -NFS4ERR_DENIED)
2896 return decode_lock_denied(xdr, &res->u.denied); 2908 return decode_lock_denied(xdr, &res->u.denied);
2897 return status; 2909 return status;
@@ -2913,8 +2925,8 @@ static int decode_locku(struct xdr_stream *xdr, struct nfs_lockres *res)
2913 2925
2914 status = decode_op_hdr(xdr, OP_LOCKU); 2926 status = decode_op_hdr(xdr, OP_LOCKU);
2915 if (status == 0) { 2927 if (status == 0) {
2916 READ_BUF(sizeof(nfs4_stateid)); 2928 READ_BUF(sizeof(res->u.stateid.data));
2917 COPYMEM(&res->u.stateid, sizeof(res->u.stateid)); 2929 COPYMEM(res->u.stateid.data, sizeof(res->u.stateid.data));
2918 } 2930 }
2919 return status; 2931 return status;
2920} 2932}
@@ -3243,7 +3255,8 @@ static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3243 if (attrlen <= *acl_len) 3255 if (attrlen <= *acl_len)
3244 xdr_read_pages(xdr, attrlen); 3256 xdr_read_pages(xdr, attrlen);
3245 *acl_len = attrlen; 3257 *acl_len = attrlen;
3246 } 3258 } else
3259 status = -EOPNOTSUPP;
3247 3260
3248out: 3261out:
3249 return status; 3262 return status;
diff --git a/fs/nfs/proc.c b/fs/nfs/proc.c
index be23c3fb9260..8fef86523d7f 100644
--- a/fs/nfs/proc.c
+++ b/fs/nfs/proc.c
@@ -216,7 +216,7 @@ static int nfs_proc_write(struct nfs_write_data *wdata)
216 216
217static int 217static int
218nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 218nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
219 int flags) 219 int flags, struct nameidata *nd)
220{ 220{
221 struct nfs_fh fhandle; 221 struct nfs_fh fhandle;
222 struct nfs_fattr fattr; 222 struct nfs_fattr fattr;
diff --git a/fs/open.c b/fs/open.c
index f0d90cf0495c..8d06ec911fd9 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -739,7 +739,8 @@ asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
739} 739}
740 740
741static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, 741static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
742 int flags, struct file *f) 742 int flags, struct file *f,
743 int (*open)(struct inode *, struct file *))
743{ 744{
744 struct inode *inode; 745 struct inode *inode;
745 int error; 746 int error;
@@ -761,11 +762,14 @@ static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
761 f->f_op = fops_get(inode->i_fop); 762 f->f_op = fops_get(inode->i_fop);
762 file_move(f, &inode->i_sb->s_files); 763 file_move(f, &inode->i_sb->s_files);
763 764
764 if (f->f_op && f->f_op->open) { 765 if (!open && f->f_op)
765 error = f->f_op->open(inode,f); 766 open = f->f_op->open;
767 if (open) {
768 error = open(inode, f);
766 if (error) 769 if (error)
767 goto cleanup_all; 770 goto cleanup_all;
768 } 771 }
772
769 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); 773 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
770 774
771 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); 775 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
@@ -814,28 +818,75 @@ struct file *filp_open(const char * filename, int flags, int mode)
814{ 818{
815 int namei_flags, error; 819 int namei_flags, error;
816 struct nameidata nd; 820 struct nameidata nd;
817 struct file *f;
818 821
819 namei_flags = flags; 822 namei_flags = flags;
820 if ((namei_flags+1) & O_ACCMODE) 823 if ((namei_flags+1) & O_ACCMODE)
821 namei_flags++; 824 namei_flags++;
822 if (namei_flags & O_TRUNC)
823 namei_flags |= 2;
824
825 error = -ENFILE;
826 f = get_empty_filp();
827 if (f == NULL)
828 return ERR_PTR(error);
829 825
830 error = open_namei(filename, namei_flags, mode, &nd); 826 error = open_namei(filename, namei_flags, mode, &nd);
831 if (!error) 827 if (!error)
832 return __dentry_open(nd.dentry, nd.mnt, flags, f); 828 return nameidata_to_filp(&nd, flags);
833 829
834 put_filp(f);
835 return ERR_PTR(error); 830 return ERR_PTR(error);
836} 831}
837EXPORT_SYMBOL(filp_open); 832EXPORT_SYMBOL(filp_open);
838 833
834/**
835 * lookup_instantiate_filp - instantiates the open intent filp
836 * @nd: pointer to nameidata
837 * @dentry: pointer to dentry
838 * @open: open callback
839 *
840 * Helper for filesystems that want to use lookup open intents and pass back
841 * a fully instantiated struct file to the caller.
842 * This function is meant to be called from within a filesystem's
843 * lookup method.
844 * Note that in case of error, nd->intent.open.file is destroyed, but the
845 * path information remains valid.
846 * If the open callback is set to NULL, then the standard f_op->open()
847 * filesystem callback is substituted.
848 */
849struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
850 int (*open)(struct inode *, struct file *))
851{
852 if (IS_ERR(nd->intent.open.file))
853 goto out;
854 if (IS_ERR(dentry))
855 goto out_err;
856 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
857 nd->intent.open.flags - 1,
858 nd->intent.open.file,
859 open);
860out:
861 return nd->intent.open.file;
862out_err:
863 release_open_intent(nd);
864 nd->intent.open.file = (struct file *)dentry;
865 goto out;
866}
867EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
868
869/**
870 * nameidata_to_filp - convert a nameidata to an open filp.
871 * @nd: pointer to nameidata
872 * @flags: open flags
873 *
874 * Note that this function destroys the original nameidata
875 */
876struct file *nameidata_to_filp(struct nameidata *nd, int flags)
877{
878 struct file *filp;
879
880 /* Pick up the filp from the open intent */
881 filp = nd->intent.open.file;
882 /* Has the filesystem initialised the file for us? */
883 if (filp->f_dentry == NULL)
884 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
885 else
886 path_release(nd);
887 return filp;
888}
889
839struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags) 890struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
840{ 891{
841 int error; 892 int error;
@@ -846,7 +897,7 @@ struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
846 if (f == NULL) 897 if (f == NULL)
847 return ERR_PTR(error); 898 return ERR_PTR(error);
848 899
849 return __dentry_open(dentry, mnt, flags, f); 900 return __dentry_open(dentry, mnt, flags, f, NULL);
850} 901}
851EXPORT_SYMBOL(dentry_open); 902EXPORT_SYMBOL(dentry_open);
852 903