NFSv4: Ensure that we check lock exclusive/shared type against open modes
[pandora-kernel.git] / fs / nfs / nfs4proc.c
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/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         default:
100                 dprintk("%s could not handle NFSv4 error %d\n",
101                                 __func__, -err);
102                 break;
103         }
104         return -EIO;
105 }
106
107 /*
108  * This is our standard bitmap for GETATTR requests.
109  */
110 const u32 nfs4_fattr_bitmap[2] = {
111         FATTR4_WORD0_TYPE
112         | FATTR4_WORD0_CHANGE
113         | FATTR4_WORD0_SIZE
114         | FATTR4_WORD0_FSID
115         | FATTR4_WORD0_FILEID,
116         FATTR4_WORD1_MODE
117         | FATTR4_WORD1_NUMLINKS
118         | FATTR4_WORD1_OWNER
119         | FATTR4_WORD1_OWNER_GROUP
120         | FATTR4_WORD1_RAWDEV
121         | FATTR4_WORD1_SPACE_USED
122         | FATTR4_WORD1_TIME_ACCESS
123         | FATTR4_WORD1_TIME_METADATA
124         | FATTR4_WORD1_TIME_MODIFY
125 };
126
127 const u32 nfs4_statfs_bitmap[2] = {
128         FATTR4_WORD0_FILES_AVAIL
129         | FATTR4_WORD0_FILES_FREE
130         | FATTR4_WORD0_FILES_TOTAL,
131         FATTR4_WORD1_SPACE_AVAIL
132         | FATTR4_WORD1_SPACE_FREE
133         | FATTR4_WORD1_SPACE_TOTAL
134 };
135
136 const u32 nfs4_pathconf_bitmap[2] = {
137         FATTR4_WORD0_MAXLINK
138         | FATTR4_WORD0_MAXNAME,
139         0
140 };
141
142 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
143                         | FATTR4_WORD0_MAXREAD
144                         | FATTR4_WORD0_MAXWRITE
145                         | FATTR4_WORD0_LEASE_TIME,
146                         FATTR4_WORD1_TIME_DELTA
147                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
148                         FATTR4_WORD2_LAYOUT_BLKSIZE
149 };
150
151 const u32 nfs4_fs_locations_bitmap[2] = {
152         FATTR4_WORD0_TYPE
153         | FATTR4_WORD0_CHANGE
154         | FATTR4_WORD0_SIZE
155         | FATTR4_WORD0_FSID
156         | FATTR4_WORD0_FILEID
157         | FATTR4_WORD0_FS_LOCATIONS,
158         FATTR4_WORD1_MODE
159         | FATTR4_WORD1_NUMLINKS
160         | FATTR4_WORD1_OWNER
161         | FATTR4_WORD1_OWNER_GROUP
162         | FATTR4_WORD1_RAWDEV
163         | FATTR4_WORD1_SPACE_USED
164         | FATTR4_WORD1_TIME_ACCESS
165         | FATTR4_WORD1_TIME_METADATA
166         | FATTR4_WORD1_TIME_MODIFY
167         | FATTR4_WORD1_MOUNTED_ON_FILEID
168 };
169
170 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
171                 struct nfs4_readdir_arg *readdir)
172 {
173         __be32 *start, *p;
174
175         BUG_ON(readdir->count < 80);
176         if (cookie > 2) {
177                 readdir->cookie = cookie;
178                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179                 return;
180         }
181
182         readdir->cookie = 0;
183         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184         if (cookie == 2)
185                 return;
186         
187         /*
188          * NFSv4 servers do not return entries for '.' and '..'
189          * Therefore, we fake these entries here.  We let '.'
190          * have cookie 0 and '..' have cookie 1.  Note that
191          * when talking to the server, we always send cookie 0
192          * instead of 1 or 2.
193          */
194         start = p = kmap_atomic(*readdir->pages, KM_USER0);
195         
196         if (cookie == 0) {
197                 *p++ = xdr_one;                                  /* next */
198                 *p++ = xdr_zero;                   /* cookie, first word */
199                 *p++ = xdr_one;                   /* cookie, second word */
200                 *p++ = xdr_one;                             /* entry len */
201                 memcpy(p, ".\0\0\0", 4);                        /* entry */
202                 p++;
203                 *p++ = xdr_one;                         /* bitmap length */
204                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
205                 *p++ = htonl(8);              /* attribute buffer length */
206                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
207         }
208         
209         *p++ = xdr_one;                                  /* next */
210         *p++ = xdr_zero;                   /* cookie, first word */
211         *p++ = xdr_two;                   /* cookie, second word */
212         *p++ = xdr_two;                             /* entry len */
213         memcpy(p, "..\0\0", 4);                         /* entry */
214         p++;
215         *p++ = xdr_one;                         /* bitmap length */
216         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
217         *p++ = htonl(8);              /* attribute buffer length */
218         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
219
220         readdir->pgbase = (char *)p - (char *)start;
221         readdir->count -= readdir->pgbase;
222         kunmap_atomic(start, KM_USER0);
223 }
224
225 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
226 {
227         int res;
228
229         might_sleep();
230
231         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
232                         nfs_wait_bit_killable, TASK_KILLABLE);
233         return res;
234 }
235
236 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
237 {
238         int res = 0;
239
240         might_sleep();
241
242         if (*timeout <= 0)
243                 *timeout = NFS4_POLL_RETRY_MIN;
244         if (*timeout > NFS4_POLL_RETRY_MAX)
245                 *timeout = NFS4_POLL_RETRY_MAX;
246         schedule_timeout_killable(*timeout);
247         if (fatal_signal_pending(current))
248                 res = -ERESTARTSYS;
249         *timeout <<= 1;
250         return res;
251 }
252
253 /* This is the error handling routine for processes that are allowed
254  * to sleep.
255  */
256 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
257 {
258         struct nfs_client *clp = server->nfs_client;
259         struct nfs4_state *state = exception->state;
260         struct inode *inode = exception->inode;
261         int ret = errorcode;
262
263         exception->retry = 0;
264         switch(errorcode) {
265                 case 0:
266                         return 0;
267                 case -NFS4ERR_OPENMODE:
268                         if (nfs_have_delegation(inode, FMODE_READ)) {
269                                 nfs_inode_return_delegation(inode);
270                                 exception->retry = 1;
271                                 return 0;
272                         }
273                         if (state == NULL)
274                                 break;
275                         nfs4_schedule_stateid_recovery(server, state);
276                         goto wait_on_recovery;
277                 case -NFS4ERR_DELEG_REVOKED:
278                 case -NFS4ERR_ADMIN_REVOKED:
279                 case -NFS4ERR_BAD_STATEID:
280                         if (state != NULL)
281                                 nfs_remove_bad_delegation(state->inode);
282                         if (state == NULL)
283                                 break;
284                         nfs4_schedule_stateid_recovery(server, state);
285                         goto wait_on_recovery;
286                 case -NFS4ERR_EXPIRED:
287                         if (state != NULL)
288                                 nfs4_schedule_stateid_recovery(server, state);
289                 case -NFS4ERR_STALE_STATEID:
290                 case -NFS4ERR_STALE_CLIENTID:
291                         nfs4_schedule_lease_recovery(clp);
292                         goto wait_on_recovery;
293 #if defined(CONFIG_NFS_V4_1)
294                 case -NFS4ERR_BADSESSION:
295                 case -NFS4ERR_BADSLOT:
296                 case -NFS4ERR_BAD_HIGH_SLOT:
297                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
298                 case -NFS4ERR_DEADSESSION:
299                 case -NFS4ERR_SEQ_FALSE_RETRY:
300                 case -NFS4ERR_SEQ_MISORDERED:
301                         dprintk("%s ERROR: %d Reset session\n", __func__,
302                                 errorcode);
303                         nfs4_schedule_session_recovery(clp->cl_session);
304                         exception->retry = 1;
305                         break;
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, 1, task))
1466                 return;
1467         rpc_call_start(task);
1468         return;
1469 unlock_no_action:
1470         rcu_read_unlock();
1471 out_no_action:
1472         task->tk_action = NULL;
1473
1474 }
1475
1476 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1477 {
1478         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1479         nfs4_open_prepare(task, calldata);
1480 }
1481
1482 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1483 {
1484         struct nfs4_opendata *data = calldata;
1485
1486         data->rpc_status = task->tk_status;
1487
1488         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1489                 return;
1490
1491         if (task->tk_status == 0) {
1492                 switch (data->o_res.f_attr->mode & S_IFMT) {
1493                         case S_IFREG:
1494                                 break;
1495                         case S_IFLNK:
1496                                 data->rpc_status = -ELOOP;
1497                                 break;
1498                         case S_IFDIR:
1499                                 data->rpc_status = -EISDIR;
1500                                 break;
1501                         default:
1502                                 data->rpc_status = -ENOTDIR;
1503                 }
1504                 renew_lease(data->o_res.server, data->timestamp);
1505                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1506                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1507         }
1508         data->rpc_done = 1;
1509 }
1510
1511 static void nfs4_open_release(void *calldata)
1512 {
1513         struct nfs4_opendata *data = calldata;
1514         struct nfs4_state *state = NULL;
1515
1516         /* If this request hasn't been cancelled, do nothing */
1517         if (data->cancelled == 0)
1518                 goto out_free;
1519         /* In case of error, no cleanup! */
1520         if (data->rpc_status != 0 || !data->rpc_done)
1521                 goto out_free;
1522         /* In case we need an open_confirm, no cleanup! */
1523         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1524                 goto out_free;
1525         state = nfs4_opendata_to_nfs4_state(data);
1526         if (!IS_ERR(state))
1527                 nfs4_close_state(state, data->o_arg.fmode);
1528 out_free:
1529         nfs4_opendata_put(data);
1530 }
1531
1532 static const struct rpc_call_ops nfs4_open_ops = {
1533         .rpc_call_prepare = nfs4_open_prepare,
1534         .rpc_call_done = nfs4_open_done,
1535         .rpc_release = nfs4_open_release,
1536 };
1537
1538 static const struct rpc_call_ops nfs4_recover_open_ops = {
1539         .rpc_call_prepare = nfs4_recover_open_prepare,
1540         .rpc_call_done = nfs4_open_done,
1541         .rpc_release = nfs4_open_release,
1542 };
1543
1544 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1545 {
1546         struct inode *dir = data->dir->d_inode;
1547         struct nfs_server *server = NFS_SERVER(dir);
1548         struct nfs_openargs *o_arg = &data->o_arg;
1549         struct nfs_openres *o_res = &data->o_res;
1550         struct rpc_task *task;
1551         struct rpc_message msg = {
1552                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1553                 .rpc_argp = o_arg,
1554                 .rpc_resp = o_res,
1555                 .rpc_cred = data->owner->so_cred,
1556         };
1557         struct rpc_task_setup task_setup_data = {
1558                 .rpc_client = server->client,
1559                 .rpc_message = &msg,
1560                 .callback_ops = &nfs4_open_ops,
1561                 .callback_data = data,
1562                 .workqueue = nfsiod_workqueue,
1563                 .flags = RPC_TASK_ASYNC,
1564         };
1565         int status;
1566
1567         kref_get(&data->kref);
1568         data->rpc_done = 0;
1569         data->rpc_status = 0;
1570         data->cancelled = 0;
1571         if (isrecover)
1572                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1573         task = rpc_run_task(&task_setup_data);
1574         if (IS_ERR(task))
1575                 return PTR_ERR(task);
1576         status = nfs4_wait_for_completion_rpc_task(task);
1577         if (status != 0) {
1578                 data->cancelled = 1;
1579                 smp_wmb();
1580         } else
1581                 status = data->rpc_status;
1582         rpc_put_task(task);
1583
1584         return status;
1585 }
1586
1587 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1588 {
1589         struct inode *dir = data->dir->d_inode;
1590         struct nfs_openres *o_res = &data->o_res;
1591         int status;
1592
1593         status = nfs4_run_open_task(data, 1);
1594         if (status != 0 || !data->rpc_done)
1595                 return status;
1596
1597         nfs_refresh_inode(dir, o_res->dir_attr);
1598
1599         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1600                 status = _nfs4_proc_open_confirm(data);
1601                 if (status != 0)
1602                         return status;
1603         }
1604
1605         return status;
1606 }
1607
1608 /*
1609  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1610  */
1611 static int _nfs4_proc_open(struct nfs4_opendata *data)
1612 {
1613         struct inode *dir = data->dir->d_inode;
1614         struct nfs_server *server = NFS_SERVER(dir);
1615         struct nfs_openargs *o_arg = &data->o_arg;
1616         struct nfs_openres *o_res = &data->o_res;
1617         int status;
1618
1619         status = nfs4_run_open_task(data, 0);
1620         if (!data->rpc_done)
1621                 return status;
1622         if (status != 0) {
1623                 if (status == -NFS4ERR_BADNAME &&
1624                                 !(o_arg->open_flags & O_CREAT))
1625                         return -ENOENT;
1626                 return status;
1627         }
1628
1629         if (o_arg->open_flags & O_CREAT) {
1630                 update_changeattr(dir, &o_res->cinfo);
1631                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1632         } else
1633                 nfs_refresh_inode(dir, o_res->dir_attr);
1634         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1635                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1636         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1637                 status = _nfs4_proc_open_confirm(data);
1638                 if (status != 0)
1639                         return status;
1640         }
1641         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1642                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1643         return 0;
1644 }
1645
1646 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1647 {
1648         unsigned int loop;
1649         int ret;
1650
1651         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1652                 ret = nfs4_wait_clnt_recover(clp);
1653                 if (ret != 0)
1654                         break;
1655                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1656                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1657                         break;
1658                 nfs4_schedule_state_manager(clp);
1659                 ret = -EIO;
1660         }
1661         return ret;
1662 }
1663
1664 static int nfs4_recover_expired_lease(struct nfs_server *server)
1665 {
1666         return nfs4_client_recover_expired_lease(server->nfs_client);
1667 }
1668
1669 /*
1670  * OPEN_EXPIRED:
1671  *      reclaim state on the server after a network partition.
1672  *      Assumes caller holds the appropriate lock
1673  */
1674 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1675 {
1676         struct nfs4_opendata *opendata;
1677         int ret;
1678
1679         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1680         if (IS_ERR(opendata))
1681                 return PTR_ERR(opendata);
1682         ret = nfs4_open_recover(opendata, state);
1683         if (ret == -ESTALE)
1684                 d_drop(ctx->dentry);
1685         nfs4_opendata_put(opendata);
1686         return ret;
1687 }
1688
1689 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1690 {
1691         struct nfs_server *server = NFS_SERVER(state->inode);
1692         struct nfs4_exception exception = { };
1693         int err;
1694
1695         do {
1696                 err = _nfs4_open_expired(ctx, state);
1697                 switch (err) {
1698                 default:
1699                         goto out;
1700                 case -NFS4ERR_GRACE:
1701                 case -NFS4ERR_DELAY:
1702                         nfs4_handle_exception(server, err, &exception);
1703                         err = 0;
1704                 }
1705         } while (exception.retry);
1706 out:
1707         return err;
1708 }
1709
1710 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1711 {
1712         struct nfs_open_context *ctx;
1713         int ret;
1714
1715         ctx = nfs4_state_find_open_context(state);
1716         if (IS_ERR(ctx))
1717                 return PTR_ERR(ctx);
1718         ret = nfs4_do_open_expired(ctx, state);
1719         put_nfs_open_context(ctx);
1720         return ret;
1721 }
1722
1723 #if defined(CONFIG_NFS_V4_1)
1724 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1725 {
1726         int status;
1727         struct nfs_server *server = NFS_SERVER(state->inode);
1728
1729         status = nfs41_test_stateid(server, state);
1730         if (status == NFS_OK)
1731                 return 0;
1732         nfs41_free_stateid(server, state);
1733         return nfs4_open_expired(sp, state);
1734 }
1735 #endif
1736
1737 /*
1738  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1739  * fields corresponding to attributes that were used to store the verifier.
1740  * Make sure we clobber those fields in the later setattr call
1741  */
1742 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1743 {
1744         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1745             !(sattr->ia_valid & ATTR_ATIME_SET))
1746                 sattr->ia_valid |= ATTR_ATIME;
1747
1748         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1749             !(sattr->ia_valid & ATTR_MTIME_SET))
1750                 sattr->ia_valid |= ATTR_MTIME;
1751 }
1752
1753 /*
1754  * Returns a referenced nfs4_state
1755  */
1756 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)
1757 {
1758         struct nfs4_state_owner  *sp;
1759         struct nfs4_state     *state = NULL;
1760         struct nfs_server       *server = NFS_SERVER(dir);
1761         struct nfs4_opendata *opendata;
1762         int status;
1763
1764         /* Protect against reboot recovery conflicts */
1765         status = -ENOMEM;
1766         if (!(sp = nfs4_get_state_owner(server, cred))) {
1767                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1768                 goto out_err;
1769         }
1770         status = nfs4_recover_expired_lease(server);
1771         if (status != 0)
1772                 goto err_put_state_owner;
1773         if (dentry->d_inode != NULL)
1774                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1775         status = -ENOMEM;
1776         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1777         if (opendata == NULL)
1778                 goto err_put_state_owner;
1779
1780         if (dentry->d_inode != NULL)
1781                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1782
1783         status = _nfs4_proc_open(opendata);
1784         if (status != 0)
1785                 goto err_opendata_put;
1786
1787         state = nfs4_opendata_to_nfs4_state(opendata);
1788         status = PTR_ERR(state);
1789         if (IS_ERR(state))
1790                 goto err_opendata_put;
1791         if (server->caps & NFS_CAP_POSIX_LOCK)
1792                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1793
1794         if (opendata->o_arg.open_flags & O_EXCL) {
1795                 nfs4_exclusive_attrset(opendata, sattr);
1796
1797                 nfs_fattr_init(opendata->o_res.f_attr);
1798                 status = nfs4_do_setattr(state->inode, cred,
1799                                 opendata->o_res.f_attr, sattr,
1800                                 state);
1801                 if (status == 0)
1802                         nfs_setattr_update_inode(state->inode, sattr);
1803                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1804         }
1805         nfs4_opendata_put(opendata);
1806         nfs4_put_state_owner(sp);
1807         *res = state;
1808         return 0;
1809 err_opendata_put:
1810         nfs4_opendata_put(opendata);
1811 err_put_state_owner:
1812         nfs4_put_state_owner(sp);
1813 out_err:
1814         *res = NULL;
1815         return status;
1816 }
1817
1818
1819 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)
1820 {
1821         struct nfs4_exception exception = { };
1822         struct nfs4_state *res;
1823         int status;
1824
1825         do {
1826                 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1827                 if (status == 0)
1828                         break;
1829                 /* NOTE: BAD_SEQID means the server and client disagree about the
1830                  * book-keeping w.r.t. state-changing operations
1831                  * (OPEN/CLOSE/LOCK/LOCKU...)
1832                  * It is actually a sign of a bug on the client or on the server.
1833                  *
1834                  * If we receive a BAD_SEQID error in the particular case of
1835                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1836                  * have unhashed the old state_owner for us, and that we can
1837                  * therefore safely retry using a new one. We should still warn
1838                  * the user though...
1839                  */
1840                 if (status == -NFS4ERR_BAD_SEQID) {
1841                         pr_warn_ratelimited("NFS: v4 server %s "
1842                                         " returned a bad sequence-id error!\n",
1843                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1844                         exception.retry = 1;
1845                         continue;
1846                 }
1847                 /*
1848                  * BAD_STATEID on OPEN means that the server cancelled our
1849                  * state before it received the OPEN_CONFIRM.
1850                  * Recover by retrying the request as per the discussion
1851                  * on Page 181 of RFC3530.
1852                  */
1853                 if (status == -NFS4ERR_BAD_STATEID) {
1854                         exception.retry = 1;
1855                         continue;
1856                 }
1857                 if (status == -EAGAIN) {
1858                         /* We must have found a delegation */
1859                         exception.retry = 1;
1860                         continue;
1861                 }
1862                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1863                                         status, &exception));
1864         } while (exception.retry);
1865         return res;
1866 }
1867
1868 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1869                             struct nfs_fattr *fattr, struct iattr *sattr,
1870                             struct nfs4_state *state)
1871 {
1872         struct nfs_server *server = NFS_SERVER(inode);
1873         struct nfs_setattrargs  arg = {
1874                 .fh             = NFS_FH(inode),
1875                 .iap            = sattr,
1876                 .server         = server,
1877                 .bitmask = server->attr_bitmask,
1878         };
1879         struct nfs_setattrres  res = {
1880                 .fattr          = fattr,
1881                 .server         = server,
1882         };
1883         struct rpc_message msg = {
1884                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1885                 .rpc_argp       = &arg,
1886                 .rpc_resp       = &res,
1887                 .rpc_cred       = cred,
1888         };
1889         unsigned long timestamp = jiffies;
1890         int status;
1891
1892         nfs_fattr_init(fattr);
1893
1894         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1895                 /* Use that stateid */
1896         } else if (state != NULL) {
1897                 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1898         } else
1899                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1900
1901         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1902         if (status == 0 && state != NULL)
1903                 renew_lease(server, timestamp);
1904         return status;
1905 }
1906
1907 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908                            struct nfs_fattr *fattr, struct iattr *sattr,
1909                            struct nfs4_state *state)
1910 {
1911         struct nfs_server *server = NFS_SERVER(inode);
1912         struct nfs4_exception exception = {
1913                 .state = state,
1914                 .inode = inode,
1915         };
1916         int err;
1917         do {
1918                 err = nfs4_handle_exception(server,
1919                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1920                                 &exception);
1921         } while (exception.retry);
1922         return err;
1923 }
1924
1925 struct nfs4_closedata {
1926         struct inode *inode;
1927         struct nfs4_state *state;
1928         struct nfs_closeargs arg;
1929         struct nfs_closeres res;
1930         struct nfs_fattr fattr;
1931         unsigned long timestamp;
1932         bool roc;
1933         u32 roc_barrier;
1934 };
1935
1936 static void nfs4_free_closedata(void *data)
1937 {
1938         struct nfs4_closedata *calldata = data;
1939         struct nfs4_state_owner *sp = calldata->state->owner;
1940         struct super_block *sb = calldata->state->inode->i_sb;
1941
1942         if (calldata->roc)
1943                 pnfs_roc_release(calldata->state->inode);
1944         nfs4_put_open_state(calldata->state);
1945         nfs_free_seqid(calldata->arg.seqid);
1946         nfs4_put_state_owner(sp);
1947         nfs_sb_deactive(sb);
1948         kfree(calldata);
1949 }
1950
1951 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1952                 fmode_t fmode)
1953 {
1954         spin_lock(&state->owner->so_lock);
1955         if (!(fmode & FMODE_READ))
1956                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1957         if (!(fmode & FMODE_WRITE))
1958                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1959         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1960         spin_unlock(&state->owner->so_lock);
1961 }
1962
1963 static void nfs4_close_done(struct rpc_task *task, void *data)
1964 {
1965         struct nfs4_closedata *calldata = data;
1966         struct nfs4_state *state = calldata->state;
1967         struct nfs_server *server = NFS_SERVER(calldata->inode);
1968
1969         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1970                 return;
1971         /* hmm. we are done with the inode, and in the process of freeing
1972          * the state_owner. we keep this around to process errors
1973          */
1974         switch (task->tk_status) {
1975                 case 0:
1976                         if (calldata->roc)
1977                                 pnfs_roc_set_barrier(state->inode,
1978                                                      calldata->roc_barrier);
1979                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1980                         renew_lease(server, calldata->timestamp);
1981                         nfs4_close_clear_stateid_flags(state,
1982                                         calldata->arg.fmode);
1983                         break;
1984                 case -NFS4ERR_STALE_STATEID:
1985                 case -NFS4ERR_OLD_STATEID:
1986                 case -NFS4ERR_BAD_STATEID:
1987                 case -NFS4ERR_EXPIRED:
1988                         if (calldata->arg.fmode == 0)
1989                                 break;
1990                 default:
1991                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1992                                 rpc_restart_call_prepare(task);
1993         }
1994         nfs_release_seqid(calldata->arg.seqid);
1995         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1996 }
1997
1998 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1999 {
2000         struct nfs4_closedata *calldata = data;
2001         struct nfs4_state *state = calldata->state;
2002         int call_close = 0;
2003
2004         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2005                 return;
2006
2007         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2008         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2009         spin_lock(&state->owner->so_lock);
2010         /* Calculate the change in open mode */
2011         if (state->n_rdwr == 0) {
2012                 if (state->n_rdonly == 0) {
2013                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2014                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2015                         calldata->arg.fmode &= ~FMODE_READ;
2016                 }
2017                 if (state->n_wronly == 0) {
2018                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2019                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2020                         calldata->arg.fmode &= ~FMODE_WRITE;
2021                 }
2022         }
2023         spin_unlock(&state->owner->so_lock);
2024
2025         if (!call_close) {
2026                 /* Note: exit _without_ calling nfs4_close_done */
2027                 task->tk_action = NULL;
2028                 return;
2029         }
2030
2031         if (calldata->arg.fmode == 0) {
2032                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2033                 if (calldata->roc &&
2034                     pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2035                         rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2036                                      task, NULL);
2037                         return;
2038                 }
2039         }
2040
2041         nfs_fattr_init(calldata->res.fattr);
2042         calldata->timestamp = jiffies;
2043         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2044                                 &calldata->arg.seq_args, &calldata->res.seq_res,
2045                                 1, task))
2046                 return;
2047         rpc_call_start(task);
2048 }
2049
2050 static const struct rpc_call_ops nfs4_close_ops = {
2051         .rpc_call_prepare = nfs4_close_prepare,
2052         .rpc_call_done = nfs4_close_done,
2053         .rpc_release = nfs4_free_closedata,
2054 };
2055
2056 /* 
2057  * It is possible for data to be read/written from a mem-mapped file 
2058  * after the sys_close call (which hits the vfs layer as a flush).
2059  * This means that we can't safely call nfsv4 close on a file until 
2060  * the inode is cleared. This in turn means that we are not good
2061  * NFSv4 citizens - we do not indicate to the server to update the file's 
2062  * share state even when we are done with one of the three share 
2063  * stateid's in the inode.
2064  *
2065  * NOTE: Caller must be holding the sp->so_owner semaphore!
2066  */
2067 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2068 {
2069         struct nfs_server *server = NFS_SERVER(state->inode);
2070         struct nfs4_closedata *calldata;
2071         struct nfs4_state_owner *sp = state->owner;
2072         struct rpc_task *task;
2073         struct rpc_message msg = {
2074                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2075                 .rpc_cred = state->owner->so_cred,
2076         };
2077         struct rpc_task_setup task_setup_data = {
2078                 .rpc_client = server->client,
2079                 .rpc_message = &msg,
2080                 .callback_ops = &nfs4_close_ops,
2081                 .workqueue = nfsiod_workqueue,
2082                 .flags = RPC_TASK_ASYNC,
2083         };
2084         int status = -ENOMEM;
2085
2086         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2087         if (calldata == NULL)
2088                 goto out;
2089         calldata->inode = state->inode;
2090         calldata->state = state;
2091         calldata->arg.fh = NFS_FH(state->inode);
2092         calldata->arg.stateid = &state->open_stateid;
2093         /* Serialization for the sequence id */
2094         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2095         if (calldata->arg.seqid == NULL)
2096                 goto out_free_calldata;
2097         calldata->arg.fmode = 0;
2098         calldata->arg.bitmask = server->cache_consistency_bitmask;
2099         calldata->res.fattr = &calldata->fattr;
2100         calldata->res.seqid = calldata->arg.seqid;
2101         calldata->res.server = server;
2102         calldata->roc = roc;
2103         nfs_sb_active(calldata->inode->i_sb);
2104
2105         msg.rpc_argp = &calldata->arg;
2106         msg.rpc_resp = &calldata->res;
2107         task_setup_data.callback_data = calldata;
2108         task = rpc_run_task(&task_setup_data);
2109         if (IS_ERR(task))
2110                 return PTR_ERR(task);
2111         status = 0;
2112         if (wait)
2113                 status = rpc_wait_for_completion_task(task);
2114         rpc_put_task(task);
2115         return status;
2116 out_free_calldata:
2117         kfree(calldata);
2118 out:
2119         if (roc)
2120                 pnfs_roc_release(state->inode);
2121         nfs4_put_open_state(state);
2122         nfs4_put_state_owner(sp);
2123         return status;
2124 }
2125
2126 static struct inode *
2127 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2128 {
2129         struct nfs4_state *state;
2130
2131         /* Protect against concurrent sillydeletes */
2132         state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2133         if (IS_ERR(state))
2134                 return ERR_CAST(state);
2135         ctx->state = state;
2136         return igrab(state->inode);
2137 }
2138
2139 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2140 {
2141         if (ctx->state == NULL)
2142                 return;
2143         if (is_sync)
2144                 nfs4_close_sync(ctx->state, ctx->mode);
2145         else
2146                 nfs4_close_state(ctx->state, ctx->mode);
2147 }
2148
2149 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2150 {
2151         struct nfs4_server_caps_arg args = {
2152                 .fhandle = fhandle,
2153         };
2154         struct nfs4_server_caps_res res = {};
2155         struct rpc_message msg = {
2156                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2157                 .rpc_argp = &args,
2158                 .rpc_resp = &res,
2159         };
2160         int status;
2161
2162         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2163         if (status == 0) {
2164                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2165                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2166                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2167                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2168                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2169                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2170                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2171                         server->caps |= NFS_CAP_ACLS;
2172                 if (res.has_links != 0)
2173                         server->caps |= NFS_CAP_HARDLINKS;
2174                 if (res.has_symlinks != 0)
2175                         server->caps |= NFS_CAP_SYMLINKS;
2176                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2177                         server->caps |= NFS_CAP_FILEID;
2178                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2179                         server->caps |= NFS_CAP_MODE;
2180                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2181                         server->caps |= NFS_CAP_NLINK;
2182                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2183                         server->caps |= NFS_CAP_OWNER;
2184                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2185                         server->caps |= NFS_CAP_OWNER_GROUP;
2186                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2187                         server->caps |= NFS_CAP_ATIME;
2188                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2189                         server->caps |= NFS_CAP_CTIME;
2190                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2191                         server->caps |= NFS_CAP_MTIME;
2192
2193                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2194                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2195                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2196                 server->acl_bitmask = res.acl_bitmask;
2197         }
2198
2199         return status;
2200 }
2201
2202 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2203 {
2204         struct nfs4_exception exception = { };
2205         int err;
2206         do {
2207                 err = nfs4_handle_exception(server,
2208                                 _nfs4_server_capabilities(server, fhandle),
2209                                 &exception);
2210         } while (exception.retry);
2211         return err;
2212 }
2213
2214 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2215                 struct nfs_fsinfo *info)
2216 {
2217         struct nfs4_lookup_root_arg args = {
2218                 .bitmask = nfs4_fattr_bitmap,
2219         };
2220         struct nfs4_lookup_res res = {
2221                 .server = server,
2222                 .fattr = info->fattr,
2223                 .fh = fhandle,
2224         };
2225         struct rpc_message msg = {
2226                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2227                 .rpc_argp = &args,
2228                 .rpc_resp = &res,
2229         };
2230
2231         nfs_fattr_init(info->fattr);
2232         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2233 }
2234
2235 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2236                 struct nfs_fsinfo *info)
2237 {
2238         struct nfs4_exception exception = { };
2239         int err;
2240         do {
2241                 err = _nfs4_lookup_root(server, fhandle, info);
2242                 switch (err) {
2243                 case 0:
2244                 case -NFS4ERR_WRONGSEC:
2245                         goto out;
2246                 default:
2247                         err = nfs4_handle_exception(server, err, &exception);
2248                 }
2249         } while (exception.retry);
2250 out:
2251         return err;
2252 }
2253
2254 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2255                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2256 {
2257         struct rpc_auth *auth;
2258         int ret;
2259
2260         auth = rpcauth_create(flavor, server->client);
2261         if (!auth) {
2262                 ret = -EIO;
2263                 goto out;
2264         }
2265         ret = nfs4_lookup_root(server, fhandle, info);
2266 out:
2267         return ret;
2268 }
2269
2270 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2271                               struct nfs_fsinfo *info)
2272 {
2273         int i, len, status = 0;
2274         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2275
2276         len = gss_mech_list_pseudoflavors(&flav_array[0]);
2277         flav_array[len] = RPC_AUTH_NULL;
2278         len += 1;
2279
2280         for (i = 0; i < len; i++) {
2281                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2282                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2283                         continue;
2284                 break;
2285         }
2286         /*
2287          * -EACCESS could mean that the user doesn't have correct permissions
2288          * to access the mount.  It could also mean that we tried to mount
2289          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2290          * existing mount programs don't handle -EACCES very well so it should
2291          * be mapped to -EPERM instead.
2292          */
2293         if (status == -EACCES)
2294                 status = -EPERM;
2295         return status;
2296 }
2297
2298 /*
2299  * get the file handle for the "/" directory on the server
2300  */
2301 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2302                               struct nfs_fsinfo *info)
2303 {
2304         int minor_version = server->nfs_client->cl_minorversion;
2305         int status = nfs4_lookup_root(server, fhandle, info);
2306         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2307                 /*
2308                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2309                  * by nfs4_map_errors() as this function exits.
2310                  */
2311                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2312         if (status == 0)
2313                 status = nfs4_server_capabilities(server, fhandle);
2314         if (status == 0)
2315                 status = nfs4_do_fsinfo(server, fhandle, info);
2316         return nfs4_map_errors(status);
2317 }
2318
2319 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2320 /*
2321  * Get locations and (maybe) other attributes of a referral.
2322  * Note that we'll actually follow the referral later when
2323  * we detect fsid mismatch in inode revalidation
2324  */
2325 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2326                              struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2327 {
2328         int status = -ENOMEM;
2329         struct page *page = NULL;
2330         struct nfs4_fs_locations *locations = NULL;
2331
2332         page = alloc_page(GFP_KERNEL);
2333         if (page == NULL)
2334                 goto out;
2335         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2336         if (locations == NULL)
2337                 goto out;
2338
2339         status = nfs4_proc_fs_locations(dir, name, locations, page);
2340         if (status != 0)
2341                 goto out;
2342         /* Make sure server returned a different fsid for the referral */
2343         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2344                 dprintk("%s: server did not return a different fsid for"
2345                         " a referral at %s\n", __func__, name->name);
2346                 status = -EIO;
2347                 goto out;
2348         }
2349         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2350         nfs_fixup_referral_attributes(&locations->fattr);
2351
2352         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2353         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2354         memset(fhandle, 0, sizeof(struct nfs_fh));
2355 out:
2356         if (page)
2357                 __free_page(page);
2358         kfree(locations);
2359         return status;
2360 }
2361
2362 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2363 {
2364         struct nfs4_getattr_arg args = {
2365                 .fh = fhandle,
2366                 .bitmask = server->attr_bitmask,
2367         };
2368         struct nfs4_getattr_res res = {
2369                 .fattr = fattr,
2370                 .server = server,
2371         };
2372         struct rpc_message msg = {
2373                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2374                 .rpc_argp = &args,
2375                 .rpc_resp = &res,
2376         };
2377         
2378         nfs_fattr_init(fattr);
2379         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2380 }
2381
2382 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2383 {
2384         struct nfs4_exception exception = { };
2385         int err;
2386         do {
2387                 err = nfs4_handle_exception(server,
2388                                 _nfs4_proc_getattr(server, fhandle, fattr),
2389                                 &exception);
2390         } while (exception.retry);
2391         return err;
2392 }
2393
2394 /* 
2395  * The file is not closed if it is opened due to the a request to change
2396  * the size of the file. The open call will not be needed once the
2397  * VFS layer lookup-intents are implemented.
2398  *
2399  * Close is called when the inode is destroyed.
2400  * If we haven't opened the file for O_WRONLY, we
2401  * need to in the size_change case to obtain a stateid.
2402  *
2403  * Got race?
2404  * Because OPEN is always done by name in nfsv4, it is
2405  * possible that we opened a different file by the same
2406  * name.  We can recognize this race condition, but we
2407  * can't do anything about it besides returning an error.
2408  *
2409  * This will be fixed with VFS changes (lookup-intent).
2410  */
2411 static int
2412 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2413                   struct iattr *sattr)
2414 {
2415         struct inode *inode = dentry->d_inode;
2416         struct rpc_cred *cred = NULL;
2417         struct nfs4_state *state = NULL;
2418         int status;
2419
2420         if (pnfs_ld_layoutret_on_setattr(inode))
2421                 pnfs_return_layout(inode);
2422
2423         nfs_fattr_init(fattr);
2424         
2425         /* Search for an existing open(O_WRITE) file */
2426         if (sattr->ia_valid & ATTR_FILE) {
2427                 struct nfs_open_context *ctx;
2428
2429                 ctx = nfs_file_open_context(sattr->ia_file);
2430                 if (ctx) {
2431                         cred = ctx->cred;
2432                         state = ctx->state;
2433                 }
2434         }
2435
2436         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2437         if (status == 0)
2438                 nfs_setattr_update_inode(inode, sattr);
2439         return status;
2440 }
2441
2442 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2443                 const struct qstr *name, struct nfs_fh *fhandle,
2444                 struct nfs_fattr *fattr)
2445 {
2446         struct nfs_server *server = NFS_SERVER(dir);
2447         int                    status;
2448         struct nfs4_lookup_arg args = {
2449                 .bitmask = server->attr_bitmask,
2450                 .dir_fh = NFS_FH(dir),
2451                 .name = name,
2452         };
2453         struct nfs4_lookup_res res = {
2454                 .server = server,
2455                 .fattr = fattr,
2456                 .fh = fhandle,
2457         };
2458         struct rpc_message msg = {
2459                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2460                 .rpc_argp = &args,
2461                 .rpc_resp = &res,
2462         };
2463
2464         nfs_fattr_init(fattr);
2465
2466         dprintk("NFS call  lookup %s\n", name->name);
2467         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2468         dprintk("NFS reply lookup: %d\n", status);
2469         return status;
2470 }
2471
2472 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2473 {
2474         memset(fh, 0, sizeof(struct nfs_fh));
2475         fattr->fsid.major = 1;
2476         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2477                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2478         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2479         fattr->nlink = 2;
2480 }
2481
2482 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2483                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2484 {
2485         struct nfs4_exception exception = { };
2486         int err;
2487         do {
2488                 int status;
2489
2490                 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2491                 switch (status) {
2492                 case -NFS4ERR_BADNAME:
2493                         return -ENOENT;
2494                 case -NFS4ERR_MOVED:
2495                         return nfs4_get_referral(dir, name, fattr, fhandle);
2496                 case -NFS4ERR_WRONGSEC:
2497                         nfs_fixup_secinfo_attributes(fattr, fhandle);
2498                 }
2499                 err = nfs4_handle_exception(NFS_SERVER(dir),
2500                                 status, &exception);
2501         } while (exception.retry);
2502         return err;
2503 }
2504
2505 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2506 {
2507         struct nfs_server *server = NFS_SERVER(inode);
2508         struct nfs4_accessargs args = {
2509                 .fh = NFS_FH(inode),
2510                 .bitmask = server->attr_bitmask,
2511         };
2512         struct nfs4_accessres res = {
2513                 .server = server,
2514         };
2515         struct rpc_message msg = {
2516                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2517                 .rpc_argp = &args,
2518                 .rpc_resp = &res,
2519                 .rpc_cred = entry->cred,
2520         };
2521         int mode = entry->mask;
2522         int status;
2523
2524         /*
2525          * Determine which access bits we want to ask for...
2526          */
2527         if (mode & MAY_READ)
2528                 args.access |= NFS4_ACCESS_READ;
2529         if (S_ISDIR(inode->i_mode)) {
2530                 if (mode & MAY_WRITE)
2531                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2532                 if (mode & MAY_EXEC)
2533                         args.access |= NFS4_ACCESS_LOOKUP;
2534         } else {
2535                 if (mode & MAY_WRITE)
2536                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2537                 if (mode & MAY_EXEC)
2538                         args.access |= NFS4_ACCESS_EXECUTE;
2539         }
2540
2541         res.fattr = nfs_alloc_fattr();
2542         if (res.fattr == NULL)
2543                 return -ENOMEM;
2544
2545         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2546         if (!status) {
2547                 entry->mask = 0;
2548                 if (res.access & NFS4_ACCESS_READ)
2549                         entry->mask |= MAY_READ;
2550                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2551                         entry->mask |= MAY_WRITE;
2552                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2553                         entry->mask |= MAY_EXEC;
2554                 nfs_refresh_inode(inode, res.fattr);
2555         }
2556         nfs_free_fattr(res.fattr);
2557         return status;
2558 }
2559
2560 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2561 {
2562         struct nfs4_exception exception = { };
2563         int err;
2564         do {
2565                 err = nfs4_handle_exception(NFS_SERVER(inode),
2566                                 _nfs4_proc_access(inode, entry),
2567                                 &exception);
2568         } while (exception.retry);
2569         return err;
2570 }
2571
2572 /*
2573  * TODO: For the time being, we don't try to get any attributes
2574  * along with any of the zero-copy operations READ, READDIR,
2575  * READLINK, WRITE.
2576  *
2577  * In the case of the first three, we want to put the GETATTR
2578  * after the read-type operation -- this is because it is hard
2579  * to predict the length of a GETATTR response in v4, and thus
2580  * align the READ data correctly.  This means that the GETATTR
2581  * may end up partially falling into the page cache, and we should
2582  * shift it into the 'tail' of the xdr_buf before processing.
2583  * To do this efficiently, we need to know the total length
2584  * of data received, which doesn't seem to be available outside
2585  * of the RPC layer.
2586  *
2587  * In the case of WRITE, we also want to put the GETATTR after
2588  * the operation -- in this case because we want to make sure
2589  * we get the post-operation mtime and size.  This means that
2590  * we can't use xdr_encode_pages() as written: we need a variant
2591  * of it which would leave room in the 'tail' iovec.
2592  *
2593  * Both of these changes to the XDR layer would in fact be quite
2594  * minor, but I decided to leave them for a subsequent patch.
2595  */
2596 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2597                 unsigned int pgbase, unsigned int pglen)
2598 {
2599         struct nfs4_readlink args = {
2600                 .fh       = NFS_FH(inode),
2601                 .pgbase   = pgbase,
2602                 .pglen    = pglen,
2603                 .pages    = &page,
2604         };
2605         struct nfs4_readlink_res res;
2606         struct rpc_message msg = {
2607                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2608                 .rpc_argp = &args,
2609                 .rpc_resp = &res,
2610         };
2611
2612         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2613 }
2614
2615 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2616                 unsigned int pgbase, unsigned int pglen)
2617 {
2618         struct nfs4_exception exception = { };
2619         int err;
2620         do {
2621                 err = nfs4_handle_exception(NFS_SERVER(inode),
2622                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2623                                 &exception);
2624         } while (exception.retry);
2625         return err;
2626 }
2627
2628 /*
2629  * Got race?
2630  * We will need to arrange for the VFS layer to provide an atomic open.
2631  * Until then, this create/open method is prone to inefficiency and race
2632  * conditions due to the lookup, create, and open VFS calls from sys_open()
2633  * placed on the wire.
2634  *
2635  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2636  * The file will be opened again in the subsequent VFS open call
2637  * (nfs4_proc_file_open).
2638  *
2639  * The open for read will just hang around to be used by any process that
2640  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2641  */
2642
2643 static int
2644 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2645                  int flags, struct nfs_open_context *ctx)
2646 {
2647         struct dentry *de = dentry;
2648         struct nfs4_state *state;
2649         struct rpc_cred *cred = NULL;
2650         fmode_t fmode = 0;
2651         int status = 0;
2652
2653         if (ctx != NULL) {
2654                 cred = ctx->cred;
2655                 de = ctx->dentry;
2656                 fmode = ctx->mode;
2657         }
2658         sattr->ia_mode &= ~current_umask();
2659         state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2660         d_drop(dentry);
2661         if (IS_ERR(state)) {
2662                 status = PTR_ERR(state);
2663                 goto out;
2664         }
2665         d_add(dentry, igrab(state->inode));
2666         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2667         if (ctx != NULL)
2668                 ctx->state = state;
2669         else
2670                 nfs4_close_sync(state, fmode);
2671 out:
2672         return status;
2673 }
2674
2675 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2676 {
2677         struct nfs_server *server = NFS_SERVER(dir);
2678         struct nfs_removeargs args = {
2679                 .fh = NFS_FH(dir),
2680                 .name.len = name->len,
2681                 .name.name = name->name,
2682                 .bitmask = server->attr_bitmask,
2683         };
2684         struct nfs_removeres res = {
2685                 .server = server,
2686         };
2687         struct rpc_message msg = {
2688                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2689                 .rpc_argp = &args,
2690                 .rpc_resp = &res,
2691         };
2692         int status = -ENOMEM;
2693
2694         res.dir_attr = nfs_alloc_fattr();
2695         if (res.dir_attr == NULL)
2696                 goto out;
2697
2698         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2699         if (status == 0) {
2700                 update_changeattr(dir, &res.cinfo);
2701                 nfs_post_op_update_inode(dir, res.dir_attr);
2702         }
2703         nfs_free_fattr(res.dir_attr);
2704 out:
2705         return status;
2706 }
2707
2708 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2709 {
2710         struct nfs4_exception exception = { };
2711         int err;
2712         do {
2713                 err = nfs4_handle_exception(NFS_SERVER(dir),
2714                                 _nfs4_proc_remove(dir, name),
2715                                 &exception);
2716         } while (exception.retry);
2717         return err;
2718 }
2719
2720 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2721 {
2722         struct nfs_server *server = NFS_SERVER(dir);
2723         struct nfs_removeargs *args = msg->rpc_argp;
2724         struct nfs_removeres *res = msg->rpc_resp;
2725
2726         args->bitmask = server->cache_consistency_bitmask;
2727         res->server = server;
2728         res->seq_res.sr_slot = NULL;
2729         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2730 }
2731
2732 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2733 {
2734         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2735
2736         if (!nfs4_sequence_done(task, &res->seq_res))
2737                 return 0;
2738         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2739                 return 0;
2740         update_changeattr(dir, &res->cinfo);
2741         nfs_post_op_update_inode(dir, res->dir_attr);
2742         return 1;
2743 }
2744
2745 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2746 {
2747         struct nfs_server *server = NFS_SERVER(dir);
2748         struct nfs_renameargs *arg = msg->rpc_argp;
2749         struct nfs_renameres *res = msg->rpc_resp;
2750
2751         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2752         arg->bitmask = server->attr_bitmask;
2753         res->server = server;
2754 }
2755
2756 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2757                                  struct inode *new_dir)
2758 {
2759         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2760
2761         if (!nfs4_sequence_done(task, &res->seq_res))
2762                 return 0;
2763         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2764                 return 0;
2765
2766         update_changeattr(old_dir, &res->old_cinfo);
2767         nfs_post_op_update_inode(old_dir, res->old_fattr);
2768         update_changeattr(new_dir, &res->new_cinfo);
2769         nfs_post_op_update_inode(new_dir, res->new_fattr);
2770         return 1;
2771 }
2772
2773 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2774                 struct inode *new_dir, struct qstr *new_name)
2775 {
2776         struct nfs_server *server = NFS_SERVER(old_dir);
2777         struct nfs_renameargs arg = {
2778                 .old_dir = NFS_FH(old_dir),
2779                 .new_dir = NFS_FH(new_dir),
2780                 .old_name = old_name,
2781                 .new_name = new_name,
2782                 .bitmask = server->attr_bitmask,
2783         };
2784         struct nfs_renameres res = {
2785                 .server = server,
2786         };
2787         struct rpc_message msg = {
2788                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2789                 .rpc_argp = &arg,
2790                 .rpc_resp = &res,
2791         };
2792         int status = -ENOMEM;
2793         
2794         res.old_fattr = nfs_alloc_fattr();
2795         res.new_fattr = nfs_alloc_fattr();
2796         if (res.old_fattr == NULL || res.new_fattr == NULL)
2797                 goto out;
2798
2799         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2800         if (!status) {
2801                 update_changeattr(old_dir, &res.old_cinfo);
2802                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2803                 update_changeattr(new_dir, &res.new_cinfo);
2804                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2805         }
2806 out:
2807         nfs_free_fattr(res.new_fattr);
2808         nfs_free_fattr(res.old_fattr);
2809         return status;
2810 }
2811
2812 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2813                 struct inode *new_dir, struct qstr *new_name)
2814 {
2815         struct nfs4_exception exception = { };
2816         int err;
2817         do {
2818                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2819                                 _nfs4_proc_rename(old_dir, old_name,
2820                                         new_dir, new_name),
2821                                 &exception);
2822         } while (exception.retry);
2823         return err;
2824 }
2825
2826 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2827 {
2828         struct nfs_server *server = NFS_SERVER(inode);
2829         struct nfs4_link_arg arg = {
2830                 .fh     = NFS_FH(inode),
2831                 .dir_fh = NFS_FH(dir),
2832                 .name   = name,
2833                 .bitmask = server->attr_bitmask,
2834         };
2835         struct nfs4_link_res res = {
2836                 .server = server,
2837         };
2838         struct rpc_message msg = {
2839                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2840                 .rpc_argp = &arg,
2841                 .rpc_resp = &res,
2842         };
2843         int status = -ENOMEM;
2844
2845         res.fattr = nfs_alloc_fattr();
2846         res.dir_attr = nfs_alloc_fattr();
2847         if (res.fattr == NULL || res.dir_attr == NULL)
2848                 goto out;
2849
2850         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2851         if (!status) {
2852                 update_changeattr(dir, &res.cinfo);
2853                 nfs_post_op_update_inode(dir, res.dir_attr);
2854                 nfs_post_op_update_inode(inode, res.fattr);
2855         }
2856 out:
2857         nfs_free_fattr(res.dir_attr);
2858         nfs_free_fattr(res.fattr);
2859         return status;
2860 }
2861
2862 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2863 {
2864         struct nfs4_exception exception = { };
2865         int err;
2866         do {
2867                 err = nfs4_handle_exception(NFS_SERVER(inode),
2868                                 _nfs4_proc_link(inode, dir, name),
2869                                 &exception);
2870         } while (exception.retry);
2871         return err;
2872 }
2873
2874 struct nfs4_createdata {
2875         struct rpc_message msg;
2876         struct nfs4_create_arg arg;
2877         struct nfs4_create_res res;
2878         struct nfs_fh fh;
2879         struct nfs_fattr fattr;
2880         struct nfs_fattr dir_fattr;
2881 };
2882
2883 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2884                 struct qstr *name, struct iattr *sattr, u32 ftype)
2885 {
2886         struct nfs4_createdata *data;
2887
2888         data = kzalloc(sizeof(*data), GFP_KERNEL);
2889         if (data != NULL) {
2890                 struct nfs_server *server = NFS_SERVER(dir);
2891
2892                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2893                 data->msg.rpc_argp = &data->arg;
2894                 data->msg.rpc_resp = &data->res;
2895                 data->arg.dir_fh = NFS_FH(dir);
2896                 data->arg.server = server;
2897                 data->arg.name = name;
2898                 data->arg.attrs = sattr;
2899                 data->arg.ftype = ftype;
2900                 data->arg.bitmask = server->attr_bitmask;
2901                 data->res.server = server;
2902                 data->res.fh = &data->fh;
2903                 data->res.fattr = &data->fattr;
2904                 data->res.dir_fattr = &data->dir_fattr;
2905                 nfs_fattr_init(data->res.fattr);
2906                 nfs_fattr_init(data->res.dir_fattr);
2907         }
2908         return data;
2909 }
2910
2911 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2912 {
2913         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2914                                     &data->arg.seq_args, &data->res.seq_res, 1);
2915         if (status == 0) {
2916                 update_changeattr(dir, &data->res.dir_cinfo);
2917                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2918                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2919         }
2920         return status;
2921 }
2922
2923 static void nfs4_free_createdata(struct nfs4_createdata *data)
2924 {
2925         kfree(data);
2926 }
2927
2928 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2929                 struct page *page, unsigned int len, struct iattr *sattr)
2930 {
2931         struct nfs4_createdata *data;
2932         int status = -ENAMETOOLONG;
2933
2934         if (len > NFS4_MAXPATHLEN)
2935                 goto out;
2936
2937         status = -ENOMEM;
2938         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2939         if (data == NULL)
2940                 goto out;
2941
2942         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2943         data->arg.u.symlink.pages = &page;
2944         data->arg.u.symlink.len = len;
2945         
2946         status = nfs4_do_create(dir, dentry, data);
2947
2948         nfs4_free_createdata(data);
2949 out:
2950         return status;
2951 }
2952
2953 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2954                 struct page *page, unsigned int len, struct iattr *sattr)
2955 {
2956         struct nfs4_exception exception = { };
2957         int err;
2958         do {
2959                 err = nfs4_handle_exception(NFS_SERVER(dir),
2960                                 _nfs4_proc_symlink(dir, dentry, page,
2961                                                         len, sattr),
2962                                 &exception);
2963         } while (exception.retry);
2964         return err;
2965 }
2966
2967 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2968                 struct iattr *sattr)
2969 {
2970         struct nfs4_createdata *data;
2971         int status = -ENOMEM;
2972
2973         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2974         if (data == NULL)
2975                 goto out;
2976
2977         status = nfs4_do_create(dir, dentry, data);
2978
2979         nfs4_free_createdata(data);
2980 out:
2981         return status;
2982 }
2983
2984 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2985                 struct iattr *sattr)
2986 {
2987         struct nfs4_exception exception = { };
2988         int err;
2989
2990         sattr->ia_mode &= ~current_umask();
2991         do {
2992                 err = nfs4_handle_exception(NFS_SERVER(dir),
2993                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2994                                 &exception);
2995         } while (exception.retry);
2996         return err;
2997 }
2998
2999 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3000                 u64 cookie, struct page **pages, unsigned int count, int plus)
3001 {
3002         struct inode            *dir = dentry->d_inode;
3003         struct nfs4_readdir_arg args = {
3004                 .fh = NFS_FH(dir),
3005                 .pages = pages,
3006                 .pgbase = 0,
3007                 .count = count,
3008                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3009                 .plus = plus,
3010         };
3011         struct nfs4_readdir_res res;
3012         struct rpc_message msg = {
3013                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3014                 .rpc_argp = &args,
3015                 .rpc_resp = &res,
3016                 .rpc_cred = cred,
3017         };
3018         int                     status;
3019
3020         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3021                         dentry->d_parent->d_name.name,
3022                         dentry->d_name.name,
3023                         (unsigned long long)cookie);
3024         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3025         res.pgbase = args.pgbase;
3026         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3027         if (status >= 0) {
3028                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3029                 status += args.pgbase;
3030         }
3031
3032         nfs_invalidate_atime(dir);
3033
3034         dprintk("%s: returns %d\n", __func__, status);
3035         return status;
3036 }
3037
3038 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3039                 u64 cookie, struct page **pages, unsigned int count, int plus)
3040 {
3041         struct nfs4_exception exception = { };
3042         int err;
3043         do {
3044                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3045                                 _nfs4_proc_readdir(dentry, cred, cookie,
3046                                         pages, count, plus),
3047                                 &exception);
3048         } while (exception.retry);
3049         return err;
3050 }
3051
3052 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3053                 struct iattr *sattr, dev_t rdev)
3054 {
3055         struct nfs4_createdata *data;
3056         int mode = sattr->ia_mode;
3057         int status = -ENOMEM;
3058
3059         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3060         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3061
3062         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3063         if (data == NULL)
3064                 goto out;
3065
3066         if (S_ISFIFO(mode))
3067                 data->arg.ftype = NF4FIFO;
3068         else if (S_ISBLK(mode)) {
3069                 data->arg.ftype = NF4BLK;
3070                 data->arg.u.device.specdata1 = MAJOR(rdev);
3071                 data->arg.u.device.specdata2 = MINOR(rdev);
3072         }
3073         else if (S_ISCHR(mode)) {
3074                 data->arg.ftype = NF4CHR;
3075                 data->arg.u.device.specdata1 = MAJOR(rdev);
3076                 data->arg.u.device.specdata2 = MINOR(rdev);
3077         }
3078         
3079         status = nfs4_do_create(dir, dentry, data);
3080
3081         nfs4_free_createdata(data);
3082 out:
3083         return status;
3084 }
3085
3086 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3087                 struct iattr *sattr, dev_t rdev)
3088 {
3089         struct nfs4_exception exception = { };
3090         int err;
3091
3092         sattr->ia_mode &= ~current_umask();
3093         do {
3094                 err = nfs4_handle_exception(NFS_SERVER(dir),
3095                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3096                                 &exception);
3097         } while (exception.retry);
3098         return err;
3099 }
3100
3101 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3102                  struct nfs_fsstat *fsstat)
3103 {
3104         struct nfs4_statfs_arg args = {
3105                 .fh = fhandle,
3106                 .bitmask = server->attr_bitmask,
3107         };
3108         struct nfs4_statfs_res res = {
3109                 .fsstat = fsstat,
3110         };
3111         struct rpc_message msg = {
3112                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3113                 .rpc_argp = &args,
3114                 .rpc_resp = &res,
3115         };
3116
3117         nfs_fattr_init(fsstat->fattr);
3118         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3119 }
3120
3121 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3122 {
3123         struct nfs4_exception exception = { };
3124         int err;
3125         do {
3126                 err = nfs4_handle_exception(server,
3127                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3128                                 &exception);
3129         } while (exception.retry);
3130         return err;
3131 }
3132
3133 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3134                 struct nfs_fsinfo *fsinfo)
3135 {
3136         struct nfs4_fsinfo_arg args = {
3137                 .fh = fhandle,
3138                 .bitmask = server->attr_bitmask,
3139         };
3140         struct nfs4_fsinfo_res res = {
3141                 .fsinfo = fsinfo,
3142         };
3143         struct rpc_message msg = {
3144                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3145                 .rpc_argp = &args,
3146                 .rpc_resp = &res,
3147         };
3148
3149         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3150 }
3151
3152 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3153 {
3154         struct nfs4_exception exception = { };
3155         int err;
3156
3157         do {
3158                 err = nfs4_handle_exception(server,
3159                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3160                                 &exception);
3161         } while (exception.retry);
3162         return err;
3163 }
3164
3165 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3166 {
3167         nfs_fattr_init(fsinfo->fattr);
3168         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3169 }
3170
3171 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3172                 struct nfs_pathconf *pathconf)
3173 {
3174         struct nfs4_pathconf_arg args = {
3175                 .fh = fhandle,
3176                 .bitmask = server->attr_bitmask,
3177         };
3178         struct nfs4_pathconf_res res = {
3179                 .pathconf = pathconf,
3180         };
3181         struct rpc_message msg = {
3182                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3183                 .rpc_argp = &args,
3184                 .rpc_resp = &res,
3185         };
3186
3187         /* None of the pathconf attributes are mandatory to implement */
3188         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3189                 memset(pathconf, 0, sizeof(*pathconf));
3190                 return 0;
3191         }
3192
3193         nfs_fattr_init(pathconf->fattr);
3194         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3195 }
3196
3197 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3198                 struct nfs_pathconf *pathconf)
3199 {
3200         struct nfs4_exception exception = { };
3201         int err;
3202
3203         do {
3204                 err = nfs4_handle_exception(server,
3205                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3206                                 &exception);
3207         } while (exception.retry);
3208         return err;
3209 }
3210
3211 void __nfs4_read_done_cb(struct nfs_read_data *data)
3212 {
3213         nfs_invalidate_atime(data->inode);
3214 }
3215
3216 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3217 {
3218         struct nfs_server *server = NFS_SERVER(data->inode);
3219
3220         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3221                 rpc_restart_call_prepare(task);