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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
30 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
35 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
43 #include <sys/vnode.h>
45 #include <sys/vfs_opreg.h>
47 #include <sys/filio.h>
51 #include <sys/pathname.h>
52 #include <sys/dirent.h>
53 #include <sys/debug.h>
54 #include <sys/vmsystm.h>
55 #include <sys/fcntl.h>
56 #include <sys/flock.h>
58 #include <sys/errno.h>
59 #include <sys/strsubr.h>
60 #include <sys/sysmacros.h>
62 #include <sys/cmn_err.h>
63 #include <sys/pathconf.h>
64 #include <sys/utsname.h>
67 #include <sys/systeminfo.h>
68 #include <sys/policy.h>
74 #include <rpc/types.h>
79 #include <nfs/nfs_clnt.h>
80 #include <nfs/nfs_acl.h>
83 #include <nfs/nfs4_kprot.h>
84 #include <nfs/rnode4.h>
85 #include <nfs/nfs4_clnt.h>
92 #include <vm/seg_map.h>
93 #include <vm/seg_kpm.h>
94 #include <vm/seg_vn.h>
96 #include <fs/fs_subr.h>
99 #include <sys/int_fmtio.h>
100 #include <sys/fs/autofs.h>
103 nfs4_ga_res_t
*di_garp
;
105 hrtime_t di_time_call
;
108 typedef enum nfs4_acl_op
{
113 static struct lm_sysid
*nfs4_find_sysid(mntinfo4_t
*mi
);
115 static void nfs4_update_dircaches(change_info4
*, vnode_t
*, vnode_t
*,
116 char *, dirattr_info_t
*);
118 static void nfs4close_otw(rnode4_t
*, cred_t
*, nfs4_open_owner_t
*,
119 nfs4_open_stream_t
*, int *, int *, nfs4_close_type_t
,
120 nfs4_error_t
*, int *);
121 static int nfs4_rdwrlbn(vnode_t
*, page_t
*, u_offset_t
, size_t, int,
123 static int nfs4write(vnode_t
*, caddr_t
, u_offset_t
, int, cred_t
*,
125 static int nfs4read(vnode_t
*, caddr_t
, offset_t
, int, size_t *,
126 cred_t
*, bool_t
, struct uio
*);
127 static int nfs4setattr(vnode_t
*, struct vattr
*, int, cred_t
*,
129 static int nfs4openattr(vnode_t
*, vnode_t
**, int, cred_t
*);
130 static int nfs4lookup(vnode_t
*, char *, vnode_t
**, cred_t
*, int);
131 static int nfs4lookup_xattr(vnode_t
*, char *, vnode_t
**, int, cred_t
*);
132 static int nfs4lookupvalidate_otw(vnode_t
*, char *, vnode_t
**, cred_t
*);
133 static int nfs4lookupnew_otw(vnode_t
*, char *, vnode_t
**, cred_t
*);
134 static int nfs4mknod(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
135 int, vnode_t
**, cred_t
*);
136 static int nfs4open_otw(vnode_t
*, char *, struct vattr
*, vnode_t
**,
137 cred_t
*, int, int, enum createmode4
, int);
138 static int nfs4rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
140 static int nfs4rename_persistent_fh(vnode_t
*, char *, vnode_t
*,
141 vnode_t
*, char *, cred_t
*, nfsstat4
*);
142 static int nfs4rename_volatile_fh(vnode_t
*, char *, vnode_t
*,
143 vnode_t
*, char *, cred_t
*, nfsstat4
*);
144 static int do_nfs4readdir(vnode_t
*, rddir4_cache
*, cred_t
*);
145 static void nfs4readdir(vnode_t
*, rddir4_cache
*, cred_t
*);
146 static int nfs4_bio(struct buf
*, stable_how4
*, cred_t
*, bool_t
);
147 static int nfs4_getapage(vnode_t
*, u_offset_t
, size_t, uint_t
*,
148 page_t
*[], size_t, struct seg
*, caddr_t
,
149 enum seg_rw
, cred_t
*);
150 static void nfs4_readahead(vnode_t
*, u_offset_t
, caddr_t
, struct seg
*,
152 static int nfs4_sync_putapage(vnode_t
*, page_t
*, u_offset_t
, size_t,
154 static int nfs4_sync_pageio(vnode_t
*, page_t
*, u_offset_t
, size_t,
156 static int nfs4_commit(vnode_t
*, offset4
, count4
, cred_t
*);
157 static void nfs4_set_mod(vnode_t
*);
158 static void nfs4_get_commit(vnode_t
*);
159 static void nfs4_get_commit_range(vnode_t
*, u_offset_t
, size_t);
160 static int nfs4_putpage_commit(vnode_t
*, offset_t
, size_t, cred_t
*);
161 static int nfs4_commit_vp(vnode_t
*, u_offset_t
, size_t, cred_t
*, int);
162 static int nfs4_sync_commit(vnode_t
*, page_t
*, offset3
, count3
,
164 static void do_nfs4_async_commit(vnode_t
*, page_t
*, offset3
, count3
,
166 static int nfs4_update_attrcache(nfsstat4
, nfs4_ga_res_t
*,
167 hrtime_t
, vnode_t
*, cred_t
*);
168 static int nfs4_open_non_reg_file(vnode_t
**, int, cred_t
*);
169 static int nfs4_safelock(vnode_t
*, const struct flock64
*, cred_t
*);
170 static void nfs4_register_lock_locally(vnode_t
*, struct flock64
*, int,
172 static int nfs4_lockrelease(vnode_t
*, int, offset_t
, cred_t
*);
173 static int nfs4_block_and_wait(clock_t *, rnode4_t
*);
174 static cred_t
*state_to_cred(nfs4_open_stream_t
*);
175 static void denied_to_flk(LOCK4denied
*, flock64_t
*, LOCKT4args
*);
176 static pid_t
lo_to_pid(lock_owner4
*);
177 static void nfs4_reinstitute_local_lock_state(vnode_t
*, flock64_t
*,
178 cred_t
*, nfs4_lock_owner_t
*);
179 static void push_reinstate(vnode_t
*, int, flock64_t
*, cred_t
*,
180 nfs4_lock_owner_t
*);
181 static int open_and_get_osp(vnode_t
*, cred_t
*, nfs4_open_stream_t
**);
182 static void nfs4_delmap_callback(struct as
*, void *, uint_t
);
183 static void nfs4_free_delmapcall(nfs4_delmapcall_t
*);
184 static nfs4_delmapcall_t
*nfs4_init_delmapcall();
185 static int nfs4_find_and_delete_delmapcall(rnode4_t
*, int *);
186 static int nfs4_is_acl_mask_valid(uint_t
, nfs4_acl_op_t
);
187 static int nfs4_create_getsecattr_return(vsecattr_t
*, vsecattr_t
*,
191 * Routines that implement the setting of v4 args for the misc. ops
193 static void nfs4args_lock_free(nfs_argop4
*);
194 static void nfs4args_lockt_free(nfs_argop4
*);
195 static void nfs4args_setattr(nfs_argop4
*, vattr_t
*, vsecattr_t
*,
196 int, rnode4_t
*, cred_t
*, bitmap4
, int *,
197 nfs4_stateid_types_t
*);
198 static void nfs4args_setattr_free(nfs_argop4
*);
199 static int nfs4args_verify(nfs_argop4
*, vattr_t
*, enum nfs_opnum4
,
201 static void nfs4args_verify_free(nfs_argop4
*);
202 static void nfs4args_write(nfs_argop4
*, stable_how4
, rnode4_t
*, cred_t
*,
203 WRITE4args
**, nfs4_stateid_types_t
*);
206 * These are the vnode ops functions that implement the vnode interface to
207 * the networked file system. See more comments below at nfs4_vnodeops.
209 static int nfs4_open(vnode_t
**, int, cred_t
*, caller_context_t
*);
210 static int nfs4_close(vnode_t
*, int, int, offset_t
, cred_t
*,
212 static int nfs4_read(vnode_t
*, struct uio
*, int, cred_t
*,
214 static int nfs4_write(vnode_t
*, struct uio
*, int, cred_t
*,
216 static int nfs4_ioctl(vnode_t
*, int, intptr_t, int, cred_t
*, int *,
218 static int nfs4_setattr(vnode_t
*, struct vattr
*, int, cred_t
*,
220 static int nfs4_access(vnode_t
*, int, int, cred_t
*, caller_context_t
*);
221 static int nfs4_readlink(vnode_t
*, struct uio
*, cred_t
*,
223 static int nfs4_fsync(vnode_t
*, int, cred_t
*, caller_context_t
*);
224 static int nfs4_create(vnode_t
*, char *, struct vattr
*, enum vcexcl
,
225 int, vnode_t
**, cred_t
*, int, caller_context_t
*,
227 static int nfs4_remove(vnode_t
*, char *, cred_t
*, caller_context_t
*,
229 static int nfs4_link(vnode_t
*, vnode_t
*, char *, cred_t
*,
230 caller_context_t
*, int);
231 static int nfs4_rename(vnode_t
*, char *, vnode_t
*, char *, cred_t
*,
232 caller_context_t
*, int);
233 static int nfs4_mkdir(vnode_t
*, char *, struct vattr
*, vnode_t
**,
234 cred_t
*, caller_context_t
*, int, vsecattr_t
*);
235 static int nfs4_rmdir(vnode_t
*, char *, vnode_t
*, cred_t
*,
236 caller_context_t
*, int);
237 static int nfs4_symlink(vnode_t
*, char *, struct vattr
*, char *,
238 cred_t
*, caller_context_t
*, int);
239 static int nfs4_readdir(vnode_t
*, struct uio
*, cred_t
*, int *,
240 caller_context_t
*, int);
241 static int nfs4_seek(vnode_t
*, offset_t
, offset_t
*, caller_context_t
*);
242 static int nfs4_getpage(vnode_t
*, offset_t
, size_t, uint_t
*,
243 page_t
*[], size_t, struct seg
*, caddr_t
,
244 enum seg_rw
, cred_t
*, caller_context_t
*);
245 static int nfs4_putpage(vnode_t
*, offset_t
, size_t, int, cred_t
*,
247 static int nfs4_map(vnode_t
*, offset_t
, struct as
*, caddr_t
*, size_t,
248 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
249 static int nfs4_addmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
250 uchar_t
, uchar_t
, uint_t
, cred_t
*, caller_context_t
*);
251 static int nfs4_cmp(vnode_t
*, vnode_t
*, caller_context_t
*);
252 static int nfs4_frlock(vnode_t
*, int, struct flock64
*, int, offset_t
,
253 struct flk_callback
*, cred_t
*, caller_context_t
*);
254 static int nfs4_space(vnode_t
*, int, struct flock64
*, int, offset_t
,
255 cred_t
*, caller_context_t
*);
256 static int nfs4_delmap(vnode_t
*, offset_t
, struct as
*, caddr_t
, size_t,
257 uint_t
, uint_t
, uint_t
, cred_t
*, caller_context_t
*);
258 static int nfs4_pageio(vnode_t
*, page_t
*, u_offset_t
, size_t, int,
259 cred_t
*, caller_context_t
*);
260 static void nfs4_dispose(vnode_t
*, page_t
*, int, int, cred_t
*,
262 static int nfs4_setsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
265 * These vnode ops are required to be called from outside this source file,
266 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
269 int nfs4_getattr(vnode_t
*, struct vattr
*, int, cred_t
*,
271 void nfs4_inactive(vnode_t
*, cred_t
*, caller_context_t
*);
272 int nfs4_lookup(vnode_t
*, char *, vnode_t
**,
273 struct pathname
*, int, vnode_t
*, cred_t
*,
274 caller_context_t
*, int *, pathname_t
*);
275 int nfs4_fid(vnode_t
*, fid_t
*, caller_context_t
*);
276 int nfs4_rwlock(vnode_t
*, int, caller_context_t
*);
277 void nfs4_rwunlock(vnode_t
*, int, caller_context_t
*);
278 int nfs4_realvp(vnode_t
*, vnode_t
**, caller_context_t
*);
279 int nfs4_pathconf(vnode_t
*, int, ulong_t
*, cred_t
*,
281 int nfs4_getsecattr(vnode_t
*, vsecattr_t
*, int, cred_t
*,
283 int nfs4_shrlock(vnode_t
*, int, struct shrlock
*, int, cred_t
*,
287 * Used for nfs4_commit_vp() to indicate if we should
288 * wait on pending writes.
290 #define NFS4_WRITE_NOWAIT 0
291 #define NFS4_WRITE_WAIT 1
293 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
296 * Error flags used to pass information about certain special errors
297 * which need to be handled specially.
300 #define NFS_VERF_MISMATCH -97
303 * Flags used to differentiate between which operation drove the
304 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
306 #define NFS4_CLOSE_OP 0x1
307 #define NFS4_DELMAP_OP 0x2
308 #define NFS4_INACTIVE_OP 0x3
310 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
312 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
313 #define ALIGN64(x, ptr, sz) \
314 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
316 x = sizeof (uint64_t) - (x); \
322 int nfs4_client_attr_debug
= 0;
323 int nfs4_client_state_debug
= 0;
324 int nfs4_client_shadow_debug
= 0;
325 int nfs4_client_lock_debug
= 0;
326 int nfs4_seqid_sync
= 0;
327 int nfs4_client_map_debug
= 0;
328 static int nfs4_pageio_debug
= 0;
329 int nfs4_client_inactive_debug
= 0;
330 int nfs4_client_recov_debug
= 0;
331 int nfs4_client_failover_debug
= 0;
332 int nfs4_client_call_debug
= 0;
333 int nfs4_client_lookup_debug
= 0;
334 int nfs4_client_zone_debug
= 0;
335 int nfs4_lost_rqst_debug
= 0;
336 int nfs4_rdattrerr_debug
= 0;
337 int nfs4_open_stream_debug
= 0;
339 int nfs4read_error_inject
;
341 static int nfs4_create_misses
= 0;
343 static int nfs4_readdir_cache_shorts
= 0;
344 static int nfs4_readdir_readahead
= 0;
346 static int nfs4_bio_do_stop
= 0;
348 static int nfs4_lostpage
= 0; /* number of times we lost original page */
350 int nfs4_mmap_debug
= 0;
352 static int nfs4_pathconf_cache_hits
= 0;
353 static int nfs4_pathconf_cache_misses
= 0;
355 int nfs4close_all_cnt
;
356 int nfs4close_one_debug
= 0;
357 int nfs4close_notw_debug
= 0;
359 int denied_to_flk_debug
= 0;
360 void *lockt_denied_debug
;
365 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
366 * or NFS4ERR_RESOURCE.
368 static int confirm_retry_sec
= 30;
370 static int nfs4_lookup_neg_cache
= 1;
373 * number of pages to read ahead
374 * optimized for 100 base-T.
376 static int nfs4_nra
= 4;
378 static int nfs4_do_symlink_cache
= 1;
380 static int nfs4_pathconf_disable_cache
= 0;
383 * These are the vnode ops routines which implement the vnode interface to
384 * the networked file system. These routines just take their parameters,
385 * make them look networkish by putting the right info into interface structs,
386 * and then calling the appropriate remote routine(s) to do the work.
388 * Note on directory name lookup cacheing: If we detect a stale fhandle,
389 * we purge the directory cache relative to that vnode. This way, the
390 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
391 * more details on rnode locking.
394 struct vnodeops
*nfs4_vnodeops
;
396 const fs_operation_def_t nfs4_vnodeops_template
[] = {
397 VOPNAME_OPEN
, { .vop_open
= nfs4_open
},
398 VOPNAME_CLOSE
, { .vop_close
= nfs4_close
},
399 VOPNAME_READ
, { .vop_read
= nfs4_read
},
400 VOPNAME_WRITE
, { .vop_write
= nfs4_write
},
401 VOPNAME_IOCTL
, { .vop_ioctl
= nfs4_ioctl
},
402 VOPNAME_GETATTR
, { .vop_getattr
= nfs4_getattr
},
403 VOPNAME_SETATTR
, { .vop_setattr
= nfs4_setattr
},
404 VOPNAME_ACCESS
, { .vop_access
= nfs4_access
},
405 VOPNAME_LOOKUP
, { .vop_lookup
= nfs4_lookup
},
406 VOPNAME_CREATE
, { .vop_create
= nfs4_create
},
407 VOPNAME_REMOVE
, { .vop_remove
= nfs4_remove
},
408 VOPNAME_LINK
, { .vop_link
= nfs4_link
},
409 VOPNAME_RENAME
, { .vop_rename
= nfs4_rename
},
410 VOPNAME_MKDIR
, { .vop_mkdir
= nfs4_mkdir
},
411 VOPNAME_RMDIR
, { .vop_rmdir
= nfs4_rmdir
},
412 VOPNAME_READDIR
, { .vop_readdir
= nfs4_readdir
},
413 VOPNAME_SYMLINK
, { .vop_symlink
= nfs4_symlink
},
414 VOPNAME_READLINK
, { .vop_readlink
= nfs4_readlink
},
415 VOPNAME_FSYNC
, { .vop_fsync
= nfs4_fsync
},
416 VOPNAME_INACTIVE
, { .vop_inactive
= nfs4_inactive
},
417 VOPNAME_FID
, { .vop_fid
= nfs4_fid
},
418 VOPNAME_RWLOCK
, { .vop_rwlock
= nfs4_rwlock
},
419 VOPNAME_RWUNLOCK
, { .vop_rwunlock
= nfs4_rwunlock
},
420 VOPNAME_SEEK
, { .vop_seek
= nfs4_seek
},
421 VOPNAME_FRLOCK
, { .vop_frlock
= nfs4_frlock
},
422 VOPNAME_SPACE
, { .vop_space
= nfs4_space
},
423 VOPNAME_REALVP
, { .vop_realvp
= nfs4_realvp
},
424 VOPNAME_GETPAGE
, { .vop_getpage
= nfs4_getpage
},
425 VOPNAME_PUTPAGE
, { .vop_putpage
= nfs4_putpage
},
426 VOPNAME_MAP
, { .vop_map
= nfs4_map
},
427 VOPNAME_ADDMAP
, { .vop_addmap
= nfs4_addmap
},
428 VOPNAME_DELMAP
, { .vop_delmap
= nfs4_delmap
},
429 /* no separate nfs4_dump */
430 VOPNAME_DUMP
, { .vop_dump
= nfs_dump
},
431 VOPNAME_PATHCONF
, { .vop_pathconf
= nfs4_pathconf
},
432 VOPNAME_PAGEIO
, { .vop_pageio
= nfs4_pageio
},
433 VOPNAME_DISPOSE
, { .vop_dispose
= nfs4_dispose
},
434 VOPNAME_SETSECATTR
, { .vop_setsecattr
= nfs4_setsecattr
},
435 VOPNAME_GETSECATTR
, { .vop_getsecattr
= nfs4_getsecattr
},
436 VOPNAME_SHRLOCK
, { .vop_shrlock
= nfs4_shrlock
},
437 VOPNAME_VNEVENT
, { .vop_vnevent
= fs_vnevent_support
},
442 * The following are subroutines and definitions to set args or get res
443 * for the different nfsv4 ops
447 nfs4args_lookup_free(nfs_argop4
*argop
, int arglen
)
451 for (i
= 0; i
< arglen
; i
++) {
452 if (argop
[i
].argop
== OP_LOOKUP
) {
454 argop
[i
].nfs_argop4_u
.oplookup
.
455 objname
.utf8string_val
,
456 argop
[i
].nfs_argop4_u
.oplookup
.
457 objname
.utf8string_len
);
463 nfs4args_lock_free(nfs_argop4
*argop
)
465 locker4
*locker
= &argop
->nfs_argop4_u
.oplock
.locker
;
467 if (locker
->new_lock_owner
== TRUE
) {
468 open_to_lock_owner4
*open_owner
;
470 open_owner
= &locker
->locker4_u
.open_owner
;
471 if (open_owner
->lock_owner
.owner_val
!= NULL
) {
472 kmem_free(open_owner
->lock_owner
.owner_val
,
473 open_owner
->lock_owner
.owner_len
);
479 nfs4args_lockt_free(nfs_argop4
*argop
)
481 lock_owner4
*lowner
= &argop
->nfs_argop4_u
.oplockt
.owner
;
483 if (lowner
->owner_val
!= NULL
) {
484 kmem_free(lowner
->owner_val
, lowner
->owner_len
);
489 nfs4args_setattr(nfs_argop4
*argop
, vattr_t
*vap
, vsecattr_t
*vsap
, int flags
,
490 rnode4_t
*rp
, cred_t
*cr
, bitmap4 supp
, int *error
,
491 nfs4_stateid_types_t
*sid_types
)
493 fattr4
*attr
= &argop
->nfs_argop4_u
.opsetattr
.obj_attributes
;
496 argop
->argop
= OP_SETATTR
;
498 * The stateid is set to 0 if client is not modifying the size
499 * and otherwise to whatever nfs4_get_stateid() returns.
501 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
502 * state struct could be found for the process/file pair. We may
503 * want to change this in the future (by OPENing the file). See
506 if (vap
->va_mask
& AT_SIZE
) {
509 mi
= VTOMI4(RTOV4(rp
));
511 argop
->nfs_argop4_u
.opsetattr
.stateid
=
512 nfs4_get_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
, mi
,
513 OP_SETATTR
, sid_types
, FALSE
);
515 bzero(&argop
->nfs_argop4_u
.opsetattr
.stateid
,
519 *error
= vattr_to_fattr4(vap
, vsap
, attr
, flags
, OP_SETATTR
, supp
);
521 bzero(attr
, sizeof (*attr
));
525 nfs4args_setattr_free(nfs_argop4
*argop
)
527 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opsetattr
.obj_attributes
);
531 nfs4args_verify(nfs_argop4
*argop
, vattr_t
*vap
, enum nfs_opnum4 op
,
540 attr
= &argop
->nfs_argop4_u
.opverify
.obj_attributes
;
543 attr
= &argop
->nfs_argop4_u
.opnverify
.obj_attributes
;
549 error
= vattr_to_fattr4(vap
, NULL
, attr
, 0, op
, supp
);
551 bzero(attr
, sizeof (*attr
));
556 nfs4args_verify_free(nfs_argop4
*argop
)
558 switch (argop
->argop
) {
560 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opverify
.obj_attributes
);
563 nfs4_fattr4_free(&argop
->nfs_argop4_u
.opnverify
.obj_attributes
);
571 nfs4args_write(nfs_argop4
*argop
, stable_how4 stable
, rnode4_t
*rp
, cred_t
*cr
,
572 WRITE4args
**wargs_pp
, nfs4_stateid_types_t
*sid_tp
)
574 WRITE4args
*wargs
= &argop
->nfs_argop4_u
.opwrite
;
575 mntinfo4_t
*mi
= VTOMI4(RTOV4(rp
));
577 argop
->argop
= OP_WRITE
;
578 wargs
->stable
= stable
;
579 wargs
->stateid
= nfs4_get_w_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
,
580 mi
, OP_WRITE
, sid_tp
);
586 nfs4args_copen_free(OPEN4cargs
*open_args
)
588 if (open_args
->owner
.owner_val
) {
589 kmem_free(open_args
->owner
.owner_val
,
590 open_args
->owner
.owner_len
);
592 if ((open_args
->opentype
== OPEN4_CREATE
) &&
593 (open_args
->mode
!= EXCLUSIVE4
)) {
594 nfs4_fattr4_free(&open_args
->createhow4_u
.createattrs
);
599 * XXX: This is referenced in modstubs.s
602 nfs4_getvnodeops(void)
604 return (nfs4_vnodeops
);
608 * The OPEN operation opens a regular file.
612 nfs4_open(vnode_t
**vpp
, int flag
, cred_t
*cr
, caller_context_t
*ct
)
617 int just_been_created
;
620 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4_open: "));
621 if (nfs_zone() != VTOMI4(*vpp
)->mi_zone
)
626 * Check to see if opening something besides a regular file;
627 * if so skip the OTW call
629 if ((*vpp
)->v_type
!= VREG
) {
630 error
= nfs4_open_non_reg_file(vpp
, flag
, cr
);
635 * XXX - would like a check right here to know if the file is
636 * executable or not, so as to skip OTW
639 if ((error
= vtodv(*vpp
, &dvp
, cr
, TRUE
)) != 0)
643 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
646 if ((error
= vtoname(*vpp
, fn
, MAXNAMELEN
)) != 0) {
647 nfs_rw_exit(&drp
->r_rwlock
);
652 * See if this file has just been CREATEd.
653 * If so, clear the flag and update the dnlc, which was previously
654 * skipped in nfs4_create.
655 * XXX need better serilization on this.
656 * XXX move this into the nf4open_otw call, after we have
657 * XXX acquired the open owner seqid sync.
659 mutex_enter(&rp
->r_statev4_lock
);
660 if (rp
->created_v4
) {
662 mutex_exit(&rp
->r_statev4_lock
);
664 dnlc_update(dvp
, fn
, *vpp
);
665 /* This is needed so we don't bump the open ref count */
666 just_been_created
= 1;
668 mutex_exit(&rp
->r_statev4_lock
);
669 just_been_created
= 0;
673 * If caller specified O_TRUNC/FTRUNC, then be sure to set
674 * FWRITE (to drive successful setattr(size=0) after open)
679 error
= nfs4open_otw(dvp
, fn
, NULL
, vpp
, cr
, 0, flag
, 0,
682 if (!error
&& !((*vpp
)->v_flag
& VROOT
))
683 dnlc_update(dvp
, fn
, *vpp
);
685 nfs_rw_exit(&drp
->r_rwlock
);
687 /* release the hold from vtodv */
690 /* exchange the shadow for the master vnode, if needed */
692 if (error
== 0 && IS_SHADOW(*vpp
, rp
))
699 * See if there's a "lost open" request to be saved and recovered.
702 nfs4open_save_lost_rqst(int error
, nfs4_lost_rqst_t
*lost_rqstp
,
703 nfs4_open_owner_t
*oop
, cred_t
*cr
, vnode_t
*vp
,
704 vnode_t
*dvp
, OPEN4cargs
*open_args
)
709 vfsp
= (dvp
? dvp
->v_vfsp
: vp
->v_vfsp
);
711 if (error
!= ETIMEDOUT
&& error
!= EINTR
&&
712 !NFS4_FRC_UNMT_ERR(error
, vfsp
)) {
713 lost_rqstp
->lr_op
= 0;
717 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
718 "nfs4open_save_lost_rqst: error %d", error
));
720 lost_rqstp
->lr_op
= OP_OPEN
;
723 * The vp (if it is not NULL) and dvp are held and rele'd via
724 * the recovery code. See nfs4_save_lost_rqst.
726 lost_rqstp
->lr_vp
= vp
;
727 lost_rqstp
->lr_dvp
= dvp
;
728 lost_rqstp
->lr_oop
= oop
;
729 lost_rqstp
->lr_osp
= NULL
;
730 lost_rqstp
->lr_lop
= NULL
;
731 lost_rqstp
->lr_cr
= cr
;
732 lost_rqstp
->lr_flk
= NULL
;
733 lost_rqstp
->lr_oacc
= open_args
->share_access
;
734 lost_rqstp
->lr_odeny
= open_args
->share_deny
;
735 lost_rqstp
->lr_oclaim
= open_args
->claim
;
736 if (open_args
->claim
== CLAIM_DELEGATE_CUR
) {
737 lost_rqstp
->lr_ostateid
=
738 open_args
->open_claim4_u
.delegate_cur_info
.delegate_stateid
;
739 srccfp
= open_args
->open_claim4_u
.delegate_cur_info
.cfile
;
741 srccfp
= open_args
->open_claim4_u
.cfile
;
743 lost_rqstp
->lr_ofile
.utf8string_len
= 0;
744 lost_rqstp
->lr_ofile
.utf8string_val
= NULL
;
745 (void) str_to_utf8(srccfp
, &lost_rqstp
->lr_ofile
);
746 lost_rqstp
->lr_putfirst
= FALSE
;
749 struct nfs4_excl_time
{
755 * The OPEN operation creates and/or opens a regular file
760 nfs4open_otw(vnode_t
*dvp
, char *file_name
, struct vattr
*in_va
,
761 vnode_t
**vpp
, cred_t
*cr
, int create_flag
, int open_flag
,
762 enum createmode4 createmode
, int file_just_been_created
)
765 rnode4_t
*drp
= VTOR4(dvp
);
768 bool_t needrecov
= FALSE
;
772 COMPOUND4args_clnt args
;
773 COMPOUND4res_clnt res
;
777 int idx_open
, idx_fattr
;
779 GETFH4res
*gf_res
= NULL
;
780 OPEN4res
*op_res
= NULL
;
783 struct nfs4_excl_time verf
;
784 bool_t did_excl_setup
= FALSE
;
787 OPEN4cargs
*open_args
;
788 nfs4_open_owner_t
*oop
= NULL
;
789 nfs4_open_stream_t
*osp
= NULL
;
791 bool_t retry_open
= FALSE
;
792 nfs4_recov_state_t recov_state
;
793 nfs4_lost_rqst_t lost_rqst
;
794 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
797 cred_t
*cred_otw
= NULL
; /* cred used to do the RPC call */
800 nfs4_sharedfh_t
*otw_sfh
;
801 nfs4_sharedfh_t
*orig_sfh
;
803 int numops
, setgid_flag
;
804 int num_bseqid_retry
= NFS4_NUM_RETRY_BAD_SEQID
+ 1;
807 * Make sure we properly deal with setting the right gid on
808 * a newly created file to reflect the parent's setgid bit
811 if (create_flag
&& in_va
) {
814 * If there is grpid mount flag used or
815 * the parent's directory has the setgid bit set
816 * _and_ the client was able to get a valid mapping
817 * for the parent dir's owner_group, we want to
818 * append NVERIFY(owner_group == dva.va_gid) and
819 * SETATTR to the CREATE compound.
821 mutex_enter(&drp
->r_statelock
);
822 if ((VTOMI4(dvp
)->mi_flags
& MI4_GRPID
||
823 drp
->r_attr
.va_mode
& VSGID
) &&
824 drp
->r_attr
.va_gid
!= GID_NOBODY
) {
825 in_va
->va_mask
|= AT_GID
;
826 in_va
->va_gid
= drp
->r_attr
.va_gid
;
829 mutex_exit(&drp
->r_statelock
);
833 * Normal/non-create compound:
834 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
836 * Open(create) compound no setgid:
837 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
838 * RESTOREFH + GETATTR
840 * Open(create) setgid:
841 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
842 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
843 * NVERIFY(grp) + SETATTR
849 } else if (create_flag
) {
859 args
.array_len
= numops
;
860 argoplist_size
= numops
* sizeof (nfs_argop4
);
861 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
863 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4open_otw: "
864 "open %s open flag 0x%x cred %p", file_name
, open_flag
,
867 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
870 * We are to create a file. Initialize the passed in vnode
876 * Check to see if the client owns a read delegation and is
877 * trying to open for write. If so, then return the delegation
878 * to avoid the server doing a cb_recall and returning DELAY.
879 * NB - we don't use the statev4_lock here because we'd have
880 * to drop the lock anyway and the result would be stale.
882 if ((open_flag
& FWRITE
) &&
883 VTOR4(vpi
)->r_deleg_type
== OPEN_DELEGATE_READ
)
884 (void) nfs4delegreturn(VTOR4(vpi
), NFS4_DR_REOPEN
);
887 * If the file has a delegation, then do an access check up
888 * front. This avoids having to an access check later after
889 * we've already done start_op, which could deadlock.
891 if (VTOR4(vpi
)->r_deleg_type
!= OPEN_DELEGATE_NONE
) {
892 if (open_flag
& FREAD
&&
893 nfs4_access(vpi
, VREAD
, 0, cr
, NULL
) == 0)
895 if (open_flag
& FWRITE
&&
896 nfs4_access(vpi
, VWRITE
, 0, cr
, NULL
) == 0)
903 recov_state
.rs_flags
= 0;
904 recov_state
.rs_num_retry_despite_err
= 0;
909 nfs4_error_zinit(&e
);
911 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
);
915 kmem_free(argop
, argoplist_size
);
919 args
.ctag
= TAG_OPEN
;
920 args
.array_len
= numops
;
923 /* putfh directory fh */
924 argop
[0].argop
= OP_CPUTFH
;
925 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
927 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
928 argop
[idx_open
].argop
= OP_COPEN
;
929 open_args
= &argop
[idx_open
].nfs_argop4_u
.opcopen
;
930 open_args
->claim
= CLAIM_NULL
;
933 open_args
->open_claim4_u
.cfile
= file_name
;
934 open_args
->owner
.owner_len
= 0;
935 open_args
->owner
.owner_val
= NULL
;
939 open_args
->opentype
= OPEN4_CREATE
;
940 open_args
->mode
= createmode
;
941 if (createmode
== EXCLUSIVE4
) {
942 if (did_excl_setup
== FALSE
) {
943 verf
.seconds
= zone_get_hostid(NULL
);
944 if (verf
.seconds
!= 0)
945 verf
.nseconds
= newnum();
950 verf
.seconds
= now
.tv_sec
;
951 verf
.nseconds
= now
.tv_nsec
;
954 * Since the server will use this value for the
955 * mtime, make sure that it can't overflow. Zero
956 * out the MSB. The actual value does not matter
957 * here, only its uniqeness.
959 verf
.seconds
&= INT32_MAX
;
960 did_excl_setup
= TRUE
;
963 /* Now copy over verifier to OPEN4args. */
964 open_args
->createhow4_u
.createverf
= *(uint64_t *)&verf
;
970 attr
= &open_args
->createhow4_u
.createattrs
;
973 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
974 supp_attrs
= svp
->sv_supp_attrs
;
975 nfs_rw_exit(&svp
->sv_lock
);
977 /* GUARDED4 or UNCHECKED4 */
978 v_error
= vattr_to_fattr4(in_va
, NULL
, attr
, 0, OP_OPEN
,
981 bzero(attr
, sizeof (*attr
));
982 nfs4args_copen_free(open_args
);
983 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
,
984 &recov_state
, FALSE
);
987 kmem_free(argop
, argoplist_size
);
993 open_args
->opentype
= OPEN4_NOCREATE
;
996 if (recov_state
.rs_sp
!= NULL
) {
997 mutex_enter(&recov_state
.rs_sp
->s_lock
);
998 open_args
->owner
.clientid
= recov_state
.rs_sp
->clientid
;
999 mutex_exit(&recov_state
.rs_sp
->s_lock
);
1001 /* XXX should we just fail here? */
1002 open_args
->owner
.clientid
= 0;
1006 * This increments oop's ref count or creates a temporary 'just_created'
1007 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1010 mutex_enter(&VTOMI4(dvp
)->mi_lock
);
1012 /* See if a permanent or just created open owner exists */
1013 oop
= find_open_owner_nolock(cr
, NFS4_JUST_CREATED
, VTOMI4(dvp
));
1016 * This open owner does not exist so create a temporary
1019 oop
= create_open_owner(cr
, VTOMI4(dvp
));
1020 ASSERT(oop
!= NULL
);
1022 mutex_exit(&VTOMI4(dvp
)->mi_lock
);
1024 /* this length never changes, do alloc before seqid sync */
1025 open_args
->owner
.owner_len
= sizeof (oop
->oo_name
);
1026 open_args
->owner
.owner_val
=
1027 kmem_alloc(open_args
->owner
.owner_len
, KM_SLEEP
);
1029 e
.error
= nfs4_start_open_seqid_sync(oop
, VTOMI4(dvp
));
1030 if (e
.error
== EAGAIN
) {
1031 open_owner_rele(oop
);
1032 nfs4args_copen_free(open_args
);
1033 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, TRUE
);
1041 /* Check to see if we need to do the OTW call */
1043 if (!nfs4_is_otw_open_necessary(oop
, open_flag
, vpi
,
1044 file_just_been_created
, &e
.error
, acc
, &recov_state
)) {
1047 * The OTW open is not necessary. Either
1048 * the open can succeed without it (eg.
1049 * delegation, error == 0) or the open
1050 * must fail due to an access failure
1051 * (error != 0). In either case, tidy
1055 nfs4_end_open_seqid_sync(oop
);
1056 open_owner_rele(oop
);
1057 nfs4args_copen_free(open_args
);
1058 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, FALSE
);
1061 kmem_free(argop
, argoplist_size
);
1066 bcopy(&oop
->oo_name
, open_args
->owner
.owner_val
,
1067 open_args
->owner
.owner_len
);
1069 seqid
= nfs4_get_open_seqid(oop
) + 1;
1070 open_args
->seqid
= seqid
;
1071 open_args
->share_access
= 0;
1072 if (open_flag
& FREAD
)
1073 open_args
->share_access
|= OPEN4_SHARE_ACCESS_READ
;
1074 if (open_flag
& FWRITE
)
1075 open_args
->share_access
|= OPEN4_SHARE_ACCESS_WRITE
;
1076 open_args
->share_deny
= OPEN4_SHARE_DENY_NONE
;
1081 * getfh w/sanity check for idx_open/idx_fattr
1083 ASSERT((idx_open
+ 1) == (idx_fattr
- 1));
1084 argop
[idx_open
+ 1].argop
= OP_GETFH
;
1087 argop
[idx_fattr
].argop
= OP_GETATTR
;
1088 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1089 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1096 svp
= drp
->r_server
;
1097 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
1098 supp_attrs
= svp
->sv_supp_attrs
;
1099 nfs_rw_exit(&svp
->sv_lock
);
1102 * For setgid case, we need to:
1103 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1105 argop
[4].argop
= OP_SAVEFH
;
1107 argop
[5].argop
= OP_CPUTFH
;
1108 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
1110 argop
[6].argop
= OP_GETATTR
;
1111 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1112 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1114 argop
[7].argop
= OP_RESTOREFH
;
1119 _v
.va_mask
= AT_GID
;
1120 _v
.va_gid
= in_va
->va_gid
;
1121 if (!(e
.error
= nfs4args_verify(&argop
[8], &_v
, OP_NVERIFY
,
1127 * We _know_ we're not messing with AT_SIZE or
1128 * AT_XTIME, so no need for stateid or flags.
1129 * Also we specify NULL rp since we're only
1130 * interested in setting owner_group attributes.
1132 nfs4args_setattr(&argop
[9], &_v
, NULL
, 0, NULL
, cr
,
1133 supp_attrs
, &e
.error
, 0);
1135 nfs4args_verify_free(&argop
[8]);
1140 * XXX - Revisit the last argument to nfs4_end_op()
1141 * once 5020486 is fixed.
1143 nfs4_end_open_seqid_sync(oop
);
1144 open_owner_rele(oop
);
1145 nfs4args_copen_free(open_args
);
1146 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, TRUE
);
1149 kmem_free(argop
, argoplist_size
);
1152 } else if (create_flag
) {
1153 argop
[1].argop
= OP_SAVEFH
;
1155 argop
[5].argop
= OP_RESTOREFH
;
1157 argop
[6].argop
= OP_GETATTR
;
1158 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1159 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
1162 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
1163 "nfs4open_otw: %s call, nm %s, rp %s",
1164 needrecov
? "recov" : "first", file_name
,
1165 rnode4info(VTOR4(dvp
))));
1169 rfs4call(VTOMI4(dvp
), &args
, &res
, cred_otw
, &doqueue
, 0, &e
);
1171 if (!e
.error
&& nfs4_need_to_bump_seqid(&res
))
1172 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
1174 needrecov
= nfs4_needs_recovery(&e
, TRUE
, dvp
->v_vfsp
);
1176 if (e
.error
|| needrecov
) {
1177 bool_t abort
= FALSE
;
1180 nfs4_bseqid_entry_t
*bsep
= NULL
;
1182 nfs4open_save_lost_rqst(e
.error
, &lost_rqst
, oop
,
1183 cred_otw
, vpi
, dvp
, open_args
);
1185 if (!e
.error
&& res
.status
== NFS4ERR_BAD_SEQID
) {
1186 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
1187 vpi
, 0, args
.ctag
, open_args
->seqid
);
1191 abort
= nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
, vpi
,
1192 NULL
, lost_rqst
.lr_op
== OP_OPEN
?
1193 &lost_rqst
: NULL
, OP_OPEN
, bsep
, NULL
, NULL
);
1196 kmem_free(bsep
, sizeof (*bsep
));
1197 /* give up if we keep getting BAD_SEQID */
1198 if (num_bseqid_retry
== 0)
1200 if (abort
== TRUE
&& e
.error
== 0)
1201 e
.error
= geterrno4(res
.status
);
1203 nfs4_end_open_seqid_sync(oop
);
1204 open_owner_rele(oop
);
1205 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1206 nfs4args_copen_free(open_args
);
1208 nfs4args_verify_free(&argop
[8]);
1209 nfs4args_setattr_free(&argop
[9]);
1212 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1217 if (!needrecov
|| abort
== TRUE
|| e
.error
== EINTR
||
1218 NFS4_FRC_UNMT_ERR(e
.error
, dvp
->v_vfsp
)) {
1219 kmem_free(argop
, argoplist_size
);
1226 * Will check and update lease after checking the rflag for
1227 * OPEN_CONFIRM in the successful OPEN call.
1229 if (res
.status
!= NFS4_OK
&& res
.array_len
<= idx_fattr
+ 1) {
1232 * XXX what if we're crossing mount points from server1:/drp
1233 * to server2:/drp/rp.
1236 /* Signal our end of use of the open seqid */
1237 nfs4_end_open_seqid_sync(oop
);
1240 * This will destroy the open owner if it was just created,
1241 * and no one else has put a reference on it.
1243 open_owner_rele(oop
);
1244 if (create_flag
&& (createmode
!= EXCLUSIVE4
) &&
1245 res
.status
== NFS4ERR_BADOWNER
)
1246 nfs4_log_badowner(VTOMI4(dvp
), OP_OPEN
);
1248 e
.error
= geterrno4(res
.status
);
1249 nfs4args_copen_free(open_args
);
1251 nfs4args_verify_free(&argop
[8]);
1252 nfs4args_setattr_free(&argop
[9]);
1254 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1255 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1257 * If the reply is NFS4ERR_ACCESS, it may be because
1258 * we are root (no root net access). If the real uid
1259 * is not root, then retry with the real uid instead.
1265 if (res
.status
== NFS4ERR_ACCESS
&&
1266 (ncr
= crnetadjust(cred_otw
)) != NULL
) {
1270 kmem_free(argop
, argoplist_size
);
1274 resop
= &res
.array
[idx_open
]; /* open res */
1275 op_res
= &resop
->nfs_resop4_u
.opopen
;
1279 * verify attrset bitmap
1282 (createmode
== UNCHECKED4
|| createmode
== GUARDED4
)) {
1283 /* make sure attrset returned is what we asked for */
1284 /* XXX Ignore this 'error' for now */
1285 if (attr
->attrmask
!= op_res
->attrset
)
1290 if (op_res
->rflags
& OPEN4_RESULT_LOCKTYPE_POSIX
) {
1291 mutex_enter(&VTOMI4(dvp
)->mi_lock
);
1292 VTOMI4(dvp
)->mi_flags
|= MI4_POSIX_LOCK
;
1293 mutex_exit(&VTOMI4(dvp
)->mi_lock
);
1296 resop
= &res
.array
[idx_open
+ 1]; /* getfh res */
1297 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
1299 otw_sfh
= sfh4_get(&gf_res
->object
, VTOMI4(dvp
));
1302 * The open stateid has been updated on the server but not
1303 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1304 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1305 * WRITE call. That, however, will use the old stateid, so go ahead
1306 * and upate the open stateid now, before any call to makenfs4node.
1309 nfs4_open_stream_t
*tmp_osp
;
1310 rnode4_t
*tmp_rp
= VTOR4(vpi
);
1312 tmp_osp
= find_open_stream(oop
, tmp_rp
);
1314 tmp_osp
->open_stateid
= op_res
->stateid
;
1315 mutex_exit(&tmp_osp
->os_sync_lock
);
1316 open_stream_rele(tmp_osp
, tmp_rp
);
1320 * We must determine if the file handle given by the otw open
1321 * is the same as the file handle which was passed in with
1322 * *vpp. This case can be reached if the file we are trying
1323 * to open has been removed and another file has been created
1324 * having the same file name. The passed in vnode is released
1327 orig_sfh
= VTOR4(vpi
)->r_fh
;
1328 fh_differs
= nfs4cmpfh(&orig_sfh
->sfh_fh
, &otw_sfh
->sfh_fh
);
1331 garp
= &res
.array
[idx_fattr
].nfs_resop4_u
.opgetattr
.ga_res
;
1333 if (create_flag
|| fh_differs
) {
1336 vp
= makenfs4node(otw_sfh
, garp
, dvp
->v_vfsp
, t
, cr
,
1337 dvp
, fn_get(VTOSV(dvp
)->sv_name
, file_name
, otw_sfh
));
1340 PURGE_ATTRCACHE4(vp
);
1342 * For the newly created vp case, make sure the rnode
1343 * isn't bad before using it.
1345 mutex_enter(&(VTOR4(vp
))->r_statelock
);
1346 if (VTOR4(vp
)->r_flags
& R4RECOVERR
)
1348 mutex_exit(&(VTOR4(vp
))->r_statelock
);
1351 nfs4_end_open_seqid_sync(oop
);
1352 nfs4args_copen_free(open_args
);
1354 nfs4args_verify_free(&argop
[8]);
1355 nfs4args_setattr_free(&argop
[9]);
1357 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1358 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
,
1360 open_owner_rele(oop
);
1364 sfh4_rele(&otw_sfh
);
1365 kmem_free(argop
, argoplist_size
);
1371 sfh4_rele(&otw_sfh
);
1374 * It seems odd to get a full set of attrs and then not update
1375 * the object's attrcache in the non-create case. Create case uses
1376 * the attrs since makenfs4node checks to see if the attrs need to
1377 * be updated (and then updates them). The non-create case should
1378 * update attrs also.
1380 if (! create_flag
&& ! fh_differs
&& !e
.error
) {
1381 nfs4_attr_cache(vp
, garp
, t
, cr
, TRUE
, NULL
);
1384 nfs4_error_zinit(&e
);
1385 if (op_res
->rflags
& OPEN4_RESULT_CONFIRM
) {
1386 /* This does not do recovery for vp explicitly. */
1387 nfs4open_confirm(vp
, &seqid
, &op_res
->stateid
, cred_otw
, FALSE
,
1388 &retry_open
, oop
, FALSE
, &e
, &num_bseqid_retry
);
1390 if (e
.error
|| e
.stat
) {
1391 nfs4_end_open_seqid_sync(oop
);
1392 nfs4args_copen_free(open_args
);
1394 nfs4args_verify_free(&argop
[8]);
1395 nfs4args_setattr_free(&argop
[9]);
1397 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1398 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
,
1400 open_owner_rele(oop
);
1401 if (create_flag
|| fh_differs
) {
1402 /* rele the makenfs4node */
1409 if (retry_open
== TRUE
) {
1410 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
1411 "nfs4open_otw: retry the open since OPEN "
1412 "CONFIRM failed with error %d stat %d",
1414 if (create_flag
&& createmode
== GUARDED4
) {
1415 NFS4_DEBUG(nfs4_client_recov_debug
,
1416 (CE_NOTE
, "nfs4open_otw: switch "
1417 "createmode from GUARDED4 to "
1419 createmode
= UNCHECKED4
;
1424 if (create_flag
&& (createmode
!= EXCLUSIVE4
) &&
1425 e
.stat
== NFS4ERR_BADOWNER
)
1426 nfs4_log_badowner(VTOMI4(dvp
), OP_OPEN
);
1428 e
.error
= geterrno4(e
.stat
);
1430 kmem_free(argop
, argoplist_size
);
1437 mutex_enter(&rp
->r_statev4_lock
);
1440 mutex_exit(&rp
->r_statev4_lock
);
1442 mutex_enter(&oop
->oo_lock
);
1443 /* Doesn't matter if 'oo_just_created' already was set as this */
1444 oop
->oo_just_created
= NFS4_PERM_CREATED
;
1445 if (oop
->oo_cred_otw
)
1446 crfree(oop
->oo_cred_otw
);
1447 oop
->oo_cred_otw
= cred_otw
;
1448 crhold(oop
->oo_cred_otw
);
1449 mutex_exit(&oop
->oo_lock
);
1451 /* returns with 'os_sync_lock' held */
1452 osp
= find_or_create_open_stream(oop
, rp
, &created_osp
);
1454 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
1455 "nfs4open_otw: failed to create an open stream"));
1456 NFS4_DEBUG(nfs4_seqid_sync
, (CE_NOTE
, "nfs4open_otw: "
1457 "signal our end of use of the open seqid"));
1459 nfs4_end_open_seqid_sync(oop
);
1460 open_owner_rele(oop
);
1461 nfs4args_copen_free(open_args
);
1463 nfs4args_verify_free(&argop
[8]);
1464 nfs4args_setattr_free(&argop
[9]);
1466 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1467 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1468 if (create_flag
|| fh_differs
)
1473 kmem_free(argop
, argoplist_size
);
1478 osp
->open_stateid
= op_res
->stateid
;
1480 if (open_flag
& FREAD
)
1481 osp
->os_share_acc_read
++;
1482 if (open_flag
& FWRITE
)
1483 osp
->os_share_acc_write
++;
1484 osp
->os_share_deny_none
++;
1487 * Need to reset this bitfield for the possible case where we were
1488 * going to OTW CLOSE the file, got a non-recoverable error, and before
1489 * we could retry the CLOSE, OPENed the file again.
1491 ASSERT(osp
->os_open_owner
->oo_seqid_inuse
);
1492 osp
->os_final_close
= 0;
1493 osp
->os_force_close
= 0;
1495 if (osp
->os_failed_reopen
)
1496 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
, "nfs4open_otw:"
1497 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1498 (void *)osp
, (void *)cr
, rnode4info(rp
)));
1500 osp
->os_failed_reopen
= 0;
1502 mutex_exit(&osp
->os_sync_lock
);
1504 nfs4_end_open_seqid_sync(oop
);
1506 if (created_osp
&& recov_state
.rs_sp
!= NULL
) {
1507 mutex_enter(&recov_state
.rs_sp
->s_lock
);
1508 nfs4_inc_state_ref_count_nolock(recov_state
.rs_sp
, VTOMI4(dvp
));
1509 mutex_exit(&recov_state
.rs_sp
->s_lock
);
1512 /* get rid of our reference to find oop */
1513 open_owner_rele(oop
);
1515 open_stream_rele(osp
, rp
);
1517 /* accept delegation, if any */
1518 nfs4_delegation_accept(rp
, CLAIM_NULL
, op_res
, garp
, cred_otw
);
1520 nfs4_end_op(VTOMI4(dvp
), dvp
, vpi
, &recov_state
, needrecov
);
1522 if (createmode
== EXCLUSIVE4
&&
1523 (in_va
->va_mask
& ~(AT_GID
| AT_SIZE
))) {
1524 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4open_otw:"
1525 " EXCLUSIVE4: sending a SETATTR"));
1527 * If doing an exclusive create, then generate
1528 * a SETATTR to set the initial attributes.
1529 * Try to set the mtime and the atime to the
1530 * server's current time. It is somewhat
1531 * expected that these fields will be used to
1532 * store the exclusive create cookie. If not,
1533 * server implementors will need to know that
1534 * a SETATTR will follow an exclusive create
1535 * and the cookie should be destroyed if
1538 * The AT_GID and AT_SIZE bits are turned off
1539 * so that the SETATTR request will not attempt
1540 * to process these. The gid will be set
1541 * separately if appropriate. The size is turned
1542 * off because it is assumed that a new file will
1543 * be created empty and if the file wasn't empty,
1544 * then the exclusive create will have failed
1545 * because the file must have existed already.
1546 * Therefore, no truncate operation is needed.
1548 in_va
->va_mask
&= ~(AT_GID
| AT_SIZE
);
1549 in_va
->va_mask
|= (AT_MTIME
| AT_ATIME
);
1551 e
.error
= nfs4setattr(vp
, in_va
, 0, cr
, NULL
);
1554 * Couldn't correct the attributes of
1555 * the newly created file and the
1556 * attributes are wrong. Remove the
1557 * file and return an error to the
1560 /* XXX will this take care of client state ? */
1561 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
1562 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1563 " remove file", e
.error
));
1565 (void) nfs4_remove(dvp
, file_name
, cr
, NULL
, 0);
1567 * Since we've reled the vnode and removed
1568 * the file we now need to return the error.
1569 * At this point we don't want to update the
1570 * dircaches, call nfs4_waitfor_purge_complete
1571 * or set vpp to vp so we need to skip these
1574 goto skip_update_dircaches
;
1579 * If we created or found the correct vnode, due to create_flag or
1580 * fh_differs being set, then update directory cache attribute, readdir
1583 if (create_flag
|| fh_differs
) {
1584 dirattr_info_t dinfo
, *dinfop
;
1587 * Make sure getattr succeeded before using results.
1588 * note: op 7 is getattr(dir) for both flavors of
1591 if (create_flag
&& res
.status
== NFS4_OK
) {
1592 dinfo
.di_time_call
= t
;
1595 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
1601 nfs4_update_dircaches(&op_res
->cinfo
, dvp
, vp
, file_name
,
1606 * If the page cache for this file was flushed from actions
1607 * above, it was done asynchronously and if that is true,
1608 * there is a need to wait here for it to complete. This must
1609 * be done outside of start_fop/end_fop.
1611 (void) nfs4_waitfor_purge_complete(vp
);
1614 * It is implicit that we are in the open case (create_flag == 0) since
1615 * fh_differs can only be set to a non-zero value in the open case.
1617 if (fh_differs
!= 0 && vpi
!= NULL
)
1621 * Be sure to set *vpp to the correct value before returning.
1625 skip_update_dircaches
:
1627 nfs4args_copen_free(open_args
);
1629 nfs4args_verify_free(&argop
[8]);
1630 nfs4args_setattr_free(&argop
[9]);
1632 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1636 kmem_free(argop
, argoplist_size
);
1641 * Reopen an open instance. cf. nfs4open_otw().
1643 * Errors are returned by the nfs4_error_t parameter.
1644 * - ep->error contains an errno value or zero.
1645 * - if it is zero, ep->stat is set to an NFS status code, if any.
1646 * If the file could not be reopened, but the caller should continue, the
1647 * file is marked dead and no error values are returned. If the caller
1648 * should stop recovering open files and start over, either the ep->error
1649 * value or ep->stat will indicate an error (either something that requires
1650 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1651 * filehandles) may be handled silently by this routine.
1652 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1653 * will be started, so the caller should not do it.
1656 * - kill_file : reopen failed in such a fashion to constitute marking the
1657 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1658 * is for cases where recovery is not possible.
1659 * - failed_reopen : same as above, except that the file has already been
1660 * marked dead, so no need to do it again.
1661 * - bailout : reopen failed but we are able to recover and retry the reopen -
1662 * either within this function immediately or via the calling function.
1666 nfs4_reopen(vnode_t
*vp
, nfs4_open_stream_t
*osp
, nfs4_error_t
*ep
,
1667 open_claim_type4 claim
, bool_t frc_use_claim_previous
,
1670 COMPOUND4args_clnt args
;
1671 COMPOUND4res_clnt res
;
1672 nfs_argop4 argop
[4];
1674 OPEN4res
*op_res
= NULL
;
1675 OPEN4cargs
*open_args
;
1677 rnode4_t
*rp
= VTOR4(vp
);
1679 cred_t
*cr
= NULL
, *cred_otw
= NULL
;
1680 nfs4_open_owner_t
*oop
= NULL
;
1682 nfs4_ga_res_t
*garp
;
1683 char fn
[MAXNAMELEN
];
1684 nfs4_recov_state_t recov
= {NULL
, 0};
1685 nfs4_lost_rqst_t lost_rqst
;
1686 mntinfo4_t
*mi
= VTOMI4(vp
);
1688 char *failed_msg
= "";
1691 nfs4_bseqid_entry_t
*bsep
= NULL
;
1693 ASSERT(nfs4_consistent_type(vp
));
1694 ASSERT(nfs_zone() == mi
->mi_zone
);
1696 nfs4_error_zinit(ep
);
1698 /* this is the cred used to find the open owner */
1699 cr
= state_to_cred(osp
);
1701 failed_msg
= "Couldn't reopen: no cred";
1704 /* use this cred for OTW operations */
1705 cred_otw
= nfs4_get_otw_cred(cr
, mi
, osp
->os_open_owner
);
1708 nfs4_error_zinit(ep
);
1710 if (mi
->mi_vfsp
->vfs_flag
& VFS_UNMOUNTED
) {
1711 /* File system has been unmounted, quit */
1713 failed_msg
= "Couldn't reopen: file system has been unmounted";
1717 oop
= osp
->os_open_owner
;
1719 ASSERT(oop
!= NULL
);
1720 if (oop
== NULL
) { /* be defensive in non-DEBUG */
1721 failed_msg
= "can't reopen: no open owner";
1724 open_owner_hold(oop
);
1726 ep
->error
= nfs4_start_open_seqid_sync(oop
, mi
);
1728 open_owner_rele(oop
);
1734 * If the rnode has a delegation and the delegation has been
1735 * recovered and the server didn't request a recall and the caller
1736 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1737 * recovery) and the rnode hasn't been marked dead, then install
1738 * the delegation stateid in the open stream. Otherwise, proceed
1739 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1741 mutex_enter(&rp
->r_statev4_lock
);
1742 if (rp
->r_deleg_type
!= OPEN_DELEGATE_NONE
&&
1743 !rp
->r_deleg_return_pending
&&
1744 (rp
->r_deleg_needs_recovery
== OPEN_DELEGATE_NONE
) &&
1745 !rp
->r_deleg_needs_recall
&&
1746 claim
!= CLAIM_DELEGATE_CUR
&& !frc_use_claim_previous
&&
1747 !(rp
->r_flags
& R4RECOVERR
)) {
1748 mutex_enter(&osp
->os_sync_lock
);
1749 osp
->os_delegation
= 1;
1750 osp
->open_stateid
= rp
->r_deleg_stateid
;
1751 mutex_exit(&osp
->os_sync_lock
);
1752 mutex_exit(&rp
->r_statev4_lock
);
1755 mutex_exit(&rp
->r_statev4_lock
);
1758 * If the file failed recovery, just quit. This failure need not
1759 * affect other reopens, so don't return an error.
1761 mutex_enter(&rp
->r_statelock
);
1762 if (rp
->r_flags
& R4RECOVERR
) {
1763 mutex_exit(&rp
->r_statelock
);
1767 mutex_exit(&rp
->r_statelock
);
1770 * argop is empty here
1772 * PUTFH, OPEN, GETATTR
1774 args
.ctag
= TAG_REOPEN
;
1778 NFS4_DEBUG(nfs4_client_failover_debug
, (CE_NOTE
,
1779 "nfs4_reopen: file is type %d, id %s",
1780 vp
->v_type
, rnode4info(VTOR4(vp
))));
1782 argop
[0].argop
= OP_CPUTFH
;
1784 if (claim
!= CLAIM_PREVIOUS
) {
1786 * if this is a file mount then
1787 * use the mntinfo parentfh
1789 argop
[0].nfs_argop4_u
.opcputfh
.sfh
=
1790 (vp
->v_flag
& VROOT
) ? mi
->mi_srvparentfh
:
1793 /* putfh fh to reopen */
1794 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
1797 argop
[1].argop
= OP_COPEN
;
1798 open_args
= &argop
[1].nfs_argop4_u
.opcopen
;
1799 open_args
->claim
= claim
;
1801 if (claim
== CLAIM_NULL
) {
1803 if ((ep
->error
= vtoname(vp
, fn
, MAXNAMELEN
)) != 0) {
1804 nfs_cmn_err(ep
->error
, CE_WARN
, "nfs4_reopen: vtoname "
1805 "failed for vp 0x%p for CLAIM_NULL with %m",
1807 failed_msg
= "Couldn't reopen: vtoname failed for "
1809 /* nothing allocated yet */
1813 open_args
->open_claim4_u
.cfile
= fn
;
1814 } else if (claim
== CLAIM_PREVIOUS
) {
1817 * We have two cases to deal with here:
1818 * 1) We're being called to reopen files in order to satisfy
1819 * a lock operation request which requires us to explicitly
1820 * reopen files which were opened under a delegation. If
1821 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1822 * that case, frc_use_claim_previous is TRUE and we must
1823 * use the rnode's current delegation type (r_deleg_type).
1824 * 2) We're reopening files during some form of recovery.
1825 * In this case, frc_use_claim_previous is FALSE and we
1826 * use the delegation type appropriate for recovery
1827 * (r_deleg_needs_recovery).
1829 mutex_enter(&rp
->r_statev4_lock
);
1830 open_args
->open_claim4_u
.delegate_type
=
1831 frc_use_claim_previous
?
1833 rp
->r_deleg_needs_recovery
;
1834 mutex_exit(&rp
->r_statev4_lock
);
1836 } else if (claim
== CLAIM_DELEGATE_CUR
) {
1838 if ((ep
->error
= vtoname(vp
, fn
, MAXNAMELEN
)) != 0) {
1839 nfs_cmn_err(ep
->error
, CE_WARN
, "nfs4_reopen: vtoname "
1840 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1841 "with %m", (void *)vp
);
1842 failed_msg
= "Couldn't reopen: vtoname failed for "
1843 "CLAIM_DELEGATE_CUR";
1844 /* nothing allocated yet */
1848 mutex_enter(&rp
->r_statev4_lock
);
1849 open_args
->open_claim4_u
.delegate_cur_info
.delegate_stateid
=
1850 rp
->r_deleg_stateid
;
1851 mutex_exit(&rp
->r_statev4_lock
);
1853 open_args
->open_claim4_u
.delegate_cur_info
.cfile
= fn
;
1855 open_args
->opentype
= OPEN4_NOCREATE
;
1856 open_args
->owner
.clientid
= mi2clientid(mi
);
1857 open_args
->owner
.owner_len
= sizeof (oop
->oo_name
);
1858 open_args
->owner
.owner_val
=
1859 kmem_alloc(open_args
->owner
.owner_len
, KM_SLEEP
);
1860 bcopy(&oop
->oo_name
, open_args
->owner
.owner_val
,
1861 open_args
->owner
.owner_len
);
1862 open_args
->share_access
= 0;
1863 open_args
->share_deny
= 0;
1865 mutex_enter(&osp
->os_sync_lock
);
1866 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
, "nfs4_reopen: osp %p rp "
1867 "%p: read acc %"PRIu64
" write acc %"PRIu64
": open ref count %d: "
1868 "mmap read %"PRIu64
" mmap write %"PRIu64
" claim %d ",
1869 (void *)osp
, (void *)rp
, osp
->os_share_acc_read
,
1870 osp
->os_share_acc_write
, osp
->os_open_ref_count
,
1871 osp
->os_mmap_read
, osp
->os_mmap_write
, claim
));
1873 if (osp
->os_share_acc_read
|| osp
->os_mmap_read
)
1874 open_args
->share_access
|= OPEN4_SHARE_ACCESS_READ
;
1875 if (osp
->os_share_acc_write
|| osp
->os_mmap_write
)
1876 open_args
->share_access
|= OPEN4_SHARE_ACCESS_WRITE
;
1877 if (osp
->os_share_deny_read
)
1878 open_args
->share_deny
|= OPEN4_SHARE_DENY_READ
;
1879 if (osp
->os_share_deny_write
)
1880 open_args
->share_deny
|= OPEN4_SHARE_DENY_WRITE
;
1881 mutex_exit(&osp
->os_sync_lock
);
1883 seqid
= nfs4_get_open_seqid(oop
) + 1;
1884 open_args
->seqid
= seqid
;
1886 /* Construct the getfh part of the compound */
1887 argop
[2].argop
= OP_GETFH
;
1889 /* Construct the getattr part of the compound */
1890 argop
[3].argop
= OP_GETATTR
;
1891 argop
[3].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
1892 argop
[3].nfs_argop4_u
.opgetattr
.mi
= mi
;
1896 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, ep
);
1899 if (!is_recov
&& !frc_use_claim_previous
&&
1900 (ep
->error
== EINTR
|| ep
->error
== ETIMEDOUT
||
1901 NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
))) {
1902 nfs4open_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
1903 cred_otw
, vp
, NULL
, open_args
);
1904 abort
= nfs4_start_recovery(ep
,
1905 VTOMI4(vp
), vp
, NULL
, NULL
,
1906 lost_rqst
.lr_op
== OP_OPEN
?
1907 &lost_rqst
: NULL
, OP_OPEN
, NULL
, NULL
, NULL
);
1908 nfs4args_copen_free(open_args
);
1912 nfs4args_copen_free(open_args
);
1914 if (ep
->error
== EACCES
&& cred_otw
!= cr
) {
1918 nfs4_end_open_seqid_sync(oop
);
1919 open_owner_rele(oop
);
1923 if (ep
->error
== ETIMEDOUT
)
1925 failed_msg
= "Couldn't reopen: rpc error";
1929 if (nfs4_need_to_bump_seqid(&res
))
1930 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
1932 switch (res
.status
) {
1934 if (recov
.rs_flags
& NFS4_RS_DELAY_MSG
) {
1935 mutex_enter(&rp
->r_statelock
);
1936 rp
->r_delay_interval
= 0;
1937 mutex_exit(&rp
->r_statelock
);
1940 case NFS4ERR_BAD_SEQID
:
1941 bsep
= nfs4_create_bseqid_entry(oop
, NULL
, vp
, 0,
1942 args
.ctag
, open_args
->seqid
);
1944 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
,
1945 NULL
, lost_rqst
.lr_op
== OP_OPEN
? &lost_rqst
:
1946 NULL
, OP_OPEN
, bsep
, NULL
, NULL
);
1948 nfs4args_copen_free(open_args
);
1949 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1950 nfs4_end_open_seqid_sync(oop
);
1951 open_owner_rele(oop
);
1953 kmem_free(bsep
, sizeof (*bsep
));
1956 case NFS4ERR_NO_GRACE
:
1957 nfs4args_copen_free(open_args
);
1958 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1959 nfs4_end_open_seqid_sync(oop
);
1960 open_owner_rele(oop
);
1962 if (claim
== CLAIM_PREVIOUS
) {
1964 * Retry as a plain open. We don't need to worry about
1965 * checking the changeinfo: it is acceptable for a
1966 * client to re-open a file and continue processing
1967 * (in the absence of locks).
1969 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
1970 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1971 "will retry as CLAIM_NULL"));
1973 nfs4_mi_kstat_inc_no_grace(mi
);
1977 "Couldn't reopen: tried reclaim outside grace period. ";
1980 nfs4_set_grace_wait(mi
);
1981 nfs4args_copen_free(open_args
);
1982 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1983 nfs4_end_open_seqid_sync(oop
);
1984 open_owner_rele(oop
);
1986 ep
->error
= nfs4_wait_for_grace(mi
, &recov
);
1991 nfs4_set_delay_wait(vp
);
1992 nfs4args_copen_free(open_args
);
1993 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
1994 nfs4_end_open_seqid_sync(oop
);
1995 open_owner_rele(oop
);
1997 ep
->error
= nfs4_wait_for_delay(vp
, &recov
);
1998 nfs4_mi_kstat_inc_delay(mi
);
2002 case NFS4ERR_FHEXPIRED
:
2003 /* recover filehandle and retry */
2004 abort
= nfs4_start_recovery(ep
,
2005 mi
, vp
, NULL
, NULL
, NULL
, OP_OPEN
, NULL
, NULL
, NULL
);
2006 nfs4args_copen_free(open_args
);
2007 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2008 nfs4_end_open_seqid_sync(oop
);
2009 open_owner_rele(oop
);
2013 failed_msg
= "Couldn't reopen: recovery aborted";
2015 case NFS4ERR_RESOURCE
:
2016 case NFS4ERR_STALE_CLIENTID
:
2017 case NFS4ERR_WRONGSEC
:
2018 case NFS4ERR_EXPIRED
:
2020 * Do not mark the file dead and let the calling
2021 * function initiate recovery.
2023 nfs4args_copen_free(open_args
);
2024 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2025 nfs4_end_open_seqid_sync(oop
);
2026 open_owner_rele(oop
);
2029 case NFS4ERR_ACCESS
:
2030 if (cred_otw
!= cr
) {
2034 nfs4args_copen_free(open_args
);
2035 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2036 nfs4_end_open_seqid_sync(oop
);
2037 open_owner_rele(oop
);
2043 NFS4_DEBUG(nfs4_client_failover_debug
, (CE_NOTE
,
2044 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2045 (void*)VTOR4(vp
)->r_server
, (void*)mi
->mi_curr_serv
,
2046 rnode4info(VTOR4(vp
))));
2047 failed_msg
= "Couldn't reopen: NFSv4 error";
2048 nfs4args_copen_free(open_args
);
2049 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2053 resop
= &res
.array
[1]; /* open res */
2054 op_res
= &resop
->nfs_resop4_u
.opopen
;
2056 garp
= &res
.array
[3].nfs_resop4_u
.opgetattr
.ga_res
;
2059 * Check if the path we reopened really is the same
2060 * file. We could end up in a situation where the file
2061 * was removed and a new file created with the same name.
2063 resop
= &res
.array
[2];
2064 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
2065 (void) nfs_rw_enter_sig(&mi
->mi_fh_lock
, RW_READER
, 0);
2066 fh_different
= (nfs4cmpfh(&rp
->r_fh
->sfh_fh
, &gf_res
->object
) != 0);
2068 if (mi
->mi_fh_expire_type
== FH4_PERSISTENT
||
2069 mi
->mi_fh_expire_type
& FH4_NOEXPIRE_WITH_OPEN
) {
2070 /* Oops, we don't have the same file */
2071 if (mi
->mi_fh_expire_type
== FH4_PERSISTENT
)
2072 failed_msg
= "Couldn't reopen: Persistent "
2073 "file handle changed";
2075 failed_msg
= "Couldn't reopen: Volatile "
2076 "(no expire on open) file handle changed";
2078 nfs4args_copen_free(open_args
);
2079 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2080 nfs_rw_exit(&mi
->mi_fh_lock
);
2085 * We have volatile file handles that don't compare.
2086 * If the fids are the same then we assume that the
2087 * file handle expired but the rnode still refers to
2088 * the same file object.
2090 * First check that we have fids or not.
2091 * If we don't we have a dumb server so we will
2092 * just assume every thing is ok for now.
2094 if (!ep
->error
&& garp
->n4g_va
.va_mask
& AT_NODEID
&&
2095 rp
->r_attr
.va_mask
& AT_NODEID
&&
2096 rp
->r_attr
.va_nodeid
!= garp
->n4g_va
.va_nodeid
) {
2098 * We have fids, but they don't
2099 * compare. So kill the file.
2102 "Couldn't reopen: file handle changed"
2103 " due to mismatched fids";
2104 nfs4args_copen_free(open_args
);
2105 (void) xdr_free(xdr_COMPOUND4res_clnt
,
2107 nfs_rw_exit(&mi
->mi_fh_lock
);
2111 * We have volatile file handles that refers
2112 * to the same file (at least they have the
2113 * same fid) or we don't have fids so we
2114 * can't tell. :(. We'll be a kind and accepting
2115 * client so we'll update the rnode's file
2116 * handle with the otw handle.
2118 * We need to drop mi->mi_fh_lock since
2119 * sh4_update acquires it. Since there is
2120 * only one recovery thread there is no
2123 nfs_rw_exit(&mi
->mi_fh_lock
);
2124 sfh4_update(rp
->r_fh
, &gf_res
->object
);
2128 nfs_rw_exit(&mi
->mi_fh_lock
);
2131 ASSERT(nfs4_consistent_type(vp
));
2134 * If the server wanted an OPEN_CONFIRM but that fails, just start
2135 * over. Presumably if there is a persistent error it will show up
2136 * when we resend the OPEN.
2138 if (op_res
->rflags
& OPEN4_RESULT_CONFIRM
) {
2139 bool_t retry_open
= FALSE
;
2141 nfs4open_confirm(vp
, &seqid
, &op_res
->stateid
,
2142 cred_otw
, is_recov
, &retry_open
,
2143 oop
, FALSE
, ep
, NULL
);
2144 if (ep
->error
|| ep
->stat
) {
2145 nfs4args_copen_free(open_args
);
2146 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2147 nfs4_end_open_seqid_sync(oop
);
2148 open_owner_rele(oop
);
2154 mutex_enter(&osp
->os_sync_lock
);
2155 osp
->open_stateid
= op_res
->stateid
;
2156 osp
->os_delegation
= 0;
2158 * Need to reset this bitfield for the possible case where we were
2159 * going to OTW CLOSE the file, got a non-recoverable error, and before
2160 * we could retry the CLOSE, OPENed the file again.
2162 ASSERT(osp
->os_open_owner
->oo_seqid_inuse
);
2163 osp
->os_final_close
= 0;
2164 osp
->os_force_close
= 0;
2165 if (claim
== CLAIM_DELEGATE_CUR
|| claim
== CLAIM_PREVIOUS
)
2166 osp
->os_dc_openacc
= open_args
->share_access
;
2167 mutex_exit(&osp
->os_sync_lock
);
2169 nfs4_end_open_seqid_sync(oop
);
2171 /* accept delegation, if any */
2172 nfs4_delegation_accept(rp
, claim
, op_res
, garp
, cred_otw
);
2174 nfs4args_copen_free(open_args
);
2176 nfs4_attr_cache(vp
, garp
, t
, cr
, TRUE
, NULL
);
2178 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2180 ASSERT(nfs4_consistent_type(vp
));
2182 open_owner_rele(oop
);
2188 nfs4_fail_recov(vp
, failed_msg
, ep
->error
, ep
->stat
);
2190 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
,
2191 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2192 (void *)osp
, (void *)cr
, rnode4info(rp
)));
2193 mutex_enter(&osp
->os_sync_lock
);
2194 osp
->os_failed_reopen
= 1;
2195 mutex_exit(&osp
->os_sync_lock
);
2198 nfs4_end_open_seqid_sync(oop
);
2199 open_owner_rele(oop
);
2203 if (cred_otw
!= NULL
)
2207 /* for . and .. OPENs */
2210 nfs4_open_non_reg_file(vnode_t
**vpp
, int flag
, cred_t
*cr
)
2215 ASSERT(nfs_zone() == VTOMI4(*vpp
)->mi_zone
);
2218 * If close-to-open consistency checking is turned off or
2219 * if there is no cached data, we can avoid
2220 * the over the wire getattr. Otherwise, force a
2221 * call to the server to get fresh attributes and to
2222 * check caches. This is required for close-to-open
2226 if (VTOMI4(*vpp
)->mi_flags
& MI4_NOCTO
||
2227 (rp
->r_dir
== NULL
&& !nfs4_has_pages(*vpp
)))
2230 gar
.n4g_va
.va_mask
= AT_ALL
;
2231 return (nfs4_getattr_otw(*vpp
, &gar
, cr
, 0));
2239 nfs4_close(vnode_t
*vp
, int flag
, int count
, offset_t offset
, cred_t
*cr
,
2240 caller_context_t
*ct
)
2246 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
2249 * Remove client state for this (lockowner, file) pair.
2250 * Issue otw v4 call to have the server do the same.
2256 * zone_enter(2) prevents processes from changing zones with NFS files
2257 * open; if we happen to get here from the wrong zone we can't do
2258 * anything over the wire.
2260 if (VTOMI4(vp
)->mi_zone
!= nfs_zone()) {
2262 * We could attempt to clean up locks, except we're sure
2263 * that the current process didn't acquire any locks on
2264 * the file: any attempt to lock a file belong to another zone
2265 * will fail, and one can't lock an NFS file and then change
2266 * zones, as that fails too.
2268 * Returning an error here is the sane thing to do. A
2269 * subsequent call to VN_RELE() which translates to a
2270 * nfs4_inactive() will clean up state: if the zone of the
2271 * vnode's origin is still alive and kicking, the inactive
2272 * thread will handle the request (from the correct zone), and
2273 * everything (minus the OTW close call) should be OK. If the
2274 * zone is going away nfs4_async_inactive() will throw away
2275 * delegations, open streams and cached pages inline.
2281 * If we are using local locking for this filesystem, then
2282 * release all of the SYSV style record locks. Otherwise,
2283 * we are doing network locking and we need to release all
2284 * of the network locks. All of the locks held by this
2285 * process on this file are released no matter what the
2286 * incoming reference count is.
2288 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
) {
2289 cleanlocks(vp
, ttoproc(curthread
)->p_pid
, 0);
2290 cleanshares(vp
, ttoproc(curthread
)->p_pid
);
2292 e
.error
= nfs4_lockrelease(vp
, flag
, offset
, cr
);
2295 struct lm_sysid
*lmsid
;
2296 lmsid
= nfs4_find_sysid(VTOMI4(vp
));
2297 if (lmsid
== NULL
) {
2298 DTRACE_PROBE2(unknown__sysid
, int, e
.error
,
2301 cleanlocks(vp
, ttoproc(curthread
)->p_pid
,
2302 (lm_sysidt(lmsid
) | LM_SYSID_CLIENT
));
2311 * If the file has been `unlinked', then purge the
2312 * DNLC so that this vnode will get reycled quicker
2313 * and the .nfs* file on the server will get removed.
2315 if (rp
->r_unldvp
!= NULL
)
2319 * If the file was open for write and there are pages,
2320 * do a synchronous flush and commit of all of the
2321 * dirty and uncommitted pages.
2324 if ((flag
& FWRITE
) && nfs4_has_pages(vp
))
2325 error
= nfs4_putpage_commit(vp
, 0, 0, cr
);
2327 mutex_enter(&rp
->r_statelock
);
2328 r_error
= rp
->r_error
;
2330 mutex_exit(&rp
->r_statelock
);
2333 * If this file type is one for which no explicit 'open' was
2334 * done, then bail now (ie. no need for protocol 'close'). If
2335 * there was an error w/the vm subsystem, return _that_ error,
2336 * otherwise, return any errors that may've been reported via
2339 if (vp
->v_type
!= VREG
)
2340 return (error
? error
: r_error
);
2343 * The sync putpage commit may have failed above, but since
2344 * we're working w/a regular file, we need to do the protocol
2345 * 'close' (nfs4close_one will figure out if an otw close is
2346 * needed or not). Report any errors _after_ doing the protocol
2349 nfs4close_one(vp
, NULL
, cr
, flag
, NULL
, &e
, CLOSE_NORM
, 0, 0, 0);
2350 n4error
= e
.error
? e
.error
: geterrno4(e
.stat
);
2353 * Error reporting prio (Hi -> Lo)
2355 * i) nfs4_putpage_commit (error)
2356 * ii) rnode's (r_error)
2357 * iii) nfs4close_one (n4error)
2359 return (error
? error
: (r_error
? r_error
: n4error
));
2363 * Initialize *lost_rqstp.
2367 nfs4close_save_lost_rqst(int error
, nfs4_lost_rqst_t
*lost_rqstp
,
2368 nfs4_open_owner_t
*oop
, nfs4_open_stream_t
*osp
, cred_t
*cr
,
2371 if (error
!= ETIMEDOUT
&& error
!= EINTR
&&
2372 !NFS4_FRC_UNMT_ERR(error
, vp
->v_vfsp
)) {
2373 lost_rqstp
->lr_op
= 0;
2377 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
2378 "nfs4close_save_lost_rqst: error %d", error
));
2380 lost_rqstp
->lr_op
= OP_CLOSE
;
2382 * The vp is held and rele'd via the recovery code.
2383 * See nfs4_save_lost_rqst.
2385 lost_rqstp
->lr_vp
= vp
;
2386 lost_rqstp
->lr_dvp
= NULL
;
2387 lost_rqstp
->lr_oop
= oop
;
2388 lost_rqstp
->lr_osp
= osp
;
2389 ASSERT(osp
!= NULL
);
2390 ASSERT(mutex_owned(&osp
->os_sync_lock
));
2391 osp
->os_pending_close
= 1;
2392 lost_rqstp
->lr_lop
= NULL
;
2393 lost_rqstp
->lr_cr
= cr
;
2394 lost_rqstp
->lr_flk
= NULL
;
2395 lost_rqstp
->lr_putfirst
= FALSE
;
2399 * Assumes you already have the open seqid sync grabbed as well as the
2400 * 'os_sync_lock'. Note: this will release the open seqid sync and
2401 * 'os_sync_lock' if client recovery starts. Calling functions have to
2402 * be prepared to handle this.
2404 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2405 * was needed and was started, and that the calling function should retry
2406 * this function; otherwise it is returned as 0.
2408 * Errors are returned via the nfs4_error_t parameter.
2411 nfs4close_otw(rnode4_t
*rp
, cred_t
*cred_otw
, nfs4_open_owner_t
*oop
,
2412 nfs4_open_stream_t
*osp
, int *recov
, int *did_start_seqid_syncp
,
2413 nfs4_close_type_t close_type
, nfs4_error_t
*ep
, int *have_sync_lockp
)
2415 COMPOUND4args_clnt args
;
2416 COMPOUND4res_clnt res
;
2417 CLOSE4args
*close_args
;
2419 nfs_argop4 argop
[3];
2424 bool_t needrecov
= FALSE
;
2425 nfs4_lost_rqst_t lost_rqst
;
2428 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp
))->mi_zone
);
2430 ASSERT(MUTEX_HELD(&osp
->os_sync_lock
));
2432 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4close_otw"));
2434 /* Only set this to 1 if recovery is started */
2437 /* do the OTW call to close the file */
2439 if (close_type
== CLOSE_RESEND
)
2440 args
.ctag
= TAG_CLOSE_LOST
;
2441 else if (close_type
== CLOSE_AFTER_RESEND
)
2442 args
.ctag
= TAG_CLOSE_UNDO
;
2444 args
.ctag
= TAG_CLOSE
;
2453 /* putfh target fh */
2454 argop
[0].argop
= OP_CPUTFH
;
2455 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
2457 argop
[1].argop
= OP_GETATTR
;
2458 argop
[1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
2459 argop
[1].nfs_argop4_u
.opgetattr
.mi
= mi
;
2461 argop
[2].argop
= OP_CLOSE
;
2462 close_args
= &argop
[2].nfs_argop4_u
.opclose
;
2464 seqid
= nfs4_get_open_seqid(oop
) + 1;
2466 close_args
->seqid
= seqid
;
2467 close_args
->open_stateid
= osp
->open_stateid
;
2469 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
2470 "nfs4close_otw: %s call, rp %s", needrecov
? "recov" : "first",
2475 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, ep
);
2477 if (!ep
->error
&& nfs4_need_to_bump_seqid(&res
)) {
2478 nfs4_set_open_seqid(seqid
, oop
, args
.ctag
);
2481 needrecov
= nfs4_needs_recovery(ep
, TRUE
, mi
->mi_vfsp
);
2482 if (ep
->error
&& !needrecov
) {
2484 * if there was an error and no recovery is to be done
2485 * then then set up the file to flush its cache if
2486 * needed for the next caller.
2488 mutex_enter(&rp
->r_statelock
);
2489 PURGE_ATTRCACHE4_LOCKED(rp
);
2490 rp
->r_flags
&= ~R4WRITEMODIFIED
;
2491 mutex_exit(&rp
->r_statelock
);
2497 nfs4_bseqid_entry_t
*bsep
= NULL
;
2499 if (close_type
!= CLOSE_RESEND
)
2500 nfs4close_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
2503 if (!ep
->error
&& res
.status
== NFS4ERR_BAD_SEQID
)
2504 bsep
= nfs4_create_bseqid_entry(oop
, NULL
, vp
,
2505 0, args
.ctag
, close_args
->seqid
);
2507 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
2508 "nfs4close_otw: initiating recovery. error %d "
2509 "res.status %d", ep
->error
, res
.status
));
2512 * Drop the 'os_sync_lock' here so we don't hit
2513 * a potential recursive mutex_enter via an
2514 * 'open_stream_hold()'.
2516 mutex_exit(&osp
->os_sync_lock
);
2517 *have_sync_lockp
= 0;
2518 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
, NULL
,
2519 (close_type
!= CLOSE_RESEND
&&
2520 lost_rqst
.lr_op
== OP_CLOSE
) ? &lost_rqst
: NULL
,
2521 OP_CLOSE
, bsep
, NULL
, NULL
);
2523 /* drop open seq sync, and let the calling function regrab it */
2524 nfs4_end_open_seqid_sync(oop
);
2525 *did_start_seqid_syncp
= 0;
2528 kmem_free(bsep
, sizeof (*bsep
));
2530 * For signals, the caller wants to quit, so don't say to
2531 * retry. For forced unmount, if it's a user thread, it
2532 * wants to quit. If it's a recovery thread, the retry
2533 * will happen higher-up on the call stack. Either way,
2534 * don't say to retry.
2536 if (abort
== FALSE
&& ep
->error
!= EINTR
&&
2537 !NFS4_FRC_UNMT_ERR(ep
->error
, mi
->mi_vfsp
) &&
2538 close_type
!= CLOSE_RESEND
&&
2539 close_type
!= CLOSE_AFTER_RESEND
)
2545 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2550 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2554 mutex_enter(&rp
->r_statev4_lock
);
2556 mutex_exit(&rp
->r_statev4_lock
);
2558 resop
= &res
.array
[2];
2559 osp
->open_stateid
= resop
->nfs_resop4_u
.opclose
.open_stateid
;
2563 * This removes the reference obtained at OPEN; ie, when the
2564 * open stream structure was created.
2566 * We don't have to worry about calling 'open_stream_rele'
2567 * since we our currently holding a reference to the open
2568 * stream which means the count cannot go to 0 with this
2571 ASSERT(osp
->os_ref_count
>= 2);
2572 osp
->os_ref_count
--;
2576 &res
.array
[1].nfs_resop4_u
.opgetattr
.ga_res
,
2577 t
, cred_otw
, TRUE
, NULL
);
2579 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
, "nfs4close_otw:"
2580 " returning %d", ep
->error
));
2582 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
2587 nfs4_read(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
2588 caller_context_t
*ct
)
2602 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
2604 if (IS_SHADOW(vp
, rp
))
2607 if (vp
->v_type
!= VREG
)
2612 if (nfs_zone() != mi
->mi_zone
)
2615 if (uiop
->uio_resid
== 0)
2618 if (uiop
->uio_loffset
< 0 || uiop
->uio_loffset
+ uiop
->uio_resid
< 0)
2621 mutex_enter(&rp
->r_statelock
);
2622 if (rp
->r_flags
& R4RECOVERRP
)
2623 error
= (rp
->r_error
? rp
->r_error
: EIO
);
2626 mutex_exit(&rp
->r_statelock
);
2631 * Bypass VM if caching has been disabled (e.g., locking) or if
2632 * using client-side direct I/O and the file is not mmap'd and
2633 * there are no cached pages.
2635 if ((vp
->v_flag
& VNOCACHE
) ||
2636 (((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
)) &&
2637 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 && !nfs4_has_pages(vp
))) {
2640 return (nfs4read(vp
, NULL
, uiop
->uio_loffset
,
2641 uiop
->uio_resid
, &resid
, cr
, FALSE
, uiop
));
2647 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
2648 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
2649 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
2651 if (error
= nfs4_validate_caches(vp
, cr
))
2654 mutex_enter(&rp
->r_statelock
);
2655 while (rp
->r_flags
& R4INCACHEPURGE
) {
2656 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
2657 mutex_exit(&rp
->r_statelock
);
2661 diff
= rp
->r_size
- uiop
->uio_loffset
;
2662 mutex_exit(&rp
->r_statelock
);
2672 error
= vpm_data_copy(vp
, off
+ on
, n
, uiop
,
2673 1, NULL
, 0, S_READ
);
2675 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
, n
, 1,
2678 error
= uiomove(base
+ on
, n
, UIO_READ
, uiop
);
2683 * If read a whole block or read to eof,
2684 * won't need this buffer again soon.
2686 mutex_enter(&rp
->r_statelock
);
2687 if (n
+ on
== MAXBSIZE
||
2688 uiop
->uio_loffset
== rp
->r_size
)
2689 flags
= SM_DONTNEED
;
2692 mutex_exit(&rp
->r_statelock
);
2694 error
= vpm_sync_pages(vp
, off
, n
, flags
);
2696 error
= segmap_release(segkmap
, base
, flags
);
2700 (void) vpm_sync_pages(vp
, off
, n
, 0);
2702 (void) segmap_release(segkmap
, base
, 0);
2705 } while (!error
&& uiop
->uio_resid
> 0);
2712 nfs4_write(vnode_t
*vp
, struct uio
*uiop
, int ioflag
, cred_t
*cr
,
2713 caller_context_t
*ct
)
2715 rlim64_t limit
= uiop
->uio_llimit
;
2731 if (IS_SHADOW(vp
, rp
))
2734 if (vp
->v_type
!= VREG
)
2739 if (nfs_zone() != mi
->mi_zone
)
2742 if (uiop
->uio_resid
== 0)
2745 mutex_enter(&rp
->r_statelock
);
2746 if (rp
->r_flags
& R4RECOVERRP
)
2747 error
= (rp
->r_error
? rp
->r_error
: EIO
);
2750 mutex_exit(&rp
->r_statelock
);
2754 if (ioflag
& FAPPEND
) {
2758 * Must serialize if appending.
2760 if (nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
)) {
2761 nfs_rw_exit(&rp
->r_rwlock
);
2762 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
,
2767 va
.va_mask
= AT_SIZE
;
2768 error
= nfs4getattr(vp
, &va
, cr
);
2771 uiop
->uio_loffset
= va
.va_size
;
2774 offset
= uiop
->uio_loffset
+ uiop
->uio_resid
;
2776 if (uiop
->uio_loffset
< (offset_t
)0 || offset
< 0)
2779 if (limit
== RLIM64_INFINITY
|| limit
> MAXOFFSET_T
)
2780 limit
= MAXOFFSET_T
;
2783 * Check to make sure that the process will not exceed
2784 * its limit on file size. It is okay to write up to
2785 * the limit, but not beyond. Thus, the write which
2786 * reaches the limit will be short and the next write
2787 * will return an error.
2790 if (offset
> uiop
->uio_llimit
) {
2791 remainder
= offset
- uiop
->uio_llimit
;
2792 uiop
->uio_resid
= uiop
->uio_llimit
- uiop
->uio_loffset
;
2793 if (uiop
->uio_resid
<= 0) {
2794 proc_t
*p
= ttoproc(curthread
);
2796 uiop
->uio_resid
+= remainder
;
2797 mutex_enter(&p
->p_lock
);
2798 (void) rctl_action(rctlproc_legacy
[RLIMIT_FSIZE
],
2799 p
->p_rctls
, p
, RCA_UNSAFE_SIGINFO
);
2800 mutex_exit(&p
->p_lock
);
2805 /* update the change attribute, if we have a write delegation */
2807 mutex_enter(&rp
->r_statev4_lock
);
2808 if (rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
)
2809 rp
->r_deleg_change
++;
2811 mutex_exit(&rp
->r_statev4_lock
);
2813 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR4(vp
)))
2817 * Bypass VM if caching has been disabled (e.g., locking) or if
2818 * using client-side direct I/O and the file is not mmap'd and
2819 * there are no cached pages.
2821 if ((vp
->v_flag
& VNOCACHE
) ||
2822 (((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
)) &&
2823 rp
->r_mapcnt
== 0 && rp
->r_inmap
== 0 && !nfs4_has_pages(vp
))) {
2826 u_offset_t org_offset
;
2827 stable_how4 stab_comm
;
2829 if (rp
->r_flags
& R4STALE
) {
2830 resid
= uiop
->uio_resid
;
2831 offset
= uiop
->uio_loffset
;
2832 error
= rp
->r_error
;
2834 * A close may have cleared r_error, if so,
2835 * propagate ESTALE error return properly
2842 bufsize
= MIN(uiop
->uio_resid
, mi
->mi_stsize
);
2843 base
= kmem_alloc(bufsize
, KM_SLEEP
);
2845 if (ioflag
& FDSYNC
)
2846 stab_comm
= DATA_SYNC4
;
2848 stab_comm
= FILE_SYNC4
;
2849 resid
= uiop
->uio_resid
;
2850 offset
= uiop
->uio_loffset
;
2851 count
= MIN(uiop
->uio_resid
, bufsize
);
2852 org_offset
= uiop
->uio_loffset
;
2853 error
= uiomove(base
, count
, UIO_WRITE
, uiop
);
2855 error
= nfs4write(vp
, base
, org_offset
,
2856 count
, cr
, &stab_comm
);
2858 mutex_enter(&rp
->r_statelock
);
2859 if (rp
->r_size
< uiop
->uio_loffset
)
2860 rp
->r_size
= uiop
->uio_loffset
;
2861 mutex_exit(&rp
->r_statelock
);
2864 } while (!error
&& uiop
->uio_resid
> 0);
2865 kmem_free(base
, bufsize
);
2869 bsize
= vp
->v_vfsp
->vfs_bsize
;
2872 off
= uiop
->uio_loffset
& MAXBMASK
; /* mapping offset */
2873 on
= uiop
->uio_loffset
& MAXBOFFSET
; /* Relative offset */
2874 n
= MIN(MAXBSIZE
- on
, uiop
->uio_resid
);
2876 resid
= uiop
->uio_resid
;
2877 offset
= uiop
->uio_loffset
;
2879 if (rp
->r_flags
& R4STALE
) {
2880 error
= rp
->r_error
;
2882 * A close may have cleared r_error, if so,
2883 * propagate ESTALE error return properly
2891 * Don't create dirty pages faster than they
2892 * can be cleaned so that the system doesn't
2893 * get imbalanced. If the async queue is
2894 * maxed out, then wait for it to drain before
2895 * creating more dirty pages. Also, wait for
2896 * any threads doing pagewalks in the vop_getattr
2897 * entry points so that they don't block for
2900 mutex_enter(&rp
->r_statelock
);
2901 while ((mi
->mi_max_threads
!= 0 &&
2902 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
2905 klwp_t
*lwp
= ttolwp(curthread
);
2909 if (!cv_wait_sig(&rp
->r_cv
, &rp
->r_statelock
)) {
2910 mutex_exit(&rp
->r_statelock
);
2919 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
2921 mutex_exit(&rp
->r_statelock
);
2924 * Touch the page and fault it in if it is not in core
2925 * before segmap_getmapflt or vpm_data_copy can lock it.
2926 * This is to avoid the deadlock if the buffer is mapped
2927 * to the same file through mmap which we want to write.
2929 uio_prefaultpages((long)n
, uiop
);
2933 * It will use kpm mappings, so no need to
2936 error
= writerp4(rp
, NULL
, n
, uiop
, 0);
2939 int pon
= uiop
->uio_loffset
& PAGEOFFSET
;
2940 size_t pn
= MIN(PAGESIZE
- pon
,
2944 mutex_enter(&rp
->r_statelock
);
2945 pagecreate
= (pon
== 0) && (pn
== PAGESIZE
||
2946 uiop
->uio_loffset
+ pn
>= rp
->r_size
);
2947 mutex_exit(&rp
->r_statelock
);
2949 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
2950 pn
, !pagecreate
, S_WRITE
);
2952 error
= writerp4(rp
, base
+ pon
, n
, uiop
,
2956 base
= segmap_getmapflt(segkmap
, vp
, off
+ on
,
2958 error
= writerp4(rp
, base
+ on
, n
, uiop
, 0);
2963 if (mi
->mi_flags
& MI4_NOAC
)
2965 else if ((uiop
->uio_loffset
% bsize
) == 0 ||
2968 * Have written a whole block.
2969 * Start an asynchronous write
2970 * and mark the buffer to
2971 * indicate that it won't be
2972 * needed again soon.
2974 flags
= SM_WRITE
| SM_ASYNC
| SM_DONTNEED
;
2977 if ((ioflag
& (FSYNC
|FDSYNC
)) ||
2978 (rp
->r_flags
& R4OUTOFSPACE
)) {
2983 error
= vpm_sync_pages(vp
, off
, n
, flags
);
2985 error
= segmap_release(segkmap
, base
, flags
);
2989 (void) vpm_sync_pages(vp
, off
, n
, 0);
2991 (void) segmap_release(segkmap
, base
, 0);
2994 * In the event that we got an access error while
2995 * faulting in a page for a write-only file just
2998 if (error
== EACCES
)
3001 } while (!error
&& uiop
->uio_resid
> 0);
3005 uiop
->uio_resid
= resid
+ remainder
;
3006 uiop
->uio_loffset
= offset
;
3008 uiop
->uio_resid
+= remainder
;
3010 mutex_enter(&rp
->r_statev4_lock
);
3011 if (rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
) {
3012 gethrestime(&rp
->r_attr
.va_mtime
);
3013 rp
->r_attr
.va_ctime
= rp
->r_attr
.va_mtime
;
3015 mutex_exit(&rp
->r_statev4_lock
);
3018 nfs_rw_exit(&rp
->r_lkserlock
);
3024 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3027 nfs4_rdwrlbn(vnode_t
*vp
, page_t
*pp
, u_offset_t off
, size_t len
,
3028 int flags
, cred_t
*cr
)
3034 stable_how4 stab_comm
;
3036 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
3037 bp
= pageio_setup(pp
, len
, vp
, flags
);
3041 * pageio_setup should have set b_addr to 0. This
3042 * is correct since we want to do I/O on a page
3043 * boundary. bp_mapin will use this addr to calculate
3044 * an offset, and then set b_addr to the kernel virtual
3045 * address it allocated for us.
3047 ASSERT(bp
->b_un
.b_addr
== 0);
3051 bp
->b_lblkno
= lbtodb(off
);
3053 bp
->b_offset
= (offset_t
)off
;
3056 if ((flags
& (B_WRITE
|B_ASYNC
)) == (B_WRITE
|B_ASYNC
) &&
3058 stab_comm
= UNSTABLE4
;
3060 stab_comm
= FILE_SYNC4
;
3062 error
= nfs4_bio(bp
, &stab_comm
, cr
, FALSE
);
3067 if (stab_comm
== UNSTABLE4
)
3068 fsdata
= C_DELAYCOMMIT
;
3070 fsdata
= C_NOCOMMIT
;
3074 pp
->p_fsdata
= fsdata
;
3075 } while ((pp
= pp
->p_next
) != savepp
);
3083 nfs4rdwr_check_osid(vnode_t
*vp
, nfs4_error_t
*ep
, cred_t
*cr
)
3085 nfs4_open_owner_t
*oop
;
3086 nfs4_open_stream_t
*osp
;
3087 rnode4_t
*rp
= VTOR4(vp
);
3088 mntinfo4_t
*mi
= VTOMI4(vp
);
3091 ASSERT(nfs_zone() == mi
->mi_zone
);
3094 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
3098 /* returns with 'os_sync_lock' held */
3099 osp
= find_open_stream(oop
, rp
);
3101 open_owner_rele(oop
);
3105 if (osp
->os_failed_reopen
) {
3106 mutex_exit(&osp
->os_sync_lock
);
3107 open_stream_rele(osp
, rp
);
3108 open_owner_rele(oop
);
3113 * Determine whether a reopen is needed. If this
3114 * is a delegation open stream, then the os_delegation bit
3118 reopen_needed
= osp
->os_delegation
;
3120 mutex_exit(&osp
->os_sync_lock
);
3121 open_owner_rele(oop
);
3123 if (reopen_needed
) {
3124 nfs4_error_zinit(ep
);
3125 nfs4_reopen(vp
, osp
, ep
, CLAIM_NULL
, FALSE
, FALSE
);
3126 mutex_enter(&osp
->os_sync_lock
);
3127 if (ep
->error
|| ep
->stat
|| osp
->os_failed_reopen
) {
3128 mutex_exit(&osp
->os_sync_lock
);
3129 open_stream_rele(osp
, rp
);
3132 mutex_exit(&osp
->os_sync_lock
);
3134 open_stream_rele(osp
, rp
);
3140 * Write to file. Writes to remote server in largest size
3141 * chunks that the server can handle. Write is synchronous.
3144 nfs4write(vnode_t
*vp
, caddr_t base
, u_offset_t offset
, int count
, cred_t
*cr
,
3145 stable_how4
*stab_comm
)
3148 COMPOUND4args_clnt args
;
3149 COMPOUND4res_clnt res
;
3152 nfs_argop4 argop
[2];
3159 nfs4_recov_state_t recov_state
;
3160 nfs4_stateid_types_t sid_types
;
3161 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3167 ASSERT(nfs_zone() == mi
->mi_zone
);
3169 stable
= *stab_comm
;
3170 *stab_comm
= FILE_SYNC4
;
3173 recov_state
.rs_flags
= 0;
3174 recov_state
.rs_num_retry_despite_err
= 0;
3175 nfs4_init_stateid_types(&sid_types
);
3177 /* Is curthread the recovery thread? */
3178 mutex_enter(&mi
->mi_lock
);
3179 recov
= (mi
->mi_recovthread
== curthread
);
3180 mutex_exit(&mi
->mi_lock
);
3183 args
.ctag
= TAG_WRITE
;
3188 e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3189 &recov_state
, NULL
);
3194 /* 0. putfh target fh */
3195 argop
[0].argop
= OP_CPUTFH
;
3196 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3199 nfs4args_write(&argop
[1], stable
, rp
, cr
, &wargs
, &sid_types
);
3203 wargs
->offset
= (offset4
)offset
;
3204 wargs
->data_val
= base
;
3206 if (mi
->mi_io_kstats
) {
3207 mutex_enter(&mi
->mi_lock
);
3208 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3209 mutex_exit(&mi
->mi_lock
);
3212 if ((vp
->v_flag
& VNOCACHE
) ||
3213 (rp
->r_flags
& R4DIRECTIO
) ||
3214 (mi
->mi_flags
& MI4_DIRECTIO
))
3215 tsize
= MIN(mi
->mi_stsize
, count
);
3217 tsize
= MIN(mi
->mi_curwrite
, count
);
3218 wargs
->data_len
= (uint_t
)tsize
;
3219 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
3221 if (mi
->mi_io_kstats
) {
3222 mutex_enter(&mi
->mi_lock
);
3223 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3224 mutex_exit(&mi
->mi_lock
);
3228 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
3229 if (e
.error
&& !needrecov
) {
3230 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3231 &recov_state
, needrecov
);
3240 * Do handling of OLD_STATEID outside
3241 * of the normal recovery framework.
3243 * If write receives a BAD stateid error while using a
3244 * delegation stateid, retry using the open stateid (if it
3245 * exists). If it doesn't have an open stateid, reopen the
3246 * file first, then retry.
3248 if (!e
.error
&& res
.status
== NFS4ERR_OLD_STATEID
&&
3249 sid_types
.cur_sid_type
!= SPEC_SID
) {
3250 nfs4_save_stateid(&wargs
->stateid
, &sid_types
);
3252 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3253 &recov_state
, needrecov
);
3254 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3256 } else if (e
.error
== 0 && res
.status
== NFS4ERR_BAD_STATEID
&&
3257 sid_types
.cur_sid_type
== DEL_SID
) {
3258 nfs4_save_stateid(&wargs
->stateid
, &sid_types
);
3259 mutex_enter(&rp
->r_statev4_lock
);
3260 rp
->r_deleg_return_pending
= TRUE
;
3261 mutex_exit(&rp
->r_statev4_lock
);
3262 if (nfs4rdwr_check_osid(vp
, &e
, cr
)) {
3264 nfs4_end_fop(mi
, vp
, NULL
, OH_WRITE
,
3265 &recov_state
, needrecov
);
3266 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3271 nfs4_end_fop(mi
, vp
, NULL
, OH_WRITE
,
3272 &recov_state
, needrecov
);
3273 /* hold needed for nfs4delegreturn_thread */
3275 nfs4delegreturn_async(rp
, (NFS4_DR_PUSH
|NFS4_DR_REOPEN
|
3276 NFS4_DR_DISCARD
), FALSE
);
3277 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3284 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
3285 "nfs4write: client got error %d, res.status %d"
3286 ", so start recovery", e
.error
, res
.status
));
3288 abort
= nfs4_start_recovery(&e
,
3289 VTOMI4(vp
), vp
, NULL
, &wargs
->stateid
,
3290 NULL
, OP_WRITE
, NULL
, NULL
, NULL
);
3292 e
.error
= geterrno4(res
.status
);
3293 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3296 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3297 &recov_state
, needrecov
);
3304 e
.error
= geterrno4(res
.status
);
3305 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3307 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3308 &recov_state
, needrecov
);
3312 resop
= &res
.array
[1]; /* write res */
3313 wres
= &resop
->nfs_resop4_u
.opwrite
;
3315 if ((int)wres
->count
> tsize
) {
3316 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3318 zcmn_err(getzoneid(), CE_WARN
,
3319 "nfs4write: server wrote %u, requested was %u",
3320 (int)wres
->count
, tsize
);
3322 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
,
3323 &recov_state
, needrecov
);
3326 if (wres
->committed
== UNSTABLE4
) {
3327 *stab_comm
= UNSTABLE4
;
3328 if (wargs
->stable
== DATA_SYNC4
||
3329 wargs
->stable
== FILE_SYNC4
) {
3330 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3332 zcmn_err(getzoneid(), CE_WARN
,
3333 "nfs4write: server %s did not commit "
3334 "to stable storage",
3335 rp
->r_server
->sv_hostname
);
3337 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
,
3338 OH_WRITE
, &recov_state
, needrecov
);
3343 tsize
= (int)wres
->count
;
3347 if (mi
->mi_io_kstats
) {
3348 mutex_enter(&mi
->mi_lock
);
3349 KSTAT_IO_PTR(mi
->mi_io_kstats
)->writes
++;
3350 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nwritten
+=
3352 mutex_exit(&mi
->mi_lock
);
3354 lwp_stat_update(LWP_STAT_OUBLK
, 1);
3355 mutex_enter(&rp
->r_statelock
);
3356 if (rp
->r_flags
& R4HAVEVERF
) {
3357 if (rp
->r_writeverf
!= wres
->writeverf
) {
3359 rp
->r_writeverf
= wres
->writeverf
;
3362 rp
->r_writeverf
= wres
->writeverf
;
3363 rp
->r_flags
|= R4HAVEVERF
;
3365 PURGE_ATTRCACHE4_LOCKED(rp
);
3366 rp
->r_flags
|= R4WRITEMODIFIED
;
3367 gethrestime(&rp
->r_attr
.va_mtime
);
3368 rp
->r_attr
.va_ctime
= rp
->r_attr
.va_mtime
;
3369 mutex_exit(&rp
->r_statelock
);
3370 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3374 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_WRITE
, &recov_state
,
3381 * Read from a file. Reads data in largest chunks our interface can handle.
3384 nfs4read(vnode_t
*vp
, caddr_t base
, offset_t offset
, int count
,
3385 size_t *residp
, cred_t
*cr
, bool_t async
, struct uio
*uiop
)
3388 COMPOUND4args_clnt args
;
3389 COMPOUND4res_clnt res
;
3391 nfs_argop4 argop
[2];
3397 bool_t needrecov
= FALSE
;
3398 nfs4_recov_state_t recov_state
;
3399 nfs4_stateid_types_t sid_types
;
3400 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3406 ASSERT(nfs_zone() == mi
->mi_zone
);
3408 args
.ctag
= async
? TAG_READAHEAD
: TAG_READ
;
3413 nfs4_init_stateid_types(&sid_types
);
3415 recov_state
.rs_flags
= 0;
3416 recov_state
.rs_num_retry_despite_err
= 0;
3419 e
.error
= nfs4_start_fop(mi
, vp
, NULL
, OH_READ
,
3420 &recov_state
, NULL
);
3424 /* putfh target fh */
3425 argop
[0].argop
= OP_CPUTFH
;
3426 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3429 argop
[1].argop
= OP_READ
;
3430 rargs
= &argop
[1].nfs_argop4_u
.opread
;
3431 rargs
->stateid
= nfs4_get_stateid(cr
, rp
, curproc
->p_pidp
->pid_id
, mi
,
3432 OP_READ
, &sid_types
, async
);
3435 if (mi
->mi_io_kstats
) {
3436 mutex_enter(&mi
->mi_lock
);
3437 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3438 mutex_exit(&mi
->mi_lock
);
3441 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
3442 "nfs4read: %s call, rp %s",
3443 needrecov
? "recov" : "first",
3446 if ((vp
->v_flag
& VNOCACHE
) ||
3447 (rp
->r_flags
& R4DIRECTIO
) ||
3448 (mi
->mi_flags
& MI4_DIRECTIO
))
3449 tsize
= MIN(mi
->mi_tsize
, count
);
3451 tsize
= MIN(mi
->mi_curread
, count
);
3453 rargs
->offset
= (offset4
)offset
;
3454 rargs
->count
= (count4
)tsize
;
3455 rargs
->res_data_val_alt
= NULL
;
3456 rargs
->res_mblk
= NULL
;
3457 rargs
->res_uiop
= NULL
;
3458 rargs
->res_maxsize
= 0;
3459 rargs
->wlist
= NULL
;
3462 rargs
->res_uiop
= uiop
;
3464 rargs
->res_data_val_alt
= base
;
3465 rargs
->res_maxsize
= tsize
;
3467 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
3469 if (nfs4read_error_inject
) {
3470 res
.status
= nfs4read_error_inject
;
3471 nfs4read_error_inject
= 0;
3475 if (mi
->mi_io_kstats
) {
3476 mutex_enter(&mi
->mi_lock
);
3477 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
3478 mutex_exit(&mi
->mi_lock
);
3481 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
3482 if (e
.error
!= 0 && !needrecov
) {
3483 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3484 &recov_state
, needrecov
);
3489 * Do proper retry for OLD and BAD stateid errors outside
3490 * of the normal recovery framework. There are two differences
3491 * between async and sync reads. The first is that we allow
3492 * retry on BAD_STATEID for async reads, but not sync reads.
3493 * The second is that we mark the file dead for a failed
3494 * attempt with a special stateid for sync reads, but just
3495 * return EIO for async reads.
3497 * If a sync read receives a BAD stateid error while using a
3498 * delegation stateid, retry using the open stateid (if it
3499 * exists). If it doesn't have an open stateid, reopen the
3500 * file first, then retry.
3502 if (e
.error
== 0 && (res
.status
== NFS4ERR_OLD_STATEID
||
3503 res
.status
== NFS4ERR_BAD_STATEID
) && async
) {
3504 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3505 &recov_state
, needrecov
);
3506 if (sid_types
.cur_sid_type
== SPEC_SID
) {
3507 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3511 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3512 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3514 } else if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3515 !async
&& sid_types
.cur_sid_type
!= SPEC_SID
) {
3516 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3517 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3518 &recov_state
, needrecov
);
3519 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3521 } else if (e
.error
== 0 && res
.status
== NFS4ERR_BAD_STATEID
&&
3522 sid_types
.cur_sid_type
== DEL_SID
) {
3523 nfs4_save_stateid(&rargs
->stateid
, &sid_types
);
3524 mutex_enter(&rp
->r_statev4_lock
);
3525 rp
->r_deleg_return_pending
= TRUE
;
3526 mutex_exit(&rp
->r_statev4_lock
);
3527 if (nfs4rdwr_check_osid(vp
, &e
, cr
)) {
3528 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3529 &recov_state
, needrecov
);
3530 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3534 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3535 &recov_state
, needrecov
);
3536 /* hold needed for nfs4delegreturn_thread */
3538 nfs4delegreturn_async(rp
, (NFS4_DR_PUSH
|NFS4_DR_REOPEN
|
3539 NFS4_DR_DISCARD
), FALSE
);
3540 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3546 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
3547 "nfs4read: initiating recovery\n"));
3548 abort
= nfs4_start_recovery(&e
,
3549 mi
, vp
, NULL
, &rargs
->stateid
,
3550 NULL
, OP_READ
, NULL
, NULL
, NULL
);
3551 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3552 &recov_state
, needrecov
);
3554 * Do not retry if we got OLD_STATEID using a special
3555 * stateid. This avoids looping with a broken server.
3557 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3558 sid_types
.cur_sid_type
== SPEC_SID
)
3561 if (abort
== FALSE
) {
3563 * Need to retry all possible stateids in
3564 * case the recovery error wasn't stateid
3565 * related or the stateids have become
3566 * stale (server reboot).
3568 nfs4_init_stateid_types(&sid_types
);
3569 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3575 e
.error
= geterrno4(res
.status
);
3576 (void) xdr_free(xdr_COMPOUND4res_clnt
,
3583 e
.error
= geterrno4(res
.status
);
3584 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
,
3585 &recov_state
, needrecov
);
3586 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3590 data_len
= res
.array
[1].nfs_resop4_u
.opread
.data_len
;
3595 if (mi
->mi_io_kstats
) {
3596 mutex_enter(&mi
->mi_lock
);
3597 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
3598 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= data_len
;
3599 mutex_exit(&mi
->mi_lock
);
3601 lwp_stat_update(LWP_STAT_INBLK
, 1);
3602 is_eof
= res
.array
[1].nfs_resop4_u
.opread
.eof
;
3603 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
3605 } while (count
&& !is_eof
);
3609 nfs4_end_fop(mi
, vp
, NULL
, OH_READ
, &recov_state
, needrecov
);
3616 nfs4_ioctl(vnode_t
*vp
, int cmd
, intptr_t arg
, int flag
, cred_t
*cr
, int *rvalp
,
3617 caller_context_t
*ct
)
3619 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3623 return (nfs4_directio(vp
, (int)arg
, cr
));
3631 nfs4_getattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3632 caller_context_t
*ct
)
3635 rnode4_t
*rp
= VTOR4(vp
);
3637 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3640 * If it has been specified that the return value will
3641 * just be used as a hint, and we are only being asked
3642 * for size, fsid or rdevid, then return the client's
3643 * notion of these values without checking to make sure
3644 * that the attribute cache is up to date.
3645 * The whole point is to avoid an over the wire GETATTR
3648 if (flags
& ATTR_HINT
) {
3649 if (!(vap
->va_mask
& ~(AT_SIZE
| AT_FSID
| AT_RDEV
))) {
3650 mutex_enter(&rp
->r_statelock
);
3651 if (vap
->va_mask
& AT_SIZE
)
3652 vap
->va_size
= rp
->r_size
;
3653 if (vap
->va_mask
& AT_FSID
)
3654 vap
->va_fsid
= rp
->r_attr
.va_fsid
;
3655 if (vap
->va_mask
& AT_RDEV
)
3656 vap
->va_rdev
= rp
->r_attr
.va_rdev
;
3657 mutex_exit(&rp
->r_statelock
);
3663 * Only need to flush pages if asking for the mtime
3664 * and if there any dirty pages or any outstanding
3665 * asynchronous (write) requests for this file.
3667 if (vap
->va_mask
& AT_MTIME
) {
3669 if (nfs4_has_pages(vp
)) {
3670 mutex_enter(&rp
->r_statev4_lock
);
3671 if (rp
->r_deleg_type
!= OPEN_DELEGATE_WRITE
) {
3672 mutex_exit(&rp
->r_statev4_lock
);
3673 if (rp
->r_flags
& R4DIRTY
||
3674 rp
->r_awcount
> 0) {
3675 mutex_enter(&rp
->r_statelock
);
3677 mutex_exit(&rp
->r_statelock
);
3679 nfs4_putpage(vp
, (u_offset_t
)0,
3681 mutex_enter(&rp
->r_statelock
);
3682 if (error
&& (error
== ENOSPC
||
3685 rp
->r_error
= error
;
3687 if (--rp
->r_gcount
== 0)
3688 cv_broadcast(&rp
->r_cv
);
3689 mutex_exit(&rp
->r_statelock
);
3692 mutex_exit(&rp
->r_statev4_lock
);
3696 return (nfs4getattr(vp
, vap
, cr
));
3700 nfs4_compare_modes(mode_t from_server
, mode_t on_client
)
3703 * If these are the only two bits cleared
3704 * on the server then return 0 (OK) else
3707 on_client
&= ~(S_ISUID
|S_ISGID
);
3708 if (on_client
== from_server
)
3716 nfs4_setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3717 caller_context_t
*ct
)
3721 if (vap
->va_mask
& AT_NOSET
)
3724 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
3728 * Don't call secpolicy_vnode_setattr, the client cannot
3729 * use its cached attributes to make security decisions
3730 * as the server may be faking mode bits or mapping uid/gid.
3731 * Always just let the server to the checking.
3732 * If we provide the ability to remove basic priviledges
3733 * to setattr (e.g. basic without chmod) then we will
3734 * need to add a check here before calling the server.
3736 error
= nfs4setattr(vp
, vap
, flags
, cr
, NULL
);
3738 if (error
== 0 && (vap
->va_mask
& AT_SIZE
) && vap
->va_size
== 0)
3739 vnevent_truncate(vp
, ct
);
3745 * To replace the "guarded" version 3 setattr, we use two types of compound
3747 * 1. The "normal" setattr, used when the size of the file isn't being
3748 * changed - { Putfh <fh>; Setattr; Getattr }/
3749 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3750 * with only ctime as the argument. If the server ctime differs from
3751 * what is cached on the client, the verify will fail, but we would
3752 * already have the ctime from the preceding getattr, so just set it
3753 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3754 * Setattr; Getattr }.
3756 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3757 * this setattr and NULL if they are not.
3760 nfs4setattr(vnode_t
*vp
, struct vattr
*vap
, int flags
, cred_t
*cr
,
3763 COMPOUND4args_clnt args
;
3764 COMPOUND4res_clnt res
, *resp
= NULL
;
3765 nfs4_ga_res_t
*garp
= NULL
;
3766 int numops
= 3; /* { Putfh; Setattr; Getattr } */
3767 nfs_argop4 argop
[5];
3768 int verify_argop
= -1;
3769 int setattr_argop
= 1;
3774 uint_t mask
= vap
->va_mask
;
3778 bool_t needrecov
= FALSE
;
3779 nfs4_recov_state_t recov_state
;
3780 nfs4_stateid_types_t sid_types
;
3783 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
3787 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
3789 nfs4_init_stateid_types(&sid_types
);
3792 * Only need to flush pages if there are any pages and
3793 * if the file is marked as dirty in some fashion. The
3794 * file must be flushed so that we can accurately
3795 * determine the size of the file and the cached data
3796 * after the SETATTR returns. A file is considered to
3797 * be dirty if it is either marked with R4DIRTY, has
3798 * outstanding i/o's active, or is mmap'd. In this
3799 * last case, we can't tell whether there are dirty
3800 * pages, so we flush just to be sure.
3802 if (nfs4_has_pages(vp
) &&
3803 ((rp
->r_flags
& R4DIRTY
) ||
3805 rp
->r_mapcnt
> 0)) {
3806 ASSERT(vp
->v_type
!= VCHR
);
3807 e
.error
= nfs4_putpage(vp
, (offset_t
)0, 0, 0, cr
, NULL
);
3808 if (e
.error
&& (e
.error
== ENOSPC
|| e
.error
== EDQUOT
)) {
3809 mutex_enter(&rp
->r_statelock
);
3811 rp
->r_error
= e
.error
;
3812 mutex_exit(&rp
->r_statelock
);
3816 if (mask
& AT_SIZE
) {
3818 * Verification setattr compound for non-deleg AT_SIZE:
3819 * { Putfh; Getattr; Verify; Setattr; Getattr }
3820 * Set ctime local here (outside the do_again label)
3821 * so that subsequent retries (after failed VERIFY)
3822 * will use ctime from GETATTR results (from failed
3823 * verify compound) as VERIFY arg.
3824 * If file has delegation, then VERIFY(time_metadata)
3825 * is of little added value, so don't bother.
3827 mutex_enter(&rp
->r_statev4_lock
);
3828 if (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
||
3829 rp
->r_deleg_return_pending
) {
3831 ctime
= rp
->r_attr
.va_ctime
;
3833 mutex_exit(&rp
->r_statev4_lock
);
3836 recov_state
.rs_flags
= 0;
3837 recov_state
.rs_num_retry_despite_err
= 0;
3839 args
.ctag
= TAG_SETATTR
;
3842 setattr_argop
= numops
- 2;
3845 args
.array_len
= numops
;
3847 e
.error
= nfs4_start_op(VTOMI4(vp
), vp
, NULL
, &recov_state
);
3852 /* putfh target fh */
3853 argop
[0].argop
= OP_CPUTFH
;
3854 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
3858 * We only care about the ctime, but need to get mtime
3859 * and size for proper cache update.
3862 argop
[1].argop
= OP_GETATTR
;
3863 argop
[1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
3864 argop
[1].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
3866 /* verify - set later in loop */
3872 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
3873 supp_attrs
= svp
->sv_supp_attrs
;
3874 nfs_rw_exit(&svp
->sv_lock
);
3876 nfs4args_setattr(&argop
[setattr_argop
], vap
, vsap
, flags
, rp
, cr
,
3877 supp_attrs
, &e
.error
, &sid_types
);
3878 stateid
= argop
[setattr_argop
].nfs_argop4_u
.opsetattr
.stateid
;
3880 /* req time field(s) overflow - return immediately */
3881 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
3882 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
3883 opsetattr
.obj_attributes
);
3886 omode
= rp
->r_attr
.va_mode
;
3889 argop
[numops
-1].argop
= OP_GETATTR
;
3890 argop
[numops
-1].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
3892 * If we are setting the ACL (indicated only by vsap != NULL), request
3893 * the ACL in this getattr. The ACL returned from this getattr will be
3894 * used in updating the ACL cache.
3897 argop
[numops
-1].nfs_argop4_u
.opgetattr
.attr_request
|=
3899 argop
[numops
-1].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
3902 * setattr iterates if the object size is set and the cached ctime
3903 * does not match the file ctime. In that case, verify the ctime first.
3907 if (verify_argop
!= -1) {
3909 * Verify that the ctime match before doing setattr.
3911 va
.va_mask
= AT_CTIME
;
3912 va
.va_ctime
= ctime
;
3914 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
3915 supp_attrs
= svp
->sv_supp_attrs
;
3916 nfs_rw_exit(&svp
->sv_lock
);
3917 e
.error
= nfs4args_verify(&argop
[verify_argop
], &va
,
3918 OP_VERIFY
, supp_attrs
);
3920 /* req time field(s) overflow - return */
3921 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3931 rfs4call(VTOMI4(vp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
3934 * Purge the access cache and ACL cache if changing either the
3935 * owner of the file, the group owner, or the mode. These may
3936 * change the access permissions of the file, so purge old
3937 * information and start over again.
3939 if (mask
& (AT_UID
| AT_GID
| AT_MODE
)) {
3940 (void) nfs4_access_purge_rp(rp
);
3941 if (rp
->r_secattr
!= NULL
) {
3942 mutex_enter(&rp
->r_statelock
);
3943 vsp
= rp
->r_secattr
;
3944 rp
->r_secattr
= NULL
;
3945 mutex_exit(&rp
->r_statelock
);
3947 nfs4_acl_free_cache(vsp
);
3952 * If res.array_len == numops, then everything succeeded,
3953 * except for possibly the final getattr. If only the
3954 * last getattr failed, give up, and don't try recovery.
3956 if (res
.array_len
== numops
) {
3957 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3965 * if either rpc call failed or completely succeeded - done
3967 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
3969 PURGE_ATTRCACHE4(vp
);
3971 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3978 * Do proper retry for OLD_STATEID outside of the normal
3979 * recovery framework.
3981 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
3982 sid_types
.cur_sid_type
!= SPEC_SID
&&
3983 sid_types
.cur_sid_type
!= NO_SID
) {
3984 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
3986 nfs4_save_stateid(&stateid
, &sid_types
);
3987 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
3988 opsetattr
.obj_attributes
);
3989 if (verify_argop
!= -1) {
3990 nfs4args_verify_free(&argop
[verify_argop
]);
3993 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4000 abort
= nfs4_start_recovery(&e
,
4001 VTOMI4(vp
), vp
, NULL
, NULL
, NULL
,
4002 OP_SETATTR
, NULL
, NULL
, NULL
);
4003 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
4006 * Do not retry if we failed with OLD_STATEID using
4007 * a special stateid. This is done to avoid looping
4008 * with a broken server.
4010 if (e
.error
== 0 && res
.status
== NFS4ERR_OLD_STATEID
&&
4011 (sid_types
.cur_sid_type
== SPEC_SID
||
4012 sid_types
.cur_sid_type
== NO_SID
))
4015 if (res
.status
== NFS4ERR_BADOWNER
)
4016 nfs4_log_badowner(VTOMI4(vp
),
4019 e
.error
= geterrno4(res
.status
);
4020 (void) xdr_free(xdr_COMPOUND4res_clnt
,
4023 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4024 opsetattr
.obj_attributes
);
4025 if (verify_argop
!= -1) {
4026 nfs4args_verify_free(&argop
[verify_argop
]);
4029 if (abort
== FALSE
) {
4031 * Need to retry all possible stateids in
4032 * case the recovery error wasn't stateid
4033 * related or the stateids have become
4034 * stale (server reboot).
4036 nfs4_init_stateid_types(&sid_types
);
4043 * Need to call nfs4_end_op before nfs4getattr to
4044 * avoid potential nfs4_start_op deadlock. See RFE
4045 * 4777612. Calls to nfs4_invalidate_pages() and
4046 * nfs4_purge_stale_fh() might also generate over the
4047 * wire calls which my cause nfs4_start_op() deadlock.
4049 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
4052 * Check to update lease.
4055 if (res
.status
== NFS4_OK
) {
4060 * Check if verify failed to see if try again
4062 if ((verify_argop
== -1) || (res
.array_len
!= 3)) {
4066 if (res
.status
== NFS4ERR_BADOWNER
)
4067 nfs4_log_badowner(VTOMI4(vp
), OP_SETATTR
);
4069 e
.error
= geterrno4(res
.status
);
4072 * When the verify request fails, the client ctime is
4073 * not in sync with the server. This is the same as
4074 * the version 3 "not synchronized" error, and we
4075 * handle it in a similar manner (XXX do we need to???).
4076 * Use the ctime returned in the first getattr for
4077 * the input to the next verify.
4078 * If we couldn't get the attributes, then we give up
4079 * because we can't complete the operation as required.
4081 garp
= &res
.array
[1].nfs_resop4_u
.opgetattr
.ga_res
;
4084 PURGE_ATTRCACHE4(vp
);
4085 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4088 * retry with a new verify value
4090 ctime
= garp
->n4g_va
.va_ctime
;
4091 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4095 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4096 opsetattr
.obj_attributes
);
4097 if (verify_argop
!= -1) {
4098 nfs4args_verify_free(&argop
[verify_argop
]);
4101 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4108 * If we are here, rfs4call has an irrecoverable error - return
4110 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4111 opsetattr
.obj_attributes
);
4112 if (verify_argop
!= -1) {
4113 nfs4args_verify_free(&argop
[verify_argop
]);
4117 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
4124 * If changing the size of the file, invalidate
4125 * any local cached data which is no longer part
4126 * of the file. We also possibly invalidate the
4127 * last page in the file. We could use
4128 * pvn_vpzero(), but this would mark the page as
4129 * modified and require it to be written back to
4130 * the server for no particularly good reason.
4131 * This way, if we access it, then we bring it
4132 * back in. A read should be cheaper than a
4135 if (mask
& AT_SIZE
) {
4136 nfs4_invalidate_pages(vp
, (vap
->va_size
& PAGEMASK
), cr
);
4139 /* either no error or one of the postop getattr failed */
4142 * XXX Perform a simplified version of wcc checking. Instead of
4143 * have another getattr to get pre-op, just purge cache if
4144 * any of the ops prior to and including the getattr failed.
4145 * If the getattr succeeded then update the attrcache accordingly.
4149 if (res
.status
== NFS4_OK
) {
4153 resop
= &res
.array
[numops
- 1];
4154 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
4157 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4158 * rather than filling it. See the function itself for details.
4160 e
.error
= nfs4_update_attrcache(res
.status
, garp
, t
, vp
, cr
);
4162 if (garp
->n4g_resbmap
& FATTR4_ACL_MASK
) {
4163 nfs4_acl_fill_cache(rp
, &garp
->n4g_vsa
);
4164 vs_ace4_destroy(&garp
->n4g_vsa
);
4168 * The ACL was supposed to be set and to be
4169 * returned in the last getattr of this
4170 * compound, but for some reason the getattr
4171 * result doesn't contain the ACL. In this
4172 * case, purge the ACL cache.
4174 if (rp
->r_secattr
!= NULL
) {
4175 mutex_enter(&rp
->r_statelock
);
4176 vsp
= rp
->r_secattr
;
4177 rp
->r_secattr
= NULL
;
4178 mutex_exit(&rp
->r_statelock
);
4180 nfs4_acl_free_cache(vsp
);
4186 if (res
.status
== NFS4_OK
&& (mask
& AT_SIZE
)) {
4188 * Set the size, rather than relying on getting it updated
4189 * via a GETATTR. With delegations the client tries to
4190 * suppress GETATTR calls.
4192 mutex_enter(&rp
->r_statelock
);
4193 rp
->r_size
= vap
->va_size
;
4194 mutex_exit(&rp
->r_statelock
);
4198 * Can free up request args and res
4200 nfs4_fattr4_free(&argop
[setattr_argop
].nfs_argop4_u
.
4201 opsetattr
.obj_attributes
);
4202 if (verify_argop
!= -1) {
4203 nfs4args_verify_free(&argop
[verify_argop
]);
4206 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4209 * Some servers will change the mode to clear the setuid
4210 * and setgid bits when changing the uid or gid. The
4211 * client needs to compensate appropriately.
4213 if (mask
& (AT_UID
| AT_GID
)) {
4214 int terror
, do_setattr
;
4217 va
.va_mask
= AT_MODE
;
4218 terror
= nfs4getattr(vp
, &va
, cr
);
4220 (((mask
& AT_MODE
) && va
.va_mode
!= vap
->va_mode
) ||
4221 (!(mask
& AT_MODE
) && va
.va_mode
!= omode
))) {
4222 va
.va_mask
= AT_MODE
;
4223 if (mask
& AT_MODE
) {
4225 * We asked the mode to be changed and what
4226 * we just got from the server in getattr is
4227 * not what we wanted it to be, so set it now.
4229 va
.va_mode
= vap
->va_mode
;
4233 * We did not ask the mode to be changed,
4234 * Check to see that the server just cleared
4235 * I_SUID and I_GUID from it. If not then
4236 * set mode to omode with UID/GID cleared.
4238 if (nfs4_compare_modes(va
.va_mode
, omode
)) {
4239 omode
&= ~(S_ISUID
|S_ISGID
);
4246 (void) nfs4setattr(vp
, &va
, 0, cr
, NULL
);
4255 nfs4_access(vnode_t
*vp
, int mode
, int flags
, cred_t
*cr
, caller_context_t
*ct
)
4257 COMPOUND4args_clnt args
;
4258 COMPOUND4res_clnt res
;
4260 uint32_t acc
, resacc
, argacc
;
4262 cred_t
*cred
, *ncr
, *ncrfree
= NULL
;
4263 nfs4_access_type_t cacc
;
4265 nfs_argop4 argop
[3];
4267 bool_t needrecov
= FALSE
, do_getattr
;
4268 nfs4_recov_state_t recov_state
;
4271 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4272 mntinfo4_t
*mi
= VTOMI4(vp
);
4274 if (nfs_zone() != mi
->mi_zone
)
4279 acc
|= ACCESS4_READ
;
4280 if (mode
& VWRITE
) {
4281 if ((vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
) && !ISVDEV(vp
->v_type
))
4283 if (vp
->v_type
== VDIR
)
4284 acc
|= ACCESS4_DELETE
;
4285 acc
|= ACCESS4_MODIFY
| ACCESS4_EXTEND
;
4288 if (vp
->v_type
== VDIR
)
4289 acc
|= ACCESS4_LOOKUP
;
4291 acc
|= ACCESS4_EXECUTE
;
4294 if (VTOR4(vp
)->r_acache
!= NULL
) {
4295 e
.error
= nfs4_validate_caches(vp
, cr
);
4301 if (vp
->v_type
== VDIR
)
4302 argacc
= ACCESS4_READ
| ACCESS4_DELETE
| ACCESS4_MODIFY
|
4303 ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
4305 argacc
= ACCESS4_READ
| ACCESS4_MODIFY
| ACCESS4_EXTEND
|
4307 recov_state
.rs_flags
= 0;
4308 recov_state
.rs_num_retry_despite_err
= 0;
4312 * ncr and ncrfree both initially
4313 * point to the memory area returned
4315 * ncrfree not NULL when exiting means
4316 * that we need to release it
4318 ncr
= crnetadjust(cred
);
4322 cacc
= nfs4_access_check(rp
, acc
, cred
);
4323 if (cacc
== NFS4_ACCESS_ALLOWED
) {
4324 if (ncrfree
!= NULL
)
4328 if (cacc
== NFS4_ACCESS_DENIED
) {
4330 * If the cred can be adjusted, try again
4331 * with the new cred.
4338 if (ncrfree
!= NULL
)
4345 * Don't take with r_statev4_lock here. r_deleg_type could
4346 * change as soon as lock is released. Since it is an int,
4347 * there is no atomicity issue.
4349 do_getattr
= (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
);
4350 num_ops
= do_getattr
? 3 : 2;
4352 args
.ctag
= TAG_ACCESS
;
4354 args
.array_len
= num_ops
;
4357 if (e
.error
= nfs4_start_fop(mi
, vp
, NULL
, OH_ACCESS
,
4358 &recov_state
, NULL
)) {
4359 if (ncrfree
!= NULL
)
4364 /* putfh target fh */
4365 argop
[0].argop
= OP_CPUTFH
;
4366 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
4369 argop
[1].argop
= OP_ACCESS
;
4370 argop
[1].nfs_argop4_u
.opaccess
.access
= argacc
;
4374 argop
[2].argop
= OP_GETATTR
;
4375 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
4376 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
4379 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
4380 "nfs4_access: %s call, rp %s", needrecov
? "recov" : "first",
4381 rnode4info(VTOR4(vp
))));
4385 rfs4call(VTOMI4(vp
), &args
, &res
, cred
, &doqueue
, 0, &e
);
4386 rpc_error
= e
.error
;
4388 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
4390 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
4391 "nfs4_access: initiating recovery\n"));
4393 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
4394 NULL
, OP_ACCESS
, NULL
, NULL
, NULL
) == FALSE
) {
4395 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_ACCESS
,
4396 &recov_state
, needrecov
);
4398 (void) xdr_free(xdr_COMPOUND4res_clnt
,
4403 nfs4_end_fop(mi
, vp
, NULL
, OH_ACCESS
, &recov_state
, needrecov
);
4409 e
.error
= geterrno4(res
.status
);
4411 * This might generate over the wire calls throught
4412 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4413 * here to avoid a deadlock.
4415 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4418 resop
= &res
.array
[1]; /* access res */
4420 resacc
= resop
->nfs_resop4_u
.opaccess
.access
;
4423 resop
++; /* getattr res */
4424 nfs4_attr_cache(vp
, &resop
->nfs_resop4_u
.opgetattr
.ga_res
,
4425 t
, cr
, FALSE
, NULL
);
4429 nfs4_access_cache(rp
, argacc
, resacc
, cred
);
4431 * we just cached results with cred; if cred is the
4432 * adjusted credentials from crnetadjust, we do not want
4433 * to release them before exiting: hence setting ncrfree
4438 /* XXX check the supported bits too? */
4439 if ((acc
& resacc
) != acc
) {
4441 * The following code implements the semantic
4442 * that a setuid root program has *at least* the
4443 * permissions of the user that is running the
4444 * program. See rfs3call() for more portions
4445 * of the implementation of this functionality.
4449 (void) xdr_free(xdr_COMPOUND4res_clnt
,
4461 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4463 if (ncrfree
!= NULL
)
4471 nfs4_readlink(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, caller_context_t
*ct
)
4473 COMPOUND4args_clnt args
;
4474 COMPOUND4res_clnt res
;
4477 nfs_argop4 argop
[3];
4479 READLINK4res
*lr_res
;
4480 nfs4_ga_res_t
*garp
;
4483 bool_t needrecov
= FALSE
;
4484 nfs4_recov_state_t recov_state
;
4486 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4488 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
4491 * Can't readlink anything other than a symbolic link.
4493 if (vp
->v_type
!= VLNK
)
4497 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
!= NULL
) {
4498 e
.error
= nfs4_validate_caches(vp
, cr
);
4501 mutex_enter(&rp
->r_statelock
);
4502 if (rp
->r_symlink
.contents
!= NULL
) {
4503 e
.error
= uiomove(rp
->r_symlink
.contents
,
4504 rp
->r_symlink
.len
, UIO_READ
, uiop
);
4505 mutex_exit(&rp
->r_statelock
);
4508 mutex_exit(&rp
->r_statelock
);
4510 recov_state
.rs_flags
= 0;
4511 recov_state
.rs_num_retry_despite_err
= 0;
4516 args
.ctag
= TAG_READLINK
;
4518 e
.error
= nfs4_start_op(VTOMI4(vp
), vp
, NULL
, &recov_state
);
4523 /* 0. putfh symlink fh */
4524 argop
[0].argop
= OP_CPUTFH
;
4525 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
4528 argop
[1].argop
= OP_READLINK
;
4531 argop
[2].argop
= OP_GETATTR
;
4532 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
4533 argop
[2].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(vp
);
4537 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
4538 "nfs4_readlink: %s call, rp %s", needrecov
? "recov" : "first",
4539 rnode4info(VTOR4(vp
))));
4543 rfs4call(VTOMI4(vp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
4545 needrecov
= nfs4_needs_recovery(&e
, FALSE
, vp
->v_vfsp
);
4547 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
4548 "nfs4_readlink: initiating recovery\n"));
4550 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
4551 NULL
, OP_READLINK
, NULL
, NULL
, NULL
) == FALSE
) {
4553 (void) xdr_free(xdr_COMPOUND4res_clnt
,
4556 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
,
4562 nfs4_end_op(VTOMI4(vp
), vp
, NULL
, &recov_state
, needrecov
);
4568 * There is an path in the code below which calls
4569 * nfs4_purge_stale_fh(), which may generate otw calls through
4570 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4571 * here to avoid nfs4_start_op() deadlock.
4574 if (res
.status
&& (res
.array_len
< args
.array_len
)) {
4576 * either Putfh or Link failed
4578 e
.error
= geterrno4(res
.status
);
4579 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
4580 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4584 resop
= &res
.array
[1]; /* readlink res */
4585 lr_res
= &resop
->nfs_resop4_u
.opreadlink
;
4588 * treat symlink names as data
4590 linkdata
= utf8_to_str(&lr_res
->link
, &len
, NULL
);
4591 if (linkdata
!= NULL
) {
4592 int uio_len
= len
- 1;
4593 /* len includes null byte, which we won't uiomove */
4594 e
.error
= uiomove(linkdata
, uio_len
, UIO_READ
, uiop
);
4595 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
== NULL
) {
4596 mutex_enter(&rp
->r_statelock
);
4597 if (rp
->r_symlink
.contents
== NULL
) {
4598 rp
->r_symlink
.contents
= linkdata
;
4599 rp
->r_symlink
.len
= uio_len
;
4600 rp
->r_symlink
.size
= len
;
4601 mutex_exit(&rp
->r_statelock
);
4603 mutex_exit(&rp
->r_statelock
);
4604 kmem_free(linkdata
, len
);
4607 kmem_free(linkdata
, len
);
4610 if (res
.status
== NFS4_OK
) {
4611 resop
++; /* getattr res */
4612 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
4614 e
.error
= nfs4_update_attrcache(res
.status
, garp
, t
, vp
, cr
);
4616 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
4619 * The over the wire error for attempting to readlink something
4620 * other than a symbolic link is ENXIO. However, we need to
4621 * return EINVAL instead of ENXIO, so we map it here.
4623 return (e
.error
== ENXIO
? EINVAL
: e
.error
);
4627 * Flush local dirty pages to stable storage on the server.
4629 * If FNODSYNC is specified, then there is nothing to do because
4630 * metadata changes are not cached on the client before being
4631 * sent to the server.
4635 nfs4_fsync(vnode_t
*vp
, int syncflag
, cred_t
*cr
, caller_context_t
*ct
)
4639 if ((syncflag
& FNODSYNC
) || IS_SWAPVP(vp
))
4641 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
4643 error
= nfs4_putpage_commit(vp
, (offset_t
)0, 0, cr
);
4645 error
= VTOR4(vp
)->r_error
;
4650 * Weirdness: if the file was removed or the target of a rename
4651 * operation while it was open, it got renamed instead. Here we
4652 * remove the renamed file.
4656 nfs4_inactive(vnode_t
*vp
, cred_t
*cr
, caller_context_t
*ct
)
4660 ASSERT(vp
!= DNLC_NO_VNODE
);
4664 if (IS_SHADOW(vp
, rp
)) {
4670 * If this is coming from the wrong zone, we let someone in the right
4671 * zone take care of it asynchronously. We can get here due to
4672 * VN_RELE() being called from pageout() or fsflush(). This call may
4673 * potentially turn into an expensive no-op if, for instance, v_count
4674 * gets incremented in the meantime, but it's still correct.
4676 if (nfs_zone() != VTOMI4(vp
)->mi_zone
) {
4677 nfs4_async_inactive(vp
, cr
);
4682 * Some of the cleanup steps might require over-the-wire
4683 * operations. Since VOP_INACTIVE can get called as a result of
4684 * other over-the-wire operations (e.g., an attribute cache update
4685 * can lead to a DNLC purge), doing those steps now would lead to a
4686 * nested call to the recovery framework, which can deadlock. So
4687 * do any over-the-wire cleanups asynchronously, in a separate
4691 mutex_enter(&rp
->r_os_lock
);
4692 mutex_enter(&rp
->r_statelock
);
4693 mutex_enter(&rp
->r_statev4_lock
);
4695 if (vp
->v_type
== VREG
&& list_head(&rp
->r_open_streams
) != NULL
) {
4696 mutex_exit(&rp
->r_statev4_lock
);
4697 mutex_exit(&rp
->r_statelock
);
4698 mutex_exit(&rp
->r_os_lock
);
4699 nfs4_async_inactive(vp
, cr
);
4703 if (rp
->r_deleg_type
== OPEN_DELEGATE_READ
||
4704 rp
->r_deleg_type
== OPEN_DELEGATE_WRITE
) {
4705 mutex_exit(&rp
->r_statev4_lock
);
4706 mutex_exit(&rp
->r_statelock
);
4707 mutex_exit(&rp
->r_os_lock
);
4708 nfs4_async_inactive(vp
, cr
);
4712 if (rp
->r_unldvp
!= NULL
) {
4713 mutex_exit(&rp
->r_statev4_lock
);
4714 mutex_exit(&rp
->r_statelock
);
4715 mutex_exit(&rp
->r_os_lock
);
4716 nfs4_async_inactive(vp
, cr
);
4719 mutex_exit(&rp
->r_statev4_lock
);
4720 mutex_exit(&rp
->r_statelock
);
4721 mutex_exit(&rp
->r_os_lock
);
4723 rp4_addfree(rp
, cr
);
4727 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4728 * various bits of state. The caller must not refer to vp after this call.
4732 nfs4_inactive_otw(vnode_t
*vp
, cred_t
*cr
)
4734 rnode4_t
*rp
= VTOR4(vp
);
4735 nfs4_recov_state_t recov_state
;
4736 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
4740 COMPOUND4args_clnt args
;
4741 COMPOUND4res_clnt res
, *resp
;
4742 nfs_argop4 argop
[2];
4748 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
4749 ASSERT(!IS_SHADOW(vp
, rp
));
4752 name
= fn_name(VTOSV(vp
)->sv_name
);
4753 NFS4_DEBUG(nfs4_client_inactive_debug
, (CE_NOTE
, "nfs4_inactive_otw: "
4754 "release vnode %s", name
));
4755 kmem_free(name
, MAXNAMELEN
);
4758 if (vp
->v_type
== VREG
) {
4759 bool_t recov_failed
= FALSE
;
4761 e
.error
= nfs4close_all(vp
, cr
);
4763 /* Check to see if recovery failed */
4764 mutex_enter(&(VTOMI4(vp
)->mi_lock
));
4765 if (VTOMI4(vp
)->mi_flags
& MI4_RECOV_FAIL
)
4766 recov_failed
= TRUE
;
4767 mutex_exit(&(VTOMI4(vp
)->mi_lock
));
4768 if (!recov_failed
) {
4769 mutex_enter(&rp
->r_statelock
);
4770 if (rp
->r_flags
& R4RECOVERR
)
4771 recov_failed
= TRUE
;
4772 mutex_exit(&rp
->r_statelock
);
4775 NFS4_DEBUG(nfs4_client_recov_debug
,
4776 (CE_NOTE
, "nfs4_inactive_otw: "
4777 "close failed (recovery failure)"));
4783 if (rp
->r_unldvp
== NULL
) {
4784 rp4_addfree(rp
, cr
);
4789 * Save the vnode pointer for the directory where the
4790 * unlinked-open file got renamed, then set it to NULL
4791 * to prevent another thread from getting here before
4792 * we're done with the remove. While we have the
4793 * statelock, make local copies of the pertinent rnode
4794 * fields. If we weren't to do this in an atomic way, the
4795 * the unl* fields could become inconsistent with respect
4796 * to each other due to a race condition between this
4797 * code and nfs_remove(). See bug report 1034328.
4799 mutex_enter(&rp
->r_statelock
);
4800 if (rp
->r_unldvp
== NULL
) {
4801 mutex_exit(&rp
->r_statelock
);
4802 rp4_addfree(rp
, cr
);
4806 unldvp
= rp
->r_unldvp
;
4807 rp
->r_unldvp
= NULL
;
4808 unlname
= rp
->r_unlname
;
4809 rp
->r_unlname
= NULL
;
4810 unlcred
= rp
->r_unlcred
;
4811 rp
->r_unlcred
= NULL
;
4812 mutex_exit(&rp
->r_statelock
);
4815 * If there are any dirty pages left, then flush
4816 * them. This is unfortunate because they just
4817 * may get thrown away during the remove operation,
4818 * but we have to do this for correctness.
4820 if (nfs4_has_pages(vp
) &&
4821 ((rp
->r_flags
& R4DIRTY
) || rp
->r_count
> 0)) {
4822 ASSERT(vp
->v_type
!= VCHR
);
4823 e
.error
= nfs4_putpage(vp
, (u_offset_t
)0, 0, 0, cr
, NULL
);
4825 mutex_enter(&rp
->r_statelock
);
4827 rp
->r_error
= e
.error
;
4828 mutex_exit(&rp
->r_statelock
);
4832 recov_state
.rs_flags
= 0;
4833 recov_state
.rs_num_retry_despite_err
= 0;
4836 * Do the remove operation on the renamed file
4838 args
.ctag
= TAG_INACTIVE
;
4841 * Remove ops: putfh dir; remove
4846 e
.error
= nfs4_start_op(VTOMI4(unldvp
), unldvp
, NULL
, &recov_state
);
4848 kmem_free(unlname
, MAXNAMELEN
);
4852 * Try again; this time around r_unldvp will be NULL, so we'll
4853 * just call rp4_addfree() and return.
4858 /* putfh directory */
4859 argop
[0].argop
= OP_CPUTFH
;
4860 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(unldvp
)->r_fh
;
4863 argop
[1].argop
= OP_CREMOVE
;
4864 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= unlname
;
4871 * Can't do this yet. We may be being called from
4872 * dnlc_purge_XXX while that routine is holding a
4873 * mutex lock to the nc_rele list. The calls to
4874 * nfs3_cache_wcc_data may result in calls to
4875 * dnlc_purge_XXX. This will result in a deadlock.
4877 rfs4call(VTOMI4(unldvp
), &args
, &res
, unlcred
, &doqueue
, 0, &e
);
4879 PURGE_ATTRCACHE4(unldvp
);
4881 } else if (res
.status
) {
4882 e
.error
= geterrno4(res
.status
);
4883 PURGE_ATTRCACHE4(unldvp
);
4885 * This code is inactive right now
4886 * but if made active there should
4887 * be a nfs4_end_op() call before
4888 * nfs4_purge_stale_fh to avoid start_op()
4889 * deadlock. See BugId: 4948726
4891 nfs4_purge_stale_fh(error
, unldvp
, cr
);
4896 resop
= &res
.array
[1];
4897 rm_res
= &resop
->nfs_resop4_u
.opremove
;
4899 * Update directory cache attribute,
4900 * readdir and dnlc caches.
4902 nfs4_update_dircaches(&rm_res
->cinfo
, unldvp
, NULL
, NULL
, NULL
);
4905 rfs4call(VTOMI4(unldvp
), &args
, &res
, unlcred
, &doqueue
, 0, &e
);
4907 PURGE_ATTRCACHE4(unldvp
);
4910 if (nfs4_needs_recovery(&e
, FALSE
, unldvp
->v_vfsp
)) {
4911 if (nfs4_start_recovery(&e
, VTOMI4(unldvp
), unldvp
, NULL
,
4912 NULL
, NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
4914 (void) xdr_free(xdr_COMPOUND4res_clnt
,
4916 nfs4_end_op(VTOMI4(unldvp
), unldvp
, NULL
,
4917 &recov_state
, TRUE
);
4918 goto recov_retry_remove
;
4921 nfs4_end_op(VTOMI4(unldvp
), unldvp
, NULL
, &recov_state
, FALSE
);
4924 * Release stuff held for the remove
4927 if (!e
.error
&& resp
)
4928 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
4930 kmem_free(unlname
, MAXNAMELEN
);
4936 * Remote file system operations having to do with directory manipulation.
4940 nfs4_lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, struct pathname
*pnp
,
4941 int flags
, vnode_t
*rdir
, cred_t
*cr
, caller_context_t
*ct
,
4942 int *direntflags
, pathname_t
*realpnp
)
4945 vnode_t
*vp
, *avp
= NULL
;
4949 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
4952 * if LOOKUP_XATTR, must replace dvp (object) with
4953 * object's attrdir before continuing with lookup
4955 if (flags
& LOOKUP_XATTR
) {
4956 error
= nfs4lookup_xattr(dvp
, nm
, &avp
, flags
, cr
);
4963 * If lookup is for "", just return dvp now. The attrdir
4964 * has already been activated (from nfs4lookup_xattr), and
4965 * the caller will RELE the original dvp -- not
4966 * the attrdir. So, set vpp and return.
4967 * Currently, when the LOOKUP_XATTR flag is
4968 * passed to VOP_LOOKUP, the name is always empty, and
4969 * shortcircuiting here avoids 3 unneeded lock/unlock
4972 * If a non-empty name was provided, then it is the
4973 * attribute name, and it will be looked up below.
4981 * The vfs layer never sends a name when asking for the
4982 * attrdir, so we should never get here (unless of course
4983 * name is passed at some time in future -- at which time
4984 * we'll blow up here).
4990 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
4993 error
= nfs4lookup(dvp
, nm
, vpp
, cr
, 0);
4994 nfs_rw_exit(&drp
->r_rwlock
);
4997 * If vnode is a device, create special vnode.
4999 if (!error
&& ISVDEV((*vpp
)->v_type
)) {
5001 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
5010 nfs4lookup_xattr(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, int flags
, cred_t
*cr
)
5014 int cflag
= ((flags
& CREATE_XATTR_DIR
) != 0);
5018 if (!(mi
->mi_vfsp
->vfs_flag
& VFS_XATTR
) &&
5019 !vfs_has_feature(mi
->mi_vfsp
, VFSFT_SYSATTR_VIEWS
))
5023 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
)))
5026 mutex_enter(&drp
->r_statelock
);
5028 * If the server doesn't support xattrs just return EINVAL
5030 if (drp
->r_xattr_dir
== NFS4_XATTR_DIR_NOTSUPP
) {
5031 mutex_exit(&drp
->r_statelock
);
5032 nfs_rw_exit(&drp
->r_rwlock
);
5037 * If there is a cached xattr directory entry,
5038 * use it as long as the attributes are valid. If the
5039 * attributes are not valid, take the simple approach and
5040 * free the cached value and re-fetch a new value.
5042 * We don't negative entry cache for now, if we did we
5043 * would need to check if the file has changed on every
5044 * lookup. But xattrs don't exist very often and failing
5045 * an openattr is not much more expensive than and NVERIFY or GETATTR
5046 * so do an openattr over the wire for now.
5048 if (drp
->r_xattr_dir
!= NULL
) {
5049 if (ATTRCACHE4_VALID(dvp
)) {
5050 VN_HOLD(drp
->r_xattr_dir
);
5051 *vpp
= drp
->r_xattr_dir
;
5052 mutex_exit(&drp
->r_statelock
);
5053 nfs_rw_exit(&drp
->r_rwlock
);
5056 VN_RELE(drp
->r_xattr_dir
);
5057 drp
->r_xattr_dir
= NULL
;
5059 mutex_exit(&drp
->r_statelock
);
5061 error
= nfs4openattr(dvp
, vpp
, cflag
, cr
);
5063 nfs_rw_exit(&drp
->r_rwlock
);
5069 nfs4lookup(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
, int skipdnlc
)
5074 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5077 * If lookup is for "", just return dvp. Don't need
5078 * to send it over the wire, look it up in the dnlc,
5079 * or perform any access checks.
5088 * Can't do lookups in non-directories.
5090 if (dvp
->v_type
!= VDIR
)
5094 * If lookup is for ".", just return dvp. Don't need
5095 * to send it over the wire or look it up in the dnlc,
5096 * just need to check access.
5098 if (nm
[0] == '.' && nm
[1] == '\0') {
5099 error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5108 if (!(drp
->r_flags
& R4LOOKUP
)) {
5109 mutex_enter(&drp
->r_statelock
);
5110 drp
->r_flags
|= R4LOOKUP
;
5111 mutex_exit(&drp
->r_statelock
);
5116 * Lookup this name in the DNLC. If there is no entry
5117 * lookup over the wire.
5120 *vpp
= dnlc_lookup(dvp
, nm
);
5123 * We need to go over the wire to lookup the name.
5125 return (nfs4lookupnew_otw(dvp
, nm
, vpp
, cr
));
5129 * We hit on the dnlc
5131 if (*vpp
!= DNLC_NO_VNODE
||
5132 (dvp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)) {
5134 * But our attrs may not be valid.
5136 if (ATTRCACHE4_VALID(dvp
)) {
5137 error
= nfs4_waitfor_purge_complete(dvp
);
5145 * If after the purge completes, check to make sure
5146 * our attrs are still valid.
5148 if (ATTRCACHE4_VALID(dvp
)) {
5150 * If we waited for a purge we may have
5151 * lost our vnode so look it up again.
5154 *vpp
= dnlc_lookup(dvp
, nm
);
5156 return (nfs4lookupnew_otw(dvp
,
5160 * The access cache should almost always hit
5162 error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5169 if (*vpp
== DNLC_NO_VNODE
) {
5179 ASSERT(*vpp
!= NULL
);
5182 * We may have gotten here we have one of the following cases:
5183 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5184 * need to validate them.
5185 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5188 * Go to the server and check if the directory has changed, if
5189 * it hasn't we are done and can use the dnlc entry.
5191 return (nfs4lookupvalidate_otw(dvp
, nm
, vpp
, cr
));
5195 * Go to the server and check if the directory has changed, if
5196 * it hasn't we are done and can use the dnlc entry. If it
5197 * has changed we get a new copy of its attributes and check
5198 * the access for VEXEC, then relookup the filename and
5199 * get its filehandle and attributes.
5201 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5202 * if the NVERIFY failed we must
5204 * cache new attributes (will set r_time_attr_inval)
5206 * recheck VEXEC access
5207 * add name to dnlc, possibly negative
5208 * if LOOKUP succeeded
5209 * cache new attributes
5211 * set a new r_time_attr_inval for dvp
5212 * check to make sure we have access
5214 * The vpp returned is the vnode passed in if the directory is valid,
5215 * a new vnode if successful lookup, or NULL on error.
5218 nfs4lookupvalidate_otw(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
)
5220 COMPOUND4args_clnt args
;
5221 COMPOUND4res_clnt res
;
5223 fattr4_change dchange
;
5225 int argoplist_size
= 7 * sizeof (nfs_argop4
);
5229 nfs4_recov_state_t recov_state
;
5234 nfs4_sharedfh_t
*sfhp
;
5235 nfs4_access_type_t cacc
;
5237 rnode4_t
*drp
= VTOR4(dvp
);
5238 nfs4_ga_res_t
*garp
= NULL
;
5239 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
5241 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5243 ASSERT(nm
[0] != '\0');
5244 ASSERT(dvp
->v_type
== VDIR
);
5245 ASSERT(nm
[0] != '.' || nm
[1] != '\0');
5246 ASSERT(*vpp
!= NULL
);
5248 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0') {
5250 args
.ctag
= TAG_LOOKUP_VPARENT
;
5253 * If dvp were a stub, it should have triggered and caused
5254 * a mount for us to get this far.
5256 ASSERT(!RP_ISSTUB(VTOR4(dvp
)));
5259 args
.ctag
= TAG_LOOKUP_VALID
;
5263 recov_state
.rs_flags
= 0;
5264 recov_state
.rs_num_retry_despite_err
= 0;
5268 /* Save the original mount point security information */
5269 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
5272 e
.error
= nfs4_start_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5273 &recov_state
, NULL
);
5275 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5281 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
5283 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5288 argop
[0].argop
= OP_CPUTFH
;
5289 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(dvp
)->r_fh
;
5291 /* 1. nverify the change info */
5292 argop
[1].argop
= OP_NVERIFY
;
5293 ver_fattr
= &argop
[1].nfs_argop4_u
.opnverify
.obj_attributes
;
5294 ver_fattr
->attrmask
= FATTR4_CHANGE_MASK
;
5295 ver_fattr
->attrlist4
= (char *)&dchange
;
5296 ptr
= (int32_t *)&dchange
;
5297 IXDR_PUT_HYPER(ptr
, VTOR4(dvp
)->r_change
);
5298 ver_fattr
->attrlist4_len
= sizeof (fattr4_change
);
5300 /* 2. getattr directory */
5301 argop
[2].argop
= OP_GETATTR
;
5302 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5303 argop
[2].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5305 /* 3. access directory */
5306 argop
[3].argop
= OP_ACCESS
;
5307 argop
[3].nfs_argop4_u
.opaccess
.access
= ACCESS4_READ
| ACCESS4_DELETE
|
5308 ACCESS4_MODIFY
| ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
5310 /* 4. lookup name */
5312 argop
[4].argop
= OP_LOOKUPP
;
5314 argop
[4].argop
= OP_CLOOKUP
;
5315 argop
[4].nfs_argop4_u
.opclookup
.cname
= nm
;
5318 /* 5. resulting file handle */
5319 argop
[5].argop
= OP_GETFH
;
5321 /* 6. resulting file attributes */
5322 argop
[6].argop
= OP_GETATTR
;
5323 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5324 argop
[6].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5329 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
5331 if (!isdotdot
&& res
.status
== NFS4ERR_MOVED
) {
5332 e
.error
= nfs4_setup_referral(dvp
, nm
, vpp
, cr
);
5333 if (e
.error
!= 0 && *vpp
!= NULL
)
5335 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5336 &recov_state
, FALSE
);
5337 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5338 kmem_free(argop
, argoplist_size
);
5342 if (nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
)) {
5344 * For WRONGSEC of a non-dotdot case, send secinfo directly
5345 * from this thread, do not go thru the recovery thread since
5346 * we need the nm information.
5348 * Not doing dotdot case because there is no specification
5349 * for (PUTFH, SECINFO "..") yet.
5351 if (!isdotdot
&& res
.status
== NFS4ERR_WRONGSEC
) {
5352 if ((e
.error
= nfs4_secinfo_vnode_otw(dvp
, nm
, cr
)))
5353 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5354 &recov_state
, FALSE
);
5356 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5357 &recov_state
, TRUE
);
5358 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5359 kmem_free(argop
, argoplist_size
);
5362 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5368 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
5369 OP_LOOKUP
, NULL
, NULL
, NULL
) == FALSE
) {
5370 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5371 &recov_state
, TRUE
);
5373 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5374 kmem_free(argop
, argoplist_size
);
5379 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
, &recov_state
, FALSE
);
5381 if (e
.error
|| res
.array_len
== 0) {
5383 * If e.error isn't set, then reply has no ops (or we couldn't
5384 * be here). The only legal way to reply without an op array
5385 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5386 * be in the reply for all other status values.
5388 * For valid replies without an ops array, return ENOTSUP
5389 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5390 * return EIO -- don't trust status.
5393 e
.error
= (res
.status
== NFS4ERR_MINOR_VERS_MISMATCH
) ?
5397 kmem_free(argop
, argoplist_size
);
5398 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5402 if (res
.status
!= NFS4ERR_SAME
) {
5403 e
.error
= geterrno4(res
.status
);
5406 * The NVERIFY "failed" so the directory has changed
5407 * First make sure PUTFH succeeded and NVERIFY "failed"
5410 if ((res
.array
[0].nfs_resop4_u
.opputfh
.status
!= NFS4_OK
) ||
5411 (res
.array
[1].nfs_resop4_u
.opnverify
.status
!= NFS4_OK
)) {
5412 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5419 * We know the NVERIFY "failed" so we must:
5420 * purge the caches (access and indirectly dnlc if needed)
5422 nfs4_purge_caches(dvp
, NFS4_NOPURGE_DNLC
, cr
, TRUE
);
5424 if (res
.array
[2].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5425 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5432 * Install new cached attributes for the directory
5434 nfs4_attr_cache(dvp
,
5435 &res
.array
[2].nfs_resop4_u
.opgetattr
.ga_res
,
5436 t
, cr
, FALSE
, NULL
);
5438 if (res
.array
[3].nfs_resop4_u
.opaccess
.status
!= NFS4_OK
) {
5439 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5442 e
.error
= geterrno4(res
.status
);
5447 * Now we know the directory is valid,
5448 * cache new directory access
5450 nfs4_access_cache(drp
,
5451 args
.array
[3].nfs_argop4_u
.opaccess
.access
,
5452 res
.array
[3].nfs_resop4_u
.opaccess
.access
, cr
);
5455 * recheck VEXEC access
5457 cacc
= nfs4_access_check(drp
, ACCESS4_LOOKUP
, cr
);
5458 if (cacc
!= NFS4_ACCESS_ALLOWED
) {
5460 * Directory permissions might have been revoked
5462 if (cacc
== NFS4_ACCESS_DENIED
) {
5470 * Somehow we must not have asked for enough
5471 * so try a singleton ACCESS, should never happen.
5473 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5481 e
.error
= geterrno4(res
.status
);
5482 if (res
.array
[4].nfs_resop4_u
.oplookup
.status
!= NFS4_OK
) {
5484 * The lookup failed, probably no entry
5486 if (e
.error
== ENOENT
&& nfs4_lookup_neg_cache
) {
5487 dnlc_update(dvp
, nm
, DNLC_NO_VNODE
);
5490 * Might be some other error, so remove
5491 * the dnlc entry to make sure we start all
5492 * over again, next time.
5494 dnlc_remove(dvp
, nm
);
5501 if (res
.array
[5].nfs_resop4_u
.opgetfh
.status
!= NFS4_OK
) {
5503 * The file exists but we can't get its fh for
5504 * some unknown reason. Remove it from the dnlc
5505 * and error out to be safe.
5507 dnlc_remove(dvp
, nm
);
5512 fhp
= &res
.array
[5].nfs_resop4_u
.opgetfh
.object
;
5513 if (fhp
->nfs_fh4_len
== 0) {
5515 * The file exists but a bogus fh
5516 * some unknown reason. Remove it from the dnlc
5517 * and error out to be safe.
5520 dnlc_remove(dvp
, nm
);
5525 sfhp
= sfh4_get(fhp
, mi
);
5527 if (res
.array
[6].nfs_resop4_u
.opgetattr
.status
== NFS4_OK
)
5528 garp
= &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
5531 * Make the new rnode
5534 e
.error
= nfs4_make_dotdot(sfhp
, t
, dvp
, cr
, &nvp
, 1);
5542 * XXX if nfs4_make_dotdot uses an existing rnode
5543 * XXX it doesn't update the attributes.
5544 * XXX for now just save them again to save an OTW
5546 nfs4_attr_cache(nvp
, garp
, t
, cr
, FALSE
, NULL
);
5548 nvp
= makenfs4node(sfhp
, garp
, dvp
->v_vfsp
, t
, cr
,
5549 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
5551 * If v_type == VNON, then garp was NULL because
5552 * the last op in the compound failed and makenfs4node
5553 * could not find the vnode for sfhp. It created
5554 * a new vnode, so we have nothing to purge here.
5556 if (nvp
->v_type
== VNON
) {
5559 vattr
.va_mask
= AT_TYPE
;
5561 * N.B. We've already called nfs4_end_fop above.
5563 e
.error
= nfs4getattr(nvp
, &vattr
, cr
);
5571 nvp
->v_type
= vattr
.va_type
;
5577 mutex_enter(&nrp
->r_statev4_lock
);
5578 if (!nrp
->created_v4
) {
5579 mutex_exit(&nrp
->r_statev4_lock
);
5580 dnlc_update(dvp
, nm
, nvp
);
5582 mutex_exit(&nrp
->r_statev4_lock
);
5593 * Because the NVERIFY "succeeded" we know that the
5594 * directory attributes are still valid
5595 * so update r_time_attr_inval
5598 mutex_enter(&drp
->r_statelock
);
5599 if (!(mi
->mi_flags
& MI4_NOAC
) && !(dvp
->v_flag
& VNOCACHE
)) {
5600 delta
= now
- drp
->r_time_attr_saved
;
5601 if (delta
< mi
->mi_acdirmin
)
5602 delta
= mi
->mi_acdirmin
;
5603 else if (delta
> mi
->mi_acdirmax
)
5604 delta
= mi
->mi_acdirmax
;
5606 drp
->r_time_attr_inval
= now
+ delta
;
5607 mutex_exit(&drp
->r_statelock
);
5608 dnlc_update(dvp
, nm
, *vpp
);
5611 * Even though we have a valid directory attr cache
5612 * and dnlc entry, we may not have access.
5613 * This should almost always hit the cache.
5615 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5621 if (*vpp
== DNLC_NO_VNODE
) {
5629 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5630 kmem_free(argop
, argoplist_size
);
5631 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5636 * We need to go over the wire to lookup the name, but
5637 * while we are there verify the directory has not
5638 * changed but if it has, get new attributes and check access
5640 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5641 * NVERIFY GETATTR ACCESS
5644 * if the NVERIFY failed we must purge the caches, add new attributes,
5645 * and cache new access.
5646 * set a new r_time_attr_inval
5647 * add name to dnlc, possibly negative
5648 * if LOOKUP succeeded
5649 * cache new attributes
5652 nfs4lookupnew_otw(vnode_t
*dvp
, char *nm
, vnode_t
**vpp
, cred_t
*cr
)
5654 COMPOUND4args_clnt args
;
5655 COMPOUND4res_clnt res
;
5657 fattr4_change dchange
;
5659 nfs4_ga_res_t
*garp
= NULL
;
5660 int argoplist_size
= 9 * sizeof (nfs_argop4
);
5664 nfs4_recov_state_t recov_state
;
5669 nfs4_sharedfh_t
*sfhp
;
5670 nfs4_access_type_t cacc
;
5672 rnode4_t
*drp
= VTOR4(dvp
);
5673 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
5675 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
5677 ASSERT(nm
[0] != '\0');
5678 ASSERT(dvp
->v_type
== VDIR
);
5679 ASSERT(nm
[0] != '.' || nm
[1] != '\0');
5680 ASSERT(*vpp
== NULL
);
5682 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0') {
5684 args
.ctag
= TAG_LOOKUP_PARENT
;
5687 * If dvp were a stub, it should have triggered and caused
5688 * a mount for us to get this far.
5690 ASSERT(!RP_ISSTUB(VTOR4(dvp
)));
5693 args
.ctag
= TAG_LOOKUP
;
5697 recov_state
.rs_flags
= 0;
5698 recov_state
.rs_num_retry_despite_err
= 0;
5702 /* Save the original mount point security information */
5703 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
5706 e
.error
= nfs4_start_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5707 &recov_state
, NULL
);
5709 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5713 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
5715 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5720 argop
[0].argop
= OP_CPUTFH
;
5721 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(dvp
)->r_fh
;
5723 /* 1. savefh for the nverify */
5724 argop
[1].argop
= OP_SAVEFH
;
5726 /* 2. lookup name */
5728 argop
[2].argop
= OP_LOOKUPP
;
5730 argop
[2].argop
= OP_CLOOKUP
;
5731 argop
[2].nfs_argop4_u
.opclookup
.cname
= nm
;
5734 /* 3. resulting file handle */
5735 argop
[3].argop
= OP_GETFH
;
5737 /* 4. resulting file attributes */
5738 argop
[4].argop
= OP_GETATTR
;
5739 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5740 argop
[4].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5742 /* 5. restorefh back the directory for the nverify */
5743 argop
[5].argop
= OP_RESTOREFH
;
5745 /* 6. nverify the change info */
5746 argop
[6].argop
= OP_NVERIFY
;
5747 ver_fattr
= &argop
[6].nfs_argop4_u
.opnverify
.obj_attributes
;
5748 ver_fattr
->attrmask
= FATTR4_CHANGE_MASK
;
5749 ver_fattr
->attrlist4
= (char *)&dchange
;
5750 ptr
= (int32_t *)&dchange
;
5751 IXDR_PUT_HYPER(ptr
, VTOR4(dvp
)->r_change
);
5752 ver_fattr
->attrlist4_len
= sizeof (fattr4_change
);
5754 /* 7. getattr directory */
5755 argop
[7].argop
= OP_GETATTR
;
5756 argop
[7].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
5757 argop
[7].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
5759 /* 8. access directory */
5760 argop
[8].argop
= OP_ACCESS
;
5761 argop
[8].nfs_argop4_u
.opaccess
.access
= ACCESS4_READ
| ACCESS4_DELETE
|
5762 ACCESS4_MODIFY
| ACCESS4_EXTEND
| ACCESS4_LOOKUP
;
5767 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
5769 if (!isdotdot
&& res
.status
== NFS4ERR_MOVED
) {
5770 e
.error
= nfs4_setup_referral(dvp
, nm
, vpp
, cr
);
5771 if (e
.error
!= 0 && *vpp
!= NULL
)
5773 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5774 &recov_state
, FALSE
);
5775 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5776 kmem_free(argop
, argoplist_size
);
5780 if (nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
)) {
5782 * For WRONGSEC of a non-dotdot case, send secinfo directly
5783 * from this thread, do not go thru the recovery thread since
5784 * we need the nm information.
5786 * Not doing dotdot case because there is no specification
5787 * for (PUTFH, SECINFO "..") yet.
5789 if (!isdotdot
&& res
.status
== NFS4ERR_WRONGSEC
) {
5790 if ((e
.error
= nfs4_secinfo_vnode_otw(dvp
, nm
, cr
)))
5791 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5792 &recov_state
, FALSE
);
5794 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5795 &recov_state
, TRUE
);
5796 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5797 kmem_free(argop
, argoplist_size
);
5800 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5804 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
5805 OP_LOOKUP
, NULL
, NULL
, NULL
) == FALSE
) {
5806 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
,
5807 &recov_state
, TRUE
);
5809 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
5810 kmem_free(argop
, argoplist_size
);
5815 nfs4_end_fop(mi
, dvp
, NULL
, OH_LOOKUP
, &recov_state
, FALSE
);
5817 if (e
.error
|| res
.array_len
== 0) {
5819 * If e.error isn't set, then reply has no ops (or we couldn't
5820 * be here). The only legal way to reply without an op array
5821 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5822 * be in the reply for all other status values.
5824 * For valid replies without an ops array, return ENOTSUP
5825 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5826 * return EIO -- don't trust status.
5829 e
.error
= (res
.status
== NFS4ERR_MINOR_VERS_MISMATCH
) ?
5832 kmem_free(argop
, argoplist_size
);
5833 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
5837 e
.error
= geterrno4(res
.status
);
5840 * The PUTFH and SAVEFH may have failed.
5842 if ((res
.array
[0].nfs_resop4_u
.opputfh
.status
!= NFS4_OK
) ||
5843 (res
.array
[1].nfs_resop4_u
.opsavefh
.status
!= NFS4_OK
)) {
5844 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5849 * Check if the file exists, if it does delay entering
5850 * into the dnlc until after we update the directory
5851 * attributes so we don't cause it to get purged immediately.
5853 if (res
.array
[2].nfs_resop4_u
.oplookup
.status
!= NFS4_OK
) {
5855 * The lookup failed, probably no entry
5857 if (e
.error
== ENOENT
&& nfs4_lookup_neg_cache
)
5858 dnlc_update(dvp
, nm
, DNLC_NO_VNODE
);
5862 if (res
.array
[3].nfs_resop4_u
.opgetfh
.status
!= NFS4_OK
) {
5864 * The file exists but we can't get its fh for
5865 * some unknown reason. Error out to be safe.
5870 fhp
= &res
.array
[3].nfs_resop4_u
.opgetfh
.object
;
5871 if (fhp
->nfs_fh4_len
== 0) {
5873 * The file exists but a bogus fh
5874 * some unknown reason. Error out to be safe.
5879 sfhp
= sfh4_get(fhp
, mi
);
5881 if (res
.array
[4].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5885 garp
= &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
5888 * The RESTOREFH may have failed
5890 if (res
.array
[5].nfs_resop4_u
.oprestorefh
.status
!= NFS4_OK
) {
5896 if (res
.array
[6].nfs_resop4_u
.opnverify
.status
!= NFS4ERR_SAME
) {
5898 * First make sure the NVERIFY failed as we expected,
5899 * if it didn't then be conservative and error out
5900 * as we can't trust the directory.
5902 if (res
.array
[6].nfs_resop4_u
.opnverify
.status
!= NFS4_OK
) {
5909 * We know the NVERIFY "failed" so the directory has changed,
5911 * purge the caches (access and indirectly dnlc if needed)
5913 nfs4_purge_caches(dvp
, NFS4_NOPURGE_DNLC
, cr
, TRUE
);
5915 if (res
.array
[7].nfs_resop4_u
.opgetattr
.status
!= NFS4_OK
) {
5919 nfs4_attr_cache(dvp
,
5920 &res
.array
[7].nfs_resop4_u
.opgetattr
.ga_res
,
5921 t
, cr
, FALSE
, NULL
);
5923 if (res
.array
[8].nfs_resop4_u
.opaccess
.status
!= NFS4_OK
) {
5924 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
5926 e
.error
= geterrno4(res
.status
);
5931 * Now we know the directory is valid,
5932 * cache new directory access
5934 nfs4_access_cache(drp
,
5935 args
.array
[8].nfs_argop4_u
.opaccess
.access
,
5936 res
.array
[8].nfs_resop4_u
.opaccess
.access
, cr
);
5939 * recheck VEXEC access
5941 cacc
= nfs4_access_check(drp
, ACCESS4_LOOKUP
, cr
);
5942 if (cacc
!= NFS4_ACCESS_ALLOWED
) {
5944 * Directory permissions might have been revoked
5946 if (cacc
== NFS4_ACCESS_DENIED
) {
5953 * Somehow we must not have asked for enough
5954 * so try a singleton ACCESS should never happen
5956 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
5963 e
.error
= geterrno4(res
.status
);
5971 * Because the NVERIFY "succeeded" we know that the
5972 * directory attributes are still valid
5973 * so update r_time_attr_inval
5976 mutex_enter(&drp
->r_statelock
);
5977 if (!(mi
->mi_flags
& MI4_NOAC
) && !(dvp
->v_flag
& VNOCACHE
)) {
5978 delta
= now
- drp
->r_time_attr_saved
;
5979 if (delta
< mi
->mi_acdirmin
)
5980 delta
= mi
->mi_acdirmin
;
5981 else if (delta
> mi
->mi_acdirmax
)
5982 delta
= mi
->mi_acdirmax
;
5984 drp
->r_time_attr_inval
= now
+ delta
;
5985 mutex_exit(&drp
->r_statelock
);
5988 * Even though we have a valid directory attr cache,
5989 * we may not have access.
5990 * This should almost always hit the cache.
5992 e
.error
= nfs4_access(dvp
, VEXEC
, 0, cr
, NULL
);
6000 * Now we have successfully completed the lookup, if the
6001 * directory has changed we now have the valid attributes.
6002 * We also know we have directory access.
6003 * Create the new rnode and insert it in the dnlc.
6006 e
.error
= nfs4_make_dotdot(sfhp
, t
, dvp
, cr
, &nvp
, 1);
6012 * XXX if nfs4_make_dotdot uses an existing rnode
6013 * XXX it doesn't update the attributes.
6014 * XXX for now just save them again to save an OTW
6016 nfs4_attr_cache(nvp
, garp
, t
, cr
, FALSE
, NULL
);
6018 nvp
= makenfs4node(sfhp
, garp
, dvp
->v_vfsp
, t
, cr
,
6019 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
6024 mutex_enter(&nrp
->r_statev4_lock
);
6025 if (!nrp
->created_v4
) {
6026 mutex_exit(&nrp
->r_statev4_lock
);
6027 dnlc_update(dvp
, nm
, nvp
);
6029 mutex_exit(&nrp
->r_statev4_lock
);
6034 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6035 kmem_free(argop
, argoplist_size
);
6036 (void) check_mnt_secinfo(mi
->mi_curr_serv
, nvp
);
6042 nfs4lookup_dump_compound(char *where
, nfs_argop4
*argbase
, int argcnt
)
6045 zoneid_t zoneid
= getzoneid();
6048 zcmn_err(zoneid
, CE_NOTE
, "%s: dumping cmpd", where
);
6049 for (i
= 0; i
< argcnt
; i
++) {
6050 nfs_argop4
*op
= &argbase
[i
];
6051 switch (op
->argop
) {
6054 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, putfh", i
);
6057 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, putrootfh", i
);
6060 s
= op
->nfs_argop4_u
.opclookup
.cname
;
6061 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookup %s", i
, s
);
6064 s
= utf8_to_str(&op
->nfs_argop4_u
.oplookup
.objname
,
6066 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookup %s", i
, s
);
6070 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, lookupp ..", i
);
6073 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, getfh", i
);
6076 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, getattr", i
);
6079 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, openattr", i
);
6082 zcmn_err(zoneid
, CE_NOTE
, "\t op %d, opcode %d", i
,
6091 * nfs4lookup_setup - constructs a multi-lookup compound request.
6093 * Given the path "nm1/nm2/.../nmn", the following compound requests
6096 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6097 * is faster, for now.
6099 * l4_getattrs indicates the type of compound requested.
6101 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6103 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6105 * total number of ops is n + 1.
6107 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6108 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6109 * before the last component, and only get attributes
6110 * for the last component. Note that the second-to-last
6111 * pathname component is XATTR_RPATH, which does NOT go
6112 * over-the-wire as a lookup.
6114 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6115 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6117 * and total number of ops is n + 5.
6119 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6120 * attribute directory: create lookups plus an OPENATTR
6121 * replacing the last lookup. Note that the last pathname
6122 * component is XATTR_RPATH, which does NOT go over-the-wire
6125 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6126 * Openattr; Getfh; Getattr }
6128 * and total number of ops is n + 5.
6130 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6133 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6134 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6136 * and total number of ops is 3*n + 1.
6138 * All cases: returns the index in the arg array of the final LOOKUP op, or
6139 * -1 if no LOOKUPs were used.
6142 nfs4lookup_setup(char *nm
, lookup4_param_t
*lookupargp
, int needgetfh
)
6144 enum lkp4_attr_setup l4_getattrs
= lookupargp
->l4_getattrs
;
6145 nfs_argop4
*argbase
, *argop
;
6147 int n
= 1; /* number of components */
6148 int nga
= 1; /* number of Getattr's in request */
6149 char c
= '\0', *s
, *p
;
6150 int lookup_idx
= -1;
6153 /* set lookuparg response result to 0 */
6154 lookupargp
->resp
->status
= NFS4_OK
;
6156 /* skip leading "/" or "." e.g. ".//./" if there is */
6158 if (*nm
!= '/' && *nm
!= '.')
6161 /* ".." is counted as 1 component */
6162 if (*nm
== '.' && *(nm
+ 1) != '/')
6167 * Find n = number of components - nm must be null terminated
6168 * Skip "." components.
6171 for (n
= 1, s
= nm
; *s
!= '\0'; s
++) {
6172 if ((*s
== '/') && (*(s
+ 1) != '/') &&
6173 (*(s
+ 1) != '\0') &&
6174 !(*(s
+ 1) == '.' && (*(s
+ 2) == '/' ||
6182 * nga is number of components that need Getfh+Getattr
6184 switch (l4_getattrs
) {
6185 case LKP4_NO_ATTRIBUTES
:
6188 case LKP4_ALL_ATTRIBUTES
:
6191 * Always have at least 1 getfh, getattr pair
6196 case LKP4_LAST_ATTRDIR
:
6197 case LKP4_LAST_NAMED_ATTR
:
6203 * If change to use the filehandle attr instead of getfh
6204 * the following line can be deleted.
6209 * calculate number of ops in request as
6210 * header + trailer + lookups + getattrs
6212 arglen
= lookupargp
->header_len
+ lookupargp
->trailer_len
+ n
+ nga
;
6214 argoplist_size
= arglen
* sizeof (nfs_argop4
);
6215 argop
= argbase
= kmem_alloc(argoplist_size
, KM_SLEEP
);
6216 lookupargp
->argsp
->array
= argop
;
6218 argcnt
= lookupargp
->header_len
;
6222 * loop and create a lookup op and possibly getattr/getfh for
6223 * each component. Skip "." components.
6225 for (s
= nm
; *s
!= '\0'; s
= p
) {
6227 * Set up a pathname struct for each component if needed
6234 for (p
= s
; (*p
!= '/') && (*p
!= '\0'); p
++)
6239 if (s
[0] == '.' && s
[1] == '\0') {
6243 if (l4_getattrs
== LKP4_LAST_ATTRDIR
&&
6244 strcmp(s
, XATTR_RPATH
) == 0) {
6245 /* getfh XXX may not be needed in future */
6246 argop
->argop
= OP_GETFH
;
6251 argop
->argop
= OP_GETATTR
;
6252 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6253 lookupargp
->ga_bits
;
6254 argop
->nfs_argop4_u
.opgetattr
.mi
=
6260 argop
->argop
= OP_OPENATTR
;
6261 } else if (l4_getattrs
== LKP4_LAST_NAMED_ATTR
&&
6262 strcmp(s
, XATTR_RPATH
) == 0) {
6264 argop
->argop
= OP_OPENATTR
;
6268 /* getfh XXX may not be needed in future */
6269 argop
->argop
= OP_GETFH
;
6274 argop
->argop
= OP_GETATTR
;
6275 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6276 lookupargp
->ga_bits
;
6277 argop
->nfs_argop4_u
.opgetattr
.mi
=
6283 } else if (s
[0] == '.' && s
[1] == '.' && s
[2] == '\0') {
6285 argop
->argop
= OP_LOOKUPP
;
6288 argop
->argop
= OP_LOOKUP
;
6289 (void) str_to_utf8(s
,
6290 &argop
->nfs_argop4_u
.oplookup
.objname
);
6292 lookup_idx
= argcnt
;
6298 if (l4_getattrs
== LKP4_ALL_ATTRIBUTES
) {
6299 /* getfh XXX may not be needed in future */
6300 argop
->argop
= OP_GETFH
;
6305 argop
->argop
= OP_GETATTR
;
6306 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6307 lookupargp
->ga_bits
;
6308 argop
->nfs_argop4_u
.opgetattr
.mi
=
6315 if ((l4_getattrs
!= LKP4_NO_ATTRIBUTES
) &&
6316 ((l4_getattrs
!= LKP4_ALL_ATTRIBUTES
) || (lookup_idx
< 0))) {
6318 /* stick in a post-lookup getfh */
6319 argop
->argop
= OP_GETFH
;
6323 /* post-lookup getattr */
6324 argop
->argop
= OP_GETATTR
;
6325 argop
->nfs_argop4_u
.opgetattr
.attr_request
=
6326 lookupargp
->ga_bits
;
6327 argop
->nfs_argop4_u
.opgetattr
.mi
= lookupargp
->mi
;
6330 argcnt
+= lookupargp
->trailer_len
; /* actual op count */
6331 lookupargp
->argsp
->array_len
= argcnt
;
6332 lookupargp
->arglen
= arglen
;
6335 if (nfs4_client_lookup_debug
)
6336 nfs4lookup_dump_compound("nfs4lookup_setup", argbase
, argcnt
);
6339 return (lookup_idx
);
6343 nfs4openattr(vnode_t
*dvp
, vnode_t
**avp
, int cflag
, cred_t
*cr
)
6345 COMPOUND4args_clnt args
;
6346 COMPOUND4res_clnt res
;
6347 GETFH4res
*gf_res
= NULL
;
6348 nfs_argop4 argop
[4];
6349 nfs_resop4
*resop
= NULL
;
6350 nfs4_sharedfh_t
*sfhp
;
6358 nfs4_recov_state_t recov_state
;
6360 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
6363 recov_state
.rs_flags
= 0;
6364 recov_state
.rs_num_retry_despite_err
= 0;
6367 /* COMPOUND: putfh, openattr, getfh, getattr */
6370 args
.ctag
= TAG_OPENATTR
;
6372 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
6379 argop
[0].argop
= OP_CPUTFH
;
6380 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6383 argop
[1].argop
= OP_OPENATTR
;
6384 argop
[1].nfs_argop4_u
.opopenattr
.createdir
= (cflag
? TRUE
: FALSE
);
6387 argop
[2].argop
= OP_GETFH
;
6390 argop
[3].argop
= OP_GETATTR
;
6391 argop
[3].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6392 argop
[3].nfs_argop4_u
.opgetattr
.mi
= VTOMI4(dvp
);
6394 NFS4_DEBUG(nfs4_client_call_debug
, (CE_NOTE
,
6395 "nfs4openattr: %s call, drp %s", needrecov
? "recov" : "first",
6400 rfs4call(VTOMI4(dvp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
6402 needrecov
= nfs4_needs_recovery(&e
, FALSE
, dvp
->v_vfsp
);
6406 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
6407 "nfs4openattr: initiating recovery\n"));
6409 abort
= nfs4_start_recovery(&e
,
6410 VTOMI4(dvp
), dvp
, NULL
, NULL
, NULL
,
6411 OP_OPENATTR
, NULL
, NULL
, NULL
);
6412 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6414 e
.error
= geterrno4(res
.status
);
6415 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6423 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6429 * If OTW errro is NOTSUPP, then it should be
6430 * translated to EINVAL. All Solaris file system
6431 * implementations return EINVAL to the syscall layer
6432 * when the attrdir cannot be created due to an
6433 * implementation restriction or noxattr mount option.
6435 if (res
.status
== NFS4ERR_NOTSUPP
) {
6436 mutex_enter(&drp
->r_statelock
);
6437 if (drp
->r_xattr_dir
)
6438 VN_RELE(drp
->r_xattr_dir
);
6439 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP
);
6440 drp
->r_xattr_dir
= NFS4_XATTR_DIR_NOTSUPP
;
6441 mutex_exit(&drp
->r_statelock
);
6445 e
.error
= geterrno4(res
.status
);
6449 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6450 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
6456 resop
= &res
.array
[0]; /* putfh res */
6457 ASSERT(resop
->nfs_resop4_u
.opgetfh
.status
== NFS4_OK
);
6459 resop
= &res
.array
[1]; /* openattr res */
6460 ASSERT(resop
->nfs_resop4_u
.opopenattr
.status
== NFS4_OK
);
6462 resop
= &res
.array
[2]; /* getfh res */
6463 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
6464 if (gf_res
->object
.nfs_fh4_len
== 0) {
6466 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6467 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6471 sfhp
= sfh4_get(&gf_res
->object
, VTOMI4(dvp
));
6472 vp
= makenfs4node(sfhp
, &res
.array
[3].nfs_resop4_u
.opgetattr
.ga_res
,
6473 dvp
->v_vfsp
, t
, cr
, dvp
,
6474 fn_get(VTOSV(dvp
)->sv_name
, XATTR_RPATH
, sfhp
));
6478 PURGE_ATTRCACHE4(vp
);
6480 mutex_enter(&vp
->v_lock
);
6481 vp
->v_flag
|= V_XATTRDIR
;
6482 mutex_exit(&vp
->v_lock
);
6486 mutex_enter(&drp
->r_statelock
);
6487 if (drp
->r_xattr_dir
)
6488 VN_RELE(drp
->r_xattr_dir
);
6490 drp
->r_xattr_dir
= vp
;
6493 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6494 * NULL. xattrs could be created at any time, and we have no
6495 * way to update pc4_xattr_exists in the base object if/when
6498 drp
->r_pathconf
.pc4_xattr_valid
= 0;
6500 mutex_exit(&drp
->r_statelock
);
6502 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
6504 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
6511 nfs4_create(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
6512 int mode
, vnode_t
**vpp
, cred_t
*cr
, int flags
, caller_context_t
*ct
,
6521 enum createmode4 createmode
;
6522 bool_t must_trunc
= FALSE
;
6525 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
6527 if (exclusive
== EXCL
&& (dvp
->v_flag
& V_XATTRDIR
)) {
6531 /* . and .. have special meaning in the protocol, reject them. */
6533 if (nm
[0] == '.' && (nm
[1] == '\0' || (nm
[1] == '.' && nm
[2] == '\0')))
6538 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
6543 * We make a copy of the attributes because the caller does not
6544 * expect us to change what va points to.
6549 * If the pathname is "", then dvp is the root vnode of
6550 * a remote file mounted over a local directory.
6551 * All that needs to be done is access
6552 * checking and truncation. Note that we avoid doing
6553 * open w/ create because the parent directory might
6554 * be in pseudo-fs and the open would fail.
6563 * We need to go over the wire, just to be sure whether the
6564 * file exists or not. Using the DNLC can be dangerous in
6565 * this case when making a decision regarding existence.
6567 error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 1);
6571 createmode
= EXCLUSIVE4
;
6573 createmode
= GUARDED4
;
6576 * error would be set if the file does not exist on the
6577 * server, so lets go create it.
6584 * File does exist on the server
6586 if (exclusive
== EXCL
)
6588 else if (vp
->v_type
== VDIR
&& (mode
& VWRITE
))
6592 * If vnode is a device, create special vnode.
6594 if (ISVDEV(vp
->v_type
)) {
6596 vp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
6599 if (!(error
= VOP_ACCESS(vp
, mode
, 0, cr
, ct
))) {
6600 if ((vattr
.va_mask
& AT_SIZE
) &&
6601 vp
->v_type
== VREG
) {
6604 * Check here for large file handled
6605 * by LF-unaware process (as
6606 * ufs_create() does)
6608 if (!(flags
& FOFFMAX
)) {
6609 mutex_enter(&rp
->r_statelock
);
6610 if (rp
->r_size
> MAXOFF32_T
)
6612 mutex_exit(&rp
->r_statelock
);
6615 /* if error is set then we need to return */
6617 nfs_rw_exit(&drp
->r_rwlock
);
6623 vattr
.va_mask
= AT_SIZE
;
6624 error
= nfs4setattr(vp
, &vattr
, 0, cr
,
6628 * we know we have a regular file that already
6629 * exists and we may end up truncating the file
6630 * as a result of the open_otw, so flush out
6631 * any dirty pages for this file first.
6633 if (nfs4_has_pages(vp
) &&
6634 ((rp
->r_flags
& R4DIRTY
) ||
6636 rp
->r_mapcnt
> 0)) {
6637 error
= nfs4_putpage(vp
,
6638 (offset_t
)0, 0, 0, cr
, ct
);
6639 if (error
&& (error
== ENOSPC
||
6650 vattr
.va_mask
= (AT_SIZE
|
6652 vattr
.va_type
= VREG
;
6653 createmode
= UNCHECKED4
;
6660 nfs_rw_exit(&drp
->r_rwlock
);
6667 if (vp
->v_type
== VREG
) {
6669 if (IS_SHADOW(vp
, trp
))
6675 * existing file got truncated, notify.
6677 vnevent_create(tvp
, ct
);
6685 dnlc_remove(dvp
, nm
);
6687 ASSERT(vattr
.va_mask
& AT_TYPE
);
6690 * If not a regular file let nfs4mknod() handle it.
6692 if (vattr
.va_type
!= VREG
) {
6693 error
= nfs4mknod(dvp
, nm
, &vattr
, exclusive
, mode
, vpp
, cr
);
6694 nfs_rw_exit(&drp
->r_rwlock
);
6699 * It _is_ a regular file.
6701 ASSERT(vattr
.va_mask
& AT_MODE
);
6702 if (MANDMODE(vattr
.va_mode
)) {
6703 nfs_rw_exit(&drp
->r_rwlock
);
6708 * If this happens to be a mknod of a regular file, then flags will
6709 * have neither FREAD or FWRITE. However, we must set at least one
6710 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6711 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6712 * set (based on openmode specified by app).
6714 if ((flags
& (FREAD
|FWRITE
)) == 0)
6715 flags
|= (FREAD
|FWRITE
);
6717 error
= nfs4open_otw(dvp
, nm
, &vattr
, vpp
, cr
, 1, flags
, createmode
, 0);
6720 /* if create was successful, throw away the file's pages */
6721 if (!error
&& (vattr
.va_mask
& AT_SIZE
))
6722 nfs4_invalidate_pages(vp
, (vattr
.va_size
& PAGEMASK
),
6724 /* release the lookup hold */
6730 * validate that we opened a regular file. This handles a misbehaving
6731 * server that returns an incorrect FH.
6733 if ((error
== 0) && *vpp
&& (*vpp
)->v_type
!= VREG
) {
6739 * If this is not an exclusive create, then the CREATE
6740 * request will be made with the GUARDED mode set. This
6741 * means that the server will return EEXIST if the file
6742 * exists. The file could exist because of a retransmitted
6743 * request. In this case, we recover by starting over and
6744 * checking to see whether the file exists. This second
6745 * time through it should and a CREATE request will not be
6748 * This handles the problem of a dangling CREATE request
6749 * which contains attributes which indicate that the file
6750 * should be truncated. This retransmitted request could
6751 * possibly truncate valid data in the file if not caught
6752 * by the duplicate request mechanism on the server or if
6753 * not caught by other means. The scenario is:
6755 * Client transmits CREATE request with size = 0
6756 * Client times out, retransmits request.
6757 * Response to the first request arrives from the server
6758 * and the client proceeds on.
6759 * Client writes data to the file.
6760 * The server now processes retransmitted CREATE request
6761 * and truncates file.
6763 * The use of the GUARDED CREATE request prevents this from
6764 * happening because the retransmitted CREATE would fail
6765 * with EEXIST and would not truncate the file.
6767 if (error
== EEXIST
&& exclusive
== NONEXCL
) {
6769 nfs4_create_misses
++;
6773 nfs_rw_exit(&drp
->r_rwlock
);
6774 if (truncating
&& !error
&& *vpp
) {
6778 * existing file got truncated, notify.
6782 if (IS_SHADOW(tvp
, trp
))
6784 vnevent_create(tvp
, ct
);
6790 * Create compound (for mkdir, mknod, symlink):
6791 * { Putfh <dfh>; Create; Getfh; Getattr }
6792 * It's okay if setattr failed to set gid - this is not considered
6793 * an error, but purge attrs in that case.
6796 call_nfs4_create_req(vnode_t
*dvp
, char *nm
, void *data
, struct vattr
*va
,
6797 vnode_t
**vpp
, cred_t
*cr
, nfs_ftype4 type
)
6799 int need_end_op
= FALSE
;
6800 COMPOUND4args_clnt args
;
6801 COMPOUND4res_clnt res
, *resp
= NULL
;
6806 rnode4_t
*drp
= VTOR4(dvp
);
6807 change_info4
*cinfo
;
6812 bool_t needrecov
= FALSE
;
6813 nfs4_recov_state_t recov_state
;
6814 nfs4_sharedfh_t
*sfhp
= NULL
;
6816 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
6817 int numops
, argoplist_size
, setgid_flag
, idx_create
, idx_fattr
;
6818 dirattr_info_t dinfo
, *dinfop
;
6822 ASSERT(type
== NF4DIR
|| type
== NF4LNK
|| type
== NF4BLK
||
6823 type
== NF4CHR
|| type
== NF4SOCK
|| type
== NF4FIFO
);
6828 * Make sure we properly deal with setting the right gid
6829 * on a new directory to reflect the parent's setgid bit
6832 if (type
== NF4DIR
) {
6835 va
->va_mode
&= ~VSGID
;
6836 dva
.va_mask
= AT_MODE
| AT_GID
;
6837 if (VOP_GETATTR(dvp
, &dva
, 0, cr
, NULL
) == 0) {
6840 * If the parent's directory has the setgid bit set
6841 * _and_ the client was able to get a valid mapping
6842 * for the parent dir's owner_group, we want to
6843 * append NVERIFY(owner_group == dva.va_gid) and
6844 * SETTATTR to the CREATE compound.
6846 if (mi
->mi_flags
& MI4_GRPID
|| dva
.va_mode
& VSGID
) {
6848 va
->va_mode
|= VSGID
;
6849 if (dva
.va_gid
!= GID_NOBODY
) {
6850 va
->va_mask
|= AT_GID
;
6851 va
->va_gid
= dva
.va_gid
;
6859 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6860 * 5:restorefh(dir) 6:getattr(dir)
6863 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6864 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6865 * 8:nverify 9:setattr
6877 ASSERT(nfs_zone() == mi
->mi_zone
);
6878 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
))) {
6881 recov_state
.rs_flags
= 0;
6882 recov_state
.rs_num_retry_despite_err
= 0;
6884 argoplist_size
= numops
* sizeof (nfs_argop4
);
6885 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
6889 args
.ctag
= TAG_SYMLINK
;
6890 else if (type
== NF4DIR
)
6891 args
.ctag
= TAG_MKDIR
;
6893 args
.ctag
= TAG_MKNOD
;
6895 args
.array_len
= numops
;
6898 if (e
.error
= nfs4_start_op(mi
, dvp
, NULL
, &recov_state
)) {
6899 nfs_rw_exit(&drp
->r_rwlock
);
6900 kmem_free(argop
, argoplist_size
);
6906 /* 0: putfh directory */
6907 argop
[0].argop
= OP_CPUTFH
;
6908 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6910 /* 1/2: Create object */
6911 argop
[idx_create
].argop
= OP_CCREATE
;
6912 argop
[idx_create
].nfs_argop4_u
.opccreate
.cname
= nm
;
6913 argop
[idx_create
].nfs_argop4_u
.opccreate
.type
= type
;
6914 if (type
== NF4LNK
) {
6916 * symlink, treat name as data
6918 ASSERT(data
!= NULL
);
6919 argop
[idx_create
].nfs_argop4_u
.opccreate
.ftype4_u
.clinkdata
=
6922 if (type
== NF4BLK
|| type
== NF4CHR
) {
6923 ASSERT(data
!= NULL
);
6924 argop
[idx_create
].nfs_argop4_u
.opccreate
.ftype4_u
.devdata
=
6925 *((specdata4
*)data
);
6928 crattr
= &argop
[idx_create
].nfs_argop4_u
.opccreate
.createattrs
;
6930 svp
= drp
->r_server
;
6931 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
6932 supp_attrs
= svp
->sv_supp_attrs
;
6933 nfs_rw_exit(&svp
->sv_lock
);
6935 if (vattr_to_fattr4(va
, NULL
, crattr
, 0, OP_CREATE
, supp_attrs
)) {
6936 nfs_rw_exit(&drp
->r_rwlock
);
6937 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
, needrecov
);
6939 kmem_free(argop
, argoplist_size
);
6943 /* 2/3: getfh fh of created object */
6944 ASSERT(idx_create
+ 1 == idx_fattr
- 1);
6945 argop
[idx_create
+ 1].argop
= OP_GETFH
;
6947 /* 3/4: getattr of new object */
6948 argop
[idx_fattr
].argop
= OP_GETATTR
;
6949 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6950 argop
[idx_fattr
].nfs_argop4_u
.opgetattr
.mi
= mi
;
6955 argop
[4].argop
= OP_SAVEFH
;
6957 argop
[5].argop
= OP_CPUTFH
;
6958 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
6960 argop
[6].argop
= OP_GETATTR
;
6961 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
6962 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
6964 argop
[7].argop
= OP_RESTOREFH
;
6969 * XXX - Revisit the last argument to nfs4_end_op()
6970 * once 5020486 is fixed.
6972 _v
.va_mask
= AT_GID
;
6973 _v
.va_gid
= va
->va_gid
;
6974 if (e
.error
= nfs4args_verify(&argop
[8], &_v
, OP_NVERIFY
,
6976 nfs4_end_op(mi
, dvp
, *vpp
, &recov_state
, TRUE
);
6977 nfs_rw_exit(&drp
->r_rwlock
);
6978 nfs4_fattr4_free(crattr
);
6979 kmem_free(argop
, argoplist_size
);
6986 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6987 * so no need for stateid or flags. Also we specify NULL
6988 * rp since we're only interested in setting owner_group
6991 nfs4args_setattr(&argop
[9], &_v
, NULL
, 0, NULL
, cr
, supp_attrs
,
6995 nfs4_end_op(mi
, dvp
, *vpp
, &recov_state
, TRUE
);
6996 nfs_rw_exit(&drp
->r_rwlock
);
6997 nfs4_fattr4_free(crattr
);
6998 nfs4args_verify_free(&argop
[8]);
6999 kmem_free(argop
, argoplist_size
);
7003 argop
[1].argop
= OP_SAVEFH
;
7005 argop
[5].argop
= OP_RESTOREFH
;
7007 argop
[6].argop
= OP_GETATTR
;
7008 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7009 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
7012 dnlc_remove(dvp
, nm
);
7016 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
7018 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
7020 PURGE_ATTRCACHE4(dvp
);
7026 if (nfs4_start_recovery(&e
, mi
, dvp
, NULL
, NULL
, NULL
,
7027 OP_CREATE
, NULL
, NULL
, NULL
) == FALSE
) {
7028 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7030 need_end_op
= FALSE
;
7031 nfs4_fattr4_free(crattr
);
7033 nfs4args_verify_free(&argop
[8]);
7034 nfs4args_setattr_free(&argop
[9]);
7036 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
7043 if (res
.status
!= NFS4_OK
&& res
.array_len
<= idx_fattr
+ 1) {
7045 if (res
.status
== NFS4ERR_BADOWNER
)
7046 nfs4_log_badowner(mi
, OP_CREATE
);
7048 e
.error
= geterrno4(res
.status
);
7051 * This check is left over from when create was implemented
7052 * using a setattr op (instead of createattrs). If the
7053 * putfh/create/getfh failed, the error was returned. If
7054 * setattr/getattr failed, we keep going.
7056 * It might be better to get rid of the GETFH also, and just
7057 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7058 * Then if any of the operations failed, we could return the
7059 * error now, and remove much of the error code below.
7061 if (res
.array_len
<= idx_fattr
) {
7063 * Either Putfh, Create or Getfh failed.
7065 PURGE_ATTRCACHE4(dvp
);
7067 * nfs4_purge_stale_fh() may generate otw calls through
7068 * nfs4_invalidate_pages. Hence the need to call
7069 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7071 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7073 need_end_op
= FALSE
;
7074 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
7079 resop
= &res
.array
[idx_create
]; /* create res */
7080 cinfo
= &resop
->nfs_resop4_u
.opcreate
.cinfo
;
7082 resop
= &res
.array
[idx_create
+ 1]; /* getfh res */
7083 gf_res
= &resop
->nfs_resop4_u
.opgetfh
;
7085 sfhp
= sfh4_get(&gf_res
->object
, mi
);
7087 *vpp
= vp
= makenfs4node(sfhp
, NULL
, dvp
->v_vfsp
, t
, cr
, dvp
,
7088 fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
7089 if (vp
->v_type
== VNON
) {
7090 vattr
.va_mask
= AT_TYPE
;
7092 * Need to call nfs4_end_op before nfs4getattr to avoid
7093 * potential nfs4_start_op deadlock. See RFE 4777612.
7095 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
,
7097 need_end_op
= FALSE
;
7098 e
.error
= nfs4getattr(vp
, &vattr
, cr
);
7104 vp
->v_type
= vattr
.va_type
;
7108 *vpp
= vp
= makenfs4node(sfhp
,
7109 &res
.array
[idx_fattr
].nfs_resop4_u
.opgetattr
.ga_res
,
7111 dvp
, fn_get(VTOSV(dvp
)->sv_name
, nm
, sfhp
));
7115 * If compound succeeded, then update dir attrs
7117 if (res
.status
== NFS4_OK
) {
7118 dinfo
.di_garp
= &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
7120 dinfo
.di_time_call
= t
;
7125 /* Update directory cache attribute, readdir and dnlc caches */
7126 nfs4_update_dircaches(cinfo
, dvp
, vp
, nm
, dinfop
);
7131 nfs_rw_exit(&drp
->r_rwlock
);
7132 nfs4_fattr4_free(crattr
);
7134 nfs4args_verify_free(&argop
[8]);
7135 nfs4args_setattr_free(&argop
[9]);
7138 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7140 nfs4_end_op(mi
, dvp
, NULL
, &recov_state
, needrecov
);
7142 kmem_free(argop
, argoplist_size
);
7148 nfs4mknod(vnode_t
*dvp
, char *nm
, struct vattr
*va
, enum vcexcl exclusive
,
7149 int mode
, vnode_t
**vpp
, cred_t
*cr
)
7154 specdata4 spec
, *specp
= NULL
;
7156 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
7158 switch (va
->va_type
) {
7161 type
= (va
->va_type
== VCHR
) ? NF4CHR
: NF4BLK
;
7162 spec
.specdata1
= getmajor(va
->va_rdev
);
7163 spec
.specdata2
= getminor(va
->va_rdev
);
7178 error
= call_nfs4_create_req(dvp
, nm
, specp
, va
, &vp
, cr
, type
);
7184 * This might not be needed any more; special case to deal
7185 * with problematic v2/v3 servers. Since create was unable
7186 * to set group correctly, not sure what hope setattr has.
7188 if (va
->va_gid
!= VTOR4(vp
)->r_attr
.va_gid
) {
7189 va
->va_mask
= AT_GID
;
7190 (void) nfs4setattr(vp
, va
, 0, cr
, NULL
);
7194 * If vnode is a device create special vnode
7196 if (ISVDEV(vp
->v_type
)) {
7197 *vpp
= specvp(vp
, vp
->v_rdev
, vp
->v_type
, cr
);
7206 * Remove requires that the current fh be the target directory.
7207 * After the operation, the current fh is unchanged.
7208 * The compound op structure is:
7209 * PUTFH(targetdir), REMOVE
7211 * Weirdness: if the vnode to be removed is open
7212 * we rename it instead of removing it and nfs_inactive
7213 * will remove the new name.
7217 nfs4_remove(vnode_t
*dvp
, char *nm
, cred_t
*cr
, caller_context_t
*ct
, int flags
)
7219 COMPOUND4args_clnt args
;
7220 COMPOUND4res_clnt res
, *resp
= NULL
;
7222 nfs_argop4 argop
[3];
7231 nfs4_recov_state_t recov_state
;
7233 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
7234 dirattr_info_t dinfo
;
7236 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
7239 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
7242 e
.error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 0);
7244 nfs_rw_exit(&drp
->r_rwlock
);
7248 if (vp
->v_type
== VDIR
) {
7250 nfs_rw_exit(&drp
->r_rwlock
);
7255 * First just remove the entry from the name cache, as it
7256 * is most likely the only entry for this vp.
7258 dnlc_remove(dvp
, nm
);
7263 * For regular file types, check to see if the file is open by looking
7264 * at the open streams.
7265 * For all other types, check the reference count on the vnode. Since
7266 * they are not opened OTW they never have an open stream.
7268 * If the file is open, rename it to .nfsXXXX.
7270 if (vp
->v_type
!= VREG
) {
7272 * If the file has a v_count > 1 then there may be more than one
7273 * entry in the name cache due multiple links or an open file,
7274 * but we don't have the real reference count so flush all
7277 if (vp
->v_count
> 1)
7281 * Now we have the real reference count.
7283 isopen
= vp
->v_count
> 1;
7285 mutex_enter(&rp
->r_os_lock
);
7286 isopen
= list_head(&rp
->r_open_streams
) != NULL
;
7287 mutex_exit(&rp
->r_os_lock
);
7290 mutex_enter(&rp
->r_statelock
);
7292 (rp
->r_unldvp
== NULL
|| strcmp(nm
, rp
->r_unlname
) == 0)) {
7293 mutex_exit(&rp
->r_statelock
);
7294 tmpname
= newname();
7295 e
.error
= nfs4rename(dvp
, nm
, dvp
, tmpname
, cr
, ct
);
7297 kmem_free(tmpname
, MAXNAMELEN
);
7299 mutex_enter(&rp
->r_statelock
);
7300 if (rp
->r_unldvp
== NULL
) {
7303 if (rp
->r_unlcred
!= NULL
)
7304 crfree(rp
->r_unlcred
);
7307 rp
->r_unlname
= tmpname
;
7309 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
7310 rp
->r_unlname
= tmpname
;
7312 mutex_exit(&rp
->r_statelock
);
7315 nfs_rw_exit(&drp
->r_rwlock
);
7319 * Actually remove the file/dir
7321 mutex_exit(&rp
->r_statelock
);
7324 * We need to flush any dirty pages which happen to
7325 * be hanging around before removing the file.
7326 * This shouldn't happen very often since in NFSv4
7327 * we should be close to open consistent.
7329 if (nfs4_has_pages(vp
) &&
7330 ((rp
->r_flags
& R4DIRTY
) || rp
->r_count
> 0)) {
7331 e
.error
= nfs4_putpage(vp
, (u_offset_t
)0, 0, 0, cr
, ct
);
7332 if (e
.error
&& (e
.error
== ENOSPC
|| e
.error
== EDQUOT
)) {
7333 mutex_enter(&rp
->r_statelock
);
7335 rp
->r_error
= e
.error
;
7336 mutex_exit(&rp
->r_statelock
);
7342 (void) nfs4delegreturn(rp
, NFS4_DR_REOPEN
);
7343 recov_state
.rs_flags
= 0;
7344 recov_state
.rs_num_retry_despite_err
= 0;
7348 * Remove ops: putfh dir; remove
7350 args
.ctag
= TAG_REMOVE
;
7354 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
7356 nfs_rw_exit(&drp
->r_rwlock
);
7361 /* putfh directory */
7362 argop
[0].argop
= OP_CPUTFH
;
7363 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
7366 argop
[1].argop
= OP_CREMOVE
;
7367 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= nm
;
7370 argop
[2].argop
= OP_GETATTR
;
7371 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7372 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
7375 dinfo
.di_time_call
= gethrtime();
7376 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
7378 PURGE_ATTRCACHE4(vp
);
7380 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
7382 PURGE_ATTRCACHE4(dvp
);
7385 if (nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
,
7386 NULL
, NULL
, NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
7388 (void) xdr_free(xdr_COMPOUND4res_clnt
,
7390 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
7397 * Matching nfs4_end_op() for start_op() above.
7398 * There is a path in the code below which calls
7399 * nfs4_purge_stale_fh(), which may generate otw calls through
7400 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7401 * here to avoid nfs4_start_op() deadlock.
7403 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
7409 e
.error
= geterrno4(res
.status
);
7410 PURGE_ATTRCACHE4(dvp
);
7411 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
7413 resop
= &res
.array
[1]; /* remove res */
7414 rm_res
= &resop
->nfs_resop4_u
.opremove
;
7417 &res
.array
[2].nfs_resop4_u
.opgetattr
.ga_res
;
7420 /* Update directory attr, readdir and dnlc caches */
7421 nfs4_update_dircaches(&rm_res
->cinfo
, dvp
, NULL
, NULL
,
7425 nfs_rw_exit(&drp
->r_rwlock
);
7427 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7434 if (IS_SHADOW(vp
, trp
))
7436 vnevent_remove(tvp
, dvp
, nm
, ct
);
7443 * Link requires that the current fh be the target directory and the
7444 * saved fh be the source fh. After the operation, the current fh is unchanged.
7445 * Thus the compound op structure is:
7446 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7451 nfs4_link(vnode_t
*tdvp
, vnode_t
*svp
, char *tnm
, cred_t
*cr
,
7452 caller_context_t
*ct
, int flags
)
7454 COMPOUND4args_clnt args
;
7455 COMPOUND4res_clnt res
, *resp
= NULL
;
7457 int argoplist_size
= 7 * sizeof (nfs_argop4
);
7460 vnode_t
*realvp
, *nvp
;
7464 bool_t needrecov
= FALSE
;
7465 nfs4_recov_state_t recov_state
;
7467 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
7468 dirattr_info_t dinfo
;
7470 ASSERT(*tnm
!= '\0');
7471 ASSERT(tdvp
->v_type
== VDIR
);
7472 ASSERT(nfs4_consistent_type(tdvp
));
7473 ASSERT(nfs4_consistent_type(svp
));
7475 if (nfs_zone() != VTOMI4(tdvp
)->mi_zone
)
7477 if (VOP_REALVP(svp
, &realvp
, ct
) == 0) {
7479 ASSERT(nfs4_consistent_type(svp
));
7485 if (!(mi
->mi_flags
& MI4_LINK
)) {
7486 return (EOPNOTSUPP
);
7488 recov_state
.rs_flags
= 0;
7489 recov_state
.rs_num_retry_despite_err
= 0;
7491 if (nfs_rw_enter_sig(&tdrp
->r_rwlock
, RW_WRITER
, INTR4(tdvp
)))
7495 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
7497 args
.ctag
= TAG_LINK
;
7500 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7501 * restorefh; getattr(fl)
7506 e
.error
= nfs4_start_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
);
7508 kmem_free(argop
, argoplist_size
);
7509 nfs_rw_exit(&tdrp
->r_rwlock
);
7514 argop
[0].argop
= OP_CPUTFH
;
7515 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(svp
)->r_fh
;
7517 /* 1. save current fh to free up the space for the dir */
7518 argop
[1].argop
= OP_SAVEFH
;
7520 /* 2. putfh targetdir */
7521 argop
[2].argop
= OP_CPUTFH
;
7522 argop
[2].nfs_argop4_u
.opcputfh
.sfh
= tdrp
->r_fh
;
7524 /* 3. link: current_fh is targetdir, saved_fh is source */
7525 argop
[3].argop
= OP_CLINK
;
7526 argop
[3].nfs_argop4_u
.opclink
.cnewname
= tnm
;
7528 /* 4. Get attributes of dir */
7529 argop
[4].argop
= OP_GETATTR
;
7530 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7531 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
7533 /* 5. If link was successful, restore current vp to file */
7534 argop
[5].argop
= OP_RESTOREFH
;
7536 /* 6. Get attributes of linked object */
7537 argop
[6].argop
= OP_GETATTR
;
7538 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
7539 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
7541 dnlc_remove(tdvp
, tnm
);
7546 rfs4call(VTOMI4(svp
), &args
, &res
, cr
, &doqueue
, 0, &e
);
7548 needrecov
= nfs4_needs_recovery(&e
, FALSE
, svp
->v_vfsp
);
7549 if (e
.error
!= 0 && !needrecov
) {
7550 PURGE_ATTRCACHE4(tdvp
);
7551 PURGE_ATTRCACHE4(svp
);
7552 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
, needrecov
);
7559 abort
= nfs4_start_recovery(&e
, VTOMI4(svp
), svp
, tdvp
,
7560 NULL
, NULL
, OP_LINK
, NULL
, NULL
, NULL
);
7561 if (abort
== FALSE
) {
7562 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
,
7564 kmem_free(argop
, argoplist_size
);
7566 (void) xdr_free(xdr_COMPOUND4res_clnt
,
7571 PURGE_ATTRCACHE4(tdvp
);
7572 PURGE_ATTRCACHE4(svp
);
7573 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
,
7574 &recov_state
, needrecov
);
7577 /* fall through for res.status case */
7581 nfs4_end_op(VTOMI4(svp
), svp
, tdvp
, &recov_state
, needrecov
);
7585 /* If link succeeded, then don't return error */
7586 e
.error
= geterrno4(res
.status
);
7587 if (res
.array_len
<= 4) {
7589 * Either Putfh, Savefh, Putfh dir, or Link failed
7591 PURGE_ATTRCACHE4(svp
);
7592 PURGE_ATTRCACHE4(tdvp
);
7593 if (e
.error
== EOPNOTSUPP
) {
7594 mutex_enter(&mi
->mi_lock
);
7595 mi
->mi_flags
&= ~MI4_LINK
;
7596 mutex_exit(&mi
->mi_lock
);
7598 /* Remap EISDIR to EPERM for non-root user for SVVS */
7600 if (e
.error
== EISDIR
&& crgetuid(cr
) != 0)
7606 /* either no error or one of the postop getattr failed */
7609 * XXX - if LINK succeeded, but no attrs were returned for link
7610 * file, purge its cache.
7612 * XXX Perform a simplified version of wcc checking. Instead of
7613 * have another getattr to get pre-op, just purge cache if
7614 * any of the ops prior to and including the getattr failed.
7615 * If the getattr succeeded then update the attrcache accordingly.
7619 * update cache with link file postattrs.
7620 * Note: at this point resop points to link res.
7622 resop
= &res
.array
[3]; /* link res */
7623 ln_res
= &resop
->nfs_resop4_u
.oplink
;
7624 if (res
.status
== NFS4_OK
)
7625 e
.error
= nfs4_update_attrcache(res
.status
,
7626 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
,
7630 * Call makenfs4node to create the new shadow vp for tnm.
7631 * We pass NULL attrs because we just cached attrs for
7632 * the src object. All we're trying to accomplish is to
7633 * to create the new shadow vnode.
7635 nvp
= makenfs4node(VTOR4(svp
)->r_fh
, NULL
, tdvp
->v_vfsp
, t
, cr
,
7636 tdvp
, fn_get(VTOSV(tdvp
)->sv_name
, tnm
, VTOR4(svp
)->r_fh
));
7638 /* Update target cache attribute, readdir and dnlc caches */
7639 dinfo
.di_garp
= &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
7640 dinfo
.di_time_call
= t
;
7643 nfs4_update_dircaches(&ln_res
->cinfo
, tdvp
, nvp
, tnm
, &dinfo
);
7644 ASSERT(nfs4_consistent_type(tdvp
));
7645 ASSERT(nfs4_consistent_type(svp
));
7646 ASSERT(nfs4_consistent_type(nvp
));
7653 * Notify the source file of this link operation.
7657 if (IS_SHADOW(svp
, trp
))
7659 vnevent_link(tvp
, ct
);
7662 kmem_free(argop
, argoplist_size
);
7664 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
7666 nfs_rw_exit(&tdrp
->r_rwlock
);
7673 nfs4_rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
7674 caller_context_t
*ct
, int flags
)
7678 if (nfs_zone() != VTOMI4(odvp
)->mi_zone
)
7680 if (VOP_REALVP(ndvp
, &realvp
, ct
) == 0)
7683 return (nfs4rename(odvp
, onm
, ndvp
, nnm
, cr
, ct
));
7687 * nfs4rename does the real work of renaming in NFS Version 4.
7689 * A file handle is considered volatile for renaming purposes if either
7690 * of the volatile bits are turned on. However, the compound may differ
7691 * based on the likelihood of the filehandle to change during rename.
7694 nfs4rename(vnode_t
*odvp
, char *onm
, vnode_t
*ndvp
, char *nnm
, cred_t
*cr
,
7695 caller_context_t
*ct
)
7699 vnode_t
*nvp
= NULL
;
7700 vnode_t
*ovp
= NULL
;
7701 char *tmpname
= NULL
;
7707 nfsstat4 stat
= NFS4_OK
;
7709 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
7710 ASSERT(nfs4_consistent_type(odvp
));
7711 ASSERT(nfs4_consistent_type(ndvp
));
7713 if (onm
[0] == '.' && (onm
[1] == '\0' ||
7714 (onm
[1] == '.' && onm
[2] == '\0')))
7717 if (nnm
[0] == '.' && (nnm
[1] == '\0' ||
7718 (nnm
[1] == '.' && nnm
[2] == '\0')))
7723 if ((intptr_t)odrp
< (intptr_t)ndrp
) {
7724 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR4(odvp
)))
7726 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR4(ndvp
))) {
7727 nfs_rw_exit(&odrp
->r_rwlock
);
7731 if (nfs_rw_enter_sig(&ndrp
->r_rwlock
, RW_WRITER
, INTR4(ndvp
)))
7733 if (nfs_rw_enter_sig(&odrp
->r_rwlock
, RW_WRITER
, INTR4(odvp
))) {
7734 nfs_rw_exit(&ndrp
->r_rwlock
);
7740 * Lookup the target file. If it exists, it needs to be
7741 * checked to see whether it is a mount point and whether
7742 * it is active (open).
7744 error
= nfs4lookup(ndvp
, nnm
, &nvp
, cr
, 0);
7748 ASSERT(nfs4_consistent_type(nvp
));
7750 * If this file has been mounted on, then just
7751 * return busy because renaming to it would remove
7752 * the mounted file system from the name space.
7754 if (vn_ismntpt(nvp
)) {
7756 nfs_rw_exit(&odrp
->r_rwlock
);
7757 nfs_rw_exit(&ndrp
->r_rwlock
);
7762 * First just remove the entry from the name cache, as it
7763 * is most likely the only entry for this vp.
7765 dnlc_remove(ndvp
, nnm
);
7769 if (nvp
->v_type
!= VREG
) {
7771 * Purge the name cache of all references to this vnode
7772 * so that we can check the reference count to infer
7773 * whether it is active or not.
7775 if (nvp
->v_count
> 1)
7778 isactive
= nvp
->v_count
> 1;
7780 mutex_enter(&rp
->r_os_lock
);
7781 isactive
= list_head(&rp
->r_open_streams
) != NULL
;
7782 mutex_exit(&rp
->r_os_lock
);
7786 * If the vnode is active and is not a directory,
7787 * arrange to rename it to a
7788 * temporary file so that it will continue to be
7789 * accessible. This implements the "unlink-open-file"
7790 * semantics for the target of a rename operation.
7791 * Before doing this though, make sure that the
7792 * source and target files are not already the same.
7794 if (isactive
&& nvp
->v_type
!= VDIR
) {
7796 * Lookup the source name.
7798 error
= nfs4lookup(odvp
, onm
, &ovp
, cr
, 0);
7801 * The source name *should* already exist.
7805 nfs_rw_exit(&odrp
->r_rwlock
);
7806 nfs_rw_exit(&ndrp
->r_rwlock
);
7810 ASSERT(nfs4_consistent_type(ovp
));
7813 * Compare the two vnodes. If they are the same,
7814 * just release all held vnodes and return success.
7816 if (VN_CMP(ovp
, nvp
)) {
7819 nfs_rw_exit(&odrp
->r_rwlock
);
7820 nfs_rw_exit(&ndrp
->r_rwlock
);
7825 * Can't mix and match directories and non-
7826 * directories in rename operations. We already
7827 * know that the target is not a directory. If
7828 * the source is a directory, return an error.
7830 if (ovp
->v_type
== VDIR
) {
7833 nfs_rw_exit(&odrp
->r_rwlock
);
7834 nfs_rw_exit(&ndrp
->r_rwlock
);
7839 * The target file exists, is not the same as
7840 * the source file, and is active. We first
7841 * try to Link it to a temporary filename to
7842 * avoid having the server removing the file
7843 * completely (which could cause data loss to
7844 * the user's POV in the event the Rename fails
7845 * -- see bug 1165874).
7848 * The do_link and did_link booleans are
7849 * introduced in the event we get NFS4ERR_FILE_OPEN
7850 * returned for the Rename. Some servers can
7851 * not Rename over an Open file, so they return
7852 * this error. The client needs to Remove the
7853 * newly created Link and do two Renames, just
7854 * as if the server didn't support LINK.
7856 tmpname
= newname();
7860 error
= nfs4_link(ndvp
, nvp
, tmpname
, cr
,
7863 if (error
== EOPNOTSUPP
|| !do_link
) {
7864 error
= nfs4_rename(ndvp
, nnm
, ndvp
, tmpname
,
7871 kmem_free(tmpname
, MAXNAMELEN
);
7874 nfs_rw_exit(&odrp
->r_rwlock
);
7875 nfs_rw_exit(&ndrp
->r_rwlock
);
7879 mutex_enter(&rp
->r_statelock
);
7880 if (rp
->r_unldvp
== NULL
) {
7882 rp
->r_unldvp
= ndvp
;
7883 if (rp
->r_unlcred
!= NULL
)
7884 crfree(rp
->r_unlcred
);
7887 rp
->r_unlname
= tmpname
;
7890 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
7891 rp
->r_unlname
= tmpname
;
7893 mutex_exit(&rp
->r_statelock
);
7896 (void) nfs4delegreturn(VTOR4(nvp
), NFS4_DR_PUSH
|NFS4_DR_REOPEN
);
7898 ASSERT(nfs4_consistent_type(nvp
));
7903 * When renaming directories to be a subdirectory of a
7904 * different parent, the dnlc entry for ".." will no
7905 * longer be valid, so it must be removed.
7907 * We do a lookup here to determine whether we are renaming
7908 * a directory and we need to check if we are renaming
7909 * an unlinked file. This might have already been done
7910 * in previous code, so we check ovp == NULL to avoid
7913 error
= nfs4lookup(odvp
, onm
, &ovp
, cr
, 0);
7915 * The source name *should* already exist.
7918 nfs_rw_exit(&odrp
->r_rwlock
);
7919 nfs_rw_exit(&ndrp
->r_rwlock
);
7925 ASSERT(ovp
!= NULL
);
7926 ASSERT(nfs4_consistent_type(ovp
));
7930 * Is the object being renamed a dir, and if so, is
7931 * it being renamed to a child of itself? The underlying
7932 * fs should ultimately return EINVAL for this case;
7933 * however, buggy beta non-Solaris NFSv4 servers at
7934 * interop testing events have allowed this behavior,
7935 * and it caused our client to panic due to a recursive
7936 * mutex_enter in fn_move.
7938 * The tedious locking in fn_move could be changed to
7939 * deal with this case, and the client could avoid the
7940 * panic; however, the client would just confuse itself
7941 * later and misbehave. A better way to handle the broken
7942 * server is to detect this condition and return EINVAL
7943 * without ever sending the the bogus rename to the server.
7944 * We know the rename is invalid -- just fail it now.
7946 if (ovp
->v_type
== VDIR
&& VN_CMP(ndvp
, ovp
)) {
7948 nfs_rw_exit(&odrp
->r_rwlock
);
7949 nfs_rw_exit(&ndrp
->r_rwlock
);
7956 (void) nfs4delegreturn(VTOR4(ovp
), NFS4_DR_PUSH
|NFS4_DR_REOPEN
);
7959 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7960 * possible for the filehandle to change due to the rename.
7961 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7962 * the fh will not change because of the rename, but we still need
7963 * to update its rnode entry with the new name for
7964 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7965 * has no effect on these for now, but for future improvements,
7966 * we might want to use it too to simplify handling of files
7967 * that are open with that flag on. (XXX)
7970 if (NFS4_VOLATILE_FH(mi
))
7971 error
= nfs4rename_volatile_fh(odvp
, onm
, ovp
, ndvp
, nnm
, cr
,
7974 error
= nfs4rename_persistent_fh(odvp
, onm
, ovp
, ndvp
, nnm
, cr
,
7977 ASSERT(nfs4_consistent_type(odvp
));
7978 ASSERT(nfs4_consistent_type(ndvp
));
7979 ASSERT(nfs4_consistent_type(ovp
));
7981 if (stat
== NFS4ERR_FILE_OPEN
&& did_link
) {
7984 * Before the 'link_call' code, we did a nfs4_lookup
7985 * that puts a VN_HOLD on nvp. After the nfs4_link
7986 * call we call VN_RELE to match that hold. We need
7987 * to place an additional VN_HOLD here since we will
7988 * be hitting that VN_RELE again.
7992 (void) nfs4_remove(ndvp
, tmpname
, cr
, NULL
, 0);
7994 /* Undo the unlinked file naming stuff we just did */
7995 mutex_enter(&rp
->r_statelock
);
7998 rp
->r_unldvp
= NULL
;
7999 if (rp
->r_unlcred
!= NULL
)
8000 crfree(rp
->r_unlcred
);
8001 rp
->r_unlcred
= NULL
;
8002 /* rp->r_unlanme points to tmpname */
8004 kmem_free(rp
->r_unlname
, MAXNAMELEN
);
8005 rp
->r_unlname
= NULL
;
8007 mutex_exit(&rp
->r_statelock
);
8017 nfs_rw_exit(&odrp
->r_rwlock
);
8018 nfs_rw_exit(&ndrp
->r_rwlock
);
8026 * when renaming directories to be a subdirectory of a
8027 * different parent, the dnlc entry for ".." will no
8028 * longer be valid, so it must be removed
8032 if (ovp
->v_type
== VDIR
) {
8033 dnlc_remove(ovp
, "..");
8034 if (rp
->r_dir
!= NULL
)
8035 nfs4_purge_rddir_cache(ovp
);
8040 * If we are renaming the unlinked file, update the
8041 * r_unldvp and r_unlname as needed.
8043 mutex_enter(&rp
->r_statelock
);
8044 if (rp
->r_unldvp
!= NULL
) {
8045 if (strcmp(rp
->r_unlname
, onm
) == 0) {
8046 (void) strncpy(rp
->r_unlname
, nnm
, MAXNAMELEN
);
8047 rp
->r_unlname
[MAXNAMELEN
- 1] = '\0';
8048 if (ndvp
!= rp
->r_unldvp
) {
8049 VN_RELE(rp
->r_unldvp
);
8050 rp
->r_unldvp
= ndvp
;
8055 mutex_exit(&rp
->r_statelock
);
8058 * Notify the rename vnevents to source vnode, and to the target
8059 * vnode if it already existed.
8065 * Notify the vnode. Each links is represented by
8066 * a different vnode, in nfsv4.
8071 if (IS_SHADOW(nvp
, trp
))
8073 vnevent_rename_dest(tvp
, ndvp
, nnm
, ct
);
8077 * if the source and destination directory are not the
8078 * same notify the destination directory.
8080 if (VTOR4(odvp
) != VTOR4(ndvp
)) {
8083 if (IS_SHADOW(ndvp
, trp
))
8085 vnevent_rename_dest_dir(tvp
, ct
);
8090 if (IS_SHADOW(ovp
, trp
))
8092 vnevent_rename_src(tvp
, odvp
, onm
, ct
);
8100 nfs_rw_exit(&odrp
->r_rwlock
);
8101 nfs_rw_exit(&ndrp
->r_rwlock
);
8107 * When the parent directory has changed, sv_dfh must be updated
8110 update_parentdir_sfh(vnode_t
*vp
, vnode_t
*ndvp
)
8112 svnode_t
*sv
= VTOSV(vp
);
8113 nfs4_sharedfh_t
*old_dfh
= sv
->sv_dfh
;
8114 nfs4_sharedfh_t
*new_dfh
= VTOR4(ndvp
)->r_fh
;
8117 sv
->sv_dfh
= new_dfh
;
8118 sfh4_rele(&old_dfh
);
8122 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8123 * when it is known that the filehandle is persistent through rename.
8125 * Rename requires that the current fh be the target directory and the
8126 * saved fh be the source directory. After the operation, the current fh
8128 * The compound op structure for persistent fh rename is:
8129 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8130 * Rather than bother with the directory postop args, we'll simply
8131 * update that a change occurred in the cache, so no post-op getattrs.
8134 nfs4rename_persistent_fh(vnode_t
*odvp
, char *onm
, vnode_t
*renvp
,
8135 vnode_t
*ndvp
, char *nnm
, cred_t
*cr
, nfsstat4
*statp
)
8137 COMPOUND4args_clnt args
;
8138 COMPOUND4res_clnt res
, *resp
= NULL
;
8141 int doqueue
, argoplist_size
;
8143 rnode4_t
*odrp
= VTOR4(odvp
);
8144 rnode4_t
*ndrp
= VTOR4(ndvp
);
8147 nfs4_recov_state_t recov_state
;
8148 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8149 dirattr_info_t dinfo
, *dinfop
;
8151 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
8153 recov_state
.rs_flags
= 0;
8154 recov_state
.rs_num_retry_despite_err
= 0;
8157 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8159 * If source/target are different dirs, then append putfh(src); getattr
8161 args
.array_len
= (odvp
== ndvp
) ? 5 : 7;
8162 argoplist_size
= args
.array_len
* sizeof (nfs_argop4
);
8163 args
.array
= argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
8168 /* No need to Lookup the file, persistent fh */
8169 args
.ctag
= TAG_RENAME
;
8172 e
.error
= nfs4_start_op(mi
, odvp
, ndvp
, &recov_state
);
8174 kmem_free(argop
, argoplist_size
);
8178 /* 0: putfh source directory */
8179 argop
[0].argop
= OP_CPUTFH
;
8180 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= odrp
->r_fh
;
8182 /* 1: Save source fh to free up current for target */
8183 argop
[1].argop
= OP_SAVEFH
;
8185 /* 2: putfh targetdir */
8186 argop
[2].argop
= OP_CPUTFH
;
8187 argop
[2].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8189 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8190 argop
[3].argop
= OP_CRENAME
;
8191 argop
[3].nfs_argop4_u
.opcrename
.coldname
= onm
;
8192 argop
[3].nfs_argop4_u
.opcrename
.cnewname
= nnm
;
8194 /* 4: getattr (targetdir) */
8195 argop
[4].argop
= OP_GETATTR
;
8196 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8197 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
8201 /* 5: putfh (sourcedir) */
8202 argop
[5].argop
= OP_CPUTFH
;
8203 argop
[5].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8205 /* 6: getattr (sourcedir) */
8206 argop
[6].argop
= OP_GETATTR
;
8207 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8208 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
8211 dnlc_remove(odvp
, onm
);
8212 dnlc_remove(ndvp
, nnm
);
8215 dinfo
.di_time_call
= gethrtime();
8216 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8218 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8220 PURGE_ATTRCACHE4(odvp
);
8221 PURGE_ATTRCACHE4(ndvp
);
8223 *statp
= res
.status
;
8227 if (nfs4_start_recovery(&e
, mi
, odvp
, ndvp
, NULL
, NULL
,
8228 OP_RENAME
, NULL
, NULL
, NULL
) == FALSE
) {
8229 nfs4_end_op(mi
, odvp
, ndvp
, &recov_state
, needrecov
);
8231 (void) xdr_free(xdr_COMPOUND4res_clnt
,
8240 * as long as OP_RENAME
8242 if (res
.status
!= NFS4_OK
&& res
.array_len
<= 4) {
8243 e
.error
= geterrno4(res
.status
);
8244 PURGE_ATTRCACHE4(odvp
);
8245 PURGE_ATTRCACHE4(ndvp
);
8247 * System V defines rename to return EEXIST, not
8248 * ENOTEMPTY if the target directory is not empty.
8249 * Over the wire, the error is NFSERR_ENOTEMPTY
8250 * which geterrno4 maps to ENOTEMPTY.
8252 if (e
.error
== ENOTEMPTY
)
8256 resop
= &res
.array
[3]; /* rename res */
8257 rn_res
= &resop
->nfs_resop4_u
.oprename
;
8259 if (res
.status
== NFS4_OK
) {
8261 * Update target attribute, readdir and dnlc
8265 &res
.array
[4].nfs_resop4_u
.opgetattr
.ga_res
;
8271 nfs4_update_dircaches(&rn_res
->target_cinfo
,
8272 ndvp
, NULL
, NULL
, dinfop
);
8275 * Update source attribute, readdir and dnlc caches
8279 update_parentdir_sfh(renvp
, ndvp
);
8283 &(res
.array
[6].nfs_resop4_u
.
8286 nfs4_update_dircaches(&rn_res
->source_cinfo
,
8287 odvp
, NULL
, NULL
, dinfop
);
8290 fn_move(VTOSV(renvp
)->sv_name
, VTOSV(ndvp
)->sv_name
,
8296 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8297 nfs4_end_op(mi
, odvp
, ndvp
, &recov_state
, needrecov
);
8298 kmem_free(argop
, argoplist_size
);
8304 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8305 * it is possible for the filehandle to change due to the rename.
8307 * The compound req in this case includes a post-rename lookup and getattr
8308 * to ensure that we have the correct fh and attributes for the object.
8310 * Rename requires that the current fh be the target directory and the
8311 * saved fh be the source directory. After the operation, the current fh
8314 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8315 * update the filehandle for the renamed object. We also get the old
8316 * filehandle for historical reasons; this should be taken out sometime.
8317 * This results in a rather cumbersome compound...
8319 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8320 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8324 nfs4rename_volatile_fh(vnode_t
*odvp
, char *onm
, vnode_t
*ovp
,
8325 vnode_t
*ndvp
, char *nnm
, cred_t
*cr
, nfsstat4
*statp
)
8327 COMPOUND4args_clnt args
;
8328 COMPOUND4res_clnt res
, *resp
= NULL
;
8334 rnode4_t
*odrp
= VTOR4(odvp
); /* old directory */
8335 rnode4_t
*ndrp
= VTOR4(ndvp
); /* new directory */
8336 rnode4_t
*orp
= VTOR4(ovp
); /* object being renamed */
8340 nfs4_recov_state_t recov_state
;
8342 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8343 dirattr_info_t dinfo
, *dinfop
= &dinfo
;
8345 ASSERT(nfs_zone() == VTOMI4(odvp
)->mi_zone
);
8347 recov_state
.rs_flags
= 0;
8348 recov_state
.rs_num_retry_despite_err
= 0;
8354 * There is a window between the RPC and updating the path and
8355 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8356 * code, so that it doesn't try to use the old path during that
8359 mutex_enter(&orp
->r_statelock
);
8360 while (orp
->r_flags
& R4RECEXPFH
) {
8361 klwp_t
*lwp
= ttolwp(curthread
);
8365 if (cv_wait_sig(&orp
->r_cv
, &orp
->r_statelock
) == 0) {
8366 mutex_exit(&orp
->r_statelock
);
8374 orp
->r_flags
|= R4RECEXPFH
;
8375 mutex_exit(&orp
->r_statelock
);
8379 args
.ctag
= TAG_RENAME_VFH
;
8380 args
.array_len
= (odvp
== ndvp
) ? 10 : 12;
8381 argoplist_size
= args
.array_len
* sizeof (nfs_argop4
);
8382 argop
= kmem_alloc(argoplist_size
, KM_SLEEP
);
8386 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8387 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8388 * LOOKUP(trgt), GETFH(new), GETATTR,
8391 * add putfh(sourcedir), getattr(sourcedir) }
8395 e
.error
= nfs4_start_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8396 &recov_state
, NULL
);
8398 kmem_free(argop
, argoplist_size
);
8399 mutex_enter(&orp
->r_statelock
);
8400 orp
->r_flags
&= ~R4RECEXPFH
;
8401 cv_broadcast(&orp
->r_cv
);
8402 mutex_exit(&orp
->r_statelock
);
8406 /* 0: putfh source directory */
8407 argop
[0].argop
= OP_CPUTFH
;
8408 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= odrp
->r_fh
;
8410 /* 1: Save source fh to free up current for target */
8411 argop
[1].argop
= OP_SAVEFH
;
8413 /* 2: Lookup pre-rename fh of renamed object */
8414 argop
[2].argop
= OP_CLOOKUP
;
8415 argop
[2].nfs_argop4_u
.opclookup
.cname
= onm
;
8417 /* 3: getfh fh of renamed object (before rename) */
8418 argop
[3].argop
= OP_GETFH
;
8420 /* 4: putfh targetdir */
8421 argop
[4].argop
= OP_CPUTFH
;
8422 argop
[4].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8424 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8425 argop
[5].argop
= OP_CRENAME
;
8426 argop
[5].nfs_argop4_u
.opcrename
.coldname
= onm
;
8427 argop
[5].nfs_argop4_u
.opcrename
.cnewname
= nnm
;
8429 /* 6: getattr of target dir (post op attrs) */
8430 argop
[6].argop
= OP_GETATTR
;
8431 argop
[6].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8432 argop
[6].nfs_argop4_u
.opgetattr
.mi
= mi
;
8434 /* 7: Lookup post-rename fh of renamed object */
8435 argop
[7].argop
= OP_CLOOKUP
;
8436 argop
[7].nfs_argop4_u
.opclookup
.cname
= nnm
;
8438 /* 8: getfh fh of renamed object (after rename) */
8439 argop
[8].argop
= OP_GETFH
;
8441 /* 9: getattr of renamed object */
8442 argop
[9].argop
= OP_GETATTR
;
8443 argop
[9].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8444 argop
[9].nfs_argop4_u
.opgetattr
.mi
= mi
;
8447 * If source/target dirs are different, then get new post-op
8448 * attrs for source dir also.
8451 /* 10: putfh (sourcedir) */
8452 argop
[10].argop
= OP_CPUTFH
;
8453 argop
[10].nfs_argop4_u
.opcputfh
.sfh
= ndrp
->r_fh
;
8455 /* 11: getattr (sourcedir) */
8456 argop
[11].argop
= OP_GETATTR
;
8457 argop
[11].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8458 argop
[11].nfs_argop4_u
.opgetattr
.mi
= mi
;
8461 dnlc_remove(odvp
, onm
);
8462 dnlc_remove(ndvp
, nnm
);
8466 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8468 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8470 PURGE_ATTRCACHE4(odvp
);
8471 PURGE_ATTRCACHE4(ndvp
);
8473 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8474 &recov_state
, needrecov
);
8478 *statp
= res
.status
;
8484 abort
= nfs4_start_recovery(&e
, mi
, odvp
, ndvp
, NULL
, NULL
,
8485 OP_RENAME
, NULL
, NULL
, NULL
);
8486 if (abort
== FALSE
) {
8487 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8488 &recov_state
, needrecov
);
8489 kmem_free(argop
, argoplist_size
);
8491 (void) xdr_free(xdr_COMPOUND4res_clnt
,
8493 mutex_enter(&orp
->r_statelock
);
8494 orp
->r_flags
&= ~R4RECEXPFH
;
8495 cv_broadcast(&orp
->r_cv
);
8496 mutex_exit(&orp
->r_statelock
);
8500 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
,
8501 &recov_state
, needrecov
);
8504 /* fall through for res.status case */
8510 * If OP_RENAME (or any prev op) failed, then return an error.
8511 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8513 if ((res
.status
!= NFS4_OK
) && (res
.array_len
<= 6)) {
8515 * Error in an op other than last Getattr
8517 e
.error
= geterrno4(res
.status
);
8518 PURGE_ATTRCACHE4(odvp
);
8519 PURGE_ATTRCACHE4(ndvp
);
8521 * System V defines rename to return EEXIST, not
8522 * ENOTEMPTY if the target directory is not empty.
8523 * Over the wire, the error is NFSERR_ENOTEMPTY
8524 * which geterrno4 maps to ENOTEMPTY.
8526 if (e
.error
== ENOTEMPTY
)
8528 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
, &recov_state
,
8533 /* rename results */
8534 rn_res
= &res
.array
[5].nfs_resop4_u
.oprename
;
8536 if (res
.status
== NFS4_OK
) {
8537 /* Update target attribute, readdir and dnlc caches */
8539 &res
.array
[6].nfs_resop4_u
.opgetattr
.ga_res
;
8541 dinfo
.di_time_call
= t
;
8545 /* Update source cache attribute, readdir and dnlc caches */
8546 nfs4_update_dircaches(&rn_res
->target_cinfo
, ndvp
, NULL
, NULL
, dinfop
);
8548 /* Update source cache attribute, readdir and dnlc caches */
8550 update_parentdir_sfh(ovp
, ndvp
);
8553 * If dinfop is non-NULL, then compound succeded, so
8554 * set di_garp to attrs for source dir. dinfop is only
8555 * set to NULL when compound fails.
8559 &res
.array
[11].nfs_resop4_u
.opgetattr
.ga_res
;
8560 nfs4_update_dircaches(&rn_res
->source_cinfo
, odvp
, NULL
, NULL
,
8565 * Update the rnode with the new component name and args,
8566 * and if the file handle changed, also update it with the new fh.
8567 * This is only necessary if the target object has an rnode
8568 * entry and there is no need to create one for it.
8570 resop
= &res
.array
[8]; /* getfh new res */
8571 ngf_res
= &resop
->nfs_resop4_u
.opgetfh
;
8574 * Update the path and filehandle for the renamed object.
8576 nfs4rename_update(ovp
, ndvp
, &ngf_res
->object
, nnm
);
8578 nfs4_end_fop(mi
, odvp
, ndvp
, OH_VFH_RENAME
, &recov_state
, needrecov
);
8580 if (res
.status
== NFS4_OK
) {
8581 resop
++; /* getattr res */
8582 e
.error
= nfs4_update_attrcache(res
.status
,
8583 &resop
->nfs_resop4_u
.opgetattr
.ga_res
,
8588 kmem_free(argop
, argoplist_size
);
8590 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8591 mutex_enter(&orp
->r_statelock
);
8592 orp
->r_flags
&= ~R4RECEXPFH
;
8593 cv_broadcast(&orp
->r_cv
);
8594 mutex_exit(&orp
->r_statelock
);
8601 nfs4_mkdir(vnode_t
*dvp
, char *nm
, struct vattr
*va
, vnode_t
**vpp
, cred_t
*cr
,
8602 caller_context_t
*ct
, int flags
, vsecattr_t
*vsecp
)
8607 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
8610 * As ".." has special meaning and rather than send a mkdir
8611 * over the wire to just let the server freak out, we just
8612 * short circuit it here and return EEXIST
8614 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0')
8618 * Decision to get the right gid and setgid bit of the
8619 * new directory is now made in call_nfs4_create_req.
8621 va
->va_mask
|= AT_MODE
;
8622 error
= call_nfs4_create_req(dvp
, nm
, NULL
, va
, &vp
, cr
, NF4DIR
);
8632 * rmdir is using the same remove v4 op as does remove.
8633 * Remove requires that the current fh be the target directory.
8634 * After the operation, the current fh is unchanged.
8635 * The compound op structure is:
8636 * PUTFH(targetdir), REMOVE
8640 nfs4_rmdir(vnode_t
*dvp
, char *nm
, vnode_t
*cdir
, cred_t
*cr
,
8641 caller_context_t
*ct
, int flags
)
8643 int need_end_op
= FALSE
;
8644 COMPOUND4args_clnt args
;
8645 COMPOUND4res_clnt res
, *resp
= NULL
;
8647 nfs_argop4 argop
[3];
8653 bool_t needrecov
= FALSE
;
8654 nfs4_recov_state_t recov_state
;
8655 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
8656 dirattr_info_t dinfo
, *dinfop
;
8658 if (nfs_zone() != VTOMI4(dvp
)->mi_zone
)
8661 * As ".." has special meaning and rather than send a rmdir
8662 * over the wire to just let the server freak out, we just
8663 * short circuit it here and return EEXIST
8665 if (nm
[0] == '.' && nm
[1] == '.' && nm
[2] == '\0')
8669 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_WRITER
, INTR4(dvp
)))
8673 * Attempt to prevent a rmdir(".") from succeeding.
8675 e
.error
= nfs4lookup(dvp
, nm
, &vp
, cr
, 0);
8677 nfs_rw_exit(&drp
->r_rwlock
);
8682 nfs_rw_exit(&drp
->r_rwlock
);
8687 * Since nfsv4 remove op works on both files and directories,
8688 * check that the removed object is indeed a directory.
8690 if (vp
->v_type
!= VDIR
) {
8692 nfs_rw_exit(&drp
->r_rwlock
);
8697 * First just remove the entry from the name cache, as it
8698 * is most likely an entry for this vp.
8700 dnlc_remove(dvp
, nm
);
8703 * If there vnode reference count is greater than one, then
8704 * there may be additional references in the DNLC which will
8705 * need to be purged. First, trying removing the entry for
8706 * the parent directory and see if that removes the additional
8707 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8708 * to completely remove any references to the directory which
8709 * might still exist in the DNLC.
8711 if (vp
->v_count
> 1) {
8712 dnlc_remove(vp
, "..");
8713 if (vp
->v_count
> 1)
8718 recov_state
.rs_flags
= 0;
8719 recov_state
.rs_num_retry_despite_err
= 0;
8722 args
.ctag
= TAG_RMDIR
;
8725 * Rmdir ops: putfh dir; remove
8730 e
.error
= nfs4_start_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
);
8732 nfs_rw_exit(&drp
->r_rwlock
);
8737 /* putfh directory */
8738 argop
[0].argop
= OP_CPUTFH
;
8739 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= drp
->r_fh
;
8742 argop
[1].argop
= OP_CREMOVE
;
8743 argop
[1].nfs_argop4_u
.opcremove
.ctarget
= nm
;
8745 /* getattr (postop attrs for dir that contained removed dir) */
8746 argop
[2].argop
= OP_GETATTR
;
8747 argop
[2].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
8748 argop
[2].nfs_argop4_u
.opgetattr
.mi
= mi
;
8750 dinfo
.di_time_call
= gethrtime();
8752 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
8754 PURGE_ATTRCACHE4(vp
);
8756 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
8758 PURGE_ATTRCACHE4(dvp
);
8762 if (nfs4_start_recovery(&e
, VTOMI4(dvp
), dvp
, NULL
, NULL
,
8763 NULL
, OP_REMOVE
, NULL
, NULL
, NULL
) == FALSE
) {
8765 (void) xdr_free(xdr_COMPOUND4res_clnt
,
8768 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
,
8770 need_end_op
= FALSE
;
8779 * Only return error if first 2 ops (OP_REMOVE or earlier)
8782 if (res
.status
!= NFS4_OK
&& res
.array_len
<= 2) {
8783 e
.error
= geterrno4(res
.status
);
8784 PURGE_ATTRCACHE4(dvp
);
8785 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
,
8786 &recov_state
, needrecov
);
8787 need_end_op
= FALSE
;
8788 nfs4_purge_stale_fh(e
.error
, dvp
, cr
);
8790 * System V defines rmdir to return EEXIST, not
8791 * ENOTEMPTY if the directory is not empty. Over
8792 * the wire, the error is NFSERR_ENOTEMPTY which
8793 * geterrno4 maps to ENOTEMPTY.
8795 if (e
.error
== ENOTEMPTY
)
8798 resop
= &res
.array
[1]; /* remove res */
8799 rm_res
= &resop
->nfs_resop4_u
.opremove
;
8801 if (res
.status
== NFS4_OK
) {
8802 resop
= &res
.array
[2]; /* dir attrs */
8804 &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
8810 /* Update dir attribute, readdir and dnlc caches */
8811 nfs4_update_dircaches(&rm_res
->cinfo
, dvp
, NULL
, NULL
,
8814 /* destroy rddir cache for dir that was removed */
8815 if (VTOR4(vp
)->r_dir
!= NULL
)
8816 nfs4_purge_rddir_cache(vp
);
8821 nfs4_end_op(VTOMI4(dvp
), dvp
, NULL
, &recov_state
, needrecov
);
8823 nfs_rw_exit(&drp
->r_rwlock
);
8826 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
8833 if (IS_SHADOW(vp
, trp
))
8835 vnevent_rmdir(tvp
, dvp
, nm
, ct
);
8845 nfs4_symlink(vnode_t
*dvp
, char *lnm
, struct vattr
*tva
, char *tnm
, cred_t
*cr
,
8846 caller_context_t
*ct
, int flags
)
8852 mntinfo4_t
*mi
= VTOMI4(dvp
);
8854 if (nfs_zone() != mi
->mi_zone
)
8856 if (!(mi
->mi_flags
& MI4_SYMLINK
))
8857 return (EOPNOTSUPP
);
8859 error
= call_nfs4_create_req(dvp
, lnm
, tnm
, tva
, &vp
, cr
, NF4LNK
);
8863 ASSERT(nfs4_consistent_type(vp
));
8865 if (nfs4_do_symlink_cache
&& rp
->r_symlink
.contents
== NULL
) {
8867 contents
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
8869 if (contents
!= NULL
) {
8870 mutex_enter(&rp
->r_statelock
);
8871 if (rp
->r_symlink
.contents
== NULL
) {
8872 rp
->r_symlink
.len
= strlen(tnm
);
8873 bcopy(tnm
, contents
, rp
->r_symlink
.len
);
8874 rp
->r_symlink
.contents
= contents
;
8875 rp
->r_symlink
.size
= MAXPATHLEN
;
8876 mutex_exit(&rp
->r_statelock
);
8878 mutex_exit(&rp
->r_statelock
);
8879 kmem_free((void *)contents
, MAXPATHLEN
);
8890 * Read directory entries.
8891 * There are some weird things to look out for here. The uio_loffset
8892 * field is either 0 or it is the offset returned from a previous
8893 * readdir. It is an opaque value used by the server to find the
8894 * correct directory block to read. The count field is the number
8895 * of blocks to read on the server. This is advisory only, the server
8896 * may return only one block's worth of entries. Entries may be compressed
8901 nfs4_readdir(vnode_t
*vp
, struct uio
*uiop
, cred_t
*cr
, int *eofp
,
8902 caller_context_t
*ct
, int flags
)
8910 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
8914 ASSERT(nfs_rw_lock_held(&rp
->r_rwlock
, RW_READER
));
8917 * Make sure that the directory cache is valid.
8919 if (rp
->r_dir
!= NULL
) {
8920 if (nfs_disable_rddir_cache
!= 0) {
8922 * Setting nfs_disable_rddir_cache in /etc/system
8923 * allows interoperability with servers that do not
8924 * properly update the attributes of directories.
8925 * Any cached information gets purged before an
8926 * access is made to it.
8928 nfs4_purge_rddir_cache(vp
);
8931 error
= nfs4_validate_caches(vp
, cr
);
8936 count
= MIN(uiop
->uio_iov
->iov_len
, MAXBSIZE
);
8939 * Short circuit last readdir which always returns 0 bytes.
8940 * This can be done after the directory has been read through
8941 * completely at least once. This will set r_direof which
8942 * can be used to find the value of the last cookie.
8944 mutex_enter(&rp
->r_statelock
);
8945 if (rp
->r_direof
!= NULL
&&
8946 uiop
->uio_loffset
== rp
->r_direof
->nfs4_ncookie
) {
8947 mutex_exit(&rp
->r_statelock
);
8949 nfs4_readdir_cache_shorts
++;
8957 * Look for a cache entry. Cache entries are identified
8958 * by the NFS cookie value and the byte count requested.
8960 rdc
= rddir4_cache_lookup(rp
, uiop
->uio_loffset
, count
);
8963 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8966 mutex_exit(&rp
->r_statelock
);
8971 * Check to see if we need to fill this entry in.
8973 if (rdc
->flags
& RDDIRREQ
) {
8974 rdc
->flags
&= ~RDDIRREQ
;
8975 rdc
->flags
|= RDDIR
;
8976 mutex_exit(&rp
->r_statelock
);
8981 nfs4readdir(vp
, rdc
, cr
);
8984 * Reacquire the lock, so that we can continue
8986 mutex_enter(&rp
->r_statelock
);
8988 * The entry is now complete
8990 rdc
->flags
&= ~RDDIR
;
8993 ASSERT(!(rdc
->flags
& RDDIR
));
8996 * If an error occurred while attempting
8997 * to fill the cache entry, mark the entry invalid and
8998 * just return the error.
9002 rdc
->flags
|= RDDIRREQ
;
9003 rddir4_cache_rele(rp
, rdc
);
9004 mutex_exit(&rp
->r_statelock
);
9009 * The cache entry is complete and good,
9010 * copyout the dirent structs to the calling
9013 error
= uiomove(rdc
->entries
, rdc
->actlen
, UIO_READ
, uiop
);
9016 * If no error occurred during the copyout,
9017 * update the offset in the uio struct to
9018 * contain the value of the next NFS 4 cookie
9019 * and set the eof value appropriately.
9022 uiop
->uio_loffset
= rdc
->nfs4_ncookie
;
9028 * Decide whether to do readahead. Don't if we
9029 * have already read to the end of directory.
9033 * Make the entry the direof only if it is cached
9035 if (rdc
->flags
& RDDIRCACHED
)
9037 rddir4_cache_rele(rp
, rdc
);
9038 mutex_exit(&rp
->r_statelock
);
9042 /* Determine if a readdir readahead should be done */
9043 if (!(rp
->r_flags
& R4LOOKUP
)) {
9044 rddir4_cache_rele(rp
, rdc
);
9045 mutex_exit(&rp
->r_statelock
);
9050 * Now look for a readahead entry.
9052 * Check to see whether we found an entry for the readahead.
9053 * If so, we don't need to do anything further, so free the new
9054 * entry if one was allocated. Otherwise, allocate a new entry, add
9055 * it to the cache, and then initiate an asynchronous readdir
9056 * operation to fill it.
9058 rrdc
= rddir4_cache_lookup(rp
, rdc
->nfs4_ncookie
, count
);
9061 * A readdir cache entry could not be obtained for the readahead. In
9062 * this case we skip the readahead and return.
9065 rddir4_cache_rele(rp
, rdc
);
9066 mutex_exit(&rp
->r_statelock
);
9071 * Check to see if we need to fill this entry in.
9073 if (rrdc
->flags
& RDDIRREQ
) {
9074 rrdc
->flags
&= ~RDDIRREQ
;
9075 rrdc
->flags
|= RDDIR
;
9076 rddir4_cache_rele(rp
, rdc
);
9077 mutex_exit(&rp
->r_statelock
);
9079 nfs4_readdir_readahead
++;
9084 nfs4_async_readdir(vp
, rrdc
, cr
, do_nfs4readdir
);
9088 rddir4_cache_rele(rp
, rrdc
);
9089 rddir4_cache_rele(rp
, rdc
);
9090 mutex_exit(&rp
->r_statelock
);
9095 do_nfs4readdir(vnode_t
*vp
, rddir4_cache
*rdc
, cred_t
*cr
)
9100 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
9105 * Obtain the readdir results for the caller.
9107 nfs4readdir(vp
, rdc
, cr
);
9109 mutex_enter(&rp
->r_statelock
);
9111 * The entry is now complete
9113 rdc
->flags
&= ~RDDIR
;
9117 rdc
->flags
|= RDDIRREQ
;
9118 rddir4_cache_rele(rp
, rdc
);
9119 mutex_exit(&rp
->r_statelock
);
9125 * Read directory entries.
9126 * There are some weird things to look out for here. The uio_loffset
9127 * field is either 0 or it is the offset returned from a previous
9128 * readdir. It is an opaque value used by the server to find the
9129 * correct directory block to read. The count field is the number
9130 * of blocks to read on the server. This is advisory only, the server
9131 * may return only one block's worth of entries. Entries may be compressed
9134 * Generates the following compound request:
9135 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9136 * must include a Lookupp as well. In this case, send:
9137 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9138 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9140 * Get complete attributes and filehandles for entries if this is the
9141 * first read of the directory. Otherwise, just get fileid's.
9144 nfs4readdir(vnode_t
*vp
, rddir4_cache
*rdc
, cred_t
*cr
)
9146 COMPOUND4args_clnt args
;
9147 COMPOUND4res_clnt res
;
9148 READDIR4args
*rargs
;
9149 READDIR4res_clnt
*rd_res
;
9151 nfs_argop4 argop
[5];
9153 rnode4_t
*rp
= VTOR4(vp
);
9154 mntinfo4_t
*mi
= VTOMI4(vp
);
9156 u_longlong_t nodeid
, pnodeid
; /* id's of dir and its parents */
9158 nfs_cookie4 cookie
= (nfs_cookie4
)rdc
->nfs4_cookie
;
9159 int num_ops
, res_opcnt
;
9160 bool_t needrecov
= FALSE
;
9161 nfs4_recov_state_t recov_state
;
9163 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
9165 ASSERT(nfs_zone() == mi
->mi_zone
);
9166 ASSERT(rdc
->flags
& RDDIR
);
9167 ASSERT(rdc
->entries
== NULL
);
9170 * If rp were a stub, it should have triggered and caused
9171 * a mount for us to get this far.
9173 ASSERT(!RP_ISSTUB(rp
));
9176 if (cookie
== (nfs_cookie4
)0 || cookie
== (nfs_cookie4
)1) {
9178 * Since nfsv4 readdir may not return entries for "." and "..",
9179 * the client must recreate them:
9180 * To find the correct nodeid, do the following:
9181 * For current node, get nodeid from dnlc.
9182 * - if current node is rootvp, set pnodeid to nodeid.
9183 * - else if parent is in the dnlc, get its nodeid from there.
9184 * - else add LOOKUPP+GETATTR to compound.
9186 nodeid
= rp
->r_attr
.va_nodeid
;
9187 if (vp
->v_flag
& VROOT
) {
9188 pnodeid
= nodeid
; /* root of mount point */
9190 dvp
= dnlc_lookup(vp
, "..");
9191 if (dvp
!= NULL
&& dvp
!= DNLC_NO_VNODE
) {
9192 /* parent in dnlc cache - no need for otw */
9193 pnodeid
= VTOR4(dvp
)->r_attr
.va_nodeid
;
9196 * parent not in dnlc cache,
9197 * do lookupp to get its id
9200 pnodeid
= 0; /* set later by getattr parent */
9206 recov_state
.rs_flags
= 0;
9207 recov_state
.rs_num_retry_despite_err
= 0;
9209 /* Save the original mount point security flavor */
9210 (void) save_mnt_secinfo(mi
->mi_curr_serv
);
9213 args
.ctag
= TAG_READDIR
;
9216 args
.array_len
= num_ops
;
9218 if (e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9219 &recov_state
, NULL
)) {
9221 * If readdir a node that is a stub for a crossed mount point,
9222 * keep the original secinfo flavor for the current file
9223 * system, not the crossed one.
9225 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9226 rdc
->error
= e
.error
;
9231 * Determine which attrs to request for dirents. This code
9232 * must be protected by nfs4_start/end_fop because of r_server
9233 * (which will change during failover recovery).
9236 if (rp
->r_flags
& (R4LOOKUP
| R4READDIRWATTR
)) {
9238 * Get all vattr attrs plus filehandle and rdattr_error
9240 rd_bitsval
= NFS4_VATTR_MASK
|
9241 FATTR4_RDATTR_ERROR_MASK
|
9242 FATTR4_FILEHANDLE_MASK
;
9244 if (rp
->r_flags
& R4READDIRWATTR
) {
9245 mutex_enter(&rp
->r_statelock
);
9246 rp
->r_flags
&= ~R4READDIRWATTR
;
9247 mutex_exit(&rp
->r_statelock
);
9250 servinfo4_t
*svp
= rp
->r_server
;
9253 * Already read directory. Use readdir with
9254 * no attrs (except for mounted_on_fileid) for updates.
9256 rd_bitsval
= FATTR4_RDATTR_ERROR_MASK
;
9259 * request mounted on fileid if supported, else request
9260 * fileid. maybe we should verify that fileid is supported
9261 * and request something else if not.
9263 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
9264 if (svp
->sv_supp_attrs
& FATTR4_MOUNTED_ON_FILEID_MASK
)
9265 rd_bitsval
|= FATTR4_MOUNTED_ON_FILEID_MASK
;
9266 nfs_rw_exit(&svp
->sv_lock
);
9269 /* putfh directory fh */
9270 argop
[0].argop
= OP_CPUTFH
;
9271 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
9273 argop
[1].argop
= OP_READDIR
;
9274 rargs
= &argop
[1].nfs_argop4_u
.opreaddir
;
9276 * 1 and 2 are reserved for client "." and ".." entry offset.
9277 * cookie 0 should be used over-the-wire to start reading at
9278 * the beginning of the directory excluding "." and "..".
9280 if (rdc
->nfs4_cookie
== 0 ||
9281 rdc
->nfs4_cookie
== 1 ||
9282 rdc
->nfs4_cookie
== 2) {
9283 rargs
->cookie
= (nfs_cookie4
)0;
9284 rargs
->cookieverf
= 0;
9286 rargs
->cookie
= (nfs_cookie4
)rdc
->nfs4_cookie
;
9287 mutex_enter(&rp
->r_statelock
);
9288 rargs
->cookieverf
= rp
->r_cookieverf4
;
9289 mutex_exit(&rp
->r_statelock
);
9291 rargs
->dircount
= MIN(rdc
->buflen
, mi
->mi_tsize
);
9292 rargs
->maxcount
= mi
->mi_tsize
;
9293 rargs
->attr_request
= rd_bitsval
;
9301 * If count < than the minimum required, we return no entries
9302 * and fail with EINVAL
9304 if (rargs
->dircount
< (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9305 rdc
->error
= EINVAL
;
9309 if (args
.array_len
== 5) {
9311 * Add lookupp and getattr for parent nodeid.
9313 argop
[2].argop
= OP_LOOKUPP
;
9315 argop
[3].argop
= OP_GETFH
;
9317 /* getattr parent */
9318 argop
[4].argop
= OP_GETATTR
;
9319 argop
[4].nfs_argop4_u
.opgetattr
.attr_request
= NFS4_VATTR_MASK
;
9320 argop
[4].nfs_argop4_u
.opgetattr
.mi
= mi
;
9325 if (mi
->mi_io_kstats
) {
9326 mutex_enter(&mi
->mi_lock
);
9327 kstat_runq_enter(KSTAT_IO_PTR(mi
->mi_io_kstats
));
9328 mutex_exit(&mi
->mi_lock
);
9331 /* capture the time of this call */
9332 rargs
->t
= t
= gethrtime();
9334 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, &e
);
9336 if (mi
->mi_io_kstats
) {
9337 mutex_enter(&mi
->mi_lock
);
9338 kstat_runq_exit(KSTAT_IO_PTR(mi
->mi_io_kstats
));
9339 mutex_exit(&mi
->mi_lock
);
9342 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
9345 * If RPC error occurred and it isn't an error that
9346 * triggers recovery, then go ahead and fail now.
9348 if (e
.error
!= 0 && !needrecov
) {
9349 rdc
->error
= e
.error
;
9356 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
9357 "nfs4readdir: initiating recovery.\n"));
9359 abort
= nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
9360 NULL
, OP_READDIR
, NULL
, NULL
, NULL
);
9361 if (abort
== FALSE
) {
9362 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9363 &recov_state
, needrecov
);
9365 (void) xdr_free(xdr_COMPOUND4res_clnt
,
9367 if (rdc
->entries
!= NULL
) {
9368 kmem_free(rdc
->entries
, rdc
->entlen
);
9369 rdc
->entries
= NULL
;
9375 rdc
->error
= e
.error
;
9379 /* fall through for res.status case */
9382 res_opcnt
= res
.array_len
;
9385 * If compound failed first 2 ops (PUTFH+READDIR), then return
9386 * failure here. Subsequent ops are for filling out dot-dot
9387 * dirent, and if they fail, we still want to give the caller
9388 * the dirents returned by (the successful) READDIR op, so we need
9389 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9391 * One example where PUTFH+READDIR ops would succeed but
9392 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9393 * but lacks x. In this case, a POSIX server's VOP_READDIR
9394 * would succeed; however, VOP_LOOKUP(..) would fail since no
9395 * x perm. We need to come up with a non-vendor-specific way
9396 * for a POSIX server to return d_ino from dotdot's dirent if
9397 * client only requests mounted_on_fileid, and just say the
9398 * LOOKUPP succeeded and fill out the GETATTR. However, if
9399 * client requested any mandatory attrs, server would be required
9400 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9405 if (res_opcnt
<= 2) {
9406 e
.error
= geterrno4(res
.status
);
9407 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_READDIR
,
9408 &recov_state
, needrecov
);
9409 nfs4_purge_stale_fh(e
.error
, vp
, cr
);
9410 rdc
->error
= e
.error
;
9411 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
9412 if (rdc
->entries
!= NULL
) {
9413 kmem_free(rdc
->entries
, rdc
->entlen
);
9414 rdc
->entries
= NULL
;
9417 * If readdir a node that is a stub for a
9418 * crossed mount point, keep the original
9419 * secinfo flavor for the current file system,
9420 * not the crossed one.
9422 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9427 resop
= &res
.array
[1]; /* readdir res */
9428 rd_res
= &resop
->nfs_resop4_u
.opreaddirclnt
;
9430 mutex_enter(&rp
->r_statelock
);
9431 rp
->r_cookieverf4
= rd_res
->cookieverf
;
9432 mutex_exit(&rp
->r_statelock
);
9435 * For "." and ".." entries
9437 * seek(cookie=0) -> "." entry with d_off = 1
9438 * seek(cookie=1) -> ".." entry with d_off = 2
9440 if (cookie
== (nfs_cookie4
) 0) {
9442 rd_res
->dotp
->d_ino
= nodeid
;
9443 if (rd_res
->dotdotp
)
9444 rd_res
->dotdotp
->d_ino
= pnodeid
;
9446 if (cookie
== (nfs_cookie4
) 1) {
9447 if (rd_res
->dotdotp
)
9448 rd_res
->dotdotp
->d_ino
= pnodeid
;
9452 /* LOOKUPP+GETATTR attemped */
9453 if (args
.array_len
== 5 && rd_res
->dotdotp
) {
9454 if (res
.status
== NFS4_OK
&& res_opcnt
== 5) {
9456 nfs4_sharedfh_t
*sfhp
;
9458 nfs4_ga_res_t
*garp
;
9460 resop
++; /* lookupp */
9461 resop
++; /* getfh */
9462 fhp
= &resop
->nfs_resop4_u
.opgetfh
.object
;
9464 resop
++; /* getattr of parent */
9467 * First, take care of finishing the
9470 garp
= &resop
->nfs_resop4_u
.opgetattr
.ga_res
;
9472 * The d_ino of .. must be the inode number
9473 * of the mounted filesystem.
9475 if (garp
->n4g_va
.va_mask
& AT_NODEID
)
9476 rd_res
->dotdotp
->d_ino
=
9477 garp
->n4g_va
.va_nodeid
;
9481 * Next, create the ".." dnlc entry
9483 sfhp
= sfh4_get(fhp
, mi
);
9484 if (!nfs4_make_dotdot(sfhp
, t
, vp
, cr
, &pvp
, 0)) {
9485 dnlc_update(vp
, "..", pvp
);
9492 if (mi
->mi_io_kstats
) {
9493 mutex_enter(&mi
->mi_lock
);
9494 KSTAT_IO_PTR(mi
->mi_io_kstats
)->reads
++;
9495 KSTAT_IO_PTR(mi
->mi_io_kstats
)->nread
+= rdc
->actlen
;
9496 mutex_exit(&mi
->mi_lock
);
9499 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
9503 * If readdir a node that is a stub for a crossed mount point,
9504 * keep the original secinfo flavor for the current file system,
9505 * not the crossed one.
9507 (void) check_mnt_secinfo(mi
->mi_curr_serv
, vp
);
9509 nfs4_end_fop(mi
, vp
, NULL
, OH_READDIR
, &recov_state
, needrecov
);
9514 nfs4_bio(struct buf
*bp
, stable_how4
*stab_comm
, cred_t
*cr
, bool_t readahead
)
9516 rnode4_t
*rp
= VTOR4(bp
->b_vp
);
9519 cred_t
*cred_otw
= NULL
;
9521 nfs4_open_stream_t
*osp
= NULL
;
9522 bool_t first_time
= TRUE
; /* first time getting otw cred */
9523 bool_t last_time
= FALSE
; /* last time getting otw cred */
9525 ASSERT(nfs_zone() == VTOMI4(bp
->b_vp
)->mi_zone
);
9527 DTRACE_IO1(start
, struct buf
*, bp
);
9528 offset
= ldbtob(bp
->b_lblkno
);
9530 if (bp
->b_flags
& B_READ
) {
9533 * Releases the osp, if it is provided.
9534 * Puts a hold on the cred_otw and the new osp (if found).
9536 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
9537 &first_time
, &last_time
);
9538 error
= bp
->b_error
= nfs4read(bp
->b_vp
, bp
->b_un
.b_addr
,
9539 offset
, bp
->b_bcount
, &bp
->b_resid
, cred_otw
,
9545 * Didn't get it all because we hit EOF,
9546 * zero all the memory beyond the EOF.
9548 /* bzero(rdaddr + */
9549 bzero(bp
->b_un
.b_addr
+
9550 bp
->b_bcount
- bp
->b_resid
, bp
->b_resid
);
9552 mutex_enter(&rp
->r_statelock
);
9553 if (bp
->b_resid
== bp
->b_bcount
&&
9554 offset
>= rp
->r_size
) {
9556 * We didn't read anything at all as we are
9557 * past EOF. Return an error indicator back
9558 * but don't destroy the pages (yet).
9562 mutex_exit(&rp
->r_statelock
);
9563 } else if (error
== EACCES
&& last_time
== FALSE
) {
9567 if (!(rp
->r_flags
& R4STALE
)) {
9570 * Releases the osp, if it is provided.
9571 * Puts a hold on the cred_otw and the new
9574 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
9575 &first_time
, &last_time
);
9576 mutex_enter(&rp
->r_statelock
);
9577 count
= MIN(bp
->b_bcount
, rp
->r_size
- offset
);
9578 mutex_exit(&rp
->r_statelock
);
9580 cmn_err(CE_PANIC
, "nfs4_bio: write count < 0");
9583 zoneid_t zoneid
= getzoneid();
9585 zcmn_err(zoneid
, CE_WARN
,
9586 "nfs4_bio: zero length write at %lld",
9588 zcmn_err(zoneid
, CE_CONT
, "flags=0x%x, "
9589 "b_bcount=%ld, file size=%lld",
9590 rp
->r_flags
, (long)bp
->b_bcount
,
9592 sfh4_printfhandle(VTOR4(bp
->b_vp
)->r_fh
);
9593 if (nfs4_bio_do_stop
)
9594 debug_enter("nfs4_bio");
9597 error
= nfs4write(bp
->b_vp
, bp
->b_un
.b_addr
, offset
,
9598 count
, cred_otw
, stab_comm
);
9599 if (error
== EACCES
&& last_time
== FALSE
) {
9603 bp
->b_error
= error
;
9604 if (error
&& error
!= EINTR
&&
9605 !(bp
->b_vp
->v_vfsp
->vfs_flag
& VFS_UNMOUNTED
)) {
9607 * Don't print EDQUOT errors on the console.
9608 * Don't print asynchronous EACCES errors.
9609 * Don't print EFBIG errors.
9610 * Print all other write errors.
9612 if (error
!= EDQUOT
&& error
!= EFBIG
&&
9614 !(bp
->b_flags
& B_ASYNC
)))
9615 nfs4_write_error(bp
->b_vp
,
9618 * Update r_error and r_flags as appropriate.
9619 * If the error was ESTALE, then mark the
9620 * rnode as not being writeable and save
9621 * the error status. Otherwise, save any
9622 * errors which occur from asynchronous
9623 * page invalidations. Any errors occurring
9624 * from other operations should be saved
9627 mutex_enter(&rp
->r_statelock
);
9628 if (error
== ESTALE
) {
9629 rp
->r_flags
|= R4STALE
;
9631 rp
->r_error
= error
;
9632 } else if (!rp
->r_error
&&
9634 (B_INVAL
|B_FORCE
|B_ASYNC
)) ==
9635 (B_INVAL
|B_FORCE
|B_ASYNC
)) {
9636 rp
->r_error
= error
;
9638 mutex_exit(&rp
->r_statelock
);
9642 error
= rp
->r_error
;
9644 * A close may have cleared r_error, if so,
9645 * propagate ESTALE error return properly
9652 if (error
!= 0 && error
!= NFS_EOF
)
9653 bp
->b_flags
|= B_ERROR
;
9656 open_stream_rele(osp
, rp
);
9658 DTRACE_IO1(done
, struct buf
*, bp
);
9665 nfs4_fid(vnode_t
*vp
, fid_t
*fidp
, caller_context_t
*ct
)
9672 nfs4_rwlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
9674 rnode4_t
*rp
= VTOR4(vp
);
9677 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
9678 return (V_WRITELOCK_FALSE
);
9681 if ((rp
->r_flags
& R4DIRECTIO
) ||
9682 (VTOMI4(vp
)->mi_flags
& MI4_DIRECTIO
)) {
9683 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_READER
, FALSE
);
9684 if (rp
->r_mapcnt
== 0 && !nfs4_has_pages(vp
))
9685 return (V_WRITELOCK_FALSE
);
9686 nfs_rw_exit(&rp
->r_rwlock
);
9689 (void) nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, FALSE
);
9690 return (V_WRITELOCK_TRUE
);
9695 nfs4_rwunlock(vnode_t
*vp
, int write_lock
, caller_context_t
*ctp
)
9697 rnode4_t
*rp
= VTOR4(vp
);
9699 nfs_rw_exit(&rp
->r_rwlock
);
9704 nfs4_seek(vnode_t
*vp
, offset_t ooff
, offset_t
*noffp
, caller_context_t
*ct
)
9706 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9710 * Because we stuff the readdir cookie into the offset field
9711 * someone may attempt to do an lseek with the cookie which
9712 * we want to succeed.
9714 if (vp
->v_type
== VDIR
)
9723 * Return all the pages from [off..off+len) in file
9727 nfs4_getpage(vnode_t
*vp
, offset_t off
, size_t len
, uint_t
*protp
,
9728 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
9729 enum seg_rw rw
, cred_t
*cr
, caller_context_t
*ct
)
9735 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9738 if (IS_SHADOW(vp
, rp
))
9741 if (vp
->v_flag
& VNOMAP
)
9748 * Now validate that the caches are up to date.
9750 if (error
= nfs4_validate_caches(vp
, cr
))
9755 mutex_enter(&rp
->r_statelock
);
9758 * Don't create dirty pages faster than they
9759 * can be cleaned so that the system doesn't
9760 * get imbalanced. If the async queue is
9761 * maxed out, then wait for it to drain before
9762 * creating more dirty pages. Also, wait for
9763 * any threads doing pagewalks in the vop_getattr
9764 * entry points so that they don't block for
9767 if (rw
== S_CREATE
) {
9768 while ((mi
->mi_max_threads
!= 0 &&
9769 rp
->r_awcount
> 2 * mi
->mi_max_threads
) ||
9771 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
9775 * If we are getting called as a side effect of an nfs_write()
9776 * operation the local file size might not be extended yet.
9777 * In this case we want to be able to return pages of zeroes.
9779 if (off
+ len
> rp
->r_size
+ PAGEOFFSET
&& seg
!= segkmap
) {
9780 NFS4_DEBUG(nfs4_pageio_debug
,
9781 (CE_NOTE
, "getpage beyond EOF: off=%lld, "
9782 "len=%llu, size=%llu, attrsize =%llu", off
,
9783 (u_longlong_t
)len
, rp
->r_size
, rp
->r_attr
.va_size
));
9784 mutex_exit(&rp
->r_statelock
);
9785 return (EFAULT
); /* beyond EOF */
9788 mutex_exit(&rp
->r_statelock
);
9790 error
= pvn_getpages(nfs4_getapage
, vp
, off
, len
, protp
,
9791 pl
, plsz
, seg
, addr
, rw
, cr
);
9792 NFS4_DEBUG(nfs4_pageio_debug
&& error
,
9793 (CE_NOTE
, "getpages error %d; off=%lld, len=%lld",
9794 error
, off
, (u_longlong_t
)len
));
9798 nfs4_purge_caches(vp
, NFS4_NOPURGE_DNLC
, cr
, FALSE
);
9801 nfs4_purge_stale_fh(error
, vp
, cr
);
9808 * Called from pvn_getpages to get a particular page.
9812 nfs4_getapage(vnode_t
*vp
, u_offset_t off
, size_t len
, uint_t
*protp
,
9813 page_t
*pl
[], size_t plsz
, struct seg
*seg
, caddr_t addr
,
9814 enum seg_rw rw
, cred_t
*cr
)
9823 u_offset_t rablkoff
;
9828 int readahead_issued
= 0;
9829 int ra_window
; /* readahead window */
9833 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
9837 ASSERT(!IS_SHADOW(vp
, rp
));
9838 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
9850 blkoff
= lbn
* bsize
;
9853 * Queueing up the readahead before doing the synchronous read
9854 * results in a significant increase in read throughput because
9855 * of the increased parallelism between the async threads and
9856 * the process context.
9858 if ((off
& ((vp
->v_vfsp
->vfs_bsize
) - 1)) == 0 &&
9860 !(vp
->v_flag
& VNOCACHE
)) {
9861 mutex_enter(&rp
->r_statelock
);
9864 * Calculate the number of readaheads to do.
9865 * a) No readaheads at offset = 0.
9866 * b) Do maximum(nfs4_nra) readaheads when the readahead
9868 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9869 * upon how far the readahead window is open or close.
9870 * d) No readaheads if rp->r_nextr is not within the scope
9871 * of the readahead window (random i/o).
9876 else if (blkoff
== rp
->r_nextr
)
9877 readahead
= nfs4_nra
;
9878 else if (rp
->r_nextr
> blkoff
&&
9879 ((ra_window
= (rp
->r_nextr
- blkoff
) / bsize
)
9881 readahead
= nfs4_nra
- ra_window
;
9885 rablkoff
= rp
->r_nextr
;
9886 while (readahead
> 0 && rablkoff
+ bsize
< rp
->r_size
) {
9887 mutex_exit(&rp
->r_statelock
);
9888 if (nfs4_async_readahead(vp
, rablkoff
+ bsize
,
9889 addr
+ (rablkoff
+ bsize
- off
),
9890 seg
, cr
, nfs4_readahead
) < 0) {
9891 mutex_enter(&rp
->r_statelock
);
9897 * Indicate that we did a readahead so
9898 * readahead offset is not updated
9899 * by the synchronous read below.
9901 readahead_issued
= 1;
9902 mutex_enter(&rp
->r_statelock
);
9904 * set readahead offset to
9905 * offset of last async readahead
9908 rp
->r_nextr
= rablkoff
;
9910 mutex_exit(&rp
->r_statelock
);
9914 if ((pagefound
= page_exists(vp
, off
)) == NULL
) {
9916 (void) nfs4_async_readahead(vp
, blkoff
, addr
, seg
, cr
,
9918 } else if (rw
== S_CREATE
) {
9920 * Block for this page is not allocated, or the offset
9921 * is beyond the current allocation size, or we're
9922 * allocating a swap slot and the page was not found,
9923 * so allocate it and return a zero page.
9925 if ((pp
= page_create_va(vp
, off
,
9926 PAGESIZE
, PG_WAIT
, seg
, addr
)) == NULL
)
9927 cmn_err(CE_PANIC
, "nfs4_getapage: page_create");
9929 mutex_enter(&rp
->r_statelock
);
9930 rp
->r_nextr
= off
+ PAGESIZE
;
9931 mutex_exit(&rp
->r_statelock
);
9934 * Need to go to server to get a block
9936 mutex_enter(&rp
->r_statelock
);
9937 if (blkoff
< rp
->r_size
&&
9938 blkoff
+ bsize
> rp
->r_size
) {
9940 * If less than a block left in
9941 * file read less than a block.
9943 if (rp
->r_size
<= off
) {
9945 * Trying to access beyond EOF,
9946 * set up to get at least one page.
9948 blksize
= off
+ PAGESIZE
- blkoff
;
9950 blksize
= rp
->r_size
- blkoff
;
9951 } else if ((off
== 0) ||
9952 (off
!= rp
->r_nextr
&& !readahead_issued
)) {
9954 blkoff
= off
; /* block = page here */
9957 mutex_exit(&rp
->r_statelock
);
9959 pp
= pvn_read_kluster(vp
, off
, seg
, addr
, &io_off
,
9960 &io_len
, blkoff
, blksize
, 0);
9963 * Some other thread has entered the page,
9970 * Now round the request size up to page boundaries.
9971 * This ensures that the entire page will be
9972 * initialized to zeroes if EOF is encountered.
9974 io_len
= ptob(btopr(io_len
));
9976 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
9980 * pageio_setup should have set b_addr to 0. This
9981 * is correct since we want to do I/O on a page
9982 * boundary. bp_mapin will use this addr to calculate
9983 * an offset, and then set b_addr to the kernel virtual
9984 * address it allocated for us.
9986 ASSERT(bp
->b_un
.b_addr
== 0);
9990 bp
->b_lblkno
= lbtodb(io_off
);
9992 bp
->b_offset
= (offset_t
)off
;
9996 * If doing a write beyond what we believe is EOF,
9997 * don't bother trying to read the pages from the
9998 * server, we'll just zero the pages here. We
9999 * don't check that the rw flag is S_WRITE here
10000 * because some implementations may attempt a
10001 * read access to the buffer before copying data.
10003 mutex_enter(&rp
->r_statelock
);
10004 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
10005 mutex_exit(&rp
->r_statelock
);
10006 bzero(bp
->b_un
.b_addr
, io_len
);
10008 mutex_exit(&rp
->r_statelock
);
10009 error
= nfs4_bio(bp
, NULL
, cr
, FALSE
);
10013 * Unmap the buffer before freeing it.
10020 pp
->p_fsdata
= C_NOCOMMIT
;
10021 } while ((pp
= pp
->p_next
) != savepp
);
10023 if (error
== NFS_EOF
) {
10025 * If doing a write system call just return
10026 * zeroed pages, else user tried to get pages
10027 * beyond EOF, return error. We don't check
10028 * that the rw flag is S_WRITE here because
10029 * some implementations may attempt a read
10030 * access to the buffer before copying data.
10032 if (seg
== segkmap
)
10038 if (!readahead_issued
&& !error
) {
10039 mutex_enter(&rp
->r_statelock
);
10040 rp
->r_nextr
= io_off
+ io_len
;
10041 mutex_exit(&rp
->r_statelock
);
10052 pvn_read_done(pp
, B_ERROR
);
10057 se_t se
= (rw
== S_CREATE
? SE_EXCL
: SE_SHARED
);
10060 * Page exists in the cache, acquire the appropriate lock.
10061 * If this fails, start all over again.
10063 if ((pp
= page_lookup(vp
, off
, se
)) == NULL
) {
10075 pvn_plist_init(pp
, pl
, plsz
, off
, io_len
, rw
);
10081 nfs4_readahead(vnode_t
*vp
, u_offset_t blkoff
, caddr_t addr
, struct seg
*seg
,
10089 uint_t bsize
, blksize
;
10090 rnode4_t
*rp
= VTOR4(vp
);
10093 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
10095 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
10097 mutex_enter(&rp
->r_statelock
);
10098 if (blkoff
< rp
->r_size
&& blkoff
+ bsize
> rp
->r_size
) {
10100 * If less than a block left in file read less
10103 blksize
= rp
->r_size
- blkoff
;
10106 mutex_exit(&rp
->r_statelock
);
10108 pp
= pvn_read_kluster(vp
, blkoff
, segkmap
, addr
,
10109 &io_off
, &io_len
, blkoff
, blksize
, 1);
10111 * The isra flag passed to the kluster function is 1, we may have
10112 * gotten a return value of NULL for a variety of reasons (# of free
10113 * pages < minfree, someone entered the page on the vnode etc). In all
10114 * cases, we want to punt on the readahead.
10120 * Now round the request size up to page boundaries.
10121 * This ensures that the entire page will be
10122 * initialized to zeroes if EOF is encountered.
10124 io_len
= ptob(btopr(io_len
));
10126 bp
= pageio_setup(pp
, io_len
, vp
, B_READ
);
10127 ASSERT(bp
!= NULL
);
10130 * pageio_setup should have set b_addr to 0. This is correct since
10131 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10132 * to calculate an offset, and then set b_addr to the kernel virtual
10133 * address it allocated for us.
10135 ASSERT(bp
->b_un
.b_addr
== 0);
10139 bp
->b_lblkno
= lbtodb(io_off
);
10141 bp
->b_offset
= (offset_t
)blkoff
;
10145 * If doing a write beyond what we believe is EOF, don't bother trying
10146 * to read the pages from the server, we'll just zero the pages here.
10147 * We don't check that the rw flag is S_WRITE here because some
10148 * implementations may attempt a read access to the buffer before
10151 mutex_enter(&rp
->r_statelock
);
10152 if (io_off
>= rp
->r_size
&& seg
== segkmap
) {
10153 mutex_exit(&rp
->r_statelock
);
10154 bzero(bp
->b_un
.b_addr
, io_len
);
10157 mutex_exit(&rp
->r_statelock
);
10158 error
= nfs4_bio(bp
, NULL
, cr
, TRUE
);
10159 if (error
== NFS_EOF
)
10164 * Unmap the buffer before freeing it.
10171 pp
->p_fsdata
= C_NOCOMMIT
;
10172 } while ((pp
= pp
->p_next
) != savepp
);
10174 pvn_read_done(pp
, error
? B_READ
| B_ERROR
: B_READ
);
10177 * In case of error set readahead offset
10178 * to the lowest offset.
10179 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10181 if (error
&& rp
->r_nextr
> io_off
) {
10182 mutex_enter(&rp
->r_statelock
);
10183 if (rp
->r_nextr
> io_off
)
10184 rp
->r_nextr
= io_off
;
10185 mutex_exit(&rp
->r_statelock
);
10190 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10191 * If len == 0, do from off to EOF.
10193 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10194 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10199 nfs4_putpage(vnode_t
*vp
, offset_t off
, size_t len
, int flags
, cred_t
*cr
,
10200 caller_context_t
*ct
)
10205 ASSERT(cr
!= NULL
);
10207 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI4(vp
)->mi_zone
)
10211 if (IS_SHADOW(vp
, rp
))
10215 * XXX - Why should this check be made here?
10217 if (vp
->v_flag
& VNOMAP
)
10220 if (len
== 0 && !(flags
& B_INVAL
) &&
10221 (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
))
10224 mutex_enter(&rp
->r_statelock
);
10226 mutex_exit(&rp
->r_statelock
);
10227 error
= nfs4_putpages(vp
, off
, len
, flags
, cr
);
10228 mutex_enter(&rp
->r_statelock
);
10230 cv_broadcast(&rp
->r_cv
);
10231 mutex_exit(&rp
->r_statelock
);
10237 * Write out a single page, possibly klustering adjacent dirty pages.
10240 nfs4_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t
*offp
, size_t *lenp
,
10241 int flags
, cred_t
*cr
)
10244 u_offset_t lbn_off
;
10251 ASSERT(!(vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
));
10252 ASSERT(pp
!= NULL
);
10253 ASSERT(cr
!= NULL
);
10254 ASSERT((flags
& B_ASYNC
) || nfs_zone() == VTOMI4(vp
)->mi_zone
);
10257 ASSERT(rp
->r_count
> 0);
10258 ASSERT(!IS_SHADOW(vp
, rp
));
10260 bsize
= MAX(vp
->v_vfsp
->vfs_bsize
, PAGESIZE
);
10261 lbn
= pp
->p_offset
/ bsize
;
10262 lbn_off
= lbn
* bsize
;
10265 * Find a kluster that fits in one block, or in
10266 * one page if pages are bigger than blocks. If
10267 * there is less file space allocated than a whole
10268 * page, we'll shorten the i/o request below.
10270 pp
= pvn_write_kluster(vp
, pp
, &io_off
, &io_len
, lbn_off
,
10271 roundup(bsize
, PAGESIZE
), flags
);
10274 * pvn_write_kluster shouldn't have returned a page with offset
10275 * behind the original page we were given. Verify that.
10277 ASSERT((pp
->p_offset
/ bsize
) >= lbn
);
10280 * Now pp will have the list of kept dirty pages marked for
10281 * write back. It will also handle invalidation and freeing
10282 * of pages that are not dirty. Check for page length rounding
10285 if (io_off
+ io_len
> lbn_off
+ bsize
) {
10286 ASSERT((io_off
+ io_len
) - (lbn_off
+ bsize
) < PAGESIZE
);
10287 io_len
= lbn_off
+ bsize
- io_off
;
10290 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10291 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10292 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10293 * progress and the r_size has not been made consistent with the
10294 * new size of the file. When the uiomove() completes the r_size is
10295 * updated and the R4MODINPROGRESS flag is cleared.
10297 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10298 * consistent value of r_size. Without this handshaking, it is
10299 * possible that nfs4_bio() picks up the old value of r_size
10300 * before the uiomove() in writerp4() completes. This will result
10301 * in the write through nfs4_bio() being dropped.
10303 * More precisely, there is a window between the time the uiomove()
10304 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10305 * operation intervenes in this window, the page will be picked up,
10306 * because it is dirty (it will be unlocked, unless it was
10307 * pagecreate'd). When the page is picked up as dirty, the dirty
10308 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10309 * checked. This will still be the old size. Therefore the page will
10310 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10311 * the page will be found to be clean and the write will be dropped.
10313 if (rp
->r_flags
& R4MODINPROGRESS
) {
10314 mutex_enter(&rp
->r_statelock
);
10315 if ((rp
->r_flags
& R4MODINPROGRESS
) &&
10316 rp
->r_modaddr
+ MAXBSIZE
> io_off
&&
10317 rp
->r_modaddr
< io_off
+ io_len
) {
10320 * A write is in progress for this region of the file.
10321 * If we did not detect R4MODINPROGRESS here then this
10322 * path through nfs_putapage() would eventually go to
10323 * nfs4_bio() and may not write out all of the data
10324 * in the pages. We end up losing data. So we decide
10325 * to set the modified bit on each page in the page
10326 * list and mark the rnode with R4DIRTY. This write
10327 * will be restarted at some later time.
10330 while (plist
!= NULL
) {
10332 page_sub(&plist
, pp
);
10334 page_io_unlock(pp
);
10337 rp
->r_flags
|= R4DIRTY
;
10338 mutex_exit(&rp
->r_statelock
);
10345 mutex_exit(&rp
->r_statelock
);
10348 if (flags
& B_ASYNC
) {
10349 error
= nfs4_async_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
,
10350 nfs4_sync_putapage
);
10352 error
= nfs4_sync_putapage(vp
, pp
, io_off
, io_len
, flags
, cr
);
10362 nfs4_sync_putapage(vnode_t
*vp
, page_t
*pp
, u_offset_t io_off
, size_t io_len
,
10363 int flags
, cred_t
*cr
)
10368 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
10372 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
10376 if ((error
== ENOSPC
|| error
== EDQUOT
|| error
== EFBIG
||
10377 error
== EACCES
) &&
10378 (flags
& (B_INVAL
|B_FORCE
)) != (B_INVAL
|B_FORCE
)) {
10379 if (!(rp
->r_flags
& R4OUTOFSPACE
)) {
10380 mutex_enter(&rp
->r_statelock
);
10381 rp
->r_flags
|= R4OUTOFSPACE
;
10382 mutex_exit(&rp
->r_statelock
);
10385 pvn_write_done(pp
, flags
);
10387 * If this was not an async thread, then try again to
10388 * write out the pages, but this time, also destroy
10389 * them whether or not the write is successful. This
10390 * will prevent memory from filling up with these
10391 * pages and destroying them is the only alternative
10392 * if they can't be written out.
10394 * Don't do this if this is an async thread because
10395 * when the pages are unlocked in pvn_write_done,
10396 * some other thread could have come along, locked
10397 * them, and queued for an async thread. It would be
10398 * possible for all of the async threads to be tied
10399 * up waiting to lock the pages again and they would
10400 * all already be locked and waiting for an async
10401 * thread to handle them. Deadlock.
10403 if (!(flags
& B_ASYNC
)) {
10404 error
= nfs4_putpage(vp
, io_off
, io_len
,
10405 B_INVAL
| B_FORCE
, cr
, NULL
);
10410 else if (rp
->r_flags
& R4OUTOFSPACE
) {
10411 mutex_enter(&rp
->r_statelock
);
10412 rp
->r_flags
&= ~R4OUTOFSPACE
;
10413 mutex_exit(&rp
->r_statelock
);
10415 pvn_write_done(pp
, flags
);
10416 if (freemem
< desfree
)
10417 (void) nfs4_commit_vp(vp
, (u_offset_t
)0, 0, cr
,
10418 NFS4_WRITE_NOWAIT
);
10425 int nfs4_force_open_before_mmap
= 0;
10430 nfs4_map(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t
*addrp
,
10431 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
10432 caller_context_t
*ct
)
10434 struct segvn_crargs vn_a
;
10436 rnode4_t
*rp
= VTOR4(vp
);
10437 mntinfo4_t
*mi
= VTOMI4(vp
);
10439 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10442 if (vp
->v_flag
& VNOMAP
)
10445 if (off
< 0 || (off
+ len
) < 0)
10448 if (vp
->v_type
!= VREG
)
10452 * If the file is delegated to the client don't do anything.
10453 * If the file is not delegated, then validate the data cache.
10455 mutex_enter(&rp
->r_statev4_lock
);
10456 if (rp
->r_deleg_type
== OPEN_DELEGATE_NONE
) {
10457 mutex_exit(&rp
->r_statev4_lock
);
10458 error
= nfs4_validate_caches(vp
, cr
);
10462 mutex_exit(&rp
->r_statev4_lock
);
10466 * Check to see if the vnode is currently marked as not cachable.
10467 * This means portions of the file are locked (through VOP_FRLOCK).
10468 * In this case the map request must be refused. We use
10469 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10471 * Atomically increment r_inmap after acquiring r_rwlock. The
10472 * idea here is to acquire r_rwlock to block read/write and
10473 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10474 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10475 * and we can prevent the deadlock that would have occurred
10476 * when nfs4_addmap() would have acquired it out of order.
10478 * Since we are not protecting r_inmap by any lock, we do not
10479 * hold any lock when we decrement it. We atomically decrement
10480 * r_inmap after we release r_lkserlock.
10483 if (nfs_rw_enter_sig(&rp
->r_rwlock
, RW_WRITER
, INTR4(vp
)))
10485 atomic_inc_uint(&rp
->r_inmap
);
10486 nfs_rw_exit(&rp
->r_rwlock
);
10488 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_READER
, INTR4(vp
))) {
10489 atomic_dec_uint(&rp
->r_inmap
);
10494 if (vp
->v_flag
& VNOCACHE
) {
10500 * Don't allow concurrent locks and mapping if mandatory locking is
10503 if (flk_has_remote_locks(vp
)) {
10505 va
.va_mask
= AT_MODE
;
10506 error
= nfs4getattr(vp
, &va
, cr
);
10509 if (MANDLOCK(vp
, va
.va_mode
)) {
10516 * It is possible that the rnode has a lost lock request that we
10517 * are still trying to recover, and that the request conflicts with
10518 * this map request.
10520 * An alternative approach would be for nfs4_safemap() to consider
10521 * queued lock requests when deciding whether to set or clear
10522 * VNOCACHE. This would require the frlock code path to call
10523 * nfs4_safemap() after enqueing a lost request.
10525 if (nfs4_map_lost_lock_conflict(vp
)) {
10531 error
= choose_addr(as
, addrp
, len
, off
, ADDR_VACALIGN
, flags
);
10533 as_rangeunlock(as
);
10537 if (vp
->v_type
== VREG
) {
10539 * We need to retrieve the open stream
10541 nfs4_open_stream_t
*osp
= NULL
;
10542 nfs4_open_owner_t
*oop
= NULL
;
10544 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
10546 /* returns with 'os_sync_lock' held */
10547 osp
= find_open_stream(oop
, rp
);
10548 open_owner_rele(oop
);
10552 if (nfs4_force_open_before_mmap
) {
10557 /* returns with 'os_sync_lock' held */
10558 error
= open_and_get_osp(vp
, cr
, &osp
);
10560 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
,
10561 "nfs4_map: we tried to OPEN the file "
10562 "but again no osp, so fail with EIO"));
10567 if (osp
->os_failed_reopen
) {
10568 mutex_exit(&osp
->os_sync_lock
);
10569 open_stream_rele(osp
, rp
);
10570 NFS4_DEBUG(nfs4_open_stream_debug
, (CE_NOTE
,
10571 "nfs4_map: os_failed_reopen set on "
10572 "osp %p, cr %p, rp %s", (void *)osp
,
10573 (void *)cr
, rnode4info(rp
)));
10577 mutex_exit(&osp
->os_sync_lock
);
10578 open_stream_rele(osp
, rp
);
10583 vn_a
.type
= (flags
& MAP_TYPE
);
10584 vn_a
.prot
= (uchar_t
)prot
;
10585 vn_a
.maxprot
= (uchar_t
)maxprot
;
10586 vn_a
.flags
= (flags
& ~MAP_TYPE
);
10590 vn_a
.lgrp_mem_policy_flags
= 0;
10592 error
= as_map(as
, *addrp
, len
, segvn_create
, &vn_a
);
10593 as_rangeunlock(as
);
10596 nfs_rw_exit(&rp
->r_lkserlock
);
10597 atomic_dec_uint(&rp
->r_inmap
);
10602 * We're most likely dealing with a kernel module that likes to READ
10603 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10604 * officially OPEN the file to create the necessary client state
10605 * for bookkeeping of os_mmap_read/write counts.
10607 * Since VOP_MAP only passes in a pointer to the vnode rather than
10608 * a double pointer, we can't handle the case where nfs4open_otw()
10609 * returns a different vnode than the one passed into VOP_MAP (since
10610 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case,
10611 * we return NULL and let nfs4_map() fail. Note: the only case where
10612 * this should happen is if the file got removed and replaced with the
10613 * same name on the server (in addition to the fact that we're trying
10614 * to VOP_MAP withouth VOP_OPENing the file in the first place).
10617 open_and_get_osp(vnode_t
*map_vp
, cred_t
*cr
, nfs4_open_stream_t
**ospp
)
10619 rnode4_t
*rp
, *drp
;
10620 vnode_t
*dvp
, *open_vp
;
10621 char file_name
[MAXNAMELEN
];
10623 nfs4_open_stream_t
*osp
;
10624 nfs4_open_owner_t
*oop
;
10630 rp
= VTOR4(open_vp
);
10631 if ((error
= vtodv(open_vp
, &dvp
, cr
, TRUE
)) != 0)
10635 if (nfs_rw_enter_sig(&drp
->r_rwlock
, RW_READER
, INTR4(dvp
))) {
10640 if ((error
= vtoname(open_vp
, file_name
, MAXNAMELEN
)) != 0) {
10641 nfs_rw_exit(&drp
->r_rwlock
);
10646 mutex_enter(&rp
->r_statev4_lock
);
10647 if (rp
->created_v4
) {
10648 rp
->created_v4
= 0;
10649 mutex_exit(&rp
->r_statev4_lock
);
10651 dnlc_update(dvp
, file_name
, open_vp
);
10652 /* This is needed so we don't bump the open ref count */
10655 mutex_exit(&rp
->r_statev4_lock
);
10661 error
= nfs4open_otw(dvp
, file_name
, NULL
, &open_vp
, cr
, 0, FREAD
, 0,
10664 nfs_rw_exit(&drp
->r_rwlock
);
10670 nfs_rw_exit(&drp
->r_rwlock
);
10674 * If nfs4open_otw() returned a different vnode then "undo"
10675 * the open and return failure to the caller.
10677 if (!VN_CMP(open_vp
, map_vp
)) {
10680 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "open_and_get_osp: "
10681 "open returned a different vnode"));
10683 * If there's an error, ignore it,
10684 * and let VOP_INACTIVE handle it.
10686 (void) nfs4close_one(open_vp
, NULL
, cr
, FREAD
, NULL
, &e
,
10687 CLOSE_NORM
, 0, 0, 0);
10694 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, VTOMI4(open_vp
));
10698 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "open_and_get_osp: "
10701 * If there's an error, ignore it,
10702 * and let VOP_INACTIVE handle it.
10704 (void) nfs4close_one(open_vp
, NULL
, cr
, FREAD
, NULL
, &e
,
10705 CLOSE_NORM
, 0, 0, 0);
10708 osp
= find_open_stream(oop
, rp
);
10709 open_owner_rele(oop
);
10715 * Please be aware that when this function is called, the address space write
10716 * a_lock is held. Do not put over the wire calls in this function.
10720 nfs4_addmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
10721 size_t len
, uchar_t prot
, uchar_t maxprot
, uint_t flags
, cred_t
*cr
,
10722 caller_context_t
*ct
)
10731 if (nfs_zone() != mi
->mi_zone
)
10733 if (vp
->v_flag
& VNOMAP
)
10737 * Don't need to update the open stream first, since this
10738 * mmap can't add any additional share access that isn't
10739 * already contained in the open stream (for the case where we
10740 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10741 * take into account os_mmap_read[write] counts).
10743 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, btopr(len
));
10745 if (vp
->v_type
== VREG
) {
10747 * We need to retrieve the open stream and update the counts.
10748 * If there is no open stream here, something is wrong.
10750 nfs4_open_stream_t
*osp
= NULL
;
10751 nfs4_open_owner_t
*oop
= NULL
;
10753 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
10755 /* returns with 'os_sync_lock' held */
10756 osp
= find_open_stream(oop
, rp
);
10757 open_owner_rele(oop
);
10760 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
,
10761 "nfs4_addmap: we should have an osp"
10762 "but we don't, so fail with EIO"));
10767 NFS4_DEBUG(nfs4_mmap_debug
, (CE_NOTE
, "nfs4_addmap: osp %p,"
10768 " pages %ld, prot 0x%x", (void *)osp
, btopr(len
), prot
));
10771 * Update the map count in the open stream.
10772 * This is necessary in the case where we
10773 * open/mmap/close/, then the server reboots, and we
10774 * attempt to reopen. If the mmap doesn't add share
10775 * access then we send an invalid reopen with
10778 * We need to specifically check each PROT_* so a mmap
10779 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10780 * read and write access. A simple comparison of prot
10781 * to ~PROT_WRITE to determine read access is insufficient
10782 * since prot can be |= with PROT_USER, etc.
10786 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10788 if ((flags
& MAP_SHARED
) && (maxprot
& PROT_WRITE
))
10789 osp
->os_mmap_write
+= btopr(len
);
10790 if (maxprot
& PROT_READ
)
10791 osp
->os_mmap_read
+= btopr(len
);
10792 if (maxprot
& PROT_EXEC
)
10793 osp
->os_mmap_read
+= btopr(len
);
10795 * Ensure that os_mmap_read gets incremented, even if
10796 * maxprot were to look like PROT_NONE.
10798 if (!(maxprot
& PROT_READ
) && !(maxprot
& PROT_WRITE
) &&
10799 !(maxprot
& PROT_EXEC
))
10800 osp
->os_mmap_read
+= btopr(len
);
10801 osp
->os_mapcnt
+= btopr(len
);
10802 mutex_exit(&osp
->os_sync_lock
);
10803 open_stream_rele(osp
, rp
);
10808 * If we got an error, then undo our
10809 * incrementing of 'r_mapcnt'.
10813 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, -btopr(len
));
10814 ASSERT(rp
->r_mapcnt
>= 0);
10821 nfs4_cmp(vnode_t
*vp1
, vnode_t
*vp2
, caller_context_t
*ct
)
10824 return (VTOR4(vp1
) == VTOR4(vp2
));
10829 nfs4_frlock(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
10830 offset_t offset
, struct flk_callback
*flk_cbp
, cred_t
*cr
,
10831 caller_context_t
*ct
)
10834 u_offset_t start
, end
;
10836 int error
= 0, intr
= INTR4(vp
);
10839 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10842 /* check for valid cmd parameter */
10843 if (cmd
!= F_GETLK
&& cmd
!= F_SETLK
&& cmd
!= F_SETLKW
)
10846 /* Verify l_type. */
10847 switch (bfp
->l_type
) {
10849 if (cmd
!= F_GETLK
&& !(flag
& FREAD
))
10853 if (cmd
!= F_GETLK
&& !(flag
& FWRITE
))
10864 /* check the validity of the lock range */
10865 if (rc
= flk_convert_lock_data(vp
, bfp
, &start
, &end
, offset
))
10867 if (rc
= flk_check_lock_data(start
, end
, MAXEND
))
10871 * If the filesystem is mounted using local locking, pass the
10872 * request off to the local locking code.
10874 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
|| vp
->v_type
!= VREG
) {
10875 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
10877 * For complete safety, we should be holding
10878 * r_lkserlock. However, we can't call
10879 * nfs4_safelock and then fs_frlock while
10880 * holding r_lkserlock, so just invoke
10881 * nfs4_safelock and expect that this will
10882 * catch enough of the cases.
10884 if (!nfs4_safelock(vp
, bfp
, cr
))
10887 return (fs_frlock(vp
, cmd
, bfp
, flag
, offset
, flk_cbp
, cr
, ct
));
10893 * Check whether the given lock request can proceed, given the
10894 * current file mappings.
10896 if (nfs_rw_enter_sig(&rp
->r_lkserlock
, RW_WRITER
, intr
))
10898 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
10899 if (!nfs4_safelock(vp
, bfp
, cr
)) {
10906 * Flush the cache after waiting for async I/O to finish. For new
10907 * locks, this is so that the process gets the latest bits from the
10908 * server. For unlocks, this is so that other clients see the
10909 * latest bits once the file has been unlocked. If currently dirty
10910 * pages can't be flushed, then don't allow a lock to be set. But
10911 * allow unlocks to succeed, to avoid having orphan locks on the
10914 if (cmd
!= F_GETLK
) {
10915 mutex_enter(&rp
->r_statelock
);
10916 while (rp
->r_count
> 0) {
10918 klwp_t
*lwp
= ttolwp(curthread
);
10922 if (cv_wait_sig(&rp
->r_cv
,
10923 &rp
->r_statelock
) == 0) {
10932 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
10934 mutex_exit(&rp
->r_statelock
);
10937 error
= nfs4_putpage(vp
, (offset_t
)0, 0, B_INVAL
, cr
, ct
);
10939 if (error
== ENOSPC
|| error
== EDQUOT
) {
10940 mutex_enter(&rp
->r_statelock
);
10942 rp
->r_error
= error
;
10943 mutex_exit(&rp
->r_statelock
);
10945 if (bfp
->l_type
!= F_UNLCK
) {
10953 * Call the lock manager to do the real work of contacting
10954 * the server and obtaining the lock.
10956 nfs4frlock(NFS4_LCK_CTYPE_NORM
, vp
, cmd
, bfp
, flag
, offset
,
10957 cr
, &e
, NULL
, NULL
);
10961 nfs4_lockcompletion(vp
, cmd
);
10964 nfs_rw_exit(&rp
->r_lkserlock
);
10970 * Free storage space associated with the specified vnode. The portion
10971 * to be freed is specified by bfp->l_start and bfp->l_len (already
10972 * normalized to a "whence" of 0).
10974 * This is an experimental facility whose continued existence is not
10975 * guaranteed. Currently, we only support the special case
10976 * of l_len == 0, meaning free to end of file.
10980 nfs4_space(vnode_t
*vp
, int cmd
, struct flock64
*bfp
, int flag
,
10981 offset_t offset
, cred_t
*cr
, caller_context_t
*ct
)
10985 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
10987 ASSERT(vp
->v_type
== VREG
);
10988 if (cmd
!= F_FREESP
)
10991 error
= convoff(vp
, bfp
, 0, offset
);
10993 ASSERT(bfp
->l_start
>= 0);
10994 if (bfp
->l_len
== 0) {
10997 va
.va_mask
= AT_SIZE
;
10998 va
.va_size
= bfp
->l_start
;
10999 error
= nfs4setattr(vp
, &va
, 0, cr
, NULL
);
11001 if (error
== 0 && bfp
->l_start
== 0)
11002 vnevent_truncate(vp
, ct
);
11012 nfs4_realvp(vnode_t
*vp
, vnode_t
**vpp
, caller_context_t
*ct
)
11017 if (vp
->v_type
== VREG
&& IS_SHADOW(vp
, rp
)) {
11025 * Setup and add an address space callback to do the work of the delmap call.
11026 * The callback will (and must be) deleted in the actual callback function.
11028 * This is done in order to take care of the problem that we have with holding
11029 * the address space's a_lock for a long period of time (e.g. if the NFS server
11030 * is down). Callbacks will be executed in the address space code while the
11031 * a_lock is not held. Holding the address space's a_lock causes things such
11032 * as ps and fork to hang because they are trying to acquire this lock as well.
11036 nfs4_delmap(vnode_t
*vp
, offset_t off
, struct as
*as
, caddr_t addr
,
11037 size_t len
, uint_t prot
, uint_t maxprot
, uint_t flags
, cred_t
*cr
,
11038 caller_context_t
*ct
)
11043 nfs4_delmap_args_t
*dmapp
;
11044 nfs4_delmapcall_t
*delmap_call
;
11046 if (vp
->v_flag
& VNOMAP
)
11050 * A process may not change zones if it has NFS pages mmap'ed
11051 * in, so we can't legitimately get here from the wrong zone.
11053 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11058 * The way that the address space of this process deletes its mapping
11059 * of this file is via the following call chains:
11060 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11061 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11063 * With the use of address space callbacks we are allowed to drop the
11064 * address space lock, a_lock, while executing the NFS operations that
11065 * need to go over the wire. Returning EAGAIN to the caller of this
11066 * function is what drives the execution of the callback that we add
11067 * below. The callback will be executed by the address space code
11068 * after dropping the a_lock. When the callback is finished, since
11069 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11070 * is called again on the same segment to finish the rest of the work
11071 * that needs to happen during unmapping.
11073 * This action of calling back into the segment driver causes
11074 * nfs4_delmap() to get called again, but since the callback was
11075 * already executed at this point, it already did the work and there
11076 * is nothing left for us to do.
11079 * - The first time nfs4_delmap is called by the current thread is when
11080 * we add the caller associated with this delmap to the delmap caller
11081 * list, add the callback, and return EAGAIN.
11082 * - The second time in this call chain when nfs4_delmap is called we
11083 * will find this caller in the delmap caller list and realize there
11084 * is no more work to do thus removing this caller from the list and
11085 * returning the error that was set in the callback execution.
11087 caller_found
= nfs4_find_and_delete_delmapcall(rp
, &error
);
11088 if (caller_found
) {
11090 * 'error' is from the actual delmap operations. To avoid
11091 * hangs, we need to handle the return of EAGAIN differently
11092 * since this is what drives the callback execution.
11093 * In this case, we don't want to return EAGAIN and do the
11094 * callback execution because there are none to execute.
11096 if (error
== EAGAIN
)
11102 /* current caller was not in the list */
11103 delmap_call
= nfs4_init_delmapcall();
11105 mutex_enter(&rp
->r_statelock
);
11106 list_insert_tail(&rp
->r_indelmap
, delmap_call
);
11107 mutex_exit(&rp
->r_statelock
);
11109 dmapp
= kmem_alloc(sizeof (nfs4_delmap_args_t
), KM_SLEEP
);
11113 dmapp
->addr
= addr
;
11115 dmapp
->prot
= prot
;
11116 dmapp
->maxprot
= maxprot
;
11117 dmapp
->flags
= flags
;
11119 dmapp
->caller
= delmap_call
;
11121 error
= as_add_callback(as
, nfs4_delmap_callback
, dmapp
,
11122 AS_UNMAP_EVENT
, addr
, len
, KM_SLEEP
);
11124 return (error
? error
: EAGAIN
);
11127 static nfs4_delmapcall_t
*
11128 nfs4_init_delmapcall()
11130 nfs4_delmapcall_t
*delmap_call
;
11132 delmap_call
= kmem_alloc(sizeof (nfs4_delmapcall_t
), KM_SLEEP
);
11133 delmap_call
->call_id
= curthread
;
11134 delmap_call
->error
= 0;
11136 return (delmap_call
);
11140 nfs4_free_delmapcall(nfs4_delmapcall_t
*delmap_call
)
11142 kmem_free(delmap_call
, sizeof (nfs4_delmapcall_t
));
11146 * Searches for the current delmap caller (based on curthread) in the list of
11147 * callers. If it is found, we remove it and free the delmap caller.
11149 * 0 if the caller wasn't found
11150 * 1 if the caller was found, removed and freed. *errp will be set
11151 * to what the result of the delmap was.
11154 nfs4_find_and_delete_delmapcall(rnode4_t
*rp
, int *errp
)
11156 nfs4_delmapcall_t
*delmap_call
;
11159 * If the list doesn't exist yet, we create it and return
11160 * that the caller wasn't found. No list = no callers.
11162 mutex_enter(&rp
->r_statelock
);
11163 if (!(rp
->r_flags
& R4DELMAPLIST
)) {
11164 /* The list does not exist */
11165 list_create(&rp
->r_indelmap
, sizeof (nfs4_delmapcall_t
),
11166 offsetof(nfs4_delmapcall_t
, call_node
));
11167 rp
->r_flags
|= R4DELMAPLIST
;
11168 mutex_exit(&rp
->r_statelock
);
11171 /* The list exists so search it */
11172 for (delmap_call
= list_head(&rp
->r_indelmap
);
11173 delmap_call
!= NULL
;
11174 delmap_call
= list_next(&rp
->r_indelmap
, delmap_call
)) {
11175 if (delmap_call
->call_id
== curthread
) {
11176 /* current caller is in the list */
11177 *errp
= delmap_call
->error
;
11178 list_remove(&rp
->r_indelmap
, delmap_call
);
11179 mutex_exit(&rp
->r_statelock
);
11180 nfs4_free_delmapcall(delmap_call
);
11185 mutex_exit(&rp
->r_statelock
);
11190 * Remove some pages from an mmap'd vnode. Just update the
11191 * count of pages. If doing close-to-open, then flush and
11192 * commit all of the pages associated with this file.
11193 * Otherwise, start an asynchronous page flush to write out
11194 * any dirty pages. This will also associate a credential
11195 * with the rnode which can be used to write the pages.
11199 nfs4_delmap_callback(struct as
*as
, void *arg
, uint_t event
)
11201 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
11204 nfs4_delmap_args_t
*dmapp
= (nfs4_delmap_args_t
*)arg
;
11206 rp
= VTOR4(dmapp
->vp
);
11207 mi
= VTOMI4(dmapp
->vp
);
11209 atomic_add_long((ulong_t
*)&rp
->r_mapcnt
, -btopr(dmapp
->len
));
11210 ASSERT(rp
->r_mapcnt
>= 0);
11213 * Initiate a page flush and potential commit if there are
11214 * pages, the file system was not mounted readonly, the segment
11215 * was mapped shared, and the pages themselves were writeable.
11217 if (nfs4_has_pages(dmapp
->vp
) &&
11218 !(dmapp
->vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
) &&
11219 dmapp
->flags
== MAP_SHARED
&& (dmapp
->maxprot
& PROT_WRITE
)) {
11220 mutex_enter(&rp
->r_statelock
);
11221 rp
->r_flags
|= R4DIRTY
;
11222 mutex_exit(&rp
->r_statelock
);
11223 e
.error
= nfs4_putpage_commit(dmapp
->vp
, dmapp
->off
,
11224 dmapp
->len
, dmapp
->cr
);
11226 mutex_enter(&rp
->r_statelock
);
11227 e
.error
= rp
->r_error
;
11229 mutex_exit(&rp
->r_statelock
);
11234 if ((rp
->r_flags
& R4DIRECTIO
) || (mi
->mi_flags
& MI4_DIRECTIO
))
11235 (void) nfs4_putpage(dmapp
->vp
, dmapp
->off
, dmapp
->len
,
11236 B_INVAL
, dmapp
->cr
, NULL
);
11239 e
.stat
= puterrno4(e
.error
);
11240 nfs4_queue_fact(RF_DELMAP_CB_ERR
, mi
, e
.stat
, 0,
11241 OP_COMMIT
, FALSE
, NULL
, 0, dmapp
->vp
);
11242 dmapp
->caller
->error
= e
.error
;
11245 /* Check to see if we need to close the file */
11247 if (dmapp
->vp
->v_type
== VREG
) {
11248 nfs4close_one(dmapp
->vp
, NULL
, dmapp
->cr
, 0, NULL
, &e
,
11249 CLOSE_DELMAP
, dmapp
->len
, dmapp
->maxprot
, dmapp
->flags
);
11251 if (e
.error
!= 0 || e
.stat
!= NFS4_OK
) {
11253 * Since it is possible that e.error == 0 and
11254 * e.stat != NFS4_OK (and vice versa),
11255 * we do the proper checking in order to get both
11256 * e.error and e.stat reporting the correct info.
11258 if (e
.stat
== NFS4_OK
)
11259 e
.stat
= puterrno4(e
.error
);
11261 e
.error
= geterrno4(e
.stat
);
11263 nfs4_queue_fact(RF_DELMAP_CB_ERR
, mi
, e
.stat
, 0,
11264 OP_CLOSE
, FALSE
, NULL
, 0, dmapp
->vp
);
11265 dmapp
->caller
->error
= e
.error
;
11269 (void) as_delete_callback(as
, arg
);
11270 kmem_free(dmapp
, sizeof (nfs4_delmap_args_t
));
11275 fattr4_maxfilesize_to_bits(uint64_t ll
)
11283 if (ll
& 0xffffffff00000000) {
11284 l
+= 32; ll
>>= 32;
11286 if (ll
& 0xffff0000) {
11287 l
+= 16; ll
>>= 16;
11305 nfs4_have_xattrs(vnode_t
*vp
, ulong_t
*valp
, cred_t
*cr
)
11307 vnode_t
*avp
= NULL
;
11310 if ((error
= nfs4lookup_xattr(vp
, "", &avp
,
11311 LOOKUP_XATTR
, cr
)) == 0)
11312 error
= do_xattr_exists_check(avp
, valp
, cr
);
11321 nfs4_pathconf(vnode_t
*vp
, int cmd
, ulong_t
*valp
, cred_t
*cr
,
11322 caller_context_t
*ct
)
11328 nfs4_ga_ext_res_t ger
;
11330 gar
.n4g_ext_res
= &ger
;
11332 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
11334 if (cmd
== _PC_PATH_MAX
|| cmd
== _PC_SYMLINK_MAX
) {
11335 *valp
= MAXPATHLEN
;
11338 if (cmd
== _PC_ACL_ENABLED
) {
11339 *valp
= _ACL_ACE_ENABLED
;
11344 if (cmd
== _PC_XATTR_EXISTS
) {
11346 * The existence of the xattr directory is not sufficient
11347 * for determining whether generic user attributes exists.
11348 * The attribute directory could only be a transient directory
11349 * used for Solaris sysattr support. Do a small readdir
11350 * to verify if the only entries are sysattrs or not.
11352 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11353 * is NULL. Once the xadir vp exists, we can create xattrs,
11354 * and we don't have any way to update the "base" object's
11355 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11358 if (ATTRCACHE4_VALID(vp
) && rp
->r_pathconf
.pc4_xattr_valid
&&
11359 rp
->r_xattr_dir
== NULL
) {
11360 return (nfs4_have_xattrs(vp
, valp
, cr
));
11362 } else { /* OLD CODE */
11363 if (ATTRCACHE4_VALID(vp
)) {
11364 mutex_enter(&rp
->r_statelock
);
11365 if (rp
->r_pathconf
.pc4_cache_valid
) {
11368 case _PC_FILESIZEBITS
:
11370 rp
->r_pathconf
.pc4_filesizebits
;
11374 rp
->r_pathconf
.pc4_link_max
;
11378 rp
->r_pathconf
.pc4_name_max
;
11380 case _PC_CHOWN_RESTRICTED
:
11382 rp
->r_pathconf
.pc4_chown_restricted
;
11386 rp
->r_pathconf
.pc4_no_trunc
;
11392 mutex_exit(&rp
->r_statelock
);
11394 nfs4_pathconf_cache_hits
++;
11398 mutex_exit(&rp
->r_statelock
);
11402 nfs4_pathconf_cache_misses
++;
11407 error
= nfs4_attr_otw(vp
, TAG_PATHCONF
, &gar
, NFS4_PATHCONF_MASK
, cr
);
11410 mutex_enter(&rp
->r_statelock
);
11411 rp
->r_pathconf
.pc4_cache_valid
= FALSE
;
11412 rp
->r_pathconf
.pc4_xattr_valid
= FALSE
;
11413 mutex_exit(&rp
->r_statelock
);
11417 /* interpret the max filesize */
11418 gar
.n4g_ext_res
->n4g_pc4
.pc4_filesizebits
=
11419 fattr4_maxfilesize_to_bits(gar
.n4g_ext_res
->n4g_maxfilesize
);
11421 /* Store the attributes we just received */
11422 nfs4_attr_cache(vp
, &gar
, t
, cr
, TRUE
, NULL
);
11425 case _PC_FILESIZEBITS
:
11426 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_filesizebits
;
11429 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_link_max
;
11432 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_name_max
;
11434 case _PC_CHOWN_RESTRICTED
:
11435 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_chown_restricted
;
11438 *valp
= gar
.n4g_ext_res
->n4g_pc4
.pc4_no_trunc
;
11440 case _PC_XATTR_EXISTS
:
11441 if (gar
.n4g_ext_res
->n4g_pc4
.pc4_xattr_exists
) {
11442 if (error
= nfs4_have_xattrs(vp
, valp
, cr
))
11454 * Called by async thread to do synchronous pageio. Do the i/o, wait
11455 * for it to complete, and cleanup the page list when done.
11458 nfs4_sync_pageio(vnode_t
*vp
, page_t
*pp
, u_offset_t io_off
, size_t io_len
,
11459 int flags
, cred_t
*cr
)
11463 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11465 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
11466 if (flags
& B_READ
)
11467 pvn_read_done(pp
, (error
? B_ERROR
: 0) | flags
);
11469 pvn_write_done(pp
, (error
? B_ERROR
: 0) | flags
);
11475 nfs4_pageio(vnode_t
*vp
, page_t
*pp
, u_offset_t io_off
, size_t io_len
,
11476 int flags
, cred_t
*cr
, caller_context_t
*ct
)
11481 if (!(flags
& B_ASYNC
) && nfs_zone() != VTOMI4(vp
)->mi_zone
)
11488 mutex_enter(&rp
->r_statelock
);
11490 mutex_exit(&rp
->r_statelock
);
11492 if (flags
& B_ASYNC
) {
11493 error
= nfs4_async_pageio(vp
, pp
, io_off
, io_len
, flags
, cr
,
11496 error
= nfs4_rdwrlbn(vp
, pp
, io_off
, io_len
, flags
, cr
);
11497 mutex_enter(&rp
->r_statelock
);
11499 cv_broadcast(&rp
->r_cv
);
11500 mutex_exit(&rp
->r_statelock
);
11506 nfs4_dispose(vnode_t
*vp
, page_t
*pp
, int fl
, int dn
, cred_t
*cr
,
11507 caller_context_t
*ct
)
11518 * We should get called with fl equal to either B_FREE or
11519 * B_INVAL. Any other value is illegal.
11521 * The page that we are either supposed to free or destroy
11522 * should be exclusive locked and its io lock should not
11525 ASSERT(fl
== B_FREE
|| fl
== B_INVAL
);
11526 ASSERT((PAGE_EXCL(pp
) && !page_iolock_assert(pp
)) || panicstr
);
11531 * If the page doesn't need to be committed or we shouldn't
11532 * even bother attempting to commit it, then just make sure
11533 * that the p_fsdata byte is clear and then either free or
11534 * destroy the page as appropriate.
11536 if (pp
->p_fsdata
== C_NOCOMMIT
|| (rp
->r_flags
& R4STALE
)) {
11537 pp
->p_fsdata
= C_NOCOMMIT
;
11541 page_destroy(pp
, dn
);
11546 * If there is a page invalidation operation going on, then
11547 * if this is one of the pages being destroyed, then just
11548 * clear the p_fsdata byte and then either free or destroy
11549 * the page as appropriate.
11551 mutex_enter(&rp
->r_statelock
);
11552 if ((rp
->r_flags
& R4TRUNCATE
) && pp
->p_offset
>= rp
->r_truncaddr
) {
11553 mutex_exit(&rp
->r_statelock
);
11554 pp
->p_fsdata
= C_NOCOMMIT
;
11558 page_destroy(pp
, dn
);
11563 * If we are freeing this page and someone else is already
11564 * waiting to do a commit, then just unlock the page and
11565 * return. That other thread will take care of commiting
11566 * this page. The page can be freed sometime after the
11567 * commit has finished. Otherwise, if the page is marked
11568 * as delay commit, then we may be getting called from
11569 * pvn_write_done, one page at a time. This could result
11570 * in one commit per page, so we end up doing lots of small
11571 * commits instead of fewer larger commits. This is bad,
11572 * we want do as few commits as possible.
11574 if (fl
== B_FREE
) {
11575 if (rp
->r_flags
& R4COMMITWAIT
) {
11577 mutex_exit(&rp
->r_statelock
);
11580 if (pp
->p_fsdata
== C_DELAYCOMMIT
) {
11581 pp
->p_fsdata
= C_COMMIT
;
11583 mutex_exit(&rp
->r_statelock
);
11589 * Check to see if there is a signal which would prevent an
11590 * attempt to commit the pages from being successful. If so,
11591 * then don't bother with all of the work to gather pages and
11592 * generate the unsuccessful RPC. Just return from here and
11593 * let the page be committed at some later time.
11595 sigintr(&smask
, VTOMI4(vp
)->mi_flags
& MI4_INT
);
11596 if (ttolwp(curthread
) != NULL
&& ISSIG(curthread
, JUSTLOOKING
)) {
11599 mutex_exit(&rp
->r_statelock
);
11605 * We are starting to need to commit pages, so let's try
11606 * to commit as many as possible at once to reduce the
11609 * Set the `commit inprogress' state bit. We must
11610 * first wait until any current one finishes. Then
11611 * we initialize the c_pages list with this page.
11613 while (rp
->r_flags
& R4COMMIT
) {
11614 rp
->r_flags
|= R4COMMITWAIT
;
11615 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
11616 rp
->r_flags
&= ~R4COMMITWAIT
;
11618 rp
->r_flags
|= R4COMMIT
;
11619 mutex_exit(&rp
->r_statelock
);
11620 ASSERT(rp
->r_commit
.c_pages
== NULL
);
11621 rp
->r_commit
.c_pages
= pp
;
11622 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11623 rp
->r_commit
.c_commlen
= PAGESIZE
;
11626 * Gather together all other pages which can be committed.
11627 * They will all be chained off r_commit.c_pages.
11629 nfs4_get_commit(vp
);
11632 * Clear the `commit inprogress' status and disconnect
11633 * the list of pages to be committed from the rnode.
11634 * At this same time, we also save the starting offset
11635 * and length of data to be committed on the server.
11637 plist
= rp
->r_commit
.c_pages
;
11638 rp
->r_commit
.c_pages
= NULL
;
11639 offset
= rp
->r_commit
.c_commbase
;
11640 len
= rp
->r_commit
.c_commlen
;
11641 mutex_enter(&rp
->r_statelock
);
11642 rp
->r_flags
&= ~R4COMMIT
;
11643 cv_broadcast(&rp
->r_commit
.c_cv
);
11644 mutex_exit(&rp
->r_statelock
);
11646 if (curproc
== proc_pageout
|| curproc
== proc_fsflush
||
11647 nfs_zone() != VTOMI4(vp
)->mi_zone
) {
11648 nfs4_async_commit(vp
, plist
, offset
, len
,
11649 cr
, do_nfs4_async_commit
);
11654 * Actually generate the COMMIT op over the wire operation.
11656 error
= nfs4_commit(vp
, (offset4
)offset
, (count4
)len
, cr
);
11659 * If we got an error during the commit, just unlock all
11660 * of the pages. The pages will get retransmitted to the
11661 * server during a putpage operation.
11664 while (plist
!= NULL
) {
11666 page_sub(&plist
, pptr
);
11673 * We've tried as hard as we can to commit the data to stable
11674 * storage on the server. We just unlock the rest of the pages
11675 * and clear the commit required state. They will be put
11676 * onto the tail of the cachelist if they are nolonger
11679 while (plist
!= pp
) {
11681 page_sub(&plist
, pptr
);
11682 pptr
->p_fsdata
= C_NOCOMMIT
;
11687 * It is possible that nfs4_commit didn't return error but
11688 * some other thread has modified the page we are going
11690 * In this case we need to rewrite the page. Do an explicit check
11691 * before attempting to free/destroy the page. If modified, needs to
11692 * be rewritten so unlock the page and return.
11694 if (hat_ismod(pp
)) {
11695 pp
->p_fsdata
= C_NOCOMMIT
;
11701 * Now, as appropriate, either free or destroy the page
11702 * that we were called with.
11704 pp
->p_fsdata
= C_NOCOMMIT
;
11708 page_destroy(pp
, dn
);
11712 * Commit requires that the current fh be the file written to.
11713 * The compound op structure is:
11714 * PUTFH(file), COMMIT
11717 nfs4_commit(vnode_t
*vp
, offset4 offset
, count4 count
, cred_t
*cr
)
11719 COMPOUND4args_clnt args
;
11720 COMPOUND4res_clnt res
;
11721 COMMIT4res
*cm_res
;
11722 nfs_argop4 argop
[2];
11727 cred_t
*cred_otw
= NULL
;
11728 bool_t needrecov
= FALSE
;
11729 nfs4_recov_state_t recov_state
;
11730 nfs4_open_stream_t
*osp
= NULL
;
11731 bool_t first_time
= TRUE
; /* first time getting OTW cred */
11732 bool_t last_time
= FALSE
; /* last time getting OTW cred */
11733 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
11735 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11740 recov_state
.rs_flags
= 0;
11741 recov_state
.rs_num_retry_despite_err
= 0;
11744 * Releases the osp, if a valid open stream is provided.
11745 * Puts a hold on the cred_otw and the new osp (if found).
11747 cred_otw
= nfs4_get_otw_cred_by_osp(rp
, cr
, &osp
,
11748 &first_time
, &last_time
);
11749 args
.ctag
= TAG_COMMIT
;
11752 * Commit ops: putfh file; commit
11754 args
.array_len
= 2;
11755 args
.array
= argop
;
11757 e
.error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11758 &recov_state
, NULL
);
11762 open_stream_rele(osp
, rp
);
11766 /* putfh directory */
11767 argop
[0].argop
= OP_CPUTFH
;
11768 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
11771 argop
[1].argop
= OP_COMMIT
;
11772 argop
[1].nfs_argop4_u
.opcommit
.offset
= offset
;
11773 argop
[1].nfs_argop4_u
.opcommit
.count
= count
;
11776 rfs4call(mi
, &args
, &res
, cred_otw
, &doqueue
, 0, &e
);
11778 needrecov
= nfs4_needs_recovery(&e
, FALSE
, mi
->mi_vfsp
);
11779 if (!needrecov
&& e
.error
) {
11780 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
, &recov_state
,
11783 if (e
.error
== EACCES
&& last_time
== FALSE
)
11784 goto get_commit_cred
;
11786 open_stream_rele(osp
, rp
);
11791 if (nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
11792 NULL
, OP_COMMIT
, NULL
, NULL
, NULL
) == FALSE
) {
11793 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11794 &recov_state
, needrecov
);
11796 (void) xdr_free(xdr_COMPOUND4res_clnt
,
11801 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11802 &recov_state
, needrecov
);
11805 open_stream_rele(osp
, rp
);
11808 /* fall through for res.status case */
11812 e
.error
= geterrno4(res
.status
);
11813 if (e
.error
== EACCES
&& last_time
== FALSE
) {
11815 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11816 &recov_state
, needrecov
);
11817 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11818 goto get_commit_cred
;
11821 * Can't do a nfs4_purge_stale_fh here because this
11822 * can cause a deadlock. nfs4_commit can
11823 * be called from nfs4_dispose which can be called
11824 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11825 * can call back to pvn_vplist_dirty.
11827 if (e
.error
== ESTALE
) {
11828 mutex_enter(&rp
->r_statelock
);
11829 rp
->r_flags
|= R4STALE
;
11831 rp
->r_error
= e
.error
;
11832 mutex_exit(&rp
->r_statelock
);
11833 PURGE_ATTRCACHE4(vp
);
11835 mutex_enter(&rp
->r_statelock
);
11837 rp
->r_error
= e
.error
;
11838 mutex_exit(&rp
->r_statelock
);
11841 ASSERT(rp
->r_flags
& R4HAVEVERF
);
11842 resop
= &res
.array
[1]; /* commit res */
11843 cm_res
= &resop
->nfs_resop4_u
.opcommit
;
11844 mutex_enter(&rp
->r_statelock
);
11845 if (cm_res
->writeverf
== rp
->r_writeverf
) {
11846 mutex_exit(&rp
->r_statelock
);
11847 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11848 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
,
11849 &recov_state
, needrecov
);
11852 open_stream_rele(osp
, rp
);
11856 rp
->r_writeverf
= cm_res
->writeverf
;
11857 mutex_exit(&rp
->r_statelock
);
11858 e
.error
= NFS_VERF_MISMATCH
;
11861 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
11862 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, OH_COMMIT
, &recov_state
, needrecov
);
11865 open_stream_rele(osp
, rp
);
11871 nfs4_set_mod(vnode_t
*vp
)
11873 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
11875 /* make sure we're looking at the master vnode, not a shadow */
11876 pvn_vplist_setdirty(RTOV4(VTOR4(vp
)), nfs_setmod_check
);
11880 * This function is used to gather a page list of the pages which
11881 * can be committed on the server.
11883 * The calling thread must have set R4COMMIT. This bit is used to
11884 * serialize access to the commit structure in the rnode. As long
11885 * as the thread has set R4COMMIT, then it can manipulate the commit
11886 * structure without requiring any other locks.
11888 * When this function is called from nfs4_dispose() the page passed
11889 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11890 * will skip it. This is not a problem since we initially add the
11891 * page to the r_commit page list.
11895 nfs4_get_commit(vnode_t
*vp
)
11903 ASSERT(rp
->r_flags
& R4COMMIT
);
11905 /* make sure we're looking at the master vnode, not a shadow */
11907 if (IS_SHADOW(vp
, rp
))
11910 vphm
= page_vnode_mutex(vp
);
11914 * If there are no pages associated with this vnode, then
11917 if ((pp
= vp
->v_pages
) == NULL
) {
11923 * Step through all of the pages associated with this vnode
11924 * looking for pages which need to be committed.
11927 /* Skip marker pages. */
11928 if (pp
->p_hash
== PVN_VPLIST_HASH_TAG
)
11932 * First short-cut everything (without the page_lock)
11933 * and see if this page does not need to be committed
11934 * or is modified if so then we'll just skip it.
11936 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
))
11940 * Attempt to lock the page. If we can't, then
11941 * someone else is messing with it or we have been
11942 * called from nfs4_dispose and this is the page that
11943 * nfs4_dispose was called with.. anyway just skip it.
11945 if (!page_trylock(pp
, SE_EXCL
))
11949 * Lets check again now that we have the page lock.
11951 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
11956 /* this had better not be a free page */
11957 ASSERT(PP_ISFREE(pp
) == 0);
11960 * The page needs to be committed and we locked it.
11961 * Update the base and length parameters and add it
11964 if (rp
->r_commit
.c_pages
== NULL
) {
11965 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11966 rp
->r_commit
.c_commlen
= PAGESIZE
;
11967 } else if (pp
->p_offset
< rp
->r_commit
.c_commbase
) {
11968 rp
->r_commit
.c_commlen
= rp
->r_commit
.c_commbase
-
11969 (offset3
)pp
->p_offset
+ rp
->r_commit
.c_commlen
;
11970 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
11971 } else if ((rp
->r_commit
.c_commbase
+ rp
->r_commit
.c_commlen
)
11973 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
11974 rp
->r_commit
.c_commbase
+ PAGESIZE
;
11976 page_add(&rp
->r_commit
.c_pages
, pp
);
11977 } while ((pp
= pp
->p_vpnext
) != vp
->v_pages
);
11983 * This routine is used to gather together a page list of the pages
11984 * which are to be committed on the server. This routine must not
11985 * be called if the calling thread holds any locked pages.
11987 * The calling thread must have set R4COMMIT. This bit is used to
11988 * serialize access to the commit structure in the rnode. As long
11989 * as the thread has set R4COMMIT, then it can manipulate the commit
11990 * structure without requiring any other locks.
11993 nfs4_get_commit_range(vnode_t
*vp
, u_offset_t soff
, size_t len
)
12002 ASSERT(rp
->r_flags
& R4COMMIT
);
12004 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12006 /* make sure we're looking at the master vnode, not a shadow */
12008 if (IS_SHADOW(vp
, rp
))
12012 * If there are no pages associated with this vnode, then
12015 if ((pp
= vp
->v_pages
) == NULL
)
12018 * Calculate the ending offset.
12021 for (off
= soff
; off
< end
; off
+= PAGESIZE
) {
12023 * Lookup each page by vp, offset.
12025 if ((pp
= page_lookup_nowait(vp
, off
, SE_EXCL
)) == NULL
)
12028 * If this page does not need to be committed or is
12029 * modified, then just skip it.
12031 if (pp
->p_fsdata
== C_NOCOMMIT
|| hat_ismod(pp
)) {
12036 ASSERT(PP_ISFREE(pp
) == 0);
12038 * The page needs to be committed and we locked it.
12039 * Update the base and length parameters and add it
12042 if (rp
->r_commit
.c_pages
== NULL
) {
12043 rp
->r_commit
.c_commbase
= (offset3
)pp
->p_offset
;
12044 rp
->r_commit
.c_commlen
= PAGESIZE
;
12046 rp
->r_commit
.c_commlen
= (offset3
)pp
->p_offset
-
12047 rp
->r_commit
.c_commbase
+ PAGESIZE
;
12049 page_add(&rp
->r_commit
.c_pages
, pp
);
12054 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12055 * Flushes and commits data to the server.
12058 nfs4_putpage_commit(vnode_t
*vp
, offset_t poff
, size_t plen
, cred_t
*cr
)
12061 verifier4 write_verf
;
12062 rnode4_t
*rp
= VTOR4(vp
);
12064 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12067 * Flush the data portion of the file and then commit any
12068 * portions which need to be committed. This may need to
12069 * be done twice if the server has changed state since
12070 * data was last written. The data will need to be
12071 * rewritten to the server and then a new commit done.
12073 * In fact, this may need to be done several times if the
12074 * server is having problems and crashing while we are
12075 * attempting to do this.
12080 * Do a flush based on the poff and plen arguments. This
12081 * will synchronously write out any modified pages in the
12082 * range specified by (poff, plen). This starts all of the
12083 * i/o operations which will be waited for in the next
12084 * call to nfs4_putpage
12087 mutex_enter(&rp
->r_statelock
);
12088 write_verf
= rp
->r_writeverf
;
12089 mutex_exit(&rp
->r_statelock
);
12091 error
= nfs4_putpage(vp
, poff
, plen
, B_ASYNC
, cr
, NULL
);
12092 if (error
== EAGAIN
)
12096 * Do a flush based on the poff and plen arguments. This
12097 * will synchronously write out any modified pages in the
12098 * range specified by (poff, plen) and wait until all of
12099 * the asynchronous i/o's in that range are done as well.
12102 error
= nfs4_putpage(vp
, poff
, plen
, 0, cr
, NULL
);
12107 mutex_enter(&rp
->r_statelock
);
12108 if (rp
->r_writeverf
!= write_verf
) {
12109 mutex_exit(&rp
->r_statelock
);
12112 mutex_exit(&rp
->r_statelock
);
12115 * Now commit any pages which might need to be committed.
12116 * If the error, NFS_VERF_MISMATCH, is returned, then
12117 * start over with the flush operation.
12119 error
= nfs4_commit_vp(vp
, poff
, plen
, cr
, NFS4_WRITE_WAIT
);
12121 if (error
== NFS_VERF_MISMATCH
)
12128 * nfs4_commit_vp() will wait for other pending commits and
12129 * will either commit the whole file or a range, plen dictates
12130 * if we commit whole file. a value of zero indicates the whole
12131 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12134 nfs4_commit_vp(vnode_t
*vp
, u_offset_t poff
, size_t plen
,
12135 cred_t
*cr
, int wait_on_writes
)
12142 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12147 * before we gather commitable pages make
12148 * sure there are no outstanding async writes
12150 if (rp
->r_count
&& wait_on_writes
== NFS4_WRITE_WAIT
) {
12151 mutex_enter(&rp
->r_statelock
);
12152 while (rp
->r_count
> 0) {
12153 cv_wait(&rp
->r_cv
, &rp
->r_statelock
);
12155 mutex_exit(&rp
->r_statelock
);
12159 * Set the `commit inprogress' state bit. We must
12160 * first wait until any current one finishes.
12162 mutex_enter(&rp
->r_statelock
);
12163 while (rp
->r_flags
& R4COMMIT
) {
12164 rp
->r_flags
|= R4COMMITWAIT
;
12165 cv_wait(&rp
->r_commit
.c_cv
, &rp
->r_statelock
);
12166 rp
->r_flags
&= ~R4COMMITWAIT
;
12168 rp
->r_flags
|= R4COMMIT
;
12169 mutex_exit(&rp
->r_statelock
);
12172 * Gather all of the pages which need to be
12176 nfs4_get_commit(vp
);
12178 nfs4_get_commit_range(vp
, poff
, plen
);
12181 * Clear the `commit inprogress' bit and disconnect the
12182 * page list which was gathered by nfs4_get_commit.
12184 plist
= rp
->r_commit
.c_pages
;
12185 rp
->r_commit
.c_pages
= NULL
;
12186 offset
= rp
->r_commit
.c_commbase
;
12187 len
= rp
->r_commit
.c_commlen
;
12188 mutex_enter(&rp
->r_statelock
);
12189 rp
->r_flags
&= ~R4COMMIT
;
12190 cv_broadcast(&rp
->r_commit
.c_cv
);
12191 mutex_exit(&rp
->r_statelock
);
12194 * If any pages need to be committed, commit them and
12195 * then unlock them so that they can be freed some
12202 * No error occurred during the flush portion
12203 * of this operation, so now attempt to commit
12204 * the data to stable storage on the server.
12206 * This will unlock all of the pages on the list.
12208 return (nfs4_sync_commit(vp
, plist
, offset
, len
, cr
));
12212 nfs4_sync_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
12218 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12220 error
= nfs4_commit(vp
, (offset4
)offset
, (count3
)count
, cr
);
12223 * If we got an error, then just unlock all of the pages
12227 while (plist
!= NULL
) {
12229 page_sub(&plist
, pp
);
12235 * We've tried as hard as we can to commit the data to stable
12236 * storage on the server. We just unlock the pages and clear
12237 * the commit required state. They will get freed later.
12239 while (plist
!= NULL
) {
12241 page_sub(&plist
, pp
);
12242 pp
->p_fsdata
= C_NOCOMMIT
;
12250 do_nfs4_async_commit(vnode_t
*vp
, page_t
*plist
, offset3 offset
, count3 count
,
12254 (void) nfs4_sync_commit(vp
, plist
, offset
, count
, cr
);
12259 nfs4_setsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
12260 caller_context_t
*ct
)
12265 vsecattr_t nfsace4_vsap
;
12268 if (nfs_zone() != mi
->mi_zone
)
12270 if (mi
->mi_flags
& MI4_ACL
) {
12271 /* if we have a delegation, return it */
12272 if (VTOR4(vp
)->r_deleg_type
!= OPEN_DELEGATE_NONE
)
12273 (void) nfs4delegreturn(VTOR4(vp
),
12274 NFS4_DR_REOPEN
|NFS4_DR_PUSH
);
12276 error
= nfs4_is_acl_mask_valid(vsecattr
->vsa_mask
,
12278 if (error
) /* EINVAL */
12281 if (vsecattr
->vsa_mask
& (VSA_ACL
| VSA_DFACL
)) {
12283 * These are aclent_t type entries.
12285 error
= vs_aent_to_ace4(vsecattr
, &nfsace4_vsap
,
12286 vp
->v_type
== VDIR
, FALSE
);
12291 * These are ace_t type entries.
12293 error
= vs_acet_to_ace4(vsecattr
, &nfsace4_vsap
,
12298 bzero(&va
, sizeof (va
));
12299 error
= nfs4setattr(vp
, &va
, flag
, cr
, &nfsace4_vsap
);
12300 vs_ace4_destroy(&nfsace4_vsap
);
12308 nfs4_getsecattr(vnode_t
*vp
, vsecattr_t
*vsecattr
, int flag
, cred_t
*cr
,
12309 caller_context_t
*ct
)
12314 rnode4_t
*rp
= VTOR4(vp
);
12317 if (nfs_zone() != mi
->mi_zone
)
12320 bzero(&gar
, sizeof (gar
));
12321 gar
.n4g_vsa
.vsa_mask
= vsecattr
->vsa_mask
;
12324 * vsecattr->vsa_mask holds the original acl request mask.
12325 * This is needed when determining what to return.
12326 * (See: nfs4_create_getsecattr_return())
12328 error
= nfs4_is_acl_mask_valid(vsecattr
->vsa_mask
, NFS4_ACL_GET
);
12329 if (error
) /* EINVAL */
12333 * If this is a referral stub, don't try to go OTW for an ACL
12335 if (RP_ISSTUB_REFERRAL(VTOR4(vp
)))
12336 return (fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
));
12338 if (mi
->mi_flags
& MI4_ACL
) {
12340 * Check if the data is cached and the cache is valid. If it
12341 * is we don't go over the wire.
12343 if (rp
->r_secattr
!= NULL
&& ATTRCACHE4_VALID(vp
)) {
12344 mutex_enter(&rp
->r_statelock
);
12345 if (rp
->r_secattr
!= NULL
) {
12346 error
= nfs4_create_getsecattr_return(
12347 rp
->r_secattr
, vsecattr
, rp
->r_attr
.va_uid
,
12349 vp
->v_type
== VDIR
);
12350 if (!error
) { /* error == 0 - Success! */
12351 mutex_exit(&rp
->r_statelock
);
12355 mutex_exit(&rp
->r_statelock
);
12359 * The getattr otw call will always get both the acl, in
12360 * the form of a list of nfsace4's, and the number of acl
12361 * entries; independent of the value of gar.n4g_vsa.vsa_mask.
12363 gar
.n4g_va
.va_mask
= AT_ALL
;
12364 error
= nfs4_getattr_otw(vp
, &gar
, cr
, 1);
12366 vs_ace4_destroy(&gar
.n4g_vsa
);
12367 if (error
== ENOTSUP
|| error
== EOPNOTSUPP
)
12368 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12372 if (!(gar
.n4g_resbmap
& FATTR4_ACL_MASK
)) {
12374 * No error was returned, but according to the response
12375 * bitmap, neither was an acl.
12377 vs_ace4_destroy(&gar
.n4g_vsa
);
12378 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12383 * Update the cache with the ACL.
12385 nfs4_acl_fill_cache(rp
, &gar
.n4g_vsa
);
12387 error
= nfs4_create_getsecattr_return(&gar
.n4g_vsa
,
12388 vsecattr
, gar
.n4g_va
.va_uid
, gar
.n4g_va
.va_gid
,
12389 vp
->v_type
== VDIR
);
12390 vs_ace4_destroy(&gar
.n4g_vsa
);
12391 if ((error
) && (vsecattr
->vsa_mask
&
12392 (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
)) &&
12393 (error
!= EACCES
)) {
12394 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12398 error
= fs_fab_acl(vp
, vsecattr
, flag
, cr
, ct
);
12403 * The function returns:
12404 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12405 * - EINVAL if the passed in "acl_mask" is an invalid request.
12407 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12408 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12410 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12411 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12412 * - We have a count field set without the corresponding acl field set. (e.g. -
12413 * VSA_ACECNT is set, but VSA_ACE is not)
12416 nfs4_is_acl_mask_valid(uint_t acl_mask
, nfs4_acl_op_t op
)
12418 /* Shortcut the masks that are always valid. */
12419 if (acl_mask
== (VSA_ACE
| VSA_ACECNT
))
12421 if (acl_mask
== (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
))
12424 if (acl_mask
& (VSA_ACE
| VSA_ACECNT
)) {
12426 * We can't have any VSA_ACL type stuff in the mask now.
12428 if (acl_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
|
12432 if (op
== NFS4_ACL_SET
) {
12433 if ((acl_mask
& VSA_ACECNT
) && !(acl_mask
& VSA_ACE
))
12438 if (acl_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
| VSA_DFACLCNT
)) {
12440 * We can't have any VSA_ACE type stuff in the mask now.
12442 if (acl_mask
& (VSA_ACE
| VSA_ACECNT
))
12445 if (op
== NFS4_ACL_SET
) {
12446 if ((acl_mask
& VSA_ACLCNT
) && !(acl_mask
& VSA_ACL
))
12449 if ((acl_mask
& VSA_DFACLCNT
) &&
12450 !(acl_mask
& VSA_DFACL
))
12458 * The theory behind creating the correct getsecattr return is simply this:
12459 * "Don't return anything that the caller is not expecting to have to free."
12462 nfs4_create_getsecattr_return(vsecattr_t
*filled_vsap
, vsecattr_t
*vsap
,
12463 uid_t uid
, gid_t gid
, int isdir
)
12466 /* Save the mask since the translators modify it. */
12467 uint_t orig_mask
= vsap
->vsa_mask
;
12469 if (orig_mask
& (VSA_ACE
| VSA_ACECNT
)) {
12470 error
= vs_ace4_to_acet(filled_vsap
, vsap
, uid
, gid
, FALSE
);
12476 * If the caller only asked for the ace count (VSA_ACECNT)
12477 * don't give them the full acl (VSA_ACE), free it.
12479 if (!orig_mask
& VSA_ACE
) {
12480 if (vsap
->vsa_aclentp
!= NULL
) {
12481 kmem_free(vsap
->vsa_aclentp
,
12482 vsap
->vsa_aclcnt
* sizeof (ace_t
));
12483 vsap
->vsa_aclentp
= NULL
;
12486 vsap
->vsa_mask
= orig_mask
;
12488 } else if (orig_mask
& (VSA_ACL
| VSA_ACLCNT
| VSA_DFACL
|
12490 error
= vs_ace4_to_aent(filled_vsap
, vsap
, uid
, gid
,
12497 * If the caller only asked for the acl count (VSA_ACLCNT)
12498 * and/or the default acl count (VSA_DFACLCNT) don't give them
12499 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12501 if (!orig_mask
& VSA_ACL
) {
12502 if (vsap
->vsa_aclentp
!= NULL
) {
12503 kmem_free(vsap
->vsa_aclentp
,
12504 vsap
->vsa_aclcnt
* sizeof (aclent_t
));
12505 vsap
->vsa_aclentp
= NULL
;
12509 if (!orig_mask
& VSA_DFACL
) {
12510 if (vsap
->vsa_dfaclentp
!= NULL
) {
12511 kmem_free(vsap
->vsa_dfaclentp
,
12512 vsap
->vsa_dfaclcnt
* sizeof (aclent_t
));
12513 vsap
->vsa_dfaclentp
= NULL
;
12516 vsap
->vsa_mask
= orig_mask
;
12523 nfs4_shrlock(vnode_t
*vp
, int cmd
, struct shrlock
*shr
, int flag
, cred_t
*cr
,
12524 caller_context_t
*ct
)
12528 if (nfs_zone() != VTOMI4(vp
)->mi_zone
)
12531 * check for valid cmd parameter
12533 if (cmd
!= F_SHARE
&& cmd
!= F_UNSHARE
&& cmd
!= F_HASREMOTELOCKS
)
12537 * Check access permissions
12539 if ((cmd
& F_SHARE
) &&
12540 (((shr
->s_access
& F_RDACC
) && (flag
& FREAD
) == 0) ||
12541 (shr
->s_access
== F_WRACC
&& (flag
& FWRITE
) == 0)))
12545 * If the filesystem is mounted using local locking, pass the
12546 * request off to the local share code.
12548 if (VTOMI4(vp
)->mi_flags
& MI4_LLOCK
)
12549 return (fs_shrlock(vp
, cmd
, shr
, flag
, cr
, ct
));
12555 * This will be properly implemented later,
12556 * see RFE: 4823948 .
12561 case F_HASREMOTELOCKS
:
12563 * NFS client can't store remote locks itself
12578 * Common code called by directory ops to update the attrcache
12581 nfs4_update_attrcache(nfsstat4 status
, nfs4_ga_res_t
*garp
,
12582 hrtime_t t
, vnode_t
*vp
, cred_t
*cr
)
12586 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12588 if (status
!= NFS4_OK
) {
12589 /* getattr not done or failed */
12590 PURGE_ATTRCACHE4(vp
);
12595 nfs4_attr_cache(vp
, garp
, t
, cr
, FALSE
, NULL
);
12597 PURGE_ATTRCACHE4(vp
);
12603 * Update directory caches for directory modification ops (link, rename, etc.)
12604 * When dinfo is NULL, manage dircaches in the old way.
12607 nfs4_update_dircaches(change_info4
*cinfo
, vnode_t
*dvp
, vnode_t
*vp
, char *nm
,
12608 dirattr_info_t
*dinfo
)
12610 rnode4_t
*drp
= VTOR4(dvp
);
12612 ASSERT(nfs_zone() == VTOMI4(dvp
)->mi_zone
);
12614 /* Purge rddir cache for dir since it changed */
12615 if (drp
->r_dir
!= NULL
)
12616 nfs4_purge_rddir_cache(dvp
);
12619 * If caller provided dinfo, then use it to manage dir caches.
12621 if (dinfo
!= NULL
) {
12623 mutex_enter(&VTOR4(vp
)->r_statev4_lock
);
12624 if (!VTOR4(vp
)->created_v4
) {
12625 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12626 dnlc_update(dvp
, nm
, vp
);
12629 * XXX don't update if the created_v4 flag is
12632 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12633 NFS4_DEBUG(nfs4_client_state_debug
,
12634 (CE_NOTE
, "nfs4_update_dircaches: "
12635 "don't update dnlc: created_v4 flag"));
12639 nfs4_attr_cache(dvp
, dinfo
->di_garp
, dinfo
->di_time_call
,
12640 dinfo
->di_cred
, FALSE
, cinfo
);
12646 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12647 * Since caller modified dir but didn't receive post-dirmod-op dir
12648 * attrs, the dir's attrs must be purged.
12650 * XXX this check and dnlc update/purge should really be atomic,
12651 * XXX but can't use rnode statelock because it'll deadlock in
12652 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12655 * XXX We also may want to check that atomic is true in the
12656 * XXX change_info struct. If it is not, the change_info may
12657 * XXX reflect changes by more than one clients which means that
12658 * XXX our cache may not be valid.
12660 PURGE_ATTRCACHE4(dvp
);
12661 if (drp
->r_change
== cinfo
->before
) {
12662 /* no changes took place in the directory prior to our link */
12664 mutex_enter(&VTOR4(vp
)->r_statev4_lock
);
12665 if (!VTOR4(vp
)->created_v4
) {
12666 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12667 dnlc_update(dvp
, nm
, vp
);
12670 * XXX dont' update if the created_v4 flag
12673 mutex_exit(&VTOR4(vp
)->r_statev4_lock
);
12674 NFS4_DEBUG(nfs4_client_state_debug
, (CE_NOTE
,
12675 "nfs4_update_dircaches: don't"
12676 " update dnlc: created_v4 flag"));
12680 /* Another client modified directory - purge its dnlc cache */
12681 dnlc_purge_vp(dvp
);
12686 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12689 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12690 * file (ie: client recovery) and otherwise set to FALSE.
12692 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12693 * initiated) calling functions.
12695 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12696 * of resending a 'lost' open request.
12698 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12699 * server that hands out BAD_SEQID on open confirm.
12701 * Errors are returned via the nfs4_error_t parameter.
12704 nfs4open_confirm(vnode_t
*vp
, seqid4
*seqid
, stateid4
*stateid
, cred_t
*cr
,
12705 bool_t reopening_file
, bool_t
*retry_open
, nfs4_open_owner_t
*oop
,
12706 bool_t resend
, nfs4_error_t
*ep
, int *num_bseqid_retryp
)
12708 COMPOUND4args_clnt args
;
12709 COMPOUND4res_clnt res
;
12710 nfs_argop4 argop
[2];
12714 OPEN_CONFIRM4args
*open_confirm_args
;
12717 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12719 mutex_enter(&oop
->oo_lock
);
12720 ASSERT(oop
->oo_seqid_inuse
);
12721 mutex_exit(&oop
->oo_lock
);
12724 recov_retry_confirm
:
12725 nfs4_error_zinit(ep
);
12726 *retry_open
= FALSE
;
12729 args
.ctag
= TAG_OPEN_CONFIRM_LOST
;
12731 args
.ctag
= TAG_OPEN_CONFIRM
;
12733 args
.array_len
= 2;
12734 args
.array
= argop
;
12736 /* putfh target fh */
12737 argop
[0].argop
= OP_CPUTFH
;
12738 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= VTOR4(vp
)->r_fh
;
12740 argop
[1].argop
= OP_OPEN_CONFIRM
;
12741 open_confirm_args
= &argop
[1].nfs_argop4_u
.opopen_confirm
;
12744 open_confirm_args
->seqid
= *seqid
;
12745 open_confirm_args
->open_stateid
= *stateid
;
12749 rfs4call(mi
, &args
, &res
, cr
, &doqueue
, 0, ep
);
12751 if (!ep
->error
&& nfs4_need_to_bump_seqid(&res
)) {
12752 nfs4_set_open_seqid((*seqid
), oop
, args
.ctag
);
12755 needrecov
= nfs4_needs_recovery(ep
, FALSE
, mi
->mi_vfsp
);
12756 if (!needrecov
&& ep
->error
)
12760 bool_t abort
= FALSE
;
12762 if (reopening_file
== FALSE
) {
12763 nfs4_bseqid_entry_t
*bsep
= NULL
;
12765 if (!ep
->error
&& res
.status
== NFS4ERR_BAD_SEQID
)
12766 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
12768 open_confirm_args
->seqid
);
12770 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
,
12771 NULL
, NULL
, OP_OPEN_CONFIRM
, bsep
, NULL
, NULL
);
12773 kmem_free(bsep
, sizeof (*bsep
));
12774 if (num_bseqid_retryp
&&
12775 --(*num_bseqid_retryp
) == 0)
12779 if ((ep
->error
== ETIMEDOUT
||
12780 res
.status
== NFS4ERR_RESOURCE
) &&
12781 abort
== FALSE
&& resend
== FALSE
) {
12783 (void) xdr_free(xdr_COMPOUND4res_clnt
,
12786 delay(SEC_TO_TICK(confirm_retry_sec
));
12787 goto recov_retry_confirm
;
12789 /* State may have changed so retry the entire OPEN op */
12790 if (abort
== FALSE
)
12791 *retry_open
= TRUE
;
12793 *retry_open
= FALSE
;
12795 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12800 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12804 resop
= &res
.array
[1]; /* open confirm res */
12805 bcopy(&resop
->nfs_resop4_u
.opopen_confirm
.open_stateid
,
12806 stateid
, sizeof (*stateid
));
12808 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)&res
);
12812 * Return the credentials associated with a client state object. The
12813 * caller is responsible for freeing the credentials.
12817 state_to_cred(nfs4_open_stream_t
*osp
)
12822 * It's ok to not lock the open stream and open owner to get
12823 * the oo_cred since this is only written once (upon creation)
12824 * and will not change.
12826 cr
= osp
->os_open_owner
->oo_cred
;
12835 * Find the sysid for the knetconfig associated with the given mi.
12837 static struct lm_sysid
*
12838 nfs4_find_sysid(mntinfo4_t
*mi
)
12840 ASSERT(nfs_zone() == mi
->mi_zone
);
12843 * Switch from RDMA knconf to original mount knconf
12845 return (lm_get_sysid(ORIG_KNCONF(mi
), &mi
->mi_curr_serv
->sv_addr
,
12846 mi
->mi_curr_serv
->sv_hostname
, NULL
));
12851 * Return a string version of the call type for easy reading.
12854 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype
)
12857 case NFS4_LCK_CTYPE_NORM
:
12859 case NFS4_LCK_CTYPE_RECLAIM
:
12860 return ("RECLAIM");
12861 case NFS4_LCK_CTYPE_RESEND
:
12863 case NFS4_LCK_CTYPE_REINSTATE
:
12864 return ("REINSTATE");
12866 cmn_err(CE_PANIC
, "nfs4frlock_get_call_type: got illegal "
12874 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12875 * Unlock requests don't have an over-the-wire locktype, so we just return
12876 * something non-threatening.
12879 static nfs_lock_type4
12880 flk_to_locktype(int cmd
, int l_type
)
12882 ASSERT(l_type
== F_RDLCK
|| l_type
== F_WRLCK
|| l_type
== F_UNLCK
);
12888 if (cmd
== F_SETLK
)
12893 if (cmd
== F_SETLK
)
12896 return (WRITEW_LT
);
12898 panic("flk_to_locktype");
12903 * Do some preliminary checks for nfs4frlock.
12906 nfs4frlock_validate_args(int cmd
, flock64_t
*flk
, int flag
, vnode_t
*vp
,
12912 * If we are setting a lock, check that the file is opened
12913 * with the correct mode.
12915 if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
12916 if ((flk
->l_type
== F_RDLCK
&& (flag
& FREAD
) == 0) ||
12917 (flk
->l_type
== F_WRLCK
&& (flag
& FWRITE
) == 0)) {
12918 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12919 "nfs4frlock_validate_args: file was opened with "
12920 "incorrect mode"));
12925 /* Convert the offset. It may need to be restored before returning. */
12926 if (error
= convoff(vp
, flk
, 0, offset
)) {
12927 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12928 "nfs4frlock_validate_args: convoff => error= %d\n",
12937 * Set the flock64's lm_sysid for nfs4frlock.
12940 nfs4frlock_get_sysid(struct lm_sysid
**lspp
, vnode_t
*vp
, flock64_t
*flk
)
12942 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
12944 /* Find the lm_sysid */
12945 *lspp
= nfs4_find_sysid(VTOMI4(vp
));
12947 if (*lspp
== NULL
) {
12948 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
12949 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12953 flk
->l_sysid
= lm_sysidt(*lspp
);
12959 * Do the remaining preliminary setup for nfs4frlock.
12962 nfs4frlock_pre_setup(clock_t *tick_delayp
, nfs4_recov_state_t
*recov_statep
,
12963 flock64_t
*flk
, short *whencep
, vnode_t
*vp
, cred_t
*search_cr
,
12967 * set tick_delay to the base delay time.
12968 * (NFS4_BASE_WAIT_TIME is in secs)
12971 *tick_delayp
= drv_usectohz(NFS4_BASE_WAIT_TIME
* 1000 * 1000);
12974 * If lock is relative to EOF, we need the newest length of the
12975 * file. Therefore invalidate the ATTR_CACHE.
12978 *whencep
= flk
->l_whence
;
12980 if (*whencep
== 2) /* SEEK_END */
12981 PURGE_ATTRCACHE4(vp
);
12983 recov_statep
->rs_flags
= 0;
12984 recov_statep
->rs_num_retry_despite_err
= 0;
12985 *cred_otw
= nfs4_get_otw_cred(search_cr
, VTOMI4(vp
), NULL
);
12989 * Initialize and allocate the data structures necessary for
12990 * the nfs4frlock call.
12991 * Allocates argsp's op array, frees up the saved_rqstpp if there is one.
12994 nfs4frlock_call_init(COMPOUND4args_clnt
*argsp
, COMPOUND4args_clnt
**argspp
,
12995 nfs_argop4
**argopp
, nfs4_op_hint_t
*op_hintp
, flock64_t
*flk
, int cmd
,
12996 bool_t
*retry
, bool_t
*did_start_fop
, COMPOUND4res_clnt
**respp
,
12997 bool_t
*skip_get_err
, nfs4_lost_rqst_t
*lost_rqstp
)
12999 int argoplist_size
;
13003 *did_start_fop
= FALSE
;
13004 *skip_get_err
= FALSE
;
13005 lost_rqstp
->lr_op
= 0;
13006 argoplist_size
= num_ops
* sizeof (nfs_argop4
);
13007 /* fill array with zero */
13008 *argopp
= kmem_zalloc(argoplist_size
, KM_SLEEP
);
13013 argsp
->array_len
= num_ops
;
13014 argsp
->array
= *argopp
;
13016 /* initialize in case of error; will get real value down below */
13017 argsp
->ctag
= TAG_NONE
;
13019 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) && flk
->l_type
== F_UNLCK
)
13020 *op_hintp
= OH_LOCKU
;
13022 *op_hintp
= OH_OTHER
;
13026 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13027 * the proper nfs4_server_t for this instance of nfs4frlock.
13028 * Returns 0 (success) or an errno value.
13031 nfs4frlock_start_call(nfs4_lock_call_type_t ctype
, vnode_t
*vp
,
13032 nfs4_op_hint_t op_hint
, nfs4_recov_state_t
*recov_statep
,
13033 bool_t
*did_start_fop
, bool_t
*startrecovp
)
13038 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13040 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
13041 error
= nfs4_start_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
13042 recov_statep
, startrecovp
);
13045 *did_start_fop
= TRUE
;
13047 *did_start_fop
= FALSE
;
13048 *startrecovp
= FALSE
;
13054 /* If the file failed recovery, just quit. */
13055 mutex_enter(&rp
->r_statelock
);
13056 if (rp
->r_flags
& R4RECOVERR
) {
13059 mutex_exit(&rp
->r_statelock
);
13066 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13067 * resend nfs4frlock call is initiated by the recovery framework.
13068 * Acquires the lop and oop seqid synchronization.
13071 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t
*resend_rqstp
,
13072 COMPOUND4args_clnt
*argsp
, nfs_argop4
*argop
, nfs4_lock_owner_t
**lopp
,
13073 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13074 LOCK4args
**lock_argsp
, LOCKU4args
**locku_argsp
)
13076 mntinfo4_t
*mi
= VTOMI4(resend_rqstp
->lr_vp
);
13079 NFS4_DEBUG((nfs4_lost_rqst_debug
|| nfs4_client_lock_debug
),
13081 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13082 ASSERT(resend_rqstp
!= NULL
);
13083 ASSERT(resend_rqstp
->lr_op
== OP_LOCK
||
13084 resend_rqstp
->lr_op
== OP_LOCKU
);
13086 *oopp
= resend_rqstp
->lr_oop
;
13087 if (resend_rqstp
->lr_oop
) {
13088 open_owner_hold(resend_rqstp
->lr_oop
);
13089 error
= nfs4_start_open_seqid_sync(resend_rqstp
->lr_oop
, mi
);
13090 ASSERT(error
== 0); /* recov thread always succeeds */
13093 /* Must resend this lost lock/locku request. */
13094 ASSERT(resend_rqstp
->lr_lop
!= NULL
);
13095 *lopp
= resend_rqstp
->lr_lop
;
13096 lock_owner_hold(resend_rqstp
->lr_lop
);
13097 error
= nfs4_start_lock_seqid_sync(resend_rqstp
->lr_lop
, mi
);
13098 ASSERT(error
== 0); /* recov thread always succeeds */
13100 *ospp
= resend_rqstp
->lr_osp
;
13102 open_stream_hold(resend_rqstp
->lr_osp
);
13104 if (resend_rqstp
->lr_op
== OP_LOCK
) {
13105 LOCK4args
*lock_args
;
13107 argop
->argop
= OP_LOCK
;
13108 *lock_argsp
= lock_args
= &argop
->nfs_argop4_u
.oplock
;
13109 lock_args
->locktype
= resend_rqstp
->lr_locktype
;
13110 lock_args
->reclaim
=
13111 (resend_rqstp
->lr_ctype
== NFS4_LCK_CTYPE_RECLAIM
);
13112 lock_args
->offset
= resend_rqstp
->lr_flk
->l_start
;
13113 lock_args
->length
= resend_rqstp
->lr_flk
->l_len
;
13114 if (lock_args
->length
== 0)
13115 lock_args
->length
= ~lock_args
->length
;
13116 nfs4_setup_lock_args(*lopp
, *oopp
, *ospp
,
13117 mi2clientid(mi
), &lock_args
->locker
);
13119 switch (resend_rqstp
->lr_ctype
) {
13120 case NFS4_LCK_CTYPE_RESEND
:
13121 argsp
->ctag
= TAG_LOCK_RESEND
;
13123 case NFS4_LCK_CTYPE_REINSTATE
:
13124 argsp
->ctag
= TAG_LOCK_REINSTATE
;
13126 case NFS4_LCK_CTYPE_RECLAIM
:
13127 argsp
->ctag
= TAG_LOCK_RECLAIM
;
13130 argsp
->ctag
= TAG_LOCK_UNKNOWN
;
13134 LOCKU4args
*locku_args
;
13135 nfs4_lock_owner_t
*lop
= resend_rqstp
->lr_lop
;
13137 argop
->argop
= OP_LOCKU
;
13138 *locku_argsp
= locku_args
= &argop
->nfs_argop4_u
.oplocku
;
13139 locku_args
->locktype
= READ_LT
;
13140 locku_args
->seqid
= lop
->lock_seqid
+ 1;
13141 mutex_enter(&lop
->lo_lock
);
13142 locku_args
->lock_stateid
= lop
->lock_stateid
;
13143 mutex_exit(&lop
->lo_lock
);
13144 locku_args
->offset
= resend_rqstp
->lr_flk
->l_start
;
13145 locku_args
->length
= resend_rqstp
->lr_flk
->l_len
;
13146 if (locku_args
->length
== 0)
13147 locku_args
->length
= ~locku_args
->length
;
13149 switch (resend_rqstp
->lr_ctype
) {
13150 case NFS4_LCK_CTYPE_RESEND
:
13151 argsp
->ctag
= TAG_LOCKU_RESEND
;
13153 case NFS4_LCK_CTYPE_REINSTATE
:
13154 argsp
->ctag
= TAG_LOCKU_REINSTATE
;
13157 argsp
->ctag
= TAG_LOCK_UNKNOWN
;
13164 * Setup the LOCKT4 arguments.
13167 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype
, nfs_argop4
*argop
,
13168 LOCKT4args
**lockt_argsp
, COMPOUND4args_clnt
*argsp
, flock64_t
*flk
,
13171 LOCKT4args
*lockt_args
;
13173 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp
))->mi_zone
);
13174 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13175 argop
->argop
= OP_LOCKT
;
13176 argsp
->ctag
= TAG_LOCKT
;
13177 lockt_args
= &argop
->nfs_argop4_u
.oplockt
;
13180 * The locktype will be READ_LT unless it's
13181 * a write lock. We do this because the Solaris
13182 * system call allows the combination of
13183 * F_UNLCK and F_GETLK* and so in that case the
13184 * unlock is mapped to a read.
13186 if (flk
->l_type
== F_WRLCK
)
13187 lockt_args
->locktype
= WRITE_LT
;
13189 lockt_args
->locktype
= READ_LT
;
13191 lockt_args
->owner
.clientid
= mi2clientid(VTOMI4(RTOV4(rp
)));
13192 /* set the lock owner4 args */
13193 nfs4_setlockowner_args(&lockt_args
->owner
, rp
,
13194 ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pidp
->pid_id
:
13196 lockt_args
->offset
= flk
->l_start
;
13197 lockt_args
->length
= flk
->l_len
;
13198 if (flk
->l_len
== 0)
13199 lockt_args
->length
= ~lockt_args
->length
;
13201 *lockt_argsp
= lockt_args
;
13205 * If the client is holding a delegation, and the open stream to be used
13206 * with this lock request is a delegation open stream, then re-open the stream.
13207 * Sets the nfs4_error_t to all zeros unless the open stream has already
13208 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13209 * means the caller should retry (like a recovery retry).
13212 nfs4frlock_check_deleg(vnode_t
*vp
, nfs4_error_t
*ep
, cred_t
*cr
, int lt
)
13214 open_delegation_type4 dt
;
13215 bool_t reopen_needed
, force
;
13216 nfs4_open_stream_t
*osp
;
13217 open_claim_type4 oclaim
;
13218 rnode4_t
*rp
= VTOR4(vp
);
13219 mntinfo4_t
*mi
= VTOMI4(vp
);
13221 ASSERT(nfs_zone() == mi
->mi_zone
);
13223 nfs4_error_zinit(ep
);
13225 mutex_enter(&rp
->r_statev4_lock
);
13226 dt
= rp
->r_deleg_type
;
13227 mutex_exit(&rp
->r_statev4_lock
);
13229 if (dt
!= OPEN_DELEGATE_NONE
) {
13230 nfs4_open_owner_t
*oop
;
13232 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
13234 ep
->stat
= NFS4ERR_IO
;
13237 /* returns with 'os_sync_lock' held */
13238 osp
= find_open_stream(oop
, rp
);
13240 open_owner_rele(oop
);
13241 ep
->stat
= NFS4ERR_IO
;
13245 if (osp
->os_failed_reopen
) {
13246 NFS4_DEBUG((nfs4_open_stream_debug
||
13247 nfs4_client_lock_debug
), (CE_NOTE
,
13248 "nfs4frlock_check_deleg: os_failed_reopen set "
13249 "for osp %p, cr %p, rp %s", (void *)osp
,
13250 (void *)cr
, rnode4info(rp
)));
13251 mutex_exit(&osp
->os_sync_lock
);
13252 open_stream_rele(osp
, rp
);
13253 open_owner_rele(oop
);
13254 ep
->stat
= NFS4ERR_IO
;
13259 * Determine whether a reopen is needed. If this
13260 * is a delegation open stream, then send the open
13261 * to the server to give visibility to the open owner.
13262 * Even if it isn't a delegation open stream, we need
13263 * to check if the previous open CLAIM_DELEGATE_CUR
13267 reopen_needed
= osp
->os_delegation
||
13269 !(osp
->os_dc_openacc
& OPEN4_SHARE_ACCESS_READ
)) ||
13271 !(osp
->os_dc_openacc
& OPEN4_SHARE_ACCESS_WRITE
)));
13273 mutex_exit(&osp
->os_sync_lock
);
13274 open_owner_rele(oop
);
13276 if (reopen_needed
) {
13278 * Always use CLAIM_PREVIOUS after server reboot.
13279 * The server will reject CLAIM_DELEGATE_CUR if
13280 * it is used during the grace period.
13282 mutex_enter(&mi
->mi_lock
);
13283 if (mi
->mi_recovflags
& MI4R_SRV_REBOOT
) {
13284 oclaim
= CLAIM_PREVIOUS
;
13287 oclaim
= CLAIM_DELEGATE_CUR
;
13290 mutex_exit(&mi
->mi_lock
);
13292 nfs4_reopen(vp
, osp
, ep
, oclaim
, force
, FALSE
);
13293 if (ep
->error
== EAGAIN
) {
13294 nfs4_error_zinit(ep
);
13295 ep
->stat
= NFS4ERR_DELAY
;
13298 open_stream_rele(osp
, rp
);
13304 * Setup the LOCKU4 arguments.
13305 * Returns errors via the nfs4_error_t.
13306 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13307 * over-the-wire. The caller must release the
13308 * reference on *lopp.
13309 * NFS4ERR_DELAY caller should retry (like recovery retry)
13310 * (other) unrecoverable error.
13313 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype
, nfs_argop4
*argop
,
13314 LOCKU4args
**locku_argsp
, flock64_t
*flk
,
13315 nfs4_lock_owner_t
**lopp
, nfs4_error_t
*ep
, COMPOUND4args_clnt
*argsp
,
13316 vnode_t
*vp
, int flag
, u_offset_t offset
, cred_t
*cr
,
13317 bool_t
*skip_get_err
, bool_t
*go_otwp
)
13319 nfs4_lock_owner_t
*lop
= NULL
;
13320 LOCKU4args
*locku_args
;
13322 bool_t is_spec
= FALSE
;
13323 rnode4_t
*rp
= VTOR4(vp
);
13325 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13326 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13328 nfs4frlock_check_deleg(vp
, ep
, cr
, F_UNLCK
);
13329 if (ep
->error
|| ep
->stat
)
13332 argop
->argop
= OP_LOCKU
;
13333 if (ctype
== NFS4_LCK_CTYPE_REINSTATE
)
13334 argsp
->ctag
= TAG_LOCKU_REINSTATE
;
13336 argsp
->ctag
= TAG_LOCKU
;
13337 locku_args
= &argop
->nfs_argop4_u
.oplocku
;
13338 *locku_argsp
= locku_args
;
13341 * XXX what should locku_args->locktype be?
13342 * setting to ALWAYS be READ_LT so at least
13343 * it is a valid locktype.
13346 locku_args
->locktype
= READ_LT
;
13348 pid
= ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pidp
->pid_id
:
13352 * Get the lock owner stateid. If no lock owner
13353 * exists, return success.
13355 lop
= find_lock_owner(rp
, pid
, LOWN_ANY
);
13357 if (lop
&& CLNT_ISSPECIAL(&lop
->lock_stateid
))
13359 if (!lop
|| is_spec
) {
13361 * No lock owner so no locks to unlock.
13362 * Return success. If there was a failed
13363 * reclaim earlier, the lock might still be
13364 * registered with the local locking code,
13365 * so notify it of the unlock.
13367 * If the lockowner is using a special stateid,
13368 * then the original lock request (that created
13369 * this lockowner) was never successful, so we
13370 * have no lock to undo OTW.
13372 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13373 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13374 "(%ld) so return success", (long)pid
));
13376 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13377 flk
->l_pid
= curproc
->p_pid
;
13378 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
13380 * Release our hold and NULL out so final_cleanup
13381 * doesn't try to end a lock seqid sync we
13385 lock_owner_rele(lop
);
13388 *skip_get_err
= TRUE
;
13393 ep
->error
= nfs4_start_lock_seqid_sync(lop
, VTOMI4(vp
));
13394 if (ep
->error
== EAGAIN
) {
13395 lock_owner_rele(lop
);
13400 mutex_enter(&lop
->lo_lock
);
13401 locku_args
->lock_stateid
= lop
->lock_stateid
;
13402 mutex_exit(&lop
->lo_lock
);
13403 locku_args
->seqid
= lop
->lock_seqid
+ 1;
13405 /* leave the ref count on lop, rele after RPC call */
13407 locku_args
->offset
= flk
->l_start
;
13408 locku_args
->length
= flk
->l_len
;
13409 if (flk
->l_len
== 0)
13410 locku_args
->length
= ~locku_args
->length
;
13416 * Setup the LOCK4 arguments.
13418 * Returns errors via the nfs4_error_t.
13419 * NFS4_OK no problems
13420 * NFS4ERR_DELAY caller should retry (like recovery retry)
13421 * (other) unrecoverable error
13424 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype
, LOCK4args
**lock_argsp
,
13425 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13426 nfs4_lock_owner_t
**lopp
, nfs_argop4
*argop
, COMPOUND4args_clnt
*argsp
,
13427 flock64_t
*flk
, int cmd
, vnode_t
*vp
, cred_t
*cr
, nfs4_error_t
*ep
)
13429 LOCK4args
*lock_args
;
13430 nfs4_open_owner_t
*oop
= NULL
;
13431 nfs4_open_stream_t
*osp
= NULL
;
13432 nfs4_lock_owner_t
*lop
= NULL
;
13434 rnode4_t
*rp
= VTOR4(vp
);
13436 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13438 nfs4frlock_check_deleg(vp
, ep
, cr
, flk
->l_type
);
13439 if (ep
->error
|| ep
->stat
!= NFS4_OK
)
13442 argop
->argop
= OP_LOCK
;
13443 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13444 argsp
->ctag
= TAG_LOCK
;
13445 else if (ctype
== NFS4_LCK_CTYPE_RECLAIM
)
13446 argsp
->ctag
= TAG_RELOCK
;
13448 argsp
->ctag
= TAG_LOCK_REINSTATE
;
13449 lock_args
= &argop
->nfs_argop4_u
.oplock
;
13450 lock_args
->locktype
= flk_to_locktype(cmd
, flk
->l_type
);
13451 lock_args
->reclaim
= ctype
== NFS4_LCK_CTYPE_RECLAIM
? 1 : 0;
13453 * Get the lock owner. If no lock owner exists,
13454 * create a 'temporary' one and grab the open seqid
13455 * synchronization (which puts a hold on the open
13456 * owner and open stream).
13457 * This also grabs the lock seqid synchronization.
13459 pid
= ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pid
: flk
->l_pid
;
13461 nfs4_find_or_create_lock_owner(pid
, rp
, cr
, &oop
, &osp
, &lop
);
13463 if (ep
->stat
!= NFS4_OK
)
13466 nfs4_setup_lock_args(lop
, oop
, osp
, mi2clientid(VTOMI4(vp
)),
13467 &lock_args
->locker
);
13469 lock_args
->offset
= flk
->l_start
;
13470 lock_args
->length
= flk
->l_len
;
13471 if (flk
->l_len
== 0)
13472 lock_args
->length
= ~lock_args
->length
;
13473 *lock_argsp
= lock_args
;
13481 * After we get the reply from the server, record the proper information
13482 * for possible resend lock requests.
13484 * Allocates memory for the saved_rqstp if we have a lost lock to save.
13487 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype
, int error
,
13488 nfs_lock_type4 locktype
, nfs4_open_owner_t
*oop
,
13489 nfs4_open_stream_t
*osp
, nfs4_lock_owner_t
*lop
, flock64_t
*flk
,
13490 nfs4_lost_rqst_t
*lost_rqstp
, cred_t
*cr
, vnode_t
*vp
)
13492 bool_t unlock
= (flk
->l_type
== F_UNLCK
);
13494 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13495 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
||
13496 ctype
== NFS4_LCK_CTYPE_REINSTATE
);
13498 if (error
!= 0 && !unlock
) {
13499 NFS4_DEBUG((nfs4_lost_rqst_debug
||
13500 nfs4_client_lock_debug
), (CE_NOTE
,
13501 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13502 " for lop %p", (void *)lop
));
13503 ASSERT(lop
!= NULL
);
13504 mutex_enter(&lop
->lo_lock
);
13505 lop
->lo_pending_rqsts
= 1;
13506 mutex_exit(&lop
->lo_lock
);
13509 lost_rqstp
->lr_putfirst
= FALSE
;
13510 lost_rqstp
->lr_op
= 0;
13513 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13514 * recovery purposes so that the lock request that was sent
13515 * can be saved and re-issued later. Ditto for EIO from a forced
13516 * unmount. This is done to have the client's local locking state
13517 * match the v4 server's state; that is, the request was
13518 * potentially received and accepted by the server but the client
13519 * thinks it was not.
13521 if (error
== ETIMEDOUT
|| error
== EINTR
||
13522 NFS4_FRC_UNMT_ERR(error
, vp
->v_vfsp
)) {
13523 NFS4_DEBUG((nfs4_lost_rqst_debug
||
13524 nfs4_client_lock_debug
), (CE_NOTE
,
13525 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13526 "lop %p oop %p osp %p", unlock
? "LOCKU" : "LOCK",
13527 (void *)lop
, (void *)oop
, (void *)osp
));
13529 lost_rqstp
->lr_op
= OP_LOCKU
;
13531 lost_rqstp
->lr_op
= OP_LOCK
;
13532 lost_rqstp
->lr_locktype
= locktype
;
13535 * Objects are held and rele'd via the recovery code.
13536 * See nfs4_save_lost_rqst.
13538 lost_rqstp
->lr_vp
= vp
;
13539 lost_rqstp
->lr_dvp
= NULL
;
13540 lost_rqstp
->lr_oop
= oop
;
13541 lost_rqstp
->lr_osp
= osp
;
13542 lost_rqstp
->lr_lop
= lop
;
13543 lost_rqstp
->lr_cr
= cr
;
13545 case NFS4_LCK_CTYPE_NORM
:
13546 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
13547 lost_rqstp
->lr_ctype
= NFS4_LCK_CTYPE_RESEND
;
13549 case NFS4_LCK_CTYPE_REINSTATE
:
13550 lost_rqstp
->lr_putfirst
= TRUE
;
13551 lost_rqstp
->lr_ctype
= ctype
;
13556 lost_rqstp
->lr_flk
= flk
;
13561 * Update lop's seqid. Also update the seqid stored in a resend request,
13562 * if any. (Some recovery errors increment the seqid, and we may have to
13563 * send the resend request again.)
13567 nfs4frlock_bump_seqid(LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13568 nfs4_open_owner_t
*oop
, nfs4_lock_owner_t
*lop
, nfs4_tag_type_t tag_type
)
13571 if (lock_args
->locker
.new_lock_owner
== TRUE
)
13572 nfs4_get_and_set_next_open_seqid(oop
, tag_type
);
13574 ASSERT(lop
->lo_flags
& NFS4_LOCK_SEQID_INUSE
);
13575 nfs4_set_lock_seqid(lop
->lock_seqid
+ 1, lop
);
13577 } else if (locku_args
) {
13578 ASSERT(lop
->lo_flags
& NFS4_LOCK_SEQID_INUSE
);
13579 nfs4_set_lock_seqid(lop
->lock_seqid
+1, lop
);
13584 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13585 * COMPOUND4 args/res for calls that need to retry.
13586 * Switches the *cred_otwp to base_cr.
13589 nfs4frlock_check_access(vnode_t
*vp
, nfs4_op_hint_t op_hint
,
13590 nfs4_recov_state_t
*recov_statep
, int needrecov
, bool_t
*did_start_fop
,
13591 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
, int error
,
13592 nfs4_lock_owner_t
**lopp
, nfs4_open_owner_t
**oopp
,
13593 nfs4_open_stream_t
**ospp
, cred_t
*base_cr
, cred_t
**cred_otwp
)
13595 nfs4_open_owner_t
*oop
= *oopp
;
13596 nfs4_open_stream_t
*osp
= *ospp
;
13597 nfs4_lock_owner_t
*lop
= *lopp
;
13598 nfs_argop4
*argop
= (*argspp
)->array
;
13600 if (*did_start_fop
) {
13601 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
, recov_statep
,
13603 *did_start_fop
= FALSE
;
13605 ASSERT((*argspp
)->array_len
== 2);
13606 if (argop
[1].argop
== OP_LOCK
)
13607 nfs4args_lock_free(&argop
[1]);
13608 else if (argop
[1].argop
== OP_LOCKT
)
13609 nfs4args_lockt_free(&argop
[1]);
13610 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13612 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)*respp
);
13617 nfs4_end_lock_seqid_sync(lop
);
13618 lock_owner_rele(lop
);
13622 /* need to free up the reference on osp for lock args */
13624 open_stream_rele(osp
, VTOR4(vp
));
13628 /* need to free up the reference on oop for lock args */
13630 nfs4_end_open_seqid_sync(oop
);
13631 open_owner_rele(oop
);
13635 crfree(*cred_otwp
);
13636 *cred_otwp
= base_cr
;
13637 crhold(*cred_otwp
);
13641 * Function to process the client's recovery for nfs4frlock.
13642 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13644 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13645 * COMPOUND4 args/res for calls that need to retry.
13647 * Note: the rp's r_lkserlock is *not* dropped during this path.
13650 nfs4frlock_recovery(int needrecov
, nfs4_error_t
*ep
,
13651 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
,
13652 LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13653 nfs4_open_owner_t
**oopp
, nfs4_open_stream_t
**ospp
,
13654 nfs4_lock_owner_t
**lopp
, rnode4_t
*rp
, vnode_t
*vp
,
13655 nfs4_recov_state_t
*recov_statep
, nfs4_op_hint_t op_hint
,
13656 bool_t
*did_start_fop
, nfs4_lost_rqst_t
*lost_rqstp
, flock64_t
*flk
)
13658 nfs4_open_owner_t
*oop
= *oopp
;
13659 nfs4_open_stream_t
*osp
= *ospp
;
13660 nfs4_lock_owner_t
*lop
= *lopp
;
13662 bool_t abort
, retry
;
13664 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13665 ASSERT((*argspp
) != NULL
);
13666 ASSERT((*respp
) != NULL
);
13667 if (lock_args
|| locku_args
)
13668 ASSERT(lop
!= NULL
);
13670 NFS4_DEBUG((nfs4_client_lock_debug
|| nfs4_client_recov_debug
),
13671 (CE_NOTE
, "nfs4frlock_recovery: initiating recovery\n"));
13676 nfs4_bseqid_entry_t
*bsep
= NULL
;
13679 op
= lock_args
? OP_LOCK
: locku_args
? OP_LOCKU
: OP_LOCKT
;
13681 if (!ep
->error
&& ep
->stat
== NFS4ERR_BAD_SEQID
) {
13685 if (lock_args
->locker
.new_lock_owner
== TRUE
)
13686 seqid
= lock_args
->locker
.locker4_u
.
13687 open_owner
.open_seqid
;
13689 seqid
= lock_args
->locker
.locker4_u
.
13690 lock_owner
.lock_seqid
;
13691 } else if (locku_args
) {
13692 seqid
= locku_args
->seqid
;
13697 bsep
= nfs4_create_bseqid_entry(oop
, lop
, vp
,
13698 flk
->l_pid
, (*argspp
)->ctag
, seqid
);
13701 abort
= nfs4_start_recovery(ep
, VTOMI4(vp
), vp
, NULL
, NULL
,
13702 (lost_rqstp
&& (lost_rqstp
->lr_op
== OP_LOCK
||
13703 lost_rqstp
->lr_op
== OP_LOCKU
)) ? lost_rqstp
:
13704 NULL
, op
, bsep
, NULL
, NULL
);
13707 kmem_free(bsep
, sizeof (*bsep
));
13711 * Return that we do not want to retry the request for 3 cases:
13712 * 1. If we received EINTR or are bailing out because of a forced
13713 * unmount, we came into this code path just for the sake of
13714 * initiating recovery, we now need to return the error.
13715 * 2. If we have aborted recovery.
13716 * 3. We received NFS4ERR_BAD_SEQID.
13718 if (ep
->error
== EINTR
|| NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
) ||
13719 abort
== TRUE
|| (ep
->error
== 0 && ep
->stat
== NFS4ERR_BAD_SEQID
))
13722 if (*did_start_fop
== TRUE
) {
13723 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
, recov_statep
,
13725 *did_start_fop
= FALSE
;
13728 if (retry
== TRUE
) {
13731 argop
= (*argspp
)->array
;
13732 ASSERT((*argspp
)->array_len
== 2);
13734 if (argop
[1].argop
== OP_LOCK
)
13735 nfs4args_lock_free(&argop
[1]);
13736 else if (argop
[1].argop
== OP_LOCKT
)
13737 nfs4args_lockt_free(&argop
[1]);
13738 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13740 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)*respp
);
13746 nfs4_end_lock_seqid_sync(lop
);
13747 lock_owner_rele(lop
);
13752 /* need to free up the reference on osp for lock args */
13754 open_stream_rele(osp
, rp
);
13758 /* need to free up the reference on oop for lock args */
13760 nfs4_end_open_seqid_sync(oop
);
13761 open_owner_rele(oop
);
13769 * Handles the successful reply from the server for nfs4frlock.
13772 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype
, int cmd
, flock64_t
*flk
,
13773 vnode_t
*vp
, int flag
, u_offset_t offset
,
13774 nfs4_lost_rqst_t
*resend_rqstp
)
13776 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13777 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) &&
13778 (flk
->l_type
== F_RDLCK
|| flk
->l_type
== F_WRLCK
)) {
13779 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
13780 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
13782 * We do not register lost locks locally in
13783 * the 'resend' case since the user/application
13784 * doesn't think we have the lock.
13786 ASSERT(!resend_rqstp
);
13787 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
13793 * Handle the DENIED reply from the server for nfs4frlock.
13794 * Returns TRUE if we should retry the request; FALSE otherwise.
13796 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13797 * COMPOUND4 args/res for calls that need to retry. Can also
13798 * drop and regrab the r_lkserlock.
13801 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype
, LOCK4args
*lock_args
,
13802 LOCKT4args
*lockt_args
, nfs4_open_owner_t
**oopp
,
13803 nfs4_open_stream_t
**ospp
, nfs4_lock_owner_t
**lopp
, int cmd
,
13804 vnode_t
*vp
, flock64_t
*flk
, nfs4_op_hint_t op_hint
,
13805 nfs4_recov_state_t
*recov_statep
, int needrecov
,
13806 COMPOUND4args_clnt
**argspp
, COMPOUND4res_clnt
**respp
,
13807 clock_t *tick_delayp
, short *whencep
, int *errorp
,
13808 nfs_resop4
*resop
, cred_t
*cr
, bool_t
*did_start_fop
,
13809 bool_t
*skip_get_err
)
13811 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13814 nfs4_open_owner_t
*oop
= *oopp
;
13815 nfs4_open_stream_t
*osp
= *ospp
;
13816 nfs4_lock_owner_t
*lop
= *lopp
;
13820 * Blocking lock needs to sleep and retry from the request.
13822 * Do not block and wait for 'resend' or 'reinstate'
13823 * lock requests, just return the error.
13825 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13827 if (cmd
== F_SETLKW
) {
13828 rnode4_t
*rp
= VTOR4(vp
);
13829 nfs_argop4
*argop
= (*argspp
)->array
;
13831 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
13833 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
13834 recov_statep
, needrecov
);
13835 *did_start_fop
= FALSE
;
13836 ASSERT((*argspp
)->array_len
== 2);
13837 if (argop
[1].argop
== OP_LOCK
)
13838 nfs4args_lock_free(&argop
[1]);
13839 else if (argop
[1].argop
== OP_LOCKT
)
13840 nfs4args_lockt_free(&argop
[1]);
13841 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
13843 (void) xdr_free(xdr_COMPOUND4res_clnt
,
13847 nfs4_end_lock_seqid_sync(lop
);
13848 lock_owner_rele(lop
);
13851 open_stream_rele(osp
, rp
);
13855 nfs4_end_open_seqid_sync(oop
);
13856 open_owner_rele(oop
);
13860 nfs_rw_exit(&rp
->r_lkserlock
);
13862 intr
= nfs4_block_and_wait(tick_delayp
, rp
);
13865 (void) nfs_rw_enter_sig(&rp
->r_lkserlock
,
13871 (void) nfs_rw_enter_sig(&rp
->r_lkserlock
,
13875 * Make sure we are still safe to lock with
13876 * regards to mmapping.
13878 if (!nfs4_safelock(vp
, flk
, cr
)) {
13885 if (ctype
== NFS4_LCK_CTYPE_NORM
)
13887 *skip_get_err
= TRUE
;
13891 } else if (lockt_args
) {
13892 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13893 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13895 denied_to_flk(&resop
->nfs_resop4_u
.oplockt
.denied
,
13898 /* according to NLM code */
13901 *skip_get_err
= TRUE
;
13908 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13911 nfs4frlock_results_default(COMPOUND4res_clnt
*resp
, int *errorp
)
13913 switch (resp
->status
) {
13914 case NFS4ERR_ACCESS
:
13915 case NFS4ERR_ADMIN_REVOKED
:
13916 case NFS4ERR_BADHANDLE
:
13917 case NFS4ERR_BAD_RANGE
:
13918 case NFS4ERR_BAD_SEQID
:
13919 case NFS4ERR_BAD_STATEID
:
13920 case NFS4ERR_BADXDR
:
13921 case NFS4ERR_DEADLOCK
:
13922 case NFS4ERR_DELAY
:
13923 case NFS4ERR_EXPIRED
:
13924 case NFS4ERR_FHEXPIRED
:
13925 case NFS4ERR_GRACE
:
13926 case NFS4ERR_INVAL
:
13927 case NFS4ERR_ISDIR
:
13928 case NFS4ERR_LEASE_MOVED
:
13929 case NFS4ERR_LOCK_NOTSUPP
:
13930 case NFS4ERR_LOCK_RANGE
:
13931 case NFS4ERR_MOVED
:
13932 case NFS4ERR_NOFILEHANDLE
:
13933 case NFS4ERR_NO_GRACE
:
13934 case NFS4ERR_OLD_STATEID
:
13935 case NFS4ERR_OPENMODE
:
13936 case NFS4ERR_RECLAIM_BAD
:
13937 case NFS4ERR_RECLAIM_CONFLICT
:
13938 case NFS4ERR_RESOURCE
:
13939 case NFS4ERR_SERVERFAULT
:
13940 case NFS4ERR_STALE
:
13941 case NFS4ERR_STALE_CLIENTID
:
13942 case NFS4ERR_STALE_STATEID
:
13945 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
13946 "nfs4frlock_results_default: got unrecognizable "
13947 "res.status %d", resp
->status
));
13948 *errorp
= NFS4ERR_INVAL
;
13953 * The lock request was successful, so update the client's state.
13956 nfs4frlock_update_state(LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
13957 LOCKT4args
*lockt_args
, nfs_resop4
*resop
, nfs4_lock_owner_t
*lop
,
13958 vnode_t
*vp
, flock64_t
*flk
, cred_t
*cr
,
13959 nfs4_lost_rqst_t
*resend_rqstp
)
13961 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
13964 LOCK4res
*lock_res
;
13966 lock_res
= &resop
->nfs_resop4_u
.oplock
;
13967 /* update the stateid with server's response */
13969 if (lock_args
->locker
.new_lock_owner
== TRUE
) {
13970 mutex_enter(&lop
->lo_lock
);
13971 lop
->lo_just_created
= NFS4_PERM_CREATED
;
13972 mutex_exit(&lop
->lo_lock
);
13975 nfs4_set_lock_stateid(lop
, lock_res
->LOCK4res_u
.lock_stateid
);
13978 * If the lock was the result of a resending a lost
13979 * request, we've synched up the stateid and seqid
13980 * with the server, but now the server might be out of sync
13981 * with what the application thinks it has for locks.
13982 * Clean that up here. It's unclear whether we should do
13983 * this even if the filesystem has been forcibly unmounted.
13984 * For most servers, it's probably wasted effort, but
13985 * RFC3530 lets servers require that unlocks exactly match
13986 * the locks that are held.
13988 if (resend_rqstp
!= NULL
&&
13989 resend_rqstp
->lr_ctype
!= NFS4_LCK_CTYPE_REINSTATE
) {
13990 nfs4_reinstitute_local_lock_state(vp
, flk
, cr
, lop
);
13994 } else if (locku_args
) {
13995 LOCKU4res
*locku_res
;
13997 locku_res
= &resop
->nfs_resop4_u
.oplocku
;
13999 /* Update the stateid with the server's response */
14000 nfs4_set_lock_stateid(lop
, locku_res
->lock_stateid
);
14001 } else if (lockt_args
) {
14002 /* Switch the lock type to express success, see fcntl */
14003 flk
->l_type
= F_UNLCK
;
14009 * Do final cleanup before exiting nfs4frlock.
14010 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14011 * COMPOUND4 args/res for calls that haven't already.
14014 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype
, COMPOUND4args_clnt
*argsp
,
14015 COMPOUND4res_clnt
*resp
, vnode_t
*vp
, nfs4_op_hint_t op_hint
,
14016 nfs4_recov_state_t
*recov_statep
, int needrecov
, nfs4_open_owner_t
*oop
,
14017 nfs4_open_stream_t
*osp
, nfs4_lock_owner_t
*lop
, flock64_t
*flk
,
14018 short whence
, u_offset_t offset
, struct lm_sysid
*ls
,
14019 int *errorp
, LOCK4args
*lock_args
, LOCKU4args
*locku_args
,
14020 bool_t did_start_fop
, bool_t skip_get_err
,
14021 cred_t
*cred_otw
, cred_t
*cred
)
14023 mntinfo4_t
*mi
= VTOMI4(vp
);
14024 rnode4_t
*rp
= VTOR4(vp
);
14025 int error
= *errorp
;
14027 int do_flush_pages
= 0;
14029 ASSERT(nfs_zone() == mi
->mi_zone
);
14031 * The client recovery code wants the raw status information,
14032 * so don't map the NFS status code to an errno value for
14033 * non-normal call types.
14035 if (ctype
== NFS4_LCK_CTYPE_NORM
) {
14036 if (*errorp
== 0 && resp
!= NULL
&& skip_get_err
== FALSE
)
14037 *errorp
= geterrno4(resp
->status
);
14038 if (did_start_fop
== TRUE
)
14039 nfs4_end_fop(mi
, vp
, NULL
, op_hint
, recov_statep
,
14043 * We've established a new lock on the server, so invalidate
14044 * the pages associated with the vnode to get the most up to
14045 * date pages from the server after acquiring the lock. We
14046 * want to be sure that the read operation gets the newest data.
14048 * We used to do this in nfs4frlock_results_ok but that doesn't
14049 * work since VOP_PUTPAGE can call nfs4_commit which calls
14050 * nfs4_start_fop. We flush the pages below after calling
14051 * nfs4_end_fop above
14052 * The flush of the page cache must be done after
14053 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14055 if (!error
&& resp
&& resp
->status
== NFS4_OK
)
14056 do_flush_pages
= 1;
14059 ASSERT(argsp
->array_len
== 2);
14060 argop
= argsp
->array
;
14061 if (argop
[1].argop
== OP_LOCK
)
14062 nfs4args_lock_free(&argop
[1]);
14063 else if (argop
[1].argop
== OP_LOCKT
)
14064 nfs4args_lockt_free(&argop
[1]);
14065 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
14067 (void) xdr_free(xdr_COMPOUND4res_clnt
, (caddr_t
)resp
);
14070 /* free the reference on the lock owner */
14072 nfs4_end_lock_seqid_sync(lop
);
14073 lock_owner_rele(lop
);
14076 /* need to free up the reference on osp for lock args */
14078 open_stream_rele(osp
, rp
);
14080 /* need to free up the reference on oop for lock args */
14082 nfs4_end_open_seqid_sync(oop
);
14083 open_owner_rele(oop
);
14086 if (do_flush_pages
)
14087 nfs4_flush_pages(vp
, cred
);
14089 (void) convoff(vp
, flk
, whence
, offset
);
14094 * Record debug information in the event we get EINVAL.
14096 mutex_enter(&mi
->mi_lock
);
14097 if (*errorp
== EINVAL
&& (lock_args
|| locku_args
) &&
14098 (!(mi
->mi_flags
& MI4_POSIX_LOCK
))) {
14099 if (!(mi
->mi_flags
& MI4_LOCK_DEBUG
)) {
14100 zcmn_err(getzoneid(), CE_NOTE
,
14101 "%s operation failed with "
14102 "EINVAL probably since the server, %s,"
14103 " doesn't support POSIX style locking",
14104 lock_args
? "LOCK" : "LOCKU",
14105 mi
->mi_curr_serv
->sv_hostname
);
14106 mi
->mi_flags
|= MI4_LOCK_DEBUG
;
14109 mutex_exit(&mi
->mi_lock
);
14116 * This calls the server and the local locking code.
14118 * Client locks are registerred locally by oring the sysid with
14119 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14120 * We need to distinguish between the two to avoid collision in case one
14121 * machine is used as both client and server.
14123 * Blocking lock requests will continually retry to acquire the lock
14126 * The ctype is defined as follows:
14127 * NFS4_LCK_CTYPE_NORM: normal lock request.
14129 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14130 * recovery, get the pid from flk instead of curproc, and don't reregister
14131 * the lock locally.
14133 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14134 * that we will use the information passed in via resend_rqstp to setup the
14135 * lock/locku request. This resend is the exact same request as the 'lost
14136 * lock', and is initiated by the recovery framework. A successful resend
14137 * request can initiate one or more reinstate requests.
14139 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14140 * does not trigger additional reinstate requests. This lock call type is
14141 * set for setting the v4 server's locking state back to match what the
14142 * client's local locking state is in the event of a received 'lost lock'.
14144 * Errors are returned via the nfs4_error_t parameter.
14147 nfs4frlock(nfs4_lock_call_type_t ctype
, vnode_t
*vp
, int cmd
, flock64_t
*flk
,
14148 int flag
, u_offset_t offset
, cred_t
*cr
, nfs4_error_t
*ep
,
14149 nfs4_lost_rqst_t
*resend_rqstp
, int *did_reclaimp
)
14151 COMPOUND4args_clnt args
, *argsp
= NULL
;
14152 COMPOUND4res_clnt res
, *resp
= NULL
;
14157 clock_t tick_delay
; /* delay in clock ticks */
14158 struct lm_sysid
*ls
;
14159 LOCK4args
*lock_args
= NULL
;
14160 LOCKU4args
*locku_args
= NULL
;
14161 LOCKT4args
*lockt_args
= NULL
;
14162 nfs4_open_owner_t
*oop
= NULL
;
14163 nfs4_open_stream_t
*osp
= NULL
;
14164 nfs4_lock_owner_t
*lop
= NULL
;
14165 bool_t needrecov
= FALSE
;
14166 nfs4_recov_state_t recov_state
;
14168 nfs4_op_hint_t op_hint
;
14169 nfs4_lost_rqst_t lost_rqst
;
14170 bool_t retry
= FALSE
;
14171 bool_t did_start_fop
= FALSE
;
14172 bool_t skip_get_err
= FALSE
;
14173 cred_t
*cred_otw
= NULL
;
14174 bool_t recovonly
; /* just queue request */
14175 int frc_no_reclaim
= 0;
14180 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14183 name
= fn_name(VTOSV(vp
)->sv_name
);
14184 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4frlock: "
14185 "%s: cmd %d, type %d, offset %llu, start %"PRIx64
", "
14186 "length %"PRIu64
", pid %d, sysid %d, call type %s, "
14187 "resend request %s", name
, cmd
, flk
->l_type
, offset
, flk
->l_start
,
14188 flk
->l_len
, ctype
== NFS4_LCK_CTYPE_NORM
? curproc
->p_pid
:
14189 flk
->l_pid
, flk
->l_sysid
, nfs4frlock_get_call_type(ctype
),
14190 resend_rqstp
? "TRUE" : "FALSE"));
14191 kmem_free(name
, MAXNAMELEN
);
14194 nfs4_error_zinit(ep
);
14195 ep
->error
= nfs4frlock_validate_args(cmd
, flk
, flag
, vp
, offset
);
14198 ep
->error
= nfs4frlock_get_sysid(&ls
, vp
, flk
);
14201 nfs4frlock_pre_setup(&tick_delay
, &recov_state
, flk
, &whence
,
14202 vp
, cr
, &cred_otw
);
14205 nfs4frlock_call_init(&args
, &argsp
, &argop
, &op_hint
, flk
, cmd
,
14206 &retry
, &did_start_fop
, &resp
, &skip_get_err
, &lost_rqst
);
14209 ep
->error
= nfs4frlock_start_call(ctype
, vp
, op_hint
, &recov_state
,
14210 &did_start_fop
, &recovonly
);
14217 * Leave the request for the recovery system to deal with.
14219 ASSERT(ctype
== NFS4_LCK_CTYPE_NORM
);
14220 ASSERT(cmd
!= F_GETLK
);
14221 ASSERT(flk
->l_type
== F_UNLCK
);
14223 nfs4_error_init(ep
, EINTR
);
14225 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14227 nfs4frlock_save_lost_rqst(ctype
, ep
->error
, READ_LT
,
14228 NULL
, NULL
, lop
, flk
, &lost_rqst
, cr
, vp
);
14229 (void) nfs4_start_recovery(ep
,
14230 VTOMI4(vp
), vp
, NULL
, NULL
,
14231 (lost_rqst
.lr_op
== OP_LOCK
||
14232 lost_rqst
.lr_op
== OP_LOCKU
) ?
14233 &lost_rqst
: NULL
, OP_LOCKU
, NULL
, NULL
, NULL
);
14234 lock_owner_rele(lop
);
14237 flk
->l_pid
= curproc
->p_pid
;
14238 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
14242 /* putfh directory fh */
14243 argop
[0].argop
= OP_CPUTFH
;
14244 argop
[0].nfs_argop4_u
.opcputfh
.sfh
= rp
->r_fh
;
14247 * Set up the over-the-wire arguments and get references to the
14251 if (ctype
== NFS4_LCK_CTYPE_RESEND
||
14252 ctype
== NFS4_LCK_CTYPE_REINSTATE
) {
14253 nfs4frlock_setup_resend_lock_args(resend_rqstp
, argsp
,
14254 &argop
[1], &lop
, &oop
, &osp
, &lock_args
, &locku_args
);
14256 bool_t go_otw
= TRUE
;
14258 ASSERT(resend_rqstp
== NULL
);
14263 nfs4frlock_setup_lockt_args(ctype
, &argop
[1],
14264 &lockt_args
, argsp
, flk
, rp
);
14268 if (flk
->l_type
== F_UNLCK
)
14269 nfs4frlock_setup_locku_args(ctype
,
14270 &argop
[1], &locku_args
, flk
,
14272 vp
, flag
, offset
, cr
,
14273 &skip_get_err
, &go_otw
);
14275 nfs4frlock_setup_lock_args(ctype
,
14276 &lock_args
, &oop
, &osp
, &lop
, &argop
[1],
14277 argsp
, flk
, cmd
, vp
, cr
, ep
);
14282 switch (ep
->stat
) {
14285 case NFS4ERR_DELAY
:
14286 /* recov thread never gets this error */
14287 ASSERT(resend_rqstp
== NULL
);
14288 ASSERT(did_start_fop
);
14290 nfs4_end_fop(VTOMI4(vp
), vp
, NULL
, op_hint
,
14291 &recov_state
, TRUE
);
14292 did_start_fop
= FALSE
;
14293 if (argop
[1].argop
== OP_LOCK
)
14294 nfs4args_lock_free(&argop
[1]);
14295 else if (argop
[1].argop
== OP_LOCKT
)
14296 nfs4args_lockt_free(&argop
[1]);
14297 kmem_free(argop
, 2 * sizeof (nfs_argop4
));
14306 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14307 "nfs4_frlock: invalid cmd %d", cmd
));
14308 ep
->error
= EINVAL
;
14316 /* XXX should we use the local reclock as a cache ? */
14318 * Unregister the lock with the local locking code before
14319 * contacting the server. This avoids a potential race where
14320 * another process gets notified that it has been granted a lock
14321 * before we can unregister ourselves locally.
14323 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
) && flk
->l_type
== F_UNLCK
) {
14324 if (ctype
== NFS4_LCK_CTYPE_NORM
)
14325 flk
->l_pid
= ttoproc(curthread
)->p_pid
;
14326 nfs4_register_lock_locally(vp
, flk
, flag
, offset
);
14330 * Send the server the lock request. Continually loop with a delay
14331 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14335 NFS4_DEBUG((nfs4_client_call_debug
|| nfs4_client_lock_debug
),
14337 "nfs4frlock: %s call, rp %s", needrecov
? "recov" : "first",
14340 if (lock_args
&& frc_no_reclaim
) {
14341 ASSERT(ctype
== NFS4_LCK_CTYPE_RECLAIM
);
14342 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14343 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14344 lock_args
->reclaim
= FALSE
;
14352 rfs4call(VTOMI4(vp
), argsp
, resp
, cred_otw
, &doqueue
, 0, ep
);
14354 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14355 "nfs4frlock: error %d, status %d", ep
->error
, resp
->status
));
14357 needrecov
= nfs4_needs_recovery(ep
, TRUE
, vp
->v_vfsp
);
14358 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14359 "nfs4frlock: needrecov %d", needrecov
));
14361 if (ep
->error
== 0 && nfs4_need_to_bump_seqid(resp
))
14362 nfs4frlock_bump_seqid(lock_args
, locku_args
, oop
, lop
,
14366 * Check if one of these mutually exclusive error cases has
14368 * need to swap credentials due to access error
14369 * recovery is needed
14370 * different error (only known case is missing Kerberos ticket)
14373 if ((ep
->error
== EACCES
||
14374 (ep
->error
== 0 && resp
->status
== NFS4ERR_ACCESS
)) &&
14376 nfs4frlock_check_access(vp
, op_hint
, &recov_state
, needrecov
,
14377 &did_start_fop
, &argsp
, &resp
, ep
->error
, &lop
, &oop
, &osp
,
14384 * LOCKT requests don't need to recover from lost
14385 * requests since they don't create/modify state.
14387 if ((ep
->error
== EINTR
||
14388 NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
)) &&
14392 * Do not attempt recovery for requests initiated by
14393 * the recovery framework. Let the framework redrive them.
14395 if (ctype
!= NFS4_LCK_CTYPE_NORM
)
14398 ASSERT(resend_rqstp
== NULL
);
14401 nfs4frlock_save_lost_rqst(ctype
, ep
->error
,
14402 flk_to_locktype(cmd
, flk
->l_type
),
14403 oop
, osp
, lop
, flk
, &lost_rqst
, cred_otw
, vp
);
14405 retry
= nfs4frlock_recovery(needrecov
, ep
, &argsp
,
14406 &resp
, lock_args
, locku_args
, &oop
, &osp
, &lop
,
14407 rp
, vp
, &recov_state
, op_hint
, &did_start_fop
,
14408 cmd
!= F_GETLK
? &lost_rqst
: NULL
, flk
);
14411 ASSERT(oop
== NULL
);
14412 ASSERT(osp
== NULL
);
14413 ASSERT(lop
== NULL
);
14420 * Bail out if have reached this point with ep->error set. Can
14421 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14422 * This happens if Kerberos ticket has expired or has been
14425 if (ep
->error
!= 0)
14429 * Process the reply.
14431 switch (resp
->status
) {
14433 resop
= &resp
->array
[1];
14434 nfs4frlock_results_ok(ctype
, cmd
, flk
, vp
, flag
, offset
,
14437 * Have a successful lock operation, now update state.
14439 nfs4frlock_update_state(lock_args
, locku_args
, lockt_args
,
14440 resop
, lop
, vp
, flk
, cr
, resend_rqstp
);
14443 case NFS4ERR_DENIED
:
14444 resop
= &resp
->array
[1];
14445 retry
= nfs4frlock_results_denied(ctype
, lock_args
, lockt_args
,
14446 &oop
, &osp
, &lop
, cmd
, vp
, flk
, op_hint
,
14447 &recov_state
, needrecov
, &argsp
, &resp
,
14448 &tick_delay
, &whence
, &ep
->error
, resop
, cr
,
14449 &did_start_fop
, &skip_get_err
);
14452 ASSERT(oop
== NULL
);
14453 ASSERT(osp
== NULL
);
14454 ASSERT(lop
== NULL
);
14459 * If the server won't let us reclaim, fall-back to trying to lock
14460 * the file from scratch. Code elsewhere will check the changeinfo
14461 * to ensure the file hasn't been changed.
14463 case NFS4ERR_NO_GRACE
:
14464 if (lock_args
&& lock_args
->reclaim
== TRUE
) {
14465 ASSERT(ctype
== NFS4_LCK_CTYPE_RECLAIM
);
14466 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14467 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14468 frc_no_reclaim
= 1;
14469 /* clean up before retrying */
14471 (void) nfs4frlock_recovery(needrecov
, ep
, &argsp
, &resp
,
14472 lock_args
, locku_args
, &oop
, &osp
, &lop
, rp
, vp
,
14473 &recov_state
, op_hint
, &did_start_fop
, NULL
, flk
);
14479 nfs4frlock_results_default(resp
, &ep
->error
);
14484 * Process and cleanup from error. Make interrupted unlock
14485 * requests look successful, since they will be handled by the
14486 * client recovery code.
14488 nfs4frlock_final_cleanup(ctype
, argsp
, resp
, vp
, op_hint
, &recov_state
,
14489 needrecov
, oop
, osp
, lop
, flk
, whence
, offset
, ls
, &ep
->error
,
14490 lock_args
, locku_args
, did_start_fop
,
14491 skip_get_err
, cred_otw
, cr
);
14493 if (ep
->error
== EINTR
&& flk
->l_type
== F_UNLCK
&&
14494 (cmd
== F_SETLK
|| cmd
== F_SETLKW
))
14501 * Return non-zero if the given lock request can be handled without
14502 * violating the constraints on concurrent mapping and locking.
14506 nfs4_safelock(vnode_t
*vp
, const struct flock64
*bfp
, cred_t
*cr
)
14508 rnode4_t
*rp
= VTOR4(vp
);
14512 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14513 ASSERT(rp
->r_mapcnt
>= 0);
14514 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock %s: "
14515 "(%"PRIx64
", %"PRIx64
"); mapcnt = %ld", bfp
->l_type
== F_WRLCK
?
14516 "write" : bfp
->l_type
== F_RDLCK
? "read" : "unlock",
14517 bfp
->l_start
, bfp
->l_len
, rp
->r_mapcnt
));
14519 if (rp
->r_mapcnt
== 0)
14520 return (1); /* always safe if not mapped */
14523 * If the file is already mapped and there are locks, then they
14524 * should be all safe locks. So adding or removing a lock is safe
14525 * as long as the new request is safe (i.e., whole-file, meaning
14526 * length and starting offset are both zero).
14529 if (bfp
->l_start
!= 0 || bfp
->l_len
!= 0) {
14530 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14531 "cannot lock a memory mapped file unless locking the "
14532 "entire file: start %"PRIx64
", len %"PRIx64
,
14533 bfp
->l_start
, bfp
->l_len
));
14537 /* mandatory locking and mapping don't mix */
14538 va
.va_mask
= AT_MODE
;
14539 error
= VOP_GETATTR(vp
, &va
, 0, cr
, NULL
);
14541 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14542 "getattr error %d", error
));
14543 return (0); /* treat errors conservatively */
14545 if (MANDLOCK(vp
, va
.va_mode
)) {
14546 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_safelock: "
14547 "cannot mandatory lock and mmap a file"));
14556 * Register the lock locally within Solaris.
14557 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14558 * recording locks locally.
14560 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14561 * are registered locally.
14564 nfs4_register_lock_locally(vnode_t
*vp
, struct flock64
*flk
, int flag
,
14573 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14576 name
= fn_name(VTOSV(vp
)->sv_name
);
14577 NFS4_DEBUG(nfs4_client_lock_debug
,
14578 (CE_NOTE
, "nfs4_register_lock_locally: %s: type %d, "
14579 "start %"PRIx64
", length %"PRIx64
", pid %ld, sysid %d",
14580 name
, flk
->l_type
, flk
->l_start
, flk
->l_len
, (long)flk
->l_pid
,
14582 kmem_free(name
, MAXNAMELEN
);
14585 /* register the lock with local locking */
14586 oldsysid
= flk
->l_sysid
;
14587 flk
->l_sysid
|= LM_SYSID_CLIENT
;
14588 error
= reclock(vp
, flk
, SETFLCK
, flag
, offset
, NULL
);
14591 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14592 "nfs4_register_lock_locally: could not register with"
14593 " local locking"));
14594 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14595 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14596 error
, (void *)vp
, flk
->l_pid
, flk
->l_sysid
));
14597 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14598 "type %d off 0x%" PRIx64
" len 0x%" PRIx64
,
14599 flk
->l_type
, flk
->l_start
, flk
->l_len
));
14600 (void) reclock(vp
, flk
, 0, flag
, offset
, NULL
);
14601 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_CONT
,
14602 "blocked by pid %d sysid 0x%x type %d "
14603 "off 0x%" PRIx64
" len 0x%" PRIx64
,
14604 flk
->l_pid
, flk
->l_sysid
, flk
->l_type
, flk
->l_start
,
14608 flk
->l_sysid
= oldsysid
;
14612 * nfs4_lockrelease:
14614 * Release any locks on the given vnode that are held by the current
14615 * process. Also removes the lock owner (if one exists) from the rnode's
14619 nfs4_lockrelease(vnode_t
*vp
, int flag
, offset_t offset
, cred_t
*cr
)
14624 nfs4_lock_owner_t
*lop
;
14625 nfs4_recov_state_t recov_state
;
14627 bool_t possible_orphan
= FALSE
;
14630 ASSERT((uintptr_t)vp
> KERNELBASE
);
14631 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14637 * If we have not locked anything then we can
14638 * just return since we have no work to do.
14640 if (rp
->r_lo_head
.lo_next_rnode
== &rp
->r_lo_head
) {
14645 * We need to comprehend that another thread may
14646 * kick off recovery and the lock_owner we have stashed
14647 * in lop might be invalid so we should NOT cache it
14650 recov_state
.rs_flags
= 0;
14651 recov_state
.rs_num_retry_despite_err
= 0;
14652 error
= nfs4_start_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
,
14655 mutex_enter(&rp
->r_statelock
);
14656 rp
->r_flags
|= R4LODANGLERS
;
14657 mutex_exit(&rp
->r_statelock
);
14661 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14664 * Check if the lock owner might have a lock (request was sent but
14665 * no response was received). Also check if there are any remote
14666 * locks on the file. (In theory we shouldn't have to make this
14667 * second check if there's no lock owner, but for now we'll be
14668 * conservative and do it anyway.) If either condition is true,
14669 * send an unlock for the entire file to the server.
14671 * Note that no explicit synchronization is needed here. At worst,
14672 * flk_has_remote_locks() will return a false positive, in which case
14673 * the unlock call wastes time but doesn't harm correctness.
14677 mutex_enter(&lop
->lo_lock
);
14678 possible_orphan
= lop
->lo_pending_rqsts
;
14679 mutex_exit(&lop
->lo_lock
);
14680 lock_owner_rele(lop
);
14683 nfs4_end_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
, 0);
14685 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14686 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14687 "lop %p.", possible_orphan
, flk_has_remote_locks(vp
),
14690 if (possible_orphan
|| flk_has_remote_locks(vp
)) {
14691 ld
.l_type
= F_UNLCK
; /* set to unlock entire file */
14692 ld
.l_whence
= 0; /* unlock from start of file */
14694 ld
.l_len
= 0; /* do entire file */
14696 ret
= VOP_FRLOCK(vp
, F_SETLK
, &ld
, flag
, offset
, NULL
,
14701 * If VOP_FRLOCK fails, make sure we unregister
14702 * local locks before we continue.
14704 ld
.l_pid
= ttoproc(curthread
)->p_pid
;
14705 nfs4_register_lock_locally(vp
, &ld
, flag
, offset
);
14706 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
14707 "nfs4_lockrelease: lock release error on vp"
14708 " %p: error %d.\n", (void *)vp
, ret
));
14712 recov_state
.rs_flags
= 0;
14713 recov_state
.rs_num_retry_despite_err
= 0;
14714 error
= nfs4_start_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
,
14717 mutex_enter(&rp
->r_statelock
);
14718 rp
->r_flags
|= R4LODANGLERS
;
14719 mutex_exit(&rp
->r_statelock
);
14724 * So, here we're going to need to retrieve the lock-owner
14725 * again (in case recovery has done a switch-a-roo) and
14726 * remove it because we can.
14728 lop
= find_lock_owner(rp
, curproc
->p_pid
, LOWN_ANY
);
14731 nfs4_rnode_remove_lock_owner(rp
, lop
);
14732 lock_owner_rele(lop
);
14735 nfs4_end_fop(mi
, vp
, NULL
, OH_LOCKU
, &recov_state
, 0);
14740 * Wait for 'tick_delay' clock ticks.
14741 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14742 * NOTE: lock_lease_time is in seconds.
14744 * XXX For future improvements, should implement a waiting queue scheme.
14747 nfs4_block_and_wait(clock_t *tick_delay
, rnode4_t
*rp
)
14749 long milliseconds_delay
;
14750 time_t lock_lease_time
;
14752 /* wait tick_delay clock ticks or siginteruptus */
14753 if (delay_sig(*tick_delay
)) {
14756 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
, "nfs4_block_and_wait: "
14757 "reissue the lock request: blocked for %ld clock ticks: %ld "
14758 "milliseconds", *tick_delay
, drv_hztousec(*tick_delay
) / 1000));
14760 /* get the lease time */
14761 lock_lease_time
= r2lease_time(rp
);
14763 /* drv_hztousec converts ticks to microseconds */
14764 milliseconds_delay
= drv_hztousec(*tick_delay
) / 1000;
14765 if (milliseconds_delay
< lock_lease_time
* 1000) {
14766 *tick_delay
= 2 * *tick_delay
;
14767 if (drv_hztousec(*tick_delay
) > lock_lease_time
* 1000 * 1000)
14768 *tick_delay
= drv_usectohz(lock_lease_time
*1000*1000);
14775 nfs4_vnops_init(void)
14780 nfs4_vnops_fini(void)
14785 * Return a reference to the directory (parent) vnode for a given vnode,
14786 * using the saved pathname information and the directory file handle. The
14787 * caller is responsible for disposing of the reference.
14788 * Returns zero or an errno value.
14790 * Caller should set need_start_op to FALSE if it is the recovery
14791 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14794 vtodv(vnode_t
*vp
, vnode_t
**dvpp
, cred_t
*cr
, bool_t need_start_op
)
14797 vnode_t
*dvp
= NULL
;
14799 nfs4_fname_t
*mfname
;
14802 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14804 if (vp
->v_flag
& VROOT
) {
14805 nfs4_sharedfh_t
*sfh
;
14809 ASSERT(vp
->v_type
== VREG
);
14812 svp
= mi
->mi_curr_serv
;
14813 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
14814 fh
.nfs_fh4_len
= svp
->sv_pfhandle
.fh_len
;
14815 fh
.nfs_fh4_val
= svp
->sv_pfhandle
.fh_buf
;
14816 sfh
= sfh4_get(&fh
, VTOMI4(vp
));
14817 nfs_rw_exit(&svp
->sv_lock
);
14818 mfname
= mi
->mi_fname
;
14820 dvp
= makenfs4node_by_fh(sfh
, NULL
, &mfname
, NULL
, mi
, cr
, 0);
14823 if (dvp
->v_type
== VNON
)
14824 dvp
->v_type
= VDIR
;
14831 if (svnp
== NULL
) {
14832 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14833 "shadow node is NULL"));
14837 if (svnp
->sv_name
== NULL
|| svnp
->sv_dfh
== NULL
) {
14838 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14839 "shadow node name or dfh val == NULL"));
14843 error
= nfs4_make_dotdot(svnp
->sv_dfh
, 0, vp
, cr
, &dvp
,
14844 (int)need_start_op
);
14846 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14847 "nfs4_make_dotdot returned %d", error
));
14851 NFS4_DEBUG(nfs4_client_shadow_debug
, (CE_NOTE
, "vtodv: "
14852 "nfs4_make_dotdot returned a NULL dvp"));
14855 if (dvp
->v_type
== VNON
)
14856 dvp
->v_type
= VDIR
;
14857 ASSERT(dvp
->v_type
== VDIR
);
14858 if (VTOR4(vp
)->r_flags
& R4ISXATTR
) {
14859 mutex_enter(&dvp
->v_lock
);
14860 dvp
->v_flag
|= V_XATTRDIR
;
14861 mutex_exit(&dvp
->v_lock
);
14868 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14869 * length that fnamep can accept, including the trailing null.
14870 * Returns 0 if okay, returns an errno value if there was a problem.
14874 vtoname(vnode_t
*vp
, char *fnamep
, ssize_t maxlen
)
14882 * If the file being opened has VROOT set, then this is
14883 * a "file" mount. sv_name will not be interesting, so
14884 * go back to the servinfo4 to get the original mount
14885 * path and strip off all but the final edge. Otherwise
14886 * just return the name from the shadow vnode.
14889 if (vp
->v_flag
& VROOT
) {
14891 svp
= VTOMI4(vp
)->mi_curr_serv
;
14892 (void) nfs_rw_enter_sig(&svp
->sv_lock
, RW_READER
, 0);
14894 fn
= strrchr(svp
->sv_path
, '/');
14901 fn
= fn_name(shvp
->sv_name
);
14905 if (strlen(fn
) < maxlen
)
14906 (void) strcpy(fnamep
, fn
);
14908 err
= ENAMETOOLONG
;
14910 if (vp
->v_flag
& VROOT
)
14911 nfs_rw_exit(&svp
->sv_lock
);
14913 kmem_free(fn
, MAXNAMELEN
);
14919 * Bookkeeping for a close that doesn't need to go over the wire.
14920 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14924 nfs4close_notw(vnode_t
*vp
, nfs4_open_stream_t
*osp
, int *have_lockp
)
14932 NFS4_DEBUG(nfs4close_notw_debug
, (CE_NOTE
, "nfs4close_notw: "
14933 "rp=%p osp=%p", (void *)rp
, (void *)osp
));
14934 ASSERT(nfs_zone() == mi
->mi_zone
);
14935 ASSERT(mutex_owned(&osp
->os_sync_lock
));
14936 ASSERT(*have_lockp
);
14938 if (!osp
->os_valid
||
14939 osp
->os_open_ref_count
> 0 || osp
->os_mapcnt
> 0) {
14944 * This removes the reference obtained at OPEN; ie,
14945 * when the open stream structure was created.
14947 * We don't have to worry about calling 'open_stream_rele'
14948 * since we our currently holding a reference to this
14949 * open stream which means the count can not go to 0 with
14952 ASSERT(osp
->os_ref_count
>= 2);
14953 osp
->os_ref_count
--;
14955 mutex_exit(&osp
->os_sync_lock
);
14958 nfs4_dec_state_ref_count(mi
);
14962 * Close all remaining open streams on the rnode. These open streams
14963 * could be here because:
14964 * - The close attempted at either close or delmap failed
14965 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14966 * - Someone did mknod on a regular file but never opened it
14969 nfs4close_all(vnode_t
*vp
, cred_t
*cr
)
14971 nfs4_open_stream_t
*osp
;
14973 nfs4_error_t e
= { 0, NFS4_OK
, RPC_SUCCESS
};
14976 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
14982 * At this point, all we know is that the last time
14983 * someone called vn_rele, the count was 1. Since then,
14984 * the vnode could have been re-activated. We want to
14985 * loop through the open streams and close each one, but
14986 * we have to be careful since once we release the rnode
14987 * hash bucket lock, someone else is free to come in and
14988 * re-activate the rnode and add new open streams. The
14989 * strategy is take the rnode hash bucket lock, verify that
14990 * the count is still 1, grab the open stream off the
14991 * head of the list and mark it invalid, then release the
14992 * rnode hash bucket lock and proceed with that open stream.
14993 * This is ok because nfs4close_one() will acquire the proper
14994 * open/create to close/destroy synchronization for open
14995 * streams, and will ensure that if someone has reopened
14996 * the open stream after we've dropped the hash bucket lock
14997 * then we'll just simply return without destroying the
14999 * Repeat until the list is empty.
15004 /* make sure vnode hasn't been reactivated */
15005 rw_enter(&rp
->r_hashq
->r_lock
, RW_READER
);
15006 mutex_enter(&vp
->v_lock
);
15007 if (vp
->v_count
> 1) {
15008 mutex_exit(&vp
->v_lock
);
15009 rw_exit(&rp
->r_hashq
->r_lock
);
15013 * Grabbing r_os_lock before releasing v_lock prevents
15014 * a window where the rnode/open stream could get
15015 * reactivated (and os_force_close set to 0) before we
15016 * had a chance to set os_force_close to 1.
15018 mutex_enter(&rp
->r_os_lock
);
15019 mutex_exit(&vp
->v_lock
);
15021 osp
= list_head(&rp
->r_open_streams
);
15023 /* nothing left to CLOSE OTW, so return */
15024 mutex_exit(&rp
->r_os_lock
);
15025 rw_exit(&rp
->r_hashq
->r_lock
);
15029 mutex_enter(&rp
->r_statev4_lock
);
15030 /* the file can't still be mem mapped */
15031 ASSERT(rp
->r_mapcnt
== 0);
15032 if (rp
->created_v4
)
15033 rp
->created_v4
= 0;
15034 mutex_exit(&rp
->r_statev4_lock
);
15037 * Grab a ref on this open stream; nfs4close_one
15038 * will mark it as invalid
15040 mutex_enter(&osp
->os_sync_lock
);
15041 osp
->os_ref_count
++;
15042 osp
->os_force_close
= 1;
15043 mutex_exit(&osp
->os_sync_lock
);
15044 mutex_exit(&rp
->r_os_lock
);
15045 rw_exit(&rp
->r_hashq
->r_lock
);
15047 nfs4close_one(vp
, osp
, cr
, 0, NULL
, &e
, CLOSE_FORCE
, 0, 0, 0);
15049 /* Update error if it isn't already non-zero */
15054 error
= geterrno4(e
.stat
);
15058 nfs4close_all_cnt
++;
15060 /* Release the ref on osp acquired above. */
15061 open_stream_rele(osp
, rp
);
15063 /* Proceed to the next open stream, if any */
15069 * nfs4close_one - close one open stream for a file if needed.
15071 * "close_type" indicates which close path this is:
15072 * CLOSE_NORM: close initiated via VOP_CLOSE.
15073 * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15074 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces
15075 * the close and release of client state for this open stream
15076 * (unless someone else has the open stream open).
15077 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15078 * (e.g., due to abort because of a signal).
15079 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15081 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15082 * recovery. Instead, the caller is expected to deal with retries.
15084 * The caller can either pass in the osp ('provided_osp') or not.
15086 * 'access_bits' represents the access we are closing/downgrading.
15088 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15089 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15090 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15092 * Errors are returned via the nfs4_error_t.
15095 nfs4close_one(vnode_t
*vp
, nfs4_open_stream_t
*provided_osp
, cred_t
*cr
,
15096 int access_bits
, nfs4_lost_rqst_t
*lrp
, nfs4_error_t
*ep
,
15097 nfs4_close_type_t close_type
, size_t len
, uint_t maxprot
,
15100 nfs4_open_owner_t
*oop
;
15101 nfs4_open_stream_t
*osp
= NULL
;
15103 int num_retries
= NFS4_NUM_RECOV_RETRIES
;
15106 nfs4_recov_state_t recov_state
;
15107 cred_t
*cred_otw
= NULL
;
15108 bool_t recovonly
= FALSE
;
15111 int close_failed
= 0;
15112 int did_dec_count
= 0;
15113 int did_start_op
= 0;
15114 int did_force_recovlock
= 0;
15115 int did_start_seqid_sync
= 0;
15116 int have_sync_lock
= 0;
15118 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
15120 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
, "closing vp %p osp %p, "
15121 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15122 (void *)vp
, (void *)provided_osp
, (void *)lrp
, close_type
,
15123 len
, maxprot
, mmap_flags
, access_bits
));
15125 nfs4_error_zinit(ep
);
15128 isrecov
= (close_type
== CLOSE_RESEND
||
15129 close_type
== CLOSE_AFTER_RESEND
);
15132 * First get the open owner.
15134 if (!provided_osp
) {
15135 oop
= find_open_owner(cr
, NFS4_PERM_CREATED
, mi
);
15137 oop
= provided_osp
->os_open_owner
;
15138 ASSERT(oop
!= NULL
);
15139 open_owner_hold(oop
);
15143 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15144 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15145 "close type %d", (void *)rp
, (void *)mi
, (void *)cr
,
15146 (void *)provided_osp
, close_type
));
15151 cred_otw
= nfs4_get_otw_cred(cr
, mi
, oop
);
15155 force_close
= (close_type
== CLOSE_FORCE
);
15158 did_force_recovlock
= 0;
15159 did_start_seqid_sync
= 0;
15160 have_sync_lock
= 0;
15162 recov_state
.rs_flags
= 0;
15163 recov_state
.rs_num_retry_despite_err
= 0;
15166 * Second synchronize with recovery.
15169 ep
->error
= nfs4_start_fop(mi
, vp
, NULL
, OH_CLOSE
,
15170 &recov_state
, &recovonly
);
15176 * If we couldn't get start_fop, but have to
15177 * cleanup state, then at least acquire the
15178 * mi_recovlock so we can synchronize with
15181 if (close_type
== CLOSE_FORCE
) {
15182 (void) nfs_rw_enter_sig(&mi
->mi_recovlock
,
15184 did_force_recovlock
= 1;
15191 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15192 * set 'recovonly' to TRUE since most likely this is due to
15193 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15194 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15195 * to retry, causing us to loop until recovery finishes. Plus we
15196 * don't need protection over the open seqid since we're not going
15197 * OTW, hence don't need to use the seqid.
15199 if (recovonly
== FALSE
) {
15200 /* need to grab the open owner sync before 'os_sync_lock' */
15201 ep
->error
= nfs4_start_open_seqid_sync(oop
, mi
);
15202 if (ep
->error
== EAGAIN
) {
15205 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15206 &recov_state
, TRUE
);
15207 if (did_force_recovlock
)
15208 nfs_rw_exit(&mi
->mi_recovlock
);
15211 did_start_seqid_sync
= 1;
15215 * Third get an open stream and acquire 'os_sync_lock' to
15216 * sychronize the opening/creating of an open stream with the
15217 * closing/destroying of an open stream.
15219 if (!provided_osp
) {
15220 /* returns with 'os_sync_lock' held */
15221 osp
= find_open_stream(oop
, rp
);
15227 osp
= provided_osp
;
15228 open_stream_hold(osp
);
15229 mutex_enter(&osp
->os_sync_lock
);
15231 have_sync_lock
= 1;
15233 ASSERT(oop
== osp
->os_open_owner
);
15236 * Fourth, do any special pre-OTW CLOSE processing
15237 * based on the specific close type.
15239 if ((close_type
== CLOSE_NORM
|| close_type
== CLOSE_AFTER_RESEND
) &&
15241 ASSERT(osp
->os_open_ref_count
> 0);
15242 osp
->os_open_ref_count
--;
15244 if (osp
->os_open_ref_count
== 0)
15245 osp
->os_final_close
= 1;
15248 if (close_type
== CLOSE_FORCE
) {
15249 /* see if somebody reopened the open stream. */
15250 if (!osp
->os_force_close
) {
15251 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
,
15252 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15253 "was reopened, vp %p", (void *)osp
, (void *)vp
));
15255 ep
->stat
= NFS4_OK
;
15259 if (!osp
->os_final_close
&& !did_dec_count
) {
15260 osp
->os_open_ref_count
--;
15265 * We can't depend on os_open_ref_count being 0 due to the
15266 * way executables are opened (VN_RELE to match a VOP_OPEN).
15269 ASSERT(osp
->os_open_ref_count
== 0);
15271 if (osp
->os_open_ref_count
!= 0) {
15272 NFS4_DEBUG(nfs4close_one_debug
, (CE_NOTE
,
15273 "nfs4close_one: should panic here on an "
15274 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15275 "since this is probably the exec problem."));
15277 osp
->os_open_ref_count
= 0;
15281 * There is the possibility that nfs4close_one()
15282 * for close_type == CLOSE_DELMAP couldn't find the
15283 * open stream, thus couldn't decrement its os_mapcnt;
15284 * therefore we can't use this ASSERT yet.
15287 ASSERT(osp
->os_mapcnt
== 0);
15289 osp
->os_mapcnt
= 0;
15292 if (close_type
== CLOSE_DELMAP
&& !did_dec_count
) {
15293 ASSERT(osp
->os_mapcnt
>= btopr(len
));
15295 if ((mmap_flags
& MAP_SHARED
) && (maxprot
& PROT_WRITE
))
15296 osp
->os_mmap_write
-= btopr(len
);
15297 if (maxprot
& PROT_READ
)
15298 osp
->os_mmap_read
-= btopr(len
);
15299 if (maxprot
& PROT_EXEC
)
15300 osp
->os_mmap_read
-= btopr(len
);
15301 /* mirror the PROT_NONE check in nfs4_addmap() */
15302 if (!(maxprot
& PROT_READ
) && !(maxprot
& PROT_WRITE
) &&
15303 !(maxprot
& PROT_EXEC
))
15304 osp
->os_mmap_read
-= btopr(len
);
15305 osp
->os_mapcnt
-= btopr(len
);
15310 nfs4_lost_rqst_t lost_rqst
;
15312 /* request should not already be in recovery queue */
15313 ASSERT(lrp
== NULL
);
15314 nfs4_error_init(ep
, EINTR
);
15315 nfs4close_save_lost_rqst(ep
->error
, &lost_rqst
, oop
,
15316 osp
, cred_otw
, vp
);
15317 mutex_exit(&osp
->os_sync_lock
);
15318 have_sync_lock
= 0;
15319 (void) nfs4_start_recovery(ep
, mi
, vp
, NULL
, NULL
,
15320 lost_rqst
.lr_op
== OP_CLOSE
?
15321 &lost_rqst
: NULL
, OP_CLOSE
, NULL
, NULL
, NULL
);
15324 goto close_cleanup
;
15328 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15329 * we stopped operating on the open owner's <old oo_name, old seqid>
15330 * space, which means we stopped operating on the open stream
15331 * too. So don't go OTW (as the seqid is likely bad, and the
15332 * stateid could be stale, potentially triggering a false
15333 * setclientid), and just clean up the client's internal state.
15335 if (osp
->os_orig_oo_name
!= oop
->oo_name
) {
15336 NFS4_DEBUG(nfs4close_one_debug
|| nfs4_client_recov_debug
,
15337 (CE_NOTE
, "nfs4close_one: skip OTW close for osp %p "
15338 "oop %p due to bad seqid (orig oo_name %" PRIx64
" current "
15339 "oo_name %" PRIx64
")",
15340 (void *)osp
, (void *)oop
, osp
->os_orig_oo_name
,
15345 /* If the file failed recovery, just quit. */
15346 mutex_enter(&rp
->r_statelock
);
15347 if (rp
->r_flags
& R4RECOVERR
) {
15350 mutex_exit(&rp
->r_statelock
);
15353 * If the force close path failed to obtain start_fop
15354 * then skip the OTW close and just remove the state.
15357 goto close_cleanup
;
15360 * Fifth, check to see if there are still mapped pages or other
15361 * opens using this open stream. If there are then we can't
15362 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15364 if (osp
->os_open_ref_count
> 0 || osp
->os_mapcnt
> 0) {
15365 nfs4_lost_rqst_t new_lost_rqst
;
15366 bool_t needrecov
= FALSE
;
15367 cred_t
*odg_cred_otw
= NULL
;
15368 seqid4 open_dg_seqid
= 0;
15370 if (osp
->os_delegation
) {
15372 * If this open stream was never OPENed OTW then we
15373 * surely can't DOWNGRADE it (especially since the
15374 * osp->open_stateid is really a delegation stateid
15375 * when os_delegation is 1).
15377 if (access_bits
& FREAD
)
15378 osp
->os_share_acc_read
--;
15379 if (access_bits
& FWRITE
)
15380 osp
->os_share_acc_write
--;
15381 osp
->os_share_deny_none
--;
15382 nfs4_error_zinit(ep
);
15385 nfs4_open_downgrade(access_bits
, 0, oop
, osp
, vp
, cr
,
15386 lrp
, ep
, &odg_cred_otw
, &open_dg_seqid
);
15387 needrecov
= nfs4_needs_recovery(ep
, TRUE
, mi
->mi_vfsp
);
15388 if (needrecov
&& !isrecov
) {
15390 nfs4_bseqid_entry_t
*bsep
= NULL
;
15392 if (!ep
->error
&& ep
->stat
== NFS4ERR_BAD_SEQID
)
15393 bsep
= nfs4_create_bseqid_entry(oop
, NULL
,
15395 lrp
? TAG_OPEN_DG_LOST
: TAG_OPEN_DG
,
15398 nfs4open_dg_save_lost_rqst(ep
->error
, &new_lost_rqst
,
15399 oop
, osp
, odg_cred_otw
, vp
, access_bits
, 0);
15400 mutex_exit(&osp
->os_sync_lock
);
15401 have_sync_lock
= 0;
15402 abort
= nfs4_start_recovery(ep
, mi
, vp
, NULL
, NULL
,
15403 new_lost_rqst
.lr_op
== OP_OPEN_DOWNGRADE
?
15404 &new_lost_rqst
: NULL
, OP_OPEN_DOWNGRADE
,
15407 crfree(odg_cred_otw
);
15409 kmem_free(bsep
, sizeof (*bsep
));
15414 if (did_start_seqid_sync
) {
15415 nfs4_end_open_seqid_sync(oop
);
15416 did_start_seqid_sync
= 0;
15418 open_stream_rele(osp
, rp
);
15421 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15422 &recov_state
, FALSE
);
15423 if (did_force_recovlock
)
15424 nfs_rw_exit(&mi
->mi_recovlock
);
15429 crfree(odg_cred_otw
);
15435 * If this open stream was created as the results of an open
15436 * while holding a delegation, then just release it; no need
15437 * to do an OTW close. Otherwise do a "normal" OTW close.
15439 if (osp
->os_delegation
) {
15440 nfs4close_notw(vp
, osp
, &have_sync_lock
);
15441 nfs4_error_zinit(ep
);
15446 * If this stream is not valid, we're done.
15448 if (!osp
->os_valid
) {
15449 nfs4_error_zinit(ep
);
15454 * Last open or mmap ref has vanished, need to do an OTW close.
15455 * First check to see if a close is still necessary.
15457 if (osp
->os_failed_reopen
) {
15458 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15459 "don't close OTW osp %p since reopen failed.",
15462 * Reopen of the open stream failed, hence the
15463 * stateid of the open stream is invalid/stale, and
15464 * sending this OTW would incorrectly cause another
15465 * round of recovery. In this case, we need to set
15466 * the 'os_valid' bit to 0 so another thread doesn't
15467 * come in and re-open this open stream before
15468 * this "closing" thread cleans up state (decrementing
15469 * the nfs4_server_t's state_ref_count and decrementing
15470 * the os_ref_count).
15474 * This removes the reference obtained at OPEN; ie,
15475 * when the open stream structure was created.
15477 * We don't have to worry about calling 'open_stream_rele'
15478 * since we our currently holding a reference to this
15479 * open stream which means the count can not go to 0 with
15482 ASSERT(osp
->os_ref_count
>= 2);
15483 osp
->os_ref_count
--;
15484 nfs4_error_zinit(ep
);
15486 goto close_cleanup
;
15489 ASSERT(osp
->os_ref_count
> 1);
15492 * Sixth, try the CLOSE OTW.
15494 nfs4close_otw(rp
, cred_otw
, oop
, osp
, &retry
, &did_start_seqid_sync
,
15495 close_type
, ep
, &have_sync_lock
);
15497 if (ep
->error
== EINTR
|| NFS4_FRC_UNMT_ERR(ep
->error
, vp
->v_vfsp
)) {
15499 * Let the recovery thread be responsible for
15500 * removing the state for CLOSE.
15507 /* See if we need to retry with a different cred */
15508 if ((ep
->error
== EACCES
||
15509 (ep
->error
== 0 && ep
->stat
== NFS4ERR_ACCESS
)) &&
15517 if (ep
->error
|| ep
->stat
)
15520 if (retry
&& !isrecov
&& num_retries
-- > 0) {
15521 if (have_sync_lock
) {
15522 mutex_exit(&osp
->os_sync_lock
);
15523 have_sync_lock
= 0;
15525 if (did_start_seqid_sync
) {
15526 nfs4_end_open_seqid_sync(oop
);
15527 did_start_seqid_sync
= 0;
15529 open_stream_rele(osp
, rp
);
15532 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
,
15533 &recov_state
, FALSE
);
15534 if (did_force_recovlock
)
15535 nfs_rw_exit(&mi
->mi_recovlock
);
15536 NFS4_DEBUG(nfs4_client_recov_debug
, (CE_NOTE
,
15537 "nfs4close_one: need to retry the close "
15543 * Seventh and lastly, process our results.
15545 if (close_failed
&& force_close
) {
15547 * It's ok to drop and regrab the 'os_sync_lock' since
15548 * nfs4close_notw() will recheck to make sure the
15549 * "close"/removal of state should happen.
15551 if (!have_sync_lock
) {
15552 mutex_enter(&osp
->os_sync_lock
);
15553 have_sync_lock
= 1;
15556 * This is last call, remove the ref on the open
15557 * stream created by open and clean everything up.
15559 osp
->os_pending_close
= 0;
15560 nfs4close_notw(vp
, osp
, &have_sync_lock
);
15561 nfs4_error_zinit(ep
);
15564 if (!close_failed
) {
15565 if (have_sync_lock
) {
15566 osp
->os_pending_close
= 0;
15567 mutex_exit(&osp
->os_sync_lock
);
15568 have_sync_lock
= 0;
15570 mutex_enter(&osp
->os_sync_lock
);
15571 osp
->os_pending_close
= 0;
15572 mutex_exit(&osp
->os_sync_lock
);
15574 if (did_start_op
&& recov_state
.rs_sp
!= NULL
) {
15575 mutex_enter(&recov_state
.rs_sp
->s_lock
);
15576 nfs4_dec_state_ref_count_nolock(recov_state
.rs_sp
, mi
);
15577 mutex_exit(&recov_state
.rs_sp
->s_lock
);
15579 nfs4_dec_state_ref_count(mi
);
15581 nfs4_error_zinit(ep
);
15585 if (have_sync_lock
)
15586 mutex_exit(&osp
->os_sync_lock
);
15588 nfs4_end_fop(mi
, vp
, NULL
, OH_CLOSE
, &recov_state
,
15589 recovonly
? TRUE
: FALSE
);
15590 if (did_force_recovlock
)
15591 nfs_rw_exit(&mi
->mi_recovlock
);
15595 open_stream_rele(osp
, rp
);
15597 if (did_start_seqid_sync
)
15598 nfs4_end_open_seqid_sync(oop
);
15599 open_owner_rele(oop
);
15604 * Convert information returned by the server in the LOCK4denied
15605 * structure to the form required by fcntl.
15608 denied_to_flk(LOCK4denied
*lockt_denied
, flock64_t
*flk
, LOCKT4args
*lockt_args
)
15610 nfs4_lo_name_t
*lo
;
15613 if (denied_to_flk_debug
) {
15614 lockt_denied_debug
= lockt_denied
;
15615 debug_enter("lockt_denied");
15619 flk
->l_type
= lockt_denied
->locktype
== READ_LT
? F_RDLCK
: F_WRLCK
;
15620 flk
->l_whence
= 0; /* aka SEEK_SET */
15621 flk
->l_start
= lockt_denied
->offset
;
15622 flk
->l_len
= lockt_denied
->length
;
15625 * If the blocking clientid matches our client id, then we can
15626 * interpret the lockowner (since we built it). If not, then
15627 * fabricate a sysid and pid. Note that the l_sysid field
15628 * in *flk already has the local sysid.
15631 if (lockt_denied
->owner
.clientid
== lockt_args
->owner
.clientid
) {
15633 if (lockt_denied
->owner
.owner_len
== sizeof (*lo
)) {
15634 lo
= (nfs4_lo_name_t
*)
15635 lockt_denied
->owner
.owner_val
;
15637 flk
->l_pid
= lo
->ln_pid
;
15639 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
15640 "denied_to_flk: bad lock owner length\n"));
15642 flk
->l_pid
= lo_to_pid(&lockt_denied
->owner
);
15645 NFS4_DEBUG(nfs4_client_lock_debug
, (CE_NOTE
,
15646 "denied_to_flk: foreign clientid\n"));
15649 * Construct a new sysid which should be different from
15650 * sysids of other systems.
15654 flk
->l_pid
= lo_to_pid(&lockt_denied
->owner
);
15659 lo_to_pid(lock_owner4
*lop
)
15665 cp
= (uchar_t
*)&lop
->clientid
;
15667 for (i
= 0; i
< sizeof (lop
->clientid
); i
++)
15668 pid
+= (pid_t
)*cp
++;
15670 cp
= (uchar_t
*)lop
->owner_val
;
15672 for (i
= 0; i
< lop
->owner_len
; i
++)
15673 pid
+= (pid_t
)*cp
++;
15679 * Given a lock pointer, returns the length of that lock.
15680 * "end" is the last locked offset the "l_len" covers from
15681 * the start of the lock.
15684 lock_to_end(flock64_t
*lock
)
15688 if (lock
->l_len
== 0)
15689 lock_end
= (off64_t
)MAXEND
;
15691 lock_end
= lock
->l_start
+ lock
->l_len
- 1;
15697 * Given the end of a lock, it will return you the length "l_len" for that lock.
15700 end_to_len(off64_t start
, off64_t end
)
15704 ASSERT(end
>= start
);
15708 lock_len
= end
- start
+ 1;
15714 * On given end for a lock it determines if it is the last locked offset
15715 * or not, if so keeps it as is, else adds one to return the length for
15719 start_check(off64_t x
)
15728 * See if these two locks overlap, and if so return 1;
15729 * otherwise, return 0.
15732 locks_intersect(flock64_t
*llfp
, flock64_t
*curfp
)
15734 off64_t llfp_end
, curfp_end
;
15736 llfp_end
= lock_to_end(llfp
);
15737 curfp_end
= lock_to_end(curfp
);
15739 if (((llfp_end
>= curfp
->l_start
) &&
15740 (llfp
->l_start
<= curfp
->l_start
)) ||
15741 ((curfp
->l_start
<= llfp
->l_start
) && (curfp_end
>= llfp
->l_start
)))
15747 * Determine what the intersecting lock region is, and add that to the
15748 * 'nl_llpp' locklist in increasing order (by l_start).
15751 nfs4_add_lock_range(flock64_t
*lost_flp
, flock64_t
*local_flp
,
15752 locklist_t
**nl_llpp
, vnode_t
*vp
)
15754 locklist_t
*intersect_llp
, *tmp_fllp
, *cur_fllp
;
15755 off64_t lost_flp_end
, local_flp_end
, len
, start
;
15757 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range:"));
15759 if (!locks_intersect(lost_flp
, local_flp
))
15762 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range: "
15763 "locks intersect"));
15765 lost_flp_end
= lock_to_end(lost_flp
);
15766 local_flp_end
= lock_to_end(local_flp
);
15768 /* Find the starting point of the intersecting region */
15769 if (local_flp
->l_start
> lost_flp
->l_start
)
15770 start
= local_flp
->l_start
;
15772 start
= lost_flp
->l_start
;
15774 /* Find the lenght of the intersecting region */
15775 if (lost_flp_end
< local_flp_end
)
15776 len
= end_to_len(start
, lost_flp_end
);
15778 len
= end_to_len(start
, local_flp_end
);
15781 * Prepare the flock structure for the intersection found and insert
15782 * it into the new list in increasing l_start order. This list contains
15783 * intersections of locks registered by the client with the local host
15784 * and the lost lock.
15785 * The lock type of this lock is the same as that of the local_flp.
15787 intersect_llp
= (locklist_t
*)kmem_alloc(sizeof (locklist_t
), KM_SLEEP
);
15788 intersect_llp
->ll_flock
.l_start
= start
;
15789 intersect_llp
->ll_flock
.l_len
= len
;
15790 intersect_llp
->ll_flock
.l_type
= local_flp
->l_type
;
15791 intersect_llp
->ll_flock
.l_pid
= local_flp
->l_pid
;
15792 intersect_llp
->ll_flock
.l_sysid
= local_flp
->l_sysid
;
15793 intersect_llp
->ll_flock
.l_whence
= 0; /* aka SEEK_SET */
15794 intersect_llp
->ll_vp
= vp
;
15796 tmp_fllp
= *nl_llpp
;
15798 while (tmp_fllp
!= NULL
&& tmp_fllp
->ll_flock
.l_start
<
15799 intersect_llp
->ll_flock
.l_start
) {
15800 cur_fllp
= tmp_fllp
;
15801 tmp_fllp
= tmp_fllp
->ll_next
;
15803 if (cur_fllp
== NULL
) {
15804 /* first on the list */
15805 intersect_llp
->ll_next
= *nl_llpp
;
15806 *nl_llpp
= intersect_llp
;
15808 intersect_llp
->ll_next
= cur_fllp
->ll_next
;
15809 cur_fllp
->ll_next
= intersect_llp
;
15812 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
, "nfs4_add_lock_range: "
15813 "created lock region: start %"PRIx64
" end %"PRIx64
" : %s\n",
15814 intersect_llp
->ll_flock
.l_start
,
15815 intersect_llp
->ll_flock
.l_start
+ intersect_llp
->ll_flock
.l_len
,
15816 intersect_llp
->ll_flock
.l_type
== F_RDLCK
? "READ" : "WRITE"));
15820 * Our local locking current state is potentially different than
15821 * what the NFSv4 server thinks we have due to a lost lock that was
15822 * resent and then received. We need to reset our "NFSv4" locking
15823 * state to match the current local locking state for this pid since
15824 * that is what the user/application sees as what the world is.
15826 * We cannot afford to drop the open/lock seqid sync since then we can
15827 * get confused about what the current local locking state "is" versus
15830 * If we are unable to fix up the locks, we send SIGLOST to the affected
15831 * process. This is not done if the filesystem has been forcibly
15832 * unmounted, in case the process has already exited and a new process
15833 * exists with the same pid.
15836 nfs4_reinstitute_local_lock_state(vnode_t
*vp
, flock64_t
*lost_flp
, cred_t
*cr
,
15837 nfs4_lock_owner_t
*lop
)
15839 locklist_t
*locks
, *llp
, *ri_llp
, *tmp_llp
;
15840 mntinfo4_t
*mi
= VTOMI4(vp
);
15841 const int cmd
= F_SETLK
;
15842 off64_t cur_start
, llp_ll_flock_end
, lost_flp_end
;
15845 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15846 "nfs4_reinstitute_local_lock_state"));
15849 * Find active locks for this vp from the local locking code.
15850 * Scan through this list and find out the locks that intersect with
15851 * the lost lock. Once we find the lock that intersects, add the
15852 * intersection area as a new lock to a new list "ri_llp". The lock
15853 * type of the intersection region lock added to ri_llp is the same
15854 * as that found in the active lock list, "list". The intersecting
15855 * region locks are added to ri_llp in increasing l_start order.
15857 ASSERT(nfs_zone() == mi
->mi_zone
);
15859 locks
= flk_active_locks_for_vp(vp
);
15862 for (llp
= locks
; llp
!= NULL
; llp
= llp
->ll_next
) {
15863 ASSERT(llp
->ll_vp
== vp
);
15865 * Pick locks that belong to this pid/lockowner
15867 if (llp
->ll_flock
.l_pid
!= lost_flp
->l_pid
)
15870 nfs4_add_lock_range(lost_flp
, &llp
->ll_flock
, &ri_llp
, vp
);
15874 * Now we have the list of intersections with the lost lock. These are
15875 * the locks that were/are active before the server replied to the
15876 * last/lost lock. Issue these locks to the server here. Playing these
15877 * locks to the server will re-establish aur current local locking state
15878 * with the v4 server.
15879 * If we get an error, send SIGLOST to the application for that lock.
15882 for (llp
= ri_llp
; llp
!= NULL
; llp
= llp
->ll_next
) {
15883 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15884 "nfs4_reinstitute_local_lock_state: need to issue "
15885 "flock: [%"PRIx64
" - %"PRIx64
"] : %s",
15886 llp
->ll_flock
.l_start
,
15887 llp
->ll_flock
.l_start
+ llp
->ll_flock
.l_len
,
15888 llp
->ll_flock
.l_type
== F_RDLCK
? "READ" :
15889 llp
->ll_flock
.l_type
== F_WRLCK
? "WRITE" : "INVALID"));
15891 * No need to relock what we already have
15893 if (llp
->ll_flock
.l_type
== lost_flp
->l_type
)
15896 push_reinstate(vp
, cmd
, &llp
->ll_flock
, cr
, lop
);
15900 * Now keeping the start of the lost lock as our reference parse the
15901 * newly created ri_llp locklist to find the ranges that we have locked
15902 * with the v4 server but not in the current local locking. We need
15903 * to unlock these ranges.
15904 * These ranges can also be reffered to as those ranges, where the lost
15905 * lock does not overlap with the locks in the ri_llp but are locked
15906 * since the server replied to the lost lock.
15908 cur_start
= lost_flp
->l_start
;
15909 lost_flp_end
= lock_to_end(lost_flp
);
15911 ul_fl
.l_type
= F_UNLCK
;
15912 ul_fl
.l_whence
= 0; /* aka SEEK_SET */
15913 ul_fl
.l_sysid
= lost_flp
->l_sysid
;
15914 ul_fl
.l_pid
= lost_flp
->l_pid
;
15916 for (llp
= ri_llp
; llp
!= NULL
; llp
= llp
->ll_next
) {
15917 llp_ll_flock_end
= lock_to_end(&llp
->ll_flock
);
15919 if (llp
->ll_flock
.l_start
<= cur_start
) {
15920 cur_start
= start_check(llp_ll_flock_end
);
15923 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15924 "nfs4_reinstitute_local_lock_state: "
15925 "UNLOCK [%"PRIx64
" - %"PRIx64
"]",
15926 cur_start
, llp
->ll_flock
.l_start
));
15928 ul_fl
.l_start
= cur_start
;
15929 ul_fl
.l_len
= end_to_len(cur_start
,
15930 (llp
->ll_flock
.l_start
- 1));
15932 push_reinstate(vp
, cmd
, &ul_fl
, cr
, lop
);
15933 cur_start
= start_check(llp_ll_flock_end
);
15937 * In the case where the lost lock ends after all intersecting locks,
15938 * unlock the last part of the lost lock range.
15940 if (cur_start
!= start_check(lost_flp_end
)) {
15941 NFS4_DEBUG(nfs4_lost_rqst_debug
, (CE_NOTE
,
15942 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15943 "lost lock region [%"PRIx64
" - %"PRIx64
"]",
15944 cur_start
, lost_flp
->l_start
+ lost_flp
->l_len
));
15946 ul_fl
.l_start
= cur_start
;
15948 * Is it an to-EOF lock? if so unlock till the end
15950 if (lost_flp
->l_len
== 0)
15953 ul_fl
.l_len
= start_check(lost_flp_end
) - cur_start
;
15955 push_reinstate(vp
, cmd
, &ul_fl
, cr
, lop
);
15959 flk_free_locklist(locks
);
15961 /* Free up our newly created locklist */
15962 for (llp
= ri_llp
; llp
!= NULL
; ) {
15963 tmp_llp
= llp
->ll_next
;
15964 kmem_free(llp
, sizeof (locklist_t
));
15969 * Now return back to the original calling nfs4frlock()
15970 * and let us naturally drop our seqid syncs.
15975 * Create a lost state record for the given lock reinstantiation request
15976 * and push it onto the lost state queue.
15979 push_reinstate(vnode_t
*vp
, int cmd
, flock64_t
*flk
, cred_t
*cr
,
15980 nfs4_lock_owner_t
*lop
)
15982 nfs4_lost_rqst_t req
;
15983 nfs_lock_type4 locktype
;
15984 nfs4_error_t e
= { EINTR
, NFS4_OK
, RPC_SUCCESS
};
15986 ASSERT(nfs_zone() == VTOMI4(vp
)->mi_zone
);
15988 locktype
= flk_to_locktype(cmd
, flk
->l_type
);
15989 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE
, EINTR
, locktype
,
15990 NULL
, NULL
, lop
, flk
, &req
, cr
, vp
);
15991 (void) nfs4_start_recovery(&e
, VTOMI4(vp
), vp
, NULL
, NULL
,
15992 (req
.lr_op
== OP_LOCK
|| req
.lr_op
== OP_LOCKU
) ?
15993 &req
: NULL
, flk
->l_type
== F_UNLCK
? OP_LOCKU
: OP_LOCK
,