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

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

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