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