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