OSDN Git Service

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