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