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