]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - fs/nfs/nfs4proc.c
93ef77666efc867134dedfd1428033c40d7bbf39
[~shefty/rdma-dev.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/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/sunrpc/gss_api.h>
45 #include <linux/nfs.h>
46 #include <linux/nfs4.h>
47 #include <linux/nfs_fs.h>
48 #include <linux/nfs_page.h>
49 #include <linux/nfs_mount.h>
50 #include <linux/namei.h>
51 #include <linux/mount.h>
52 #include <linux/module.h>
53 #include <linux/sunrpc/bc_xprt.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56
57 #include "nfs4_fs.h"
58 #include "delegation.h"
59 #include "internal.h"
60 #include "iostat.h"
61 #include "callback.h"
62 #include "pnfs.h"
63
64 #define NFSDBG_FACILITY         NFSDBG_PROC
65
66 #define NFS4_POLL_RETRY_MIN     (HZ/10)
67 #define NFS4_POLL_RETRY_MAX     (15*HZ)
68
69 #define NFS4_MAX_LOOP_ON_RECOVER (10)
70
71 struct nfs4_opendata;
72 static int _nfs4_proc_open(struct nfs4_opendata *data);
73 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
74 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
75 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
76 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
77                              const struct qstr *name, struct nfs_fh *fhandle,
78                              struct nfs_fattr *fattr);
79 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
80 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81                             struct nfs_fattr *fattr, struct iattr *sattr,
82                             struct nfs4_state *state);
83 #ifdef CONFIG_NFS_V4_1
84 static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
85 static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
86 #endif
87 /* Prevent leaks of NFSv4 errors into userland */
88 static int nfs4_map_errors(int err)
89 {
90         if (err >= -1000)
91                 return err;
92         switch (err) {
93         case -NFS4ERR_RESOURCE:
94                 return -EREMOTEIO;
95         case -NFS4ERR_WRONGSEC:
96                 return -EPERM;
97         case -NFS4ERR_BADOWNER:
98         case -NFS4ERR_BADNAME:
99                 return -EINVAL;
100         default:
101                 dprintk("%s could not handle NFSv4 error %d\n",
102                                 __func__, -err);
103                 break;
104         }
105         return -EIO;
106 }
107
108 /*
109  * This is our standard bitmap for GETATTR requests.
110  */
111 const u32 nfs4_fattr_bitmap[2] = {
112         FATTR4_WORD0_TYPE
113         | FATTR4_WORD0_CHANGE
114         | FATTR4_WORD0_SIZE
115         | FATTR4_WORD0_FSID
116         | FATTR4_WORD0_FILEID,
117         FATTR4_WORD1_MODE
118         | FATTR4_WORD1_NUMLINKS
119         | FATTR4_WORD1_OWNER
120         | FATTR4_WORD1_OWNER_GROUP
121         | FATTR4_WORD1_RAWDEV
122         | FATTR4_WORD1_SPACE_USED
123         | FATTR4_WORD1_TIME_ACCESS
124         | FATTR4_WORD1_TIME_METADATA
125         | FATTR4_WORD1_TIME_MODIFY
126 };
127
128 const u32 nfs4_statfs_bitmap[2] = {
129         FATTR4_WORD0_FILES_AVAIL
130         | FATTR4_WORD0_FILES_FREE
131         | FATTR4_WORD0_FILES_TOTAL,
132         FATTR4_WORD1_SPACE_AVAIL
133         | FATTR4_WORD1_SPACE_FREE
134         | FATTR4_WORD1_SPACE_TOTAL
135 };
136
137 const u32 nfs4_pathconf_bitmap[2] = {
138         FATTR4_WORD0_MAXLINK
139         | FATTR4_WORD0_MAXNAME,
140         0
141 };
142
143 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
144                         | FATTR4_WORD0_MAXREAD
145                         | FATTR4_WORD0_MAXWRITE
146                         | FATTR4_WORD0_LEASE_TIME,
147                         FATTR4_WORD1_TIME_DELTA
148                         | FATTR4_WORD1_FS_LAYOUT_TYPES
149 };
150
151 const u32 nfs4_fs_locations_bitmap[2] = {
152         FATTR4_WORD0_TYPE
153         | FATTR4_WORD0_CHANGE
154         | FATTR4_WORD0_SIZE
155         | FATTR4_WORD0_FSID
156         | FATTR4_WORD0_FILEID
157         | FATTR4_WORD0_FS_LOCATIONS,
158         FATTR4_WORD1_MODE
159         | FATTR4_WORD1_NUMLINKS
160         | FATTR4_WORD1_OWNER
161         | FATTR4_WORD1_OWNER_GROUP
162         | FATTR4_WORD1_RAWDEV
163         | FATTR4_WORD1_SPACE_USED
164         | FATTR4_WORD1_TIME_ACCESS
165         | FATTR4_WORD1_TIME_METADATA
166         | FATTR4_WORD1_TIME_MODIFY
167         | FATTR4_WORD1_MOUNTED_ON_FILEID
168 };
169
170 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
171                 struct nfs4_readdir_arg *readdir)
172 {
173         __be32 *start, *p;
174
175         BUG_ON(readdir->count < 80);
176         if (cookie > 2) {
177                 readdir->cookie = cookie;
178                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179                 return;
180         }
181
182         readdir->cookie = 0;
183         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184         if (cookie == 2)
185                 return;
186         
187         /*
188          * NFSv4 servers do not return entries for '.' and '..'
189          * Therefore, we fake these entries here.  We let '.'
190          * have cookie 0 and '..' have cookie 1.  Note that
191          * when talking to the server, we always send cookie 0
192          * instead of 1 or 2.
193          */
194         start = p = kmap_atomic(*readdir->pages, KM_USER0);
195         
196         if (cookie == 0) {
197                 *p++ = xdr_one;                                  /* next */
198                 *p++ = xdr_zero;                   /* cookie, first word */
199                 *p++ = xdr_one;                   /* cookie, second word */
200                 *p++ = xdr_one;                             /* entry len */
201                 memcpy(p, ".\0\0\0", 4);                        /* entry */
202                 p++;
203                 *p++ = xdr_one;                         /* bitmap length */
204                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
205                 *p++ = htonl(8);              /* attribute buffer length */
206                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
207         }
208         
209         *p++ = xdr_one;                                  /* next */
210         *p++ = xdr_zero;                   /* cookie, first word */
211         *p++ = xdr_two;                   /* cookie, second word */
212         *p++ = xdr_two;                             /* entry len */
213         memcpy(p, "..\0\0", 4);                         /* entry */
214         p++;
215         *p++ = xdr_one;                         /* bitmap length */
216         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
217         *p++ = htonl(8);              /* attribute buffer length */
218         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
219
220         readdir->pgbase = (char *)p - (char *)start;
221         readdir->count -= readdir->pgbase;
222         kunmap_atomic(start, KM_USER0);
223 }
224
225 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
226 {
227         int res;
228
229         might_sleep();
230
231         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
232                         nfs_wait_bit_killable, TASK_KILLABLE);
233         return res;
234 }
235
236 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
237 {
238         int res = 0;
239
240         might_sleep();
241
242         if (*timeout <= 0)
243                 *timeout = NFS4_POLL_RETRY_MIN;
244         if (*timeout > NFS4_POLL_RETRY_MAX)
245                 *timeout = NFS4_POLL_RETRY_MAX;
246         schedule_timeout_killable(*timeout);
247         if (fatal_signal_pending(current))
248                 res = -ERESTARTSYS;
249         *timeout <<= 1;
250         return res;
251 }
252
253 /* This is the error handling routine for processes that are allowed
254  * to sleep.
255  */
256 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
257 {
258         struct nfs_client *clp = server->nfs_client;
259         struct nfs4_state *state = exception->state;
260         int ret = errorcode;
261
262         exception->retry = 0;
263         switch(errorcode) {
264                 case 0:
265                         return 0;
266                 case -NFS4ERR_ADMIN_REVOKED:
267                 case -NFS4ERR_BAD_STATEID:
268                 case -NFS4ERR_OPENMODE:
269                         if (state == NULL)
270                                 break;
271                         nfs4_schedule_stateid_recovery(server, state);
272                         goto wait_on_recovery;
273                 case -NFS4ERR_EXPIRED:
274                         if (state != NULL)
275                                 nfs4_schedule_stateid_recovery(server, state);
276                 case -NFS4ERR_STALE_STATEID:
277                 case -NFS4ERR_STALE_CLIENTID:
278                         nfs4_schedule_lease_recovery(clp);
279                         goto wait_on_recovery;
280 #if defined(CONFIG_NFS_V4_1)
281                 case -NFS4ERR_BADSESSION:
282                 case -NFS4ERR_BADSLOT:
283                 case -NFS4ERR_BAD_HIGH_SLOT:
284                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
285                 case -NFS4ERR_DEADSESSION:
286                 case -NFS4ERR_SEQ_FALSE_RETRY:
287                 case -NFS4ERR_SEQ_MISORDERED:
288                         dprintk("%s ERROR: %d Reset session\n", __func__,
289                                 errorcode);
290                         nfs4_schedule_session_recovery(clp->cl_session);
291                         exception->retry = 1;
292                         break;
293 #endif /* defined(CONFIG_NFS_V4_1) */
294                 case -NFS4ERR_FILE_OPEN:
295                         if (exception->timeout > HZ) {
296                                 /* We have retried a decent amount, time to
297                                  * fail
298                                  */
299                                 ret = -EBUSY;
300                                 break;
301                         }
302                 case -NFS4ERR_GRACE:
303                 case -NFS4ERR_DELAY:
304                 case -EKEYEXPIRED:
305                         ret = nfs4_delay(server->client, &exception->timeout);
306                         if (ret != 0)
307                                 break;
308                 case -NFS4ERR_RETRY_UNCACHED_REP:
309                 case -NFS4ERR_OLD_STATEID:
310                         exception->retry = 1;
311                         break;
312                 case -NFS4ERR_BADOWNER:
313                         /* The following works around a Linux server bug! */
314                 case -NFS4ERR_BADNAME:
315                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
316                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
317                                 exception->retry = 1;
318                                 printk(KERN_WARNING "NFS: v4 server %s "
319                                                 "does not accept raw "
320                                                 "uid/gids. "
321                                                 "Reenabling the idmapper.\n",
322                                                 server->nfs_client->cl_hostname);
323                         }
324         }
325         /* We failed to handle the error */
326         return nfs4_map_errors(ret);
327 wait_on_recovery:
328         ret = nfs4_wait_clnt_recover(clp);
329         if (ret == 0)
330                 exception->retry = 1;
331         return ret;
332 }
333
334
335 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
336 {
337         spin_lock(&clp->cl_lock);
338         if (time_before(clp->cl_last_renewal,timestamp))
339                 clp->cl_last_renewal = timestamp;
340         spin_unlock(&clp->cl_lock);
341 }
342
343 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
344 {
345         do_renew_lease(server->nfs_client, timestamp);
346 }
347
348 #if defined(CONFIG_NFS_V4_1)
349
350 /*
351  * nfs4_free_slot - free a slot and efficiently update slot table.
352  *
353  * freeing a slot is trivially done by clearing its respective bit
354  * in the bitmap.
355  * If the freed slotid equals highest_used_slotid we want to update it
356  * so that the server would be able to size down the slot table if needed,
357  * otherwise we know that the highest_used_slotid is still in use.
358  * When updating highest_used_slotid there may be "holes" in the bitmap
359  * so we need to scan down from highest_used_slotid to 0 looking for the now
360  * highest slotid in use.
361  * If none found, highest_used_slotid is set to -1.
362  *
363  * Must be called while holding tbl->slot_tbl_lock
364  */
365 static void
366 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
367 {
368         int free_slotid = free_slot - tbl->slots;
369         int slotid = free_slotid;
370
371         BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
372         /* clear used bit in bitmap */
373         __clear_bit(slotid, tbl->used_slots);
374
375         /* update highest_used_slotid when it is freed */
376         if (slotid == tbl->highest_used_slotid) {
377                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
378                 if (slotid < tbl->max_slots)
379                         tbl->highest_used_slotid = slotid;
380                 else
381                         tbl->highest_used_slotid = -1;
382         }
383         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384                 free_slotid, tbl->highest_used_slotid);
385 }
386
387 /*
388  * Signal state manager thread if session fore channel is drained
389  */
390 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
391 {
392         struct rpc_task *task;
393
394         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
395                 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396                 if (task)
397                         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
398                 return;
399         }
400
401         if (ses->fc_slot_table.highest_used_slotid != -1)
402                 return;
403
404         dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405         complete(&ses->fc_slot_table.complete);
406 }
407
408 /*
409  * Signal state manager thread if session back channel is drained
410  */
411 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
412 {
413         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414             ses->bc_slot_table.highest_used_slotid != -1)
415                 return;
416         dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417         complete(&ses->bc_slot_table.complete);
418 }
419
420 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
421 {
422         struct nfs4_slot_table *tbl;
423
424         tbl = &res->sr_session->fc_slot_table;
425         if (!res->sr_slot) {
426                 /* just wake up the next guy waiting since
427                  * we may have not consumed a slot after all */
428                 dprintk("%s: No slot\n", __func__);
429                 return;
430         }
431
432         spin_lock(&tbl->slot_tbl_lock);
433         nfs4_free_slot(tbl, res->sr_slot);
434         nfs4_check_drain_fc_complete(res->sr_session);
435         spin_unlock(&tbl->slot_tbl_lock);
436         res->sr_slot = NULL;
437 }
438
439 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
440 {
441         unsigned long timestamp;
442         struct nfs_client *clp;
443
444         /*
445          * sr_status remains 1 if an RPC level error occurred. The server
446          * may or may not have processed the sequence operation..
447          * Proceed as if the server received and processed the sequence
448          * operation.
449          */
450         if (res->sr_status == 1)
451                 res->sr_status = NFS_OK;
452
453         /* don't increment the sequence number if the task wasn't sent */
454         if (!RPC_WAS_SENT(task))
455                 goto out;
456
457         /* Check the SEQUENCE operation status */
458         switch (res->sr_status) {
459         case 0:
460                 /* Update the slot's sequence and clientid lease timer */
461                 ++res->sr_slot->seq_nr;
462                 timestamp = res->sr_renewal_time;
463                 clp = res->sr_session->clp;
464                 do_renew_lease(clp, timestamp);
465                 /* Check sequence flags */
466                 if (res->sr_status_flags != 0)
467                         nfs4_schedule_lease_recovery(clp);
468                 break;
469         case -NFS4ERR_DELAY:
470                 /* The server detected a resend of the RPC call and
471                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
472                  * of RFC5661.
473                  */
474                 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
475                         __func__,
476                         res->sr_slot - res->sr_session->fc_slot_table.slots,
477                         res->sr_slot->seq_nr);
478                 goto out_retry;
479         default:
480                 /* Just update the slot sequence no. */
481                 ++res->sr_slot->seq_nr;
482         }
483 out:
484         /* The session may be reset by one of the error handlers. */
485         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
486         nfs41_sequence_free_slot(res);
487         return 1;
488 out_retry:
489         if (!rpc_restart_call(task))
490                 goto out;
491         rpc_delay(task, NFS4_POLL_RETRY_MAX);
492         return 0;
493 }
494
495 static int nfs4_sequence_done(struct rpc_task *task,
496                                struct nfs4_sequence_res *res)
497 {
498         if (res->sr_session == NULL)
499                 return 1;
500         return nfs41_sequence_done(task, res);
501 }
502
503 /*
504  * nfs4_find_slot - efficiently look for a free slot
505  *
506  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507  * If found, we mark the slot as used, update the highest_used_slotid,
508  * and respectively set up the sequence operation args.
509  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
510  *
511  * Note: must be called with under the slot_tbl_lock.
512  */
513 static u8
514 nfs4_find_slot(struct nfs4_slot_table *tbl)
515 {
516         int slotid;
517         u8 ret_id = NFS4_MAX_SLOT_TABLE;
518         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
519
520         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522                 tbl->max_slots);
523         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524         if (slotid >= tbl->max_slots)
525                 goto out;
526         __set_bit(slotid, tbl->used_slots);
527         if (slotid > tbl->highest_used_slotid)
528                 tbl->highest_used_slotid = slotid;
529         ret_id = slotid;
530 out:
531         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
533         return ret_id;
534 }
535
536 int nfs41_setup_sequence(struct nfs4_session *session,
537                                 struct nfs4_sequence_args *args,
538                                 struct nfs4_sequence_res *res,
539                                 int cache_reply,
540                                 struct rpc_task *task)
541 {
542         struct nfs4_slot *slot;
543         struct nfs4_slot_table *tbl;
544         u8 slotid;
545
546         dprintk("--> %s\n", __func__);
547         /* slot already allocated? */
548         if (res->sr_slot != NULL)
549                 return 0;
550
551         tbl = &session->fc_slot_table;
552
553         spin_lock(&tbl->slot_tbl_lock);
554         if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
555             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
556                 /*
557                  * The state manager will wait until the slot table is empty.
558                  * Schedule the reset thread
559                  */
560                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561                 spin_unlock(&tbl->slot_tbl_lock);
562                 dprintk("%s Schedule Session Reset\n", __func__);
563                 return -EAGAIN;
564         }
565
566         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
567             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
568                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569                 spin_unlock(&tbl->slot_tbl_lock);
570                 dprintk("%s enforce FIFO order\n", __func__);
571                 return -EAGAIN;
572         }
573
574         slotid = nfs4_find_slot(tbl);
575         if (slotid == NFS4_MAX_SLOT_TABLE) {
576                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577                 spin_unlock(&tbl->slot_tbl_lock);
578                 dprintk("<-- %s: no free slots\n", __func__);
579                 return -EAGAIN;
580         }
581         spin_unlock(&tbl->slot_tbl_lock);
582
583         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
584         slot = tbl->slots + slotid;
585         args->sa_session = session;
586         args->sa_slotid = slotid;
587         args->sa_cache_this = cache_reply;
588
589         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
590
591         res->sr_session = session;
592         res->sr_slot = slot;
593         res->sr_renewal_time = jiffies;
594         res->sr_status_flags = 0;
595         /*
596          * sr_status is only set in decode_sequence, and so will remain
597          * set to 1 if an rpc level failure occurs.
598          */
599         res->sr_status = 1;
600         return 0;
601 }
602 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
603
604 int nfs4_setup_sequence(const struct nfs_server *server,
605                         struct nfs4_sequence_args *args,
606                         struct nfs4_sequence_res *res,
607                         int cache_reply,
608                         struct rpc_task *task)
609 {
610         struct nfs4_session *session = nfs4_get_session(server);
611         int ret = 0;
612
613         if (session == NULL) {
614                 args->sa_session = NULL;
615                 res->sr_session = NULL;
616                 goto out;
617         }
618
619         dprintk("--> %s clp %p session %p sr_slot %td\n",
620                 __func__, session->clp, session, res->sr_slot ?
621                         res->sr_slot - session->fc_slot_table.slots : -1);
622
623         ret = nfs41_setup_sequence(session, args, res, cache_reply,
624                                    task);
625 out:
626         dprintk("<-- %s status=%d\n", __func__, ret);
627         return ret;
628 }
629
630 struct nfs41_call_sync_data {
631         const struct nfs_server *seq_server;
632         struct nfs4_sequence_args *seq_args;
633         struct nfs4_sequence_res *seq_res;
634         int cache_reply;
635 };
636
637 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
638 {
639         struct nfs41_call_sync_data *data = calldata;
640
641         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
642
643         if (nfs4_setup_sequence(data->seq_server, data->seq_args,
644                                 data->seq_res, data->cache_reply, task))
645                 return;
646         rpc_call_start(task);
647 }
648
649 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
650 {
651         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
652         nfs41_call_sync_prepare(task, calldata);
653 }
654
655 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656 {
657         struct nfs41_call_sync_data *data = calldata;
658
659         nfs41_sequence_done(task, data->seq_res);
660 }
661
662 struct rpc_call_ops nfs41_call_sync_ops = {
663         .rpc_call_prepare = nfs41_call_sync_prepare,
664         .rpc_call_done = nfs41_call_sync_done,
665 };
666
667 struct rpc_call_ops nfs41_call_priv_sync_ops = {
668         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
669         .rpc_call_done = nfs41_call_sync_done,
670 };
671
672 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673                                    struct nfs_server *server,
674                                    struct rpc_message *msg,
675                                    struct nfs4_sequence_args *args,
676                                    struct nfs4_sequence_res *res,
677                                    int cache_reply,
678                                    int privileged)
679 {
680         int ret;
681         struct rpc_task *task;
682         struct nfs41_call_sync_data data = {
683                 .seq_server = server,
684                 .seq_args = args,
685                 .seq_res = res,
686                 .cache_reply = cache_reply,
687         };
688         struct rpc_task_setup task_setup = {
689                 .rpc_client = clnt,
690                 .rpc_message = msg,
691                 .callback_ops = &nfs41_call_sync_ops,
692                 .callback_data = &data
693         };
694
695         res->sr_slot = NULL;
696         if (privileged)
697                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
698         task = rpc_run_task(&task_setup);
699         if (IS_ERR(task))
700                 ret = PTR_ERR(task);
701         else {
702                 ret = task->tk_status;
703                 rpc_put_task(task);
704         }
705         return ret;
706 }
707
708 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
709                             struct nfs_server *server,
710                             struct rpc_message *msg,
711                             struct nfs4_sequence_args *args,
712                             struct nfs4_sequence_res *res,
713                             int cache_reply)
714 {
715         return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
716 }
717
718 #else
719 static int nfs4_sequence_done(struct rpc_task *task,
720                                struct nfs4_sequence_res *res)
721 {
722         return 1;
723 }
724 #endif /* CONFIG_NFS_V4_1 */
725
726 int _nfs4_call_sync(struct rpc_clnt *clnt,
727                     struct nfs_server *server,
728                     struct rpc_message *msg,
729                     struct nfs4_sequence_args *args,
730                     struct nfs4_sequence_res *res,
731                     int cache_reply)
732 {
733         args->sa_session = res->sr_session = NULL;
734         return rpc_call_sync(clnt, msg, 0);
735 }
736
737 static inline
738 int nfs4_call_sync(struct rpc_clnt *clnt,
739                    struct nfs_server *server,
740                    struct rpc_message *msg,
741                    struct nfs4_sequence_args *args,
742                    struct nfs4_sequence_res *res,
743                    int cache_reply)
744 {
745         return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
746                                                 args, res, cache_reply);
747 }
748
749 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
750 {
751         struct nfs_inode *nfsi = NFS_I(dir);
752
753         spin_lock(&dir->i_lock);
754         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
755         if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
756                 nfs_force_lookup_revalidate(dir);
757         nfsi->change_attr = cinfo->after;
758         spin_unlock(&dir->i_lock);
759 }
760
761 struct nfs4_opendata {
762         struct kref kref;
763         struct nfs_openargs o_arg;
764         struct nfs_openres o_res;
765         struct nfs_open_confirmargs c_arg;
766         struct nfs_open_confirmres c_res;
767         struct nfs_fattr f_attr;
768         struct nfs_fattr dir_attr;
769         struct path path;
770         struct dentry *dir;
771         struct nfs4_state_owner *owner;
772         struct nfs4_state *state;
773         struct iattr attrs;
774         unsigned long timestamp;
775         unsigned int rpc_done : 1;
776         int rpc_status;
777         int cancelled;
778 };
779
780
781 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
782 {
783         p->o_res.f_attr = &p->f_attr;
784         p->o_res.dir_attr = &p->dir_attr;
785         p->o_res.seqid = p->o_arg.seqid;
786         p->c_res.seqid = p->c_arg.seqid;
787         p->o_res.server = p->o_arg.server;
788         nfs_fattr_init(&p->f_attr);
789         nfs_fattr_init(&p->dir_attr);
790 }
791
792 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
793                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
794                 const struct iattr *attrs,
795                 gfp_t gfp_mask)
796 {
797         struct dentry *parent = dget_parent(path->dentry);
798         struct inode *dir = parent->d_inode;
799         struct nfs_server *server = NFS_SERVER(dir);
800         struct nfs4_opendata *p;
801
802         p = kzalloc(sizeof(*p), gfp_mask);
803         if (p == NULL)
804                 goto err;
805         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
806         if (p->o_arg.seqid == NULL)
807                 goto err_free;
808         path_get(path);
809         p->path = *path;
810         p->dir = parent;
811         p->owner = sp;
812         atomic_inc(&sp->so_count);
813         p->o_arg.fh = NFS_FH(dir);
814         p->o_arg.open_flags = flags;
815         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
816         p->o_arg.clientid = server->nfs_client->cl_clientid;
817         p->o_arg.id = sp->so_owner_id.id;
818         p->o_arg.name = &p->path.dentry->d_name;
819         p->o_arg.server = server;
820         p->o_arg.bitmask = server->attr_bitmask;
821         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
822         if (flags & O_CREAT) {
823                 u32 *s;
824
825                 p->o_arg.u.attrs = &p->attrs;
826                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
827                 s = (u32 *) p->o_arg.u.verifier.data;
828                 s[0] = jiffies;
829                 s[1] = current->pid;
830         }
831         p->c_arg.fh = &p->o_res.fh;
832         p->c_arg.stateid = &p->o_res.stateid;
833         p->c_arg.seqid = p->o_arg.seqid;
834         nfs4_init_opendata_res(p);
835         kref_init(&p->kref);
836         return p;
837 err_free:
838         kfree(p);
839 err:
840         dput(parent);
841         return NULL;
842 }
843
844 static void nfs4_opendata_free(struct kref *kref)
845 {
846         struct nfs4_opendata *p = container_of(kref,
847                         struct nfs4_opendata, kref);
848
849         nfs_free_seqid(p->o_arg.seqid);
850         if (p->state != NULL)
851                 nfs4_put_open_state(p->state);
852         nfs4_put_state_owner(p->owner);
853         dput(p->dir);
854         path_put(&p->path);
855         kfree(p);
856 }
857
858 static void nfs4_opendata_put(struct nfs4_opendata *p)
859 {
860         if (p != NULL)
861                 kref_put(&p->kref, nfs4_opendata_free);
862 }
863
864 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
865 {
866         int ret;
867
868         ret = rpc_wait_for_completion_task(task);
869         return ret;
870 }
871
872 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
873 {
874         int ret = 0;
875
876         if (open_mode & O_EXCL)
877                 goto out;
878         switch (mode & (FMODE_READ|FMODE_WRITE)) {
879                 case FMODE_READ:
880                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
881                                 && state->n_rdonly != 0;
882                         break;
883                 case FMODE_WRITE:
884                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
885                                 && state->n_wronly != 0;
886                         break;
887                 case FMODE_READ|FMODE_WRITE:
888                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
889                                 && state->n_rdwr != 0;
890         }
891 out:
892         return ret;
893 }
894
895 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
896 {
897         if ((delegation->type & fmode) != fmode)
898                 return 0;
899         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
900                 return 0;
901         nfs_mark_delegation_referenced(delegation);
902         return 1;
903 }
904
905 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
906 {
907         switch (fmode) {
908                 case FMODE_WRITE:
909                         state->n_wronly++;
910                         break;
911                 case FMODE_READ:
912                         state->n_rdonly++;
913                         break;
914                 case FMODE_READ|FMODE_WRITE:
915                         state->n_rdwr++;
916         }
917         nfs4_state_set_mode_locked(state, state->state | fmode);
918 }
919
920 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
921 {
922         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
924         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
925         switch (fmode) {
926                 case FMODE_READ:
927                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
928                         break;
929                 case FMODE_WRITE:
930                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
931                         break;
932                 case FMODE_READ|FMODE_WRITE:
933                         set_bit(NFS_O_RDWR_STATE, &state->flags);
934         }
935 }
936
937 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
938 {
939         write_seqlock(&state->seqlock);
940         nfs_set_open_stateid_locked(state, stateid, fmode);
941         write_sequnlock(&state->seqlock);
942 }
943
944 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
945 {
946         /*
947          * Protect the call to nfs4_state_set_mode_locked and
948          * serialise the stateid update
949          */
950         write_seqlock(&state->seqlock);
951         if (deleg_stateid != NULL) {
952                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
953                 set_bit(NFS_DELEGATED_STATE, &state->flags);
954         }
955         if (open_stateid != NULL)
956                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
957         write_sequnlock(&state->seqlock);
958         spin_lock(&state->owner->so_lock);
959         update_open_stateflags(state, fmode);
960         spin_unlock(&state->owner->so_lock);
961 }
962
963 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
964 {
965         struct nfs_inode *nfsi = NFS_I(state->inode);
966         struct nfs_delegation *deleg_cur;
967         int ret = 0;
968
969         fmode &= (FMODE_READ|FMODE_WRITE);
970
971         rcu_read_lock();
972         deleg_cur = rcu_dereference(nfsi->delegation);
973         if (deleg_cur == NULL)
974                 goto no_delegation;
975
976         spin_lock(&deleg_cur->lock);
977         if (nfsi->delegation != deleg_cur ||
978             (deleg_cur->type & fmode) != fmode)
979                 goto no_delegation_unlock;
980
981         if (delegation == NULL)
982                 delegation = &deleg_cur->stateid;
983         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
984                 goto no_delegation_unlock;
985
986         nfs_mark_delegation_referenced(deleg_cur);
987         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
988         ret = 1;
989 no_delegation_unlock:
990         spin_unlock(&deleg_cur->lock);
991 no_delegation:
992         rcu_read_unlock();
993
994         if (!ret && open_stateid != NULL) {
995                 __update_open_stateid(state, open_stateid, NULL, fmode);
996                 ret = 1;
997         }
998
999         return ret;
1000 }
1001
1002
1003 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1004 {
1005         struct nfs_delegation *delegation;
1006
1007         rcu_read_lock();
1008         delegation = rcu_dereference(NFS_I(inode)->delegation);
1009         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1010                 rcu_read_unlock();
1011                 return;
1012         }
1013         rcu_read_unlock();
1014         nfs_inode_return_delegation(inode);
1015 }
1016
1017 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1018 {
1019         struct nfs4_state *state = opendata->state;
1020         struct nfs_inode *nfsi = NFS_I(state->inode);
1021         struct nfs_delegation *delegation;
1022         int open_mode = opendata->o_arg.open_flags & O_EXCL;
1023         fmode_t fmode = opendata->o_arg.fmode;
1024         nfs4_stateid stateid;
1025         int ret = -EAGAIN;
1026
1027         for (;;) {
1028                 if (can_open_cached(state, fmode, open_mode)) {
1029                         spin_lock(&state->owner->so_lock);
1030                         if (can_open_cached(state, fmode, open_mode)) {
1031                                 update_open_stateflags(state, fmode);
1032                                 spin_unlock(&state->owner->so_lock);
1033                                 goto out_return_state;
1034                         }
1035                         spin_unlock(&state->owner->so_lock);
1036                 }
1037                 rcu_read_lock();
1038                 delegation = rcu_dereference(nfsi->delegation);
1039                 if (delegation == NULL ||
1040                     !can_open_delegated(delegation, fmode)) {
1041                         rcu_read_unlock();
1042                         break;
1043                 }
1044                 /* Save the delegation */
1045                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1046                 rcu_read_unlock();
1047                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1048                 if (ret != 0)
1049                         goto out;
1050                 ret = -EAGAIN;
1051
1052                 /* Try to update the stateid using the delegation */
1053                 if (update_open_stateid(state, NULL, &stateid, fmode))
1054                         goto out_return_state;
1055         }
1056 out:
1057         return ERR_PTR(ret);
1058 out_return_state:
1059         atomic_inc(&state->count);
1060         return state;
1061 }
1062
1063 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1064 {
1065         struct inode *inode;
1066         struct nfs4_state *state = NULL;
1067         struct nfs_delegation *delegation;
1068         int ret;
1069
1070         if (!data->rpc_done) {
1071                 state = nfs4_try_open_cached(data);
1072                 goto out;
1073         }
1074
1075         ret = -EAGAIN;
1076         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1077                 goto err;
1078         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1079         ret = PTR_ERR(inode);
1080         if (IS_ERR(inode))
1081                 goto err;
1082         ret = -ENOMEM;
1083         state = nfs4_get_open_state(inode, data->owner);
1084         if (state == NULL)
1085                 goto err_put_inode;
1086         if (data->o_res.delegation_type != 0) {
1087                 int delegation_flags = 0;
1088
1089                 rcu_read_lock();
1090                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1091                 if (delegation)
1092                         delegation_flags = delegation->flags;
1093                 rcu_read_unlock();
1094                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1095                         nfs_inode_set_delegation(state->inode,
1096                                         data->owner->so_cred,
1097                                         &data->o_res);
1098                 else
1099                         nfs_inode_reclaim_delegation(state->inode,
1100                                         data->owner->so_cred,
1101                                         &data->o_res);
1102         }
1103
1104         update_open_stateid(state, &data->o_res.stateid, NULL,
1105                         data->o_arg.fmode);
1106         iput(inode);
1107 out:
1108         return state;
1109 err_put_inode:
1110         iput(inode);
1111 err:
1112         return ERR_PTR(ret);
1113 }
1114
1115 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1116 {
1117         struct nfs_inode *nfsi = NFS_I(state->inode);
1118         struct nfs_open_context *ctx;
1119
1120         spin_lock(&state->inode->i_lock);
1121         list_for_each_entry(ctx, &nfsi->open_files, list) {
1122                 if (ctx->state != state)
1123                         continue;
1124                 get_nfs_open_context(ctx);
1125                 spin_unlock(&state->inode->i_lock);
1126                 return ctx;
1127         }
1128         spin_unlock(&state->inode->i_lock);
1129         return ERR_PTR(-ENOENT);
1130 }
1131
1132 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1133 {
1134         struct nfs4_opendata *opendata;
1135
1136         opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1137         if (opendata == NULL)
1138                 return ERR_PTR(-ENOMEM);
1139         opendata->state = state;
1140         atomic_inc(&state->count);
1141         return opendata;
1142 }
1143
1144 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1145 {
1146         struct nfs4_state *newstate;
1147         int ret;
1148
1149         opendata->o_arg.open_flags = 0;
1150         opendata->o_arg.fmode = fmode;
1151         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1152         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1153         nfs4_init_opendata_res(opendata);
1154         ret = _nfs4_recover_proc_open(opendata);
1155         if (ret != 0)
1156                 return ret; 
1157         newstate = nfs4_opendata_to_nfs4_state(opendata);
1158         if (IS_ERR(newstate))
1159                 return PTR_ERR(newstate);
1160         nfs4_close_state(&opendata->path, newstate, fmode);
1161         *res = newstate;
1162         return 0;
1163 }
1164
1165 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1166 {
1167         struct nfs4_state *newstate;
1168         int ret;
1169
1170         /* memory barrier prior to reading state->n_* */
1171         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1172         smp_rmb();
1173         if (state->n_rdwr != 0) {
1174                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1175                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1176                 if (ret != 0)
1177                         return ret;
1178                 if (newstate != state)
1179                         return -ESTALE;
1180         }
1181         if (state->n_wronly != 0) {
1182                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1183                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1184                 if (ret != 0)
1185                         return ret;
1186                 if (newstate != state)
1187                         return -ESTALE;
1188         }
1189         if (state->n_rdonly != 0) {
1190                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1191                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1192                 if (ret != 0)
1193                         return ret;
1194                 if (newstate != state)
1195                         return -ESTALE;
1196         }
1197         /*
1198          * We may have performed cached opens for all three recoveries.
1199          * Check if we need to update the current stateid.
1200          */
1201         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1202             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1203                 write_seqlock(&state->seqlock);
1204                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1205                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1206                 write_sequnlock(&state->seqlock);
1207         }
1208         return 0;
1209 }
1210
1211 /*
1212  * OPEN_RECLAIM:
1213  *      reclaim state on the server after a reboot.
1214  */
1215 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1216 {
1217         struct nfs_delegation *delegation;
1218         struct nfs4_opendata *opendata;
1219         fmode_t delegation_type = 0;
1220         int status;
1221
1222         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1223         if (IS_ERR(opendata))
1224                 return PTR_ERR(opendata);
1225         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1226         opendata->o_arg.fh = NFS_FH(state->inode);
1227         rcu_read_lock();
1228         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1229         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1230                 delegation_type = delegation->type;
1231         rcu_read_unlock();
1232         opendata->o_arg.u.delegation_type = delegation_type;
1233         status = nfs4_open_recover(opendata, state);
1234         nfs4_opendata_put(opendata);
1235         return status;
1236 }
1237
1238 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1239 {
1240         struct nfs_server *server = NFS_SERVER(state->inode);
1241         struct nfs4_exception exception = { };
1242         int err;
1243         do {
1244                 err = _nfs4_do_open_reclaim(ctx, state);
1245                 if (err != -NFS4ERR_DELAY)
1246                         break;
1247                 nfs4_handle_exception(server, err, &exception);
1248         } while (exception.retry);
1249         return err;
1250 }
1251
1252 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1253 {
1254         struct nfs_open_context *ctx;
1255         int ret;
1256
1257         ctx = nfs4_state_find_open_context(state);
1258         if (IS_ERR(ctx))
1259                 return PTR_ERR(ctx);
1260         ret = nfs4_do_open_reclaim(ctx, state);
1261         put_nfs_open_context(ctx);
1262         return ret;
1263 }
1264
1265 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1266 {
1267         struct nfs4_opendata *opendata;
1268         int ret;
1269
1270         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1271         if (IS_ERR(opendata))
1272                 return PTR_ERR(opendata);
1273         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1274         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1275                         sizeof(opendata->o_arg.u.delegation.data));
1276         ret = nfs4_open_recover(opendata, state);
1277         nfs4_opendata_put(opendata);
1278         return ret;
1279 }
1280
1281 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1282 {
1283         struct nfs4_exception exception = { };
1284         struct nfs_server *server = NFS_SERVER(state->inode);
1285         int err;
1286         do {
1287                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1288                 switch (err) {
1289                         case 0:
1290                         case -ENOENT:
1291                         case -ESTALE:
1292                                 goto out;
1293                         case -NFS4ERR_BADSESSION:
1294                         case -NFS4ERR_BADSLOT:
1295                         case -NFS4ERR_BAD_HIGH_SLOT:
1296                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1297                         case -NFS4ERR_DEADSESSION:
1298                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1299                                 goto out;
1300                         case -NFS4ERR_STALE_CLIENTID:
1301                         case -NFS4ERR_STALE_STATEID:
1302                         case -NFS4ERR_EXPIRED:
1303                                 /* Don't recall a delegation if it was lost */
1304                                 nfs4_schedule_lease_recovery(server->nfs_client);
1305                                 goto out;
1306                         case -ERESTARTSYS:
1307                                 /*
1308                                  * The show must go on: exit, but mark the
1309                                  * stateid as needing recovery.
1310                                  */
1311                         case -NFS4ERR_ADMIN_REVOKED:
1312                         case -NFS4ERR_BAD_STATEID:
1313                                 nfs4_schedule_stateid_recovery(server, state);
1314                         case -EKEYEXPIRED:
1315                                 /*
1316                                  * User RPCSEC_GSS context has expired.
1317                                  * We cannot recover this stateid now, so
1318                                  * skip it and allow recovery thread to
1319                                  * proceed.
1320                                  */
1321                         case -ENOMEM:
1322                                 err = 0;
1323                                 goto out;
1324                 }
1325                 err = nfs4_handle_exception(server, err, &exception);
1326         } while (exception.retry);
1327 out:
1328         return err;
1329 }
1330
1331 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1332 {
1333         struct nfs4_opendata *data = calldata;
1334
1335         data->rpc_status = task->tk_status;
1336         if (data->rpc_status == 0) {
1337                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1338                                 sizeof(data->o_res.stateid.data));
1339                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1340                 renew_lease(data->o_res.server, data->timestamp);
1341                 data->rpc_done = 1;
1342         }
1343 }
1344
1345 static void nfs4_open_confirm_release(void *calldata)
1346 {
1347         struct nfs4_opendata *data = calldata;
1348         struct nfs4_state *state = NULL;
1349
1350         /* If this request hasn't been cancelled, do nothing */
1351         if (data->cancelled == 0)
1352                 goto out_free;
1353         /* In case of error, no cleanup! */
1354         if (!data->rpc_done)
1355                 goto out_free;
1356         state = nfs4_opendata_to_nfs4_state(data);
1357         if (!IS_ERR(state))
1358                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1359 out_free:
1360         nfs4_opendata_put(data);
1361 }
1362
1363 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1364         .rpc_call_done = nfs4_open_confirm_done,
1365         .rpc_release = nfs4_open_confirm_release,
1366 };
1367
1368 /*
1369  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1370  */
1371 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1372 {
1373         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1374         struct rpc_task *task;
1375         struct  rpc_message msg = {
1376                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1377                 .rpc_argp = &data->c_arg,
1378                 .rpc_resp = &data->c_res,
1379                 .rpc_cred = data->owner->so_cred,
1380         };
1381         struct rpc_task_setup task_setup_data = {
1382                 .rpc_client = server->client,
1383                 .rpc_message = &msg,
1384                 .callback_ops = &nfs4_open_confirm_ops,
1385                 .callback_data = data,
1386                 .workqueue = nfsiod_workqueue,
1387                 .flags = RPC_TASK_ASYNC,
1388         };
1389         int status;
1390
1391         kref_get(&data->kref);
1392         data->rpc_done = 0;
1393         data->rpc_status = 0;
1394         data->timestamp = jiffies;
1395         task = rpc_run_task(&task_setup_data);
1396         if (IS_ERR(task))
1397                 return PTR_ERR(task);
1398         status = nfs4_wait_for_completion_rpc_task(task);
1399         if (status != 0) {
1400                 data->cancelled = 1;
1401                 smp_wmb();
1402         } else
1403                 status = data->rpc_status;
1404         rpc_put_task(task);
1405         return status;
1406 }
1407
1408 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1409 {
1410         struct nfs4_opendata *data = calldata;
1411         struct nfs4_state_owner *sp = data->owner;
1412
1413         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1414                 return;
1415         /*
1416          * Check if we still need to send an OPEN call, or if we can use
1417          * a delegation instead.
1418          */
1419         if (data->state != NULL) {
1420                 struct nfs_delegation *delegation;
1421
1422                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1423                         goto out_no_action;
1424                 rcu_read_lock();
1425                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1426                 if (delegation != NULL &&
1427                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1428                         rcu_read_unlock();
1429                         goto out_no_action;
1430                 }
1431                 rcu_read_unlock();
1432         }
1433         /* Update sequence id. */
1434         data->o_arg.id = sp->so_owner_id.id;
1435         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1436         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1437                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1438                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1439         }
1440         data->timestamp = jiffies;
1441         if (nfs4_setup_sequence(data->o_arg.server,
1442                                 &data->o_arg.seq_args,
1443                                 &data->o_res.seq_res, 1, task))
1444                 return;
1445         rpc_call_start(task);
1446         return;
1447 out_no_action:
1448         task->tk_action = NULL;
1449
1450 }
1451
1452 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1453 {
1454         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1455         nfs4_open_prepare(task, calldata);
1456 }
1457
1458 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1459 {
1460         struct nfs4_opendata *data = calldata;
1461
1462         data->rpc_status = task->tk_status;
1463
1464         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1465                 return;
1466
1467         if (task->tk_status == 0) {
1468                 switch (data->o_res.f_attr->mode & S_IFMT) {
1469                         case S_IFREG:
1470                                 break;
1471                         case S_IFLNK:
1472                                 data->rpc_status = -ELOOP;
1473                                 break;
1474                         case S_IFDIR:
1475                                 data->rpc_status = -EISDIR;
1476                                 break;
1477                         default:
1478                                 data->rpc_status = -ENOTDIR;
1479                 }
1480                 renew_lease(data->o_res.server, data->timestamp);
1481                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1482                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1483         }
1484         data->rpc_done = 1;
1485 }
1486
1487 static void nfs4_open_release(void *calldata)
1488 {
1489         struct nfs4_opendata *data = calldata;
1490         struct nfs4_state *state = NULL;
1491
1492         /* If this request hasn't been cancelled, do nothing */
1493         if (data->cancelled == 0)
1494                 goto out_free;
1495         /* In case of error, no cleanup! */
1496         if (data->rpc_status != 0 || !data->rpc_done)
1497                 goto out_free;
1498         /* In case we need an open_confirm, no cleanup! */
1499         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1500                 goto out_free;
1501         state = nfs4_opendata_to_nfs4_state(data);
1502         if (!IS_ERR(state))
1503                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1504 out_free:
1505         nfs4_opendata_put(data);
1506 }
1507
1508 static const struct rpc_call_ops nfs4_open_ops = {
1509         .rpc_call_prepare = nfs4_open_prepare,
1510         .rpc_call_done = nfs4_open_done,
1511         .rpc_release = nfs4_open_release,
1512 };
1513
1514 static const struct rpc_call_ops nfs4_recover_open_ops = {
1515         .rpc_call_prepare = nfs4_recover_open_prepare,
1516         .rpc_call_done = nfs4_open_done,
1517         .rpc_release = nfs4_open_release,
1518 };
1519
1520 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1521 {
1522         struct inode *dir = data->dir->d_inode;
1523         struct nfs_server *server = NFS_SERVER(dir);
1524         struct nfs_openargs *o_arg = &data->o_arg;
1525         struct nfs_openres *o_res = &data->o_res;
1526         struct rpc_task *task;
1527         struct rpc_message msg = {
1528                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1529                 .rpc_argp = o_arg,
1530                 .rpc_resp = o_res,
1531                 .rpc_cred = data->owner->so_cred,
1532         };
1533         struct rpc_task_setup task_setup_data = {
1534                 .rpc_client = server->client,
1535                 .rpc_message = &msg,
1536                 .callback_ops = &nfs4_open_ops,
1537                 .callback_data = data,
1538                 .workqueue = nfsiod_workqueue,
1539                 .flags = RPC_TASK_ASYNC,
1540         };
1541         int status;
1542
1543         kref_get(&data->kref);
1544         data->rpc_done = 0;
1545         data->rpc_status = 0;
1546         data->cancelled = 0;
1547         if (isrecover)
1548                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1549         task = rpc_run_task(&task_setup_data);
1550         if (IS_ERR(task))
1551                 return PTR_ERR(task);
1552         status = nfs4_wait_for_completion_rpc_task(task);
1553         if (status != 0) {
1554                 data->cancelled = 1;
1555                 smp_wmb();
1556         } else
1557                 status = data->rpc_status;
1558         rpc_put_task(task);
1559
1560         return status;
1561 }
1562
1563 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1564 {
1565         struct inode *dir = data->dir->d_inode;
1566         struct nfs_openres *o_res = &data->o_res;
1567         int status;
1568
1569         status = nfs4_run_open_task(data, 1);
1570         if (status != 0 || !data->rpc_done)
1571                 return status;
1572
1573         nfs_refresh_inode(dir, o_res->dir_attr);
1574
1575         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1576                 status = _nfs4_proc_open_confirm(data);
1577                 if (status != 0)
1578                         return status;
1579         }
1580
1581         return status;
1582 }
1583
1584 /*
1585  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1586  */
1587 static int _nfs4_proc_open(struct nfs4_opendata *data)
1588 {
1589         struct inode *dir = data->dir->d_inode;
1590         struct nfs_server *server = NFS_SERVER(dir);
1591         struct nfs_openargs *o_arg = &data->o_arg;
1592         struct nfs_openres *o_res = &data->o_res;
1593         int status;
1594
1595         status = nfs4_run_open_task(data, 0);
1596         if (status != 0 || !data->rpc_done)
1597                 return status;
1598
1599         if (o_arg->open_flags & O_CREAT) {
1600                 update_changeattr(dir, &o_res->cinfo);
1601                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1602         } else
1603                 nfs_refresh_inode(dir, o_res->dir_attr);
1604         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1605                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1606         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1607                 status = _nfs4_proc_open_confirm(data);
1608                 if (status != 0)
1609                         return status;
1610         }
1611         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1612                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1613         return 0;
1614 }
1615
1616 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1617 {
1618         unsigned int loop;
1619         int ret;
1620
1621         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1622                 ret = nfs4_wait_clnt_recover(clp);
1623                 if (ret != 0)
1624                         break;
1625                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1626                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1627                         break;
1628                 nfs4_schedule_state_manager(clp);
1629                 ret = -EIO;
1630         }
1631         return ret;
1632 }
1633
1634 static int nfs4_recover_expired_lease(struct nfs_server *server)
1635 {
1636         return nfs4_client_recover_expired_lease(server->nfs_client);
1637 }
1638
1639 /*
1640  * OPEN_EXPIRED:
1641  *      reclaim state on the server after a network partition.
1642  *      Assumes caller holds the appropriate lock
1643  */
1644 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1645 {
1646         struct nfs4_opendata *opendata;
1647         int ret;
1648
1649         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1650         if (IS_ERR(opendata))
1651                 return PTR_ERR(opendata);
1652         ret = nfs4_open_recover(opendata, state);
1653         if (ret == -ESTALE)
1654                 d_drop(ctx->path.dentry);
1655         nfs4_opendata_put(opendata);
1656         return ret;
1657 }
1658
1659 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1660 {
1661         struct nfs_server *server = NFS_SERVER(state->inode);
1662         struct nfs4_exception exception = { };
1663         int err;
1664
1665         do {
1666                 err = _nfs4_open_expired(ctx, state);
1667                 switch (err) {
1668                 default:
1669                         goto out;
1670                 case -NFS4ERR_GRACE:
1671                 case -NFS4ERR_DELAY:
1672                         nfs4_handle_exception(server, err, &exception);
1673                         err = 0;
1674                 }
1675         } while (exception.retry);
1676 out:
1677         return err;
1678 }
1679
1680 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1681 {
1682         struct nfs_open_context *ctx;
1683         int ret;
1684
1685         ctx = nfs4_state_find_open_context(state);
1686         if (IS_ERR(ctx))
1687                 return PTR_ERR(ctx);
1688         ret = nfs4_do_open_expired(ctx, state);
1689         put_nfs_open_context(ctx);
1690         return ret;
1691 }
1692
1693 #if defined(CONFIG_NFS_V4_1)
1694 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1695 {
1696         int status;
1697         struct nfs_server *server = NFS_SERVER(state->inode);
1698
1699         status = nfs41_test_stateid(server, state);
1700         if (status == NFS_OK)
1701                 return 0;
1702         nfs41_free_stateid(server, state);
1703         return nfs4_open_expired(sp, state);
1704 }
1705 #endif
1706
1707 /*
1708  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1709  * fields corresponding to attributes that were used to store the verifier.
1710  * Make sure we clobber those fields in the later setattr call
1711  */
1712 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1713 {
1714         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1715             !(sattr->ia_valid & ATTR_ATIME_SET))
1716                 sattr->ia_valid |= ATTR_ATIME;
1717
1718         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1719             !(sattr->ia_valid & ATTR_MTIME_SET))
1720                 sattr->ia_valid |= ATTR_MTIME;
1721 }
1722
1723 /*
1724  * Returns a referenced nfs4_state
1725  */
1726 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1727 {
1728         struct nfs4_state_owner  *sp;
1729         struct nfs4_state     *state = NULL;
1730         struct nfs_server       *server = NFS_SERVER(dir);
1731         struct nfs4_opendata *opendata;
1732         int status;
1733
1734         /* Protect against reboot recovery conflicts */
1735         status = -ENOMEM;
1736         if (!(sp = nfs4_get_state_owner(server, cred))) {
1737                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1738                 goto out_err;
1739         }
1740         status = nfs4_recover_expired_lease(server);
1741         if (status != 0)
1742                 goto err_put_state_owner;
1743         if (path->dentry->d_inode != NULL)
1744                 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1745         status = -ENOMEM;
1746         opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1747         if (opendata == NULL)
1748                 goto err_put_state_owner;
1749
1750         if (path->dentry->d_inode != NULL)
1751                 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1752
1753         status = _nfs4_proc_open(opendata);
1754         if (status != 0)
1755                 goto err_opendata_put;
1756
1757         state = nfs4_opendata_to_nfs4_state(opendata);
1758         status = PTR_ERR(state);
1759         if (IS_ERR(state))
1760                 goto err_opendata_put;
1761         if (server->caps & NFS_CAP_POSIX_LOCK)
1762                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1763
1764         if (opendata->o_arg.open_flags & O_EXCL) {
1765                 nfs4_exclusive_attrset(opendata, sattr);
1766
1767                 nfs_fattr_init(opendata->o_res.f_attr);
1768                 status = nfs4_do_setattr(state->inode, cred,
1769                                 opendata->o_res.f_attr, sattr,
1770                                 state);
1771                 if (status == 0)
1772                         nfs_setattr_update_inode(state->inode, sattr);
1773                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1774         }
1775         nfs4_opendata_put(opendata);
1776         nfs4_put_state_owner(sp);
1777         *res = state;
1778         return 0;
1779 err_opendata_put:
1780         nfs4_opendata_put(opendata);
1781 err_put_state_owner:
1782         nfs4_put_state_owner(sp);
1783 out_err:
1784         *res = NULL;
1785         return status;
1786 }
1787
1788
1789 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1790 {
1791         struct nfs4_exception exception = { };
1792         struct nfs4_state *res;
1793         int status;
1794
1795         do {
1796                 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1797                 if (status == 0)
1798                         break;
1799                 /* NOTE: BAD_SEQID means the server and client disagree about the
1800                  * book-keeping w.r.t. state-changing operations
1801                  * (OPEN/CLOSE/LOCK/LOCKU...)
1802                  * It is actually a sign of a bug on the client or on the server.
1803                  *
1804                  * If we receive a BAD_SEQID error in the particular case of
1805                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1806                  * have unhashed the old state_owner for us, and that we can
1807                  * therefore safely retry using a new one. We should still warn
1808                  * the user though...
1809                  */
1810                 if (status == -NFS4ERR_BAD_SEQID) {
1811                         printk(KERN_WARNING "NFS: v4 server %s "
1812                                         " returned a bad sequence-id error!\n",
1813                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1814                         exception.retry = 1;
1815                         continue;
1816                 }
1817                 /*
1818                  * BAD_STATEID on OPEN means that the server cancelled our
1819                  * state before it received the OPEN_CONFIRM.
1820                  * Recover by retrying the request as per the discussion
1821                  * on Page 181 of RFC3530.
1822                  */
1823                 if (status == -NFS4ERR_BAD_STATEID) {
1824                         exception.retry = 1;
1825                         continue;
1826                 }
1827                 if (status == -EAGAIN) {
1828                         /* We must have found a delegation */
1829                         exception.retry = 1;
1830                         continue;
1831                 }
1832                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1833                                         status, &exception));
1834         } while (exception.retry);
1835         return res;
1836 }
1837
1838 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1839                             struct nfs_fattr *fattr, struct iattr *sattr,
1840                             struct nfs4_state *state)
1841 {
1842         struct nfs_server *server = NFS_SERVER(inode);
1843         struct nfs_setattrargs  arg = {
1844                 .fh             = NFS_FH(inode),
1845                 .iap            = sattr,
1846                 .server         = server,
1847                 .bitmask = server->attr_bitmask,
1848         };
1849         struct nfs_setattrres  res = {
1850                 .fattr          = fattr,
1851                 .server         = server,
1852         };
1853         struct rpc_message msg = {
1854                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1855                 .rpc_argp       = &arg,
1856                 .rpc_resp       = &res,
1857                 .rpc_cred       = cred,
1858         };
1859         unsigned long timestamp = jiffies;
1860         int status;
1861
1862         nfs_fattr_init(fattr);
1863
1864         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1865                 /* Use that stateid */
1866         } else if (state != NULL) {
1867                 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1868         } else
1869                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1870
1871         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1872         if (status == 0 && state != NULL)
1873                 renew_lease(server, timestamp);
1874         return status;
1875 }
1876
1877 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1878                            struct nfs_fattr *fattr, struct iattr *sattr,
1879                            struct nfs4_state *state)
1880 {
1881         struct nfs_server *server = NFS_SERVER(inode);
1882         struct nfs4_exception exception = { };
1883         int err;
1884         do {
1885                 err = nfs4_handle_exception(server,
1886                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1887                                 &exception);
1888         } while (exception.retry);
1889         return err;
1890 }
1891
1892 struct nfs4_closedata {
1893         struct path path;
1894         struct inode *inode;
1895         struct nfs4_state *state;
1896         struct nfs_closeargs arg;
1897         struct nfs_closeres res;
1898         struct nfs_fattr fattr;
1899         unsigned long timestamp;
1900         bool roc;
1901         u32 roc_barrier;
1902 };
1903
1904 static void nfs4_free_closedata(void *data)
1905 {
1906         struct nfs4_closedata *calldata = data;
1907         struct nfs4_state_owner *sp = calldata->state->owner;
1908
1909         if (calldata->roc)
1910                 pnfs_roc_release(calldata->state->inode);
1911         nfs4_put_open_state(calldata->state);
1912         nfs_free_seqid(calldata->arg.seqid);
1913         nfs4_put_state_owner(sp);
1914         path_put(&calldata->path);
1915         kfree(calldata);
1916 }
1917
1918 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1919                 fmode_t fmode)
1920 {
1921         spin_lock(&state->owner->so_lock);
1922         if (!(fmode & FMODE_READ))
1923                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1924         if (!(fmode & FMODE_WRITE))
1925                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1926         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1927         spin_unlock(&state->owner->so_lock);
1928 }
1929
1930 static void nfs4_close_done(struct rpc_task *task, void *data)
1931 {
1932         struct nfs4_closedata *calldata = data;
1933         struct nfs4_state *state = calldata->state;
1934         struct nfs_server *server = NFS_SERVER(calldata->inode);
1935
1936         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1937                 return;
1938         /* hmm. we are done with the inode, and in the process of freeing
1939          * the state_owner. we keep this around to process errors
1940          */
1941         switch (task->tk_status) {
1942                 case 0:
1943                         if (calldata->roc)
1944                                 pnfs_roc_set_barrier(state->inode,
1945                                                      calldata->roc_barrier);
1946                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1947                         renew_lease(server, calldata->timestamp);
1948                         nfs4_close_clear_stateid_flags(state,
1949                                         calldata->arg.fmode);
1950                         break;
1951                 case -NFS4ERR_STALE_STATEID:
1952                 case -NFS4ERR_OLD_STATEID:
1953                 case -NFS4ERR_BAD_STATEID:
1954                 case -NFS4ERR_EXPIRED:
1955                         if (calldata->arg.fmode == 0)
1956                                 break;
1957                 default:
1958                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1959                                 rpc_restart_call_prepare(task);
1960         }
1961         nfs_release_seqid(calldata->arg.seqid);
1962         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1963 }
1964
1965 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1966 {
1967         struct nfs4_closedata *calldata = data;
1968         struct nfs4_state *state = calldata->state;
1969         int call_close = 0;
1970
1971         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1972                 return;
1973
1974         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1975         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1976         spin_lock(&state->owner->so_lock);
1977         /* Calculate the change in open mode */
1978         if (state->n_rdwr == 0) {
1979                 if (state->n_rdonly == 0) {
1980                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1981                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1982                         calldata->arg.fmode &= ~FMODE_READ;
1983                 }
1984                 if (state->n_wronly == 0) {
1985                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1986                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1987                         calldata->arg.fmode &= ~FMODE_WRITE;
1988                 }
1989         }
1990         spin_unlock(&state->owner->so_lock);
1991
1992         if (!call_close) {
1993                 /* Note: exit _without_ calling nfs4_close_done */
1994                 task->tk_action = NULL;
1995                 return;
1996         }
1997
1998         if (calldata->arg.fmode == 0) {
1999                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2000                 if (calldata->roc &&
2001                     pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2002                         rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2003                                      task, NULL);
2004                         return;
2005                 }
2006         }
2007
2008         nfs_fattr_init(calldata->res.fattr);
2009         calldata->timestamp = jiffies;
2010         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2011                                 &calldata->arg.seq_args, &calldata->res.seq_res,
2012                                 1, task))
2013                 return;
2014         rpc_call_start(task);
2015 }
2016
2017 static const struct rpc_call_ops nfs4_close_ops = {
2018         .rpc_call_prepare = nfs4_close_prepare,
2019         .rpc_call_done = nfs4_close_done,
2020         .rpc_release = nfs4_free_closedata,
2021 };
2022
2023 /* 
2024  * It is possible for data to be read/written from a mem-mapped file 
2025  * after the sys_close call (which hits the vfs layer as a flush).
2026  * This means that we can't safely call nfsv4 close on a file until 
2027  * the inode is cleared. This in turn means that we are not good
2028  * NFSv4 citizens - we do not indicate to the server to update the file's 
2029  * share state even when we are done with one of the three share 
2030  * stateid's in the inode.
2031  *
2032  * NOTE: Caller must be holding the sp->so_owner semaphore!
2033  */
2034 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2035 {
2036         struct nfs_server *server = NFS_SERVER(state->inode);
2037         struct nfs4_closedata *calldata;
2038         struct nfs4_state_owner *sp = state->owner;
2039         struct rpc_task *task;
2040         struct rpc_message msg = {
2041                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2042                 .rpc_cred = state->owner->so_cred,
2043         };
2044         struct rpc_task_setup task_setup_data = {
2045                 .rpc_client = server->client,
2046                 .rpc_message = &msg,
2047                 .callback_ops = &nfs4_close_ops,
2048                 .workqueue = nfsiod_workqueue,
2049                 .flags = RPC_TASK_ASYNC,
2050         };
2051         int status = -ENOMEM;
2052
2053         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2054         if (calldata == NULL)
2055                 goto out;
2056         calldata->inode = state->inode;
2057         calldata->state = state;
2058         calldata->arg.fh = NFS_FH(state->inode);
2059         calldata->arg.stateid = &state->open_stateid;
2060         /* Serialization for the sequence id */
2061         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2062         if (calldata->arg.seqid == NULL)
2063                 goto out_free_calldata;
2064         calldata->arg.fmode = 0;
2065         calldata->arg.bitmask = server->cache_consistency_bitmask;
2066         calldata->res.fattr = &calldata->fattr;
2067         calldata->res.seqid = calldata->arg.seqid;
2068         calldata->res.server = server;
2069         calldata->roc = roc;
2070         path_get(path);
2071         calldata->path = *path;
2072
2073         msg.rpc_argp = &calldata->arg;
2074         msg.rpc_resp = &calldata->res;
2075         task_setup_data.callback_data = calldata;
2076         task = rpc_run_task(&task_setup_data);
2077         if (IS_ERR(task))
2078                 return PTR_ERR(task);
2079         status = 0;
2080         if (wait)
2081                 status = rpc_wait_for_completion_task(task);
2082         rpc_put_task(task);
2083         return status;
2084 out_free_calldata:
2085         kfree(calldata);
2086 out:
2087         if (roc)
2088                 pnfs_roc_release(state->inode);
2089         nfs4_put_open_state(state);
2090         nfs4_put_state_owner(sp);
2091         return status;
2092 }
2093
2094 static struct inode *
2095 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2096 {
2097         struct nfs4_state *state;
2098
2099         /* Protect against concurrent sillydeletes */
2100         state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2101         if (IS_ERR(state))
2102                 return ERR_CAST(state);
2103         ctx->state = state;
2104         return igrab(state->inode);
2105 }
2106
2107 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2108 {
2109         if (ctx->state == NULL)
2110                 return;
2111         if (is_sync)
2112                 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2113         else
2114                 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2115 }
2116
2117 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2118 {
2119         struct nfs4_server_caps_arg args = {
2120                 .fhandle = fhandle,
2121         };
2122         struct nfs4_server_caps_res res = {};
2123         struct rpc_message msg = {
2124                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2125                 .rpc_argp = &args,
2126                 .rpc_resp = &res,
2127         };
2128         int status;
2129
2130         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2131         if (status == 0) {
2132                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2133                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2134                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2135                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2136                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2137                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2138                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2139                         server->caps |= NFS_CAP_ACLS;
2140                 if (res.has_links != 0)
2141                         server->caps |= NFS_CAP_HARDLINKS;
2142                 if (res.has_symlinks != 0)
2143                         server->caps |= NFS_CAP_SYMLINKS;
2144                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2145                         server->caps |= NFS_CAP_FILEID;
2146                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2147                         server->caps |= NFS_CAP_MODE;
2148                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2149                         server->caps |= NFS_CAP_NLINK;
2150                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2151                         server->caps |= NFS_CAP_OWNER;
2152                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2153                         server->caps |= NFS_CAP_OWNER_GROUP;
2154                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2155                         server->caps |= NFS_CAP_ATIME;
2156                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2157                         server->caps |= NFS_CAP_CTIME;
2158                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2159                         server->caps |= NFS_CAP_MTIME;
2160
2161                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2162                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2163                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2164                 server->acl_bitmask = res.acl_bitmask;
2165         }
2166
2167         return status;
2168 }
2169
2170 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2171 {
2172         struct nfs4_exception exception = { };
2173         int err;
2174         do {
2175                 err = nfs4_handle_exception(server,
2176                                 _nfs4_server_capabilities(server, fhandle),
2177                                 &exception);
2178         } while (exception.retry);
2179         return err;
2180 }
2181
2182 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183                 struct nfs_fsinfo *info)
2184 {
2185         struct nfs4_lookup_root_arg args = {
2186                 .bitmask = nfs4_fattr_bitmap,
2187         };
2188         struct nfs4_lookup_res res = {
2189                 .server = server,
2190                 .fattr = info->fattr,
2191                 .fh = fhandle,
2192         };
2193         struct rpc_message msg = {
2194                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2195                 .rpc_argp = &args,
2196                 .rpc_resp = &res,
2197         };
2198
2199         nfs_fattr_init(info->fattr);
2200         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2201 }
2202
2203 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2204                 struct nfs_fsinfo *info)
2205 {
2206         struct nfs4_exception exception = { };
2207         int err;
2208         do {
2209                 err = _nfs4_lookup_root(server, fhandle, info);
2210                 switch (err) {
2211                 case 0:
2212                 case -NFS4ERR_WRONGSEC:
2213                         break;
2214                 default:
2215                         err = nfs4_handle_exception(server, err, &exception);
2216                 }
2217         } while (exception.retry);
2218         return err;
2219 }
2220
2221 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2222                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2223 {
2224         struct rpc_auth *auth;
2225         int ret;
2226
2227         auth = rpcauth_create(flavor, server->client);
2228         if (!auth) {
2229                 ret = -EIO;
2230                 goto out;
2231         }
2232         ret = nfs4_lookup_root(server, fhandle, info);
2233 out:
2234         return ret;
2235 }
2236
2237 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2238                               struct nfs_fsinfo *info)
2239 {
2240         int i, len, status = 0;
2241         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2242
2243         len = gss_mech_list_pseudoflavors(&flav_array[0]);
2244         flav_array[len] = RPC_AUTH_NULL;
2245         len += 1;
2246
2247         for (i = 0; i < len; i++) {
2248                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2249                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2250                         continue;
2251                 break;
2252         }
2253         /*
2254          * -EACCESS could mean that the user doesn't have correct permissions
2255          * to access the mount.  It could also mean that we tried to mount
2256          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2257          * existing mount programs don't handle -EACCES very well so it should
2258          * be mapped to -EPERM instead.
2259          */
2260         if (status == -EACCES)
2261                 status = -EPERM;
2262         return status;
2263 }
2264
2265 /*
2266  * get the file handle for the "/" directory on the server
2267  */
2268 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2269                               struct nfs_fsinfo *info)
2270 {
2271         int minor_version = server->nfs_client->cl_minorversion;
2272         int status = nfs4_lookup_root(server, fhandle, info);
2273         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2274                 /*
2275                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2276                  * by nfs4_map_errors() as this function exits.
2277                  */
2278                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2279         if (status == 0)
2280                 status = nfs4_server_capabilities(server, fhandle);
2281         if (status == 0)
2282                 status = nfs4_do_fsinfo(server, fhandle, info);
2283         return nfs4_map_errors(status);
2284 }
2285
2286 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2287 /*
2288  * Get locations and (maybe) other attributes of a referral.
2289  * Note that we'll actually follow the referral later when
2290  * we detect fsid mismatch in inode revalidation
2291  */
2292 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2293                              struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2294 {
2295         int status = -ENOMEM;
2296         struct page *page = NULL;
2297         struct nfs4_fs_locations *locations = NULL;
2298
2299         page = alloc_page(GFP_KERNEL);
2300         if (page == NULL)
2301                 goto out;
2302         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2303         if (locations == NULL)
2304                 goto out;
2305
2306         status = nfs4_proc_fs_locations(dir, name, locations, page);
2307         if (status != 0)
2308                 goto out;
2309         /* Make sure server returned a different fsid for the referral */
2310         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2311                 dprintk("%s: server did not return a different fsid for"
2312                         " a referral at %s\n", __func__, name->name);
2313                 status = -EIO;
2314                 goto out;
2315         }
2316         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2317         nfs_fixup_referral_attributes(&locations->fattr);
2318
2319         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2320         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2321         memset(fhandle, 0, sizeof(struct nfs_fh));
2322 out:
2323         if (page)
2324                 __free_page(page);
2325         kfree(locations);
2326         return status;
2327 }
2328
2329 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2330 {
2331         struct nfs4_getattr_arg args = {
2332                 .fh = fhandle,
2333                 .bitmask = server->attr_bitmask,
2334         };
2335         struct nfs4_getattr_res res = {
2336                 .fattr = fattr,
2337                 .server = server,
2338         };
2339         struct rpc_message msg = {
2340                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2341                 .rpc_argp = &args,
2342                 .rpc_resp = &res,
2343         };
2344         
2345         nfs_fattr_init(fattr);
2346         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2347 }
2348
2349 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2350 {
2351         struct nfs4_exception exception = { };
2352         int err;
2353         do {
2354                 err = nfs4_handle_exception(server,
2355                                 _nfs4_proc_getattr(server, fhandle, fattr),
2356                                 &exception);
2357         } while (exception.retry);
2358         return err;
2359 }
2360
2361 /* 
2362  * The file is not closed if it is opened due to the a request to change
2363  * the size of the file. The open call will not be needed once the
2364  * VFS layer lookup-intents are implemented.
2365  *
2366  * Close is called when the inode is destroyed.
2367  * If we haven't opened the file for O_WRONLY, we
2368  * need to in the size_change case to obtain a stateid.
2369  *
2370  * Got race?
2371  * Because OPEN is always done by name in nfsv4, it is
2372  * possible that we opened a different file by the same
2373  * name.  We can recognize this race condition, but we
2374  * can't do anything about it besides returning an error.
2375  *
2376  * This will be fixed with VFS changes (lookup-intent).
2377  */
2378 static int
2379 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2380                   struct iattr *sattr)
2381 {
2382         struct inode *inode = dentry->d_inode;
2383         struct rpc_cred *cred = NULL;
2384         struct nfs4_state *state = NULL;
2385         int status;
2386
2387         if (pnfs_ld_layoutret_on_setattr(inode))
2388                 pnfs_return_layout(inode);
2389
2390         nfs_fattr_init(fattr);
2391         
2392         /* Search for an existing open(O_WRITE) file */
2393         if (sattr->ia_valid & ATTR_FILE) {
2394                 struct nfs_open_context *ctx;
2395
2396                 ctx = nfs_file_open_context(sattr->ia_file);
2397                 if (ctx) {
2398                         cred = ctx->cred;
2399                         state = ctx->state;
2400                 }
2401         }
2402
2403         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2404         if (status == 0)
2405                 nfs_setattr_update_inode(inode, sattr);
2406         return status;
2407 }
2408
2409 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2410                 const struct nfs_fh *dirfh, const struct qstr *name,
2411                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2412 {
2413         int                    status;
2414         struct nfs4_lookup_arg args = {
2415                 .bitmask = server->attr_bitmask,
2416                 .dir_fh = dirfh,
2417                 .name = name,
2418         };
2419         struct nfs4_lookup_res res = {
2420                 .server = server,
2421                 .fattr = fattr,
2422                 .fh = fhandle,
2423         };
2424         struct rpc_message msg = {
2425                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2426                 .rpc_argp = &args,
2427                 .rpc_resp = &res,
2428         };
2429
2430         nfs_fattr_init(fattr);
2431
2432         dprintk("NFS call  lookupfh %s\n", name->name);
2433         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2434         dprintk("NFS reply lookupfh: %d\n", status);
2435         return status;
2436 }
2437
2438 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2439                               struct qstr *name, struct nfs_fh *fhandle,
2440                               struct nfs_fattr *fattr)
2441 {
2442         struct nfs4_exception exception = { };
2443         int err;
2444         do {
2445                 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2446                 /* FIXME: !!!! */
2447                 if (err == -NFS4ERR_MOVED) {
2448                         err = -EREMOTE;
2449                         break;
2450                 }
2451                 err = nfs4_handle_exception(server, err, &exception);
2452         } while (exception.retry);
2453         return err;
2454 }
2455
2456 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2457                 const struct qstr *name, struct nfs_fh *fhandle,
2458                 struct nfs_fattr *fattr)
2459 {
2460         int status;
2461         
2462         dprintk("NFS call  lookup %s\n", name->name);
2463         status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2464         if (status == -NFS4ERR_MOVED)
2465                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2466         dprintk("NFS reply lookup: %d\n", status);
2467         return status;
2468 }
2469
2470 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2471 {
2472         memset(fh, 0, sizeof(struct nfs_fh));
2473         fattr->fsid.major = 1;
2474         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2475                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2476         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2477         fattr->nlink = 2;
2478 }
2479
2480 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2481                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2482 {
2483         struct nfs4_exception exception = { };
2484         int err;
2485         do {
2486                 err = nfs4_handle_exception(NFS_SERVER(dir),
2487                                 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2488                                 &exception);
2489                 if (err == -EPERM)
2490                         nfs_fixup_secinfo_attributes(fattr, fhandle);
2491         } while (exception.retry);
2492         return err;
2493 }
2494
2495 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2496 {
2497         struct nfs_server *server = NFS_SERVER(inode);
2498         struct nfs4_accessargs args = {
2499                 .fh = NFS_FH(inode),
2500                 .bitmask = server->attr_bitmask,
2501         };
2502         struct nfs4_accessres res = {
2503                 .server = server,
2504         };
2505         struct rpc_message msg = {
2506                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2507                 .rpc_argp = &args,
2508                 .rpc_resp = &res,
2509                 .rpc_cred = entry->cred,
2510         };
2511         int mode = entry->mask;
2512         int status;
2513
2514         /*
2515          * Determine which access bits we want to ask for...
2516          */
2517         if (mode & MAY_READ)
2518                 args.access |= NFS4_ACCESS_READ;
2519         if (S_ISDIR(inode->i_mode)) {
2520                 if (mode & MAY_WRITE)
2521                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2522                 if (mode & MAY_EXEC)
2523                         args.access |= NFS4_ACCESS_LOOKUP;
2524         } else {
2525                 if (mode & MAY_WRITE)
2526                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2527                 if (mode & MAY_EXEC)
2528                         args.access |= NFS4_ACCESS_EXECUTE;
2529         }
2530
2531         res.fattr = nfs_alloc_fattr();
2532         if (res.fattr == NULL)
2533                 return -ENOMEM;
2534
2535         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2536         if (!status) {
2537                 entry->mask = 0;
2538                 if (res.access & NFS4_ACCESS_READ)
2539                         entry->mask |= MAY_READ;
2540                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2541                         entry->mask |= MAY_WRITE;
2542                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2543                         entry->mask |= MAY_EXEC;
2544                 nfs_refresh_inode(inode, res.fattr);
2545         }
2546         nfs_free_fattr(res.fattr);
2547         return status;
2548 }
2549
2550 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2551 {
2552         struct nfs4_exception exception = { };
2553         int err;
2554         do {
2555                 err = nfs4_handle_exception(NFS_SERVER(inode),
2556                                 _nfs4_proc_access(inode, entry),
2557                                 &exception);
2558         } while (exception.retry);
2559         return err;
2560 }
2561
2562 /*
2563  * TODO: For the time being, we don't try to get any attributes
2564  * along with any of the zero-copy operations READ, READDIR,
2565  * READLINK, WRITE.
2566  *
2567  * In the case of the first three, we want to put the GETATTR
2568  * after the read-type operation -- this is because it is hard
2569  * to predict the length of a GETATTR response in v4, and thus
2570  * align the READ data correctly.  This means that the GETATTR
2571  * may end up partially falling into the page cache, and we should
2572  * shift it into the 'tail' of the xdr_buf before processing.
2573  * To do this efficiently, we need to know the total length
2574  * of data received, which doesn't seem to be available outside
2575  * of the RPC layer.
2576  *
2577  * In the case of WRITE, we also want to put the GETATTR after
2578  * the operation -- in this case because we want to make sure
2579  * we get the post-operation mtime and size.  This means that
2580  * we can't use xdr_encode_pages() as written: we need a variant
2581  * of it which would leave room in the 'tail' iovec.
2582  *
2583  * Both of these changes to the XDR layer would in fact be quite
2584  * minor, but I decided to leave them for a subsequent patch.
2585  */
2586 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2587                 unsigned int pgbase, unsigned int pglen)
2588 {
2589         struct nfs4_readlink args = {
2590                 .fh       = NFS_FH(inode),
2591                 .pgbase   = pgbase,
2592                 .pglen    = pglen,
2593                 .pages    = &page,
2594         };
2595         struct nfs4_readlink_res res;
2596         struct rpc_message msg = {
2597                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2598                 .rpc_argp = &args,
2599                 .rpc_resp = &res,
2600         };
2601
2602         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2603 }
2604
2605 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2606                 unsigned int pgbase, unsigned int pglen)
2607 {
2608         struct nfs4_exception exception = { };
2609         int err;
2610         do {
2611                 err = nfs4_handle_exception(NFS_SERVER(inode),
2612                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2613                                 &exception);
2614         } while (exception.retry);
2615         return err;
2616 }
2617
2618 /*
2619  * Got race?
2620  * We will need to arrange for the VFS layer to provide an atomic open.
2621  * Until then, this create/open method is prone to inefficiency and race
2622  * conditions due to the lookup, create, and open VFS calls from sys_open()
2623  * placed on the wire.
2624  *
2625  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2626  * The file will be opened again in the subsequent VFS open call
2627  * (nfs4_proc_file_open).
2628  *
2629  * The open for read will just hang around to be used by any process that
2630  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2631  */
2632
2633 static int
2634 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2635                  int flags, struct nfs_open_context *ctx)
2636 {
2637         struct path my_path = {
2638                 .dentry = dentry,
2639         };
2640         struct path *path = &my_path;
2641         struct nfs4_state *state;
2642         struct rpc_cred *cred = NULL;
2643         fmode_t fmode = 0;
2644         int status = 0;
2645
2646         if (ctx != NULL) {
2647                 cred = ctx->cred;
2648                 path = &ctx->path;
2649                 fmode = ctx->mode;
2650         }
2651         sattr->ia_mode &= ~current_umask();
2652         state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2653         d_drop(dentry);
2654         if (IS_ERR(state)) {
2655                 status = PTR_ERR(state);
2656                 goto out;
2657         }
2658         d_add(dentry, igrab(state->inode));
2659         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2660         if (ctx != NULL)
2661                 ctx->state = state;
2662         else
2663                 nfs4_close_sync(path, state, fmode);
2664 out:
2665         return status;
2666 }
2667
2668 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2669 {
2670         struct nfs_server *server = NFS_SERVER(dir);
2671         struct nfs_removeargs args = {
2672                 .fh = NFS_FH(dir),
2673                 .name.len = name->len,
2674                 .name.name = name->name,
2675                 .bitmask = server->attr_bitmask,
2676         };
2677         struct nfs_removeres res = {
2678                 .server = server,
2679         };
2680         struct rpc_message msg = {
2681                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2682                 .rpc_argp = &args,
2683                 .rpc_resp = &res,
2684         };
2685         int status = -ENOMEM;
2686
2687         res.dir_attr = nfs_alloc_fattr();
2688         if (res.dir_attr == NULL)
2689                 goto out;
2690
2691         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2692         if (status == 0) {
2693                 update_changeattr(dir, &res.cinfo);
2694                 nfs_post_op_update_inode(dir, res.dir_attr);
2695         }
2696         nfs_free_fattr(res.dir_attr);
2697 out:
2698         return status;
2699 }
2700
2701 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2702 {
2703         struct nfs4_exception exception = { };
2704         int err;
2705         do {
2706                 err = nfs4_handle_exception(NFS_SERVER(dir),
2707                                 _nfs4_proc_remove(dir, name),
2708                                 &exception);
2709         } while (exception.retry);
2710         return err;
2711 }
2712
2713 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2714 {
2715         struct nfs_server *server = NFS_SERVER(dir);
2716         struct nfs_removeargs *args = msg->rpc_argp;
2717         struct nfs_removeres *res = msg->rpc_resp;
2718
2719         args->bitmask = server->cache_consistency_bitmask;
2720         res->server = server;
2721         res->seq_res.sr_slot = NULL;
2722         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2723 }
2724
2725 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2726 {
2727         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2728
2729         if (!nfs4_sequence_done(task, &res->seq_res))
2730                 return 0;
2731         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2732                 return 0;
2733         update_changeattr(dir, &res->cinfo);
2734         nfs_post_op_update_inode(dir, res->dir_attr);
2735         return 1;
2736 }
2737
2738 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2739 {
2740         struct nfs_server *server = NFS_SERVER(dir);
2741         struct nfs_renameargs *arg = msg->rpc_argp;
2742         struct nfs_renameres *res = msg->rpc_resp;
2743
2744         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2745         arg->bitmask = server->attr_bitmask;
2746         res->server = server;
2747 }
2748
2749 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2750                                  struct inode *new_dir)
2751 {
2752         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2753
2754         if (!nfs4_sequence_done(task, &res->seq_res))
2755                 return 0;
2756         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2757                 return 0;
2758
2759         update_changeattr(old_dir, &res->old_cinfo);
2760         nfs_post_op_update_inode(old_dir, res->old_fattr);
2761         update_changeattr(new_dir, &res->new_cinfo);
2762         nfs_post_op_update_inode(new_dir, res->new_fattr);
2763         return 1;
2764 }
2765
2766 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2767                 struct inode *new_dir, struct qstr *new_name)
2768 {
2769         struct nfs_server *server = NFS_SERVER(old_dir);
2770         struct nfs_renameargs arg = {
2771                 .old_dir = NFS_FH(old_dir),
2772                 .new_dir = NFS_FH(new_dir),
2773                 .old_name = old_name,
2774                 .new_name = new_name,
2775                 .bitmask = server->attr_bitmask,
2776         };
2777         struct nfs_renameres res = {
2778                 .server = server,
2779         };
2780         struct rpc_message msg = {
2781                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2782                 .rpc_argp = &arg,
2783                 .rpc_resp = &res,
2784         };
2785         int status = -ENOMEM;
2786         
2787         res.old_fattr = nfs_alloc_fattr();
2788         res.new_fattr = nfs_alloc_fattr();
2789         if (res.old_fattr == NULL || res.new_fattr == NULL)
2790                 goto out;
2791
2792         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2793         if (!status) {
2794                 update_changeattr(old_dir, &res.old_cinfo);
2795                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2796                 update_changeattr(new_dir, &res.new_cinfo);
2797                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2798         }
2799 out:
2800         nfs_free_fattr(res.new_fattr);
2801         nfs_free_fattr(res.old_fattr);
2802         return status;
2803 }
2804
2805 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2806                 struct inode *new_dir, struct qstr *new_name)
2807 {
2808         struct nfs4_exception exception = { };
2809         int err;
2810         do {
2811                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2812                                 _nfs4_proc_rename(old_dir, old_name,
2813                                         new_dir, new_name),
2814                                 &exception);
2815         } while (exception.retry);
2816         return err;
2817 }
2818
2819 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2820 {
2821         struct nfs_server *server = NFS_SERVER(inode);
2822         struct nfs4_link_arg arg = {
2823                 .fh     = NFS_FH(inode),
2824                 .dir_fh = NFS_FH(dir),
2825                 .name   = name,
2826                 .bitmask = server->attr_bitmask,
2827         };
2828         struct nfs4_link_res res = {
2829                 .server = server,
2830         };
2831         struct rpc_message msg = {
2832                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2833                 .rpc_argp = &arg,
2834                 .rpc_resp = &res,
2835         };
2836         int status = -ENOMEM;
2837
2838         res.fattr = nfs_alloc_fattr();
2839         res.dir_attr = nfs_alloc_fattr();
2840         if (res.fattr == NULL || res.dir_attr == NULL)
2841                 goto out;
2842
2843         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2844         if (!status) {
2845                 update_changeattr(dir, &res.cinfo);
2846                 nfs_post_op_update_inode(dir, res.dir_attr);
2847                 nfs_post_op_update_inode(inode, res.fattr);
2848         }
2849 out:
2850         nfs_free_fattr(res.dir_attr);
2851         nfs_free_fattr(res.fattr);
2852         return status;
2853 }
2854
2855 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2856 {
2857         struct nfs4_exception exception = { };
2858         int err;
2859         do {
2860                 err = nfs4_handle_exception(NFS_SERVER(inode),
2861                                 _nfs4_proc_link(inode, dir, name),
2862                                 &exception);
2863         } while (exception.retry);
2864         return err;
2865 }
2866
2867 struct nfs4_createdata {
2868         struct rpc_message msg;
2869         struct nfs4_create_arg arg;
2870         struct nfs4_create_res res;
2871         struct nfs_fh fh;
2872         struct nfs_fattr fattr;
2873         struct nfs_fattr dir_fattr;
2874 };
2875
2876 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2877                 struct qstr *name, struct iattr *sattr, u32 ftype)
2878 {
2879         struct nfs4_createdata *data;
2880
2881         data = kzalloc(sizeof(*data), GFP_KERNEL);
2882         if (data != NULL) {
2883                 struct nfs_server *server = NFS_SERVER(dir);
2884
2885                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2886                 data->msg.rpc_argp = &data->arg;
2887                 data->msg.rpc_resp = &data->res;
2888                 data->arg.dir_fh = NFS_FH(dir);
2889                 data->arg.server = server;
2890                 data->arg.name = name;
2891                 data->arg.attrs = sattr;
2892                 data->arg.ftype = ftype;
2893                 data->arg.bitmask = server->attr_bitmask;
2894                 data->res.server = server;
2895                 data->res.fh = &data->fh;
2896                 data->res.fattr = &data->fattr;
2897                 data->res.dir_fattr = &data->dir_fattr;
2898                 nfs_fattr_init(data->res.fattr);
2899                 nfs_fattr_init(data->res.dir_fattr);
2900         }
2901         return data;
2902 }
2903
2904 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2905 {
2906         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2907                                     &data->arg.seq_args, &data->res.seq_res, 1);
2908         if (status == 0) {
2909                 update_changeattr(dir, &data->res.dir_cinfo);
2910                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2911                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2912         }
2913         return status;
2914 }
2915
2916 static void nfs4_free_createdata(struct nfs4_createdata *data)
2917 {
2918         kfree(data);
2919 }
2920
2921 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2922                 struct page *page, unsigned int len, struct iattr *sattr)
2923 {
2924         struct nfs4_createdata *data;
2925         int status = -ENAMETOOLONG;
2926
2927         if (len > NFS4_MAXPATHLEN)
2928                 goto out;
2929
2930         status = -ENOMEM;
2931         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2932         if (data == NULL)
2933                 goto out;
2934
2935         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2936         data->arg.u.symlink.pages = &page;
2937         data->arg.u.symlink.len = len;
2938         
2939         status = nfs4_do_create(dir, dentry, data);
2940
2941         nfs4_free_createdata(data);
2942 out:
2943         return status;
2944 }
2945
2946 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2947                 struct page *page, unsigned int len, struct iattr *sattr)
2948 {
2949         struct nfs4_exception exception = { };
2950         int err;
2951         do {
2952                 err = nfs4_handle_exception(NFS_SERVER(dir),
2953                                 _nfs4_proc_symlink(dir, dentry, page,
2954                                                         len, sattr),
2955                                 &exception);
2956         } while (exception.retry);
2957         return err;
2958 }
2959
2960 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2961                 struct iattr *sattr)
2962 {
2963         struct nfs4_createdata *data;
2964         int status = -ENOMEM;
2965
2966         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2967         if (data == NULL)
2968                 goto out;
2969
2970         status = nfs4_do_create(dir, dentry, data);
2971
2972         nfs4_free_createdata(data);
2973 out:
2974         return status;
2975 }
2976
2977 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2978                 struct iattr *sattr)
2979 {
2980         struct nfs4_exception exception = { };
2981         int err;
2982
2983         sattr->ia_mode &= ~current_umask();
2984         do {
2985                 err = nfs4_handle_exception(NFS_SERVER(dir),
2986                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2987                                 &exception);
2988         } while (exception.retry);
2989         return err;
2990 }
2991
2992 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2993                 u64 cookie, struct page **pages, unsigned int count, int plus)
2994 {
2995         struct inode            *dir = dentry->d_inode;
2996         struct nfs4_readdir_arg args = {
2997                 .fh = NFS_FH(dir),
2998                 .pages = pages,
2999                 .pgbase = 0,
3000                 .count = count,
3001                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3002                 .plus = plus,
3003         };
3004         struct nfs4_readdir_res res;
3005         struct rpc_message msg = {
3006                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3007                 .rpc_argp = &args,
3008                 .rpc_resp = &res,
3009                 .rpc_cred = cred,
3010         };
3011         int                     status;
3012
3013         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3014                         dentry->d_parent->d_name.name,
3015                         dentry->d_name.name,
3016                         (unsigned long long)cookie);
3017         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3018         res.pgbase = args.pgbase;
3019         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3020         if (status >= 0) {
3021                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3022                 status += args.pgbase;
3023         }
3024
3025         nfs_invalidate_atime(dir);
3026
3027         dprintk("%s: returns %d\n", __func__, status);
3028         return status;
3029 }
3030
3031 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3032                 u64 cookie, struct page **pages, unsigned int count, int plus)
3033 {
3034         struct nfs4_exception exception = { };
3035         int err;
3036         do {
3037                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3038                                 _nfs4_proc_readdir(dentry, cred, cookie,
3039                                         pages, count, plus),
3040                                 &exception);
3041         } while (exception.retry);
3042         return err;
3043 }
3044
3045 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3046                 struct iattr *sattr, dev_t rdev)
3047 {
3048         struct nfs4_createdata *data;
3049         int mode = sattr->ia_mode;
3050         int status = -ENOMEM;
3051
3052         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3053         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3054
3055         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3056         if (data == NULL)
3057                 goto out;
3058
3059         if (S_ISFIFO(mode))
3060                 data->arg.ftype = NF4FIFO;
3061         else if (S_ISBLK(mode)) {
3062                 data->arg.ftype = NF4BLK;
3063                 data->arg.u.device.specdata1 = MAJOR(rdev);
3064                 data->arg.u.device.specdata2 = MINOR(rdev);
3065         }
3066         else if (S_ISCHR(mode)) {
3067                 data->arg.ftype = NF4CHR;
3068                 data->arg.u.device.specdata1 = MAJOR(rdev);
3069                 data->arg.u.device.specdata2 = MINOR(rdev);
3070         }
3071         
3072         status = nfs4_do_create(dir, dentry, data);
3073
3074         nfs4_free_createdata(data);
3075 out:
3076         return status;
3077 }
3078
3079 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3080                 struct iattr *sattr, dev_t rdev)
3081 {
3082         struct nfs4_exception exception = { };
3083         int err;
3084
3085         sattr->ia_mode &= ~current_umask();
3086         do {
3087                 err = nfs4_handle_exception(NFS_SERVER(dir),
3088                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3089                                 &exception);
3090         } while (exception.retry);
3091         return err;
3092 }
3093
3094 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3095                  struct nfs_fsstat *fsstat)
3096 {
3097         struct nfs4_statfs_arg args = {
3098                 .fh = fhandle,
3099                 .bitmask = server->attr_bitmask,
3100         };
3101         struct nfs4_statfs_res res = {
3102                 .fsstat = fsstat,
3103         };
3104         struct rpc_message msg = {
3105                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3106                 .rpc_argp = &args,
3107                 .rpc_resp = &res,
3108         };
3109
3110         nfs_fattr_init(fsstat->fattr);
3111         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3112 }
3113
3114 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3115 {
3116         struct nfs4_exception exception = { };
3117         int err;
3118         do {
3119                 err = nfs4_handle_exception(server,
3120                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3121                                 &exception);
3122         } while (exception.retry);
3123         return err;
3124 }
3125
3126 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3127                 struct nfs_fsinfo *fsinfo)
3128 {
3129         struct nfs4_fsinfo_arg args = {
3130                 .fh = fhandle,
3131                 .bitmask = server->attr_bitmask,
3132         };
3133         struct nfs4_fsinfo_res res = {
3134                 .fsinfo = fsinfo,
3135         };
3136         struct rpc_message msg = {
3137                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3138                 .rpc_argp = &args,
3139                 .rpc_resp = &res,
3140         };
3141
3142         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3143 }
3144
3145 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3146 {
3147         struct nfs4_exception exception = { };
3148         int err;
3149
3150         do {
3151                 err = nfs4_handle_exception(server,
3152                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3153                                 &exception);
3154         } while (exception.retry);
3155         return err;
3156 }
3157
3158 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3159 {
3160         nfs_fattr_init(fsinfo->fattr);
3161         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3162 }
3163
3164 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3165                 struct nfs_pathconf *pathconf)
3166 {
3167         struct nfs4_pathconf_arg args = {
3168                 .fh = fhandle,
3169                 .bitmask = server->attr_bitmask,
3170         };
3171         struct nfs4_pathconf_res res = {
3172                 .pathconf = pathconf,
3173         };
3174         struct rpc_message msg = {
3175                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3176                 .rpc_argp = &args,
3177                 .rpc_resp = &res,
3178         };
3179
3180         /* None of the pathconf attributes are mandatory to implement */
3181         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3182                 memset(pathconf, 0, sizeof(*pathconf));
3183                 return 0;
3184         }
3185
3186         nfs_fattr_init(pathconf->fattr);
3187         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3188 }
3189
3190 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3191                 struct nfs_pathconf *pathconf)
3192 {
3193         struct nfs4_exception exception = { };
3194         int err;
3195
3196         do {
3197                 err = nfs4_handle_exception(server,
3198                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3199                                 &exception);
3200         } while (exception.retry);
3201         return err;
3202 }
3203
3204 void __nfs4_read_done_cb(struct nfs_read_data *data)
3205 {
3206         nfs_invalidate_atime(data->inode);
3207 }
3208
3209 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3210 {
3211         struct nfs_server *server = NFS_SERVER(data->inode);
3212
3213         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3214                 nfs_restart_rpc(task, server->nfs_client);
3215                 return -EAGAIN;
3216         }
3217
3218         __nfs4_read_done_cb(data);
3219         if (task->tk_status > 0)
3220                 renew_lease(server, data->timestamp);
3221         return 0;
3222 }
3223
3224 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3225 {
3226
3227         dprintk("--> %s\n", __func__);
3228
3229         if (!nfs4_sequence_done(task, &data->res.seq_res))
3230                 return -EAGAIN;
3231
3232         return data->read_done_cb ? data->read_done_cb(task, data) :
3233                                     nfs4_read_done_cb(task, data);
3234 }
3235
3236 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3237 {
3238         data->timestamp   = jiffies;
3239         data->read_done_cb = nfs4_read_done_cb;
3240         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3241 }
3242
3243 /* Reset the the nfs_read_data to send the read to the MDS. */
3244 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3245 {
3246         dprintk("%s Reset task for i/o through\n", __func__);
3247         put_lseg(data->lseg);
3248         data->lseg = NULL;
3249         /* offsets will differ in the dense stripe case */
3250         data->args.offset = data->mds_offset;
3251         data->ds_clp = NULL;
3252         data->args.fh     = NFS_FH(data->inode);
3253         data->read_done_cb = nfs4_read_done_cb;
3254         task->tk_ops = data->mds_ops;
3255         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3256 }
3257 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3258
3259 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3260 {
3261         struct inode *inode = data->inode;
3262         
3263         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3264                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3265                 return -EAGAIN;
3266         }
3267         if (task->tk_status >= 0) {
3268                 renew_lease(NFS_SERVER(inode), data->timestamp);
3269                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3270         }
3271         return 0;
3272 }
3273
3274 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3275 {
3276         if (!nfs4_sequence_done(task, &data->res.seq_res))
3277                 return -EAGAIN;
3278         return data->write_done_cb ? data->write_done_cb(task, data) :
3279                 nfs4_write_done_cb(task, data);
3280 }
3281
3282 /* Reset the the nfs_write_data to send the write to the MDS. */
3283 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3284 {
3285         dprintk("%s Reset task for i/o through\n", __func__);
3286         put_lseg(data->lseg);
3287         data->lseg          = NULL;
3288         data->ds_clp        = NULL;
3289         data->write_done_cb = nfs4_write_done_cb;
3290         data->args.fh       = NFS_FH(data->inode);
3291         data->args.bitmask  = data->res.server->cache_consistency_bitmask;
3292         data->args.offset   = data->mds_offset;
3293         data->res.fattr     = &data->fattr;
3294         task->tk_ops        = data->mds_ops;
3295         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3296 }
3297 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3298
3299 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3300 {
3301         struct nfs_server *server = NFS_SERVER(data->inode);
3302
3303         if (data->lseg) {
3304                 data->args.bitmask = NULL;
3305                 data->res.fattr = NULL;
3306         } else
3307                 data->args.bitmask = server->cache_consistency_bitmask;
3308         if (!data->write_done_cb)
3309                 data->write_done_cb = nfs4_write_done_cb;
3310         data->res.server = server;
3311         data->timestamp   = jiffies;
3312
3313         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3314 }
3315
3316 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3317 {
3318         struct inode *inode = data->inode;
3319
3320         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3321                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3322                 return -EAGAIN;
3323         }
3324         nfs_refresh_inode(inode, data->res.fattr);
3325         return 0;
3326 }
3327
3328 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3329 {
3330         if (!nfs4_sequence_done(task, &data->res.seq_res))
3331                 return -EAGAIN;
3332         return data->write_done_cb(task, data);
3333 }
3334
3335 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3336 {
3337         struct nfs_server *server = NFS_SERVER(data->inode);
3338
3339         if (data->lseg) {
3340                 data->args.bitmask = NULL;
3341                 data->res.fattr = NULL;
3342         } else
3343                 data->args.bitmask = server->cache_consistency_bitmask;
3344         if (!data->write_done_cb)
3345                 data->write_done_cb = nfs4_commit_done_cb;
3346         data->res.server = server;
3347         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3348 }
3349
3350 struct nfs4_renewdata {
3351         struct nfs_client       *client;
3352         unsigned long           timestamp;
3353 };
3354
3355 /*
3356  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3357  * standalone procedure for queueing an asynchronous RENEW.
3358  */
3359 static void nfs4_renew_release(void *calldata)
3360 {
3361         struct nfs4_renewdata *data = calldata;
3362         struct nfs_client *clp = data->client;
3363
3364         if (atomic_read(&clp->cl_count) > 1)
3365                 nfs4_schedule_state_renewal(clp);
3366         nfs_put_client(clp);
3367         kfree(data);
3368 }
3369
3370 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3371 {
3372         struct nfs4_renewdata *data = calldata;
3373         struct nfs_client *clp = data->client;
3374         unsigned long timestamp = data->timestamp;
3375
3376         if (task->tk_status < 0) {
3377                 /* Unless we're shutting down, schedule state recovery! */
3378                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3379                         nfs4_schedule_lease_recovery(clp);
3380                 return;
3381         }
3382         do_renew_lease(clp, timestamp);
3383 }
3384
3385 static const struct rpc_call_ops nfs4_renew_ops = {
3386         .rpc_call_done = nfs4_renew_done,
3387         .rpc_release = nfs4_renew_release,
3388 };
3389
3390 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3391 {
3392         struct rpc_message msg = {
3393                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3394                 .rpc_argp       = clp,
3395                 .rpc_cred       = cred,
3396         };
3397         struct nfs4_renewdata *data;
3398
3399         if (!atomic_inc_not_zero(&clp->cl_count))
3400                 return -EIO;
3401         data = kmalloc(sizeof(*data), GFP_KERNEL);
3402         if (data == NULL)
3403                 return -ENOMEM;
3404         data->client = clp;
3405         data->timestamp = jiffies;
3406         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3407                         &nfs4_renew_ops, data);
3408 }
3409