f363fd6c7f4db7acaa1491d12ff4c544005b4478
[linux-2.6.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/mm.h>
39 #include <linux/utsname.h>
40 #include <linux/delay.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/smp_lock.h>
49 #include <linux/namei.h>
50 #include <linux/mount.h>
51
52 #include "nfs4_fs.h"
53 #include "delegation.h"
54
55 #define NFSDBG_FACILITY         NFSDBG_PROC
56
57 #define NFS4_POLL_RETRY_MIN     (1*HZ)
58 #define NFS4_POLL_RETRY_MAX     (15*HZ)
59
60 static 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);
61 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
62 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
63 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
64 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
65 extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66 extern struct rpc_procinfo nfs4_procedures[];
67
68 /* Prevent leaks of NFSv4 errors into userland */
69 int nfs4_map_errors(int err)
70 {
71         if (err < -1000) {
72                 dprintk("%s could not handle NFSv4 error %d\n",
73                                 __FUNCTION__, -err);
74                 return -EIO;
75         }
76         return err;
77 }
78
79 /*
80  * This is our standard bitmap for GETATTR requests.
81  */
82 const u32 nfs4_fattr_bitmap[2] = {
83         FATTR4_WORD0_TYPE
84         | FATTR4_WORD0_CHANGE
85         | FATTR4_WORD0_SIZE
86         | FATTR4_WORD0_FSID
87         | FATTR4_WORD0_FILEID,
88         FATTR4_WORD1_MODE
89         | FATTR4_WORD1_NUMLINKS
90         | FATTR4_WORD1_OWNER
91         | FATTR4_WORD1_OWNER_GROUP
92         | FATTR4_WORD1_RAWDEV
93         | FATTR4_WORD1_SPACE_USED
94         | FATTR4_WORD1_TIME_ACCESS
95         | FATTR4_WORD1_TIME_METADATA
96         | FATTR4_WORD1_TIME_MODIFY
97 };
98
99 const u32 nfs4_statfs_bitmap[2] = {
100         FATTR4_WORD0_FILES_AVAIL
101         | FATTR4_WORD0_FILES_FREE
102         | FATTR4_WORD0_FILES_TOTAL,
103         FATTR4_WORD1_SPACE_AVAIL
104         | FATTR4_WORD1_SPACE_FREE
105         | FATTR4_WORD1_SPACE_TOTAL
106 };
107
108 const u32 nfs4_pathconf_bitmap[2] = {
109         FATTR4_WORD0_MAXLINK
110         | FATTR4_WORD0_MAXNAME,
111         0
112 };
113
114 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
115                         | FATTR4_WORD0_MAXREAD
116                         | FATTR4_WORD0_MAXWRITE
117                         | FATTR4_WORD0_LEASE_TIME,
118                         0
119 };
120
121 static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
122                 struct nfs4_readdir_arg *readdir)
123 {
124         u32 *start, *p;
125
126         BUG_ON(readdir->count < 80);
127         if (cookie > 2) {
128                 readdir->cookie = cookie;
129                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
130                 return;
131         }
132
133         readdir->cookie = 0;
134         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
135         if (cookie == 2)
136                 return;
137         
138         /*
139          * NFSv4 servers do not return entries for '.' and '..'
140          * Therefore, we fake these entries here.  We let '.'
141          * have cookie 0 and '..' have cookie 1.  Note that
142          * when talking to the server, we always send cookie 0
143          * instead of 1 or 2.
144          */
145         start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
146         
147         if (cookie == 0) {
148                 *p++ = xdr_one;                                  /* next */
149                 *p++ = xdr_zero;                   /* cookie, first word */
150                 *p++ = xdr_one;                   /* cookie, second word */
151                 *p++ = xdr_one;                             /* entry len */
152                 memcpy(p, ".\0\0\0", 4);                        /* entry */
153                 p++;
154                 *p++ = xdr_one;                         /* bitmap length */
155                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
156                 *p++ = htonl(8);              /* attribute buffer length */
157                 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
158         }
159         
160         *p++ = xdr_one;                                  /* next */
161         *p++ = xdr_zero;                   /* cookie, first word */
162         *p++ = xdr_two;                   /* cookie, second word */
163         *p++ = xdr_two;                             /* entry len */
164         memcpy(p, "..\0\0", 4);                         /* entry */
165         p++;
166         *p++ = xdr_one;                         /* bitmap length */
167         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
168         *p++ = htonl(8);              /* attribute buffer length */
169         p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
170
171         readdir->pgbase = (char *)p - (char *)start;
172         readdir->count -= readdir->pgbase;
173         kunmap_atomic(start, KM_USER0);
174 }
175
176 static void
177 renew_lease(struct nfs_server *server, unsigned long timestamp)
178 {
179         struct nfs4_client *clp = server->nfs4_state;
180         spin_lock(&clp->cl_lock);
181         if (time_before(clp->cl_last_renewal,timestamp))
182                 clp->cl_last_renewal = timestamp;
183         spin_unlock(&clp->cl_lock);
184 }
185
186 static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
187 {
188         struct nfs_inode *nfsi = NFS_I(inode);
189
190         spin_lock(&inode->i_lock);
191         nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
192         if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193                 nfsi->change_attr = cinfo->after;
194         spin_unlock(&inode->i_lock);
195 }
196
197 /* Helper for asynchronous RPC calls */
198 static int nfs4_call_async(struct rpc_clnt *clnt, rpc_action tk_begin,
199                 rpc_action tk_exit, void *calldata)
200 {
201         struct rpc_task *task;
202
203         if (!(task = rpc_new_task(clnt, tk_exit, RPC_TASK_ASYNC)))
204                 return -ENOMEM;
205
206         task->tk_calldata = calldata;
207         task->tk_action = tk_begin;
208         rpc_execute(task);
209         return 0;
210 }
211
212 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
213 {
214         struct inode *inode = state->inode;
215
216         open_flags &= (FMODE_READ|FMODE_WRITE);
217         /* Protect against nfs4_find_state() */
218         spin_lock(&state->owner->so_lock);
219         spin_lock(&inode->i_lock);
220         state->state |= open_flags;
221         /* NB! List reordering - see the reclaim code for why.  */
222         if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
223                 list_move(&state->open_states, &state->owner->so_states);
224         if (open_flags & FMODE_READ)
225                 state->nreaders++;
226         memcpy(&state->stateid, stateid, sizeof(state->stateid));
227         spin_unlock(&inode->i_lock);
228         spin_unlock(&state->owner->so_lock);
229 }
230
231 /*
232  * OPEN_RECLAIM:
233  *      reclaim state on the server after a reboot.
234  */
235 static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
236 {
237         struct inode *inode = state->inode;
238         struct nfs_server *server = NFS_SERVER(inode);
239         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
240         struct nfs_openargs o_arg = {
241                 .fh = NFS_FH(inode),
242                 .id = sp->so_id,
243                 .open_flags = state->state,
244                 .clientid = server->nfs4_state->cl_clientid,
245                 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
246                 .bitmask = server->attr_bitmask,
247         };
248         struct nfs_openres o_res = {
249                 .server = server,       /* Grrr */
250         };
251         struct rpc_message msg = {
252                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
253                 .rpc_argp       = &o_arg,
254                 .rpc_resp       = &o_res,
255                 .rpc_cred       = sp->so_cred,
256         };
257         int status;
258
259         if (delegation != NULL) {
260                 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
261                         memcpy(&state->stateid, &delegation->stateid,
262                                         sizeof(state->stateid));
263                         set_bit(NFS_DELEGATED_STATE, &state->flags);
264                         return 0;
265                 }
266                 o_arg.u.delegation_type = delegation->type;
267         }
268         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
269         if (o_arg.seqid == NULL)
270                 return -ENOMEM;
271         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
272         /* Confirm the sequence as being established */
273         nfs_confirm_seqid(&sp->so_seqid, status);
274         nfs_increment_open_seqid(status, o_arg.seqid);
275         if (status == 0) {
276                 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
277                 if (o_res.delegation_type != 0) {
278                         nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
279                         /* Did the server issue an immediate delegation recall? */
280                         if (o_res.do_recall)
281                                 nfs_async_inode_return_delegation(inode, &o_res.stateid);
282                 }
283         }
284         nfs_free_seqid(o_arg.seqid);
285         clear_bit(NFS_DELEGATED_STATE, &state->flags);
286         /* Ensure we update the inode attributes */
287         NFS_CACHEINV(inode);
288         return status;
289 }
290
291 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
292 {
293         struct nfs_server *server = NFS_SERVER(state->inode);
294         struct nfs4_exception exception = { };
295         int err;
296         do {
297                 err = _nfs4_open_reclaim(sp, state);
298                 if (err != -NFS4ERR_DELAY)
299                         break;
300                 nfs4_handle_exception(server, err, &exception);
301         } while (exception.retry);
302         return err;
303 }
304
305 static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
306 {
307         struct nfs4_state_owner  *sp  = state->owner;
308         struct inode *inode = dentry->d_inode;
309         struct nfs_server *server = NFS_SERVER(inode);
310         struct dentry *parent = dget_parent(dentry);
311         struct nfs_openargs arg = {
312                 .fh = NFS_FH(parent->d_inode),
313                 .clientid = server->nfs4_state->cl_clientid,
314                 .name = &dentry->d_name,
315                 .id = sp->so_id,
316                 .server = server,
317                 .bitmask = server->attr_bitmask,
318                 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
319         };
320         struct nfs_openres res = {
321                 .server = server,
322         };
323         struct  rpc_message msg = {
324                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
325                 .rpc_argp       = &arg,
326                 .rpc_resp       = &res,
327                 .rpc_cred       = sp->so_cred,
328         };
329         int status = 0;
330
331         if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
332                 goto out;
333         if (state->state == 0)
334                 goto out;
335         arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
336         status = -ENOMEM;
337         if (arg.seqid == NULL)
338                 goto out;
339         arg.open_flags = state->state;
340         memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
341         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
342         nfs_increment_open_seqid(status, arg.seqid);
343         if (status != 0)
344                 goto out_free;
345         if(res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
346                 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
347                                 sp, &res.stateid, arg.seqid);
348                 if (status != 0)
349                         goto out_free;
350         }
351         nfs_confirm_seqid(&sp->so_seqid, 0);
352         if (status >= 0) {
353                 memcpy(state->stateid.data, res.stateid.data,
354                                 sizeof(state->stateid.data));
355                 clear_bit(NFS_DELEGATED_STATE, &state->flags);
356         }
357 out_free:
358         nfs_free_seqid(arg.seqid);
359 out:
360         dput(parent);
361         return status;
362 }
363
364 int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
365 {
366         struct nfs4_exception exception = { };
367         struct nfs_server *server = NFS_SERVER(dentry->d_inode);
368         int err;
369         do {
370                 err = _nfs4_open_delegation_recall(dentry, state);
371                 switch (err) {
372                         case 0:
373                                 return err;
374                         case -NFS4ERR_STALE_CLIENTID:
375                         case -NFS4ERR_STALE_STATEID:
376                         case -NFS4ERR_EXPIRED:
377                                 /* Don't recall a delegation if it was lost */
378                                 nfs4_schedule_state_recovery(server->nfs4_state);
379                                 return err;
380                 }
381                 err = nfs4_handle_exception(server, err, &exception);
382         } while (exception.retry);
383         return err;
384 }
385
386 static 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)
387 {
388         struct nfs_open_confirmargs arg = {
389                 .fh             = fh,
390                 .seqid          = seqid,
391                 .stateid        = *stateid,
392         };
393         struct nfs_open_confirmres res;
394         struct  rpc_message msg = {
395                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
396                 .rpc_argp       = &arg,
397                 .rpc_resp       = &res,
398                 .rpc_cred       = sp->so_cred,
399         };
400         int status;
401
402         status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
403         /* Confirm the sequence as being established */
404         nfs_confirm_seqid(&sp->so_seqid, status);
405         nfs_increment_open_seqid(status, seqid);
406         if (status >= 0)
407                 memcpy(stateid, &res.stateid, sizeof(*stateid));
408         return status;
409 }
410
411 static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner  *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
412 {
413         struct nfs_server *server = NFS_SERVER(dir);
414         struct rpc_message msg = {
415                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
416                 .rpc_argp = o_arg,
417                 .rpc_resp = o_res,
418                 .rpc_cred = sp->so_cred,
419         };
420         int status;
421
422         /* Update sequence id. The caller must serialize! */
423         o_arg->id = sp->so_id;
424         o_arg->clientid = sp->so_client->cl_clientid;
425
426         status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
427         if (status == 0) {
428                 /* OPEN on anything except a regular file is disallowed in NFSv4 */
429                 switch (o_res->f_attr->mode & S_IFMT) {
430                         case S_IFREG:
431                                 break;
432                         case S_IFLNK:
433                                 status = -ELOOP;
434                                 break;
435                         case S_IFDIR:
436                                 status = -EISDIR;
437                                 break;
438                         default:
439                                 status = -ENOTDIR;
440                 }
441         }
442
443         nfs_increment_open_seqid(status, o_arg->seqid);
444         if (status != 0)
445                 goto out;
446         if (o_arg->open_flags & O_CREAT) {
447                 update_changeattr(dir, &o_res->cinfo);
448                 nfs_post_op_update_inode(dir, o_res->dir_attr);
449         } else
450                 nfs_refresh_inode(dir, o_res->dir_attr);
451         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
452                 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
453                                 sp, &o_res->stateid, o_arg->seqid);
454                 if (status != 0)
455                         goto out;
456         }
457         nfs_confirm_seqid(&sp->so_seqid, 0);
458         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
459                 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
460 out:
461         return status;
462 }
463
464 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
465 {
466         struct nfs_access_entry cache;
467         int mask = 0;
468         int status;
469
470         if (openflags & FMODE_READ)
471                 mask |= MAY_READ;
472         if (openflags & FMODE_WRITE)
473                 mask |= MAY_WRITE;
474         status = nfs_access_get_cached(inode, cred, &cache);
475         if (status == 0)
476                 goto out;
477
478         /* Be clever: ask server to check for all possible rights */
479         cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
480         cache.cred = cred;
481         cache.jiffies = jiffies;
482         status = _nfs4_proc_access(inode, &cache);
483         if (status != 0)
484                 return status;
485         nfs_access_add_cache(inode, &cache);
486 out:
487         if ((cache.mask & mask) == mask)
488                 return 0;
489         return -EACCES;
490 }
491
492 /*
493  * OPEN_EXPIRED:
494  *      reclaim state on the server after a network partition.
495  *      Assumes caller holds the appropriate lock
496  */
497 static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
498 {
499         struct dentry *parent = dget_parent(dentry);
500         struct inode *dir = parent->d_inode;
501         struct inode *inode = state->inode;
502         struct nfs_server *server = NFS_SERVER(dir);
503         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
504         struct nfs_fattr f_attr, dir_attr;
505         struct nfs_openargs o_arg = {
506                 .fh = NFS_FH(dir),
507                 .open_flags = state->state,
508                 .name = &dentry->d_name,
509                 .bitmask = server->attr_bitmask,
510                 .claim = NFS4_OPEN_CLAIM_NULL,
511         };
512         struct nfs_openres o_res = {
513                 .f_attr = &f_attr,
514                 .dir_attr = &dir_attr,
515                 .server = server,
516         };
517         int status = 0;
518
519         if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
520                 status = _nfs4_do_access(inode, sp->so_cred, state->state);
521                 if (status < 0)
522                         goto out;
523                 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
524                 set_bit(NFS_DELEGATED_STATE, &state->flags);
525                 goto out;
526         }
527         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
528         status = -ENOMEM;
529         if (o_arg.seqid == NULL)
530                 goto out;
531         nfs_fattr_init(&f_attr);
532         nfs_fattr_init(&dir_attr);
533         status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
534         if (status != 0)
535                 goto out_nodeleg;
536         /* Check if files differ */
537         if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
538                 goto out_stale;
539         /* Has the file handle changed? */
540         if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
541                 /* Verify if the change attributes are the same */
542                 if (f_attr.change_attr != NFS_I(inode)->change_attr)
543                         goto out_stale;
544                 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
545                         goto out_stale;
546                 /* Lets just pretend that this is the same file */
547                 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
548                 NFS_I(inode)->fileid = f_attr.fileid;
549         }
550         memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
551         if (o_res.delegation_type != 0) {
552                 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
553                         nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
554                 else
555                         nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
556         }
557 out_nodeleg:
558         nfs_free_seqid(o_arg.seqid);
559         clear_bit(NFS_DELEGATED_STATE, &state->flags);
560 out:
561         dput(parent);
562         return status;
563 out_stale:
564         status = -ESTALE;
565         /* Invalidate the state owner so we don't ever use it again */
566         nfs4_drop_state_owner(sp);
567         d_drop(dentry);
568         /* Should we be trying to close that stateid? */
569         goto out_nodeleg;
570 }
571
572 static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
573 {
574         struct nfs_server *server = NFS_SERVER(dentry->d_inode);
575         struct nfs4_exception exception = { };
576         int err;
577
578         do {
579                 err = _nfs4_open_expired(sp, state, dentry);
580                 if (err == -NFS4ERR_DELAY)
581                         nfs4_handle_exception(server, err, &exception);
582         } while (exception.retry);
583         return err;
584 }
585
586 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
587 {
588         struct nfs_inode *nfsi = NFS_I(state->inode);
589         struct nfs_open_context *ctx;
590         int status;
591
592         spin_lock(&state->inode->i_lock);
593         list_for_each_entry(ctx, &nfsi->open_files, list) {
594                 if (ctx->state != state)
595                         continue;
596                 get_nfs_open_context(ctx);
597                 spin_unlock(&state->inode->i_lock);
598                 status = nfs4_do_open_expired(sp, state, ctx->dentry);
599                 put_nfs_open_context(ctx);
600                 return status;
601         }
602         spin_unlock(&state->inode->i_lock);
603         return -ENOENT;
604 }
605
606 /*
607  * Returns an nfs4_state + an extra reference to the inode
608  */
609 static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
610 {
611         struct nfs_delegation *delegation;
612         struct nfs_server *server = NFS_SERVER(inode);
613         struct nfs4_client *clp = server->nfs4_state;
614         struct nfs_inode *nfsi = NFS_I(inode);
615         struct nfs4_state_owner *sp = NULL;
616         struct nfs4_state *state = NULL;
617         int open_flags = flags & (FMODE_READ|FMODE_WRITE);
618         int err;
619
620         /* Protect against reboot recovery - NOTE ORDER! */
621         down_read(&clp->cl_sem);
622         /* Protect against delegation recall */
623         down_read(&nfsi->rwsem);
624         delegation = NFS_I(inode)->delegation;
625         err = -ENOENT;
626         if (delegation == NULL || (delegation->type & open_flags) != open_flags)
627                 goto out_err;
628         err = -ENOMEM;
629         if (!(sp = nfs4_get_state_owner(server, cred))) {
630                 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
631                 goto out_err;
632         }
633         state = nfs4_get_open_state(inode, sp);
634         if (state == NULL)
635                 goto out_err;
636
637         err = -ENOENT;
638         if ((state->state & open_flags) == open_flags) {
639                 spin_lock(&inode->i_lock);
640                 if (open_flags & FMODE_READ)
641                         state->nreaders++;
642                 if (open_flags & FMODE_WRITE)
643                         state->nwriters++;
644                 spin_unlock(&inode->i_lock);
645                 goto out_ok;
646         } else if (state->state != 0)
647                 goto out_err;
648
649         lock_kernel();
650         err = _nfs4_do_access(inode, cred, open_flags);
651         unlock_kernel();
652         if (err != 0)
653                 goto out_err;
654         set_bit(NFS_DELEGATED_STATE, &state->flags);
655         update_open_stateid(state, &delegation->stateid, open_flags);
656 out_ok:
657         nfs4_put_state_owner(sp);
658         up_read(&nfsi->rwsem);
659         up_read(&clp->cl_sem);
660         igrab(inode);
661         *res = state;
662         return 0; 
663 out_err:
664         if (sp != NULL) {
665                 if (state != NULL)
666                         nfs4_put_open_state(state);
667                 nfs4_put_state_owner(sp);
668         }
669         up_read(&nfsi->rwsem);
670         up_read(&clp->cl_sem);
671         if (err != -EACCES)
672                 nfs_inode_return_delegation(inode);
673         return err;
674 }
675
676 static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
677 {
678         struct nfs4_exception exception = { };
679         struct nfs4_state *res;
680         int err;
681
682         do {
683                 err = _nfs4_open_delegated(inode, flags, cred, &res);
684                 if (err == 0)
685                         break;
686                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
687                                         err, &exception));
688         } while (exception.retry);
689         return res;
690 }
691
692 /*
693  * Returns an nfs4_state + an referenced inode
694  */
695 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
696 {
697         struct nfs4_state_owner  *sp;
698         struct nfs4_state     *state = NULL;
699         struct nfs_server       *server = NFS_SERVER(dir);
700         struct nfs4_client *clp = server->nfs4_state;
701         struct inode *inode = NULL;
702         int                     status;
703         struct nfs_fattr f_attr, dir_attr;
704         struct nfs_openargs o_arg = {
705                 .fh             = NFS_FH(dir),
706                 .open_flags     = flags,
707                 .name           = &dentry->d_name,
708                 .server         = server,
709                 .bitmask = server->attr_bitmask,
710                 .claim = NFS4_OPEN_CLAIM_NULL,
711         };
712         struct nfs_openres o_res = {
713                 .f_attr         = &f_attr,
714                 .dir_attr       = &dir_attr,
715                 .server         = server,
716         };
717
718         /* Protect against reboot recovery conflicts */
719         down_read(&clp->cl_sem);
720         status = -ENOMEM;
721         if (!(sp = nfs4_get_state_owner(server, cred))) {
722                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
723                 goto out_err;
724         }
725         if (flags & O_EXCL) {
726                 u32 *p = (u32 *) o_arg.u.verifier.data;
727                 p[0] = jiffies;
728                 p[1] = current->pid;
729         } else
730                 o_arg.u.attrs = sattr;
731         /* Serialization for the sequence id */
732
733         o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
734         if (o_arg.seqid == NULL)
735                 return -ENOMEM;
736         nfs_fattr_init(&f_attr);
737         nfs_fattr_init(&dir_attr);
738         status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
739         if (status != 0)
740                 goto out_err;
741
742         status = -ENOMEM;
743         inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
744         if (!inode)
745                 goto out_err;
746         state = nfs4_get_open_state(inode, sp);
747         if (!state)
748                 goto out_err;
749         update_open_stateid(state, &o_res.stateid, flags);
750         if (o_res.delegation_type != 0)
751                 nfs_inode_set_delegation(inode, cred, &o_res);
752         nfs_free_seqid(o_arg.seqid);
753         nfs4_put_state_owner(sp);
754         up_read(&clp->cl_sem);
755         *res = state;
756         return 0;
757 out_err:
758         if (sp != NULL) {
759                 if (state != NULL)
760                         nfs4_put_open_state(state);
761                 nfs_free_seqid(o_arg.seqid);
762                 nfs4_put_state_owner(sp);
763         }
764         /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
765         up_read(&clp->cl_sem);
766         if (inode != NULL)
767                 iput(inode);
768         *res = NULL;
769         return status;
770 }
771
772
773 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
774 {
775         struct nfs4_exception exception = { };
776         struct nfs4_state *res;
777         int status;
778
779         do {
780                 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
781                 if (status == 0)
782                         break;
783                 /* NOTE: BAD_SEQID means the server and client disagree about the
784                  * book-keeping w.r.t. state-changing operations
785                  * (OPEN/CLOSE/LOCK/LOCKU...)
786                  * It is actually a sign of a bug on the client or on the server.
787                  *
788                  * If we receive a BAD_SEQID error in the particular case of
789                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
790                  * have unhashed the old state_owner for us, and that we can
791                  * therefore safely retry using a new one. We should still warn
792                  * the user though...
793                  */
794                 if (status == -NFS4ERR_BAD_SEQID) {
795                         printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
796                         exception.retry = 1;
797                         continue;
798                 }
799                 /*
800                  * BAD_STATEID on OPEN means that the server cancelled our
801                  * state before it received the OPEN_CONFIRM.
802                  * Recover by retrying the request as per the discussion
803                  * on Page 181 of RFC3530.
804                  */
805                 if (status == -NFS4ERR_BAD_STATEID) {
806                         exception.retry = 1;
807                         continue;
808                 }
809                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
810                                         status, &exception));
811         } while (exception.retry);
812         return res;
813 }
814
815 static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
816                 struct nfs_fh *fhandle, struct iattr *sattr,
817                 struct nfs4_state *state)
818 {
819         struct nfs_setattrargs  arg = {
820                 .fh             = fhandle,
821                 .iap            = sattr,
822                 .server         = server,
823                 .bitmask = server->attr_bitmask,
824         };
825         struct nfs_setattrres  res = {
826                 .fattr          = fattr,
827                 .server         = server,
828         };
829         struct rpc_message msg = {
830                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
831                 .rpc_argp       = &arg,
832                 .rpc_resp       = &res,
833         };
834         int status;
835
836         nfs_fattr_init(fattr);
837
838         if (state != NULL) {
839                 msg.rpc_cred = state->owner->so_cred;
840                 nfs4_copy_stateid(&arg.stateid, state, current->files);
841         } else
842                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
843
844         status = rpc_call_sync(server->client, &msg, 0);
845         return status;
846 }
847
848 static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
849                 struct nfs_fh *fhandle, struct iattr *sattr,
850                 struct nfs4_state *state)
851 {
852         struct nfs4_exception exception = { };
853         int err;
854         do {
855                 err = nfs4_handle_exception(server,
856                                 _nfs4_do_setattr(server, fattr, fhandle, sattr,
857                                         state),
858                                 &exception);
859         } while (exception.retry);
860         return err;
861 }
862
863 struct nfs4_closedata {
864         struct inode *inode;
865         struct nfs4_state *state;
866         struct nfs_closeargs arg;
867         struct nfs_closeres res;
868 };
869
870 static void nfs4_free_closedata(struct nfs4_closedata *calldata)
871 {
872         struct nfs4_state *state = calldata->state;
873         struct nfs4_state_owner *sp = state->owner;
874
875         nfs4_put_open_state(calldata->state);
876         nfs_free_seqid(calldata->arg.seqid);
877         nfs4_put_state_owner(sp);
878         kfree(calldata);
879 }
880
881 static void nfs4_close_done(struct rpc_task *task)
882 {
883         struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
884         struct nfs4_state *state = calldata->state;
885         struct nfs_server *server = NFS_SERVER(calldata->inode);
886
887         /* hmm. we are done with the inode, and in the process of freeing
888          * the state_owner. we keep this around to process errors
889          */
890         nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
891         switch (task->tk_status) {
892                 case 0:
893                         memcpy(&state->stateid, &calldata->res.stateid,
894                                         sizeof(state->stateid));
895                         break;
896                 case -NFS4ERR_STALE_STATEID:
897                 case -NFS4ERR_EXPIRED:
898                         state->state = calldata->arg.open_flags;
899                         nfs4_schedule_state_recovery(server->nfs4_state);
900                         break;
901                 default:
902                         if (nfs4_async_handle_error(task, server) == -EAGAIN) {
903                                 rpc_restart_call(task);
904                                 return;
905                         }
906         }
907         state->state = calldata->arg.open_flags;
908         nfs4_free_closedata(calldata);
909 }
910
911 static void nfs4_close_begin(struct rpc_task *task)
912 {
913         struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
914         struct nfs4_state *state = calldata->state;
915         struct rpc_message msg = {
916                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
917                 .rpc_argp = &calldata->arg,
918                 .rpc_resp = &calldata->res,
919                 .rpc_cred = state->owner->so_cred,
920         };
921         int mode = 0;
922         int status;
923
924         status = nfs_wait_on_sequence(calldata->arg.seqid, task);
925         if (status != 0)
926                 return;
927         /* Don't reorder reads */
928         smp_rmb();
929         /* Recalculate the new open mode in case someone reopened the file
930          * while we were waiting in line to be scheduled.
931          */
932         if (state->nreaders != 0)
933                 mode |= FMODE_READ;
934         if (state->nwriters != 0)
935                 mode |= FMODE_WRITE;
936         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
937                 state->state = mode;
938         if (mode == state->state) {
939                 nfs4_free_closedata(calldata);
940                 task->tk_exit = NULL;
941                 rpc_exit(task, 0);
942                 return;
943         }
944         if (mode != 0)
945                 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
946         calldata->arg.open_flags = mode;
947         rpc_call_setup(task, &msg, 0);
948 }
949
950 /* 
951  * It is possible for data to be read/written from a mem-mapped file 
952  * after the sys_close call (which hits the vfs layer as a flush).
953  * This means that we can't safely call nfsv4 close on a file until 
954  * the inode is cleared. This in turn means that we are not good
955  * NFSv4 citizens - we do not indicate to the server to update the file's 
956  * share state even when we are done with one of the three share 
957  * stateid's in the inode.
958  *
959  * NOTE: Caller must be holding the sp->so_owner semaphore!
960  */
961 int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode) 
962 {
963         struct nfs4_closedata *calldata;
964         int status = -ENOMEM;
965
966         calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
967         if (calldata == NULL)
968                 goto out;
969         calldata->inode = inode;
970         calldata->state = state;
971         calldata->arg.fh = NFS_FH(inode);
972         calldata->arg.stateid = &state->stateid;
973         /* Serialization for the sequence id */
974         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
975         if (calldata->arg.seqid == NULL)
976                 goto out_free_calldata;
977
978         status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_close_begin,
979                         nfs4_close_done, calldata);
980         if (status == 0)
981                 goto out;
982
983         nfs_free_seqid(calldata->arg.seqid);
984 out_free_calldata:
985         kfree(calldata);
986 out:
987         return status;
988 }
989
990 static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
991 {
992         struct file *filp;
993
994         filp = lookup_instantiate_filp(nd, dentry, NULL);
995         if (!IS_ERR(filp)) {
996                 struct nfs_open_context *ctx;
997                 ctx = (struct nfs_open_context *)filp->private_data;
998                 ctx->state = state;
999         } else
1000                 nfs4_close_state(state, nd->intent.open.flags);
1001 }
1002
1003 struct dentry *
1004 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1005 {
1006         struct iattr attr;
1007         struct rpc_cred *cred;
1008         struct nfs4_state *state;
1009         struct dentry *res;
1010
1011         if (nd->flags & LOOKUP_CREATE) {
1012                 attr.ia_mode = nd->intent.open.create_mode;
1013                 attr.ia_valid = ATTR_MODE;
1014                 if (!IS_POSIXACL(dir))
1015                         attr.ia_mode &= ~current->fs->umask;
1016         } else {
1017                 attr.ia_valid = 0;
1018                 BUG_ON(nd->intent.open.flags & O_CREAT);
1019         }
1020
1021         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1022         if (IS_ERR(cred))
1023                 return (struct dentry *)cred;
1024         state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1025         put_rpccred(cred);
1026         if (IS_ERR(state)) {
1027                 if (PTR_ERR(state) == -ENOENT)
1028                         d_add(dentry, NULL);
1029                 return (struct dentry *)state;
1030         }
1031         res = d_add_unique(dentry, state->inode);
1032         if (res != NULL)
1033                 dentry = res;
1034         nfs4_intent_set_file(nd, dentry, state);
1035         return res;
1036 }
1037
1038 int
1039 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1040 {
1041         struct rpc_cred *cred;
1042         struct nfs4_state *state;
1043         struct inode *inode;
1044
1045         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1046         if (IS_ERR(cred))
1047                 return PTR_ERR(cred);
1048         state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1049         if (IS_ERR(state))
1050                 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1051         put_rpccred(cred);
1052         if (IS_ERR(state)) {
1053                 switch (PTR_ERR(state)) {
1054                         case -EPERM:
1055                         case -EACCES:
1056                         case -EDQUOT:
1057                         case -ENOSPC:
1058                         case -EROFS:
1059                                 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1060                                 return 1;
1061                         case -ENOENT:
1062                                 if (dentry->d_inode == NULL)
1063                                         return 1;
1064                 }
1065                 goto out_drop;
1066         }
1067         inode = state->inode;
1068         iput(inode);
1069         if (inode == dentry->d_inode) {
1070                 nfs4_intent_set_file(nd, dentry, state);
1071                 return 1;
1072         }
1073         nfs4_close_state(state, openflags);
1074 out_drop:
1075         d_drop(dentry);
1076         return 0;
1077 }
1078
1079
1080 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1081 {
1082         struct nfs4_server_caps_res res = {};
1083         struct rpc_message msg = {
1084                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1085                 .rpc_argp = fhandle,
1086                 .rpc_resp = &res,
1087         };
1088         int status;
1089
1090         status = rpc_call_sync(server->client, &msg, 0);
1091         if (status == 0) {
1092                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1093                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1094                         server->caps |= NFS_CAP_ACLS;
1095                 if (res.has_links != 0)
1096                         server->caps |= NFS_CAP_HARDLINKS;
1097                 if (res.has_symlinks != 0)
1098                         server->caps |= NFS_CAP_SYMLINKS;
1099                 server->acl_bitmask = res.acl_bitmask;
1100         }
1101         return status;
1102 }
1103
1104 static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1105 {
1106         struct nfs4_exception exception = { };
1107         int err;
1108         do {
1109                 err = nfs4_handle_exception(server,
1110                                 _nfs4_server_capabilities(server, fhandle),
1111                                 &exception);
1112         } while (exception.retry);
1113         return err;
1114 }
1115
1116 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1117                 struct nfs_fsinfo *info)
1118 {
1119         struct nfs4_lookup_root_arg args = {
1120                 .bitmask = nfs4_fattr_bitmap,
1121         };
1122         struct nfs4_lookup_res res = {
1123                 .server = server,
1124                 .fattr = info->fattr,
1125                 .fh = fhandle,
1126         };
1127         struct rpc_message msg = {
1128                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1129                 .rpc_argp = &args,
1130                 .rpc_resp = &res,
1131         };
1132         nfs_fattr_init(info->fattr);
1133         return rpc_call_sync(server->client, &msg, 0);
1134 }
1135
1136 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1137                 struct nfs_fsinfo *info)
1138 {
1139         struct nfs4_exception exception = { };
1140         int err;
1141         do {
1142                 err = nfs4_handle_exception(server,
1143                                 _nfs4_lookup_root(server, fhandle, info),
1144                                 &exception);
1145         } while (exception.retry);
1146         return err;
1147 }
1148
1149 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1150                 struct nfs_fsinfo *info)
1151 {
1152         struct nfs_fattr *      fattr = info->fattr;
1153         unsigned char *         p;
1154         struct qstr             q;
1155         struct nfs4_lookup_arg args = {
1156                 .dir_fh = fhandle,
1157                 .name = &q,
1158                 .bitmask = nfs4_fattr_bitmap,
1159         };
1160         struct nfs4_lookup_res res = {
1161                 .server = server,
1162                 .fattr = fattr,
1163                 .fh = fhandle,
1164         };
1165         struct rpc_message msg = {
1166                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1167                 .rpc_argp = &args,
1168                 .rpc_resp = &res,
1169         };
1170         int status;
1171
1172         /*
1173          * Now we do a separate LOOKUP for each component of the mount path.
1174          * The LOOKUPs are done separately so that we can conveniently
1175          * catch an ERR_WRONGSEC if it occurs along the way...
1176          */
1177         status = nfs4_lookup_root(server, fhandle, info);
1178         if (status)
1179                 goto out;
1180
1181         p = server->mnt_path;
1182         for (;;) {
1183                 struct nfs4_exception exception = { };
1184
1185                 while (*p == '/')
1186                         p++;
1187                 if (!*p)
1188                         break;
1189                 q.name = p;
1190                 while (*p && (*p != '/'))
1191                         p++;
1192                 q.len = p - q.name;
1193
1194                 do {
1195                         nfs_fattr_init(fattr);
1196                         status = nfs4_handle_exception(server,
1197                                         rpc_call_sync(server->client, &msg, 0),
1198                                         &exception);
1199                 } while (exception.retry);
1200                 if (status == 0)
1201                         continue;
1202                 if (status == -ENOENT) {
1203                         printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1204                         printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1205                 }
1206                 break;
1207         }
1208         if (status == 0)
1209                 status = nfs4_server_capabilities(server, fhandle);
1210         if (status == 0)
1211                 status = nfs4_do_fsinfo(server, fhandle, info);
1212 out:
1213         return status;
1214 }
1215
1216 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1217 {
1218         struct nfs4_getattr_arg args = {
1219                 .fh = fhandle,
1220                 .bitmask = server->attr_bitmask,
1221         };
1222         struct nfs4_getattr_res res = {
1223                 .fattr = fattr,
1224                 .server = server,
1225         };
1226         struct rpc_message msg = {
1227                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1228                 .rpc_argp = &args,
1229                 .rpc_resp = &res,
1230         };
1231         
1232         nfs_fattr_init(fattr);
1233         return rpc_call_sync(server->client, &msg, 0);
1234 }
1235
1236 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1237 {
1238         struct nfs4_exception exception = { };
1239         int err;
1240         do {
1241                 err = nfs4_handle_exception(server,
1242                                 _nfs4_proc_getattr(server, fhandle, fattr),
1243                                 &exception);
1244         } while (exception.retry);
1245         return err;
1246 }
1247
1248 /* 
1249  * The file is not closed if it is opened due to the a request to change
1250  * the size of the file. The open call will not be needed once the
1251  * VFS layer lookup-intents are implemented.
1252  *
1253  * Close is called when the inode is destroyed.
1254  * If we haven't opened the file for O_WRONLY, we
1255  * need to in the size_change case to obtain a stateid.
1256  *
1257  * Got race?
1258  * Because OPEN is always done by name in nfsv4, it is
1259  * possible that we opened a different file by the same
1260  * name.  We can recognize this race condition, but we
1261  * can't do anything about it besides returning an error.
1262  *
1263  * This will be fixed with VFS changes (lookup-intent).
1264  */
1265 static int
1266 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1267                   struct iattr *sattr)
1268 {
1269         struct rpc_cred *cred;
1270         struct inode *inode = dentry->d_inode;
1271         struct nfs4_state *state;
1272         int status;
1273
1274         nfs_fattr_init(fattr);
1275         
1276         cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1277         if (IS_ERR(cred))
1278                 return PTR_ERR(cred);
1279         /* Search for an existing WRITE delegation first */
1280         state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1281         if (!IS_ERR(state)) {
1282                 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1283                 iput(inode);
1284         } else {
1285                 /* Search for an existing open(O_WRITE) stateid */
1286                 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1287         }
1288
1289         status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1290                         NFS_FH(inode), sattr, state);
1291         if (status == 0)
1292                 nfs_setattr_update_inode(inode, sattr);
1293         if (state != NULL)
1294                 nfs4_close_state(state, FMODE_WRITE);
1295         put_rpccred(cred);
1296         return status;
1297 }
1298
1299 static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1300                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1301 {
1302         int                    status;
1303         struct nfs_server *server = NFS_SERVER(dir);
1304         struct nfs4_lookup_arg args = {
1305                 .bitmask = server->attr_bitmask,
1306                 .dir_fh = NFS_FH(dir),
1307                 .name = name,
1308         };
1309         struct nfs4_lookup_res res = {
1310                 .server = server,
1311                 .fattr = fattr,
1312                 .fh = fhandle,
1313         };
1314         struct rpc_message msg = {
1315                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1316                 .rpc_argp = &args,
1317                 .rpc_resp = &res,
1318         };
1319         
1320         nfs_fattr_init(fattr);
1321         
1322         dprintk("NFS call  lookup %s\n", name->name);
1323         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1324         dprintk("NFS reply lookup: %d\n", status);
1325         return status;
1326 }
1327
1328 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1329 {
1330         struct nfs4_exception exception = { };
1331         int err;
1332         do {
1333                 err = nfs4_handle_exception(NFS_SERVER(dir),
1334                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1335                                 &exception);
1336         } while (exception.retry);
1337         return err;
1338 }
1339
1340 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1341 {
1342         struct nfs4_accessargs args = {
1343                 .fh = NFS_FH(inode),
1344         };
1345         struct nfs4_accessres res = { 0 };
1346         struct rpc_message msg = {
1347                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1348                 .rpc_argp = &args,
1349                 .rpc_resp = &res,
1350                 .rpc_cred = entry->cred,
1351         };
1352         int mode = entry->mask;
1353         int status;
1354
1355         /*
1356          * Determine which access bits we want to ask for...
1357          */
1358         if (mode & MAY_READ)
1359                 args.access |= NFS4_ACCESS_READ;
1360         if (S_ISDIR(inode->i_mode)) {
1361                 if (mode & MAY_WRITE)
1362                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1363                 if (mode & MAY_EXEC)
1364                         args.access |= NFS4_ACCESS_LOOKUP;
1365         } else {
1366                 if (mode & MAY_WRITE)
1367                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1368                 if (mode & MAY_EXEC)
1369                         args.access |= NFS4_ACCESS_EXECUTE;
1370         }
1371         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1372         if (!status) {
1373                 entry->mask = 0;
1374                 if (res.access & NFS4_ACCESS_READ)
1375                         entry->mask |= MAY_READ;
1376                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1377                         entry->mask |= MAY_WRITE;
1378                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1379                         entry->mask |= MAY_EXEC;
1380         }
1381         return status;
1382 }
1383
1384 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1385 {
1386         struct nfs4_exception exception = { };
1387         int err;
1388         do {
1389                 err = nfs4_handle_exception(NFS_SERVER(inode),
1390                                 _nfs4_proc_access(inode, entry),
1391                                 &exception);
1392         } while (exception.retry);
1393         return err;
1394 }
1395
1396 /*
1397  * TODO: For the time being, we don't try to get any attributes
1398  * along with any of the zero-copy operations READ, READDIR,
1399  * READLINK, WRITE.
1400  *
1401  * In the case of the first three, we want to put the GETATTR
1402  * after the read-type operation -- this is because it is hard
1403  * to predict the length of a GETATTR response in v4, and thus
1404  * align the READ data correctly.  This means that the GETATTR
1405  * may end up partially falling into the page cache, and we should
1406  * shift it into the 'tail' of the xdr_buf before processing.
1407  * To do this efficiently, we need to know the total length
1408  * of data received, which doesn't seem to be available outside
1409  * of the RPC layer.
1410  *
1411  * In the case of WRITE, we also want to put the GETATTR after
1412  * the operation -- in this case because we want to make sure
1413  * we get the post-operation mtime and size.  This means that
1414  * we can't use xdr_encode_pages() as written: we need a variant
1415  * of it which would leave room in the 'tail' iovec.
1416  *
1417  * Both of these changes to the XDR layer would in fact be quite
1418  * minor, but I decided to leave them for a subsequent patch.
1419  */
1420 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1421                 unsigned int pgbase, unsigned int pglen)
1422 {
1423         struct nfs4_readlink args = {
1424                 .fh       = NFS_FH(inode),
1425                 .pgbase   = pgbase,
1426                 .pglen    = pglen,
1427                 .pages    = &page,
1428         };
1429         struct rpc_message msg = {
1430                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1431                 .rpc_argp = &args,
1432                 .rpc_resp = NULL,
1433         };
1434
1435         return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1436 }
1437
1438 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1439                 unsigned int pgbase, unsigned int pglen)
1440 {
1441         struct nfs4_exception exception = { };
1442         int err;
1443         do {
1444                 err = nfs4_handle_exception(NFS_SERVER(inode),
1445                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1446                                 &exception);
1447         } while (exception.retry);
1448         return err;
1449 }
1450
1451 static int _nfs4_proc_read(struct nfs_read_data *rdata)
1452 {
1453         int flags = rdata->flags;
1454         struct inode *inode = rdata->inode;
1455         struct nfs_fattr *fattr = rdata->res.fattr;
1456         struct nfs_server *server = NFS_SERVER(inode);
1457         struct rpc_message msg = {
1458                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_READ],
1459                 .rpc_argp       = &rdata->args,
1460                 .rpc_resp       = &rdata->res,
1461                 .rpc_cred       = rdata->cred,
1462         };
1463         unsigned long timestamp = jiffies;
1464         int status;
1465
1466         dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
1467                         (long long) rdata->args.offset);
1468
1469         nfs_fattr_init(fattr);
1470         status = rpc_call_sync(server->client, &msg, flags);
1471         if (!status)
1472                 renew_lease(server, timestamp);
1473         dprintk("NFS reply read: %d\n", status);
1474         return status;
1475 }
1476
1477 static int nfs4_proc_read(struct nfs_read_data *rdata)
1478 {
1479         struct nfs4_exception exception = { };
1480         int err;
1481         do {
1482                 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1483                                 _nfs4_proc_read(rdata),
1484                                 &exception);
1485         } while (exception.retry);
1486         return err;
1487 }
1488
1489 static int _nfs4_proc_write(struct nfs_write_data *wdata)
1490 {
1491         int rpcflags = wdata->flags;
1492         struct inode *inode = wdata->inode;
1493         struct nfs_fattr *fattr = wdata->res.fattr;
1494         struct nfs_server *server = NFS_SERVER(inode);
1495         struct rpc_message msg = {
1496                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1497                 .rpc_argp       = &wdata->args,
1498                 .rpc_resp       = &wdata->res,
1499                 .rpc_cred       = wdata->cred,
1500         };
1501         int status;
1502
1503         dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
1504                         (long long) wdata->args.offset);
1505
1506         nfs_fattr_init(fattr);
1507         status = rpc_call_sync(server->client, &msg, rpcflags);
1508         dprintk("NFS reply write: %d\n", status);
1509         return status;
1510 }
1511
1512 static int nfs4_proc_write(struct nfs_write_data *wdata)
1513 {
1514         struct nfs4_exception exception = { };
1515         int err;
1516         do {
1517                 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1518                                 _nfs4_proc_write(wdata),
1519                                 &exception);
1520         } while (exception.retry);
1521         return err;
1522 }
1523
1524 static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1525 {
1526         struct inode *inode = cdata->inode;
1527         struct nfs_fattr *fattr = cdata->res.fattr;
1528         struct nfs_server *server = NFS_SERVER(inode);
1529         struct rpc_message msg = {
1530                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1531                 .rpc_argp       = &cdata->args,
1532                 .rpc_resp       = &cdata->res,
1533                 .rpc_cred       = cdata->cred,
1534         };
1535         int status;
1536
1537         dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
1538                         (long long) cdata->args.offset);
1539
1540         nfs_fattr_init(fattr);
1541         status = rpc_call_sync(server->client, &msg, 0);
1542         dprintk("NFS reply commit: %d\n", status);
1543         return status;
1544 }
1545
1546 static int nfs4_proc_commit(struct nfs_write_data *cdata)
1547 {
1548         struct nfs4_exception exception = { };
1549         int err;
1550         do {
1551                 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1552                                 _nfs4_proc_commit(cdata),
1553                                 &exception);
1554         } while (exception.retry);
1555         return err;
1556 }
1557
1558 /*
1559  * Got race?
1560  * We will need to arrange for the VFS layer to provide an atomic open.
1561  * Until then, this create/open method is prone to inefficiency and race
1562  * conditions due to the lookup, create, and open VFS calls from sys_open()
1563  * placed on the wire.
1564  *
1565  * Given the above sorry state of affairs, I'm simply sending an OPEN.
1566  * The file will be opened again in the subsequent VFS open call
1567  * (nfs4_proc_file_open).
1568  *
1569  * The open for read will just hang around to be used by any process that
1570  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1571  */
1572
1573 static int
1574 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1575                  int flags, struct nameidata *nd)
1576 {
1577         struct nfs4_state *state;
1578         struct rpc_cred *cred;
1579         int status = 0;
1580
1581         cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1582         if (IS_ERR(cred)) {
1583                 status = PTR_ERR(cred);
1584                 goto out;
1585         }
1586         state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1587         put_rpccred(cred);
1588         if (IS_ERR(state)) {
1589                 status = PTR_ERR(state);
1590                 goto out;
1591         }
1592         d_instantiate(dentry, state->inode);
1593         if (flags & O_EXCL) {
1594                 struct nfs_fattr fattr;
1595                 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1596                                      NFS_FH(state->inode), sattr, state);
1597                 if (status == 0)
1598                         nfs_setattr_update_inode(state->inode, sattr);
1599         }
1600         if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1601                 nfs4_intent_set_file(nd, dentry, state);
1602         else
1603                 nfs4_close_state(state, flags);
1604 out:
1605         return status;
1606 }
1607
1608 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1609 {
1610         struct nfs4_remove_arg args = {
1611                 .fh = NFS_FH(dir),
1612                 .name = name,
1613         };
1614         struct nfs4_change_info res;
1615         struct rpc_message msg = {
1616                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1617                 .rpc_argp       = &args,
1618                 .rpc_resp       = &res,
1619         };
1620         int                     status;
1621
1622         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1623         if (status == 0)
1624                 update_changeattr(dir, &res);
1625         return status;
1626 }
1627
1628 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1629 {
1630         struct nfs4_exception exception = { };
1631         int err;
1632         do {
1633                 err = nfs4_handle_exception(NFS_SERVER(dir),
1634                                 _nfs4_proc_remove(dir, name),
1635                                 &exception);
1636         } while (exception.retry);
1637         return err;
1638 }
1639
1640 struct unlink_desc {
1641         struct nfs4_remove_arg  args;
1642         struct nfs4_change_info res;
1643 };
1644
1645 static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1646                 struct qstr *name)
1647 {
1648         struct unlink_desc *up;
1649
1650         up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1651         if (!up)
1652                 return -ENOMEM;
1653         
1654         up->args.fh = NFS_FH(dir->d_inode);
1655         up->args.name = name;
1656         
1657         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1658         msg->rpc_argp = &up->args;
1659         msg->rpc_resp = &up->res;
1660         return 0;
1661 }
1662
1663 static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1664 {
1665         struct rpc_message *msg = &task->tk_msg;
1666         struct unlink_desc *up;
1667         
1668         if (msg->rpc_resp != NULL) {
1669                 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1670                 update_changeattr(dir->d_inode, &up->res);
1671                 kfree(up);
1672                 msg->rpc_resp = NULL;
1673                 msg->rpc_argp = NULL;
1674         }
1675         return 0;
1676 }
1677
1678 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1679                 struct inode *new_dir, struct qstr *new_name)
1680 {
1681         struct nfs4_rename_arg arg = {
1682                 .old_dir = NFS_FH(old_dir),
1683                 .new_dir = NFS_FH(new_dir),
1684                 .old_name = old_name,
1685                 .new_name = new_name,
1686         };
1687         struct nfs4_rename_res res = { };
1688         struct rpc_message msg = {
1689                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1690                 .rpc_argp = &arg,
1691                 .rpc_resp = &res,
1692         };
1693         int                     status;
1694         
1695         status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1696
1697         if (!status) {
1698                 update_changeattr(old_dir, &res.old_cinfo);
1699                 update_changeattr(new_dir, &res.new_cinfo);
1700         }
1701         return status;
1702 }
1703
1704 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1705                 struct inode *new_dir, struct qstr *new_name)
1706 {
1707         struct nfs4_exception exception = { };
1708         int err;
1709         do {
1710                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1711                                 _nfs4_proc_rename(old_dir, old_name,
1712                                         new_dir, new_name),
1713                                 &exception);
1714         } while (exception.retry);
1715         return err;
1716 }
1717
1718 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1719 {
1720         struct nfs4_link_arg arg = {
1721                 .fh     = NFS_FH(inode),
1722                 .dir_fh = NFS_FH(dir),
1723                 .name   = name,
1724         };
1725         struct nfs4_change_info cinfo = { };
1726         struct rpc_message msg = {
1727                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1728                 .rpc_argp = &arg,
1729                 .rpc_resp = &cinfo,
1730         };
1731         int                     status;
1732
1733         status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1734         if (!status)
1735                 update_changeattr(dir, &cinfo);
1736
1737         return status;
1738 }
1739
1740 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1741 {
1742         struct nfs4_exception exception = { };
1743         int err;
1744         do {
1745                 err = nfs4_handle_exception(NFS_SERVER(inode),
1746                                 _nfs4_proc_link(inode, dir, name),
1747                                 &exception);
1748         } while (exception.retry);
1749         return err;
1750 }
1751
1752 static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1753                 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1754                 struct nfs_fattr *fattr)
1755 {
1756         struct nfs_server *server = NFS_SERVER(dir);
1757         struct nfs_fattr dir_fattr;
1758         struct nfs4_create_arg arg = {
1759                 .dir_fh = NFS_FH(dir),
1760                 .server = server,
1761                 .name = name,
1762                 .attrs = sattr,
1763                 .ftype = NF4LNK,
1764                 .bitmask = server->attr_bitmask,
1765         };
1766         struct nfs4_create_res res = {
1767                 .server = server,
1768                 .fh = fhandle,
1769                 .fattr = fattr,
1770                 .dir_fattr = &dir_fattr,
1771         };
1772         struct rpc_message msg = {
1773                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1774                 .rpc_argp = &arg,
1775                 .rpc_resp = &res,
1776         };
1777         int                     status;
1778
1779         if (path->len > NFS4_MAXPATHLEN)
1780                 return -ENAMETOOLONG;
1781         arg.u.symlink = path;
1782         nfs_fattr_init(fattr);
1783         nfs_fattr_init(&dir_fattr);
1784         
1785         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1786         if (!status)
1787                 update_changeattr(dir, &res.dir_cinfo);
1788         nfs_post_op_update_inode(dir, res.dir_fattr);
1789         return status;
1790 }
1791
1792 static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1793                 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1794                 struct nfs_fattr *fattr)
1795 {
1796         struct nfs4_exception exception = { };
1797         int err;
1798         do {
1799                 err = nfs4_handle_exception(NFS_SERVER(dir),
1800                                 _nfs4_proc_symlink(dir, name, path, sattr,
1801                                         fhandle, fattr),
1802                                 &exception);
1803         } while (exception.retry);
1804         return err;
1805 }
1806
1807 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1808                 struct iattr *sattr)
1809 {
1810         struct nfs_server *server = NFS_SERVER(dir);
1811         struct nfs_fh fhandle;
1812         struct nfs_fattr fattr, dir_fattr;
1813         struct nfs4_create_arg arg = {
1814                 .dir_fh = NFS_FH(dir),
1815                 .server = server,
1816                 .name = &dentry->d_name,
1817                 .attrs = sattr,
1818                 .ftype = NF4DIR,
1819                 .bitmask = server->attr_bitmask,
1820         };
1821         struct nfs4_create_res res = {
1822                 .server = server,
1823                 .fh = &fhandle,
1824                 .fattr = &fattr,
1825                 .dir_fattr = &dir_fattr,
1826         };
1827         struct rpc_message msg = {
1828                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1829                 .rpc_argp = &arg,
1830                 .rpc_resp = &res,
1831         };
1832         int                     status;
1833
1834         nfs_fattr_init(&fattr);
1835         nfs_fattr_init(&dir_fattr);
1836         
1837         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1838         if (!status) {
1839                 update_changeattr(dir, &res.dir_cinfo);
1840                 nfs_post_op_update_inode(dir, res.dir_fattr);
1841                 status = nfs_instantiate(dentry, &fhandle, &fattr);
1842         }
1843         return status;
1844 }
1845
1846 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1847                 struct iattr *sattr)
1848 {
1849         struct nfs4_exception exception = { };
1850         int err;
1851         do {
1852                 err = nfs4_handle_exception(NFS_SERVER(dir),
1853                                 _nfs4_proc_mkdir(dir, dentry, sattr),
1854                                 &exception);
1855         } while (exception.retry);
1856         return err;
1857 }
1858
1859 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1860                   u64 cookie, struct page *page, unsigned int count, int plus)
1861 {
1862         struct inode            *dir = dentry->d_inode;
1863         struct nfs4_readdir_arg args = {
1864                 .fh = NFS_FH(dir),
1865                 .pages = &page,
1866                 .pgbase = 0,
1867                 .count = count,
1868                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1869         };
1870         struct nfs4_readdir_res res;
1871         struct rpc_message msg = {
1872                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1873                 .rpc_argp = &args,
1874                 .rpc_resp = &res,
1875                 .rpc_cred = cred,
1876         };
1877         int                     status;
1878
1879         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
1880                         dentry->d_parent->d_name.name,
1881                         dentry->d_name.name,
1882                         (unsigned long long)cookie);
1883         lock_kernel();
1884         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1885         res.pgbase = args.pgbase;
1886         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1887         if (status == 0)
1888                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1889         unlock_kernel();
1890         dprintk("%s: returns %d\n", __FUNCTION__, status);
1891         return status;
1892 }
1893
1894 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1895                   u64 cookie, struct page *page, unsigned int count, int plus)
1896 {
1897         struct nfs4_exception exception = { };
1898         int err;
1899         do {
1900                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1901                                 _nfs4_proc_readdir(dentry, cred, cookie,
1902                                         page, count, plus),
1903                                 &exception);
1904         } while (exception.retry);
1905         return err;
1906 }
1907
1908 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1909                 struct iattr *sattr, dev_t rdev)
1910 {
1911         struct nfs_server *server = NFS_SERVER(dir);
1912         struct nfs_fh fh;
1913         struct nfs_fattr fattr, dir_fattr;
1914         struct nfs4_create_arg arg = {
1915                 .dir_fh = NFS_FH(dir),
1916                 .server = server,
1917                 .name = &dentry->d_name,
1918                 .attrs = sattr,
1919                 .bitmask = server->attr_bitmask,
1920         };
1921         struct nfs4_create_res res = {
1922                 .server = server,
1923                 .fh = &fh,
1924                 .fattr = &fattr,
1925                 .dir_fattr = &dir_fattr,
1926         };
1927         struct rpc_message msg = {
1928                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1929                 .rpc_argp = &arg,
1930                 .rpc_resp = &res,
1931         };
1932         int                     status;
1933         int                     mode = sattr->ia_mode;
1934
1935         nfs_fattr_init(&fattr);
1936         nfs_fattr_init(&dir_fattr);
1937
1938         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1939         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1940         if (S_ISFIFO(mode))
1941                 arg.ftype = NF4FIFO;
1942         else if (S_ISBLK(mode)) {
1943                 arg.ftype = NF4BLK;
1944                 arg.u.device.specdata1 = MAJOR(rdev);
1945                 arg.u.device.specdata2 = MINOR(rdev);
1946         }
1947         else if (S_ISCHR(mode)) {
1948                 arg.ftype = NF4CHR;
1949                 arg.u.device.specdata1 = MAJOR(rdev);
1950                 arg.u.device.specdata2 = MINOR(rdev);
1951         }
1952         else
1953                 arg.ftype = NF4SOCK;
1954         
1955         status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1956         if (status == 0) {
1957                 update_changeattr(dir, &res.dir_cinfo);
1958                 nfs_post_op_update_inode(dir, res.dir_fattr);
1959                 status = nfs_instantiate(dentry, &fh, &fattr);
1960         }
1961         return status;
1962 }
1963
1964 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1965                 struct iattr *sattr, dev_t rdev)
1966 {
1967         struct nfs4_exception exception = { };
1968         int err;
1969         do {
1970                 err = nfs4_handle_exception(NFS_SERVER(dir),
1971                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1972                                 &exception);
1973         } while (exception.retry);
1974         return err;
1975 }
1976
1977 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1978                  struct nfs_fsstat *fsstat)
1979 {
1980         struct nfs4_statfs_arg args = {
1981                 .fh = fhandle,
1982                 .bitmask = server->attr_bitmask,
1983         };
1984         struct rpc_message msg = {
1985                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1986                 .rpc_argp = &args,
1987                 .rpc_resp = fsstat,
1988         };
1989
1990         nfs_fattr_init(fsstat->fattr);
1991         return rpc_call_sync(server->client, &msg, 0);
1992 }
1993
1994 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1995 {
1996         struct nfs4_exception exception = { };
1997         int err;
1998         do {
1999                 err = nfs4_handle_exception(server,
2000                                 _nfs4_proc_statfs(server, fhandle, fsstat),
2001                                 &exception);
2002         } while (exception.retry);
2003         return err;
2004 }
2005
2006 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2007                 struct nfs_fsinfo *fsinfo)
2008 {
2009         struct nfs4_fsinfo_arg args = {
2010                 .fh = fhandle,
2011                 .bitmask = server->attr_bitmask,
2012         };
2013         struct rpc_message msg = {
2014                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2015                 .rpc_argp = &args,
2016                 .rpc_resp = fsinfo,
2017         };
2018
2019         return rpc_call_sync(server->client, &msg, 0);
2020 }
2021
2022 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2023 {
2024         struct nfs4_exception exception = { };
2025         int err;
2026
2027         do {
2028                 err = nfs4_handle_exception(server,
2029                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2030                                 &exception);
2031         } while (exception.retry);
2032         return err;
2033 }
2034
2035 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2036 {
2037         nfs_fattr_init(fsinfo->fattr);
2038         return nfs4_do_fsinfo(server, fhandle, fsinfo);
2039 }
2040
2041 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2042                 struct nfs_pathconf *pathconf)
2043 {
2044         struct nfs4_pathconf_arg args = {
2045                 .fh = fhandle,
2046                 .bitmask = server->attr_bitmask,
2047         };
2048         struct rpc_message msg = {
2049                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2050                 .rpc_argp = &args,
2051                 .rpc_resp = pathconf,
2052         };
2053
2054         /* None of the pathconf attributes are mandatory to implement */
2055         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2056                 memset(pathconf, 0, sizeof(*pathconf));
2057                 return 0;
2058         }
2059
2060         nfs_fattr_init(pathconf->fattr);
2061         return rpc_call_sync(server->client, &msg, 0);
2062 }
2063
2064 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2065                 struct nfs_pathconf *pathconf)
2066 {
2067         struct nfs4_exception exception = { };
2068         int err;
2069
2070         do {
2071                 err = nfs4_handle_exception(server,
2072                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
2073                                 &exception);
2074         } while (exception.retry);
2075         return err;
2076 }
2077
2078 static void
2079 nfs4_read_done(struct rpc_task *task)
2080 {
2081         struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
2082         struct inode *inode = data->inode;
2083
2084         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2085                 rpc_restart_call(task);
2086                 return;
2087         }
2088         if (task->tk_status > 0)
2089                 renew_lease(NFS_SERVER(inode), data->timestamp);
2090         /* Call back common NFS readpage processing */
2091         nfs_readpage_result(task);
2092 }
2093
2094 static void
2095 nfs4_proc_read_setup(struct nfs_read_data *data)
2096 {
2097         struct rpc_task *task = &data->task;
2098         struct rpc_message msg = {
2099                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2100                 .rpc_argp = &data->args,
2101                 .rpc_resp = &data->res,
2102                 .rpc_cred = data->cred,
2103         };
2104         struct inode *inode = data->inode;
2105         int flags;
2106
2107         data->timestamp   = jiffies;
2108
2109         /* N.B. Do we need to test? Never called for swapfile inode */
2110         flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2111
2112         /* Finalize the task. */
2113         rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
2114         rpc_call_setup(task, &msg, 0);
2115 }
2116
2117 static void
2118 nfs4_write_done(struct rpc_task *task)
2119 {
2120         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2121         struct inode *inode = data->inode;
2122         
2123         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2124                 rpc_restart_call(task);
2125                 return;
2126         }
2127         if (task->tk_status >= 0)
2128                 renew_lease(NFS_SERVER(inode), data->timestamp);
2129         /* Call back common NFS writeback processing */
2130         nfs_writeback_done(task);
2131 }
2132
2133 static void
2134 nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2135 {
2136         struct rpc_task *task = &data->task;
2137         struct rpc_message msg = {
2138                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2139                 .rpc_argp = &data->args,
2140                 .rpc_resp = &data->res,
2141                 .rpc_cred = data->cred,
2142         };
2143         struct inode *inode = data->inode;
2144         int stable;
2145         int flags;
2146         
2147         if (how & FLUSH_STABLE) {
2148                 if (!NFS_I(inode)->ncommit)
2149                         stable = NFS_FILE_SYNC;
2150                 else
2151                         stable = NFS_DATA_SYNC;
2152         } else
2153                 stable = NFS_UNSTABLE;
2154         data->args.stable = stable;
2155
2156         data->timestamp   = jiffies;
2157
2158         /* Set the initial flags for the task.  */
2159         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2160
2161         /* Finalize the task. */
2162         rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2163         rpc_call_setup(task, &msg, 0);
2164 }
2165
2166 static void
2167 nfs4_commit_done(struct rpc_task *task)
2168 {
2169         struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2170         struct inode *inode = data->inode;
2171         
2172         if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2173                 rpc_restart_call(task);
2174                 return;
2175         }
2176         /* Call back common NFS writeback processing */
2177         nfs_commit_done(task);
2178 }
2179
2180 static void
2181 nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2182 {
2183         struct rpc_task *task = &data->task;
2184         struct rpc_message msg = {
2185                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2186                 .rpc_argp = &data->args,
2187                 .rpc_resp = &data->res,
2188                 .rpc_cred = data->cred,
2189         };      
2190         struct inode *inode = data->inode;
2191         int flags;
2192         
2193         /* Set the initial flags for the task.  */
2194         flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2195
2196         /* Finalize the task. */
2197         rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2198         rpc_call_setup(task, &msg, 0);  
2199 }
2200
2201 /*
2202  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2203  * standalone procedure for queueing an asynchronous RENEW.
2204  */
2205 static void
2206 renew_done(struct rpc_task *task)
2207 {
2208         struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2209         unsigned long timestamp = (unsigned long)task->tk_calldata;
2210
2211         if (task->tk_status < 0) {
2212                 switch (task->tk_status) {
2213                         case -NFS4ERR_STALE_CLIENTID:
2214                         case -NFS4ERR_EXPIRED:
2215                         case -NFS4ERR_CB_PATH_DOWN:
2216                                 nfs4_schedule_state_recovery(clp);
2217                 }
2218                 return;
2219         }
2220         spin_lock(&clp->cl_lock);
2221         if (time_before(clp->cl_last_renewal,timestamp))
2222                 clp->cl_last_renewal = timestamp;
2223         spin_unlock(&clp->cl_lock);
2224 }
2225
2226 int
2227 nfs4_proc_async_renew(struct nfs4_client *clp)
2228 {
2229         struct rpc_message msg = {
2230                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2231                 .rpc_argp       = clp,
2232                 .rpc_cred       = clp->cl_cred,
2233         };
2234
2235         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2236                         renew_done, (void *)jiffies);
2237 }
2238
2239 int
2240 nfs4_proc_renew(struct nfs4_client *clp)
2241 {
2242         struct rpc_message msg = {
2243                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2244                 .rpc_argp       = clp,
2245                 .rpc_cred       = clp->cl_cred,
2246         };
2247         unsigned long now = jiffies;
2248         int status;
2249
2250         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2251         if (status < 0)
2252                 return status;
2253         spin_lock(&clp->cl_lock);
2254         if (time_before(clp->cl_last_renewal,now))
2255                 clp->cl_last_renewal = now;
2256         spin_unlock(&clp->cl_lock);
2257         return 0;
2258 }
2259
2260 static inline int nfs4_server_supports_acls(struct nfs_server *server)
2261 {
2262         return (server->caps & NFS_CAP_ACLS)
2263                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2264                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2265 }
2266
2267 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2268  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2269  * the stack.
2270  */
2271 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2272
2273 static void buf_to_pages(const void *buf, size_t buflen,
2274                 struct page **pages, unsigned int *pgbase)
2275 {
2276         const void *p = buf;
2277
2278         *pgbase = offset_in_page(buf);
2279         p -= *pgbase;
2280         while (p < buf + buflen) {
2281                 *(pages++) = virt_to_page(p);
2282                 p += PAGE_CACHE_SIZE;
2283         }
2284 }
2285
2286 struct nfs4_cached_acl {
2287         int cached;
2288         size_t len;
2289         char data[0];
2290 };
2291
2292 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2293 {
2294         struct nfs_inode *nfsi = NFS_I(inode);
2295
2296         spin_lock(&inode->i_lock);
2297         kfree(nfsi->nfs4_acl);
2298         nfsi->nfs4_acl = acl;
2299         spin_unlock(&inode->i_lock);
2300 }
2301
2302 static void nfs4_zap_acl_attr(struct inode *inode)
2303 {
2304         nfs4_set_cached_acl(inode, NULL);
2305 }
2306
2307 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2308 {
2309         struct nfs_inode *nfsi = NFS_I(inode);
2310         struct nfs4_cached_acl *acl;
2311         int ret = -ENOENT;
2312
2313         spin_lock(&inode->i_lock);
2314         acl = nfsi->nfs4_acl;
2315         if (acl == NULL)
2316                 goto out;
2317         if (buf == NULL) /* user is just asking for length */
2318                 goto out_len;
2319         if (acl->cached == 0)
2320                 goto out;
2321         ret = -ERANGE; /* see getxattr(2) man page */
2322         if (acl->len > buflen)
2323                 goto out;
2324         memcpy(buf, acl->data, acl->len);
2325 out_len:
2326         ret = acl->len;
2327 out:
2328         spin_unlock(&inode->i_lock);
2329         return ret;
2330 }
2331
2332 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2333 {
2334         struct nfs4_cached_acl *acl;
2335
2336         if (buf && acl_len <= PAGE_SIZE) {
2337                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2338                 if (acl == NULL)
2339                         goto out;
2340                 acl->cached = 1;
2341                 memcpy(acl->data, buf, acl_len);
2342         } else {
2343                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2344                 if (acl == NULL)
2345                         goto out;
2346                 acl->cached = 0;
2347         }
2348         acl->len = acl_len;
2349 out:
2350         nfs4_set_cached_acl(inode, acl);
2351 }
2352
2353 static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2354 {
2355         struct page *pages[NFS4ACL_MAXPAGES];
2356         struct nfs_getaclargs args = {
2357                 .fh = NFS_FH(inode),
2358                 .acl_pages = pages,
2359                 .acl_len = buflen,
2360         };
2361         size_t resp_len = buflen;
2362         void *resp_buf;
2363         struct rpc_message msg = {
2364                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2365                 .rpc_argp = &args,
2366                 .rpc_resp = &resp_len,
2367         };
2368         struct page *localpage = NULL;
2369         int ret;
2370
2371         if (buflen < PAGE_SIZE) {
2372                 /* As long as we're doing a round trip to the server anyway,
2373                  * let's be prepared for a page of acl data. */
2374                 localpage = alloc_page(GFP_KERNEL);
2375                 resp_buf = page_address(localpage);
2376                 if (localpage == NULL)
2377                         return -ENOMEM;
2378                 args.acl_pages[0] = localpage;
2379                 args.acl_pgbase = 0;
2380                 resp_len = args.acl_len = PAGE_SIZE;
2381         } else {
2382                 resp_buf = buf;
2383                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2384         }
2385         ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2386         if (ret)
2387                 goto out_free;
2388         if (resp_len > args.acl_len)
2389                 nfs4_write_cached_acl(inode, NULL, resp_len);
2390         else
2391                 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2392         if (buf) {
2393                 ret = -ERANGE;
2394                 if (resp_len > buflen)
2395                         goto out_free;
2396                 if (localpage)
2397                         memcpy(buf, resp_buf, resp_len);
2398         }
2399         ret = resp_len;
2400 out_free:
2401         if (localpage)
2402                 __free_page(localpage);
2403         return ret;
2404 }
2405
2406 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2407 {
2408         struct nfs_server *server = NFS_SERVER(inode);
2409         int ret;
2410
2411         if (!nfs4_server_supports_acls(server))
2412                 return -EOPNOTSUPP;
2413         ret = nfs_revalidate_inode(server, inode);
2414         if (ret < 0)
2415                 return ret;
2416         ret = nfs4_read_cached_acl(inode, buf, buflen);
2417         if (ret != -ENOENT)
2418                 return ret;
2419         return nfs4_get_acl_uncached(inode, buf, buflen);
2420 }
2421
2422 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2423 {
2424         struct nfs_server *server = NFS_SERVER(inode);
2425         struct page *pages[NFS4ACL_MAXPAGES];
2426         struct nfs_setaclargs arg = {
2427                 .fh             = NFS_FH(inode),
2428                 .acl_pages      = pages,
2429                 .acl_len        = buflen,
2430         };
2431         struct rpc_message msg = {
2432                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2433                 .rpc_argp       = &arg,
2434                 .rpc_resp       = NULL,
2435         };
2436         int ret;
2437
2438         if (!nfs4_server_supports_acls(server))
2439                 return -EOPNOTSUPP;
2440         nfs_inode_return_delegation(inode);
2441         buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2442         ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
2443         if (ret == 0)
2444                 nfs4_write_cached_acl(inode, buf, buflen);
2445         return ret;
2446 }
2447
2448 static int
2449 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
2450 {
2451         struct nfs4_client *clp = server->nfs4_state;
2452
2453         if (!clp || task->tk_status >= 0)
2454                 return 0;
2455         switch(task->tk_status) {
2456                 case -NFS4ERR_STALE_CLIENTID:
2457                 case -NFS4ERR_STALE_STATEID:
2458                 case -NFS4ERR_EXPIRED:
2459                         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2460                         nfs4_schedule_state_recovery(clp);
2461                         if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2462                                 rpc_wake_up_task(task);
2463                         task->tk_status = 0;
2464                         return -EAGAIN;
2465                 case -NFS4ERR_GRACE:
2466                 case -NFS4ERR_DELAY:
2467                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
2468                         task->tk_status = 0;
2469                         return -EAGAIN;
2470                 case -NFS4ERR_OLD_STATEID:
2471                         task->tk_status = 0;
2472                         return -EAGAIN;
2473         }
2474         task->tk_status = nfs4_map_errors(task->tk_status);
2475         return 0;
2476 }
2477
2478 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2479 {
2480         DEFINE_WAIT(wait);
2481         sigset_t oldset;
2482         int interruptible, res = 0;
2483
2484         might_sleep();
2485
2486         rpc_clnt_sigmask(clnt, &oldset);
2487         interruptible = TASK_UNINTERRUPTIBLE;
2488         if (clnt->cl_intr)
2489                 interruptible = TASK_INTERRUPTIBLE;
2490         prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2491         nfs4_schedule_state_recovery(clp);
2492         if (clnt->cl_intr && signalled())
2493                 res = -ERESTARTSYS;
2494         else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2495                 schedule();
2496         finish_wait(&clp->cl_waitq, &wait);
2497         rpc_clnt_sigunmask(clnt, &oldset);
2498         return res;
2499 }
2500
2501 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2502 {
2503         sigset_t oldset;
2504         int res = 0;
2505
2506         might_sleep();
2507
2508         if (*timeout <= 0)
2509                 *timeout = NFS4_POLL_RETRY_MIN;
2510         if (*timeout > NFS4_POLL_RETRY_MAX)
2511                 *timeout = NFS4_POLL_RETRY_MAX;
2512         rpc_clnt_sigmask(clnt, &oldset);
2513         if (clnt->cl_intr) {
2514                 schedule_timeout_interruptible(*timeout);
2515                 if (signalled())
2516                         res = -ERESTARTSYS;
2517         } else
2518                 schedule_timeout_uninterruptible(*timeout);
2519         rpc_clnt_sigunmask(clnt, &oldset);
2520         *timeout <<= 1;
2521         return res;
2522 }
2523
2524 /* This is the error handling routine for processes that are allowed
2525  * to sleep.
2526  */
2527 int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2528 {
2529         struct nfs4_client *clp = server->nfs4_state;
2530         int ret = errorcode;
2531
2532         exception->retry = 0;
2533         switch(errorcode) {
2534                 case 0:
2535                         return 0;
2536                 case -NFS4ERR_STALE_CLIENTID:
2537                 case -NFS4ERR_STALE_STATEID:
2538                 case -NFS4ERR_EXPIRED:
2539                         ret = nfs4_wait_clnt_recover(server->client, clp);
2540                         if (ret == 0)
2541                                 exception->retry = 1;
2542                         break;
2543                 case -NFS4ERR_GRACE:
2544                 case -NFS4ERR_DELAY:
2545                         ret = nfs4_delay(server->client, &exception->timeout);
2546                         if (ret == 0)
2547                                 exception->retry = 1;
2548                         break;
2549                 case -NFS4ERR_OLD_STATEID:
2550                         if (ret == 0)
2551                                 exception->retry = 1;
2552         }
2553         /* We failed to handle the error */
2554         return nfs4_map_errors(ret);
2555 }
2556
2557 int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2558 {
2559         nfs4_verifier sc_verifier;
2560         struct nfs4_setclientid setclientid = {
2561                 .sc_verifier = &sc_verifier,
2562                 .sc_prog = program,
2563         };
2564         struct rpc_message msg = {
2565                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2566                 .rpc_argp = &setclientid,
2567                 .rpc_resp = clp,
2568                 .rpc_cred = clp->cl_cred,
2569         };
2570         u32 *p;
2571         int loop = 0;
2572         int status;
2573
2574         p = (u32*)sc_verifier.data;
2575         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2576         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2577
2578         for(;;) {
2579                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2580                                 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2581                                 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2582                                 clp->cl_cred->cr_ops->cr_name,
2583                                 clp->cl_id_uniquifier);
2584                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2585                                 sizeof(setclientid.sc_netid), "tcp");
2586                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2587                                 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2588                                 clp->cl_ipaddr, port >> 8, port & 255);
2589
2590                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2591                 if (status != -NFS4ERR_CLID_INUSE)
2592                         break;
2593                 if (signalled())
2594                         break;
2595                 if (loop++ & 1)
2596                         ssleep(clp->cl_lease_time + 1);
2597                 else
2598                         if (++clp->cl_id_uniquifier == 0)
2599                                 break;
2600         }
2601         return status;
2602 }
2603
2604 int
2605 nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2606 {
2607         struct nfs_fsinfo fsinfo;
2608         struct rpc_message msg = {
2609                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2610                 .rpc_argp = clp,
2611                 .rpc_resp = &fsinfo,
2612                 .rpc_cred = clp->cl_cred,
2613         };
2614         unsigned long now;
2615         int status;
2616
2617         now = jiffies;
2618         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2619         if (status == 0) {
2620                 spin_lock(&clp->cl_lock);
2621                 clp->cl_lease_time = fsinfo.lease_time * HZ;
2622                 clp->cl_last_renewal = now;
2623                 spin_unlock(&clp->cl_lock);
2624         }
2625         return status;
2626 }
2627
2628 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2629 {
2630         struct nfs4_delegreturnargs args = {
2631                 .fhandle = NFS_FH(inode),
2632                 .stateid = stateid,
2633         };
2634         struct rpc_message msg = {
2635                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2636                 .rpc_argp = &args,
2637                 .rpc_cred = cred,
2638         };
2639
2640         return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2641 }
2642
2643 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2644 {
2645         struct nfs_server *server = NFS_SERVER(inode);
2646         struct nfs4_exception exception = { };
2647         int err;
2648         do {
2649                 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2650                 switch (err) {
2651                         case -NFS4ERR_STALE_STATEID:
2652                         case -NFS4ERR_EXPIRED:
2653                                 nfs4_schedule_state_recovery(server->nfs4_state);
2654                         case 0:
2655                                 return 0;
2656                 }
2657                 err = nfs4_handle_exception(server, err, &exception);
2658         } while (exception.retry);
2659         return err;
2660 }
2661
2662 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2663 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2664
2665 /* 
2666  * sleep, with exponential backoff, and retry the LOCK operation. 
2667  */
2668 static unsigned long
2669 nfs4_set_lock_task_retry(unsigned long timeout)
2670 {
2671         schedule_timeout_interruptible(timeout);
2672         timeout <<= 1;
2673         if (timeout > NFS4_LOCK_MAXTIMEOUT)
2674                 return NFS4_LOCK_MAXTIMEOUT;
2675         return timeout;
2676 }
2677
2678 static inline int
2679 nfs4_lck_type(int cmd, struct file_lock *request)
2680 {
2681         /* set lock type */
2682         switch (request->fl_type) {
2683                 case F_RDLCK:
2684                         return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2685                 case F_WRLCK:
2686                         return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2687                 case F_UNLCK:
2688                         return NFS4_WRITE_LT; 
2689         }
2690         BUG();
2691         return 0;
2692 }
2693
2694 static inline uint64_t
2695 nfs4_lck_length(struct file_lock *request)
2696 {
2697         if (request->fl_end == OFFSET_MAX)
2698                 return ~(uint64_t)0;
2699         return request->fl_end - request->fl_start + 1;
2700 }
2701
2702 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2703 {
2704         struct inode *inode = state->inode;
2705         struct nfs_server *server = NFS_SERVER(inode);
2706         struct nfs4_client *clp = server->nfs4_state;
2707         struct nfs_lockargs arg = {
2708                 .fh = NFS_FH(inode),
2709                 .type = nfs4_lck_type(cmd, request),
2710                 .offset = request->fl_start,
2711                 .length = nfs4_lck_length(request),
2712         };
2713         struct nfs_lockres res = {
2714                 .server = server,
2715         };
2716         struct rpc_message msg = {
2717                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2718                 .rpc_argp       = &arg,
2719                 .rpc_resp       = &res,
2720                 .rpc_cred       = state->owner->so_cred,
2721         };
2722         struct nfs_lowner nlo;
2723         struct nfs4_lock_state *lsp;
2724         int status;
2725
2726         down_read(&clp->cl_sem);
2727         nlo.clientid = clp->cl_clientid;
2728         status = nfs4_set_lock_state(state, request);
2729         if (status != 0)
2730                 goto out;
2731         lsp = request->fl_u.nfs4_fl.owner;
2732         nlo.id = lsp->ls_id; 
2733         arg.u.lockt = &nlo;
2734         status = rpc_call_sync(server->client, &msg, 0);
2735         if (!status) {
2736                 request->fl_type = F_UNLCK;
2737         } else if (status == -NFS4ERR_DENIED) {
2738                 int64_t len, start, end;
2739                 start = res.u.denied.offset;
2740                 len = res.u.denied.length;
2741                 end = start + len - 1;
2742                 if (end < 0 || len == 0)
2743                         request->fl_end = OFFSET_MAX;
2744                 else
2745                         request->fl_end = (loff_t)end;
2746                 request->fl_start = (loff_t)start;
2747                 request->fl_type = F_WRLCK;
2748                 if (res.u.denied.type & 1)
2749                         request->fl_type = F_RDLCK;
2750                 request->fl_pid = 0;
2751                 status = 0;
2752         }
2753 out:
2754         up_read(&clp->cl_sem);
2755         return status;
2756 }
2757
2758 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2759 {
2760         struct nfs4_exception exception = { };
2761         int err;
2762
2763         do {
2764                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2765                                 _nfs4_proc_getlk(state, cmd, request),
2766                                 &exception);
2767         } while (exception.retry);
2768         return err;
2769 }
2770
2771 static int do_vfs_lock(struct file *file, struct file_lock *fl)
2772 {
2773         int res = 0;
2774         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2775                 case FL_POSIX:
2776                         res = posix_lock_file_wait(file, fl);
2777                         break;
2778                 case FL_FLOCK:
2779                         res = flock_lock_file_wait(file, fl);
2780                         break;
2781                 default:
2782                         BUG();
2783         }
2784         if (res < 0)
2785                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2786                                 __FUNCTION__);
2787         return res;
2788 }
2789
2790 struct nfs4_unlockdata {
2791         struct nfs_lockargs arg;
2792         struct nfs_locku_opargs luargs;
2793         struct nfs_lockres res;
2794         struct nfs4_lock_state *lsp;
2795         struct nfs_open_context *ctx;
2796         atomic_t refcount;
2797         struct completion completion;
2798 };
2799
2800 static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
2801 {
2802         if (atomic_dec_and_test(&calldata->refcount)) {
2803                 nfs_free_seqid(calldata->luargs.seqid);
2804                 nfs4_put_lock_state(calldata->lsp);
2805                 put_nfs_open_context(calldata->ctx);
2806                 kfree(calldata);
2807         }
2808 }
2809
2810 static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
2811 {
2812         complete(&calldata->completion);
2813         nfs4_locku_release_calldata(calldata);
2814 }
2815
2816 static void nfs4_locku_done(struct rpc_task *task)
2817 {
2818         struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2819
2820         nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2821         switch (task->tk_status) {
2822                 case 0:
2823                         memcpy(calldata->lsp->ls_stateid.data,
2824                                         calldata->res.u.stateid.data,
2825                                         sizeof(calldata->lsp->ls_stateid.data));
2826                         break;
2827                 case -NFS4ERR_STALE_STATEID:
2828                 case -NFS4ERR_EXPIRED:
2829                         nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2830                         break;
2831                 default:
2832                         if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2833                                 rpc_restart_call(task);
2834                                 return;
2835                         }
2836         }
2837         nfs4_locku_complete(calldata);
2838 }
2839
2840 static void nfs4_locku_begin(struct rpc_task *task)
2841 {
2842         struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
2843         struct rpc_message msg = {
2844                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2845                 .rpc_argp       = &calldata->arg,
2846                 .rpc_resp       = &calldata->res,
2847                 .rpc_cred       = calldata->lsp->ls_state->owner->so_cred,
2848         };
2849         int status;
2850
2851         status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
2852         if (status != 0)
2853                 return;
2854         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
2855                 nfs4_locku_complete(calldata);
2856                 task->tk_exit = NULL;
2857                 rpc_exit(task, 0);
2858                 return;
2859         }
2860         rpc_call_setup(task, &msg, 0);
2861 }
2862
2863 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2864 {
2865         struct nfs4_unlockdata *calldata;
2866         struct inode *inode = state->inode;
2867         struct nfs_server *server = NFS_SERVER(inode);
2868         struct nfs4_lock_state *lsp;
2869         int status;
2870
2871         status = nfs4_set_lock_state(state, request);
2872         if (status != 0)
2873                 return status;
2874         lsp = request->fl_u.nfs4_fl.owner;
2875         /* We might have lost the locks! */
2876         if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
2877                 return 0;
2878         calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
2879         if (calldata == NULL)
2880                 return -ENOMEM;
2881         calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2882         if (calldata->luargs.seqid == NULL) {
2883                 kfree(calldata);
2884                 return -ENOMEM;
2885         }
2886         calldata->luargs.stateid = &lsp->ls_stateid;
2887         calldata->arg.fh = NFS_FH(inode);
2888         calldata->arg.type = nfs4_lck_type(cmd, request);
2889         calldata->arg.offset = request->fl_start;
2890         calldata->arg.length = nfs4_lck_length(request);
2891         calldata->arg.u.locku = &calldata->luargs;
2892         calldata->res.server = server;
2893         calldata->lsp = lsp;
2894         atomic_inc(&lsp->ls_count);
2895
2896         /* Ensure we don't close file until we're done freeing locks! */
2897         calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
2898
2899         atomic_set(&calldata->refcount, 2);
2900         init_completion(&calldata->completion);
2901
2902         status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
2903                         nfs4_locku_done, calldata);
2904         if (status == 0)
2905                 wait_for_completion_interruptible(&calldata->completion);
2906         do_vfs_lock(request->fl_file, request);
2907         nfs4_locku_release_calldata(calldata);
2908         return status;
2909 }
2910
2911 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2912 {
2913         struct inode *inode = state->inode;
2914         struct nfs_server *server = NFS_SERVER(inode);
2915         struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
2916         struct nfs_lock_opargs largs = {
2917                 .lock_stateid = &lsp->ls_stateid,
2918                 .open_stateid = &state->stateid,
2919                 .lock_owner = {
2920                         .clientid = server->nfs4_state->cl_clientid,
2921                         .id = lsp->ls_id,
2922                 },
2923                 .reclaim = reclaim,
2924         };
2925         struct nfs_lockargs arg = {
2926                 .fh = NFS_FH(inode),
2927                 .type = nfs4_lck_type(cmd, request),
2928                 .offset = request->fl_start,
2929                 .length = nfs4_lck_length(request),
2930                 .u = {
2931                         .lock = &largs,
2932                 },
2933         };
2934         struct nfs_lockres res = {
2935                 .server = server,
2936         };
2937         struct rpc_message msg = {
2938                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2939                 .rpc_argp       = &arg,
2940                 .rpc_resp       = &res,
2941                 .rpc_cred       = state->owner->so_cred,
2942         };
2943         int status = -ENOMEM;
2944
2945         largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
2946         if (largs.lock_seqid == NULL)
2947                 return -ENOMEM;
2948         if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
2949                 struct nfs4_state_owner *owner = state->owner;
2950
2951                 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
2952                 if (largs.open_seqid == NULL)
2953                         goto out;
2954                 largs.new_lock_owner = 1;
2955                 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2956                 /* increment open seqid on success, and seqid mutating errors */
2957                 if (largs.new_lock_owner != 0) {
2958                         nfs_increment_open_seqid(status, largs.open_seqid);
2959                         if (status == 0)
2960                                 nfs_confirm_seqid(&lsp->ls_seqid, 0);
2961                 }
2962                 nfs_free_seqid(largs.open_seqid);
2963         } else
2964                 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2965         /* increment lock seqid on success, and seqid mutating errors*/
2966         nfs_increment_lock_seqid(status, largs.lock_seqid);
2967         /* save the returned stateid. */
2968         if (status == 0) {
2969                 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
2970                                 sizeof(lsp->ls_stateid.data));
2971                 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2972         } else if (status == -NFS4ERR_DENIED)
2973                 status = -EAGAIN;
2974 out:
2975         nfs_free_seqid(largs.lock_seqid);
2976         return status;
2977 }
2978
2979 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2980 {
2981         struct nfs_server *server = NFS_SERVER(state->inode);
2982         struct nfs4_exception exception = { };
2983         int err;
2984
2985         do {
2986                 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2987                 if (err != -NFS4ERR_DELAY)
2988                         break;
2989                 nfs4_handle_exception(server, err, &exception);
2990         } while (exception.retry);
2991         return err;
2992 }
2993
2994 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2995 {
2996         struct nfs_server *server = NFS_SERVER(state->inode);
2997         struct nfs4_exception exception = { };
2998         int err;
2999
3000         do {
3001                 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3002                 if (err != -NFS4ERR_DELAY)
3003                         break;
3004                 nfs4_handle_exception(server, err, &exception);
3005         } while (exception.retry);
3006         return err;
3007 }
3008
3009 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3010 {
3011         struct nfs4_client *clp = state->owner->so_client;
3012         int status;
3013
3014         down_read(&clp->cl_sem);
3015         status = nfs4_set_lock_state(state, request);
3016         if (status == 0)
3017                 status = _nfs4_do_setlk(state, cmd, request, 0);
3018         if (status == 0) {
3019                 /* Note: we always want to sleep here! */
3020                 request->fl_flags |= FL_SLEEP;
3021                 if (do_vfs_lock(request->fl_file, request) < 0)
3022                         printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3023         }
3024         up_read(&clp->cl_sem);
3025         return status;
3026 }
3027
3028 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3029 {
3030         struct nfs4_exception exception = { };
3031         int err;
3032
3033         do {
3034                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3035                                 _nfs4_proc_setlk(state, cmd, request),
3036                                 &exception);
3037         } while (exception.retry);
3038         return err;
3039 }
3040
3041 static int
3042 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3043 {
3044         struct nfs_open_context *ctx;
3045         struct nfs4_state *state;
3046         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3047         int status;
3048
3049         /* verify open state */
3050         ctx = (struct nfs_open_context *)filp->private_data;
3051         state = ctx->state;
3052
3053         if (request->fl_start < 0 || request->fl_end < 0)
3054                 return -EINVAL;
3055
3056         if (IS_GETLK(cmd))
3057                 return nfs4_proc_getlk(state, F_GETLK, request);
3058
3059         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3060                 return -EINVAL;
3061
3062         if (request->fl_type == F_UNLCK)
3063                 return nfs4_proc_unlck(state, cmd, request);
3064
3065         do {
3066                 status = nfs4_proc_setlk(state, cmd, request);
3067                 if ((status != -EAGAIN) || IS_SETLK(cmd))
3068                         break;
3069                 timeout = nfs4_set_lock_task_retry(timeout);
3070                 status = -ERESTARTSYS;
3071                 if (signalled())
3072                         break;
3073         } while(status < 0);
3074         return status;
3075 }
3076
3077
3078 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3079
3080 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3081                 size_t buflen, int flags)
3082 {
3083         struct inode *inode = dentry->d_inode;
3084
3085         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3086                 return -EOPNOTSUPP;
3087
3088         if (!S_ISREG(inode->i_mode) &&
3089             (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3090                 return -EPERM;
3091
3092         return nfs4_proc_set_acl(inode, buf, buflen);
3093 }
3094
3095 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
3096  * and that's what we'll do for e.g. user attributes that haven't been set.
3097  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3098  * attributes in kernel-managed attribute namespaces. */
3099 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3100                 size_t buflen)
3101 {
3102         struct inode *inode = dentry->d_inode;
3103
3104         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3105                 return -EOPNOTSUPP;
3106
3107         return nfs4_proc_get_acl(inode, buf, buflen);
3108 }
3109
3110 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3111 {
3112         size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
3113
3114         if (buf && buflen < len)
3115                 return -ERANGE;
3116         if (buf)
3117                 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3118         return len;
3119 }
3120
3121 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3122         .recover_open   = nfs4_open_reclaim,
3123         .recover_lock   = nfs4_lock_reclaim,
3124 };
3125
3126 struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3127         .recover_open   = nfs4_open_expired,
3128         .recover_lock   = nfs4_lock_expired,
3129 };
3130
3131 static struct inode_operations nfs4_file_inode_operations = {
3132         .permission     = nfs_permission,
3133         .getattr        = nfs_getattr,
3134         .setattr        = nfs_setattr,
3135         .getxattr       = nfs4_getxattr,
3136         .setxattr       = nfs4_setxattr,
3137         .listxattr      = nfs4_listxattr,
3138 };
3139
3140 struct nfs_rpc_ops      nfs_v4_clientops = {
3141         .version        = 4,                    /* protocol version */
3142         .dentry_ops     = &nfs4_dentry_operations,
3143         .dir_inode_ops  = &nfs4_dir_inode_operations,
3144         .file_inode_ops = &nfs4_file_inode_operations,
3145         .getroot        = nfs4_proc_get_root,
3146         .getattr        = nfs4_proc_getattr,
3147         .setattr        = nfs4_proc_setattr,
3148         .lookup         = nfs4_proc_lookup,
3149         .access         = nfs4_proc_access,
3150         .readlink       = nfs4_proc_readlink,
3151         .read           = nfs4_proc_read,
3152         .write          = nfs4_proc_write,
3153         .commit         = nfs4_proc_commit,
3154         .create         = nfs4_proc_create,
3155         .remove         = nfs4_proc_remove,
3156         .unlink_setup   = nfs4_proc_unlink_setup,
3157         .unlink_done    = nfs4_proc_unlink_done,
3158         .rename         = nfs4_proc_rename,
3159         .link           = nfs4_proc_link,
3160         .symlink        = nfs4_proc_symlink,
3161         .mkdir          = nfs4_proc_mkdir,
3162         .rmdir          = nfs4_proc_remove,
3163         .readdir        = nfs4_proc_readdir,
3164         .mknod          = nfs4_proc_mknod,
3165         .statfs         = nfs4_proc_statfs,
3166         .fsinfo         = nfs4_proc_fsinfo,
3167         .pathconf       = nfs4_proc_pathconf,
3168         .decode_dirent  = nfs4_decode_dirent,
3169         .read_setup     = nfs4_proc_read_setup,
3170         .write_setup    = nfs4_proc_write_setup,
3171         .commit_setup   = nfs4_proc_commit_setup,
3172         .file_open      = nfs_open,
3173         .file_release   = nfs_release,
3174         .lock           = nfs4_proc_lock,
3175         .clear_acl_cache = nfs4_zap_acl_attr,
3176 };
3177
3178 /*
3179  * Local variables:
3180  *  c-basic-offset: 8
3181  * End:
3182  */