Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[linux-2.6.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         int ret = errorcode;
261
262         exception->retry = 0;
263         switch(errorcode) {
264                 case 0:
265                         return 0;
266                 case -NFS4ERR_ADMIN_REVOKED:
267                 case -NFS4ERR_BAD_STATEID:
268                 case -NFS4ERR_OPENMODE:
269                         if (state == NULL)
270                                 break;
271                         nfs4_schedule_stateid_recovery(server, state);
272                         goto wait_on_recovery;
273                 case -NFS4ERR_EXPIRED:
274                         if (state != NULL)
275                                 nfs4_schedule_stateid_recovery(server, state);
276                 case -NFS4ERR_STALE_STATEID:
277                 case -NFS4ERR_STALE_CLIENTID:
278                         nfs4_schedule_lease_recovery(clp);
279                         goto wait_on_recovery;
280 #if defined(CONFIG_NFS_V4_1)
281                 case -NFS4ERR_BADSESSION:
282                 case -NFS4ERR_BADSLOT:
283                 case -NFS4ERR_BAD_HIGH_SLOT:
284                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
285                 case -NFS4ERR_DEADSESSION:
286                 case -NFS4ERR_SEQ_FALSE_RETRY:
287                 case -NFS4ERR_SEQ_MISORDERED:
288                         dprintk("%s ERROR: %d Reset session\n", __func__,
289                                 errorcode);
290                         nfs4_schedule_session_recovery(clp->cl_session);
291                         exception->retry = 1;
292                         break;
293 #endif /* defined(CONFIG_NFS_V4_1) */
294                 case -NFS4ERR_FILE_OPEN:
295                         if (exception->timeout > HZ) {
296                                 /* We have retried a decent amount, time to
297                                  * fail
298                                  */
299                                 ret = -EBUSY;
300                                 break;
301                         }
302                 case -NFS4ERR_GRACE:
303                 case -NFS4ERR_DELAY:
304                 case -EKEYEXPIRED:
305                         ret = nfs4_delay(server->client, &exception->timeout);
306                         if (ret != 0)
307                                 break;
308                 case -NFS4ERR_RETRY_UNCACHED_REP:
309                 case -NFS4ERR_OLD_STATEID:
310                         exception->retry = 1;
311                         break;
312                 case -NFS4ERR_BADOWNER:
313                         /* The following works around a Linux server bug! */
314                 case -NFS4ERR_BADNAME:
315                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
316                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
317                                 exception->retry = 1;
318                                 printk(KERN_WARNING "NFS: v4 server %s "
319                                                 "does not accept raw "
320                                                 "uid/gids. "
321                                                 "Reenabling the idmapper.\n",
322                                                 server->nfs_client->cl_hostname);
323                         }
324         }
325         /* We failed to handle the error */
326         return nfs4_map_errors(ret);
327 wait_on_recovery:
328         ret = nfs4_wait_clnt_recover(clp);
329         if (ret == 0)
330                 exception->retry = 1;
331         return ret;
332 }
333
334
335 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
336 {
337         spin_lock(&clp->cl_lock);
338         if (time_before(clp->cl_last_renewal,timestamp))
339                 clp->cl_last_renewal = timestamp;
340         spin_unlock(&clp->cl_lock);
341 }
342
343 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
344 {
345         do_renew_lease(server->nfs_client, timestamp);
346 }
347
348 #if defined(CONFIG_NFS_V4_1)
349
350 /*
351  * nfs4_free_slot - free a slot and efficiently update slot table.
352  *
353  * freeing a slot is trivially done by clearing its respective bit
354  * in the bitmap.
355  * If the freed slotid equals highest_used_slotid we want to update it
356  * so that the server would be able to size down the slot table if needed,
357  * otherwise we know that the highest_used_slotid is still in use.
358  * When updating highest_used_slotid there may be "holes" in the bitmap
359  * so we need to scan down from highest_used_slotid to 0 looking for the now
360  * highest slotid in use.
361  * If none found, highest_used_slotid is set to -1.
362  *
363  * Must be called while holding tbl->slot_tbl_lock
364  */
365 static void
366 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
367 {
368         int free_slotid = free_slot - tbl->slots;
369         int slotid = free_slotid;
370
371         BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
372         /* clear used bit in bitmap */
373         __clear_bit(slotid, tbl->used_slots);
374
375         /* update highest_used_slotid when it is freed */
376         if (slotid == tbl->highest_used_slotid) {
377                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
378                 if (slotid < tbl->max_slots)
379                         tbl->highest_used_slotid = slotid;
380                 else
381                         tbl->highest_used_slotid = -1;
382         }
383         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384                 free_slotid, tbl->highest_used_slotid);
385 }
386
387 /*
388  * Signal state manager thread if session fore channel is drained
389  */
390 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
391 {
392         struct rpc_task *task;
393
394         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
395                 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396                 if (task)
397                         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
398                 return;
399         }
400
401         if (ses->fc_slot_table.highest_used_slotid != -1)
402                 return;
403
404         dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405         complete(&ses->fc_slot_table.complete);
406 }
407
408 /*
409  * Signal state manager thread if session back channel is drained
410  */
411 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
412 {
413         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414             ses->bc_slot_table.highest_used_slotid != -1)
415                 return;
416         dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417         complete(&ses->bc_slot_table.complete);
418 }
419
420 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
421 {
422         struct nfs4_slot_table *tbl;
423
424         tbl = &res->sr_session->fc_slot_table;
425         if (!res->sr_slot) {
426                 /* just wake up the next guy waiting since
427                  * we may have not consumed a slot after all */
428                 dprintk("%s: No slot\n", __func__);
429                 return;
430         }
431
432         spin_lock(&tbl->slot_tbl_lock);
433         nfs4_free_slot(tbl, res->sr_slot);
434         nfs4_check_drain_fc_complete(res->sr_session);
435         spin_unlock(&tbl->slot_tbl_lock);
436         res->sr_slot = NULL;
437 }
438
439 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
440 {
441         unsigned long timestamp;
442         struct nfs_client *clp;
443
444         /*
445          * sr_status remains 1 if an RPC level error occurred. The server
446          * may or may not have processed the sequence operation..
447          * Proceed as if the server received and processed the sequence
448          * operation.
449          */
450         if (res->sr_status == 1)
451                 res->sr_status = NFS_OK;
452
453         /* don't increment the sequence number if the task wasn't sent */
454         if (!RPC_WAS_SENT(task))
455                 goto out;
456
457         /* Check the SEQUENCE operation status */
458         switch (res->sr_status) {
459         case 0:
460                 /* Update the slot's sequence and clientid lease timer */
461                 ++res->sr_slot->seq_nr;
462                 timestamp = res->sr_renewal_time;
463                 clp = res->sr_session->clp;
464                 do_renew_lease(clp, timestamp);
465                 /* Check sequence flags */
466                 if (res->sr_status_flags != 0)
467                         nfs4_schedule_lease_recovery(clp);
468                 break;
469         case -NFS4ERR_DELAY:
470                 /* The server detected a resend of the RPC call and
471                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
472                  * of RFC5661.
473                  */
474                 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
475                         __func__,
476                         res->sr_slot - res->sr_session->fc_slot_table.slots,
477                         res->sr_slot->seq_nr);
478                 goto out_retry;
479         default:
480                 /* Just update the slot sequence no. */
481                 ++res->sr_slot->seq_nr;
482         }
483 out:
484         /* The session may be reset by one of the error handlers. */
485         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
486         nfs41_sequence_free_slot(res);
487         return 1;
488 out_retry:
489         if (!rpc_restart_call(task))
490                 goto out;
491         rpc_delay(task, NFS4_POLL_RETRY_MAX);
492         return 0;
493 }
494
495 static int nfs4_sequence_done(struct rpc_task *task,
496                                struct nfs4_sequence_res *res)
497 {
498         if (res->sr_session == NULL)
499                 return 1;
500         return nfs41_sequence_done(task, res);
501 }
502
503 /*
504  * nfs4_find_slot - efficiently look for a free slot
505  *
506  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507  * If found, we mark the slot as used, update the highest_used_slotid,
508  * and respectively set up the sequence operation args.
509  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
510  *
511  * Note: must be called with under the slot_tbl_lock.
512  */
513 static u8
514 nfs4_find_slot(struct nfs4_slot_table *tbl)
515 {
516         int slotid;
517         u8 ret_id = NFS4_MAX_SLOT_TABLE;
518         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
519
520         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522                 tbl->max_slots);
523         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524         if (slotid >= tbl->max_slots)
525                 goto out;
526         __set_bit(slotid, tbl->used_slots);
527         if (slotid > tbl->highest_used_slotid)
528                 tbl->highest_used_slotid = slotid;
529         ret_id = slotid;
530 out:
531         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
533         return ret_id;
534 }
535
536 int nfs41_setup_sequence(struct nfs4_session *session,
537                                 struct nfs4_sequence_args *args,
538                                 struct nfs4_sequence_res *res,
539                                 int cache_reply,
540                                 struct rpc_task *task)
541 {
542         struct nfs4_slot *slot;
543         struct nfs4_slot_table *tbl;
544         u8 slotid;
545
546         dprintk("--> %s\n", __func__);
547         /* slot already allocated? */
548         if (res->sr_slot != NULL)
549                 return 0;
550
551         tbl = &session->fc_slot_table;
552
553         spin_lock(&tbl->slot_tbl_lock);
554         if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
555             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
556                 /*
557                  * The state manager will wait until the slot table is empty.
558                  * Schedule the reset thread
559                  */
560                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561                 spin_unlock(&tbl->slot_tbl_lock);
562                 dprintk("%s Schedule Session Reset\n", __func__);
563                 return -EAGAIN;
564         }
565
566         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
567             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
568                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569                 spin_unlock(&tbl->slot_tbl_lock);
570                 dprintk("%s enforce FIFO order\n", __func__);
571                 return -EAGAIN;
572         }
573
574         slotid = nfs4_find_slot(tbl);
575         if (slotid == NFS4_MAX_SLOT_TABLE) {
576                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577                 spin_unlock(&tbl->slot_tbl_lock);
578                 dprintk("<-- %s: no free slots\n", __func__);
579                 return -EAGAIN;
580         }
581         spin_unlock(&tbl->slot_tbl_lock);
582
583         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
584         slot = tbl->slots + slotid;
585         args->sa_session = session;
586         args->sa_slotid = slotid;
587         args->sa_cache_this = cache_reply;
588
589         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
590
591         res->sr_session = session;
592         res->sr_slot = slot;
593         res->sr_renewal_time = jiffies;
594         res->sr_status_flags = 0;
595         /*
596          * sr_status is only set in decode_sequence, and so will remain
597          * set to 1 if an rpc level failure occurs.
598          */
599         res->sr_status = 1;
600         return 0;
601 }
602 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
603
604 int nfs4_setup_sequence(const struct nfs_server *server,
605                         struct nfs4_sequence_args *args,
606                         struct nfs4_sequence_res *res,
607                         int cache_reply,
608                         struct rpc_task *task)
609 {
610         struct nfs4_session *session = nfs4_get_session(server);
611         int ret = 0;
612
613         if (session == NULL) {
614                 args->sa_session = NULL;
615                 res->sr_session = NULL;
616                 goto out;
617         }
618
619         dprintk("--> %s clp %p session %p sr_slot %td\n",
620                 __func__, session->clp, session, res->sr_slot ?
621                         res->sr_slot - session->fc_slot_table.slots : -1);
622
623         ret = nfs41_setup_sequence(session, args, res, cache_reply,
624                                    task);
625 out:
626         dprintk("<-- %s status=%d\n", __func__, ret);
627         return ret;
628 }
629
630 struct nfs41_call_sync_data {
631         const struct nfs_server *seq_server;
632         struct nfs4_sequence_args *seq_args;
633         struct nfs4_sequence_res *seq_res;
634         int cache_reply;
635 };
636
637 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
638 {
639         struct nfs41_call_sync_data *data = calldata;
640
641         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
642
643         if (nfs4_setup_sequence(data->seq_server, data->seq_args,
644                                 data->seq_res, data->cache_reply, task))
645                 return;
646         rpc_call_start(task);
647 }
648
649 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
650 {
651         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
652         nfs41_call_sync_prepare(task, calldata);
653 }
654
655 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656 {
657         struct nfs41_call_sync_data *data = calldata;
658
659         nfs41_sequence_done(task, data->seq_res);
660 }
661
662 struct rpc_call_ops nfs41_call_sync_ops = {
663         .rpc_call_prepare = nfs41_call_sync_prepare,
664         .rpc_call_done = nfs41_call_sync_done,
665 };
666
667 struct rpc_call_ops nfs41_call_priv_sync_ops = {
668         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
669         .rpc_call_done = nfs41_call_sync_done,
670 };
671
672 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673                                    struct nfs_server *server,
674                                    struct rpc_message *msg,
675                                    struct nfs4_sequence_args *args,
676                                    struct nfs4_sequence_res *res,
677                                    int cache_reply,
678                                    int privileged)
679 {
680         int ret;
681         struct rpc_task *task;
682         struct nfs41_call_sync_data data = {
683                 .seq_server = server,
684                 .seq_args = args,
685                 .seq_res = res,
686                 .cache_reply = cache_reply,
687         };
688         struct rpc_task_setup task_setup = {
689                 .rpc_client = clnt,
690                 .rpc_message = msg,
691                 .callback_ops = &nfs41_call_sync_ops,
692                 .callback_data = &data
693         };
694
695         res->sr_slot = NULL;
696         if (privileged)
697                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
698         task = rpc_run_task(&task_setup);
699         if (IS_ERR(task))
700                 ret = PTR_ERR(task);
701         else {
702                 ret = task->tk_status;
703                 rpc_put_task(task);
704         }
705         return ret;
706 }
707
708 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
709                             struct nfs_server *server,
710                             struct rpc_message *msg,
711                             struct nfs4_sequence_args *args,
712                             struct nfs4_sequence_res *res,
713                             int cache_reply)
714 {
715         return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
716 }
717
718 #else
719 static int nfs4_sequence_done(struct rpc_task *task,
720                                struct nfs4_sequence_res *res)
721 {
722         return 1;
723 }
724 #endif /* CONFIG_NFS_V4_1 */
725
726 int _nfs4_call_sync(struct rpc_clnt *clnt,
727                     struct nfs_server *server,
728                     struct rpc_message *msg,
729                     struct nfs4_sequence_args *args,
730                     struct nfs4_sequence_res *res,
731                     int cache_reply)
732 {
733         args->sa_session = res->sr_session = NULL;
734         return rpc_call_sync(clnt, msg, 0);
735 }
736
737 static inline
738 int nfs4_call_sync(struct rpc_clnt *clnt,
739                    struct nfs_server *server,
740                    struct rpc_message *msg,
741                    struct nfs4_sequence_args *args,
742                    struct nfs4_sequence_res *res,
743                    int cache_reply)
744 {
745         return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
746                                                 args, res, cache_reply);
747 }
748
749 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
750 {
751         struct nfs_inode *nfsi = NFS_I(dir);
752
753         spin_lock(&dir->i_lock);
754         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
755         if (!cinfo->atomic || cinfo->before != dir->i_version)
756                 nfs_force_lookup_revalidate(dir);
757         dir->i_version = cinfo->after;
758         spin_unlock(&dir->i_lock);
759 }
760
761 struct nfs4_opendata {
762         struct kref kref;
763         struct nfs_openargs o_arg;
764         struct nfs_openres o_res;
765         struct nfs_open_confirmargs c_arg;
766         struct nfs_open_confirmres c_res;
767         struct nfs_fattr f_attr;
768         struct nfs_fattr dir_attr;
769         struct dentry *dir;
770         struct dentry *dentry;
771         struct nfs4_state_owner *owner;
772         struct nfs4_state *state;
773         struct iattr attrs;
774         unsigned long timestamp;
775         unsigned int rpc_done : 1;
776         int rpc_status;
777         int cancelled;
778 };
779
780
781 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
782 {
783         p->o_res.f_attr = &p->f_attr;
784         p->o_res.dir_attr = &p->dir_attr;
785         p->o_res.seqid = p->o_arg.seqid;
786         p->c_res.seqid = p->c_arg.seqid;
787         p->o_res.server = p->o_arg.server;
788         nfs_fattr_init(&p->f_attr);
789         nfs_fattr_init(&p->dir_attr);
790 }
791
792 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
793                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
794                 const struct iattr *attrs,
795                 gfp_t gfp_mask)
796 {
797         struct dentry *parent = dget_parent(dentry);
798         struct inode *dir = parent->d_inode;
799         struct nfs_server *server = NFS_SERVER(dir);
800         struct nfs4_opendata *p;
801
802         p = kzalloc(sizeof(*p), gfp_mask);
803         if (p == NULL)
804                 goto err;
805         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
806         if (p->o_arg.seqid == NULL)
807                 goto err_free;
808         nfs_sb_active(dentry->d_sb);
809         p->dentry = dget(dentry);
810         p->dir = parent;
811         p->owner = sp;
812         atomic_inc(&sp->so_count);
813         p->o_arg.fh = NFS_FH(dir);
814         p->o_arg.open_flags = flags;
815         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
816         p->o_arg.clientid = server->nfs_client->cl_clientid;
817         p->o_arg.id = sp->so_owner_id.id;
818         p->o_arg.name = &dentry->d_name;
819         p->o_arg.server = server;
820         p->o_arg.bitmask = server->attr_bitmask;
821         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
822         if (flags & O_CREAT) {
823                 u32 *s;
824
825                 p->o_arg.u.attrs = &p->attrs;
826                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
827                 s = (u32 *) p->o_arg.u.verifier.data;
828                 s[0] = jiffies;
829                 s[1] = current->pid;
830         }
831         p->c_arg.fh = &p->o_res.fh;
832         p->c_arg.stateid = &p->o_res.stateid;
833         p->c_arg.seqid = p->o_arg.seqid;
834         nfs4_init_opendata_res(p);
835         kref_init(&p->kref);
836         return p;
837 err_free:
838         kfree(p);
839 err:
840         dput(parent);
841         return NULL;
842 }
843
844 static void nfs4_opendata_free(struct kref *kref)
845 {
846         struct nfs4_opendata *p = container_of(kref,
847                         struct nfs4_opendata, kref);
848         struct super_block *sb = p->dentry->d_sb;
849
850         nfs_free_seqid(p->o_arg.seqid);
851         if (p->state != NULL)
852                 nfs4_put_open_state(p->state);
853         nfs4_put_state_owner(p->owner);
854         dput(p->dir);
855         dput(p->dentry);
856         nfs_sb_deactive(sb);
857         kfree(p);
858 }
859
860 static void nfs4_opendata_put(struct nfs4_opendata *p)
861 {
862         if (p != NULL)
863                 kref_put(&p->kref, nfs4_opendata_free);
864 }
865
866 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
867 {
868         int ret;
869
870         ret = rpc_wait_for_completion_task(task);
871         return ret;
872 }
873
874 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
875 {
876         int ret = 0;
877
878         if (open_mode & O_EXCL)
879                 goto out;
880         switch (mode & (FMODE_READ|FMODE_WRITE)) {
881                 case FMODE_READ:
882                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
883                                 && state->n_rdonly != 0;
884                         break;
885                 case FMODE_WRITE:
886                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
887                                 && state->n_wronly != 0;
888                         break;
889                 case FMODE_READ|FMODE_WRITE:
890                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
891                                 && state->n_rdwr != 0;
892         }
893 out:
894         return ret;
895 }
896
897 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
898 {
899         if (delegation == NULL)
900                 return 0;
901         if ((delegation->type & fmode) != fmode)
902                 return 0;
903         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
904                 return 0;
905         nfs_mark_delegation_referenced(delegation);
906         return 1;
907 }
908
909 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
910 {
911         switch (fmode) {
912                 case FMODE_WRITE:
913                         state->n_wronly++;
914                         break;
915                 case FMODE_READ:
916                         state->n_rdonly++;
917                         break;
918                 case FMODE_READ|FMODE_WRITE:
919                         state->n_rdwr++;
920         }
921         nfs4_state_set_mode_locked(state, state->state | fmode);
922 }
923
924 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
925 {
926         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
927                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
928         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
929         switch (fmode) {
930                 case FMODE_READ:
931                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
932                         break;
933                 case FMODE_WRITE:
934                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
935                         break;
936                 case FMODE_READ|FMODE_WRITE:
937                         set_bit(NFS_O_RDWR_STATE, &state->flags);
938         }
939 }
940
941 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
942 {
943         write_seqlock(&state->seqlock);
944         nfs_set_open_stateid_locked(state, stateid, fmode);
945         write_sequnlock(&state->seqlock);
946 }
947
948 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
949 {
950         /*
951          * Protect the call to nfs4_state_set_mode_locked and
952          * serialise the stateid update
953          */
954         write_seqlock(&state->seqlock);
955         if (deleg_stateid != NULL) {
956                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
957                 set_bit(NFS_DELEGATED_STATE, &state->flags);
958         }
959         if (open_stateid != NULL)
960                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
961         write_sequnlock(&state->seqlock);
962         spin_lock(&state->owner->so_lock);
963         update_open_stateflags(state, fmode);
964         spin_unlock(&state->owner->so_lock);
965 }
966
967 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
968 {
969         struct nfs_inode *nfsi = NFS_I(state->inode);
970         struct nfs_delegation *deleg_cur;
971         int ret = 0;
972
973         fmode &= (FMODE_READ|FMODE_WRITE);
974
975         rcu_read_lock();
976         deleg_cur = rcu_dereference(nfsi->delegation);
977         if (deleg_cur == NULL)
978                 goto no_delegation;
979
980         spin_lock(&deleg_cur->lock);
981         if (nfsi->delegation != deleg_cur ||
982             (deleg_cur->type & fmode) != fmode)
983                 goto no_delegation_unlock;
984
985         if (delegation == NULL)
986                 delegation = &deleg_cur->stateid;
987         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
988                 goto no_delegation_unlock;
989
990         nfs_mark_delegation_referenced(deleg_cur);
991         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
992         ret = 1;
993 no_delegation_unlock:
994         spin_unlock(&deleg_cur->lock);
995 no_delegation:
996         rcu_read_unlock();
997
998         if (!ret && open_stateid != NULL) {
999                 __update_open_stateid(state, open_stateid, NULL, fmode);
1000                 ret = 1;
1001         }
1002
1003         return ret;
1004 }
1005
1006
1007 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1008 {
1009         struct nfs_delegation *delegation;
1010
1011         rcu_read_lock();
1012         delegation = rcu_dereference(NFS_I(inode)->delegation);
1013         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1014                 rcu_read_unlock();
1015                 return;
1016         }
1017         rcu_read_unlock();
1018         nfs_inode_return_delegation(inode);
1019 }
1020
1021 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1022 {
1023         struct nfs4_state *state = opendata->state;
1024         struct nfs_inode *nfsi = NFS_I(state->inode);
1025         struct nfs_delegation *delegation;
1026         int open_mode = opendata->o_arg.open_flags & O_EXCL;
1027         fmode_t fmode = opendata->o_arg.fmode;
1028         nfs4_stateid stateid;
1029         int ret = -EAGAIN;
1030
1031         for (;;) {
1032                 if (can_open_cached(state, fmode, open_mode)) {
1033                         spin_lock(&state->owner->so_lock);
1034                         if (can_open_cached(state, fmode, open_mode)) {
1035                                 update_open_stateflags(state, fmode);
1036                                 spin_unlock(&state->owner->so_lock);
1037                                 goto out_return_state;
1038                         }
1039                         spin_unlock(&state->owner->so_lock);
1040                 }
1041                 rcu_read_lock();
1042                 delegation = rcu_dereference(nfsi->delegation);
1043                 if (!can_open_delegated(delegation, fmode)) {
1044                         rcu_read_unlock();
1045                         break;
1046                 }
1047                 /* Save the delegation */
1048                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1049                 rcu_read_unlock();
1050                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1051                 if (ret != 0)
1052                         goto out;
1053                 ret = -EAGAIN;
1054
1055                 /* Try to update the stateid using the delegation */
1056                 if (update_open_stateid(state, NULL, &stateid, fmode))
1057                         goto out_return_state;
1058         }
1059 out:
1060         return ERR_PTR(ret);
1061 out_return_state:
1062         atomic_inc(&state->count);
1063         return state;
1064 }
1065
1066 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1067 {
1068         struct inode *inode;
1069         struct nfs4_state *state = NULL;
1070         struct nfs_delegation *delegation;
1071         int ret;
1072
1073         if (!data->rpc_done) {
1074                 state = nfs4_try_open_cached(data);
1075                 goto out;
1076         }
1077
1078         ret = -EAGAIN;
1079         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1080                 goto err;
1081         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1082         ret = PTR_ERR(inode);
1083         if (IS_ERR(inode))
1084                 goto err;
1085         ret = -ENOMEM;
1086         state = nfs4_get_open_state(inode, data->owner);
1087         if (state == NULL)
1088                 goto err_put_inode;
1089         if (data->o_res.delegation_type != 0) {
1090                 int delegation_flags = 0;
1091
1092                 rcu_read_lock();
1093                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1094                 if (delegation)
1095                         delegation_flags = delegation->flags;
1096                 rcu_read_unlock();
1097                 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1098                         pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1099                                         "returning a delegation for "
1100                                         "OPEN(CLAIM_DELEGATE_CUR)\n",
1101                                         NFS_CLIENT(inode)->cl_server);
1102                 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1103                         nfs_inode_set_delegation(state->inode,
1104                                         data->owner->so_cred,
1105                                         &data->o_res);
1106                 else
1107                         nfs_inode_reclaim_delegation(state->inode,
1108                                         data->owner->so_cred,
1109                                         &data->o_res);
1110         }
1111
1112         update_open_stateid(state, &data->o_res.stateid, NULL,
1113                         data->o_arg.fmode);
1114         iput(inode);
1115 out:
1116         return state;
1117 err_put_inode:
1118         iput(inode);
1119 err:
1120         return ERR_PTR(ret);
1121 }
1122
1123 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1124 {
1125         struct nfs_inode *nfsi = NFS_I(state->inode);
1126         struct nfs_open_context *ctx;
1127
1128         spin_lock(&state->inode->i_lock);
1129         list_for_each_entry(ctx, &nfsi->open_files, list) {
1130                 if (ctx->state != state)
1131                         continue;
1132                 get_nfs_open_context(ctx);
1133                 spin_unlock(&state->inode->i_lock);
1134                 return ctx;
1135         }
1136         spin_unlock(&state->inode->i_lock);
1137         return ERR_PTR(-ENOENT);
1138 }
1139
1140 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1141 {
1142         struct nfs4_opendata *opendata;
1143
1144         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1145         if (opendata == NULL)
1146                 return ERR_PTR(-ENOMEM);
1147         opendata->state = state;
1148         atomic_inc(&state->count);
1149         return opendata;
1150 }
1151
1152 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1153 {
1154         struct nfs4_state *newstate;
1155         int ret;
1156
1157         opendata->o_arg.open_flags = 0;
1158         opendata->o_arg.fmode = fmode;
1159         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1160         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1161         nfs4_init_opendata_res(opendata);
1162         ret = _nfs4_recover_proc_open(opendata);
1163         if (ret != 0)
1164                 return ret; 
1165         newstate = nfs4_opendata_to_nfs4_state(opendata);
1166         if (IS_ERR(newstate))
1167                 return PTR_ERR(newstate);
1168         nfs4_close_state(newstate, fmode);
1169         *res = newstate;
1170         return 0;
1171 }
1172
1173 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1174 {
1175         struct nfs4_state *newstate;
1176         int ret;
1177
1178         /* memory barrier prior to reading state->n_* */
1179         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1180         smp_rmb();
1181         if (state->n_rdwr != 0) {
1182                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1183                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1184                 if (ret != 0)
1185                         return ret;
1186                 if (newstate != state)
1187                         return -ESTALE;
1188         }
1189         if (state->n_wronly != 0) {
1190                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1191                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1192                 if (ret != 0)
1193                         return ret;
1194                 if (newstate != state)
1195                         return -ESTALE;
1196         }
1197         if (state->n_rdonly != 0) {
1198                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1199                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1200                 if (ret != 0)
1201                         return ret;
1202                 if (newstate != state)
1203                         return -ESTALE;
1204         }
1205         /*
1206          * We may have performed cached opens for all three recoveries.
1207          * Check if we need to update the current stateid.
1208          */
1209         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1210             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1211                 write_seqlock(&state->seqlock);
1212                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1213                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1214                 write_sequnlock(&state->seqlock);
1215         }
1216         return 0;
1217 }
1218
1219 /*
1220  * OPEN_RECLAIM:
1221  *      reclaim state on the server after a reboot.
1222  */
1223 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1224 {
1225         struct nfs_delegation *delegation;
1226         struct nfs4_opendata *opendata;
1227         fmode_t delegation_type = 0;
1228         int status;
1229
1230         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1231         if (IS_ERR(opendata))
1232                 return PTR_ERR(opendata);
1233         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1234         opendata->o_arg.fh = NFS_FH(state->inode);
1235         rcu_read_lock();
1236         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1237         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1238                 delegation_type = delegation->type;
1239         rcu_read_unlock();
1240         opendata->o_arg.u.delegation_type = delegation_type;
1241         status = nfs4_open_recover(opendata, state);
1242         nfs4_opendata_put(opendata);
1243         return status;
1244 }
1245
1246 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1247 {
1248         struct nfs_server *server = NFS_SERVER(state->inode);
1249         struct nfs4_exception exception = { };
1250         int err;
1251         do {
1252                 err = _nfs4_do_open_reclaim(ctx, state);
1253                 if (err != -NFS4ERR_DELAY)
1254                         break;
1255                 nfs4_handle_exception(server, err, &exception);
1256         } while (exception.retry);
1257         return err;
1258 }
1259
1260 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1261 {
1262         struct nfs_open_context *ctx;
1263         int ret;
1264
1265         ctx = nfs4_state_find_open_context(state);
1266         if (IS_ERR(ctx))
1267                 return PTR_ERR(ctx);
1268         ret = nfs4_do_open_reclaim(ctx, state);
1269         put_nfs_open_context(ctx);
1270         return ret;
1271 }
1272
1273 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1274 {
1275         struct nfs4_opendata *opendata;
1276         int ret;
1277
1278         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1279         if (IS_ERR(opendata))
1280                 return PTR_ERR(opendata);
1281         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1282         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1283                         sizeof(opendata->o_arg.u.delegation.data));
1284         ret = nfs4_open_recover(opendata, state);
1285         nfs4_opendata_put(opendata);
1286         return ret;
1287 }
1288
1289 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1290 {
1291         struct nfs4_exception exception = { };
1292         struct nfs_server *server = NFS_SERVER(state->inode);
1293         int err;
1294         do {
1295                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1296                 switch (err) {
1297                         case 0:
1298                         case -ENOENT:
1299                         case -ESTALE:
1300                                 goto out;
1301                         case -NFS4ERR_BADSESSION:
1302                         case -NFS4ERR_BADSLOT:
1303                         case -NFS4ERR_BAD_HIGH_SLOT:
1304                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1305                         case -NFS4ERR_DEADSESSION:
1306                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1307                                 goto out;
1308                         case -NFS4ERR_STALE_CLIENTID:
1309                         case -NFS4ERR_STALE_STATEID:
1310                         case -NFS4ERR_EXPIRED:
1311                                 /* Don't recall a delegation if it was lost */
1312                                 nfs4_schedule_lease_recovery(server->nfs_client);
1313                                 goto out;
1314                         case -ERESTARTSYS:
1315                                 /*
1316                                  * The show must go on: exit, but mark the
1317                                  * stateid as needing recovery.
1318                                  */
1319                         case -NFS4ERR_ADMIN_REVOKED:
1320                         case -NFS4ERR_BAD_STATEID:
1321                                 nfs4_schedule_stateid_recovery(server, state);
1322                         case -EKEYEXPIRED:
1323                                 /*
1324                                  * User RPCSEC_GSS context has expired.
1325                                  * We cannot recover this stateid now, so
1326                                  * skip it and allow recovery thread to
1327                                  * proceed.
1328                                  */
1329                         case -ENOMEM:
1330                                 err = 0;
1331                                 goto out;
1332                 }
1333                 err = nfs4_handle_exception(server, err, &exception);
1334         } while (exception.retry);
1335 out:
1336         return err;
1337 }
1338
1339 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1340 {
1341         struct nfs4_opendata *data = calldata;
1342
1343         data->rpc_status = task->tk_status;
1344         if (data->rpc_status == 0) {
1345                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1346                                 sizeof(data->o_res.stateid.data));
1347                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1348                 renew_lease(data->o_res.server, data->timestamp);
1349                 data->rpc_done = 1;
1350         }
1351 }
1352
1353 static void nfs4_open_confirm_release(void *calldata)
1354 {
1355         struct nfs4_opendata *data = calldata;
1356         struct nfs4_state *state = NULL;
1357
1358         /* If this request hasn't been cancelled, do nothing */
1359         if (data->cancelled == 0)
1360                 goto out_free;
1361         /* In case of error, no cleanup! */
1362         if (!data->rpc_done)
1363                 goto out_free;
1364         state = nfs4_opendata_to_nfs4_state(data);
1365         if (!IS_ERR(state))
1366                 nfs4_close_state(state, data->o_arg.fmode);
1367 out_free:
1368         nfs4_opendata_put(data);
1369 }
1370
1371 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1372         .rpc_call_done = nfs4_open_confirm_done,
1373         .rpc_release = nfs4_open_confirm_release,
1374 };
1375
1376 /*
1377  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1378  */
1379 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1380 {
1381         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1382         struct rpc_task *task;
1383         struct  rpc_message msg = {
1384                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1385                 .rpc_argp = &data->c_arg,
1386                 .rpc_resp = &data->c_res,
1387                 .rpc_cred = data->owner->so_cred,
1388         };
1389         struct rpc_task_setup task_setup_data = {
1390                 .rpc_client = server->client,
1391                 .rpc_message = &msg,
1392                 .callback_ops = &nfs4_open_confirm_ops,
1393                 .callback_data = data,
1394                 .workqueue = nfsiod_workqueue,
1395                 .flags = RPC_TASK_ASYNC,
1396         };
1397         int status;
1398
1399         kref_get(&data->kref);
1400         data->rpc_done = 0;
1401         data->rpc_status = 0;
1402         data->timestamp = jiffies;
1403         task = rpc_run_task(&task_setup_data);
1404         if (IS_ERR(task))
1405                 return PTR_ERR(task);
1406         status = nfs4_wait_for_completion_rpc_task(task);
1407         if (status != 0) {
1408                 data->cancelled = 1;
1409                 smp_wmb();
1410         } else
1411                 status = data->rpc_status;
1412         rpc_put_task(task);
1413         return status;
1414 }
1415
1416 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1417 {
1418         struct nfs4_opendata *data = calldata;
1419         struct nfs4_state_owner *sp = data->owner;
1420
1421         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1422                 return;
1423         /*
1424          * Check if we still need to send an OPEN call, or if we can use
1425          * a delegation instead.
1426          */
1427         if (data->state != NULL) {
1428                 struct nfs_delegation *delegation;
1429
1430                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1431                         goto out_no_action;
1432                 rcu_read_lock();
1433                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1434                 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1435                     can_open_delegated(delegation, data->o_arg.fmode))
1436                         goto unlock_no_action;
1437                 rcu_read_unlock();
1438         }
1439         /* Update sequence id. */
1440         data->o_arg.id = sp->so_owner_id.id;
1441         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1442         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1443                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1444                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1445         }
1446         data->timestamp = jiffies;
1447         if (nfs4_setup_sequence(data->o_arg.server,
1448                                 &data->o_arg.seq_args,
1449                                 &data->o_res.seq_res, 1, task))
1450                 return;
1451         rpc_call_start(task);
1452         return;
1453 unlock_no_action:
1454         rcu_read_unlock();
1455 out_no_action:
1456         task->tk_action = NULL;
1457
1458 }
1459
1460 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1461 {
1462         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1463         nfs4_open_prepare(task, calldata);
1464 }
1465
1466 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1467 {
1468         struct nfs4_opendata *data = calldata;
1469
1470         data->rpc_status = task->tk_status;
1471
1472         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1473                 return;
1474
1475         if (task->tk_status == 0) {
1476                 switch (data->o_res.f_attr->mode & S_IFMT) {
1477                         case S_IFREG:
1478                                 break;
1479                         case S_IFLNK:
1480                                 data->rpc_status = -ELOOP;
1481                                 break;
1482                         case S_IFDIR:
1483                                 data->rpc_status = -EISDIR;
1484                                 break;
1485                         default:
1486                                 data->rpc_status = -ENOTDIR;
1487                 }
1488                 renew_lease(data->o_res.server, data->timestamp);
1489                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1490                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1491         }
1492         data->rpc_done = 1;
1493 }
1494
1495 static void nfs4_open_release(void *calldata)
1496 {
1497         struct nfs4_opendata *data = calldata;
1498         struct nfs4_state *state = NULL;
1499
1500         /* If this request hasn't been cancelled, do nothing */
1501         if (data->cancelled == 0)
1502                 goto out_free;
1503         /* In case of error, no cleanup! */
1504         if (data->rpc_status != 0 || !data->rpc_done)
1505                 goto out_free;
1506         /* In case we need an open_confirm, no cleanup! */
1507         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1508                 goto out_free;
1509         state = nfs4_opendata_to_nfs4_state(data);
1510         if (!IS_ERR(state))
1511                 nfs4_close_state(state, data->o_arg.fmode);
1512 out_free:
1513         nfs4_opendata_put(data);
1514 }
1515
1516 static const struct rpc_call_ops nfs4_open_ops = {
1517         .rpc_call_prepare = nfs4_open_prepare,
1518         .rpc_call_done = nfs4_open_done,
1519         .rpc_release = nfs4_open_release,
1520 };
1521
1522 static const struct rpc_call_ops nfs4_recover_open_ops = {
1523         .rpc_call_prepare = nfs4_recover_open_prepare,
1524         .rpc_call_done = nfs4_open_done,
1525         .rpc_release = nfs4_open_release,
1526 };
1527
1528 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1529 {
1530         struct inode *dir = data->dir->d_inode;
1531         struct nfs_server *server = NFS_SERVER(dir);
1532         struct nfs_openargs *o_arg = &data->o_arg;
1533         struct nfs_openres *o_res = &data->o_res;
1534         struct rpc_task *task;
1535         struct rpc_message msg = {
1536                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1537                 .rpc_argp = o_arg,
1538                 .rpc_resp = o_res,
1539                 .rpc_cred = data->owner->so_cred,
1540         };
1541         struct rpc_task_setup task_setup_data = {
1542                 .rpc_client = server->client,
1543                 .rpc_message = &msg,
1544                 .callback_ops = &nfs4_open_ops,
1545                 .callback_data = data,
1546                 .workqueue = nfsiod_workqueue,
1547                 .flags = RPC_TASK_ASYNC,
1548         };
1549         int status;
1550
1551         kref_get(&data->kref);
1552         data->rpc_done = 0;
1553         data->rpc_status = 0;
1554         data->cancelled = 0;
1555         if (isrecover)
1556                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1557         task = rpc_run_task(&task_setup_data);
1558         if (IS_ERR(task))
1559                 return PTR_ERR(task);
1560         status = nfs4_wait_for_completion_rpc_task(task);
1561         if (status != 0) {
1562                 data->cancelled = 1;
1563                 smp_wmb();
1564         } else
1565                 status = data->rpc_status;
1566         rpc_put_task(task);
1567
1568         return status;
1569 }
1570
1571 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1572 {
1573         struct inode *dir = data->dir->d_inode;
1574         struct nfs_openres *o_res = &data->o_res;
1575         int status;
1576
1577         status = nfs4_run_open_task(data, 1);
1578         if (status != 0 || !data->rpc_done)
1579                 return status;
1580
1581         nfs_refresh_inode(dir, o_res->dir_attr);
1582
1583         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1584                 status = _nfs4_proc_open_confirm(data);
1585                 if (status != 0)
1586                         return status;
1587         }
1588
1589         return status;
1590 }
1591
1592 /*
1593  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1594  */
1595 static int _nfs4_proc_open(struct nfs4_opendata *data)
1596 {
1597         struct inode *dir = data->dir->d_inode;
1598         struct nfs_server *server = NFS_SERVER(dir);
1599         struct nfs_openargs *o_arg = &data->o_arg;
1600         struct nfs_openres *o_res = &data->o_res;
1601         int status;
1602
1603         status = nfs4_run_open_task(data, 0);
1604         if (!data->rpc_done)
1605                 return status;
1606         if (status != 0) {
1607                 if (status == -NFS4ERR_BADNAME &&
1608                                 !(o_arg->open_flags & O_CREAT))
1609                         return -ENOENT;
1610                 return status;
1611         }
1612
1613         if (o_arg->open_flags & O_CREAT) {
1614                 update_changeattr(dir, &o_res->cinfo);
1615                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1616         } else
1617                 nfs_refresh_inode(dir, o_res->dir_attr);
1618         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1619                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1620         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1621                 status = _nfs4_proc_open_confirm(data);
1622                 if (status != 0)
1623                         return status;
1624         }
1625         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1626                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1627         return 0;
1628 }
1629
1630 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1631 {
1632         unsigned int loop;
1633         int ret;
1634
1635         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1636                 ret = nfs4_wait_clnt_recover(clp);
1637                 if (ret != 0)
1638                         break;
1639                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1640                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1641                         break;
1642                 nfs4_schedule_state_manager(clp);
1643                 ret = -EIO;
1644         }
1645         return ret;
1646 }
1647
1648 static int nfs4_recover_expired_lease(struct nfs_server *server)
1649 {
1650         return nfs4_client_recover_expired_lease(server->nfs_client);
1651 }
1652
1653 /*
1654  * OPEN_EXPIRED:
1655  *      reclaim state on the server after a network partition.
1656  *      Assumes caller holds the appropriate lock
1657  */
1658 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1659 {
1660         struct nfs4_opendata *opendata;
1661         int ret;
1662
1663         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1664         if (IS_ERR(opendata))
1665                 return PTR_ERR(opendata);
1666         ret = nfs4_open_recover(opendata, state);
1667         if (ret == -ESTALE)
1668                 d_drop(ctx->dentry);
1669         nfs4_opendata_put(opendata);
1670         return ret;
1671 }
1672
1673 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1674 {
1675         struct nfs_server *server = NFS_SERVER(state->inode);
1676         struct nfs4_exception exception = { };
1677         int err;
1678
1679         do {
1680                 err = _nfs4_open_expired(ctx, state);
1681                 switch (err) {
1682                 default:
1683                         goto out;
1684                 case -NFS4ERR_GRACE:
1685                 case -NFS4ERR_DELAY:
1686                         nfs4_handle_exception(server, err, &exception);
1687                         err = 0;
1688                 }
1689         } while (exception.retry);
1690 out:
1691         return err;
1692 }
1693
1694 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1695 {
1696         struct nfs_open_context *ctx;
1697         int ret;
1698
1699         ctx = nfs4_state_find_open_context(state);
1700         if (IS_ERR(ctx))
1701                 return PTR_ERR(ctx);
1702         ret = nfs4_do_open_expired(ctx, state);
1703         put_nfs_open_context(ctx);
1704         return ret;
1705 }
1706
1707 #if defined(CONFIG_NFS_V4_1)
1708 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1709 {
1710         int status;
1711         struct nfs_server *server = NFS_SERVER(state->inode);
1712
1713         status = nfs41_test_stateid(server, state);
1714         if (status == NFS_OK)
1715                 return 0;
1716         nfs41_free_stateid(server, state);
1717         return nfs4_open_expired(sp, state);
1718 }
1719 #endif
1720
1721 /*
1722  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1723  * fields corresponding to attributes that were used to store the verifier.
1724  * Make sure we clobber those fields in the later setattr call
1725  */
1726 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1727 {
1728         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1729             !(sattr->ia_valid & ATTR_ATIME_SET))
1730                 sattr->ia_valid |= ATTR_ATIME;
1731
1732         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1733             !(sattr->ia_valid & ATTR_MTIME_SET))
1734                 sattr->ia_valid |= ATTR_MTIME;
1735 }
1736
1737 /*
1738  * Returns a referenced nfs4_state
1739  */
1740 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)
1741 {
1742         struct nfs4_state_owner  *sp;
1743         struct nfs4_state     *state = NULL;
1744         struct nfs_server       *server = NFS_SERVER(dir);
1745         struct nfs4_opendata *opendata;
1746         int status;
1747
1748         /* Protect against reboot recovery conflicts */
1749         status = -ENOMEM;
1750         if (!(sp = nfs4_get_state_owner(server, cred))) {
1751                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1752                 goto out_err;
1753         }
1754         status = nfs4_recover_expired_lease(server);
1755         if (status != 0)
1756                 goto err_put_state_owner;
1757         if (dentry->d_inode != NULL)
1758                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1759         status = -ENOMEM;
1760         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1761         if (opendata == NULL)
1762                 goto err_put_state_owner;
1763
1764         if (dentry->d_inode != NULL)
1765                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1766
1767         status = _nfs4_proc_open(opendata);
1768         if (status != 0)
1769                 goto err_opendata_put;
1770
1771         state = nfs4_opendata_to_nfs4_state(opendata);
1772         status = PTR_ERR(state);
1773         if (IS_ERR(state))
1774                 goto err_opendata_put;
1775         if (server->caps & NFS_CAP_POSIX_LOCK)
1776                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1777
1778         if (opendata->o_arg.open_flags & O_EXCL) {
1779                 nfs4_exclusive_attrset(opendata, sattr);
1780
1781                 nfs_fattr_init(opendata->o_res.f_attr);
1782                 status = nfs4_do_setattr(state->inode, cred,
1783                                 opendata->o_res.f_attr, sattr,
1784                                 state);
1785                 if (status == 0)
1786                         nfs_setattr_update_inode(state->inode, sattr);
1787                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1788         }
1789         nfs4_opendata_put(opendata);
1790         nfs4_put_state_owner(sp);
1791         *res = state;
1792         return 0;
1793 err_opendata_put:
1794         nfs4_opendata_put(opendata);
1795 err_put_state_owner:
1796         nfs4_put_state_owner(sp);
1797 out_err:
1798         *res = NULL;
1799         return status;
1800 }
1801
1802
1803 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)
1804 {
1805         struct nfs4_exception exception = { };
1806         struct nfs4_state *res;
1807         int status;
1808
1809         do {
1810                 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1811                 if (status == 0)
1812                         break;
1813                 /* NOTE: BAD_SEQID means the server and client disagree about the
1814                  * book-keeping w.r.t. state-changing operations
1815                  * (OPEN/CLOSE/LOCK/LOCKU...)
1816                  * It is actually a sign of a bug on the client or on the server.
1817                  *
1818                  * If we receive a BAD_SEQID error in the particular case of
1819                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1820                  * have unhashed the old state_owner for us, and that we can
1821                  * therefore safely retry using a new one. We should still warn
1822                  * the user though...
1823                  */
1824                 if (status == -NFS4ERR_BAD_SEQID) {
1825                         printk(KERN_WARNING "NFS: v4 server %s "
1826                                         " returned a bad sequence-id error!\n",
1827                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1828                         exception.retry = 1;
1829                         continue;
1830                 }
1831                 /*
1832                  * BAD_STATEID on OPEN means that the server cancelled our
1833                  * state before it received the OPEN_CONFIRM.
1834                  * Recover by retrying the request as per the discussion
1835                  * on Page 181 of RFC3530.
1836                  */
1837                 if (status == -NFS4ERR_BAD_STATEID) {
1838                         exception.retry = 1;
1839                         continue;
1840                 }
1841                 if (status == -EAGAIN) {
1842                         /* We must have found a delegation */
1843                         exception.retry = 1;
1844                         continue;
1845                 }
1846                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1847                                         status, &exception));
1848         } while (exception.retry);
1849         return res;
1850 }
1851
1852 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1853                             struct nfs_fattr *fattr, struct iattr *sattr,
1854                             struct nfs4_state *state)
1855 {
1856         struct nfs_server *server = NFS_SERVER(inode);
1857         struct nfs_setattrargs  arg = {
1858                 .fh             = NFS_FH(inode),
1859                 .iap            = sattr,
1860                 .server         = server,
1861                 .bitmask = server->attr_bitmask,
1862         };
1863         struct nfs_setattrres  res = {
1864                 .fattr          = fattr,
1865                 .server         = server,
1866         };
1867         struct rpc_message msg = {
1868                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1869                 .rpc_argp       = &arg,
1870                 .rpc_resp       = &res,
1871                 .rpc_cred       = cred,
1872         };
1873         unsigned long timestamp = jiffies;
1874         int status;
1875
1876         nfs_fattr_init(fattr);
1877
1878         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1879                 /* Use that stateid */
1880         } else if (state != NULL) {
1881                 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1882         } else
1883                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1884
1885         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1886         if (status == 0 && state != NULL)
1887                 renew_lease(server, timestamp);
1888         return status;
1889 }
1890
1891 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1892                            struct nfs_fattr *fattr, struct iattr *sattr,
1893                            struct nfs4_state *state)
1894 {
1895         struct nfs_server *server = NFS_SERVER(inode);
1896         struct nfs4_exception exception = { };
1897         int err;
1898         do {
1899                 err = nfs4_handle_exception(server,
1900                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1901                                 &exception);
1902         } while (exception.retry);
1903         return err;
1904 }
1905
1906 struct nfs4_closedata {
1907         struct inode *inode;
1908         struct nfs4_state *state;
1909         struct nfs_closeargs arg;
1910         struct nfs_closeres res;
1911         struct nfs_fattr fattr;
1912         unsigned long timestamp;
1913         bool roc;
1914         u32 roc_barrier;
1915 };
1916
1917 static void nfs4_free_closedata(void *data)
1918 {
1919         struct nfs4_closedata *calldata = data;
1920         struct nfs4_state_owner *sp = calldata->state->owner;
1921         struct super_block *sb = calldata->state->inode->i_sb;
1922
1923         if (calldata->roc)
1924                 pnfs_roc_release(calldata->state->inode);
1925         nfs4_put_open_state(calldata->state);
1926         nfs_free_seqid(calldata->arg.seqid);
1927         nfs4_put_state_owner(sp);
1928         nfs_sb_deactive(sb);
1929         kfree(calldata);
1930 }
1931
1932 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1933                 fmode_t fmode)
1934 {
1935         spin_lock(&state->owner->so_lock);
1936         if (!(fmode & FMODE_READ))
1937                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1938         if (!(fmode & FMODE_WRITE))
1939                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1941         spin_unlock(&state->owner->so_lock);
1942 }
1943
1944 static void nfs4_close_done(struct rpc_task *task, void *data)
1945 {
1946         struct nfs4_closedata *calldata = data;
1947         struct nfs4_state *state = calldata->state;
1948         struct nfs_server *server = NFS_SERVER(calldata->inode);
1949
1950         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1951                 return;
1952         /* hmm. we are done with the inode, and in the process of freeing
1953          * the state_owner. we keep this around to process errors
1954          */
1955         switch (task->tk_status) {
1956                 case 0:
1957                         if (calldata->roc)
1958                                 pnfs_roc_set_barrier(state->inode,
1959                                                      calldata->roc_barrier);
1960                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1961                         renew_lease(server, calldata->timestamp);
1962                         nfs4_close_clear_stateid_flags(state,
1963                                         calldata->arg.fmode);
1964                         break;
1965                 case -NFS4ERR_STALE_STATEID:
1966                 case -NFS4ERR_OLD_STATEID:
1967                 case -NFS4ERR_BAD_STATEID:
1968                 case -NFS4ERR_EXPIRED:
1969                         if (calldata->arg.fmode == 0)
1970                                 break;
1971                 default:
1972                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1973                                 rpc_restart_call_prepare(task);
1974         }
1975         nfs_release_seqid(calldata->arg.seqid);
1976         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1977 }
1978
1979 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1980 {
1981         struct nfs4_closedata *calldata = data;
1982         struct nfs4_state *state = calldata->state;
1983         int call_close = 0;
1984
1985         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1986                 return;
1987
1988         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1989         calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1990         spin_lock(&state->owner->so_lock);
1991         /* Calculate the change in open mode */
1992         if (state->n_rdwr == 0) {
1993                 if (state->n_rdonly == 0) {
1994                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1995                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1996                         calldata->arg.fmode &= ~FMODE_READ;
1997                 }
1998                 if (state->n_wronly == 0) {
1999                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2000                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2001                         calldata->arg.fmode &= ~FMODE_WRITE;
2002                 }
2003         }
2004         spin_unlock(&state->owner->so_lock);
2005
2006         if (!call_close) {
2007                 /* Note: exit _without_ calling nfs4_close_done */
2008                 task->tk_action = NULL;
2009                 return;
2010         }
2011
2012         if (calldata->arg.fmode == 0) {
2013                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2014                 if (calldata->roc &&
2015                     pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2016                         rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2017                                      task, NULL);
2018                         return;
2019                 }
2020         }
2021
2022         nfs_fattr_init(calldata->res.fattr);
2023         calldata->timestamp = jiffies;
2024         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2025                                 &calldata->arg.seq_args, &calldata->res.seq_res,
2026                                 1, task))
2027                 return;
2028         rpc_call_start(task);
2029 }
2030
2031 static const struct rpc_call_ops nfs4_close_ops = {
2032         .rpc_call_prepare = nfs4_close_prepare,
2033         .rpc_call_done = nfs4_close_done,
2034         .rpc_release = nfs4_free_closedata,
2035 };
2036
2037 /* 
2038  * It is possible for data to be read/written from a mem-mapped file 
2039  * after the sys_close call (which hits the vfs layer as a flush).
2040  * This means that we can't safely call nfsv4 close on a file until 
2041  * the inode is cleared. This in turn means that we are not good
2042  * NFSv4 citizens - we do not indicate to the server to update the file's 
2043  * share state even when we are done with one of the three share 
2044  * stateid's in the inode.
2045  *
2046  * NOTE: Caller must be holding the sp->so_owner semaphore!
2047  */
2048 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2049 {
2050         struct nfs_server *server = NFS_SERVER(state->inode);
2051         struct nfs4_closedata *calldata;
2052         struct nfs4_state_owner *sp = state->owner;
2053         struct rpc_task *task;
2054         struct rpc_message msg = {
2055                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2056                 .rpc_cred = state->owner->so_cred,
2057         };
2058         struct rpc_task_setup task_setup_data = {
2059                 .rpc_client = server->client,
2060                 .rpc_message = &msg,
2061                 .callback_ops = &nfs4_close_ops,
2062                 .workqueue = nfsiod_workqueue,
2063                 .flags = RPC_TASK_ASYNC,
2064         };
2065         int status = -ENOMEM;
2066
2067         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2068         if (calldata == NULL)
2069                 goto out;
2070         calldata->inode = state->inode;
2071         calldata->state = state;
2072         calldata->arg.fh = NFS_FH(state->inode);
2073         calldata->arg.stateid = &state->open_stateid;
2074         /* Serialization for the sequence id */
2075         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2076         if (calldata->arg.seqid == NULL)
2077                 goto out_free_calldata;
2078         calldata->arg.fmode = 0;
2079         calldata->arg.bitmask = server->cache_consistency_bitmask;
2080         calldata->res.fattr = &calldata->fattr;
2081         calldata->res.seqid = calldata->arg.seqid;
2082         calldata->res.server = server;
2083         calldata->roc = roc;
2084         nfs_sb_active(calldata->inode->i_sb);
2085
2086         msg.rpc_argp = &calldata->arg;
2087         msg.rpc_resp = &calldata->res;
2088         task_setup_data.callback_data = calldata;
2089         task = rpc_run_task(&task_setup_data);
2090         if (IS_ERR(task))
2091                 return PTR_ERR(task);
2092         status = 0;
2093         if (wait)
2094                 status = rpc_wait_for_completion_task(task);
2095         rpc_put_task(task);
2096         return status;
2097 out_free_calldata:
2098         kfree(calldata);
2099 out:
2100         if (roc)
2101                 pnfs_roc_release(state->inode);
2102         nfs4_put_open_state(state);
2103         nfs4_put_state_owner(sp);
2104         return status;
2105 }
2106
2107 static struct inode *
2108 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2109 {
2110         struct nfs4_state *state;
2111
2112         /* Protect against concurrent sillydeletes */
2113         state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2114         if (IS_ERR(state))
2115                 return ERR_CAST(state);
2116         ctx->state = state;
2117         return igrab(state->inode);
2118 }
2119
2120 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2121 {
2122         if (ctx->state == NULL)
2123                 return;
2124         if (is_sync)
2125                 nfs4_close_sync(ctx->state, ctx->mode);
2126         else
2127                 nfs4_close_state(ctx->state, ctx->mode);
2128 }
2129
2130 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2131 {
2132         struct nfs4_server_caps_arg args = {
2133                 .fhandle = fhandle,
2134         };
2135         struct nfs4_server_caps_res res = {};
2136         struct rpc_message msg = {
2137                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2138                 .rpc_argp = &args,
2139                 .rpc_resp = &res,
2140         };
2141         int status;
2142
2143         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2144         if (status == 0) {
2145                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2146                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2147                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2148                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2149                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2150                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2151                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2152                         server->caps |= NFS_CAP_ACLS;
2153                 if (res.has_links != 0)
2154                         server->caps |= NFS_CAP_HARDLINKS;
2155                 if (res.has_symlinks != 0)
2156                         server->caps |= NFS_CAP_SYMLINKS;
2157                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2158                         server->caps |= NFS_CAP_FILEID;
2159                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2160                         server->caps |= NFS_CAP_MODE;
2161                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2162                         server->caps |= NFS_CAP_NLINK;
2163                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2164                         server->caps |= NFS_CAP_OWNER;
2165                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2166                         server->caps |= NFS_CAP_OWNER_GROUP;
2167                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2168                         server->caps |= NFS_CAP_ATIME;
2169                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2170                         server->caps |= NFS_CAP_CTIME;
2171                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2172                         server->caps |= NFS_CAP_MTIME;
2173
2174                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2175                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2176                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2177                 server->acl_bitmask = res.acl_bitmask;
2178         }
2179
2180         return status;
2181 }
2182
2183 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2184 {
2185         struct nfs4_exception exception = { };
2186         int err;
2187         do {
2188                 err = nfs4_handle_exception(server,
2189                                 _nfs4_server_capabilities(server, fhandle),
2190                                 &exception);
2191         } while (exception.retry);
2192         return err;
2193 }
2194
2195 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2196                 struct nfs_fsinfo *info)
2197 {
2198         struct nfs4_lookup_root_arg args = {
2199                 .bitmask = nfs4_fattr_bitmap,
2200         };
2201         struct nfs4_lookup_res res = {
2202                 .server = server,
2203                 .fattr = info->fattr,
2204                 .fh = fhandle,
2205         };
2206         struct rpc_message msg = {
2207                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2208                 .rpc_argp = &args,
2209                 .rpc_resp = &res,
2210         };
2211
2212         nfs_fattr_init(info->fattr);
2213         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2214 }
2215
2216 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217                 struct nfs_fsinfo *info)
2218 {
2219         struct nfs4_exception exception = { };
2220         int err;
2221         do {
2222                 err = _nfs4_lookup_root(server, fhandle, info);
2223                 switch (err) {
2224                 case 0:
2225                 case -NFS4ERR_WRONGSEC:
2226                         break;
2227                 default:
2228                         err = nfs4_handle_exception(server, err, &exception);
2229                 }
2230         } while (exception.retry);
2231         return err;
2232 }
2233
2234 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2235                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2236 {
2237         struct rpc_auth *auth;
2238         int ret;
2239
2240         auth = rpcauth_create(flavor, server->client);
2241         if (!auth) {
2242                 ret = -EIO;
2243                 goto out;
2244         }
2245         ret = nfs4_lookup_root(server, fhandle, info);
2246 out:
2247         return ret;
2248 }
2249
2250 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2251                               struct nfs_fsinfo *info)
2252 {
2253         int i, len, status = 0;
2254         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2255
2256         len = gss_mech_list_pseudoflavors(&flav_array[0]);
2257         flav_array[len] = RPC_AUTH_NULL;
2258         len += 1;
2259
2260         for (i = 0; i < len; i++) {
2261                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2262                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2263                         continue;
2264                 break;
2265         }
2266         /*
2267          * -EACCESS could mean that the user doesn't have correct permissions
2268          * to access the mount.  It could also mean that we tried to mount
2269          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2270          * existing mount programs don't handle -EACCES very well so it should
2271          * be mapped to -EPERM instead.
2272          */
2273         if (status == -EACCES)
2274                 status = -EPERM;
2275         return status;
2276 }
2277
2278 /*
2279  * get the file handle for the "/" directory on the server
2280  */
2281 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2282                               struct nfs_fsinfo *info)
2283 {
2284         int minor_version = server->nfs_client->cl_minorversion;
2285         int status = nfs4_lookup_root(server, fhandle, info);
2286         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2287                 /*
2288                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2289                  * by nfs4_map_errors() as this function exits.
2290                  */
2291                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2292         if (status == 0)
2293                 status = nfs4_server_capabilities(server, fhandle);
2294         if (status == 0)
2295                 status = nfs4_do_fsinfo(server, fhandle, info);
2296         return nfs4_map_errors(status);
2297 }
2298
2299 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2300 /*
2301  * Get locations and (maybe) other attributes of a referral.
2302  * Note that we'll actually follow the referral later when
2303  * we detect fsid mismatch in inode revalidation
2304  */
2305 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2306                              struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2307 {
2308         int status = -ENOMEM;
2309         struct page *page = NULL;
2310         struct nfs4_fs_locations *locations = NULL;
2311
2312         page = alloc_page(GFP_KERNEL);
2313         if (page == NULL)
2314                 goto out;
2315         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2316         if (locations == NULL)
2317                 goto out;
2318
2319         status = nfs4_proc_fs_locations(dir, name, locations, page);
2320         if (status != 0)
2321                 goto out;
2322         /* Make sure server returned a different fsid for the referral */
2323         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2324                 dprintk("%s: server did not return a different fsid for"
2325                         " a referral at %s\n", __func__, name->name);
2326                 status = -EIO;
2327                 goto out;
2328         }
2329         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2330         nfs_fixup_referral_attributes(&locations->fattr);
2331
2332         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2333         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2334         memset(fhandle, 0, sizeof(struct nfs_fh));
2335 out:
2336         if (page)
2337                 __free_page(page);
2338         kfree(locations);
2339         return status;
2340 }
2341
2342 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2343 {
2344         struct nfs4_getattr_arg args = {
2345                 .fh = fhandle,
2346                 .bitmask = server->attr_bitmask,
2347         };
2348         struct nfs4_getattr_res res = {
2349                 .fattr = fattr,
2350                 .server = server,
2351         };
2352         struct rpc_message msg = {
2353                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2354                 .rpc_argp = &args,
2355                 .rpc_resp = &res,
2356         };
2357         
2358         nfs_fattr_init(fattr);
2359         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2360 }
2361
2362 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2363 {
2364         struct nfs4_exception exception = { };
2365         int err;
2366         do {
2367                 err = nfs4_handle_exception(server,
2368                                 _nfs4_proc_getattr(server, fhandle, fattr),
2369                                 &exception);
2370         } while (exception.retry);
2371         return err;
2372 }
2373
2374 /* 
2375  * The file is not closed if it is opened due to the a request to change
2376  * the size of the file. The open call will not be needed once the
2377  * VFS layer lookup-intents are implemented.
2378  *
2379  * Close is called when the inode is destroyed.
2380  * If we haven't opened the file for O_WRONLY, we
2381  * need to in the size_change case to obtain a stateid.
2382  *
2383  * Got race?
2384  * Because OPEN is always done by name in nfsv4, it is
2385  * possible that we opened a different file by the same
2386  * name.  We can recognize this race condition, but we
2387  * can't do anything about it besides returning an error.
2388  *
2389  * This will be fixed with VFS changes (lookup-intent).
2390  */
2391 static int
2392 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2393                   struct iattr *sattr)
2394 {
2395         struct inode *inode = dentry->d_inode;
2396         struct rpc_cred *cred = NULL;
2397         struct nfs4_state *state = NULL;
2398         int status;
2399
2400         if (pnfs_ld_layoutret_on_setattr(inode))
2401                 pnfs_return_layout(inode);
2402
2403         nfs_fattr_init(fattr);
2404         
2405         /* Search for an existing open(O_WRITE) file */
2406         if (sattr->ia_valid & ATTR_FILE) {
2407                 struct nfs_open_context *ctx;
2408
2409                 ctx = nfs_file_open_context(sattr->ia_file);
2410                 if (ctx) {
2411                         cred = ctx->cred;
2412                         state = ctx->state;
2413                 }
2414         }
2415
2416         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2417         if (status == 0)
2418                 nfs_setattr_update_inode(inode, sattr);
2419         return status;
2420 }
2421
2422 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2423                 const struct qstr *name, struct nfs_fh *fhandle,
2424                 struct nfs_fattr *fattr)
2425 {
2426         struct nfs_server *server = NFS_SERVER(dir);
2427         int                    status;
2428         struct nfs4_lookup_arg args = {
2429                 .bitmask = server->attr_bitmask,
2430                 .dir_fh = NFS_FH(dir),
2431                 .name = name,
2432         };
2433         struct nfs4_lookup_res res = {
2434                 .server = server,
2435                 .fattr = fattr,
2436                 .fh = fhandle,
2437         };
2438         struct rpc_message msg = {
2439                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2440                 .rpc_argp = &args,
2441                 .rpc_resp = &res,
2442         };
2443
2444         nfs_fattr_init(fattr);
2445
2446         dprintk("NFS call  lookup %s\n", name->name);
2447         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2448         dprintk("NFS reply lookup: %d\n", status);
2449         return status;
2450 }
2451
2452 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2453 {
2454         memset(fh, 0, sizeof(struct nfs_fh));
2455         fattr->fsid.major = 1;
2456         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2457                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2458         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2459         fattr->nlink = 2;
2460 }
2461
2462 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2463                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2464 {
2465         struct nfs4_exception exception = { };
2466         int err;
2467         do {
2468                 int status;
2469
2470                 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2471                 switch (status) {
2472                 case -NFS4ERR_BADNAME:
2473                         return -ENOENT;
2474                 case -NFS4ERR_MOVED:
2475                         return nfs4_get_referral(dir, name, fattr, fhandle);
2476                 case -NFS4ERR_WRONGSEC:
2477                         nfs_fixup_secinfo_attributes(fattr, fhandle);
2478                 }
2479                 err = nfs4_handle_exception(NFS_SERVER(dir),
2480                                 status, &exception);
2481         } while (exception.retry);
2482         return err;
2483 }
2484
2485 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2486 {
2487         struct nfs_server *server = NFS_SERVER(inode);
2488         struct nfs4_accessargs args = {
2489                 .fh = NFS_FH(inode),
2490                 .bitmask = server->attr_bitmask,
2491         };
2492         struct nfs4_accessres res = {
2493                 .server = server,
2494         };
2495         struct rpc_message msg = {
2496                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2497                 .rpc_argp = &args,
2498                 .rpc_resp = &res,
2499                 .rpc_cred = entry->cred,
2500         };
2501         int mode = entry->mask;
2502         int status;
2503
2504         /*
2505          * Determine which access bits we want to ask for...
2506          */
2507         if (mode & MAY_READ)
2508                 args.access |= NFS4_ACCESS_READ;
2509         if (S_ISDIR(inode->i_mode)) {
2510                 if (mode & MAY_WRITE)
2511                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2512                 if (mode & MAY_EXEC)
2513                         args.access |= NFS4_ACCESS_LOOKUP;
2514         } else {
2515                 if (mode & MAY_WRITE)
2516                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2517                 if (mode & MAY_EXEC)
2518                         args.access |= NFS4_ACCESS_EXECUTE;
2519         }
2520
2521         res.fattr = nfs_alloc_fattr();
2522         if (res.fattr == NULL)
2523                 return -ENOMEM;
2524
2525         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2526         if (!status) {
2527                 entry->mask = 0;
2528                 if (res.access & NFS4_ACCESS_READ)
2529                         entry->mask |= MAY_READ;
2530                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2531                         entry->mask |= MAY_WRITE;
2532                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2533                         entry->mask |= MAY_EXEC;
2534                 nfs_refresh_inode(inode, res.fattr);
2535         }
2536         nfs_free_fattr(res.fattr);
2537         return status;
2538 }
2539
2540 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2541 {
2542         struct nfs4_exception exception = { };
2543         int err;
2544         do {
2545                 err = nfs4_handle_exception(NFS_SERVER(inode),
2546                                 _nfs4_proc_access(inode, entry),
2547                                 &exception);
2548         } while (exception.retry);
2549         return err;
2550 }
2551
2552 /*
2553  * TODO: For the time being, we don't try to get any attributes
2554  * along with any of the zero-copy operations READ, READDIR,
2555  * READLINK, WRITE.
2556  *
2557  * In the case of the first three, we want to put the GETATTR
2558  * after the read-type operation -- this is because it is hard
2559  * to predict the length of a GETATTR response in v4, and thus
2560  * align the READ data correctly.  This means that the GETATTR
2561  * may end up partially falling into the page cache, and we should
2562  * shift it into the 'tail' of the xdr_buf before processing.
2563  * To do this efficiently, we need to know the total length
2564  * of data received, which doesn't seem to be available outside
2565  * of the RPC layer.
2566  *
2567  * In the case of WRITE, we also want to put the GETATTR after
2568  * the operation -- in this case because we want to make sure
2569  * we get the post-operation mtime and size.  This means that
2570  * we can't use xdr_encode_pages() as written: we need a variant
2571  * of it which would leave room in the 'tail' iovec.
2572  *
2573  * Both of these changes to the XDR layer would in fact be quite
2574  * minor, but I decided to leave them for a subsequent patch.
2575  */
2576 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2577                 unsigned int pgbase, unsigned int pglen)
2578 {
2579         struct nfs4_readlink args = {
2580                 .fh       = NFS_FH(inode),
2581                 .pgbase   = pgbase,
2582                 .pglen    = pglen,
2583                 .pages    = &page,
2584         };
2585         struct nfs4_readlink_res res;
2586         struct rpc_message msg = {
2587                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2588                 .rpc_argp = &args,
2589                 .rpc_resp = &res,
2590         };
2591
2592         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2593 }
2594
2595 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2596                 unsigned int pgbase, unsigned int pglen)
2597 {
2598         struct nfs4_exception exception = { };
2599         int err;
2600         do {
2601                 err = nfs4_handle_exception(NFS_SERVER(inode),
2602                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2603                                 &exception);
2604         } while (exception.retry);
2605         return err;
2606 }
2607
2608 /*
2609  * Got race?
2610  * We will need to arrange for the VFS layer to provide an atomic open.
2611  * Until then, this create/open method is prone to inefficiency and race
2612  * conditions due to the lookup, create, and open VFS calls from sys_open()
2613  * placed on the wire.
2614  *
2615  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2616  * The file will be opened again in the subsequent VFS open call
2617  * (nfs4_proc_file_open).
2618  *
2619  * The open for read will just hang around to be used by any process that
2620  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2621  */
2622
2623 static int
2624 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2625                  int flags, struct nfs_open_context *ctx)
2626 {
2627         struct dentry *de = dentry;
2628         struct nfs4_state *state;
2629         struct rpc_cred *cred = NULL;
2630         fmode_t fmode = 0;
2631         int status = 0;
2632
2633         if (ctx != NULL) {
2634                 cred = ctx->cred;
2635                 de = ctx->dentry;
2636                 fmode = ctx->mode;
2637         }
2638         sattr->ia_mode &= ~current_umask();
2639         state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2640         d_drop(dentry);
2641         if (IS_ERR(state)) {
2642                 status = PTR_ERR(state);
2643                 goto out;
2644         }
2645         d_add(dentry, igrab(state->inode));
2646         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2647         if (ctx != NULL)
2648                 ctx->state = state;
2649         else
2650                 nfs4_close_sync(state, fmode);
2651 out:
2652         return status;
2653 }
2654
2655 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2656 {
2657         struct nfs_server *server = NFS_SERVER(dir);
2658         struct nfs_removeargs args = {
2659                 .fh = NFS_FH(dir),
2660                 .name.len = name->len,
2661                 .name.name = name->name,
2662                 .bitmask = server->attr_bitmask,
2663         };
2664         struct nfs_removeres res = {
2665                 .server = server,
2666         };
2667         struct rpc_message msg = {
2668                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2669                 .rpc_argp = &args,
2670                 .rpc_resp = &res,
2671         };
2672         int status = -ENOMEM;
2673
2674         res.dir_attr = nfs_alloc_fattr();
2675         if (res.dir_attr == NULL)
2676                 goto out;
2677
2678         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2679         if (status == 0) {
2680                 update_changeattr(dir, &res.cinfo);
2681                 nfs_post_op_update_inode(dir, res.dir_attr);
2682         }
2683         nfs_free_fattr(res.dir_attr);
2684 out:
2685         return status;
2686 }
2687
2688 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2689 {
2690         struct nfs4_exception exception = { };
2691         int err;
2692         do {
2693                 err = nfs4_handle_exception(NFS_SERVER(dir),
2694                                 _nfs4_proc_remove(dir, name),
2695                                 &exception);
2696         } while (exception.retry);
2697         return err;
2698 }
2699
2700 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2701 {
2702         struct nfs_server *server = NFS_SERVER(dir);
2703         struct nfs_removeargs *args = msg->rpc_argp;
2704         struct nfs_removeres *res = msg->rpc_resp;
2705
2706         args->bitmask = server->cache_consistency_bitmask;
2707         res->server = server;
2708         res->seq_res.sr_slot = NULL;
2709         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2710 }
2711
2712 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2713 {
2714         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2715
2716         if (!nfs4_sequence_done(task, &res->seq_res))
2717                 return 0;
2718         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2719                 return 0;
2720         update_changeattr(dir, &res->cinfo);
2721         nfs_post_op_update_inode(dir, res->dir_attr);
2722         return 1;
2723 }
2724
2725 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2726 {
2727         struct nfs_server *server = NFS_SERVER(dir);
2728         struct nfs_renameargs *arg = msg->rpc_argp;
2729         struct nfs_renameres *res = msg->rpc_resp;
2730
2731         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2732         arg->bitmask = server->attr_bitmask;
2733         res->server = server;
2734 }
2735
2736 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2737                                  struct inode *new_dir)
2738 {
2739         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2740
2741         if (!nfs4_sequence_done(task, &res->seq_res))
2742                 return 0;
2743         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2744                 return 0;
2745
2746         update_changeattr(old_dir, &res->old_cinfo);
2747         nfs_post_op_update_inode(old_dir, res->old_fattr);
2748         update_changeattr(new_dir, &res->new_cinfo);
2749         nfs_post_op_update_inode(new_dir, res->new_fattr);
2750         return 1;
2751 }
2752
2753 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2754                 struct inode *new_dir, struct qstr *new_name)
2755 {
2756         struct nfs_server *server = NFS_SERVER(old_dir);
2757         struct nfs_renameargs arg = {
2758                 .old_dir = NFS_FH(old_dir),
2759                 .new_dir = NFS_FH(new_dir),
2760                 .old_name = old_name,
2761                 .new_name = new_name,
2762                 .bitmask = server->attr_bitmask,
2763         };
2764         struct nfs_renameres res = {
2765                 .server = server,
2766         };
2767         struct rpc_message msg = {
2768                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2769                 .rpc_argp = &arg,
2770                 .rpc_resp = &res,
2771         };
2772         int status = -ENOMEM;
2773         
2774         res.old_fattr = nfs_alloc_fattr();
2775         res.new_fattr = nfs_alloc_fattr();
2776         if (res.old_fattr == NULL || res.new_fattr == NULL)
2777                 goto out;
2778
2779         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2780         if (!status) {
2781                 update_changeattr(old_dir, &res.old_cinfo);
2782                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2783                 update_changeattr(new_dir, &res.new_cinfo);
2784                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2785         }
2786 out:
2787         nfs_free_fattr(res.new_fattr);
2788         nfs_free_fattr(res.old_fattr);
2789         return status;
2790 }
2791
2792 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2793                 struct inode *new_dir, struct qstr *new_name)
2794 {
2795         struct nfs4_exception exception = { };
2796         int err;
2797         do {
2798                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2799                                 _nfs4_proc_rename(old_dir, old_name,
2800                                         new_dir, new_name),
2801                                 &exception);
2802         } while (exception.retry);
2803         return err;
2804 }
2805
2806 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2807 {
2808         struct nfs_server *server = NFS_SERVER(inode);
2809         struct nfs4_link_arg arg = {
2810                 .fh     = NFS_FH(inode),
2811                 .dir_fh = NFS_FH(dir),
2812                 .name   = name,
2813                 .bitmask = server->attr_bitmask,
2814         };
2815         struct nfs4_link_res res = {
2816                 .server = server,
2817         };
2818         struct rpc_message msg = {
2819                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2820                 .rpc_argp = &arg,
2821                 .rpc_resp = &res,
2822         };
2823         int status = -ENOMEM;
2824
2825         res.fattr = nfs_alloc_fattr();
2826         res.dir_attr = nfs_alloc_fattr();
2827         if (res.fattr == NULL || res.dir_attr == NULL)
2828                 goto out;
2829
2830         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2831         if (!status) {
2832                 update_changeattr(dir, &res.cinfo);
2833                 nfs_post_op_update_inode(dir, res.dir_attr);
2834                 nfs_post_op_update_inode(inode, res.fattr);
2835         }
2836 out:
2837         nfs_free_fattr(res.dir_attr);
2838         nfs_free_fattr(res.fattr);
2839         return status;
2840 }
2841
2842 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2843 {
2844         struct nfs4_exception exception = { };
2845         int err;
2846         do {
2847                 err = nfs4_handle_exception(NFS_SERVER(inode),
2848                                 _nfs4_proc_link(inode, dir, name),
2849                                 &exception);
2850         } while (exception.retry);
2851         return err;
2852 }
2853
2854 struct nfs4_createdata {
2855         struct rpc_message msg;
2856         struct nfs4_create_arg arg;
2857         struct nfs4_create_res res;
2858         struct nfs_fh fh;
2859         struct nfs_fattr fattr;
2860         struct nfs_fattr dir_fattr;
2861 };
2862
2863 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2864                 struct qstr *name, struct iattr *sattr, u32 ftype)
2865 {
2866         struct nfs4_createdata *data;
2867
2868         data = kzalloc(sizeof(*data), GFP_KERNEL);
2869         if (data != NULL) {
2870                 struct nfs_server *server = NFS_SERVER(dir);
2871
2872                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2873                 data->msg.rpc_argp = &data->arg;
2874                 data->msg.rpc_resp = &data->res;
2875                 data->arg.dir_fh = NFS_FH(dir);
2876                 data->arg.server = server;
2877                 data->arg.name = name;
2878                 data->arg.attrs = sattr;
2879                 data->arg.ftype = ftype;
2880                 data->arg.bitmask = server->attr_bitmask;
2881                 data->res.server = server;
2882                 data->res.fh = &data->fh;
2883                 data->res.fattr = &data->fattr;
2884                 data->res.dir_fattr = &data->dir_fattr;
2885                 nfs_fattr_init(data->res.fattr);
2886                 nfs_fattr_init(data->res.dir_fattr);
2887         }
2888         return data;
2889 }
2890
2891 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2892 {
2893         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2894                                     &data->arg.seq_args, &data->res.seq_res, 1);
2895         if (status == 0) {
2896                 update_changeattr(dir, &data->res.dir_cinfo);
2897                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2898                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2899         }
2900         return status;
2901 }
2902
2903 static void nfs4_free_createdata(struct nfs4_createdata *data)
2904 {
2905         kfree(data);
2906 }
2907
2908 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2909                 struct page *page, unsigned int len, struct iattr *sattr)
2910 {
2911         struct nfs4_createdata *data;
2912         int status = -ENAMETOOLONG;
2913
2914         if (len > NFS4_MAXPATHLEN)
2915                 goto out;
2916
2917         status = -ENOMEM;
2918         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2919         if (data == NULL)
2920                 goto out;
2921
2922         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2923         data->arg.u.symlink.pages = &page;
2924         data->arg.u.symlink.len = len;
2925         
2926         status = nfs4_do_create(dir, dentry, data);
2927
2928         nfs4_free_createdata(data);
2929 out:
2930         return status;
2931 }
2932
2933 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2934                 struct page *page, unsigned int len, struct iattr *sattr)
2935 {
2936         struct nfs4_exception exception = { };
2937         int err;
2938         do {
2939                 err = nfs4_handle_exception(NFS_SERVER(dir),
2940                                 _nfs4_proc_symlink(dir, dentry, page,
2941                                                         len, sattr),
2942                                 &exception);
2943         } while (exception.retry);
2944         return err;
2945 }
2946
2947 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2948                 struct iattr *sattr)
2949 {
2950         struct nfs4_createdata *data;
2951         int status = -ENOMEM;
2952
2953         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2954         if (data == NULL)
2955                 goto out;
2956
2957         status = nfs4_do_create(dir, dentry, data);
2958
2959         nfs4_free_createdata(data);
2960 out:
2961         return status;
2962 }
2963
2964 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2965                 struct iattr *sattr)
2966 {
2967         struct nfs4_exception exception = { };
2968         int err;
2969
2970         sattr->ia_mode &= ~current_umask();
2971         do {
2972                 err = nfs4_handle_exception(NFS_SERVER(dir),
2973                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2974                                 &exception);
2975         } while (exception.retry);
2976         return err;
2977 }
2978
2979 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2980                 u64 cookie, struct page **pages, unsigned int count, int plus)
2981 {
2982         struct inode            *dir = dentry->d_inode;
2983         struct nfs4_readdir_arg args = {
2984                 .fh = NFS_FH(dir),
2985                 .pages = pages,
2986                 .pgbase = 0,
2987                 .count = count,
2988                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2989                 .plus = plus,
2990         };
2991         struct nfs4_readdir_res res;
2992         struct rpc_message msg = {
2993                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2994                 .rpc_argp = &args,
2995                 .rpc_resp = &res,
2996                 .rpc_cred = cred,
2997         };
2998         int                     status;
2999
3000         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3001                         dentry->d_parent->d_name.name,
3002                         dentry->d_name.name,
3003                         (unsigned long long)cookie);
3004         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3005         res.pgbase = args.pgbase;
3006         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3007         if (status >= 0) {
3008                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3009                 status += args.pgbase;
3010         }
3011
3012         nfs_invalidate_atime(dir);
3013
3014         dprintk("%s: returns %d\n", __func__, status);
3015         return status;
3016 }
3017
3018 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3019                 u64 cookie, struct page **pages, unsigned int count, int plus)
3020 {
3021         struct nfs4_exception exception = { };
3022         int err;
3023         do {
3024                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3025                                 _nfs4_proc_readdir(dentry, cred, cookie,
3026                                         pages, count, plus),
3027                                 &exception);
3028         } while (exception.retry);
3029         return err;
3030 }
3031
3032 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3033                 struct iattr *sattr, dev_t rdev)
3034 {
3035         struct nfs4_createdata *data;
3036         int mode = sattr->ia_mode;
3037         int status = -ENOMEM;
3038
3039         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3040         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3041
3042         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3043         if (data == NULL)
3044                 goto out;
3045
3046         if (S_ISFIFO(mode))
3047                 data->arg.ftype = NF4FIFO;
3048         else if (S_ISBLK(mode)) {
3049                 data->arg.ftype = NF4BLK;
3050                 data->arg.u.device.specdata1 = MAJOR(rdev);
3051                 data->arg.u.device.specdata2 = MINOR(rdev);
3052         }
3053         else if (S_ISCHR(mode)) {
3054                 data->arg.ftype = NF4CHR;
3055                 data->arg.u.device.specdata1 = MAJOR(rdev);
3056                 data->arg.u.device.specdata2 = MINOR(rdev);
3057         }
3058         
3059         status = nfs4_do_create(dir, dentry, data);
3060
3061         nfs4_free_createdata(data);
3062 out:
3063         return status;
3064 }
3065
3066 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3067                 struct iattr *sattr, dev_t rdev)
3068 {
3069         struct nfs4_exception exception = { };
3070         int err;
3071
3072         sattr->ia_mode &= ~current_umask();
3073         do {
3074                 err = nfs4_handle_exception(NFS_SERVER(dir),
3075                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3076                                 &exception);
3077         } while (exception.retry);
3078         return err;
3079 }
3080
3081 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3082                  struct nfs_fsstat *fsstat)
3083 {
3084         struct nfs4_statfs_arg args = {
3085                 .fh = fhandle,
3086                 .bitmask = server->attr_bitmask,
3087         };
3088         struct nfs4_statfs_res res = {
3089                 .fsstat = fsstat,
3090         };
3091         struct rpc_message msg = {
3092                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3093                 .rpc_argp = &args,
3094                 .rpc_resp = &res,
3095         };
3096
3097         nfs_fattr_init(fsstat->fattr);
3098         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3099 }
3100
3101 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3102 {
3103         struct nfs4_exception exception = { };
3104         int err;
3105         do {
3106                 err = nfs4_handle_exception(server,
3107                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3108                                 &exception);
3109         } while (exception.retry);
3110         return err;
3111 }
3112
3113 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3114                 struct nfs_fsinfo *fsinfo)
3115 {
3116         struct nfs4_fsinfo_arg args = {
3117                 .fh = fhandle,
3118                 .bitmask = server->attr_bitmask,
3119         };
3120         struct nfs4_fsinfo_res res = {
3121                 .fsinfo = fsinfo,
3122         };
3123         struct rpc_message msg = {
3124                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3125                 .rpc_argp = &args,
3126                 .rpc_resp = &res,
3127         };
3128
3129         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3130 }
3131
3132 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3133 {
3134         struct nfs4_exception exception = { };
3135         int err;
3136
3137         do {
3138                 err = nfs4_handle_exception(server,
3139                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3140                                 &exception);
3141         } while (exception.retry);
3142         return err;
3143 }
3144
3145 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3146 {
3147         nfs_fattr_init(fsinfo->fattr);
3148         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3149 }
3150
3151 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3152                 struct nfs_pathconf *pathconf)
3153 {
3154         struct nfs4_pathconf_arg args = {
3155                 .fh = fhandle,
3156                 .bitmask = server->attr_bitmask,
3157         };
3158         struct nfs4_pathconf_res res = {
3159                 .pathconf = pathconf,
3160         };
3161         struct rpc_message msg = {
3162                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3163                 .rpc_argp = &args,
3164                 .rpc_resp = &res,
3165         };
3166
3167         /* None of the pathconf attributes are mandatory to implement */
3168         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3169                 memset(pathconf, 0, sizeof(*pathconf));
3170                 return 0;
3171         }
3172
3173         nfs_fattr_init(pathconf->fattr);
3174         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3175 }
3176
3177 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3178                 struct nfs_pathconf *pathconf)
3179 {
3180         struct nfs4_exception exception = { };
3181         int err;
3182
3183         do {
3184                 err = nfs4_handle_exception(server,
3185                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3186                                 &exception);
3187         } while (exception.retry);
3188         return err;
3189 }
3190
3191 void __nfs4_read_done_cb(struct nfs_read_data *data)
3192 {
3193         nfs_invalidate_atime(data->inode);
3194 }
3195
3196 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3197 {
3198         struct nfs_server *server = NFS_SERVER(data->inode);
3199
3200         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3201                 rpc_restart_call_prepare(task);
3202                 return -EAGAIN;
3203         }
3204
3205         __nfs4_read_done_cb(data);
3206         if (task->tk_status > 0)
3207                 renew_lease(server, data->timestamp);
3208         return 0;
3209 }
3210
3211 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3212 {
3213
3214         dprintk("--> %s\n", __func__);
3215
3216         if (!nfs4_sequence_done(task, &data->res.seq_res))
3217                 return -EAGAIN;
3218
3219         return data->read_done_cb ? data->read_done_cb(task, data) :
3220                                     nfs4_read_done_cb(task, data);
3221 }
3222
3223 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3224 {
3225         data->timestamp   = jiffies;
3226         data->read_done_cb = nfs4_read_done_cb;
3227         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3228 }
3229
3230 /* Reset the the nfs_read_data to send the read to the MDS. */
3231 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3232 {
3233         dprintk("%s Reset task for i/o through\n", __func__);
3234         put_lseg(data->lseg);
3235         data->lseg = NULL;
3236         /* offsets will differ in the dense stripe case */
3237         data->args.offset = data->mds_offset;
3238         data->ds_clp = NULL;
3239         data->args.fh     = NFS_FH(data->inode);
3240         data->read_done_cb = nfs4_read_done_cb;
3241         task->tk_ops = data->mds_ops;
3242         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3243 }
3244 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3245
3246 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3247 {
3248         struct inode *inode = data->inode;
3249         
3250         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3251                 rpc_restart_call_prepare(task);
3252                 return -EAGAIN;
3253         }
3254         if (task->tk_status >= 0) {
3255                 renew_lease(NFS_SERVER(inode), data->timestamp);
3256                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3257         }
3258         return 0;
3259 }
3260
3261 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3262 {
3263         if (!nfs4_sequence_done(task, &data->res.seq_res))
3264                 return -EAGAIN;
3265         return data->write_done_cb ? data->write_done_cb(task, data) :
3266                 nfs4_write_done_cb(task, data);
3267 }
3268
3269 /* Reset the the nfs_write_data to send the write to the MDS. */
3270 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3271 {
3272         dprintk("%s Reset task for i/o through\n", __func__);
3273         put_lseg(data->lseg);
3274         data->lseg          = NULL;
3275         data->ds_clp        = NULL;
3276         data->write_done_cb = nfs4_write_done_cb;
3277         data->args.fh       = NFS_FH(data->inode);
3278         data->args.bitmask  = data->res.server->cache_consistency_bitmask;
3279         data->args.offset   = data->mds_offset;
3280         data->res.fattr     = &data->fattr;
3281         task->tk_ops        = data->mds_ops;
3282         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3283 }
3284 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3285
3286 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3287 {
3288         struct nfs_server *server = NFS_SERVER(data->inode);
3289
3290         if (data->lseg) {
3291                 data->args.bitmask = NULL;
3292                 data->res.fattr = NULL;
3293         } else
3294                 data->args.bitmask = server->cache_consistency_bitmask;
3295         if (!data->write_done_cb)
3296                 data->write_done_cb = nfs4_write_done_cb;
3297         data->res.server = server;
3298         data->timestamp   = jiffies;
3299
3300         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3301 }
3302
3303 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3304 {
3305         struct inode *inode = data->inode;
3306
3307         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3308                 rpc_restart_call_prepare(task);
3309                 return -EAGAIN;
3310         }
3311         nfs_refresh_inode(inode, data->res.fattr);
3312         return 0;
3313 }
3314
3315 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3316 {
3317         if (!nfs4_sequence_done(task, &data->res.seq_res))
3318                 return -EAGAIN;
3319         return data->write_done_cb(task, data);
3320 }
3321
3322 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3323 {
3324         struct nfs_server *server = NFS_SERVER(data->inode);
3325
3326         if (data->lseg) {
3327                 data->args.bitmask = NULL;
3328                 data->res.fattr = NULL;
3329         } else
3330                 data->args.bitmask = server->cache_consistency_bitmask;
3331         if (!data->write_done_cb)
3332                 data->write_done_cb = nfs4_commit_done_cb;
3333         data->res.server = server;
3334         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3335 }
3336
3337 struct nfs4_renewdata {
3338         struct nfs_client       *client;
3339         unsigned long           timestamp;
3340 };
3341
3342 /*
3343  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3344  * standalone procedure for queueing an asynchronous RENEW.
3345  */
3346 static void nfs4_renew_release(void *calldata)
3347 {
3348         struct nfs4_renewdata *data = calldata;
3349         struct nfs_client *clp = data->client;
3350
3351         if (atomic_read(&clp->cl_count) > 1)
3352                 nfs4_schedule_state_renewal(clp);
3353         nfs_put_client(clp);
3354         kfree(data);
3355 }
3356
3357 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3358 {
3359         struct nfs4_renewdata *data = calldata;
3360         struct nfs_client *clp = data->client;
3361         unsigned long timestamp = data->timestamp;
3362
3363         if (task->tk_status < 0) {
3364                 /* Unless we're shutting down, schedule state recovery! */
3365                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3366                         return;
3367                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3368                         nfs4_schedule_lease_recovery(clp);
3369                         return;
3370                 }
3371                 nfs4_schedule_path_down_recovery(clp);
3372         }
3373         do_renew_lease(clp, timestamp);
3374 }
3375
3376 static const struct rpc_call_ops nfs4_renew_ops = {
3377         .rpc_call_done = nfs4_renew_done,
3378         .rpc_release = nfs4_renew_release,
3379 };
3380
3381 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3382 {
3383         struct rpc_message msg = {
3384                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3385                 .rpc_argp       = clp,
3386                 .rpc_cred       = cred,
3387         };
3388         struct nfs4_renewdata *data;
3389
3390         if (renew_flags == 0)
3391                 return 0;
3392         if (!atomic_inc_not_zero(&clp->cl_count))
3393                 return -EIO;
3394         data = kmalloc(sizeof(*data), GFP_NOFS);
3395         if (data == NULL)
3396                 return -ENOMEM;
3397         data->client = clp;
3398         data->timestamp = jiffies;
3399         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3400                         &nfs4_renew_ops, data);
3401 }
3402
3403 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3404 {
3405         struct rpc_message msg = {
3406                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3407                 .rpc_argp       = clp,
3408                 .rpc_cred       = cred,
3409         };
3410         unsigned long now = jiffies;
3411         int status;
3412
3413         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3414         if (status < 0)
3415                 return status;
3416         do_renew_lease(clp, now);
3417         return 0;
3418 }
3419
3420 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3421 {
3422         return (server->caps & NFS_CAP_ACLS)
3423                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3424                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3425 }
3426
3427 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3428  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3429  * the stack.
3430  */
3431 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3432
3433 static void buf_to_pages(const void *buf, size_t buflen,
3434                 struct page **pages, unsigned int *pgbase)
3435 {
3436         const void *p = buf;
3437
3438         *pgbase = offset_in_page(buf);
3439         p -= *pgbase;
3440         while (p < buf + buflen) {
3441                 *(pages++) = virt_to_page(p);
3442                 p += PAGE_CACHE_SIZE;
3443         }
3444 }
3445
3446 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3447                 struct page **pages, unsigned int *pgbase)
3448 {
3449         struct page *newpage, **spages;
3450         int rc = 0;
3451         size_t len;
3452         spages = pages;
3453
3454         do {
3455                 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3456                 newpage = alloc_page(GFP_KERNEL);
3457
3458                 if (newpage == NULL)
3459                         goto unwind;
3460                 memcpy(page_address(newpage), buf, len);
3461                 buf += len;
3462                 buflen -= len;
3463                 *pages++ = newpage;
3464                 rc++;
3465         } while (buflen != 0);
3466
3467         return rc;
3468
3469 unwind:
3470         for(; rc > 0; rc--)
3471                 __free_page(spages[rc-1]);
3472         return -ENOMEM;
3473 }
3474
3475 struct nfs4_cached_acl {
3476         int cached;
3477         size_t len;
3478         char data[0];
3479 };
3480
3481 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3482 {
3483         struct nfs_inode *nfsi = NFS_I(inode);
3484
3485         spin_lock(&inode->i_lock);
3486         kfree(nfsi->nfs4_acl);
3487         nfsi->nfs4_acl = acl;
3488         spin_unlock(&inode->i_lock);
3489 }
3490
3491 static void nfs4_zap_acl_attr(struct inode *inode)
3492 {
3493         nfs4_set_cached_acl(inode, NULL);
3494 }
3495
3496 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3497 {
3498         struct nfs_inode *nfsi = NFS_I(inode);
3499         struct nfs4_cached_acl *acl;
3500         int ret = -ENOENT;
3501
3502         spin_lock(&inode->i_lock);
3503         acl = nfsi->nfs4_acl;
3504         if (acl == NULL)
3505                 goto out;
3506         if (buf == NULL) /* user is just asking for length */
3507                 goto out_len;
3508         if (acl->cached == 0)
3509                 goto out;
3510         ret = -ERANGE; /* see getxattr(2) man page */
3511         if (acl->len > buflen)
3512                 goto out;
3513         memcpy(buf, acl->data, acl->len);
3514 out_len:
3515         ret = acl->len;
3516 out:
3517         spin_unlock(&inode->i_lock);
3518         return ret;
3519 }
3520
3521 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3522 {
3523         struct nfs4_cached_acl *acl;
3524
3525         if (buf && acl_len <= PAGE_SIZE) {
3526                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3527                 if (acl == NULL)
3528                         goto out;
3529                 acl->cached = 1;
3530                 memcpy(acl->data, buf, acl_len);
3531         } else {
3532                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3533                 if (acl == NULL)
3534                         goto out;
3535                 acl->cached = 0;
3536         }
3537         acl->len = acl_len;
3538 out:
3539         nfs4_set_cached_acl(inode, acl);
3540 }
3541
3542 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3543 {
3544         struct page *pages[NFS4ACL_MAXPAGES];
3545         struct nfs_getaclargs args = {
3546                 .fh = NFS_FH(inode),
3547                 .acl_pages = pages,
3548                 .acl_len = buflen,
3549         };
3550         struct nfs_getaclres res = {
3551                 .acl_len = buflen,
3552         };
3553         void *resp_buf;
3554         struct rpc_message msg = {
3555                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3556                 .rpc_argp = &args,
3557                 .rpc_resp = &res,
3558         };
3559         struct page *localpage = NULL;
3560         int ret;
3561
3562         if (buflen < PAGE_SIZE) {
3563                 /* As long as we're doing a round trip to the server anyway,
3564                  * let's be prepared for a page of acl data. */
3565                 localpage = alloc_page(GFP_KERNEL);
3566                 resp_buf = page_address(localpage);
3567                 if (localpage == NULL)
3568                         return -ENOMEM;
3569                 args.acl_pages[0] = localpage;
3570                 args.acl_pgbase = 0;
3571                 args.acl_len = PAGE_SIZE;
3572         } else {
3573                 resp_buf = buf;
3574                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3575         }
3576         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3577         if (ret)
3578                 goto out_free;
3579         if (res.acl_len > args.acl_len)
3580                 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3581         else
3582                 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3583         if (buf) {
3584                 ret = -ERANGE;
3585                 if (res.acl_len > buflen)
3586                         goto out_free;
3587                 if (localpage)
3588                         memcpy(buf, resp_buf, res.acl_len);
3589         }
3590         ret = res.acl_len;
3591 out_free:
3592         if (localpage)
3593                 __free_page(localpage);
3594         return ret;
3595 }
3596
3597 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3598 {
3599         struct nfs4_exception exception = { };
3600         ssize_t ret;
3601         do {
3602                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3603                 if (ret >= 0)
3604                         break;
3605                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3606         } while (exception.retry);
3607         return ret;
3608 }
3609
3610 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3611 {
3612         struct nfs_server *server = NFS_SERVER(inode);
3613         int ret;
3614
3615         if (!nfs4_server_supports_acls(server))
3616                 return -EOPNOTSUPP;
3617         ret = nfs_revalidate_inode(server, inode);
3618         if (ret < 0)
3619                 return ret;
3620         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3621                 nfs_zap_acl_cache(inode);
3622         ret = nfs4_read_cached_acl(inode, buf, buflen);
3623         if (ret != -ENOENT)
3624                 return ret;
3625         return nfs4_get_acl_uncached(inode, buf, buflen);
3626 }
3627
3628 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3629 {
3630         struct nfs_server *server = NFS_SERVER(inode);
3631         struct page *pages[NFS4ACL_MAXPAGES];
3632         struct nfs_setaclargs arg = {
3633                 .fh             = NFS_FH(inode),
3634                 .acl_pages      = pages,
3635                 .acl_len        = buflen,
3636         };
3637         struct nfs_setaclres res;
3638         struct rpc_message msg = {
3639                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3640                 .rpc_argp       = &arg,
3641                 .rpc_resp       = &res,
3642         };
3643         int ret, i;
3644
3645         if (!nfs4_server_supports_acls(server))
3646                 return -EOPNOTSUPP;
3647         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3648         if (i < 0)
3649                 return i;
3650         nfs_inode_return_delegation(inode);
3651         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3652
3653         /*
3654          * Free each page after tx, so the only ref left is
3655          * held by the network stack
3656          */
3657         for (; i > 0; i--)
3658                 put_page(pages[i-1]);
3659
3660         /*
3661          * Acl update can result in inode attribute update.
3662          * so mark the attribute cache invalid.
3663          */
3664         spin_lock(&inode->i_lock);
3665         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3666         spin_unlock(&inode->i_lock);
3667         nfs_access_zap_cache(inode);
3668         nfs_zap_acl_cache(inode);
3669         return ret;
3670 }
3671
3672 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3673 {
3674         struct nfs4_exception exception = { };
3675         int err;
3676         do {
3677                 err = nfs4_handle_exception(NFS_SERVER(inode),
3678                                 __nfs4_proc_set_acl(inode, buf, buflen),
3679                                 &exception);
3680         } while (exception.retry);
3681         return err;
3682 }
3683
3684 static int
3685 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3686 {
3687         struct nfs_client *clp = server->nfs_client;
3688
3689         if (task->tk_status >= 0)
3690                 return 0;
3691         switch(task->tk_status) {
3692                 case -NFS4ERR_ADMIN_REVOKED:
3693                 case -NFS4ERR_BAD_STATEID:
3694                 case -NFS4ERR_OPENMODE:
3695                         if (state == NULL)
3696                                 break;
3697                         nfs4_schedule_stateid_recovery(server, state);
3698                         goto wait_on_recovery;
3699                 case -NFS4ERR_EXPIRED:
3700                         if (state != NULL)
3701                                 nfs4_schedule_stateid_recovery(server, state);
3702                 case -NFS4ERR_STALE_STATEID:
3703                 case -NFS4ERR_STALE_CLIENTID:
3704                         nfs4_schedule_lease_recovery(clp);
3705                         goto wait_on_recovery;
3706 #if defined(CONFIG_NFS_V4_1)
3707                 case -NFS4ERR_BADSESSION:
3708                 case -NFS4ERR_BADSLOT:
3709                 case -NFS4ERR_BAD_HIGH_SLOT:
3710                 case -NFS4ERR_DEADSESSION:
3711                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3712                 case -NFS4ERR_SEQ_FALSE_RETRY:
3713                 case -NFS4ERR_SEQ_MISORDERED:
3714                         dprintk("%s ERROR %d, Reset session\n", __func__,
3715                                 task->tk_status);
3716                         nfs4_schedule_session_recovery(clp->cl_session);
3717                         task->tk_status = 0;
3718                         return -EAGAIN;
3719 #endif /* CONFIG_NFS_V4_1 */
3720                 case -NFS4ERR_DELAY:
3721                         nfs_inc_server_stats(server, NFSIOS_DELAY);
3722                 case -NFS4ERR_GRACE:
3723                 case -EKEYEXPIRED:
3724                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
3725                         task->tk_status = 0;
3726                         return -EAGAIN;
3727                 case -NFS4ERR_RETRY_UNCACHED_REP:
3728                 case -NFS4ERR_OLD_STATEID:
3729                         task->tk_status = 0;
3730                         return -EAGAIN;
3731         }
3732         task->tk_status = nfs4_map_errors(task->tk_status);
3733         return 0;
3734 wait_on_recovery:
3735         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3736         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3737                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3738         task->tk_status = 0;
3739         return -EAGAIN;
3740 }
3741
3742 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3743                 unsigned short port, struct rpc_cred *cred,
3744                 struct nfs4_setclientid_res *res)
3745 {
3746         nfs4_verifier sc_verifier;
3747         struct nfs4_setclientid setclientid = {
3748                 .sc_verifier = &sc_verifier,
3749                 .sc_prog = program,
3750                 .sc_cb_ident = clp->cl_cb_ident,
3751         };
3752         struct rpc_message msg = {
3753                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3754                 .rpc_argp = &setclientid,
3755                 .rpc_resp = res,
3756                 .rpc_cred = cred,
3757         };
3758         __be32 *p;
3759         int loop = 0;
3760         int status;
3761
3762         p = (__be32*)sc_verifier.data;
3763         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3764         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3765
3766         for(;;) {
3767                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3768                                 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3769                                 clp->cl_ipaddr,
3770                                 rpc_peeraddr2str(clp->cl_rpcclient,
3771                                                         RPC_DISPLAY_ADDR),
3772                                 rpc_peeraddr2str(clp->cl_rpcclient,
3773                                                         RPC_DISPLAY_PROTO),
3774                                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3775                                 clp->cl_id_uniquifier);
3776                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3777                                 sizeof(setclientid.sc_netid),
3778                                 rpc_peeraddr2str(clp->cl_rpcclient,
3779                                                         RPC_DISPLAY_NETID));
3780                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3781                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3782                                 clp->cl_ipaddr, port >> 8, port & 255);
3783
3784                 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3785                 if (status != -NFS4ERR_CLID_INUSE)
3786                         break;
3787                 if (loop != 0) {
3788                         ++clp->cl_id_uniquifier;
3789                         break;
3790                 }
3791                 ++loop;
3792                 ssleep(clp->cl_lease_time / HZ + 1);
3793         }
3794         return status;
3795 }
3796
3797 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3798                 struct nfs4_setclientid_res *arg,
3799                 struct rpc_cred *cred)
3800 {
3801         struct nfs_fsinfo fsinfo;
3802         struct rpc_message msg = {
3803                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3804                 .rpc_argp = arg,
3805                 .rpc_resp = &fsinfo,
3806                 .rpc_cred = cred,
3807         };
3808         unsigned long now;
3809         int status;
3810
3811         now = jiffies;
3812         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3813         if (status == 0) {
3814                 spin_lock(&clp->cl_lock);
3815                 clp->cl_lease_time = fsinfo.lease_time * HZ;
3816                 clp->cl_last_renewal = now;
3817                 spin_unlock(&clp->cl_lock);
3818         }
3819         return status;
3820 }
3821
3822 struct nfs4_delegreturndata {
3823         struct nfs4_delegreturnargs args;
3824         struct nfs4_delegreturnres res;
3825         struct nfs_fh fh;
3826         nfs4_stateid stateid;
3827         unsigned long timestamp;
3828         struct nfs_fattr fattr;
3829         int rpc_status;
3830 };
3831
3832 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3833 {
3834         struct nfs4_delegreturndata *data = calldata;
3835
3836         if (!nfs4_sequence_done(task, &data->res.seq_res))
3837                 return;
3838
3839         switch (task->tk_status) {
3840         case -NFS4ERR_STALE_STATEID:
3841         case -NFS4ERR_EXPIRED:
3842         case 0:
3843                 renew_lease(data->res.server, data->timestamp);
3844                 break;
3845         default:
3846                 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3847                                 -EAGAIN) {
3848                         rpc_restart_call_prepare(task);
3849                         return;
3850                 }
3851         }
3852         data->rpc_status = task->tk_status;
3853 }
3854
3855 static void nfs4_delegreturn_release(void *calldata)
3856 {
3857         kfree(calldata);
3858 }
3859
3860 #if defined(CONFIG_NFS_V4_1)
3861 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3862 {
3863         struct nfs4_delegreturndata *d_data;
3864
3865         d_data = (struct nfs4_delegreturndata *)data;
3866
3867         if (nfs4_setup_sequence(d_data->res.server,
3868                                 &d_data->args.seq_args,
3869                                 &d_data->res.seq_res, 1, task))
3870                 return;
3871         rpc_call_start(task);
3872 }
3873 #endif /* CONFIG_NFS_V4_1 */
3874
3875 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3876 #if defined(CONFIG_NFS_V4_1)
3877         .rpc_call_prepare = nfs4_delegreturn_prepare,
3878 #endif /* CONFIG_NFS_V4_1 */
3879         .rpc_call_done = nfs4_delegreturn_done,
3880         .rpc_release = nfs4_delegreturn_release,
3881 };
3882
3883 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3884 {
3885         struct nfs4_delegreturndata *data;
3886         struct nfs_server *server = NFS_SERVER(inode);
3887         struct rpc_task *task;
3888         struct rpc_message msg = {
3889                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3890                 .rpc_cred = cred,
3891         };
3892         struct rpc_task_setup task_setup_data = {
3893                 .rpc_client = server->client,
3894                 .rpc_message = &msg,
3895                 .callback_ops = &nfs4_delegreturn_ops,
3896                 .flags = RPC_TASK_ASYNC,
3897         };
3898         int status = 0;
3899
3900         data = kzalloc(sizeof(*data), GFP_NOFS);
3901         if (data == NULL)
3902                 return -ENOMEM;
3903         data->args.fhandle = &data->fh;
3904         data->args.stateid = &data->stateid;
3905         data->args.bitmask = server->attr_bitmask;
3906         nfs_copy_fh(&data->fh, NFS_FH(inode));
3907         memcpy(&data->stateid, stateid, sizeof(data->stateid));
3908         data->res.fattr = &data->fattr;
3909         data->res.server = server;
3910         nfs_fattr_init(data->res.fattr);
3911         data->timestamp = jiffies;
3912         data->rpc_status = 0;
3913
3914         task_setup_data.callback_data = data;
3915         msg.rpc_argp = &data->args;
3916         msg.rpc_resp = &data->res;
3917         task = rpc_run_task(&task_setup_data);
3918         if (IS_ERR(task))
3919                 return PTR_ERR(task);
3920         if (!issync)
3921                 goto out;
3922         status = nfs4_wait_for_completion_rpc_task(task);
3923         if (status != 0)
3924                 goto out;
3925         status = data->rpc_status;
3926         if (status != 0)
3927                 goto out;
3928         nfs_refresh_inode(inode, &data->fattr);
3929 out:
3930         rpc_put_task(task);
3931         return status;
3932 }
3933
3934 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3935 {
3936         struct nfs_server *server = NFS_SERVER(inode);
3937         struct nfs4_exception exception = { };
3938         int err;
3939         do {
3940                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3941                 switch (err) {
3942                         case -NFS4ERR_STALE_STATEID:
3943                         case -NFS4ERR_EXPIRED:
3944                         case 0:
3945                                 return 0;
3946                 }
3947                 err = nfs4_handle_exception(server, err, &exception);
3948         } while (exception.retry);
3949         return err;
3950 }
3951
3952 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3953 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3954
3955 /* 
3956  * sleep, with exponential backoff, and retry the LOCK operation. 
3957  */
3958 static unsigned long
3959 nfs4_set_lock_task_retry(unsigned long timeout)
3960 {
3961         schedule_timeout_killable(timeout);
3962         timeout <<= 1;
3963         if (timeout > NFS4_LOCK_MAXTIMEOUT)
3964                 return NFS4_LOCK_MAXTIMEOUT;
3965         return timeout;
3966 }
3967
3968 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3969 {
3970         struct inode *inode = state->inode;
3971         struct nfs_server *server = NFS_SERVER(inode);
3972         struct nfs_client *clp = server->nfs_client;
3973         struct nfs_lockt_args arg = {
3974                 .fh = NFS_FH(inode),
3975                 .fl = request,
3976         };
3977         struct nfs_lockt_res res = {
3978                 .denied = request,
3979         };
3980         struct rpc_message msg = {
3981                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3982                 .rpc_argp       = &arg,
3983                 .rpc_resp       = &res,
3984                 .rpc_cred       = state->owner->so_cred,
3985         };
3986         struct nfs4_lock_state *lsp;
3987         int status;
3988
3989         arg.lock_owner.clientid = clp->cl_clientid;
3990         status = nfs4_set_lock_state(state, request);
3991         if (status != 0)
3992                 goto out;
3993         lsp = request->fl_u.nfs4_fl.owner;
3994         arg.lock_owner.id = lsp->ls_id.id;
3995         arg.lock_owner.s_dev = server->s_dev;
3996         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3997         switch (status) {
3998                 case 0:
3999                         request->fl_type = F_UNLCK;
4000                         break;
4001                 case -NFS4ERR_DENIED:
4002                         status = 0;
4003         }
4004         request->fl_ops->fl_release_private(request);
4005 out:
4006         return status;
4007 }
4008
4009 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4010 {
4011         struct nfs4_exception exception = { };
4012         int err;
4013
4014         do {
4015                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4016                                 _nfs4_proc_getlk(state, cmd, request),
4017                                 &exception);
4018         } while (exception.retry);
4019         return err;
4020 }
4021
4022 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4023 {
4024         int res = 0;
4025         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4026                 case FL_POSIX:
4027                         res = posix_lock_file_wait(file, fl);
4028                         break;
4029                 case FL_FLOCK:
4030                         res = flock_lock_file_wait(file, fl);
4031                         break;
4032                 default:
4033                         BUG();
4034         }
4035         return res;
4036 }
4037
4038 struct nfs4_unlockdata {
4039         struct nfs_locku_args arg;
4040         struct nfs_locku_res res;
4041         struct nfs4_lock_state *lsp;
4042         struct nfs_open_context *ctx;
4043         struct file_lock fl;
4044         const struct nfs_server *server;
4045         unsigned long timestamp;
4046 };
4047
4048 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4049                 struct nfs_open_context *ctx,
4050                 struct nfs4_lock_state *lsp,
4051                 struct nfs_seqid *seqid)
4052 {
4053         struct nfs4_unlockdata *p;
4054         struct inode *inode = lsp->ls_state->inode;
4055
4056         p = kzalloc(sizeof(*p), GFP_NOFS);
4057         if (p == NULL)
4058                 return NULL;
4059         p->arg.fh = NFS_FH(inode);
4060         p->arg.fl = &p->fl;
4061         p->arg.seqid = seqid;
4062         p->res.seqid = seqid;
4063         p->arg.stateid = &lsp->ls_stateid;
4064         p->lsp = lsp;
4065         atomic_inc(&lsp->ls_count);
4066         /* Ensure we don't close file until we're done freeing locks! */
4067         p->ctx = get_nfs_open_context(ctx);
4068         memcpy(&p->fl, fl, sizeof(p->fl));
4069         p->server = NFS_SERVER(inode);
4070         return p;
4071 }
4072
4073 static void nfs4_locku_release_calldata(void *data)
4074 {
4075         struct nfs4_unlockdata *calldata = data;
4076         nfs_free_seqid(calldata->arg.seqid);
4077         nfs4_put_lock_state(calldata->lsp);
4078         put_nfs_open_context(calldata->ctx);
4079         kfree(calldata);
4080 }
4081
4082 static void nfs4_locku_done(struct rpc_task *task, void *data)
4083 {
4084         struct nfs4_unlockdata *calldata = data;
4085
4086         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4087                 return;
4088         switch (task->tk_status) {
4089                 case 0:
4090                         memcpy(calldata->lsp->ls_stateid.data,
4091                                         calldata->res.stateid.data,
4092                                         sizeof(calldata->lsp->ls_stateid.data));
4093                         renew_lease(calldata->server, calldata->timestamp);
4094                         break;
4095                 case -NFS4ERR_BAD_STATEID:
4096                 case -NFS4ERR_OLD_STATEID:
4097                 case -NFS4ERR_STALE_STATEID:
4098                 case -NFS4ERR_EXPIRED:
4099                         break;
4100                 default:
4101                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4102                                 rpc_restart_call_prepare(task);
4103         }
4104 }
4105
4106 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4107 {
4108         struct nfs4_unlockdata *calldata = data;
4109
4110         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4111                 return;
4112         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4113                 /* Note: exit _without_ running nfs4_locku_done */
4114                 task->tk_action = NULL;
4115                 return;
4116         }
4117         calldata->timestamp = jiffies;
4118         if (nfs4_setup_sequence(calldata->server,
4119                                 &calldata->arg.seq_args,
4120                                 &calldata->res.seq_res, 1, task))
4121                 return;
4122         rpc_call_start(task);
4123 }
4124
4125 static const struct rpc_call_ops nfs4_locku_ops = {
4126         .rpc_call_prepare = nfs4_locku_prepare,
4127         .rpc_call_done = nfs4_locku_done,
4128         .rpc_release = nfs4_locku_release_calldata,
4129 };
4130
4131 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4132                 struct nfs_open_context *ctx,
4133                 struct nfs4_lock_state *lsp,
4134                 struct nfs_seqid *seqid)
4135 {
4136         struct nfs4_unlockdata *data;
4137         struct rpc_message msg = {
4138                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4139                 .rpc_cred = ctx->cred,
4140         };
4141         struct rpc_task_setup task_setup_data = {
4142                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4143                 .rpc_message = &msg,
4144                 .callback_ops = &nfs4_locku_ops,
4145                 .workqueue = nfsiod_workqueue,
4146                 .flags = RPC_TASK_ASYNC,
4147         };
4148
4149         /* Ensure this is an unlock - when canceling a lock, the
4150          * canceled lock is passed in, and it won't be an unlock.
4151          */
4152         fl->fl_type = F_UNLCK;
4153
4154         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4155         if (data == NULL) {
4156                 nfs_free_seqid(seqid);
4157                 return ERR_PTR(-ENOMEM);
4158         }
4159
4160         msg.rpc_argp = &data->arg;
4161         msg.rpc_resp = &data->res;
4162         task_setup_data.callback_data = data;
4163         return rpc_run_task(&task_setup_data);
4164 }
4165
4166 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4167 {
4168         struct nfs_inode *nfsi = NFS_I(state->inode);
4169         struct nfs_seqid *seqid;
4170         struct nfs4_lock_state *lsp;
4171         struct rpc_task *task;
4172         int status = 0;
4173         unsigned char fl_flags = request->fl_flags;
4174
4175         status = nfs4_set_lock_state(state, request);
4176         /* Unlock _before_ we do the RPC call */
4177         request->fl_flags |= FL_EXISTS;
4178         down_read(&nfsi->rwsem);
4179         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4180                 up_read(&nfsi->rwsem);
4181                 goto out;
4182         }
4183         up_read(&nfsi->rwsem);
4184         if (status != 0)
4185                 goto out;
4186         /* Is this a delegated lock? */
4187         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4188                 goto out;
4189         lsp = request->fl_u.nfs4_fl.owner;
4190         seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4191         status = -ENOMEM;
4192         if (seqid == NULL)
4193                 goto out;
4194         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4195         status = PTR_ERR(task);
4196         if (IS_ERR(task))
4197                 goto out;
4198         status = nfs4_wait_for_completion_rpc_task(task);
4199         rpc_put_task(task);
4200 out:
4201         request->fl_flags = fl_flags;
4202         return status;
4203 }
4204
4205 struct nfs4_lockdata {
4206         struct nfs_lock_args arg;
4207         struct nfs_lock_res res;
4208         struct nfs4_lock_state *lsp;
4209         struct nfs_open_context *ctx;
4210         struct file_lock fl;
4211         unsigned long timestamp;
4212         int rpc_status;
4213         int cancelled;
4214         struct nfs_server *server;
4215 };
4216
4217 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4218                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4219                 gfp_t gfp_mask)
4220 {
4221         struct nfs4_lockdata *p;
4222         struct inode *inode = lsp->ls_state->inode;
4223         struct nfs_server *server = NFS_SERVER(inode);
4224
4225         p = kzalloc(sizeof(*p), gfp_mask);
4226         if (p == NULL)
4227                 return NULL;
4228
4229         p->arg.fh = NFS_FH(inode);
4230         p->arg.fl = &p->fl;
4231         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4232         if (p->arg.open_seqid == NULL)
4233                 goto out_free;
4234         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4235         if (p->arg.lock_seqid == NULL)
4236                 goto out_free_seqid;
4237         p->arg.lock_stateid = &lsp->ls_stateid;
4238         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4239         p->arg.lock_owner.id = lsp->ls_id.id;
4240         p->arg.lock_owner.s_dev = server->s_dev;
4241         p->res.lock_seqid = p->arg.lock_seqid;
4242         p->lsp = lsp;
4243         p->server = server;
4244         atomic_inc(&lsp->ls_count);
4245         p->ctx = get_nfs_open_context(ctx);
4246         memcpy(&p->fl, fl, sizeof(p->fl));
4247         return p;
4248 out_free_seqid:
4249         nfs_free_seqid(p->arg.open_seqid);
4250 out_free:
4251         kfree(p);
4252         return NULL;
4253 }
4254
4255 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4256 {
4257         struct nfs4_lockdata *data = calldata;
4258         struct nfs4_state *state = data->lsp->ls_state;
4259
4260         dprintk("%s: begin!\n", __func__);
4261         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4262                 return;
4263         /* Do we need to do an open_to_lock_owner? */
4264         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4265                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4266                         return;
4267                 data->arg.open_stateid = &state->stateid;
4268                 data->arg.new_lock_owner = 1;
4269                 data->res.open_seqid = data->arg.open_seqid;
4270         } else
4271                 data->arg.new_lock_owner = 0;
4272         data->timestamp = jiffies;
4273         if (nfs4_setup_sequence(data->server,
4274                                 &data->arg.seq_args,
4275                                 &data->res.seq_res, 1, task))
4276                 return;
4277         rpc_call_start(task);
4278         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4279 }
4280
4281 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4282 {
4283         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4284         nfs4_lock_prepare(task, calldata);
4285 }
4286
4287 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4288 {
4289         struct nfs4_lockdata *data = calldata;
4290
4291         dprintk("%s: begin!\n", __func__);
4292
4293         if (!nfs4_sequence_done(task, &data->res.seq_res))
4294                 return;
4295
4296         data->rpc_status = task->tk_status;
4297         if (data->arg.new_lock_owner != 0) {
4298                 if (data->rpc_status == 0)
4299                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4300                 else
4301                         goto out;
4302         }
4303         if (data->rpc_status == 0) {
4304                 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4305                                         sizeof(data->lsp->ls_stateid.data));
4306                 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4307                 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4308         }
4309 out:
4310         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4311 }
4312
4313 static void nfs4_lock_release(void *calldata)
4314 {
4315         struct nfs4_lockdata *data = calldata;
4316
4317         dprintk("%s: begin!\n", __func__);
4318         nfs_free_seqid(data->arg.open_seqid);
4319         if (data->cancelled != 0) {
4320                 struct rpc_task *task;
4321                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4322                                 data->arg.lock_seqid);
4323                 if (!IS_ERR(task))
4324                         rpc_put_task_async(task);
4325                 dprintk("%s: cancelling lock!\n", __func__);
4326         } else
4327                 nfs_free_seqid(data->arg.lock_seqid);
4328         nfs4_put_lock_state(data->lsp);
4329         put_nfs_open_context(data->ctx);
4330         kfree(data);
4331         dprintk("%s: done!\n", __func__);
4332 }
4333
4334 static const struct rpc_call_ops nfs4_lock_ops = {
4335         .rpc_call_prepare = nfs4_lock_prepare,
4336         .rpc_call_done = nfs4_lock_done,
4337         .rpc_release = nfs4_lock_release,
4338 };
4339
4340 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4341         .rpc_call_prepare = nfs4_recover_lock_prepare,
4342         .rpc_call_done = nfs4_lock_done,
4343         .rpc_release = nfs4_lock_release,
4344 };
4345
4346 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4347 {
4348         switch (error) {
4349         case -NFS4ERR_ADMIN_REVOKED:
4350         case -NFS4ERR_BAD_STATEID:
4351                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4352                 if (new_lock_owner != 0 ||
4353                    (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4354                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4355                 break;
4356         case -NFS4ERR_STALE_STATEID:
4357                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4358         case -NFS4ERR_EXPIRED:
4359                 nfs4_schedule_lease_recovery(server->nfs_client);
4360         };
4361 }
4362
4363 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4364 {
4365         struct nfs4_lockdata *data;
4366         struct rpc_task *task;
4367         struct rpc_message msg = {
4368                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4369                 .rpc_cred = state->owner->so_cred,
4370         };
4371         struct rpc_task_setup task_setup_data = {
4372                 .rpc_client = NFS_CLIENT(state->inode),
4373                 .rpc_message = &msg,
4374                 .callback_ops = &nfs4_lock_ops,
4375                 .workqueue = nfsiod_workqueue,
4376                 .flags = RPC_TASK_ASYNC,
4377         };
4378         int ret;
4379
4380         dprintk("%s: begin!\n", __func__);
4381         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4382                         fl->fl_u.nfs4_fl.owner,
4383                         recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4384         if (data == NULL)
4385                 return -ENOMEM;
4386         if (IS_SETLKW(cmd))
4387                 data->arg.block = 1;
4388         if (recovery_type > NFS_LOCK_NEW) {
4389                 if (recovery_type == NFS_LOCK_RECLAIM)
4390                         data->arg.reclaim = NFS_LOCK_RECLAIM;
4391                 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4392         }
4393         msg.rpc_argp = &data->arg;
4394         msg.rpc_resp = &data->res;
4395         task_setup_data.callback_data = data;
4396         task = rpc_run_task(&task_setup_data);
4397         if (IS_ERR(task))
4398                 return PTR_ERR(task);
4399         ret = nfs4_wait_for_completion_rpc_task(task);
4400         if (ret == 0) {
4401                 ret = data->rpc_status;
4402                 if (ret)
4403                         nfs4_handle_setlk_error(data->server, data->lsp,
4404                                         data->arg.new_lock_owner, ret);
4405         } else
4406                 data->cancelled = 1;
4407         rpc_put_task(task);
4408         dprintk("%s: done, ret = %d!\n", __func__, ret);
4409         return ret;
4410 }
4411
4412 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4413 {
4414         struct nfs_server *server = NFS_SERVER(state->inode);
4415         struct nfs4_exception exception = { };
4416         int err;
4417
4418         do {
4419                 /* Cache the lock if possible... */
4420                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4421                         return 0;
4422                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4423                 if (err != -NFS4ERR_DELAY)
4424                         break;
4425                 nfs4_handle_exception(server, err, &exception);
4426         } while (exception.retry);
4427         return err;
4428 }
4429
4430 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4431 {
4432         struct nfs_server *server = NFS_SERVER(state->inode);
4433         struct nfs4_exception exception = { };
4434         int err;
4435
4436         err = nfs4_set_lock_state(state, request);
4437         if (err != 0)
4438                 return err;
4439         do {
4440                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4441                         return 0;
4442                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4443                 switch (err) {
4444                 default:
4445                         goto out;
4446                 case -NFS4ERR_GRACE:
4447                 case -NFS4ERR_DELAY:
4448                         nfs4_handle_exception(server, err, &exception);
4449                         err = 0;
4450                 }
4451         } while (exception.retry);
4452 out:
4453         return err;
4454 }
4455
4456 #if defined(CONFIG_NFS_V4_1)
4457 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4458 {
4459         int status;
4460         struct nfs_server *server = NFS_SERVER(state->inode);
4461
4462         status = nfs41_test_stateid(server, state);
4463         if (status == NFS_OK)
4464                 return 0;
4465         nfs41_free_stateid(server, state);
4466         return nfs4_lock_expired(state, request);
4467 }
4468 #endif
4469
4470 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4471 {
4472         struct nfs_inode *nfsi = NFS_I(state->inode);
4473         unsigned char fl_flags = request->fl_flags;
4474         int status = -ENOLCK;
4475
4476         if ((fl_flags & FL_POSIX) &&
4477                         !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4478                 goto out;
4479         /* Is this a delegated open? */
4480         status = nfs4_set_lock_state(state, request);
4481         if (status != 0)
4482                 goto out;
4483         request->fl_flags |= FL_ACCESS;
4484         status = do_vfs_lock(request->fl_file, request);
4485         if (status < 0)
4486                 goto out;
4487         down_read(&nfsi->rwsem);
4488         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4489                 /* Yes: cache locks! */
4490                 /* ...but avoid races with delegation recall... */
4491                 request->fl_flags = fl_flags & ~FL_SLEEP;
4492                 status = do_vfs_lock(request->fl_file, request);
4493                 goto out_unlock;
4494         }
4495         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4496         if (status != 0)
4497                 goto out_unlock;
4498         /* Note: we always want to sleep here! */
4499         request->fl_flags = fl_flags | FL_SLEEP;
4500         if (do_vfs_lock(request->fl_file, request) < 0)
4501                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4502 out_unlock:
4503         up_read(&nfsi->rwsem);
4504 out:
4505         request->fl_flags = fl_flags;
4506         return status;
4507 }
4508
4509 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4510 {
4511         struct nfs4_exception exception = { };
4512         int err;
4513
4514         do {
4515                 err = _nfs4_proc_setlk(state, cmd, request);
4516                 if (err == -NFS4ERR_DENIED)
4517                         err = -EAGAIN;
4518                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4519                                 err, &exception);
4520         } while (exception.retry);
4521         return err;
4522 }
4523
4524 static int
4525 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4526 {
4527         struct nfs_open_context *ctx;
4528         struct nfs4_state *state;
4529         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4530         int status;
4531
4532         /* verify open state */
4533         ctx = nfs_file_open_context(filp);
4534         state = ctx->state;
4535
4536         if (request->fl_start < 0 || request->fl_end < 0)
4537                 return -EINVAL;
4538
4539         if (IS_GETLK(cmd)) {
4540                 if (state != NULL)
4541                         return nfs4_proc_getlk(state, F_GETLK, request);
4542                 return 0;
4543         }
4544
4545         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4546                 return -EINVAL;
4547
4548         if (request->fl_type == F_UNLCK) {
4549                 if (state != NULL)
4550                         return nfs4_proc_unlck(state, cmd, request);
4551                 return 0;
4552         }
4553
4554         if (state == NULL)
4555                 return -ENOLCK;
4556         do {
4557                 status = nfs4_proc_setlk(state, cmd, request);
4558                 if ((status != -EAGAIN) || IS_SETLK(cmd))
4559                         break;
4560                 timeout = nfs4_set_lock_task_retry(timeout);
4561                 status = -ERESTARTSYS;
4562                 if (signalled())
4563                         break;
4564         } while(status < 0);
4565         return status;
4566 }
4567
4568 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4569 {
4570         struct nfs_server *server = NFS_SERVER(state->inode);
4571         struct nfs4_exception exception = { };
4572         int err;
4573
4574         err = nfs4_set_lock_state(state, fl);
4575         if (err != 0)
4576                 goto out;
4577         do {
4578                 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4579                 switch (err) {
4580                         default:
4581                                 printk(KERN_ERR "%s: unhandled error %d.\n",
4582                                                 __func__, err);
4583                         case 0:
4584                         case -ESTALE:
4585                                 goto out;
4586                         case -NFS4ERR_EXPIRED:
4587                                 nfs4_schedule_stateid_recovery(server, state);
4588                         case -NFS4ERR_STALE_CLIENTID:
4589                         case -NFS4ERR_STALE_STATEID:
4590                                 nfs4_schedule_lease_recovery(server->nfs_client);
4591                                 goto out;
4592                         case -NFS4ERR_BADSESSION:
4593                         case -NFS4ERR_BADSLOT:
4594                         case -NFS4ERR_BAD_HIGH_SLOT:
4595                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4596                         case -NFS4ERR_DEADSESSION:
4597                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4598                                 goto out;
4599                         case -ERESTARTSYS:
4600                                 /*
4601                                  * The show must go on: exit, but mark the
4602                                  * stateid as needing recovery.
4603                                  */
4604                         case -NFS4ERR_ADMIN_REVOKED:
4605                         case -NFS4ERR_BAD_STATEID:
4606                         case -NFS4ERR_OPENMODE:
4607                                 nfs4_schedule_stateid_recovery(server, state);
4608                                 err = 0;
4609                                 goto out;
4610                         case -EKEYEXPIRED:
4611                                 /*
4612                                  * User RPCSEC_GSS context has expired.
4613                                  * We cannot recover this stateid now, so
4614                                  * skip it and allow recovery thread to
4615                                  * proceed.
4616                                  */
4617                                 err = 0;
4618                                 goto out;
4619                         case -ENOMEM:
4620                         case -NFS4ERR_DENIED:
4621                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4622                                 err = 0;
4623                                 goto out;
4624                         case -NFS4ERR_DELAY:
4625                                 break;
4626                 }
4627                 err = nfs4_handle_exception(server, err, &exception);
4628         } while (exception.retry);
4629 out:
4630         return err;
4631 }
4632
4633 static void nfs4_release_lockowner_release(void *calldata)
4634 {
4635         kfree(calldata);
4636 }
4637
4638 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4639         .rpc_release = nfs4_release_lockowner_release,
4640 };
4641
4642 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4643 {
4644         struct nfs_server *server = lsp->ls_state->owner->so_server;
4645         struct nfs_release_lockowner_args *args;
4646         struct rpc_message msg = {
4647                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4648         };
4649
4650         if (server->nfs_client->cl_mvops->minor_version != 0)
4651                 return;
4652         args = kmalloc(sizeof(*args), GFP_NOFS);
4653         if (!args)
4654                 return;
4655         args->lock_owner.clientid = server->nfs_client->cl_clientid;
4656         args->lock_owner.id = lsp->ls_id.id;
4657         args->lock_owner.s_dev = server->s_dev;
4658         msg.rpc_argp = args;
4659         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4660 }
4661
4662 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4663
4664 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4665                                    const void *buf, size_t buflen,
4666                                    int flags, int type)
4667 {
4668         if (strcmp(key, "") != 0)
4669                 return -EINVAL;
4670
4671         return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4672 }
4673
4674 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4675                                    void *buf, size_t buflen, int type)
4676 {
4677         if (strcmp(key, "") != 0)
4678                 return -EINVAL;
4679
4680         return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4681 }
4682
4683 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4684                                        size_t list_len, const char *name,
4685                                        size_t name_len, int type)
4686 {
4687         size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4688
4689         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4690                 return 0;
4691
4692         if (list && len <= list_len)
4693                 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4694         return len;
4695 }
4696
4697 /*
4698  * nfs_fhget will use either the mounted_on_fileid or the fileid
4699  */
4700 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4701 {
4702         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4703                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4704               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4705               (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4706                 return;
4707
4708         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4709                 NFS_ATTR_FATTR_NLINK;
4710         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4711         fattr->nlink = 2;
4712 }
4713
4714 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4715                 struct nfs4_fs_locations *fs_locations, struct page *page)
4716 {
4717         struct nfs_server *server = NFS_SERVER(dir);
4718         u32 bitmask[2] = {
4719                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4720         };
4721         struct nfs4_fs_locations_arg args = {
4722                 .dir_fh = NFS_FH(dir),
4723                 .name = name,
4724                 .page = page,
4725                 .bitmask = bitmask,
4726         };
4727         struct nfs4_fs_locations_res res = {
4728                 .fs_locations = fs_locations,
4729         };
4730         struct rpc_message msg = {
4731                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4732                 .rpc_argp = &args,
4733                 .rpc_resp = &res,
4734         };
4735         int status;
4736
4737         dprintk("%s: start\n", __func__);
4738
4739         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4740          * is not supported */
4741         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4742                 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4743         else
4744                 bitmask[0] |= FATTR4_WORD0_FILEID;
4745
4746         nfs_fattr_init(&fs_locations->fattr);
4747         fs_locations->server = server;
4748         fs_locations->nlocations = 0;
4749         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4750         dprintk("%s: returned status = %d\n", __func__, status);
4751         return status;
4752 }
4753
4754 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4755 {
4756         int status;
4757         struct nfs4_secinfo_arg args = {
4758                 .dir_fh = NFS_FH(dir),
4759                 .name   = name,
4760         };
4761         struct nfs4_secinfo_res res = {
4762                 .flavors     = flavors,
4763         };
4764         struct rpc_message msg = {
4765                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4766                 .rpc_argp = &args,
4767                 .rpc_resp = &res,
4768         };
4769
4770         dprintk("NFS call  secinfo %s\n", name->name);
4771         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4772         dprintk("NFS reply  secinfo: %d\n", status);
4773         return status;
4774 }
4775
4776 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4777 {
4778         struct nfs4_exception exception = { };
4779         int err;
4780         do {
4781                 err = nfs4_handle_exception(NFS_SERVER(dir),
4782                                 _nfs4_proc_secinfo(dir, name, flavors),
4783                                 &exception);
4784         } while (exception.retry);
4785         return err;
4786 }
4787
4788 #ifdef CONFIG_NFS_V4_1
4789 /*
4790  * Check the exchange flags returned by the server for invalid flags, having
4791  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4792  * DS flags set.
4793  */
4794 static int nfs4_check_cl_exchange_flags(u32 flags)
4795 {
4796         if (flags & ~EXCHGID4_FLAG_MASK_R)
4797                 goto out_inval;
4798         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4799             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4800                 goto out_inval;
4801         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4802                 goto out_inval;
4803         return NFS_OK;
4804 out_inval:
4805         return -NFS4ERR_INVAL;
4806 }
4807
4808 static bool
4809 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4810 {
4811         if (a->server_scope_sz == b->server_scope_sz &&
4812             memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4813                 return true;
4814
4815         return false;
4816 }
4817
4818 /*
4819  * nfs4_proc_exchange_id()
4820  *
4821  * Since the clientid has expired, all compounds using sessions
4822  * associated with the stale clientid will be returning
4823  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4824  * be in some phase of session reset.
4825  */
4826 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4827 {
4828         nfs4_verifier verifier;
4829         struct nfs41_exchange_id_args args = {
4830                 .client = clp,
4831                 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4832         };
4833         struct nfs41_exchange_id_res res = {
4834                 .client = clp,
4835         };
4836         int status;
4837         struct rpc_message msg = {
4838                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4839                 .rpc_argp = &args,
4840                 .rpc_resp = &res,
4841                 .rpc_cred = cred,
4842         };
4843         __be32 *p;
4844
4845         dprintk("--> %s\n", __func__);
4846         BUG_ON(clp == NULL);
4847
4848         p = (u32 *)verifier.data;
4849         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4850         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4851         args.verifier = &verifier;
4852
4853         args.id_len = scnprintf(args.id, sizeof(args.id),
4854                                 "%s/%s.%s/%u",
4855                                 clp->cl_ipaddr,
4856                                 init_utsname()->nodename,
4857                                 init_utsname()->domainname,
4858                                 clp->cl_rpcclient->cl_auth->au_flavor);
4859
4860         res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4861         if (unlikely(!res.server_scope))
4862                 return -ENOMEM;
4863
4864         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4865         if (!status)
4866                 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4867
4868         if (!status) {
4869                 if (clp->server_scope &&
4870                     !nfs41_same_server_scope(clp->server_scope,
4871                                              res.server_scope)) {
4872                         dprintk("%s: server_scope mismatch detected\n",
4873                                 __func__);
4874                         set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4875                         kfree(clp->server_scope);
4876                         clp->server_scope = NULL;
4877                 }
4878
4879                 if (!clp->server_scope)
4880                         clp->server_scope = res.server_scope;
4881                 else
4882                         kfree(res.server_scope);
4883         }
4884
4885         dprintk("<-- %s status= %d\n", __func__, status);
4886         return status;
4887 }
4888
4889 struct nfs4_get_lease_time_data {
4890         struct nfs4_get_lease_time_args *args;
4891         struct nfs4_get_lease_time_res *res;
4892         struct nfs_client *clp;
4893 };
4894
4895 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4896                                         void *calldata)
4897 {
4898         int ret;
4899         struct nfs4_get_lease_time_data *data =
4900                         (struct nfs4_get_lease_time_data *)calldata;
4901
4902         dprintk("--> %s\n", __func__);
4903         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4904         /* just setup sequence, do not trigger session recovery
4905            since we're invoked within one */
4906         ret = nfs41_setup_sequence(data->clp->cl_session,
4907                                    &data->args->la_seq_args,
4908                                    &data->res->lr_seq_res, 0, task);
4909
4910         BUG_ON(ret == -EAGAIN);
4911         rpc_call_start(task);
4912         dprintk("<-- %s\n", __func__);
4913 }
4914
4915 /*
4916  * Called from nfs4_state_manager thread for session setup, so don't recover
4917  * from sequence operation or clientid errors.
4918  */
4919 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4920 {
4921         struct nfs4_get_lease_time_data *data =
4922                         (struct nfs4_get_lease_time_data *)calldata;
4923
4924         dprintk("--> %s\n", __func__);
4925         if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4926                 return;
4927         switch (task->tk_status) {
4928         case -NFS4ERR_DELAY:
4929         case -NFS4ERR_GRACE:
4930                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4931                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4932                 task->tk_status = 0;
4933                 /* fall through */
4934         case -NFS4ERR_RETRY_UNCACHED_REP:
4935                 rpc_restart_call_prepare(task);
4936                 return;
4937         }
4938         dprintk("<-- %s\n", __func__);
4939 }
4940
4941 struct rpc_call_ops nfs4_get_lease_time_ops = {
4942         .rpc_call_prepare = nfs4_get_lease_time_prepare,
4943         .rpc_call_done = nfs4_get_lease_time_done,
4944 };
4945
4946 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4947 {
4948         struct rpc_task *task;
4949         struct nfs4_get_lease_time_args args;
4950         struct nfs4_get_lease_time_res res = {
4951                 .lr_fsinfo = fsinfo,
4952         };
4953         struct nfs4_get_lease_time_data data = {
4954                 .args = &args,
4955                 .res = &res,
4956                 .clp = clp,
4957         };
4958         struct rpc_message msg = {
4959                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4960                 .rpc_argp = &args,
4961                 .rpc_resp = &res,
4962         };
4963         struct rpc_task_setup task_setup = {
4964                 .rpc_client = clp->cl_rpcclient,
4965                 .rpc_message = &msg,
4966                 .callback_ops = &nfs4_get_lease_time_ops,
4967                 .callback_data = &data,
4968                 .flags = RPC_TASK_TIMEOUT,
4969         };
4970         int status;
4971
4972         dprintk("--> %s\n", __func__);
4973         task = rpc_run_task(&task_setup);
4974
4975         if (IS_ERR(task))
4976                 status = PTR_ERR(task);
4977         else {
4978                 status = task->tk_status;
4979                 rpc_put_task(task);
4980         }
4981         dprintk("<-- %s return %d\n", __func__, status);
4982
4983         return status;
4984 }
4985
4986 /*
4987  * Reset a slot table
4988  */
4989 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4990                                  int ivalue)
4991 {
4992         struct nfs4_slot *new = NULL;
4993         int i;
4994         int ret = 0;
4995
4996         dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4997                 max_reqs, tbl->max_slots);
4998
4999         /* Does the newly negotiated max_reqs match the existing slot table? */
5000         if (max_reqs != tbl->max_slots) {
5001                 ret = -ENOMEM;
5002                 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
5003                               GFP_NOFS);
5004                 if (!new)
5005                         goto out;
5006                 ret = 0;
5007                 kfree(tbl->slots);
5008         }
5009         spin_lock(&tbl->slot_tbl_lock);
5010         if (new) {
5011                 tbl->slots = new;
5012                 tbl->max_slots = max_reqs;
5013         }
5014         for (i = 0; i < tbl->max_slots; ++i)
5015                 tbl->slots[i].seq_nr = ivalue;
5016         spin_unlock(&tbl->slot_tbl_lock);
5017         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5018                 tbl, tbl->slots, tbl->max_slots);
5019 out:
5020         dprintk("<-- %s: return %d\n", __func__, ret);
5021         return ret;
5022 }
5023
5024 /*
5025  * Reset the forechannel and backchannel slot tables
5026  */
5027 static int nfs4_reset_slot_tables(struct nfs4_session *session)
5028 {
5029         int status;
5030
5031         status = nfs4_reset_slot_table(&session->fc_slot_table,
5032                         session->fc_attrs.max_reqs, 1);
5033         if (status)
5034                 return status;
5035
5036         status = nfs4_reset_slot_table(&session->bc_slot_table,
5037                         session->bc_attrs.max_reqs, 0);
5038         return status;
5039 }
5040
5041 /* Destroy the slot table */
5042 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5043 {
5044         if (session->fc_slot_table.slots != NULL) {
5045                 kfree(session->fc_slot_table.slots);
5046                 session->fc_slot_table.slots = NULL;
5047         }
5048         if (session->bc_slot_table.slots != NULL) {
5049                 kfree(session->bc_slot_table.slots);
5050                 session->bc_slot_table.slots = NULL;
5051         }
5052         return;
5053 }
5054
5055 /*
5056  * Initialize slot table
5057  */
5058 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5059                 int max_slots, int ivalue)
5060 {
5061         struct nfs4_slot *slot;
5062         int ret = -ENOMEM;
5063
5064         BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5065
5066         dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5067
5068         slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5069         if (!slot)
5070                 goto out;
5071         ret = 0;
5072
5073         spin_lock(&tbl->slot_tbl_lock);
5074         tbl->max_slots = max_slots;
5075         tbl->slots = slot;
5076         tbl->highest_used_slotid = -1;  /* no slot is currently used */
5077         spin_unlock(&tbl->slot_tbl_lock);
5078         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5079                 tbl, tbl->slots, tbl->max_slots);
5080 out:
5081         dprintk("<-- %s: return %d\n", __func__, ret);
5082         return ret;
5083 }
5084
5085 /*
5086  * Initialize the forechannel and backchannel tables
5087  */
5088 static int nfs4_init_slot_tables(struct nfs4_session *session)
5089 {
5090         struct nfs4_slot_table *tbl;
5091         int status = 0;
5092
5093         tbl = &session->fc_slot_table;
5094         if (tbl->slots == NULL) {
5095                 status = nfs4_init_slot_table(tbl,
5096                                 session->fc_attrs.max_reqs, 1);
5097                 if (status)
5098                         return status;
5099         }
5100
5101         tbl = &session->bc_slot_table;
5102         if (tbl->slots == NULL) {
5103                 status = nfs4_init_slot_table(tbl,
5104                                 session->bc_attrs.max_reqs, 0);
5105                 if (status)
5106                         nfs4_destroy_slot_tables(session);
5107         }
5108
5109         return status;
5110 }
5111
5112 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5113 {
5114         struct nfs4_session *session;
5115         struct nfs4_slot_table *tbl;
5116
5117         session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5118         if (!session)
5119                 return NULL;
5120
5121         tbl = &session->fc_slot_table;
5122         tbl->highest_used_slotid = -1;
5123         spin_lock_init(&tbl->slot_tbl_lock);
5124         rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5125         init_completion(&tbl->complete);
5126
5127         tbl = &session->bc_slot_table;
5128         tbl->highest_used_slotid = -1;
5129         spin_lock_init(&tbl->slot_tbl_lock);
5130         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5131         init_completion(&tbl->complete);
5132
5133         session->session_state = 1<<NFS4_SESSION_INITING;
5134
5135         session->clp = clp;
5136         return session;
5137 }
5138
5139 void nfs4_destroy_session(struct nfs4_session *session)
5140 {
5141         nfs4_proc_destroy_session(session);
5142         dprintk("%s Destroy backchannel for xprt %p\n",
5143                 __func__, session->clp->cl_rpcclient->cl_xprt);
5144         xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5145                                 NFS41_BC_MIN_CALLBACKS);
5146         nfs4_destroy_slot_tables(session);
5147         kfree(session);
5148 }
5149
5150 /*
5151  * Initialize the values to be used by the client in CREATE_SESSION
5152  * If nfs4_init_session set the fore channel request and response sizes,
5153  * use them.
5154  *
5155  * Set the back channel max_resp_sz_cached to zero to force the client to
5156  * always set csa_cachethis to FALSE because the current implementation
5157  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5158  */
5159 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5160 {
5161         struct nfs4_session *session = args->client->cl_session;
5162         unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5163                      mxresp_sz = session->fc_attrs.max_resp_sz;
5164
5165         if (mxrqst_sz == 0)
5166                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5167         if (mxresp_sz == 0)
5168                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5169         /* Fore channel attributes */
5170         args->fc_attrs.max_rqst_sz = mxrqst_sz;
5171         args->fc_attrs.max_resp_sz = mxresp_sz;
5172         args->fc_attrs.max_ops = NFS4_MAX_OPS;
5173         args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5174
5175         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5176                 "max_ops=%u max_reqs=%u\n",
5177                 __func__,
5178                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5179                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5180
5181         /* Back channel attributes */
5182         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5183         args->bc_attrs.max_resp_sz = PAGE_SIZE;
5184         args->bc_attrs.max_resp_sz_cached = 0;
5185         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5186         args->bc_attrs.max_reqs = 1;
5187
5188         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5189                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5190                 __func__,
5191                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5192                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5193                 args->bc_attrs.max_reqs);
5194 }
5195
5196 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5197 {
5198         struct nfs4_channel_attrs *sent = &args->fc_attrs;
5199         struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5200
5201         if (rcvd->max_resp_sz > sent->max_resp_sz)
5202                 return -EINVAL;
5203         /*
5204          * Our requested max_ops is the minimum we need; we're not
5205          * prepared to break up compounds into smaller pieces than that.
5206          * So, no point even trying to continue if the server won't
5207          * cooperate:
5208          */
5209         if (rcvd->max_ops < sent->max_ops)
5210                 return -EINVAL;
5211         if (rcvd->max_reqs == 0)
5212                 return -EINVAL;
5213         return 0;
5214 }
5215
5216 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5217 {
5218         struct nfs4_channel_attrs *sent = &args->bc_attrs;
5219         struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5220
5221         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5222                 return -EINVAL;
5223         if (rcvd->max_resp_sz < sent->max_resp_sz)
5224                 return -EINVAL;
5225         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5226                 return -EINVAL;
5227         /* These would render the backchannel useless: */
5228         if (rcvd->max_ops  == 0)
5229                 return -EINVAL;
5230         if (rcvd->max_reqs == 0)
5231                 return -EINVAL;
5232         return 0;
5233 }
5234
5235 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5236                                      struct nfs4_session *session)
5237 {
5238         int ret;
5239
5240         ret = nfs4_verify_fore_channel_attrs(args, session);
5241         if (ret)
5242                 return ret;
5243         return nfs4_verify_back_channel_attrs(args, session);
5244 }
5245
5246 static int _nfs4_proc_create_session(struct nfs_client *clp)
5247 {
5248         struct nfs4_session *session = clp->cl_session;
5249         struct nfs41_create_session_args args = {
5250                 .client = clp,
5251                 .cb_program = NFS4_CALLBACK,
5252         };
5253         struct nfs41_create_session_res res = {
5254                 .client = clp,
5255         };
5256         struct rpc_message msg = {
5257                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5258                 .rpc_argp = &args,
5259                 .rpc_resp = &res,
5260         };
5261         int status;
5262
5263         nfs4_init_channel_attrs(&args);
5264         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5265
5266         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5267
5268         if (!status)
5269                 /* Verify the session's negotiated channel_attrs values */
5270                 status = nfs4_verify_channel_attrs(&args, session);
5271         if (!status) {
5272                 /* Increment the clientid slot sequence id */
5273                 clp->cl_seqid++;
5274         }
5275
5276         return status;
5277 }
5278
5279 /*
5280  * Issues a CREATE_SESSION operation to the server.
5281  * It is the responsibility of the caller to verify the session is
5282  * expired before calling this routine.
5283  */
5284 int nfs4_proc_create_session(struct nfs_client *clp)
5285 {
5286         int status;
5287         unsigned *ptr;
5288         struct nfs4_session *session = clp->cl_session;
5289
5290         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5291
5292         status = _nfs4_proc_create_session(clp);
5293         if (status)
5294                 goto out;
5295
5296         /* Init and reset the fore channel */
5297         status = nfs4_init_slot_tables(session);
5298         dprintk("slot table initialization returned %d\n", status);
5299         if (status)
5300                 goto out;
5301         status = nfs4_reset_slot_tables(session);
5302         dprintk("slot table reset returned %d\n", status);
5303         if (status)
5304                 goto out;
5305
5306         ptr = (unsigned *)&session->sess_id.data[0];
5307         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5308                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5309 out:
5310         dprintk("<-- %s\n", __func__);
5311         return status;
5312 }
5313
5314 /*
5315  * Issue the over-the-wire RPC DESTROY_SESSION.
5316  * The caller must serialize access to this routine.
5317  */
5318 int nfs4_proc_destroy_session(struct nfs4_session *session)
5319 {
5320         int status = 0;
5321         struct rpc_message msg;
5322
5323         dprintk("--> nfs4_proc_destroy_session\n");
5324
5325         /* session is still being setup */
5326         if (session->clp->cl_cons_state != NFS_CS_READY)
5327                 return status;
5328
5329         msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5330         msg.rpc_argp = session;
5331         msg.rpc_resp = NULL;
5332         msg.rpc_cred = NULL;
5333         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5334
5335         if (status)
5336                 printk(KERN_WARNING
5337                         "Got error %d from the server on DESTROY_SESSION. "
5338                         "Session has been destroyed regardless...\n", status);
5339
5340         dprintk("<-- nfs4_proc_destroy_session\n");
5341         return status;
5342 }
5343
5344 int nfs4_init_session(struct nfs_server *server)
5345 {
5346         struct nfs_client *clp = server->nfs_client;
5347         struct nfs4_session *session;
5348         unsigned int rsize, wsize;
5349         int ret;
5350
5351         if (!nfs4_has_session(clp))
5352                 return 0;
5353
5354         session = clp->cl_session;
5355         if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5356                 return 0;
5357
5358         rsize = server->rsize;
5359         if (rsize == 0)
5360                 rsize = NFS_MAX_FILE_IO_SIZE;
5361         wsize = server->wsize;
5362         if (wsize == 0)
5363                 wsize = NFS_MAX_FILE_IO_SIZE;
5364
5365         session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5366         session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5367
5368         ret = nfs4_recover_expired_lease(server);
5369         if (!ret)
5370                 ret = nfs4_check_client_ready(clp);
5371         return ret;
5372 }
5373
5374 int nfs4_init_ds_session(struct nfs_client *clp)
5375 {
5376         struct nfs4_session *session = clp->cl_session;
5377         int ret;
5378
5379         if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5380                 return 0;
5381
5382         ret = nfs4_client_recover_expired_lease(clp);
5383         if (!ret)
5384                 /* Test for the DS role */
5385                 if (!is_ds_client(clp))
5386                         ret = -ENODEV;
5387         if (!ret)
5388                 ret = nfs4_check_client_ready(clp);
5389         return ret;
5390
5391 }
5392 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5393
5394
5395 /*
5396  * Renew the cl_session lease.
5397  */
5398 struct nfs4_sequence_data {
5399         struct nfs_client *clp;
5400         struct nfs4_sequence_args args;
5401         struct nfs4_sequence_res res;
5402 };
5403
5404 static void nfs41_sequence_release(void *data)
5405 {
5406         struct nfs4_sequence_data *calldata = data;
5407         struct nfs_client *clp = calldata->clp;
5408
5409         if (atomic_read(&clp->cl_count) > 1)
5410                 nfs4_schedule_state_renewal(clp);
5411         nfs_put_client(clp);
5412         kfree(calldata);
5413 }
5414
5415 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5416 {
5417         switch(task->tk_status) {
5418         case -NFS4ERR_DELAY:
5419                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5420                 return -EAGAIN;
5421         default:
5422                 nfs4_schedule_lease_recovery(clp);
5423         }
5424         return 0;
5425 }
5426
5427 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5428 {
5429         struct nfs4_sequence_data *calldata = data;
5430         struct nfs_client *clp = calldata->clp;
5431
5432         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5433                 return;
5434
5435         if (task->tk_status < 0) {
5436                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5437                 if (atomic_read(&clp->cl_count) == 1)
5438                         goto out;
5439
5440                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5441                         rpc_restart_call_prepare(task);
5442                         return;
5443                 }
5444         }
5445         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5446 out:
5447         dprintk("<-- %s\n", __func__);
5448 }
5449
5450 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5451 {
5452         struct nfs4_sequence_data *calldata = data;
5453         struct nfs_client *clp = calldata->clp;
5454         struct nfs4_sequence_args *args;
5455         struct nfs4_sequence_res *res;
5456
5457         args = task->tk_msg.rpc_argp;
5458         res = task->tk_msg.rpc_resp;
5459
5460         if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5461                 return;
5462         rpc_call_start(task);
5463 }
5464
5465 static const struct rpc_call_ops nfs41_sequence_ops = {
5466         .rpc_call_done = nfs41_sequence_call_done,
5467         .rpc_call_prepare = nfs41_sequence_prepare,
5468         .rpc_release = nfs41_sequence_release,
5469 };
5470
5471 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5472 {
5473         struct nfs4_sequence_data *calldata;
5474         struct rpc_message msg = {
5475                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5476                 .rpc_cred = cred,
5477         };
5478         struct rpc_task_setup task_setup_data = {
5479                 .rpc_client = clp->cl_rpcclient,
5480                 .rpc_message = &msg,
5481                 .callback_ops = &nfs41_sequence_ops,
5482                 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5483         };
5484
5485         if (!atomic_inc_not_zero(&clp->cl_count))
5486                 return ERR_PTR(-EIO);
5487         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5488         if (calldata == NULL) {
5489                 nfs_put_client(clp);
5490                 return ERR_PTR(-ENOMEM);
5491         }
5492         msg.rpc_argp = &calldata->args;
5493         msg.rpc_resp = &calldata->res;
5494         calldata->clp = clp;
5495         task_setup_data.callback_data = calldata;
5496
5497         return rpc_run_task(&task_setup_data);
5498 }
5499
5500 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5501 {
5502         struct rpc_task *task;
5503         int ret = 0;
5504
5505         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5506                 return 0;
5507         task = _nfs41_proc_sequence(clp, cred);
5508         if (IS_ERR(task))
5509                 ret = PTR_ERR(task);
5510         else
5511                 rpc_put_task_async(task);
5512         dprintk("<-- %s status=%d\n", __func__, ret);
5513         return ret;
5514 }
5515
5516 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5517 {
5518         struct rpc_task *task;
5519         int ret;
5520
5521         task = _nfs41_proc_sequence(clp, cred);
5522         if (IS_ERR(task)) {
5523                 ret = PTR_ERR(task);
5524                 goto out;
5525         }
5526         ret = rpc_wait_for_completion_task(task);
5527         if (!ret) {
5528                 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5529
5530                 if (task->tk_status == 0)
5531                         nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5532                 ret = task->tk_status;
5533         }
5534         rpc_put_task(task);
5535 out:
5536         dprintk("<-- %s status=%d\n", __func__, ret);
5537         return ret;
5538 }
5539
5540 struct nfs4_reclaim_complete_data {
5541         struct nfs_client *clp;
5542         struct nfs41_reclaim_complete_args arg;
5543         struct nfs41_reclaim_complete_res res;
5544 };
5545
5546 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5547 {
5548         struct nfs4_reclaim_complete_data *calldata = data;
5549
5550         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5551         if (nfs41_setup_sequence(calldata->clp->cl_session,
5552                                 &calldata->arg.seq_args,
5553                                 &calldata->res.seq_res, 0, task))
5554                 return;
5555
5556         rpc_call_start(task);
5557 }
5558
5559 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5560 {
5561         switch(task->tk_status) {
5562         case 0:
5563         case -NFS4ERR_COMPLETE_ALREADY:
5564         case -NFS4ERR_WRONG_CRED: /* What to do here? */
5565                 break;
5566         case -NFS4ERR_DELAY:
5567                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5568                 /* fall through */
5569         case -NFS4ERR_RETRY_UNCACHED_REP:
5570                 return -EAGAIN;
5571         default:
5572                 nfs4_schedule_lease_recovery(clp);
5573         }
5574         return 0;
5575 }
5576
5577 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5578 {
5579         struct nfs4_reclaim_complete_data *calldata = data;
5580         struct nfs_client *clp = calldata->clp;
5581         struct nfs4_sequence_res *res = &calldata->res.seq_res;
5582
5583         dprintk("--> %s\n", __func__);
5584         if (!nfs41_sequence_done(task, res))
5585                 return;
5586
5587         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5588                 rpc_restart_call_prepare(task);
5589                 return;
5590         }
5591         dprintk("<-- %s\n", __func__);
5592 }
5593
5594 static void nfs4_free_reclaim_complete_data(void *data)
5595 {
5596         struct nfs4_reclaim_complete_data *calldata = data;
5597
5598         kfree(calldata);
5599 }
5600
5601 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5602         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5603         .rpc_call_done = nfs4_reclaim_complete_done,
5604         .rpc_release = nfs4_free_reclaim_complete_data,
5605 };
5606
5607 /*
5608  * Issue a global reclaim complete.
5609  */
5610 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5611 {
5612         struct nfs4_reclaim_complete_data *calldata;
5613         struct rpc_task *task;
5614         struct rpc_message msg = {
5615                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5616         };
5617         struct rpc_task_setup task_setup_data = {
5618                 .rpc_client = clp->cl_rpcclient,
5619                 .rpc_message = &msg,
5620                 .callback_ops = &nfs4_reclaim_complete_call_ops,
5621                 .flags = RPC_TASK_ASYNC,
5622         };
5623         int status = -ENOMEM;
5624
5625         dprintk("--> %s\n", __func__);
5626         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5627         if (calldata == NULL)
5628                 goto out;
5629         calldata->clp = clp;
5630         calldata->arg.one_fs = 0;
5631
5632         msg.rpc_argp = &calldata->arg;
5633         msg.rpc_resp = &calldata->res;
5634         task_setup_data.callback_data = calldata;
5635         task = rpc_run_task(&task_setup_data);
5636         if (IS_ERR(task)) {
5637                 status = PTR_ERR(task);
5638                 goto out;
5639         }
5640         status = nfs4_wait_for_completion_rpc_task(task);
5641         if (status == 0)
5642                 status = task->tk_status;
5643         rpc_put_task(task);
5644         return 0;
5645 out:
5646         dprintk("<-- %s status=%d\n", __func__, status);
5647         return status;
5648 }
5649
5650 static void
5651 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5652 {
5653         struct nfs4_layoutget *lgp = calldata;
5654         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5655
5656         dprintk("--> %s\n", __func__);
5657         /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5658          * right now covering the LAYOUTGET we are about to send.
5659          * However, that is not so catastrophic, and there seems
5660          * to be no way to prevent it completely.
5661          */
5662         if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5663                                 &lgp->res.seq_res, 0, task))
5664                 return;
5665         if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5666                                           NFS_I(lgp->args.inode)->layout,
5667                                           lgp->args.ctx->state)) {
5668                 rpc_exit(task, NFS4_OK);
5669                 return;
5670         }
5671         rpc_call_start(task);
5672 }
5673
5674 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5675 {
5676         struct nfs4_layoutget *lgp = calldata;
5677         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5678
5679         dprintk("--> %s\n", __func__);
5680
5681         if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5682                 return;
5683
5684         switch (task->tk_status) {
5685         case 0:
5686                 break;
5687         case -NFS4ERR_LAYOUTTRYLATER:
5688         case -NFS4ERR_RECALLCONFLICT:
5689                 task->tk_status = -NFS4ERR_DELAY;
5690                 /* Fall through */
5691         default:
5692                 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5693                         rpc_restart_call_prepare(task);
5694                         return;
5695                 }
5696         }
5697         dprintk("<-- %s\n", __func__);
5698 }
5699
5700 static void nfs4_layoutget_release(void *calldata)
5701 {
5702         struct nfs4_layoutget *lgp = calldata;
5703
5704         dprintk("--> %s\n", __func__);
5705         put_nfs_open_context(lgp->args.ctx);
5706         kfree(calldata);
5707         dprintk("<-- %s\n", __func__);
5708 }
5709
5710 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5711         .rpc_call_prepare = nfs4_layoutget_prepare,
5712         .rpc_call_done = nfs4_layoutget_done,
5713         .rpc_release = nfs4_layoutget_release,
5714 };
5715
5716 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5717 {
5718         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5719         struct rpc_task *task;
5720         struct rpc_message msg = {
5721                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5722                 .rpc_argp = &lgp->args,
5723                 .rpc_resp = &lgp->res,
5724         };
5725         struct rpc_task_setup task_setup_data = {
5726                 .rpc_client = server->client,
5727                 .rpc_message = &msg,
5728                 .callback_ops = &nfs4_layoutget_call_ops,
5729                 .callback_data = lgp,
5730                 .flags = RPC_TASK_ASYNC,
5731         };
5732         int status = 0;
5733
5734         dprintk("--> %s\n", __func__);
5735
5736         lgp->res.layoutp = &lgp->args.layout;
5737         lgp->res.seq_res.sr_slot = NULL;
5738         task = rpc_run_task(&task_setup_data);
5739         if (IS_ERR(task))
5740                 return PTR_ERR(task);
5741         status = nfs4_wait_for_completion_rpc_task(task);
5742         if (status == 0)
5743                 status = task->tk_status;
5744         if (status == 0)
5745                 status = pnfs_layout_process(lgp);
5746         rpc_put_task(task);
5747         dprintk("<-- %s status=%d\n", __func__, status);
5748         return status;
5749 }
5750
5751 static void
5752 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5753 {
5754         struct nfs4_layoutreturn *lrp = calldata;
5755
5756         dprintk("--> %s\n", __func__);
5757         if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5758                                 &lrp->res.seq_res, 0, task))
5759                 return;
5760         rpc_call_start(task);
5761 }
5762
5763 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5764 {
5765         struct nfs4_layoutreturn *lrp = calldata;
5766         struct nfs_server *server;
5767         struct pnfs_layout_hdr *lo = lrp->args.layout;
5768
5769         dprintk("--> %s\n", __func__);
5770
5771         if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5772                 return;
5773
5774         server = NFS_SERVER(lrp->args.inode);
5775         if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5776                 rpc_restart_call_prepare(task);
5777                 return;
5778         }
5779         spin_lock(&lo->plh_inode->i_lock);
5780         if (task->tk_status == 0) {
5781                 if (lrp->res.lrs_present) {
5782                         pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5783                 } else
5784                         BUG_ON(!list_empty(&lo->plh_segs));
5785         }
5786         lo->plh_block_lgets--;
5787         spin_unlock(&lo->plh_inode->i_lock);
5788         dprintk("<-- %s\n", __func__);
5789 }
5790
5791 static void nfs4_layoutreturn_release(void *calldata)
5792 {
5793         struct nfs4_layoutreturn *lrp = calldata;
5794
5795         dprintk("--> %s\n", __func__);
5796         put_layout_hdr(lrp->args.layout);
5797         kfree(calldata);
5798         dprintk("<-- %s\n", __func__);
5799 }
5800
5801 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5802         .rpc_call_prepare = nfs4_layoutreturn_prepare,
5803         .rpc_call_done = nfs4_layoutreturn_done,
5804         .rpc_release = nfs4_layoutreturn_release,
5805 };
5806
5807 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5808 {
5809         struct rpc_task *task;
5810         struct rpc_message msg = {
5811                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5812                 .rpc_argp = &lrp->args,
5813                 .rpc_resp = &lrp->res,
5814         };
5815         struct rpc_task_setup task_setup_data = {
5816                 .rpc_client = lrp->clp->cl_rpcclient,
5817                 .rpc_message = &msg,
5818                 .callback_ops = &nfs4_layoutreturn_call_ops,
5819                 .callback_data = lrp,
5820         };
5821         int status;
5822
5823         dprintk("--> %s\n", __func__);
5824         task = rpc_run_task(&task_setup_data);
5825         if (IS_ERR(task))
5826                 return PTR_ERR(task);
5827         status = task->tk_status;
5828         dprintk("<-- %s status=%d\n", __func__, status);
5829         rpc_put_task(task);
5830         return status;
5831 }
5832
5833 /*
5834  * Retrieve the list of Data Server devices from the MDS.
5835  */
5836 static int _nfs4_getdevicelist(struct nfs_server *server,
5837                                     const struct nfs_fh *fh,
5838                                     struct pnfs_devicelist *devlist)
5839 {
5840         struct nfs4_getdevicelist_args args = {
5841                 .fh = fh,
5842                 .layoutclass = server->pnfs_curr_ld->id,
5843         };
5844         struct nfs4_getdevicelist_res res = {
5845                 .devlist = devlist,
5846         };
5847         struct rpc_message msg = {
5848                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5849                 .rpc_argp = &args,
5850                 .rpc_resp = &res,
5851         };
5852         int status;
5853
5854         dprintk("--> %s\n", __func__);
5855         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5856                                 &res.seq_res, 0);
5857         dprintk("<-- %s status=%d\n", __func__, status);
5858         return status;
5859 }
5860
5861 int nfs4_proc_getdevicelist(struct nfs_server *server,
5862                             const struct nfs_fh *fh,
5863                             struct pnfs_devicelist *devlist)
5864 {
5865         struct nfs4_exception exception = { };
5866         int err;
5867
5868         do {
5869                 err = nfs4_handle_exception(server,
5870                                 _nfs4_getdevicelist(server, fh, devlist),
5871                                 &exception);
5872         } while (exception.retry);
5873
5874         dprintk("%s: err=%d, num_devs=%u\n", __func__,
5875                 err, devlist->num_devs);
5876
5877         return err;
5878 }
5879 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5880
5881 static int
5882 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5883 {
5884         struct nfs4_getdeviceinfo_args args = {
5885                 .pdev = pdev,
5886         };
5887         struct nfs4_getdeviceinfo_res res = {
5888                 .pdev = pdev,
5889         };
5890         struct rpc_message msg = {
5891                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5892                 .rpc_argp = &args,
5893                 .rpc_resp = &res,
5894         };
5895         int status;
5896
5897         dprintk("--> %s\n", __func__);
5898         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5899         dprintk("<-- %s status=%d\n", __func__, status);
5900
5901         return status;
5902 }
5903
5904 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5905 {
5906         struct nfs4_exception exception = { };
5907         int err;
5908
5909         do {
5910                 err = nfs4_handle_exception(server,
5911                                         _nfs4_proc_getdeviceinfo(server, pdev),
5912                                         &exception);
5913         } while (exception.retry);
5914         return err;
5915 }
5916 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5917
5918 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5919 {
5920         struct nfs4_layoutcommit_data *data = calldata;
5921         struct nfs_server *server = NFS_SERVER(data->args.inode);
5922
5923         if (nfs4_setup_sequence(server, &data->args.seq_args,
5924                                 &data->res.seq_res, 1, task))
5925                 return;
5926         rpc_call_start(task);
5927 }
5928
5929 static void
5930 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5931 {
5932         struct nfs4_layoutcommit_data *data = calldata;
5933         struct nfs_server *server = NFS_SERVER(data->args.inode);
5934
5935         if (!nfs4_sequence_done(task, &data->res.seq_res))
5936                 return;
5937
5938         switch (task->tk_status) { /* Just ignore these failures */
5939         case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5940         case NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
5941         case NFS4ERR_BADLAYOUT:     /* no layout */
5942         case NFS4ERR_GRACE:         /* loca_recalim always false */
5943                 task->tk_status = 0;
5944         }
5945
5946         if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5947                 rpc_restart_call_prepare(task);
5948                 return;
5949         }
5950
5951         if (task->tk_status == 0)
5952                 nfs_post_op_update_inode_force_wcc(data->args.inode,
5953                                                    data->res.fattr);
5954 }
5955
5956 static void nfs4_layoutcommit_release(void *calldata)
5957 {
5958         struct nfs4_layoutcommit_data *data = calldata;
5959         struct pnfs_layout_segment *lseg, *tmp;
5960         unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
5961
5962         pnfs_cleanup_layoutcommit(data);
5963         /* Matched by references in pnfs_set_layoutcommit */
5964         list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5965                 list_del_init(&lseg->pls_lc_list);
5966                 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5967                                        &lseg->pls_flags))
5968                         put_lseg(lseg);
5969         }
5970
5971         clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5972         smp_mb__after_clear_bit();
5973         wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5974
5975         put_rpccred(data->cred);
5976         kfree(data);
5977 }
5978
5979 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5980         .rpc_call_prepare = nfs4_layoutcommit_prepare,
5981         .rpc_call_done = nfs4_layoutcommit_done,
5982         .rpc_release = nfs4_layoutcommit_release,
5983 };
5984
5985 int
5986 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
5987 {
5988         struct rpc_message msg = {
5989                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5990                 .rpc_argp = &data->args,
5991                 .rpc_resp = &data->res,
5992                 .rpc_cred = data->cred,
5993         };
5994         struct rpc_task_setup task_setup_data = {
5995                 .task = &data->task,
5996                 .rpc_client = NFS_CLIENT(data->args.inode),
5997                 .rpc_message = &msg,
5998                 .callback_ops = &nfs4_layoutcommit_ops,
5999                 .callback_data = data,
6000                 .flags = RPC_TASK_ASYNC,
6001         };
6002         struct rpc_task *task;
6003         int status = 0;
6004
6005         dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6006                 "lbw: %llu inode %lu\n",
6007                 data->task.tk_pid, sync,
6008                 data->args.lastbytewritten,
6009                 data->args.inode->i_ino);
6010
6011         task = rpc_run_task(&task_setup_data);
6012         if (IS_ERR(task))
6013                 return PTR_ERR(task);
6014         if (sync == false)
6015                 goto out;
6016         status = nfs4_wait_for_completion_rpc_task(task);
6017         if (status != 0)
6018                 goto out;
6019         status = task->tk_status;
6020 out:
6021         dprintk("%s: status %d\n", __func__, status);
6022         rpc_put_task(task);
6023         return status;
6024 }
6025
6026 static int
6027 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6028                     struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6029 {
6030         struct nfs41_secinfo_no_name_args args = {
6031                 .style = SECINFO_STYLE_CURRENT_FH,
6032         };
6033         struct nfs4_secinfo_res res = {
6034                 .flavors = flavors,
6035         };
6036         struct rpc_message msg = {
6037                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6038                 .rpc_argp = &args,
6039                 .rpc_resp = &res,
6040         };
6041         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6042 }
6043
6044 static int
6045 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6046                            struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6047 {
6048         struct nfs4_exception exception = { };
6049         int err;
6050         do {
6051                 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6052                 switch (err) {
6053                 case 0:
6054                 case -NFS4ERR_WRONGSEC:
6055                 case -NFS4ERR_NOTSUPP:
6056                         break;
6057                 default:
6058                         err = nfs4_handle_exception(server, err, &exception);
6059                 }
6060         } while (exception.retry);
6061         return err;
6062 }
6063
6064 static int
6065 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6066                     struct nfs_fsinfo *info)
6067 {
6068         int err;
6069         struct page *page;
6070         rpc_authflavor_t flavor;
6071         struct nfs4_secinfo_flavors *flavors;
6072
6073         page = alloc_page(GFP_KERNEL);
6074         if (!page) {
6075                 err = -ENOMEM;
6076                 goto out;
6077         }
6078
6079         flavors = page_address(page);
6080         err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6081
6082         /*
6083          * Fall back on "guess and check" method if
6084          * the server doesn't support SECINFO_NO_NAME
6085          */
6086         if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6087                 err = nfs4_find_root_sec(server, fhandle, info);
6088                 goto out_freepage;
6089         }
6090         if (err)
6091                 goto out_freepage;
6092
6093         flavor = nfs_find_best_sec(flavors);
6094         if (err == 0)
6095                 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6096
6097 out_freepage:
6098         put_page(page);
6099         if (err == -EACCES)
6100                 return -EPERM;
6101 out:
6102         return err;
6103 }
6104 static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6105 {
6106         int status;
6107         struct nfs41_test_stateid_args args = {
6108                 .stateid = &state->stateid,
6109         };
6110         struct nfs41_test_stateid_res res;
6111         struct rpc_message msg = {
6112                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6113                 .rpc_argp = &args,
6114                 .rpc_resp = &res,
6115         };
6116         args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6117         status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6118         return status;
6119 }
6120
6121 static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6122 {
6123         struct nfs4_exception exception = { };
6124         int err;
6125         do {
6126                 err = nfs4_handle_exception(server,
6127                                 _nfs41_test_stateid(server, state),
6128                                 &exception);
6129         } while (exception.retry);
6130         return err;
6131 }
6132
6133 static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6134 {
6135         int status;
6136         struct nfs41_free_stateid_args args = {
6137                 .stateid = &state->stateid,
6138         };
6139         struct nfs41_free_stateid_res res;
6140         struct rpc_message msg = {
6141                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6142                 .rpc_argp = &args,
6143                 .rpc_resp = &res,
6144         };
6145
6146         args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6147         status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6148         return status;
6149 }
6150
6151 static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6152 {
6153         struct nfs4_exception exception = { };
6154         int err;
6155         do {
6156                 err = nfs4_handle_exception(server,
6157                                 _nfs4_free_stateid(server, state),
6158                                 &exception);
6159         } while (exception.retry);
6160         return err;
6161 }
6162 #endif /* CONFIG_NFS_V4_1 */
6163
6164 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6165         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6166         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6167         .recover_open   = nfs4_open_reclaim,
6168         .recover_lock   = nfs4_lock_reclaim,
6169         .establish_clid = nfs4_init_clientid,
6170         .get_clid_cred  = nfs4_get_setclientid_cred,
6171 };
6172
6173 #if defined(CONFIG_NFS_V4_1)
6174 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6175         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6176         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6177         .recover_open   = nfs4_open_reclaim,
6178         .recover_lock   = nfs4_lock_reclaim,
6179         .establish_clid = nfs41_init_clientid,
6180         .get_clid_cred  = nfs4_get_exchange_id_cred,
6181         .reclaim_complete = nfs41_proc_reclaim_complete,
6182 };
6183 #endif /* CONFIG_NFS_V4_1 */
6184
6185 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6186         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6187         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6188         .recover_open   = nfs4_open_expired,
6189         .recover_lock   = nfs4_lock_expired,
6190         .establish_clid = nfs4_init_clientid,
6191         .get_clid_cred  = nfs4_get_setclientid_cred,
6192 };
6193
6194 #if defined(CONFIG_NFS_V4_1)
6195 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6196         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6197         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6198         .recover_open   = nfs41_open_expired,
6199         .recover_lock   = nfs41_lock_expired,
6200         .establish_clid = nfs41_init_clientid,
6201         .get_clid_cred  = nfs4_get_exchange_id_cred,
6202 };
6203 #endif /* CONFIG_NFS_V4_1 */
6204
6205 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6206         .sched_state_renewal = nfs4_proc_async_renew,
6207         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6208         .renew_lease = nfs4_proc_renew,
6209 };
6210
6211 #if defined(CONFIG_NFS_V4_1)
6212 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6213         .sched_state_renewal = nfs41_proc_async_sequence,
6214         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6215         .renew_lease = nfs4_proc_sequence,
6216 };
6217 #endif
6218
6219 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6220         .minor_version = 0,
6221         .call_sync = _nfs4_call_sync,
6222         .validate_stateid = nfs4_validate_delegation_stateid,
6223         .find_root_sec = nfs4_find_root_sec,
6224         .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6225         .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6226         .state_renewal_ops = &nfs40_state_renewal_ops,
6227 };
6228
6229 #if defined(CONFIG_NFS_V4_1)
6230 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6231         .minor_version = 1,
6232         .call_sync = _nfs4_call_sync_session,
6233         .validate_stateid = nfs41_validate_delegation_stateid,
6234         .find_root_sec = nfs41_find_root_sec,
6235         .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6236         .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6237         .state_renewal_ops = &nfs41_state_renewal_ops,
6238 };
6239 #endif
6240
6241 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6242         [0] = &nfs_v4_0_minor_ops,
6243 #if defined(CONFIG_NFS_V4_1)
6244         [1] = &nfs_v4_1_minor_ops,
6245 #endif
6246 };
6247
6248 static const struct inode_operations nfs4_file_inode_operations = {
6249         .permission     = nfs_permission,
6250         .getattr        = nfs_getattr,
6251         .setattr        = nfs_setattr,
6252         .getxattr       = generic_getxattr,
6253         .setxattr       = generic_setxattr,
6254         .listxattr      = generic_listxattr,
6255         .removexattr    = generic_removexattr,
6256 };
6257
6258 const struct nfs_rpc_ops nfs_v4_clientops = {
6259         .version        = 4,                    /* protocol version */
6260         .dentry_ops     = &nfs4_dentry_operations,
6261         .dir_inode_ops  = &nfs4_dir_inode_operations,
6262         .file_inode_ops = &nfs4_file_inode_operations,
6263         .file_ops       = &nfs4_file_operations,
6264         .getroot        = nfs4_proc_get_root,
6265         .getattr        = nfs4_proc_getattr,
62