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