4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2016 STRATO AG. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
32 * Use is subject to license terms.
36 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
41 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
49 #include <sys/vnode.h>
52 #include <sys/filio.h>
56 #include <sys/pathname.h>
57 #include <sys/dirent.h>
58 #include <sys/debug.h>
59 #include <sys/vmsystm.h>
60 #include <sys/fcntl.h>
61 #include <sys/flock.h>
63 #include <sys/errno.h>
64 #include <sys/strsubr.h>
65 #include <sys/sysmacros.h>
67 #include <sys/cmn_err.h>
68 #include <sys/pathconf.h>
69 #include <sys/utsname.h>
72 #include <sys/systeminfo.h>
73 #include <sys/policy.h>
79 #include <rpc/types.h>
84 #include <nfs/nfs_clnt.h>
85 #include <nfs/nfs_acl.h>
88 #include <nfs/nfs4_kprot.h>
89 #include <nfs/rnode4.h>
90 #include <nfs/nfs4_clnt.h>
97 #include <vm/seg_map.h>
98 #include <vm/seg_kpm.h>
99 #include <vm/seg_vn.h>
101 #include <sys/fs_subr.h>
104 #include <sys/int_fmtio.h>
105 #include <sys/fs/autofs.h>
108 nfs4_ga_res_t
*di_garp
;
110 hrtime_t di_time_call
;
113 typedef enum nfs4_acl_op
{
118 static struct lm_sysid
*nfs4_find_sysid(mntinfo4_t
*);
120 static void nfs4_update_dircaches(change_info4
*, vnode_t
*, vnode_t
*,
121 char *, dirattr_info_t
*);
123 static void nfs4close_otw(rnode4_t
*, cred_t
*, nfs4_open_owner_t
*,
124 nfs4_open_stream_t
*, int *, int *, nfs4_close_type_t
,
125 nfs4_error_t
*, int *);
126 static int nfs4_rdwrlbn(vnode_t
*, page_t
*, uoff_t
, size_t, int,
128 static int nfs4write(vnode_t
*, caddr_t
, uoff_t
, int, cred_t
*,
130 static int nfs4read(vnode_t
*, caddr_t
, offset_t
, int, size_t *,
131 cred_t
*, bool_t
, struct uio
*);
132 static int nfs4setattr(vnode_t
*, struct vattr
*, int, cred_t
*,
134 static int nfs4openattr(vnode_t
*, vnode_t
**, int, cred_t
*);
135 static int nfs4lookup(vnode_t
*, char *, vnode_t
**, cred_t
*, int);
136 static int nfs4lookup_xattr(vnode_t
*, char *, vnode_t
**, int, cred_t
*);
137 static int nfs4lookupvalidate_otw(vnode_t
*, char *, vnode_t
**, cred_t
*);
138 static int nfs4lookupnew_otw(vnode_t
*, char *, vnode_t
**, cred_t
*);
139 static int nfs4mknod(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
140 int, vnode_t
**, cred_t
*);
141 static int nfs4open_otw(vnode_t
*, char *, struct vattr
*, vnode_t
**,
142 cred_t
*, int, int, enum createmode4
, int);
143 static int nfs4rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
145 static int nfs4rename_persistent_fh(vnode_t
*, char *, vnode_t
*,
146 vnode_t
*, char *, cred_t
*, nfsstat4
*);
147 static int nfs4rename_volatile_fh(vnode_t
*, char *, vnode_t
*,
148 vnode_t
*, char *, cred_t
*, nfsstat4
*);
149 static int do_nfs4readdir(vnode_t
*, rddir4_cache
*, cred_t
*);
150 static void nfs4readdir(vnode_t
*, rddir4_cache
*, cred_t
*);
151 static int nfs4_bio(struct buf
*, stable_how4
*, cred_t
*, bool_t
);
152 static int nfs4_getapage(vnode_t
*, uoff_t
, size_t, uint_t
*,
153 page_t
*[], size_t, struct seg
*, caddr_t
,
154 enum seg_rw
, cred_t
*);
155 static void nfs4_readahead(vnode_t
*, uoff_t
, caddr_t
, struct seg
*,
157 static int nfs4_sync_putapage(vnode_t
*, page_t
*, uoff_t
, size_t,
159 static int nfs4_sync_pageio(vnode_t
*, page_t
*, uoff_t
, size_t,
161 static int nfs4_commit(vnode_t
*, offset4
, count4
, cred_t
*);
162 static void nfs4_set_mod(vnode_t
*);
163 static void nfs4_get_commit(vnode_t
*);
164 static void nfs4_get_commit_range(vnode_t
*, uoff_t
, size_t);
165 static int nfs4_putpage_commit(vnode_t
*, offset_t
, size_t, cred_t
*);
166 static int nfs4_commit_vp(vnode_t
*, uoff_t
, size_t, cred_t
*, int);
167 static int nfs4_sync_commit(vnode_t
*, page_t
*, offset3
, count3
,
169 static void do_nfs4_async_commit(vnode_t
*, page_t
*, offset3
, count3
,
171 static int nfs4_update_attrcache(nfsstat4
, nfs4_ga_res_t
*,
172 hrtime_t
, vnode_t
*, cred_t
*);
173 static int nfs4_open_non_reg_file(vnode_t
**, int, cred_t
*);
174 static int nfs4_safelock(vnode_t
*, const struct flock64
*, cred_t
*);
175 static void nfs4_register_lock_locally(vnode_t
*, struct flock64
*, int,
177 static int nfs4_lockrelease(vnode_t
*, int, offset_t
, cred_t
*);
178 static int nfs4_block_and_wait(clock_t *, rnode4_t
*);
179 static cred_t
*state_to_cred(nfs4_open_stream_t
*);
180 static void denied_to_flk(LOCK4denied
*, flock64_t
*, LOCKT4args
*);
181 static pid_t
lo_to_pid(lock_owner4
*);
182 static void nfs4_reinstitute_local_lock_state(vnode_t
*, flock64_t
*,
183 cred_t
*, nfs4_lock_owner_t
*);
184 static void push_reinstate(vnode_t
*, int, flock64_t
*, cred_t
*,
185 nfs4_lock_owner_t
*);
186 static int open_and_get_osp(vnode_t
*, cred_t
*, nfs4_open_stream_t
**);
187 static void nfs4_delmap_callback(struct as
*, void *, uint_t
);
188 static void nfs4_free_delmapcall(nfs4_delmapcall_t
*);
189 static nfs4_delmapcall_t
*nfs4_init_delmapcall();
190 static int nfs4_find_and_delete_delmapcall(rnode4_t
*, int *);
191 static int nfs4_is_acl_mask_valid(uint_t
, nfs4_acl_op_t
);
192 static int nfs4_create_getsecattr_return(vsecattr_t
*, vsecattr_t
*,
196 * Routines that implement the setting of v4 args for the misc. ops
198 static void nfs4args_lock_free(nfs_argop4
*);
199 static void nfs4args_lockt_free(nfs_argop4
*);
200 static void nfs4args_setattr(nfs_argop4
*, vattr_t
*, vsecattr_t
*,
201 int, rnode4_t
*, cred_t
*, bitmap4
, int *,
202 nfs4_stateid_types_t
*);
203 static void nfs4args_setattr_free(nfs_argop4
*);
204 static int nfs4args_verify(nfs_argop4
*, vattr_t
*, enum nfs_opnum4
,
206 static void nfs4args_verify_free(nfs_argop4
*);
207 static void nfs4args_write(nfs_argop4
*, stable_how4
, rnode4_t
*, cred_t
*,
208 WRITE4args
**, nfs4_stateid_types_t
*);
211 * These are the vnode ops functions that implement the vnode interface to
212 * the networked file system. See more comments below at nfs4_vnodeops.
214 static int nfs4_open(vnode_t
**, int, cred_t
*, caller_context_t
*);
215 static int nfs4_close(vnode_t
*, int, int, offset_t
, cred_t
*,
217 static int nfs4_read(vnode_t
*, struct uio
*, int, cred_t
*,
219 static int nfs4_write(vnode_t
*, struct uio
*, int, cred_t
*,
221 static int nfs4_ioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *,
223 static int nfs4_setattr(vnode_t
*, struct vattr
*, int, cred_t
*,
225 static int nfs4_access(vnode_t
*, int, int, cred_t
*, caller_context_t
*);
226 static int nfs4_readlink(vnode_t
*, struct uio
*, cred_t
*,
228 static int nfs4_fsync(vnode_t
*, int, cred_t
*, caller_context_t
*);
229 static int nfs4_create(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
230 int, vnode_t
**, cred_t
*, int, caller_context_t
*,
232 static int nfs4_remove(vnode_t
*, char *, cred_t
*, caller_context_t
*,
234 static int nfs4_link(vnode_t
*, vnode_t
*, char *, cred_t
*,
235 caller_context_t
*, int);
236 static int nfs4_rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
237 caller_context_t
*, int);
238 static int nfs4_mkdir(vnode_t
*, char *, struct vattr
*, vnode_t
**,
239 cred_t
*, caller_context_t
*, int, vsecattr_t
*);
240 static int nfs4_rmdir(vnode_t
*, char *, vnode_t
*, cred_t
*,
241 caller_context_t
*, int);
242 static int nfs4_symlink(vnode_t
*, char *, struct vattr
*, char *,
243 cred_t
*, caller_context_t
*, int);
244 static int nfs4_readdir(vnode_t
*, struct uio
*, cred_t
*, int *,
245 caller_context_t
*, int);
246 static int nfs4_seek(vnode_t
*, offset_t
, offset_t
*, caller_context_t
*);
247 static int nfs4_getpage(vnode_t
*, offset_t
, size_t, uint_t
*,
248 page_t
*[], size_t, struct seg
*, caddr_t
,
249 enum seg_rw
, cred_t
*, caller_context_t
*);
250 static int nfs4_putpage(vnode_t
*, offset_t
, size_t, int, cred_t
*,
252 static int nfs4_map(vnode_t
*, offset_t
, struct as
*, caddr_t
*, size_t,
253 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
254 static int nfs4_addmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
255 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
256 static int nfs4_cmp(vnode_t
*, vnode_t
*, caller_context_t
*);
257 static int nfs4_frlock(vnode_t
*, int, struct flock64
*, int, offset_t
,
258 struct flk_callback
*, cred_t
*, caller_context_t
*);
259 static int nfs4_space(vnode_t
*, int, struct flock64
*, int, offset_t
,
260 cred_t
*, caller_context_t
*);
261 static int nfs4_delmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
262 uint_t
, uint_t
, uint_t
, cred_t
*, caller_context_t
*);
263 static int nfs4_pageio(vnode_t
*, page_t
*, uoff_t
, size_t, int,
264 cred_t
*, caller_context_t
*);
265 static void nfs4_dispose(vnode_t
*, page_t
*, int, int, cred_t
*,
267 static int nfs4_setsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
270 * These vnode ops are required to be called from outside this source file,
271 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
274 int nfs4_getattr(vnode_t
*, struct vattr
*, int, cred_t
*,
276 void nfs4_inactive(vnode_t
*, cred_t
*, caller_context_t
*);
277 int nfs4_lookup(vnode_t
*, char *, vnode_t
**,
278 struct pathname
*, int, vnode_t
*, cred_t
*,
279 caller_context_t
*, int *, pathname_t
*);
280 int nfs4_fid(vnode_t
*, fid_t
*, caller_context_t
*);
281 int nfs4_rwlock(vnode_t
*, int, caller_context_t
*);
282 void nfs4_rwunlock(vnode_t
*, int, caller_context_t
*);
283 int nfs4_realvp(vnode_t
*, vnode_t
**, caller_context_t
*);
284 int nfs4_pathconf(vnode_t
*, int, ulong_t
*, cred_t
*,
286 int nfs4_getsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
288 int nfs4_shrlock(vnode_t
*, int, struct shrlock
*, int, cred_t
*,
292 * Used for nfs4_commit_vp() to indicate if we should
293 * wait on pending writes.
295 #define NFS4_WRITE_NOWAIT 0
296 #define NFS4_WRITE_WAIT 1
298 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
301 * Error flags used to pass information about certain special errors
302 * which need to be handled specially.
305 #define NFS_VERF_MISMATCH -97
308 * Flags used to differentiate between which operation drove the
309 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
311 #define NFS4_CLOSE_OP 0x1
312 #define NFS4_DELMAP_OP 0x2
313 #define NFS4_INACTIVE_OP 0x3
315 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
317 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
318 #define ALIGN64(x, ptr, sz) \
319 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
321 x = sizeof (uint64_t) - (x); \
327 int nfs4_client_attr_debug
= 0;
328 int nfs4_client_state_debug
= 0;
329 int nfs4_client_shadow_debug
= 0;
330 int nfs4_client_lock_debug
= 0;
331 int nfs4_seqid_sync
= 0;
332 int nfs4_client_map_debug
= 0;
333 static int nfs4_pageio_debug
= 0;
334 int nfs4_client_inactive_debug
= 0;
335 int nfs4_client_recov_debug
= 0;
336 int nfs4_client_failover_debug
= 0;
337 int nfs4_client_call_debug
= 0;
338 int nfs4_client_lookup_debug
= 0;
339 int nfs4_client_zone_debug
= 0;
340 int nfs4_lost_rqst_debug
= 0;
341 int nfs4_rdattrerr_debug
= 0;
342 int nfs4_open_stream_debug
= 0;
344 int nfs4read_error_inject
;
346 static int nfs4_create_misses
= 0;
348 static int nfs4_readdir_cache_shorts
= 0;
349 static int nfs4_readdir_readahead
= 0;
351 static int nfs4_bio_do_stop
= 0;
353 static int nfs4_lostpage
= 0; /* number of times we lost original page */
355 int nfs4_mmap_debug
= 0;
357 static int nfs4_pathconf_cache_hits
= 0;
358 static int nfs4_pathconf_cache_misses
= 0;
360 int nfs4close_all_cnt
;
361 int nfs4close_one_debug
= 0;
362 int nfs4close_notw_debug
= 0;
364 int denied_to_flk_debug
= 0;
365 void *lockt_denied_debug
;
370 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
371 * or NFS4ERR_RESOURCE.
373 static int confirm_retry_sec
= 30;
375 static int nfs4_lookup_neg_cache
= 1;
378 * number of pages to read ahead
379 * optimized for 100 base-T.
381 static int nfs4_nra
= 4;
383 static int nfs4_do_symlink_cache
= 1;
385 static int nfs4_pathconf_disable_cache
= 0;
388 * These are the vnode ops routines which implement the vnode interface to
389 * the networked file system. These routines just take their parameters,
390 * make them look networkish by putting the right info into interface structs,
391 * and then calling the appropriate remote routine(s) to do the work.
393 * Note on directory name lookup cacheing: If we detect a stale fhandle,
394 * we purge the directory cache relative to that vnode. This way, the
395 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
396 * more details on rnode locking.
399 const struct vnodeops nfs4_vnodeops
= {
401 .vop_open
= nfs4_open
,
402 .vop_close
= nfs4_close
,
403 .vop_read
= nfs4_read
,
404 .vop_write
= nfs4_write
,
405 .vop_ioctl
= nfs4_ioctl
,
406 .vop_getattr
= nfs4_getattr
,
407 .vop_setattr
= nfs4_setattr
,
408 .vop_access
= nfs4_access
,
409 .vop_lookup
= nfs4_lookup
,
410 .vop_create
= nfs4_create
,
411 .vop_remove
= nfs4_remove
,
412 .vop_link
= nfs4_link
,
413 .vop_rename
= nfs4_rename
,
414 .vop_mkdir
= nfs4_mkdir
,
415 .vop_rmdir
= nfs4_rmdir
,
416 .vop_readdir
= nfs4_readdir
,
417 .vop_symlink
= nfs4_symlink
,
418 .vop_readlink
= nfs4_readlink
,
419 .vop_fsync
= nfs4_fsync
,
420 .vop_inactive
= nfs4_inactive
,
422 .vop_rwlock
= nfs4_rwlock
,
423 .vop_rwunlock
= nfs4_rwunlock
,
424 .vop_seek
= nfs4_seek
,
425 .vop_frlock
= nfs4_frlock
,
426 .vop_space
= nfs4_space
,
427 .vop_realvp
= nfs4_realvp
,
428 .vop_getpage
= nfs4_getpage
,
429 .vop_putpage
= nfs4_putpage
,
431 .vop_addmap
= nfs4_addmap
,
432 .vop_delmap
= nfs4_delmap
,
433 /* no separate nfs4_dump */
434 .vop_dump
= nfs_dump
,
435 .vop_pathconf
= nfs4_pathconf
,
436 .vop_pageio
= nfs4_pageio
,
437 .vop_dispose
= nfs4_dispose
,
438 .vop_setsecattr
= nfs4_setsecattr
,
439 .vop_getsecattr
= nfs4_getsecattr
,
440 .vop_shrlock
= nfs4_shrlock
,
441 .vop_vnevent
= fs_vnevent_support
,
445 * The following are subroutines and definitions to set args or get res
446 * for the different nfsv4 ops
450 nfs4args_lookup_free(nfs_argop4
*argop
, int arglen
)
454 for (i
= 0; i
< arglen
; i
++) {
455 if (argop
[i
].argop
== OP_LOOKUP
) {
457 argop
[i
].nfs_argop4_u
.oplookup
.
458 objname
.utf8string_val
,
459 argop
[i
].nfs_argop4_u
.oplookup
.
460 objname
.utf8string_len
);
466 nfs4args_lock_free(nfs_argop4
*argop
)
468 locker4
*locker
= &argop
->nfs_argop4_u
.oplock
.locker
;
470 if (locker
->new_lock_owner
== TRUE
) {
471 open_to_lock_owner4
*open_owner
;
473 open_owner
= &locker
->locker4_u
.open_owner
;
474 if (open_owner
->lock_owner
.owner_val
!= NULL
) {
475 kmem_free(open_owner
->lock_owner
.owner_val
,
476 open_owner
->lock_owner
.owner_len
);
482 nfs4args_lockt_free(nfs_argop4
*argop
)
484 lock_owner4
*lowner
= &argop
->nfs_argop4_u
.oplockt
.owner
;
486 if (lowner
->owner_val
!= NULL
) {
487 kmem_free(lowner
->owner_val
, lowner
->owner_len
);
492 nfs4args_setattr(nfs_argop4
*argop
, vattr_t
*vap
, vsecattr_t
*vsap
, int flags
,
493 rnode4_t
*rp
, cred_t
*cr
, bitmap4 supp
, int *error
,
494 nfs4_stateid_types_t
*sid_types
)
496 fattr4
*attr
= &argop
->nfs_argop4_u
.opsetattr
.obj_attributes
;
499 argop
->argop
= OP_SETATTR
;
501 * The stateid is set to 0 if client is not modifying the size
502 * and otherwise to whatever nfs4_get_stateid() returns.
504 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
505 * state struct could be found for the process/file pair. We may
506 * want to change this in the future (by OPENing the file). See
509 if (vap
->va_mask
& VATTR_SIZE
) {
512 mi
= VTOMI4(RTOV4(rp
));
514 argop
->nfs_argop4_u
.opsetattr
.stateid
=
515 nfs4_get_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
, mi
,
516 OP_SETATTR
, sid_types
, FALSE
);
518 bzero(&argop
->nfs_argop4_u
.opsetattr
.stateid
,
522 *error
= vattr_to_fattr4(vap
, vsap
, attr
, flags
, OP_SETATTR
, supp
);
524 bzero(attr
, sizeof (*attr
));
528 nfs4args_setattr_free(nfs_argop4
*argop
)
530 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opsetattr
.obj_attributes
);
534 nfs4args_verify(nfs_argop4
*argop
, vattr_t
*vap
, enum nfs_opnum4 op
,
543 attr
= &argop
->nfs_argop4_u
.opverify
.obj_attributes
;
546 attr
= &argop
->nfs_argop4_u
.opnverify
.obj_attributes
;
552 error
= vattr_to_fattr4(vap
, NULL
, attr
, 0, op
, supp
);
554 bzero(attr
, sizeof (*attr
));
559 nfs4args_verify_free(nfs_argop4
*argop
)
561 switch (argop
->argop
) {
563 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opverify
.obj_attributes
);
566 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opnverify
.obj_attributes
);
574 nfs4args_write(nfs_argop4
*argop
, stable_how4 stable
, rnode4_t
*rp
, cred_t
*cr
,
575 WRITE4args
**wargs_pp
, nfs4_stateid_types_t
*sid_tp
)
577 WRITE4args
*wargs
= &argop
->nfs_argop4_u
.opwrite
;
578 mntinfo4_t
*mi
= VTOMI4(RTOV4(rp
));
580 argop
->argop
= OP_WRITE
;
581 wargs
->stable
= stable
;
582 wargs
->stateid
= nfs4_get_w_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
,
583 mi
, OP_WRITE
, sid_tp
);
589 nfs4args_copen_free(OPEN4cargs
*open_args
)
591 if (open_args
->owner
.owner_val
) {
592 kmem_free(open_args
->owner
.owner_val
,
593 open_args
->owner
.owner_len
);
595 if ((open_args
->opentype
== OPEN4_CREATE
) &&
596 (open_args
->mode
!= EXCLUSIVE4
)) {
597 nfs4_fattr4_free(&open_args
->createhow4_u
.createattrs
);
602 * XXX: This is referenced in modstubs.s
604 const struct vnodeops
*
605 nfs4_getvnodeops(void)
607 return (&nfs4_vnodeops
);
611 * The OPEN operation opens a regular file.
615 nfs4_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
620 int just_been_created
;
623 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4_open: "));
624 if (nfs_zone() != VTOMI4(*vpp
)->mi_zone
)
629 * Check to see if opening something besides a regular file;
630 * if so skip the OTW call
632 if ((*vpp
)->v_type
!= VREG
) {
633 error
= nfs4_open_non_reg_file(vpp
, flag
, cr
);
638 * XXX - would like a check right here to know if the file is
639 * executable or not, so as to skip OTW
642 if ((error
= vtodv(*vpp
, &dvp
, cr
, TRUE
)) != 0)
646 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
649 if ((error
= vtoname(*vpp
, fn
, MAXNAMELEN
)) != 0) {
650 nfs_rw_exit(&drp
->r_rwlock
);
655 * See if this file has just been CREATEd.
656 * If so, clear the flag and update the dnlc, which was previously
657 * skipped in nfs4_create.
658 * XXX need better serilization on this.
659 * XXX move this into the nf4open_otw call, after we have
660 * XXX acquired the open owner seqid sync.
662 mutex_enter(&rp
->r_statev4_lock
);
663 if (rp
->created_v4
) {
665 mutex_exit(&rp
->r_statev4_lock
);
667 dnlc_update(dvp
, fn
, *vpp
);
668 /* This is needed so we don't bump the open ref count */
669 just_been_created
= 1;
671 mutex_exit(&rp
->r_statev4_lock
);
672 just_been_created
= 0;
676 * If caller specified O_TRUNC/FTRUNC, then be sure to set
677 * FWRITE (to drive successful setattr(size=0) after open)
682 error
= nfs4open_otw(dvp
, fn
, NULL
, vpp
, cr
, 0, flag
, 0,
685 if (!error
&& !((*vpp
)->v_flag
& VROOT
))
686 dnlc_update(dvp
, fn
, *vpp
);
688 nfs_rw_exit(&drp
->r_rwlock
);
690 /* release the hold from vtodv */
693 /* exchange the shadow for the master vnode, if needed */
695 if (error
== 0 && IS_SHADOW(*vpp
, rp
))
702 * See if there's a "lost open" request to be saved and recovered.
705 nfs4open_save_lost_rqst(int error
, nfs4_lost_rqst_t
*lost_rqstp
,
706 nfs4_open_owner_t
*oop
, cred_t
*cr
, vnode_t
*vp
,
707 vnode_t
*dvp
, OPEN4cargs
*open_args
)
712 vfsp
= (dvp
? dvp
->v_vfsp
: vp
->v_vfsp
);
714 if (error
!= ETIMEDOUT
&& error
!= EINTR
&&
715 !NFS4_FRC_UNMT_ERR(error
, vfsp
)) {
716 lost_rqstp
->lr_op
= 0;
720 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
721 "nfs4open_save_lost_rqst: error %d", error
));
723 lost_rqstp
->lr_op
= OP_OPEN
;
726 * The vp (if it is not NULL) and dvp are held and rele'd via
727 * the recovery code. See nfs4_save_lost_rqst.
729 lost_rqstp
->lr_vp
= vp
;
730 lost_rqstp
->lr_dvp
= dvp
;
731 lost_rqstp
->lr_oop
= oop
;
732 lost_rqstp
->lr_osp
= NULL
;
733 lost_rqstp
->lr_lop
= NULL
;
734 lost_rqstp
->lr_cr
= cr
;
735 lost_rqstp
->lr_flk
= NULL
;
736 lost_rqstp
->lr_oacc
= open_args
->share_access
;
737 lost_rqstp
->lr_odeny
= open_args
->share_deny
;
738 lost_rqstp
->lr_oclaim
= open_args
->claim
;
739 if (open_args
->claim
== CLAIM_DELEGATE_CUR
) {
740 lost_rqstp
->lr_ostateid
=
741 open_args
->open_claim4_u
.delegate_cur_info
.delegate_stateid
;
742 srccfp
= open_args
->open_claim4_u
.delegate_cur_info
.cfile
;
744 srccfp
= open_args
->open_claim4_u
.cfile
;
746 lost_rqstp
->lr_ofile
.utf8string_len
= 0;
747 lost_rqstp
->lr_ofile
.utf8string_val
= NULL
;
748 (void) str_to_utf8(srccfp
, &lost_rqstp
->lr_ofile
);
749 lost_rqstp
->lr_putfirst
= FALSE
;
752 struct nfs4_excl_time
{
758 * The OPEN operation creates and/or opens a regular file
763 nfs4open_otw(vnode_t
*dvp
, char *file_name
, struct vattr
*in_va
,
764 vnode_t
**vpp
, cred_t
*cr
, int create_flag
, int open_flag
,
765 enum createmode4 createmode
, int file_just_been_created
)
768 rnode4_t
*drp
= VTOR4(dvp
);
771 bool_t needrecov
= FALSE
;
775 COMPOUND4args_clnt args
;
776 COMPOUND4res_clnt res
;
780 int idx_open
, idx_fattr
;
782 GETFH4res
*gf_res
= NULL
;
783 OPEN4res
*op_res
= NULL
;
786 struct nfs4_excl_time verf
;
787 bool_t did_excl_setup
= FALSE
;
790 OPEN4cargs
*open_args
;
791 nfs4_open_owner_t
*oop
= NULL
;
792 nfs4_open_stream_t
*osp
= NULL
;
794 bool_t retry_open
= FALSE
;
795 nfs4_recov_state_t recov_state
;
796 nfs4_lost_rqst_t lost_rqst
;
797 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
800 cred_t
*cred_otw
= NULL
; /* cred used to do the RPC call */
803 nfs4_sharedfh_t
*otw_sfh
;
804 nfs4_sharedfh_t
*orig_sfh
;
806 int numops
, setgid_flag
;
807 int num_bseqid_retry
= NFS4_NUM_RETRY_BAD_SEQID
+ 1;
810 * Make sure we properly deal with setting the right gid on
811 * a newly created file to reflect the parent's setgid bit
814 if (create_flag
&& in_va
) {
817 * If there is grpid mount flag used or
818 * the parent's directory has the setgid bit set
819 * _and_ the client was able to get a valid mapping
820 * for the parent dir's owner_group, we want to
821 * append NVERIFY(owner_group == dva.va_gid) and
822 * SETATTR to the CREATE compound.
824 mutex_enter(&drp
->r_statelock
);
825 if ((VTOMI4(dvp
)->mi_flags
& MI4_GRPID
||
826 drp
->r_attr
.va_mode
& VSGID
) &&
827 drp
->r_attr
.va_gid
!= GID_NOBODY
) {
828 in_va
->va_mask
|= VATTR_GID
;
829 in_va
->va_gid
= drp
->r_attr
.va_gid
;
832 mutex_exit(&drp
->r_statelock
);
836 * Normal/non-create compound:
837 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
839 * Open(create) compound no setgid:
840 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
841 * RESTOREFH + GETATTR
843 * Open(create) setgid:
844 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
845 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
846 * NVERIFY(grp) + SETATTR
852 } else if (create_flag
) {
862 args
.array_len
= numops
;
863 argoplist_size
= numops
* sizeof (nfs_argop4
);
864 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
866 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4open_otw: "
867 "open %s open flag 0x%x cred %p", file_name
, open_flag
,
870 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
873 * We are to create a file. Initialize the passed in vnode
879 * Check to see if the client owns a read delegation and is
880 * trying to open for write. If so, then return the delegation
881 * to avoid the server doing a cb_recall and returning DELAY.
882 * NB - we don't use the statev4_lock here because we'd have
883 * to drop the lock anyway and the result would be stale.
885 if ((open_flag
& FWRITE
) &&
886 VTOR4(vpi
)->r_deleg_type
== OPEN_DELEGATE_READ
)
887 (void) nfs4delegreturn(VTOR4(vpi
), NFS4_DR_REOPEN
);
890 * If the file has a delegation, then do an access check up
891 * front. This avoids having to an access check later after
892 * we've already done start_op, which could deadlock.
894 if (VTOR4(vpi
)->r_deleg_type
!= OPEN_DELEGATE_NONE
) {
895 if (open_flag
& FREAD
&&
896 nfs4_access(vpi
, VREAD
, 0, cr
, NULL
) == 0)
898 if (open_flag
& FWRITE
&&
899 nfs4_access(vpi
, VWRITE
, 0, cr
, NULL
) == 0)
906 recov_state
.rs_flags
= 0;
907 recov_state
.rs_num_retry_despite_err
= 0;
912 nfs4_error_zinit(&e
);
914 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
);
918 kmem_free(argop
, argoplist_size
);
922 args
.ctag
= TAG_OPEN
;
923 args
.array_len
= numops
;
926 /* putfh directory fh */
927 argop
[0].argop
= OP_CPUTFH
;
928 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
930 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
931 argop
[idx_open
].argop
= OP_COPEN
;
932 open_args
= &argop
[idx_open
].nfs_argop4_u
.opcopen
;
933 open_args
->claim
= CLAIM_NULL
;
936 open_args
->open_claim4_u
.cfile
= file_name
;
937 open_args
->owner
.owner_len
= 0;
938 open_args
->owner
.owner_val
= NULL
;
942 open_args
->opentype
= OPEN4_CREATE
;
943 open_args
->mode
= createmode
;
944 if (createmode
== EXCLUSIVE4
) {
945 if (did_excl_setup
== FALSE
) {
946 verf
.seconds
= zone_get_hostid(NULL
);
947 if (verf
.seconds
!= 0)
948 verf
.nseconds
= newnum();
953 verf
.seconds
= now
.tv_sec
;
954 verf
.nseconds
= now
.tv_nsec
;
957 * Since the server will use this value for the
958 * mtime, make sure that it can't overflow. Zero
959 * out the MSB. The actual value does not matter
960 * here, only its uniqeness.
962 verf
.seconds
&= INT32_MAX
;
963 did_excl_setup
= TRUE
;
966 /* Now copy over verifier to OPEN4args. */
967 open_args
->createhow4_u
.createverf
= *(uint64_t *)&verf
;
973 attr
= &open_args
->createhow4_u
.createattrs
;
976 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
977 supp_attrs
= svp
->sv_supp_attrs
;
978 nfs_rw_exit(&svp
->sv_lock
);
980 /* GUARDED4 or UNCHECKED4 */
981 v_error
= vattr_to_fattr4(in_va
, NULL
, attr
, 0, OP_OPEN
,
984 bzero(attr
, sizeof (*attr
));
985 nfs4args_copen_free(open_args
);
986 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
,
987 &recov_state
, FALSE
);
990 kmem_free(argop
, argoplist_size
);
996 open_args
->opentype
= OPEN4_NOCREATE
;
999 if (recov_state
.rs_sp
!= NULL
) {
1000 mutex_enter(&recov_state
.rs_sp
->s_lock
);
1001 open_args
->owner
.clientid
= recov_state
.rs_sp
->clientid
;
1002 mutex_exit(&recov_state
.rs_sp
->s_lock
);
1004 /* XXX should we just fail here? */
1005 open_args
->owner
.clientid
= 0;
1009 * This increments oop's ref count or creates a temporary 'just_created'
1010 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1013 mutex_enter(&VTOMI4(dvp
)->mi_lock
);
1015 /* See if a permanent or just created open owner exists */
1016 oop
= find_open_owner_nolock(cr
, NFS4_JUST_CREATED
, VTOMI4(dvp
));
1019 * This open owner does not exist so create a temporary
1022 oop
= create_open_owner(cr
, VTOMI4(dvp
));
1023 ASSERT(oop
!= NULL
);
1025 mutex_exit(&VTOMI4(dvp
)->mi_lock
);
1027 /* this length never changes, do alloc before seqid sync */
1028 open_args
->owner
.owner_len
= sizeof (oop
->oo_name
);
1029 open_args
->owner
.owner_val
=
1030 kmem_alloc(open_args
->owner
.owner_len
, KM_SLEEP
);
1032 e
.error
= nfs4_start_open_seqid_sync(oop
, VTOMI4(dvp
));
1033 if (e
.error
== EAGAIN
) {
1034 open_owner_rele(oop
);
1035 nfs4args_copen_free(open_args
);
1036 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, TRUE
);
1044 /* Check to see if we need to do the OTW call */
1046 if (!nfs4_is_otw_open_necessary(oop
, open_flag
, vpi
,
1047 file_just_been_created
, &e
.error
, acc
, &recov_state
)) {
1050 * The OTW open is not necessary. Either
1051 * the open can succeed without it (eg.
1052 * delegation, error == 0) or the open
1053 * must fail due to an access failure
1054 * (error != 0). In either case, tidy
1058 nfs4_end_open_seqid_sync(oop
);
1059 open_owner_rele(oop
);
1060 nfs4args_copen_free(open_args
);
1061 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, FALSE
);
1064 kmem_free(argop
, argoplist_size
);
1069 bcopy(&oop
->oo_name
, open_args
->owner
.owner_val
,
1070 open_args
->owner
.owner_len
);
1072 seqid
= nfs4_get_open_seqid(oop
) + 1;
1073 open_args
->seqid
= seqid
;
1074 open_args
->share_access
= 0;
1075 if (open_flag
& FREAD
)
1076 open_args
->share_access
|= OPEN4_SHARE_ACCESS_READ
;
1077 if (open_flag
& FWRITE
)
1078 open_args
->share_access
|= OPEN4_SHARE_ACCESS_WRITE
;
1079 open_args
->share_deny
= OPEN4_SHARE_DENY_NONE
;
1084 * getfh w/sanity check for idx_open/idx_fattr
1086 ASSERT((idx_open
+ 1) == (idx_fattr
- 1));
1087 argop
[idx_open
+ 1].argop
= OP_GETFH
;
1090 argop
[idx_fattr
].argop
= OP_GETATTR
;
1091 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1092 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1099 svp
= drp
->r_server
;
1100 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
1101 supp_attrs
= svp
->sv_supp_attrs
;
1102 nfs_rw_exit(&svp
->sv_lock
);
1105 * For setgid case, we need to:
1106 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1108 argop
[4].argop
= OP_SAVEFH
;
1110 argop
[5].argop
= OP_CPUTFH
;
1111 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
1113 argop
[6].argop
= OP_GETATTR
;
1114 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1115 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1117 argop
[7].argop
= OP_RESTOREFH
;
1122 _v
.va_mask
= VATTR_GID
;
1123 _v
.va_gid
= in_va
->va_gid
;
1124 if (!(e
.error
= nfs4args_verify(&argop
[8], &_v
, OP_NVERIFY
,
1130 * We _know_ we're not messing with VATTR_SIZE or
1131 * VATTR_XTIME, so no need for stateid or flags.
1132 * Also we specify NULL rp since we're only
1133 * interested in setting owner_group attributes.
1135 nfs4args_setattr(&argop
[9], &_v
, NULL
, 0, NULL
, cr
,
1136 supp_attrs
, &e
.error
, 0);
1138 nfs4args_verify_free(&argop
[8]);
1143 * XXX - Revisit the last argument to nfs4_end_op()
1144 * once 5020486 is fixed.
1146 nfs4_end_open_seqid_sync(oop
);
1147 open_owner_rele(oop
);
1148 nfs4args_copen_free(open_args
);
1149 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, TRUE
);
1152 kmem_free(argop
, argoplist_size
);
1155 } else if (create_flag
) {
1156 argop
[1].argop
= OP_SAVEFH
;
1158 argop
[5].argop
= OP_RESTOREFH
;
1160 argop
[6].argop
= OP_GETATTR
;
1161 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1162 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1165 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
1166 "nfs4open_otw: %s call, nm %s, rp %s",
1167 needrecov
? "recov" : "first", file_name
,
1168 rnode4info(VTOR4(dvp
))));
1172 rfs4call(VTOMI4(dvp
), &args
, &res
, cred_otw
, &doqueue
, 0, &e
);
1174 if (!e
.error
&& nfs4_need_to_bump_seqid(&res
))
1175 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
1177 needrecov
= nfs4_needs_recovery(&e
, TRUE
, dvp
->v_vfsp
);
1179 if (e
.error
|| needrecov
) {
1180 bool_t abort
= FALSE
;
1183 nfs4_bseqid_entry_t
*bsep
= NULL
;
1185 nfs4open_save_lost_rqst(e
.error
, &lost_rqst
, oop
,
1186 cred_otw
, vpi
, dvp
, open_args
);
1188 if (!e
.error
&& res
.status
== NFS4ERR_BAD_SEQID
) {
1189 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
1190 vpi
, 0, args
.ctag
, open_args
->seqid
);
1194 abort
= nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
, vpi
,
1195 NULL
, lost_rqst
.lr_op
== OP_OPEN
?
1196 &lost_rqst
: NULL
, OP_OPEN
, bsep
, NULL
, NULL
);
1199 kmem_free(bsep
, sizeof (*bsep
));
1200 /* give up if we keep getting BAD_SEQID */
1201 if (num_bseqid_retry
== 0)
1203 if (abort
== TRUE
&& e
.error
== 0)
1204 e
.error
= geterrno4(res
.status
);
1206 nfs4_end_open_seqid_sync(oop
);
1207 open_owner_rele(oop
);
1208 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1209 nfs4args_copen_free(open_args
);
1211 nfs4args_verify_free(&argop
[8]);
1212 nfs4args_setattr_free(&argop
[9]);
1215 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1220 if (!needrecov
|| abort
== TRUE
|| e
.error
== EINTR
||
1221 NFS4_FRC_UNMT_ERR(e
.error
, dvp
->v_vfsp
)) {
1222 kmem_free(argop
, argoplist_size
);
1229 * Will check and update lease after checking the rflag for
1230 * OPEN_CONFIRM in the successful OPEN call.
1232 if (res
.status
!= NFS4_OK
&& res
.array_len
<= idx_fattr
+ 1) {
1235 * XXX what if we're crossing mount points from server1:/drp
1236 * to server2:/drp/rp.
1239 /* Signal our end of use of the open seqid */
1240 nfs4_end_open_seqid_sync(oop
);
1243 * This will destroy the open owner if it was just created,
1244 * and no one else has put a reference on it.
1246 open_owner_rele(oop
);
1247 if (create_flag
&& (createmode
!= EXCLUSIVE4
) &&
1248 res
.status
== NFS4ERR_BADOWNER
)
1249 nfs4_log_badowner(VTOMI4(dvp
), OP_OPEN
);
1251 e
.error
= geterrno4(res
.status
);
1252 nfs4args_copen_free(open_args
);
1254 nfs4args_verify_free(&argop
[8]);
1255 nfs4args_setattr_free(&argop
[9]);
1257 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1258 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1260 * If the reply is NFS4ERR_ACCESS, it may be because
1261 * we are root (no root net access). If the real uid
1262 * is not root, then retry with the real uid instead.
1268 if (res
.status
== NFS4ERR_ACCESS
&&
1269 (ncr
= crnetadjust(cred_otw
)) != NULL
) {
1273 kmem_free(argop
, argoplist_size
);
1277 resop
= &res
.array
[idx_open
]; /* open res */
1278 op_res
= &resop
->nfs_resop4_u
.opopen
;
1282 * verify attrset bitmap
1285 (createmode
== UNCHECKED4
|| createmode
== GUARDED4
)) {
1286 /* make sure attrset returned is what we asked for */
1287 /* XXX Ignore this 'error' for now */
1288 if (attr
->attrmask
!= op_res
->attrset
)
1293 if (op_res
->rflags
& OPEN4_RESULT_LOCKTYPE_POSIX
) {
1294 mutex_enter(&VTOMI4(dvp
)->mi_lock
);
1295 VTOMI4(dvp
)->mi_flags
|= MI4_POSIX_LOCK
;
1296 mutex_exit(&VTOMI4(dvp
)->mi_lock
);
1299 resop
= &res
.array
[idx_open
+ 1]; /* getfh res */
1300 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
1302 otw_sfh
= sfh4_get(&gf_res
->object
, VTOMI4(dvp
));
1305 * The open stateid has been updated on the server but not
1306 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1307 * flush_pages->fop_putpage->...->nfs4write where we will issue an OTW
1308 * WRITE call. That, however, will use the old stateid, so go ahead
1309 * and upate the open stateid now, before any call to makenfs4node.
1312 nfs4_open_stream_t
*tmp_osp
;
1313 rnode4_t
*tmp_rp
= VTOR4(vpi
);
1315 tmp_osp
= find_open_stream(oop
, tmp_rp
);
1317 tmp_osp
->open_stateid
= op_res
->stateid
;
1318 mutex_exit(&tmp_osp
->os_sync_lock
);
1319 open_stream_rele(tmp_osp
, tmp_rp
);
1323 * We must determine if the file handle given by the otw open
1324 * is the same as the file handle which was passed in with
1325 * *vpp. This case can be reached if the file we are trying
1326 * to open has been removed and another file has been created
1327 * having the same file name. The passed in vnode is released
1330 orig_sfh
= VTOR4(vpi
)->r_fh
;
1331 fh_differs
= nfs4cmpfh(&orig_sfh
->sfh_fh
, &otw_sfh
->sfh_fh
);
1334 garp
= &res
.array
[idx_fattr
].nfs_resop4_u
.opgetattr
.ga_res
;
1336 if (create_flag
|| fh_differs
) {
1339 vp
= makenfs4node(otw_sfh
, garp
, dvp
->v_vfsp
, t
, cr
,
1340 dvp
, fn_get(VTOSV(dvp
)->sv_name
, file_name
, otw_sfh
));
1343 PURGE_ATTRCACHE4(vp
);
1345 * For the newly created vp case, make sure the rnode
1346 * isn't bad before using it.
1348 mutex_enter(&(VTOR4(vp
))->r_statelock
);
1349 if (VTOR4(vp
)->r_flags
& R4RECOVERR
)
1351 mutex_exit(&(VTOR4(vp
))->r_statelock
);
1354 nfs4_end_open_seqid_sync(oop
);
1355 nfs4args_copen_free(open_args
);
1357 nfs4args_verify_free(&argop
[8]);
1358 nfs4args_setattr_free(&argop
[9]);
1360 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1361 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
,
1363 open_owner_rele(oop
);
1367 sfh4_rele(&otw_sfh
);
1368 kmem_free(argop
, argoplist_size
);
1374 sfh4_rele(&otw_sfh
);
1377 * It seems odd to get a full set of attrs and then not update
1378 * the object's attrcache in the non-create case. Create case uses
1379 * the attrs since makenfs4node checks to see if the attrs need to
1380 * be updated (and then updates them). The non-create case should
1381 * update attrs also.
1383 if (! create_flag
&& ! fh_differs
&& !e
.error
) {
1384 nfs4_attr_cache(vp
, garp
, t
, cr
, TRUE
, NULL
);
1387 nfs4_error_zinit(&e
);
1388 if (op_res
->rflags
& OPEN4_RESULT_CONFIRM
) {
1389 /* This does not do recovery for vp explicitly. */
1390 nfs4open_confirm(vp
, &seqid
, &op_res
->stateid
, cred_otw
, FALSE
,
1391 &retry_open
, oop
, FALSE
, &e
, &num_bseqid_retry
);
1393 if (e
.error
|| e
.stat
) {
1394 nfs4_end_open_seqid_sync(oop
);
1395 nfs4args_copen_free(open_args
);
1397 nfs4args_verify_free(&argop
[8]);
1398 nfs4args_setattr_free(&argop
[9]);
1400 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1401 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
,
1403 open_owner_rele(oop
);
1404 if (create_flag
|| fh_differs
) {
1405 /* rele the makenfs4node */
1412 if (retry_open
== TRUE
) {
1413 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
1414 "nfs4open_otw: retry the open since OPEN "
1415 "CONFIRM failed with error %d stat %d",
1417 if (create_flag
&& createmode
== GUARDED4
) {
1418 NFS4_DEBUG(nfs4_client_recov_debug
,
1419 (CE_NOTE
, "nfs4open_otw: switch "
1420 "createmode from GUARDED4 to "
1422 createmode
= UNCHECKED4
;
1427 if (create_flag
&& (createmode
!= EXCLUSIVE4
) &&
1428 e
.stat
== NFS4ERR_BADOWNER
)
1429 nfs4_log_badowner(VTOMI4(dvp
), OP_OPEN
);
1431 e
.error
= geterrno4(e
.stat
);
1433 kmem_free(argop
, argoplist_size
);
1440 mutex_enter(&rp
->r_statev4_lock
);
1443 mutex_exit(&rp
->r_statev4_lock
);
1445 mutex_enter(&oop
->oo_lock
);
1446 /* Doesn't matter if 'oo_just_created' already was set as this */
1447 oop
->oo_just_created
= NFS4_PERM_CREATED
;
1448 if (oop
->oo_cred_otw
)
1449 crfree(oop
->oo_cred_otw
);
1450 oop
->oo_cred_otw
= cred_otw
;
1451 crhold(oop
->oo_cred_otw
);
1452 mutex_exit(&oop
->oo_lock
);
1454 /* returns with 'os_sync_lock' held */
1455 osp
= find_or_create_open_stream(oop
, rp
, &created_osp
);
1457 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
1458 "nfs4open_otw: failed to create an open stream"));
1459 NFS4_DEBUG(nfs4_seqid_sync
, (CE_NOTE
, "nfs4open_otw: "
1460 "signal our end of use of the open seqid"));
1462 nfs4_end_open_seqid_sync(oop
);
1463 open_owner_rele(oop
);
1464 nfs4args_copen_free(open_args
);
1466 nfs4args_verify_free(&argop
[8]);
1467 nfs4args_setattr_free(&argop
[9]);
1469 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1470 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1471 if (create_flag
|| fh_differs
)
1476 kmem_free(argop
, argoplist_size
);
1481 osp
->open_stateid
= op_res
->stateid
;
1483 if (open_flag
& FREAD
)
1484 osp
->os_share_acc_read
++;
1485 if (open_flag
& FWRITE
)
1486 osp
->os_share_acc_write
++;
1487 osp
->os_share_deny_none
++;
1490 * Need to reset this bitfield for the possible case where we were
1491 * going to OTW CLOSE the file, got a non-recoverable error, and before
1492 * we could retry the CLOSE, OPENed the file again.
1494 ASSERT(osp
->os_open_owner
->oo_seqid_inuse
);
1495 osp
->os_final_close
= 0;
1496 osp
->os_force_close
= 0;
1498 if (osp
->os_failed_reopen
)
1499 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
, "nfs4open_otw:"
1500 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1501 (void *)osp
, (void *)cr
, rnode4info(rp
)));
1503 osp
->os_failed_reopen
= 0;
1505 mutex_exit(&osp
->os_sync_lock
);
1507 nfs4_end_open_seqid_sync(oop
);
1509 if (created_osp
&& recov_state
.rs_sp
!= NULL
) {
1510 mutex_enter(&recov_state
.rs_sp
->s_lock
);
1511 nfs4_inc_state_ref_count_nolock(recov_state
.rs_sp
, VTOMI4(dvp
));
1512 mutex_exit(&recov_state
.rs_sp
->s_lock
);
1515 /* get rid of our reference to find oop */
1516 open_owner_rele(oop
);
1518 open_stream_rele(osp
, rp
);
1520 /* accept delegation, if any */
1521 nfs4_delegation_accept(rp
, CLAIM_NULL
, op_res
, garp
, cred_otw
);
1523 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1525 if (createmode
== EXCLUSIVE4
&&
1526 (in_va
->va_mask
& ~(VATTR_GID
| VATTR_SIZE
))) {
1527 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4open_otw:"
1528 " EXCLUSIVE4: sending a SETATTR"));
1530 * If doing an exclusive create, then generate
1531 * a SETATTR to set the initial attributes.
1532 * Try to set the mtime and the atime to the
1533 * server's current time. It is somewhat
1534 * expected that these fields will be used to
1535 * store the exclusive create cookie. If not,
1536 * server implementors will need to know that
1537 * a SETATTR will follow an exclusive create
1538 * and the cookie should be destroyed if
1541 * The VATTR_GID and VATTR_SIZE bits are turned off
1542 * so that the SETATTR request will not attempt
1543 * to process these. The gid will be set
1544 * separately if appropriate. The size is turned
1545 * off because it is assumed that a new file will
1546 * be created empty and if the file wasn't empty,
1547 * then the exclusive create will have failed
1548 * because the file must have existed already.
1549 * Therefore, no truncate operation is needed.
1551 in_va
->va_mask
&= ~(VATTR_GID
| VATTR_SIZE
);
1552 in_va
->va_mask
|= (VATTR_MTIME
| VATTR_ATIME
);
1554 e
.error
= nfs4setattr(vp
, in_va
, 0, cr
, NULL
);
1557 * Couldn't correct the attributes of
1558 * the newly created file and the
1559 * attributes are wrong. Remove the
1560 * file and return an error to the
1563 /* XXX will this take care of client state ? */
1564 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
1565 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1566 " remove file", e
.error
));
1568 (void) nfs4_remove(dvp
, file_name
, cr
, NULL
, 0);
1570 * Since we've reled the vnode and removed
1571 * the file we now need to return the error.
1572 * At this point we don't want to update the
1573 * dircaches, call nfs4_waitfor_purge_complete
1574 * or set vpp to vp so we need to skip these
1577 goto skip_update_dircaches
;
1582 * If we created or found the correct vnode, due to create_flag or
1583 * fh_differs being set, then update directory cache attribute, readdir
1586 if (create_flag
|| fh_differs
) {
1587 dirattr_info_t dinfo
, *dinfop
;
1590 * Make sure getattr succeeded before using results.
1591 * note: op 7 is getattr(dir) for both flavors of
1594 if (create_flag
&& res
.status
== NFS4_OK
) {
1595 dinfo
.di_time_call
= t
;
1598 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
1604 nfs4_update_dircaches(&op_res
->cinfo
, dvp
, vp
, file_name
,
1609 * If the page cache for this file was flushed from actions
1610 * above, it was done asynchronously and if that is true,
1611 * there is a need to wait here for it to complete. This must
1612 * be done outside of start_fop/end_fop.
1614 (void) nfs4_waitfor_purge_complete(vp
);
1617 * It is implicit that we are in the open case (create_flag == 0) since
1618 * fh_differs can only be set to a non-zero value in the open case.
1620 if (fh_differs
!= 0 && vpi
!= NULL
)
1624 * Be sure to set *vpp to the correct value before returning.
1628 skip_update_dircaches
:
1630 nfs4args_copen_free(open_args
);
1632 nfs4args_verify_free(&argop
[8]);
1633 nfs4args_setattr_free(&argop
[9]);
1635 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1639 kmem_free(argop
, argoplist_size
);
1644 * Reopen an open instance. cf. nfs4open_otw().
1646 * Errors are returned by the nfs4_error_t parameter.
1647 * - ep->error contains an errno value or zero.
1648 * - if it is zero, ep->stat is set to an NFS status code, if any.
1649 * If the file could not be reopened, but the caller should continue, the
1650 * file is marked dead and no error values are returned. If the caller
1651 * should stop recovering open files and start over, either the ep->error
1652 * value or ep->stat will indicate an error (either something that requires
1653 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1654 * filehandles) may be handled silently by this routine.
1655 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1656 * will be started, so the caller should not do it.
1659 * - kill_file : reopen failed in such a fashion to constitute marking the
1660 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1661 * is for cases where recovery is not possible.
1662 * - failed_reopen : same as above, except that the file has already been
1663 * marked dead, so no need to do it again.
1664 * - bailout : reopen failed but we are able to recover and retry the reopen -
1665 * either within this function immediately or via the calling function.
1669 nfs4_reopen(vnode_t
*vp
, nfs4_open_stream_t
*osp
, nfs4_error_t
*ep
,
1670 open_claim_type4 claim
, bool_t frc_use_claim_previous
,
1673 COMPOUND4args_clnt args
;
1674 COMPOUND4res_clnt res
;
1675 nfs_argop4 argop
[4];
1677 OPEN4res
*op_res
= NULL
;
1678 OPEN4cargs
*open_args
;
1680 rnode4_t
*rp
= VTOR4(vp
);
1682 cred_t
*cr
= NULL
, *cred_otw
= NULL
;
1683 nfs4_open_owner_t
*oop
= NULL
;
1685 nfs4_ga_res_t
*garp
;
1686 char fn
[MAXNAMELEN
];
1687 nfs4_recov_state_t recov
= {NULL
, 0};
1688 nfs4_lost_rqst_t lost_rqst
;
1689 mntinfo4_t
*mi
= VTOMI4(vp
);
1691 char *failed_msg
= "";
1694 nfs4_bseqid_entry_t
*bsep
= NULL
;
1696 ASSERT(nfs4_consistent_type(vp
));
1697 ASSERT(nfs_zone() == mi
->mi_zone
);
1699 nfs4_error_zinit(ep
);
1701 /* this is the cred used to find the open owner */
1702 cr
= state_to_cred(osp
);
1704 failed_msg
= "Couldn't reopen: no cred";
1707 /* use this cred for OTW operations */
1708 cred_otw
= nfs4_get_otw_cred(cr
, mi
, osp
->os_open_owner
);
1711 nfs4_error_zinit(ep
);
1713 if (mi
->mi_vfsp
->vfs_flag
& VFS_UNMOUNTED
) {
1714 /* File system has been unmounted, quit */
1716 failed_msg
= "Couldn't reopen: file system has been unmounted";
1720 oop
= osp
->os_open_owner
;
1722 ASSERT(oop
!= NULL
);
1723 if (oop
== NULL
) { /* be defensive in non-DEBUG */
1724 failed_msg
= "can't reopen: no open owner";
1727 open_owner_hold(oop
);
1729 ep
->error
= nfs4_start_open_seqid_sync(oop
, mi
);
1731 open_owner_rele(oop
);
1737 * If the rnode has a delegation and the delegation has been
1738 * recovered and the server didn't request a recall and the caller
1739 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1740 * recovery) and the rnode hasn't been marked dead, then install
1741 * the delegation stateid in the open stream. Otherwise, proceed
1742 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1744 mutex_enter(&rp
->r_statev4_lock
);
1745 if (rp
->r_deleg_type
!= OPEN_DELEGATE_NONE
&&
1746 !rp
->r_deleg_return_pending
&&
1747 (rp
->r_deleg_needs_recovery
== OPEN_DELEGATE_NONE
) &&
1748 !rp
->r_deleg_needs_recall
&&
1749 claim
!= CLAIM_DELEGATE_CUR
&& !frc_use_claim_previous
&&
1750 !(rp
->r_flags
& R4RECOVERR
)) {
1751 mutex_enter(&osp
->os_sync_lock
);
1752 osp
->os_delegation
= 1;
1753 osp
->open_stateid
= rp
->r_deleg_stateid
;
1754 mutex_exit(&osp
->os_sync_lock
);
1755 mutex_exit(&rp
->r_statev4_lock
);
1758 mutex_exit(&rp
->r_statev4_lock
);
1761 * If the file failed recovery, just quit. This failure need not
1762 * affect other reopens, so don't return an error.
1764 mutex_enter(&rp
->r_statelock
);
1765 if (rp
->r_flags
& R4RECOVERR
) {
1766 mutex_exit(&rp
->r_statelock
);
1770 mutex_exit(&rp
->r_statelock
);
1773 * argop is empty here
1775 * PUTFH, OPEN, GETATTR
1777 args
.ctag
= TAG_REOPEN
;
1781 NFS4_DEBUG(nfs4_client_failover_debug
, (CE_NOTE
,
1782 "nfs4_reopen: file is type %d, id %s",
1783 vp
->v_type
, rnode4info(VTOR4(vp
))));
1785 argop
[0].argop
= OP_CPUTFH
;
1787 if (claim
!= CLAIM_PREVIOUS
) {
1789 * if this is a file mount then
1790 * use the mntinfo parentfh
1792 argop
[0].nfs_argop4_u
.opcputfh
.sfh
=
1793 (vp
->v_flag
& VROOT
) ? mi
->mi_srvparentfh
:
1796 /* putfh fh to reopen */
1797 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
1800 argop
[1].argop
= OP_COPEN
;
1801 open_args
= &argop
[1].nfs_argop4_u
.opcopen
;
1802 open_args
->claim
= claim
;
1804 if (claim
== CLAIM_NULL
) {
1806 if ((ep
->error
= vtoname(vp
, fn
, MAXNAMELEN
)) != 0) {
1807 nfs_cmn_err(ep
->error
, CE_WARN
, "nfs4_reopen: vtoname "
1808 "failed for vp 0x%p for CLAIM_NULL with %m",
1810 failed_msg
= "Couldn't reopen: vtoname failed for "
1812 /* nothing allocated yet */
1816 open_args
->open_claim4_u
.cfile
= fn
;
1817 } else if (claim
== CLAIM_PREVIOUS
) {
1820 * We have two cases to deal with here:
1821 * 1) We're being called to reopen files in order to satisfy
1822 * a lock operation request which requires us to explicitly
1823 * reopen files which were opened under a delegation. If
1824 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1825 * that case, frc_use_claim_previous is TRUE and we must
1826 * use the rnode's current delegation type (r_deleg_type).
1827 * 2) We're reopening files during some form of recovery.
1828 * In this case, frc_use_claim_previous is FALSE and we
1829 * use the delegation type appropriate for recovery
1830 * (r_deleg_needs_recovery).
1832 mutex_enter(&rp
->r_statev4_lock
);
1833 open_args
->open_claim4_u
.delegate_type
=
1834 frc_use_claim_previous
?
1836 rp
->r_deleg_needs_recovery
;
1837 mutex_exit(&rp
->r_statev4_lock
);
1839 } else if (claim
== CLAIM_DELEGATE_CUR
) {
1841 if ((ep
->error
= vtoname(vp
, fn
, MAXNAMELEN
)) != 0) {
1842 nfs_cmn_err(ep
->error
, CE_WARN
, "nfs4_reopen: vtoname "
1843 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1844 "with %m", (void *)vp
);
1845 failed_msg
= "Couldn't reopen: vtoname failed for "
1846 "CLAIM_DELEGATE_CUR";
1847 /* nothing allocated yet */
1851 mutex_enter(&rp
->r_statev4_lock
);
1852 open_args
->open_claim4_u
.delegate_cur_info
.delegate_stateid
=
1853 rp
->r_deleg_stateid
;
1854 mutex_exit(&rp
->r_statev4_lock
);
1856 open_args
->open_claim4_u
.delegate_cur_info
.cfile
= fn
;
1858 open_args
->opentype
= OPEN4_NOCREATE
;
1859 open_args
->owner
.clientid
= mi2clientid(mi
);
1860 open_args
->owner
.owner_len
= sizeof (oop
->oo_name
);
1861 open_args
->owner
.owner_val
=
1862 kmem_alloc(open_args
->owner
.owner_len
, KM_SLEEP
);
1863 bcopy(&oop
->oo_name
, open_args
->owner
.owner_val
,
1864 open_args
->owner
.owner_len
);
1865 open_args
->share_access
= 0;
1866 open_args
->share_deny
= 0;
1868 mutex_enter(&osp
->os_sync_lock
);
1869 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
, "nfs4_reopen: osp %p rp "
1870 "%p: read acc %"PRIu64
" write acc %"PRIu64
": open ref count %d: "
1871 "mmap read %"PRIu64
" mmap write %"PRIu64
" claim %d ",
1872 (void *)osp
, (void *)rp
, osp
->os_share_acc_read
,
1873 osp
->os_share_acc_write
, osp
->os_open_ref_count
,
1874 osp
->os_mmap_read
, osp
->os_mmap_write
, claim
));
1876 if (osp
->os_share_acc_read
|| osp
->os_mmap_read
)
1877 open_args
->share_access
|= OPEN4_SHARE_ACCESS_READ
;
1878 if (osp
->os_share_acc_write
|| osp
->os_mmap_write
)
1879 open_args
->share_access
|= OPEN4_SHARE_ACCESS_WRITE
;
1880 if (osp
->os_share_deny_read
)
1881 open_args
->share_deny
|= OPEN4_SHARE_DENY_READ
;
1882 if (osp
->os_share_deny_write
)
1883 open_args
->share_deny
|= OPEN4_SHARE_DENY_WRITE
;
1884 mutex_exit(&osp
->os_sync_lock
);
1886 seqid
= nfs4_get_open_seqid(oop
) + 1;
1887 open_args
->seqid
= seqid
;
1889 /* Construct the getfh part of the compound */
1890 argop
[2].argop
= OP_GETFH
;
1892 /* Construct the getattr part of the compound */
1893 argop
[3].argop
= OP_GETATTR
;
1894 argop
[3].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1895 argop
[3].nfs_argop4_u
.opgetattr
.mi
= mi
;
1899 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, ep
);
1902 if (!is_recov
&& !frc_use_claim_previous
&&
1903 (ep
->error
== EINTR
|| ep
->error
== ETIMEDOUT
||
1904 NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
))) {
1905 nfs4open_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
1906 cred_otw
, vp
, NULL
, open_args
);
1907 abort
= nfs4_start_recovery(ep
,
1908 VTOMI4(vp
), vp
, NULL
, NULL
,
1909 lost_rqst
.lr_op
== OP_OPEN
?
1910 &lost_rqst
: NULL
, OP_OPEN
, NULL
, NULL
, NULL
);
1911 nfs4args_copen_free(open_args
);
1915 nfs4args_copen_free(open_args
);
1917 if (ep
->error
== EACCES
&& cred_otw
!= cr
) {
1921 nfs4_end_open_seqid_sync(oop
);
1922 open_owner_rele(oop
);
1926 if (ep
->error
== ETIMEDOUT
)
1928 failed_msg
= "Couldn't reopen: rpc error";
1932 if (nfs4_need_to_bump_seqid(&res
))
1933 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
1935 switch (res
.status
) {
1937 if (recov
.rs_flags
& NFS4_RS_DELAY_MSG
) {
1938 mutex_enter(&rp
->r_statelock
);
1939 rp
->r_delay_interval
= 0;
1940 mutex_exit(&rp
->r_statelock
);
1943 case NFS4ERR_BAD_SEQID
:
1944 bsep
= nfs4_create_bseqid_entry(oop
, NULL
, vp
, 0,
1945 args
.ctag
, open_args
->seqid
);
1947 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
,
1948 NULL
, lost_rqst
.lr_op
== OP_OPEN
? &lost_rqst
:
1949 NULL
, OP_OPEN
, bsep
, NULL
, NULL
);
1951 nfs4args_copen_free(open_args
);
1952 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1953 nfs4_end_open_seqid_sync(oop
);
1954 open_owner_rele(oop
);
1956 kmem_free(bsep
, sizeof (*bsep
));
1959 case NFS4ERR_NO_GRACE
:
1960 nfs4args_copen_free(open_args
);
1961 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1962 nfs4_end_open_seqid_sync(oop
);
1963 open_owner_rele(oop
);
1965 if (claim
== CLAIM_PREVIOUS
) {
1967 * Retry as a plain open. We don't need to worry about
1968 * checking the changeinfo: it is acceptable for a
1969 * client to re-open a file and continue processing
1970 * (in the absence of locks).
1972 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
1973 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1974 "will retry as CLAIM_NULL"));
1976 nfs4_mi_kstat_inc_no_grace(mi
);
1980 "Couldn't reopen: tried reclaim outside grace period. ";
1983 nfs4_set_grace_wait(mi
);
1984 nfs4args_copen_free(open_args
);
1985 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1986 nfs4_end_open_seqid_sync(oop
);
1987 open_owner_rele(oop
);
1989 ep
->error
= nfs4_wait_for_grace(mi
, &recov
);
1994 nfs4_set_delay_wait(vp
);
1995 nfs4args_copen_free(open_args
);
1996 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1997 nfs4_end_open_seqid_sync(oop
);
1998 open_owner_rele(oop
);
2000 ep
->error
= nfs4_wait_for_delay(vp
, &recov
);
2001 nfs4_mi_kstat_inc_delay(mi
);
2005 case NFS4ERR_FHEXPIRED
:
2006 /* recover filehandle and retry */
2007 abort
= nfs4_start_recovery(ep
,
2008 mi
, vp
, NULL
, NULL
, NULL
, OP_OPEN
, NULL
, NULL
, NULL
);
2009 nfs4args_copen_free(open_args
);
2010 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2011 nfs4_end_open_seqid_sync(oop
);
2012 open_owner_rele(oop
);
2016 failed_msg
= "Couldn't reopen: recovery aborted";
2018 case NFS4ERR_RESOURCE
:
2019 case NFS4ERR_STALE_CLIENTID
:
2020 case NFS4ERR_WRONGSEC
:
2021 case NFS4ERR_EXPIRED
:
2023 * Do not mark the file dead and let the calling
2024 * function initiate recovery.
2026 nfs4args_copen_free(open_args
);
2027 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2028 nfs4_end_open_seqid_sync(oop
);
2029 open_owner_rele(oop
);
2032 case NFS4ERR_ACCESS
:
2033 if (cred_otw
!= cr
) {
2037 nfs4args_copen_free(open_args
);
2038 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2039 nfs4_end_open_seqid_sync(oop
);
2040 open_owner_rele(oop
);
2046 NFS4_DEBUG(nfs4_client_failover_debug
, (CE_NOTE
,
2047 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2048 (void*)VTOR4(vp
)->r_server
, (void*)mi
->mi_curr_serv
,
2049 rnode4info(VTOR4(vp
))));
2050 failed_msg
= "Couldn't reopen: NFSv4 error";
2051 nfs4args_copen_free(open_args
);
2052 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2056 resop
= &res
.array
[1]; /* open res */
2057 op_res
= &resop
->nfs_resop4_u
.opopen
;
2059 garp
= &res
.array
[3].nfs_resop4_u
.opgetattr
.ga_res
;
2062 * Check if the path we reopened really is the same
2063 * file. We could end up in a situation where the file
2064 * was removed and a new file created with the same name.
2066 resop
= &res
.array
[2];
2067 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
2068 (void) nfs_rw_enter_sig(&mi
->mi_fh_lock
, RW_READER
, 0);
2069 fh_different
= (nfs4cmpfh(&rp
->r_fh
->sfh_fh
, &gf_res
->object
) != 0);
2071 if (mi
->mi_fh_expire_type
== FH4_PERSISTENT
||
2072 mi
->mi_fh_expire_type
& FH4_NOEXPIRE_WITH_OPEN
) {
2073 /* Oops, we don't have the same file */
2074 if (mi
->mi_fh_expire_type
== FH4_PERSISTENT
)
2075 failed_msg
= "Couldn't reopen: Persistent "
2076 "file handle changed";
2078 failed_msg
= "Couldn't reopen: Volatile "
2079 "(no expire on open) file handle changed";
2081 nfs4args_copen_free(open_args
);
2082 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2083 nfs_rw_exit(&mi
->mi_fh_lock
);
2088 * We have volatile file handles that don't compare.
2089 * If the fids are the same then we assume that the
2090 * file handle expired but the rnode still refers to
2091 * the same file object.
2093 * First check that we have fids or not.
2094 * If we don't we have a dumb server so we will
2095 * just assume every thing is ok for now.
2097 if (!ep
->error
&& garp
->n4g_va
.va_mask
& VATTR_NODEID
&&
2098 rp
->r_attr
.va_mask
& VATTR_NODEID
&&
2099 rp
->r_attr
.va_nodeid
!= garp
->n4g_va
.va_nodeid
) {
2101 * We have fids, but they don't
2102 * compare. So kill the file.
2105 "Couldn't reopen: file handle changed"
2106 " due to mismatched fids";
2107 nfs4args_copen_free(open_args
);
2108 xdr_free(xdr_COMPOUND4res_clnt
,
2110 nfs_rw_exit(&mi
->mi_fh_lock
);
2114 * We have volatile file handles that refers
2115 * to the same file (at least they have the
2116 * same fid) or we don't have fids so we
2117 * can't tell. :(. We'll be a kind and accepting
2118 * client so we'll update the rnode's file
2119 * handle with the otw handle.
2121 * We need to drop mi->mi_fh_lock since
2122 * sh4_update acquires it. Since there is
2123 * only one recovery thread there is no
2126 nfs_rw_exit(&mi
->mi_fh_lock
);
2127 sfh4_update(rp
->r_fh
, &gf_res
->object
);
2131 nfs_rw_exit(&mi
->mi_fh_lock
);
2134 ASSERT(nfs4_consistent_type(vp
));
2137 * If the server wanted an OPEN_CONFIRM but that fails, just start
2138 * over. Presumably if there is a persistent error it will show up
2139 * when we resend the OPEN.
2141 if (op_res
->rflags
& OPEN4_RESULT_CONFIRM
) {
2142 bool_t retry_open
= FALSE
;
2144 nfs4open_confirm(vp
, &seqid
, &op_res
->stateid
,
2145 cred_otw
, is_recov
, &retry_open
,
2146 oop
, FALSE
, ep
, NULL
);
2147 if (ep
->error
|| ep
->stat
) {
2148 nfs4args_copen_free(open_args
);
2149 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2150 nfs4_end_open_seqid_sync(oop
);
2151 open_owner_rele(oop
);
2157 mutex_enter(&osp
->os_sync_lock
);
2158 osp
->open_stateid
= op_res
->stateid
;
2159 osp
->os_delegation
= 0;
2161 * Need to reset this bitfield for the possible case where we were
2162 * going to OTW CLOSE the file, got a non-recoverable error, and before
2163 * we could retry the CLOSE, OPENed the file again.
2165 ASSERT(osp
->os_open_owner
->oo_seqid_inuse
);
2166 osp
->os_final_close
= 0;
2167 osp
->os_force_close
= 0;
2168 if (claim
== CLAIM_DELEGATE_CUR
|| claim
== CLAIM_PREVIOUS
)
2169 osp
->os_dc_openacc
= open_args
->share_access
;
2170 mutex_exit(&osp
->os_sync_lock
);
2172 nfs4_end_open_seqid_sync(oop
);
2174 /* accept delegation, if any */
2175 nfs4_delegation_accept(rp
, claim
, op_res
, garp
, cred_otw
);
2177 nfs4args_copen_free(open_args
);
2179 nfs4_attr_cache(vp
, garp
, t
, cr
, TRUE
, NULL
);
2181 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2183 ASSERT(nfs4_consistent_type(vp
));
2185 open_owner_rele(oop
);
2191 nfs4_fail_recov(vp
, failed_msg
, ep
->error
, ep
->stat
);
2193 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
,
2194 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2195 (void *)osp
, (void *)cr
, rnode4info(rp
)));
2196 mutex_enter(&osp
->os_sync_lock
);
2197 osp
->os_failed_reopen
= 1;
2198 mutex_exit(&osp
->os_sync_lock
);
2201 nfs4_end_open_seqid_sync(oop
);
2202 open_owner_rele(oop
);
2206 if (cred_otw
!= NULL
)
2210 /* for . and .. OPENs */
2213 nfs4_open_non_reg_file(vnode_t
**vpp
, int flag
, cred_t
*cr
)
2218 ASSERT(nfs_zone() == VTOMI4(*vpp
)->mi_zone
);
2221 * If close-to-open consistency checking is turned off or
2222 * if there is no cached data, we can avoid
2223 * the over the wire getattr. Otherwise, force a
2224 * call to the server to get fresh attributes and to
2225 * check caches. This is required for close-to-open
2229 if (VTOMI4(*vpp
)->mi_flags
& MI4_NOCTO
||
2230 (rp
->r_dir
== NULL
&& !nfs4_has_pages(*vpp
)))
2233 return (nfs4_getattr_otw(*vpp
, &gar
, cr
, 0));
2241 nfs4_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
2242 caller_context_t
*ct
)
2248 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
2251 * Remove client state for this (lockowner, file) pair.
2252 * Issue otw v4 call to have the server do the same.
2258 * zone_enter(2) prevents processes from changing zones with NFS files
2259 * open; if we happen to get here from the wrong zone we can't do
2260 * anything over the wire.
2262 if (VTOMI4(vp
)->mi_zone
!= nfs_zone()) {
2264 * We could attempt to clean up locks, except we're sure
2265 * that the current process didn't acquire any locks on
2266 * the file: any attempt to lock a file belong to another zone
2267 * will fail, and one can't lock an NFS file and then change
2268 * zones, as that fails too.
2270 * Returning an error here is the sane thing to do. A
2271 * subsequent call to VN_RELE() which translates to a
2272 * nfs4_inactive() will clean up state: if the zone of the
2273 * vnode's origin is still alive and kicking, the inactive
2274 * thread will handle the request (from the correct zone), and
2275 * everything (minus the OTW close call) should be OK. If the
2276 * zone is going away nfs4_async_inactive() will throw away
2277 * delegations, open streams and cached pages inline.
2283 * If we are using local locking for this filesystem, then
2284 * release all of the SYSV style record locks. Otherwise,
2285 * we are doing network locking and we need to release all
2286 * of the network locks. All of the locks held by this
2287 * process on this file are released no matter what the
2288 * incoming reference count is.
2290 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
) {
2291 cleanlocks(vp
, ttoproc(curthread
)->p_pid
, 0);
2292 cleanshares(vp
, ttoproc(curthread
)->p_pid
);
2294 e
.error
= nfs4_lockrelease(vp
, flag
, offset
, cr
);
2297 struct lm_sysid
*lmsid
;
2298 lmsid
= nfs4_find_sysid(VTOMI4(vp
));
2299 if (lmsid
== NULL
) {
2300 DTRACE_PROBE2(unknown__sysid
, int, e
.error
,
2303 cleanlocks(vp
, ttoproc(curthread
)->p_pid
,
2304 (lm_sysidt(lmsid
) | LM_SYSID_CLIENT
));
2306 lm_rel_sysid(lmsid
);
2315 * If the file has been `unlinked', then purge the
2316 * DNLC so that this vnode will get reycled quicker
2317 * and the .nfs* file on the server will get removed.
2319 if (rp
->r_unldvp
!= NULL
)
2323 * If the file was open for write and there are pages,
2324 * do a synchronous flush and commit of all of the
2325 * dirty and uncommitted pages.
2328 if ((flag
& FWRITE
) && nfs4_has_pages(vp
))
2329 error
= nfs4_putpage_commit(vp
, 0, 0, cr
);
2331 mutex_enter(&rp
->r_statelock
);
2332 r_error
= rp
->r_error
;
2334 mutex_exit(&rp
->r_statelock
);
2337 * If this file type is one for which no explicit 'open' was
2338 * done, then bail now (ie. no need for protocol 'close'). If
2339 * there was an error w/the vm subsystem, return _that_ error,
2340 * otherwise, return any errors that may've been reported via
2343 if (vp
->v_type
!= VREG
)
2344 return (error
? error
: r_error
);
2347 * The sync putpage commit may have failed above, but since
2348 * we're working w/a regular file, we need to do the protocol
2349 * 'close' (nfs4close_one will figure out if an otw close is
2350 * needed or not). Report any errors _after_ doing the protocol
2353 nfs4close_one(vp
, NULL
, cr
, flag
, NULL
, &e
, CLOSE_NORM
, 0, 0, 0);
2354 n4error
= e
.error
? e
.error
: geterrno4(e
.stat
);
2357 * Error reporting prio (Hi -> Lo)
2359 * i) nfs4_putpage_commit (error)
2360 * ii) rnode's (r_error)
2361 * iii) nfs4close_one (n4error)
2363 return (error
? error
: (r_error
? r_error
: n4error
));
2367 * Initialize *lost_rqstp.
2371 nfs4close_save_lost_rqst(int error
, nfs4_lost_rqst_t
*lost_rqstp
,
2372 nfs4_open_owner_t
*oop
, nfs4_open_stream_t
*osp
, cred_t
*cr
,
2375 if (error
!= ETIMEDOUT
&& error
!= EINTR
&&
2376 !NFS4_FRC_UNMT_ERR(error
, vp
->v_vfsp
)) {
2377 lost_rqstp
->lr_op
= 0;
2381 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
2382 "nfs4close_save_lost_rqst: error %d", error
));
2384 lost_rqstp
->lr_op
= OP_CLOSE
;
2386 * The vp is held and rele'd via the recovery code.
2387 * See nfs4_save_lost_rqst.
2389 lost_rqstp
->lr_vp
= vp
;
2390 lost_rqstp
->lr_dvp
= NULL
;
2391 lost_rqstp
->lr_oop
= oop
;
2392 lost_rqstp
->lr_osp
= osp
;
2393 ASSERT(osp
!= NULL
);
2394 ASSERT(mutex_owned(&osp
->os_sync_lock
));
2395 osp
->os_pending_close
= 1;
2396 lost_rqstp
->lr_lop
= NULL
;
2397 lost_rqstp
->lr_cr
= cr
;
2398 lost_rqstp
->lr_flk
= NULL
;
2399 lost_rqstp
->lr_putfirst
= FALSE
;
2403 * Assumes you already have the open seqid sync grabbed as well as the
2404 * 'os_sync_lock'. Note: this will release the open seqid sync and
2405 * 'os_sync_lock' if client recovery starts. Calling functions have to
2406 * be prepared to handle this.
2408 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2409 * was needed and was started, and that the calling function should retry
2410 * this function; otherwise it is returned as 0.
2412 * Errors are returned via the nfs4_error_t parameter.
2415 nfs4close_otw(rnode4_t
*rp
, cred_t
*cred_otw
, nfs4_open_owner_t
*oop
,
2416 nfs4_open_stream_t
*osp
, int *recov
, int *did_start_seqid_syncp
,
2417 nfs4_close_type_t close_type
, nfs4_error_t
*ep
, int *have_sync_lockp
)
2419 COMPOUND4args_clnt args
;
2420 COMPOUND4res_clnt res
;
2421 CLOSE4args
*close_args
;
2423 nfs_argop4 argop
[3];
2428 bool_t needrecov
= FALSE
;
2429 nfs4_lost_rqst_t lost_rqst
;
2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp
))->mi_zone
);
2434 ASSERT(MUTEX_HELD(&osp
->os_sync_lock
));
2436 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4close_otw"));
2438 /* Only set this to 1 if recovery is started */
2441 /* do the OTW call to close the file */
2443 if (close_type
== CLOSE_RESEND
)
2444 args
.ctag
= TAG_CLOSE_LOST
;
2445 else if (close_type
== CLOSE_AFTER_RESEND
)
2446 args
.ctag
= TAG_CLOSE_UNDO
;
2448 args
.ctag
= TAG_CLOSE
;
2457 /* putfh target fh */
2458 argop
[0].argop
= OP_CPUTFH
;
2459 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
2461 argop
[1].argop
= OP_GETATTR
;
2462 argop
[1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
2463 argop
[1].nfs_argop4_u
.opgetattr
.mi
= mi
;
2465 argop
[2].argop
= OP_CLOSE
;
2466 close_args
= &argop
[2].nfs_argop4_u
.opclose
;
2468 seqid
= nfs4_get_open_seqid(oop
) + 1;
2470 close_args
->seqid
= seqid
;
2471 close_args
->open_stateid
= osp
->open_stateid
;
2473 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
2474 "nfs4close_otw: %s call, rp %s", needrecov
? "recov" : "first",
2479 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, ep
);
2481 if (!ep
->error
&& nfs4_need_to_bump_seqid(&res
)) {
2482 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
2485 needrecov
= nfs4_needs_recovery(ep
, TRUE
, mi
->mi_vfsp
);
2486 if (ep
->error
&& !needrecov
) {
2488 * if there was an error and no recovery is to be done
2489 * then then set up the file to flush its cache if
2490 * needed for the next caller.
2492 mutex_enter(&rp
->r_statelock
);
2493 PURGE_ATTRCACHE4_LOCKED(rp
);
2494 rp
->r_flags
&= ~R4WRITEMODIFIED
;
2495 mutex_exit(&rp
->r_statelock
);
2501 nfs4_bseqid_entry_t
*bsep
= NULL
;
2503 if (close_type
!= CLOSE_RESEND
)
2504 nfs4close_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
2507 if (!ep
->error
&& res
.status
== NFS4ERR_BAD_SEQID
)
2508 bsep
= nfs4_create_bseqid_entry(oop
, NULL
, vp
,
2509 0, args
.ctag
, close_args
->seqid
);
2511 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
2512 "nfs4close_otw: initiating recovery. error %d "
2513 "res.status %d", ep
->error
, res
.status
));
2516 * Drop the 'os_sync_lock' here so we don't hit
2517 * a potential recursive mutex_enter via an
2518 * 'open_stream_hold()'.
2520 mutex_exit(&osp
->os_sync_lock
);
2521 *have_sync_lockp
= 0;
2522 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
, NULL
,
2523 (close_type
!= CLOSE_RESEND
&&
2524 lost_rqst
.lr_op
== OP_CLOSE
) ? &lost_rqst
: NULL
,
2525 OP_CLOSE
, bsep
, NULL
, NULL
);
2527 /* drop open seq sync, and let the calling function regrab it */
2528 nfs4_end_open_seqid_sync(oop
);
2529 *did_start_seqid_syncp
= 0;
2532 kmem_free(bsep
, sizeof (*bsep
));
2534 * For signals, the caller wants to quit, so don't say to
2535 * retry. For forced unmount, if it's a user thread, it
2536 * wants to quit. If it's a recovery thread, the retry
2537 * will happen higher-up on the call stack. Either way,
2538 * don't say to retry.
2540 if (abort
== FALSE
&& ep
->error
!= EINTR
&&
2541 !NFS4_FRC_UNMT_ERR(ep
->error
, mi
->mi_vfsp
) &&
2542 close_type
!= CLOSE_RESEND
&&
2543 close_type
!= CLOSE_AFTER_RESEND
)
2549 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2554 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2558 mutex_enter(&rp
->r_statev4_lock
);
2560 mutex_exit(&rp
->r_statev4_lock
);
2562 resop
= &res
.array
[2];
2563 osp
->open_stateid
= resop
->nfs_resop4_u
.opclose
.open_stateid
;
2567 * This removes the reference obtained at OPEN; ie, when the
2568 * open stream structure was created.
2570 * We don't have to worry about calling 'open_stream_rele'
2571 * since we our currently holding a reference to the open
2572 * stream which means the count cannot go to 0 with this
2575 ASSERT(osp
->os_ref_count
>= 2);
2576 osp
->os_ref_count
--;
2578 if (ep
->error
== 0) {
2580 * Avoid a deadlock with the r_serial thread waiting for
2581 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be
2582 * held by us. We will wait in nfs4_attr_cache() for the
2583 * completion of the r_serial thread.
2585 mutex_exit(&osp
->os_sync_lock
);
2586 *have_sync_lockp
= 0;
2589 &res
.array
[1].nfs_resop4_u
.opgetattr
.ga_res
,
2590 t
, cred_otw
, TRUE
, NULL
);
2593 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4close_otw:"
2594 " returning %d", ep
->error
));
2596 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2601 nfs4_read(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
2602 caller_context_t
*ct
)
2616 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
2618 if (IS_SHADOW(vp
, rp
))
2621 if (vp
->v_type
!= VREG
)
2626 if (nfs_zone() != mi
->mi_zone
)
2629 if (uiop
->uio_resid
== 0)
2632 if (uiop
->uio_loffset
< 0 || uiop
->uio_loffset
+ uiop
->uio_resid
< 0)
2635 mutex_enter(&rp
->r_statelock
);
2636 if (rp
->r_flags
& R4RECOVERRP
)
2637 error
= (rp
->r_error
? rp
->r_error
: EIO
);
2640 mutex_exit(&rp
->r_statelock
);
2645 * Bypass VM if caching has been disabled (e.g., locking) or if
2646 * using client-side direct I/O and the file is not mmap'd and
2647 * there are no cached pages.
2649 if ((vp
->v_flag
& VNOCACHE
) ||
2650 (((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
)) &&
2651 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 && !nfs4_has_pages(vp
))) {
2654 return (nfs4read(vp
, NULL
, uiop
->uio_loffset
,
2655 uiop
->uio_resid
, &resid
, cr
, FALSE
, uiop
));
2661 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
2662 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
2663 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
2665 if (error
= nfs4_validate_caches(vp
, cr
))
2668 mutex_enter(&rp
->r_statelock
);
2669 while (rp
->r_flags
& R4INCACHEPURGE
) {
2670 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
2671 mutex_exit(&rp
->r_statelock
);
2675 diff
= rp
->r_size
- uiop
->uio_loffset
;
2676 mutex_exit(&rp
->r_statelock
);
2686 error
= vpm_data_copy(vp
, off
+ on
, n
, uiop
,
2687 1, NULL
, 0, S_READ
);
2689 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
, n
, 1,
2692 error
= uiomove(base
+ on
, n
, UIO_READ
, uiop
);
2697 * If read a whole block or read to eof,
2698 * won't need this buffer again soon.
2700 mutex_enter(&rp
->r_statelock
);
2701 if (n
+ on
== MAXBSIZE
||
2702 uiop
->uio_loffset
== rp
->r_size
)
2703 flags
= SM_DONTNEED
;
2706 mutex_exit(&rp
->r_statelock
);
2708 error
= vpm_sync_pages(vp
, off
, n
, flags
);
2710 error
= segmap_release(segkmap
, base
, flags
);
2714 (void) vpm_sync_pages(vp
, off
, n
, 0);
2716 (void) segmap_release(segkmap
, base
, 0);
2719 } while (!error
&& uiop
->uio_resid
> 0);
2726 nfs4_write(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
2727 caller_context_t
*ct
)
2729 rlim_t limit
= uiop
->uio_llimit
;
2745 if (IS_SHADOW(vp
, rp
))
2748 if (vp
->v_type
!= VREG
)
2753 if (nfs_zone() != mi
->mi_zone
)
2756 if (uiop
->uio_resid
== 0)
2759 mutex_enter(&rp
->r_statelock
);
2760 if (rp
->r_flags
& R4RECOVERRP
)
2761 error
= (rp
->r_error
? rp
->r_error
: EIO
);
2764 mutex_exit(&rp
->r_statelock
);
2768 if (ioflag
& FAPPEND
) {
2772 * Must serialize if appending.
2774 if (nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
)) {
2775 nfs_rw_exit(&rp
->r_rwlock
);
2776 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
,
2781 va
.va_mask
= VATTR_SIZE
;
2782 error
= nfs4getattr(vp
, &va
, cr
);
2785 uiop
->uio_loffset
= va
.va_size
;
2788 offset
= uiop
->uio_loffset
+ uiop
->uio_resid
;
2790 if (uiop
->uio_loffset
< 0 || offset
< 0)
2793 if (limit
== RLIM_INFINITY
|| limit
> MAXOFFSET_T
)
2794 limit
= MAXOFFSET_T
;
2797 * Check to make sure that the process will not exceed
2798 * its limit on file size. It is okay to write up to
2799 * the limit, but not beyond. Thus, the write which
2800 * reaches the limit will be short and the next write
2801 * will return an error.
2804 if (offset
> uiop
->uio_llimit
) {
2805 remainder
= offset
- uiop
->uio_llimit
;
2806 uiop
->uio_resid
= uiop
->uio_llimit
- uiop
->uio_loffset
;
2807 if (uiop
->uio_resid
<= 0) {
2808 proc_t
*p
= ttoproc(curthread
);
2810 uiop
->uio_resid
+= remainder
;
2811 mutex_enter(&p
->p_lock
);
2812 (void) rctl_action(rctlproc_legacy
[RLIMIT_FSIZE
],
2813 p
->p_rctls
, p
, RCA_UNSAFE_SIGINFO
);
2814 mutex_exit(&p
->p_lock
);
2819 /* update the change attribute, if we have a write delegation */
2821 mutex_enter(&rp
->r_statev4_lock
);
2822 if (rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
)
2823 rp
->r_deleg_change
++;
2825 mutex_exit(&rp
->r_statev4_lock
);
2827 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR4(vp
)))
2831 * Bypass VM if caching has been disabled (e.g., locking) or if
2832 * using client-side direct I/O and the file is not mmap'd and
2833 * there are no cached pages.
2835 if ((vp
->v_flag
& VNOCACHE
) ||
2836 (((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
)) &&
2837 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 && !nfs4_has_pages(vp
))) {
2841 stable_how4 stab_comm
;
2843 if (rp
->r_flags
& R4STALE
) {
2844 resid
= uiop
->uio_resid
;
2845 offset
= uiop
->uio_loffset
;
2846 error
= rp
->r_error
;
2848 * A close may have cleared r_error, if so,
2849 * propagate ESTALE error return properly
2856 bufsize
= MIN(uiop
->uio_resid
, mi
->mi_stsize
);
2857 base
= kmem_alloc(bufsize
, KM_SLEEP
);
2859 if (ioflag
& FDSYNC
)
2860 stab_comm
= DATA_SYNC4
;
2862 stab_comm
= FILE_SYNC4
;
2863 resid
= uiop
->uio_resid
;
2864 offset
= uiop
->uio_loffset
;
2865 count
= MIN(uiop
->uio_resid
, bufsize
);
2866 org_offset
= uiop
->uio_loffset
;
2867 error
= uiomove(base
, count
, UIO_WRITE
, uiop
);
2869 error
= nfs4write(vp
, base
, org_offset
,
2870 count
, cr
, &stab_comm
);
2872 mutex_enter(&rp
->r_statelock
);
2873 if (rp
->r_size
< uiop
->uio_loffset
)
2874 rp
->r_size
= uiop
->uio_loffset
;
2875 mutex_exit(&rp
->r_statelock
);
2878 } while (!error
&& uiop
->uio_resid
> 0);
2879 kmem_free(base
, bufsize
);
2883 bsize
= vp
->v_vfsp
->vfs_bsize
;
2886 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
2887 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
2888 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
2890 resid
= uiop
->uio_resid
;
2891 offset
= uiop
->uio_loffset
;
2893 if (rp
->r_flags
& R4STALE
) {
2894 error
= rp
->r_error
;
2896 * A close may have cleared r_error, if so,
2897 * propagate ESTALE error return properly
2905 * Don't create dirty pages faster than they
2906 * can be cleaned so that the system doesn't
2907 * get imbalanced. If the async queue is
2908 * maxed out, then wait for it to drain before
2909 * creating more dirty pages. Also, wait for
2910 * any threads doing pagewalks in the vop_getattr
2911 * entry points so that they don't block for
2914 mutex_enter(&rp
->r_statelock
);
2915 while ((mi
->mi_max_threads
!= 0 &&
2916 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
2919 klwp_t
*lwp
= ttolwp(curthread
);
2923 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
2924 mutex_exit(&rp
->r_statelock
);
2933 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
2935 mutex_exit(&rp
->r_statelock
);
2938 * Touch the page and fault it in if it is not in core
2939 * before segmap_getmapflt or vpm_data_copy can lock it.
2940 * This is to avoid the deadlock if the buffer is mapped
2941 * to the same file through mmap which we want to write.
2943 uio_prefaultpages((long)n
, uiop
);
2947 * It will use kpm mappings, so no need to
2950 error
= writerp4(rp
, NULL
, n
, uiop
, 0);
2953 int pon
= uiop
->uio_loffset
& PAGEOFFSET
;
2954 size_t pn
= MIN(PAGESIZE
- pon
,
2958 mutex_enter(&rp
->r_statelock
);
2959 pagecreate
= (pon
== 0) && (pn
== PAGESIZE
||
2960 uiop
->uio_loffset
+ pn
>= rp
->r_size
);
2961 mutex_exit(&rp
->r_statelock
);
2963 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
2964 pn
, !pagecreate
, S_WRITE
);
2966 error
= writerp4(rp
, base
+ pon
, n
, uiop
,
2970 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
2972 error
= writerp4(rp
, base
+ on
, n
, uiop
, 0);
2977 if (mi
->mi_flags
& MI4_NOAC
)
2979 else if ((uiop
->uio_loffset
% bsize
) == 0 ||
2982 * Have written a whole block.
2983 * Start an asynchronous write
2984 * and mark the buffer to
2985 * indicate that it won't be
2986 * needed again soon.
2988 flags
= SM_WRITE
| SM_ASYNC
| SM_DONTNEED
;
2991 if ((ioflag
& (FSYNC
|FDSYNC
)) ||
2992 (rp
->r_flags
& R4OUTOFSPACE
)) {
2997 error
= vpm_sync_pages(vp
, off
, n
, flags
);
2999 error
= segmap_release(segkmap
, base
, flags
);
3003 (void) vpm_sync_pages(vp
, off
, n
, 0);
3005 (void) segmap_release(segkmap
, base
, 0);
3008 * In the event that we got an access error while
3009 * faulting in a page for a write-only file just
3012 if (error
== EACCES
)
3015 } while (!error
&& uiop
->uio_resid
> 0);
3019 uiop
->uio_resid
= resid
+ remainder
;
3020 uiop
->uio_loffset
= offset
;
3022 uiop
->uio_resid
+= remainder
;
3024 mutex_enter(&rp
->r_statev4_lock
);
3025 if (rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
) {
3026 gethrestime(&rp
->r_attr
.va_mtime
);
3027 rp
->r_attr
.va_ctime
= rp
->r_attr
.va_mtime
;
3029 mutex_exit(&rp
->r_statev4_lock
);
3032 nfs_rw_exit(&rp
->r_lkserlock
);
3038 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3041 nfs4_rdwrlbn(vnode_t
*vp
, page_t
*pp
, uoff_t off
, size_t len
,
3042 int flags
, cred_t
*cr
)
3048 stable_how4 stab_comm
;
3050 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
3051 bp
= pageio_setup(pp
, len
, vp
, flags
);
3055 * pageio_setup should have set b_addr to 0. This
3056 * is correct since we want to do I/O on a page
3057 * boundary. bp_mapin will use this addr to calculate
3058 * an offset, and then set b_addr to the kernel virtual
3059 * address it allocated for us.
3061 ASSERT(bp
->b_un
.b_addr
== 0);
3065 bp
->b_lblkno
= lbtodb(off
);
3067 bp
->b_offset
= (offset_t
)off
;
3070 if ((flags
& (B_WRITE
|B_ASYNC
)) == (B_WRITE
|B_ASYNC
) &&
3072 stab_comm
= UNSTABLE4
;
3074 stab_comm
= FILE_SYNC4
;
3076 error
= nfs4_bio(bp
, &stab_comm
, cr
, FALSE
);
3081 if (stab_comm
== UNSTABLE4
)
3082 fsdata
= C_DELAYCOMMIT
;
3084 fsdata
= C_NOCOMMIT
;
3088 pp
->p_fsdata
= fsdata
;
3089 } while ((pp
= pp
->p_next
) != savepp
);
3097 nfs4rdwr_check_osid(vnode_t
*vp
, nfs4_error_t
*ep
, cred_t
*cr
)
3099 nfs4_open_owner_t
*oop
;
3100 nfs4_open_stream_t
*osp
;
3101 rnode4_t
*rp
= VTOR4(vp
);
3102 mntinfo4_t
*mi
= VTOMI4(vp
);
3105 ASSERT(nfs_zone() == mi
->mi_zone
);
3108 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
3112 /* returns with 'os_sync_lock' held */
3113 osp
= find_open_stream(oop
, rp
);
3115 open_owner_rele(oop
);
3119 if (osp
->os_failed_reopen
) {
3120 mutex_exit(&osp
->os_sync_lock
);
3121 open_stream_rele(osp
, rp
);
3122 open_owner_rele(oop
);
3127 * Determine whether a reopen is needed. If this
3128 * is a delegation open stream, then the os_delegation bit
3132 reopen_needed
= osp
->os_delegation
;
3134 mutex_exit(&osp
->os_sync_lock
);
3135 open_owner_rele(oop
);
3137 if (reopen_needed
) {
3138 nfs4_error_zinit(ep
);
3139 nfs4_reopen(vp
, osp
, ep
, CLAIM_NULL
, FALSE
, FALSE
);
3140 mutex_enter(&osp
->os_sync_lock
);
3141 if (ep
->error
|| ep
->stat
|| osp
->os_failed_reopen
) {
3142 mutex_exit(&osp
->os_sync_lock
);
3143 open_stream_rele(osp
, rp
);
3146 mutex_exit(&osp
->os_sync_lock
);
3148 open_stream_rele(osp
, rp
);
3154 * Write to file. Writes to remote server in largest size
3155 * chunks that the server can handle. Write is synchronous.
3158 nfs4write(vnode_t
*vp
, caddr_t base
, uoff_t offset
, int count
, cred_t
*cr
,
3159 stable_how4
*stab_comm
)
3162 COMPOUND4args_clnt args
;
3163 COMPOUND4res_clnt res
;
3166 nfs_argop4 argop
[2];
3173 nfs4_recov_state_t recov_state
;
3174 nfs4_stateid_types_t sid_types
;
3175 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3181 ASSERT(nfs_zone() == mi
->mi_zone
);
3183 stable
= *stab_comm
;
3184 *stab_comm
= FILE_SYNC4
;
3187 recov_state
.rs_flags
= 0;
3188 recov_state
.rs_num_retry_despite_err
= 0;
3189 nfs4_init_stateid_types(&sid_types
);
3191 /* Is curthread the recovery thread? */
3192 mutex_enter(&mi
->mi_lock
);
3193 recov
= (mi
->mi_recovthread
== curthread
);
3194 mutex_exit(&mi
->mi_lock
);
3197 args
.ctag
= TAG_WRITE
;
3202 e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3203 &recov_state
, NULL
);
3208 /* 0. putfh target fh */
3209 argop
[0].argop
= OP_CPUTFH
;
3210 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3213 nfs4args_write(&argop
[1], stable
, rp
, cr
, &wargs
, &sid_types
);
3217 wargs
->offset
= (offset4
)offset
;
3218 wargs
->data_val
= base
;
3220 if (mi
->mi_io_kstats
) {
3221 mutex_enter(&mi
->mi_lock
);
3222 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3223 mutex_exit(&mi
->mi_lock
);
3226 if ((vp
->v_flag
& VNOCACHE
) ||
3227 (rp
->r_flags
& R4DIRECTIO
) ||
3228 (mi
->mi_flags
& MI4_DIRECTIO
))
3229 tsize
= MIN(mi
->mi_stsize
, count
);
3231 tsize
= MIN(mi
->mi_curwrite
, count
);
3232 wargs
->data_len
= (uint_t
)tsize
;
3233 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
3235 if (mi
->mi_io_kstats
) {
3236 mutex_enter(&mi
->mi_lock
);
3237 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3238 mutex_exit(&mi
->mi_lock
);
3242 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
3243 if (e
.error
&& !needrecov
) {
3244 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3245 &recov_state
, needrecov
);
3254 * Do handling of OLD_STATEID outside
3255 * of the normal recovery framework.
3257 * If write receives a BAD stateid error while using a
3258 * delegation stateid, retry using the open stateid (if it
3259 * exists). If it doesn't have an open stateid, reopen the
3260 * file first, then retry.
3262 if (!e
.error
&& res
.status
== NFS4ERR_OLD_STATEID
&&
3263 sid_types
.cur_sid_type
!= SPEC_SID
) {
3264 nfs4_save_stateid(&wargs
->stateid
, &sid_types
);
3266 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3267 &recov_state
, needrecov
);
3268 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3270 } else if (e
.error
== 0 && res
.status
== NFS4ERR_BAD_STATEID
&&
3271 sid_types
.cur_sid_type
== DEL_SID
) {
3272 nfs4_save_stateid(&wargs
->stateid
, &sid_types
);
3273 mutex_enter(&rp
->r_statev4_lock
);
3274 rp
->r_deleg_return_pending
= TRUE
;
3275 mutex_exit(&rp
->r_statev4_lock
);
3276 if (nfs4rdwr_check_osid(vp
, &e
, cr
)) {
3278 nfs4_end_fop(mi
, vp
, NULL
, OH_WRITE
,
3279 &recov_state
, needrecov
);
3280 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3284 nfs4_end_fop(mi
, vp
, NULL
, OH_WRITE
,
3285 &recov_state
, needrecov
);
3286 /* hold needed for nfs4delegreturn_thread */
3288 nfs4delegreturn_async(rp
, (NFS4_DR_PUSH
|NFS4_DR_REOPEN
|
3289 NFS4_DR_DISCARD
), FALSE
);
3290 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3297 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
3298 "nfs4write: client got error %d, res.status %d"
3299 ", so start recovery", e
.error
, res
.status
));
3301 abort
= nfs4_start_recovery(&e
,
3302 VTOMI4(vp
), vp
, NULL
, &wargs
->stateid
,
3303 NULL
, OP_WRITE
, NULL
, NULL
, NULL
);
3305 e
.error
= geterrno4(res
.status
);
3306 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3308 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3309 &recov_state
, needrecov
);
3316 e
.error
= geterrno4(res
.status
);
3317 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3319 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3320 &recov_state
, needrecov
);
3324 resop
= &res
.array
[1]; /* write res */
3325 wres
= &resop
->nfs_resop4_u
.opwrite
;
3327 if ((int)wres
->count
> tsize
) {
3328 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3330 zcmn_err(getzoneid(), CE_WARN
,
3331 "nfs4write: server wrote %u, requested was %u",
3332 (int)wres
->count
, tsize
);
3334 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3335 &recov_state
, needrecov
);
3338 if (wres
->committed
== UNSTABLE4
) {
3339 *stab_comm
= UNSTABLE4
;
3340 if (wargs
->stable
== DATA_SYNC4
||
3341 wargs
->stable
== FILE_SYNC4
) {
3342 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3343 zcmn_err(getzoneid(), CE_WARN
,
3344 "nfs4write: server %s did not commit "
3345 "to stable storage",
3346 rp
->r_server
->sv_hostname
);
3348 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
,
3349 OH_WRITE
, &recov_state
, needrecov
);
3354 tsize
= (int)wres
->count
;
3358 if (mi
->mi_io_kstats
) {
3359 mutex_enter(&mi
->mi_lock
);
3360 KSTAT_IO_PTR(mi
->mi_io_kstats
)->writes
++;
3361 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nwritten
+=
3363 mutex_exit(&mi
->mi_lock
);
3365 lwp_stat_update(LWP_STAT_OUBLK
, 1);
3366 mutex_enter(&rp
->r_statelock
);
3367 if (rp
->r_flags
& R4HAVEVERF
) {
3368 if (rp
->r_writeverf
!= wres
->writeverf
) {
3370 rp
->r_writeverf
= wres
->writeverf
;
3373 rp
->r_writeverf
= wres
->writeverf
;
3374 rp
->r_flags
|= R4HAVEVERF
;
3376 PURGE_ATTRCACHE4_LOCKED(rp
);
3377 rp
->r_flags
|= R4WRITEMODIFIED
;
3378 gethrestime(&rp
->r_attr
.va_mtime
);
3379 rp
->r_attr
.va_ctime
= rp
->r_attr
.va_mtime
;
3380 mutex_exit(&rp
->r_statelock
);
3381 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3385 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
, &recov_state
,
3392 * Read from a file. Reads data in largest chunks our interface can handle.
3395 nfs4read(vnode_t
*vp
, caddr_t base
, offset_t offset
, int count
,
3396 size_t *residp
, cred_t
*cr
, bool_t async
, struct uio
*uiop
)
3399 COMPOUND4args_clnt args
;
3400 COMPOUND4res_clnt res
;
3402 nfs_argop4 argop
[2];
3408 bool_t needrecov
= FALSE
;
3409 nfs4_recov_state_t recov_state
;
3410 nfs4_stateid_types_t sid_types
;
3411 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3417 ASSERT(nfs_zone() == mi
->mi_zone
);
3419 args
.ctag
= async
? TAG_READAHEAD
: TAG_READ
;
3424 nfs4_init_stateid_types(&sid_types
);
3426 recov_state
.rs_flags
= 0;
3427 recov_state
.rs_num_retry_despite_err
= 0;
3430 e
.error
= nfs4_start_fop(mi
, vp
, NULL
, OH_READ
,
3431 &recov_state
, NULL
);
3435 /* putfh target fh */
3436 argop
[0].argop
= OP_CPUTFH
;
3437 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3440 argop
[1].argop
= OP_READ
;
3441 rargs
= &argop
[1].nfs_argop4_u
.opread
;
3442 rargs
->stateid
= nfs4_get_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
, mi
,
3443 OP_READ
, &sid_types
, async
);
3446 if (mi
->mi_io_kstats
) {
3447 mutex_enter(&mi
->mi_lock
);
3448 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3449 mutex_exit(&mi
->mi_lock
);
3452 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
3453 "nfs4read: %s call, rp %s",
3454 needrecov
? "recov" : "first",
3457 if ((vp
->v_flag
& VNOCACHE
) ||
3458 (rp
->r_flags
& R4DIRECTIO
) ||
3459 (mi
->mi_flags
& MI4_DIRECTIO
))
3460 tsize
= MIN(mi
->mi_tsize
, count
);
3462 tsize
= MIN(mi
->mi_curread
, count
);
3464 rargs
->offset
= (offset4
)offset
;
3465 rargs
->count
= (count4
)tsize
;
3466 rargs
->res_data_val_alt
= NULL
;
3467 rargs
->res_mblk
= NULL
;
3468 rargs
->res_uiop
= NULL
;
3469 rargs
->res_maxsize
= 0;
3470 rargs
->wlist
= NULL
;
3473 rargs
->res_uiop
= uiop
;
3475 rargs
->res_data_val_alt
= base
;
3476 rargs
->res_maxsize
= tsize
;
3478 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
3480 if (nfs4read_error_inject
) {
3481 res
.status
= nfs4read_error_inject
;
3482 nfs4read_error_inject
= 0;
3486 if (mi
->mi_io_kstats
) {
3487 mutex_enter(&mi
->mi_lock
);
3488 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3489 mutex_exit(&mi
->mi_lock
);
3492 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
3493 if (e
.error
!= 0 && !needrecov
) {
3494 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3495 &recov_state
, needrecov
);
3500 * Do proper retry for OLD and BAD stateid errors outside
3501 * of the normal recovery framework. There are two differences
3502 * between async and sync reads. The first is that we allow
3503 * retry on BAD_STATEID for async reads, but not sync reads.
3504 * The second is that we mark the file dead for a failed
3505 * attempt with a special stateid for sync reads, but just
3506 * return EIO for async reads.
3508 * If a sync read receives a BAD stateid error while using a
3509 * delegation stateid, retry using the open stateid (if it
3510 * exists). If it doesn't have an open stateid, reopen the
3511 * file first, then retry.
3513 if (e
.error
== 0 && (res
.status
== NFS4ERR_OLD_STATEID
||
3514 res
.status
== NFS4ERR_BAD_STATEID
) && async
) {
3515 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3516 &recov_state
, needrecov
);
3517 if (sid_types
.cur_sid_type
== SPEC_SID
) {
3518 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3521 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3522 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3524 } else if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3525 !async
&& sid_types
.cur_sid_type
!= SPEC_SID
) {
3526 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3527 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3528 &recov_state
, needrecov
);
3529 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3531 } else if (e
.error
== 0 && res
.status
== NFS4ERR_BAD_STATEID
&&
3532 sid_types
.cur_sid_type
== DEL_SID
) {
3533 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3534 mutex_enter(&rp
->r_statev4_lock
);
3535 rp
->r_deleg_return_pending
= TRUE
;
3536 mutex_exit(&rp
->r_statev4_lock
);
3537 if (nfs4rdwr_check_osid(vp
, &e
, cr
)) {
3538 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3539 &recov_state
, needrecov
);
3540 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3543 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3544 &recov_state
, needrecov
);
3545 /* hold needed for nfs4delegreturn_thread */
3547 nfs4delegreturn_async(rp
, (NFS4_DR_PUSH
|NFS4_DR_REOPEN
|
3548 NFS4_DR_DISCARD
), FALSE
);
3549 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3555 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
3556 "nfs4read: initiating recovery\n"));
3557 abort
= nfs4_start_recovery(&e
,
3558 mi
, vp
, NULL
, &rargs
->stateid
,
3559 NULL
, OP_READ
, NULL
, NULL
, NULL
);
3560 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3561 &recov_state
, needrecov
);
3563 * Do not retry if we got OLD_STATEID using a special
3564 * stateid. This avoids looping with a broken server.
3566 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3567 sid_types
.cur_sid_type
== SPEC_SID
)
3570 if (abort
== FALSE
) {
3572 * Need to retry all possible stateids in
3573 * case the recovery error wasn't stateid
3574 * related or the stateids have become
3575 * stale (server reboot).
3577 nfs4_init_stateid_types(&sid_types
);
3578 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3583 e
.error
= geterrno4(res
.status
);
3584 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3590 e
.error
= geterrno4(res
.status
);
3591 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3592 &recov_state
, needrecov
);
3593 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3597 data_len
= res
.array
[1].nfs_resop4_u
.opread
.data_len
;
3602 if (mi
->mi_io_kstats
) {
3603 mutex_enter(&mi
->mi_lock
);
3604 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
3605 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= data_len
;
3606 mutex_exit(&mi
->mi_lock
);
3608 lwp_stat_update(LWP_STAT_INBLK
, 1);
3609 is_eof
= res
.array
[1].nfs_resop4_u
.opread
.eof
;
3610 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3612 } while (count
&& !is_eof
);
3616 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
, &recov_state
, needrecov
);
3623 nfs4_ioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
,
3624 caller_context_t
*ct
)
3626 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3630 return (nfs4_directio(vp
, (int)arg
, cr
));
3638 nfs4_getattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3639 caller_context_t
*ct
)
3642 rnode4_t
*rp
= VTOR4(vp
);
3644 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3647 * If it has been specified that the return value will
3648 * just be used as a hint, and we are only being asked
3649 * for size, fsid or rdevid, then return the client's
3650 * notion of these values without checking to make sure
3651 * that the attribute cache is up to date.
3652 * The whole point is to avoid an over the wire GETATTR
3655 if (flags
& ATTR_HINT
) {
3656 if (!(vap
->va_mask
& ~(VATTR_SIZE
| VATTR_FSID
| VATTR_RDEV
))) {
3657 mutex_enter(&rp
->r_statelock
);
3658 if (vap
->va_mask
& VATTR_SIZE
)
3659 vap
->va_size
= rp
->r_size
;
3660 if (vap
->va_mask
& VATTR_FSID
)
3661 vap
->va_fsid
= rp
->r_attr
.va_fsid
;
3662 if (vap
->va_mask
& VATTR_RDEV
)
3663 vap
->va_rdev
= rp
->r_attr
.va_rdev
;
3664 mutex_exit(&rp
->r_statelock
);
3670 * Only need to flush pages if asking for the mtime
3671 * and if there any dirty pages or any outstanding
3672 * asynchronous (write) requests for this file.
3674 if (vap
->va_mask
& VATTR_MTIME
) {
3676 if (nfs4_has_pages(vp
)) {
3677 mutex_enter(&rp
->r_statev4_lock
);
3678 if (rp
->r_deleg_type
!= OPEN_DELEGATE_WRITE
) {
3679 mutex_exit(&rp
->r_statev4_lock
);
3680 if (rp
->r_flags
& R4DIRTY
||
3681 rp
->r_awcount
> 0) {
3682 mutex_enter(&rp
->r_statelock
);
3684 mutex_exit(&rp
->r_statelock
);
3688 mutex_enter(&rp
->r_statelock
);
3689 if (error
&& (error
== ENOSPC
||
3692 rp
->r_error
= error
;
3694 if (--rp
->r_gcount
== 0)
3695 cv_broadcast(&rp
->r_cv
);
3696 mutex_exit(&rp
->r_statelock
);
3699 mutex_exit(&rp
->r_statev4_lock
);
3703 return (nfs4getattr(vp
, vap
, cr
));
3707 nfs4_compare_modes(mode_t from_server
, mode_t on_client
)
3710 * If these are the only two bits cleared
3711 * on the server then return 0 (OK) else
3714 on_client
&= ~(S_ISUID
|S_ISGID
);
3715 if (on_client
== from_server
)
3723 nfs4_setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3724 caller_context_t
*ct
)
3728 if (vap
->va_mask
& VATTR_NOSET
)
3731 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3735 * Don't call secpolicy_vnode_setattr, the client cannot
3736 * use its cached attributes to make security decisions
3737 * as the server may be faking mode bits or mapping uid/gid.
3738 * Always just let the server to the checking.
3739 * If we provide the ability to remove basic priviledges
3740 * to setattr (e.g. basic without chmod) then we will
3741 * need to add a check here before calling the server.
3743 error
= nfs4setattr(vp
, vap
, flags
, cr
, NULL
);
3745 if (error
== 0 && (vap
->va_mask
& VATTR_SIZE
) && vap
->va_size
== 0)
3746 vnevent_truncate(vp
, ct
);
3752 * To replace the "guarded" version 3 setattr, we use two types of compound
3754 * 1. The "normal" setattr, used when the size of the file isn't being
3755 * changed - { Putfh <fh>; Setattr; Getattr }/
3756 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3757 * with only ctime as the argument. If the server ctime differs from
3758 * what is cached on the client, the verify will fail, but we would
3759 * already have the ctime from the preceding getattr, so just set it
3760 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3761 * Setattr; Getattr }.
3763 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3764 * this setattr and NULL if they are not.
3767 nfs4setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3770 COMPOUND4args_clnt args
;
3771 COMPOUND4res_clnt res
, *resp
= NULL
;
3772 nfs4_ga_res_t
*garp
= NULL
;
3773 int numops
= 3; /* { Putfh; Setattr; Getattr } */
3774 nfs_argop4 argop
[5];
3775 int verify_argop
= -1;
3776 int setattr_argop
= 1;
3781 uint_t mask
= vap
->va_mask
;
3785 bool_t needrecov
= FALSE
;
3786 nfs4_recov_state_t recov_state
;
3787 nfs4_stateid_types_t sid_types
;
3790 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3794 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
3796 nfs4_init_stateid_types(&sid_types
);
3799 * Only need to flush pages if there are any pages and
3800 * if the file is marked as dirty in some fashion. The
3801 * file must be flushed so that we can accurately
3802 * determine the size of the file and the cached data
3803 * after the SETATTR returns. A file is considered to
3804 * be dirty if it is either marked with R4DIRTY, has
3805 * outstanding i/o's active, or is mmap'd. In this
3806 * last case, we can't tell whether there are dirty
3807 * pages, so we flush just to be sure.
3809 if (nfs4_has_pages(vp
) &&
3810 ((rp
->r_flags
& R4DIRTY
) ||
3812 rp
->r_mapcnt
> 0)) {
3813 ASSERT(vp
->v_type
!= VCHR
);
3814 e
.error
= nfs4_putpage(vp
, 0, 0, 0, cr
, NULL
);
3815 if (e
.error
&& (e
.error
== ENOSPC
|| e
.error
== EDQUOT
)) {
3816 mutex_enter(&rp
->r_statelock
);
3818 rp
->r_error
= e
.error
;
3819 mutex_exit(&rp
->r_statelock
);
3823 if (mask
& VATTR_SIZE
) {
3825 * Verification setattr compound for non-deleg VATTR_SIZE:
3826 * { Putfh; Getattr; Verify; Setattr; Getattr }
3827 * Set ctime local here (outside the do_again label)
3828 * so that subsequent retries (after failed VERIFY)
3829 * will use ctime from GETATTR results (from failed
3830 * verify compound) as VERIFY arg.
3831 * If file has delegation, then VERIFY(time_metadata)
3832 * is of little added value, so don't bother.
3834 mutex_enter(&rp
->r_statev4_lock
);
3835 if (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
||
3836 rp
->r_deleg_return_pending
) {
3838 ctime
= rp
->r_attr
.va_ctime
;
3840 mutex_exit(&rp
->r_statev4_lock
);
3843 recov_state
.rs_flags
= 0;
3844 recov_state
.rs_num_retry_despite_err
= 0;
3846 args
.ctag
= TAG_SETATTR
;
3849 setattr_argop
= numops
- 2;
3852 args
.array_len
= numops
;
3854 e
.error
= nfs4_start_op(VTOMI4(vp
), vp
, NULL
, &recov_state
);
3859 /* putfh target fh */
3860 argop
[0].argop
= OP_CPUTFH
;
3861 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3865 * We only care about the ctime, but need to get mtime
3866 * and size for proper cache update.
3869 argop
[1].argop
= OP_GETATTR
;
3870 argop
[1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
3871 argop
[1].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
3873 /* verify - set later in loop */
3879 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
3880 supp_attrs
= svp
->sv_supp_attrs
;
3881 nfs_rw_exit(&svp
->sv_lock
);
3883 nfs4args_setattr(&argop
[setattr_argop
], vap
, vsap
, flags
, rp
, cr
,
3884 supp_attrs
, &e
.error
, &sid_types
);
3885 stateid
= argop
[setattr_argop
].nfs_argop4_u
.opsetattr
.stateid
;
3887 /* req time field(s) overflow - return immediately */
3888 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
3889 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
3890 opsetattr
.obj_attributes
);
3893 omode
= rp
->r_attr
.va_mode
;
3896 argop
[numops
-1].argop
= OP_GETATTR
;
3897 argop
[numops
-1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
3899 * If we are setting the ACL (indicated only by vsap != NULL), request
3900 * the ACL in this getattr. The ACL returned from this getattr will be
3901 * used in updating the ACL cache.
3904 argop
[numops
-1].nfs_argop4_u
.opgetattr
.attr_request
|=
3906 argop
[numops
-1].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
3909 * setattr iterates if the object size is set and the cached ctime
3910 * does not match the file ctime. In that case, verify the ctime first.
3914 if (verify_argop
!= -1) {
3916 * Verify that the ctime match before doing setattr.
3918 va
.va_mask
= VATTR_CTIME
;
3919 va
.va_ctime
= ctime
;
3921 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
3922 supp_attrs
= svp
->sv_supp_attrs
;
3923 nfs_rw_exit(&svp
->sv_lock
);
3924 e
.error
= nfs4args_verify(&argop
[verify_argop
], &va
,
3925 OP_VERIFY
, supp_attrs
);
3927 /* req time field(s) overflow - return */
3928 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3938 rfs4call(VTOMI4(vp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
3941 * Purge the access cache and ACL cache if changing either the
3942 * owner of the file, the group owner, or the mode. These may
3943 * change the access permissions of the file, so purge old
3944 * information and start over again.
3946 if (mask
& (VATTR_UID
| VATTR_GID
| VATTR_MODE
)) {
3947 (void) nfs4_access_purge_rp(rp
);
3948 if (rp
->r_secattr
!= NULL
) {
3949 mutex_enter(&rp
->r_statelock
);
3950 vsp
= rp
->r_secattr
;
3951 rp
->r_secattr
= NULL
;
3952 mutex_exit(&rp
->r_statelock
);
3954 nfs4_acl_free_cache(vsp
);
3959 * If res.array_len == numops, then everything succeeded,
3960 * except for possibly the final getattr. If only the
3961 * last getattr failed, give up, and don't try recovery.
3963 if (res
.array_len
== numops
) {
3964 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3972 * if either rpc call failed or completely succeeded - done
3974 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
3976 PURGE_ATTRCACHE4(vp
);
3978 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3985 * Do proper retry for OLD_STATEID outside of the normal
3986 * recovery framework.
3988 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3989 sid_types
.cur_sid_type
!= SPEC_SID
&&
3990 sid_types
.cur_sid_type
!= NO_SID
) {
3991 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3993 nfs4_save_stateid(&stateid
, &sid_types
);
3994 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
3995 opsetattr
.obj_attributes
);
3996 if (verify_argop
!= -1) {
3997 nfs4args_verify_free(&argop
[verify_argop
]);
4000 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4007 abort
= nfs4_start_recovery(&e
,
4008 VTOMI4(vp
), vp
, NULL
, NULL
, NULL
,
4009 OP_SETATTR
, NULL
, NULL
, NULL
);
4010 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
4013 * Do not retry if we failed with OLD_STATEID using
4014 * a special stateid. This is done to avoid looping
4015 * with a broken server.
4017 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
4018 (sid_types
.cur_sid_type
== SPEC_SID
||
4019 sid_types
.cur_sid_type
== NO_SID
))
4022 if (res
.status
== NFS4ERR_BADOWNER
)
4023 nfs4_log_badowner(VTOMI4(vp
),
4026 e
.error
= geterrno4(res
.status
);
4027 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4029 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4030 opsetattr
.obj_attributes
);
4031 if (verify_argop
!= -1) {
4032 nfs4args_verify_free(&argop
[verify_argop
]);
4035 if (abort
== FALSE
) {
4037 * Need to retry all possible stateids in
4038 * case the recovery error wasn't stateid
4039 * related or the stateids have become
4040 * stale (server reboot).
4042 nfs4_init_stateid_types(&sid_types
);
4049 * Need to call nfs4_end_op before nfs4getattr to
4050 * avoid potential nfs4_start_op deadlock. See RFE
4051 * 4777612. Calls to nfs4_invalidate_pages() and
4052 * nfs4_purge_stale_fh() might also generate over the
4053 * wire calls which my cause nfs4_start_op() deadlock.
4055 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
4058 * Check to update lease.
4061 if (res
.status
== NFS4_OK
) {
4066 * Check if verify failed to see if try again
4068 if ((verify_argop
== -1) || (res
.array_len
!= 3)) {
4072 if (res
.status
== NFS4ERR_BADOWNER
)
4073 nfs4_log_badowner(VTOMI4(vp
), OP_SETATTR
);
4075 e
.error
= geterrno4(res
.status
);
4078 * When the verify request fails, the client ctime is
4079 * not in sync with the server. This is the same as
4080 * the version 3 "not synchronized" error, and we
4081 * handle it in a similar manner (XXX do we need to???).
4082 * Use the ctime returned in the first getattr for
4083 * the input to the next verify.
4084 * If we couldn't get the attributes, then we give up
4085 * because we can't complete the operation as required.
4087 garp
= &res
.array
[1].nfs_resop4_u
.opgetattr
.ga_res
;
4090 PURGE_ATTRCACHE4(vp
);
4091 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4094 * retry with a new verify value
4096 ctime
= garp
->n4g_va
.va_ctime
;
4097 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4101 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4102 opsetattr
.obj_attributes
);
4103 if (verify_argop
!= -1) {
4104 nfs4args_verify_free(&argop
[verify_argop
]);
4107 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4114 * If we are here, rfs4call has an irrecoverable error - return
4116 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4117 opsetattr
.obj_attributes
);
4118 if (verify_argop
!= -1) {
4119 nfs4args_verify_free(&argop
[verify_argop
]);
4123 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
4130 * If changing the size of the file, invalidate
4131 * any local cached data which is no longer part
4132 * of the file. We also possibly invalidate the
4133 * last page in the file. We could use
4134 * pvn_vpzero(), but this would mark the page as
4135 * modified and require it to be written back to
4136 * the server for no particularly good reason.
4137 * This way, if we access it, then we bring it
4138 * back in. A read should be cheaper than a
4141 if (mask
& VATTR_SIZE
) {
4142 nfs4_invalidate_pages(vp
, (vap
->va_size
& PAGEMASK
), cr
);
4145 /* either no error or one of the postop getattr failed */
4148 * XXX Perform a simplified version of wcc checking. Instead of
4149 * have another getattr to get pre-op, just purge cache if
4150 * any of the ops prior to and including the getattr failed.
4151 * If the getattr succeeded then update the attrcache accordingly.
4155 if (res
.status
== NFS4_OK
) {
4159 resop
= &res
.array
[numops
- 1];
4160 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
4163 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4164 * rather than filling it. See the function itself for details.
4166 e
.error
= nfs4_update_attrcache(res
.status
, garp
, t
, vp
, cr
);
4168 if (garp
->n4g_resbmap
& FATTR4_ACL_MASK
) {
4169 nfs4_acl_fill_cache(rp
, &garp
->n4g_vsa
);
4170 vs_ace4_destroy(&garp
->n4g_vsa
);
4174 * The ACL was supposed to be set and to be
4175 * returned in the last getattr of this
4176 * compound, but for some reason the getattr
4177 * result doesn't contain the ACL. In this
4178 * case, purge the ACL cache.
4180 if (rp
->r_secattr
!= NULL
) {
4181 mutex_enter(&rp
->r_statelock
);
4182 vsp
= rp
->r_secattr
;
4183 rp
->r_secattr
= NULL
;
4184 mutex_exit(&rp
->r_statelock
);
4186 nfs4_acl_free_cache(vsp
);
4192 if (res
.status
== NFS4_OK
&& (mask
& VATTR_SIZE
)) {
4194 * Set the size, rather than relying on getting it updated
4195 * via a GETATTR. With delegations the client tries to
4196 * suppress GETATTR calls.
4198 mutex_enter(&rp
->r_statelock
);
4199 rp
->r_size
= vap
->va_size
;
4200 mutex_exit(&rp
->r_statelock
);
4204 * Can free up request args and res
4206 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4207 opsetattr
.obj_attributes
);
4208 if (verify_argop
!= -1) {
4209 nfs4args_verify_free(&argop
[verify_argop
]);
4212 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4215 * Some servers will change the mode to clear the setuid
4216 * and setgid bits when changing the uid or gid. The
4217 * client needs to compensate appropriately.
4219 if (mask
& (VATTR_UID
| VATTR_GID
)) {
4220 int terror
, do_setattr
;
4223 va
.va_mask
= VATTR_MODE
;
4224 terror
= nfs4getattr(vp
, &va
, cr
);
4226 (((mask
& VATTR_MODE
) && va
.va_mode
!= vap
->va_mode
) ||
4227 (!(mask
& VATTR_MODE
) && va
.va_mode
!= omode
))) {
4228 va
.va_mask
= VATTR_MODE
;
4229 if (mask
& VATTR_MODE
) {
4231 * We asked the mode to be changed and what
4232 * we just got from the server in getattr is
4233 * not what we wanted it to be, so set it now.
4235 va
.va_mode
= vap
->va_mode
;
4239 * We did not ask the mode to be changed,
4240 * Check to see that the server just cleared
4241 * I_SUID and I_GUID from it. If not then
4242 * set mode to omode with UID/GID cleared.
4244 if (nfs4_compare_modes(va
.va_mode
, omode
)) {
4245 omode
&= ~(S_ISUID
|S_ISGID
);
4252 (void) nfs4setattr(vp
, &va
, 0, cr
, NULL
);
4261 nfs4_access(vnode_t
*vp
, int mode
, int flags
, cred_t
*cr
, caller_context_t
*ct
)
4263 COMPOUND4args_clnt args
;
4264 COMPOUND4res_clnt res
;
4266 uint32_t acc
, resacc
, argacc
;
4268 cred_t
*cred
, *ncr
, *ncrfree
= NULL
;
4269 nfs4_access_type_t cacc
;
4271 nfs_argop4 argop
[3];
4273 bool_t needrecov
= FALSE
, do_getattr
;
4274 nfs4_recov_state_t recov_state
;
4277 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4278 mntinfo4_t
*mi
= VTOMI4(vp
);
4280 if (nfs_zone() != mi
->mi_zone
)
4285 acc
|= ACCESS4_READ
;
4286 if (mode
& VWRITE
) {
4287 if ((vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
) && !ISVDEV(vp
->v_type
))
4289 if (vp
->v_type
== VDIR
)
4290 acc
|= ACCESS4_DELETE
;
4291 acc
|= ACCESS4_MODIFY
| ACCESS4_EXTEND
;
4294 if (vp
->v_type
== VDIR
)
4295 acc
|= ACCESS4_LOOKUP
;
4297 acc
|= ACCESS4_EXECUTE
;
4300 if (VTOR4(vp
)->r_acache
!= NULL
) {
4301 e
.error
= nfs4_validate_caches(vp
, cr
);
4307 if (vp
->v_type
== VDIR
)
4308 argacc
= ACCESS4_READ
| ACCESS4_DELETE
| ACCESS4_MODIFY
|
4309 ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
4311 argacc
= ACCESS4_READ
| ACCESS4_MODIFY
| ACCESS4_EXTEND
|
4313 recov_state
.rs_flags
= 0;
4314 recov_state
.rs_num_retry_despite_err
= 0;
4318 * ncr and ncrfree both initially
4319 * point to the memory area returned
4321 * ncrfree not NULL when exiting means
4322 * that we need to release it
4324 ncr
= crnetadjust(cred
);
4328 cacc
= nfs4_access_check(rp
, acc
, cred
);
4329 if (cacc
== NFS4_ACCESS_ALLOWED
) {
4330 if (ncrfree
!= NULL
)
4334 if (cacc
== NFS4_ACCESS_DENIED
) {
4336 * If the cred can be adjusted, try again
4337 * with the new cred.
4344 if (ncrfree
!= NULL
)
4351 * Don't take with r_statev4_lock here. r_deleg_type could
4352 * change as soon as lock is released. Since it is an int,
4353 * there is no atomicity issue.
4355 do_getattr
= (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
);
4356 num_ops
= do_getattr
? 3 : 2;
4358 args
.ctag
= TAG_ACCESS
;
4360 args
.array_len
= num_ops
;
4363 if (e
.error
= nfs4_start_fop(mi
, vp
, NULL
, OH_ACCESS
,
4364 &recov_state
, NULL
)) {
4365 if (ncrfree
!= NULL
)
4370 /* putfh target fh */
4371 argop
[0].argop
= OP_CPUTFH
;
4372 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
4375 argop
[1].argop
= OP_ACCESS
;
4376 argop
[1].nfs_argop4_u
.opaccess
.access
= argacc
;
4380 argop
[2].argop
= OP_GETATTR
;
4381 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
4382 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
4385 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
4386 "nfs4_access: %s call, rp %s", needrecov
? "recov" : "first",
4387 rnode4info(VTOR4(vp
))));
4391 rfs4call(VTOMI4(vp
), &args
, &res
, cred
, &doqueue
, 0, &e
);
4392 rpc_error
= e
.error
;
4394 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
4396 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
4397 "nfs4_access: initiating recovery\n"));
4399 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
4400 NULL
, OP_ACCESS
, NULL
, NULL
, NULL
) == FALSE
) {
4401 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_ACCESS
,
4402 &recov_state
, needrecov
);
4404 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4408 nfs4_end_fop(mi
, vp
, NULL
, OH_ACCESS
, &recov_state
, needrecov
);
4414 e
.error
= geterrno4(res
.status
);
4416 * This might generate over the wire calls throught
4417 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4418 * here to avoid a deadlock.
4420 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4423 resop
= &res
.array
[1]; /* access res */
4425 resacc
= resop
->nfs_resop4_u
.opaccess
.access
;
4428 resop
++; /* getattr res */
4429 nfs4_attr_cache(vp
, &resop
->nfs_resop4_u
.opgetattr
.ga_res
,
4430 t
, cr
, FALSE
, NULL
);
4434 nfs4_access_cache(rp
, argacc
, resacc
, cred
);
4436 * we just cached results with cred; if cred is the
4437 * adjusted credentials from crnetadjust, we do not want
4438 * to release them before exiting: hence setting ncrfree
4443 /* XXX check the supported bits too? */
4444 if ((acc
& resacc
) != acc
) {
4446 * The following code implements the semantic
4447 * that a setuid root program has *at least* the
4448 * permissions of the user that is running the
4449 * program. See rfs3call() for more portions
4450 * of the implementation of this functionality.
4454 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4465 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4467 if (ncrfree
!= NULL
)
4475 nfs4_readlink(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, caller_context_t
*ct
)
4477 COMPOUND4args_clnt args
;
4478 COMPOUND4res_clnt res
;
4481 nfs_argop4 argop
[3];
4483 READLINK4res
*lr_res
;
4484 nfs4_ga_res_t
*garp
;
4487 bool_t needrecov
= FALSE
;
4488 nfs4_recov_state_t recov_state
;
4490 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4492 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
4495 * Can't readlink anything other than a symbolic link.
4497 if (vp
->v_type
!= VLNK
)
4501 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
!= NULL
) {
4502 e
.error
= nfs4_validate_caches(vp
, cr
);
4505 mutex_enter(&rp
->r_statelock
);
4506 if (rp
->r_symlink
.contents
!= NULL
) {
4507 e
.error
= uiomove(rp
->r_symlink
.contents
,
4508 rp
->r_symlink
.len
, UIO_READ
, uiop
);
4509 mutex_exit(&rp
->r_statelock
);
4512 mutex_exit(&rp
->r_statelock
);
4514 recov_state
.rs_flags
= 0;
4515 recov_state
.rs_num_retry_despite_err
= 0;
4520 args
.ctag
= TAG_READLINK
;
4522 e
.error
= nfs4_start_op(VTOMI4(vp
), vp
, NULL
, &recov_state
);
4527 /* 0. putfh symlink fh */
4528 argop
[0].argop
= OP_CPUTFH
;
4529 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
4532 argop
[1].argop
= OP_READLINK
;
4535 argop
[2].argop
= OP_GETATTR
;
4536 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
4537 argop
[2].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
4541 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
4542 "nfs4_readlink: %s call, rp %s", needrecov
? "recov" : "first",
4543 rnode4info(VTOR4(vp
))));
4547 rfs4call(VTOMI4(vp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
4549 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
4551 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
4552 "nfs4_readlink: initiating recovery\n"));
4554 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
4555 NULL
, OP_READLINK
, NULL
, NULL
, NULL
) == FALSE
) {
4557 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4559 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
4565 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
4571 * There is an path in the code below which calls
4572 * nfs4_purge_stale_fh(), which may generate otw calls through
4573 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4574 * here to avoid nfs4_start_op() deadlock.
4577 if (res
.status
&& (res
.array_len
< args
.array_len
)) {
4579 * either Putfh or Link failed
4581 e
.error
= geterrno4(res
.status
);
4582 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4583 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4587 resop
= &res
.array
[1]; /* readlink res */
4588 lr_res
= &resop
->nfs_resop4_u
.opreadlink
;
4591 * treat symlink names as data
4593 linkdata
= utf8_to_str((utf8string
*)&lr_res
->link
, &len
, NULL
);
4594 if (linkdata
!= NULL
) {
4595 int uio_len
= len
- 1;
4596 /* len includes null byte, which we won't uiomove */
4597 e
.error
= uiomove(linkdata
, uio_len
, UIO_READ
, uiop
);
4598 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
== NULL
) {
4599 mutex_enter(&rp
->r_statelock
);
4600 if (rp
->r_symlink
.contents
== NULL
) {
4601 rp
->r_symlink
.contents
= linkdata
;
4602 rp
->r_symlink
.len
= uio_len
;
4603 rp
->r_symlink
.size
= len
;
4604 mutex_exit(&rp
->r_statelock
);
4606 mutex_exit(&rp
->r_statelock
);
4607 kmem_free(linkdata
, len
);
4610 kmem_free(linkdata
, len
);
4613 if (res
.status
== NFS4_OK
) {
4614 resop
++; /* getattr res */
4615 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
4617 e
.error
= nfs4_update_attrcache(res
.status
, garp
, t
, vp
, cr
);
4619 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4622 * The over the wire error for attempting to readlink something
4623 * other than a symbolic link is ENXIO. However, we need to
4624 * return EINVAL instead of ENXIO, so we map it here.
4626 return (e
.error
== ENXIO
? EINVAL
: e
.error
);
4630 * Flush local dirty pages to stable storage on the server.
4632 * If FNODSYNC is specified, then there is nothing to do because
4633 * metadata changes are not cached on the client before being
4634 * sent to the server.
4638 nfs4_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
4642 if ((syncflag
& FNODSYNC
) || IS_SWAPVP(vp
))
4644 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
4646 error
= nfs4_putpage_commit(vp
, 0, 0, cr
);
4648 error
= VTOR4(vp
)->r_error
;
4653 * Weirdness: if the file was removed or the target of a rename
4654 * operation while it was open, it got renamed instead. Here we
4655 * remove the renamed file.
4659 nfs4_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
4663 ASSERT(vp
!= DNLC_NO_VNODE
);
4667 if (IS_SHADOW(vp
, rp
)) {
4673 * If this is coming from the wrong zone, we let someone in the right
4674 * zone take care of it asynchronously. We can get here due to
4675 * VN_RELE() being called from pageout() or fsflush(). This call may
4676 * potentially turn into an expensive no-op if, for instance, v_count
4677 * gets incremented in the meantime, but it's still correct.
4679 if (nfs_zone() != VTOMI4(vp
)->mi_zone
) {
4680 nfs4_async_inactive(vp
, cr
);
4685 * Some of the cleanup steps might require over-the-wire
4686 * operations. Since fop_inactive can get called as a result of
4687 * other over-the-wire operations (e.g., an attribute cache update
4688 * can lead to a DNLC purge), doing those steps now would lead to a
4689 * nested call to the recovery framework, which can deadlock. So
4690 * do any over-the-wire cleanups asynchronously, in a separate
4694 mutex_enter(&rp
->r_os_lock
);
4695 mutex_enter(&rp
->r_statelock
);
4696 mutex_enter(&rp
->r_statev4_lock
);
4698 if (vp
->v_type
== VREG
&& list_head(&rp
->r_open_streams
) != NULL
) {
4699 mutex_exit(&rp
->r_statev4_lock
);
4700 mutex_exit(&rp
->r_statelock
);
4701 mutex_exit(&rp
->r_os_lock
);
4702 nfs4_async_inactive(vp
, cr
);
4706 if (rp
->r_deleg_type
== OPEN_DELEGATE_READ
||
4707 rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
) {
4708 mutex_exit(&rp
->r_statev4_lock
);
4709 mutex_exit(&rp
->r_statelock
);
4710 mutex_exit(&rp
->r_os_lock
);
4711 nfs4_async_inactive(vp
, cr
);
4715 if (rp
->r_unldvp
!= NULL
) {
4716 mutex_exit(&rp
->r_statev4_lock
);
4717 mutex_exit(&rp
->r_statelock
);
4718 mutex_exit(&rp
->r_os_lock
);
4719 nfs4_async_inactive(vp
, cr
);
4722 mutex_exit(&rp
->r_statev4_lock
);
4723 mutex_exit(&rp
->r_statelock
);
4724 mutex_exit(&rp
->r_os_lock
);
4726 rp4_addfree(rp
, cr
);
4730 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4731 * various bits of state. The caller must not refer to vp after this call.
4735 nfs4_inactive_otw(vnode_t
*vp
, cred_t
*cr
)
4737 rnode4_t
*rp
= VTOR4(vp
);
4738 nfs4_recov_state_t recov_state
;
4739 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4743 COMPOUND4args_clnt args
;
4744 COMPOUND4res_clnt res
, *resp
;
4745 nfs_argop4 argop
[2];
4751 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
4752 ASSERT(!IS_SHADOW(vp
, rp
));
4755 name
= fn_name(VTOSV(vp
)->sv_name
);
4756 NFS4_DEBUG(nfs4_client_inactive_debug
, (CE_NOTE
, "nfs4_inactive_otw: "
4757 "release vnode %s", name
));
4758 kmem_free(name
, MAXNAMELEN
);
4761 if (vp
->v_type
== VREG
) {
4762 bool_t recov_failed
= FALSE
;
4764 e
.error
= nfs4close_all(vp
, cr
);
4766 /* Check to see if recovery failed */
4767 mutex_enter(&(VTOMI4(vp
)->mi_lock
));
4768 if (VTOMI4(vp
)->mi_flags
& MI4_RECOV_FAIL
)
4769 recov_failed
= TRUE
;
4770 mutex_exit(&(VTOMI4(vp
)->mi_lock
));
4771 if (!recov_failed
) {
4772 mutex_enter(&rp
->r_statelock
);
4773 if (rp
->r_flags
& R4RECOVERR
)
4774 recov_failed
= TRUE
;
4775 mutex_exit(&rp
->r_statelock
);
4778 NFS4_DEBUG(nfs4_client_recov_debug
,
4779 (CE_NOTE
, "nfs4_inactive_otw: "
4780 "close failed (recovery failure)"));
4786 if (rp
->r_unldvp
== NULL
) {
4787 rp4_addfree(rp
, cr
);
4792 * Save the vnode pointer for the directory where the
4793 * unlinked-open file got renamed, then set it to NULL
4794 * to prevent another thread from getting here before
4795 * we're done with the remove. While we have the
4796 * statelock, make local copies of the pertinent rnode
4797 * fields. If we weren't to do this in an atomic way, the
4798 * the unl* fields could become inconsistent with respect
4799 * to each other due to a race condition between this
4800 * code and nfs_remove(). See bug report 1034328.
4802 mutex_enter(&rp
->r_statelock
);
4803 if (rp
->r_unldvp
== NULL
) {
4804 mutex_exit(&rp
->r_statelock
);
4805 rp4_addfree(rp
, cr
);
4809 unldvp
= rp
->r_unldvp
;
4810 rp
->r_unldvp
= NULL
;
4811 unlname
= rp
->r_unlname
;
4812 rp
->r_unlname
= NULL
;
4813 unlcred
= rp
->r_unlcred
;
4814 rp
->r_unlcred
= NULL
;
4815 mutex_exit(&rp
->r_statelock
);
4818 * If there are any dirty pages left, then flush
4819 * them. This is unfortunate because they just
4820 * may get thrown away during the remove operation,
4821 * but we have to do this for correctness.
4823 if (nfs4_has_pages(vp
) &&
4824 ((rp
->r_flags
& R4DIRTY
) || rp
->r_count
> 0)) {
4825 ASSERT(vp
->v_type
!= VCHR
);
4826 e
.error
= nfs4_putpage(vp
, 0, 0, 0, cr
, NULL
);
4828 mutex_enter(&rp
->r_statelock
);
4830 rp
->r_error
= e
.error
;
4831 mutex_exit(&rp
->r_statelock
);
4835 recov_state
.rs_flags
= 0;
4836 recov_state
.rs_num_retry_despite_err
= 0;
4839 * Do the remove operation on the renamed file
4841 args
.ctag
= TAG_INACTIVE
;
4844 * Remove ops: putfh dir; remove
4849 e
.error
= nfs4_start_op(VTOMI4(unldvp
), unldvp
, NULL
, &recov_state
);
4851 kmem_free(unlname
, MAXNAMELEN
);
4855 * Try again; this time around r_unldvp will be NULL, so we'll
4856 * just call rp4_addfree() and return.
4861 /* putfh directory */
4862 argop
[0].argop
= OP_CPUTFH
;
4863 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(unldvp
)->r_fh
;
4866 argop
[1].argop
= OP_CREMOVE
;
4867 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= unlname
;
4874 * Can't do this yet. We may be being called from
4875 * dnlc_purge_XXX while that routine is holding a
4876 * mutex lock to the nc_rele list. The calls to
4877 * nfs3_cache_wcc_data may result in calls to
4878 * dnlc_purge_XXX. This will result in a deadlock.
4880 rfs4call(VTOMI4(unldvp
), &args
, &res
, unlcred
, &doqueue
, 0, &e
);
4882 PURGE_ATTRCACHE4(unldvp
);
4884 } else if (res
.status
) {
4885 e
.error
= geterrno4(res
.status
);
4886 PURGE_ATTRCACHE4(unldvp
);
4888 * This code is inactive right now
4889 * but if made active there should
4890 * be a nfs4_end_op() call before
4891 * nfs4_purge_stale_fh to avoid start_op()
4892 * deadlock. See BugId: 4948726
4894 nfs4_purge_stale_fh(error
, unldvp
, cr
);
4899 resop
= &res
.array
[1];
4900 rm_res
= &resop
->nfs_resop4_u
.opremove
;
4902 * Update directory cache attribute,
4903 * readdir and dnlc caches.
4905 nfs4_update_dircaches(&rm_res
->cinfo
, unldvp
, NULL
, NULL
, NULL
);
4908 rfs4call(VTOMI4(unldvp
), &args
, &res
, unlcred
, &doqueue
, 0, &e
);
4910 PURGE_ATTRCACHE4(unldvp
);
4913 if (nfs4_needs_recovery(&e
, FALSE
, unldvp
->v_vfsp
)) {
4914 if (nfs4_start_recovery(&e
, VTOMI4(unldvp
), unldvp
, NULL
,
4915 NULL
, NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
4917 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4918 nfs4_end_op(VTOMI4(unldvp
), unldvp
, NULL
,
4919 &recov_state
, TRUE
);
4920 goto recov_retry_remove
;
4923 nfs4_end_op(VTOMI4(unldvp
), unldvp
, NULL
, &recov_state
, FALSE
);
4926 * Release stuff held for the remove
4929 if (!e
.error
&& resp
)
4930 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
4932 kmem_free(unlname
, MAXNAMELEN
);
4938 * Remote file system operations having to do with directory manipulation.
4942 nfs4_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
4943 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
4944 int *direntflags
, pathname_t
*realpnp
)
4947 vnode_t
*vp
, *avp
= NULL
;
4951 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
4954 * if LOOKUP_XATTR, must replace dvp (object) with
4955 * object's attrdir before continuing with lookup
4957 if (flags
& LOOKUP_XATTR
) {
4958 error
= nfs4lookup_xattr(dvp
, nm
, &avp
, flags
, cr
);
4965 * If lookup is for "", just return dvp now. The attrdir
4966 * has already been activated (from nfs4lookup_xattr), and
4967 * the caller will RELE the original dvp -- not
4968 * the attrdir. So, set vpp and return.
4969 * Currently, when the LOOKUP_XATTR flag is
4970 * passed to fop_lookup, the name is always empty, and
4971 * shortcircuiting here avoids 3 unneeded lock/unlock
4974 * If a non-empty name was provided, then it is the
4975 * attribute name, and it will be looked up below.
4983 * The vfs layer never sends a name when asking for the
4984 * attrdir, so we should never get here (unless of course
4985 * name is passed at some time in future -- at which time
4986 * we'll blow up here).
4992 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
4995 error
= nfs4lookup(dvp
, nm
, vpp
, cr
, 0);
4996 nfs_rw_exit(&drp
->r_rwlock
);
4999 * If vnode is a device, create special vnode.
5001 if (!error
&& ISVDEV((*vpp
)->v_type
)) {
5003 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
5012 nfs4lookup_xattr(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, int flags
, cred_t
*cr
)
5016 int cflag
= ((flags
& CREATE_XATTR_DIR
) != 0);
5020 if (!(mi
->mi_vfsp
->vfs_flag
& VFS_XATTR
) &&
5021 !vfs_has_feature(mi
->mi_vfsp
, VFSFT_SYSATTR_VIEWS
))
5025 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
5028 mutex_enter(&drp
->r_statelock
);
5030 * If the server doesn't support xattrs just return EINVAL
5032 if (drp
->r_xattr_dir
== NFS4_XATTR_DIR_NOTSUPP
) {
5033 mutex_exit(&drp
->r_statelock
);
5034 nfs_rw_exit(&drp
->r_rwlock
);
5039 * If there is a cached xattr directory entry,
5040 * use it as long as the attributes are valid. If the
5041 * attributes are not valid, take the simple approach and
5042 * free the cached value and re-fetch a new value.
5044 * We don't negative entry cache for now, if we did we
5045 * would need to check if the file has changed on every
5046 * lookup. But xattrs don't exist very often and failing
5047 * an openattr is not much more expensive than and NVERIFY or GETATTR
5048 * so do an openattr over the wire for now.
5050 if (drp
->r_xattr_dir
!= NULL
) {
5051 if (ATTRCACHE4_VALID(dvp
)) {
5052 VN_HOLD(drp
->r_xattr_dir
);
5053 *vpp
= drp
->r_xattr_dir
;
5054 mutex_exit(&drp
->r_statelock
);
5055 nfs_rw_exit(&drp
->r_rwlock
);
5058 VN_RELE(drp
->r_xattr_dir
);
5059 drp
->r_xattr_dir
= NULL
;
5061 mutex_exit(&drp
->r_statelock
);
5063 error
= nfs4openattr(dvp
, vpp
, cflag
, cr
);
5065 nfs_rw_exit(&drp
->r_rwlock
);
5071 nfs4lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
, int skipdnlc
)
5076 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5079 * If lookup is for "", just return dvp. Don't need
5080 * to send it over the wire, look it up in the dnlc,
5081 * or perform any access checks.
5090 * Can't do lookups in non-directories.
5092 if (dvp
->v_type
!= VDIR
)
5096 * If lookup is for ".", just return dvp. Don't need
5097 * to send it over the wire or look it up in the dnlc,
5098 * just need to check access.
5100 if (nm
[0] == '.' && nm
[1] == '\0') {
5101 error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5110 if (!(drp
->r_flags
& R4LOOKUP
)) {
5111 mutex_enter(&drp
->r_statelock
);
5112 drp
->r_flags
|= R4LOOKUP
;
5113 mutex_exit(&drp
->r_statelock
);
5118 * Lookup this name in the DNLC. If there is no entry
5119 * lookup over the wire.
5122 *vpp
= dnlc_lookup(dvp
, nm
);
5125 * We need to go over the wire to lookup the name.
5127 return (nfs4lookupnew_otw(dvp
, nm
, vpp
, cr
));
5131 * We hit on the dnlc
5133 if (*vpp
!= DNLC_NO_VNODE
||
5134 (dvp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)) {
5136 * But our attrs may not be valid.
5138 if (ATTRCACHE4_VALID(dvp
)) {
5139 error
= nfs4_waitfor_purge_complete(dvp
);
5147 * If after the purge completes, check to make sure
5148 * our attrs are still valid.
5150 if (ATTRCACHE4_VALID(dvp
)) {
5152 * If we waited for a purge we may have
5153 * lost our vnode so look it up again.
5156 *vpp
= dnlc_lookup(dvp
, nm
);
5158 return (nfs4lookupnew_otw(dvp
,
5162 * The access cache should almost always hit
5164 error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5171 if (*vpp
== DNLC_NO_VNODE
) {
5181 ASSERT(*vpp
!= NULL
);
5184 * We may have gotten here we have one of the following cases:
5185 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5186 * need to validate them.
5187 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5190 * Go to the server and check if the directory has changed, if
5191 * it hasn't we are done and can use the dnlc entry.
5193 return (nfs4lookupvalidate_otw(dvp
, nm
, vpp
, cr
));
5197 * Go to the server and check if the directory has changed, if
5198 * it hasn't we are done and can use the dnlc entry. If it
5199 * has changed we get a new copy of its attributes and check
5200 * the access for VEXEC, then relookup the filename and
5201 * get its filehandle and attributes.
5203 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5204 * if the NVERIFY failed we must
5206 * cache new attributes (will set r_time_attr_inval)
5208 * recheck VEXEC access
5209 * add name to dnlc, possibly negative
5210 * if LOOKUP succeeded
5211 * cache new attributes
5213 * set a new r_time_attr_inval for dvp
5214 * check to make sure we have access
5216 * The vpp returned is the vnode passed in if the directory is valid,
5217 * a new vnode if successful lookup, or NULL on error.
5220 nfs4lookupvalidate_otw(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
)
5222 COMPOUND4args_clnt args
;
5223 COMPOUND4res_clnt res
;
5225 fattr4_change dchange
;
5227 int argoplist_size
= 7 * sizeof (nfs_argop4
);
5231 nfs4_recov_state_t recov_state
;
5236 nfs4_sharedfh_t
*sfhp
;
5237 nfs4_access_type_t cacc
;
5239 rnode4_t
*drp
= VTOR4(dvp
);
5240 nfs4_ga_res_t
*garp
= NULL
;
5241 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
5243 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5245 ASSERT(nm
[0] != '\0');
5246 ASSERT(dvp
->v_type
== VDIR
);
5247 ASSERT(nm
[0] != '.' || nm
[1] != '\0');
5248 ASSERT(*vpp
!= NULL
);
5250 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0') {
5252 args
.ctag
= TAG_LOOKUP_VPARENT
;
5255 * If dvp were a stub, it should have triggered and caused
5256 * a mount for us to get this far.
5258 ASSERT(!RP_ISSTUB(VTOR4(dvp
)));
5261 args
.ctag
= TAG_LOOKUP_VALID
;
5265 recov_state
.rs_flags
= 0;
5266 recov_state
.rs_num_retry_despite_err
= 0;
5270 /* Save the original mount point security information */
5271 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
5274 e
.error
= nfs4_start_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5275 &recov_state
, NULL
);
5277 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5283 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
5285 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5290 argop
[0].argop
= OP_CPUTFH
;
5291 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(dvp
)->r_fh
;
5293 /* 1. nverify the change info */
5294 argop
[1].argop
= OP_NVERIFY
;
5295 ver_fattr
= &argop
[1].nfs_argop4_u
.opnverify
.obj_attributes
;
5296 ver_fattr
->attrmask
= FATTR4_CHANGE_MASK
;
5297 ver_fattr
->attrlist4
= (char *)&dchange
;
5298 ptr
= (int32_t *)&dchange
;
5299 IXDR_PUT_HYPER(ptr
, VTOR4(dvp
)->r_change
);
5300 ver_fattr
->attrlist4_len
= sizeof (fattr4_change
);
5302 /* 2. getattr directory */
5303 argop
[2].argop
= OP_GETATTR
;
5304 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5305 argop
[2].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5307 /* 3. access directory */
5308 argop
[3].argop
= OP_ACCESS
;
5309 argop
[3].nfs_argop4_u
.opaccess
.access
= ACCESS4_READ
| ACCESS4_DELETE
|
5310 ACCESS4_MODIFY
| ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
5312 /* 4. lookup name */
5314 argop
[4].argop
= OP_LOOKUPP
;
5316 argop
[4].argop
= OP_CLOOKUP
;
5317 argop
[4].nfs_argop4_u
.opclookup
.cname
= nm
;
5320 /* 5. resulting file handle */
5321 argop
[5].argop
= OP_GETFH
;
5323 /* 6. resulting file attributes */
5324 argop
[6].argop
= OP_GETATTR
;
5325 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5326 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5331 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
5333 if (!isdotdot
&& res
.status
== NFS4ERR_MOVED
) {
5334 e
.error
= nfs4_setup_referral(dvp
, nm
, vpp
, cr
);
5335 if (e
.error
!= 0 && *vpp
!= NULL
)
5337 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5338 &recov_state
, FALSE
);
5339 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5340 kmem_free(argop
, argoplist_size
);
5344 if (nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
)) {
5346 * For WRONGSEC of a non-dotdot case, send secinfo directly
5347 * from this thread, do not go thru the recovery thread since
5348 * we need the nm information.
5350 * Not doing dotdot case because there is no specification
5351 * for (PUTFH, SECINFO "..") yet.
5353 if (!isdotdot
&& res
.status
== NFS4ERR_WRONGSEC
) {
5354 if ((e
.error
= nfs4_secinfo_vnode_otw(dvp
, nm
, cr
)))
5355 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5356 &recov_state
, FALSE
);
5358 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5359 &recov_state
, TRUE
);
5360 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5361 kmem_free(argop
, argoplist_size
);
5364 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5370 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
5371 OP_LOOKUP
, NULL
, NULL
, NULL
) == FALSE
) {
5372 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5373 &recov_state
, TRUE
);
5375 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5376 kmem_free(argop
, argoplist_size
);
5381 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
, &recov_state
, FALSE
);
5383 if (e
.error
|| res
.array_len
== 0) {
5385 * If e.error isn't set, then reply has no ops (or we couldn't
5386 * be here). The only legal way to reply without an op array
5387 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5388 * be in the reply for all other status values.
5390 * For valid replies without an ops array, return ENOTSUP
5391 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5392 * return EIO -- don't trust status.
5395 e
.error
= (res
.status
== NFS4ERR_MINOR_VERS_MISMATCH
) ?
5399 kmem_free(argop
, argoplist_size
);
5400 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5404 if (res
.status
!= NFS4ERR_SAME
) {
5405 e
.error
= geterrno4(res
.status
);
5408 * The NVERIFY "failed" so the directory has changed
5409 * First make sure PUTFH succeeded and NVERIFY "failed"
5412 if ((res
.array
[0].nfs_resop4_u
.opputfh
.status
!= NFS4_OK
) ||
5413 (res
.array
[1].nfs_resop4_u
.opnverify
.status
!= NFS4_OK
)) {
5414 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5421 * We know the NVERIFY "failed" so we must:
5422 * purge the caches (access and indirectly dnlc if needed)
5424 nfs4_purge_caches(dvp
, NFS4_NOPURGE_DNLC
, cr
, TRUE
);
5426 if (res
.array
[2].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5427 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5434 * Install new cached attributes for the directory
5436 nfs4_attr_cache(dvp
,
5437 &res
.array
[2].nfs_resop4_u
.opgetattr
.ga_res
,
5438 t
, cr
, FALSE
, NULL
);
5440 if (res
.array
[3].nfs_resop4_u
.opaccess
.status
!= NFS4_OK
) {
5441 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5444 e
.error
= geterrno4(res
.status
);
5449 * Now we know the directory is valid,
5450 * cache new directory access
5452 nfs4_access_cache(drp
,
5453 args
.array
[3].nfs_argop4_u
.opaccess
.access
,
5454 res
.array
[3].nfs_resop4_u
.opaccess
.access
, cr
);
5457 * recheck VEXEC access
5459 cacc
= nfs4_access_check(drp
, ACCESS4_LOOKUP
, cr
);
5460 if (cacc
!= NFS4_ACCESS_ALLOWED
) {
5462 * Directory permissions might have been revoked
5464 if (cacc
== NFS4_ACCESS_DENIED
) {
5472 * Somehow we must not have asked for enough
5473 * so try a singleton ACCESS, should never happen.
5475 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5483 e
.error
= geterrno4(res
.status
);
5484 if (res
.array
[4].nfs_resop4_u
.oplookup
.status
!= NFS4_OK
) {
5486 * The lookup failed, probably no entry
5488 if (e
.error
== ENOENT
&& nfs4_lookup_neg_cache
) {
5489 dnlc_update(dvp
, nm
, DNLC_NO_VNODE
);
5492 * Might be some other error, so remove
5493 * the dnlc entry to make sure we start all
5494 * over again, next time.
5496 dnlc_remove(dvp
, nm
);
5503 if (res
.array
[5].nfs_resop4_u
.opgetfh
.status
!= NFS4_OK
) {
5505 * The file exists but we can't get its fh for
5506 * some unknown reason. Remove it from the dnlc
5507 * and error out to be safe.
5509 dnlc_remove(dvp
, nm
);
5514 fhp
= &res
.array
[5].nfs_resop4_u
.opgetfh
.object
;
5515 if (fhp
->nfs_fh4_len
== 0) {
5517 * The file exists but a bogus fh
5518 * some unknown reason. Remove it from the dnlc
5519 * and error out to be safe.
5522 dnlc_remove(dvp
, nm
);
5527 sfhp
= sfh4_get(fhp
, mi
);
5529 if (res
.array
[6].nfs_resop4_u
.opgetattr
.status
== NFS4_OK
)
5530 garp
= &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
5533 * Make the new rnode
5536 e
.error
= nfs4_make_dotdot(sfhp
, t
, dvp
, cr
, &nvp
, 1);
5544 * XXX if nfs4_make_dotdot uses an existing rnode
5545 * XXX it doesn't update the attributes.
5546 * XXX for now just save them again to save an OTW
5548 nfs4_attr_cache(nvp
, garp
, t
, cr
, FALSE
, NULL
);
5550 nvp
= makenfs4node(sfhp
, garp
, dvp
->v_vfsp
, t
, cr
,
5551 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
5553 * If v_type == VNON, then garp was NULL because
5554 * the last op in the compound failed and makenfs4node
5555 * could not find the vnode for sfhp. It created
5556 * a new vnode, so we have nothing to purge here.
5558 if (nvp
->v_type
== VNON
) {
5561 vattr
.va_mask
= VATTR_TYPE
;
5563 * N.B. We've already called nfs4_end_fop above.
5565 e
.error
= nfs4getattr(nvp
, &vattr
, cr
);
5573 nvp
->v_type
= vattr
.va_type
;
5579 mutex_enter(&nrp
->r_statev4_lock
);
5580 if (!nrp
->created_v4
) {
5581 mutex_exit(&nrp
->r_statev4_lock
);
5582 dnlc_update(dvp
, nm
, nvp
);
5584 mutex_exit(&nrp
->r_statev4_lock
);
5595 * Because the NVERIFY "succeeded" we know that the
5596 * directory attributes are still valid
5597 * so update r_time_attr_inval
5600 mutex_enter(&drp
->r_statelock
);
5601 if (!(mi
->mi_flags
& MI4_NOAC
) && !(dvp
->v_flag
& VNOCACHE
)) {
5602 delta
= now
- drp
->r_time_attr_saved
;
5603 if (delta
< mi
->mi_acdirmin
)
5604 delta
= mi
->mi_acdirmin
;
5605 else if (delta
> mi
->mi_acdirmax
)
5606 delta
= mi
->mi_acdirmax
;
5608 drp
->r_time_attr_inval
= now
+ delta
;
5609 mutex_exit(&drp
->r_statelock
);
5610 dnlc_update(dvp
, nm
, *vpp
);
5613 * Even though we have a valid directory attr cache
5614 * and dnlc entry, we may not have access.
5615 * This should almost always hit the cache.
5617 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5623 if (*vpp
== DNLC_NO_VNODE
) {
5631 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5632 kmem_free(argop
, argoplist_size
);
5633 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5638 * We need to go over the wire to lookup the name, but
5639 * while we are there verify the directory has not
5640 * changed but if it has, get new attributes and check access
5642 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5643 * NVERIFY GETATTR ACCESS
5646 * if the NVERIFY failed we must purge the caches, add new attributes,
5647 * and cache new access.
5648 * set a new r_time_attr_inval
5649 * add name to dnlc, possibly negative
5650 * if LOOKUP succeeded
5651 * cache new attributes
5654 nfs4lookupnew_otw(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
)
5656 COMPOUND4args_clnt args
;
5657 COMPOUND4res_clnt res
;
5659 fattr4_change dchange
;
5661 nfs4_ga_res_t
*garp
= NULL
;
5662 int argoplist_size
= 9 * sizeof (nfs_argop4
);
5666 nfs4_recov_state_t recov_state
;
5671 nfs4_sharedfh_t
*sfhp
;
5672 nfs4_access_type_t cacc
;
5674 rnode4_t
*drp
= VTOR4(dvp
);
5675 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
5677 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5679 ASSERT(nm
[0] != '\0');
5680 ASSERT(dvp
->v_type
== VDIR
);
5681 ASSERT(nm
[0] != '.' || nm
[1] != '\0');
5682 ASSERT(*vpp
== NULL
);
5684 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0') {
5686 args
.ctag
= TAG_LOOKUP_PARENT
;
5689 * If dvp were a stub, it should have triggered and caused
5690 * a mount for us to get this far.
5692 ASSERT(!RP_ISSTUB(VTOR4(dvp
)));
5695 args
.ctag
= TAG_LOOKUP
;
5699 recov_state
.rs_flags
= 0;
5700 recov_state
.rs_num_retry_despite_err
= 0;
5704 /* Save the original mount point security information */
5705 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
5708 e
.error
= nfs4_start_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5709 &recov_state
, NULL
);
5711 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5715 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
5717 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5722 argop
[0].argop
= OP_CPUTFH
;
5723 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(dvp
)->r_fh
;
5725 /* 1. savefh for the nverify */
5726 argop
[1].argop
= OP_SAVEFH
;
5728 /* 2. lookup name */
5730 argop
[2].argop
= OP_LOOKUPP
;
5732 argop
[2].argop
= OP_CLOOKUP
;
5733 argop
[2].nfs_argop4_u
.opclookup
.cname
= nm
;
5736 /* 3. resulting file handle */
5737 argop
[3].argop
= OP_GETFH
;
5739 /* 4. resulting file attributes */
5740 argop
[4].argop
= OP_GETATTR
;
5741 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5742 argop
[4].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5744 /* 5. restorefh back the directory for the nverify */
5745 argop
[5].argop
= OP_RESTOREFH
;
5747 /* 6. nverify the change info */
5748 argop
[6].argop
= OP_NVERIFY
;
5749 ver_fattr
= &argop
[6].nfs_argop4_u
.opnverify
.obj_attributes
;
5750 ver_fattr
->attrmask
= FATTR4_CHANGE_MASK
;
5751 ver_fattr
->attrlist4
= (char *)&dchange
;
5752 ptr
= (int32_t *)&dchange
;
5753 IXDR_PUT_HYPER(ptr
, VTOR4(dvp
)->r_change
);
5754 ver_fattr
->attrlist4_len
= sizeof (fattr4_change
);
5756 /* 7. getattr directory */
5757 argop
[7].argop
= OP_GETATTR
;
5758 argop
[7].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5759 argop
[7].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5761 /* 8. access directory */
5762 argop
[8].argop
= OP_ACCESS
;
5763 argop
[8].nfs_argop4_u
.opaccess
.access
= ACCESS4_READ
| ACCESS4_DELETE
|
5764 ACCESS4_MODIFY
| ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
5769 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
5771 if (!isdotdot
&& res
.status
== NFS4ERR_MOVED
) {
5772 e
.error
= nfs4_setup_referral(dvp
, nm
, vpp
, cr
);
5773 if (e
.error
!= 0 && *vpp
!= NULL
)
5775 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5776 &recov_state
, FALSE
);
5777 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5778 kmem_free(argop
, argoplist_size
);
5782 if (nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
)) {
5784 * For WRONGSEC of a non-dotdot case, send secinfo directly
5785 * from this thread, do not go thru the recovery thread since
5786 * we need the nm information.
5788 * Not doing dotdot case because there is no specification
5789 * for (PUTFH, SECINFO "..") yet.
5791 if (!isdotdot
&& res
.status
== NFS4ERR_WRONGSEC
) {
5792 if ((e
.error
= nfs4_secinfo_vnode_otw(dvp
, nm
, cr
)))
5793 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5794 &recov_state
, FALSE
);
5796 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5797 &recov_state
, TRUE
);
5798 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5799 kmem_free(argop
, argoplist_size
);
5802 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5806 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
5807 OP_LOOKUP
, NULL
, NULL
, NULL
) == FALSE
) {
5808 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5809 &recov_state
, TRUE
);
5811 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5812 kmem_free(argop
, argoplist_size
);
5817 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
, &recov_state
, FALSE
);
5819 if (e
.error
|| res
.array_len
== 0) {
5821 * If e.error isn't set, then reply has no ops (or we couldn't
5822 * be here). The only legal way to reply without an op array
5823 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5824 * be in the reply for all other status values.
5826 * For valid replies without an ops array, return ENOTSUP
5827 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5828 * return EIO -- don't trust status.
5831 e
.error
= (res
.status
== NFS4ERR_MINOR_VERS_MISMATCH
) ?
5834 kmem_free(argop
, argoplist_size
);
5835 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5839 e
.error
= geterrno4(res
.status
);
5842 * The PUTFH and SAVEFH may have failed.
5844 if ((res
.array
[0].nfs_resop4_u
.opputfh
.status
!= NFS4_OK
) ||
5845 (res
.array
[1].nfs_resop4_u
.opsavefh
.status
!= NFS4_OK
)) {
5846 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5851 * Check if the file exists, if it does delay entering
5852 * into the dnlc until after we update the directory
5853 * attributes so we don't cause it to get purged immediately.
5855 if (res
.array
[2].nfs_resop4_u
.oplookup
.status
!= NFS4_OK
) {
5857 * The lookup failed, probably no entry
5859 if (e
.error
== ENOENT
&& nfs4_lookup_neg_cache
)
5860 dnlc_update(dvp
, nm
, DNLC_NO_VNODE
);
5864 if (res
.array
[3].nfs_resop4_u
.opgetfh
.status
!= NFS4_OK
) {
5866 * The file exists but we can't get its fh for
5867 * some unknown reason. Error out to be safe.
5872 fhp
= &res
.array
[3].nfs_resop4_u
.opgetfh
.object
;
5873 if (fhp
->nfs_fh4_len
== 0) {
5875 * The file exists but a bogus fh
5876 * some unknown reason. Error out to be safe.
5881 sfhp
= sfh4_get(fhp
, mi
);
5883 if (res
.array
[4].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5887 garp
= &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
5890 * The RESTOREFH may have failed
5892 if (res
.array
[5].nfs_resop4_u
.oprestorefh
.status
!= NFS4_OK
) {
5898 if (res
.array
[6].nfs_resop4_u
.opnverify
.status
!= NFS4ERR_SAME
) {
5900 * First make sure the NVERIFY failed as we expected,
5901 * if it didn't then be conservative and error out
5902 * as we can't trust the directory.
5904 if (res
.array
[6].nfs_resop4_u
.opnverify
.status
!= NFS4_OK
) {
5911 * We know the NVERIFY "failed" so the directory has changed,
5913 * purge the caches (access and indirectly dnlc if needed)
5915 nfs4_purge_caches(dvp
, NFS4_NOPURGE_DNLC
, cr
, TRUE
);
5917 if (res
.array
[7].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5921 nfs4_attr_cache(dvp
,
5922 &res
.array
[7].nfs_resop4_u
.opgetattr
.ga_res
,
5923 t
, cr
, FALSE
, NULL
);
5925 if (res
.array
[8].nfs_resop4_u
.opaccess
.status
!= NFS4_OK
) {
5926 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5928 e
.error
= geterrno4(res
.status
);
5933 * Now we know the directory is valid,
5934 * cache new directory access
5936 nfs4_access_cache(drp
,
5937 args
.array
[8].nfs_argop4_u
.opaccess
.access
,
5938 res
.array
[8].nfs_resop4_u
.opaccess
.access
, cr
);
5941 * recheck VEXEC access
5943 cacc
= nfs4_access_check(drp
, ACCESS4_LOOKUP
, cr
);
5944 if (cacc
!= NFS4_ACCESS_ALLOWED
) {
5946 * Directory permissions might have been revoked
5948 if (cacc
== NFS4_ACCESS_DENIED
) {
5955 * Somehow we must not have asked for enough
5956 * so try a singleton ACCESS should never happen
5958 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5965 e
.error
= geterrno4(res
.status
);
5973 * Because the NVERIFY "succeeded" we know that the
5974 * directory attributes are still valid
5975 * so update r_time_attr_inval
5978 mutex_enter(&drp
->r_statelock
);
5979 if (!(mi
->mi_flags
& MI4_NOAC
) && !(dvp
->v_flag
& VNOCACHE
)) {
5980 delta
= now
- drp
->r_time_attr_saved
;
5981 if (delta
< mi
->mi_acdirmin
)
5982 delta
= mi
->mi_acdirmin
;
5983 else if (delta
> mi
->mi_acdirmax
)
5984 delta
= mi
->mi_acdirmax
;
5986 drp
->r_time_attr_inval
= now
+ delta
;
5987 mutex_exit(&drp
->r_statelock
);
5990 * Even though we have a valid directory attr cache,
5991 * we may not have access.
5992 * This should almost always hit the cache.
5994 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
6002 * Now we have successfully completed the lookup, if the
6003 * directory has changed we now have the valid attributes.
6004 * We also know we have directory access.
6005 * Create the new rnode and insert it in the dnlc.
6008 e
.error
= nfs4_make_dotdot(sfhp
, t
, dvp
, cr
, &nvp
, 1);
6014 * XXX if nfs4_make_dotdot uses an existing rnode
6015 * XXX it doesn't update the attributes.
6016 * XXX for now just save them again to save an OTW
6018 nfs4_attr_cache(nvp
, garp
, t
, cr
, FALSE
, NULL
);
6020 nvp
= makenfs4node(sfhp
, garp
, dvp
->v_vfsp
, t
, cr
,
6021 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
6026 mutex_enter(&nrp
->r_statev4_lock
);
6027 if (!nrp
->created_v4
) {
6028 mutex_exit(&nrp
->r_statev4_lock
);
6029 dnlc_update(dvp
, nm
, nvp
);
6031 mutex_exit(&nrp
->r_statev4_lock
);
6036 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6037 kmem_free(argop
, argoplist_size
);
6038 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
6044 nfs4lookup_dump_compound(char *where
, nfs_argop4
*argbase
, int argcnt
)
6047 zoneid_t zoneid
= getzoneid();
6050 zcmn_err(zoneid
, CE_NOTE
, "%s: dumping cmpd", where
);
6051 for (i
= 0; i
< argcnt
; i
++) {
6052 nfs_argop4
*op
= &argbase
[i
];
6053 switch (op
->argop
) {
6056 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, putfh", i
);
6059 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, putrootfh", i
);
6062 s
= op
->nfs_argop4_u
.opclookup
.cname
;
6063 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookup %s", i
, s
);
6066 s
= utf8_to_str(&op
->nfs_argop4_u
.oplookup
.objname
,
6068 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookup %s", i
, s
);
6072 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookupp ..", i
);
6075 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, getfh", i
);
6078 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, getattr", i
);
6081 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, openattr", i
);
6084 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, opcode %d", i
,
6093 * nfs4lookup_setup - constructs a multi-lookup compound request.
6095 * Given the path "nm1/nm2/.../nmn", the following compound requests
6098 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6099 * is faster, for now.
6101 * l4_getattrs indicates the type of compound requested.
6103 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6105 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6107 * total number of ops is n + 1.
6109 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6110 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6111 * before the last component, and only get attributes
6112 * for the last component. Note that the second-to-last
6113 * pathname component is XATTR_RPATH, which does NOT go
6114 * over-the-wire as a lookup.
6116 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6117 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6119 * and total number of ops is n + 5.
6121 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6122 * attribute directory: create lookups plus an OPENATTR
6123 * replacing the last lookup. Note that the last pathname
6124 * component is XATTR_RPATH, which does NOT go over-the-wire
6127 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6128 * Openattr; Getfh; Getattr }
6130 * and total number of ops is n + 5.
6132 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6135 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6136 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6138 * and total number of ops is 3*n + 1.
6140 * All cases: returns the index in the arg array of the final LOOKUP op, or
6141 * -1 if no LOOKUPs were used.
6144 nfs4lookup_setup(char *nm
, lookup4_param_t
*lookupargp
, int needgetfh
)
6146 enum lkp4_attr_setup l4_getattrs
= lookupargp
->l4_getattrs
;
6147 nfs_argop4
*argbase
, *argop
;
6149 int n
= 1; /* number of components */
6150 int nga
= 1; /* number of Getattr's in request */
6151 char c
= '\0', *s
, *p
;
6152 int lookup_idx
= -1;
6155 /* set lookuparg response result to 0 */
6156 lookupargp
->resp
->status
= NFS4_OK
;
6158 /* skip leading "/" or "." e.g. ".//./" if there is */
6160 if (*nm
!= '/' && *nm
!= '.')
6163 /* ".." is counted as 1 component */
6164 if (*nm
== '.' && *(nm
+ 1) != '/')
6169 * Find n = number of components - nm must be null terminated
6170 * Skip "." components.
6173 for (n
= 1, s
= nm
; *s
!= '\0'; s
++) {
6174 if ((*s
== '/') && (*(s
+ 1) != '/') &&
6175 (*(s
+ 1) != '\0') &&
6176 !(*(s
+ 1) == '.' && (*(s
+ 2) == '/' ||
6184 * nga is number of components that need Getfh+Getattr
6186 switch (l4_getattrs
) {
6187 case LKP4_NO_ATTRIBUTES
:
6190 case LKP4_ALL_ATTRIBUTES
:
6193 * Always have at least 1 getfh, getattr pair
6198 case LKP4_LAST_ATTRDIR
:
6199 case LKP4_LAST_NAMED_ATTR
:
6205 * If change to use the filehandle attr instead of getfh
6206 * the following line can be deleted.
6211 * calculate number of ops in request as
6212 * header + trailer + lookups + getattrs
6214 arglen
= lookupargp
->header_len
+ lookupargp
->trailer_len
+ n
+ nga
;
6216 argoplist_size
= arglen
* sizeof (nfs_argop4
);
6217 argop
= argbase
= kmem_alloc(argoplist_size
, KM_SLEEP
);
6218 lookupargp
->argsp
->array
= argop
;
6220 argcnt
= lookupargp
->header_len
;
6224 * loop and create a lookup op and possibly getattr/getfh for
6225 * each component. Skip "." components.
6227 for (s
= nm
; *s
!= '\0'; s
= p
) {
6229 * Set up a pathname struct for each component if needed
6236 for (p
= s
; (*p
!= '/') && (*p
!= '\0'); p
++)
6241 if (s
[0] == '.' && s
[1] == '\0') {
6245 if (l4_getattrs
== LKP4_LAST_ATTRDIR
&&
6246 strcmp(s
, XATTR_RPATH
) == 0) {
6247 /* getfh XXX may not be needed in future */
6248 argop
->argop
= OP_GETFH
;
6253 argop
->argop
= OP_GETATTR
;
6254 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6255 lookupargp
->ga_bits
;
6256 argop
->nfs_argop4_u
.opgetattr
.mi
=
6262 argop
->argop
= OP_OPENATTR
;
6263 } else if (l4_getattrs
== LKP4_LAST_NAMED_ATTR
&&
6264 strcmp(s
, XATTR_RPATH
) == 0) {
6266 argop
->argop
= OP_OPENATTR
;
6270 /* getfh XXX may not be needed in future */
6271 argop
->argop
= OP_GETFH
;
6276 argop
->argop
= OP_GETATTR
;
6277 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6278 lookupargp
->ga_bits
;
6279 argop
->nfs_argop4_u
.opgetattr
.mi
=
6285 } else if (s
[0] == '.' && s
[1] == '.' && s
[2] == '\0') {
6287 argop
->argop
= OP_LOOKUPP
;
6290 argop
->argop
= OP_LOOKUP
;
6291 (void) str_to_utf8(s
,
6292 &argop
->nfs_argop4_u
.oplookup
.objname
);
6294 lookup_idx
= argcnt
;
6300 if (l4_getattrs
== LKP4_ALL_ATTRIBUTES
) {
6301 /* getfh XXX may not be needed in future */
6302 argop
->argop
= OP_GETFH
;
6307 argop
->argop
= OP_GETATTR
;
6308 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6309 lookupargp
->ga_bits
;
6310 argop
->nfs_argop4_u
.opgetattr
.mi
=
6317 if ((l4_getattrs
!= LKP4_NO_ATTRIBUTES
) &&
6318 ((l4_getattrs
!= LKP4_ALL_ATTRIBUTES
) || (lookup_idx
< 0))) {
6320 /* stick in a post-lookup getfh */
6321 argop
->argop
= OP_GETFH
;
6325 /* post-lookup getattr */
6326 argop
->argop
= OP_GETATTR
;
6327 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6328 lookupargp
->ga_bits
;
6329 argop
->nfs_argop4_u
.opgetattr
.mi
= lookupargp
->mi
;
6332 argcnt
+= lookupargp
->trailer_len
; /* actual op count */
6333 lookupargp
->argsp
->array_len
= argcnt
;
6334 lookupargp
->arglen
= arglen
;
6337 if (nfs4_client_lookup_debug
)
6338 nfs4lookup_dump_compound("nfs4lookup_setup", argbase
, argcnt
);
6341 return (lookup_idx
);
6345 nfs4openattr(vnode_t
*dvp
, vnode_t
**avp
, int cflag
, cred_t
*cr
)
6347 COMPOUND4args_clnt args
;
6348 COMPOUND4res_clnt res
;
6349 GETFH4res
*gf_res
= NULL
;
6350 nfs_argop4 argop
[4];
6351 nfs_resop4
*resop
= NULL
;
6352 nfs4_sharedfh_t
*sfhp
;
6360 nfs4_recov_state_t recov_state
;
6362 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
6365 recov_state
.rs_flags
= 0;
6366 recov_state
.rs_num_retry_despite_err
= 0;
6369 /* COMPOUND: putfh, openattr, getfh, getattr */
6372 args
.ctag
= TAG_OPENATTR
;
6374 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
6381 argop
[0].argop
= OP_CPUTFH
;
6382 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6385 argop
[1].argop
= OP_OPENATTR
;
6386 argop
[1].nfs_argop4_u
.opopenattr
.createdir
= (cflag
? TRUE
: FALSE
);
6389 argop
[2].argop
= OP_GETFH
;
6392 argop
[3].argop
= OP_GETATTR
;
6393 argop
[3].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6394 argop
[3].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
6396 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
6397 "nfs4openattr: %s call, drp %s", needrecov
? "recov" : "first",
6402 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
6404 needrecov
= nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
);
6408 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
6409 "nfs4openattr: initiating recovery\n"));
6411 abort
= nfs4_start_recovery(&e
,
6412 VTOMI4(dvp
), dvp
, NULL
, NULL
, NULL
,
6413 OP_OPENATTR
, NULL
, NULL
, NULL
);
6414 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6416 e
.error
= geterrno4(res
.status
);
6417 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6425 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6431 * If OTW errro is NOTSUPP, then it should be
6432 * translated to EINVAL. All Solaris file system
6433 * implementations return EINVAL to the syscall layer
6434 * when the attrdir cannot be created due to an
6435 * implementation restriction or noxattr mount option.
6437 if (res
.status
== NFS4ERR_NOTSUPP
) {
6438 mutex_enter(&drp
->r_statelock
);
6439 if (drp
->r_xattr_dir
)
6440 VN_RELE(drp
->r_xattr_dir
);
6441 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP
);
6442 drp
->r_xattr_dir
= NFS4_XATTR_DIR_NOTSUPP
;
6443 mutex_exit(&drp
->r_statelock
);
6447 e
.error
= geterrno4(res
.status
);
6451 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6452 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
6458 resop
= &res
.array
[0]; /* putfh res */
6459 ASSERT(resop
->nfs_resop4_u
.opgetfh
.status
== NFS4_OK
);
6461 resop
= &res
.array
[1]; /* openattr res */
6462 ASSERT(resop
->nfs_resop4_u
.opopenattr
.status
== NFS4_OK
);
6464 resop
= &res
.array
[2]; /* getfh res */
6465 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
6466 if (gf_res
->object
.nfs_fh4_len
== 0) {
6468 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6469 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6473 sfhp
= sfh4_get(&gf_res
->object
, VTOMI4(dvp
));
6474 vp
= makenfs4node(sfhp
, &res
.array
[3].nfs_resop4_u
.opgetattr
.ga_res
,
6475 dvp
->v_vfsp
, t
, cr
, dvp
,
6476 fn_get(VTOSV(dvp
)->sv_name
, XATTR_RPATH
, sfhp
));
6480 PURGE_ATTRCACHE4(vp
);
6482 mutex_enter(&vp
->v_lock
);
6483 vp
->v_flag
|= V_XATTRDIR
;
6484 mutex_exit(&vp
->v_lock
);
6488 mutex_enter(&drp
->r_statelock
);
6489 if (drp
->r_xattr_dir
)
6490 VN_RELE(drp
->r_xattr_dir
);
6492 drp
->r_xattr_dir
= vp
;
6495 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6496 * NULL. xattrs could be created at any time, and we have no
6497 * way to update pc4_xattr_exists in the base object if/when
6500 drp
->r_pathconf
.pc4_xattr_valid
= 0;
6502 mutex_exit(&drp
->r_statelock
);
6504 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6506 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6513 nfs4_create(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
6514 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flags
, caller_context_t
*ct
,
6523 enum createmode4 createmode
;
6524 bool_t must_trunc
= FALSE
;
6527 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
6529 if (exclusive
== EXCL
&& (dvp
->v_flag
& V_XATTRDIR
)) {
6533 /* . and .. have special meaning in the protocol, reject them. */
6535 if (nm
[0] == '.' && (nm
[1] == '\0' || (nm
[1] == '.' && nm
[2] == '\0')))
6540 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
6545 * We make a copy of the attributes because the caller does not
6546 * expect us to change what va points to.
6551 * If the pathname is "", then dvp is the root vnode of
6552 * a remote file mounted over a local directory.
6553 * All that needs to be done is access
6554 * checking and truncation. Note that we avoid doing
6555 * open w/ create because the parent directory might
6556 * be in pseudo-fs and the open would fail.
6565 * We need to go over the wire, just to be sure whether the
6566 * file exists or not. Using the DNLC can be dangerous in
6567 * this case when making a decision regarding existence.
6569 error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 1);
6573 createmode
= EXCLUSIVE4
;
6575 createmode
= GUARDED4
;
6578 * error would be set if the file does not exist on the
6579 * server, so lets go create it.
6586 * File does exist on the server
6588 if (exclusive
== EXCL
)
6590 else if (vp
->v_type
== VDIR
&& (mode
& VWRITE
))
6594 * If vnode is a device, create special vnode.
6596 if (ISVDEV(vp
->v_type
)) {
6598 vp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
6601 if (!(error
= fop_access(vp
, mode
, 0, cr
, ct
))) {
6602 if ((vattr
.va_mask
& VATTR_SIZE
) &&
6603 vp
->v_type
== VREG
) {
6607 vattr
.va_mask
= VATTR_SIZE
;
6608 error
= nfs4setattr(vp
, &vattr
, 0, cr
,
6612 * we know we have a regular file that already
6613 * exists and we may end up truncating the file
6614 * as a result of the open_otw, so flush out
6615 * any dirty pages for this file first.
6617 if (nfs4_has_pages(vp
) &&
6618 ((rp
->r_flags
& R4DIRTY
) ||
6620 rp
->r_mapcnt
> 0)) {
6621 error
= nfs4_putpage(vp
,
6623 if (error
&& (error
== ENOSPC
||
6634 vattr
.va_mask
= (VATTR_SIZE
|
6635 VATTR_TYPE
| VATTR_MODE
);
6636 vattr
.va_type
= VREG
;
6637 createmode
= UNCHECKED4
;
6644 nfs_rw_exit(&drp
->r_rwlock
);
6651 if (vp
->v_type
== VREG
) {
6653 if (IS_SHADOW(vp
, trp
))
6659 * existing file got truncated, notify.
6661 vnevent_create(tvp
, ct
);
6669 dnlc_remove(dvp
, nm
);
6671 ASSERT(vattr
.va_mask
& VATTR_TYPE
);
6674 * If not a regular file let nfs4mknod() handle it.
6676 if (vattr
.va_type
!= VREG
) {
6677 error
= nfs4mknod(dvp
, nm
, &vattr
, exclusive
, mode
, vpp
, cr
);
6678 nfs_rw_exit(&drp
->r_rwlock
);
6683 * It _is_ a regular file.
6685 ASSERT(vattr
.va_mask
& VATTR_MODE
);
6686 if (MANDMODE(vattr
.va_mode
)) {
6687 nfs_rw_exit(&drp
->r_rwlock
);
6692 * If this happens to be a mknod of a regular file, then flags will
6693 * have neither FREAD or FWRITE. However, we must set at least one
6694 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6695 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6696 * set (based on openmode specified by app).
6698 if ((flags
& (FREAD
|FWRITE
)) == 0)
6699 flags
|= (FREAD
|FWRITE
);
6701 error
= nfs4open_otw(dvp
, nm
, &vattr
, vpp
, cr
, 1, flags
, createmode
, 0);
6704 /* if create was successful, throw away the file's pages */
6705 if (!error
&& (vattr
.va_mask
& VATTR_SIZE
))
6706 nfs4_invalidate_pages(vp
, (vattr
.va_size
& PAGEMASK
),
6708 /* release the lookup hold */
6714 * validate that we opened a regular file. This handles a misbehaving
6715 * server that returns an incorrect FH.
6717 if ((error
== 0) && *vpp
&& (*vpp
)->v_type
!= VREG
) {
6723 * If this is not an exclusive create, then the CREATE
6724 * request will be made with the GUARDED mode set. This
6725 * means that the server will return EEXIST if the file
6726 * exists. The file could exist because of a retransmitted
6727 * request. In this case, we recover by starting over and
6728 * checking to see whether the file exists. This second
6729 * time through it should and a CREATE request will not be
6732 * This handles the problem of a dangling CREATE request
6733 * which contains attributes which indicate that the file
6734 * should be truncated. This retransmitted request could
6735 * possibly truncate valid data in the file if not caught
6736 * by the duplicate request mechanism on the server or if
6737 * not caught by other means. The scenario is:
6739 * Client transmits CREATE request with size = 0
6740 * Client times out, retransmits request.
6741 * Response to the first request arrives from the server
6742 * and the client proceeds on.
6743 * Client writes data to the file.
6744 * The server now processes retransmitted CREATE request
6745 * and truncates file.
6747 * The use of the GUARDED CREATE request prevents this from
6748 * happening because the retransmitted CREATE would fail
6749 * with EEXIST and would not truncate the file.
6751 if (error
== EEXIST
&& exclusive
== NONEXCL
) {
6753 nfs4_create_misses
++;
6757 nfs_rw_exit(&drp
->r_rwlock
);
6758 if (truncating
&& !error
&& *vpp
) {
6762 * existing file got truncated, notify.
6766 if (IS_SHADOW(tvp
, trp
))
6768 vnevent_create(tvp
, ct
);
6774 * Create compound (for mkdir, mknod, symlink):
6775 * { Putfh <dfh>; Create; Getfh; Getattr }
6776 * It's okay if setattr failed to set gid - this is not considered
6777 * an error, but purge attrs in that case.
6780 call_nfs4_create_req(vnode_t
*dvp
, char *nm
, void *data
, struct vattr
*va
,
6781 vnode_t
**vpp
, cred_t
*cr
, nfs_ftype4 type
)
6783 int need_end_op
= FALSE
;
6784 COMPOUND4args_clnt args
;
6785 COMPOUND4res_clnt res
, *resp
= NULL
;
6790 rnode4_t
*drp
= VTOR4(dvp
);
6791 change_info4
*cinfo
;
6796 bool_t needrecov
= FALSE
;
6797 nfs4_recov_state_t recov_state
;
6798 nfs4_sharedfh_t
*sfhp
= NULL
;
6800 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
6801 int numops
, argoplist_size
, setgid_flag
, idx_create
, idx_fattr
;
6802 dirattr_info_t dinfo
, *dinfop
;
6806 ASSERT(type
== NF4DIR
|| type
== NF4LNK
|| type
== NF4BLK
||
6807 type
== NF4CHR
|| type
== NF4SOCK
|| type
== NF4FIFO
);
6812 * Make sure we properly deal with setting the right gid
6813 * on a new directory to reflect the parent's setgid bit
6816 if (type
== NF4DIR
) {
6819 va
->va_mode
&= ~VSGID
;
6820 dva
.va_mask
= VATTR_MODE
| VATTR_GID
;
6821 if (fop_getattr(dvp
, &dva
, 0, cr
, NULL
) == 0) {
6824 * If the parent's directory has the setgid bit set
6825 * _and_ the client was able to get a valid mapping
6826 * for the parent dir's owner_group, we want to
6827 * append NVERIFY(owner_group == dva.va_gid) and
6828 * SETTATTR to the CREATE compound.
6830 if (mi
->mi_flags
& MI4_GRPID
|| dva
.va_mode
& VSGID
) {
6832 va
->va_mode
|= VSGID
;
6833 if (dva
.va_gid
!= GID_NOBODY
) {
6834 va
->va_mask
|= VATTR_GID
;
6835 va
->va_gid
= dva
.va_gid
;
6843 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6844 * 5:restorefh(dir) 6:getattr(dir)
6847 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6848 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6849 * 8:nverify 9:setattr
6861 ASSERT(nfs_zone() == mi
->mi_zone
);
6862 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
))) {
6865 recov_state
.rs_flags
= 0;
6866 recov_state
.rs_num_retry_despite_err
= 0;
6868 argoplist_size
= numops
* sizeof (nfs_argop4
);
6869 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
6873 args
.ctag
= TAG_SYMLINK
;
6874 else if (type
== NF4DIR
)
6875 args
.ctag
= TAG_MKDIR
;
6877 args
.ctag
= TAG_MKNOD
;
6879 args
.array_len
= numops
;
6882 if (e
.error
= nfs4_start_op(mi
, dvp
, NULL
, &recov_state
)) {
6883 nfs_rw_exit(&drp
->r_rwlock
);
6884 kmem_free(argop
, argoplist_size
);
6890 /* 0: putfh directory */
6891 argop
[0].argop
= OP_CPUTFH
;
6892 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6894 /* 1/2: Create object */
6895 argop
[idx_create
].argop
= OP_CCREATE
;
6896 argop
[idx_create
].nfs_argop4_u
.opccreate
.cname
= nm
;
6897 argop
[idx_create
].nfs_argop4_u
.opccreate
.type
= type
;
6898 if (type
== NF4LNK
) {
6900 * symlink, treat name as data
6902 ASSERT(data
!= NULL
);
6903 argop
[idx_create
].nfs_argop4_u
.opccreate
.ftype4_u
.clinkdata
=
6906 if (type
== NF4BLK
|| type
== NF4CHR
) {
6907 ASSERT(data
!= NULL
);
6908 argop
[idx_create
].nfs_argop4_u
.opccreate
.ftype4_u
.devdata
=
6909 *((specdata4
*)data
);
6912 crattr
= &argop
[idx_create
].nfs_argop4_u
.opccreate
.createattrs
;
6914 svp
= drp
->r_server
;
6915 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
6916 supp_attrs
= svp
->sv_supp_attrs
;
6917 nfs_rw_exit(&svp
->sv_lock
);
6919 if (vattr_to_fattr4(va
, NULL
, crattr
, 0, OP_CREATE
, supp_attrs
)) {
6920 nfs_rw_exit(&drp
->r_rwlock
);
6921 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
, needrecov
);
6923 kmem_free(argop
, argoplist_size
);
6927 /* 2/3: getfh fh of created object */
6928 ASSERT(idx_create
+ 1 == idx_fattr
- 1);
6929 argop
[idx_create
+ 1].argop
= OP_GETFH
;
6931 /* 3/4: getattr of new object */
6932 argop
[idx_fattr
].argop
= OP_GETATTR
;
6933 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6934 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.mi
= mi
;
6939 argop
[4].argop
= OP_SAVEFH
;
6941 argop
[5].argop
= OP_CPUTFH
;
6942 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6944 argop
[6].argop
= OP_GETATTR
;
6945 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6946 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
6948 argop
[7].argop
= OP_RESTOREFH
;
6953 * XXX - Revisit the last argument to nfs4_end_op()
6954 * once 5020486 is fixed.
6956 _v
.va_mask
= VATTR_GID
;
6957 _v
.va_gid
= va
->va_gid
;
6958 if (e
.error
= nfs4args_verify(&argop
[8], &_v
, OP_NVERIFY
,
6960 nfs4_end_op(mi
, dvp
, *vpp
, &recov_state
, TRUE
);
6961 nfs_rw_exit(&drp
->r_rwlock
);
6962 nfs4_fattr4_free(crattr
);
6963 kmem_free(argop
, argoplist_size
);
6970 * We _know_ we're not messing with VATTR_SIZE or
6971 * VATTR_XTIME, so no need for stateid or flags. Also we
6972 * specify NULL rp since we're only interested in setting
6973 * owner_group attributes.
6975 nfs4args_setattr(&argop
[9], &_v
, NULL
, 0, NULL
, cr
, supp_attrs
,
6979 nfs4_end_op(mi
, dvp
, *vpp
, &recov_state
, TRUE
);
6980 nfs_rw_exit(&drp
->r_rwlock
);
6981 nfs4_fattr4_free(crattr
);
6982 nfs4args_verify_free(&argop
[8]);
6983 kmem_free(argop
, argoplist_size
);
6987 argop
[1].argop
= OP_SAVEFH
;
6989 argop
[5].argop
= OP_RESTOREFH
;
6991 argop
[6].argop
= OP_GETATTR
;
6992 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6993 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
6996 dnlc_remove(dvp
, nm
);
7000 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
7002 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
7004 PURGE_ATTRCACHE4(dvp
);
7010 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
7011 OP_CREATE
, NULL
, NULL
, NULL
) == FALSE
) {
7012 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7014 need_end_op
= FALSE
;
7015 nfs4_fattr4_free(crattr
);
7017 nfs4args_verify_free(&argop
[8]);
7018 nfs4args_setattr_free(&argop
[9]);
7020 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
7027 if (res
.status
!= NFS4_OK
&& res
.array_len
<= idx_fattr
+ 1) {
7029 if (res
.status
== NFS4ERR_BADOWNER
)
7030 nfs4_log_badowner(mi
, OP_CREATE
);
7032 e
.error
= geterrno4(res
.status
);
7035 * This check is left over from when create was implemented
7036 * using a setattr op (instead of createattrs). If the
7037 * putfh/create/getfh failed, the error was returned. If
7038 * setattr/getattr failed, we keep going.
7040 * It might be better to get rid of the GETFH also, and just
7041 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7042 * Then if any of the operations failed, we could return the
7043 * error now, and remove much of the error code below.
7045 if (res
.array_len
<= idx_fattr
) {
7047 * Either Putfh, Create or Getfh failed.
7049 PURGE_ATTRCACHE4(dvp
);
7051 * nfs4_purge_stale_fh() may generate otw calls through
7052 * nfs4_invalidate_pages. Hence the need to call
7053 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7055 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7057 need_end_op
= FALSE
;
7058 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
7063 resop
= &res
.array
[idx_create
]; /* create res */
7064 cinfo
= &resop
->nfs_resop4_u
.opcreate
.cinfo
;
7066 resop
= &res
.array
[idx_create
+ 1]; /* getfh res */
7067 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
7069 sfhp
= sfh4_get(&gf_res
->object
, mi
);
7071 *vpp
= vp
= makenfs4node(sfhp
, NULL
, dvp
->v_vfsp
, t
, cr
, dvp
,
7072 fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
7073 if (vp
->v_type
== VNON
) {
7074 vattr
.va_mask
= VATTR_TYPE
;
7076 * Need to call nfs4_end_op before nfs4getattr to avoid
7077 * potential nfs4_start_op deadlock. See RFE 4777612.
7079 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7081 need_end_op
= FALSE
;
7082 e
.error
= nfs4getattr(vp
, &vattr
, cr
);
7088 vp
->v_type
= vattr
.va_type
;
7092 *vpp
= vp
= makenfs4node(sfhp
,
7093 &res
.array
[idx_fattr
].nfs_resop4_u
.opgetattr
.ga_res
,
7095 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
7099 * If compound succeeded, then update dir attrs
7101 if (res
.status
== NFS4_OK
) {
7102 dinfo
.di_garp
= &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
7104 dinfo
.di_time_call
= t
;
7109 /* Update directory cache attribute, readdir and dnlc caches */
7110 nfs4_update_dircaches(cinfo
, dvp
, vp
, nm
, dinfop
);
7115 nfs_rw_exit(&drp
->r_rwlock
);
7116 nfs4_fattr4_free(crattr
);
7118 nfs4args_verify_free(&argop
[8]);
7119 nfs4args_setattr_free(&argop
[9]);
7122 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7124 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
, needrecov
);
7126 kmem_free(argop
, argoplist_size
);
7132 nfs4mknod(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
7133 int mode
, vnode_t
**vpp
, cred_t
*cr
)
7138 specdata4 spec
, *specp
= NULL
;
7140 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
7142 switch (va
->va_type
) {
7145 type
= (va
->va_type
== VCHR
) ? NF4CHR
: NF4BLK
;
7146 spec
.specdata1
= getmajor(va
->va_rdev
);
7147 spec
.specdata2
= getminor(va
->va_rdev
);
7162 error
= call_nfs4_create_req(dvp
, nm
, specp
, va
, &vp
, cr
, type
);
7168 * This might not be needed any more; special case to deal
7169 * with problematic v2/v3 servers. Since create was unable
7170 * to set group correctly, not sure what hope setattr has.
7172 if (va
->va_gid
!= VTOR4(vp
)->r_attr
.va_gid
) {
7173 va
->va_mask
= VATTR_GID
;
7174 (void) nfs4setattr(vp
, va
, 0, cr
, NULL
);
7178 * If vnode is a device create special vnode
7180 if (ISVDEV(vp
->v_type
)) {
7181 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
7190 * Remove requires that the current fh be the target directory.
7191 * After the operation, the current fh is unchanged.
7192 * The compound op structure is:
7193 * PUTFH(targetdir), REMOVE
7195 * Weirdness: if the vnode to be removed is open
7196 * we rename it instead of removing it and nfs_inactive
7197 * will remove the new name.
7201 nfs4_remove(vnode_t
*dvp
, char *nm
, cred_t
*cr
, caller_context_t
*ct
, int flags
)
7203 COMPOUND4args_clnt args
;
7204 COMPOUND4res_clnt res
, *resp
= NULL
;
7206 nfs_argop4 argop
[3];
7215 nfs4_recov_state_t recov_state
;
7217 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
7218 dirattr_info_t dinfo
;
7220 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
7223 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
7226 e
.error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 0);
7228 nfs_rw_exit(&drp
->r_rwlock
);
7232 if (vp
->v_type
== VDIR
) {
7234 nfs_rw_exit(&drp
->r_rwlock
);
7239 * First just remove the entry from the name cache, as it
7240 * is most likely the only entry for this vp.
7242 dnlc_remove(dvp
, nm
);
7247 * For regular file types, check to see if the file is open by looking
7248 * at the open streams.
7249 * For all other types, check the reference count on the vnode. Since
7250 * they are not opened OTW they never have an open stream.
7252 * If the file is open, rename it to .nfsXXXX.
7254 if (vp
->v_type
!= VREG
) {
7256 * If the file has a v_count > 1 then there may be more than one
7257 * entry in the name cache due multiple links or an open file,
7258 * but we don't have the real reference count so flush all
7261 if (vp
->v_count
> 1)
7265 * Now we have the real reference count.
7267 isopen
= vp
->v_count
> 1;
7269 mutex_enter(&rp
->r_os_lock
);
7270 isopen
= list_head(&rp
->r_open_streams
) != NULL
;
7271 mutex_exit(&rp
->r_os_lock
);
7274 mutex_enter(&rp
->r_statelock
);
7276 (rp
->r_unldvp
== NULL
|| strcmp(nm
, rp
->r_unlname
) == 0)) {
7277 mutex_exit(&rp
->r_statelock
);
7278 tmpname
= newname();
7279 e
.error
= nfs4rename(dvp
, nm
, dvp
, tmpname
, cr
, ct
);
7281 kmem_free(tmpname
, MAXNAMELEN
);
7283 mutex_enter(&rp
->r_statelock
);
7284 if (rp
->r_unldvp
== NULL
) {
7287 if (rp
->r_unlcred
!= NULL
)
7288 crfree(rp
->r_unlcred
);
7291 rp
->r_unlname
= tmpname
;
7293 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
7294 rp
->r_unlname
= tmpname
;
7296 mutex_exit(&rp
->r_statelock
);
7299 nfs_rw_exit(&drp
->r_rwlock
);
7303 * Actually remove the file/dir
7305 mutex_exit(&rp
->r_statelock
);
7308 * We need to flush any dirty pages which happen to
7309 * be hanging around before removing the file.
7310 * This shouldn't happen very often since in NFSv4
7311 * we should be close to open consistent.
7313 if (nfs4_has_pages(vp
) &&
7314 ((rp
->r_flags
& R4DIRTY
) || rp
->r_count
> 0)) {
7315 e
.error
= nfs4_putpage(vp
, 0, 0, 0, cr
, ct
);
7316 if (e
.error
&& (e
.error
== ENOSPC
|| e
.error
== EDQUOT
)) {
7317 mutex_enter(&rp
->r_statelock
);
7319 rp
->r_error
= e
.error
;
7320 mutex_exit(&rp
->r_statelock
);
7326 (void) nfs4delegreturn(rp
, NFS4_DR_REOPEN
);
7327 recov_state
.rs_flags
= 0;
7328 recov_state
.rs_num_retry_despite_err
= 0;
7332 * Remove ops: putfh dir; remove
7334 args
.ctag
= TAG_REMOVE
;
7338 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
7340 nfs_rw_exit(&drp
->r_rwlock
);
7345 /* putfh directory */
7346 argop
[0].argop
= OP_CPUTFH
;
7347 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
7350 argop
[1].argop
= OP_CREMOVE
;
7351 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= nm
;
7354 argop
[2].argop
= OP_GETATTR
;
7355 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7356 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
7359 dinfo
.di_time_call
= gethrtime();
7360 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
7362 PURGE_ATTRCACHE4(vp
);
7364 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
7366 PURGE_ATTRCACHE4(dvp
);
7369 if (nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
,
7370 NULL
, NULL
, NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
7372 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
7373 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
7380 * Matching nfs4_end_op() for start_op() above.
7381 * There is a path in the code below which calls
7382 * nfs4_purge_stale_fh(), which may generate otw calls through
7383 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7384 * here to avoid nfs4_start_op() deadlock.
7386 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
7392 e
.error
= geterrno4(res
.status
);
7393 PURGE_ATTRCACHE4(dvp
);
7394 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
7396 resop
= &res
.array
[1]; /* remove res */
7397 rm_res
= &resop
->nfs_resop4_u
.opremove
;
7400 &res
.array
[2].nfs_resop4_u
.opgetattr
.ga_res
;
7403 /* Update directory attr, readdir and dnlc caches */
7404 nfs4_update_dircaches(&rm_res
->cinfo
, dvp
, NULL
, NULL
,
7408 nfs_rw_exit(&drp
->r_rwlock
);
7410 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7417 if (IS_SHADOW(vp
, trp
))
7419 vnevent_remove(tvp
, dvp
, nm
, ct
);
7426 * Link requires that the current fh be the target directory and the
7427 * saved fh be the source fh. After the operation, the current fh is unchanged.
7428 * Thus the compound op structure is:
7429 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7434 nfs4_link(vnode_t
*tdvp
, vnode_t
*svp
, char *tnm
, cred_t
*cr
,
7435 caller_context_t
*ct
, int flags
)
7437 COMPOUND4args_clnt args
;
7438 COMPOUND4res_clnt res
, *resp
= NULL
;
7440 int argoplist_size
= 7 * sizeof (nfs_argop4
);
7443 vnode_t
*realvp
, *nvp
;
7447 bool_t needrecov
= FALSE
;
7448 nfs4_recov_state_t recov_state
;
7450 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
7451 dirattr_info_t dinfo
;
7453 ASSERT(*tnm
!= '\0');
7454 ASSERT(tdvp
->v_type
== VDIR
);
7455 ASSERT(nfs4_consistent_type(tdvp
));
7456 ASSERT(nfs4_consistent_type(svp
));
7458 if (nfs_zone() != VTOMI4(tdvp
)->mi_zone
)
7460 if (fop_realvp(svp
, &realvp
, ct
) == 0) {
7462 ASSERT(nfs4_consistent_type(svp
));
7468 if (!(mi
->mi_flags
& MI4_LINK
)) {
7469 return (EOPNOTSUPP
);
7471 recov_state
.rs_flags
= 0;
7472 recov_state
.rs_num_retry_despite_err
= 0;
7474 if (nfs_rw_enter_sig(&tdrp
->r_rwlock
, RW_WRITER
, INTR4(tdvp
)))
7478 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
7480 args
.ctag
= TAG_LINK
;
7483 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7484 * restorefh; getattr(fl)
7489 e
.error
= nfs4_start_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
);
7491 kmem_free(argop
, argoplist_size
);
7492 nfs_rw_exit(&tdrp
->r_rwlock
);
7497 argop
[0].argop
= OP_CPUTFH
;
7498 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(svp
)->r_fh
;
7500 /* 1. save current fh to free up the space for the dir */
7501 argop
[1].argop
= OP_SAVEFH
;
7503 /* 2. putfh targetdir */
7504 argop
[2].argop
= OP_CPUTFH
;
7505 argop
[2].nfs_argop4_u
.opcputfh
.sfh
= tdrp
->r_fh
;
7507 /* 3. link: current_fh is targetdir, saved_fh is source */
7508 argop
[3].argop
= OP_CLINK
;
7509 argop
[3].nfs_argop4_u
.opclink
.cnewname
= tnm
;
7511 /* 4. Get attributes of dir */
7512 argop
[4].argop
= OP_GETATTR
;
7513 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7514 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
7516 /* 5. If link was successful, restore current vp to file */
7517 argop
[5].argop
= OP_RESTOREFH
;
7519 /* 6. Get attributes of linked object */
7520 argop
[6].argop
= OP_GETATTR
;
7521 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7522 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
7524 dnlc_remove(tdvp
, tnm
);
7529 rfs4call(VTOMI4(svp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
7531 needrecov
= nfs4_needs_recovery(&e
, FALSE
, svp
->v_vfsp
);
7532 if (e
.error
!= 0 && !needrecov
) {
7533 PURGE_ATTRCACHE4(tdvp
);
7534 PURGE_ATTRCACHE4(svp
);
7535 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
, needrecov
);
7542 abort
= nfs4_start_recovery(&e
, VTOMI4(svp
), svp
, tdvp
,
7543 NULL
, NULL
, OP_LINK
, NULL
, NULL
, NULL
);
7544 if (abort
== FALSE
) {
7545 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
,
7547 kmem_free(argop
, argoplist_size
);
7549 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
7553 PURGE_ATTRCACHE4(tdvp
);
7554 PURGE_ATTRCACHE4(svp
);
7555 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
,
7556 &recov_state
, needrecov
);
7559 /* fall through for res.status case */
7563 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
, needrecov
);
7567 /* If link succeeded, then don't return error */
7568 e
.error
= geterrno4(res
.status
);
7569 if (res
.array_len
<= 4) {
7571 * Either Putfh, Savefh, Putfh dir, or Link failed
7573 PURGE_ATTRCACHE4(svp
);
7574 PURGE_ATTRCACHE4(tdvp
);
7575 if (e
.error
== EOPNOTSUPP
) {
7576 mutex_enter(&mi
->mi_lock
);
7577 mi
->mi_flags
&= ~MI4_LINK
;
7578 mutex_exit(&mi
->mi_lock
);
7581 if (e
.error
== EISDIR
&& crgetuid(cr
) != 0)
7587 /* either no error or one of the postop getattr failed */
7590 * XXX - if LINK succeeded, but no attrs were returned for link
7591 * file, purge its cache.
7593 * XXX Perform a simplified version of wcc checking. Instead of
7594 * have another getattr to get pre-op, just purge cache if
7595 * any of the ops prior to and including the getattr failed.
7596 * If the getattr succeeded then update the attrcache accordingly.
7600 * update cache with link file postattrs.
7601 * Note: at this point resop points to link res.
7603 resop
= &res
.array
[3]; /* link res */
7604 ln_res
= &resop
->nfs_resop4_u
.oplink
;
7605 if (res
.status
== NFS4_OK
)
7606 e
.error
= nfs4_update_attrcache(res
.status
,
7607 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
,
7611 * Call makenfs4node to create the new shadow vp for tnm.
7612 * We pass NULL attrs because we just cached attrs for
7613 * the src object. All we're trying to accomplish is to
7614 * to create the new shadow vnode.
7616 nvp
= makenfs4node(VTOR4(svp
)->r_fh
, NULL
, tdvp
->v_vfsp
, t
, cr
,
7617 tdvp
, fn_get(VTOSV(tdvp
)->sv_name
, tnm
, VTOR4(svp
)->r_fh
));
7619 /* Update target cache attribute, readdir and dnlc caches */
7620 dinfo
.di_garp
= &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
7621 dinfo
.di_time_call
= t
;
7624 nfs4_update_dircaches(&ln_res
->cinfo
, tdvp
, nvp
, tnm
, &dinfo
);
7625 ASSERT(nfs4_consistent_type(tdvp
));
7626 ASSERT(nfs4_consistent_type(svp
));
7627 ASSERT(nfs4_consistent_type(nvp
));
7634 * Notify the source file of this link operation.
7638 if (IS_SHADOW(svp
, trp
))
7640 vnevent_link(tvp
, ct
);
7643 kmem_free(argop
, argoplist_size
);
7645 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7647 nfs_rw_exit(&tdrp
->r_rwlock
);
7654 nfs4_rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
7655 caller_context_t
*ct
, int flags
)
7659 if (nfs_zone() != VTOMI4(odvp
)->mi_zone
)
7661 if (fop_realvp(ndvp
, &realvp
, ct
) == 0)
7664 return (nfs4rename(odvp
, onm
, ndvp
, nnm
, cr
, ct
));
7668 * nfs4rename does the real work of renaming in NFS Version 4.
7670 * A file handle is considered volatile for renaming purposes if either
7671 * of the volatile bits are turned on. However, the compound may differ
7672 * based on the likelihood of the filehandle to change during rename.
7675 nfs4rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
7676 caller_context_t
*ct
)
7680 vnode_t
*nvp
= NULL
;
7681 vnode_t
*ovp
= NULL
;
7682 char *tmpname
= NULL
;
7688 nfsstat4 stat
= NFS4_OK
;
7690 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
7691 ASSERT(nfs4_consistent_type(odvp
));
7692 ASSERT(nfs4_consistent_type(ndvp
));
7694 if (onm
[0] == '.' && (onm
[1] == '\0' ||
7695 (onm
[1] == '.' && onm
[2] == '\0')))
7698 if (nnm
[0] == '.' && (nnm
[1] == '\0' ||
7699 (nnm
[1] == '.' && nnm
[2] == '\0')))
7704 if ((intptr_t)odrp
< (intptr_t)ndrp
) {
7705 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR4(odvp
)))
7707 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR4(ndvp
))) {
7708 nfs_rw_exit(&odrp
->r_rwlock
);
7712 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR4(ndvp
)))
7714 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR4(odvp
))) {
7715 nfs_rw_exit(&ndrp
->r_rwlock
);
7721 * Lookup the target file. If it exists, it needs to be
7722 * checked to see whether it is a mount point and whether
7723 * it is active (open).
7725 error
= nfs4lookup(ndvp
, nnm
, &nvp
, cr
, 0);
7729 ASSERT(nfs4_consistent_type(nvp
));
7731 * If this file has been mounted on, then just
7732 * return busy because renaming to it would remove
7733 * the mounted file system from the name space.
7735 if (vn_ismntpt(nvp
)) {
7737 nfs_rw_exit(&odrp
->r_rwlock
);
7738 nfs_rw_exit(&ndrp
->r_rwlock
);
7743 * First just remove the entry from the name cache, as it
7744 * is most likely the only entry for this vp.
7746 dnlc_remove(ndvp
, nnm
);
7750 if (nvp
->v_type
!= VREG
) {
7752 * Purge the name cache of all references to this vnode
7753 * so that we can check the reference count to infer
7754 * whether it is active or not.
7756 if (nvp
->v_count
> 1)
7759 isactive
= nvp
->v_count
> 1;
7761 mutex_enter(&rp
->r_os_lock
);
7762 isactive
= list_head(&rp
->r_open_streams
) != NULL
;
7763 mutex_exit(&rp
->r_os_lock
);
7767 * If the vnode is active and is not a directory,
7768 * arrange to rename it to a
7769 * temporary file so that it will continue to be
7770 * accessible. This implements the "unlink-open-file"
7771 * semantics for the target of a rename operation.
7772 * Before doing this though, make sure that the
7773 * source and target files are not already the same.
7775 if (isactive
&& nvp
->v_type
!= VDIR
) {
7777 * Lookup the source name.
7779 error
= nfs4lookup(odvp
, onm
, &ovp
, cr
, 0);
7782 * The source name *should* already exist.
7786 nfs_rw_exit(&odrp
->r_rwlock
);
7787 nfs_rw_exit(&ndrp
->r_rwlock
);
7791 ASSERT(nfs4_consistent_type(ovp
));
7794 * Compare the two vnodes. If they are the same,
7795 * just release all held vnodes and return success.
7797 if (VN_CMP(ovp
, nvp
)) {
7800 nfs_rw_exit(&odrp
->r_rwlock
);
7801 nfs_rw_exit(&ndrp
->r_rwlock
);
7806 * Can't mix and match directories and non-
7807 * directories in rename operations. We already
7808 * know that the target is not a directory. If
7809 * the source is a directory, return an error.
7811 if (ovp
->v_type
== VDIR
) {
7814 nfs_rw_exit(&odrp
->r_rwlock
);
7815 nfs_rw_exit(&ndrp
->r_rwlock
);
7820 * The target file exists, is not the same as
7821 * the source file, and is active. We first
7822 * try to Link it to a temporary filename to
7823 * avoid having the server removing the file
7824 * completely (which could cause data loss to
7825 * the user's POV in the event the Rename fails
7826 * -- see bug 1165874).
7829 * The do_link and did_link booleans are
7830 * introduced in the event we get NFS4ERR_FILE_OPEN
7831 * returned for the Rename. Some servers can
7832 * not Rename over an Open file, so they return
7833 * this error. The client needs to Remove the
7834 * newly created Link and do two Renames, just
7835 * as if the server didn't support LINK.
7837 tmpname
= newname();
7841 error
= nfs4_link(ndvp
, nvp
, tmpname
, cr
,
7844 if (error
== EOPNOTSUPP
|| !do_link
) {
7845 error
= nfs4_rename(ndvp
, nnm
, ndvp
, tmpname
,
7852 kmem_free(tmpname
, MAXNAMELEN
);
7855 nfs_rw_exit(&odrp
->r_rwlock
);
7856 nfs_rw_exit(&ndrp
->r_rwlock
);
7860 mutex_enter(&rp
->r_statelock
);
7861 if (rp
->r_unldvp
== NULL
) {
7863 rp
->r_unldvp
= ndvp
;
7864 if (rp
->r_unlcred
!= NULL
)
7865 crfree(rp
->r_unlcred
);
7868 rp
->r_unlname
= tmpname
;
7871 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
7872 rp
->r_unlname
= tmpname
;
7874 mutex_exit(&rp
->r_statelock
);
7877 (void) nfs4delegreturn(VTOR4(nvp
), NFS4_DR_PUSH
|NFS4_DR_REOPEN
);
7879 ASSERT(nfs4_consistent_type(nvp
));
7884 * When renaming directories to be a subdirectory of a
7885 * different parent, the dnlc entry for ".." will no
7886 * longer be valid, so it must be removed.
7888 * We do a lookup here to determine whether we are renaming
7889 * a directory and we need to check if we are renaming
7890 * an unlinked file. This might have already been done
7891 * in previous code, so we check ovp == NULL to avoid
7894 error
= nfs4lookup(odvp
, onm
, &ovp
, cr
, 0);
7896 * The source name *should* already exist.
7899 nfs_rw_exit(&odrp
->r_rwlock
);
7900 nfs_rw_exit(&ndrp
->r_rwlock
);
7906 ASSERT(ovp
!= NULL
);
7907 ASSERT(nfs4_consistent_type(ovp
));
7911 * Is the object being renamed a dir, and if so, is
7912 * it being renamed to a child of itself? The underlying
7913 * fs should ultimately return EINVAL for this case;
7914 * however, buggy beta non-Solaris NFSv4 servers at
7915 * interop testing events have allowed this behavior,
7916 * and it caused our client to panic due to a recursive
7917 * mutex_enter in fn_move.
7919 * The tedious locking in fn_move could be changed to
7920 * deal with this case, and the client could avoid the
7921 * panic; however, the client would just confuse itself
7922 * later and misbehave. A better way to handle the broken
7923 * server is to detect this condition and return EINVAL
7924 * without ever sending the the bogus rename to the server.
7925 * We know the rename is invalid -- just fail it now.
7927 if (ovp
->v_type
== VDIR
&& VN_CMP(ndvp
, ovp
)) {
7929 nfs_rw_exit(&odrp
->r_rwlock
);
7930 nfs_rw_exit(&ndrp
->r_rwlock
);
7937 (void) nfs4delegreturn(VTOR4(ovp
), NFS4_DR_PUSH
|NFS4_DR_REOPEN
);
7940 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7941 * possible for the filehandle to change due to the rename.
7942 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7943 * the fh will not change because of the rename, but we still need
7944 * to update its rnode entry with the new name for
7945 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7946 * has no effect on these for now, but for future improvements,
7947 * we might want to use it too to simplify handling of files
7948 * that are open with that flag on. (XXX)
7951 if (NFS4_VOLATILE_FH(mi
))
7952 error
= nfs4rename_volatile_fh(odvp
, onm
, ovp
, ndvp
, nnm
, cr
,
7955 error
= nfs4rename_persistent_fh(odvp
, onm
, ovp
, ndvp
, nnm
, cr
,
7958 ASSERT(nfs4_consistent_type(odvp
));
7959 ASSERT(nfs4_consistent_type(ndvp
));
7960 ASSERT(nfs4_consistent_type(ovp
));
7962 if (stat
== NFS4ERR_FILE_OPEN
&& did_link
) {
7965 * Before the 'link_call' code, we did a nfs4_lookup
7966 * that puts a VN_HOLD on nvp. After the nfs4_link
7967 * call we call VN_RELE to match that hold. We need
7968 * to place an additional VN_HOLD here since we will
7969 * be hitting that VN_RELE again.
7973 (void) nfs4_remove(ndvp
, tmpname
, cr
, NULL
, 0);
7975 /* Undo the unlinked file naming stuff we just did */
7976 mutex_enter(&rp
->r_statelock
);
7979 rp
->r_unldvp
= NULL
;
7980 if (rp
->r_unlcred
!= NULL
)
7981 crfree(rp
->r_unlcred
);
7982 rp
->r_unlcred
= NULL
;
7983 /* rp->r_unlanme points to tmpname */
7985 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
7986 rp
->r_unlname
= NULL
;
7988 mutex_exit(&rp
->r_statelock
);
7998 nfs_rw_exit(&odrp
->r_rwlock
);
7999 nfs_rw_exit(&ndrp
->r_rwlock
);
8007 * when renaming directories to be a subdirectory of a
8008 * different parent, the dnlc entry for ".." will no
8009 * longer be valid, so it must be removed
8013 if (ovp
->v_type
== VDIR
) {
8014 dnlc_remove(ovp
, "..");
8015 if (rp
->r_dir
!= NULL
)
8016 nfs4_purge_rddir_cache(ovp
);
8021 * If we are renaming the unlinked file, update the
8022 * r_unldvp and r_unlname as needed.
8024 mutex_enter(&rp
->r_statelock
);
8025 if (rp
->r_unldvp
!= NULL
) {
8026 if (strcmp(rp
->r_unlname
, onm
) == 0) {
8027 (void) strncpy(rp
->r_unlname
, nnm
, MAXNAMELEN
);
8028 rp
->r_unlname
[MAXNAMELEN
- 1] = '\0';
8029 if (ndvp
!= rp
->r_unldvp
) {
8030 VN_RELE(rp
->r_unldvp
);
8031 rp
->r_unldvp
= ndvp
;
8036 mutex_exit(&rp
->r_statelock
);
8039 * Notify the rename vnevents to source vnode, and to the target
8040 * vnode if it already existed.
8046 * Notify the vnode. Each links is represented by
8047 * a different vnode, in nfsv4.
8052 if (IS_SHADOW(nvp
, trp
))
8054 vnevent_rename_dest(tvp
, ndvp
, nnm
, ct
);
8058 * if the source and destination directory are not the
8059 * same notify the destination directory.
8061 if (VTOR4(odvp
) != VTOR4(ndvp
)) {
8064 if (IS_SHADOW(ndvp
, trp
))
8066 vnevent_rename_dest_dir(tvp
, ct
);
8071 if (IS_SHADOW(ovp
, trp
))
8073 vnevent_rename_src(tvp
, odvp
, onm
, ct
);
8081 nfs_rw_exit(&odrp
->r_rwlock
);
8082 nfs_rw_exit(&ndrp
->r_rwlock
);
8088 * When the parent directory has changed, sv_dfh must be updated
8091 update_parentdir_sfh(vnode_t
*vp
, vnode_t
*ndvp
)
8093 svnode_t
*sv
= VTOSV(vp
);
8094 nfs4_sharedfh_t
*old_dfh
= sv
->sv_dfh
;
8095 nfs4_sharedfh_t
*new_dfh
= VTOR4(ndvp
)->r_fh
;
8098 sv
->sv_dfh
= new_dfh
;
8099 sfh4_rele(&old_dfh
);
8103 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8104 * when it is known that the filehandle is persistent through rename.
8106 * Rename requires that the current fh be the target directory and the
8107 * saved fh be the source directory. After the operation, the current fh
8109 * The compound op structure for persistent fh rename is:
8110 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8111 * Rather than bother with the directory postop args, we'll simply
8112 * update that a change occurred in the cache, so no post-op getattrs.
8115 nfs4rename_persistent_fh(vnode_t
*odvp
, char *onm
, vnode_t
*renvp
,
8116 vnode_t
*ndvp
, char *nnm
, cred_t
*cr
, nfsstat4
*statp
)
8118 COMPOUND4args_clnt args
;
8119 COMPOUND4res_clnt res
, *resp
= NULL
;
8122 int doqueue
, argoplist_size
;
8124 rnode4_t
*odrp
= VTOR4(odvp
);
8125 rnode4_t
*ndrp
= VTOR4(ndvp
);
8128 nfs4_recov_state_t recov_state
;
8129 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8130 dirattr_info_t dinfo
, *dinfop
;
8132 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
8134 recov_state
.rs_flags
= 0;
8135 recov_state
.rs_num_retry_despite_err
= 0;
8138 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8140 * If source/target are different dirs, then append putfh(src); getattr
8142 args
.array_len
= (odvp
== ndvp
) ? 5 : 7;
8143 argoplist_size
= args
.array_len
* sizeof (nfs_argop4
);
8144 args
.array
= argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
8149 /* No need to Lookup the file, persistent fh */
8150 args
.ctag
= TAG_RENAME
;
8153 e
.error
= nfs4_start_op(mi
, odvp
, ndvp
, &recov_state
);
8155 kmem_free(argop
, argoplist_size
);
8159 /* 0: putfh source directory */
8160 argop
[0].argop
= OP_CPUTFH
;
8161 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= odrp
->r_fh
;
8163 /* 1: Save source fh to free up current for target */
8164 argop
[1].argop
= OP_SAVEFH
;
8166 /* 2: putfh targetdir */
8167 argop
[2].argop
= OP_CPUTFH
;
8168 argop
[2].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8170 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8171 argop
[3].argop
= OP_CRENAME
;
8172 argop
[3].nfs_argop4_u
.opcrename
.coldname
= onm
;
8173 argop
[3].nfs_argop4_u
.opcrename
.cnewname
= nnm
;
8175 /* 4: getattr (targetdir) */
8176 argop
[4].argop
= OP_GETATTR
;
8177 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8178 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
8182 /* 5: putfh (sourcedir) */
8183 argop
[5].argop
= OP_CPUTFH
;
8184 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8186 /* 6: getattr (sourcedir) */
8187 argop
[6].argop
= OP_GETATTR
;
8188 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8189 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
8192 dnlc_remove(odvp
, onm
);
8193 dnlc_remove(ndvp
, nnm
);
8196 dinfo
.di_time_call
= gethrtime();
8197 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8199 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8201 PURGE_ATTRCACHE4(odvp
);
8202 PURGE_ATTRCACHE4(ndvp
);
8204 *statp
= res
.status
;
8208 if (nfs4_start_recovery(&e
, mi
, odvp
, ndvp
, NULL
, NULL
,
8209 OP_RENAME
, NULL
, NULL
, NULL
) == FALSE
) {
8210 nfs4_end_op(mi
, odvp
, ndvp
, &recov_state
, needrecov
);
8212 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
8220 * as long as OP_RENAME
8222 if (res
.status
!= NFS4_OK
&& res
.array_len
<= 4) {
8223 e
.error
= geterrno4(res
.status
);
8224 PURGE_ATTRCACHE4(odvp
);
8225 PURGE_ATTRCACHE4(ndvp
);
8227 * System V defines rename to return EEXIST, not
8228 * ENOTEMPTY if the target directory is not empty.
8229 * Over the wire, the error is NFSERR_ENOTEMPTY
8230 * which geterrno4 maps to ENOTEMPTY.
8232 if (e
.error
== ENOTEMPTY
)
8236 resop
= &res
.array
[3]; /* rename res */
8237 rn_res
= &resop
->nfs_resop4_u
.oprename
;
8239 if (res
.status
== NFS4_OK
) {
8241 * Update target attribute, readdir and dnlc
8245 &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
8251 nfs4_update_dircaches(&rn_res
->target_cinfo
,
8252 ndvp
, NULL
, NULL
, dinfop
);
8255 * Update source attribute, readdir and dnlc caches
8259 update_parentdir_sfh(renvp
, ndvp
);
8263 &(res
.array
[6].nfs_resop4_u
.
8266 nfs4_update_dircaches(&rn_res
->source_cinfo
,
8267 odvp
, NULL
, NULL
, dinfop
);
8270 fn_move(VTOSV(renvp
)->sv_name
, VTOSV(ndvp
)->sv_name
,
8276 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8277 nfs4_end_op(mi
, odvp
, ndvp
, &recov_state
, needrecov
);
8278 kmem_free(argop
, argoplist_size
);
8284 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8285 * it is possible for the filehandle to change due to the rename.
8287 * The compound req in this case includes a post-rename lookup and getattr
8288 * to ensure that we have the correct fh and attributes for the object.
8290 * Rename requires that the current fh be the target directory and the
8291 * saved fh be the source directory. After the operation, the current fh
8294 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8295 * update the filehandle for the renamed object. We also get the old
8296 * filehandle for historical reasons; this should be taken out sometime.
8297 * This results in a rather cumbersome compound...
8299 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8300 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8304 nfs4rename_volatile_fh(vnode_t
*odvp
, char *onm
, vnode_t
*ovp
,
8305 vnode_t
*ndvp
, char *nnm
, cred_t
*cr
, nfsstat4
*statp
)
8307 COMPOUND4args_clnt args
;
8308 COMPOUND4res_clnt res
, *resp
= NULL
;
8314 rnode4_t
*odrp
= VTOR4(odvp
); /* old directory */
8315 rnode4_t
*ndrp
= VTOR4(ndvp
); /* new directory */
8316 rnode4_t
*orp
= VTOR4(ovp
); /* object being renamed */
8320 nfs4_recov_state_t recov_state
;
8322 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8323 dirattr_info_t dinfo
, *dinfop
= &dinfo
;
8325 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
8327 recov_state
.rs_flags
= 0;
8328 recov_state
.rs_num_retry_despite_err
= 0;
8334 * There is a window between the RPC and updating the path and
8335 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8336 * code, so that it doesn't try to use the old path during that
8339 mutex_enter(&orp
->r_statelock
);
8340 while (orp
->r_flags
& R4RECEXPFH
) {
8341 klwp_t
*lwp
= ttolwp(curthread
);
8345 if (cv_wait_sig(&orp
->r_cv
, &orp
->r_statelock
) == 0) {
8346 mutex_exit(&orp
->r_statelock
);
8354 orp
->r_flags
|= R4RECEXPFH
;
8355 mutex_exit(&orp
->r_statelock
);
8359 args
.ctag
= TAG_RENAME_VFH
;
8360 args
.array_len
= (odvp
== ndvp
) ? 10 : 12;
8361 argoplist_size
= args
.array_len
* sizeof (nfs_argop4
);
8362 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
8366 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8367 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8368 * LOOKUP(trgt), GETFH(new), GETATTR,
8371 * add putfh(sourcedir), getattr(sourcedir) }
8375 e
.error
= nfs4_start_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8376 &recov_state
, NULL
);
8378 kmem_free(argop
, argoplist_size
);
8379 mutex_enter(&orp
->r_statelock
);
8380 orp
->r_flags
&= ~R4RECEXPFH
;
8381 cv_broadcast(&orp
->r_cv
);
8382 mutex_exit(&orp
->r_statelock
);
8386 /* 0: putfh source directory */
8387 argop
[0].argop
= OP_CPUTFH
;
8388 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= odrp
->r_fh
;
8390 /* 1: Save source fh to free up current for target */
8391 argop
[1].argop
= OP_SAVEFH
;
8393 /* 2: Lookup pre-rename fh of renamed object */
8394 argop
[2].argop
= OP_CLOOKUP
;
8395 argop
[2].nfs_argop4_u
.opclookup
.cname
= onm
;
8397 /* 3: getfh fh of renamed object (before rename) */
8398 argop
[3].argop
= OP_GETFH
;
8400 /* 4: putfh targetdir */
8401 argop
[4].argop
= OP_CPUTFH
;
8402 argop
[4].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8404 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8405 argop
[5].argop
= OP_CRENAME
;
8406 argop
[5].nfs_argop4_u
.opcrename
.coldname
= onm
;
8407 argop
[5].nfs_argop4_u
.opcrename
.cnewname
= nnm
;
8409 /* 6: getattr of target dir (post op attrs) */
8410 argop
[6].argop
= OP_GETATTR
;
8411 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8412 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
8414 /* 7: Lookup post-rename fh of renamed object */
8415 argop
[7].argop
= OP_CLOOKUP
;
8416 argop
[7].nfs_argop4_u
.opclookup
.cname
= nnm
;
8418 /* 8: getfh fh of renamed object (after rename) */
8419 argop
[8].argop
= OP_GETFH
;
8421 /* 9: getattr of renamed object */
8422 argop
[9].argop
= OP_GETATTR
;
8423 argop
[9].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8424 argop
[9].nfs_argop4_u
.opgetattr
.mi
= mi
;
8427 * If source/target dirs are different, then get new post-op
8428 * attrs for source dir also.
8431 /* 10: putfh (sourcedir) */
8432 argop
[10].argop
= OP_CPUTFH
;
8433 argop
[10].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8435 /* 11: getattr (sourcedir) */
8436 argop
[11].argop
= OP_GETATTR
;
8437 argop
[11].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8438 argop
[11].nfs_argop4_u
.opgetattr
.mi
= mi
;
8441 dnlc_remove(odvp
, onm
);
8442 dnlc_remove(ndvp
, nnm
);
8446 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8448 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8450 PURGE_ATTRCACHE4(odvp
);
8451 PURGE_ATTRCACHE4(ndvp
);
8453 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8454 &recov_state
, needrecov
);
8458 *statp
= res
.status
;
8464 abort
= nfs4_start_recovery(&e
, mi
, odvp
, ndvp
, NULL
, NULL
,
8465 OP_RENAME
, NULL
, NULL
, NULL
);
8466 if (abort
== FALSE
) {
8467 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8468 &recov_state
, needrecov
);
8469 kmem_free(argop
, argoplist_size
);
8471 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
8472 mutex_enter(&orp
->r_statelock
);
8473 orp
->r_flags
&= ~R4RECEXPFH
;
8474 cv_broadcast(&orp
->r_cv
);
8475 mutex_exit(&orp
->r_statelock
);
8479 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8480 &recov_state
, needrecov
);
8483 /* fall through for res.status case */
8489 * If OP_RENAME (or any prev op) failed, then return an error.
8490 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8492 if ((res
.status
!= NFS4_OK
) && (res
.array_len
<= 6)) {
8494 * Error in an op other than last Getattr
8496 e
.error
= geterrno4(res
.status
);
8497 PURGE_ATTRCACHE4(odvp
);
8498 PURGE_ATTRCACHE4(ndvp
);
8500 * System V defines rename to return EEXIST, not
8501 * ENOTEMPTY if the target directory is not empty.
8502 * Over the wire, the error is NFSERR_ENOTEMPTY
8503 * which geterrno4 maps to ENOTEMPTY.
8505 if (e
.error
== ENOTEMPTY
)
8507 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
, &recov_state
,
8512 /* rename results */
8513 rn_res
= &res
.array
[5].nfs_resop4_u
.oprename
;
8515 if (res
.status
== NFS4_OK
) {
8516 /* Update target attribute, readdir and dnlc caches */
8518 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
8520 dinfo
.di_time_call
= t
;
8524 /* Update source cache attribute, readdir and dnlc caches */
8525 nfs4_update_dircaches(&rn_res
->target_cinfo
, ndvp
, NULL
, NULL
, dinfop
);
8527 /* Update source cache attribute, readdir and dnlc caches */
8529 update_parentdir_sfh(ovp
, ndvp
);
8532 * If dinfop is non-NULL, then compound succeded, so
8533 * set di_garp to attrs for source dir. dinfop is only
8534 * set to NULL when compound fails.
8538 &res
.array
[11].nfs_resop4_u
.opgetattr
.ga_res
;
8539 nfs4_update_dircaches(&rn_res
->source_cinfo
, odvp
, NULL
, NULL
,
8544 * Update the rnode with the new component name and args,
8545 * and if the file handle changed, also update it with the new fh.
8546 * This is only necessary if the target object has an rnode
8547 * entry and there is no need to create one for it.
8549 resop
= &res
.array
[8]; /* getfh new res */
8550 ngf_res
= &resop
->nfs_resop4_u
.opgetfh
;
8553 * Update the path and filehandle for the renamed object.
8555 nfs4rename_update(ovp
, ndvp
, &ngf_res
->object
, nnm
);
8557 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
, &recov_state
, needrecov
);
8559 if (res
.status
== NFS4_OK
) {
8560 resop
++; /* getattr res */
8561 e
.error
= nfs4_update_attrcache(res
.status
,
8562 &resop
->nfs_resop4_u
.opgetattr
.ga_res
,
8567 kmem_free(argop
, argoplist_size
);
8569 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8570 mutex_enter(&orp
->r_statelock
);
8571 orp
->r_flags
&= ~R4RECEXPFH
;
8572 cv_broadcast(&orp
->r_cv
);
8573 mutex_exit(&orp
->r_statelock
);
8580 nfs4_mkdir(vnode_t
*dvp
, char *nm
, struct vattr
*va
, vnode_t
**vpp
, cred_t
*cr
,
8581 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
8586 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
8589 * As ".." has special meaning and rather than send a mkdir
8590 * over the wire to just let the server freak out, we just
8591 * short circuit it here and return EEXIST
8593 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0')
8597 * Decision to get the right gid and setgid bit of the
8598 * new directory is now made in call_nfs4_create_req.
8600 va
->va_mask
|= VATTR_MODE
;
8601 error
= call_nfs4_create_req(dvp
, nm
, NULL
, va
, &vp
, cr
, NF4DIR
);
8611 * rmdir is using the same remove v4 op as does remove.
8612 * Remove requires that the current fh be the target directory.
8613 * After the operation, the current fh is unchanged.
8614 * The compound op structure is:
8615 * PUTFH(targetdir), REMOVE
8619 nfs4_rmdir(vnode_t
*dvp
, char *nm
, vnode_t
*cdir
, cred_t
*cr
,
8620 caller_context_t
*ct
, int flags
)
8622 int need_end_op
= FALSE
;
8623 COMPOUND4args_clnt args
;
8624 COMPOUND4res_clnt res
, *resp
= NULL
;
8626 nfs_argop4 argop
[3];
8632 bool_t needrecov
= FALSE
;
8633 nfs4_recov_state_t recov_state
;
8634 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8635 dirattr_info_t dinfo
, *dinfop
;
8637 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
8640 * As ".." has special meaning and rather than send a rmdir
8641 * over the wire to just let the server freak out, we just
8642 * short circuit it here and return EEXIST
8644 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0')
8648 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
8652 * Attempt to prevent a rmdir(".") from succeeding.
8654 e
.error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 0);
8656 nfs_rw_exit(&drp
->r_rwlock
);
8661 nfs_rw_exit(&drp
->r_rwlock
);
8666 * Since nfsv4 remove op works on both files and directories,
8667 * check that the removed object is indeed a directory.
8669 if (vp
->v_type
!= VDIR
) {
8671 nfs_rw_exit(&drp
->r_rwlock
);
8676 * First just remove the entry from the name cache, as it
8677 * is most likely an entry for this vp.
8679 dnlc_remove(dvp
, nm
);
8682 * If there vnode reference count is greater than one, then
8683 * there may be additional references in the DNLC which will
8684 * need to be purged. First, trying removing the entry for
8685 * the parent directory and see if that removes the additional
8686 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8687 * to completely remove any references to the directory which
8688 * might still exist in the DNLC.
8690 if (vp
->v_count
> 1) {
8691 dnlc_remove(vp
, "..");
8692 if (vp
->v_count
> 1)
8697 recov_state
.rs_flags
= 0;
8698 recov_state
.rs_num_retry_despite_err
= 0;
8701 args
.ctag
= TAG_RMDIR
;
8704 * Rmdir ops: putfh dir; remove
8709 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
8711 nfs_rw_exit(&drp
->r_rwlock
);
8716 /* putfh directory */
8717 argop
[0].argop
= OP_CPUTFH
;
8718 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
8721 argop
[1].argop
= OP_CREMOVE
;
8722 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= nm
;
8724 /* getattr (postop attrs for dir that contained removed dir) */
8725 argop
[2].argop
= OP_GETATTR
;
8726 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8727 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
8729 dinfo
.di_time_call
= gethrtime();
8731 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8733 PURGE_ATTRCACHE4(vp
);
8735 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8737 PURGE_ATTRCACHE4(dvp
);
8741 if (nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
, NULL
, NULL
,
8742 NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
8744 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
8746 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
8748 need_end_op
= FALSE
;
8757 * Only return error if first 2 ops (OP_REMOVE or earlier)
8760 if (res
.status
!= NFS4_OK
&& res
.array_len
<= 2) {
8761 e
.error
= geterrno4(res
.status
);
8762 PURGE_ATTRCACHE4(dvp
);
8763 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
,
8764 &recov_state
, needrecov
);
8765 need_end_op
= FALSE
;
8766 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
8768 * System V defines rmdir to return EEXIST, not
8769 * ENOTEMPTY if the directory is not empty. Over
8770 * the wire, the error is NFSERR_ENOTEMPTY which
8771 * geterrno4 maps to ENOTEMPTY.
8773 if (e
.error
== ENOTEMPTY
)
8776 resop
= &res
.array
[1]; /* remove res */
8777 rm_res
= &resop
->nfs_resop4_u
.opremove
;
8779 if (res
.status
== NFS4_OK
) {
8780 resop
= &res
.array
[2]; /* dir attrs */
8782 &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
8788 /* Update dir attribute, readdir and dnlc caches */
8789 nfs4_update_dircaches(&rm_res
->cinfo
, dvp
, NULL
, NULL
,
8792 /* destroy rddir cache for dir that was removed */
8793 if (VTOR4(vp
)->r_dir
!= NULL
)
8794 nfs4_purge_rddir_cache(vp
);
8799 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
8801 nfs_rw_exit(&drp
->r_rwlock
);
8804 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8811 if (IS_SHADOW(vp
, trp
))
8813 vnevent_rmdir(tvp
, dvp
, nm
, ct
);
8823 nfs4_symlink(vnode_t
*dvp
, char *lnm
, struct vattr
*tva
, char *tnm
, cred_t
*cr
,
8824 caller_context_t
*ct
, int flags
)
8830 mntinfo4_t
*mi
= VTOMI4(dvp
);
8832 if (nfs_zone() != mi
->mi_zone
)
8834 if (!(mi
->mi_flags
& MI4_SYMLINK
))
8835 return (EOPNOTSUPP
);
8837 error
= call_nfs4_create_req(dvp
, lnm
, tnm
, tva
, &vp
, cr
, NF4LNK
);
8841 ASSERT(nfs4_consistent_type(vp
));
8843 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
== NULL
) {
8845 contents
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
8847 if (contents
!= NULL
) {
8848 mutex_enter(&rp
->r_statelock
);
8849 if (rp
->r_symlink
.contents
== NULL
) {
8850 rp
->r_symlink
.len
= strlen(tnm
);
8851 bcopy(tnm
, contents
, rp
->r_symlink
.len
);
8852 rp
->r_symlink
.contents
= contents
;
8853 rp
->r_symlink
.size
= MAXPATHLEN
;
8854 mutex_exit(&rp
->r_statelock
);
8856 mutex_exit(&rp
->r_statelock
);
8857 kmem_free((void *)contents
, MAXPATHLEN
);
8868 * Read directory entries.
8869 * There are some weird things to look out for here. The uio_loffset
8870 * field is either 0 or it is the offset returned from a previous
8871 * readdir. It is an opaque value used by the server to find the
8872 * correct directory block to read. The count field is the number
8873 * of blocks to read on the server. This is advisory only, the server
8874 * may return only one block's worth of entries. Entries may be compressed
8879 nfs4_readdir(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, int *eofp
,
8880 caller_context_t
*ct
, int flags
)
8888 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
8892 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
8895 * Make sure that the directory cache is valid.
8897 if (rp
->r_dir
!= NULL
) {
8898 if (nfs_disable_rddir_cache
!= 0) {
8900 * Setting nfs_disable_rddir_cache in /etc/system
8901 * allows interoperability with servers that do not
8902 * properly update the attributes of directories.
8903 * Any cached information gets purged before an
8904 * access is made to it.
8906 nfs4_purge_rddir_cache(vp
);
8909 error
= nfs4_validate_caches(vp
, cr
);
8914 count
= MIN(uiop
->uio_iov
->iov_len
, MAXBSIZE
);
8917 * Short circuit last readdir which always returns 0 bytes.
8918 * This can be done after the directory has been read through
8919 * completely at least once. This will set r_direof which
8920 * can be used to find the value of the last cookie.
8922 mutex_enter(&rp
->r_statelock
);
8923 if (rp
->r_direof
!= NULL
&&
8924 uiop
->uio_loffset
== rp
->r_direof
->nfs4_ncookie
) {
8925 mutex_exit(&rp
->r_statelock
);
8927 nfs4_readdir_cache_shorts
++;
8935 * Look for a cache entry. Cache entries are identified
8936 * by the NFS cookie value and the byte count requested.
8938 rdc
= rddir4_cache_lookup(rp
, uiop
->uio_loffset
, count
);
8941 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8944 mutex_exit(&rp
->r_statelock
);
8949 * Check to see if we need to fill this entry in.
8951 if (rdc
->flags
& RDDIRREQ
) {
8952 rdc
->flags
&= ~RDDIRREQ
;
8953 rdc
->flags
|= RDDIR
;
8954 mutex_exit(&rp
->r_statelock
);
8959 nfs4readdir(vp
, rdc
, cr
);
8962 * Reacquire the lock, so that we can continue
8964 mutex_enter(&rp
->r_statelock
);
8966 * The entry is now complete
8968 rdc
->flags
&= ~RDDIR
;
8971 ASSERT(!(rdc
->flags
& RDDIR
));
8974 * If an error occurred while attempting
8975 * to fill the cache entry, mark the entry invalid and
8976 * just return the error.
8980 rdc
->flags
|= RDDIRREQ
;
8981 rddir4_cache_rele(rp
, rdc
);
8982 mutex_exit(&rp
->r_statelock
);
8987 * The cache entry is complete and good,
8988 * copyout the dirent structs to the calling
8991 error
= uiomove(rdc
->entries
, rdc
->actlen
, UIO_READ
, uiop
);
8994 * If no error occurred during the copyout,
8995 * update the offset in the uio struct to
8996 * contain the value of the next NFS 4 cookie
8997 * and set the eof value appropriately.
9000 uiop
->uio_loffset
= rdc
->nfs4_ncookie
;
9006 * Decide whether to do readahead. Don't if we
9007 * have already read to the end of directory.
9011 * Make the entry the direof only if it is cached
9013 if (rdc
->flags
& RDDIRCACHED
)
9015 rddir4_cache_rele(rp
, rdc
);
9016 mutex_exit(&rp
->r_statelock
);
9020 /* Determine if a readdir readahead should be done */
9021 if (!(rp
->r_flags
& R4LOOKUP
)) {
9022 rddir4_cache_rele(rp
, rdc
);
9023 mutex_exit(&rp
->r_statelock
);
9028 * Now look for a readahead entry.
9030 * Check to see whether we found an entry for the readahead.
9031 * If so, we don't need to do anything further, so free the new
9032 * entry if one was allocated. Otherwise, allocate a new entry, add
9033 * it to the cache, and then initiate an asynchronous readdir
9034 * operation to fill it.
9036 rrdc
= rddir4_cache_lookup(rp
, rdc
->nfs4_ncookie
, count
);
9039 * A readdir cache entry could not be obtained for the readahead. In
9040 * this case we skip the readahead and return.
9043 rddir4_cache_rele(rp
, rdc
);
9044 mutex_exit(&rp
->r_statelock
);
9049 * Check to see if we need to fill this entry in.
9051 if (rrdc
->flags
& RDDIRREQ
) {
9052 rrdc
->flags
&= ~RDDIRREQ
;
9053 rrdc
->flags
|= RDDIR
;
9054 rddir4_cache_rele(rp
, rdc
);
9055 mutex_exit(&rp
->r_statelock
);
9057 nfs4_readdir_readahead
++;
9062 nfs4_async_readdir(vp
, rrdc
, cr
, do_nfs4readdir
);
9066 rddir4_cache_rele(rp
, rrdc
);
9067 rddir4_cache_rele(rp
, rdc
);
9068 mutex_exit(&rp
->r_statelock
);
9073 do_nfs4readdir(vnode_t
*vp
, rddir4_cache
*rdc
, cred_t
*cr
)
9078 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
9083 * Obtain the readdir results for the caller.
9085 nfs4readdir(vp
, rdc
, cr
);
9087 mutex_enter(&rp
->r_statelock
);
9089 * The entry is now complete
9091 rdc
->flags
&= ~RDDIR
;
9095 rdc
->flags
|= RDDIRREQ
;
9096 rddir4_cache_rele(rp
, rdc
);
9097 mutex_exit(&rp
->r_statelock
);
9103 * Read directory entries.
9104 * There are some weird things to look out for here. The uio_loffset
9105 * field is either 0 or it is the offset returned from a previous
9106 * readdir. It is an opaque value used by the server to find the
9107 * correct directory block to read. The count field is the number
9108 * of blocks to read on the server. This is advisory only, the server
9109 * may return only one block's worth of entries. Entries may be compressed
9112 * Generates the following compound request:
9113 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9114 * must include a Lookupp as well. In this case, send:
9115 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9116 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9118 * Get complete attributes and filehandles for entries if this is the
9119 * first read of the directory. Otherwise, just get fileid's.
9122 nfs4readdir(vnode_t
*vp
, rddir4_cache
*rdc
, cred_t
*cr
)
9124 COMPOUND4args_clnt args
;
9125 COMPOUND4res_clnt res
;
9126 READDIR4args
*rargs
;
9127 READDIR4res_clnt
*rd_res
;
9129 nfs_argop4 argop
[5];
9131 rnode4_t
*rp
= VTOR4(vp
);
9132 mntinfo4_t
*mi
= VTOMI4(vp
);
9134 u_longlong_t nodeid
, pnodeid
; /* id's of dir and its parents */
9136 nfs_cookie4 cookie
= (nfs_cookie4
)rdc
->nfs4_cookie
;
9137 int num_ops
, res_opcnt
;
9138 bool_t needrecov
= FALSE
;
9139 nfs4_recov_state_t recov_state
;
9141 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
9143 ASSERT(nfs_zone() == mi
->mi_zone
);
9144 ASSERT(rdc
->flags
& RDDIR
);
9145 ASSERT(rdc
->entries
== NULL
);
9148 * If rp were a stub, it should have triggered and caused
9149 * a mount for us to get this far.
9151 ASSERT(!RP_ISSTUB(rp
));
9154 if (cookie
== (nfs_cookie4
)0 || cookie
== (nfs_cookie4
)1) {
9156 * Since nfsv4 readdir may not return entries for "." and "..",
9157 * the client must recreate them:
9158 * To find the correct nodeid, do the following:
9159 * For current node, get nodeid from dnlc.
9160 * - if current node is rootvp, set pnodeid to nodeid.
9161 * - else if parent is in the dnlc, get its nodeid from there.
9162 * - else add LOOKUPP+GETATTR to compound.
9164 nodeid
= rp
->r_attr
.va_nodeid
;
9165 if (vp
->v_flag
& VROOT
) {
9166 pnodeid
= nodeid
; /* root of mount point */
9168 dvp
= dnlc_lookup(vp
, "..");
9169 if (dvp
!= NULL
&& dvp
!= DNLC_NO_VNODE
) {
9170 /* parent in dnlc cache - no need for otw */
9171 pnodeid
= VTOR4(dvp
)->r_attr
.va_nodeid
;
9174 * parent not in dnlc cache,
9175 * do lookupp to get its id
9178 pnodeid
= 0; /* set later by getattr parent */
9184 recov_state
.rs_flags
= 0;
9185 recov_state
.rs_num_retry_despite_err
= 0;
9187 /* Save the original mount point security flavor */
9188 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
9191 args
.ctag
= TAG_READDIR
;
9194 args
.array_len
= num_ops
;
9196 if (e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9197 &recov_state
, NULL
)) {
9199 * If readdir a node that is a stub for a crossed mount point,
9200 * keep the original secinfo flavor for the current file
9201 * system, not the crossed one.
9203 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9204 rdc
->error
= e
.error
;
9209 * Determine which attrs to request for dirents. This code
9210 * must be protected by nfs4_start/end_fop because of r_server
9211 * (which will change during failover recovery).
9214 if (rp
->r_flags
& (R4LOOKUP
| R4READDIRWATTR
)) {
9216 * Get all vattr attrs plus filehandle and rdattr_error
9218 rd_bitsval
= NFS4_VATTR_MASK
|
9219 FATTR4_RDATTR_ERROR_MASK
|
9220 FATTR4_FILEHANDLE_MASK
;
9222 if (rp
->r_flags
& R4READDIRWATTR
) {
9223 mutex_enter(&rp
->r_statelock
);
9224 rp
->r_flags
&= ~R4READDIRWATTR
;
9225 mutex_exit(&rp
->r_statelock
);
9228 servinfo4_t
*svp
= rp
->r_server
;
9231 * Already read directory. Use readdir with
9232 * no attrs (except for mounted_on_fileid) for updates.
9234 rd_bitsval
= FATTR4_RDATTR_ERROR_MASK
;
9237 * request mounted on fileid if supported, else request
9238 * fileid. maybe we should verify that fileid is supported
9239 * and request something else if not.
9241 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
9242 if (svp
->sv_supp_attrs
& FATTR4_MOUNTED_ON_FILEID_MASK
)
9243 rd_bitsval
|= FATTR4_MOUNTED_ON_FILEID_MASK
;
9244 nfs_rw_exit(&svp
->sv_lock
);
9247 /* putfh directory fh */
9248 argop
[0].argop
= OP_CPUTFH
;
9249 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
9251 argop
[1].argop
= OP_READDIR
;
9252 rargs
= &argop
[1].nfs_argop4_u
.opreaddir
;
9254 * 1 and 2 are reserved for client "." and ".." entry offset.
9255 * cookie 0 should be used over-the-wire to start reading at
9256 * the beginning of the directory excluding "." and "..".
9258 if (rdc
->nfs4_cookie
== 0 ||
9259 rdc
->nfs4_cookie
== 1 ||
9260 rdc
->nfs4_cookie
== 2) {
9261 rargs
->cookie
= (nfs_cookie4
)0;
9262 rargs
->cookieverf
= 0;
9264 rargs
->cookie
= (nfs_cookie4
)rdc
->nfs4_cookie
;
9265 mutex_enter(&rp
->r_statelock
);
9266 rargs
->cookieverf
= rp
->r_cookieverf4
;
9267 mutex_exit(&rp
->r_statelock
);
9269 rargs
->dircount
= MIN(rdc
->buflen
, mi
->mi_tsize
);
9270 rargs
->maxcount
= mi
->mi_tsize
;
9271 rargs
->attr_request
= rd_bitsval
;
9279 * If count < than the minimum required, we return no entries
9280 * and fail with EINVAL
9282 if (rargs
->dircount
< (DIRENT_RECLEN(1) + DIRENT_RECLEN(2))) {
9283 rdc
->error
= EINVAL
;
9287 if (args
.array_len
== 5) {
9289 * Add lookupp and getattr for parent nodeid.
9291 argop
[2].argop
= OP_LOOKUPP
;
9293 argop
[3].argop
= OP_GETFH
;
9295 /* getattr parent */
9296 argop
[4].argop
= OP_GETATTR
;
9297 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
9298 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
9303 if (mi
->mi_io_kstats
) {
9304 mutex_enter(&mi
->mi_lock
);
9305 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
9306 mutex_exit(&mi
->mi_lock
);
9309 /* capture the time of this call */
9310 rargs
->t
= t
= gethrtime();
9312 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
9314 if (mi
->mi_io_kstats
) {
9315 mutex_enter(&mi
->mi_lock
);
9316 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
9317 mutex_exit(&mi
->mi_lock
);
9320 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
9323 * If RPC error occurred and it isn't an error that
9324 * triggers recovery, then go ahead and fail now.
9326 if (e
.error
!= 0 && !needrecov
) {
9327 rdc
->error
= e
.error
;
9334 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
9335 "nfs4readdir: initiating recovery.\n"));
9337 abort
= nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
9338 NULL
, OP_READDIR
, NULL
, NULL
, NULL
);
9339 if (abort
== FALSE
) {
9340 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9341 &recov_state
, needrecov
);
9343 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
9344 if (rdc
->entries
!= NULL
) {
9345 kmem_free(rdc
->entries
, rdc
->entlen
);
9346 rdc
->entries
= NULL
;
9352 rdc
->error
= e
.error
;
9356 /* fall through for res.status case */
9359 res_opcnt
= res
.array_len
;
9362 * If compound failed first 2 ops (PUTFH+READDIR), then return
9363 * failure here. Subsequent ops are for filling out dot-dot
9364 * dirent, and if they fail, we still want to give the caller
9365 * the dirents returned by (the successful) READDIR op, so we need
9366 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9368 * One example where PUTFH+READDIR ops would succeed but
9369 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9370 * but lacks x. In this case, a POSIX server's fop_readdir
9371 * would succeed; however, fop_lookup(..) would fail since no
9372 * x perm. We need to come up with a non-vendor-specific way
9373 * for a POSIX server to return d_ino from dotdot's dirent if
9374 * client only requests mounted_on_fileid, and just say the
9375 * LOOKUPP succeeded and fill out the GETATTR. However, if
9376 * client requested any mandatory attrs, server would be required
9377 * to fail the GETATTR op because it can't call fop_lookup+fop_getattr
9382 if (res_opcnt
<= 2) {
9383 e
.error
= geterrno4(res
.status
);
9384 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9385 &recov_state
, needrecov
);
9386 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
9387 rdc
->error
= e
.error
;
9388 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
9389 if (rdc
->entries
!= NULL
) {
9390 kmem_free(rdc
->entries
, rdc
->entlen
);
9391 rdc
->entries
= NULL
;
9394 * If readdir a node that is a stub for a
9395 * crossed mount point, keep the original
9396 * secinfo flavor for the current file system,
9397 * not the crossed one.
9399 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9404 resop
= &res
.array
[1]; /* readdir res */
9405 rd_res
= &resop
->nfs_resop4_u
.opreaddirclnt
;
9407 mutex_enter(&rp
->r_statelock
);
9408 rp
->r_cookieverf4
= rd_res
->cookieverf
;
9409 mutex_exit(&rp
->r_statelock
);
9412 * For "." and ".." entries
9414 * seek(cookie=0) -> "." entry with d_off = 1
9415 * seek(cookie=1) -> ".." entry with d_off = 2
9417 if (cookie
== (nfs_cookie4
) 0) {
9419 rd_res
->dotp
->d_ino
= nodeid
;
9420 if (rd_res
->dotdotp
)
9421 rd_res
->dotdotp
->d_ino
= pnodeid
;
9423 if (cookie
== (nfs_cookie4
) 1) {
9424 if (rd_res
->dotdotp
)
9425 rd_res
->dotdotp
->d_ino
= pnodeid
;
9429 /* LOOKUPP+GETATTR attemped */
9430 if (args
.array_len
== 5 && rd_res
->dotdotp
) {
9431 if (res
.status
== NFS4_OK
&& res_opcnt
== 5) {
9433 nfs4_sharedfh_t
*sfhp
;
9435 nfs4_ga_res_t
*garp
;
9437 resop
++; /* lookupp */
9438 resop
++; /* getfh */
9439 fhp
= &resop
->nfs_resop4_u
.opgetfh
.object
;
9441 resop
++; /* getattr of parent */
9444 * First, take care of finishing the
9447 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
9449 * The d_ino of .. must be the inode number
9450 * of the mounted filesystem.
9452 if (garp
->n4g_va
.va_mask
& VATTR_NODEID
)
9453 rd_res
->dotdotp
->d_ino
=
9454 garp
->n4g_va
.va_nodeid
;
9458 * Next, create the ".." dnlc entry
9460 sfhp
= sfh4_get(fhp
, mi
);
9461 if (!nfs4_make_dotdot(sfhp
, t
, vp
, cr
, &pvp
, 0)) {
9462 dnlc_update(vp
, "..", pvp
);
9469 if (mi
->mi_io_kstats
) {
9470 mutex_enter(&mi
->mi_lock
);
9471 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
9472 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= rdc
->actlen
;
9473 mutex_exit(&mi
->mi_lock
);
9476 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
9480 * If readdir a node that is a stub for a crossed mount point,
9481 * keep the original secinfo flavor for the current file system,
9482 * not the crossed one.
9484 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9486 nfs4_end_fop(mi
, vp
, NULL
, OH_READDIR
, &recov_state
, needrecov
);
9491 nfs4_bio(struct buf
*bp
, stable_how4
*stab_comm
, cred_t
*cr
, bool_t readahead
)
9493 rnode4_t
*rp
= VTOR4(bp
->b_vp
);
9496 cred_t
*cred_otw
= NULL
;
9498 nfs4_open_stream_t
*osp
= NULL
;
9499 bool_t first_time
= TRUE
; /* first time getting otw cred */
9500 bool_t last_time
= FALSE
; /* last time getting otw cred */
9502 ASSERT(nfs_zone() == VTOMI4(bp
->b_vp
)->mi_zone
);
9504 DTRACE_IO1(start
, struct buf
*, bp
);
9505 offset
= ldbtob(bp
->b_lblkno
);
9507 if (bp
->b_flags
& B_READ
) {
9510 * Releases the osp, if it is provided.
9511 * Puts a hold on the cred_otw and the new osp (if found).
9513 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
9514 &first_time
, &last_time
);
9515 error
= bp
->b_error
= nfs4read(bp
->b_vp
, bp
->b_un
.b_addr
,
9516 offset
, bp
->b_bcount
, &bp
->b_resid
, cred_otw
,
9522 * Didn't get it all because we hit EOF,
9523 * zero all the memory beyond the EOF.
9525 /* bzero(rdaddr + */
9526 bzero(bp
->b_un
.b_addr
+
9527 bp
->b_bcount
- bp
->b_resid
, bp
->b_resid
);
9529 mutex_enter(&rp
->r_statelock
);
9530 if (bp
->b_resid
== bp
->b_bcount
&&
9531 offset
>= rp
->r_size
) {
9533 * We didn't read anything at all as we are
9534 * past EOF. Return an error indicator back
9535 * but don't destroy the pages (yet).
9539 mutex_exit(&rp
->r_statelock
);
9540 } else if (error
== EACCES
&& last_time
== FALSE
) {
9544 if (!(rp
->r_flags
& R4STALE
)) {
9547 * Releases the osp, if it is provided.
9548 * Puts a hold on the cred_otw and the new
9551 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
9552 &first_time
, &last_time
);
9553 mutex_enter(&rp
->r_statelock
);
9554 count
= MIN(bp
->b_bcount
, rp
->r_size
- offset
);
9555 mutex_exit(&rp
->r_statelock
);
9557 cmn_err(CE_PANIC
, "nfs4_bio: write count < 0");
9560 zoneid_t zoneid
= getzoneid();
9562 zcmn_err(zoneid
, CE_WARN
,
9563 "nfs4_bio: zero length write at %lld",
9565 zcmn_err(zoneid
, CE_CONT
, "flags=0x%x, "
9566 "b_bcount=%ld, file size=%lld",
9567 rp
->r_flags
, (long)bp
->b_bcount
,
9569 sfh4_printfhandle(VTOR4(bp
->b_vp
)->r_fh
);
9570 if (nfs4_bio_do_stop
)
9571 debug_enter("nfs4_bio");
9574 error
= nfs4write(bp
->b_vp
, bp
->b_un
.b_addr
, offset
,
9575 count
, cred_otw
, stab_comm
);
9576 if (error
== EACCES
&& last_time
== FALSE
) {
9580 bp
->b_error
= error
;
9581 if (error
&& error
!= EINTR
&&
9582 !(bp
->b_vp
->v_vfsp
->vfs_flag
& VFS_UNMOUNTED
)) {
9584 * Don't print EDQUOT errors on the console.
9585 * Don't print asynchronous EACCES errors.
9586 * Don't print EFBIG errors.
9587 * Print all other write errors.
9589 if (error
!= EDQUOT
&& error
!= EFBIG
&&
9591 !(bp
->b_flags
& B_ASYNC
)))
9592 nfs4_write_error(bp
->b_vp
,
9595 * Update r_error and r_flags as appropriate.
9596 * If the error was ESTALE, then mark the
9597 * rnode as not being writeable and save
9598 * the error status. Otherwise, save any
9599 * errors which occur from asynchronous
9600 * page invalidations. Any errors occurring
9601 * from other operations should be saved
9604 mutex_enter(&rp
->r_statelock
);
9605 if (error
== ESTALE
) {
9606 rp
->r_flags
|= R4STALE
;
9608 rp
->r_error
= error
;
9609 } else if (!rp
->r_error
&&
9611 (B_INVAL
|B_FORCE
|B_ASYNC
)) ==
9612 (B_INVAL
|B_FORCE
|B_ASYNC
)) {
9613 rp
->r_error
= error
;
9615 mutex_exit(&rp
->r_statelock
);
9619 error
= rp
->r_error
;
9621 * A close may have cleared r_error, if so,
9622 * propagate ESTALE error return properly
9629 if (error
!= 0 && error
!= NFS_EOF
)
9630 bp
->b_flags
|= B_ERROR
;
9633 open_stream_rele(osp
, rp
);
9635 DTRACE_IO1(done
, struct buf
*, bp
);
9642 nfs4_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
9649 nfs4_rwlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
9651 rnode4_t
*rp
= VTOR4(vp
);
9654 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
9655 return (V_WRITELOCK_FALSE
);
9658 if ((rp
->r_flags
& R4DIRECTIO
) ||
9659 (VTOMI4(vp
)->mi_flags
& MI4_DIRECTIO
)) {
9660 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
9661 if (rp
->r_mapcnt
== 0 && !nfs4_has_pages(vp
))
9662 return (V_WRITELOCK_FALSE
);
9663 nfs_rw_exit(&rp
->r_rwlock
);
9666 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, FALSE
);
9667 return (V_WRITELOCK_TRUE
);
9672 nfs4_rwunlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
9674 rnode4_t
*rp
= VTOR4(vp
);
9676 nfs_rw_exit(&rp
->r_rwlock
);
9681 nfs4_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, caller_context_t
*ct
)
9683 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9687 * Because we stuff the readdir cookie into the offset field
9688 * someone may attempt to do an lseek with the cookie which
9689 * we want to succeed.
9691 if (vp
->v_type
== VDIR
)
9700 * Return all the pages from [off..off+len) in file
9704 nfs4_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
9705 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
9706 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
9712 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9715 if (IS_SHADOW(vp
, rp
))
9718 if (vp
->v_flag
& VNOMAP
)
9725 * Now validate that the caches are up to date.
9727 if (error
= nfs4_validate_caches(vp
, cr
))
9732 mutex_enter(&rp
->r_statelock
);
9735 * Don't create dirty pages faster than they
9736 * can be cleaned so that the system doesn't
9737 * get imbalanced. If the async queue is
9738 * maxed out, then wait for it to drain before
9739 * creating more dirty pages. Also, wait for
9740 * any threads doing pagewalks in the vop_getattr
9741 * entry points so that they don't block for
9744 if (rw
== S_CREATE
) {
9745 while ((mi
->mi_max_threads
!= 0 &&
9746 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
9748 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
9752 * If we are getting called as a side effect of an nfs_write()
9753 * operation the local file size might not be extended yet.
9754 * In this case we want to be able to return pages of zeroes.
9756 if (off
+ len
> rp
->r_size
+ PAGEOFFSET
&& seg
!= segkmap
) {
9757 NFS4_DEBUG(nfs4_pageio_debug
,
9758 (CE_NOTE
, "getpage beyond EOF: off=%lld, "
9759 "len=%llu, size=%llu, attrsize =%llu", off
,
9760 (u_longlong_t
)len
, rp
->r_size
, rp
->r_attr
.va_size
));
9761 mutex_exit(&rp
->r_statelock
);
9762 return (EFAULT
); /* beyond EOF */
9765 mutex_exit(&rp
->r_statelock
);
9767 error
= pvn_getpages(nfs4_getapage
, vp
, off
, len
, protp
,
9768 pl
, plsz
, seg
, addr
, rw
, cr
);
9769 NFS4_DEBUG(nfs4_pageio_debug
&& error
,
9770 (CE_NOTE
, "getpages error %d; off=%lld, len=%lld",
9771 error
, off
, (u_longlong_t
)len
));
9775 nfs4_purge_caches(vp
, NFS4_NOPURGE_DNLC
, cr
, FALSE
);
9778 nfs4_purge_stale_fh(error
, vp
, cr
);
9785 * Called from pvn_getpages to get a particular page.
9789 nfs4_getapage(vnode_t
*vp
, uoff_t off
, size_t len
, uint_t
*protp
,
9790 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
9791 enum seg_rw rw
, cred_t
*cr
)
9805 int readahead_issued
= 0;
9806 int ra_window
; /* readahead window */
9810 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9814 ASSERT(!IS_SHADOW(vp
, rp
));
9815 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
9827 blkoff
= lbn
* bsize
;
9830 * Queueing up the readahead before doing the synchronous read
9831 * results in a significant increase in read throughput because
9832 * of the increased parallelism between the async threads and
9833 * the process context.
9835 if ((off
& ((vp
->v_vfsp
->vfs_bsize
) - 1)) == 0 &&
9837 !(vp
->v_flag
& VNOCACHE
)) {
9838 mutex_enter(&rp
->r_statelock
);
9841 * Calculate the number of readaheads to do.
9842 * a) No readaheads at offset = 0.
9843 * b) Do maximum(nfs4_nra) readaheads when the readahead
9845 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9846 * upon how far the readahead window is open or close.
9847 * d) No readaheads if rp->r_nextr is not within the scope
9848 * of the readahead window (random i/o).
9853 else if (blkoff
== rp
->r_nextr
)
9854 readahead
= nfs4_nra
;
9855 else if (rp
->r_nextr
> blkoff
&&
9856 ((ra_window
= (rp
->r_nextr
- blkoff
) / bsize
)
9858 readahead
= nfs4_nra
- ra_window
;
9862 rablkoff
= rp
->r_nextr
;
9863 while (readahead
> 0 && rablkoff
+ bsize
< rp
->r_size
) {
9864 mutex_exit(&rp
->r_statelock
);
9865 if (nfs4_async_readahead(vp
, rablkoff
+ bsize
,
9866 addr
+ (rablkoff
+ bsize
- off
),
9867 seg
, cr
, nfs4_readahead
) < 0) {
9868 mutex_enter(&rp
->r_statelock
);
9874 * Indicate that we did a readahead so
9875 * readahead offset is not updated
9876 * by the synchronous read below.
9878 readahead_issued
= 1;
9879 mutex_enter(&rp
->r_statelock
);
9881 * set readahead offset to
9882 * offset of last async readahead
9885 rp
->r_nextr
= rablkoff
;
9887 mutex_exit(&rp
->r_statelock
);
9891 if ((pagefound
= page_exists(&vp
->v_object
, off
)) == NULL
) {
9893 (void) nfs4_async_readahead(vp
, blkoff
, addr
, seg
, cr
,
9895 } else if (rw
== S_CREATE
) {
9897 * Block for this page is not allocated, or the offset
9898 * is beyond the current allocation size, or we're
9899 * allocating a swap slot and the page was not found,
9900 * so allocate it and return a zero page.
9902 if ((pp
= page_create_va(&vp
->v_object
, off
,
9903 PAGESIZE
, PG_WAIT
, seg
, addr
)) == NULL
)
9904 cmn_err(CE_PANIC
, "nfs4_getapage: page_create");
9906 mutex_enter(&rp
->r_statelock
);
9907 rp
->r_nextr
= off
+ PAGESIZE
;
9908 mutex_exit(&rp
->r_statelock
);
9911 * Need to go to server to get a block
9913 mutex_enter(&rp
->r_statelock
);
9914 if (blkoff
< rp
->r_size
&&
9915 blkoff
+ bsize
> rp
->r_size
) {
9917 * If less than a block left in
9918 * file read less than a block.
9920 if (rp
->r_size
<= off
) {
9922 * Trying to access beyond EOF,
9923 * set up to get at least one page.
9925 blksize
= off
+ PAGESIZE
- blkoff
;
9927 blksize
= rp
->r_size
- blkoff
;
9928 } else if ((off
== 0) ||
9929 (off
!= rp
->r_nextr
&& !readahead_issued
)) {
9931 blkoff
= off
; /* block = page here */
9934 mutex_exit(&rp
->r_statelock
);
9936 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
9937 &io_len
, blkoff
, blksize
, 0);
9940 * Some other thread has entered the page,
9947 * Now round the request size up to page boundaries.
9948 * This ensures that the entire page will be
9949 * initialized to zeroes if EOF is encountered.
9951 io_len
= ptob(btopr(io_len
));
9953 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
9957 * pageio_setup should have set b_addr to 0. This
9958 * is correct since we want to do I/O on a page
9959 * boundary. bp_mapin will use this addr to calculate
9960 * an offset, and then set b_addr to the kernel virtual
9961 * address it allocated for us.
9963 ASSERT(bp
->b_un
.b_addr
== 0);
9967 bp
->b_lblkno
= lbtodb(io_off
);
9969 bp
->b_offset
= (offset_t
)off
;
9973 * If doing a write beyond what we believe is EOF,
9974 * don't bother trying to read the pages from the
9975 * server, we'll just zero the pages here. We
9976 * don't check that the rw flag is S_WRITE here
9977 * because some implementations may attempt a
9978 * read access to the buffer before copying data.
9980 mutex_enter(&rp
->r_statelock
);
9981 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
9982 mutex_exit(&rp
->r_statelock
);
9983 bzero(bp
->b_un
.b_addr
, io_len
);
9985 mutex_exit(&rp
->r_statelock
);
9986 error
= nfs4_bio(bp
, NULL
, cr
, FALSE
);
9990 * Unmap the buffer before freeing it.
9997 pp
->p_fsdata
= C_NOCOMMIT
;
9998 } while ((pp
= pp
->p_next
) != savepp
);
10000 if (error
== NFS_EOF
) {
10002 * If doing a write system call just return
10003 * zeroed pages, else user tried to get pages
10004 * beyond EOF, return error. We don't check
10005 * that the rw flag is S_WRITE here because
10006 * some implementations may attempt a read
10007 * access to the buffer before copying data.
10009 if (seg
== segkmap
)
10015 if (!readahead_issued
&& !error
) {
10016 mutex_enter(&rp
->r_statelock
);
10017 rp
->r_nextr
= io_off
+ io_len
;
10018 mutex_exit(&rp
->r_statelock
);
10029 pvn_read_done(pp
, B_ERROR
);
10034 se_t se
= (rw
== S_CREATE
? SE_EXCL
: SE_SHARED
);
10037 * Page exists in the cache, acquire the appropriate lock.
10038 * If this fails, start all over again.
10040 if ((pp
= page_lookup(&vp
->v_object
, off
, se
)) == NULL
) {
10052 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
10058 nfs4_readahead(vnode_t
*vp
, uoff_t blkoff
, caddr_t addr
, struct seg
*seg
,
10066 uint_t bsize
, blksize
;
10067 rnode4_t
*rp
= VTOR4(vp
);
10070 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
10072 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
10074 mutex_enter(&rp
->r_statelock
);
10075 if (blkoff
< rp
->r_size
&& blkoff
+ bsize
> rp
->r_size
) {
10077 * If less than a block left in file read less
10080 blksize
= rp
->r_size
- blkoff
;
10083 mutex_exit(&rp
->r_statelock
);
10085 pp
= pvn_read_kluster(vp
, blkoff
, segkmap
, addr
,
10086 &io_off
, &io_len
, blkoff
, blksize
, 1);
10088 * The isra flag passed to the kluster function is 1, we may have
10089 * gotten a return value of NULL for a variety of reasons (# of free
10090 * pages < minfree, someone entered the page on the vnode etc). In all
10091 * cases, we want to punt on the readahead.
10097 * Now round the request size up to page boundaries.
10098 * This ensures that the entire page will be
10099 * initialized to zeroes if EOF is encountered.
10101 io_len
= ptob(btopr(io_len
));
10103 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
10104 ASSERT(bp
!= NULL
);
10107 * pageio_setup should have set b_addr to 0. This is correct since
10108 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10109 * to calculate an offset, and then set b_addr to the kernel virtual
10110 * address it allocated for us.
10112 ASSERT(bp
->b_un
.b_addr
== 0);
10116 bp
->b_lblkno
= lbtodb(io_off
);
10118 bp
->b_offset
= (offset_t
)blkoff
;
10122 * If doing a write beyond what we believe is EOF, don't bother trying
10123 * to read the pages from the server, we'll just zero the pages here.
10124 * We don't check that the rw flag is S_WRITE here because some
10125 * implementations may attempt a read access to the buffer before
10128 mutex_enter(&rp
->r_statelock
);
10129 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
10130 mutex_exit(&rp
->r_statelock
);
10131 bzero(bp
->b_un
.b_addr
, io_len
);
10134 mutex_exit(&rp
->r_statelock
);
10135 error
= nfs4_bio(bp
, NULL
, cr
, TRUE
);
10136 if (error
== NFS_EOF
)
10141 * Unmap the buffer before freeing it.
10148 pp
->p_fsdata
= C_NOCOMMIT
;
10149 } while ((pp
= pp
->p_next
) != savepp
);
10151 pvn_read_done(pp
, error
? B_READ
| B_ERROR
: B_READ
);
10154 * In case of error set readahead offset
10155 * to the lowest offset.
10156 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10158 if (error
&& rp
->r_nextr
> io_off
) {
10159 mutex_enter(&rp
->r_statelock
);
10160 if (rp
->r_nextr
> io_off
)
10161 rp
->r_nextr
= io_off
;
10162 mutex_exit(&rp
->r_statelock
);
10167 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10168 * If len == 0, do from off to EOF.
10170 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10171 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10176 nfs4_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
10177 caller_context_t
*ct
)
10182 ASSERT(cr
!= NULL
);
10184 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI4(vp
)->mi_zone
)
10188 if (IS_SHADOW(vp
, rp
))
10192 * XXX - Why should this check be made here?
10194 if (vp
->v_flag
& VNOMAP
)
10197 if (len
== 0 && !(flags
& B_INVAL
) &&
10198 (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
))
10201 mutex_enter(&rp
->r_statelock
);
10203 mutex_exit(&rp
->r_statelock
);
10204 error
= nfs4_putpages(vp
, off
, len
, flags
, cr
);
10205 mutex_enter(&rp
->r_statelock
);
10207 cv_broadcast(&rp
->r_cv
);
10208 mutex_exit(&rp
->r_statelock
);
10214 * Write out a single page, possibly klustering adjacent dirty pages.
10217 nfs4_putapage(vnode_t
*vp
, page_t
*pp
, uoff_t
*offp
, size_t *lenp
,
10218 int flags
, cred_t
*cr
)
10228 ASSERT(!(vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
));
10229 ASSERT(pp
!= NULL
);
10230 ASSERT(cr
!= NULL
);
10231 ASSERT((flags
& B_ASYNC
) || nfs_zone() == VTOMI4(vp
)->mi_zone
);
10234 ASSERT(rp
->r_count
> 0);
10235 ASSERT(!IS_SHADOW(vp
, rp
));
10237 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
10238 lbn
= pp
->p_offset
/ bsize
;
10239 lbn_off
= lbn
* bsize
;
10242 * Find a kluster that fits in one block, or in
10243 * one page if pages are bigger than blocks. If
10244 * there is less file space allocated than a whole
10245 * page, we'll shorten the i/o request below.
10247 pp
= pvn_write_kluster(vp
, pp
, &io_off
, &io_len
, lbn_off
,
10248 roundup(bsize
, PAGESIZE
), flags
);
10251 * pvn_write_kluster shouldn't have returned a page with offset
10252 * behind the original page we were given. Verify that.
10254 ASSERT((pp
->p_offset
/ bsize
) >= lbn
);
10257 * Now pp will have the list of kept dirty pages marked for
10258 * write back. It will also handle invalidation and freeing
10259 * of pages that are not dirty. Check for page length rounding
10262 if (io_off
+ io_len
> lbn_off
+ bsize
) {
10263 ASSERT((io_off
+ io_len
) - (lbn_off
+ bsize
) < PAGESIZE
);
10264 io_len
= lbn_off
+ bsize
- io_off
;
10267 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10268 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10269 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10270 * progress and the r_size has not been made consistent with the
10271 * new size of the file. When the uiomove() completes the r_size is
10272 * updated and the R4MODINPROGRESS flag is cleared.
10274 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10275 * consistent value of r_size. Without this handshaking, it is
10276 * possible that nfs4_bio() picks up the old value of r_size
10277 * before the uiomove() in writerp4() completes. This will result
10278 * in the write through nfs4_bio() being dropped.
10280 * More precisely, there is a window between the time the uiomove()
10281 * completes and the time the r_size is updated. If a fop_putpage()
10282 * operation intervenes in this window, the page will be picked up,
10283 * because it is dirty (it will be unlocked, unless it was
10284 * pagecreate'd). When the page is picked up as dirty, the dirty
10285 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10286 * checked. This will still be the old size. Therefore the page will
10287 * not be written out. When segmap_release() calls fop_putpage(),
10288 * the page will be found to be clean and the write will be dropped.
10290 if (rp
->r_flags
& R4MODINPROGRESS
) {
10291 mutex_enter(&rp
->r_statelock
);
10292 if ((rp
->r_flags
& R4MODINPROGRESS
) &&
10293 rp
->r_modaddr
+ MAXBSIZE
> io_off
&&
10294 rp
->r_modaddr
< io_off
+ io_len
) {
10297 * A write is in progress for this region of the file.
10298 * If we did not detect R4MODINPROGRESS here then this
10299 * path through nfs_putapage() would eventually go to
10300 * nfs4_bio() and may not write out all of the data
10301 * in the pages. We end up losing data. So we decide
10302 * to set the modified bit on each page in the page
10303 * list and mark the rnode with R4DIRTY. This write
10304 * will be restarted at some later time.
10307 while (plist
!= NULL
) {
10309 page_sub(&plist
, pp
);
10311 page_io_unlock(pp
);
10314 rp
->r_flags
|= R4DIRTY
;
10315 mutex_exit(&rp
->r_statelock
);
10322 mutex_exit(&rp
->r_statelock
);
10325 if (flags
& B_ASYNC
) {
10326 error
= nfs4_async_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
,
10327 nfs4_sync_putapage
);
10329 error
= nfs4_sync_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
);
10339 nfs4_sync_putapage(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
10340 int flags
, cred_t
*cr
)
10345 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
10349 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
10353 if ((error
== ENOSPC
|| error
== EDQUOT
|| error
== EFBIG
||
10354 error
== EACCES
) &&
10355 (flags
& (B_INVAL
|B_FORCE
)) != (B_INVAL
|B_FORCE
)) {
10356 if (!(rp
->r_flags
& R4OUTOFSPACE
)) {
10357 mutex_enter(&rp
->r_statelock
);
10358 rp
->r_flags
|= R4OUTOFSPACE
;
10359 mutex_exit(&rp
->r_statelock
);
10362 pvn_write_done(pp
, flags
);
10364 * If this was not an async thread, then try again to
10365 * write out the pages, but this time, also destroy
10366 * them whether or not the write is successful. This
10367 * will prevent memory from filling up with these
10368 * pages and destroying them is the only alternative
10369 * if they can't be written out.
10371 * Don't do this if this is an async thread because
10372 * when the pages are unlocked in pvn_write_done,
10373 * some other thread could have come along, locked
10374 * them, and queued for an async thread. It would be
10375 * possible for all of the async threads to be tied
10376 * up waiting to lock the pages again and they would
10377 * all already be locked and waiting for an async
10378 * thread to handle them. Deadlock.
10380 if (!(flags
& B_ASYNC
)) {
10381 error
= nfs4_putpage(vp
, io_off
, io_len
,
10382 B_INVAL
| B_FORCE
, cr
, NULL
);
10387 else if (rp
->r_flags
& R4OUTOFSPACE
) {
10388 mutex_enter(&rp
->r_statelock
);
10389 rp
->r_flags
&= ~R4OUTOFSPACE
;
10390 mutex_exit(&rp
->r_statelock
);
10392 pvn_write_done(pp
, flags
);
10393 if (freemem
< desfree
)
10394 (void) nfs4_commit_vp(vp
, 0, 0, cr
,
10395 NFS4_WRITE_NOWAIT
);
10402 int nfs4_force_open_before_mmap
= 0;
10407 nfs4_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
10408 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
10409 caller_context_t
*ct
)
10411 struct segvn_crargs vn_a
;
10413 rnode4_t
*rp
= VTOR4(vp
);
10414 mntinfo4_t
*mi
= VTOMI4(vp
);
10416 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10419 if (vp
->v_flag
& VNOMAP
)
10422 if (off
< 0 || (off
+ len
) < 0)
10425 if (vp
->v_type
!= VREG
)
10429 * If the file is delegated to the client don't do anything.
10430 * If the file is not delegated, then validate the data cache.
10432 mutex_enter(&rp
->r_statev4_lock
);
10433 if (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
) {
10434 mutex_exit(&rp
->r_statev4_lock
);
10435 error
= nfs4_validate_caches(vp
, cr
);
10439 mutex_exit(&rp
->r_statev4_lock
);
10443 * Check to see if the vnode is currently marked as not cachable.
10444 * This means portions of the file are locked (through fop_frlock).
10445 * In this case the map request must be refused. We use
10446 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10448 * Atomically increment r_inmap after acquiring r_rwlock. The
10449 * idea here is to acquire r_rwlock to block read/write and
10450 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10451 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10452 * and we can prevent the deadlock that would have occurred
10453 * when nfs4_addmap() would have acquired it out of order.
10455 * Since we are not protecting r_inmap by any lock, we do not
10456 * hold any lock when we decrement it. We atomically decrement
10457 * r_inmap after we release r_lkserlock.
10460 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, INTR4(vp
)))
10462 atomic_inc_uint(&rp
->r_inmap
);
10463 nfs_rw_exit(&rp
->r_rwlock
);
10465 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR4(vp
))) {
10466 atomic_dec_uint(&rp
->r_inmap
);
10470 if (vp
->v_flag
& VNOCACHE
) {
10476 * Don't allow concurrent locks and mapping if mandatory locking is
10479 if (flk_has_remote_locks(vp
)) {
10481 va
.va_mask
= VATTR_MODE
;
10482 error
= nfs4getattr(vp
, &va
, cr
);
10485 if (MANDLOCK(vp
, va
.va_mode
)) {
10492 * It is possible that the rnode has a lost lock request that we
10493 * are still trying to recover, and that the request conflicts with
10494 * this map request.
10496 * An alternative approach would be for nfs4_safemap() to consider
10497 * queued lock requests when deciding whether to set or clear
10498 * VNOCACHE. This would require the frlock code path to call
10499 * nfs4_safemap() after enqueing a lost request.
10501 if (nfs4_map_lost_lock_conflict(vp
)) {
10507 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
10509 as_rangeunlock(as
);
10513 if (vp
->v_type
== VREG
) {
10515 * We need to retrieve the open stream
10517 nfs4_open_stream_t
*osp
= NULL
;
10518 nfs4_open_owner_t
*oop
= NULL
;
10520 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
10522 /* returns with 'os_sync_lock' held */
10523 osp
= find_open_stream(oop
, rp
);
10524 open_owner_rele(oop
);
10528 if (nfs4_force_open_before_mmap
) {
10533 /* returns with 'os_sync_lock' held */
10534 error
= open_and_get_osp(vp
, cr
, &osp
);
10536 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
,
10537 "nfs4_map: we tried to OPEN the file "
10538 "but again no osp, so fail with EIO"));
10543 if (osp
->os_failed_reopen
) {
10544 mutex_exit(&osp
->os_sync_lock
);
10545 open_stream_rele(osp
, rp
);
10546 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
,
10547 "nfs4_map: os_failed_reopen set on "
10548 "osp %p, cr %p, rp %s", (void *)osp
,
10549 (void *)cr
, rnode4info(rp
)));
10553 mutex_exit(&osp
->os_sync_lock
);
10554 open_stream_rele(osp
, rp
);
10559 vn_a
.type
= (flags
& MAP_TYPE
);
10560 vn_a
.prot
= (uchar_t
)prot
;
10561 vn_a
.maxprot
= (uchar_t
)maxprot
;
10562 vn_a
.flags
= (flags
& ~MAP_TYPE
);
10566 vn_a
.lgrp_mem_policy_flags
= 0;
10568 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
10569 as_rangeunlock(as
);
10572 nfs_rw_exit(&rp
->r_lkserlock
);
10573 atomic_dec_uint(&rp
->r_inmap
);
10578 * We're most likely dealing with a kernel module that likes to READ
10579 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10580 * officially OPEN the file to create the necessary client state
10581 * for bookkeeping of os_mmap_read/write counts.
10583 * Since fop_map only passes in a pointer to the vnode rather than
10584 * a double pointer, we can't handle the case where nfs4open_otw()
10585 * returns a different vnode than the one passed into fop_map (since
10586 * fop_delmap will not see the vnode nfs4open_otw used). In this case,
10587 * we return NULL and let nfs4_map() fail. Note: the only case where
10588 * this should happen is if the file got removed and replaced with the
10589 * same name on the server (in addition to the fact that we're trying
10590 * to fop_map withouth fop_opening the file in the first place).
10593 open_and_get_osp(vnode_t
*map_vp
, cred_t
*cr
, nfs4_open_stream_t
**ospp
)
10595 rnode4_t
*rp
, *drp
;
10596 vnode_t
*dvp
, *open_vp
;
10597 char file_name
[MAXNAMELEN
];
10599 nfs4_open_stream_t
*osp
;
10600 nfs4_open_owner_t
*oop
;
10606 rp
= VTOR4(open_vp
);
10607 if ((error
= vtodv(open_vp
, &dvp
, cr
, TRUE
)) != 0)
10611 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
))) {
10616 if ((error
= vtoname(open_vp
, file_name
, MAXNAMELEN
)) != 0) {
10617 nfs_rw_exit(&drp
->r_rwlock
);
10622 mutex_enter(&rp
->r_statev4_lock
);
10623 if (rp
->created_v4
) {
10624 rp
->created_v4
= 0;
10625 mutex_exit(&rp
->r_statev4_lock
);
10627 dnlc_update(dvp
, file_name
, open_vp
);
10628 /* This is needed so we don't bump the open ref count */
10631 mutex_exit(&rp
->r_statev4_lock
);
10637 error
= nfs4open_otw(dvp
, file_name
, NULL
, &open_vp
, cr
, 0, FREAD
, 0,
10640 nfs_rw_exit(&drp
->r_rwlock
);
10646 nfs_rw_exit(&drp
->r_rwlock
);
10650 * If nfs4open_otw() returned a different vnode then "undo"
10651 * the open and return failure to the caller.
10653 if (!VN_CMP(open_vp
, map_vp
)) {
10656 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "open_and_get_osp: "
10657 "open returned a different vnode"));
10659 * If there's an error, ignore it,
10660 * and let fop_inactive handle it.
10662 (void) nfs4close_one(open_vp
, NULL
, cr
, FREAD
, NULL
, &e
,
10663 CLOSE_NORM
, 0, 0, 0);
10670 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, VTOMI4(open_vp
));
10674 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "open_and_get_osp: "
10677 * If there's an error, ignore it,
10678 * and let fop_inactive handle it.
10680 (void) nfs4close_one(open_vp
, NULL
, cr
, FREAD
, NULL
, &e
,
10681 CLOSE_NORM
, 0, 0, 0);
10684 osp
= find_open_stream(oop
, rp
);
10685 open_owner_rele(oop
);
10691 * Please be aware that when this function is called, the address space write
10692 * a_lock is held. Do not put over the wire calls in this function.
10696 nfs4_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
10697 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
10698 caller_context_t
*ct
)
10707 if (nfs_zone() != mi
->mi_zone
)
10709 if (vp
->v_flag
& VNOMAP
)
10713 * Don't need to update the open stream first, since this
10714 * mmap can't add any additional share access that isn't
10715 * already contained in the open stream (for the case where we
10716 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10717 * take into account os_mmap_read[write] counts).
10719 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, btopr(len
));
10721 if (vp
->v_type
== VREG
) {
10723 * We need to retrieve the open stream and update the counts.
10724 * If there is no open stream here, something is wrong.
10726 nfs4_open_stream_t
*osp
= NULL
;
10727 nfs4_open_owner_t
*oop
= NULL
;
10729 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
10731 /* returns with 'os_sync_lock' held */
10732 osp
= find_open_stream(oop
, rp
);
10733 open_owner_rele(oop
);
10736 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
,
10737 "nfs4_addmap: we should have an osp"
10738 "but we don't, so fail with EIO"));
10743 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "nfs4_addmap: osp %p,"
10744 " pages %ld, prot 0x%x", (void *)osp
, btopr(len
), prot
));
10747 * Update the map count in the open stream.
10748 * This is necessary in the case where we
10749 * open/mmap/close/, then the server reboots, and we
10750 * attempt to reopen. If the mmap doesn't add share
10751 * access then we send an invalid reopen with
10754 * We need to specifically check each PROT_* so a mmap
10755 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10756 * read and write access. A simple comparison of prot
10757 * to ~PROT_WRITE to determine read access is insufficient
10758 * since prot can be |= with PROT_USER, etc.
10762 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10764 if ((flags
& MAP_SHARED
) && (maxprot
& PROT_WRITE
))
10765 osp
->os_mmap_write
+= btopr(len
);
10766 if (maxprot
& PROT_READ
)
10767 osp
->os_mmap_read
+= btopr(len
);
10768 if (maxprot
& PROT_EXEC
)
10769 osp
->os_mmap_read
+= btopr(len
);
10771 * Ensure that os_mmap_read gets incremented, even if
10772 * maxprot were to look like PROT_NONE.
10774 if (!(maxprot
& PROT_READ
) && !(maxprot
& PROT_WRITE
) &&
10775 !(maxprot
& PROT_EXEC
))
10776 osp
->os_mmap_read
+= btopr(len
);
10777 osp
->os_mapcnt
+= btopr(len
);
10778 mutex_exit(&osp
->os_sync_lock
);
10779 open_stream_rele(osp
, rp
);
10784 * If we got an error, then undo our
10785 * incrementing of 'r_mapcnt'.
10789 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, -btopr(len
));
10790 ASSERT(rp
->r_mapcnt
>= 0);
10797 nfs4_cmp(vnode_t
*vp1
, vnode_t
*vp2
, caller_context_t
*ct
)
10800 return (VTOR4(vp1
) == VTOR4(vp2
));
10805 nfs4_frlock(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
10806 offset_t offset
, struct flk_callback
*flk_cbp
, cred_t
*cr
,
10807 caller_context_t
*ct
)
10812 int error
= 0, intr
= INTR4(vp
);
10815 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10818 /* check for valid cmd parameter */
10819 if (cmd
!= F_GETLK
&& cmd
!= F_SETLK
&& cmd
!= F_SETLKW
)
10822 /* Verify l_type. */
10823 switch (bfp
->l_type
) {
10825 if (cmd
!= F_GETLK
&& !(flag
& FREAD
))
10829 if (cmd
!= F_GETLK
&& !(flag
& FWRITE
))
10840 /* check the validity of the lock range */
10841 if (rc
= flk_convert_lock_data(vp
, bfp
, &start
, &end
, offset
))
10843 if (rc
= flk_check_lock_data(start
, end
, MAXEND
))
10847 * If the filesystem is mounted using local locking, pass the
10848 * request off to the local locking code.
10850 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
|| vp
->v_type
!= VREG
) {
10851 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
10853 * For complete safety, we should be holding
10854 * r_lkserlock. However, we can't call
10855 * nfs4_safelock and then fs_frlock while
10856 * holding r_lkserlock, so just invoke
10857 * nfs4_safelock and expect that this will
10858 * catch enough of the cases.
10860 if (!nfs4_safelock(vp
, bfp
, cr
))
10863 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
10869 * Check whether the given lock request can proceed, given the
10870 * current file mappings.
10872 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_WRITER
, intr
))
10874 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
10875 if (!nfs4_safelock(vp
, bfp
, cr
)) {
10882 * Flush the cache after waiting for async I/O to finish. For new
10883 * locks, this is so that the process gets the latest bits from the
10884 * server. For unlocks, this is so that other clients see the
10885 * latest bits once the file has been unlocked. If currently dirty
10886 * pages can't be flushed, then don't allow a lock to be set. But
10887 * allow unlocks to succeed, to avoid having orphan locks on the
10890 if (cmd
!= F_GETLK
) {
10891 mutex_enter(&rp
->r_statelock
);
10892 while (rp
->r_count
> 0) {
10894 klwp_t
*lwp
= ttolwp(curthread
);
10898 if (cv_wait_sig(&rp
->r_cv
,
10899 &rp
->r_statelock
) == 0) {
10908 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
10911 mutex_exit(&rp
->r_statelock
);
10914 error
= nfs4_putpage(vp
, 0, 0, B_INVAL
, cr
, ct
);
10916 if (error
== ENOSPC
|| error
== EDQUOT
) {
10917 mutex_enter(&rp
->r_statelock
);
10919 rp
->r_error
= error
;
10920 mutex_exit(&rp
->r_statelock
);
10922 if (bfp
->l_type
!= F_UNLCK
) {
10930 * Call the lock manager to do the real work of contacting
10931 * the server and obtaining the lock.
10933 nfs4frlock(NFS4_LCK_CTYPE_NORM
, vp
, cmd
, bfp
, flag
, offset
,
10934 cr
, &e
, NULL
, NULL
);
10938 nfs4_lockcompletion(vp
, cmd
);
10941 nfs_rw_exit(&rp
->r_lkserlock
);
10947 * Free storage space associated with the specified vnode. The portion
10948 * to be freed is specified by bfp->l_start and bfp->l_len (already
10949 * normalized to a "whence" of 0).
10951 * This is an experimental facility whose continued existence is not
10952 * guaranteed. Currently, we only support the special case
10953 * of l_len == 0, meaning free to end of file.
10957 nfs4_space(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
10958 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
10962 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10964 ASSERT(vp
->v_type
== VREG
);
10965 if (cmd
!= F_FREESP
)
10968 error
= convoff(vp
, bfp
, 0, offset
);
10970 ASSERT(bfp
->l_start
>= 0);
10971 if (bfp
->l_len
== 0) {
10974 va
.va_mask
= VATTR_SIZE
;
10975 va
.va_size
= bfp
->l_start
;
10976 error
= nfs4setattr(vp
, &va
, 0, cr
, NULL
);
10978 if (error
== 0 && bfp
->l_start
== 0)
10979 vnevent_truncate(vp
, ct
);
10989 nfs4_realvp(vnode_t
*vp
, vnode_t
**vpp
, caller_context_t
*ct
)
10994 if (vp
->v_type
== VREG
&& IS_SHADOW(vp
, rp
)) {
11002 * Setup and add an address space callback to do the work of the delmap call.
11003 * The callback will (and must be) deleted in the actual callback function.
11005 * This is done in order to take care of the problem that we have with holding
11006 * the address space's a_lock for a long period of time (e.g. if the NFS server
11007 * is down). Callbacks will be executed in the address space code while the
11008 * a_lock is not held. Holding the address space's a_lock causes things such
11009 * as ps and fork to hang because they are trying to acquire this lock as well.
11013 nfs4_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
11014 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
11015 caller_context_t
*ct
)
11020 nfs4_delmap_args_t
*dmapp
;
11021 nfs4_delmapcall_t
*delmap_call
;
11023 if (vp
->v_flag
& VNOMAP
)
11027 * A process may not change zones if it has NFS pages mmap'ed
11028 * in, so we can't legitimately get here from the wrong zone.
11030 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11035 * The way that the address space of this process deletes its mapping
11036 * of this file is via the following call chains:
11037 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11038 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11040 * With the use of address space callbacks we are allowed to drop the
11041 * address space lock, a_lock, while executing the NFS operations that
11042 * need to go over the wire. Returning EAGAIN to the caller of this
11043 * function is what drives the execution of the callback that we add
11044 * below. The callback will be executed by the address space code
11045 * after dropping the a_lock. When the callback is finished, since
11046 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11047 * is called again on the same segment to finish the rest of the work
11048 * that needs to happen during unmapping.
11050 * This action of calling back into the segment driver causes
11051 * nfs4_delmap() to get called again, but since the callback was
11052 * already executed at this point, it already did the work and there
11053 * is nothing left for us to do.
11056 * - The first time nfs4_delmap is called by the current thread is when
11057 * we add the caller associated with this delmap to the delmap caller
11058 * list, add the callback, and return EAGAIN.
11059 * - The second time in this call chain when nfs4_delmap is called we
11060 * will find this caller in the delmap caller list and realize there
11061 * is no more work to do thus removing this caller from the list and
11062 * returning the error that was set in the callback execution.
11064 caller_found
= nfs4_find_and_delete_delmapcall(rp
, &error
);
11065 if (caller_found
) {
11067 * 'error' is from the actual delmap operations. To avoid
11068 * hangs, we need to handle the return of EAGAIN differently
11069 * since this is what drives the callback execution.
11070 * In this case, we don't want to return EAGAIN and do the
11071 * callback execution because there are none to execute.
11073 if (error
== EAGAIN
)
11079 /* current caller was not in the list */
11080 delmap_call
= nfs4_init_delmapcall();
11082 mutex_enter(&rp
->r_statelock
);
11083 list_insert_tail(&rp
->r_indelmap
, delmap_call
);
11084 mutex_exit(&rp
->r_statelock
);
11086 dmapp
= kmem_alloc(sizeof (nfs4_delmap_args_t
), KM_SLEEP
);
11090 dmapp
->addr
= addr
;
11092 dmapp
->prot
= prot
;
11093 dmapp
->maxprot
= maxprot
;
11094 dmapp
->flags
= flags
;
11096 dmapp
->caller
= delmap_call
;
11098 error
= as_add_callback(as
, nfs4_delmap_callback
, dmapp
,
11099 AS_UNMAP_EVENT
, addr
, len
, KM_SLEEP
);
11101 return (error
? error
: EAGAIN
);
11104 static nfs4_delmapcall_t
*
11105 nfs4_init_delmapcall()
11107 nfs4_delmapcall_t
*delmap_call
;
11109 delmap_call
= kmem_alloc(sizeof (nfs4_delmapcall_t
), KM_SLEEP
);
11110 delmap_call
->call_id
= curthread
;
11111 delmap_call
->error
= 0;
11113 return (delmap_call
);
11117 nfs4_free_delmapcall(nfs4_delmapcall_t
*delmap_call
)
11119 kmem_free(delmap_call
, sizeof (nfs4_delmapcall_t
));
11123 * Searches for the current delmap caller (based on curthread) in the list of
11124 * callers. If it is found, we remove it and free the delmap caller.
11126 * 0 if the caller wasn't found
11127 * 1 if the caller was found, removed and freed. *errp will be set
11128 * to what the result of the delmap was.
11131 nfs4_find_and_delete_delmapcall(rnode4_t
*rp
, int *errp
)
11133 nfs4_delmapcall_t
*delmap_call
;
11136 * If the list doesn't exist yet, we create it and return
11137 * that the caller wasn't found. No list = no callers.
11139 mutex_enter(&rp
->r_statelock
);
11140 if (!(rp
->r_flags
& R4DELMAPLIST
)) {
11141 /* The list does not exist */
11142 list_create(&rp
->r_indelmap
, sizeof (nfs4_delmapcall_t
),
11143 offsetof(nfs4_delmapcall_t
, call_node
));
11144 rp
->r_flags
|= R4DELMAPLIST
;
11145 mutex_exit(&rp
->r_statelock
);
11148 /* The list exists so search it */
11149 for (delmap_call
= list_head(&rp
->r_indelmap
);
11150 delmap_call
!= NULL
;
11151 delmap_call
= list_next(&rp
->r_indelmap
, delmap_call
)) {
11152 if (delmap_call
->call_id
== curthread
) {
11153 /* current caller is in the list */
11154 *errp
= delmap_call
->error
;
11155 list_remove(&rp
->r_indelmap
, delmap_call
);
11156 mutex_exit(&rp
->r_statelock
);
11157 nfs4_free_delmapcall(delmap_call
);
11162 mutex_exit(&rp
->r_statelock
);
11167 * Remove some pages from an mmap'd vnode. Just update the
11168 * count of pages. If doing close-to-open, then flush and
11169 * commit all of the pages associated with this file.
11170 * Otherwise, start an asynchronous page flush to write out
11171 * any dirty pages. This will also associate a credential
11172 * with the rnode which can be used to write the pages.
11176 nfs4_delmap_callback(struct as
*as
, void *arg
, uint_t event
)
11178 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
11181 nfs4_delmap_args_t
*dmapp
= (nfs4_delmap_args_t
*)arg
;
11183 rp
= VTOR4(dmapp
->vp
);
11184 mi
= VTOMI4(dmapp
->vp
);
11186 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, -btopr(dmapp
->len
));
11187 ASSERT(rp
->r_mapcnt
>= 0);
11190 * Initiate a page flush and potential commit if there are
11191 * pages, the file system was not mounted readonly, the segment
11192 * was mapped shared, and the pages themselves were writeable.
11194 if (nfs4_has_pages(dmapp
->vp
) &&
11195 !(dmapp
->vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
) &&
11196 dmapp
->flags
== MAP_SHARED
&& (dmapp
->maxprot
& PROT_WRITE
)) {
11197 mutex_enter(&rp
->r_statelock
);
11198 rp
->r_flags
|= R4DIRTY
;
11199 mutex_exit(&rp
->r_statelock
);
11200 e
.error
= nfs4_putpage_commit(dmapp
->vp
, dmapp
->off
,
11201 dmapp
->len
, dmapp
->cr
);
11203 mutex_enter(&rp
->r_statelock
);
11204 e
.error
= rp
->r_error
;
11206 mutex_exit(&rp
->r_statelock
);
11211 if ((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
))
11212 (void) nfs4_putpage(dmapp
->vp
, dmapp
->off
, dmapp
->len
,
11213 B_INVAL
, dmapp
->cr
, NULL
);
11216 e
.stat
= puterrno4(e
.error
);
11217 nfs4_queue_fact(RF_DELMAP_CB_ERR
, mi
, e
.stat
, 0,
11218 OP_COMMIT
, FALSE
, NULL
, 0, dmapp
->vp
);
11219 dmapp
->caller
->error
= e
.error
;
11222 /* Check to see if we need to close the file */
11224 if (dmapp
->vp
->v_type
== VREG
) {
11225 nfs4close_one(dmapp
->vp
, NULL
, dmapp
->cr
, 0, NULL
, &e
,
11226 CLOSE_DELMAP
, dmapp
->len
, dmapp
->maxprot
, dmapp
->flags
);
11228 if (e
.error
!= 0 || e
.stat
!= NFS4_OK
) {
11230 * Since it is possible that e.error == 0 and
11231 * e.stat != NFS4_OK (and vice versa),
11232 * we do the proper checking in order to get both
11233 * e.error and e.stat reporting the correct info.
11235 if (e
.stat
== NFS4_OK
)
11236 e
.stat
= puterrno4(e
.error
);
11238 e
.error
= geterrno4(e
.stat
);
11240 nfs4_queue_fact(RF_DELMAP_CB_ERR
, mi
, e
.stat
, 0,
11241 OP_CLOSE
, FALSE
, NULL
, 0, dmapp
->vp
);
11242 dmapp
->caller
->error
= e
.error
;
11246 (void) as_delete_callback(as
, arg
);
11247 kmem_free(dmapp
, sizeof (nfs4_delmap_args_t
));
11252 fattr4_maxfilesize_to_bits(uint64_t ll
)
11260 if (ll
& 0xffffffff00000000) {
11261 l
+= 32; ll
>>= 32;
11263 if (ll
& 0xffff0000) {
11264 l
+= 16; ll
>>= 16;
11282 nfs4_have_xattrs(vnode_t
*vp
, ulong_t
*valp
, cred_t
*cr
)
11284 vnode_t
*avp
= NULL
;
11287 if ((error
= nfs4lookup_xattr(vp
, "", &avp
,
11288 LOOKUP_XATTR
, cr
)) == 0)
11289 error
= do_xattr_exists_check(avp
, valp
, cr
);
11298 nfs4_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
11299 caller_context_t
*ct
)
11305 nfs4_ga_ext_res_t ger
;
11307 gar
.n4g_ext_res
= &ger
;
11309 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
11311 if (cmd
== _PC_PATH_MAX
|| cmd
== _PC_SYMLINK_MAX
) {
11312 *valp
= MAXPATHLEN
;
11315 if (cmd
== _PC_ACL_ENABLED
) {
11316 *valp
= _ACL_ACE_ENABLED
;
11321 if (cmd
== _PC_XATTR_EXISTS
) {
11323 * The existence of the xattr directory is not sufficient
11324 * for determining whether generic user attributes exists.
11325 * The attribute directory could only be a transient directory
11326 * used for Solaris sysattr support. Do a small readdir
11327 * to verify if the only entries are sysattrs or not.
11329 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11330 * is NULL. Once the xadir vp exists, we can create xattrs,
11331 * and we don't have any way to update the "base" object's
11332 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11335 if (ATTRCACHE4_VALID(vp
) && rp
->r_pathconf
.pc4_xattr_valid
&&
11336 rp
->r_xattr_dir
== NULL
) {
11337 return (nfs4_have_xattrs(vp
, valp
, cr
));
11339 } else { /* OLD CODE */
11340 if (ATTRCACHE4_VALID(vp
)) {
11341 mutex_enter(&rp
->r_statelock
);
11342 if (rp
->r_pathconf
.pc4_cache_valid
) {
11345 case _PC_FILESIZEBITS
:
11347 rp
->r_pathconf
.pc4_filesizebits
;
11351 rp
->r_pathconf
.pc4_link_max
;
11355 rp
->r_pathconf
.pc4_name_max
;
11357 case _PC_CHOWN_RESTRICTED
:
11359 rp
->r_pathconf
.pc4_chown_restricted
;
11363 rp
->r_pathconf
.pc4_no_trunc
;
11369 mutex_exit(&rp
->r_statelock
);
11371 nfs4_pathconf_cache_hits
++;
11375 mutex_exit(&rp
->r_statelock
);
11379 nfs4_pathconf_cache_misses
++;
11384 error
= nfs4_attr_otw(vp
, TAG_PATHCONF
, &gar
, NFS4_PATHCONF_MASK
, cr
);
11387 mutex_enter(&rp
->r_statelock
);
11388 rp
->r_pathconf
.pc4_cache_valid
= FALSE
;
11389 rp
->r_pathconf
.pc4_xattr_valid
= FALSE
;
11390 mutex_exit(&rp
->r_statelock
);
11394 /* interpret the max filesize */
11395 gar
.n4g_ext_res
->n4g_pc4
.pc4_filesizebits
=
11396 fattr4_maxfilesize_to_bits(gar
.n4g_ext_res
->n4g_maxfilesize
);
11398 /* Store the attributes we just received */
11399 nfs4_attr_cache(vp
, &gar
, t
, cr
, TRUE
, NULL
);
11402 case _PC_FILESIZEBITS
:
11403 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_filesizebits
;
11406 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_link_max
;
11409 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_name_max
;
11411 case _PC_CHOWN_RESTRICTED
:
11412 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_chown_restricted
;
11415 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_no_trunc
;
11417 case _PC_XATTR_EXISTS
:
11418 if (gar
.n4g_ext_res
->n4g_pc4
.pc4_xattr_exists
) {
11419 if (error
= nfs4_have_xattrs(vp
, valp
, cr
))
11431 * Called by async thread to do synchronous pageio. Do the i/o, wait
11432 * for it to complete, and cleanup the page list when done.
11435 nfs4_sync_pageio(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
11436 int flags
, cred_t
*cr
)
11440 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11442 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
11443 if (flags
& B_READ
)
11444 pvn_read_done(pp
, (error
? B_ERROR
: 0) | flags
);
11446 pvn_write_done(pp
, (error
? B_ERROR
: 0) | flags
);
11452 nfs4_pageio(vnode_t
*vp
, page_t
*pp
, uoff_t io_off
, size_t io_len
,
11453 int flags
, cred_t
*cr
, caller_context_t
*ct
)
11458 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI4(vp
)->mi_zone
)
11465 mutex_enter(&rp
->r_statelock
);
11467 mutex_exit(&rp
->r_statelock
);
11469 if (flags
& B_ASYNC
) {
11470 error
= nfs4_async_pageio(vp
, pp
, io_off
, io_len
, flags
, cr
,
11473 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
11474 mutex_enter(&rp
->r_statelock
);
11476 cv_broadcast(&rp
->r_cv
);
11477 mutex_exit(&rp
->r_statelock
);
11483 nfs4_dispose(vnode_t
*vp
, page_t
*pp
, int fl
, int dn
, cred_t
*cr
,
11484 caller_context_t
*ct
)
11495 * We should get called with fl equal to either B_FREE or
11496 * B_INVAL. Any other value is illegal.
11498 * The page that we are either supposed to free or destroy
11499 * should be exclusive locked and its io lock should not
11502 ASSERT(fl
== B_FREE
|| fl
== B_INVAL
);
11503 ASSERT((PAGE_EXCL(pp
) && !page_iolock_assert(pp
)) || panicstr
);
11508 * If the page doesn't need to be committed or we shouldn't
11509 * even bother attempting to commit it, then just make sure
11510 * that the p_fsdata byte is clear and then either free or
11511 * destroy the page as appropriate.
11513 if (pp
->p_fsdata
== C_NOCOMMIT
|| (rp
->r_flags
& R4STALE
)) {
11514 pp
->p_fsdata
= C_NOCOMMIT
;
11518 page_destroy(pp
, dn
);
11523 * If there is a page invalidation operation going on, then
11524 * if this is one of the pages being destroyed, then just
11525 * clear the p_fsdata byte and then either free or destroy
11526 * the page as appropriate.
11528 mutex_enter(&rp
->r_statelock
);
11529 if ((rp
->r_flags
& R4TRUNCATE
) && pp
->p_offset
>= rp
->r_truncaddr
) {
11530 mutex_exit(&rp
->r_statelock
);
11531 pp
->p_fsdata
= C_NOCOMMIT
;
11535 page_destroy(pp
, dn
);
11540 * If we are freeing this page and someone else is already
11541 * waiting to do a commit, then just unlock the page and
11542 * return. That other thread will take care of commiting
11543 * this page. The page can be freed sometime after the
11544 * commit has finished. Otherwise, if the page is marked
11545 * as delay commit, then we may be getting called from
11546 * pvn_write_done, one page at a time. This could result
11547 * in one commit per page, so we end up doing lots of small
11548 * commits instead of fewer larger commits. This is bad,
11549 * we want do as few commits as possible.
11551 if (fl
== B_FREE
) {
11552 if (rp
->r_flags
& R4COMMITWAIT
) {
11554 mutex_exit(&rp
->r_statelock
);
11557 if (pp
->p_fsdata
== C_DELAYCOMMIT
) {
11558 pp
->p_fsdata
= C_COMMIT
;
11560 mutex_exit(&rp
->r_statelock
);
11566 * Check to see if there is a signal which would prevent an
11567 * attempt to commit the pages from being successful. If so,
11568 * then don't bother with all of the work to gather pages and
11569 * generate the unsuccessful RPC. Just return from here and
11570 * let the page be committed at some later time.
11572 sigintr(&smask
, VTOMI4(vp
)->mi_flags
& MI4_INT
);
11573 if (ttolwp(curthread
) != NULL
&& ISSIG(curthread
, JUSTLOOKING
)) {
11576 mutex_exit(&rp
->r_statelock
);
11582 * We are starting to need to commit pages, so let's try
11583 * to commit as many as possible at once to reduce the
11586 * Set the `commit inprogress' state bit. We must
11587 * first wait until any current one finishes. Then
11588 * we initialize the c_pages list with this page.
11590 while (rp
->r_flags
& R4COMMIT
) {
11591 rp
->r_flags
|= R4COMMITWAIT
;
11592 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
11593 rp
->r_flags
&= ~R4COMMITWAIT
;
11595 rp
->r_flags
|= R4COMMIT
;
11596 mutex_exit(&rp
->r_statelock
);
11597 ASSERT(rp
->r_commit
.c_pages
== NULL
);
11598 rp
->r_commit
.c_pages
= pp
;
11599 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11600 rp
->r_commit
.c_commlen
= PAGESIZE
;
11603 * Gather together all other pages which can be committed.
11604 * They will all be chained off r_commit.c_pages.
11606 nfs4_get_commit(vp
);
11609 * Clear the `commit inprogress' status and disconnect
11610 * the list of pages to be committed from the rnode.
11611 * At this same time, we also save the starting offset
11612 * and length of data to be committed on the server.
11614 plist
= rp
->r_commit
.c_pages
;
11615 rp
->r_commit
.c_pages
= NULL
;
11616 offset
= rp
->r_commit
.c_commbase
;
11617 len
= rp
->r_commit
.c_commlen
;
11618 mutex_enter(&rp
->r_statelock
);
11619 rp
->r_flags
&= ~R4COMMIT
;
11620 cv_broadcast(&rp
->r_commit
.c_cv
);
11621 mutex_exit(&rp
->r_statelock
);
11623 if (curproc
== proc_pageout
|| curproc
== proc_fsflush
||
11624 nfs_zone() != VTOMI4(vp
)->mi_zone
) {
11625 nfs4_async_commit(vp
, plist
, offset
, len
,
11626 cr
, do_nfs4_async_commit
);
11631 * Actually generate the COMMIT op over the wire operation.
11633 error
= nfs4_commit(vp
, (offset4
)offset
, (count4
)len
, cr
);
11636 * If we got an error during the commit, just unlock all
11637 * of the pages. The pages will get retransmitted to the
11638 * server during a putpage operation.
11641 while (plist
!= NULL
) {
11643 page_sub(&plist
, pptr
);
11650 * We've tried as hard as we can to commit the data to stable
11651 * storage on the server. We just unlock the rest of the pages
11652 * and clear the commit required state. They will be put
11653 * onto the tail of the cachelist if they are nolonger
11656 while (plist
!= pp
) {
11658 page_sub(&plist
, pptr
);
11659 pptr
->p_fsdata
= C_NOCOMMIT
;
11664 * It is possible that nfs4_commit didn't return error but
11665 * some other thread has modified the page we are going
11667 * In this case we need to rewrite the page. Do an explicit check
11668 * before attempting to free/destroy the page. If modified, needs to
11669 * be rewritten so unlock the page and return.
11671 if (hat_ismod(pp
)) {
11672 pp
->p_fsdata
= C_NOCOMMIT
;
11678 * Now, as appropriate, either free or destroy the page
11679 * that we were called with.
11681 pp
->p_fsdata
= C_NOCOMMIT
;
11685 page_destroy(pp
, dn
);
11689 * Commit requires that the current fh be the file written to.
11690 * The compound op structure is:
11691 * PUTFH(file), COMMIT
11694 nfs4_commit(vnode_t
*vp
, offset4 offset
, count4 count
, cred_t
*cr
)
11696 COMPOUND4args_clnt args
;
11697 COMPOUND4res_clnt res
;
11698 COMMIT4res
*cm_res
;
11699 nfs_argop4 argop
[2];
11704 cred_t
*cred_otw
= NULL
;
11705 bool_t needrecov
= FALSE
;
11706 nfs4_recov_state_t recov_state
;
11707 nfs4_open_stream_t
*osp
= NULL
;
11708 bool_t first_time
= TRUE
; /* first time getting OTW cred */
11709 bool_t last_time
= FALSE
; /* last time getting OTW cred */
11710 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
11712 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11717 recov_state
.rs_flags
= 0;
11718 recov_state
.rs_num_retry_despite_err
= 0;
11721 * Releases the osp, if a valid open stream is provided.
11722 * Puts a hold on the cred_otw and the new osp (if found).
11724 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
11725 &first_time
, &last_time
);
11726 args
.ctag
= TAG_COMMIT
;
11729 * Commit ops: putfh file; commit
11731 args
.array_len
= 2;
11732 args
.array
= argop
;
11734 e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11735 &recov_state
, NULL
);
11739 open_stream_rele(osp
, rp
);
11743 /* putfh directory */
11744 argop
[0].argop
= OP_CPUTFH
;
11745 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
11748 argop
[1].argop
= OP_COMMIT
;
11749 argop
[1].nfs_argop4_u
.opcommit
.offset
= offset
;
11750 argop
[1].nfs_argop4_u
.opcommit
.count
= count
;
11753 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, &e
);
11755 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
11756 if (!needrecov
&& e
.error
) {
11757 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
, &recov_state
,
11760 if (e
.error
== EACCES
&& last_time
== FALSE
)
11761 goto get_commit_cred
;
11763 open_stream_rele(osp
, rp
);
11768 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
11769 NULL
, OP_COMMIT
, NULL
, NULL
, NULL
) == FALSE
) {
11770 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11771 &recov_state
, needrecov
);
11773 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11777 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11778 &recov_state
, needrecov
);
11781 open_stream_rele(osp
, rp
);
11784 /* fall through for res.status case */
11788 e
.error
= geterrno4(res
.status
);
11789 if (e
.error
== EACCES
&& last_time
== FALSE
) {
11791 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11792 &recov_state
, needrecov
);
11793 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11794 goto get_commit_cred
;
11797 * Can't do a nfs4_purge_stale_fh here because this
11798 * can cause a deadlock. nfs4_commit can
11799 * be called from nfs4_dispose which can be called
11800 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11801 * can call back to pvn_vplist_dirty.
11803 if (e
.error
== ESTALE
) {
11804 mutex_enter(&rp
->r_statelock
);
11805 rp
->r_flags
|= R4STALE
;
11807 rp
->r_error
= e
.error
;
11808 mutex_exit(&rp
->r_statelock
);
11809 PURGE_ATTRCACHE4(vp
);
11811 mutex_enter(&rp
->r_statelock
);
11813 rp
->r_error
= e
.error
;
11814 mutex_exit(&rp
->r_statelock
);
11817 ASSERT(rp
->r_flags
& R4HAVEVERF
);
11818 resop
= &res
.array
[1]; /* commit res */
11819 cm_res
= &resop
->nfs_resop4_u
.opcommit
;
11820 mutex_enter(&rp
->r_statelock
);
11821 if (cm_res
->writeverf
== rp
->r_writeverf
) {
11822 mutex_exit(&rp
->r_statelock
);
11823 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11824 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11825 &recov_state
, needrecov
);
11828 open_stream_rele(osp
, rp
);
11832 rp
->r_writeverf
= cm_res
->writeverf
;
11833 mutex_exit(&rp
->r_statelock
);
11834 e
.error
= NFS_VERF_MISMATCH
;
11837 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11838 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
, &recov_state
, needrecov
);
11841 open_stream_rele(osp
, rp
);
11847 nfs4_set_mod(vnode_t
*vp
)
11849 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11851 /* make sure we're looking at the master vnode, not a shadow */
11852 pvn_vplist_setdirty(RTOV4(VTOR4(vp
)), nfs_setmod_check
);
11856 * This function is used to gather a page list of the pages which
11857 * can be committed on the server.
11859 * The calling thread must have set R4COMMIT. This bit is used to
11860 * serialize access to the commit structure in the rnode. As long
11861 * as the thread has set R4COMMIT, then it can manipulate the commit
11862 * structure without requiring any other locks.
11864 * When this function is called from nfs4_dispose() the page passed
11865 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11866 * will skip it. This is not a problem since we initially add the
11867 * page to the r_commit page list.
11871 nfs4_get_commit(vnode_t
*vp
)
11878 ASSERT(rp
->r_flags
& R4COMMIT
);
11880 /* make sure we're looking at the master vnode, not a shadow */
11882 if (IS_SHADOW(vp
, rp
))
11885 vmobject_lock(&vp
->v_object
);
11888 * Step through all of the pages associated with this vnode
11889 * looking for pages which need to be committed.
11891 for (pp
= vmobject_get_head(&vp
->v_object
);
11893 pp
= vmobject_get_next(&vp
->v_object
, pp
)) {
11894 /* Skip marker pages. */
11895 if (PP_ISPVN_TAG(pp
))
11899 * First short-cut everything (without the page_lock)
11900 * and see if this page does not need to be committed
11901 * or is modified if so then we'll just skip it.
11903 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
))
11907 * Attempt to lock the page. If we can't, then
11908 * someone else is messing with it or we have been
11909 * called from nfs4_dispose and this is the page that
11910 * nfs4_dispose was called with.. anyway just skip it.
11912 if (!page_trylock(pp
, SE_EXCL
))
11916 * Lets check again now that we have the page lock.
11918 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
11923 /* this had better not be a free page */
11924 ASSERT(PP_ISFREE(pp
) == 0);
11927 * The page needs to be committed and we locked it.
11928 * Update the base and length parameters and add it
11931 if (rp
->r_commit
.c_pages
== NULL
) {
11932 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11933 rp
->r_commit
.c_commlen
= PAGESIZE
;
11934 } else if (pp
->p_offset
< rp
->r_commit
.c_commbase
) {
11935 rp
->r_commit
.c_commlen
= rp
->r_commit
.c_commbase
-
11936 (offset3
)pp
->p_offset
+ rp
->r_commit
.c_commlen
;
11937 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11938 } else if ((rp
->r_commit
.c_commbase
+ rp
->r_commit
.c_commlen
)
11940 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
11941 rp
->r_commit
.c_commbase
+ PAGESIZE
;
11943 page_add(&rp
->r_commit
.c_pages
, pp
);
11946 vmobject_unlock(&vp
->v_object
);
11950 * This routine is used to gather together a page list of the pages
11951 * which are to be committed on the server. This routine must not
11952 * be called if the calling thread holds any locked pages.
11954 * The calling thread must have set R4COMMIT. This bit is used to
11955 * serialize access to the commit structure in the rnode. As long
11956 * as the thread has set R4COMMIT, then it can manipulate the commit
11957 * structure without requiring any other locks.
11960 nfs4_get_commit_range(vnode_t
*vp
, uoff_t soff
, size_t len
)
11969 ASSERT(rp
->r_flags
& R4COMMIT
);
11971 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11973 /* make sure we're looking at the master vnode, not a shadow */
11975 if (IS_SHADOW(vp
, rp
))
11979 * If there are no pages associated with this vnode, then
11982 if (!vn_has_cached_data(vp
))
11985 * Calculate the ending offset.
11988 for (off
= soff
; off
< end
; off
+= PAGESIZE
) {
11990 * Lookup each page by vp, offset.
11992 if ((pp
= page_lookup_nowait(&vp
->v_object
, off
, SE_EXCL
)) == NULL
)
11995 * If this page does not need to be committed or is
11996 * modified, then just skip it.
11998 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
12003 ASSERT(PP_ISFREE(pp
) == 0);
12005 * The page needs to be committed and we locked it.
12006 * Update the base and length parameters and add it
12009 if (rp
->r_commit
.c_pages
== NULL
) {
12010 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
12011 rp
->r_commit
.c_commlen
= PAGESIZE
;
12013 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
12014 rp
->r_commit
.c_commbase
+ PAGESIZE
;
12016 page_add(&rp
->r_commit
.c_pages
, pp
);
12021 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12022 * Flushes and commits data to the server.
12025 nfs4_putpage_commit(vnode_t
*vp
, offset_t poff
, size_t plen
, cred_t
*cr
)
12028 verifier4 write_verf
;
12029 rnode4_t
*rp
= VTOR4(vp
);
12031 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12034 * Flush the data portion of the file and then commit any
12035 * portions which need to be committed. This may need to
12036 * be done twice if the server has changed state since
12037 * data was last written. The data will need to be
12038 * rewritten to the server and then a new commit done.
12040 * In fact, this may need to be done several times if the
12041 * server is having problems and crashing while we are
12042 * attempting to do this.
12047 * Do a flush based on the poff and plen arguments. This
12048 * will synchronously write out any modified pages in the
12049 * range specified by (poff, plen). This starts all of the
12050 * i/o operations which will be waited for in the next
12051 * call to nfs4_putpage
12054 mutex_enter(&rp
->r_statelock
);
12055 write_verf
= rp
->r_writeverf
;
12056 mutex_exit(&rp
->r_statelock
);
12058 error
= nfs4_putpage(vp
, poff
, plen
, B_ASYNC
, cr
, NULL
);
12059 if (error
== EAGAIN
)
12063 * Do a flush based on the poff and plen arguments. This
12064 * will synchronously write out any modified pages in the
12065 * range specified by (poff, plen) and wait until all of
12066 * the asynchronous i/o's in that range are done as well.
12069 error
= nfs4_putpage(vp
, poff
, plen
, 0, cr
, NULL
);
12074 mutex_enter(&rp
->r_statelock
);
12075 if (rp
->r_writeverf
!= write_verf
) {
12076 mutex_exit(&rp
->r_statelock
);
12079 mutex_exit(&rp
->r_statelock
);
12082 * Now commit any pages which might need to be committed.
12083 * If the error, NFS_VERF_MISMATCH, is returned, then
12084 * start over with the flush operation.
12086 error
= nfs4_commit_vp(vp
, poff
, plen
, cr
, NFS4_WRITE_WAIT
);
12088 if (error
== NFS_VERF_MISMATCH
)
12095 * nfs4_commit_vp() will wait for other pending commits and
12096 * will either commit the whole file or a range, plen dictates
12097 * if we commit whole file. a value of zero indicates the whole
12098 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12101 nfs4_commit_vp(vnode_t
*vp
, uoff_t poff
, size_t plen
,
12102 cred_t
*cr
, int wait_on_writes
)
12109 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12114 * before we gather commitable pages make
12115 * sure there are no outstanding async writes
12117 if (rp
->r_count
&& wait_on_writes
== NFS4_WRITE_WAIT
) {
12118 mutex_enter(&rp
->r_statelock
);
12119 while (rp
->r_count
> 0) {
12120 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
12122 mutex_exit(&rp
->r_statelock
);
12126 * Set the `commit inprogress' state bit. We must
12127 * first wait until any current one finishes.
12129 mutex_enter(&rp
->r_statelock
);
12130 while (rp
->r_flags
& R4COMMIT
) {
12131 rp
->r_flags
|= R4COMMITWAIT
;
12132 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
12133 rp
->r_flags
&= ~R4COMMITWAIT
;
12135 rp
->r_flags
|= R4COMMIT
;
12136 mutex_exit(&rp
->r_statelock
);
12139 * Gather all of the pages which need to be
12143 nfs4_get_commit(vp
);
12145 nfs4_get_commit_range(vp
, poff
, plen
);
12148 * Clear the `commit inprogress' bit and disconnect the
12149 * page list which was gathered by nfs4_get_commit.
12151 plist
= rp
->r_commit
.c_pages
;
12152 rp
->r_commit
.c_pages
= NULL
;
12153 offset
= rp
->r_commit
.c_commbase
;
12154 len
= rp
->r_commit
.c_commlen
;
12155 mutex_enter(&rp
->r_statelock
);
12156 rp
->r_flags
&= ~R4COMMIT
;
12157 cv_broadcast(&rp
->r_commit
.c_cv
);
12158 mutex_exit(&rp
->r_statelock
);
12161 * If any pages need to be committed, commit them and
12162 * then unlock them so that they can be freed some
12169 * No error occurred during the flush portion
12170 * of this operation, so now attempt to commit
12171 * the data to stable storage on the server.
12173 * This will unlock all of the pages on the list.
12175 return (nfs4_sync_commit(vp
, plist
, offset
, len
, cr
));
12179 nfs4_sync_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
12185 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12187 error
= nfs4_commit(vp
, (offset4
)offset
, (count3
)count
, cr
);
12190 * If we got an error, then just unlock all of the pages
12194 while (plist
!= NULL
) {
12196 page_sub(&plist
, pp
);
12202 * We've tried as hard as we can to commit the data to stable
12203 * storage on the server. We just unlock the pages and clear
12204 * the commit required state. They will get freed later.
12206 while (plist
!= NULL
) {
12208 page_sub(&plist
, pp
);
12209 pp
->p_fsdata
= C_NOCOMMIT
;
12217 do_nfs4_async_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
12221 (void) nfs4_sync_commit(vp
, plist
, offset
, count
, cr
);
12226 nfs4_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
12227 caller_context_t
*ct
)
12232 vsecattr_t nfsace4_vsap
;
12235 if (nfs_zone() != mi
->mi_zone
)
12237 if (mi
->mi_flags
& MI4_ACL
) {
12238 /* if we have a delegation, return it */
12239 if (VTOR4(vp
)->r_deleg_type
!= OPEN_DELEGATE_NONE
)
12240 (void) nfs4delegreturn(VTOR4(vp
),
12241 NFS4_DR_REOPEN
|NFS4_DR_PUSH
);
12243 error
= nfs4_is_acl_mask_valid(vsecattr
->vsa_mask
,
12245 if (error
) /* EINVAL */
12248 if (vsecattr
->vsa_mask
& (VSA_ACL
| VSA_DFACL
)) {
12250 * These are aclent_t type entries.
12252 error
= vs_aent_to_ace4(vsecattr
, &nfsace4_vsap
,
12253 vp
->v_type
== VDIR
, FALSE
);
12258 * These are ace_t type entries.
12260 error
= vs_acet_to_ace4(vsecattr
, &nfsace4_vsap
,
12265 bzero(&va
, sizeof (va
));
12266 error
= nfs4setattr(vp
, &va
, flag
, cr
, &nfsace4_vsap
);
12267 vs_ace4_destroy(&nfsace4_vsap
);
12275 nfs4_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
12276 caller_context_t
*ct
)
12281 rnode4_t
*rp
= VTOR4(vp
);
12284 if (nfs_zone() != mi
->mi_zone
)
12287 bzero(&gar
, sizeof (gar
));
12288 gar
.n4g_vsa
.vsa_mask
= vsecattr
->vsa_mask
;
12291 * vsecattr->vsa_mask holds the original acl request mask.
12292 * This is needed when determining what to return.
12293 * (See: nfs4_create_getsecattr_return())
12295 error
= nfs4_is_acl_mask_valid(vsecattr
->vsa_mask
, NFS4_ACL_GET
);
12296 if (error
) /* EINVAL */
12300 * If this is a referral stub, don't try to go OTW for an ACL
12302 if (RP_ISSTUB_REFERRAL(VTOR4(vp
)))
12303 return (fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
));
12305 if (mi
->mi_flags
& MI4_ACL
) {
12307 * Check if the data is cached and the cache is valid. If it
12308 * is we don't go over the wire.
12310 if (rp
->r_secattr
!= NULL
&& ATTRCACHE4_VALID(vp
)) {
12311 mutex_enter(&rp
->r_statelock
);
12312 if (rp
->r_secattr
!= NULL
) {
12313 error
= nfs4_create_getsecattr_return(
12314 rp
->r_secattr
, vsecattr
, rp
->r_attr
.va_uid
,
12316 vp
->v_type
== VDIR
);
12317 if (!error
) { /* error == 0 - Success! */
12318 mutex_exit(&rp
->r_statelock
);
12322 mutex_exit(&rp
->r_statelock
);
12326 * The getattr otw call will always get both the acl, in
12327 * the form of a list of nfsace4's, and the number of acl
12328 * entries; independent of the value of gar.n4g_va.va_mask.
12330 error
= nfs4_getattr_otw(vp
, &gar
, cr
, 1);
12332 vs_ace4_destroy(&gar
.n4g_vsa
);
12333 if (error
== ENOTSUP
|| error
== EOPNOTSUPP
)
12334 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12338 if (!(gar
.n4g_resbmap
& FATTR4_ACL_MASK
)) {
12340 * No error was returned, but according to the response
12341 * bitmap, neither was an acl.
12343 vs_ace4_destroy(&gar
.n4g_vsa
);
12344 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12349 * Update the cache with the ACL.
12351 nfs4_acl_fill_cache(rp
, &gar
.n4g_vsa
);
12353 error
= nfs4_create_getsecattr_return(&gar
.n4g_vsa
,
12354 vsecattr
, gar
.n4g_va
.va_uid
, gar
.n4g_va
.va_gid
,
12355 vp
->v_type
== VDIR
);
12356 vs_ace4_destroy(&gar
.n4g_vsa
);
12357 if ((error
) && (vsecattr
->vsa_mask
&
12358 (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
)) &&
12359 (error
!= EACCES
)) {
12360 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12364 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12369 * The function returns:
12370 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12371 * - EINVAL if the passed in "acl_mask" is an invalid request.
12373 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12374 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12376 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12377 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12378 * - We have a count field set without the corresponding acl field set. (e.g. -
12379 * VSA_ACECNT is set, but VSA_ACE is not)
12382 nfs4_is_acl_mask_valid(uint_t acl_mask
, nfs4_acl_op_t op
)
12384 /* Shortcut the masks that are always valid. */
12385 if (acl_mask
== (VSA_ACE
| VSA_ACECNT
))
12387 if (acl_mask
== (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
))
12390 if (acl_mask
& (VSA_ACE
| VSA_ACECNT
)) {
12392 * We can't have any VSA_ACL type stuff in the mask now.
12394 if (acl_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
|
12398 if (op
== NFS4_ACL_SET
) {
12399 if ((acl_mask
& VSA_ACECNT
) && !(acl_mask
& VSA_ACE
))
12404 if (acl_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
)) {
12406 * We can't have any VSA_ACE type stuff in the mask now.
12408 if (acl_mask
& (VSA_ACE
| VSA_ACECNT
))
12411 if (op
== NFS4_ACL_SET
) {
12412 if ((acl_mask
& VSA_ACLCNT
) && !(acl_mask
& VSA_ACL
))
12415 if ((acl_mask
& VSA_DFACLCNT
) &&
12416 !(acl_mask
& VSA_DFACL
))
12424 * The theory behind creating the correct getsecattr return is simply this:
12425 * "Don't return anything that the caller is not expecting to have to free."
12428 nfs4_create_getsecattr_return(vsecattr_t
*filled_vsap
, vsecattr_t
*vsap
,
12429 uid_t uid
, gid_t gid
, int isdir
)
12432 /* Save the mask since the translators modify it. */
12433 uint_t orig_mask
= vsap
->vsa_mask
;
12435 if (orig_mask
& (VSA_ACE
| VSA_ACECNT
)) {
12436 error
= vs_ace4_to_acet(filled_vsap
, vsap
, uid
, gid
, FALSE
);
12442 * If the caller only asked for the ace count (VSA_ACECNT)
12443 * don't give them the full acl (VSA_ACE), free it.
12445 if (!orig_mask
& VSA_ACE
) {
12446 if (vsap
->vsa_aclentp
!= NULL
) {
12447 kmem_free(vsap
->vsa_aclentp
,
12448 vsap
->vsa_aclcnt
* sizeof (ace_t
));
12449 vsap
->vsa_aclentp
= NULL
;
12452 vsap
->vsa_mask
= orig_mask
;
12454 } else if (orig_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
|
12456 error
= vs_ace4_to_aent(filled_vsap
, vsap
, uid
, gid
,
12463 * If the caller only asked for the acl count (VSA_ACLCNT)
12464 * and/or the default acl count (VSA_DFACLCNT) don't give them
12465 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12467 if (!orig_mask
& VSA_ACL
) {
12468 if (vsap
->vsa_aclentp
!= NULL
) {
12469 kmem_free(vsap
->vsa_aclentp
,
12470 vsap
->vsa_aclcnt
* sizeof (aclent_t
));
12471 vsap
->vsa_aclentp
= NULL
;
12475 if (!orig_mask
& VSA_DFACL
) {
12476 if (vsap
->vsa_dfaclentp
!= NULL
) {
12477 kmem_free(vsap
->vsa_dfaclentp
,
12478 vsap
->vsa_dfaclcnt
* sizeof (aclent_t
));
12479 vsap
->vsa_dfaclentp
= NULL
;
12482 vsap
->vsa_mask
= orig_mask
;
12489 nfs4_shrlock(vnode_t
*vp
, int cmd
, struct shrlock
*shr
, int flag
, cred_t
*cr
,
12490 caller_context_t
*ct
)
12494 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
12497 * check for valid cmd parameter
12499 if (cmd
!= F_SHARE
&& cmd
!= F_UNSHARE
&& cmd
!= F_HASREMOTELOCKS
)
12503 * Check access permissions
12505 if ((cmd
& F_SHARE
) &&
12506 (((shr
->s_access
& F_RDACC
) && (flag
& FREAD
) == 0) ||
12507 (shr
->s_access
== F_WRACC
&& (flag
& FWRITE
) == 0)))
12511 * If the filesystem is mounted using local locking, pass the
12512 * request off to the local share code.
12514 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
)
12515 return (fs_shrlock(vp
, cmd
, shr
, flag
, cr
, ct
));
12521 * This will be properly implemented later,
12522 * see RFE: 4823948 .
12527 case F_HASREMOTELOCKS
:
12529 * NFS client can't store remote locks itself
12544 * Common code called by directory ops to update the attrcache
12547 nfs4_update_attrcache(nfsstat4 status
, nfs4_ga_res_t
*garp
,
12548 hrtime_t t
, vnode_t
*vp
, cred_t
*cr
)
12552 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12554 if (status
!= NFS4_OK
) {
12555 /* getattr not done or failed */
12556 PURGE_ATTRCACHE4(vp
);
12561 nfs4_attr_cache(vp
, garp
, t
, cr
, FALSE
, NULL
);
12563 PURGE_ATTRCACHE4(vp
);
12569 * Update directory caches for directory modification ops (link, rename, etc.)
12570 * When dinfo is NULL, manage dircaches in the old way.
12573 nfs4_update_dircaches(change_info4
*cinfo
, vnode_t
*dvp
, vnode_t
*vp
, char *nm
,
12574 dirattr_info_t
*dinfo
)
12576 rnode4_t
*drp
= VTOR4(dvp
);
12578 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
12580 /* Purge rddir cache for dir since it changed */
12581 if (drp
->r_dir
!= NULL
)
12582 nfs4_purge_rddir_cache(dvp
);
12585 * If caller provided dinfo, then use it to manage dir caches.
12587 if (dinfo
!= NULL
) {
12589 mutex_enter(&VTOR4(vp
)->r_statev4_lock
);
12590 if (!VTOR4(vp
)->created_v4
) {
12591 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12592 dnlc_update(dvp
, nm
, vp
);
12595 * XXX don't update if the created_v4 flag is
12598 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12599 NFS4_DEBUG(nfs4_client_state_debug
,
12600 (CE_NOTE
, "nfs4_update_dircaches: "
12601 "don't update dnlc: created_v4 flag"));
12605 nfs4_attr_cache(dvp
, dinfo
->di_garp
, dinfo
->di_time_call
,
12606 dinfo
->di_cred
, FALSE
, cinfo
);
12612 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12613 * Since caller modified dir but didn't receive post-dirmod-op dir
12614 * attrs, the dir's attrs must be purged.
12616 * XXX this check and dnlc update/purge should really be atomic,
12617 * XXX but can't use rnode statelock because it'll deadlock in
12618 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12621 * XXX We also may want to check that atomic is true in the
12622 * XXX change_info struct. If it is not, the change_info may
12623 * XXX reflect changes by more than one clients which means that
12624 * XXX our cache may not be valid.
12626 PURGE_ATTRCACHE4(dvp
);
12627 if (drp
->r_change
== cinfo
->before
) {
12628 /* no changes took place in the directory prior to our link */
12630 mutex_enter(&VTOR4(vp
)->r_statev4_lock
);
12631 if (!VTOR4(vp
)->created_v4
) {
12632 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12633 dnlc_update(dvp
, nm
, vp
);
12636 * XXX dont' update if the created_v4 flag
12639 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12640 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
12641 "nfs4_update_dircaches: don't"
12642 " update dnlc: created_v4 flag"));
12646 /* Another client modified directory - purge its dnlc cache */
12647 dnlc_purge_vp(dvp
);
12652 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12655 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12656 * file (ie: client recovery) and otherwise set to FALSE.
12658 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12659 * initiated) calling functions.
12661 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12662 * of resending a 'lost' open request.
12664 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12665 * server that hands out BAD_SEQID on open confirm.
12667 * Errors are returned via the nfs4_error_t parameter.
12670 nfs4open_confirm(vnode_t
*vp
, seqid4
*seqid
, stateid4
*stateid
, cred_t
*cr
,
12671 bool_t reopening_file
, bool_t
*retry_open
, nfs4_open_owner_t
*oop
,
12672 bool_t resend
, nfs4_error_t
*ep
, int *num_bseqid_retryp
)
12674 COMPOUND4args_clnt args
;
12675 COMPOUND4res_clnt res
;
12676 nfs_argop4 argop
[2];
12680 OPEN_CONFIRM4args
*open_confirm_args
;
12683 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12685 mutex_enter(&oop
->oo_lock
);
12686 ASSERT(oop
->oo_seqid_inuse
);
12687 mutex_exit(&oop
->oo_lock
);
12690 recov_retry_confirm
:
12691 nfs4_error_zinit(ep
);
12692 *retry_open
= FALSE
;
12695 args
.ctag
= TAG_OPEN_CONFIRM_LOST
;
12697 args
.ctag
= TAG_OPEN_CONFIRM
;
12699 args
.array_len
= 2;
12700 args
.array
= argop
;
12702 /* putfh target fh */
12703 argop
[0].argop
= OP_CPUTFH
;
12704 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
12706 argop
[1].argop
= OP_OPEN_CONFIRM
;
12707 open_confirm_args
= &argop
[1].nfs_argop4_u
.opopen_confirm
;
12710 open_confirm_args
->seqid
= *seqid
;
12711 open_confirm_args
->open_stateid
= *stateid
;
12715 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, ep
);
12717 if (!ep
->error
&& nfs4_need_to_bump_seqid(&res
)) {
12718 nfs4_set_open_seqid((*seqid
), oop
, args
.ctag
);
12721 needrecov
= nfs4_needs_recovery(ep
, FALSE
, mi
->mi_vfsp
);
12722 if (!needrecov
&& ep
->error
)
12726 bool_t abort
= FALSE
;
12728 if (reopening_file
== FALSE
) {
12729 nfs4_bseqid_entry_t
*bsep
= NULL
;
12731 if (!ep
->error
&& res
.status
== NFS4ERR_BAD_SEQID
)
12732 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
12734 open_confirm_args
->seqid
);
12736 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
,
12737 NULL
, NULL
, OP_OPEN_CONFIRM
, bsep
, NULL
, NULL
);
12739 kmem_free(bsep
, sizeof (*bsep
));
12740 if (num_bseqid_retryp
&&
12741 --(*num_bseqid_retryp
) == 0)
12745 if ((ep
->error
== ETIMEDOUT
||
12746 res
.status
== NFS4ERR_RESOURCE
) &&
12747 abort
== FALSE
&& resend
== FALSE
) {
12749 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12751 ddi_sleep(confirm_retry_sec
);
12752 goto recov_retry_confirm
;
12754 /* State may have changed so retry the entire OPEN op */
12755 if (abort
== FALSE
)
12756 *retry_open
= TRUE
;
12758 *retry_open
= FALSE
;
12760 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12765 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12769 resop
= &res
.array
[1]; /* open confirm res */
12770 bcopy(&resop
->nfs_resop4_u
.opopen_confirm
.open_stateid
,
12771 stateid
, sizeof (*stateid
));
12773 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12777 * Return the credentials associated with a client state object. The
12778 * caller is responsible for freeing the credentials.
12782 state_to_cred(nfs4_open_stream_t
*osp
)
12787 * It's ok to not lock the open stream and open owner to get
12788 * the oo_cred since this is only written once (upon creation)
12789 * and will not change.
12791 cr
= osp
->os_open_owner
->oo_cred
;
12800 * Find the sysid for the knetconfig associated with the given mi.
12802 static struct lm_sysid
*
12803 nfs4_find_sysid(mntinfo4_t
*mi
)
12805 ASSERT(nfs_zone() == mi
->mi_zone
);
12808 * Switch from RDMA knconf to original mount knconf
12810 return (lm_get_sysid(ORIG_KNCONF(mi
), &mi
->mi_curr_serv
->sv_addr
,
12811 mi
->mi_curr_serv
->sv_hostname
, NULL
));
12816 * Return a string version of the call type for easy reading.
12819 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype
)
12822 case NFS4_LCK_CTYPE_NORM
:
12824 case NFS4_LCK_CTYPE_RECLAIM
:
12825 return ("RECLAIM");
12826 case NFS4_LCK_CTYPE_RESEND
:
12828 case NFS4_LCK_CTYPE_REINSTATE
:
12829 return ("REINSTATE");
12831 cmn_err(CE_PANIC
, "nfs4frlock_get_call_type: got illegal "
12839 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12840 * Unlock requests don't have an over-the-wire locktype, so we just return
12841 * something non-threatening.
12844 static nfs_lock_type4
12845 flk_to_locktype(int cmd
, int l_type
)
12847 ASSERT(l_type
== F_RDLCK
|| l_type
== F_WRLCK
|| l_type
== F_UNLCK
);
12853 if (cmd
== F_SETLK
)
12858 if (cmd
== F_SETLK
)
12861 return (WRITEW_LT
);
12863 panic("flk_to_locktype");
12868 * Do some preliminary checks for nfs4frlock.
12871 nfs4frlock_validate_args(int cmd
, flock64_t
*flk
, int flag
, vnode_t
*vp
,
12877 * If we are setting a lock, check that the file is opened
12878 * with the correct mode.
12880 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
12881 if ((flk
->l_type
== F_RDLCK
&& (flag
& FREAD
) == 0) ||
12882 (flk
->l_type
== F_WRLCK
&& (flag
& FWRITE
) == 0)) {
12883 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12884 "nfs4frlock_validate_args: file was opened with "
12885 "incorrect mode"));
12890 /* Convert the offset. It may need to be restored before returning. */
12891 if (error
= convoff(vp
, flk
, 0, offset
)) {
12892 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12893 "nfs4frlock_validate_args: convoff => error= %d\n",
12902 * Set the flock64's lm_sysid for nfs4frlock.
12905 nfs4frlock_get_sysid(struct lm_sysid
**lspp
, vnode_t
*vp
, flock64_t
*flk
)
12907 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12909 /* Find the lm_sysid */
12910 *lspp
= nfs4_find_sysid(VTOMI4(vp
));
12912 if (*lspp
== NULL
) {
12913 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12914 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12918 flk
->l_sysid
= lm_sysidt(*lspp
);
12924 * Do the remaining preliminary setup for nfs4frlock.
12927 nfs4frlock_pre_setup(clock_t *tick_delayp
, nfs4_recov_state_t
*recov_statep
,
12928 flock64_t
*flk
, short *whencep
, vnode_t
*vp
, cred_t
*search_cr
,
12932 * set tick_delay to the base delay time.
12933 * (NFS4_BASE_WAIT_TIME is in secs)
12936 *tick_delayp
= drv_usectohz(NFS4_BASE_WAIT_TIME
* 1000 * 1000);
12939 * If lock is relative to EOF, we need the newest length of the
12940 * file. Therefore invalidate the ATTR_CACHE.
12943 *whencep
= flk
->l_whence
;
12945 if (*whencep
== 2) /* SEEK_END */
12946 PURGE_ATTRCACHE4(vp
);
12948 recov_statep
->rs_flags
= 0;
12949 recov_statep
->rs_num_retry_despite_err
= 0;
12950 *cred_otw
= nfs4_get_otw_cred(search_cr
, VTOMI4(vp
), NULL
);
12954 * Initialize and allocate the data structures necessary for
12955 * the nfs4frlock call.
12956 * Allocates argsp's op array.
12959 nfs4frlock_call_init(COMPOUND4args_clnt
*argsp
, COMPOUND4args_clnt
**argspp
,
12960 nfs_argop4
**argopp
, nfs4_op_hint_t
*op_hintp
, flock64_t
*flk
, int cmd
,
12961 bool_t
*retry
, bool_t
*did_start_fop
, COMPOUND4res_clnt
**respp
,
12962 bool_t
*skip_get_err
, nfs4_lost_rqst_t
*lost_rqstp
)
12964 int argoplist_size
;
12968 *did_start_fop
= FALSE
;
12969 *skip_get_err
= FALSE
;
12970 lost_rqstp
->lr_op
= 0;
12971 argoplist_size
= num_ops
* sizeof (nfs_argop4
);
12972 /* fill array with zero */
12973 *argopp
= kmem_zalloc(argoplist_size
, KM_SLEEP
);
12978 argsp
->array_len
= num_ops
;
12979 argsp
->array
= *argopp
;
12981 /* initialize in case of error; will get real value down below */
12982 argsp
->ctag
= TAG_NONE
;
12984 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) && flk
->l_type
== F_UNLCK
)
12985 *op_hintp
= OH_LOCKU
;
12987 *op_hintp
= OH_OTHER
;
12991 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
12992 * the proper nfs4_server_t for this instance of nfs4frlock.
12993 * Returns 0 (success) or an errno value.
12996 nfs4frlock_start_call(nfs4_lock_call_type_t ctype
, vnode_t
*vp
,
12997 nfs4_op_hint_t op_hint
, nfs4_recov_state_t
*recov_statep
,
12998 bool_t
*did_start_fop
, bool_t
*startrecovp
)
13003 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13005 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
13006 error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
13007 recov_statep
, startrecovp
);
13010 *did_start_fop
= TRUE
;
13012 *did_start_fop
= FALSE
;
13013 *startrecovp
= FALSE
;
13019 /* If the file failed recovery, just quit. */
13020 mutex_enter(&rp
->r_statelock
);
13021 if (rp
->r_flags
& R4RECOVERR
) {
13024 mutex_exit(&rp
->r_statelock
);
13031 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13032 * resend nfs4frlock call is initiated by the recovery framework.
13033 * Acquires the lop and oop seqid synchronization.
13036 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t
*resend_rqstp
,
13037 COMPOUND4args_clnt
*argsp
, nfs_argop4
*argop
, nfs4_lock_owner_t
**lopp
,
13038 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13039 LOCK4args
**lock_argsp
, LOCKU4args
**locku_argsp
)
13041 mntinfo4_t
*mi
= VTOMI4(resend_rqstp
->lr_vp
);
13044 NFS4_DEBUG((nfs4_lost_rqst_debug
|| nfs4_client_lock_debug
),
13046 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13047 ASSERT(resend_rqstp
!= NULL
);
13048 ASSERT(resend_rqstp
->lr_op
== OP_LOCK
||
13049 resend_rqstp
->lr_op
== OP_LOCKU
);
13051 *oopp
= resend_rqstp
->lr_oop
;
13052 if (resend_rqstp
->lr_oop
) {
13053 open_owner_hold(resend_rqstp
->lr_oop
);
13054 error
= nfs4_start_open_seqid_sync(resend_rqstp
->lr_oop
, mi
);
13055 ASSERT(error
== 0); /* recov thread always succeeds */
13058 /* Must resend this lost lock/locku request. */
13059 ASSERT(resend_rqstp
->lr_lop
!= NULL
);
13060 *lopp
= resend_rqstp
->lr_lop
;
13061 lock_owner_hold(resend_rqstp
->lr_lop
);
13062 error
= nfs4_start_lock_seqid_sync(resend_rqstp
->lr_lop
, mi
);
13063 ASSERT(error
== 0); /* recov thread always succeeds */
13065 *ospp
= resend_rqstp
->lr_osp
;
13067 open_stream_hold(resend_rqstp
->lr_osp
);
13069 if (resend_rqstp
->lr_op
== OP_LOCK
) {
13070 LOCK4args
*lock_args
;
13072 argop
->argop
= OP_LOCK
;
13073 *lock_argsp
= lock_args
= &argop
->nfs_argop4_u
.oplock
;
13074 lock_args
->locktype
= resend_rqstp
->lr_locktype
;
13075 lock_args
->reclaim
=
13076 (resend_rqstp
->lr_ctype
== NFS4_LCK_CTYPE_RECLAIM
);
13077 lock_args
->offset
= resend_rqstp
->lr_flk
->l_start
;
13078 lock_args
->length
= resend_rqstp
->lr_flk
->l_len
;
13079 if (lock_args
->length
== 0)
13080 lock_args
->length
= ~lock_args
->length
;
13081 nfs4_setup_lock_args(*lopp
, *oopp
, *ospp
,
13082 mi2clientid(mi
), &lock_args
->locker
);
13084 switch (resend_rqstp
->lr_ctype
) {
13085 case NFS4_LCK_CTYPE_RESEND
:
13086 argsp
->ctag
= TAG_LOCK_RESEND
;
13088 case NFS4_LCK_CTYPE_REINSTATE
:
13089 argsp
->ctag
= TAG_LOCK_REINSTATE
;
13091 case NFS4_LCK_CTYPE_RECLAIM
:
13092 argsp
->ctag
= TAG_LOCK_RECLAIM
;
13095 argsp
->ctag
= TAG_LOCK_UNKNOWN
;
13099 LOCKU4args
*locku_args
;
13100 nfs4_lock_owner_t
*lop
= resend_rqstp
->lr_lop
;
13102 argop
->argop
= OP_LOCKU
;
13103 *locku_argsp
= locku_args
= &argop
->nfs_argop4_u
.oplocku
;
13104 locku_args
->locktype
= READ_LT
;
13105 locku_args
->seqid
= lop
->lock_seqid
+ 1;
13106 mutex_enter(&lop
->lo_lock
);
13107 locku_args
->lock_stateid
= lop
->lock_stateid
;
13108 mutex_exit(&lop
->lo_lock
);
13109 locku_args
->offset
= resend_rqstp
->lr_flk
->l_start
;
13110 locku_args
->length
= resend_rqstp
->lr_flk
->l_len
;
13111 if (locku_args
->length
== 0)
13112 locku_args
->length
= ~locku_args
->length
;
13114 switch (resend_rqstp
->lr_ctype
) {
13115 case NFS4_LCK_CTYPE_RESEND
:
13116 argsp
->ctag
= TAG_LOCKU_RESEND
;
13118 case NFS4_LCK_CTYPE_REINSTATE
:
13119 argsp
->ctag
= TAG_LOCKU_REINSTATE
;
13122 argsp
->ctag
= TAG_LOCK_UNKNOWN
;
13129 * Setup the LOCKT4 arguments.
13132 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype
, nfs_argop4
*argop
,
13133 LOCKT4args
**lockt_argsp
, COMPOUND4args_clnt
*argsp
, flock64_t
*flk
,
13136 LOCKT4args
*lockt_args
;
13138 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp
))->mi_zone
);
13139 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13140 argop
->argop
= OP_LOCKT
;
13141 argsp
->ctag
= TAG_LOCKT
;
13142 lockt_args
= &argop
->nfs_argop4_u
.oplockt
;
13145 * The locktype will be READ_LT unless it's
13146 * a write lock. We do this because the Solaris
13147 * system call allows the combination of
13148 * F_UNLCK and F_GETLK* and so in that case the
13149 * unlock is mapped to a read.
13151 if (flk
->l_type
== F_WRLCK
)
13152 lockt_args
->locktype
= WRITE_LT
;
13154 lockt_args
->locktype
= READ_LT
;
13156 lockt_args
->owner
.clientid
= mi2clientid(VTOMI4(RTOV4(rp
)));
13157 /* set the lock owner4 args */
13158 nfs4_setlockowner_args(&lockt_args
->owner
, rp
,
13159 ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pidp
->pid_id
:
13161 lockt_args
->offset
= flk
->l_start
;
13162 lockt_args
->length
= flk
->l_len
;
13163 if (flk
->l_len
== 0)
13164 lockt_args
->length
= ~lockt_args
->length
;
13166 *lockt_argsp
= lockt_args
;
13170 * If the client is holding a delegation, and the open stream to be used
13171 * with this lock request is a delegation open stream, then re-open the stream.
13172 * Sets the nfs4_error_t to all zeros unless the open stream has already
13173 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13174 * means the caller should retry (like a recovery retry).
13177 nfs4frlock_check_deleg(vnode_t
*vp
, nfs4_error_t
*ep
, cred_t
*cr
, int lt
)
13179 open_delegation_type4 dt
;
13180 bool_t reopen_needed
, force
;
13181 nfs4_open_stream_t
*osp
;
13182 open_claim_type4 oclaim
;
13183 rnode4_t
*rp
= VTOR4(vp
);
13184 mntinfo4_t
*mi
= VTOMI4(vp
);
13186 ASSERT(nfs_zone() == mi
->mi_zone
);
13188 nfs4_error_zinit(ep
);
13190 mutex_enter(&rp
->r_statev4_lock
);
13191 dt
= rp
->r_deleg_type
;
13192 mutex_exit(&rp
->r_statev4_lock
);
13194 if (dt
!= OPEN_DELEGATE_NONE
) {
13195 nfs4_open_owner_t
*oop
;
13197 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
13199 ep
->stat
= NFS4ERR_IO
;
13202 /* returns with 'os_sync_lock' held */
13203 osp
= find_open_stream(oop
, rp
);
13205 open_owner_rele(oop
);
13206 ep
->stat
= NFS4ERR_IO
;
13210 if (osp
->os_failed_reopen
) {
13211 NFS4_DEBUG((nfs4_open_stream_debug
||
13212 nfs4_client_lock_debug
), (CE_NOTE
,
13213 "nfs4frlock_check_deleg: os_failed_reopen set "
13214 "for osp %p, cr %p, rp %s", (void *)osp
,
13215 (void *)cr
, rnode4info(rp
)));
13216 mutex_exit(&osp
->os_sync_lock
);
13217 open_stream_rele(osp
, rp
);
13218 open_owner_rele(oop
);
13219 ep
->stat
= NFS4ERR_IO
;
13224 * Determine whether a reopen is needed. If this
13225 * is a delegation open stream, then send the open
13226 * to the server to give visibility to the open owner.
13227 * Even if it isn't a delegation open stream, we need
13228 * to check if the previous open CLAIM_DELEGATE_CUR
13232 reopen_needed
= osp
->os_delegation
||
13234 !(osp
->os_dc_openacc
& OPEN4_SHARE_ACCESS_READ
)) ||
13236 !(osp
->os_dc_openacc
& OPEN4_SHARE_ACCESS_WRITE
)));
13238 mutex_exit(&osp
->os_sync_lock
);
13239 open_owner_rele(oop
);
13241 if (reopen_needed
) {
13243 * Always use CLAIM_PREVIOUS after server reboot.
13244 * The server will reject CLAIM_DELEGATE_CUR if
13245 * it is used during the grace period.
13247 mutex_enter(&mi
->mi_lock
);
13248 if (mi
->mi_recovflags
& MI4R_SRV_REBOOT
) {
13249 oclaim
= CLAIM_PREVIOUS
;
13252 oclaim
= CLAIM_DELEGATE_CUR
;
13255 mutex_exit(&mi
->mi_lock
);
13257 nfs4_reopen(vp
, osp
, ep
, oclaim
, force
, FALSE
);
13258 if (ep
->error
== EAGAIN
) {
13259 nfs4_error_zinit(ep
);
13260 ep
->stat
= NFS4ERR_DELAY
;
13263 open_stream_rele(osp
, rp
);
13269 * Setup the LOCKU4 arguments.
13270 * Returns errors via the nfs4_error_t.
13271 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13272 * over-the-wire. The caller must release the
13273 * reference on *lopp.
13274 * NFS4ERR_DELAY caller should retry (like recovery retry)
13275 * (other) unrecoverable error.
13278 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype
, nfs_argop4
*argop
,
13279 LOCKU4args
**locku_argsp
, flock64_t
*flk
,
13280 nfs4_lock_owner_t
**lopp
, nfs4_error_t
*ep
, COMPOUND4args_clnt
*argsp
,
13281 vnode_t
*vp
, int flag
, uoff_t offset
, cred_t
*cr
,
13282 bool_t
*skip_get_err
, bool_t
*go_otwp
)
13284 nfs4_lock_owner_t
*lop
= NULL
;
13285 LOCKU4args
*locku_args
;
13287 bool_t is_spec
= FALSE
;
13288 rnode4_t
*rp
= VTOR4(vp
);
13290 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13291 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13293 nfs4frlock_check_deleg(vp
, ep
, cr
, F_UNLCK
);
13294 if (ep
->error
|| ep
->stat
)
13297 argop
->argop
= OP_LOCKU
;
13298 if (ctype
== NFS4_LCK_CTYPE_REINSTATE
)
13299 argsp
->ctag
= TAG_LOCKU_REINSTATE
;
13301 argsp
->ctag
= TAG_LOCKU
;
13302 locku_args
= &argop
->nfs_argop4_u
.oplocku
;
13303 *locku_argsp
= locku_args
;
13305 /* locktype should be set to any legal value */
13306 locku_args
->locktype
= READ_LT
;
13308 pid
= ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pidp
->pid_id
:
13312 * Get the lock owner stateid. If no lock owner
13313 * exists, return success.
13315 lop
= find_lock_owner(rp
, pid
, LOWN_ANY
);
13317 if (lop
&& CLNT_ISSPECIAL(&lop
->lock_stateid
))
13319 if (!lop
|| is_spec
) {
13321 * No lock owner so no locks to unlock.
13322 * Return success. If there was a failed
13323 * reclaim earlier, the lock might still be
13324 * registered with the local locking code,
13325 * so notify it of the unlock.
13327 * If the lockowner is using a special stateid,
13328 * then the original lock request (that created
13329 * this lockowner) was never successful, so we
13330 * have no lock to undo OTW.
13332 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13333 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13334 "(%ld) so return success", (long)pid
));
13336 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13337 flk
->l_pid
= curproc
->p_pid
;
13338 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
13340 * Release our hold and NULL out so final_cleanup
13341 * doesn't try to end a lock seqid sync we
13345 lock_owner_rele(lop
);
13348 *skip_get_err
= TRUE
;
13353 ep
->error
= nfs4_start_lock_seqid_sync(lop
, VTOMI4(vp
));
13354 if (ep
->error
== EAGAIN
) {
13355 lock_owner_rele(lop
);
13360 mutex_enter(&lop
->lo_lock
);
13361 locku_args
->lock_stateid
= lop
->lock_stateid
;
13362 mutex_exit(&lop
->lo_lock
);
13363 locku_args
->seqid
= lop
->lock_seqid
+ 1;
13365 /* leave the ref count on lop, rele after RPC call */
13367 locku_args
->offset
= flk
->l_start
;
13368 locku_args
->length
= flk
->l_len
;
13369 if (flk
->l_len
== 0)
13370 locku_args
->length
= ~locku_args
->length
;
13376 * Setup the LOCK4 arguments.
13378 * Returns errors via the nfs4_error_t.
13379 * NFS4_OK no problems
13380 * NFS4ERR_DELAY caller should retry (like recovery retry)
13381 * (other) unrecoverable error
13384 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype
, LOCK4args
**lock_argsp
,
13385 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13386 nfs4_lock_owner_t
**lopp
, nfs_argop4
*argop
, COMPOUND4args_clnt
*argsp
,
13387 flock64_t
*flk
, int cmd
, vnode_t
*vp
, cred_t
*cr
, nfs4_error_t
*ep
)
13389 LOCK4args
*lock_args
;
13390 nfs4_open_owner_t
*oop
= NULL
;
13391 nfs4_open_stream_t
*osp
= NULL
;
13392 nfs4_lock_owner_t
*lop
= NULL
;
13394 rnode4_t
*rp
= VTOR4(vp
);
13396 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13398 nfs4frlock_check_deleg(vp
, ep
, cr
, flk
->l_type
);
13399 if (ep
->error
|| ep
->stat
!= NFS4_OK
)
13402 argop
->argop
= OP_LOCK
;
13403 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13404 argsp
->ctag
= TAG_LOCK
;
13405 else if (ctype
== NFS4_LCK_CTYPE_RECLAIM
)
13406 argsp
->ctag
= TAG_RELOCK
;
13408 argsp
->ctag
= TAG_LOCK_REINSTATE
;
13409 lock_args
= &argop
->nfs_argop4_u
.oplock
;
13410 lock_args
->locktype
= flk_to_locktype(cmd
, flk
->l_type
);
13411 lock_args
->reclaim
= ctype
== NFS4_LCK_CTYPE_RECLAIM
? 1 : 0;
13413 * Get the lock owner. If no lock owner exists,
13414 * create a 'temporary' one and grab the open seqid
13415 * synchronization (which puts a hold on the open
13416 * owner and open stream).
13417 * This also grabs the lock seqid synchronization.
13419 pid
= ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pid
: flk
->l_pid
;
13421 nfs4_find_or_create_lock_owner(pid
, rp
, cr
, &oop
, &osp
, &lop
);
13423 if (ep
->stat
!= NFS4_OK
)
13426 nfs4_setup_lock_args(lop
, oop
, osp
, mi2clientid(VTOMI4(vp
)),
13427 &lock_args
->locker
);
13429 lock_args
->offset
= flk
->l_start
;
13430 lock_args
->length
= flk
->l_len
;
13431 if (flk
->l_len
== 0)
13432 lock_args
->length
= ~lock_args
->length
;
13433 *lock_argsp
= lock_args
;
13441 * After we get the reply from the server, record the proper information
13442 * for possible resend lock requests.
13445 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype
, int error
,
13446 nfs_lock_type4 locktype
, nfs4_open_owner_t
*oop
,
13447 nfs4_open_stream_t
*osp
, nfs4_lock_owner_t
*lop
, flock64_t
*flk
,
13448 nfs4_lost_rqst_t
*lost_rqstp
, cred_t
*cr
, vnode_t
*vp
)
13450 bool_t unlock
= (flk
->l_type
== F_UNLCK
);
13452 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13453 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
||
13454 ctype
== NFS4_LCK_CTYPE_REINSTATE
);
13456 if (error
!= 0 && !unlock
) {
13457 NFS4_DEBUG((nfs4_lost_rqst_debug
||
13458 nfs4_client_lock_debug
), (CE_NOTE
,
13459 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13460 " for lop %p", (void *)lop
));
13461 ASSERT(lop
!= NULL
);
13462 mutex_enter(&lop
->lo_lock
);
13463 lop
->lo_pending_rqsts
= 1;
13464 mutex_exit(&lop
->lo_lock
);
13467 lost_rqstp
->lr_putfirst
= FALSE
;
13468 lost_rqstp
->lr_op
= 0;
13471 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13472 * recovery purposes so that the lock request that was sent
13473 * can be saved and re-issued later. Ditto for EIO from a forced
13474 * unmount. This is done to have the client's local locking state
13475 * match the v4 server's state; that is, the request was
13476 * potentially received and accepted by the server but the client
13477 * thinks it was not.
13479 if (error
== ETIMEDOUT
|| error
== EINTR
||
13480 NFS4_FRC_UNMT_ERR(error
, vp
->v_vfsp
)) {
13481 NFS4_DEBUG((nfs4_lost_rqst_debug
||
13482 nfs4_client_lock_debug
), (CE_NOTE
,
13483 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13484 "lop %p oop %p osp %p", unlock
? "LOCKU" : "LOCK",
13485 (void *)lop
, (void *)oop
, (void *)osp
));
13487 lost_rqstp
->lr_op
= OP_LOCKU
;
13489 lost_rqstp
->lr_op
= OP_LOCK
;
13490 lost_rqstp
->lr_locktype
= locktype
;
13493 * Objects are held and rele'd via the recovery code.
13494 * See nfs4_save_lost_rqst.
13496 lost_rqstp
->lr_vp
= vp
;
13497 lost_rqstp
->lr_dvp
= NULL
;
13498 lost_rqstp
->lr_oop
= oop
;
13499 lost_rqstp
->lr_osp
= osp
;
13500 lost_rqstp
->lr_lop
= lop
;
13501 lost_rqstp
->lr_cr
= cr
;
13503 case NFS4_LCK_CTYPE_NORM
:
13504 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
13505 lost_rqstp
->lr_ctype
= NFS4_LCK_CTYPE_RESEND
;
13507 case NFS4_LCK_CTYPE_REINSTATE
:
13508 lost_rqstp
->lr_putfirst
= TRUE
;
13509 lost_rqstp
->lr_ctype
= ctype
;
13514 lost_rqstp
->lr_flk
= flk
;
13519 * Update lop's seqid. Also update the seqid stored in a resend request,
13520 * if any. (Some recovery errors increment the seqid, and we may have to
13521 * send the resend request again.)
13525 nfs4frlock_bump_seqid(LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13526 nfs4_open_owner_t
*oop
, nfs4_lock_owner_t
*lop
, nfs4_tag_type_t tag_type
)
13529 if (lock_args
->locker
.new_lock_owner
== TRUE
)
13530 nfs4_get_and_set_next_open_seqid(oop
, tag_type
);
13532 ASSERT(lop
->lo_flags
& NFS4_LOCK_SEQID_INUSE
);
13533 nfs4_set_lock_seqid(lop
->lock_seqid
+ 1, lop
);
13535 } else if (locku_args
) {
13536 ASSERT(lop
->lo_flags
& NFS4_LOCK_SEQID_INUSE
);
13537 nfs4_set_lock_seqid(lop
->lock_seqid
+1, lop
);
13542 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13543 * COMPOUND4 args/res for calls that need to retry.
13544 * Switches the *cred_otwp to base_cr.
13547 nfs4frlock_check_access(vnode_t
*vp
, nfs4_op_hint_t op_hint
,
13548 nfs4_recov_state_t
*recov_statep
, int needrecov
, bool_t
*did_start_fop
,
13549 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
, int error
,
13550 nfs4_lock_owner_t
**lopp
, nfs4_open_owner_t
**oopp
,
13551 nfs4_open_stream_t
**ospp
, cred_t
*base_cr
, cred_t
**cred_otwp
)
13553 nfs4_open_owner_t
*oop
= *oopp
;
13554 nfs4_open_stream_t
*osp
= *ospp
;
13555 nfs4_lock_owner_t
*lop
= *lopp
;
13556 nfs_argop4
*argop
= (*argspp
)->array
;
13558 if (*did_start_fop
) {
13559 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
, recov_statep
,
13561 *did_start_fop
= FALSE
;
13563 ASSERT((*argspp
)->array_len
== 2);
13564 if (argop
[1].argop
== OP_LOCK
)
13565 nfs4args_lock_free(&argop
[1]);
13566 else if (argop
[1].argop
== OP_LOCKT
)
13567 nfs4args_lockt_free(&argop
[1]);
13568 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13570 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)*respp
);
13575 nfs4_end_lock_seqid_sync(lop
);
13576 lock_owner_rele(lop
);
13580 /* need to free up the reference on osp for lock args */
13582 open_stream_rele(osp
, VTOR4(vp
));
13586 /* need to free up the reference on oop for lock args */
13588 nfs4_end_open_seqid_sync(oop
);
13589 open_owner_rele(oop
);
13593 crfree(*cred_otwp
);
13594 *cred_otwp
= base_cr
;
13595 crhold(*cred_otwp
);
13599 * Function to process the client's recovery for nfs4frlock.
13600 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13602 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13603 * COMPOUND4 args/res for calls that need to retry.
13605 * Note: the rp's r_lkserlock is *not* dropped during this path.
13608 nfs4frlock_recovery(int needrecov
, nfs4_error_t
*ep
,
13609 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
,
13610 LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13611 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13612 nfs4_lock_owner_t
**lopp
, rnode4_t
*rp
, vnode_t
*vp
,
13613 nfs4_recov_state_t
*recov_statep
, nfs4_op_hint_t op_hint
,
13614 bool_t
*did_start_fop
, nfs4_lost_rqst_t
*lost_rqstp
, flock64_t
*flk
)
13616 nfs4_open_owner_t
*oop
= *oopp
;
13617 nfs4_open_stream_t
*osp
= *ospp
;
13618 nfs4_lock_owner_t
*lop
= *lopp
;
13620 bool_t abort
, retry
;
13622 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13623 ASSERT((*argspp
) != NULL
);
13624 ASSERT((*respp
) != NULL
);
13625 if (lock_args
|| locku_args
)
13626 ASSERT(lop
!= NULL
);
13628 NFS4_DEBUG((nfs4_client_lock_debug
|| nfs4_client_recov_debug
),
13629 (CE_NOTE
, "nfs4frlock_recovery: initiating recovery\n"));
13634 nfs4_bseqid_entry_t
*bsep
= NULL
;
13637 op
= lock_args
? OP_LOCK
: locku_args
? OP_LOCKU
: OP_LOCKT
;
13639 if (!ep
->error
&& ep
->stat
== NFS4ERR_BAD_SEQID
) {
13643 if (lock_args
->locker
.new_lock_owner
== TRUE
)
13644 seqid
= lock_args
->locker
.locker4_u
.
13645 open_owner
.open_seqid
;
13647 seqid
= lock_args
->locker
.locker4_u
.
13648 lock_owner
.lock_seqid
;
13649 } else if (locku_args
) {
13650 seqid
= locku_args
->seqid
;
13655 bsep
= nfs4_create_bseqid_entry(oop
, lop
, vp
,
13656 flk
->l_pid
, (*argspp
)->ctag
, seqid
);
13659 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
, NULL
,
13660 (lost_rqstp
&& (lost_rqstp
->lr_op
== OP_LOCK
||
13661 lost_rqstp
->lr_op
== OP_LOCKU
)) ? lost_rqstp
:
13662 NULL
, op
, bsep
, NULL
, NULL
);
13665 kmem_free(bsep
, sizeof (*bsep
));
13669 * Return that we do not want to retry the request for 3 cases:
13670 * 1. If we received EINTR or are bailing out because of a forced
13671 * unmount, we came into this code path just for the sake of
13672 * initiating recovery, we now need to return the error.
13673 * 2. If we have aborted recovery.
13674 * 3. We received NFS4ERR_BAD_SEQID.
13676 if (ep
->error
== EINTR
|| NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
) ||
13677 abort
== TRUE
|| (ep
->error
== 0 && ep
->stat
== NFS4ERR_BAD_SEQID
))
13680 if (*did_start_fop
== TRUE
) {
13681 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
, recov_statep
,
13683 *did_start_fop
= FALSE
;
13686 if (retry
== TRUE
) {
13689 argop
= (*argspp
)->array
;
13690 ASSERT((*argspp
)->array_len
== 2);
13692 if (argop
[1].argop
== OP_LOCK
)
13693 nfs4args_lock_free(&argop
[1]);
13694 else if (argop
[1].argop
== OP_LOCKT
)
13695 nfs4args_lockt_free(&argop
[1]);
13696 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13698 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)*respp
);
13704 nfs4_end_lock_seqid_sync(lop
);
13705 lock_owner_rele(lop
);
13710 /* need to free up the reference on osp for lock args */
13712 open_stream_rele(osp
, rp
);
13716 /* need to free up the reference on oop for lock args */
13718 nfs4_end_open_seqid_sync(oop
);
13719 open_owner_rele(oop
);
13727 * Handles the successful reply from the server for nfs4frlock.
13730 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype
, int cmd
, flock64_t
*flk
,
13731 vnode_t
*vp
, int flag
, uoff_t offset
,
13732 nfs4_lost_rqst_t
*resend_rqstp
)
13734 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13735 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) &&
13736 (flk
->l_type
== F_RDLCK
|| flk
->l_type
== F_WRLCK
)) {
13737 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
13738 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
13740 * We do not register lost locks locally in
13741 * the 'resend' case since the user/application
13742 * doesn't think we have the lock.
13744 ASSERT(!resend_rqstp
);
13745 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
13751 * Handle the DENIED reply from the server for nfs4frlock.
13752 * Returns TRUE if we should retry the request; FALSE otherwise.
13754 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13755 * COMPOUND4 args/res for calls that need to retry. Can also
13756 * drop and regrab the r_lkserlock.
13759 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype
, LOCK4args
*lock_args
,
13760 LOCKT4args
*lockt_args
, nfs4_open_owner_t
**oopp
,
13761 nfs4_open_stream_t
**ospp
, nfs4_lock_owner_t
**lopp
, int cmd
,
13762 vnode_t
*vp
, flock64_t
*flk
, nfs4_op_hint_t op_hint
,
13763 nfs4_recov_state_t
*recov_statep
, int needrecov
,
13764 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
,
13765 clock_t *tick_delayp
, short *whencep
, int *errorp
,
13766 nfs_resop4
*resop
, cred_t
*cr
, bool_t
*did_start_fop
,
13767 bool_t
*skip_get_err
)
13769 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13772 nfs4_open_owner_t
*oop
= *oopp
;
13773 nfs4_open_stream_t
*osp
= *ospp
;
13774 nfs4_lock_owner_t
*lop
= *lopp
;
13778 * Blocking lock needs to sleep and retry from the request.
13780 * Do not block and wait for 'resend' or 'reinstate'
13781 * lock requests, just return the error.
13783 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13785 if (cmd
== F_SETLKW
) {
13786 rnode4_t
*rp
= VTOR4(vp
);
13787 nfs_argop4
*argop
= (*argspp
)->array
;
13789 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13791 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
13792 recov_statep
, needrecov
);
13793 *did_start_fop
= FALSE
;
13794 ASSERT((*argspp
)->array_len
== 2);
13795 if (argop
[1].argop
== OP_LOCK
)
13796 nfs4args_lock_free(&argop
[1]);
13797 else if (argop
[1].argop
== OP_LOCKT
)
13798 nfs4args_lockt_free(&argop
[1]);
13799 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13801 xdr_free(xdr_COMPOUND4res_clnt
,
13805 nfs4_end_lock_seqid_sync(lop
);
13806 lock_owner_rele(lop
);
13809 open_stream_rele(osp
, rp
);
13813 nfs4_end_open_seqid_sync(oop
);
13814 open_owner_rele(oop
);
13818 nfs_rw_exit(&rp
->r_lkserlock
);
13820 intr
= nfs4_block_and_wait(tick_delayp
, rp
);
13823 (void) nfs_rw_enter_sig(&rp
->r_lkserlock
,
13829 (void) nfs_rw_enter_sig(&rp
->r_lkserlock
,
13833 * Make sure we are still safe to lock with
13834 * regards to mmapping.
13836 if (!nfs4_safelock(vp
, flk
, cr
)) {
13843 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13845 *skip_get_err
= TRUE
;
13849 } else if (lockt_args
) {
13850 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13851 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13853 denied_to_flk(&resop
->nfs_resop4_u
.oplockt
.denied
,
13856 /* according to NLM code */
13859 *skip_get_err
= TRUE
;
13866 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13869 nfs4frlock_results_default(COMPOUND4res_clnt
*resp
, int *errorp
)
13871 switch (resp
->status
) {
13872 case NFS4ERR_ACCESS
:
13873 case NFS4ERR_ADMIN_REVOKED
:
13874 case NFS4ERR_BADHANDLE
:
13875 case NFS4ERR_BAD_RANGE
:
13876 case NFS4ERR_BAD_SEQID
:
13877 case NFS4ERR_BAD_STATEID
:
13878 case NFS4ERR_BADXDR
:
13879 case NFS4ERR_DEADLOCK
:
13880 case NFS4ERR_DELAY
:
13881 case NFS4ERR_EXPIRED
:
13882 case NFS4ERR_FHEXPIRED
:
13883 case NFS4ERR_GRACE
:
13884 case NFS4ERR_INVAL
:
13885 case NFS4ERR_ISDIR
:
13886 case NFS4ERR_LEASE_MOVED
:
13887 case NFS4ERR_LOCK_NOTSUPP
:
13888 case NFS4ERR_LOCK_RANGE
:
13889 case NFS4ERR_MOVED
:
13890 case NFS4ERR_NOFILEHANDLE
:
13891 case NFS4ERR_NO_GRACE
:
13892 case NFS4ERR_OLD_STATEID
:
13893 case NFS4ERR_OPENMODE
:
13894 case NFS4ERR_RECLAIM_BAD
:
13895 case NFS4ERR_RECLAIM_CONFLICT
:
13896 case NFS4ERR_RESOURCE
:
13897 case NFS4ERR_SERVERFAULT
:
13898 case NFS4ERR_STALE
:
13899 case NFS4ERR_STALE_CLIENTID
:
13900 case NFS4ERR_STALE_STATEID
:
13903 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13904 "nfs4frlock_results_default: got unrecognizable "
13905 "res.status %d", resp
->status
));
13906 *errorp
= NFS4ERR_INVAL
;
13911 * The lock request was successful, so update the client's state.
13914 nfs4frlock_update_state(LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13915 LOCKT4args
*lockt_args
, nfs_resop4
*resop
, nfs4_lock_owner_t
*lop
,
13916 vnode_t
*vp
, flock64_t
*flk
, cred_t
*cr
,
13917 nfs4_lost_rqst_t
*resend_rqstp
)
13919 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13922 LOCK4res
*lock_res
;
13924 lock_res
= &resop
->nfs_resop4_u
.oplock
;
13925 /* update the stateid with server's response */
13927 if (lock_args
->locker
.new_lock_owner
== TRUE
) {
13928 mutex_enter(&lop
->lo_lock
);
13929 lop
->lo_just_created
= NFS4_PERM_CREATED
;
13930 mutex_exit(&lop
->lo_lock
);
13933 nfs4_set_lock_stateid(lop
, lock_res
->LOCK4res_u
.lock_stateid
);
13936 * If the lock was the result of a resending a lost
13937 * request, we've synched up the stateid and seqid
13938 * with the server, but now the server might be out of sync
13939 * with what the application thinks it has for locks.
13940 * Clean that up here. It's unclear whether we should do
13941 * this even if the filesystem has been forcibly unmounted.
13942 * For most servers, it's probably wasted effort, but
13943 * RFC 7530 lets servers require that unlocks exactly match
13944 * the locks that are held.
13946 if (resend_rqstp
!= NULL
&&
13947 resend_rqstp
->lr_ctype
!= NFS4_LCK_CTYPE_REINSTATE
) {
13948 nfs4_reinstitute_local_lock_state(vp
, flk
, cr
, lop
);
13952 } else if (locku_args
) {
13953 LOCKU4res
*locku_res
;
13955 locku_res
= &resop
->nfs_resop4_u
.oplocku
;
13957 /* Update the stateid with the server's response */
13958 nfs4_set_lock_stateid(lop
, locku_res
->lock_stateid
);
13959 } else if (lockt_args
) {
13960 /* Switch the lock type to express success, see fcntl */
13961 flk
->l_type
= F_UNLCK
;
13967 * Do final cleanup before exiting nfs4frlock.
13968 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13969 * COMPOUND4 args/res for calls that haven't already.
13972 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype
, COMPOUND4args_clnt
*argsp
,
13973 COMPOUND4res_clnt
*resp
, vnode_t
*vp
, nfs4_op_hint_t op_hint
,
13974 nfs4_recov_state_t
*recov_statep
, int needrecov
, nfs4_open_owner_t
*oop
,
13975 nfs4_open_stream_t
*osp
, nfs4_lock_owner_t
*lop
, flock64_t
*flk
,
13976 short whence
, uoff_t offset
, struct lm_sysid
*ls
,
13977 int *errorp
, LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13978 bool_t did_start_fop
, bool_t skip_get_err
,
13979 cred_t
*cred_otw
, cred_t
*cred
)
13981 mntinfo4_t
*mi
= VTOMI4(vp
);
13982 rnode4_t
*rp
= VTOR4(vp
);
13983 int error
= *errorp
;
13985 int do_flush_pages
= 0;
13987 ASSERT(nfs_zone() == mi
->mi_zone
);
13989 * The client recovery code wants the raw status information,
13990 * so don't map the NFS status code to an errno value for
13991 * non-normal call types.
13993 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
13994 if (*errorp
== 0 && resp
!= NULL
&& skip_get_err
== FALSE
)
13995 *errorp
= geterrno4(resp
->status
);
13996 if (did_start_fop
== TRUE
)
13997 nfs4_end_fop(mi
, vp
, NULL
, op_hint
, recov_statep
,
14001 * We've established a new lock on the server, so invalidate
14002 * the pages associated with the vnode to get the most up to
14003 * date pages from the server after acquiring the lock. We
14004 * want to be sure that the read operation gets the newest data.
14006 * We used to do this in nfs4frlock_results_ok but that doesn't
14007 * work since fop_putpage can call nfs4_commit which calls
14008 * nfs4_start_fop. We flush the pages below after calling
14009 * nfs4_end_fop above
14010 * The flush of the page cache must be done after
14011 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14013 if (!error
&& resp
&& resp
->status
== NFS4_OK
)
14014 do_flush_pages
= 1;
14017 ASSERT(argsp
->array_len
== 2);
14018 argop
= argsp
->array
;
14019 if (argop
[1].argop
== OP_LOCK
)
14020 nfs4args_lock_free(&argop
[1]);
14021 else if (argop
[1].argop
== OP_LOCKT
)
14022 nfs4args_lockt_free(&argop
[1]);
14023 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
14025 xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
14028 /* free the reference on the lock owner */
14030 nfs4_end_lock_seqid_sync(lop
);
14031 lock_owner_rele(lop
);
14034 /* need to free up the reference on osp for lock args */
14036 open_stream_rele(osp
, rp
);
14038 /* need to free up the reference on oop for lock args */
14040 nfs4_end_open_seqid_sync(oop
);
14041 open_owner_rele(oop
);
14044 if (do_flush_pages
)
14045 nfs4_flush_pages(vp
, cred
);
14047 (void) convoff(vp
, flk
, whence
, offset
);
14052 * Record debug information in the event we get EINVAL.
14054 mutex_enter(&mi
->mi_lock
);
14055 if (*errorp
== EINVAL
&& (lock_args
|| locku_args
) &&
14056 (!(mi
->mi_flags
& MI4_POSIX_LOCK
))) {
14057 if (!(mi
->mi_flags
& MI4_LOCK_DEBUG
)) {
14058 zcmn_err(getzoneid(), CE_NOTE
,
14059 "%s operation failed with "
14060 "EINVAL probably since the server, %s,"
14061 " doesn't support POSIX style locking",
14062 lock_args
? "LOCK" : "LOCKU",
14063 mi
->mi_curr_serv
->sv_hostname
);
14064 mi
->mi_flags
|= MI4_LOCK_DEBUG
;
14067 mutex_exit(&mi
->mi_lock
);
14074 * This calls the server and the local locking code.
14076 * Client locks are registerred locally by oring the sysid with
14077 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14078 * We need to distinguish between the two to avoid collision in case one
14079 * machine is used as both client and server.
14081 * Blocking lock requests will continually retry to acquire the lock
14084 * The ctype is defined as follows:
14085 * NFS4_LCK_CTYPE_NORM: normal lock request.
14087 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14088 * recovery, get the pid from flk instead of curproc, and don't reregister
14089 * the lock locally.
14091 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14092 * that we will use the information passed in via resend_rqstp to setup the
14093 * lock/locku request. This resend is the exact same request as the 'lost
14094 * lock', and is initiated by the recovery framework. A successful resend
14095 * request can initiate one or more reinstate requests.
14097 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14098 * does not trigger additional reinstate requests. This lock call type is
14099 * set for setting the v4 server's locking state back to match what the
14100 * client's local locking state is in the event of a received 'lost lock'.
14102 * Errors are returned via the nfs4_error_t parameter.
14105 nfs4frlock(nfs4_lock_call_type_t ctype
, vnode_t
*vp
, int cmd
, flock64_t
*flk
,
14106 int flag
, uoff_t offset
, cred_t
*cr
, nfs4_error_t
*ep
,
14107 nfs4_lost_rqst_t
*resend_rqstp
, int *did_reclaimp
)
14109 COMPOUND4args_clnt args
, *argsp
= NULL
;
14110 COMPOUND4res_clnt res
, *resp
= NULL
;
14115 clock_t tick_delay
; /* delay in clock ticks */
14116 struct lm_sysid
*ls
;
14117 LOCK4args
*lock_args
= NULL
;
14118 LOCKU4args
*locku_args
= NULL
;
14119 LOCKT4args
*lockt_args
= NULL
;
14120 nfs4_open_owner_t
*oop
= NULL
;
14121 nfs4_open_stream_t
*osp
= NULL
;
14122 nfs4_lock_owner_t
*lop
= NULL
;
14123 bool_t needrecov
= FALSE
;
14124 nfs4_recov_state_t recov_state
;
14126 nfs4_op_hint_t op_hint
;
14127 nfs4_lost_rqst_t lost_rqst
;
14128 bool_t retry
= FALSE
;
14129 bool_t did_start_fop
= FALSE
;
14130 bool_t skip_get_err
= FALSE
;
14131 cred_t
*cred_otw
= NULL
;
14132 bool_t recovonly
; /* just queue request */
14133 int frc_no_reclaim
= 0;
14138 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14141 name
= fn_name(VTOSV(vp
)->sv_name
);
14142 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4frlock: "
14143 "%s: cmd %d, type %d, offset %llu, start %"PRIx64
", "
14144 "length %"PRIu64
", pid %d, sysid %d, call type %s, "
14145 "resend request %s", name
, cmd
, flk
->l_type
, offset
, flk
->l_start
,
14146 flk
->l_len
, ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pid
:
14147 flk
->l_pid
, flk
->l_sysid
, nfs4frlock_get_call_type(ctype
),
14148 resend_rqstp
? "TRUE" : "FALSE"));
14149 kmem_free(name
, MAXNAMELEN
);
14152 nfs4_error_zinit(ep
);
14153 ep
->error
= nfs4frlock_validate_args(cmd
, flk
, flag
, vp
, offset
);
14156 ep
->error
= nfs4frlock_get_sysid(&ls
, vp
, flk
);
14159 nfs4frlock_pre_setup(&tick_delay
, &recov_state
, flk
, &whence
,
14160 vp
, cr
, &cred_otw
);
14163 nfs4frlock_call_init(&args
, &argsp
, &argop
, &op_hint
, flk
, cmd
,
14164 &retry
, &did_start_fop
, &resp
, &skip_get_err
, &lost_rqst
);
14167 ep
->error
= nfs4frlock_start_call(ctype
, vp
, op_hint
, &recov_state
,
14168 &did_start_fop
, &recovonly
);
14175 * Leave the request for the recovery system to deal with.
14177 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
14178 ASSERT(cmd
!= F_GETLK
);
14179 ASSERT(flk
->l_type
== F_UNLCK
);
14181 nfs4_error_init(ep
, EINTR
);
14183 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14185 nfs4frlock_save_lost_rqst(ctype
, ep
->error
, READ_LT
,
14186 NULL
, NULL
, lop
, flk
, &lost_rqst
, cr
, vp
);
14187 (void) nfs4_start_recovery(ep
,
14188 VTOMI4(vp
), vp
, NULL
, NULL
,
14189 (lost_rqst
.lr_op
== OP_LOCK
||
14190 lost_rqst
.lr_op
== OP_LOCKU
) ?
14191 &lost_rqst
: NULL
, OP_LOCKU
, NULL
, NULL
, NULL
);
14192 lock_owner_rele(lop
);
14195 flk
->l_pid
= curproc
->p_pid
;
14196 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
14200 /* putfh directory fh */
14201 argop
[0].argop
= OP_CPUTFH
;
14202 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
14205 * Set up the over-the-wire arguments and get references to the
14209 if (ctype
== NFS4_LCK_CTYPE_RESEND
||
14210 ctype
== NFS4_LCK_CTYPE_REINSTATE
) {
14211 nfs4frlock_setup_resend_lock_args(resend_rqstp
, argsp
,
14212 &argop
[1], &lop
, &oop
, &osp
, &lock_args
, &locku_args
);
14214 bool_t go_otw
= TRUE
;
14216 ASSERT(resend_rqstp
== NULL
);
14220 nfs4frlock_setup_lockt_args(ctype
, &argop
[1],
14221 &lockt_args
, argsp
, flk
, rp
);
14225 if (flk
->l_type
== F_UNLCK
)
14226 nfs4frlock_setup_locku_args(ctype
,
14227 &argop
[1], &locku_args
, flk
,
14229 vp
, flag
, offset
, cr
,
14230 &skip_get_err
, &go_otw
);
14232 nfs4frlock_setup_lock_args(ctype
,
14233 &lock_args
, &oop
, &osp
, &lop
, &argop
[1],
14234 argsp
, flk
, cmd
, vp
, cr
, ep
);
14239 switch (ep
->stat
) {
14242 case NFS4ERR_DELAY
:
14243 /* recov thread never gets this error */
14244 ASSERT(resend_rqstp
== NULL
);
14245 ASSERT(did_start_fop
);
14247 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
14248 &recov_state
, TRUE
);
14249 did_start_fop
= FALSE
;
14250 if (argop
[1].argop
== OP_LOCK
)
14251 nfs4args_lock_free(&argop
[1]);
14252 else if (argop
[1].argop
== OP_LOCKT
)
14253 nfs4args_lockt_free(&argop
[1]);
14254 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
14263 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14264 "nfs4_frlock: invalid cmd %d", cmd
));
14265 ep
->error
= EINVAL
;
14273 /* XXX should we use the local reclock as a cache ? */
14275 * Unregister the lock with the local locking code before
14276 * contacting the server. This avoids a potential race where
14277 * another process gets notified that it has been granted a lock
14278 * before we can unregister ourselves locally.
14280 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) && flk
->l_type
== F_UNLCK
) {
14281 if (ctype
== NFS4_LCK_CTYPE_NORM
)
14282 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
14283 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
14287 * Send the server the lock request. Continually loop with a delay
14288 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14292 NFS4_DEBUG((nfs4_client_call_debug
|| nfs4_client_lock_debug
),
14294 "nfs4frlock: %s call, rp %s", needrecov
? "recov" : "first",
14297 if (lock_args
&& frc_no_reclaim
) {
14298 ASSERT(ctype
== NFS4_LCK_CTYPE_RECLAIM
);
14299 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14300 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14301 lock_args
->reclaim
= FALSE
;
14309 rfs4call(VTOMI4(vp
), argsp
, resp
, cred_otw
, &doqueue
, 0, ep
);
14311 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14312 "nfs4frlock: error %d, status %d", ep
->error
, resp
->status
));
14314 needrecov
= nfs4_needs_recovery(ep
, TRUE
, vp
->v_vfsp
);
14315 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14316 "nfs4frlock: needrecov %d", needrecov
));
14318 if (ep
->error
== 0 && nfs4_need_to_bump_seqid(resp
))
14319 nfs4frlock_bump_seqid(lock_args
, locku_args
, oop
, lop
,
14323 * Check if one of these mutually exclusive error cases has
14325 * need to swap credentials due to access error
14326 * recovery is needed
14327 * different error (only known case is missing Kerberos ticket)
14330 if ((ep
->error
== EACCES
||
14331 (ep
->error
== 0 && resp
->status
== NFS4ERR_ACCESS
)) &&
14333 nfs4frlock_check_access(vp
, op_hint
, &recov_state
, needrecov
,
14334 &did_start_fop
, &argsp
, &resp
, ep
->error
, &lop
, &oop
, &osp
,
14341 * LOCKT requests don't need to recover from lost
14342 * requests since they don't create/modify state.
14344 if ((ep
->error
== EINTR
||
14345 NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
)) &&
14349 * Do not attempt recovery for requests initiated by
14350 * the recovery framework. Let the framework redrive them.
14352 if (ctype
!= NFS4_LCK_CTYPE_NORM
)
14355 ASSERT(resend_rqstp
== NULL
);
14358 nfs4frlock_save_lost_rqst(ctype
, ep
->error
,
14359 flk_to_locktype(cmd
, flk
->l_type
),
14360 oop
, osp
, lop
, flk
, &lost_rqst
, cred_otw
, vp
);
14362 retry
= nfs4frlock_recovery(needrecov
, ep
, &argsp
,
14363 &resp
, lock_args
, locku_args
, &oop
, &osp
, &lop
,
14364 rp
, vp
, &recov_state
, op_hint
, &did_start_fop
,
14365 cmd
!= F_GETLK
? &lost_rqst
: NULL
, flk
);
14368 ASSERT(oop
== NULL
);
14369 ASSERT(osp
== NULL
);
14370 ASSERT(lop
== NULL
);
14377 * Bail out if have reached this point with ep->error set. Can
14378 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14379 * This happens if Kerberos ticket has expired or has been
14382 if (ep
->error
!= 0)
14386 * Process the reply.
14388 switch (resp
->status
) {
14390 resop
= &resp
->array
[1];
14391 nfs4frlock_results_ok(ctype
, cmd
, flk
, vp
, flag
, offset
,
14394 * Have a successful lock operation, now update state.
14396 nfs4frlock_update_state(lock_args
, locku_args
, lockt_args
,
14397 resop
, lop
, vp
, flk
, cr
, resend_rqstp
);
14400 case NFS4ERR_DENIED
:
14401 resop
= &resp
->array
[1];
14402 retry
= nfs4frlock_results_denied(ctype
, lock_args
, lockt_args
,
14403 &oop
, &osp
, &lop
, cmd
, vp
, flk
, op_hint
,
14404 &recov_state
, needrecov
, &argsp
, &resp
,
14405 &tick_delay
, &whence
, &ep
->error
, resop
, cr
,
14406 &did_start_fop
, &skip_get_err
);
14409 ASSERT(oop
== NULL
);
14410 ASSERT(osp
== NULL
);
14411 ASSERT(lop
== NULL
);
14416 * If the server won't let us reclaim, fall-back to trying to lock
14417 * the file from scratch. Code elsewhere will check the changeinfo
14418 * to ensure the file hasn't been changed.
14420 case NFS4ERR_NO_GRACE
:
14421 if (lock_args
&& lock_args
->reclaim
== TRUE
) {
14422 ASSERT(ctype
== NFS4_LCK_CTYPE_RECLAIM
);
14423 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14424 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14425 frc_no_reclaim
= 1;
14426 /* clean up before retrying */
14428 (void) nfs4frlock_recovery(needrecov
, ep
, &argsp
, &resp
,
14429 lock_args
, locku_args
, &oop
, &osp
, &lop
, rp
, vp
,
14430 &recov_state
, op_hint
, &did_start_fop
, NULL
, flk
);
14436 nfs4frlock_results_default(resp
, &ep
->error
);
14441 * Process and cleanup from error. Make interrupted unlock
14442 * requests look successful, since they will be handled by the
14443 * client recovery code.
14445 nfs4frlock_final_cleanup(ctype
, argsp
, resp
, vp
, op_hint
, &recov_state
,
14446 needrecov
, oop
, osp
, lop
, flk
, whence
, offset
, ls
, &ep
->error
,
14447 lock_args
, locku_args
, did_start_fop
,
14448 skip_get_err
, cred_otw
, cr
);
14450 if (ep
->error
== EINTR
&& flk
->l_type
== F_UNLCK
&&
14451 (cmd
== F_SETLK
|| cmd
== F_SETLKW
))
14458 * Return non-zero if the given lock request can be handled without
14459 * violating the constraints on concurrent mapping and locking.
14463 nfs4_safelock(vnode_t
*vp
, const struct flock64
*bfp
, cred_t
*cr
)
14465 rnode4_t
*rp
= VTOR4(vp
);
14469 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14470 ASSERT(rp
->r_mapcnt
>= 0);
14471 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock %s: "
14472 "(%"PRIx64
", %"PRIx64
"); mapcnt = %ld", bfp
->l_type
== F_WRLCK
?
14473 "write" : bfp
->l_type
== F_RDLCK
? "read" : "unlock",
14474 bfp
->l_start
, bfp
->l_len
, rp
->r_mapcnt
));
14476 if (rp
->r_mapcnt
== 0)
14477 return (1); /* always safe if not mapped */
14480 * If the file is already mapped and there are locks, then they
14481 * should be all safe locks. So adding or removing a lock is safe
14482 * as long as the new request is safe (i.e., whole-file, meaning
14483 * length and starting offset are both zero).
14486 if (bfp
->l_start
!= 0 || bfp
->l_len
!= 0) {
14487 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14488 "cannot lock a memory mapped file unless locking the "
14489 "entire file: start %"PRIx64
", len %"PRIx64
,
14490 bfp
->l_start
, bfp
->l_len
));
14494 /* mandatory locking and mapping don't mix */
14495 va
.va_mask
= VATTR_MODE
;
14496 error
= fop_getattr(vp
, &va
, 0, cr
, NULL
);
14498 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14499 "getattr error %d", error
));
14500 return (0); /* treat errors conservatively */
14502 if (MANDLOCK(vp
, va
.va_mode
)) {
14503 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14504 "cannot mandatory lock and mmap a file"));
14513 * Register the lock locally within Solaris.
14514 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14515 * recording locks locally.
14517 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14518 * are registered locally.
14521 nfs4_register_lock_locally(vnode_t
*vp
, struct flock64
*flk
, int flag
,
14530 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14533 name
= fn_name(VTOSV(vp
)->sv_name
);
14534 NFS4_DEBUG(nfs4_client_lock_debug
,
14535 (CE_NOTE
, "nfs4_register_lock_locally: %s: type %d, "
14536 "start %"PRIx64
", length %"PRIx64
", pid %ld, sysid %d",
14537 name
, flk
->l_type
, flk
->l_start
, flk
->l_len
, (long)flk
->l_pid
,
14539 kmem_free(name
, MAXNAMELEN
);
14542 /* register the lock with local locking */
14543 oldsysid
= flk
->l_sysid
;
14544 flk
->l_sysid
|= LM_SYSID_CLIENT
;
14545 error
= reclock(vp
, flk
, SETFLCK
, flag
, offset
, NULL
);
14548 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14549 "nfs4_register_lock_locally: could not register with"
14550 " local locking"));
14551 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14552 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14553 error
, (void *)vp
, flk
->l_pid
, flk
->l_sysid
));
14554 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14555 "type %d off 0x%" PRIx64
" len 0x%" PRIx64
,
14556 flk
->l_type
, flk
->l_start
, flk
->l_len
));
14557 (void) reclock(vp
, flk
, 0, flag
, offset
, NULL
);
14558 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14559 "blocked by pid %d sysid 0x%x type %d "
14560 "off 0x%" PRIx64
" len 0x%" PRIx64
,
14561 flk
->l_pid
, flk
->l_sysid
, flk
->l_type
, flk
->l_start
,
14565 flk
->l_sysid
= oldsysid
;
14569 * nfs4_lockrelease:
14571 * Release any locks on the given vnode that are held by the current
14572 * process. Also removes the lock owner (if one exists) from the rnode's
14576 nfs4_lockrelease(vnode_t
*vp
, int flag
, offset_t offset
, cred_t
*cr
)
14581 nfs4_lock_owner_t
*lop
;
14582 nfs4_recov_state_t recov_state
;
14584 bool_t possible_orphan
= FALSE
;
14587 ASSERT((uintptr_t)vp
> KERNELBASE
);
14588 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14594 * If we have not locked anything then we can
14595 * just return since we have no work to do.
14597 if (rp
->r_lo_head
.lo_next_rnode
== &rp
->r_lo_head
) {
14602 * We need to comprehend that another thread may
14603 * kick off recovery and the lock_owner we have stashed
14604 * in lop might be invalid so we should NOT cache it
14607 recov_state
.rs_flags
= 0;
14608 recov_state
.rs_num_retry_despite_err
= 0;
14609 error
= nfs4_start_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
,
14612 mutex_enter(&rp
->r_statelock
);
14613 rp
->r_flags
|= R4LODANGLERS
;
14614 mutex_exit(&rp
->r_statelock
);
14618 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14621 * Check if the lock owner might have a lock (request was sent but
14622 * no response was received). Also check if there are any remote
14623 * locks on the file. (In theory we shouldn't have to make this
14624 * second check if there's no lock owner, but for now we'll be
14625 * conservative and do it anyway.) If either condition is true,
14626 * send an unlock for the entire file to the server.
14628 * Note that no explicit synchronization is needed here. At worst,
14629 * flk_has_remote_locks() will return a false positive, in which case
14630 * the unlock call wastes time but doesn't harm correctness.
14634 mutex_enter(&lop
->lo_lock
);
14635 possible_orphan
= lop
->lo_pending_rqsts
;
14636 mutex_exit(&lop
->lo_lock
);
14637 lock_owner_rele(lop
);
14640 nfs4_end_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
, 0);
14642 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14643 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14644 "lop %p.", possible_orphan
, flk_has_remote_locks(vp
),
14647 if (possible_orphan
|| flk_has_remote_locks(vp
)) {
14648 ld
.l_type
= F_UNLCK
; /* set to unlock entire file */
14649 ld
.l_whence
= 0; /* unlock from start of file */
14651 ld
.l_len
= 0; /* do entire file */
14653 ret
= fop_frlock(vp
, F_SETLK
, &ld
, flag
, offset
, NULL
,
14658 * If fop_frlock fails, make sure we unregister
14659 * local locks before we continue.
14661 ld
.l_pid
= ttoproc(curthread
)->p_pid
;
14662 nfs4_register_lock_locally(vp
, &ld
, flag
, offset
);
14663 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14664 "nfs4_lockrelease: lock release error on vp"
14665 " %p: error %d.\n", (void *)vp
, ret
));
14669 recov_state
.rs_flags
= 0;
14670 recov_state
.rs_num_retry_despite_err
= 0;
14671 error
= nfs4_start_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
,
14674 mutex_enter(&rp
->r_statelock
);
14675 rp
->r_flags
|= R4LODANGLERS
;
14676 mutex_exit(&rp
->r_statelock
);
14681 * So, here we're going to need to retrieve the lock-owner
14682 * again (in case recovery has done a switch-a-roo) and
14683 * remove it because we can.
14685 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14688 nfs4_rnode_remove_lock_owner(rp
, lop
);
14689 lock_owner_rele(lop
);
14692 nfs4_end_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
, 0);
14697 * Wait for 'tick_delay' clock ticks.
14698 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14699 * NOTE: lock_lease_time is in seconds.
14701 * XXX For future improvements, should implement a waiting queue scheme.
14704 nfs4_block_and_wait(clock_t *tick_delay
, rnode4_t
*rp
)
14706 long milliseconds_delay
;
14707 time_t lock_lease_time
;
14709 /* wait tick_delay clock ticks or siginteruptus */
14710 if (delay_sig(*tick_delay
)) {
14713 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_block_and_wait: "
14714 "reissue the lock request: blocked for %ld clock ticks: %ld "
14715 "milliseconds", *tick_delay
, drv_hztousec(*tick_delay
) / 1000));
14717 /* get the lease time */
14718 lock_lease_time
= r2lease_time(rp
);
14720 /* drv_hztousec converts ticks to microseconds */
14721 milliseconds_delay
= drv_hztousec(*tick_delay
) / 1000;
14722 if (milliseconds_delay
< lock_lease_time
* 1000) {
14723 *tick_delay
= 2 * *tick_delay
;
14724 if (drv_hztousec(*tick_delay
) > lock_lease_time
* 1000 * 1000)
14725 *tick_delay
= drv_usectohz(lock_lease_time
*1000*1000);
14732 nfs4_vnops_init(void)
14737 nfs4_vnops_fini(void)
14742 * Return a reference to the directory (parent) vnode for a given vnode,
14743 * using the saved pathname information and the directory file handle. The
14744 * caller is responsible for disposing of the reference.
14745 * Returns zero or an errno value.
14747 * Caller should set need_start_op to FALSE if it is the recovery
14748 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14751 vtodv(vnode_t
*vp
, vnode_t
**dvpp
, cred_t
*cr
, bool_t need_start_op
)
14754 vnode_t
*dvp
= NULL
;
14756 nfs4_fname_t
*mfname
;
14759 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14761 if (vp
->v_flag
& VROOT
) {
14762 nfs4_sharedfh_t
*sfh
;
14766 ASSERT(vp
->v_type
== VREG
);
14769 svp
= mi
->mi_curr_serv
;
14770 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
14771 fh
.nfs_fh4_len
= svp
->sv_pfhandle
.fh_len
;
14772 fh
.nfs_fh4_val
= svp
->sv_pfhandle
.fh_buf
;
14773 sfh
= sfh4_get(&fh
, VTOMI4(vp
));
14774 nfs_rw_exit(&svp
->sv_lock
);
14775 mfname
= mi
->mi_fname
;
14777 dvp
= makenfs4node_by_fh(sfh
, NULL
, &mfname
, NULL
, mi
, cr
, 0);
14780 if (dvp
->v_type
== VNON
)
14781 dvp
->v_type
= VDIR
;
14788 if (svnp
== NULL
) {
14789 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14790 "shadow node is NULL"));
14794 if (svnp
->sv_name
== NULL
|| svnp
->sv_dfh
== NULL
) {
14795 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14796 "shadow node name or dfh val == NULL"));
14800 error
= nfs4_make_dotdot(svnp
->sv_dfh
, 0, vp
, cr
, &dvp
,
14801 (int)need_start_op
);
14803 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14804 "nfs4_make_dotdot returned %d", error
));
14808 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14809 "nfs4_make_dotdot returned a NULL dvp"));
14812 if (dvp
->v_type
== VNON
)
14813 dvp
->v_type
= VDIR
;
14814 ASSERT(dvp
->v_type
== VDIR
);
14815 if (VTOR4(vp
)->r_flags
& R4ISXATTR
) {
14816 mutex_enter(&dvp
->v_lock
);
14817 dvp
->v_flag
|= V_XATTRDIR
;
14818 mutex_exit(&dvp
->v_lock
);
14825 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14826 * length that fnamep can accept, including the trailing null.
14827 * Returns 0 if okay, returns an errno value if there was a problem.
14831 vtoname(vnode_t
*vp
, char *fnamep
, ssize_t maxlen
)
14839 * If the file being opened has VROOT set, then this is
14840 * a "file" mount. sv_name will not be interesting, so
14841 * go back to the servinfo4 to get the original mount
14842 * path and strip off all but the final edge. Otherwise
14843 * just return the name from the shadow vnode.
14846 if (vp
->v_flag
& VROOT
) {
14848 svp
= VTOMI4(vp
)->mi_curr_serv
;
14849 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
14851 fn
= strrchr(svp
->sv_path
, '/');
14858 fn
= fn_name(shvp
->sv_name
);
14862 if (strlen(fn
) < maxlen
)
14863 (void) strcpy(fnamep
, fn
);
14865 err
= ENAMETOOLONG
;
14867 if (vp
->v_flag
& VROOT
)
14868 nfs_rw_exit(&svp
->sv_lock
);
14870 kmem_free(fn
, MAXNAMELEN
);
14876 * Bookkeeping for a close that doesn't need to go over the wire.
14877 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14881 nfs4close_notw(vnode_t
*vp
, nfs4_open_stream_t
*osp
, int *have_lockp
)
14889 NFS4_DEBUG(nfs4close_notw_debug
, (CE_NOTE
, "nfs4close_notw: "
14890 "rp=%p osp=%p", (void *)rp
, (void *)osp
));
14891 ASSERT(nfs_zone() == mi
->mi_zone
);
14892 ASSERT(mutex_owned(&osp
->os_sync_lock
));
14893 ASSERT(*have_lockp
);
14895 if (!osp
->os_valid
||
14896 osp
->os_open_ref_count
> 0 || osp
->os_mapcnt
> 0) {
14901 * This removes the reference obtained at OPEN; ie,
14902 * when the open stream structure was created.
14904 * We don't have to worry about calling 'open_stream_rele'
14905 * since we our currently holding a reference to this
14906 * open stream which means the count can not go to 0 with
14909 ASSERT(osp
->os_ref_count
>= 2);
14910 osp
->os_ref_count
--;
14912 mutex_exit(&osp
->os_sync_lock
);
14915 nfs4_dec_state_ref_count(mi
);
14919 * Close all remaining open streams on the rnode. These open streams
14920 * could be here because:
14921 * - The close attempted at either close or delmap failed
14922 * - Some kernel entity did fop_open but never did fop_close
14923 * - Someone did mknod on a regular file but never opened it
14926 nfs4close_all(vnode_t
*vp
, cred_t
*cr
)
14928 nfs4_open_stream_t
*osp
;
14930 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
14933 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14939 * At this point, all we know is that the last time
14940 * someone called vn_rele, the count was 1. Since then,
14941 * the vnode could have been re-activated. We want to
14942 * loop through the open streams and close each one, but
14943 * we have to be careful since once we release the rnode
14944 * hash bucket lock, someone else is free to come in and
14945 * re-activate the rnode and add new open streams. The
14946 * strategy is take the rnode hash bucket lock, verify that
14947 * the count is still 1, grab the open stream off the
14948 * head of the list and mark it invalid, then release the
14949 * rnode hash bucket lock and proceed with that open stream.
14950 * This is ok because nfs4close_one() will acquire the proper
14951 * open/create to close/destroy synchronization for open
14952 * streams, and will ensure that if someone has reopened
14953 * the open stream after we've dropped the hash bucket lock
14954 * then we'll just simply return without destroying the
14956 * Repeat until the list is empty.
14961 /* make sure vnode hasn't been reactivated */
14962 rw_enter(&rp
->r_hashq
->r_lock
, RW_READER
);
14963 mutex_enter(&vp
->v_lock
);
14964 if (vp
->v_count
> 1) {
14965 mutex_exit(&vp
->v_lock
);
14966 rw_exit(&rp
->r_hashq
->r_lock
);
14970 * Grabbing r_os_lock before releasing v_lock prevents
14971 * a window where the rnode/open stream could get
14972 * reactivated (and os_force_close set to 0) before we
14973 * had a chance to set os_force_close to 1.
14975 mutex_enter(&rp
->r_os_lock
);
14976 mutex_exit(&vp
->v_lock
);
14978 osp
= list_head(&rp
->r_open_streams
);
14980 /* nothing left to CLOSE OTW, so return */
14981 mutex_exit(&rp
->r_os_lock
);
14982 rw_exit(&rp
->r_hashq
->r_lock
);
14986 mutex_enter(&rp
->r_statev4_lock
);
14987 /* the file can't still be mem mapped */
14988 ASSERT(rp
->r_mapcnt
== 0);
14989 if (rp
->created_v4
)
14990 rp
->created_v4
= 0;
14991 mutex_exit(&rp
->r_statev4_lock
);
14994 * Grab a ref on this open stream; nfs4close_one
14995 * will mark it as invalid
14997 mutex_enter(&osp
->os_sync_lock
);
14998 osp
->os_ref_count
++;
14999 osp
->os_force_close
= 1;
15000 mutex_exit(&osp
->os_sync_lock
);
15001 mutex_exit(&rp
->r_os_lock
);
15002 rw_exit(&rp
->r_hashq
->r_lock
);
15004 nfs4close_one(vp
, osp
, cr
, 0, NULL
, &e
, CLOSE_FORCE
, 0, 0, 0);
15006 /* Update error if it isn't already non-zero */
15011 error
= geterrno4(e
.stat
);
15015 nfs4close_all_cnt
++;
15017 /* Release the ref on osp acquired above. */
15018 open_stream_rele(osp
, rp
);
15020 /* Proceed to the next open stream, if any */
15026 * nfs4close_one - close one open stream for a file if needed.
15028 * "close_type" indicates which close path this is:
15029 * CLOSE_NORM: close initiated via fop_close.
15030 * CLOSE_DELMAP: close initiated via fop_delmap.
15031 * CLOSE_FORCE: close initiated via fop_inactive. This path forces
15032 * the close and release of client state for this open stream
15033 * (unless someone else has the open stream open).
15034 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15035 * (e.g., due to abort because of a signal).
15036 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15038 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15039 * recovery. Instead, the caller is expected to deal with retries.
15041 * The caller can either pass in the osp ('provided_osp') or not.
15043 * 'access_bits' represents the access we are closing/downgrading.
15045 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15046 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15047 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15049 * Errors are returned via the nfs4_error_t.
15052 nfs4close_one(vnode_t
*vp
, nfs4_open_stream_t
*provided_osp
, cred_t
*cr
,
15053 int access_bits
, nfs4_lost_rqst_t
*lrp
, nfs4_error_t
*ep
,
15054 nfs4_close_type_t close_type
, size_t len
, uint_t maxprot
,
15057 nfs4_open_owner_t
*oop
;
15058 nfs4_open_stream_t
*osp
= NULL
;
15060 int num_retries
= NFS4_NUM_RECOV_RETRIES
;
15063 nfs4_recov_state_t recov_state
;
15064 cred_t
*cred_otw
= NULL
;
15065 bool_t recovonly
= FALSE
;
15068 int close_failed
= 0;
15069 int did_dec_count
= 0;
15070 int did_start_op
= 0;
15071 int did_force_recovlock
= 0;
15072 int did_start_seqid_sync
= 0;
15073 int have_sync_lock
= 0;
15075 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
15077 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
, "closing vp %p osp %p, "
15078 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15079 (void *)vp
, (void *)provided_osp
, (void *)lrp
, close_type
,
15080 len
, maxprot
, mmap_flags
, access_bits
));
15082 nfs4_error_zinit(ep
);
15085 isrecov
= (close_type
== CLOSE_RESEND
||
15086 close_type
== CLOSE_AFTER_RESEND
);
15089 * First get the open owner.
15091 if (!provided_osp
) {
15092 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
15094 oop
= provided_osp
->os_open_owner
;
15095 ASSERT(oop
!= NULL
);
15096 open_owner_hold(oop
);
15100 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15101 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15102 "close type %d", (void *)rp
, (void *)mi
, (void *)cr
,
15103 (void *)provided_osp
, close_type
));
15108 cred_otw
= nfs4_get_otw_cred(cr
, mi
, oop
);
15112 force_close
= (close_type
== CLOSE_FORCE
);
15115 did_force_recovlock
= 0;
15116 did_start_seqid_sync
= 0;
15117 have_sync_lock
= 0;
15119 recov_state
.rs_flags
= 0;
15120 recov_state
.rs_num_retry_despite_err
= 0;
15123 * Second synchronize with recovery.
15126 ep
->error
= nfs4_start_fop(mi
, vp
, NULL
, OH_CLOSE
,
15127 &recov_state
, &recovonly
);
15133 * If we couldn't get start_fop, but have to
15134 * cleanup state, then at least acquire the
15135 * mi_recovlock so we can synchronize with
15138 if (close_type
== CLOSE_FORCE
) {
15139 (void) nfs_rw_enter_sig(&mi
->mi_recovlock
,
15141 did_force_recovlock
= 1;
15148 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15149 * set 'recovonly' to TRUE since most likely this is due to
15150 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15151 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15152 * to retry, causing us to loop until recovery finishes. Plus we
15153 * don't need protection over the open seqid since we're not going
15154 * OTW, hence don't need to use the seqid.
15156 if (recovonly
== FALSE
) {
15157 /* need to grab the open owner sync before 'os_sync_lock' */
15158 ep
->error
= nfs4_start_open_seqid_sync(oop
, mi
);
15159 if (ep
->error
== EAGAIN
) {
15162 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15163 &recov_state
, TRUE
);
15164 if (did_force_recovlock
)
15165 nfs_rw_exit(&mi
->mi_recovlock
);
15168 did_start_seqid_sync
= 1;
15172 * Third get an open stream and acquire 'os_sync_lock' to
15173 * sychronize the opening/creating of an open stream with the
15174 * closing/destroying of an open stream.
15176 if (!provided_osp
) {
15177 /* returns with 'os_sync_lock' held */
15178 osp
= find_open_stream(oop
, rp
);
15184 osp
= provided_osp
;
15185 open_stream_hold(osp
);
15186 mutex_enter(&osp
->os_sync_lock
);
15188 have_sync_lock
= 1;
15190 ASSERT(oop
== osp
->os_open_owner
);
15193 * Fourth, do any special pre-OTW CLOSE processing
15194 * based on the specific close type.
15196 if ((close_type
== CLOSE_NORM
|| close_type
== CLOSE_AFTER_RESEND
) &&
15198 ASSERT(osp
->os_open_ref_count
> 0);
15199 osp
->os_open_ref_count
--;
15201 if (osp
->os_open_ref_count
== 0)
15202 osp
->os_final_close
= 1;
15205 if (close_type
== CLOSE_FORCE
) {
15206 /* see if somebody reopened the open stream. */
15207 if (!osp
->os_force_close
) {
15208 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
,
15209 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15210 "was reopened, vp %p", (void *)osp
, (void *)vp
));
15212 ep
->stat
= NFS4_OK
;
15216 if (!osp
->os_final_close
&& !did_dec_count
) {
15217 osp
->os_open_ref_count
--;
15222 * We can't depend on os_open_ref_count being 0 due to the
15223 * way executables are opened (VN_RELE to match a fop_open).
15226 ASSERT(osp
->os_open_ref_count
== 0);
15228 if (osp
->os_open_ref_count
!= 0) {
15229 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
,
15230 "nfs4close_one: should panic here on an "
15231 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15232 "since this is probably the exec problem."));
15234 osp
->os_open_ref_count
= 0;
15238 * There is the possibility that nfs4close_one()
15239 * for close_type == CLOSE_DELMAP couldn't find the
15240 * open stream, thus couldn't decrement its os_mapcnt;
15241 * therefore we can't use this ASSERT yet.
15244 ASSERT(osp
->os_mapcnt
== 0);
15246 osp
->os_mapcnt
= 0;
15249 if (close_type
== CLOSE_DELMAP
&& !did_dec_count
) {
15250 ASSERT(osp
->os_mapcnt
>= btopr(len
));
15252 if ((mmap_flags
& MAP_SHARED
) && (maxprot
& PROT_WRITE
))
15253 osp
->os_mmap_write
-= btopr(len
);
15254 if (maxprot
& PROT_READ
)
15255 osp
->os_mmap_read
-= btopr(len
);
15256 if (maxprot
& PROT_EXEC
)
15257 osp
->os_mmap_read
-= btopr(len
);
15258 /* mirror the PROT_NONE check in nfs4_addmap() */
15259 if (!(maxprot
& PROT_READ
) && !(maxprot
& PROT_WRITE
) &&
15260 !(maxprot
& PROT_EXEC
))
15261 osp
->os_mmap_read
-= btopr(len
);
15262 osp
->os_mapcnt
-= btopr(len
);
15267 nfs4_lost_rqst_t lost_rqst
;
15269 /* request should not already be in recovery queue */
15270 ASSERT(lrp
== NULL
);
15271 nfs4_error_init(ep
, EINTR
);
15272 nfs4close_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
15273 osp
, cred_otw
, vp
);
15274 mutex_exit(&osp
->os_sync_lock
);
15275 have_sync_lock
= 0;
15276 (void) nfs4_start_recovery(ep
, mi
, vp
, NULL
, NULL
,
15277 lost_rqst
.lr_op
== OP_CLOSE
?
15278 &lost_rqst
: NULL
, OP_CLOSE
, NULL
, NULL
, NULL
);
15281 goto close_cleanup
;
15285 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15286 * we stopped operating on the open owner's <old oo_name, old seqid>
15287 * space, which means we stopped operating on the open stream
15288 * too. So don't go OTW (as the seqid is likely bad, and the
15289 * stateid could be stale, potentially triggering a false
15290 * setclientid), and just clean up the client's internal state.
15292 if (osp
->os_orig_oo_name
!= oop
->oo_name
) {
15293 NFS4_DEBUG(nfs4close_one_debug
|| nfs4_client_recov_debug
,
15294 (CE_NOTE
, "nfs4close_one: skip OTW close for osp %p "
15295 "oop %p due to bad seqid (orig oo_name %" PRIx64
" current "
15296 "oo_name %" PRIx64
")",
15297 (void *)osp
, (void *)oop
, osp
->os_orig_oo_name
,
15302 /* If the file failed recovery, just quit. */
15303 mutex_enter(&rp
->r_statelock
);
15304 if (rp
->r_flags
& R4RECOVERR
) {
15307 mutex_exit(&rp
->r_statelock
);
15310 * If the force close path failed to obtain start_fop
15311 * then skip the OTW close and just remove the state.
15314 goto close_cleanup
;
15317 * Fifth, check to see if there are still mapped pages or other
15318 * opens using this open stream. If there are then we can't
15319 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15321 if (osp
->os_open_ref_count
> 0 || osp
->os_mapcnt
> 0) {
15322 nfs4_lost_rqst_t new_lost_rqst
;
15323 bool_t needrecov
= FALSE
;
15324 cred_t
*odg_cred_otw
= NULL
;
15325 seqid4 open_dg_seqid
= 0;
15327 if (osp
->os_delegation
) {
15329 * If this open stream was never OPENed OTW then we
15330 * surely can't DOWNGRADE it (especially since the
15331 * osp->open_stateid is really a delegation stateid
15332 * when os_delegation is 1).
15334 if (access_bits
& FREAD
)
15335 osp
->os_share_acc_read
--;
15336 if (access_bits
& FWRITE
)
15337 osp
->os_share_acc_write
--;
15338 osp
->os_share_deny_none
--;
15339 nfs4_error_zinit(ep
);
15342 nfs4_open_downgrade(access_bits
, 0, oop
, osp
, vp
, cr
,
15343 lrp
, ep
, &odg_cred_otw
, &open_dg_seqid
);
15344 needrecov
= nfs4_needs_recovery(ep
, TRUE
, mi
->mi_vfsp
);
15345 if (needrecov
&& !isrecov
) {
15347 nfs4_bseqid_entry_t
*bsep
= NULL
;
15349 if (!ep
->error
&& ep
->stat
== NFS4ERR_BAD_SEQID
)
15350 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
15352 lrp
? TAG_OPEN_DG_LOST
: TAG_OPEN_DG
,
15355 nfs4open_dg_save_lost_rqst(ep
->error
, &new_lost_rqst
,
15356 oop
, osp
, odg_cred_otw
, vp
, access_bits
, 0);
15357 mutex_exit(&osp
->os_sync_lock
);
15358 have_sync_lock
= 0;
15359 abort
= nfs4_start_recovery(ep
, mi
, vp
, NULL
, NULL
,
15360 new_lost_rqst
.lr_op
== OP_OPEN_DOWNGRADE
?
15361 &new_lost_rqst
: NULL
, OP_OPEN_DOWNGRADE
,
15364 crfree(odg_cred_otw
);
15366 kmem_free(bsep
, sizeof (*bsep
));
15371 if (did_start_seqid_sync
) {
15372 nfs4_end_open_seqid_sync(oop
);
15373 did_start_seqid_sync
= 0;
15375 open_stream_rele(osp
, rp
);
15378 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15379 &recov_state
, FALSE
);
15380 if (did_force_recovlock
)
15381 nfs_rw_exit(&mi
->mi_recovlock
);
15386 crfree(odg_cred_otw
);
15392 * If this open stream was created as the results of an open
15393 * while holding a delegation, then just release it; no need
15394 * to do an OTW close. Otherwise do a "normal" OTW close.
15396 if (osp
->os_delegation
) {
15397 nfs4close_notw(vp
, osp
, &have_sync_lock
);
15398 nfs4_error_zinit(ep
);
15403 * If this stream is not valid, we're done.
15405 if (!osp
->os_valid
) {
15406 nfs4_error_zinit(ep
);
15411 * Last open or mmap ref has vanished, need to do an OTW close.
15412 * First check to see if a close is still necessary.
15414 if (osp
->os_failed_reopen
) {
15415 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15416 "don't close OTW osp %p since reopen failed.",
15419 * Reopen of the open stream failed, hence the
15420 * stateid of the open stream is invalid/stale, and
15421 * sending this OTW would incorrectly cause another
15422 * round of recovery. In this case, we need to set
15423 * the 'os_valid' bit to 0 so another thread doesn't
15424 * come in and re-open this open stream before
15425 * this "closing" thread cleans up state (decrementing
15426 * the nfs4_server_t's state_ref_count and decrementing
15427 * the os_ref_count).
15431 * This removes the reference obtained at OPEN; ie,
15432 * when the open stream structure was created.
15434 * We don't have to worry about calling 'open_stream_rele'
15435 * since we our currently holding a reference to this
15436 * open stream which means the count can not go to 0 with
15439 ASSERT(osp
->os_ref_count
>= 2);
15440 osp
->os_ref_count
--;
15441 nfs4_error_zinit(ep
);
15443 goto close_cleanup
;
15446 ASSERT(osp
->os_ref_count
> 1);
15449 * Sixth, try the CLOSE OTW.
15451 nfs4close_otw(rp
, cred_otw
, oop
, osp
, &retry
, &did_start_seqid_sync
,
15452 close_type
, ep
, &have_sync_lock
);
15454 if (ep
->error
== EINTR
|| NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
)) {
15456 * Let the recovery thread be responsible for
15457 * removing the state for CLOSE.
15464 /* See if we need to retry with a different cred */
15465 if ((ep
->error
== EACCES
||
15466 (ep
->error
== 0 && ep
->stat
== NFS4ERR_ACCESS
)) &&
15474 if (ep
->error
|| ep
->stat
)
15477 if (retry
&& !isrecov
&& num_retries
-- > 0) {
15478 if (have_sync_lock
) {
15479 mutex_exit(&osp
->os_sync_lock
);
15480 have_sync_lock
= 0;
15482 if (did_start_seqid_sync
) {
15483 nfs4_end_open_seqid_sync(oop
);
15484 did_start_seqid_sync
= 0;
15486 open_stream_rele(osp
, rp
);
15489 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15490 &recov_state
, FALSE
);
15491 if (did_force_recovlock
)
15492 nfs_rw_exit(&mi
->mi_recovlock
);
15493 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15494 "nfs4close_one: need to retry the close "
15500 * Seventh and lastly, process our results.
15502 if (close_failed
&& force_close
) {
15504 * It's ok to drop and regrab the 'os_sync_lock' since
15505 * nfs4close_notw() will recheck to make sure the
15506 * "close"/removal of state should happen.
15508 if (!have_sync_lock
) {
15509 mutex_enter(&osp
->os_sync_lock
);
15510 have_sync_lock
= 1;
15513 * This is last call, remove the ref on the open
15514 * stream created by open and clean everything up.
15516 osp
->os_pending_close
= 0;
15517 nfs4close_notw(vp
, osp
, &have_sync_lock
);
15518 nfs4_error_zinit(ep
);
15521 if (!close_failed
) {
15522 if (have_sync_lock
) {
15523 osp
->os_pending_close
= 0;
15524 mutex_exit(&osp
->os_sync_lock
);
15525 have_sync_lock
= 0;
15527 mutex_enter(&osp
->os_sync_lock
);
15528 osp
->os_pending_close
= 0;
15529 mutex_exit(&osp
->os_sync_lock
);
15531 if (did_start_op
&& recov_state
.rs_sp
!= NULL
) {
15532 mutex_enter(&recov_state
.rs_sp
->s_lock
);
15533 nfs4_dec_state_ref_count_nolock(recov_state
.rs_sp
, mi
);
15534 mutex_exit(&recov_state
.rs_sp
->s_lock
);
15536 nfs4_dec_state_ref_count(mi
);
15538 nfs4_error_zinit(ep
);
15542 if (have_sync_lock
)
15543 mutex_exit(&osp
->os_sync_lock
);
15545 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
, &recov_state
,
15546 recovonly
? TRUE
: FALSE
);
15547 if (did_force_recovlock
)
15548 nfs_rw_exit(&mi
->mi_recovlock
);
15552 open_stream_rele(osp
, rp
);
15554 if (did_start_seqid_sync
)
15555 nfs4_end_open_seqid_sync(oop
);
15556 open_owner_rele(oop
);
15561 * Convert information returned by the server in the LOCK4denied
15562 * structure to the form required by fcntl.
15565 denied_to_flk(LOCK4denied
*lockt_denied
, flock64_t
*flk
, LOCKT4args
*lockt_args
)
15567 nfs4_lo_name_t
*lo
;
15570 if (denied_to_flk_debug
) {
15571 lockt_denied_debug
= lockt_denied
;
15572 debug_enter("lockt_denied");
15576 flk
->l_type
= lockt_denied
->locktype
== READ_LT
? F_RDLCK
: F_WRLCK
;
15577 flk
->l_whence
= 0; /* aka SEEK_SET */
15578 flk
->l_start
= lockt_denied
->offset
;
15579 flk
->l_len
= lockt_denied
->length
;
15582 * If the blocking clientid matches our client id, then we can
15583 * interpret the lockowner (since we built it). If not, then
15584 * fabricate a sysid and pid. Note that the l_sysid field
15585 * in *flk already has the local sysid.
15588 if (lockt_denied
->owner
.clientid
== lockt_args
->owner
.clientid
) {
15590 if (lockt_denied
->owner
.owner_len
== sizeof (*lo
)) {
15591 lo
= (nfs4_lo_name_t
*)
15592 lockt_denied
->owner
.owner_val
;
15594 flk
->l_pid
= lo
->ln_pid
;
15596 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
15597 "denied_to_flk: bad lock owner length\n"));
15599 flk
->l_pid
= lo_to_pid(&lockt_denied
->owner
);
15602 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
15603 "denied_to_flk: foreign clientid\n"));
15606 * Construct a new sysid which should be different from
15607 * sysids of other systems.
15611 flk
->l_pid
= lo_to_pid(&lockt_denied
->owner
);
15616 lo_to_pid(lock_owner4
*lop
)
15622 cp
= (uchar_t
*)&lop
->clientid
;
15624 for (i
= 0; i
< sizeof (lop
->clientid
); i
++)
15625 pid
+= (pid_t
)*cp
++;
15627 cp
= (uchar_t
*)lop
->owner_val
;
15629 for (i
= 0; i
< lop
->owner_len
; i
++)
15630 pid
+= (pid_t
)*cp
++;
15636 * Given a lock pointer, returns the length of that lock.
15637 * "end" is the last locked offset the "l_len" covers from
15638 * the start of the lock.
15641 lock_to_end(flock64_t
*lock
)
15645 if (lock
->l_len
== 0)
15646 lock_end
= (off64_t
)MAXEND
;
15648 lock_end
= lock
->l_start
+ lock
->l_len
- 1;
15654 * Given the end of a lock, it will return you the length "l_len" for that lock.
15657 end_to_len(off64_t start
, off64_t end
)
15661 ASSERT(end
>= start
);
15665 lock_len
= end
- start
+ 1;
15671 * On given end for a lock it determines if it is the last locked offset
15672 * or not, if so keeps it as is, else adds one to return the length for
15676 start_check(off64_t x
)
15685 * See if these two locks overlap, and if so return 1;
15686 * otherwise, return 0.
15689 locks_intersect(flock64_t
*llfp
, flock64_t
*curfp
)
15691 off64_t llfp_end
, curfp_end
;
15693 llfp_end
= lock_to_end(llfp
);
15694 curfp_end
= lock_to_end(curfp
);
15696 if (((llfp_end
>= curfp
->l_start
) &&
15697 (llfp
->l_start
<= curfp
->l_start
)) ||
15698 ((curfp
->l_start
<= llfp
->l_start
) && (curfp_end
>= llfp
->l_start
)))
15704 * Determine what the intersecting lock region is, and add that to the
15705 * 'nl_llpp' locklist in increasing order (by l_start).
15708 nfs4_add_lock_range(flock64_t
*lost_flp
, flock64_t
*local_flp
,
15709 locklist_t
**nl_llpp
, vnode_t
*vp
)
15711 locklist_t
*intersect_llp
, *tmp_fllp
, *cur_fllp
;
15712 off64_t lost_flp_end
, local_flp_end
, len
, start
;
15714 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range:"));
15716 if (!locks_intersect(lost_flp
, local_flp
))
15719 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range: "
15720 "locks intersect"));
15722 lost_flp_end
= lock_to_end(lost_flp
);
15723 local_flp_end
= lock_to_end(local_flp
);
15725 /* Find the starting point of the intersecting region */
15726 if (local_flp
->l_start
> lost_flp
->l_start
)
15727 start
= local_flp
->l_start
;
15729 start
= lost_flp
->l_start
;
15731 /* Find the lenght of the intersecting region */
15732 if (lost_flp_end
< local_flp_end
)
15733 len
= end_to_len(start
, lost_flp_end
);
15735 len
= end_to_len(start
, local_flp_end
);
15738 * Prepare the flock structure for the intersection found and insert
15739 * it into the new list in increasing l_start order. This list contains
15740 * intersections of locks registered by the client with the local host
15741 * and the lost lock.
15742 * The lock type of this lock is the same as that of the local_flp.
15744 intersect_llp
= (locklist_t
*)kmem_alloc(sizeof (locklist_t
), KM_SLEEP
);
15745 intersect_llp
->ll_flock
.l_start
= start
;
15746 intersect_llp
->ll_flock
.l_len
= len
;
15747 intersect_llp
->ll_flock
.l_type
= local_flp
->l_type
;
15748 intersect_llp
->ll_flock
.l_pid
= local_flp
->l_pid
;
15749 intersect_llp
->ll_flock
.l_sysid
= local_flp
->l_sysid
;
15750 intersect_llp
->ll_flock
.l_whence
= 0; /* aka SEEK_SET */
15751 intersect_llp
->ll_vp
= vp
;
15753 tmp_fllp
= *nl_llpp
;
15755 while (tmp_fllp
!= NULL
&& tmp_fllp
->ll_flock
.l_start
<
15756 intersect_llp
->ll_flock
.l_start
) {
15757 cur_fllp
= tmp_fllp
;
15758 tmp_fllp
= tmp_fllp
->ll_next
;
15760 if (cur_fllp
== NULL
) {
15761 /* first on the list */
15762 intersect_llp
->ll_next
= *nl_llpp
;
15763 *nl_llpp
= intersect_llp
;
15765 intersect_llp
->ll_next
= cur_fllp
->ll_next
;
15766 cur_fllp
->ll_next
= intersect_llp
;
15769 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range: "
15770 "created lock region: start %"PRIx64
" end %"PRIx64
" : %s\n",
15771 intersect_llp
->ll_flock
.l_start
,
15772 intersect_llp
->ll_flock
.l_start
+ intersect_llp
->ll_flock
.l_len
,
15773 intersect_llp
->ll_flock
.l_type
== F_RDLCK
? "READ" : "WRITE"));
15777 * Our local locking current state is potentially different than
15778 * what the NFSv4 server thinks we have due to a lost lock that was
15779 * resent and then received. We need to reset our "NFSv4" locking
15780 * state to match the current local locking state for this pid since
15781 * that is what the user/application sees as what the world is.
15783 * We cannot afford to drop the open/lock seqid sync since then we can
15784 * get confused about what the current local locking state "is" versus
15787 * If we are unable to fix up the locks, we send SIGLOST to the affected
15788 * process. This is not done if the filesystem has been forcibly
15789 * unmounted, in case the process has already exited and a new process
15790 * exists with the same pid.
15793 nfs4_reinstitute_local_lock_state(vnode_t
*vp
, flock64_t
*lost_flp
, cred_t
*cr
,
15794 nfs4_lock_owner_t
*lop
)
15796 locklist_t
*locks
, *llp
, *ri_llp
, *tmp_llp
;
15797 mntinfo4_t
*mi
= VTOMI4(vp
);
15798 const int cmd
= F_SETLK
;
15799 off64_t cur_start
, llp_ll_flock_end
, lost_flp_end
;
15802 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15803 "nfs4_reinstitute_local_lock_state"));
15806 * Find active locks for this vp from the local locking code.
15807 * Scan through this list and find out the locks that intersect with
15808 * the lost lock. Once we find the lock that intersects, add the
15809 * intersection area as a new lock to a new list "ri_llp". The lock
15810 * type of the intersection region lock added to ri_llp is the same
15811 * as that found in the active lock list, "list". The intersecting
15812 * region locks are added to ri_llp in increasing l_start order.
15814 ASSERT(nfs_zone() == mi
->mi_zone
);
15816 locks
= flk_active_locks_for_vp(vp
);
15819 for (llp
= locks
; llp
!= NULL
; llp
= llp
->ll_next
) {
15820 ASSERT(llp
->ll_vp
== vp
);
15822 * Pick locks that belong to this pid/lockowner
15824 if (llp
->ll_flock
.l_pid
!= lost_flp
->l_pid
)
15827 nfs4_add_lock_range(lost_flp
, &llp
->ll_flock
, &ri_llp
, vp
);
15831 * Now we have the list of intersections with the lost lock. These are
15832 * the locks that were/are active before the server replied to the
15833 * last/lost lock. Issue these locks to the server here. Playing these
15834 * locks to the server will re-establish our current local locking state
15835 * with the v4 server.
15836 * If we get an error, send SIGLOST to the application for that lock.
15839 for (llp
= ri_llp
; llp
!= NULL
; llp
= llp
->ll_next
) {
15840 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15841 "nfs4_reinstitute_local_lock_state: need to issue "
15842 "flock: [%"PRIx64
" - %"PRIx64
"] : %s",
15843 llp
->ll_flock
.l_start
,
15844 llp
->ll_flock
.l_start
+ llp
->ll_flock
.l_len
,
15845 llp
->ll_flock
.l_type
== F_RDLCK
? "READ" :
15846 llp
->ll_flock
.l_type
== F_WRLCK
? "WRITE" : "INVALID"));
15848 * No need to relock what we already have
15850 if (llp
->ll_flock
.l_type
== lost_flp
->l_type
)
15853 push_reinstate(vp
, cmd
, &llp
->ll_flock
, cr
, lop
);
15857 * Now keeping the start of the lost lock as our reference parse the
15858 * newly created ri_llp locklist to find the ranges that we have locked
15859 * with the v4 server but not in the current local locking. We need
15860 * to unlock these ranges.
15861 * These ranges can also be reffered to as those ranges, where the lost
15862 * lock does not overlap with the locks in the ri_llp but are locked
15863 * since the server replied to the lost lock.
15865 cur_start
= lost_flp
->l_start
;
15866 lost_flp_end
= lock_to_end(lost_flp
);
15868 ul_fl
.l_type
= F_UNLCK
;
15869 ul_fl
.l_whence
= 0; /* aka SEEK_SET */
15870 ul_fl
.l_sysid
= lost_flp
->l_sysid
;
15871 ul_fl
.l_pid
= lost_flp
->l_pid
;
15873 for (llp
= ri_llp
; llp
!= NULL
; llp
= llp
->ll_next
) {
15874 llp_ll_flock_end
= lock_to_end(&llp
->ll_flock
);
15876 if (llp
->ll_flock
.l_start
<= cur_start
) {
15877 cur_start
= start_check(llp_ll_flock_end
);
15880 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15881 "nfs4_reinstitute_local_lock_state: "
15882 "UNLOCK [%"PRIx64
" - %"PRIx64
"]",
15883 cur_start
, llp
->ll_flock
.l_start
));
15885 ul_fl
.l_start
= cur_start
;
15886 ul_fl
.l_len
= end_to_len(cur_start
,
15887 (llp
->ll_flock
.l_start
- 1));
15889 push_reinstate(vp
, cmd
, &ul_fl
, cr
, lop
);
15890 cur_start
= start_check(llp_ll_flock_end
);
15894 * In the case where the lost lock ends after all intersecting locks,
15895 * unlock the last part of the lost lock range.
15897 if (cur_start
!= start_check(lost_flp_end
)) {
15898 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15899 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15900 "lost lock region [%"PRIx64
" - %"PRIx64
"]",
15901 cur_start
, lost_flp
->l_start
+ lost_flp
->l_len
));
15903 ul_fl
.l_start
= cur_start
;
15905 * Is it an to-EOF lock? if so unlock till the end
15907 if (lost_flp
->l_len
== 0)
15910 ul_fl
.l_len
= start_check(lost_flp_end
) - cur_start
;
15912 push_reinstate(vp
, cmd
, &ul_fl
, cr
, lop
);
15916 flk_free_locklist(locks
);
15918 /* Free up our newly created locklist */
15919 for (llp
= ri_llp
; llp
!= NULL
; ) {
15920 tmp_llp
= llp
->ll_next
;
15921 kmem_free(llp
, sizeof (locklist_t
));
15926 * Now return back to the original calling nfs4frlock()
15927 * and let us naturally drop our seqid syncs.
15932 * Create a lost state record for the given lock reinstantiation request
15933 * and push it onto the lost state queue.
15936 push_reinstate(vnode_t
*vp
, int cmd
, flock64_t
*flk
, cred_t
*cr
,
15937 nfs4_lock_owner_t
*lop
)
15939 nfs4_lost_rqst_t req
;
15940 nfs_lock_type4 locktype
;
15941 nfs4_error_t e
= { EINTR
, NFS4_OK
, RPC_SUCCESS
};
15943 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
15945 locktype
= flk_to_locktype(cmd
, flk
->l_type
);
15946 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE
, EINTR
, locktype
,
15947 NULL
, NULL
, lop
, flk
, &req
, cr
, vp
);
15948 (void) nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
15949 (req
.lr_op
== OP_LOCK
|| req
.lr_op
== OP_LOCKU
) ?
15950 &req
: NULL
, flk
->l_type
== F_UNLCK
? OP_LOCKU
: OP_LOCK
,