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