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