]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - fs/nfs/nfs4proc.c
079614deca3ff9e5b858765f4a402a2cfd923fcd
[~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 dentry *dir;
770         struct dentry *dentry;
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 dentry *dentry,
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(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         nfs_sb_active(dentry->d_sb);
809         p->dentry = dget(dentry);
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 = &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         struct super_block *sb = p->dentry->d_sb;
849
850         nfs_free_seqid(p->o_arg.seqid);
851         if (p->state != NULL)
852                 nfs4_put_open_state(p->state);
853         nfs4_put_state_owner(p->owner);
854         dput(p->dir);
855         dput(p->dentry);
856         nfs_sb_deactive(sb);
857         kfree(p);
858 }
859
860 static void nfs4_opendata_put(struct nfs4_opendata *p)
861 {
862         if (p != NULL)
863                 kref_put(&p->kref, nfs4_opendata_free);
864 }
865
866 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
867 {
868         int ret;
869
870         ret = rpc_wait_for_completion_task(task);
871         return ret;
872 }
873
874 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
875 {
876         int ret = 0;
877
878         if (open_mode & O_EXCL)
879                 goto out;
880         switch (mode & (FMODE_READ|FMODE_WRITE)) {
881                 case FMODE_READ:
882                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
883                                 && state->n_rdonly != 0;
884                         break;
885                 case FMODE_WRITE:
886                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
887                                 && state->n_wronly != 0;
888                         break;
889                 case FMODE_READ|FMODE_WRITE:
890                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
891                                 && state->n_rdwr != 0;
892         }
893 out:
894         return ret;
895 }
896
897 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
898 {
899         if ((delegation->type & fmode) != fmode)
900                 return 0;
901         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
902                 return 0;
903         nfs_mark_delegation_referenced(delegation);
904         return 1;
905 }
906
907 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
908 {
909         switch (fmode) {
910                 case FMODE_WRITE:
911                         state->n_wronly++;
912                         break;
913                 case FMODE_READ:
914                         state->n_rdonly++;
915                         break;
916                 case FMODE_READ|FMODE_WRITE:
917                         state->n_rdwr++;
918         }
919         nfs4_state_set_mode_locked(state, state->state | fmode);
920 }
921
922 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
923 {
924         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
925                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
926         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
927         switch (fmode) {
928                 case FMODE_READ:
929                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
930                         break;
931                 case FMODE_WRITE:
932                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
933                         break;
934                 case FMODE_READ|FMODE_WRITE:
935                         set_bit(NFS_O_RDWR_STATE, &state->flags);
936         }
937 }
938
939 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
940 {
941         write_seqlock(&state->seqlock);
942         nfs_set_open_stateid_locked(state, stateid, fmode);
943         write_sequnlock(&state->seqlock);
944 }
945
946 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
947 {
948         /*
949          * Protect the call to nfs4_state_set_mode_locked and
950          * serialise the stateid update
951          */
952         write_seqlock(&state->seqlock);
953         if (deleg_stateid != NULL) {
954                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
955                 set_bit(NFS_DELEGATED_STATE, &state->flags);
956         }
957         if (open_stateid != NULL)
958                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
959         write_sequnlock(&state->seqlock);
960         spin_lock(&state->owner->so_lock);
961         update_open_stateflags(state, fmode);
962         spin_unlock(&state->owner->so_lock);
963 }
964
965 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
966 {
967         struct nfs_inode *nfsi = NFS_I(state->inode);
968         struct nfs_delegation *deleg_cur;
969         int ret = 0;
970
971         fmode &= (FMODE_READ|FMODE_WRITE);
972
973         rcu_read_lock();
974         deleg_cur = rcu_dereference(nfsi->delegation);
975         if (deleg_cur == NULL)
976                 goto no_delegation;
977
978         spin_lock(&deleg_cur->lock);
979         if (nfsi->delegation != deleg_cur ||
980             (deleg_cur->type & fmode) != fmode)
981                 goto no_delegation_unlock;
982
983         if (delegation == NULL)
984                 delegation = &deleg_cur->stateid;
985         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
986                 goto no_delegation_unlock;
987
988         nfs_mark_delegation_referenced(deleg_cur);
989         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
990         ret = 1;
991 no_delegation_unlock:
992         spin_unlock(&deleg_cur->lock);
993 no_delegation:
994         rcu_read_unlock();
995
996         if (!ret && open_stateid != NULL) {
997                 __update_open_stateid(state, open_stateid, NULL, fmode);
998                 ret = 1;
999         }
1000
1001         return ret;
1002 }
1003
1004
1005 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1006 {
1007         struct nfs_delegation *delegation;
1008
1009         rcu_read_lock();
1010         delegation = rcu_dereference(NFS_I(inode)->delegation);
1011         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1012                 rcu_read_unlock();
1013                 return;
1014         }
1015         rcu_read_unlock();
1016         nfs_inode_return_delegation(inode);
1017 }
1018
1019 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1020 {
1021         struct nfs4_state *state = opendata->state;
1022         struct nfs_inode *nfsi = NFS_I(state->inode);
1023         struct nfs_delegation *delegation;
1024         int open_mode = opendata->o_arg.open_flags & O_EXCL;
1025         fmode_t fmode = opendata->o_arg.fmode;
1026         nfs4_stateid stateid;
1027         int ret = -EAGAIN;
1028
1029         for (;;) {
1030                 if (can_open_cached(state, fmode, open_mode)) {
1031                         spin_lock(&state->owner->so_lock);
1032                         if (can_open_cached(state, fmode, open_mode)) {
1033                                 update_open_stateflags(state, fmode);
1034                                 spin_unlock(&state->owner->so_lock);
1035                                 goto out_return_state;
1036                         }
1037                         spin_unlock(&state->owner->so_lock);
1038                 }
1039                 rcu_read_lock();
1040                 delegation = rcu_dereference(nfsi->delegation);
1041                 if (delegation == NULL ||
1042                     !can_open_delegated(delegation, fmode)) {
1043                         rcu_read_unlock();
1044                         break;
1045                 }
1046                 /* Save the delegation */
1047                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1048                 rcu_read_unlock();
1049                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1050                 if (ret != 0)
1051                         goto out;
1052                 ret = -EAGAIN;
1053
1054                 /* Try to update the stateid using the delegation */
1055                 if (update_open_stateid(state, NULL, &stateid, fmode))
1056                         goto out_return_state;
1057         }
1058 out:
1059         return ERR_PTR(ret);
1060 out_return_state:
1061         atomic_inc(&state->count);
1062         return state;
1063 }
1064
1065 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1066 {
1067         struct inode *inode;
1068         struct nfs4_state *state = NULL;
1069         struct nfs_delegation *delegation;
1070         int ret;
1071
1072         if (!data->rpc_done) {
1073                 state = nfs4_try_open_cached(data);
1074                 goto out;
1075         }
1076
1077         ret = -EAGAIN;
1078         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1079                 goto err;
1080         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1081         ret = PTR_ERR(inode);
1082         if (IS_ERR(inode))
1083                 goto err;
1084         ret = -ENOMEM;
1085         state = nfs4_get_open_state(inode, data->owner);
1086         if (state == NULL)
1087                 goto err_put_inode;
1088         if (data->o_res.delegation_type != 0) {
1089                 int delegation_flags = 0;
1090
1091                 rcu_read_lock();
1092                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1093                 if (delegation)
1094                         delegation_flags = delegation->flags;
1095                 rcu_read_unlock();
1096                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1097                         nfs_inode_set_delegation(state->inode,
1098                                         data->owner->so_cred,
1099                                         &data->o_res);
1100                 else
1101                         nfs_inode_reclaim_delegation(state->inode,
1102                                         data->owner->so_cred,
1103                                         &data->o_res);
1104         }
1105
1106         update_open_stateid(state, &data->o_res.stateid, NULL,
1107                         data->o_arg.fmode);
1108         iput(inode);
1109 out:
1110         return state;
1111 err_put_inode:
1112         iput(inode);
1113 err:
1114         return ERR_PTR(ret);
1115 }
1116
1117 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1118 {
1119         struct nfs_inode *nfsi = NFS_I(state->inode);
1120         struct nfs_open_context *ctx;
1121
1122         spin_lock(&state->inode->i_lock);
1123         list_for_each_entry(ctx, &nfsi->open_files, list) {
1124                 if (ctx->state != state)
1125                         continue;
1126                 get_nfs_open_context(ctx);
1127                 spin_unlock(&state->inode->i_lock);
1128                 return ctx;
1129         }
1130         spin_unlock(&state->inode->i_lock);
1131         return ERR_PTR(-ENOENT);
1132 }
1133
1134 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1135 {
1136         struct nfs4_opendata *opendata;
1137
1138         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1139         if (opendata == NULL)
1140                 return ERR_PTR(-ENOMEM);
1141         opendata->state = state;
1142         atomic_inc(&state->count);
1143         return opendata;
1144 }
1145
1146 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1147 {
1148         struct nfs4_state *newstate;
1149         int ret;
1150
1151         opendata->o_arg.open_flags = 0;
1152         opendata->o_arg.fmode = fmode;
1153         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1154         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1155         nfs4_init_opendata_res(opendata);
1156         ret = _nfs4_recover_proc_open(opendata);
1157         if (ret != 0)
1158                 return ret; 
1159         newstate = nfs4_opendata_to_nfs4_state(opendata);
1160         if (IS_ERR(newstate))
1161                 return PTR_ERR(newstate);
1162         nfs4_close_state(newstate, fmode);
1163         *res = newstate;
1164         return 0;
1165 }
1166
1167 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1168 {
1169         struct nfs4_state *newstate;
1170         int ret;
1171
1172         /* memory barrier prior to reading state->n_* */
1173         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1174         smp_rmb();
1175         if (state->n_rdwr != 0) {
1176                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1177                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1178                 if (ret != 0)
1179                         return ret;
1180                 if (newstate != state)
1181                         return -ESTALE;
1182         }
1183         if (state->n_wronly != 0) {
1184                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1185                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1186                 if (ret != 0)
1187                         return ret;
1188                 if (newstate != state)
1189                         return -ESTALE;
1190         }
1191         if (state->n_rdonly != 0) {
1192                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1193                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1194                 if (ret != 0)
1195                         return ret;
1196                 if (newstate != state)
1197                         return -ESTALE;
1198         }
1199         /*
1200          * We may have performed cached opens for all three recoveries.
1201          * Check if we need to update the current stateid.
1202          */
1203         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1204             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1205                 write_seqlock(&state->seqlock);
1206                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1207                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1208                 write_sequnlock(&state->seqlock);
1209         }
1210         return 0;
1211 }
1212
1213 /*
1214  * OPEN_RECLAIM:
1215  *      reclaim state on the server after a reboot.
1216  */
1217 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1218 {
1219         struct nfs_delegation *delegation;
1220         struct nfs4_opendata *opendata;
1221         fmode_t delegation_type = 0;
1222         int status;
1223
1224         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1225         if (IS_ERR(opendata))
1226                 return PTR_ERR(opendata);
1227         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1228         opendata->o_arg.fh = NFS_FH(state->inode);
1229         rcu_read_lock();
1230         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1231         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1232                 delegation_type = delegation->type;
1233         rcu_read_unlock();
1234         opendata->o_arg.u.delegation_type = delegation_type;
1235         status = nfs4_open_recover(opendata, state);
1236         nfs4_opendata_put(opendata);
1237         return status;
1238 }
1239
1240 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1241 {
1242         struct nfs_server *server = NFS_SERVER(state->inode);
1243         struct nfs4_exception exception = { };
1244         int err;
1245         do {
1246                 err = _nfs4_do_open_reclaim(ctx, state);
1247                 if (err != -NFS4ERR_DELAY)
1248                         break;
1249                 nfs4_handle_exception(server, err, &exception);
1250         } while (exception.retry);
1251         return err;
1252 }
1253
1254 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1255 {
1256         struct nfs_open_context *ctx;
1257         int ret;
1258
1259         ctx = nfs4_state_find_open_context(state);
1260         if (IS_ERR(ctx))
1261                 return PTR_ERR(ctx);
1262         ret = nfs4_do_open_reclaim(ctx, state);
1263         put_nfs_open_context(ctx);
1264         return ret;
1265 }
1266
1267 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1268 {
1269         struct nfs4_opendata *opendata;
1270         int ret;
1271
1272         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1273         if (IS_ERR(opendata))
1274                 return PTR_ERR(opendata);
1275         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1276         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1277                         sizeof(opendata->o_arg.u.delegation.data));
1278         ret = nfs4_open_recover(opendata, state);
1279         nfs4_opendata_put(opendata);
1280         return ret;
1281 }
1282
1283 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1284 {
1285         struct nfs4_exception exception = { };
1286         struct nfs_server *server = NFS_SERVER(state->inode);
1287         int err;
1288         do {
1289                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1290                 switch (err) {
1291                         case 0:
1292                         case -ENOENT:
1293                         case -ESTALE:
1294                                 goto out;
1295                         case -NFS4ERR_BADSESSION:
1296                         case -NFS4ERR_BADSLOT:
1297                         case -NFS4ERR_BAD_HIGH_SLOT:
1298                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1299                         case -NFS4ERR_DEADSESSION:
1300                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1301                                 goto out;
1302                         case -NFS4ERR_STALE_CLIENTID:
1303                         case -NFS4ERR_STALE_STATEID:
1304                         case -NFS4ERR_EXPIRED:
1305                                 /* Don't recall a delegation if it was lost */
1306                                 nfs4_schedule_lease_recovery(server->nfs_client);
1307                                 goto out;
1308                         case -ERESTARTSYS:
1309                                 /*
1310                                  * The show must go on: exit, but mark the
1311                                  * stateid as needing recovery.
1312                                  */
1313                         case -NFS4ERR_ADMIN_REVOKED:
1314                         case -NFS4ERR_BAD_STATEID:
1315                                 nfs4_schedule_stateid_recovery(server, state);
1316                         case -EKEYEXPIRED:
1317                                 /*
1318                                  * User RPCSEC_GSS context has expired.
1319                                  * We cannot recover this stateid now, so
1320                                  * skip it and allow recovery thread to
1321                                  * proceed.
1322                                  */
1323                         case -ENOMEM:
1324                                 err = 0;
1325                                 goto out;
1326                 }
1327                 err = nfs4_handle_exception(server, err, &exception);
1328         } while (exception.retry);
1329 out:
1330         return err;
1331 }
1332
1333 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1334 {
1335         struct nfs4_opendata *data = calldata;
1336
1337         data->rpc_status = task->tk_status;
1338         if (data->rpc_status == 0) {
1339                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1340                                 sizeof(data->o_res.stateid.data));
1341                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1342                 renew_lease(data->o_res.server, data->timestamp);
1343                 data->rpc_done = 1;
1344         }
1345 }
1346
1347 static void nfs4_open_confirm_release(void *calldata)
1348 {
1349         struct nfs4_opendata *data = calldata;
1350         struct nfs4_state *state = NULL;
1351
1352         /* If this request hasn't been cancelled, do nothing */
1353         if (data->cancelled == 0)
1354                 goto out_free;
1355         /* In case of error, no cleanup! */
1356         if (!data->rpc_done)
1357                 goto out_free;
1358         state = nfs4_opendata_to_nfs4_state(data);
1359         if (!IS_ERR(state))
1360                 nfs4_close_state(state, data->o_arg.fmode);
1361 out_free:
1362         nfs4_opendata_put(data);
1363 }
1364
1365 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1366         .rpc_call_done = nfs4_open_confirm_done,
1367         .rpc_release = nfs4_open_confirm_release,
1368 };
1369
1370 /*
1371  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1372  */
1373 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1374 {
1375         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1376         struct rpc_task *task;
1377         struct  rpc_message msg = {
1378                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1379                 .rpc_argp = &data->c_arg,
1380                 .rpc_resp = &data->c_res,
1381                 .rpc_cred = data->owner->so_cred,
1382         };
1383         struct rpc_task_setup task_setup_data = {
1384                 .rpc_client = server->client,
1385                 .rpc_message = &msg,
1386                 .callback_ops = &nfs4_open_confirm_ops,
1387                 .callback_data = data,
1388                 .workqueue = nfsiod_workqueue,
1389                 .flags = RPC_TASK_ASYNC,
1390         };
1391         int status;
1392
1393         kref_get(&data->kref);
1394         data->rpc_done = 0;
1395         data->rpc_status = 0;
1396         data->timestamp = jiffies;
1397         task = rpc_run_task(&task_setup_data);
1398         if (IS_ERR(task))
1399                 return PTR_ERR(task);
1400         status = nfs4_wait_for_completion_rpc_task(task);
1401         if (status != 0) {
1402                 data->cancelled = 1;
1403                 smp_wmb();
1404         } else
1405                 status = data->rpc_status;
1406         rpc_put_task(task);
1407         return status;
1408 }
1409
1410 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1411 {
1412         struct nfs4_opendata *data = calldata;
1413         struct nfs4_state_owner *sp = data->owner;
1414
1415         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1416                 return;
1417         /*
1418          * Check if we still need to send an OPEN call, or if we can use
1419          * a delegation instead.
1420          */
1421         if (data->state != NULL) {
1422                 struct nfs_delegation *delegation;
1423
1424                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1425                         goto out_no_action;
1426                 rcu_read_lock();
1427                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1428                 if (delegation != NULL &&
1429                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1430                         rcu_read_unlock();
1431                         goto out_no_action;
1432                 }
1433                 rcu_read_unlock();
1434         }
1435         /* Update sequence id. */
1436         data->o_arg.id = sp->so_owner_id.id;
1437         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1438         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1439                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1440                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1441         }
1442         data->timestamp = jiffies;
1443         if (nfs4_setup_sequence(data->o_arg.server,
1444                                 &data->o_arg.seq_args,
1445                                 &data->o_res.seq_res, 1, task))
1446                 return;
1447         rpc_call_start(task);
1448         return;
1449 out_no_action:
1450         task->tk_action = NULL;
1451
1452 }
1453
1454 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1455 {
1456         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1457         nfs4_open_prepare(task, calldata);
1458 }
1459
1460 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1461 {
1462         struct nfs4_opendata *data = calldata;
1463
1464         data->rpc_status = task->tk_status;
1465
1466         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1467                 return;
1468
1469         if (task->tk_status == 0) {
1470                 switch (data->o_res.f_attr->mode & S_IFMT) {
1471                         case S_IFREG:
1472                                 break;
1473                         case S_IFLNK:
1474                                 data->rpc_status = -ELOOP;
1475                                 break;
1476                         case S_IFDIR:
1477                                 data->rpc_status = -EISDIR;
1478                                 break;
1479                         default:
1480                                 data->rpc_status = -ENOTDIR;
1481                 }
1482                 renew_lease(data->o_res.server, data->timestamp);
1483                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1484                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1485         }
1486         data->rpc_done = 1;
1487 }
1488
1489 static void nfs4_open_release(void *calldata)
1490 {
1491         struct nfs4_opendata *data = calldata;
1492         struct nfs4_state *state = NULL;
1493
1494         /* If this request hasn't been cancelled, do nothing */
1495         if (data->cancelled == 0)
1496                 goto out_free;
1497         /* In case of error, no cleanup! */
1498         if (data->rpc_status != 0 || !data->rpc_done)
1499                 goto out_free;
1500         /* In case we need an open_confirm, no cleanup! */
1501         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1502                 goto out_free;
1503         state = nfs4_opendata_to_nfs4_state(data);
1504         if (!IS_ERR(state))
1505                 nfs4_close_state(state, data->o_arg.fmode);
1506 out_free:
1507         nfs4_opendata_put(data);
1508 }
1509
1510 static const struct rpc_call_ops nfs4_open_ops = {
1511         .rpc_call_prepare = nfs4_open_prepare,
1512         .rpc_call_done = nfs4_open_done,
1513         .rpc_release = nfs4_open_release,
1514 };
1515
1516 static const struct rpc_call_ops nfs4_recover_open_ops = {
1517         .rpc_call_prepare = nfs4_recover_open_prepare,
1518         .rpc_call_done = nfs4_open_done,
1519         .rpc_release = nfs4_open_release,
1520 };
1521
1522 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1523 {
1524         struct inode *dir = data->dir->d_inode;
1525         struct nfs_server *server = NFS_SERVER(dir);
1526         struct nfs_openargs *o_arg = &data->o_arg;
1527         struct nfs_openres *o_res = &data->o_res;
1528         struct rpc_task *task;
1529         struct rpc_message msg = {
1530                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1531                 .rpc_argp = o_arg,
1532                 .rpc_resp = o_res,
1533                 .rpc_cred = data->owner->so_cred,
1534         };
1535         struct rpc_task_setup task_setup_data = {
1536                 .rpc_client = server->client,
1537                 .rpc_message = &msg,
1538                 .callback_ops = &nfs4_open_ops,
1539                 .callback_data = data,
1540                 .workqueue = nfsiod_workqueue,
1541                 .flags = RPC_TASK_ASYNC,
1542         };
1543         int status;
1544
1545         kref_get(&data->kref);
1546         data->rpc_done = 0;
1547         data->rpc_status = 0;
1548         data->cancelled = 0;
1549         if (isrecover)
1550                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1551         task = rpc_run_task(&task_setup_data);
1552         if (IS_ERR(task))
1553                 return PTR_ERR(task);
1554         status = nfs4_wait_for_completion_rpc_task(task);
1555         if (status != 0) {
1556                 data->cancelled = 1;
1557                 smp_wmb();
1558         } else
1559                 status = data->rpc_status;
1560         rpc_put_task(task);
1561
1562         return status;
1563 }
1564
1565 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1566 {
1567         struct inode *dir = data->dir->d_inode;
1568         struct nfs_openres *o_res = &data->o_res;
1569         int status;
1570
1571         status = nfs4_run_open_task(data, 1);
1572         if (status != 0 || !data->rpc_done)
1573                 return status;
1574
1575         nfs_refresh_inode(dir, o_res->dir_attr);
1576
1577         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1578                 status = _nfs4_proc_open_confirm(data);
1579                 if (status != 0)
1580                         return status;
1581         }
1582
1583         return status;
1584 }
1585
1586 /*
1587  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1588  */
1589 static int _nfs4_proc_open(struct nfs4_opendata *data)
1590 {
1591         struct inode *dir = data->dir->d_inode;
1592         struct nfs_server *server = NFS_SERVER(dir);
1593         struct nfs_openargs *o_arg = &data->o_arg;
1594         struct nfs_openres *o_res = &data->o_res;
1595         int status;
1596
1597         status = nfs4_run_open_task(data, 0);
1598         if (status != 0 || !data->rpc_done)
1599                 return status;
1600
1601         if (o_arg->open_flags & O_CREAT) {
1602                 update_changeattr(dir, &o_res->cinfo);
1603                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1604         } else
1605                 nfs_refresh_inode(dir, o_res->dir_attr);
1606         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1607                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1608         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1609                 status = _nfs4_proc_open_confirm(data);
1610                 if (status != 0)
1611                         return status;
1612         }
1613         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1614                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1615         return 0;
1616 }
1617
1618 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1619 {
1620         unsigned int loop;
1621         int ret;
1622
1623         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1624                 ret = nfs4_wait_clnt_recover(clp);
1625                 if (ret != 0)
1626                         break;
1627                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1628                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1629                         break;
1630                 nfs4_schedule_state_manager(clp);
1631                 ret = -EIO;
1632         }
1633         return ret;
1634 }
1635
1636 static int nfs4_recover_expired_lease(struct nfs_server *server)
1637 {
1638         return nfs4_client_recover_expired_lease(server->nfs_client);
1639 }
1640
1641 /*
1642  * OPEN_EXPIRED:
1643  *      reclaim state on the server after a network partition.
1644  *      Assumes caller holds the appropriate lock
1645  */
1646 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1647 {
1648         struct nfs4_opendata *opendata;
1649         int ret;
1650
1651         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1652         if (IS_ERR(opendata))
1653                 return PTR_ERR(opendata);
1654         ret = nfs4_open_recover(opendata, state);
1655         if (ret == -ESTALE)
1656                 d_drop(ctx->dentry);
1657         nfs4_opendata_put(opendata);
1658         return ret;
1659 }
1660
1661 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1662 {
1663         struct nfs_server *server = NFS_SERVER(state->inode);
1664         struct nfs4_exception exception = { };
1665         int err;
1666
1667         do {
1668                 err = _nfs4_open_expired(ctx, state);
1669                 switch (err) {
1670                 default:
1671                         goto out;
1672                 case -NFS4ERR_GRACE:
1673                 case -NFS4ERR_DELAY:
1674                         nfs4_handle_exception(server, err, &exception);
1675                         err = 0;
1676                 }
1677         } while (exception.retry);
1678 out:
1679         return err;
1680 }
1681
1682 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1683 {
1684         struct nfs_open_context *ctx;
1685         int ret;
1686
1687         ctx = nfs4_state_find_open_context(state);
1688         if (IS_ERR(ctx))
1689                 return PTR_ERR(ctx);
1690         ret = nfs4_do_open_expired(ctx, state);
1691         put_nfs_open_context(ctx);
1692         return ret;
1693 }
1694
1695 #if defined(CONFIG_NFS_V4_1)
1696 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1697 {
1698         int status;
1699         struct nfs_server *server = NFS_SERVER(state->inode);
1700
1701         status = nfs41_test_stateid(server, state);
1702         if (status == NFS_OK)
1703                 return 0;
1704         nfs41_free_stateid(server, state);
1705         return nfs4_open_expired(sp, state);
1706 }
1707 #endif
1708
1709 /*
1710  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1711  * fields corresponding to attributes that were used to store the verifier.
1712  * Make sure we clobber those fields in the later setattr call
1713  */
1714 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1715 {
1716         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1717             !(sattr->ia_valid & ATTR_ATIME_SET))
1718                 sattr->ia_valid |= ATTR_ATIME;
1719
1720         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1721             !(sattr->ia_valid & ATTR_MTIME_SET))
1722                 sattr->ia_valid |= ATTR_MTIME;
1723 }
1724
1725 /*
1726  * Returns a referenced nfs4_state
1727  */
1728 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1729 {
1730         struct nfs4_state_owner  *sp;
1731         struct nfs4_state     *state = NULL;
1732         struct nfs_server       *server = NFS_SERVER(dir);
1733         struct nfs4_opendata *opendata;
1734         int status;
1735
1736         /* Protect against reboot recovery conflicts */
1737         status = -ENOMEM;
1738         if (!(sp = nfs4_get_state_owner(server, cred))) {
1739                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1740                 goto out_err;
1741         }
1742         status = nfs4_recover_expired_lease(server);
1743         if (status != 0)
1744                 goto err_put_state_owner;
1745         if (dentry->d_inode != NULL)
1746                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1747         status = -ENOMEM;
1748         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1749         if (opendata == NULL)
1750                 goto err_put_state_owner;
1751
1752         if (dentry->d_inode != NULL)
1753                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1754
1755         status = _nfs4_proc_open(opendata);
1756         if (status != 0)
1757                 goto err_opendata_put;
1758
1759         state = nfs4_opendata_to_nfs4_state(opendata);
1760         status = PTR_ERR(state);
1761         if (IS_ERR(state))
1762                 goto err_opendata_put;
1763         if (server->caps & NFS_CAP_POSIX_LOCK)
1764                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1765
1766         if (opendata->o_arg.open_flags & O_EXCL) {
1767                 nfs4_exclusive_attrset(opendata, sattr);
1768
1769                 nfs_fattr_init(opendata->o_res.f_attr);
1770                 status = nfs4_do_setattr(state->inode, cred,
1771                                 opendata->o_res.f_attr, sattr,
1772                                 state);
1773                 if (status == 0)
1774                         nfs_setattr_update_inode(state->inode, sattr);
1775                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1776         }
1777         nfs4_opendata_put(opendata);
1778         nfs4_put_state_owner(sp);
1779         *res = state;
1780         return 0;
1781 err_opendata_put:
1782         nfs4_opendata_put(opendata);
1783 err_put_state_owner:
1784         nfs4_put_state_owner(sp);
1785 out_err:
1786         *res = NULL;
1787         return status;
1788 }
1789
1790
1791 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1792 {
1793         struct nfs4_exception exception = { };
1794         struct nfs4_state *res;
1795         int status;
1796
1797         do {
1798                 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1799                 if (status == 0)
1800                         break;
1801                 /* NOTE: BAD_SEQID means the server and client disagree about the
1802                  * book-keeping w.r.t. state-changing operations
1803                  * (OPEN/CLOSE/LOCK/LOCKU...)
1804                  * It is actually a sign of a bug on the client or on the server.
1805                  *
1806                  * If we receive a BAD_SEQID error in the particular case of
1807                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1808                  * have unhashed the old state_owner for us, and that we can
1809                  * therefore safely retry using a new one. We should still warn
1810                  * the user though...
1811                  */
1812                 if (status == -NFS4ERR_BAD_SEQID) {
1813                         printk(KERN_WARNING "NFS: v4 server %s "
1814                                         " returned a bad sequence-id error!\n",
1815                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1816                         exception.retry = 1;
1817                         continue;
1818                 }
1819                 /*
1820                  * BAD_STATEID on OPEN means that the server cancelled our
1821                  * state before it received the OPEN_CONFIRM.
1822                  * Recover by retrying the request as per the discussion
1823                  * on Page 181 of RFC3530.
1824                  */
1825                 if (status == -NFS4ERR_BAD_STATEID) {
1826                         exception.retry = 1;
1827                         continue;
1828                 }
1829                 if (status == -EAGAIN) {
1830                         /* We must have found a delegation */
1831                         exception.retry = 1;
1832                         continue;
1833                 }
1834                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1835                                         status, &exception));
1836         } while (exception.retry);
1837         return res;
1838 }
1839
1840 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1841                             struct nfs_fattr *fattr, struct iattr *sattr,
1842                             struct nfs4_state *state)
1843 {
1844         struct nfs_server *server = NFS_SERVER(inode);
1845         struct nfs_setattrargs  arg = {
1846                 .fh             = NFS_FH(inode),
1847                 .iap            = sattr,
1848                 .server         = server,
1849                 .bitmask = server->attr_bitmask,
1850         };
1851         struct nfs_setattrres  res = {
1852                 .fattr          = fattr,
1853                 .server         = server,
1854         };
1855         struct rpc_message msg = {
1856                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1857                 .rpc_argp       = &arg,
1858                 .rpc_resp       = &res,
1859                 .rpc_cred       = cred,
1860         };
1861         unsigned long timestamp = jiffies;
1862         int status;
1863
1864         nfs_fattr_init(fattr);
1865
1866         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1867                 /* Use that stateid */
1868         } else if (state != NULL) {
1869                 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1870         } else
1871                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1872
1873         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1874         if (status == 0 && state != NULL)
1875                 renew_lease(server, timestamp);
1876         return status;
1877 }
1878
1879 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1880                            struct nfs_fattr *fattr, struct iattr *sattr,
1881                            struct nfs4_state *state)
1882 {
1883         struct nfs_server *server = NFS_SERVER(inode);
1884         struct nfs4_exception exception = { };
1885         int err;
1886         do {
1887                 err = nfs4_handle_exception(server,
1888                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1889                                 &exception);
1890         } while (exception.retry);
1891         return err;
1892 }
1893
1894 struct nfs4_closedata {
1895         struct inode *inode;
1896         struct nfs4_state *state;
1897         struct nfs_closeargs arg;
1898         struct nfs_closeres res;
1899         struct nfs_fattr fattr;
1900         unsigned long timestamp;
1901         bool roc;
1902         u32 roc_barrier;
1903 };
1904
1905 static void nfs4_free_closedata(void *data)
1906 {
1907         struct nfs4_closedata *calldata = data;
1908         struct nfs4_state_owner *sp = calldata->state->owner;
1909         struct super_block *sb = calldata->state->inode->i_sb;
1910
1911         if (calldata->roc)
1912                 pnfs_roc_release(calldata->state->inode);
1913         nfs4_put_open_state(calldata->state);
1914         nfs_free_seqid(calldata->arg.seqid);
1915         nfs4_put_state_owner(sp);
1916         nfs_sb_deactive(sb);
1917         kfree(calldata);
1918 }
1919
1920 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1921                 fmode_t fmode)
1922 {
1923         spin_lock(&state->owner->so_lock);
1924         if (!(fmode & FMODE_READ))
1925                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1926         if (!(fmode & FMODE_WRITE))
1927                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1928         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1929         spin_unlock(&state->owner->so_lock);
1930 }
1931
1932 static void nfs4_close_done(struct rpc_task *task, void *data)
1933 {
1934         struct nfs4_closedata *calldata = data;
1935         struct nfs4_state *state = calldata->state;
1936         struct nfs_server *server = NFS_SERVER(calldata->inode);
1937
1938         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1939                 return;
1940         /* hmm. we are done with the inode, and in the process of freeing
1941          * the state_owner. we keep this around to process errors
1942          */
1943         switch (task->tk_status) {
1944                 case 0:
1945                         if (calldata->roc)
1946                                 pnfs_roc_set_barrier(state->inode,
1947                                                      calldata->roc_barrier);
1948                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1949                         renew_lease(server, calldata->timestamp);
1950                         nfs4_close_clear_stateid_flags(state,
1951                                         calldata->arg.fmode);
1952                         break;
1953                 case -NFS4ERR_STALE_STATEID:
1954                 case -NFS4ERR_OLD_STATEID:
1955                 case -NFS4ERR_BAD_STATEID:
1956                 case -NFS4ERR_EXPIRED:
1957                         if (calldata->arg.fmode == 0)
1958                                 break;
1959                 default:
1960                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1961                                 rpc_restart_call_prepare(task);
1962         }
1963         nfs_release_seqid(calldata->arg.seqid);
1964         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1965 }
1966
1967 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1968 {
1969         struct nfs4_closedata *calldata = data;
1970         struct nfs4_state *state = calldata->state;
1971         int call_close = 0;
1972
1973         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1974                 return;
1975
1976         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1977         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1978         spin_lock(&state->owner->so_lock);
1979         /* Calculate the change in open mode */
1980         if (state->n_rdwr == 0) {
1981                 if (state->n_rdonly == 0) {
1982                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1983                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1984                         calldata->arg.fmode &= ~FMODE_READ;
1985                 }
1986                 if (state->n_wronly == 0) {
1987                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1988                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1989                         calldata->arg.fmode &= ~FMODE_WRITE;
1990                 }
1991         }
1992         spin_unlock(&state->owner->so_lock);
1993
1994         if (!call_close) {
1995                 /* Note: exit _without_ calling nfs4_close_done */
1996                 task->tk_action = NULL;
1997                 return;
1998         }
1999
2000         if (calldata->arg.fmode == 0) {
2001                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2002                 if (calldata->roc &&
2003                     pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2004                         rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2005                                      task, NULL);
2006                         return;
2007                 }
2008         }
2009
2010         nfs_fattr_init(calldata->res.fattr);
2011         calldata->timestamp = jiffies;
2012         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2013                                 &calldata->arg.seq_args, &calldata->res.seq_res,
2014                                 1, task))
2015                 return;
2016         rpc_call_start(task);
2017 }
2018
2019 static const struct rpc_call_ops nfs4_close_ops = {
2020         .rpc_call_prepare = nfs4_close_prepare,
2021         .rpc_call_done = nfs4_close_done,
2022         .rpc_release = nfs4_free_closedata,
2023 };
2024
2025 /* 
2026  * It is possible for data to be read/written from a mem-mapped file 
2027  * after the sys_close call (which hits the vfs layer as a flush).
2028  * This means that we can't safely call nfsv4 close on a file until 
2029  * the inode is cleared. This in turn means that we are not good
2030  * NFSv4 citizens - we do not indicate to the server to update the file's 
2031  * share state even when we are done with one of the three share 
2032  * stateid's in the inode.
2033  *
2034  * NOTE: Caller must be holding the sp->so_owner semaphore!
2035  */
2036 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2037 {
2038         struct nfs_server *server = NFS_SERVER(state->inode);
2039         struct nfs4_closedata *calldata;
2040         struct nfs4_state_owner *sp = state->owner;
2041         struct rpc_task *task;
2042         struct rpc_message msg = {
2043                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2044                 .rpc_cred = state->owner->so_cred,
2045         };
2046         struct rpc_task_setup task_setup_data = {
2047                 .rpc_client = server->client,
2048                 .rpc_message = &msg,
2049                 .callback_ops = &nfs4_close_ops,
2050                 .workqueue = nfsiod_workqueue,
2051                 .flags = RPC_TASK_ASYNC,
2052         };
2053         int status = -ENOMEM;
2054
2055         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2056         if (calldata == NULL)
2057                 goto out;
2058         calldata->inode = state->inode;
2059         calldata->state = state;
2060         calldata->arg.fh = NFS_FH(state->inode);
2061         calldata->arg.stateid = &state->open_stateid;
2062         /* Serialization for the sequence id */
2063         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2064         if (calldata->arg.seqid == NULL)
2065                 goto out_free_calldata;
2066         calldata->arg.fmode = 0;
2067         calldata->arg.bitmask = server->cache_consistency_bitmask;
2068         calldata->res.fattr = &calldata->fattr;
2069         calldata->res.seqid = calldata->arg.seqid;
2070         calldata->res.server = server;
2071         calldata->roc = roc;
2072         nfs_sb_active(calldata->inode->i_sb);
2073
2074         msg.rpc_argp = &calldata->arg;
2075         msg.rpc_resp = &calldata->res;
2076         task_setup_data.callback_data = calldata;
2077         task = rpc_run_task(&task_setup_data);
2078         if (IS_ERR(task))
2079                 return PTR_ERR(task);
2080         status = 0;
2081         if (wait)
2082                 status = rpc_wait_for_completion_task(task);
2083         rpc_put_task(task);
2084         return status;
2085 out_free_calldata:
2086         kfree(calldata);
2087 out:
2088         if (roc)
2089                 pnfs_roc_release(state->inode);
2090         nfs4_put_open_state(state);
2091         nfs4_put_state_owner(sp);
2092         return status;
2093 }
2094
2095 static struct inode *
2096 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2097 {
2098         struct nfs4_state *state;
2099
2100         /* Protect against concurrent sillydeletes */
2101         state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2102         if (IS_ERR(state))
2103                 return ERR_CAST(state);
2104         ctx->state = state;
2105         return igrab(state->inode);
2106 }
2107
2108 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2109 {
2110         if (ctx->state == NULL)
2111                 return;
2112         if (is_sync)
2113                 nfs4_close_sync(ctx->state, ctx->mode);
2114         else
2115                 nfs4_close_state(ctx->state, ctx->mode);
2116 }
2117
2118 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2119 {
2120         struct nfs4_server_caps_arg args = {
2121                 .fhandle = fhandle,
2122         };
2123         struct nfs4_server_caps_res res = {};
2124         struct rpc_message msg = {
2125                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2126                 .rpc_argp = &args,
2127                 .rpc_resp = &res,
2128         };
2129         int status;
2130
2131         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2132         if (status == 0) {
2133                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2134                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2135                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2136                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2137                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2138                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2139                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2140                         server->caps |= NFS_CAP_ACLS;
2141                 if (res.has_links != 0)
2142                         server->caps |= NFS_CAP_HARDLINKS;
2143                 if (res.has_symlinks != 0)
2144                         server->caps |= NFS_CAP_SYMLINKS;
2145                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2146                         server->caps |= NFS_CAP_FILEID;
2147                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2148                         server->caps |= NFS_CAP_MODE;
2149                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2150                         server->caps |= NFS_CAP_NLINK;
2151                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2152                         server->caps |= NFS_CAP_OWNER;
2153                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2154                         server->caps |= NFS_CAP_OWNER_GROUP;
2155                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2156                         server->caps |= NFS_CAP_ATIME;
2157                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2158                         server->caps |= NFS_CAP_CTIME;
2159                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2160                         server->caps |= NFS_CAP_MTIME;
2161
2162                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2163                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2164                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2165                 server->acl_bitmask = res.acl_bitmask;
2166         }
2167
2168         return status;
2169 }
2170
2171 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2172 {
2173         struct nfs4_exception exception = { };
2174         int err;
2175         do {
2176                 err = nfs4_handle_exception(server,
2177                                 _nfs4_server_capabilities(server, fhandle),
2178                                 &exception);
2179         } while (exception.retry);
2180         return err;
2181 }
2182
2183 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2184                 struct nfs_fsinfo *info)
2185 {
2186         struct nfs4_lookup_root_arg args = {
2187                 .bitmask = nfs4_fattr_bitmap,
2188         };
2189         struct nfs4_lookup_res res = {
2190                 .server = server,
2191                 .fattr = info->fattr,
2192                 .fh = fhandle,
2193         };
2194         struct rpc_message msg = {
2195                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2196                 .rpc_argp = &args,
2197                 .rpc_resp = &res,
2198         };
2199
2200         nfs_fattr_init(info->fattr);
2201         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2202 }
2203
2204 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2205                 struct nfs_fsinfo *info)
2206 {
2207         struct nfs4_exception exception = { };
2208         int err;
2209         do {
2210                 err = _nfs4_lookup_root(server, fhandle, info);
2211                 switch (err) {
2212                 case 0:
2213                 case -NFS4ERR_WRONGSEC:
2214                         break;
2215                 default:
2216                         err = nfs4_handle_exception(server, err, &exception);
2217                 }
2218         } while (exception.retry);
2219         return err;
2220 }
2221
2222 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2223                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2224 {
2225         struct rpc_auth *auth;
2226         int ret;
2227
2228         auth = rpcauth_create(flavor, server->client);
2229         if (!auth) {
2230                 ret = -EIO;
2231                 goto out;
2232         }
2233         ret = nfs4_lookup_root(server, fhandle, info);
2234 out:
2235         return ret;
2236 }
2237
2238 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2239                               struct nfs_fsinfo *info)
2240 {
2241         int i, len, status = 0;
2242         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2243
2244         len = gss_mech_list_pseudoflavors(&flav_array[0]);
2245         flav_array[len] = RPC_AUTH_NULL;
2246         len += 1;
2247
2248         for (i = 0; i < len; i++) {
2249                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2250                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2251                         continue;
2252                 break;
2253         }
2254         /*
2255          * -EACCESS could mean that the user doesn't have correct permissions
2256          * to access the mount.  It could also mean that we tried to mount
2257          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2258          * existing mount programs don't handle -EACCES very well so it should
2259          * be mapped to -EPERM instead.
2260          */
2261         if (status == -EACCES)
2262                 status = -EPERM;
2263         return status;
2264 }
2265
2266 /*
2267  * get the file handle for the "/" directory on the server
2268  */
2269 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2270                               struct nfs_fsinfo *info)
2271 {
2272         int minor_version = server->nfs_client->cl_minorversion;
2273         int status = nfs4_lookup_root(server, fhandle, info);
2274         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2275                 /*
2276                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2277                  * by nfs4_map_errors() as this function exits.
2278                  */
2279                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2280         if (status == 0)
2281                 status = nfs4_server_capabilities(server, fhandle);
2282         if (status == 0)
2283                 status = nfs4_do_fsinfo(server, fhandle, info);
2284         return nfs4_map_errors(status);
2285 }
2286
2287 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2288 /*
2289  * Get locations and (maybe) other attributes of a referral.
2290  * Note that we'll actually follow the referral later when
2291  * we detect fsid mismatch in inode revalidation
2292  */
2293 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2294                              struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2295 {
2296         int status = -ENOMEM;
2297         struct page *page = NULL;
2298         struct nfs4_fs_locations *locations = NULL;
2299
2300         page = alloc_page(GFP_KERNEL);
2301         if (page == NULL)
2302                 goto out;
2303         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2304         if (locations == NULL)
2305                 goto out;
2306
2307         status = nfs4_proc_fs_locations(dir, name, locations, page);
2308         if (status != 0)
2309                 goto out;
2310         /* Make sure server returned a different fsid for the referral */
2311         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2312                 dprintk("%s: server did not return a different fsid for"
2313                         " a referral at %s\n", __func__, name->name);
2314                 status = -EIO;
2315                 goto out;
2316         }
2317         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2318         nfs_fixup_referral_attributes(&locations->fattr);
2319
2320         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2321         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2322         memset(fhandle, 0, sizeof(struct nfs_fh));
2323 out:
2324         if (page)
2325                 __free_page(page);
2326         kfree(locations);
2327         return status;
2328 }
2329
2330 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2331 {
2332         struct nfs4_getattr_arg args = {
2333                 .fh = fhandle,
2334                 .bitmask = server->attr_bitmask,
2335         };
2336         struct nfs4_getattr_res res = {
2337                 .fattr = fattr,
2338                 .server = server,
2339         };
2340         struct rpc_message msg = {
2341                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2342                 .rpc_argp = &args,
2343                 .rpc_resp = &res,
2344         };
2345         
2346         nfs_fattr_init(fattr);
2347         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2348 }
2349
2350 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2351 {
2352         struct nfs4_exception exception = { };
2353         int err;
2354         do {
2355                 err = nfs4_handle_exception(server,
2356                                 _nfs4_proc_getattr(server, fhandle, fattr),
2357                                 &exception);
2358         } while (exception.retry);
2359         return err;
2360 }
2361
2362 /* 
2363  * The file is not closed if it is opened due to the a request to change
2364  * the size of the file. The open call will not be needed once the
2365  * VFS layer lookup-intents are implemented.
2366  *
2367  * Close is called when the inode is destroyed.
2368  * If we haven't opened the file for O_WRONLY, we
2369  * need to in the size_change case to obtain a stateid.
2370  *
2371  * Got race?
2372  * Because OPEN is always done by name in nfsv4, it is
2373  * possible that we opened a different file by the same
2374  * name.  We can recognize this race condition, but we
2375  * can't do anything about it besides returning an error.
2376  *
2377  * This will be fixed with VFS changes (lookup-intent).
2378  */
2379 static int
2380 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2381                   struct iattr *sattr)
2382 {
2383         struct inode *inode = dentry->d_inode;
2384         struct rpc_cred *cred = NULL;
2385         struct nfs4_state *state = NULL;
2386         int status;
2387
2388         if (pnfs_ld_layoutret_on_setattr(inode))
2389                 pnfs_return_layout(inode);
2390
2391         nfs_fattr_init(fattr);
2392         
2393         /* Search for an existing open(O_WRITE) file */
2394         if (sattr->ia_valid & ATTR_FILE) {
2395                 struct nfs_open_context *ctx;
2396
2397                 ctx = nfs_file_open_context(sattr->ia_file);
2398                 if (ctx) {
2399                         cred = ctx->cred;
2400                         state = ctx->state;
2401                 }
2402         }
2403
2404         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2405         if (status == 0)
2406                 nfs_setattr_update_inode(inode, sattr);
2407         return status;
2408 }
2409
2410 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2411                 const struct nfs_fh *dirfh, const struct qstr *name,
2412                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2413 {
2414         int                    status;
2415         struct nfs4_lookup_arg args = {
2416                 .bitmask = server->attr_bitmask,
2417                 .dir_fh = dirfh,
2418                 .name = name,
2419         };
2420         struct nfs4_lookup_res res = {
2421                 .server = server,
2422                 .fattr = fattr,
2423                 .fh = fhandle,
2424         };
2425         struct rpc_message msg = {
2426                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2427                 .rpc_argp = &args,
2428                 .rpc_resp = &res,
2429         };
2430
2431         nfs_fattr_init(fattr);
2432
2433         dprintk("NFS call  lookupfh %s\n", name->name);
2434         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2435         dprintk("NFS reply lookupfh: %d\n", status);
2436         return status;
2437 }
2438
2439 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2440                               struct qstr *name, struct nfs_fh *fhandle,
2441                               struct nfs_fattr *fattr)
2442 {
2443         struct nfs4_exception exception = { };
2444         int err;
2445         do {
2446                 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2447                 /* FIXME: !!!! */
2448                 if (err == -NFS4ERR_MOVED) {
2449                         err = -EREMOTE;
2450                         break;
2451                 }
2452                 err = nfs4_handle_exception(server, err, &exception);
2453         } while (exception.retry);
2454         return err;
2455 }
2456
2457 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2458                 const struct qstr *name, struct nfs_fh *fhandle,
2459                 struct nfs_fattr *fattr)
2460 {
2461         int status;
2462         
2463         dprintk("NFS call  lookup %s\n", name->name);
2464         status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2465         if (status == -NFS4ERR_MOVED)
2466                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2467         dprintk("NFS reply lookup: %d\n", status);
2468         return status;
2469 }
2470
2471 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2472 {
2473         memset(fh, 0, sizeof(struct nfs_fh));
2474         fattr->fsid.major = 1;
2475         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2476                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2477         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2478         fattr->nlink = 2;
2479 }
2480
2481 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2482                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2483 {
2484         struct nfs4_exception exception = { };
2485         int err;
2486         do {
2487                 err = nfs4_handle_exception(NFS_SERVER(dir),
2488                                 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2489                                 &exception);
2490                 if (err == -EPERM)
2491                         nfs_fixup_secinfo_attributes(fattr, fhandle);
2492         } while (exception.retry);
2493         return err;
2494 }
2495
2496 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2497 {
2498         struct nfs_server *server = NFS_SERVER(inode);
2499         struct nfs4_accessargs args = {
2500                 .fh = NFS_FH(inode),
2501                 .bitmask = server->attr_bitmask,
2502         };
2503         struct nfs4_accessres res = {
2504                 .server = server,
2505         };
2506         struct rpc_message msg = {
2507                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2508                 .rpc_argp = &args,
2509                 .rpc_resp = &res,
2510                 .rpc_cred = entry->cred,
2511         };
2512         int mode = entry->mask;
2513         int status;
2514
2515         /*
2516          * Determine which access bits we want to ask for...
2517          */
2518         if (mode & MAY_READ)
2519                 args.access |= NFS4_ACCESS_READ;
2520         if (S_ISDIR(inode->i_mode)) {
2521                 if (mode & MAY_WRITE)
2522                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2523                 if (mode & MAY_EXEC)
2524                         args.access |= NFS4_ACCESS_LOOKUP;
2525         } else {
2526                 if (mode & MAY_WRITE)
2527                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2528                 if (mode & MAY_EXEC)
2529                         args.access |= NFS4_ACCESS_EXECUTE;
2530         }
2531
2532         res.fattr = nfs_alloc_fattr();
2533         if (res.fattr == NULL)
2534                 return -ENOMEM;
2535
2536         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2537         if (!status) {
2538                 entry->mask = 0;
2539                 if (res.access & NFS4_ACCESS_READ)
2540                         entry->mask |= MAY_READ;
2541                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2542                         entry->mask |= MAY_WRITE;
2543                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2544                         entry->mask |= MAY_EXEC;
2545                 nfs_refresh_inode(inode, res.fattr);
2546         }
2547         nfs_free_fattr(res.fattr);
2548         return status;
2549 }
2550
2551 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2552 {
2553         struct nfs4_exception exception = { };
2554         int err;
2555         do {
2556                 err = nfs4_handle_exception(NFS_SERVER(inode),
2557                                 _nfs4_proc_access(inode, entry),
2558                                 &exception);
2559         } while (exception.retry);
2560         return err;
2561 }
2562
2563 /*
2564  * TODO: For the time being, we don't try to get any attributes
2565  * along with any of the zero-copy operations READ, READDIR,
2566  * READLINK, WRITE.
2567  *
2568  * In the case of the first three, we want to put the GETATTR
2569  * after the read-type operation -- this is because it is hard
2570  * to predict the length of a GETATTR response in v4, and thus
2571  * align the READ data correctly.  This means that the GETATTR
2572  * may end up partially falling into the page cache, and we should
2573  * shift it into the 'tail' of the xdr_buf before processing.
2574  * To do this efficiently, we need to know the total length
2575  * of data received, which doesn't seem to be available outside
2576  * of the RPC layer.
2577  *
2578  * In the case of WRITE, we also want to put the GETATTR after
2579  * the operation -- in this case because we want to make sure
2580  * we get the post-operation mtime and size.  This means that
2581  * we can't use xdr_encode_pages() as written: we need a variant
2582  * of it which would leave room in the 'tail' iovec.
2583  *
2584  * Both of these changes to the XDR layer would in fact be quite
2585  * minor, but I decided to leave them for a subsequent patch.
2586  */
2587 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2588                 unsigned int pgbase, unsigned int pglen)
2589 {
2590         struct nfs4_readlink args = {
2591                 .fh       = NFS_FH(inode),
2592                 .pgbase   = pgbase,
2593                 .pglen    = pglen,
2594                 .pages    = &page,
2595         };
2596         struct nfs4_readlink_res res;
2597         struct rpc_message msg = {
2598                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2599                 .rpc_argp = &args,
2600                 .rpc_resp = &res,
2601         };
2602
2603         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2604 }
2605
2606 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2607                 unsigned int pgbase, unsigned int pglen)
2608 {
2609         struct nfs4_exception exception = { };
2610         int err;
2611         do {
2612                 err = nfs4_handle_exception(NFS_SERVER(inode),
2613                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2614                                 &exception);
2615         } while (exception.retry);
2616         return err;
2617 }
2618
2619 /*
2620  * Got race?
2621  * We will need to arrange for the VFS layer to provide an atomic open.
2622  * Until then, this create/open method is prone to inefficiency and race
2623  * conditions due to the lookup, create, and open VFS calls from sys_open()
2624  * placed on the wire.
2625  *
2626  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2627  * The file will be opened again in the subsequent VFS open call
2628  * (nfs4_proc_file_open).
2629  *
2630  * The open for read will just hang around to be used by any process that
2631  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2632  */
2633
2634 static int
2635 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2636                  int flags, struct nfs_open_context *ctx)
2637 {
2638         struct dentry *de = dentry;
2639         struct nfs4_state *state;
2640         struct rpc_cred *cred = NULL;
2641         fmode_t fmode = 0;
2642         int status = 0;
2643
2644         if (ctx != NULL) {
2645                 cred = ctx->cred;
2646                 de = ctx->dentry;
2647                 fmode = ctx->mode;
2648         }
2649         sattr->ia_mode &= ~current_umask();
2650         state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2651         d_drop(dentry);
2652         if (IS_ERR(state)) {
2653                 status = PTR_ERR(state);
2654                 goto out;
2655         }
2656         d_add(dentry, igrab(state->inode));
2657         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2658         if (ctx != NULL)
2659                 ctx->state = state;
2660         else
2661                 nfs4_close_sync(state, fmode);
2662 out:
2663         return status;
2664 }
2665
2666 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2667 {
2668         struct nfs_server *server = NFS_SERVER(dir);
2669         struct nfs_removeargs args = {
2670                 .fh = NFS_FH(dir),
2671                 .name.len = name->len,
2672                 .name.name = name->name,
2673                 .bitmask = server->attr_bitmask,
2674         };
2675         struct nfs_removeres res = {
2676                 .server = server,
2677         };
2678         struct rpc_message msg = {
2679                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2680                 .rpc_argp = &args,
2681                 .rpc_resp = &res,
2682         };
2683         int status = -ENOMEM;
2684
2685         res.dir_attr = nfs_alloc_fattr();
2686         if (res.dir_attr == NULL)
2687                 goto out;
2688
2689         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2690         if (status == 0) {
2691                 update_changeattr(dir, &res.cinfo);
2692                 nfs_post_op_update_inode(dir, res.dir_attr);
2693         }
2694         nfs_free_fattr(res.dir_attr);
2695 out:
2696         return status;
2697 }
2698
2699 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2700 {
2701         struct nfs4_exception exception = { };
2702         int err;
2703         do {
2704                 err = nfs4_handle_exception(NFS_SERVER(dir),
2705                                 _nfs4_proc_remove(dir, name),
2706                                 &exception);
2707         } while (exception.retry);
2708         return err;
2709 }
2710
2711 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2712 {
2713         struct nfs_server *server = NFS_SERVER(dir);
2714         struct nfs_removeargs *args = msg->rpc_argp;
2715         struct nfs_removeres *res = msg->rpc_resp;
2716
2717         args->bitmask = server->cache_consistency_bitmask;
2718         res->server = server;
2719         res->seq_res.sr_slot = NULL;
2720         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2721 }
2722
2723 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2724 {
2725         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2726
2727         if (!nfs4_sequence_done(task, &res->seq_res))
2728                 return 0;
2729         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2730                 return 0;
2731         update_changeattr(dir, &res->cinfo);
2732         nfs_post_op_update_inode(dir, res->dir_attr);
2733         return 1;
2734 }
2735
2736 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2737 {
2738         struct nfs_server *server = NFS_SERVER(dir);
2739         struct nfs_renameargs *arg = msg->rpc_argp;
2740         struct nfs_renameres *res = msg->rpc_resp;
2741
2742         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2743         arg->bitmask = server->attr_bitmask;
2744         res->server = server;
2745 }
2746
2747 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2748                                  struct inode *new_dir)
2749 {
2750         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2751
2752         if (!nfs4_sequence_done(task, &res->seq_res))
2753                 return 0;
2754         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2755                 return 0;
2756
2757         update_changeattr(old_dir, &res->old_cinfo);
2758         nfs_post_op_update_inode(old_dir, res->old_fattr);
2759         update_changeattr(new_dir, &res->new_cinfo);
2760         nfs_post_op_update_inode(new_dir, res->new_fattr);
2761         return 1;
2762 }
2763
2764 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2765                 struct inode *new_dir, struct qstr *new_name)
2766 {
2767         struct nfs_server *server = NFS_SERVER(old_dir);
2768         struct nfs_renameargs arg = {
2769                 .old_dir = NFS_FH(old_dir),
2770                 .new_dir = NFS_FH(new_dir),
2771                 .old_name = old_name,
2772                 .new_name = new_name,
2773                 .bitmask = server->attr_bitmask,
2774         };
2775         struct nfs_renameres res = {
2776                 .server = server,
2777         };
2778         struct rpc_message msg = {
2779                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2780                 .rpc_argp = &arg,
2781                 .rpc_resp = &res,
2782         };
2783         int status = -ENOMEM;
2784         
2785         res.old_fattr = nfs_alloc_fattr();
2786         res.new_fattr = nfs_alloc_fattr();
2787         if (res.old_fattr == NULL || res.new_fattr == NULL)
2788                 goto out;
2789
2790         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2791         if (!status) {
2792                 update_changeattr(old_dir, &res.old_cinfo);
2793                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2794                 update_changeattr(new_dir, &res.new_cinfo);
2795                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2796         }
2797 out:
2798         nfs_free_fattr(res.new_fattr);
2799         nfs_free_fattr(res.old_fattr);
2800         return status;
2801 }
2802
2803 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2804                 struct inode *new_dir, struct qstr *new_name)
2805 {
2806         struct nfs4_exception exception = { };
2807         int err;
2808         do {
2809                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2810                                 _nfs4_proc_rename(old_dir, old_name,
2811                                         new_dir, new_name),
2812                                 &exception);
2813         } while (exception.retry);
2814         return err;
2815 }
2816
2817 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2818 {
2819         struct nfs_server *server = NFS_SERVER(inode);
2820         struct nfs4_link_arg arg = {
2821                 .fh     = NFS_FH(inode),
2822                 .dir_fh = NFS_FH(dir),
2823                 .name   = name,
2824                 .bitmask = server->attr_bitmask,
2825         };
2826         struct nfs4_link_res res = {
2827                 .server = server,
2828         };
2829         struct rpc_message msg = {
2830                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2831                 .rpc_argp = &arg,
2832                 .rpc_resp = &res,
2833         };
2834         int status = -ENOMEM;
2835
2836         res.fattr = nfs_alloc_fattr();
2837         res.dir_attr = nfs_alloc_fattr();
2838         if (res.fattr == NULL || res.dir_attr == NULL)
2839                 goto out;
2840
2841         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2842         if (!status) {
2843                 update_changeattr(dir, &res.cinfo);
2844                 nfs_post_op_update_inode(dir, res.dir_attr);
2845                 nfs_post_op_update_inode(inode, res.fattr);
2846         }
2847 out:
2848         nfs_free_fattr(res.dir_attr);
2849         nfs_free_fattr(res.fattr);
2850         return status;
2851 }
2852
2853 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2854 {
2855         struct nfs4_exception exception = { };
2856         int err;
2857         do {
2858                 err = nfs4_handle_exception(NFS_SERVER(inode),
2859                                 _nfs4_proc_link(inode, dir, name),
2860                                 &exception);
2861         } while (exception.retry);
2862         return err;
2863 }
2864
2865 struct nfs4_createdata {
2866         struct rpc_message msg;
2867         struct nfs4_create_arg arg;
2868         struct nfs4_create_res res;
2869         struct nfs_fh fh;
2870         struct nfs_fattr fattr;
2871         struct nfs_fattr dir_fattr;
2872 };
2873
2874 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2875                 struct qstr *name, struct iattr *sattr, u32 ftype)
2876 {
2877         struct nfs4_createdata *data;
2878
2879         data = kzalloc(sizeof(*data), GFP_KERNEL);
2880         if (data != NULL) {
2881                 struct nfs_server *server = NFS_SERVER(dir);
2882
2883                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2884                 data->msg.rpc_argp = &data->arg;
2885                 data->msg.rpc_resp = &data->res;
2886                 data->arg.dir_fh = NFS_FH(dir);
2887                 data->arg.server = server;
2888                 data->arg.name = name;
2889                 data->arg.attrs = sattr;
2890                 data->arg.ftype = ftype;
2891                 data->arg.bitmask = server->attr_bitmask;
2892                 data->res.server = server;
2893                 data->res.fh = &data->fh;
2894                 data->res.fattr = &data->fattr;
2895                 data->res.dir_fattr = &data->dir_fattr;
2896                 nfs_fattr_init(data->res.fattr);
2897                 nfs_fattr_init(data->res.dir_fattr);
2898         }
2899         return data;
2900 }
2901
2902 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2903 {
2904         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2905                                     &data->arg.seq_args, &data->res.seq_res, 1);
2906         if (status == 0) {
2907                 update_changeattr(dir, &data->res.dir_cinfo);
2908                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2909                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2910         }
2911         return status;
2912 }
2913
2914 static void nfs4_free_createdata(struct nfs4_createdata *data)
2915 {
2916         kfree(data);
2917 }
2918
2919 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2920                 struct page *page, unsigned int len, struct iattr *sattr)
2921 {
2922         struct nfs4_createdata *data;
2923         int status = -ENAMETOOLONG;
2924
2925         if (len > NFS4_MAXPATHLEN)
2926                 goto out;
2927
2928         status = -ENOMEM;
2929         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2930         if (data == NULL)
2931                 goto out;
2932
2933         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2934         data->arg.u.symlink.pages = &page;
2935         data->arg.u.symlink.len = len;
2936         
2937         status = nfs4_do_create(dir, dentry, data);
2938
2939         nfs4_free_createdata(data);
2940 out:
2941         return status;
2942 }
2943
2944 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2945                 struct page *page, unsigned int len, struct iattr *sattr)
2946 {
2947         struct nfs4_exception exception = { };
2948         int err;
2949         do {
2950                 err = nfs4_handle_exception(NFS_SERVER(dir),
2951                                 _nfs4_proc_symlink(dir, dentry, page,
2952                                                         len, sattr),
2953                                 &exception);
2954         } while (exception.retry);
2955         return err;
2956 }
2957
2958 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2959                 struct iattr *sattr)
2960 {
2961         struct nfs4_createdata *data;
2962         int status = -ENOMEM;
2963
2964         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2965         if (data == NULL)
2966                 goto out;
2967
2968         status = nfs4_do_create(dir, dentry, data);
2969
2970         nfs4_free_createdata(data);
2971 out:
2972         return status;
2973 }
2974
2975 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2976                 struct iattr *sattr)
2977 {
2978         struct nfs4_exception exception = { };
2979         int err;
2980
2981         sattr->ia_mode &= ~current_umask();
2982         do {
2983                 err = nfs4_handle_exception(NFS_SERVER(dir),
2984                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2985                                 &exception);
2986         } while (exception.retry);
2987         return err;
2988 }
2989
2990 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2991                 u64 cookie, struct page **pages, unsigned int count, int plus)
2992 {
2993         struct inode            *dir = dentry->d_inode;
2994         struct nfs4_readdir_arg args = {
2995                 .fh = NFS_FH(dir),
2996                 .pages = pages,
2997                 .pgbase = 0,
2998                 .count = count,
2999                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3000                 .plus = plus,
3001         };
3002         struct nfs4_readdir_res res;
3003         struct rpc_message msg = {
3004                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3005                 .rpc_argp = &args,
3006                 .rpc_resp = &res,
3007                 .rpc_cred = cred,
3008         };
3009         int                     status;
3010
3011         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3012                         dentry->d_parent->d_name.name,
3013                         dentry->d_name.name,
3014                         (unsigned long long)cookie);
3015         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3016         res.pgbase = args.pgbase;
3017         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3018         if (status >= 0) {
3019                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3020                 status += args.pgbase;
3021         }
3022
3023         nfs_invalidate_atime(dir);
3024
3025         dprintk("%s: returns %d\n", __func__, status);
3026         return status;
3027 }
3028
3029 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3030                 u64 cookie, struct page **pages, unsigned int count, int plus)
3031 {
3032         struct nfs4_exception exception = { };
3033         int err;
3034         do {
3035                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3036                                 _nfs4_proc_readdir(dentry, cred, cookie,
3037                                         pages, count, plus),
3038                                 &exception);
3039         } while (exception.retry);
3040         return err;
3041 }
3042
3043 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3044                 struct iattr *sattr, dev_t rdev)
3045 {
3046         struct nfs4_createdata *data;
3047         int mode = sattr->ia_mode;
3048         int status = -ENOMEM;
3049
3050         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3051         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3052
3053         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3054         if (data == NULL)
3055                 goto out;
3056
3057         if (S_ISFIFO(mode))
3058                 data->arg.ftype = NF4FIFO;
3059         else if (S_ISBLK(mode)) {
3060                 data->arg.ftype = NF4BLK;
3061                 data->arg.u.device.specdata1 = MAJOR(rdev);
3062                 data->arg.u.device.specdata2 = MINOR(rdev);
3063         }
3064         else if (S_ISCHR(mode)) {
3065                 data->arg.ftype = NF4CHR;
3066                 data->arg.u.device.specdata1 = MAJOR(rdev);
3067                 data->arg.u.device.specdata2 = MINOR(rdev);
3068         }
3069         
3070         status = nfs4_do_create(dir, dentry, data);
3071
3072         nfs4_free_createdata(data);
3073 out:
3074         return status;
3075 }
3076
3077 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3078                 struct iattr *sattr, dev_t rdev)
3079 {
3080         struct nfs4_exception exception = { };
3081         int err;
3082
3083         sattr->ia_mode &= ~current_umask();
3084         do {
3085                 err = nfs4_handle_exception(NFS_SERVER(dir),
3086                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3087                                 &exception);
3088         } while (exception.retry);
3089         return err;
3090 }
3091
3092 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3093                  struct nfs_fsstat *fsstat)
3094 {
3095         struct nfs4_statfs_arg args = {
3096                 .fh = fhandle,
3097                 .bitmask = server->attr_bitmask,
3098         };
3099         struct nfs4_statfs_res res = {
3100                 .fsstat = fsstat,
3101         };
3102         struct rpc_message msg = {
3103                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3104                 .rpc_argp = &args,
3105                 .rpc_resp = &res,
3106         };
3107
3108         nfs_fattr_init(fsstat->fattr);
3109         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3110 }
3111
3112 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3113 {
3114         struct nfs4_exception exception = { };
3115         int err;
3116         do {
3117                 err = nfs4_handle_exception(server,
3118                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3119                                 &exception);
3120         } while (exception.retry);
3121         return err;
3122 }
3123
3124 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3125                 struct nfs_fsinfo *fsinfo)
3126 {
3127         struct nfs4_fsinfo_arg args = {
3128                 .fh = fhandle,
3129                 .bitmask = server->attr_bitmask,
3130         };
3131         struct nfs4_fsinfo_res res = {
3132                 .fsinfo = fsinfo,
3133         };
3134         struct rpc_message msg = {
3135                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3136                 .rpc_argp = &args,
3137                 .rpc_resp = &res,
3138         };
3139
3140         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3141 }
3142
3143 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3144 {
3145         struct nfs4_exception exception = { };
3146         int err;
3147
3148         do {
3149                 err = nfs4_handle_exception(server,
3150                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3151                                 &exception);
3152         } while (exception.retry);
3153         return err;
3154 }
3155
3156 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3157 {
3158         nfs_fattr_init(fsinfo->fattr);
3159         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3160 }
3161
3162 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3163                 struct nfs_pathconf *pathconf)
3164 {
3165         struct nfs4_pathconf_arg args = {
3166                 .fh = fhandle,
3167                 .bitmask = server->attr_bitmask,
3168         };
3169         struct nfs4_pathconf_res res = {
3170                 .pathconf = pathconf,
3171         };
3172         struct rpc_message msg = {
3173                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3174                 .rpc_argp = &args,
3175                 .rpc_resp = &res,
3176         };
3177
3178         /* None of the pathconf attributes are mandatory to implement */
3179         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3180                 memset(pathconf, 0, sizeof(*pathconf));
3181                 return 0;
3182         }
3183
3184         nfs_fattr_init(pathconf->fattr);
3185         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3186 }
3187
3188 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3189                 struct nfs_pathconf *pathconf)
3190 {
3191         struct nfs4_exception exception = { };
3192         int err;
3193
3194         do {
3195                 err = nfs4_handle_exception(server,
3196                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3197                                 &exception);
3198         } while (exception.retry);
3199         return err;
3200 }
3201
3202 void __nfs4_read_done_cb(struct nfs_read_data *data)
3203 {
3204         nfs_invalidate_atime(data->inode);
3205 }
3206
3207 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3208 {
3209         struct nfs_server *server = NFS_SERVER(data->inode);
3210
3211         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3212                 nfs_restart_rpc(task, server->nfs_client);
3213                 return -EAGAIN;
3214         }
3215
3216         __nfs4_read_done_cb(data);
3217         if (task->tk_status > 0)
3218                 renew_lease(server, data->timestamp);
3219         return 0;
3220 }
3221
3222 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3223 {
3224
3225         dprintk("--> %s\n", __func__);
3226
3227         if (!nfs4_sequence_done(task, &data->res.seq_res))
3228                 return -EAGAIN;
3229
3230         return data->read_done_cb ? data->read_done_cb(task, data) :
3231                                     nfs4_read_done_cb(task, data);
3232 }
3233
3234 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3235 {
3236         data->timestamp   = jiffies;
3237         data->read_done_cb = nfs4_read_done_cb;
3238         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3239 }
3240
3241 /* Reset the the nfs_read_data to send the read to the MDS. */
3242 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3243 {
3244         dprintk("%s Reset task for i/o through\n", __func__);
3245         put_lseg(data->lseg);
3246         data->lseg = NULL;
3247         /* offsets will differ in the dense stripe case */
3248         data->args.offset = data->mds_offset;
3249         data->ds_clp = NULL;
3250         data->args.fh     = NFS_FH(data->inode);
3251         data->read_done_cb = nfs4_read_done_cb;
3252         task->tk_ops = data->mds_ops;
3253         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3254 }
3255 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3256
3257 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3258 {
3259         struct inode *inode = data->inode;
3260         
3261         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3262                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3263                 return -EAGAIN;
3264         }
3265         if (task->tk_status >= 0) {
3266                 renew_lease(NFS_SERVER(inode), data->timestamp);
3267                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3268         }
3269         return 0;
3270 }
3271
3272 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3273 {
3274         if (!nfs4_sequence_done(task, &data->res.seq_res))
3275                 return -EAGAIN;
3276         return data->write_done_cb ? data->write_done_cb(task, data) :
3277                 nfs4_write_done_cb(task, data);
3278 }
3279
3280 /* Reset the the nfs_write_data to send the write to the MDS. */
3281 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3282 {
3283         dprintk("%s Reset task for i/o through\n", __func__);
3284         put_lseg(data->lseg);
3285         data->lseg          = NULL;
3286         data->ds_clp        = NULL;
3287         data->write_done_cb = nfs4_write_done_cb;
3288         data->args.fh       = NFS_FH(data->inode);
3289         data->args.bitmask  = data->res.server->cache_consistency_bitmask;
3290         data->args.offset   = data->mds_offset;
3291         data->res.fattr     = &data->fattr;
3292         task->tk_ops        = data->mds_ops;
3293         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3294 }
3295 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3296
3297 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3298 {
3299         struct nfs_server *server = NFS_SERVER(data->inode);
3300
3301         if (data->lseg) {
3302                 data->args.bitmask = NULL;
3303                 data->res.fattr = NULL;
3304         } else
3305                 data->args.bitmask = server->cache_consistency_bitmask;
3306         if (!data->write_done_cb)
3307                 data->write_done_cb = nfs4_write_done_cb;
3308         data->res.server = server;
3309         data->timestamp   = jiffies;
3310
3311         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3312 }
3313
3314 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3315 {
3316         struct inode *inode = data->inode;
3317
3318         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3319                 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3320                 return -EAGAIN;
3321         }
3322         nfs_refresh_inode(inode, data->res.fattr);
3323         return 0;
3324 }
3325
3326 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3327 {
3328         if (!nfs4_sequence_done(task, &data->res.seq_res))
3329                 return -EAGAIN;
3330         return data->write_done_cb(task, data);
3331 }
3332
3333 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3334 {
3335         struct nfs_server *server = NFS_SERVER(data->inode);
3336
3337         if (data->lseg) {
3338                 data->args.bitmask = NULL;
3339                 data->res.fattr = NULL;
3340         } else
3341                 data->args.bitmask = server->cache_consistency_bitmask;
3342         if (!data->write_done_cb)
3343                 data->write_done_cb = nfs4_commit_done_cb;
3344         data->res.server = server;
3345         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3346 }
3347
3348 struct nfs4_renewdata {
3349         struct nfs_client       *client;
3350         unsigned long           timestamp;
3351 };
3352
3353 /*
3354  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3355  * standalone procedure for queueing an asynchronous RENEW.
3356  */
3357 static void nfs4_renew_release(void *calldata)
3358 {
3359         struct nfs4_renewdata *data = calldata;
3360         struct nfs_client *clp = data->client;
3361
3362         if (atomic_read(&clp->cl_count) > 1)
3363                 nfs4_schedule_state_renewal(clp);
3364         nfs_put_client(clp);
3365         kfree(data);
3366 }
3367
3368 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3369 {
3370         struct nfs4_renewdata *data = calldata;
3371         struct nfs_client *clp = data->client;
3372         unsigned long timestamp = data->timestamp;
3373
3374         if (task->tk_status < 0) {
3375                 /* Unless we're shutting down, schedule state recovery! */
3376                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3377                         nfs4_schedule_lease_recovery(clp);
3378                 return;
3379         }
3380         do_renew_lease(clp, timestamp);
3381 }
3382
3383 static const struct rpc_call_ops nfs4_renew_ops = {
3384         .rpc_call_done = nfs4_renew_done,
3385         .rpc_release = nfs4_renew_release,
3386 };
3387
3388 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3389 {
3390         struct rpc_message msg = {
3391                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3392                 .rpc_argp       = clp,
3393                 .rpc_cred       = cred,
3394         };
3395         struct nfs4_renewdata *data;
3396
3397         if (!atomic_inc_not_zero(&clp->cl_count))
3398                 return -EIO;
3399         data = kmalloc(sizeof(*data), GFP_KERNEL);
3400         if (data == NULL)
3401                 return -ENOMEM;
3402         data->client = clp;
3403         data->timestamp = jiffies;
3404         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3405                         &nfs4_renew_ops, data);
3406 }
3407
3408 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3409 {
3410         struct rpc_message msg = {