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