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