~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfs/nfs4proc.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.10 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.83 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.153 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.200 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.200 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.76 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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