dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / kernel / fs / nfs / nfs4_vnops.c
blob6b71a0d80b6aebc9cdb671825a72d6b96c54c091
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2016 STRATO AG. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
32 * Use is subject to license terms.
36 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
37 * All Rights Reserved
41 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/cred.h>
48 #include <sys/time.h>
49 #include <sys/vnode.h>
50 #include <sys/vfs.h>
51 #include <sys/file.h>
52 #include <sys/filio.h>
53 #include <sys/uio.h>
54 #include <sys/buf.h>
55 #include <sys/mman.h>
56 #include <sys/pathname.h>
57 #include <sys/dirent.h>
58 #include <sys/debug.h>
59 #include <sys/vmsystm.h>
60 #include <sys/fcntl.h>
61 #include <sys/flock.h>
62 #include <sys/swap.h>
63 #include <sys/errno.h>
64 #include <sys/strsubr.h>
65 #include <sys/sysmacros.h>
66 #include <sys/kmem.h>
67 #include <sys/cmn_err.h>
68 #include <sys/pathconf.h>
69 #include <sys/utsname.h>
70 #include <sys/dnlc.h>
71 #include <sys/acl.h>
72 #include <sys/systeminfo.h>
73 #include <sys/policy.h>
74 #include <sys/sdt.h>
75 #include <sys/list.h>
76 #include <sys/stat.h>
77 #include <sys/zone.h>
79 #include <rpc/types.h>
80 #include <rpc/auth.h>
81 #include <rpc/clnt.h>
83 #include <nfs/nfs.h>
84 #include <nfs/nfs_clnt.h>
85 #include <nfs/nfs_acl.h>
86 #include <nfs/lm.h>
87 #include <nfs/nfs4.h>
88 #include <nfs/nfs4_kprot.h>
89 #include <nfs/rnode4.h>
90 #include <nfs/nfs4_clnt.h>
92 #include <vm/hat.h>
93 #include <vm/as.h>
94 #include <vm/page.h>
95 #include <vm/pvn.h>
96 #include <vm/seg.h>
97 #include <vm/seg_map.h>
98 #include <vm/seg_kpm.h>
99 #include <vm/seg_vn.h>
101 #include <sys/fs_subr.h>
103 #include <sys/ddi.h>
104 #include <sys/int_fmtio.h>
105 #include <sys/fs/autofs.h>
107 typedef struct {
108 nfs4_ga_res_t *di_garp;
109 cred_t *di_cred;
110 hrtime_t di_time_call;
111 } dirattr_info_t;
113 typedef enum nfs4_acl_op {
114 NFS4_ACL_GET,
115 NFS4_ACL_SET
116 } nfs4_acl_op_t;
118 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *);
120 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
121 char *, dirattr_info_t *);
123 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
124 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
125 nfs4_error_t *, int *);
126 static int nfs4_rdwrlbn(vnode_t *, page_t *, uoff_t, size_t, int,
127 cred_t *);
128 static int nfs4write(vnode_t *, caddr_t, uoff_t, int, cred_t *,
129 stable_how4 *);
130 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
131 cred_t *, bool_t, struct uio *);
132 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
133 vsecattr_t *);
134 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
135 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
136 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
137 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
138 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
139 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
140 int, vnode_t **, cred_t *);
141 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
142 cred_t *, int, int, enum createmode4, int);
143 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
144 caller_context_t *);
145 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
146 vnode_t *, char *, cred_t *, nfsstat4 *);
147 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
148 vnode_t *, char *, cred_t *, nfsstat4 *);
149 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
150 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
151 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
152 static int nfs4_getapage(vnode_t *, uoff_t, size_t, uint_t *,
153 page_t *[], size_t, struct seg *, caddr_t,
154 enum seg_rw, cred_t *);
155 static void nfs4_readahead(vnode_t *, uoff_t, caddr_t, struct seg *,
156 cred_t *);
157 static int nfs4_sync_putapage(vnode_t *, page_t *, uoff_t, size_t,
158 int, cred_t *);
159 static int nfs4_sync_pageio(vnode_t *, page_t *, uoff_t, size_t,
160 int, cred_t *);
161 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
162 static void nfs4_set_mod(vnode_t *);
163 static void nfs4_get_commit(vnode_t *);
164 static void nfs4_get_commit_range(vnode_t *, uoff_t, size_t);
165 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
166 static int nfs4_commit_vp(vnode_t *, uoff_t, size_t, cred_t *, int);
167 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
168 cred_t *);
169 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
170 cred_t *);
171 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
172 hrtime_t, vnode_t *, cred_t *);
173 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
174 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
175 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
176 uoff_t);
177 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
178 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
179 static cred_t *state_to_cred(nfs4_open_stream_t *);
180 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
181 static pid_t lo_to_pid(lock_owner4 *);
182 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
183 cred_t *, nfs4_lock_owner_t *);
184 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
185 nfs4_lock_owner_t *);
186 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
187 static void nfs4_delmap_callback(struct as *, void *, uint_t);
188 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
189 static nfs4_delmapcall_t *nfs4_init_delmapcall();
190 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
191 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
192 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
193 uid_t, gid_t, int);
196 * Routines that implement the setting of v4 args for the misc. ops
198 static void nfs4args_lock_free(nfs_argop4 *);
199 static void nfs4args_lockt_free(nfs_argop4 *);
200 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
201 int, rnode4_t *, cred_t *, bitmap4, int *,
202 nfs4_stateid_types_t *);
203 static void nfs4args_setattr_free(nfs_argop4 *);
204 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
205 bitmap4);
206 static void nfs4args_verify_free(nfs_argop4 *);
207 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
208 WRITE4args **, nfs4_stateid_types_t *);
211 * These are the vnode ops functions that implement the vnode interface to
212 * the networked file system. See more comments below at nfs4_vnodeops.
214 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
215 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
216 caller_context_t *);
217 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
218 caller_context_t *);
219 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
220 caller_context_t *);
221 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
222 caller_context_t *);
223 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
224 caller_context_t *);
225 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
226 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
227 caller_context_t *);
228 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
229 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
230 int, vnode_t **, cred_t *, int, caller_context_t *,
231 vsecattr_t *);
232 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
233 int);
234 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
235 caller_context_t *, int);
236 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
237 caller_context_t *, int);
238 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
239 cred_t *, caller_context_t *, int, vsecattr_t *);
240 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
241 caller_context_t *, int);
242 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
243 cred_t *, caller_context_t *, int);
244 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
245 caller_context_t *, int);
246 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
247 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
248 page_t *[], size_t, struct seg *, caddr_t,
249 enum seg_rw, cred_t *, caller_context_t *);
250 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
251 caller_context_t *);
252 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
253 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
254 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
255 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
256 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
257 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
258 struct flk_callback *, cred_t *, caller_context_t *);
259 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
260 cred_t *, caller_context_t *);
261 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
262 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
263 static int nfs4_pageio(vnode_t *, page_t *, uoff_t, size_t, int,
264 cred_t *, caller_context_t *);
265 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
266 caller_context_t *);
267 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
268 caller_context_t *);
270 * These vnode ops are required to be called from outside this source file,
271 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
272 * as static.
274 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
275 caller_context_t *);
276 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
277 int nfs4_lookup(vnode_t *, char *, vnode_t **,
278 struct pathname *, int, vnode_t *, cred_t *,
279 caller_context_t *, int *, pathname_t *);
280 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
281 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
282 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
283 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
284 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
285 caller_context_t *);
286 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
287 caller_context_t *);
288 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
289 caller_context_t *);
292 * Used for nfs4_commit_vp() to indicate if we should
293 * wait on pending writes.
295 #define NFS4_WRITE_NOWAIT 0
296 #define NFS4_WRITE_WAIT 1
298 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
301 * Error flags used to pass information about certain special errors
302 * which need to be handled specially.
304 #define NFS_EOF -98
305 #define NFS_VERF_MISMATCH -97
308 * Flags used to differentiate between which operation drove the
309 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
311 #define NFS4_CLOSE_OP 0x1
312 #define NFS4_DELMAP_OP 0x2
313 #define NFS4_INACTIVE_OP 0x3
315 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
317 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
318 #define ALIGN64(x, ptr, sz) \
319 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
320 if (x) { \
321 x = sizeof (uint64_t) - (x); \
322 sz -= (x); \
323 ptr += (x); \
326 #ifdef DEBUG
327 int nfs4_client_attr_debug = 0;
328 int nfs4_client_state_debug = 0;
329 int nfs4_client_shadow_debug = 0;
330 int nfs4_client_lock_debug = 0;
331 int nfs4_seqid_sync = 0;
332 int nfs4_client_map_debug = 0;
333 static int nfs4_pageio_debug = 0;
334 int nfs4_client_inactive_debug = 0;
335 int nfs4_client_recov_debug = 0;
336 int nfs4_client_failover_debug = 0;
337 int nfs4_client_call_debug = 0;
338 int nfs4_client_lookup_debug = 0;
339 int nfs4_client_zone_debug = 0;
340 int nfs4_lost_rqst_debug = 0;
341 int nfs4_rdattrerr_debug = 0;
342 int nfs4_open_stream_debug = 0;
344 int nfs4read_error_inject;
346 static int nfs4_create_misses = 0;
348 static int nfs4_readdir_cache_shorts = 0;
349 static int nfs4_readdir_readahead = 0;
351 static int nfs4_bio_do_stop = 0;
353 static int nfs4_lostpage = 0; /* number of times we lost original page */
355 int nfs4_mmap_debug = 0;
357 static int nfs4_pathconf_cache_hits = 0;
358 static int nfs4_pathconf_cache_misses = 0;
360 int nfs4close_all_cnt;
361 int nfs4close_one_debug = 0;
362 int nfs4close_notw_debug = 0;
364 int denied_to_flk_debug = 0;
365 void *lockt_denied_debug;
367 #endif
370 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
371 * or NFS4ERR_RESOURCE.
373 static int confirm_retry_sec = 30;
375 static int nfs4_lookup_neg_cache = 1;
378 * number of pages to read ahead
379 * optimized for 100 base-T.
381 static int nfs4_nra = 4;
383 static int nfs4_do_symlink_cache = 1;
385 static int nfs4_pathconf_disable_cache = 0;
388 * These are the vnode ops routines which implement the vnode interface to
389 * the networked file system. These routines just take their parameters,
390 * make them look networkish by putting the right info into interface structs,
391 * and then calling the appropriate remote routine(s) to do the work.
393 * Note on directory name lookup cacheing: If we detect a stale fhandle,
394 * we purge the directory cache relative to that vnode. This way, the
395 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
396 * more details on rnode locking.
399 const struct vnodeops nfs4_vnodeops = {
400 .vnop_name = "nfs4",
401 .vop_open = nfs4_open,
402 .vop_close = nfs4_close,
403 .vop_read = nfs4_read,
404 .vop_write = nfs4_write,
405 .vop_ioctl = nfs4_ioctl,
406 .vop_getattr = nfs4_getattr,
407 .vop_setattr = nfs4_setattr,
408 .vop_access = nfs4_access,
409 .vop_lookup = nfs4_lookup,
410 .vop_create = nfs4_create,
411 .vop_remove = nfs4_remove,
412 .vop_link = nfs4_link,
413 .vop_rename = nfs4_rename,
414 .vop_mkdir = nfs4_mkdir,
415 .vop_rmdir = nfs4_rmdir,
416 .vop_readdir = nfs4_readdir,
417 .vop_symlink = nfs4_symlink,
418 .vop_readlink = nfs4_readlink,
419 .vop_fsync = nfs4_fsync,
420 .vop_inactive = nfs4_inactive,
421 .vop_fid = nfs4_fid,
422 .vop_rwlock = nfs4_rwlock,
423 .vop_rwunlock = nfs4_rwunlock,
424 .vop_seek = nfs4_seek,
425 .vop_frlock = nfs4_frlock,
426 .vop_space = nfs4_space,
427 .vop_realvp = nfs4_realvp,
428 .vop_getpage = nfs4_getpage,
429 .vop_putpage = nfs4_putpage,
430 .vop_map = nfs4_map,
431 .vop_addmap = nfs4_addmap,
432 .vop_delmap = nfs4_delmap,
433 /* no separate nfs4_dump */
434 .vop_dump = nfs_dump,
435 .vop_pathconf = nfs4_pathconf,
436 .vop_pageio = nfs4_pageio,
437 .vop_dispose = nfs4_dispose,
438 .vop_setsecattr = nfs4_setsecattr,
439 .vop_getsecattr = nfs4_getsecattr,
440 .vop_shrlock = nfs4_shrlock,
441 .vop_vnevent = fs_vnevent_support,
445 * The following are subroutines and definitions to set args or get res
446 * for the different nfsv4 ops
449 void
450 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
452 int i;
454 for (i = 0; i < arglen; i++) {
455 if (argop[i].argop == OP_LOOKUP) {
456 kmem_free(
457 argop[i].nfs_argop4_u.oplookup.
458 objname.utf8string_val,
459 argop[i].nfs_argop4_u.oplookup.
460 objname.utf8string_len);
465 static void
466 nfs4args_lock_free(nfs_argop4 *argop)
468 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
470 if (locker->new_lock_owner == TRUE) {
471 open_to_lock_owner4 *open_owner;
473 open_owner = &locker->locker4_u.open_owner;
474 if (open_owner->lock_owner.owner_val != NULL) {
475 kmem_free(open_owner->lock_owner.owner_val,
476 open_owner->lock_owner.owner_len);
481 static void
482 nfs4args_lockt_free(nfs_argop4 *argop)
484 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
486 if (lowner->owner_val != NULL) {
487 kmem_free(lowner->owner_val, lowner->owner_len);
491 static void
492 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
493 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
494 nfs4_stateid_types_t *sid_types)
496 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
497 mntinfo4_t *mi;
499 argop->argop = OP_SETATTR;
501 * The stateid is set to 0 if client is not modifying the size
502 * and otherwise to whatever nfs4_get_stateid() returns.
504 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
505 * state struct could be found for the process/file pair. We may
506 * want to change this in the future (by OPENing the file). See
507 * bug # 4474852.
509 if (vap->va_mask & AT_SIZE) {
511 ASSERT(rp != NULL);
512 mi = VTOMI4(RTOV4(rp));
514 argop->nfs_argop4_u.opsetattr.stateid =
515 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
516 OP_SETATTR, sid_types, FALSE);
517 } else {
518 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
519 sizeof (stateid4));
522 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
523 if (*error)
524 bzero(attr, sizeof (*attr));
527 static void
528 nfs4args_setattr_free(nfs_argop4 *argop)
530 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
533 static int
534 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
535 bitmap4 supp)
537 fattr4 *attr;
538 int error = 0;
540 argop->argop = op;
541 switch (op) {
542 case OP_VERIFY:
543 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
544 break;
545 case OP_NVERIFY:
546 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
547 break;
548 default:
549 return (EINVAL);
551 if (!error)
552 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
553 if (error)
554 bzero(attr, sizeof (*attr));
555 return (error);
558 static void
559 nfs4args_verify_free(nfs_argop4 *argop)
561 switch (argop->argop) {
562 case OP_VERIFY:
563 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
564 break;
565 case OP_NVERIFY:
566 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
567 break;
568 default:
569 break;
573 static void
574 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
575 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
577 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
578 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
580 argop->argop = OP_WRITE;
581 wargs->stable = stable;
582 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
583 mi, OP_WRITE, sid_tp);
584 wargs->mblk = NULL;
585 *wargs_pp = wargs;
588 void
589 nfs4args_copen_free(OPEN4cargs *open_args)
591 if (open_args->owner.owner_val) {
592 kmem_free(open_args->owner.owner_val,
593 open_args->owner.owner_len);
595 if ((open_args->opentype == OPEN4_CREATE) &&
596 (open_args->mode != EXCLUSIVE4)) {
597 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
602 * XXX: This is referenced in modstubs.s
604 const struct vnodeops *
605 nfs4_getvnodeops(void)
607 return (&nfs4_vnodeops);
611 * The OPEN operation opens a regular file.
613 /*ARGSUSED3*/
614 static int
615 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
617 vnode_t *dvp = NULL;
618 rnode4_t *rp, *drp;
619 int error;
620 int just_been_created;
621 char fn[MAXNAMELEN];
623 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
624 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
625 return (EIO);
626 rp = VTOR4(*vpp);
629 * Check to see if opening something besides a regular file;
630 * if so skip the OTW call
632 if ((*vpp)->v_type != VREG) {
633 error = nfs4_open_non_reg_file(vpp, flag, cr);
634 return (error);
638 * XXX - would like a check right here to know if the file is
639 * executable or not, so as to skip OTW
642 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
643 return (error);
645 drp = VTOR4(dvp);
646 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
647 return (EINTR);
649 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
650 nfs_rw_exit(&drp->r_rwlock);
651 return (error);
655 * See if this file has just been CREATEd.
656 * If so, clear the flag and update the dnlc, which was previously
657 * skipped in nfs4_create.
658 * XXX need better serilization on this.
659 * XXX move this into the nf4open_otw call, after we have
660 * XXX acquired the open owner seqid sync.
662 mutex_enter(&rp->r_statev4_lock);
663 if (rp->created_v4) {
664 rp->created_v4 = 0;
665 mutex_exit(&rp->r_statev4_lock);
667 dnlc_update(dvp, fn, *vpp);
668 /* This is needed so we don't bump the open ref count */
669 just_been_created = 1;
670 } else {
671 mutex_exit(&rp->r_statev4_lock);
672 just_been_created = 0;
676 * If caller specified O_TRUNC/FTRUNC, then be sure to set
677 * FWRITE (to drive successful setattr(size=0) after open)
679 if (flag & FTRUNC)
680 flag |= FWRITE;
682 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
683 just_been_created);
685 if (!error && !((*vpp)->v_flag & VROOT))
686 dnlc_update(dvp, fn, *vpp);
688 nfs_rw_exit(&drp->r_rwlock);
690 /* release the hold from vtodv */
691 VN_RELE(dvp);
693 /* exchange the shadow for the master vnode, if needed */
695 if (error == 0 && IS_SHADOW(*vpp, rp))
696 sv_exchange(vpp);
698 return (error);
702 * See if there's a "lost open" request to be saved and recovered.
704 static void
705 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
706 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
707 vnode_t *dvp, OPEN4cargs *open_args)
709 vfs_t *vfsp;
710 char *srccfp;
712 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
714 if (error != ETIMEDOUT && error != EINTR &&
715 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
716 lost_rqstp->lr_op = 0;
717 return;
720 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
721 "nfs4open_save_lost_rqst: error %d", error));
723 lost_rqstp->lr_op = OP_OPEN;
726 * The vp (if it is not NULL) and dvp are held and rele'd via
727 * the recovery code. See nfs4_save_lost_rqst.
729 lost_rqstp->lr_vp = vp;
730 lost_rqstp->lr_dvp = dvp;
731 lost_rqstp->lr_oop = oop;
732 lost_rqstp->lr_osp = NULL;
733 lost_rqstp->lr_lop = NULL;
734 lost_rqstp->lr_cr = cr;
735 lost_rqstp->lr_flk = NULL;
736 lost_rqstp->lr_oacc = open_args->share_access;
737 lost_rqstp->lr_odeny = open_args->share_deny;
738 lost_rqstp->lr_oclaim = open_args->claim;
739 if (open_args->claim == CLAIM_DELEGATE_CUR) {
740 lost_rqstp->lr_ostateid =
741 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
742 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
743 } else {
744 srccfp = open_args->open_claim4_u.cfile;
746 lost_rqstp->lr_ofile.utf8string_len = 0;
747 lost_rqstp->lr_ofile.utf8string_val = NULL;
748 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
749 lost_rqstp->lr_putfirst = FALSE;
752 struct nfs4_excl_time {
753 uint32 seconds;
754 uint32 nseconds;
758 * The OPEN operation creates and/or opens a regular file
760 * ARGSUSED
762 static int
763 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
764 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
765 enum createmode4 createmode, int file_just_been_created)
767 rnode4_t *rp;
768 rnode4_t *drp = VTOR4(dvp);
769 vnode_t *vp = NULL;
770 vnode_t *vpi = *vpp;
771 bool_t needrecov = FALSE;
773 int doqueue = 1;
775 COMPOUND4args_clnt args;
776 COMPOUND4res_clnt res;
777 nfs_argop4 *argop;
778 nfs_resop4 *resop;
779 int argoplist_size;
780 int idx_open, idx_fattr;
782 GETFH4res *gf_res = NULL;
783 OPEN4res *op_res = NULL;
784 nfs4_ga_res_t *garp;
785 fattr4 *attr = NULL;
786 struct nfs4_excl_time verf;
787 bool_t did_excl_setup = FALSE;
788 int created_osp;
790 OPEN4cargs *open_args;
791 nfs4_open_owner_t *oop = NULL;
792 nfs4_open_stream_t *osp = NULL;
793 seqid4 seqid = 0;
794 bool_t retry_open = FALSE;
795 nfs4_recov_state_t recov_state;
796 nfs4_lost_rqst_t lost_rqst;
797 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
798 hrtime_t t;
799 int acc = 0;
800 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
801 cred_t *ncr = NULL;
803 nfs4_sharedfh_t *otw_sfh;
804 nfs4_sharedfh_t *orig_sfh;
805 int fh_differs = 0;
806 int numops, setgid_flag;
807 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
810 * Make sure we properly deal with setting the right gid on
811 * a newly created file to reflect the parent's setgid bit
813 setgid_flag = 0;
814 if (create_flag && in_va) {
817 * If there is grpid mount flag used or
818 * the parent's directory has the setgid bit set
819 * _and_ the client was able to get a valid mapping
820 * for the parent dir's owner_group, we want to
821 * append NVERIFY(owner_group == dva.va_gid) and
822 * SETATTR to the CREATE compound.
824 mutex_enter(&drp->r_statelock);
825 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
826 drp->r_attr.va_mode & VSGID) &&
827 drp->r_attr.va_gid != GID_NOBODY) {
828 in_va->va_mask |= AT_GID;
829 in_va->va_gid = drp->r_attr.va_gid;
830 setgid_flag = 1;
832 mutex_exit(&drp->r_statelock);
836 * Normal/non-create compound:
837 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
839 * Open(create) compound no setgid:
840 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
841 * RESTOREFH + GETATTR
843 * Open(create) setgid:
844 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
845 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
846 * NVERIFY(grp) + SETATTR
848 if (setgid_flag) {
849 numops = 10;
850 idx_open = 1;
851 idx_fattr = 3;
852 } else if (create_flag) {
853 numops = 7;
854 idx_open = 2;
855 idx_fattr = 4;
856 } else {
857 numops = 4;
858 idx_open = 1;
859 idx_fattr = 3;
862 args.array_len = numops;
863 argoplist_size = numops * sizeof (nfs_argop4);
864 argop = kmem_alloc(argoplist_size, KM_SLEEP);
866 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
867 "open %s open flag 0x%x cred %p", file_name, open_flag,
868 (void *)cr));
870 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
871 if (create_flag) {
873 * We are to create a file. Initialize the passed in vnode
874 * pointer.
876 vpi = NULL;
877 } else {
879 * Check to see if the client owns a read delegation and is
880 * trying to open for write. If so, then return the delegation
881 * to avoid the server doing a cb_recall and returning DELAY.
882 * NB - we don't use the statev4_lock here because we'd have
883 * to drop the lock anyway and the result would be stale.
885 if ((open_flag & FWRITE) &&
886 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
887 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
890 * If the file has a delegation, then do an access check up
891 * front. This avoids having to an access check later after
892 * we've already done start_op, which could deadlock.
894 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
895 if (open_flag & FREAD &&
896 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
897 acc |= VREAD;
898 if (open_flag & FWRITE &&
899 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
900 acc |= VWRITE;
904 drp = VTOR4(dvp);
906 recov_state.rs_flags = 0;
907 recov_state.rs_num_retry_despite_err = 0;
908 cred_otw = cr;
910 recov_retry:
911 fh_differs = 0;
912 nfs4_error_zinit(&e);
914 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
915 if (e.error) {
916 if (ncr != NULL)
917 crfree(ncr);
918 kmem_free(argop, argoplist_size);
919 return (e.error);
922 args.ctag = TAG_OPEN;
923 args.array_len = numops;
924 args.array = argop;
926 /* putfh directory fh */
927 argop[0].argop = OP_CPUTFH;
928 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
930 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
931 argop[idx_open].argop = OP_COPEN;
932 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
933 open_args->claim = CLAIM_NULL;
935 /* name of file */
936 open_args->open_claim4_u.cfile = file_name;
937 open_args->owner.owner_len = 0;
938 open_args->owner.owner_val = NULL;
940 if (create_flag) {
941 /* CREATE a file */
942 open_args->opentype = OPEN4_CREATE;
943 open_args->mode = createmode;
944 if (createmode == EXCLUSIVE4) {
945 if (did_excl_setup == FALSE) {
946 verf.seconds = zone_get_hostid(NULL);
947 if (verf.seconds != 0)
948 verf.nseconds = newnum();
949 else {
950 timestruc_t now;
952 gethrestime(&now);
953 verf.seconds = now.tv_sec;
954 verf.nseconds = now.tv_nsec;
957 * Since the server will use this value for the
958 * mtime, make sure that it can't overflow. Zero
959 * out the MSB. The actual value does not matter
960 * here, only its uniqeness.
962 verf.seconds &= INT32_MAX;
963 did_excl_setup = TRUE;
966 /* Now copy over verifier to OPEN4args. */
967 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
968 } else {
969 int v_error;
970 bitmap4 supp_attrs;
971 servinfo4_t *svp;
973 attr = &open_args->createhow4_u.createattrs;
975 svp = drp->r_server;
976 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
977 supp_attrs = svp->sv_supp_attrs;
978 nfs_rw_exit(&svp->sv_lock);
980 /* GUARDED4 or UNCHECKED4 */
981 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
982 supp_attrs);
983 if (v_error) {
984 bzero(attr, sizeof (*attr));
985 nfs4args_copen_free(open_args);
986 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
987 &recov_state, FALSE);
988 if (ncr != NULL)
989 crfree(ncr);
990 kmem_free(argop, argoplist_size);
991 return (v_error);
994 } else {
995 /* NO CREATE */
996 open_args->opentype = OPEN4_NOCREATE;
999 if (recov_state.rs_sp != NULL) {
1000 mutex_enter(&recov_state.rs_sp->s_lock);
1001 open_args->owner.clientid = recov_state.rs_sp->clientid;
1002 mutex_exit(&recov_state.rs_sp->s_lock);
1003 } else {
1004 /* XXX should we just fail here? */
1005 open_args->owner.clientid = 0;
1009 * This increments oop's ref count or creates a temporary 'just_created'
1010 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1011 * completes.
1013 mutex_enter(&VTOMI4(dvp)->mi_lock);
1015 /* See if a permanent or just created open owner exists */
1016 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1017 if (!oop) {
1019 * This open owner does not exist so create a temporary
1020 * just created one.
1022 oop = create_open_owner(cr, VTOMI4(dvp));
1023 ASSERT(oop != NULL);
1025 mutex_exit(&VTOMI4(dvp)->mi_lock);
1027 /* this length never changes, do alloc before seqid sync */
1028 open_args->owner.owner_len = sizeof (oop->oo_name);
1029 open_args->owner.owner_val =
1030 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1032 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1033 if (e.error == EAGAIN) {
1034 open_owner_rele(oop);
1035 nfs4args_copen_free(open_args);
1036 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1037 if (ncr != NULL) {
1038 crfree(ncr);
1039 ncr = NULL;
1041 goto recov_retry;
1044 /* Check to see if we need to do the OTW call */
1045 if (!create_flag) {
1046 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1047 file_just_been_created, &e.error, acc, &recov_state)) {
1050 * The OTW open is not necessary. Either
1051 * the open can succeed without it (eg.
1052 * delegation, error == 0) or the open
1053 * must fail due to an access failure
1054 * (error != 0). In either case, tidy
1055 * up and return.
1058 nfs4_end_open_seqid_sync(oop);
1059 open_owner_rele(oop);
1060 nfs4args_copen_free(open_args);
1061 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1062 if (ncr != NULL)
1063 crfree(ncr);
1064 kmem_free(argop, argoplist_size);
1065 return (e.error);
1069 bcopy(&oop->oo_name, open_args->owner.owner_val,
1070 open_args->owner.owner_len);
1072 seqid = nfs4_get_open_seqid(oop) + 1;
1073 open_args->seqid = seqid;
1074 open_args->share_access = 0;
1075 if (open_flag & FREAD)
1076 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1077 if (open_flag & FWRITE)
1078 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1079 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1084 * getfh w/sanity check for idx_open/idx_fattr
1086 ASSERT((idx_open + 1) == (idx_fattr - 1));
1087 argop[idx_open + 1].argop = OP_GETFH;
1089 /* getattr */
1090 argop[idx_fattr].argop = OP_GETATTR;
1091 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1092 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1094 if (setgid_flag) {
1095 vattr_t _v;
1096 servinfo4_t *svp;
1097 bitmap4 supp_attrs;
1099 svp = drp->r_server;
1100 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1101 supp_attrs = svp->sv_supp_attrs;
1102 nfs_rw_exit(&svp->sv_lock);
1105 * For setgid case, we need to:
1106 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1108 argop[4].argop = OP_SAVEFH;
1110 argop[5].argop = OP_CPUTFH;
1111 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1113 argop[6].argop = OP_GETATTR;
1114 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1115 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1117 argop[7].argop = OP_RESTOREFH;
1120 * nverify
1122 _v.va_mask = AT_GID;
1123 _v.va_gid = in_va->va_gid;
1124 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1125 supp_attrs))) {
1128 * setattr
1130 * We _know_ we're not messing with AT_SIZE or
1131 * AT_XTIME, so no need for stateid or flags.
1132 * Also we specify NULL rp since we're only
1133 * interested in setting owner_group attributes.
1135 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1136 supp_attrs, &e.error, 0);
1137 if (e.error)
1138 nfs4args_verify_free(&argop[8]);
1141 if (e.error) {
1143 * XXX - Revisit the last argument to nfs4_end_op()
1144 * once 5020486 is fixed.
1146 nfs4_end_open_seqid_sync(oop);
1147 open_owner_rele(oop);
1148 nfs4args_copen_free(open_args);
1149 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1150 if (ncr != NULL)
1151 crfree(ncr);
1152 kmem_free(argop, argoplist_size);
1153 return (e.error);
1155 } else if (create_flag) {
1156 argop[1].argop = OP_SAVEFH;
1158 argop[5].argop = OP_RESTOREFH;
1160 argop[6].argop = OP_GETATTR;
1161 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1162 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1165 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1166 "nfs4open_otw: %s call, nm %s, rp %s",
1167 needrecov ? "recov" : "first", file_name,
1168 rnode4info(VTOR4(dvp))));
1170 t = gethrtime();
1172 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1174 if (!e.error && nfs4_need_to_bump_seqid(&res))
1175 nfs4_set_open_seqid(seqid, oop, args.ctag);
1177 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1179 if (e.error || needrecov) {
1180 bool_t abort = FALSE;
1182 if (needrecov) {
1183 nfs4_bseqid_entry_t *bsep = NULL;
1185 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1186 cred_otw, vpi, dvp, open_args);
1188 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1189 bsep = nfs4_create_bseqid_entry(oop, NULL,
1190 vpi, 0, args.ctag, open_args->seqid);
1191 num_bseqid_retry--;
1194 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1195 NULL, lost_rqst.lr_op == OP_OPEN ?
1196 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1198 if (bsep)
1199 kmem_free(bsep, sizeof (*bsep));
1200 /* give up if we keep getting BAD_SEQID */
1201 if (num_bseqid_retry == 0)
1202 abort = TRUE;
1203 if (abort == TRUE && e.error == 0)
1204 e.error = geterrno4(res.status);
1206 nfs4_end_open_seqid_sync(oop);
1207 open_owner_rele(oop);
1208 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1209 nfs4args_copen_free(open_args);
1210 if (setgid_flag) {
1211 nfs4args_verify_free(&argop[8]);
1212 nfs4args_setattr_free(&argop[9]);
1214 if (!e.error)
1215 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1216 if (ncr != NULL) {
1217 crfree(ncr);
1218 ncr = NULL;
1220 if (!needrecov || abort == TRUE || e.error == EINTR ||
1221 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1222 kmem_free(argop, argoplist_size);
1223 return (e.error);
1225 goto recov_retry;
1229 * Will check and update lease after checking the rflag for
1230 * OPEN_CONFIRM in the successful OPEN call.
1232 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1235 * XXX what if we're crossing mount points from server1:/drp
1236 * to server2:/drp/rp.
1239 /* Signal our end of use of the open seqid */
1240 nfs4_end_open_seqid_sync(oop);
1243 * This will destroy the open owner if it was just created,
1244 * and no one else has put a reference on it.
1246 open_owner_rele(oop);
1247 if (create_flag && (createmode != EXCLUSIVE4) &&
1248 res.status == NFS4ERR_BADOWNER)
1249 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1251 e.error = geterrno4(res.status);
1252 nfs4args_copen_free(open_args);
1253 if (setgid_flag) {
1254 nfs4args_verify_free(&argop[8]);
1255 nfs4args_setattr_free(&argop[9]);
1257 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1258 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1260 * If the reply is NFS4ERR_ACCESS, it may be because
1261 * we are root (no root net access). If the real uid
1262 * is not root, then retry with the real uid instead.
1264 if (ncr != NULL) {
1265 crfree(ncr);
1266 ncr = NULL;
1268 if (res.status == NFS4ERR_ACCESS &&
1269 (ncr = crnetadjust(cred_otw)) != NULL) {
1270 cred_otw = ncr;
1271 goto recov_retry;
1273 kmem_free(argop, argoplist_size);
1274 return (e.error);
1277 resop = &res.array[idx_open]; /* open res */
1278 op_res = &resop->nfs_resop4_u.opopen;
1280 #ifdef DEBUG
1282 * verify attrset bitmap
1284 if (create_flag &&
1285 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1286 /* make sure attrset returned is what we asked for */
1287 /* XXX Ignore this 'error' for now */
1288 if (attr->attrmask != op_res->attrset)
1289 /* EMPTY */;
1291 #endif
1293 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1294 mutex_enter(&VTOMI4(dvp)->mi_lock);
1295 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1296 mutex_exit(&VTOMI4(dvp)->mi_lock);
1299 resop = &res.array[idx_open + 1]; /* getfh res */
1300 gf_res = &resop->nfs_resop4_u.opgetfh;
1302 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1305 * The open stateid has been updated on the server but not
1306 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1307 * flush_pages->fop_putpage->...->nfs4write where we will issue an OTW
1308 * WRITE call. That, however, will use the old stateid, so go ahead
1309 * and upate the open stateid now, before any call to makenfs4node.
1311 if (vpi) {
1312 nfs4_open_stream_t *tmp_osp;
1313 rnode4_t *tmp_rp = VTOR4(vpi);
1315 tmp_osp = find_open_stream(oop, tmp_rp);
1316 if (tmp_osp) {
1317 tmp_osp->open_stateid = op_res->stateid;
1318 mutex_exit(&tmp_osp->os_sync_lock);
1319 open_stream_rele(tmp_osp, tmp_rp);
1323 * We must determine if the file handle given by the otw open
1324 * is the same as the file handle which was passed in with
1325 * *vpp. This case can be reached if the file we are trying
1326 * to open has been removed and another file has been created
1327 * having the same file name. The passed in vnode is released
1328 * later.
1330 orig_sfh = VTOR4(vpi)->r_fh;
1331 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1334 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1336 if (create_flag || fh_differs) {
1337 int rnode_err = 0;
1339 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1340 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1342 if (e.error)
1343 PURGE_ATTRCACHE4(vp);
1345 * For the newly created vp case, make sure the rnode
1346 * isn't bad before using it.
1348 mutex_enter(&(VTOR4(vp))->r_statelock);
1349 if (VTOR4(vp)->r_flags & R4RECOVERR)
1350 rnode_err = EIO;
1351 mutex_exit(&(VTOR4(vp))->r_statelock);
1353 if (rnode_err) {
1354 nfs4_end_open_seqid_sync(oop);
1355 nfs4args_copen_free(open_args);
1356 if (setgid_flag) {
1357 nfs4args_verify_free(&argop[8]);
1358 nfs4args_setattr_free(&argop[9]);
1360 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1361 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1362 needrecov);
1363 open_owner_rele(oop);
1364 VN_RELE(vp);
1365 if (ncr != NULL)
1366 crfree(ncr);
1367 sfh4_rele(&otw_sfh);
1368 kmem_free(argop, argoplist_size);
1369 return (EIO);
1371 } else {
1372 vp = vpi;
1374 sfh4_rele(&otw_sfh);
1377 * It seems odd to get a full set of attrs and then not update
1378 * the object's attrcache in the non-create case. Create case uses
1379 * the attrs since makenfs4node checks to see if the attrs need to
1380 * be updated (and then updates them). The non-create case should
1381 * update attrs also.
1383 if (! create_flag && ! fh_differs && !e.error) {
1384 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1387 nfs4_error_zinit(&e);
1388 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1389 /* This does not do recovery for vp explicitly. */
1390 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1391 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1393 if (e.error || e.stat) {
1394 nfs4_end_open_seqid_sync(oop);
1395 nfs4args_copen_free(open_args);
1396 if (setgid_flag) {
1397 nfs4args_verify_free(&argop[8]);
1398 nfs4args_setattr_free(&argop[9]);
1400 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1401 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1402 needrecov);
1403 open_owner_rele(oop);
1404 if (create_flag || fh_differs) {
1405 /* rele the makenfs4node */
1406 VN_RELE(vp);
1408 if (ncr != NULL) {
1409 crfree(ncr);
1410 ncr = NULL;
1412 if (retry_open == TRUE) {
1413 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1414 "nfs4open_otw: retry the open since OPEN "
1415 "CONFIRM failed with error %d stat %d",
1416 e.error, e.stat));
1417 if (create_flag && createmode == GUARDED4) {
1418 NFS4_DEBUG(nfs4_client_recov_debug,
1419 (CE_NOTE, "nfs4open_otw: switch "
1420 "createmode from GUARDED4 to "
1421 "UNCHECKED4"));
1422 createmode = UNCHECKED4;
1424 goto recov_retry;
1426 if (!e.error) {
1427 if (create_flag && (createmode != EXCLUSIVE4) &&
1428 e.stat == NFS4ERR_BADOWNER)
1429 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1431 e.error = geterrno4(e.stat);
1433 kmem_free(argop, argoplist_size);
1434 return (e.error);
1438 rp = VTOR4(vp);
1440 mutex_enter(&rp->r_statev4_lock);
1441 if (create_flag)
1442 rp->created_v4 = 1;
1443 mutex_exit(&rp->r_statev4_lock);
1445 mutex_enter(&oop->oo_lock);
1446 /* Doesn't matter if 'oo_just_created' already was set as this */
1447 oop->oo_just_created = NFS4_PERM_CREATED;
1448 if (oop->oo_cred_otw)
1449 crfree(oop->oo_cred_otw);
1450 oop->oo_cred_otw = cred_otw;
1451 crhold(oop->oo_cred_otw);
1452 mutex_exit(&oop->oo_lock);
1454 /* returns with 'os_sync_lock' held */
1455 osp = find_or_create_open_stream(oop, rp, &created_osp);
1456 if (!osp) {
1457 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1458 "nfs4open_otw: failed to create an open stream"));
1459 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1460 "signal our end of use of the open seqid"));
1462 nfs4_end_open_seqid_sync(oop);
1463 open_owner_rele(oop);
1464 nfs4args_copen_free(open_args);
1465 if (setgid_flag) {
1466 nfs4args_verify_free(&argop[8]);
1467 nfs4args_setattr_free(&argop[9]);
1469 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1470 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1471 if (create_flag || fh_differs)
1472 VN_RELE(vp);
1473 if (ncr != NULL)
1474 crfree(ncr);
1476 kmem_free(argop, argoplist_size);
1477 return (EINVAL);
1481 osp->open_stateid = op_res->stateid;
1483 if (open_flag & FREAD)
1484 osp->os_share_acc_read++;
1485 if (open_flag & FWRITE)
1486 osp->os_share_acc_write++;
1487 osp->os_share_deny_none++;
1490 * Need to reset this bitfield for the possible case where we were
1491 * going to OTW CLOSE the file, got a non-recoverable error, and before
1492 * we could retry the CLOSE, OPENed the file again.
1494 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1495 osp->os_final_close = 0;
1496 osp->os_force_close = 0;
1497 #ifdef DEBUG
1498 if (osp->os_failed_reopen)
1499 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1500 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1501 (void *)osp, (void *)cr, rnode4info(rp)));
1502 #endif
1503 osp->os_failed_reopen = 0;
1505 mutex_exit(&osp->os_sync_lock);
1507 nfs4_end_open_seqid_sync(oop);
1509 if (created_osp && recov_state.rs_sp != NULL) {
1510 mutex_enter(&recov_state.rs_sp->s_lock);
1511 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1512 mutex_exit(&recov_state.rs_sp->s_lock);
1515 /* get rid of our reference to find oop */
1516 open_owner_rele(oop);
1518 open_stream_rele(osp, rp);
1520 /* accept delegation, if any */
1521 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1523 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1525 if (createmode == EXCLUSIVE4 &&
1526 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1527 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1528 " EXCLUSIVE4: sending a SETATTR"));
1530 * If doing an exclusive create, then generate
1531 * a SETATTR to set the initial attributes.
1532 * Try to set the mtime and the atime to the
1533 * server's current time. It is somewhat
1534 * expected that these fields will be used to
1535 * store the exclusive create cookie. If not,
1536 * server implementors will need to know that
1537 * a SETATTR will follow an exclusive create
1538 * and the cookie should be destroyed if
1539 * appropriate.
1541 * The AT_GID and AT_SIZE bits are turned off
1542 * so that the SETATTR request will not attempt
1543 * to process these. The gid will be set
1544 * separately if appropriate. The size is turned
1545 * off because it is assumed that a new file will
1546 * be created empty and if the file wasn't empty,
1547 * then the exclusive create will have failed
1548 * because the file must have existed already.
1549 * Therefore, no truncate operation is needed.
1551 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1552 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1554 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1555 if (e.error) {
1557 * Couldn't correct the attributes of
1558 * the newly created file and the
1559 * attributes are wrong. Remove the
1560 * file and return an error to the
1561 * application.
1563 /* XXX will this take care of client state ? */
1564 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1565 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1566 " remove file", e.error));
1567 VN_RELE(vp);
1568 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1570 * Since we've reled the vnode and removed
1571 * the file we now need to return the error.
1572 * At this point we don't want to update the
1573 * dircaches, call nfs4_waitfor_purge_complete
1574 * or set vpp to vp so we need to skip these
1575 * as well.
1577 goto skip_update_dircaches;
1582 * If we created or found the correct vnode, due to create_flag or
1583 * fh_differs being set, then update directory cache attribute, readdir
1584 * and dnlc caches.
1586 if (create_flag || fh_differs) {
1587 dirattr_info_t dinfo, *dinfop;
1590 * Make sure getattr succeeded before using results.
1591 * note: op 7 is getattr(dir) for both flavors of
1592 * open(create).
1594 if (create_flag && res.status == NFS4_OK) {
1595 dinfo.di_time_call = t;
1596 dinfo.di_cred = cr;
1597 dinfo.di_garp =
1598 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1599 dinfop = &dinfo;
1600 } else {
1601 dinfop = NULL;
1604 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1605 dinfop);
1609 * If the page cache for this file was flushed from actions
1610 * above, it was done asynchronously and if that is true,
1611 * there is a need to wait here for it to complete. This must
1612 * be done outside of start_fop/end_fop.
1614 (void) nfs4_waitfor_purge_complete(vp);
1617 * It is implicit that we are in the open case (create_flag == 0) since
1618 * fh_differs can only be set to a non-zero value in the open case.
1620 if (fh_differs != 0 && vpi != NULL)
1621 VN_RELE(vpi);
1624 * Be sure to set *vpp to the correct value before returning.
1626 *vpp = vp;
1628 skip_update_dircaches:
1630 nfs4args_copen_free(open_args);
1631 if (setgid_flag) {
1632 nfs4args_verify_free(&argop[8]);
1633 nfs4args_setattr_free(&argop[9]);
1635 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1637 if (ncr)
1638 crfree(ncr);
1639 kmem_free(argop, argoplist_size);
1640 return (e.error);
1644 * Reopen an open instance. cf. nfs4open_otw().
1646 * Errors are returned by the nfs4_error_t parameter.
1647 * - ep->error contains an errno value or zero.
1648 * - if it is zero, ep->stat is set to an NFS status code, if any.
1649 * If the file could not be reopened, but the caller should continue, the
1650 * file is marked dead and no error values are returned. If the caller
1651 * should stop recovering open files and start over, either the ep->error
1652 * value or ep->stat will indicate an error (either something that requires
1653 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1654 * filehandles) may be handled silently by this routine.
1655 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1656 * will be started, so the caller should not do it.
1658 * Gotos:
1659 * - kill_file : reopen failed in such a fashion to constitute marking the
1660 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1661 * is for cases where recovery is not possible.
1662 * - failed_reopen : same as above, except that the file has already been
1663 * marked dead, so no need to do it again.
1664 * - bailout : reopen failed but we are able to recover and retry the reopen -
1665 * either within this function immediately or via the calling function.
1668 void
1669 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1670 open_claim_type4 claim, bool_t frc_use_claim_previous,
1671 bool_t is_recov)
1673 COMPOUND4args_clnt args;
1674 COMPOUND4res_clnt res;
1675 nfs_argop4 argop[4];
1676 nfs_resop4 *resop;
1677 OPEN4res *op_res = NULL;
1678 OPEN4cargs *open_args;
1679 GETFH4res *gf_res;
1680 rnode4_t *rp = VTOR4(vp);
1681 int doqueue = 1;
1682 cred_t *cr = NULL, *cred_otw = NULL;
1683 nfs4_open_owner_t *oop = NULL;
1684 seqid4 seqid;
1685 nfs4_ga_res_t *garp;
1686 char fn[MAXNAMELEN];
1687 nfs4_recov_state_t recov = {NULL, 0};
1688 nfs4_lost_rqst_t lost_rqst;
1689 mntinfo4_t *mi = VTOMI4(vp);
1690 bool_t abort;
1691 char *failed_msg = "";
1692 int fh_different;
1693 hrtime_t t;
1694 nfs4_bseqid_entry_t *bsep = NULL;
1696 ASSERT(nfs4_consistent_type(vp));
1697 ASSERT(nfs_zone() == mi->mi_zone);
1699 nfs4_error_zinit(ep);
1701 /* this is the cred used to find the open owner */
1702 cr = state_to_cred(osp);
1703 if (cr == NULL) {
1704 failed_msg = "Couldn't reopen: no cred";
1705 goto kill_file;
1707 /* use this cred for OTW operations */
1708 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1710 top:
1711 nfs4_error_zinit(ep);
1713 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1714 /* File system has been unmounted, quit */
1715 ep->error = EIO;
1716 failed_msg = "Couldn't reopen: file system has been unmounted";
1717 goto kill_file;
1720 oop = osp->os_open_owner;
1722 ASSERT(oop != NULL);
1723 if (oop == NULL) { /* be defensive in non-DEBUG */
1724 failed_msg = "can't reopen: no open owner";
1725 goto kill_file;
1727 open_owner_hold(oop);
1729 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1730 if (ep->error) {
1731 open_owner_rele(oop);
1732 oop = NULL;
1733 goto bailout;
1737 * If the rnode has a delegation and the delegation has been
1738 * recovered and the server didn't request a recall and the caller
1739 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1740 * recovery) and the rnode hasn't been marked dead, then install
1741 * the delegation stateid in the open stream. Otherwise, proceed
1742 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1744 mutex_enter(&rp->r_statev4_lock);
1745 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1746 !rp->r_deleg_return_pending &&
1747 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1748 !rp->r_deleg_needs_recall &&
1749 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1750 !(rp->r_flags & R4RECOVERR)) {
1751 mutex_enter(&osp->os_sync_lock);
1752 osp->os_delegation = 1;
1753 osp->open_stateid = rp->r_deleg_stateid;
1754 mutex_exit(&osp->os_sync_lock);
1755 mutex_exit(&rp->r_statev4_lock);
1756 goto bailout;
1758 mutex_exit(&rp->r_statev4_lock);
1761 * If the file failed recovery, just quit. This failure need not
1762 * affect other reopens, so don't return an error.
1764 mutex_enter(&rp->r_statelock);
1765 if (rp->r_flags & R4RECOVERR) {
1766 mutex_exit(&rp->r_statelock);
1767 ep->error = 0;
1768 goto failed_reopen;
1770 mutex_exit(&rp->r_statelock);
1773 * argop is empty here
1775 * PUTFH, OPEN, GETATTR
1777 args.ctag = TAG_REOPEN;
1778 args.array_len = 4;
1779 args.array = argop;
1781 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1782 "nfs4_reopen: file is type %d, id %s",
1783 vp->v_type, rnode4info(VTOR4(vp))));
1785 argop[0].argop = OP_CPUTFH;
1787 if (claim != CLAIM_PREVIOUS) {
1789 * if this is a file mount then
1790 * use the mntinfo parentfh
1792 argop[0].nfs_argop4_u.opcputfh.sfh =
1793 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1794 VTOSV(vp)->sv_dfh;
1795 } else {
1796 /* putfh fh to reopen */
1797 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1800 argop[1].argop = OP_COPEN;
1801 open_args = &argop[1].nfs_argop4_u.opcopen;
1802 open_args->claim = claim;
1804 if (claim == CLAIM_NULL) {
1806 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1807 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1808 "failed for vp 0x%p for CLAIM_NULL with %m",
1809 (void *)vp);
1810 failed_msg = "Couldn't reopen: vtoname failed for "
1811 "CLAIM_NULL";
1812 /* nothing allocated yet */
1813 goto kill_file;
1816 open_args->open_claim4_u.cfile = fn;
1817 } else if (claim == CLAIM_PREVIOUS) {
1820 * We have two cases to deal with here:
1821 * 1) We're being called to reopen files in order to satisfy
1822 * a lock operation request which requires us to explicitly
1823 * reopen files which were opened under a delegation. If
1824 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1825 * that case, frc_use_claim_previous is TRUE and we must
1826 * use the rnode's current delegation type (r_deleg_type).
1827 * 2) We're reopening files during some form of recovery.
1828 * In this case, frc_use_claim_previous is FALSE and we
1829 * use the delegation type appropriate for recovery
1830 * (r_deleg_needs_recovery).
1832 mutex_enter(&rp->r_statev4_lock);
1833 open_args->open_claim4_u.delegate_type =
1834 frc_use_claim_previous ?
1835 rp->r_deleg_type :
1836 rp->r_deleg_needs_recovery;
1837 mutex_exit(&rp->r_statev4_lock);
1839 } else if (claim == CLAIM_DELEGATE_CUR) {
1841 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1842 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1843 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1844 "with %m", (void *)vp);
1845 failed_msg = "Couldn't reopen: vtoname failed for "
1846 "CLAIM_DELEGATE_CUR";
1847 /* nothing allocated yet */
1848 goto kill_file;
1851 mutex_enter(&rp->r_statev4_lock);
1852 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1853 rp->r_deleg_stateid;
1854 mutex_exit(&rp->r_statev4_lock);
1856 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1858 open_args->opentype = OPEN4_NOCREATE;
1859 open_args->owner.clientid = mi2clientid(mi);
1860 open_args->owner.owner_len = sizeof (oop->oo_name);
1861 open_args->owner.owner_val =
1862 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1863 bcopy(&oop->oo_name, open_args->owner.owner_val,
1864 open_args->owner.owner_len);
1865 open_args->share_access = 0;
1866 open_args->share_deny = 0;
1868 mutex_enter(&osp->os_sync_lock);
1869 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1870 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1871 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1872 (void *)osp, (void *)rp, osp->os_share_acc_read,
1873 osp->os_share_acc_write, osp->os_open_ref_count,
1874 osp->os_mmap_read, osp->os_mmap_write, claim));
1876 if (osp->os_share_acc_read || osp->os_mmap_read)
1877 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1878 if (osp->os_share_acc_write || osp->os_mmap_write)
1879 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1880 if (osp->os_share_deny_read)
1881 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1882 if (osp->os_share_deny_write)
1883 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1884 mutex_exit(&osp->os_sync_lock);
1886 seqid = nfs4_get_open_seqid(oop) + 1;
1887 open_args->seqid = seqid;
1889 /* Construct the getfh part of the compound */
1890 argop[2].argop = OP_GETFH;
1892 /* Construct the getattr part of the compound */
1893 argop[3].argop = OP_GETATTR;
1894 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1895 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1897 t = gethrtime();
1899 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1901 if (ep->error) {
1902 if (!is_recov && !frc_use_claim_previous &&
1903 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1904 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1905 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1906 cred_otw, vp, NULL, open_args);
1907 abort = nfs4_start_recovery(ep,
1908 VTOMI4(vp), vp, NULL, NULL,
1909 lost_rqst.lr_op == OP_OPEN ?
1910 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1911 nfs4args_copen_free(open_args);
1912 goto bailout;
1915 nfs4args_copen_free(open_args);
1917 if (ep->error == EACCES && cred_otw != cr) {
1918 crfree(cred_otw);
1919 cred_otw = cr;
1920 crhold(cred_otw);
1921 nfs4_end_open_seqid_sync(oop);
1922 open_owner_rele(oop);
1923 oop = NULL;
1924 goto top;
1926 if (ep->error == ETIMEDOUT)
1927 goto bailout;
1928 failed_msg = "Couldn't reopen: rpc error";
1929 goto kill_file;
1932 if (nfs4_need_to_bump_seqid(&res))
1933 nfs4_set_open_seqid(seqid, oop, args.ctag);
1935 switch (res.status) {
1936 case NFS4_OK:
1937 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1938 mutex_enter(&rp->r_statelock);
1939 rp->r_delay_interval = 0;
1940 mutex_exit(&rp->r_statelock);
1942 break;
1943 case NFS4ERR_BAD_SEQID:
1944 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1945 args.ctag, open_args->seqid);
1947 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1948 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1949 NULL, OP_OPEN, bsep, NULL, NULL);
1951 nfs4args_copen_free(open_args);
1952 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1953 nfs4_end_open_seqid_sync(oop);
1954 open_owner_rele(oop);
1955 oop = NULL;
1956 kmem_free(bsep, sizeof (*bsep));
1958 goto kill_file;
1959 case NFS4ERR_NO_GRACE:
1960 nfs4args_copen_free(open_args);
1961 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1962 nfs4_end_open_seqid_sync(oop);
1963 open_owner_rele(oop);
1964 oop = NULL;
1965 if (claim == CLAIM_PREVIOUS) {
1967 * Retry as a plain open. We don't need to worry about
1968 * checking the changeinfo: it is acceptable for a
1969 * client to re-open a file and continue processing
1970 * (in the absence of locks).
1972 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1973 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1974 "will retry as CLAIM_NULL"));
1975 claim = CLAIM_NULL;
1976 nfs4_mi_kstat_inc_no_grace(mi);
1977 goto top;
1979 failed_msg =
1980 "Couldn't reopen: tried reclaim outside grace period. ";
1981 goto kill_file;
1982 case NFS4ERR_GRACE:
1983 nfs4_set_grace_wait(mi);
1984 nfs4args_copen_free(open_args);
1985 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1986 nfs4_end_open_seqid_sync(oop);
1987 open_owner_rele(oop);
1988 oop = NULL;
1989 ep->error = nfs4_wait_for_grace(mi, &recov);
1990 if (ep->error != 0)
1991 goto bailout;
1992 goto top;
1993 case NFS4ERR_DELAY:
1994 nfs4_set_delay_wait(vp);
1995 nfs4args_copen_free(open_args);
1996 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1997 nfs4_end_open_seqid_sync(oop);
1998 open_owner_rele(oop);
1999 oop = NULL;
2000 ep->error = nfs4_wait_for_delay(vp, &recov);
2001 nfs4_mi_kstat_inc_delay(mi);
2002 if (ep->error != 0)
2003 goto bailout;
2004 goto top;
2005 case NFS4ERR_FHEXPIRED:
2006 /* recover filehandle and retry */
2007 abort = nfs4_start_recovery(ep,
2008 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2009 nfs4args_copen_free(open_args);
2010 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2011 nfs4_end_open_seqid_sync(oop);
2012 open_owner_rele(oop);
2013 oop = NULL;
2014 if (abort == FALSE)
2015 goto top;
2016 failed_msg = "Couldn't reopen: recovery aborted";
2017 goto kill_file;
2018 case NFS4ERR_RESOURCE:
2019 case NFS4ERR_STALE_CLIENTID:
2020 case NFS4ERR_WRONGSEC:
2021 case NFS4ERR_EXPIRED:
2023 * Do not mark the file dead and let the calling
2024 * function initiate recovery.
2026 nfs4args_copen_free(open_args);
2027 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2028 nfs4_end_open_seqid_sync(oop);
2029 open_owner_rele(oop);
2030 oop = NULL;
2031 goto bailout;
2032 case NFS4ERR_ACCESS:
2033 if (cred_otw != cr) {
2034 crfree(cred_otw);
2035 cred_otw = cr;
2036 crhold(cred_otw);
2037 nfs4args_copen_free(open_args);
2038 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2039 nfs4_end_open_seqid_sync(oop);
2040 open_owner_rele(oop);
2041 oop = NULL;
2042 goto top;
2044 /* fall through */
2045 default:
2046 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2047 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2048 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2049 rnode4info(VTOR4(vp))));
2050 failed_msg = "Couldn't reopen: NFSv4 error";
2051 nfs4args_copen_free(open_args);
2052 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2053 goto kill_file;
2056 resop = &res.array[1]; /* open res */
2057 op_res = &resop->nfs_resop4_u.opopen;
2059 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2062 * Check if the path we reopened really is the same
2063 * file. We could end up in a situation where the file
2064 * was removed and a new file created with the same name.
2066 resop = &res.array[2];
2067 gf_res = &resop->nfs_resop4_u.opgetfh;
2068 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2069 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2070 if (fh_different) {
2071 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2072 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2073 /* Oops, we don't have the same file */
2074 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2075 failed_msg = "Couldn't reopen: Persistent "
2076 "file handle changed";
2077 else
2078 failed_msg = "Couldn't reopen: Volatile "
2079 "(no expire on open) file handle changed";
2081 nfs4args_copen_free(open_args);
2082 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2083 nfs_rw_exit(&mi->mi_fh_lock);
2084 goto kill_file;
2086 } else {
2088 * We have volatile file handles that don't compare.
2089 * If the fids are the same then we assume that the
2090 * file handle expired but the rnode still refers to
2091 * the same file object.
2093 * First check that we have fids or not.
2094 * If we don't we have a dumb server so we will
2095 * just assume every thing is ok for now.
2097 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2098 rp->r_attr.va_mask & AT_NODEID &&
2099 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2101 * We have fids, but they don't
2102 * compare. So kill the file.
2104 failed_msg =
2105 "Couldn't reopen: file handle changed"
2106 " due to mismatched fids";
2107 nfs4args_copen_free(open_args);
2108 xdr_free(xdr_COMPOUND4res_clnt,
2109 (caddr_t)&res);
2110 nfs_rw_exit(&mi->mi_fh_lock);
2111 goto kill_file;
2112 } else {
2114 * We have volatile file handles that refers
2115 * to the same file (at least they have the
2116 * same fid) or we don't have fids so we
2117 * can't tell. :(. We'll be a kind and accepting
2118 * client so we'll update the rnode's file
2119 * handle with the otw handle.
2121 * We need to drop mi->mi_fh_lock since
2122 * sh4_update acquires it. Since there is
2123 * only one recovery thread there is no
2124 * race.
2126 nfs_rw_exit(&mi->mi_fh_lock);
2127 sfh4_update(rp->r_fh, &gf_res->object);
2130 } else {
2131 nfs_rw_exit(&mi->mi_fh_lock);
2134 ASSERT(nfs4_consistent_type(vp));
2137 * If the server wanted an OPEN_CONFIRM but that fails, just start
2138 * over. Presumably if there is a persistent error it will show up
2139 * when we resend the OPEN.
2141 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2142 bool_t retry_open = FALSE;
2144 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2145 cred_otw, is_recov, &retry_open,
2146 oop, FALSE, ep, NULL);
2147 if (ep->error || ep->stat) {
2148 nfs4args_copen_free(open_args);
2149 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2150 nfs4_end_open_seqid_sync(oop);
2151 open_owner_rele(oop);
2152 oop = NULL;
2153 goto top;
2157 mutex_enter(&osp->os_sync_lock);
2158 osp->open_stateid = op_res->stateid;
2159 osp->os_delegation = 0;
2161 * Need to reset this bitfield for the possible case where we were
2162 * going to OTW CLOSE the file, got a non-recoverable error, and before
2163 * we could retry the CLOSE, OPENed the file again.
2165 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2166 osp->os_final_close = 0;
2167 osp->os_force_close = 0;
2168 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2169 osp->os_dc_openacc = open_args->share_access;
2170 mutex_exit(&osp->os_sync_lock);
2172 nfs4_end_open_seqid_sync(oop);
2174 /* accept delegation, if any */
2175 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2177 nfs4args_copen_free(open_args);
2179 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2181 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2183 ASSERT(nfs4_consistent_type(vp));
2185 open_owner_rele(oop);
2186 crfree(cr);
2187 crfree(cred_otw);
2188 return;
2190 kill_file:
2191 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2192 failed_reopen:
2193 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2194 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2195 (void *)osp, (void *)cr, rnode4info(rp)));
2196 mutex_enter(&osp->os_sync_lock);
2197 osp->os_failed_reopen = 1;
2198 mutex_exit(&osp->os_sync_lock);
2199 bailout:
2200 if (oop != NULL) {
2201 nfs4_end_open_seqid_sync(oop);
2202 open_owner_rele(oop);
2204 if (cr != NULL)
2205 crfree(cr);
2206 if (cred_otw != NULL)
2207 crfree(cred_otw);
2210 /* for . and .. OPENs */
2211 /* ARGSUSED */
2212 static int
2213 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2215 rnode4_t *rp;
2216 nfs4_ga_res_t gar;
2218 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2221 * If close-to-open consistency checking is turned off or
2222 * if there is no cached data, we can avoid
2223 * the over the wire getattr. Otherwise, force a
2224 * call to the server to get fresh attributes and to
2225 * check caches. This is required for close-to-open
2226 * consistency.
2228 rp = VTOR4(*vpp);
2229 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2230 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2231 return (0);
2233 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2237 * CLOSE a file
2239 /* ARGSUSED */
2240 static int
2241 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2242 caller_context_t *ct)
2244 rnode4_t *rp;
2245 int error = 0;
2246 int r_error = 0;
2247 int n4error = 0;
2248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2251 * Remove client state for this (lockowner, file) pair.
2252 * Issue otw v4 call to have the server do the same.
2255 rp = VTOR4(vp);
2258 * zone_enter(2) prevents processes from changing zones with NFS files
2259 * open; if we happen to get here from the wrong zone we can't do
2260 * anything over the wire.
2262 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2264 * We could attempt to clean up locks, except we're sure
2265 * that the current process didn't acquire any locks on
2266 * the file: any attempt to lock a file belong to another zone
2267 * will fail, and one can't lock an NFS file and then change
2268 * zones, as that fails too.
2270 * Returning an error here is the sane thing to do. A
2271 * subsequent call to VN_RELE() which translates to a
2272 * nfs4_inactive() will clean up state: if the zone of the
2273 * vnode's origin is still alive and kicking, the inactive
2274 * thread will handle the request (from the correct zone), and
2275 * everything (minus the OTW close call) should be OK. If the
2276 * zone is going away nfs4_async_inactive() will throw away
2277 * delegations, open streams and cached pages inline.
2279 return (EIO);
2283 * If we are using local locking for this filesystem, then
2284 * release all of the SYSV style record locks. Otherwise,
2285 * we are doing network locking and we need to release all
2286 * of the network locks. All of the locks held by this
2287 * process on this file are released no matter what the
2288 * incoming reference count is.
2290 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2291 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2292 cleanshares(vp, ttoproc(curthread)->p_pid);
2293 } else
2294 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2296 if (e.error) {
2297 struct lm_sysid *lmsid;
2298 lmsid = nfs4_find_sysid(VTOMI4(vp));
2299 if (lmsid == NULL) {
2300 DTRACE_PROBE2(unknown__sysid, int, e.error,
2301 vnode_t *, vp);
2302 } else {
2303 cleanlocks(vp, ttoproc(curthread)->p_pid,
2304 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2306 lm_rel_sysid(lmsid);
2308 return (e.error);
2311 if (count > 1)
2312 return (0);
2315 * If the file has been `unlinked', then purge the
2316 * DNLC so that this vnode will get reycled quicker
2317 * and the .nfs* file on the server will get removed.
2319 if (rp->r_unldvp != NULL)
2320 dnlc_purge_vp(vp);
2323 * If the file was open for write and there are pages,
2324 * do a synchronous flush and commit of all of the
2325 * dirty and uncommitted pages.
2327 ASSERT(!e.error);
2328 if ((flag & FWRITE) && nfs4_has_pages(vp))
2329 error = nfs4_putpage_commit(vp, 0, 0, cr);
2331 mutex_enter(&rp->r_statelock);
2332 r_error = rp->r_error;
2333 rp->r_error = 0;
2334 mutex_exit(&rp->r_statelock);
2337 * If this file type is one for which no explicit 'open' was
2338 * done, then bail now (ie. no need for protocol 'close'). If
2339 * there was an error w/the vm subsystem, return _that_ error,
2340 * otherwise, return any errors that may've been reported via
2341 * the rnode.
2343 if (vp->v_type != VREG)
2344 return (error ? error : r_error);
2347 * The sync putpage commit may have failed above, but since
2348 * we're working w/a regular file, we need to do the protocol
2349 * 'close' (nfs4close_one will figure out if an otw close is
2350 * needed or not). Report any errors _after_ doing the protocol
2351 * 'close'.
2353 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2354 n4error = e.error ? e.error : geterrno4(e.stat);
2357 * Error reporting prio (Hi -> Lo)
2359 * i) nfs4_putpage_commit (error)
2360 * ii) rnode's (r_error)
2361 * iii) nfs4close_one (n4error)
2363 return (error ? error : (r_error ? r_error : n4error));
2367 * Initialize *lost_rqstp.
2370 static void
2371 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2372 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2373 vnode_t *vp)
2375 if (error != ETIMEDOUT && error != EINTR &&
2376 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2377 lost_rqstp->lr_op = 0;
2378 return;
2381 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2382 "nfs4close_save_lost_rqst: error %d", error));
2384 lost_rqstp->lr_op = OP_CLOSE;
2386 * The vp is held and rele'd via the recovery code.
2387 * See nfs4_save_lost_rqst.
2389 lost_rqstp->lr_vp = vp;
2390 lost_rqstp->lr_dvp = NULL;
2391 lost_rqstp->lr_oop = oop;
2392 lost_rqstp->lr_osp = osp;
2393 ASSERT(osp != NULL);
2394 ASSERT(mutex_owned(&osp->os_sync_lock));
2395 osp->os_pending_close = 1;
2396 lost_rqstp->lr_lop = NULL;
2397 lost_rqstp->lr_cr = cr;
2398 lost_rqstp->lr_flk = NULL;
2399 lost_rqstp->lr_putfirst = FALSE;
2403 * Assumes you already have the open seqid sync grabbed as well as the
2404 * 'os_sync_lock'. Note: this will release the open seqid sync and
2405 * 'os_sync_lock' if client recovery starts. Calling functions have to
2406 * be prepared to handle this.
2408 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2409 * was needed and was started, and that the calling function should retry
2410 * this function; otherwise it is returned as 0.
2412 * Errors are returned via the nfs4_error_t parameter.
2414 static void
2415 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2416 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2417 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2419 COMPOUND4args_clnt args;
2420 COMPOUND4res_clnt res;
2421 CLOSE4args *close_args;
2422 nfs_resop4 *resop;
2423 nfs_argop4 argop[3];
2424 int doqueue = 1;
2425 mntinfo4_t *mi;
2426 seqid4 seqid;
2427 vnode_t *vp;
2428 bool_t needrecov = FALSE;
2429 nfs4_lost_rqst_t lost_rqst;
2430 hrtime_t t;
2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2434 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2436 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2438 /* Only set this to 1 if recovery is started */
2439 *recov = 0;
2441 /* do the OTW call to close the file */
2443 if (close_type == CLOSE_RESEND)
2444 args.ctag = TAG_CLOSE_LOST;
2445 else if (close_type == CLOSE_AFTER_RESEND)
2446 args.ctag = TAG_CLOSE_UNDO;
2447 else
2448 args.ctag = TAG_CLOSE;
2450 args.array_len = 3;
2451 args.array = argop;
2453 vp = RTOV4(rp);
2455 mi = VTOMI4(vp);
2457 /* putfh target fh */
2458 argop[0].argop = OP_CPUTFH;
2459 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2461 argop[1].argop = OP_GETATTR;
2462 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2463 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2465 argop[2].argop = OP_CLOSE;
2466 close_args = &argop[2].nfs_argop4_u.opclose;
2468 seqid = nfs4_get_open_seqid(oop) + 1;
2470 close_args->seqid = seqid;
2471 close_args->open_stateid = osp->open_stateid;
2473 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2474 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2475 rnode4info(rp)));
2477 t = gethrtime();
2479 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2481 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2482 nfs4_set_open_seqid(seqid, oop, args.ctag);
2485 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2486 if (ep->error && !needrecov) {
2488 * if there was an error and no recovery is to be done
2489 * then then set up the file to flush its cache if
2490 * needed for the next caller.
2492 mutex_enter(&rp->r_statelock);
2493 PURGE_ATTRCACHE4_LOCKED(rp);
2494 rp->r_flags &= ~R4WRITEMODIFIED;
2495 mutex_exit(&rp->r_statelock);
2496 return;
2499 if (needrecov) {
2500 bool_t abort;
2501 nfs4_bseqid_entry_t *bsep = NULL;
2503 if (close_type != CLOSE_RESEND)
2504 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2505 osp, cred_otw, vp);
2507 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2508 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2509 0, args.ctag, close_args->seqid);
2511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2512 "nfs4close_otw: initiating recovery. error %d "
2513 "res.status %d", ep->error, res.status));
2516 * Drop the 'os_sync_lock' here so we don't hit
2517 * a potential recursive mutex_enter via an
2518 * 'open_stream_hold()'.
2520 mutex_exit(&osp->os_sync_lock);
2521 *have_sync_lockp = 0;
2522 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2523 (close_type != CLOSE_RESEND &&
2524 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2525 OP_CLOSE, bsep, NULL, NULL);
2527 /* drop open seq sync, and let the calling function regrab it */
2528 nfs4_end_open_seqid_sync(oop);
2529 *did_start_seqid_syncp = 0;
2531 if (bsep)
2532 kmem_free(bsep, sizeof (*bsep));
2534 * For signals, the caller wants to quit, so don't say to
2535 * retry. For forced unmount, if it's a user thread, it
2536 * wants to quit. If it's a recovery thread, the retry
2537 * will happen higher-up on the call stack. Either way,
2538 * don't say to retry.
2540 if (abort == FALSE && ep->error != EINTR &&
2541 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2542 close_type != CLOSE_RESEND &&
2543 close_type != CLOSE_AFTER_RESEND)
2544 *recov = 1;
2545 else
2546 *recov = 0;
2548 if (!ep->error)
2549 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2550 return;
2553 if (res.status) {
2554 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2555 return;
2558 mutex_enter(&rp->r_statev4_lock);
2559 rp->created_v4 = 0;
2560 mutex_exit(&rp->r_statev4_lock);
2562 resop = &res.array[2];
2563 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2564 osp->os_valid = 0;
2567 * This removes the reference obtained at OPEN; ie, when the
2568 * open stream structure was created.
2570 * We don't have to worry about calling 'open_stream_rele'
2571 * since we our currently holding a reference to the open
2572 * stream which means the count cannot go to 0 with this
2573 * decrement.
2575 ASSERT(osp->os_ref_count >= 2);
2576 osp->os_ref_count--;
2578 if (ep->error == 0) {
2580 * Avoid a deadlock with the r_serial thread waiting for
2581 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be
2582 * held by us. We will wait in nfs4_attr_cache() for the
2583 * completion of the r_serial thread.
2585 mutex_exit(&osp->os_sync_lock);
2586 *have_sync_lockp = 0;
2588 nfs4_attr_cache(vp,
2589 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2590 t, cred_otw, TRUE, NULL);
2593 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2594 " returning %d", ep->error));
2596 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2599 /* ARGSUSED */
2600 static int
2601 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2602 caller_context_t *ct)
2604 rnode4_t *rp;
2605 uoff_t off;
2606 offset_t diff;
2607 uint_t on;
2608 uint_t n;
2609 caddr_t base;
2610 uint_t flags;
2611 int error;
2612 mntinfo4_t *mi;
2614 rp = VTOR4(vp);
2616 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2618 if (IS_SHADOW(vp, rp))
2619 vp = RTOV4(rp);
2621 if (vp->v_type != VREG)
2622 return (EISDIR);
2624 mi = VTOMI4(vp);
2626 if (nfs_zone() != mi->mi_zone)
2627 return (EIO);
2629 if (uiop->uio_resid == 0)
2630 return (0);
2632 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2633 return (EINVAL);
2635 mutex_enter(&rp->r_statelock);
2636 if (rp->r_flags & R4RECOVERRP)
2637 error = (rp->r_error ? rp->r_error : EIO);
2638 else
2639 error = 0;
2640 mutex_exit(&rp->r_statelock);
2641 if (error)
2642 return (error);
2645 * Bypass VM if caching has been disabled (e.g., locking) or if
2646 * using client-side direct I/O and the file is not mmap'd and
2647 * there are no cached pages.
2649 if ((vp->v_flag & VNOCACHE) ||
2650 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2651 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2652 size_t resid = 0;
2654 return (nfs4read(vp, NULL, uiop->uio_loffset,
2655 uiop->uio_resid, &resid, cr, FALSE, uiop));
2658 error = 0;
2660 do {
2661 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2662 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2663 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2665 if (error = nfs4_validate_caches(vp, cr))
2666 break;
2668 mutex_enter(&rp->r_statelock);
2669 while (rp->r_flags & R4INCACHEPURGE) {
2670 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2671 mutex_exit(&rp->r_statelock);
2672 return (EINTR);
2675 diff = rp->r_size - uiop->uio_loffset;
2676 mutex_exit(&rp->r_statelock);
2677 if (diff <= 0)
2678 break;
2679 if (diff < n)
2680 n = (uint_t)diff;
2682 if (vpm_enable) {
2684 * Copy data.
2686 error = vpm_data_copy(vp, off + on, n, uiop,
2687 1, NULL, 0, S_READ);
2688 } else {
2689 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2690 S_READ);
2692 error = uiomove(base + on, n, UIO_READ, uiop);
2695 if (!error) {
2697 * If read a whole block or read to eof,
2698 * won't need this buffer again soon.
2700 mutex_enter(&rp->r_statelock);
2701 if (n + on == MAXBSIZE ||
2702 uiop->uio_loffset == rp->r_size)
2703 flags = SM_DONTNEED;
2704 else
2705 flags = 0;
2706 mutex_exit(&rp->r_statelock);
2707 if (vpm_enable) {
2708 error = vpm_sync_pages(vp, off, n, flags);
2709 } else {
2710 error = segmap_release(segkmap, base, flags);
2712 } else {
2713 if (vpm_enable) {
2714 (void) vpm_sync_pages(vp, off, n, 0);
2715 } else {
2716 (void) segmap_release(segkmap, base, 0);
2719 } while (!error && uiop->uio_resid > 0);
2721 return (error);
2724 /* ARGSUSED */
2725 static int
2726 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2727 caller_context_t *ct)
2729 rlim64_t limit = uiop->uio_llimit;
2730 rnode4_t *rp;
2731 uoff_t off;
2732 caddr_t base;
2733 uint_t flags;
2734 int remainder;
2735 size_t n;
2736 int on;
2737 int error;
2738 int resid;
2739 uoff_t offset;
2740 mntinfo4_t *mi;
2741 uint_t bsize;
2743 rp = VTOR4(vp);
2745 if (IS_SHADOW(vp, rp))
2746 vp = RTOV4(rp);
2748 if (vp->v_type != VREG)
2749 return (EISDIR);
2751 mi = VTOMI4(vp);
2753 if (nfs_zone() != mi->mi_zone)
2754 return (EIO);
2756 if (uiop->uio_resid == 0)
2757 return (0);
2759 mutex_enter(&rp->r_statelock);
2760 if (rp->r_flags & R4RECOVERRP)
2761 error = (rp->r_error ? rp->r_error : EIO);
2762 else
2763 error = 0;
2764 mutex_exit(&rp->r_statelock);
2765 if (error)
2766 return (error);
2768 if (ioflag & FAPPEND) {
2769 struct vattr va;
2772 * Must serialize if appending.
2774 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2775 nfs_rw_exit(&rp->r_rwlock);
2776 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2777 INTR4(vp)))
2778 return (EINTR);
2781 va.va_mask = AT_SIZE;
2782 error = nfs4getattr(vp, &va, cr);
2783 if (error)
2784 return (error);
2785 uiop->uio_loffset = va.va_size;
2788 offset = uiop->uio_loffset + uiop->uio_resid;
2790 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2791 return (EINVAL);
2793 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2794 limit = MAXOFFSET_T;
2797 * Check to make sure that the process will not exceed
2798 * its limit on file size. It is okay to write up to
2799 * the limit, but not beyond. Thus, the write which
2800 * reaches the limit will be short and the next write
2801 * will return an error.
2803 remainder = 0;
2804 if (offset > uiop->uio_llimit) {
2805 remainder = offset - uiop->uio_llimit;
2806 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2807 if (uiop->uio_resid <= 0) {
2808 proc_t *p = ttoproc(curthread);
2810 uiop->uio_resid += remainder;
2811 mutex_enter(&p->p_lock);
2812 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2813 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2814 mutex_exit(&p->p_lock);
2815 return (EFBIG);
2819 /* update the change attribute, if we have a write delegation */
2821 mutex_enter(&rp->r_statev4_lock);
2822 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2823 rp->r_deleg_change++;
2825 mutex_exit(&rp->r_statev4_lock);
2827 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2828 return (EINTR);
2831 * Bypass VM if caching has been disabled (e.g., locking) or if
2832 * using client-side direct I/O and the file is not mmap'd and
2833 * there are no cached pages.
2835 if ((vp->v_flag & VNOCACHE) ||
2836 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2837 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2838 size_t bufsize;
2839 int count;
2840 uoff_t org_offset;
2841 stable_how4 stab_comm;
2842 nfs4_fwrite:
2843 if (rp->r_flags & R4STALE) {
2844 resid = uiop->uio_resid;
2845 offset = uiop->uio_loffset;
2846 error = rp->r_error;
2848 * A close may have cleared r_error, if so,
2849 * propagate ESTALE error return properly
2851 if (error == 0)
2852 error = ESTALE;
2853 goto bottom;
2856 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2857 base = kmem_alloc(bufsize, KM_SLEEP);
2858 do {
2859 if (ioflag & FDSYNC)
2860 stab_comm = DATA_SYNC4;
2861 else
2862 stab_comm = FILE_SYNC4;
2863 resid = uiop->uio_resid;
2864 offset = uiop->uio_loffset;
2865 count = MIN(uiop->uio_resid, bufsize);
2866 org_offset = uiop->uio_loffset;
2867 error = uiomove(base, count, UIO_WRITE, uiop);
2868 if (!error) {
2869 error = nfs4write(vp, base, org_offset,
2870 count, cr, &stab_comm);
2871 if (!error) {
2872 mutex_enter(&rp->r_statelock);
2873 if (rp->r_size < uiop->uio_loffset)
2874 rp->r_size = uiop->uio_loffset;
2875 mutex_exit(&rp->r_statelock);
2878 } while (!error && uiop->uio_resid > 0);
2879 kmem_free(base, bufsize);
2880 goto bottom;
2883 bsize = vp->v_vfsp->vfs_bsize;
2885 do {
2886 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2887 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2888 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2890 resid = uiop->uio_resid;
2891 offset = uiop->uio_loffset;
2893 if (rp->r_flags & R4STALE) {
2894 error = rp->r_error;
2896 * A close may have cleared r_error, if so,
2897 * propagate ESTALE error return properly
2899 if (error == 0)
2900 error = ESTALE;
2901 break;
2905 * Don't create dirty pages faster than they
2906 * can be cleaned so that the system doesn't
2907 * get imbalanced. If the async queue is
2908 * maxed out, then wait for it to drain before
2909 * creating more dirty pages. Also, wait for
2910 * any threads doing pagewalks in the vop_getattr
2911 * entry points so that they don't block for
2912 * long periods.
2914 mutex_enter(&rp->r_statelock);
2915 while ((mi->mi_max_threads != 0 &&
2916 rp->r_awcount > 2 * mi->mi_max_threads) ||
2917 rp->r_gcount > 0) {
2918 if (INTR4(vp)) {
2919 klwp_t *lwp = ttolwp(curthread);
2921 if (lwp != NULL)
2922 lwp->lwp_nostop++;
2923 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2924 mutex_exit(&rp->r_statelock);
2925 if (lwp != NULL)
2926 lwp->lwp_nostop--;
2927 error = EINTR;
2928 goto bottom;
2930 if (lwp != NULL)
2931 lwp->lwp_nostop--;
2932 } else
2933 cv_wait(&rp->r_cv, &rp->r_statelock);
2935 mutex_exit(&rp->r_statelock);
2938 * Touch the page and fault it in if it is not in core
2939 * before segmap_getmapflt or vpm_data_copy can lock it.
2940 * This is to avoid the deadlock if the buffer is mapped
2941 * to the same file through mmap which we want to write.
2943 uio_prefaultpages((long)n, uiop);
2945 if (vpm_enable) {
2947 * It will use kpm mappings, so no need to
2948 * pass an address.
2950 error = writerp4(rp, NULL, n, uiop, 0);
2951 } else {
2952 if (segmap_kpm) {
2953 int pon = uiop->uio_loffset & PAGEOFFSET;
2954 size_t pn = MIN(PAGESIZE - pon,
2955 uiop->uio_resid);
2956 int pagecreate;
2958 mutex_enter(&rp->r_statelock);
2959 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2960 uiop->uio_loffset + pn >= rp->r_size);
2961 mutex_exit(&rp->r_statelock);
2963 base = segmap_getmapflt(segkmap, vp, off + on,
2964 pn, !pagecreate, S_WRITE);
2966 error = writerp4(rp, base + pon, n, uiop,
2967 pagecreate);
2969 } else {
2970 base = segmap_getmapflt(segkmap, vp, off + on,
2971 n, 0, S_READ);
2972 error = writerp4(rp, base + on, n, uiop, 0);
2976 if (!error) {
2977 if (mi->mi_flags & MI4_NOAC)
2978 flags = SM_WRITE;
2979 else if ((uiop->uio_loffset % bsize) == 0 ||
2980 IS_SWAPVP(vp)) {
2982 * Have written a whole block.
2983 * Start an asynchronous write
2984 * and mark the buffer to
2985 * indicate that it won't be
2986 * needed again soon.
2988 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2989 } else
2990 flags = 0;
2991 if ((ioflag & (FSYNC|FDSYNC)) ||
2992 (rp->r_flags & R4OUTOFSPACE)) {
2993 flags &= ~SM_ASYNC;
2994 flags |= SM_WRITE;
2996 if (vpm_enable) {
2997 error = vpm_sync_pages(vp, off, n, flags);
2998 } else {
2999 error = segmap_release(segkmap, base, flags);
3001 } else {
3002 if (vpm_enable) {
3003 (void) vpm_sync_pages(vp, off, n, 0);
3004 } else {
3005 (void) segmap_release(segkmap, base, 0);
3008 * In the event that we got an access error while
3009 * faulting in a page for a write-only file just
3010 * force a write.
3012 if (error == EACCES)
3013 goto nfs4_fwrite;
3015 } while (!error && uiop->uio_resid > 0);
3017 bottom:
3018 if (error) {
3019 uiop->uio_resid = resid + remainder;
3020 uiop->uio_loffset = offset;
3021 } else {
3022 uiop->uio_resid += remainder;
3024 mutex_enter(&rp->r_statev4_lock);
3025 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3026 gethrestime(&rp->r_attr.va_mtime);
3027 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3029 mutex_exit(&rp->r_statev4_lock);
3032 nfs_rw_exit(&rp->r_lkserlock);
3034 return (error);
3038 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3040 static int
3041 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, uoff_t off, size_t len,
3042 int flags, cred_t *cr)
3044 struct buf *bp;
3045 int error;
3046 page_t *savepp;
3047 uchar_t fsdata;
3048 stable_how4 stab_comm;
3050 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3051 bp = pageio_setup(pp, len, vp, flags);
3052 ASSERT(bp != NULL);
3055 * pageio_setup should have set b_addr to 0. This
3056 * is correct since we want to do I/O on a page
3057 * boundary. bp_mapin will use this addr to calculate
3058 * an offset, and then set b_addr to the kernel virtual
3059 * address it allocated for us.
3061 ASSERT(bp->b_un.b_addr == 0);
3063 bp->b_edev = 0;
3064 bp->b_dev = 0;
3065 bp->b_lblkno = lbtodb(off);
3066 bp->b_file = vp;
3067 bp->b_offset = (offset_t)off;
3068 bp_mapin(bp);
3070 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3071 freemem > desfree)
3072 stab_comm = UNSTABLE4;
3073 else
3074 stab_comm = FILE_SYNC4;
3076 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3078 bp_mapout(bp);
3079 pageio_done(bp);
3081 if (stab_comm == UNSTABLE4)
3082 fsdata = C_DELAYCOMMIT;
3083 else
3084 fsdata = C_NOCOMMIT;
3086 savepp = pp;
3087 do {
3088 pp->p_fsdata = fsdata;
3089 } while ((pp = pp->p_next) != savepp);
3091 return (error);
3096 static int
3097 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3099 nfs4_open_owner_t *oop;
3100 nfs4_open_stream_t *osp;
3101 rnode4_t *rp = VTOR4(vp);
3102 mntinfo4_t *mi = VTOMI4(vp);
3103 int reopen_needed;
3105 ASSERT(nfs_zone() == mi->mi_zone);
3108 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3109 if (!oop)
3110 return (EIO);
3112 /* returns with 'os_sync_lock' held */
3113 osp = find_open_stream(oop, rp);
3114 if (!osp) {
3115 open_owner_rele(oop);
3116 return (EIO);
3119 if (osp->os_failed_reopen) {
3120 mutex_exit(&osp->os_sync_lock);
3121 open_stream_rele(osp, rp);
3122 open_owner_rele(oop);
3123 return (EIO);
3127 * Determine whether a reopen is needed. If this
3128 * is a delegation open stream, then the os_delegation bit
3129 * should be set.
3132 reopen_needed = osp->os_delegation;
3134 mutex_exit(&osp->os_sync_lock);
3135 open_owner_rele(oop);
3137 if (reopen_needed) {
3138 nfs4_error_zinit(ep);
3139 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3140 mutex_enter(&osp->os_sync_lock);
3141 if (ep->error || ep->stat || osp->os_failed_reopen) {
3142 mutex_exit(&osp->os_sync_lock);
3143 open_stream_rele(osp, rp);
3144 return (EIO);
3146 mutex_exit(&osp->os_sync_lock);
3148 open_stream_rele(osp, rp);
3150 return (0);
3154 * Write to file. Writes to remote server in largest size
3155 * chunks that the server can handle. Write is synchronous.
3157 static int
3158 nfs4write(vnode_t *vp, caddr_t base, uoff_t offset, int count, cred_t *cr,
3159 stable_how4 *stab_comm)
3161 mntinfo4_t *mi;
3162 COMPOUND4args_clnt args;
3163 COMPOUND4res_clnt res;
3164 WRITE4args *wargs;
3165 WRITE4res *wres;
3166 nfs_argop4 argop[2];
3167 nfs_resop4 *resop;
3168 int tsize;
3169 stable_how4 stable;
3170 rnode4_t *rp;
3171 int doqueue = 1;
3172 bool_t needrecov;
3173 nfs4_recov_state_t recov_state;
3174 nfs4_stateid_types_t sid_types;
3175 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3176 int recov;
3178 rp = VTOR4(vp);
3179 mi = VTOMI4(vp);
3181 ASSERT(nfs_zone() == mi->mi_zone);
3183 stable = *stab_comm;
3184 *stab_comm = FILE_SYNC4;
3186 needrecov = FALSE;
3187 recov_state.rs_flags = 0;
3188 recov_state.rs_num_retry_despite_err = 0;
3189 nfs4_init_stateid_types(&sid_types);
3191 /* Is curthread the recovery thread? */
3192 mutex_enter(&mi->mi_lock);
3193 recov = (mi->mi_recovthread == curthread);
3194 mutex_exit(&mi->mi_lock);
3196 recov_retry:
3197 args.ctag = TAG_WRITE;
3198 args.array_len = 2;
3199 args.array = argop;
3201 if (!recov) {
3202 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3203 &recov_state, NULL);
3204 if (e.error)
3205 return (e.error);
3208 /* 0. putfh target fh */
3209 argop[0].argop = OP_CPUTFH;
3210 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3212 /* 1. write */
3213 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3215 do {
3217 wargs->offset = (offset4)offset;
3218 wargs->data_val = base;
3220 if (mi->mi_io_kstats) {
3221 mutex_enter(&mi->mi_lock);
3222 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3223 mutex_exit(&mi->mi_lock);
3226 if ((vp->v_flag & VNOCACHE) ||
3227 (rp->r_flags & R4DIRECTIO) ||
3228 (mi->mi_flags & MI4_DIRECTIO))
3229 tsize = MIN(mi->mi_stsize, count);
3230 else
3231 tsize = MIN(mi->mi_curwrite, count);
3232 wargs->data_len = (uint_t)tsize;
3233 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3235 if (mi->mi_io_kstats) {
3236 mutex_enter(&mi->mi_lock);
3237 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3238 mutex_exit(&mi->mi_lock);
3241 if (!recov) {
3242 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3243 if (e.error && !needrecov) {
3244 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3245 &recov_state, needrecov);
3246 return (e.error);
3248 } else {
3249 if (e.error)
3250 return (e.error);
3254 * Do handling of OLD_STATEID outside
3255 * of the normal recovery framework.
3257 * If write receives a BAD stateid error while using a
3258 * delegation stateid, retry using the open stateid (if it
3259 * exists). If it doesn't have an open stateid, reopen the
3260 * file first, then retry.
3262 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3263 sid_types.cur_sid_type != SPEC_SID) {
3264 nfs4_save_stateid(&wargs->stateid, &sid_types);
3265 if (!recov)
3266 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3267 &recov_state, needrecov);
3268 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3269 goto recov_retry;
3270 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3271 sid_types.cur_sid_type == DEL_SID) {
3272 nfs4_save_stateid(&wargs->stateid, &sid_types);
3273 mutex_enter(&rp->r_statev4_lock);
3274 rp->r_deleg_return_pending = TRUE;
3275 mutex_exit(&rp->r_statev4_lock);
3276 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3277 if (!recov)
3278 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3279 &recov_state, needrecov);
3280 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3281 return (EIO);
3283 if (!recov)
3284 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3285 &recov_state, needrecov);
3286 /* hold needed for nfs4delegreturn_thread */
3287 VN_HOLD(vp);
3288 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3289 NFS4_DR_DISCARD), FALSE);
3290 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3291 goto recov_retry;
3294 if (needrecov) {
3295 bool_t abort;
3297 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3298 "nfs4write: client got error %d, res.status %d"
3299 ", so start recovery", e.error, res.status));
3301 abort = nfs4_start_recovery(&e,
3302 VTOMI4(vp), vp, NULL, &wargs->stateid,
3303 NULL, OP_WRITE, NULL, NULL, NULL);
3304 if (!e.error) {
3305 e.error = geterrno4(res.status);
3306 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3308 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3309 &recov_state, needrecov);
3310 if (abort == FALSE)
3311 goto recov_retry;
3312 return (e.error);
3315 if (res.status) {
3316 e.error = geterrno4(res.status);
3317 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3318 if (!recov)
3319 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3320 &recov_state, needrecov);
3321 return (e.error);
3324 resop = &res.array[1]; /* write res */
3325 wres = &resop->nfs_resop4_u.opwrite;
3327 if ((int)wres->count > tsize) {
3328 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3330 zcmn_err(getzoneid(), CE_WARN,
3331 "nfs4write: server wrote %u, requested was %u",
3332 (int)wres->count, tsize);
3333 if (!recov)
3334 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3335 &recov_state, needrecov);
3336 return (EIO);
3338 if (wres->committed == UNSTABLE4) {
3339 *stab_comm = UNSTABLE4;
3340 if (wargs->stable == DATA_SYNC4 ||
3341 wargs->stable == FILE_SYNC4) {
3342 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3343 zcmn_err(getzoneid(), CE_WARN,
3344 "nfs4write: server %s did not commit "
3345 "to stable storage",
3346 rp->r_server->sv_hostname);
3347 if (!recov)
3348 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3349 OH_WRITE, &recov_state, needrecov);
3350 return (EIO);
3354 tsize = (int)wres->count;
3355 count -= tsize;
3356 base += tsize;
3357 offset += tsize;
3358 if (mi->mi_io_kstats) {
3359 mutex_enter(&mi->mi_lock);
3360 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3361 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3362 tsize;
3363 mutex_exit(&mi->mi_lock);
3365 lwp_stat_update(LWP_STAT_OUBLK, 1);
3366 mutex_enter(&rp->r_statelock);
3367 if (rp->r_flags & R4HAVEVERF) {
3368 if (rp->r_writeverf != wres->writeverf) {
3369 nfs4_set_mod(vp);
3370 rp->r_writeverf = wres->writeverf;
3372 } else {
3373 rp->r_writeverf = wres->writeverf;
3374 rp->r_flags |= R4HAVEVERF;
3376 PURGE_ATTRCACHE4_LOCKED(rp);
3377 rp->r_flags |= R4WRITEMODIFIED;
3378 gethrestime(&rp->r_attr.va_mtime);
3379 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3380 mutex_exit(&rp->r_statelock);
3381 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3382 } while (count);
3384 if (!recov)
3385 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3386 needrecov);
3388 return (e.error);
3392 * Read from a file. Reads data in largest chunks our interface can handle.
3394 static int
3395 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3396 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3398 mntinfo4_t *mi;
3399 COMPOUND4args_clnt args;
3400 COMPOUND4res_clnt res;
3401 READ4args *rargs;
3402 nfs_argop4 argop[2];
3403 int tsize;
3404 int doqueue;
3405 rnode4_t *rp;
3406 int data_len;
3407 bool_t is_eof;
3408 bool_t needrecov = FALSE;
3409 nfs4_recov_state_t recov_state;
3410 nfs4_stateid_types_t sid_types;
3411 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3413 rp = VTOR4(vp);
3414 mi = VTOMI4(vp);
3415 doqueue = 1;
3417 ASSERT(nfs_zone() == mi->mi_zone);
3419 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3421 args.array_len = 2;
3422 args.array = argop;
3424 nfs4_init_stateid_types(&sid_types);
3426 recov_state.rs_flags = 0;
3427 recov_state.rs_num_retry_despite_err = 0;
3429 recov_retry:
3430 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3431 &recov_state, NULL);
3432 if (e.error)
3433 return (e.error);
3435 /* putfh target fh */
3436 argop[0].argop = OP_CPUTFH;
3437 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3439 /* read */
3440 argop[1].argop = OP_READ;
3441 rargs = &argop[1].nfs_argop4_u.opread;
3442 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3443 OP_READ, &sid_types, async);
3445 do {
3446 if (mi->mi_io_kstats) {
3447 mutex_enter(&mi->mi_lock);
3448 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3449 mutex_exit(&mi->mi_lock);
3452 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3453 "nfs4read: %s call, rp %s",
3454 needrecov ? "recov" : "first",
3455 rnode4info(rp)));
3457 if ((vp->v_flag & VNOCACHE) ||
3458 (rp->r_flags & R4DIRECTIO) ||
3459 (mi->mi_flags & MI4_DIRECTIO))
3460 tsize = MIN(mi->mi_tsize, count);
3461 else
3462 tsize = MIN(mi->mi_curread, count);
3464 rargs->offset = (offset4)offset;
3465 rargs->count = (count4)tsize;
3466 rargs->res_data_val_alt = NULL;
3467 rargs->res_mblk = NULL;
3468 rargs->res_uiop = NULL;
3469 rargs->res_maxsize = 0;
3470 rargs->wlist = NULL;
3472 if (uiop)
3473 rargs->res_uiop = uiop;
3474 else
3475 rargs->res_data_val_alt = base;
3476 rargs->res_maxsize = tsize;
3478 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3479 #ifdef DEBUG
3480 if (nfs4read_error_inject) {
3481 res.status = nfs4read_error_inject;
3482 nfs4read_error_inject = 0;
3484 #endif
3486 if (mi->mi_io_kstats) {
3487 mutex_enter(&mi->mi_lock);
3488 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3489 mutex_exit(&mi->mi_lock);
3492 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3493 if (e.error != 0 && !needrecov) {
3494 nfs4_end_fop(mi, vp, NULL, OH_READ,
3495 &recov_state, needrecov);
3496 return (e.error);
3500 * Do proper retry for OLD and BAD stateid errors outside
3501 * of the normal recovery framework. There are two differences
3502 * between async and sync reads. The first is that we allow
3503 * retry on BAD_STATEID for async reads, but not sync reads.
3504 * The second is that we mark the file dead for a failed
3505 * attempt with a special stateid for sync reads, but just
3506 * return EIO for async reads.
3508 * If a sync read receives a BAD stateid error while using a
3509 * delegation stateid, retry using the open stateid (if it
3510 * exists). If it doesn't have an open stateid, reopen the
3511 * file first, then retry.
3513 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3514 res.status == NFS4ERR_BAD_STATEID) && async) {
3515 nfs4_end_fop(mi, vp, NULL, OH_READ,
3516 &recov_state, needrecov);
3517 if (sid_types.cur_sid_type == SPEC_SID) {
3518 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3519 return (EIO);
3521 nfs4_save_stateid(&rargs->stateid, &sid_types);
3522 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3523 goto recov_retry;
3524 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3525 !async && sid_types.cur_sid_type != SPEC_SID) {
3526 nfs4_save_stateid(&rargs->stateid, &sid_types);
3527 nfs4_end_fop(mi, vp, NULL, OH_READ,
3528 &recov_state, needrecov);
3529 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3530 goto recov_retry;
3531 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3532 sid_types.cur_sid_type == DEL_SID) {
3533 nfs4_save_stateid(&rargs->stateid, &sid_types);
3534 mutex_enter(&rp->r_statev4_lock);
3535 rp->r_deleg_return_pending = TRUE;
3536 mutex_exit(&rp->r_statev4_lock);
3537 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3538 nfs4_end_fop(mi, vp, NULL, OH_READ,
3539 &recov_state, needrecov);
3540 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3541 return (EIO);
3543 nfs4_end_fop(mi, vp, NULL, OH_READ,
3544 &recov_state, needrecov);
3545 /* hold needed for nfs4delegreturn_thread */
3546 VN_HOLD(vp);
3547 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3548 NFS4_DR_DISCARD), FALSE);
3549 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3550 goto recov_retry;
3552 if (needrecov) {
3553 bool_t abort;
3555 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3556 "nfs4read: initiating recovery\n"));
3557 abort = nfs4_start_recovery(&e,
3558 mi, vp, NULL, &rargs->stateid,
3559 NULL, OP_READ, NULL, NULL, NULL);
3560 nfs4_end_fop(mi, vp, NULL, OH_READ,
3561 &recov_state, needrecov);
3563 * Do not retry if we got OLD_STATEID using a special
3564 * stateid. This avoids looping with a broken server.
3566 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3567 sid_types.cur_sid_type == SPEC_SID)
3568 abort = TRUE;
3570 if (abort == FALSE) {
3572 * Need to retry all possible stateids in
3573 * case the recovery error wasn't stateid
3574 * related or the stateids have become
3575 * stale (server reboot).
3577 nfs4_init_stateid_types(&sid_types);
3578 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3579 goto recov_retry;
3582 if (!e.error) {
3583 e.error = geterrno4(res.status);
3584 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3586 return (e.error);
3589 if (res.status) {
3590 e.error = geterrno4(res.status);
3591 nfs4_end_fop(mi, vp, NULL, OH_READ,
3592 &recov_state, needrecov);
3593 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3594 return (e.error);
3597 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3598 count -= data_len;
3599 if (base)
3600 base += data_len;
3601 offset += data_len;
3602 if (mi->mi_io_kstats) {
3603 mutex_enter(&mi->mi_lock);
3604 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3605 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3606 mutex_exit(&mi->mi_lock);
3608 lwp_stat_update(LWP_STAT_INBLK, 1);
3609 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3610 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3612 } while (count && !is_eof);
3614 *residp = count;
3616 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3618 return (e.error);
3621 /* ARGSUSED */
3622 static int
3623 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3624 caller_context_t *ct)
3626 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3627 return (EIO);
3628 switch (cmd) {
3629 case _FIODIRECTIO:
3630 return (nfs4_directio(vp, (int)arg, cr));
3631 default:
3632 return (ENOTTY);
3636 /* ARGSUSED */
3638 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3639 caller_context_t *ct)
3641 int error;
3642 rnode4_t *rp = VTOR4(vp);
3644 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3645 return (EIO);
3647 * If it has been specified that the return value will
3648 * just be used as a hint, and we are only being asked
3649 * for size, fsid or rdevid, then return the client's
3650 * notion of these values without checking to make sure
3651 * that the attribute cache is up to date.
3652 * The whole point is to avoid an over the wire GETATTR
3653 * call.
3655 if (flags & ATTR_HINT) {
3656 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3657 mutex_enter(&rp->r_statelock);
3658 if (vap->va_mask & AT_SIZE)
3659 vap->va_size = rp->r_size;
3660 if (vap->va_mask & AT_FSID)
3661 vap->va_fsid = rp->r_attr.va_fsid;
3662 if (vap->va_mask & AT_RDEV)
3663 vap->va_rdev = rp->r_attr.va_rdev;
3664 mutex_exit(&rp->r_statelock);
3665 return (0);
3670 * Only need to flush pages if asking for the mtime
3671 * and if there any dirty pages or any outstanding
3672 * asynchronous (write) requests for this file.
3674 if (vap->va_mask & AT_MTIME) {
3675 rp = VTOR4(vp);
3676 if (nfs4_has_pages(vp)) {
3677 mutex_enter(&rp->r_statev4_lock);
3678 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3679 mutex_exit(&rp->r_statev4_lock);
3680 if (rp->r_flags & R4DIRTY ||
3681 rp->r_awcount > 0) {
3682 mutex_enter(&rp->r_statelock);
3683 rp->r_gcount++;
3684 mutex_exit(&rp->r_statelock);
3685 error =
3686 nfs4_putpage(vp, 0,
3687 0, 0, cr, NULL);
3688 mutex_enter(&rp->r_statelock);
3689 if (error && (error == ENOSPC ||
3690 error == EDQUOT)) {
3691 if (!rp->r_error)
3692 rp->r_error = error;
3694 if (--rp->r_gcount == 0)
3695 cv_broadcast(&rp->r_cv);
3696 mutex_exit(&rp->r_statelock);
3698 } else {
3699 mutex_exit(&rp->r_statev4_lock);
3703 return (nfs4getattr(vp, vap, cr));
3707 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3710 * If these are the only two bits cleared
3711 * on the server then return 0 (OK) else
3712 * return 1 (BAD).
3714 on_client &= ~(S_ISUID|S_ISGID);
3715 if (on_client == from_server)
3716 return (0);
3717 else
3718 return (1);
3721 /*ARGSUSED4*/
3722 static int
3723 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3724 caller_context_t *ct)
3726 int error;
3728 if (vap->va_mask & AT_NOSET)
3729 return (EINVAL);
3731 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3732 return (EIO);
3735 * Don't call secpolicy_vnode_setattr, the client cannot
3736 * use its cached attributes to make security decisions
3737 * as the server may be faking mode bits or mapping uid/gid.
3738 * Always just let the server to the checking.
3739 * If we provide the ability to remove basic priviledges
3740 * to setattr (e.g. basic without chmod) then we will
3741 * need to add a check here before calling the server.
3743 error = nfs4setattr(vp, vap, flags, cr, NULL);
3745 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3746 vnevent_truncate(vp, ct);
3748 return (error);
3752 * To replace the "guarded" version 3 setattr, we use two types of compound
3753 * setattr requests:
3754 * 1. The "normal" setattr, used when the size of the file isn't being
3755 * changed - { Putfh <fh>; Setattr; Getattr }/
3756 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3757 * with only ctime as the argument. If the server ctime differs from
3758 * what is cached on the client, the verify will fail, but we would
3759 * already have the ctime from the preceding getattr, so just set it
3760 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3761 * Setattr; Getattr }.
3763 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3764 * this setattr and NULL if they are not.
3766 static int
3767 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3768 vsecattr_t *vsap)
3770 COMPOUND4args_clnt args;
3771 COMPOUND4res_clnt res, *resp = NULL;
3772 nfs4_ga_res_t *garp = NULL;
3773 int numops = 3; /* { Putfh; Setattr; Getattr } */
3774 nfs_argop4 argop[5];
3775 int verify_argop = -1;
3776 int setattr_argop = 1;
3777 nfs_resop4 *resop;
3778 vattr_t va;
3779 rnode4_t *rp;
3780 int doqueue = 1;
3781 uint_t mask = vap->va_mask;
3782 mode_t omode;
3783 vsecattr_t *vsp;
3784 timestruc_t ctime;
3785 bool_t needrecov = FALSE;
3786 nfs4_recov_state_t recov_state;
3787 nfs4_stateid_types_t sid_types;
3788 stateid4 stateid;
3789 hrtime_t t;
3790 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3791 servinfo4_t *svp;
3792 bitmap4 supp_attrs;
3794 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3795 rp = VTOR4(vp);
3796 nfs4_init_stateid_types(&sid_types);
3799 * Only need to flush pages if there are any pages and
3800 * if the file is marked as dirty in some fashion. The
3801 * file must be flushed so that we can accurately
3802 * determine the size of the file and the cached data
3803 * after the SETATTR returns. A file is considered to
3804 * be dirty if it is either marked with R4DIRTY, has
3805 * outstanding i/o's active, or is mmap'd. In this
3806 * last case, we can't tell whether there are dirty
3807 * pages, so we flush just to be sure.
3809 if (nfs4_has_pages(vp) &&
3810 ((rp->r_flags & R4DIRTY) ||
3811 rp->r_count > 0 ||
3812 rp->r_mapcnt > 0)) {
3813 ASSERT(vp->v_type != VCHR);
3814 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3815 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3816 mutex_enter(&rp->r_statelock);
3817 if (!rp->r_error)
3818 rp->r_error = e.error;
3819 mutex_exit(&rp->r_statelock);
3823 if (mask & AT_SIZE) {
3825 * Verification setattr compound for non-deleg AT_SIZE:
3826 * { Putfh; Getattr; Verify; Setattr; Getattr }
3827 * Set ctime local here (outside the do_again label)
3828 * so that subsequent retries (after failed VERIFY)
3829 * will use ctime from GETATTR results (from failed
3830 * verify compound) as VERIFY arg.
3831 * If file has delegation, then VERIFY(time_metadata)
3832 * is of little added value, so don't bother.
3834 mutex_enter(&rp->r_statev4_lock);
3835 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3836 rp->r_deleg_return_pending) {
3837 numops = 5;
3838 ctime = rp->r_attr.va_ctime;
3840 mutex_exit(&rp->r_statev4_lock);
3843 recov_state.rs_flags = 0;
3844 recov_state.rs_num_retry_despite_err = 0;
3846 args.ctag = TAG_SETATTR;
3847 do_again:
3848 recov_retry:
3849 setattr_argop = numops - 2;
3851 args.array = argop;
3852 args.array_len = numops;
3854 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3855 if (e.error)
3856 return (e.error);
3859 /* putfh target fh */
3860 argop[0].argop = OP_CPUTFH;
3861 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3863 if (numops == 5) {
3865 * We only care about the ctime, but need to get mtime
3866 * and size for proper cache update.
3868 /* getattr */
3869 argop[1].argop = OP_GETATTR;
3870 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3871 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3873 /* verify - set later in loop */
3874 verify_argop = 2;
3877 /* setattr */
3878 svp = rp->r_server;
3879 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3880 supp_attrs = svp->sv_supp_attrs;
3881 nfs_rw_exit(&svp->sv_lock);
3883 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3884 supp_attrs, &e.error, &sid_types);
3885 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3886 if (e.error) {
3887 /* req time field(s) overflow - return immediately */
3888 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3889 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3890 opsetattr.obj_attributes);
3891 return (e.error);
3893 omode = rp->r_attr.va_mode;
3895 /* getattr */
3896 argop[numops-1].argop = OP_GETATTR;
3897 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3899 * If we are setting the ACL (indicated only by vsap != NULL), request
3900 * the ACL in this getattr. The ACL returned from this getattr will be
3901 * used in updating the ACL cache.
3903 if (vsap != NULL)
3904 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3905 FATTR4_ACL_MASK;
3906 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3909 * setattr iterates if the object size is set and the cached ctime
3910 * does not match the file ctime. In that case, verify the ctime first.
3913 do {
3914 if (verify_argop != -1) {
3916 * Verify that the ctime match before doing setattr.
3918 va.va_mask = AT_CTIME;
3919 va.va_ctime = ctime;
3920 svp = rp->r_server;
3921 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3922 supp_attrs = svp->sv_supp_attrs;
3923 nfs_rw_exit(&svp->sv_lock);
3924 e.error = nfs4args_verify(&argop[verify_argop], &va,
3925 OP_VERIFY, supp_attrs);
3926 if (e.error) {
3927 /* req time field(s) overflow - return */
3928 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3929 needrecov);
3930 break;
3934 doqueue = 1;
3936 t = gethrtime();
3938 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3941 * Purge the access cache and ACL cache if changing either the
3942 * owner of the file, the group owner, or the mode. These may
3943 * change the access permissions of the file, so purge old
3944 * information and start over again.
3946 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3947 (void) nfs4_access_purge_rp(rp);
3948 if (rp->r_secattr != NULL) {
3949 mutex_enter(&rp->r_statelock);
3950 vsp = rp->r_secattr;
3951 rp->r_secattr = NULL;
3952 mutex_exit(&rp->r_statelock);
3953 if (vsp != NULL)
3954 nfs4_acl_free_cache(vsp);
3959 * If res.array_len == numops, then everything succeeded,
3960 * except for possibly the final getattr. If only the
3961 * last getattr failed, give up, and don't try recovery.
3963 if (res.array_len == numops) {
3964 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3965 needrecov);
3966 if (! e.error)
3967 resp = &res;
3968 break;
3972 * if either rpc call failed or completely succeeded - done
3974 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3975 if (e.error) {
3976 PURGE_ATTRCACHE4(vp);
3977 if (!needrecov) {
3978 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3979 needrecov);
3980 break;
3985 * Do proper retry for OLD_STATEID outside of the normal
3986 * recovery framework.
3988 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3989 sid_types.cur_sid_type != SPEC_SID &&
3990 sid_types.cur_sid_type != NO_SID) {
3991 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3992 needrecov);
3993 nfs4_save_stateid(&stateid, &sid_types);
3994 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3995 opsetattr.obj_attributes);
3996 if (verify_argop != -1) {
3997 nfs4args_verify_free(&argop[verify_argop]);
3998 verify_argop = -1;
4000 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4001 goto recov_retry;
4004 if (needrecov) {
4005 bool_t abort;
4007 abort = nfs4_start_recovery(&e,
4008 VTOMI4(vp), vp, NULL, NULL, NULL,
4009 OP_SETATTR, NULL, NULL, NULL);
4010 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4011 needrecov);
4013 * Do not retry if we failed with OLD_STATEID using
4014 * a special stateid. This is done to avoid looping
4015 * with a broken server.
4017 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4018 (sid_types.cur_sid_type == SPEC_SID ||
4019 sid_types.cur_sid_type == NO_SID))
4020 abort = TRUE;
4021 if (!e.error) {
4022 if (res.status == NFS4ERR_BADOWNER)
4023 nfs4_log_badowner(VTOMI4(vp),
4024 OP_SETATTR);
4026 e.error = geterrno4(res.status);
4027 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4029 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4030 opsetattr.obj_attributes);
4031 if (verify_argop != -1) {
4032 nfs4args_verify_free(&argop[verify_argop]);
4033 verify_argop = -1;
4035 if (abort == FALSE) {
4037 * Need to retry all possible stateids in
4038 * case the recovery error wasn't stateid
4039 * related or the stateids have become
4040 * stale (server reboot).
4042 nfs4_init_stateid_types(&sid_types);
4043 goto recov_retry;
4045 return (e.error);
4049 * Need to call nfs4_end_op before nfs4getattr to
4050 * avoid potential nfs4_start_op deadlock. See RFE
4051 * 4777612. Calls to nfs4_invalidate_pages() and
4052 * nfs4_purge_stale_fh() might also generate over the
4053 * wire calls which my cause nfs4_start_op() deadlock.
4055 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4058 * Check to update lease.
4060 resp = &res;
4061 if (res.status == NFS4_OK) {
4062 break;
4066 * Check if verify failed to see if try again
4068 if ((verify_argop == -1) || (res.array_len != 3)) {
4070 * can't continue...
4072 if (res.status == NFS4ERR_BADOWNER)
4073 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4075 e.error = geterrno4(res.status);
4076 } else {
4078 * When the verify request fails, the client ctime is
4079 * not in sync with the server. This is the same as
4080 * the version 3 "not synchronized" error, and we
4081 * handle it in a similar manner (XXX do we need to???).
4082 * Use the ctime returned in the first getattr for
4083 * the input to the next verify.
4084 * If we couldn't get the attributes, then we give up
4085 * because we can't complete the operation as required.
4087 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4089 if (e.error) {
4090 PURGE_ATTRCACHE4(vp);
4091 nfs4_purge_stale_fh(e.error, vp, cr);
4092 } else {
4094 * retry with a new verify value
4096 ctime = garp->n4g_va.va_ctime;
4097 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4098 resp = NULL;
4100 if (!e.error) {
4101 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4102 opsetattr.obj_attributes);
4103 if (verify_argop != -1) {
4104 nfs4args_verify_free(&argop[verify_argop]);
4105 verify_argop = -1;
4107 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4108 goto do_again;
4110 } while (!e.error);
4112 if (e.error) {
4114 * If we are here, rfs4call has an irrecoverable error - return
4116 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4117 opsetattr.obj_attributes);
4118 if (verify_argop != -1) {
4119 nfs4args_verify_free(&argop[verify_argop]);
4120 verify_argop = -1;
4122 if (resp)
4123 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4124 return (e.error);
4130 * If changing the size of the file, invalidate
4131 * any local cached data which is no longer part
4132 * of the file. We also possibly invalidate the
4133 * last page in the file. We could use
4134 * pvn_vpzero(), but this would mark the page as
4135 * modified and require it to be written back to
4136 * the server for no particularly good reason.
4137 * This way, if we access it, then we bring it
4138 * back in. A read should be cheaper than a
4139 * write.
4141 if (mask & AT_SIZE) {
4142 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4145 /* either no error or one of the postop getattr failed */
4148 * XXX Perform a simplified version of wcc checking. Instead of
4149 * have another getattr to get pre-op, just purge cache if
4150 * any of the ops prior to and including the getattr failed.
4151 * If the getattr succeeded then update the attrcache accordingly.
4154 garp = NULL;
4155 if (res.status == NFS4_OK) {
4157 * Last getattr
4159 resop = &res.array[numops - 1];
4160 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4163 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4164 * rather than filling it. See the function itself for details.
4166 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4167 if (garp != NULL) {
4168 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4169 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4170 vs_ace4_destroy(&garp->n4g_vsa);
4171 } else {
4172 if (vsap != NULL) {
4174 * The ACL was supposed to be set and to be
4175 * returned in the last getattr of this
4176 * compound, but for some reason the getattr
4177 * result doesn't contain the ACL. In this
4178 * case, purge the ACL cache.
4180 if (rp->r_secattr != NULL) {
4181 mutex_enter(&rp->r_statelock);
4182 vsp = rp->r_secattr;
4183 rp->r_secattr = NULL;
4184 mutex_exit(&rp->r_statelock);
4185 if (vsp != NULL)
4186 nfs4_acl_free_cache(vsp);
4192 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4194 * Set the size, rather than relying on getting it updated
4195 * via a GETATTR. With delegations the client tries to
4196 * suppress GETATTR calls.
4198 mutex_enter(&rp->r_statelock);
4199 rp->r_size = vap->va_size;
4200 mutex_exit(&rp->r_statelock);
4204 * Can free up request args and res
4206 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4207 opsetattr.obj_attributes);
4208 if (verify_argop != -1) {
4209 nfs4args_verify_free(&argop[verify_argop]);
4210 verify_argop = -1;
4212 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4215 * Some servers will change the mode to clear the setuid
4216 * and setgid bits when changing the uid or gid. The
4217 * client needs to compensate appropriately.
4219 if (mask & (AT_UID | AT_GID)) {
4220 int terror, do_setattr;
4222 do_setattr = 0;
4223 va.va_mask = AT_MODE;
4224 terror = nfs4getattr(vp, &va, cr);
4225 if (!terror &&
4226 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4227 (!(mask & AT_MODE) && va.va_mode != omode))) {
4228 va.va_mask = AT_MODE;
4229 if (mask & AT_MODE) {
4231 * We asked the mode to be changed and what
4232 * we just got from the server in getattr is
4233 * not what we wanted it to be, so set it now.
4235 va.va_mode = vap->va_mode;
4236 do_setattr = 1;
4237 } else {
4239 * We did not ask the mode to be changed,
4240 * Check to see that the server just cleared
4241 * I_SUID and I_GUID from it. If not then
4242 * set mode to omode with UID/GID cleared.
4244 if (nfs4_compare_modes(va.va_mode, omode)) {
4245 omode &= ~(S_ISUID|S_ISGID);
4246 va.va_mode = omode;
4247 do_setattr = 1;
4251 if (do_setattr)
4252 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4256 return (e.error);
4259 /* ARGSUSED */
4260 static int
4261 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4263 COMPOUND4args_clnt args;
4264 COMPOUND4res_clnt res;
4265 int doqueue;
4266 uint32_t acc, resacc, argacc;
4267 rnode4_t *rp;
4268 cred_t *cred, *ncr, *ncrfree = NULL;
4269 nfs4_access_type_t cacc;
4270 int num_ops;
4271 nfs_argop4 argop[3];
4272 nfs_resop4 *resop;
4273 bool_t needrecov = FALSE, do_getattr;
4274 nfs4_recov_state_t recov_state;
4275 int rpc_error;
4276 hrtime_t t;
4277 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4278 mntinfo4_t *mi = VTOMI4(vp);
4280 if (nfs_zone() != mi->mi_zone)
4281 return (EIO);
4283 acc = 0;
4284 if (mode & VREAD)
4285 acc |= ACCESS4_READ;
4286 if (mode & VWRITE) {
4287 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4288 return (EROFS);
4289 if (vp->v_type == VDIR)
4290 acc |= ACCESS4_DELETE;
4291 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4293 if (mode & VEXEC) {
4294 if (vp->v_type == VDIR)
4295 acc |= ACCESS4_LOOKUP;
4296 else
4297 acc |= ACCESS4_EXECUTE;
4300 if (VTOR4(vp)->r_acache != NULL) {
4301 e.error = nfs4_validate_caches(vp, cr);
4302 if (e.error)
4303 return (e.error);
4306 rp = VTOR4(vp);
4307 if (vp->v_type == VDIR)
4308 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4309 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4310 else
4311 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4312 ACCESS4_EXECUTE;
4313 recov_state.rs_flags = 0;
4314 recov_state.rs_num_retry_despite_err = 0;
4316 cred = cr;
4318 * ncr and ncrfree both initially
4319 * point to the memory area returned
4320 * by crnetadjust();
4321 * ncrfree not NULL when exiting means
4322 * that we need to release it
4324 ncr = crnetadjust(cred);
4325 ncrfree = ncr;
4327 tryagain:
4328 cacc = nfs4_access_check(rp, acc, cred);
4329 if (cacc == NFS4_ACCESS_ALLOWED) {
4330 if (ncrfree != NULL)
4331 crfree(ncrfree);
4332 return (0);
4334 if (cacc == NFS4_ACCESS_DENIED) {
4336 * If the cred can be adjusted, try again
4337 * with the new cred.
4339 if (ncr != NULL) {
4340 cred = ncr;
4341 ncr = NULL;
4342 goto tryagain;
4344 if (ncrfree != NULL)
4345 crfree(ncrfree);
4346 return (EACCES);
4349 recov_retry:
4351 * Don't take with r_statev4_lock here. r_deleg_type could
4352 * change as soon as lock is released. Since it is an int,
4353 * there is no atomicity issue.
4355 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4356 num_ops = do_getattr ? 3 : 2;
4358 args.ctag = TAG_ACCESS;
4360 args.array_len = num_ops;
4361 args.array = argop;
4363 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4364 &recov_state, NULL)) {
4365 if (ncrfree != NULL)
4366 crfree(ncrfree);
4367 return (e.error);
4370 /* putfh target fh */
4371 argop[0].argop = OP_CPUTFH;
4372 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4374 /* access */
4375 argop[1].argop = OP_ACCESS;
4376 argop[1].nfs_argop4_u.opaccess.access = argacc;
4378 /* getattr */
4379 if (do_getattr) {
4380 argop[2].argop = OP_GETATTR;
4381 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4382 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4385 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4386 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4387 rnode4info(VTOR4(vp))));
4389 doqueue = 1;
4390 t = gethrtime();
4391 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4392 rpc_error = e.error;
4394 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4395 if (needrecov) {
4396 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4397 "nfs4_access: initiating recovery\n"));
4399 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4400 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4401 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4402 &recov_state, needrecov);
4403 if (!e.error)
4404 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4405 goto recov_retry;
4408 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4410 if (e.error)
4411 goto out;
4413 if (res.status) {
4414 e.error = geterrno4(res.status);
4416 * This might generate over the wire calls throught
4417 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4418 * here to avoid a deadlock.
4420 nfs4_purge_stale_fh(e.error, vp, cr);
4421 goto out;
4423 resop = &res.array[1]; /* access res */
4425 resacc = resop->nfs_resop4_u.opaccess.access;
4427 if (do_getattr) {
4428 resop++; /* getattr res */
4429 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4430 t, cr, FALSE, NULL);
4433 if (!e.error) {
4434 nfs4_access_cache(rp, argacc, resacc, cred);
4436 * we just cached results with cred; if cred is the
4437 * adjusted credentials from crnetadjust, we do not want
4438 * to release them before exiting: hence setting ncrfree
4439 * to NULL
4441 if (cred != cr)
4442 ncrfree = NULL;
4443 /* XXX check the supported bits too? */
4444 if ((acc & resacc) != acc) {
4446 * The following code implements the semantic
4447 * that a setuid root program has *at least* the
4448 * permissions of the user that is running the
4449 * program. See rfs3call() for more portions
4450 * of the implementation of this functionality.
4452 /* XXX-LP */
4453 if (ncr != NULL) {
4454 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4455 cred = ncr;
4456 ncr = NULL;
4457 goto tryagain;
4459 e.error = EACCES;
4463 out:
4464 if (!rpc_error)
4465 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4467 if (ncrfree != NULL)
4468 crfree(ncrfree);
4470 return (e.error);
4473 /* ARGSUSED */
4474 static int
4475 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4477 COMPOUND4args_clnt args;
4478 COMPOUND4res_clnt res;
4479 int doqueue;
4480 rnode4_t *rp;
4481 nfs_argop4 argop[3];
4482 nfs_resop4 *resop;
4483 READLINK4res *lr_res;
4484 nfs4_ga_res_t *garp;
4485 uint_t len;
4486 char *linkdata;
4487 bool_t needrecov = FALSE;
4488 nfs4_recov_state_t recov_state;
4489 hrtime_t t;
4490 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4492 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4493 return (EIO);
4495 * Can't readlink anything other than a symbolic link.
4497 if (vp->v_type != VLNK)
4498 return (EINVAL);
4500 rp = VTOR4(vp);
4501 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4502 e.error = nfs4_validate_caches(vp, cr);
4503 if (e.error)
4504 return (e.error);
4505 mutex_enter(&rp->r_statelock);
4506 if (rp->r_symlink.contents != NULL) {
4507 e.error = uiomove(rp->r_symlink.contents,
4508 rp->r_symlink.len, UIO_READ, uiop);
4509 mutex_exit(&rp->r_statelock);
4510 return (e.error);
4512 mutex_exit(&rp->r_statelock);
4514 recov_state.rs_flags = 0;
4515 recov_state.rs_num_retry_despite_err = 0;
4517 recov_retry:
4518 args.array_len = 3;
4519 args.array = argop;
4520 args.ctag = TAG_READLINK;
4522 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4523 if (e.error) {
4524 return (e.error);
4527 /* 0. putfh symlink fh */
4528 argop[0].argop = OP_CPUTFH;
4529 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4531 /* 1. readlink */
4532 argop[1].argop = OP_READLINK;
4534 /* 2. getattr */
4535 argop[2].argop = OP_GETATTR;
4536 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4537 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4539 doqueue = 1;
4541 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4542 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4543 rnode4info(VTOR4(vp))));
4545 t = gethrtime();
4547 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4549 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4550 if (needrecov) {
4551 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4552 "nfs4_readlink: initiating recovery\n"));
4554 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4555 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4556 if (!e.error)
4557 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4559 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4560 needrecov);
4561 goto recov_retry;
4565 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4567 if (e.error)
4568 return (e.error);
4571 * There is an path in the code below which calls
4572 * nfs4_purge_stale_fh(), which may generate otw calls through
4573 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4574 * here to avoid nfs4_start_op() deadlock.
4577 if (res.status && (res.array_len < args.array_len)) {
4579 * either Putfh or Link failed
4581 e.error = geterrno4(res.status);
4582 nfs4_purge_stale_fh(e.error, vp, cr);
4583 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4584 return (e.error);
4587 resop = &res.array[1]; /* readlink res */
4588 lr_res = &resop->nfs_resop4_u.opreadlink;
4591 * treat symlink names as data
4593 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4594 if (linkdata != NULL) {
4595 int uio_len = len - 1;
4596 /* len includes null byte, which we won't uiomove */
4597 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4598 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4599 mutex_enter(&rp->r_statelock);
4600 if (rp->r_symlink.contents == NULL) {
4601 rp->r_symlink.contents = linkdata;
4602 rp->r_symlink.len = uio_len;
4603 rp->r_symlink.size = len;
4604 mutex_exit(&rp->r_statelock);
4605 } else {
4606 mutex_exit(&rp->r_statelock);
4607 kmem_free(linkdata, len);
4609 } else {
4610 kmem_free(linkdata, len);
4613 if (res.status == NFS4_OK) {
4614 resop++; /* getattr res */
4615 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4617 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4619 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4622 * The over the wire error for attempting to readlink something
4623 * other than a symbolic link is ENXIO. However, we need to
4624 * return EINVAL instead of ENXIO, so we map it here.
4626 return (e.error == ENXIO ? EINVAL : e.error);
4630 * Flush local dirty pages to stable storage on the server.
4632 * If FNODSYNC is specified, then there is nothing to do because
4633 * metadata changes are not cached on the client before being
4634 * sent to the server.
4636 /* ARGSUSED */
4637 static int
4638 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4640 int error;
4642 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4643 return (0);
4644 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4645 return (EIO);
4646 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4647 if (!error)
4648 error = VTOR4(vp)->r_error;
4649 return (error);
4653 * Weirdness: if the file was removed or the target of a rename
4654 * operation while it was open, it got renamed instead. Here we
4655 * remove the renamed file.
4657 /* ARGSUSED */
4658 void
4659 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4661 rnode4_t *rp;
4663 ASSERT(vp != DNLC_NO_VNODE);
4665 rp = VTOR4(vp);
4667 if (IS_SHADOW(vp, rp)) {
4668 sv_inactive(vp);
4669 return;
4673 * If this is coming from the wrong zone, we let someone in the right
4674 * zone take care of it asynchronously. We can get here due to
4675 * VN_RELE() being called from pageout() or fsflush(). This call may
4676 * potentially turn into an expensive no-op if, for instance, v_count
4677 * gets incremented in the meantime, but it's still correct.
4679 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4680 nfs4_async_inactive(vp, cr);
4681 return;
4685 * Some of the cleanup steps might require over-the-wire
4686 * operations. Since fop_inactive can get called as a result of
4687 * other over-the-wire operations (e.g., an attribute cache update
4688 * can lead to a DNLC purge), doing those steps now would lead to a
4689 * nested call to the recovery framework, which can deadlock. So
4690 * do any over-the-wire cleanups asynchronously, in a separate
4691 * thread.
4694 mutex_enter(&rp->r_os_lock);
4695 mutex_enter(&rp->r_statelock);
4696 mutex_enter(&rp->r_statev4_lock);
4698 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4699 mutex_exit(&rp->r_statev4_lock);
4700 mutex_exit(&rp->r_statelock);
4701 mutex_exit(&rp->r_os_lock);
4702 nfs4_async_inactive(vp, cr);
4703 return;
4706 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4707 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4708 mutex_exit(&rp->r_statev4_lock);
4709 mutex_exit(&rp->r_statelock);
4710 mutex_exit(&rp->r_os_lock);
4711 nfs4_async_inactive(vp, cr);
4712 return;
4715 if (rp->r_unldvp != NULL) {
4716 mutex_exit(&rp->r_statev4_lock);
4717 mutex_exit(&rp->r_statelock);
4718 mutex_exit(&rp->r_os_lock);
4719 nfs4_async_inactive(vp, cr);
4720 return;
4722 mutex_exit(&rp->r_statev4_lock);
4723 mutex_exit(&rp->r_statelock);
4724 mutex_exit(&rp->r_os_lock);
4726 rp4_addfree(rp, cr);
4730 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4731 * various bits of state. The caller must not refer to vp after this call.
4734 void
4735 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4737 rnode4_t *rp = VTOR4(vp);
4738 nfs4_recov_state_t recov_state;
4739 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4740 vnode_t *unldvp;
4741 char *unlname;
4742 cred_t *unlcred;
4743 COMPOUND4args_clnt args;
4744 COMPOUND4res_clnt res, *resp;
4745 nfs_argop4 argop[2];
4746 int doqueue;
4747 #ifdef DEBUG
4748 char *name;
4749 #endif
4751 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4752 ASSERT(!IS_SHADOW(vp, rp));
4754 #ifdef DEBUG
4755 name = fn_name(VTOSV(vp)->sv_name);
4756 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4757 "release vnode %s", name));
4758 kmem_free(name, MAXNAMELEN);
4759 #endif
4761 if (vp->v_type == VREG) {
4762 bool_t recov_failed = FALSE;
4764 e.error = nfs4close_all(vp, cr);
4765 if (e.error) {
4766 /* Check to see if recovery failed */
4767 mutex_enter(&(VTOMI4(vp)->mi_lock));
4768 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4769 recov_failed = TRUE;
4770 mutex_exit(&(VTOMI4(vp)->mi_lock));
4771 if (!recov_failed) {
4772 mutex_enter(&rp->r_statelock);
4773 if (rp->r_flags & R4RECOVERR)
4774 recov_failed = TRUE;
4775 mutex_exit(&rp->r_statelock);
4777 if (recov_failed) {
4778 NFS4_DEBUG(nfs4_client_recov_debug,
4779 (CE_NOTE, "nfs4_inactive_otw: "
4780 "close failed (recovery failure)"));
4785 redo:
4786 if (rp->r_unldvp == NULL) {
4787 rp4_addfree(rp, cr);
4788 return;
4792 * Save the vnode pointer for the directory where the
4793 * unlinked-open file got renamed, then set it to NULL
4794 * to prevent another thread from getting here before
4795 * we're done with the remove. While we have the
4796 * statelock, make local copies of the pertinent rnode
4797 * fields. If we weren't to do this in an atomic way, the
4798 * the unl* fields could become inconsistent with respect
4799 * to each other due to a race condition between this
4800 * code and nfs_remove(). See bug report 1034328.
4802 mutex_enter(&rp->r_statelock);
4803 if (rp->r_unldvp == NULL) {
4804 mutex_exit(&rp->r_statelock);
4805 rp4_addfree(rp, cr);
4806 return;
4809 unldvp = rp->r_unldvp;
4810 rp->r_unldvp = NULL;
4811 unlname = rp->r_unlname;
4812 rp->r_unlname = NULL;
4813 unlcred = rp->r_unlcred;
4814 rp->r_unlcred = NULL;
4815 mutex_exit(&rp->r_statelock);
4818 * If there are any dirty pages left, then flush
4819 * them. This is unfortunate because they just
4820 * may get thrown away during the remove operation,
4821 * but we have to do this for correctness.
4823 if (nfs4_has_pages(vp) &&
4824 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4825 ASSERT(vp->v_type != VCHR);
4826 e.error = nfs4_putpage(vp, 0, 0, 0, cr, NULL);
4827 if (e.error) {
4828 mutex_enter(&rp->r_statelock);
4829 if (!rp->r_error)
4830 rp->r_error = e.error;
4831 mutex_exit(&rp->r_statelock);
4835 recov_state.rs_flags = 0;
4836 recov_state.rs_num_retry_despite_err = 0;
4837 recov_retry_remove:
4839 * Do the remove operation on the renamed file
4841 args.ctag = TAG_INACTIVE;
4844 * Remove ops: putfh dir; remove
4846 args.array_len = 2;
4847 args.array = argop;
4849 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4850 if (e.error) {
4851 kmem_free(unlname, MAXNAMELEN);
4852 crfree(unlcred);
4853 VN_RELE(unldvp);
4855 * Try again; this time around r_unldvp will be NULL, so we'll
4856 * just call rp4_addfree() and return.
4858 goto redo;
4861 /* putfh directory */
4862 argop[0].argop = OP_CPUTFH;
4863 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4865 /* remove */
4866 argop[1].argop = OP_CREMOVE;
4867 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4869 doqueue = 1;
4870 resp = &res;
4872 #if 0 /* notyet */
4874 * Can't do this yet. We may be being called from
4875 * dnlc_purge_XXX while that routine is holding a
4876 * mutex lock to the nc_rele list. The calls to
4877 * nfs3_cache_wcc_data may result in calls to
4878 * dnlc_purge_XXX. This will result in a deadlock.
4880 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4881 if (e.error) {
4882 PURGE_ATTRCACHE4(unldvp);
4883 resp = NULL;
4884 } else if (res.status) {
4885 e.error = geterrno4(res.status);
4886 PURGE_ATTRCACHE4(unldvp);
4888 * This code is inactive right now
4889 * but if made active there should
4890 * be a nfs4_end_op() call before
4891 * nfs4_purge_stale_fh to avoid start_op()
4892 * deadlock. See BugId: 4948726
4894 nfs4_purge_stale_fh(error, unldvp, cr);
4895 } else {
4896 nfs_resop4 *resop;
4897 REMOVE4res *rm_res;
4899 resop = &res.array[1];
4900 rm_res = &resop->nfs_resop4_u.opremove;
4902 * Update directory cache attribute,
4903 * readdir and dnlc caches.
4905 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4907 #else
4908 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4910 PURGE_ATTRCACHE4(unldvp);
4911 #endif
4913 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4914 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4915 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4916 if (!e.error)
4917 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4918 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4919 &recov_state, TRUE);
4920 goto recov_retry_remove;
4923 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4926 * Release stuff held for the remove
4928 VN_RELE(unldvp);
4929 if (!e.error && resp)
4930 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4932 kmem_free(unlname, MAXNAMELEN);
4933 crfree(unlcred);
4934 goto redo;
4938 * Remote file system operations having to do with directory manipulation.
4940 /* ARGSUSED3 */
4942 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4943 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4944 int *direntflags, pathname_t *realpnp)
4946 int error;
4947 vnode_t *vp, *avp = NULL;
4948 rnode4_t *drp;
4950 *vpp = NULL;
4951 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4952 return (EPERM);
4954 * if LOOKUP_XATTR, must replace dvp (object) with
4955 * object's attrdir before continuing with lookup
4957 if (flags & LOOKUP_XATTR) {
4958 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4959 if (error)
4960 return (error);
4962 dvp = avp;
4965 * If lookup is for "", just return dvp now. The attrdir
4966 * has already been activated (from nfs4lookup_xattr), and
4967 * the caller will RELE the original dvp -- not
4968 * the attrdir. So, set vpp and return.
4969 * Currently, when the LOOKUP_XATTR flag is
4970 * passed to fop_lookup, the name is always empty, and
4971 * shortcircuiting here avoids 3 unneeded lock/unlock
4972 * pairs.
4974 * If a non-empty name was provided, then it is the
4975 * attribute name, and it will be looked up below.
4977 if (*nm == '\0') {
4978 *vpp = dvp;
4979 return (0);
4983 * The vfs layer never sends a name when asking for the
4984 * attrdir, so we should never get here (unless of course
4985 * name is passed at some time in future -- at which time
4986 * we'll blow up here).
4988 ASSERT(0);
4991 drp = VTOR4(dvp);
4992 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4993 return (EINTR);
4995 error = nfs4lookup(dvp, nm, vpp, cr, 0);
4996 nfs_rw_exit(&drp->r_rwlock);
4999 * If vnode is a device, create special vnode.
5001 if (!error && ISVDEV((*vpp)->v_type)) {
5002 vp = *vpp;
5003 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5004 VN_RELE(vp);
5007 return (error);
5010 /* ARGSUSED */
5011 static int
5012 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5014 int error;
5015 rnode4_t *drp;
5016 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5017 mntinfo4_t *mi;
5019 mi = VTOMI4(dvp);
5020 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5021 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5022 return (EINVAL);
5024 drp = VTOR4(dvp);
5025 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5026 return (EINTR);
5028 mutex_enter(&drp->r_statelock);
5030 * If the server doesn't support xattrs just return EINVAL
5032 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5033 mutex_exit(&drp->r_statelock);
5034 nfs_rw_exit(&drp->r_rwlock);
5035 return (EINVAL);
5039 * If there is a cached xattr directory entry,
5040 * use it as long as the attributes are valid. If the
5041 * attributes are not valid, take the simple approach and
5042 * free the cached value and re-fetch a new value.
5044 * We don't negative entry cache for now, if we did we
5045 * would need to check if the file has changed on every
5046 * lookup. But xattrs don't exist very often and failing
5047 * an openattr is not much more expensive than and NVERIFY or GETATTR
5048 * so do an openattr over the wire for now.
5050 if (drp->r_xattr_dir != NULL) {
5051 if (ATTRCACHE4_VALID(dvp)) {
5052 VN_HOLD(drp->r_xattr_dir);
5053 *vpp = drp->r_xattr_dir;
5054 mutex_exit(&drp->r_statelock);
5055 nfs_rw_exit(&drp->r_rwlock);
5056 return (0);
5058 VN_RELE(drp->r_xattr_dir);
5059 drp->r_xattr_dir = NULL;
5061 mutex_exit(&drp->r_statelock);
5063 error = nfs4openattr(dvp, vpp, cflag, cr);
5065 nfs_rw_exit(&drp->r_rwlock);
5067 return (error);
5070 static int
5071 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5073 int error;
5074 rnode4_t *drp;
5076 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5079 * If lookup is for "", just return dvp. Don't need
5080 * to send it over the wire, look it up in the dnlc,
5081 * or perform any access checks.
5083 if (*nm == '\0') {
5084 VN_HOLD(dvp);
5085 *vpp = dvp;
5086 return (0);
5090 * Can't do lookups in non-directories.
5092 if (dvp->v_type != VDIR)
5093 return (ENOTDIR);
5096 * If lookup is for ".", just return dvp. Don't need
5097 * to send it over the wire or look it up in the dnlc,
5098 * just need to check access.
5100 if (nm[0] == '.' && nm[1] == '\0') {
5101 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5102 if (error)
5103 return (error);
5104 VN_HOLD(dvp);
5105 *vpp = dvp;
5106 return (0);
5109 drp = VTOR4(dvp);
5110 if (!(drp->r_flags & R4LOOKUP)) {
5111 mutex_enter(&drp->r_statelock);
5112 drp->r_flags |= R4LOOKUP;
5113 mutex_exit(&drp->r_statelock);
5116 *vpp = NULL;
5118 * Lookup this name in the DNLC. If there is no entry
5119 * lookup over the wire.
5121 if (!skipdnlc)
5122 *vpp = dnlc_lookup(dvp, nm);
5123 if (*vpp == NULL) {
5125 * We need to go over the wire to lookup the name.
5127 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5131 * We hit on the dnlc
5133 if (*vpp != DNLC_NO_VNODE ||
5134 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5136 * But our attrs may not be valid.
5138 if (ATTRCACHE4_VALID(dvp)) {
5139 error = nfs4_waitfor_purge_complete(dvp);
5140 if (error) {
5141 VN_RELE(*vpp);
5142 *vpp = NULL;
5143 return (error);
5147 * If after the purge completes, check to make sure
5148 * our attrs are still valid.
5150 if (ATTRCACHE4_VALID(dvp)) {
5152 * If we waited for a purge we may have
5153 * lost our vnode so look it up again.
5155 VN_RELE(*vpp);
5156 *vpp = dnlc_lookup(dvp, nm);
5157 if (*vpp == NULL)
5158 return (nfs4lookupnew_otw(dvp,
5159 nm, vpp, cr));
5162 * The access cache should almost always hit
5164 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5166 if (error) {
5167 VN_RELE(*vpp);
5168 *vpp = NULL;
5169 return (error);
5171 if (*vpp == DNLC_NO_VNODE) {
5172 VN_RELE(*vpp);
5173 *vpp = NULL;
5174 return (ENOENT);
5176 return (0);
5181 ASSERT(*vpp != NULL);
5184 * We may have gotten here we have one of the following cases:
5185 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5186 * need to validate them.
5187 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5188 * must validate.
5190 * Go to the server and check if the directory has changed, if
5191 * it hasn't we are done and can use the dnlc entry.
5193 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5197 * Go to the server and check if the directory has changed, if
5198 * it hasn't we are done and can use the dnlc entry. If it
5199 * has changed we get a new copy of its attributes and check
5200 * the access for VEXEC, then relookup the filename and
5201 * get its filehandle and attributes.
5203 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5204 * if the NVERIFY failed we must
5205 * purge the caches
5206 * cache new attributes (will set r_time_attr_inval)
5207 * cache new access
5208 * recheck VEXEC access
5209 * add name to dnlc, possibly negative
5210 * if LOOKUP succeeded
5211 * cache new attributes
5212 * else
5213 * set a new r_time_attr_inval for dvp
5214 * check to make sure we have access
5216 * The vpp returned is the vnode passed in if the directory is valid,
5217 * a new vnode if successful lookup, or NULL on error.
5219 static int
5220 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5222 COMPOUND4args_clnt args;
5223 COMPOUND4res_clnt res;
5224 fattr4 *ver_fattr;
5225 fattr4_change dchange;
5226 int32_t *ptr;
5227 int argoplist_size = 7 * sizeof (nfs_argop4);
5228 nfs_argop4 *argop;
5229 int doqueue;
5230 mntinfo4_t *mi;
5231 nfs4_recov_state_t recov_state;
5232 hrtime_t t;
5233 int isdotdot;
5234 vnode_t *nvp;
5235 nfs_fh4 *fhp;
5236 nfs4_sharedfh_t *sfhp;
5237 nfs4_access_type_t cacc;
5238 rnode4_t *nrp;
5239 rnode4_t *drp = VTOR4(dvp);
5240 nfs4_ga_res_t *garp = NULL;
5241 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5243 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5244 ASSERT(nm != NULL);
5245 ASSERT(nm[0] != '\0');
5246 ASSERT(dvp->v_type == VDIR);
5247 ASSERT(nm[0] != '.' || nm[1] != '\0');
5248 ASSERT(*vpp != NULL);
5250 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5251 isdotdot = 1;
5252 args.ctag = TAG_LOOKUP_VPARENT;
5253 } else {
5255 * If dvp were a stub, it should have triggered and caused
5256 * a mount for us to get this far.
5258 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5260 isdotdot = 0;
5261 args.ctag = TAG_LOOKUP_VALID;
5264 mi = VTOMI4(dvp);
5265 recov_state.rs_flags = 0;
5266 recov_state.rs_num_retry_despite_err = 0;
5268 nvp = NULL;
5270 /* Save the original mount point security information */
5271 (void) save_mnt_secinfo(mi->mi_curr_serv);
5273 recov_retry:
5274 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5275 &recov_state, NULL);
5276 if (e.error) {
5277 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5278 VN_RELE(*vpp);
5279 *vpp = NULL;
5280 return (e.error);
5283 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5285 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5286 args.array_len = 7;
5287 args.array = argop;
5289 /* 0. putfh file */
5290 argop[0].argop = OP_CPUTFH;
5291 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5293 /* 1. nverify the change info */
5294 argop[1].argop = OP_NVERIFY;
5295 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5296 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5297 ver_fattr->attrlist4 = (char *)&dchange;
5298 ptr = (int32_t *)&dchange;
5299 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5300 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5302 /* 2. getattr directory */
5303 argop[2].argop = OP_GETATTR;
5304 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5305 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5307 /* 3. access directory */
5308 argop[3].argop = OP_ACCESS;
5309 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5310 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5312 /* 4. lookup name */
5313 if (isdotdot) {
5314 argop[4].argop = OP_LOOKUPP;
5315 } else {
5316 argop[4].argop = OP_CLOOKUP;
5317 argop[4].nfs_argop4_u.opclookup.cname = nm;
5320 /* 5. resulting file handle */
5321 argop[5].argop = OP_GETFH;
5323 /* 6. resulting file attributes */
5324 argop[6].argop = OP_GETATTR;
5325 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5326 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5328 doqueue = 1;
5329 t = gethrtime();
5331 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5333 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5334 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5335 if (e.error != 0 && *vpp != NULL)
5336 VN_RELE(*vpp);
5337 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5338 &recov_state, FALSE);
5339 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5340 kmem_free(argop, argoplist_size);
5341 return (e.error);
5344 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5346 * For WRONGSEC of a non-dotdot case, send secinfo directly
5347 * from this thread, do not go thru the recovery thread since
5348 * we need the nm information.
5350 * Not doing dotdot case because there is no specification
5351 * for (PUTFH, SECINFO "..") yet.
5353 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5354 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5355 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5356 &recov_state, FALSE);
5357 else
5358 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5359 &recov_state, TRUE);
5360 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5361 kmem_free(argop, argoplist_size);
5362 if (!e.error)
5363 goto recov_retry;
5364 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5365 VN_RELE(*vpp);
5366 *vpp = NULL;
5367 return (e.error);
5370 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5371 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5372 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5373 &recov_state, TRUE);
5375 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5376 kmem_free(argop, argoplist_size);
5377 goto recov_retry;
5381 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5383 if (e.error || res.array_len == 0) {
5385 * If e.error isn't set, then reply has no ops (or we couldn't
5386 * be here). The only legal way to reply without an op array
5387 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5388 * be in the reply for all other status values.
5390 * For valid replies without an ops array, return ENOTSUP
5391 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5392 * return EIO -- don't trust status.
5394 if (e.error == 0)
5395 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5396 ENOTSUP : EIO;
5397 VN_RELE(*vpp);
5398 *vpp = NULL;
5399 kmem_free(argop, argoplist_size);
5400 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5401 return (e.error);
5404 if (res.status != NFS4ERR_SAME) {
5405 e.error = geterrno4(res.status);
5408 * The NVERIFY "failed" so the directory has changed
5409 * First make sure PUTFH succeeded and NVERIFY "failed"
5410 * cleanly.
5412 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5413 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5414 nfs4_purge_stale_fh(e.error, dvp, cr);
5415 VN_RELE(*vpp);
5416 *vpp = NULL;
5417 goto exit;
5421 * We know the NVERIFY "failed" so we must:
5422 * purge the caches (access and indirectly dnlc if needed)
5424 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5426 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5427 nfs4_purge_stale_fh(e.error, dvp, cr);
5428 VN_RELE(*vpp);
5429 *vpp = NULL;
5430 goto exit;
5434 * Install new cached attributes for the directory
5436 nfs4_attr_cache(dvp,
5437 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5438 t, cr, FALSE, NULL);
5440 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5441 nfs4_purge_stale_fh(e.error, dvp, cr);
5442 VN_RELE(*vpp);
5443 *vpp = NULL;
5444 e.error = geterrno4(res.status);
5445 goto exit;
5449 * Now we know the directory is valid,
5450 * cache new directory access
5452 nfs4_access_cache(drp,
5453 args.array[3].nfs_argop4_u.opaccess.access,
5454 res.array[3].nfs_resop4_u.opaccess.access, cr);
5457 * recheck VEXEC access
5459 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5460 if (cacc != NFS4_ACCESS_ALLOWED) {
5462 * Directory permissions might have been revoked
5464 if (cacc == NFS4_ACCESS_DENIED) {
5465 e.error = EACCES;
5466 VN_RELE(*vpp);
5467 *vpp = NULL;
5468 goto exit;
5472 * Somehow we must not have asked for enough
5473 * so try a singleton ACCESS, should never happen.
5475 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5476 if (e.error) {
5477 VN_RELE(*vpp);
5478 *vpp = NULL;
5479 goto exit;
5483 e.error = geterrno4(res.status);
5484 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5486 * The lookup failed, probably no entry
5488 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5489 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5490 } else {
5492 * Might be some other error, so remove
5493 * the dnlc entry to make sure we start all
5494 * over again, next time.
5496 dnlc_remove(dvp, nm);
5498 VN_RELE(*vpp);
5499 *vpp = NULL;
5500 goto exit;
5503 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5505 * The file exists but we can't get its fh for
5506 * some unknown reason. Remove it from the dnlc
5507 * and error out to be safe.
5509 dnlc_remove(dvp, nm);
5510 VN_RELE(*vpp);
5511 *vpp = NULL;
5512 goto exit;
5514 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5515 if (fhp->nfs_fh4_len == 0) {
5517 * The file exists but a bogus fh
5518 * some unknown reason. Remove it from the dnlc
5519 * and error out to be safe.
5521 e.error = ENOENT;
5522 dnlc_remove(dvp, nm);
5523 VN_RELE(*vpp);
5524 *vpp = NULL;
5525 goto exit;
5527 sfhp = sfh4_get(fhp, mi);
5529 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5530 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5533 * Make the new rnode
5535 if (isdotdot) {
5536 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5537 if (e.error) {
5538 sfh4_rele(&sfhp);
5539 VN_RELE(*vpp);
5540 *vpp = NULL;
5541 goto exit;
5544 * XXX if nfs4_make_dotdot uses an existing rnode
5545 * XXX it doesn't update the attributes.
5546 * XXX for now just save them again to save an OTW
5548 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5549 } else {
5550 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5551 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5553 * If v_type == VNON, then garp was NULL because
5554 * the last op in the compound failed and makenfs4node
5555 * could not find the vnode for sfhp. It created
5556 * a new vnode, so we have nothing to purge here.
5558 if (nvp->v_type == VNON) {
5559 vattr_t vattr;
5561 vattr.va_mask = AT_TYPE;
5563 * N.B. We've already called nfs4_end_fop above.
5565 e.error = nfs4getattr(nvp, &vattr, cr);
5566 if (e.error) {
5567 sfh4_rele(&sfhp);
5568 VN_RELE(*vpp);
5569 *vpp = NULL;
5570 VN_RELE(nvp);
5571 goto exit;
5573 nvp->v_type = vattr.va_type;
5576 sfh4_rele(&sfhp);
5578 nrp = VTOR4(nvp);
5579 mutex_enter(&nrp->r_statev4_lock);
5580 if (!nrp->created_v4) {
5581 mutex_exit(&nrp->r_statev4_lock);
5582 dnlc_update(dvp, nm, nvp);
5583 } else
5584 mutex_exit(&nrp->r_statev4_lock);
5586 VN_RELE(*vpp);
5587 *vpp = nvp;
5588 } else {
5589 hrtime_t now;
5590 hrtime_t delta = 0;
5592 e.error = 0;
5595 * Because the NVERIFY "succeeded" we know that the
5596 * directory attributes are still valid
5597 * so update r_time_attr_inval
5599 now = gethrtime();
5600 mutex_enter(&drp->r_statelock);
5601 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5602 delta = now - drp->r_time_attr_saved;
5603 if (delta < mi->mi_acdirmin)
5604 delta = mi->mi_acdirmin;
5605 else if (delta > mi->mi_acdirmax)
5606 delta = mi->mi_acdirmax;
5608 drp->r_time_attr_inval = now + delta;
5609 mutex_exit(&drp->r_statelock);
5610 dnlc_update(dvp, nm, *vpp);
5613 * Even though we have a valid directory attr cache
5614 * and dnlc entry, we may not have access.
5615 * This should almost always hit the cache.
5617 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5618 if (e.error) {
5619 VN_RELE(*vpp);
5620 *vpp = NULL;
5623 if (*vpp == DNLC_NO_VNODE) {
5624 VN_RELE(*vpp);
5625 *vpp = NULL;
5626 e.error = ENOENT;
5630 exit:
5631 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5632 kmem_free(argop, argoplist_size);
5633 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5634 return (e.error);
5638 * We need to go over the wire to lookup the name, but
5639 * while we are there verify the directory has not
5640 * changed but if it has, get new attributes and check access
5642 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5643 * NVERIFY GETATTR ACCESS
5645 * With the results:
5646 * if the NVERIFY failed we must purge the caches, add new attributes,
5647 * and cache new access.
5648 * set a new r_time_attr_inval
5649 * add name to dnlc, possibly negative
5650 * if LOOKUP succeeded
5651 * cache new attributes
5653 static int
5654 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5656 COMPOUND4args_clnt args;
5657 COMPOUND4res_clnt res;
5658 fattr4 *ver_fattr;
5659 fattr4_change dchange;
5660 int32_t *ptr;
5661 nfs4_ga_res_t *garp = NULL;
5662 int argoplist_size = 9 * sizeof (nfs_argop4);
5663 nfs_argop4 *argop;
5664 int doqueue;
5665 mntinfo4_t *mi;
5666 nfs4_recov_state_t recov_state;
5667 hrtime_t t;
5668 int isdotdot;
5669 vnode_t *nvp;
5670 nfs_fh4 *fhp;
5671 nfs4_sharedfh_t *sfhp;
5672 nfs4_access_type_t cacc;
5673 rnode4_t *nrp;
5674 rnode4_t *drp = VTOR4(dvp);
5675 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5677 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5678 ASSERT(nm != NULL);
5679 ASSERT(nm[0] != '\0');
5680 ASSERT(dvp->v_type == VDIR);
5681 ASSERT(nm[0] != '.' || nm[1] != '\0');
5682 ASSERT(*vpp == NULL);
5684 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5685 isdotdot = 1;
5686 args.ctag = TAG_LOOKUP_PARENT;
5687 } else {
5689 * If dvp were a stub, it should have triggered and caused
5690 * a mount for us to get this far.
5692 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5694 isdotdot = 0;
5695 args.ctag = TAG_LOOKUP;
5698 mi = VTOMI4(dvp);
5699 recov_state.rs_flags = 0;
5700 recov_state.rs_num_retry_despite_err = 0;
5702 nvp = NULL;
5704 /* Save the original mount point security information */
5705 (void) save_mnt_secinfo(mi->mi_curr_serv);
5707 recov_retry:
5708 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5709 &recov_state, NULL);
5710 if (e.error) {
5711 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5712 return (e.error);
5715 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5717 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5718 args.array_len = 9;
5719 args.array = argop;
5721 /* 0. putfh file */
5722 argop[0].argop = OP_CPUTFH;
5723 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5725 /* 1. savefh for the nverify */
5726 argop[1].argop = OP_SAVEFH;
5728 /* 2. lookup name */
5729 if (isdotdot) {
5730 argop[2].argop = OP_LOOKUPP;
5731 } else {
5732 argop[2].argop = OP_CLOOKUP;
5733 argop[2].nfs_argop4_u.opclookup.cname = nm;
5736 /* 3. resulting file handle */
5737 argop[3].argop = OP_GETFH;
5739 /* 4. resulting file attributes */
5740 argop[4].argop = OP_GETATTR;
5741 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5742 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5744 /* 5. restorefh back the directory for the nverify */
5745 argop[5].argop = OP_RESTOREFH;
5747 /* 6. nverify the change info */
5748 argop[6].argop = OP_NVERIFY;
5749 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5750 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5751 ver_fattr->attrlist4 = (char *)&dchange;
5752 ptr = (int32_t *)&dchange;
5753 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5754 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5756 /* 7. getattr directory */
5757 argop[7].argop = OP_GETATTR;
5758 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5759 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5761 /* 8. access directory */
5762 argop[8].argop = OP_ACCESS;
5763 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5764 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5766 doqueue = 1;
5767 t = gethrtime();
5769 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5771 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5772 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5773 if (e.error != 0 && *vpp != NULL)
5774 VN_RELE(*vpp);
5775 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5776 &recov_state, FALSE);
5777 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5778 kmem_free(argop, argoplist_size);
5779 return (e.error);
5782 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5784 * For WRONGSEC of a non-dotdot case, send secinfo directly
5785 * from this thread, do not go thru the recovery thread since
5786 * we need the nm information.
5788 * Not doing dotdot case because there is no specification
5789 * for (PUTFH, SECINFO "..") yet.
5791 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5792 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5793 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5794 &recov_state, FALSE);
5795 else
5796 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5797 &recov_state, TRUE);
5798 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5799 kmem_free(argop, argoplist_size);
5800 if (!e.error)
5801 goto recov_retry;
5802 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5803 return (e.error);
5806 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5807 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5809 &recov_state, TRUE);
5811 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5812 kmem_free(argop, argoplist_size);
5813 goto recov_retry;
5817 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5819 if (e.error || res.array_len == 0) {
5821 * If e.error isn't set, then reply has no ops (or we couldn't
5822 * be here). The only legal way to reply without an op array
5823 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5824 * be in the reply for all other status values.
5826 * For valid replies without an ops array, return ENOTSUP
5827 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5828 * return EIO -- don't trust status.
5830 if (e.error == 0)
5831 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5832 ENOTSUP : EIO;
5834 kmem_free(argop, argoplist_size);
5835 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5836 return (e.error);
5839 e.error = geterrno4(res.status);
5842 * The PUTFH and SAVEFH may have failed.
5844 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5845 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5846 nfs4_purge_stale_fh(e.error, dvp, cr);
5847 goto exit;
5851 * Check if the file exists, if it does delay entering
5852 * into the dnlc until after we update the directory
5853 * attributes so we don't cause it to get purged immediately.
5855 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5857 * The lookup failed, probably no entry
5859 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5860 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5861 goto exit;
5864 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5866 * The file exists but we can't get its fh for
5867 * some unknown reason. Error out to be safe.
5869 goto exit;
5872 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5873 if (fhp->nfs_fh4_len == 0) {
5875 * The file exists but a bogus fh
5876 * some unknown reason. Error out to be safe.
5878 e.error = EIO;
5879 goto exit;
5881 sfhp = sfh4_get(fhp, mi);
5883 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5884 sfh4_rele(&sfhp);
5885 goto exit;
5887 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5890 * The RESTOREFH may have failed
5892 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5893 sfh4_rele(&sfhp);
5894 e.error = EIO;
5895 goto exit;
5898 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5900 * First make sure the NVERIFY failed as we expected,
5901 * if it didn't then be conservative and error out
5902 * as we can't trust the directory.
5904 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5905 sfh4_rele(&sfhp);
5906 e.error = EIO;
5907 goto exit;
5911 * We know the NVERIFY "failed" so the directory has changed,
5912 * so we must:
5913 * purge the caches (access and indirectly dnlc if needed)
5915 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5917 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5918 sfh4_rele(&sfhp);
5919 goto exit;
5921 nfs4_attr_cache(dvp,
5922 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5923 t, cr, FALSE, NULL);
5925 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5926 nfs4_purge_stale_fh(e.error, dvp, cr);
5927 sfh4_rele(&sfhp);
5928 e.error = geterrno4(res.status);
5929 goto exit;
5933 * Now we know the directory is valid,
5934 * cache new directory access
5936 nfs4_access_cache(drp,
5937 args.array[8].nfs_argop4_u.opaccess.access,
5938 res.array[8].nfs_resop4_u.opaccess.access, cr);
5941 * recheck VEXEC access
5943 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5944 if (cacc != NFS4_ACCESS_ALLOWED) {
5946 * Directory permissions might have been revoked
5948 if (cacc == NFS4_ACCESS_DENIED) {
5949 sfh4_rele(&sfhp);
5950 e.error = EACCES;
5951 goto exit;
5955 * Somehow we must not have asked for enough
5956 * so try a singleton ACCESS should never happen
5958 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5959 if (e.error) {
5960 sfh4_rele(&sfhp);
5961 goto exit;
5965 e.error = geterrno4(res.status);
5966 } else {
5967 hrtime_t now;
5968 hrtime_t delta = 0;
5970 e.error = 0;
5973 * Because the NVERIFY "succeeded" we know that the
5974 * directory attributes are still valid
5975 * so update r_time_attr_inval
5977 now = gethrtime();
5978 mutex_enter(&drp->r_statelock);
5979 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5980 delta = now - drp->r_time_attr_saved;
5981 if (delta < mi->mi_acdirmin)
5982 delta = mi->mi_acdirmin;
5983 else if (delta > mi->mi_acdirmax)
5984 delta = mi->mi_acdirmax;
5986 drp->r_time_attr_inval = now + delta;
5987 mutex_exit(&drp->r_statelock);
5990 * Even though we have a valid directory attr cache,
5991 * we may not have access.
5992 * This should almost always hit the cache.
5994 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5995 if (e.error) {
5996 sfh4_rele(&sfhp);
5997 goto exit;
6002 * Now we have successfully completed the lookup, if the
6003 * directory has changed we now have the valid attributes.
6004 * We also know we have directory access.
6005 * Create the new rnode and insert it in the dnlc.
6007 if (isdotdot) {
6008 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6009 if (e.error) {
6010 sfh4_rele(&sfhp);
6011 goto exit;
6014 * XXX if nfs4_make_dotdot uses an existing rnode
6015 * XXX it doesn't update the attributes.
6016 * XXX for now just save them again to save an OTW
6018 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6019 } else {
6020 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6021 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6023 sfh4_rele(&sfhp);
6025 nrp = VTOR4(nvp);
6026 mutex_enter(&nrp->r_statev4_lock);
6027 if (!nrp->created_v4) {
6028 mutex_exit(&nrp->r_statev4_lock);
6029 dnlc_update(dvp, nm, nvp);
6030 } else
6031 mutex_exit(&nrp->r_statev4_lock);
6033 *vpp = nvp;
6035 exit:
6036 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6037 kmem_free(argop, argoplist_size);
6038 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6039 return (e.error);
6042 #ifdef DEBUG
6043 void
6044 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6046 uint_t i, len;
6047 zoneid_t zoneid = getzoneid();
6048 char *s;
6050 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6051 for (i = 0; i < argcnt; i++) {
6052 nfs_argop4 *op = &argbase[i];
6053 switch (op->argop) {
6054 case OP_CPUTFH:
6055 case OP_PUTFH:
6056 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6057 break;
6058 case OP_PUTROOTFH:
6059 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6060 break;
6061 case OP_CLOOKUP:
6062 s = op->nfs_argop4_u.opclookup.cname;
6063 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6064 break;
6065 case OP_LOOKUP:
6066 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6067 &len, NULL);
6068 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6069 kmem_free(s, len);
6070 break;
6071 case OP_LOOKUPP:
6072 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6073 break;
6074 case OP_GETFH:
6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6076 break;
6077 case OP_GETATTR:
6078 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6079 break;
6080 case OP_OPENATTR:
6081 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6082 break;
6083 default:
6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6085 op->argop);
6086 break;
6090 #endif
6093 * nfs4lookup_setup - constructs a multi-lookup compound request.
6095 * Given the path "nm1/nm2/.../nmn", the following compound requests
6096 * may be created:
6098 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6099 * is faster, for now.
6101 * l4_getattrs indicates the type of compound requested.
6103 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6105 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6107 * total number of ops is n + 1.
6109 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6110 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6111 * before the last component, and only get attributes
6112 * for the last component. Note that the second-to-last
6113 * pathname component is XATTR_RPATH, which does NOT go
6114 * over-the-wire as a lookup.
6116 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6117 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6119 * and total number of ops is n + 5.
6121 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6122 * attribute directory: create lookups plus an OPENATTR
6123 * replacing the last lookup. Note that the last pathname
6124 * component is XATTR_RPATH, which does NOT go over-the-wire
6125 * as a lookup.
6127 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6128 * Openattr; Getfh; Getattr }
6130 * and total number of ops is n + 5.
6132 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6133 * nodes too.
6135 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6136 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6138 * and total number of ops is 3*n + 1.
6140 * All cases: returns the index in the arg array of the final LOOKUP op, or
6141 * -1 if no LOOKUPs were used.
6144 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6146 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6147 nfs_argop4 *argbase, *argop;
6148 int arglen, argcnt;
6149 int n = 1; /* number of components */
6150 int nga = 1; /* number of Getattr's in request */
6151 char c = '\0', *s, *p;
6152 int lookup_idx = -1;
6153 int argoplist_size;
6155 /* set lookuparg response result to 0 */
6156 lookupargp->resp->status = NFS4_OK;
6158 /* skip leading "/" or "." e.g. ".//./" if there is */
6159 for (; ; nm++) {
6160 if (*nm != '/' && *nm != '.')
6161 break;
6163 /* ".." is counted as 1 component */
6164 if (*nm == '.' && *(nm + 1) != '/')
6165 break;
6169 * Find n = number of components - nm must be null terminated
6170 * Skip "." components.
6172 if (*nm != '\0')
6173 for (n = 1, s = nm; *s != '\0'; s++) {
6174 if ((*s == '/') && (*(s + 1) != '/') &&
6175 (*(s + 1) != '\0') &&
6176 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6177 *(s + 2) == '\0')))
6178 n++;
6180 else
6181 n = 0;
6184 * nga is number of components that need Getfh+Getattr
6186 switch (l4_getattrs) {
6187 case LKP4_NO_ATTRIBUTES:
6188 nga = 0;
6189 break;
6190 case LKP4_ALL_ATTRIBUTES:
6191 nga = n;
6193 * Always have at least 1 getfh, getattr pair
6195 if (nga == 0)
6196 nga++;
6197 break;
6198 case LKP4_LAST_ATTRDIR:
6199 case LKP4_LAST_NAMED_ATTR:
6200 nga = n+1;
6201 break;
6205 * If change to use the filehandle attr instead of getfh
6206 * the following line can be deleted.
6208 nga *= 2;
6211 * calculate number of ops in request as
6212 * header + trailer + lookups + getattrs
6214 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6216 argoplist_size = arglen * sizeof (nfs_argop4);
6217 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6218 lookupargp->argsp->array = argop;
6220 argcnt = lookupargp->header_len;
6221 argop += argcnt;
6224 * loop and create a lookup op and possibly getattr/getfh for
6225 * each component. Skip "." components.
6227 for (s = nm; *s != '\0'; s = p) {
6229 * Set up a pathname struct for each component if needed
6231 while (*s == '/')
6232 s++;
6233 if (*s == '\0')
6234 break;
6236 for (p = s; (*p != '/') && (*p != '\0'); p++)
6238 c = *p;
6239 *p = '\0';
6241 if (s[0] == '.' && s[1] == '\0') {
6242 *p = c;
6243 continue;
6245 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6246 strcmp(s, XATTR_RPATH) == 0) {
6247 /* getfh XXX may not be needed in future */
6248 argop->argop = OP_GETFH;
6249 argop++;
6250 argcnt++;
6252 /* getattr */
6253 argop->argop = OP_GETATTR;
6254 argop->nfs_argop4_u.opgetattr.attr_request =
6255 lookupargp->ga_bits;
6256 argop->nfs_argop4_u.opgetattr.mi =
6257 lookupargp->mi;
6258 argop++;
6259 argcnt++;
6261 /* openattr */
6262 argop->argop = OP_OPENATTR;
6263 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6264 strcmp(s, XATTR_RPATH) == 0) {
6265 /* openattr */
6266 argop->argop = OP_OPENATTR;
6267 argop++;
6268 argcnt++;
6270 /* getfh XXX may not be needed in future */
6271 argop->argop = OP_GETFH;
6272 argop++;
6273 argcnt++;
6275 /* getattr */
6276 argop->argop = OP_GETATTR;
6277 argop->nfs_argop4_u.opgetattr.attr_request =
6278 lookupargp->ga_bits;
6279 argop->nfs_argop4_u.opgetattr.mi =
6280 lookupargp->mi;
6281 argop++;
6282 argcnt++;
6283 *p = c;
6284 continue;
6285 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6286 /* lookupp */
6287 argop->argop = OP_LOOKUPP;
6288 } else {
6289 /* lookup */
6290 argop->argop = OP_LOOKUP;
6291 (void) str_to_utf8(s,
6292 &argop->nfs_argop4_u.oplookup.objname);
6294 lookup_idx = argcnt;
6295 argop++;
6296 argcnt++;
6298 *p = c;
6300 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6301 /* getfh XXX may not be needed in future */
6302 argop->argop = OP_GETFH;
6303 argop++;
6304 argcnt++;
6306 /* getattr */
6307 argop->argop = OP_GETATTR;
6308 argop->nfs_argop4_u.opgetattr.attr_request =
6309 lookupargp->ga_bits;
6310 argop->nfs_argop4_u.opgetattr.mi =
6311 lookupargp->mi;
6312 argop++;
6313 argcnt++;
6317 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6318 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6319 if (needgetfh) {
6320 /* stick in a post-lookup getfh */
6321 argop->argop = OP_GETFH;
6322 argcnt++;
6323 argop++;
6325 /* post-lookup getattr */
6326 argop->argop = OP_GETATTR;
6327 argop->nfs_argop4_u.opgetattr.attr_request =
6328 lookupargp->ga_bits;
6329 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6330 argcnt++;
6332 argcnt += lookupargp->trailer_len; /* actual op count */
6333 lookupargp->argsp->array_len = argcnt;
6334 lookupargp->arglen = arglen;
6336 #ifdef DEBUG
6337 if (nfs4_client_lookup_debug)
6338 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6339 #endif
6341 return (lookup_idx);
6344 static int
6345 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6347 COMPOUND4args_clnt args;
6348 COMPOUND4res_clnt res;
6349 GETFH4res *gf_res = NULL;
6350 nfs_argop4 argop[4];
6351 nfs_resop4 *resop = NULL;
6352 nfs4_sharedfh_t *sfhp;
6353 hrtime_t t;
6354 nfs4_error_t e;
6356 rnode4_t *drp;
6357 int doqueue = 1;
6358 vnode_t *vp;
6359 int needrecov = 0;
6360 nfs4_recov_state_t recov_state;
6362 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6364 *avp = NULL;
6365 recov_state.rs_flags = 0;
6366 recov_state.rs_num_retry_despite_err = 0;
6368 recov_retry:
6369 /* COMPOUND: putfh, openattr, getfh, getattr */
6370 args.array_len = 4;
6371 args.array = argop;
6372 args.ctag = TAG_OPENATTR;
6374 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6375 if (e.error)
6376 return (e.error);
6378 drp = VTOR4(dvp);
6380 /* putfh */
6381 argop[0].argop = OP_CPUTFH;
6382 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6384 /* openattr */
6385 argop[1].argop = OP_OPENATTR;
6386 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6388 /* getfh */
6389 argop[2].argop = OP_GETFH;
6391 /* getattr */
6392 argop[3].argop = OP_GETATTR;
6393 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6394 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6396 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6397 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6398 rnode4info(drp)));
6400 t = gethrtime();
6402 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6404 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6405 if (needrecov) {
6406 bool_t abort;
6408 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6409 "nfs4openattr: initiating recovery\n"));
6411 abort = nfs4_start_recovery(&e,
6412 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6413 OP_OPENATTR, NULL, NULL, NULL);
6414 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6415 if (!e.error) {
6416 e.error = geterrno4(res.status);
6417 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6419 if (abort == FALSE)
6420 goto recov_retry;
6421 return (e.error);
6424 if (e.error) {
6425 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6426 return (e.error);
6429 if (res.status) {
6431 * If OTW errro is NOTSUPP, then it should be
6432 * translated to EINVAL. All Solaris file system
6433 * implementations return EINVAL to the syscall layer
6434 * when the attrdir cannot be created due to an
6435 * implementation restriction or noxattr mount option.
6437 if (res.status == NFS4ERR_NOTSUPP) {
6438 mutex_enter(&drp->r_statelock);
6439 if (drp->r_xattr_dir)
6440 VN_RELE(drp->r_xattr_dir);
6441 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6442 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6443 mutex_exit(&drp->r_statelock);
6445 e.error = EINVAL;
6446 } else {
6447 e.error = geterrno4(res.status);
6450 if (e.error) {
6451 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6452 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6453 needrecov);
6454 return (e.error);
6458 resop = &res.array[0]; /* putfh res */
6459 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6461 resop = &res.array[1]; /* openattr res */
6462 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6464 resop = &res.array[2]; /* getfh res */
6465 gf_res = &resop->nfs_resop4_u.opgetfh;
6466 if (gf_res->object.nfs_fh4_len == 0) {
6467 *avp = NULL;
6468 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6469 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6470 return (ENOENT);
6473 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6474 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6475 dvp->v_vfsp, t, cr, dvp,
6476 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6477 sfh4_rele(&sfhp);
6479 if (e.error)
6480 PURGE_ATTRCACHE4(vp);
6482 mutex_enter(&vp->v_lock);
6483 vp->v_flag |= V_XATTRDIR;
6484 mutex_exit(&vp->v_lock);
6486 *avp = vp;
6488 mutex_enter(&drp->r_statelock);
6489 if (drp->r_xattr_dir)
6490 VN_RELE(drp->r_xattr_dir);
6491 VN_HOLD(vp);
6492 drp->r_xattr_dir = vp;
6495 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6496 * NULL. xattrs could be created at any time, and we have no
6497 * way to update pc4_xattr_exists in the base object if/when
6498 * it happens.
6500 drp->r_pathconf.pc4_xattr_valid = 0;
6502 mutex_exit(&drp->r_statelock);
6504 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6506 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6508 return (0);
6511 /* ARGSUSED */
6512 static int
6513 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6514 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6515 vsecattr_t *vsecp)
6517 int error;
6518 vnode_t *vp = NULL;
6519 rnode4_t *rp;
6520 struct vattr vattr;
6521 rnode4_t *drp;
6522 vnode_t *tempvp;
6523 enum createmode4 createmode;
6524 bool_t must_trunc = FALSE;
6525 int truncating = 0;
6527 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6528 return (EPERM);
6529 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6530 return (EINVAL);
6533 /* . and .. have special meaning in the protocol, reject them. */
6535 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6536 return (EISDIR);
6538 drp = VTOR4(dvp);
6540 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6541 return (EINTR);
6543 top:
6545 * We make a copy of the attributes because the caller does not
6546 * expect us to change what va points to.
6548 vattr = *va;
6551 * If the pathname is "", then dvp is the root vnode of
6552 * a remote file mounted over a local directory.
6553 * All that needs to be done is access
6554 * checking and truncation. Note that we avoid doing
6555 * open w/ create because the parent directory might
6556 * be in pseudo-fs and the open would fail.
6558 if (*nm == '\0') {
6559 error = 0;
6560 VN_HOLD(dvp);
6561 vp = dvp;
6562 must_trunc = TRUE;
6563 } else {
6565 * We need to go over the wire, just to be sure whether the
6566 * file exists or not. Using the DNLC can be dangerous in
6567 * this case when making a decision regarding existence.
6569 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6572 if (exclusive)
6573 createmode = EXCLUSIVE4;
6574 else
6575 createmode = GUARDED4;
6578 * error would be set if the file does not exist on the
6579 * server, so lets go create it.
6581 if (error) {
6582 goto create_otw;
6586 * File does exist on the server
6588 if (exclusive == EXCL)
6589 error = EEXIST;
6590 else if (vp->v_type == VDIR && (mode & VWRITE))
6591 error = EISDIR;
6592 else {
6594 * If vnode is a device, create special vnode.
6596 if (ISVDEV(vp->v_type)) {
6597 tempvp = vp;
6598 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6599 VN_RELE(tempvp);
6601 if (!(error = fop_access(vp, mode, 0, cr, ct))) {
6602 if ((vattr.va_mask & AT_SIZE) &&
6603 vp->v_type == VREG) {
6604 rp = VTOR4(vp);
6606 * Check here for large file handled
6607 * by LF-unaware process (as
6608 * ufs_create() does)
6610 if (!(flags & FOFFMAX)) {
6611 mutex_enter(&rp->r_statelock);
6612 if (rp->r_size > MAXOFF32_T)
6613 error = EOVERFLOW;
6614 mutex_exit(&rp->r_statelock);
6617 /* if error is set then we need to return */
6618 if (error) {
6619 nfs_rw_exit(&drp->r_rwlock);
6620 VN_RELE(vp);
6621 return (error);
6624 if (must_trunc) {
6625 vattr.va_mask = AT_SIZE;
6626 error = nfs4setattr(vp, &vattr, 0, cr,
6627 NULL);
6628 } else {
6630 * we know we have a regular file that already
6631 * exists and we may end up truncating the file
6632 * as a result of the open_otw, so flush out
6633 * any dirty pages for this file first.
6635 if (nfs4_has_pages(vp) &&
6636 ((rp->r_flags & R4DIRTY) ||
6637 rp->r_count > 0 ||
6638 rp->r_mapcnt > 0)) {
6639 error = nfs4_putpage(vp,
6640 (offset_t)0, 0, 0, cr, ct);
6641 if (error && (error == ENOSPC ||
6642 error == EDQUOT)) {
6643 mutex_enter(
6644 &rp->r_statelock);
6645 if (!rp->r_error)
6646 rp->r_error =
6647 error;
6648 mutex_exit(
6649 &rp->r_statelock);
6652 vattr.va_mask = (AT_SIZE |
6653 AT_TYPE | AT_MODE);
6654 vattr.va_type = VREG;
6655 createmode = UNCHECKED4;
6656 truncating = 1;
6657 goto create_otw;
6662 nfs_rw_exit(&drp->r_rwlock);
6663 if (error) {
6664 VN_RELE(vp);
6665 } else {
6666 vnode_t *tvp;
6667 rnode4_t *trp;
6668 tvp = vp;
6669 if (vp->v_type == VREG) {
6670 trp = VTOR4(vp);
6671 if (IS_SHADOW(vp, trp))
6672 tvp = RTOV4(trp);
6675 if (must_trunc) {
6677 * existing file got truncated, notify.
6679 vnevent_create(tvp, ct);
6682 *vpp = vp;
6684 return (error);
6686 create_otw:
6687 dnlc_remove(dvp, nm);
6689 ASSERT(vattr.va_mask & AT_TYPE);
6692 * If not a regular file let nfs4mknod() handle it.
6694 if (vattr.va_type != VREG) {
6695 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6696 nfs_rw_exit(&drp->r_rwlock);
6697 return (error);
6701 * It _is_ a regular file.
6703 ASSERT(vattr.va_mask & AT_MODE);
6704 if (MANDMODE(vattr.va_mode)) {
6705 nfs_rw_exit(&drp->r_rwlock);
6706 return (EACCES);
6710 * If this happens to be a mknod of a regular file, then flags will
6711 * have neither FREAD or FWRITE. However, we must set at least one
6712 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6713 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6714 * set (based on openmode specified by app).
6716 if ((flags & (FREAD|FWRITE)) == 0)
6717 flags |= (FREAD|FWRITE);
6719 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6721 if (vp != NULL) {
6722 /* if create was successful, throw away the file's pages */
6723 if (!error && (vattr.va_mask & AT_SIZE))
6724 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6725 cr);
6726 /* release the lookup hold */
6727 VN_RELE(vp);
6728 vp = NULL;
6732 * validate that we opened a regular file. This handles a misbehaving
6733 * server that returns an incorrect FH.
6735 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6736 error = EISDIR;
6737 VN_RELE(*vpp);
6741 * If this is not an exclusive create, then the CREATE
6742 * request will be made with the GUARDED mode set. This
6743 * means that the server will return EEXIST if the file
6744 * exists. The file could exist because of a retransmitted
6745 * request. In this case, we recover by starting over and
6746 * checking to see whether the file exists. This second
6747 * time through it should and a CREATE request will not be
6748 * sent.
6750 * This handles the problem of a dangling CREATE request
6751 * which contains attributes which indicate that the file
6752 * should be truncated. This retransmitted request could
6753 * possibly truncate valid data in the file if not caught
6754 * by the duplicate request mechanism on the server or if
6755 * not caught by other means. The scenario is:
6757 * Client transmits CREATE request with size = 0
6758 * Client times out, retransmits request.
6759 * Response to the first request arrives from the server
6760 * and the client proceeds on.
6761 * Client writes data to the file.
6762 * The server now processes retransmitted CREATE request
6763 * and truncates file.
6765 * The use of the GUARDED CREATE request prevents this from
6766 * happening because the retransmitted CREATE would fail
6767 * with EEXIST and would not truncate the file.
6769 if (error == EEXIST && exclusive == NONEXCL) {
6770 #ifdef DEBUG
6771 nfs4_create_misses++;
6772 #endif
6773 goto top;
6775 nfs_rw_exit(&drp->r_rwlock);
6776 if (truncating && !error && *vpp) {
6777 vnode_t *tvp;
6778 rnode4_t *trp;
6780 * existing file got truncated, notify.
6782 tvp = *vpp;
6783 trp = VTOR4(tvp);
6784 if (IS_SHADOW(tvp, trp))
6785 tvp = RTOV4(trp);
6786 vnevent_create(tvp, ct);
6788 return (error);
6792 * Create compound (for mkdir, mknod, symlink):
6793 * { Putfh <dfh>; Create; Getfh; Getattr }
6794 * It's okay if setattr failed to set gid - this is not considered
6795 * an error, but purge attrs in that case.
6797 static int
6798 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6799 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6801 int need_end_op = FALSE;
6802 COMPOUND4args_clnt args;
6803 COMPOUND4res_clnt res, *resp = NULL;
6804 nfs_argop4 *argop;
6805 nfs_resop4 *resop;
6806 int doqueue;
6807 mntinfo4_t *mi;
6808 rnode4_t *drp = VTOR4(dvp);
6809 change_info4 *cinfo;
6810 GETFH4res *gf_res;
6811 struct vattr vattr;
6812 vnode_t *vp;
6813 fattr4 *crattr;
6814 bool_t needrecov = FALSE;
6815 nfs4_recov_state_t recov_state;
6816 nfs4_sharedfh_t *sfhp = NULL;
6817 hrtime_t t;
6818 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6819 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6820 dirattr_info_t dinfo, *dinfop;
6821 servinfo4_t *svp;
6822 bitmap4 supp_attrs;
6824 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6825 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6827 mi = VTOMI4(dvp);
6830 * Make sure we properly deal with setting the right gid
6831 * on a new directory to reflect the parent's setgid bit
6833 setgid_flag = 0;
6834 if (type == NF4DIR) {
6835 struct vattr dva;
6837 va->va_mode &= ~VSGID;
6838 dva.va_mask = AT_MODE | AT_GID;
6839 if (fop_getattr(dvp, &dva, 0, cr, NULL) == 0) {
6842 * If the parent's directory has the setgid bit set
6843 * _and_ the client was able to get a valid mapping
6844 * for the parent dir's owner_group, we want to
6845 * append NVERIFY(owner_group == dva.va_gid) and
6846 * SETTATTR to the CREATE compound.
6848 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6849 setgid_flag = 1;
6850 va->va_mode |= VSGID;
6851 if (dva.va_gid != GID_NOBODY) {
6852 va->va_mask |= AT_GID;
6853 va->va_gid = dva.va_gid;
6860 * Create ops:
6861 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6862 * 5:restorefh(dir) 6:getattr(dir)
6864 * if (setgid)
6865 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6866 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6867 * 8:nverify 9:setattr
6869 if (setgid_flag) {
6870 numops = 10;
6871 idx_create = 1;
6872 idx_fattr = 3;
6873 } else {
6874 numops = 7;
6875 idx_create = 2;
6876 idx_fattr = 4;
6879 ASSERT(nfs_zone() == mi->mi_zone);
6880 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6881 return (EINTR);
6883 recov_state.rs_flags = 0;
6884 recov_state.rs_num_retry_despite_err = 0;
6886 argoplist_size = numops * sizeof (nfs_argop4);
6887 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6889 recov_retry:
6890 if (type == NF4LNK)
6891 args.ctag = TAG_SYMLINK;
6892 else if (type == NF4DIR)
6893 args.ctag = TAG_MKDIR;
6894 else
6895 args.ctag = TAG_MKNOD;
6897 args.array_len = numops;
6898 args.array = argop;
6900 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6901 nfs_rw_exit(&drp->r_rwlock);
6902 kmem_free(argop, argoplist_size);
6903 return (e.error);
6905 need_end_op = TRUE;
6908 /* 0: putfh directory */
6909 argop[0].argop = OP_CPUTFH;
6910 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6912 /* 1/2: Create object */
6913 argop[idx_create].argop = OP_CCREATE;
6914 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6915 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6916 if (type == NF4LNK) {
6918 * symlink, treat name as data
6920 ASSERT(data != NULL);
6921 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6922 (char *)data;
6924 if (type == NF4BLK || type == NF4CHR) {
6925 ASSERT(data != NULL);
6926 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6927 *((specdata4 *)data);
6930 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6932 svp = drp->r_server;
6933 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6934 supp_attrs = svp->sv_supp_attrs;
6935 nfs_rw_exit(&svp->sv_lock);
6937 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6938 nfs_rw_exit(&drp->r_rwlock);
6939 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6940 e.error = EINVAL;
6941 kmem_free(argop, argoplist_size);
6942 return (e.error);
6945 /* 2/3: getfh fh of created object */
6946 ASSERT(idx_create + 1 == idx_fattr - 1);
6947 argop[idx_create + 1].argop = OP_GETFH;
6949 /* 3/4: getattr of new object */
6950 argop[idx_fattr].argop = OP_GETATTR;
6951 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6952 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6954 if (setgid_flag) {
6955 vattr_t _v;
6957 argop[4].argop = OP_SAVEFH;
6959 argop[5].argop = OP_CPUTFH;
6960 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6962 argop[6].argop = OP_GETATTR;
6963 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6964 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6966 argop[7].argop = OP_RESTOREFH;
6969 * nverify
6971 * XXX - Revisit the last argument to nfs4_end_op()
6972 * once 5020486 is fixed.
6974 _v.va_mask = AT_GID;
6975 _v.va_gid = va->va_gid;
6976 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6977 supp_attrs)) {
6978 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6979 nfs_rw_exit(&drp->r_rwlock);
6980 nfs4_fattr4_free(crattr);
6981 kmem_free(argop, argoplist_size);
6982 return (e.error);
6986 * setattr
6988 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6989 * so no need for stateid or flags. Also we specify NULL
6990 * rp since we're only interested in setting owner_group
6991 * attributes.
6993 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
6994 &e.error, 0);
6996 if (e.error) {
6997 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6998 nfs_rw_exit(&drp->r_rwlock);
6999 nfs4_fattr4_free(crattr);
7000 nfs4args_verify_free(&argop[8]);
7001 kmem_free(argop, argoplist_size);
7002 return (e.error);
7004 } else {
7005 argop[1].argop = OP_SAVEFH;
7007 argop[5].argop = OP_RESTOREFH;
7009 argop[6].argop = OP_GETATTR;
7010 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7011 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7014 dnlc_remove(dvp, nm);
7016 doqueue = 1;
7017 t = gethrtime();
7018 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7020 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7021 if (e.error) {
7022 PURGE_ATTRCACHE4(dvp);
7023 if (!needrecov)
7024 goto out;
7027 if (needrecov) {
7028 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7029 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7030 nfs4_end_op(mi, dvp, NULL, &recov_state,
7031 needrecov);
7032 need_end_op = FALSE;
7033 nfs4_fattr4_free(crattr);
7034 if (setgid_flag) {
7035 nfs4args_verify_free(&argop[8]);
7036 nfs4args_setattr_free(&argop[9]);
7038 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7039 goto recov_retry;
7043 resp = &res;
7045 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7047 if (res.status == NFS4ERR_BADOWNER)
7048 nfs4_log_badowner(mi, OP_CREATE);
7050 e.error = geterrno4(res.status);
7053 * This check is left over from when create was implemented
7054 * using a setattr op (instead of createattrs). If the
7055 * putfh/create/getfh failed, the error was returned. If
7056 * setattr/getattr failed, we keep going.
7058 * It might be better to get rid of the GETFH also, and just
7059 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7060 * Then if any of the operations failed, we could return the
7061 * error now, and remove much of the error code below.
7063 if (res.array_len <= idx_fattr) {
7065 * Either Putfh, Create or Getfh failed.
7067 PURGE_ATTRCACHE4(dvp);
7069 * nfs4_purge_stale_fh() may generate otw calls through
7070 * nfs4_invalidate_pages. Hence the need to call
7071 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7073 nfs4_end_op(mi, dvp, NULL, &recov_state,
7074 needrecov);
7075 need_end_op = FALSE;
7076 nfs4_purge_stale_fh(e.error, dvp, cr);
7077 goto out;
7081 resop = &res.array[idx_create]; /* create res */
7082 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7084 resop = &res.array[idx_create + 1]; /* getfh res */
7085 gf_res = &resop->nfs_resop4_u.opgetfh;
7087 sfhp = sfh4_get(&gf_res->object, mi);
7088 if (e.error) {
7089 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7090 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7091 if (vp->v_type == VNON) {
7092 vattr.va_mask = AT_TYPE;
7094 * Need to call nfs4_end_op before nfs4getattr to avoid
7095 * potential nfs4_start_op deadlock. See RFE 4777612.
7097 nfs4_end_op(mi, dvp, NULL, &recov_state,
7098 needrecov);
7099 need_end_op = FALSE;
7100 e.error = nfs4getattr(vp, &vattr, cr);
7101 if (e.error) {
7102 VN_RELE(vp);
7103 *vpp = NULL;
7104 goto out;
7106 vp->v_type = vattr.va_type;
7108 e.error = 0;
7109 } else {
7110 *vpp = vp = makenfs4node(sfhp,
7111 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7112 dvp->v_vfsp, t, cr,
7113 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7117 * If compound succeeded, then update dir attrs
7119 if (res.status == NFS4_OK) {
7120 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7121 dinfo.di_cred = cr;
7122 dinfo.di_time_call = t;
7123 dinfop = &dinfo;
7124 } else
7125 dinfop = NULL;
7127 /* Update directory cache attribute, readdir and dnlc caches */
7128 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7130 out:
7131 if (sfhp != NULL)
7132 sfh4_rele(&sfhp);
7133 nfs_rw_exit(&drp->r_rwlock);
7134 nfs4_fattr4_free(crattr);
7135 if (setgid_flag) {
7136 nfs4args_verify_free(&argop[8]);
7137 nfs4args_setattr_free(&argop[9]);
7139 if (resp)
7140 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7141 if (need_end_op)
7142 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7144 kmem_free(argop, argoplist_size);
7145 return (e.error);
7148 /* ARGSUSED */
7149 static int
7150 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7151 int mode, vnode_t **vpp, cred_t *cr)
7153 int error;
7154 vnode_t *vp;
7155 nfs_ftype4 type;
7156 specdata4 spec, *specp = NULL;
7158 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7160 switch (va->va_type) {
7161 case VCHR:
7162 case VBLK:
7163 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7164 spec.specdata1 = getmajor(va->va_rdev);
7165 spec.specdata2 = getminor(va->va_rdev);
7166 specp = &spec;
7167 break;
7169 case VFIFO:
7170 type = NF4FIFO;
7171 break;
7172 case VSOCK:
7173 type = NF4SOCK;
7174 break;
7176 default:
7177 return (EINVAL);
7180 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7181 if (error) {
7182 return (error);
7186 * This might not be needed any more; special case to deal
7187 * with problematic v2/v3 servers. Since create was unable
7188 * to set group correctly, not sure what hope setattr has.
7190 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7191 va->va_mask = AT_GID;
7192 (void) nfs4setattr(vp, va, 0, cr, NULL);
7196 * If vnode is a device create special vnode
7198 if (ISVDEV(vp->v_type)) {
7199 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7200 VN_RELE(vp);
7201 } else {
7202 *vpp = vp;
7204 return (error);
7208 * Remove requires that the current fh be the target directory.
7209 * After the operation, the current fh is unchanged.
7210 * The compound op structure is:
7211 * PUTFH(targetdir), REMOVE
7213 * Weirdness: if the vnode to be removed is open
7214 * we rename it instead of removing it and nfs_inactive
7215 * will remove the new name.
7217 /* ARGSUSED */
7218 static int
7219 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7221 COMPOUND4args_clnt args;
7222 COMPOUND4res_clnt res, *resp = NULL;
7223 REMOVE4res *rm_res;
7224 nfs_argop4 argop[3];
7225 nfs_resop4 *resop;
7226 vnode_t *vp;
7227 char *tmpname;
7228 int doqueue;
7229 mntinfo4_t *mi;
7230 rnode4_t *rp;
7231 rnode4_t *drp;
7232 int needrecov = 0;
7233 nfs4_recov_state_t recov_state;
7234 int isopen;
7235 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7236 dirattr_info_t dinfo;
7238 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7239 return (EPERM);
7240 drp = VTOR4(dvp);
7241 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7242 return (EINTR);
7244 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7245 if (e.error) {
7246 nfs_rw_exit(&drp->r_rwlock);
7247 return (e.error);
7250 if (vp->v_type == VDIR) {
7251 VN_RELE(vp);
7252 nfs_rw_exit(&drp->r_rwlock);
7253 return (EISDIR);
7257 * First just remove the entry from the name cache, as it
7258 * is most likely the only entry for this vp.
7260 dnlc_remove(dvp, nm);
7262 rp = VTOR4(vp);
7265 * For regular file types, check to see if the file is open by looking
7266 * at the open streams.
7267 * For all other types, check the reference count on the vnode. Since
7268 * they are not opened OTW they never have an open stream.
7270 * If the file is open, rename it to .nfsXXXX.
7272 if (vp->v_type != VREG) {
7274 * If the file has a v_count > 1 then there may be more than one
7275 * entry in the name cache due multiple links or an open file,
7276 * but we don't have the real reference count so flush all
7277 * possible entries.
7279 if (vp->v_count > 1)
7280 dnlc_purge_vp(vp);
7283 * Now we have the real reference count.
7285 isopen = vp->v_count > 1;
7286 } else {
7287 mutex_enter(&rp->r_os_lock);
7288 isopen = list_head(&rp->r_open_streams) != NULL;
7289 mutex_exit(&rp->r_os_lock);
7292 mutex_enter(&rp->r_statelock);
7293 if (isopen &&
7294 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7295 mutex_exit(&rp->r_statelock);
7296 tmpname = newname();
7297 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7298 if (e.error)
7299 kmem_free(tmpname, MAXNAMELEN);
7300 else {
7301 mutex_enter(&rp->r_statelock);
7302 if (rp->r_unldvp == NULL) {
7303 VN_HOLD(dvp);
7304 rp->r_unldvp = dvp;
7305 if (rp->r_unlcred != NULL)
7306 crfree(rp->r_unlcred);
7307 crhold(cr);
7308 rp->r_unlcred = cr;
7309 rp->r_unlname = tmpname;
7310 } else {
7311 kmem_free(rp->r_unlname, MAXNAMELEN);
7312 rp->r_unlname = tmpname;
7314 mutex_exit(&rp->r_statelock);
7316 VN_RELE(vp);
7317 nfs_rw_exit(&drp->r_rwlock);
7318 return (e.error);
7321 * Actually remove the file/dir
7323 mutex_exit(&rp->r_statelock);
7326 * We need to flush any dirty pages which happen to
7327 * be hanging around before removing the file.
7328 * This shouldn't happen very often since in NFSv4
7329 * we should be close to open consistent.
7331 if (nfs4_has_pages(vp) &&
7332 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7333 e.error = nfs4_putpage(vp, 0, 0, 0, cr, ct);
7334 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7335 mutex_enter(&rp->r_statelock);
7336 if (!rp->r_error)
7337 rp->r_error = e.error;
7338 mutex_exit(&rp->r_statelock);
7342 mi = VTOMI4(dvp);
7344 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7345 recov_state.rs_flags = 0;
7346 recov_state.rs_num_retry_despite_err = 0;
7348 recov_retry:
7350 * Remove ops: putfh dir; remove
7352 args.ctag = TAG_REMOVE;
7353 args.array_len = 3;
7354 args.array = argop;
7356 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7357 if (e.error) {
7358 nfs_rw_exit(&drp->r_rwlock);
7359 VN_RELE(vp);
7360 return (e.error);
7363 /* putfh directory */
7364 argop[0].argop = OP_CPUTFH;
7365 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7367 /* remove */
7368 argop[1].argop = OP_CREMOVE;
7369 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7371 /* getattr dir */
7372 argop[2].argop = OP_GETATTR;
7373 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7374 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7376 doqueue = 1;
7377 dinfo.di_time_call = gethrtime();
7378 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7380 PURGE_ATTRCACHE4(vp);
7382 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7383 if (e.error)
7384 PURGE_ATTRCACHE4(dvp);
7386 if (needrecov) {
7387 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7388 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7389 if (!e.error)
7390 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7391 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7392 needrecov);
7393 goto recov_retry;
7398 * Matching nfs4_end_op() for start_op() above.
7399 * There is a path in the code below which calls
7400 * nfs4_purge_stale_fh(), which may generate otw calls through
7401 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7402 * here to avoid nfs4_start_op() deadlock.
7404 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7406 if (!e.error) {
7407 resp = &res;
7409 if (res.status) {
7410 e.error = geterrno4(res.status);
7411 PURGE_ATTRCACHE4(dvp);
7412 nfs4_purge_stale_fh(e.error, dvp, cr);
7413 } else {
7414 resop = &res.array[1]; /* remove res */
7415 rm_res = &resop->nfs_resop4_u.opremove;
7417 dinfo.di_garp =
7418 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7419 dinfo.di_cred = cr;
7421 /* Update directory attr, readdir and dnlc caches */
7422 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7423 &dinfo);
7426 nfs_rw_exit(&drp->r_rwlock);
7427 if (resp)
7428 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7430 if (e.error == 0) {
7431 vnode_t *tvp;
7432 rnode4_t *trp;
7433 trp = VTOR4(vp);
7434 tvp = vp;
7435 if (IS_SHADOW(vp, trp))
7436 tvp = RTOV4(trp);
7437 vnevent_remove(tvp, dvp, nm, ct);
7439 VN_RELE(vp);
7440 return (e.error);
7444 * Link requires that the current fh be the target directory and the
7445 * saved fh be the source fh. After the operation, the current fh is unchanged.
7446 * Thus the compound op structure is:
7447 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7448 * GETATTR(file)
7450 /* ARGSUSED */
7451 static int
7452 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7453 caller_context_t *ct, int flags)
7455 COMPOUND4args_clnt args;
7456 COMPOUND4res_clnt res, *resp = NULL;
7457 LINK4res *ln_res;
7458 int argoplist_size = 7 * sizeof (nfs_argop4);
7459 nfs_argop4 *argop;
7460 nfs_resop4 *resop;
7461 vnode_t *realvp, *nvp;
7462 int doqueue;
7463 mntinfo4_t *mi;
7464 rnode4_t *tdrp;
7465 bool_t needrecov = FALSE;
7466 nfs4_recov_state_t recov_state;
7467 hrtime_t t;
7468 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7469 dirattr_info_t dinfo;
7471 ASSERT(*tnm != '\0');
7472 ASSERT(tdvp->v_type == VDIR);
7473 ASSERT(nfs4_consistent_type(tdvp));
7474 ASSERT(nfs4_consistent_type(svp));
7476 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7477 return (EPERM);
7478 if (fop_realvp(svp, &realvp, ct) == 0) {
7479 svp = realvp;
7480 ASSERT(nfs4_consistent_type(svp));
7483 tdrp = VTOR4(tdvp);
7484 mi = VTOMI4(svp);
7486 if (!(mi->mi_flags & MI4_LINK)) {
7487 return (EOPNOTSUPP);
7489 recov_state.rs_flags = 0;
7490 recov_state.rs_num_retry_despite_err = 0;
7492 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7493 return (EINTR);
7495 recov_retry:
7496 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7498 args.ctag = TAG_LINK;
7501 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7502 * restorefh; getattr(fl)
7504 args.array_len = 7;
7505 args.array = argop;
7507 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7508 if (e.error) {
7509 kmem_free(argop, argoplist_size);
7510 nfs_rw_exit(&tdrp->r_rwlock);
7511 return (e.error);
7514 /* 0. putfh file */
7515 argop[0].argop = OP_CPUTFH;
7516 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7518 /* 1. save current fh to free up the space for the dir */
7519 argop[1].argop = OP_SAVEFH;
7521 /* 2. putfh targetdir */
7522 argop[2].argop = OP_CPUTFH;
7523 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7525 /* 3. link: current_fh is targetdir, saved_fh is source */
7526 argop[3].argop = OP_CLINK;
7527 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7529 /* 4. Get attributes of dir */
7530 argop[4].argop = OP_GETATTR;
7531 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7532 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7534 /* 5. If link was successful, restore current vp to file */
7535 argop[5].argop = OP_RESTOREFH;
7537 /* 6. Get attributes of linked object */
7538 argop[6].argop = OP_GETATTR;
7539 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7540 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7542 dnlc_remove(tdvp, tnm);
7544 doqueue = 1;
7545 t = gethrtime();
7547 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7549 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7550 if (e.error != 0 && !needrecov) {
7551 PURGE_ATTRCACHE4(tdvp);
7552 PURGE_ATTRCACHE4(svp);
7553 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7554 goto out;
7557 if (needrecov) {
7558 bool_t abort;
7560 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7561 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7562 if (abort == FALSE) {
7563 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7564 needrecov);
7565 kmem_free(argop, argoplist_size);
7566 if (!e.error)
7567 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7568 goto recov_retry;
7569 } else {
7570 if (e.error != 0) {
7571 PURGE_ATTRCACHE4(tdvp);
7572 PURGE_ATTRCACHE4(svp);
7573 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7574 &recov_state, needrecov);
7575 goto out;
7577 /* fall through for res.status case */
7581 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7583 resp = &res;
7584 if (res.status) {
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 /* XXX-LP */
7599 if (e.error == EISDIR && crgetuid(cr) != 0)
7600 e.error = EPERM;
7601 goto out;
7605 /* either no error or one of the postop getattr failed */
7608 * XXX - if LINK succeeded, but no attrs were returned for link
7609 * file, purge its cache.
7611 * XXX Perform a simplified version of wcc checking. Instead of
7612 * have another getattr to get pre-op, just purge cache if
7613 * any of the ops prior to and including the getattr failed.
7614 * If the getattr succeeded then update the attrcache accordingly.
7618 * update cache with link file postattrs.
7619 * Note: at this point resop points to link res.
7621 resop = &res.array[3]; /* link res */
7622 ln_res = &resop->nfs_resop4_u.oplink;
7623 if (res.status == NFS4_OK)
7624 e.error = nfs4_update_attrcache(res.status,
7625 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7626 t, svp, cr);
7629 * Call makenfs4node to create the new shadow vp for tnm.
7630 * We pass NULL attrs because we just cached attrs for
7631 * the src object. All we're trying to accomplish is to
7632 * to create the new shadow vnode.
7634 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7635 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7637 /* Update target cache attribute, readdir and dnlc caches */
7638 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7639 dinfo.di_time_call = t;
7640 dinfo.di_cred = cr;
7642 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7643 ASSERT(nfs4_consistent_type(tdvp));
7644 ASSERT(nfs4_consistent_type(svp));
7645 ASSERT(nfs4_consistent_type(nvp));
7646 VN_RELE(nvp);
7648 if (!e.error) {
7649 vnode_t *tvp;
7650 rnode4_t *trp;
7652 * Notify the source file of this link operation.
7654 trp = VTOR4(svp);
7655 tvp = svp;
7656 if (IS_SHADOW(svp, trp))
7657 tvp = RTOV4(trp);
7658 vnevent_link(tvp, ct);
7660 out:
7661 kmem_free(argop, argoplist_size);
7662 if (resp)
7663 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7665 nfs_rw_exit(&tdrp->r_rwlock);
7667 return (e.error);
7670 /* ARGSUSED */
7671 static int
7672 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7673 caller_context_t *ct, int flags)
7675 vnode_t *realvp;
7677 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7678 return (EPERM);
7679 if (fop_realvp(ndvp, &realvp, ct) == 0)
7680 ndvp = realvp;
7682 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7686 * nfs4rename does the real work of renaming in NFS Version 4.
7688 * A file handle is considered volatile for renaming purposes if either
7689 * of the volatile bits are turned on. However, the compound may differ
7690 * based on the likelihood of the filehandle to change during rename.
7692 static int
7693 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7694 caller_context_t *ct)
7696 int error;
7697 mntinfo4_t *mi;
7698 vnode_t *nvp = NULL;
7699 vnode_t *ovp = NULL;
7700 char *tmpname = NULL;
7701 rnode4_t *rp;
7702 rnode4_t *odrp;
7703 rnode4_t *ndrp;
7704 int did_link = 0;
7705 int do_link = 1;
7706 nfsstat4 stat = NFS4_OK;
7708 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7709 ASSERT(nfs4_consistent_type(odvp));
7710 ASSERT(nfs4_consistent_type(ndvp));
7712 if (onm[0] == '.' && (onm[1] == '\0' ||
7713 (onm[1] == '.' && onm[2] == '\0')))
7714 return (EINVAL);
7716 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7717 (nnm[1] == '.' && nnm[2] == '\0')))
7718 return (EINVAL);
7720 odrp = VTOR4(odvp);
7721 ndrp = VTOR4(ndvp);
7722 if ((intptr_t)odrp < (intptr_t)ndrp) {
7723 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7724 return (EINTR);
7725 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7726 nfs_rw_exit(&odrp->r_rwlock);
7727 return (EINTR);
7729 } else {
7730 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7731 return (EINTR);
7732 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7733 nfs_rw_exit(&ndrp->r_rwlock);
7734 return (EINTR);
7739 * Lookup the target file. If it exists, it needs to be
7740 * checked to see whether it is a mount point and whether
7741 * it is active (open).
7743 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7744 if (!error) {
7745 int isactive;
7747 ASSERT(nfs4_consistent_type(nvp));
7749 * If this file has been mounted on, then just
7750 * return busy because renaming to it would remove
7751 * the mounted file system from the name space.
7753 if (vn_ismntpt(nvp)) {
7754 VN_RELE(nvp);
7755 nfs_rw_exit(&odrp->r_rwlock);
7756 nfs_rw_exit(&ndrp->r_rwlock);
7757 return (EBUSY);
7761 * First just remove the entry from the name cache, as it
7762 * is most likely the only entry for this vp.
7764 dnlc_remove(ndvp, nnm);
7766 rp = VTOR4(nvp);
7768 if (nvp->v_type != VREG) {
7770 * Purge the name cache of all references to this vnode
7771 * so that we can check the reference count to infer
7772 * whether it is active or not.
7774 if (nvp->v_count > 1)
7775 dnlc_purge_vp(nvp);
7777 isactive = nvp->v_count > 1;
7778 } else {
7779 mutex_enter(&rp->r_os_lock);
7780 isactive = list_head(&rp->r_open_streams) != NULL;
7781 mutex_exit(&rp->r_os_lock);
7785 * If the vnode is active and is not a directory,
7786 * arrange to rename it to a
7787 * temporary file so that it will continue to be
7788 * accessible. This implements the "unlink-open-file"
7789 * semantics for the target of a rename operation.
7790 * Before doing this though, make sure that the
7791 * source and target files are not already the same.
7793 if (isactive && nvp->v_type != VDIR) {
7795 * Lookup the source name.
7797 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7800 * The source name *should* already exist.
7802 if (error) {
7803 VN_RELE(nvp);
7804 nfs_rw_exit(&odrp->r_rwlock);
7805 nfs_rw_exit(&ndrp->r_rwlock);
7806 return (error);
7809 ASSERT(nfs4_consistent_type(ovp));
7812 * Compare the two vnodes. If they are the same,
7813 * just release all held vnodes and return success.
7815 if (VN_CMP(ovp, nvp)) {
7816 VN_RELE(ovp);
7817 VN_RELE(nvp);
7818 nfs_rw_exit(&odrp->r_rwlock);
7819 nfs_rw_exit(&ndrp->r_rwlock);
7820 return (0);
7824 * Can't mix and match directories and non-
7825 * directories in rename operations. We already
7826 * know that the target is not a directory. If
7827 * the source is a directory, return an error.
7829 if (ovp->v_type == VDIR) {
7830 VN_RELE(ovp);
7831 VN_RELE(nvp);
7832 nfs_rw_exit(&odrp->r_rwlock);
7833 nfs_rw_exit(&ndrp->r_rwlock);
7834 return (ENOTDIR);
7836 link_call:
7838 * The target file exists, is not the same as
7839 * the source file, and is active. We first
7840 * try to Link it to a temporary filename to
7841 * avoid having the server removing the file
7842 * completely (which could cause data loss to
7843 * the user's POV in the event the Rename fails
7844 * -- see bug 1165874).
7847 * The do_link and did_link booleans are
7848 * introduced in the event we get NFS4ERR_FILE_OPEN
7849 * returned for the Rename. Some servers can
7850 * not Rename over an Open file, so they return
7851 * this error. The client needs to Remove the
7852 * newly created Link and do two Renames, just
7853 * as if the server didn't support LINK.
7855 tmpname = newname();
7856 error = 0;
7858 if (do_link) {
7859 error = nfs4_link(ndvp, nvp, tmpname, cr,
7860 NULL, 0);
7862 if (error == EOPNOTSUPP || !do_link) {
7863 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7864 cr, NULL, 0);
7865 did_link = 0;
7866 } else {
7867 did_link = 1;
7869 if (error) {
7870 kmem_free(tmpname, MAXNAMELEN);
7871 VN_RELE(ovp);
7872 VN_RELE(nvp);
7873 nfs_rw_exit(&odrp->r_rwlock);
7874 nfs_rw_exit(&ndrp->r_rwlock);
7875 return (error);
7878 mutex_enter(&rp->r_statelock);
7879 if (rp->r_unldvp == NULL) {
7880 VN_HOLD(ndvp);
7881 rp->r_unldvp = ndvp;
7882 if (rp->r_unlcred != NULL)
7883 crfree(rp->r_unlcred);
7884 crhold(cr);
7885 rp->r_unlcred = cr;
7886 rp->r_unlname = tmpname;
7887 } else {
7888 if (rp->r_unlname)
7889 kmem_free(rp->r_unlname, MAXNAMELEN);
7890 rp->r_unlname = tmpname;
7892 mutex_exit(&rp->r_statelock);
7895 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7897 ASSERT(nfs4_consistent_type(nvp));
7900 if (ovp == NULL) {
7902 * When renaming directories to be a subdirectory of a
7903 * different parent, the dnlc entry for ".." will no
7904 * longer be valid, so it must be removed.
7906 * We do a lookup here to determine whether we are renaming
7907 * a directory and we need to check if we are renaming
7908 * an unlinked file. This might have already been done
7909 * in previous code, so we check ovp == NULL to avoid
7910 * doing it twice.
7912 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7914 * The source name *should* already exist.
7916 if (error) {
7917 nfs_rw_exit(&odrp->r_rwlock);
7918 nfs_rw_exit(&ndrp->r_rwlock);
7919 if (nvp) {
7920 VN_RELE(nvp);
7922 return (error);
7924 ASSERT(ovp != NULL);
7925 ASSERT(nfs4_consistent_type(ovp));
7929 * Is the object being renamed a dir, and if so, is
7930 * it being renamed to a child of itself? The underlying
7931 * fs should ultimately return EINVAL for this case;
7932 * however, buggy beta non-Solaris NFSv4 servers at
7933 * interop testing events have allowed this behavior,
7934 * and it caused our client to panic due to a recursive
7935 * mutex_enter in fn_move.
7937 * The tedious locking in fn_move could be changed to
7938 * deal with this case, and the client could avoid the
7939 * panic; however, the client would just confuse itself
7940 * later and misbehave. A better way to handle the broken
7941 * server is to detect this condition and return EINVAL
7942 * without ever sending the the bogus rename to the server.
7943 * We know the rename is invalid -- just fail it now.
7945 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7946 VN_RELE(ovp);
7947 nfs_rw_exit(&odrp->r_rwlock);
7948 nfs_rw_exit(&ndrp->r_rwlock);
7949 if (nvp) {
7950 VN_RELE(nvp);
7952 return (EINVAL);
7955 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7958 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7959 * possible for the filehandle to change due to the rename.
7960 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7961 * the fh will not change because of the rename, but we still need
7962 * to update its rnode entry with the new name for
7963 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7964 * has no effect on these for now, but for future improvements,
7965 * we might want to use it too to simplify handling of files
7966 * that are open with that flag on. (XXX)
7968 mi = VTOMI4(odvp);
7969 if (NFS4_VOLATILE_FH(mi))
7970 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7971 &stat);
7972 else
7973 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7974 &stat);
7976 ASSERT(nfs4_consistent_type(odvp));
7977 ASSERT(nfs4_consistent_type(ndvp));
7978 ASSERT(nfs4_consistent_type(ovp));
7980 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7981 do_link = 0;
7983 * Before the 'link_call' code, we did a nfs4_lookup
7984 * that puts a VN_HOLD on nvp. After the nfs4_link
7985 * call we call VN_RELE to match that hold. We need
7986 * to place an additional VN_HOLD here since we will
7987 * be hitting that VN_RELE again.
7989 VN_HOLD(nvp);
7991 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
7993 /* Undo the unlinked file naming stuff we just did */
7994 mutex_enter(&rp->r_statelock);
7995 if (rp->r_unldvp) {
7996 VN_RELE(ndvp);
7997 rp->r_unldvp = NULL;
7998 if (rp->r_unlcred != NULL)
7999 crfree(rp->r_unlcred);
8000 rp->r_unlcred = NULL;
8001 /* rp->r_unlanme points to tmpname */
8002 if (rp->r_unlname)
8003 kmem_free(rp->r_unlname, MAXNAMELEN);
8004 rp->r_unlname = NULL;
8006 mutex_exit(&rp->r_statelock);
8008 if (nvp) {
8009 VN_RELE(nvp);
8011 goto link_call;
8014 if (error) {
8015 VN_RELE(ovp);
8016 nfs_rw_exit(&odrp->r_rwlock);
8017 nfs_rw_exit(&ndrp->r_rwlock);
8018 if (nvp) {
8019 VN_RELE(nvp);
8021 return (error);
8025 * when renaming directories to be a subdirectory of a
8026 * different parent, the dnlc entry for ".." will no
8027 * longer be valid, so it must be removed
8029 rp = VTOR4(ovp);
8030 if (ndvp != odvp) {
8031 if (ovp->v_type == VDIR) {
8032 dnlc_remove(ovp, "..");
8033 if (rp->r_dir != NULL)
8034 nfs4_purge_rddir_cache(ovp);
8039 * If we are renaming the unlinked file, update the
8040 * r_unldvp and r_unlname as needed.
8042 mutex_enter(&rp->r_statelock);
8043 if (rp->r_unldvp != NULL) {
8044 if (strcmp(rp->r_unlname, onm) == 0) {
8045 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8046 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8047 if (ndvp != rp->r_unldvp) {
8048 VN_RELE(rp->r_unldvp);
8049 rp->r_unldvp = ndvp;
8050 VN_HOLD(ndvp);
8054 mutex_exit(&rp->r_statelock);
8057 * Notify the rename vnevents to source vnode, and to the target
8058 * vnode if it already existed.
8060 if (error == 0) {
8061 vnode_t *tvp;
8062 rnode4_t *trp;
8064 * Notify the vnode. Each links is represented by
8065 * a different vnode, in nfsv4.
8067 if (nvp) {
8068 trp = VTOR4(nvp);
8069 tvp = nvp;
8070 if (IS_SHADOW(nvp, trp))
8071 tvp = RTOV4(trp);
8072 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8076 * if the source and destination directory are not the
8077 * same notify the destination directory.
8079 if (VTOR4(odvp) != VTOR4(ndvp)) {
8080 trp = VTOR4(ndvp);
8081 tvp = ndvp;
8082 if (IS_SHADOW(ndvp, trp))
8083 tvp = RTOV4(trp);
8084 vnevent_rename_dest_dir(tvp, ct);
8087 trp = VTOR4(ovp);
8088 tvp = ovp;
8089 if (IS_SHADOW(ovp, trp))
8090 tvp = RTOV4(trp);
8091 vnevent_rename_src(tvp, odvp, onm, ct);
8094 if (nvp) {
8095 VN_RELE(nvp);
8097 VN_RELE(ovp);
8099 nfs_rw_exit(&odrp->r_rwlock);
8100 nfs_rw_exit(&ndrp->r_rwlock);
8102 return (error);
8106 * When the parent directory has changed, sv_dfh must be updated
8108 static void
8109 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8111 svnode_t *sv = VTOSV(vp);
8112 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8113 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8115 sfh4_hold(new_dfh);
8116 sv->sv_dfh = new_dfh;
8117 sfh4_rele(&old_dfh);
8121 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8122 * when it is known that the filehandle is persistent through rename.
8124 * Rename requires that the current fh be the target directory and the
8125 * saved fh be the source directory. After the operation, the current fh
8126 * is unchanged.
8127 * The compound op structure for persistent fh rename is:
8128 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8129 * Rather than bother with the directory postop args, we'll simply
8130 * update that a change occurred in the cache, so no post-op getattrs.
8132 static int
8133 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8134 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8136 COMPOUND4args_clnt args;
8137 COMPOUND4res_clnt res, *resp = NULL;
8138 nfs_argop4 *argop;
8139 nfs_resop4 *resop;
8140 int doqueue, argoplist_size;
8141 mntinfo4_t *mi;
8142 rnode4_t *odrp = VTOR4(odvp);
8143 rnode4_t *ndrp = VTOR4(ndvp);
8144 RENAME4res *rn_res;
8145 bool_t needrecov;
8146 nfs4_recov_state_t recov_state;
8147 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8148 dirattr_info_t dinfo, *dinfop;
8150 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8152 recov_state.rs_flags = 0;
8153 recov_state.rs_num_retry_despite_err = 0;
8156 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8158 * If source/target are different dirs, then append putfh(src); getattr
8160 args.array_len = (odvp == ndvp) ? 5 : 7;
8161 argoplist_size = args.array_len * sizeof (nfs_argop4);
8162 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8164 recov_retry:
8165 *statp = NFS4_OK;
8167 /* No need to Lookup the file, persistent fh */
8168 args.ctag = TAG_RENAME;
8170 mi = VTOMI4(odvp);
8171 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8172 if (e.error) {
8173 kmem_free(argop, argoplist_size);
8174 return (e.error);
8177 /* 0: putfh source directory */
8178 argop[0].argop = OP_CPUTFH;
8179 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8181 /* 1: Save source fh to free up current for target */
8182 argop[1].argop = OP_SAVEFH;
8184 /* 2: putfh targetdir */
8185 argop[2].argop = OP_CPUTFH;
8186 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8188 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8189 argop[3].argop = OP_CRENAME;
8190 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8191 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8193 /* 4: getattr (targetdir) */
8194 argop[4].argop = OP_GETATTR;
8195 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8196 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8198 if (ndvp != odvp) {
8200 /* 5: putfh (sourcedir) */
8201 argop[5].argop = OP_CPUTFH;
8202 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8204 /* 6: getattr (sourcedir) */
8205 argop[6].argop = OP_GETATTR;
8206 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8207 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8210 dnlc_remove(odvp, onm);
8211 dnlc_remove(ndvp, nnm);
8213 doqueue = 1;
8214 dinfo.di_time_call = gethrtime();
8215 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8217 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8218 if (e.error) {
8219 PURGE_ATTRCACHE4(odvp);
8220 PURGE_ATTRCACHE4(ndvp);
8221 } else {
8222 *statp = res.status;
8225 if (needrecov) {
8226 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8227 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8228 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8229 if (!e.error)
8230 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8231 goto recov_retry;
8235 if (!e.error) {
8236 resp = &res;
8238 * as long as OP_RENAME
8240 if (res.status != NFS4_OK && res.array_len <= 4) {
8241 e.error = geterrno4(res.status);
8242 PURGE_ATTRCACHE4(odvp);
8243 PURGE_ATTRCACHE4(ndvp);
8245 * System V defines rename to return EEXIST, not
8246 * ENOTEMPTY if the target directory is not empty.
8247 * Over the wire, the error is NFSERR_ENOTEMPTY
8248 * which geterrno4 maps to ENOTEMPTY.
8250 if (e.error == ENOTEMPTY)
8251 e.error = EEXIST;
8252 } else {
8254 resop = &res.array[3]; /* rename res */
8255 rn_res = &resop->nfs_resop4_u.oprename;
8257 if (res.status == NFS4_OK) {
8259 * Update target attribute, readdir and dnlc
8260 * caches.
8262 dinfo.di_garp =
8263 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8264 dinfo.di_cred = cr;
8265 dinfop = &dinfo;
8266 } else
8267 dinfop = NULL;
8269 nfs4_update_dircaches(&rn_res->target_cinfo,
8270 ndvp, NULL, NULL, dinfop);
8273 * Update source attribute, readdir and dnlc caches
8276 if (ndvp != odvp) {
8277 update_parentdir_sfh(renvp, ndvp);
8279 if (dinfop)
8280 dinfo.di_garp =
8281 &(res.array[6].nfs_resop4_u.
8282 opgetattr.ga_res);
8284 nfs4_update_dircaches(&rn_res->source_cinfo,
8285 odvp, NULL, NULL, dinfop);
8288 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8289 nnm);
8293 if (resp)
8294 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8295 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8296 kmem_free(argop, argoplist_size);
8298 return (e.error);
8302 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8303 * it is possible for the filehandle to change due to the rename.
8305 * The compound req in this case includes a post-rename lookup and getattr
8306 * to ensure that we have the correct fh and attributes for the object.
8308 * Rename requires that the current fh be the target directory and the
8309 * saved fh be the source directory. After the operation, the current fh
8310 * is unchanged.
8312 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8313 * update the filehandle for the renamed object. We also get the old
8314 * filehandle for historical reasons; this should be taken out sometime.
8315 * This results in a rather cumbersome compound...
8317 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8318 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8321 static int
8322 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8323 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8325 COMPOUND4args_clnt args;
8326 COMPOUND4res_clnt res, *resp = NULL;
8327 int argoplist_size;
8328 nfs_argop4 *argop;
8329 nfs_resop4 *resop;
8330 int doqueue;
8331 mntinfo4_t *mi;
8332 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8333 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8334 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8335 RENAME4res *rn_res;
8336 GETFH4res *ngf_res;
8337 bool_t needrecov;
8338 nfs4_recov_state_t recov_state;
8339 hrtime_t t;
8340 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8341 dirattr_info_t dinfo, *dinfop = &dinfo;
8343 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8345 recov_state.rs_flags = 0;
8346 recov_state.rs_num_retry_despite_err = 0;
8348 recov_retry:
8349 *statp = NFS4_OK;
8352 * There is a window between the RPC and updating the path and
8353 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8354 * code, so that it doesn't try to use the old path during that
8355 * window.
8357 mutex_enter(&orp->r_statelock);
8358 while (orp->r_flags & R4RECEXPFH) {
8359 klwp_t *lwp = ttolwp(curthread);
8361 if (lwp != NULL)
8362 lwp->lwp_nostop++;
8363 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8364 mutex_exit(&orp->r_statelock);
8365 if (lwp != NULL)
8366 lwp->lwp_nostop--;
8367 return (EINTR);
8369 if (lwp != NULL)
8370 lwp->lwp_nostop--;
8372 orp->r_flags |= R4RECEXPFH;
8373 mutex_exit(&orp->r_statelock);
8375 mi = VTOMI4(odvp);
8377 args.ctag = TAG_RENAME_VFH;
8378 args.array_len = (odvp == ndvp) ? 10 : 12;
8379 argoplist_size = args.array_len * sizeof (nfs_argop4);
8380 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8383 * Rename ops:
8384 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8385 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8386 * LOOKUP(trgt), GETFH(new), GETATTR,
8388 * if (odvp != ndvp)
8389 * add putfh(sourcedir), getattr(sourcedir) }
8391 args.array = argop;
8393 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8394 &recov_state, NULL);
8395 if (e.error) {
8396 kmem_free(argop, argoplist_size);
8397 mutex_enter(&orp->r_statelock);
8398 orp->r_flags &= ~R4RECEXPFH;
8399 cv_broadcast(&orp->r_cv);
8400 mutex_exit(&orp->r_statelock);
8401 return (e.error);
8404 /* 0: putfh source directory */
8405 argop[0].argop = OP_CPUTFH;
8406 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8408 /* 1: Save source fh to free up current for target */
8409 argop[1].argop = OP_SAVEFH;
8411 /* 2: Lookup pre-rename fh of renamed object */
8412 argop[2].argop = OP_CLOOKUP;
8413 argop[2].nfs_argop4_u.opclookup.cname = onm;
8415 /* 3: getfh fh of renamed object (before rename) */
8416 argop[3].argop = OP_GETFH;
8418 /* 4: putfh targetdir */
8419 argop[4].argop = OP_CPUTFH;
8420 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8422 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8423 argop[5].argop = OP_CRENAME;
8424 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8425 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8427 /* 6: getattr of target dir (post op attrs) */
8428 argop[6].argop = OP_GETATTR;
8429 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8430 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8432 /* 7: Lookup post-rename fh of renamed object */
8433 argop[7].argop = OP_CLOOKUP;
8434 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8436 /* 8: getfh fh of renamed object (after rename) */
8437 argop[8].argop = OP_GETFH;
8439 /* 9: getattr of renamed object */
8440 argop[9].argop = OP_GETATTR;
8441 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8442 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8445 * If source/target dirs are different, then get new post-op
8446 * attrs for source dir also.
8448 if (ndvp != odvp) {
8449 /* 10: putfh (sourcedir) */
8450 argop[10].argop = OP_CPUTFH;
8451 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8453 /* 11: getattr (sourcedir) */
8454 argop[11].argop = OP_GETATTR;
8455 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8456 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8459 dnlc_remove(odvp, onm);
8460 dnlc_remove(ndvp, nnm);
8462 doqueue = 1;
8463 t = gethrtime();
8464 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8466 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8467 if (e.error) {
8468 PURGE_ATTRCACHE4(odvp);
8469 PURGE_ATTRCACHE4(ndvp);
8470 if (!needrecov) {
8471 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8472 &recov_state, needrecov);
8473 goto out;
8475 } else {
8476 *statp = res.status;
8479 if (needrecov) {
8480 bool_t abort;
8482 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8483 OP_RENAME, NULL, NULL, NULL);
8484 if (abort == FALSE) {
8485 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8486 &recov_state, needrecov);
8487 kmem_free(argop, argoplist_size);
8488 if (!e.error)
8489 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8490 mutex_enter(&orp->r_statelock);
8491 orp->r_flags &= ~R4RECEXPFH;
8492 cv_broadcast(&orp->r_cv);
8493 mutex_exit(&orp->r_statelock);
8494 goto recov_retry;
8495 } else {
8496 if (e.error != 0) {
8497 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8498 &recov_state, needrecov);
8499 goto out;
8501 /* fall through for res.status case */
8505 resp = &res;
8507 * If OP_RENAME (or any prev op) failed, then return an error.
8508 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8510 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8512 * Error in an op other than last Getattr
8514 e.error = geterrno4(res.status);
8515 PURGE_ATTRCACHE4(odvp);
8516 PURGE_ATTRCACHE4(ndvp);
8518 * System V defines rename to return EEXIST, not
8519 * ENOTEMPTY if the target directory is not empty.
8520 * Over the wire, the error is NFSERR_ENOTEMPTY
8521 * which geterrno4 maps to ENOTEMPTY.
8523 if (e.error == ENOTEMPTY)
8524 e.error = EEXIST;
8525 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8526 needrecov);
8527 goto out;
8530 /* rename results */
8531 rn_res = &res.array[5].nfs_resop4_u.oprename;
8533 if (res.status == NFS4_OK) {
8534 /* Update target attribute, readdir and dnlc caches */
8535 dinfo.di_garp =
8536 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8537 dinfo.di_cred = cr;
8538 dinfo.di_time_call = t;
8539 } else
8540 dinfop = NULL;
8542 /* Update source cache attribute, readdir and dnlc caches */
8543 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8545 /* Update source cache attribute, readdir and dnlc caches */
8546 if (ndvp != odvp) {
8547 update_parentdir_sfh(ovp, ndvp);
8550 * If dinfop is non-NULL, then compound succeded, so
8551 * set di_garp to attrs for source dir. dinfop is only
8552 * set to NULL when compound fails.
8554 if (dinfop)
8555 dinfo.di_garp =
8556 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8557 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8558 dinfop);
8562 * Update the rnode with the new component name and args,
8563 * and if the file handle changed, also update it with the new fh.
8564 * This is only necessary if the target object has an rnode
8565 * entry and there is no need to create one for it.
8567 resop = &res.array[8]; /* getfh new res */
8568 ngf_res = &resop->nfs_resop4_u.opgetfh;
8571 * Update the path and filehandle for the renamed object.
8573 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8575 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8577 if (res.status == NFS4_OK) {
8578 resop++; /* getattr res */
8579 e.error = nfs4_update_attrcache(res.status,
8580 &resop->nfs_resop4_u.opgetattr.ga_res,
8581 t, ovp, cr);
8584 out:
8585 kmem_free(argop, argoplist_size);
8586 if (resp)
8587 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8588 mutex_enter(&orp->r_statelock);
8589 orp->r_flags &= ~R4RECEXPFH;
8590 cv_broadcast(&orp->r_cv);
8591 mutex_exit(&orp->r_statelock);
8593 return (e.error);
8596 /* ARGSUSED */
8597 static int
8598 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8599 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8601 int error;
8602 vnode_t *vp;
8604 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8605 return (EPERM);
8607 * As ".." has special meaning and rather than send a mkdir
8608 * over the wire to just let the server freak out, we just
8609 * short circuit it here and return EEXIST
8611 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8612 return (EEXIST);
8615 * Decision to get the right gid and setgid bit of the
8616 * new directory is now made in call_nfs4_create_req.
8618 va->va_mask |= AT_MODE;
8619 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8620 if (error)
8621 return (error);
8623 *vpp = vp;
8624 return (0);
8629 * rmdir is using the same remove v4 op as does remove.
8630 * Remove requires that the current fh be the target directory.
8631 * After the operation, the current fh is unchanged.
8632 * The compound op structure is:
8633 * PUTFH(targetdir), REMOVE
8635 /*ARGSUSED4*/
8636 static int
8637 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8638 caller_context_t *ct, int flags)
8640 int need_end_op = FALSE;
8641 COMPOUND4args_clnt args;
8642 COMPOUND4res_clnt res, *resp = NULL;
8643 REMOVE4res *rm_res;
8644 nfs_argop4 argop[3];
8645 nfs_resop4 *resop;
8646 vnode_t *vp;
8647 int doqueue;
8648 mntinfo4_t *mi;
8649 rnode4_t *drp;
8650 bool_t needrecov = FALSE;
8651 nfs4_recov_state_t recov_state;
8652 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8653 dirattr_info_t dinfo, *dinfop;
8655 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8656 return (EPERM);
8658 * As ".." has special meaning and rather than send a rmdir
8659 * over the wire to just let the server freak out, we just
8660 * short circuit it here and return EEXIST
8662 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8663 return (EEXIST);
8665 drp = VTOR4(dvp);
8666 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8667 return (EINTR);
8670 * Attempt to prevent a rmdir(".") from succeeding.
8672 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8673 if (e.error) {
8674 nfs_rw_exit(&drp->r_rwlock);
8675 return (e.error);
8677 if (vp == cdir) {
8678 VN_RELE(vp);
8679 nfs_rw_exit(&drp->r_rwlock);
8680 return (EINVAL);
8684 * Since nfsv4 remove op works on both files and directories,
8685 * check that the removed object is indeed a directory.
8687 if (vp->v_type != VDIR) {
8688 VN_RELE(vp);
8689 nfs_rw_exit(&drp->r_rwlock);
8690 return (ENOTDIR);
8694 * First just remove the entry from the name cache, as it
8695 * is most likely an entry for this vp.
8697 dnlc_remove(dvp, nm);
8700 * If there vnode reference count is greater than one, then
8701 * there may be additional references in the DNLC which will
8702 * need to be purged. First, trying removing the entry for
8703 * the parent directory and see if that removes the additional
8704 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8705 * to completely remove any references to the directory which
8706 * might still exist in the DNLC.
8708 if (vp->v_count > 1) {
8709 dnlc_remove(vp, "..");
8710 if (vp->v_count > 1)
8711 dnlc_purge_vp(vp);
8714 mi = VTOMI4(dvp);
8715 recov_state.rs_flags = 0;
8716 recov_state.rs_num_retry_despite_err = 0;
8718 recov_retry:
8719 args.ctag = TAG_RMDIR;
8722 * Rmdir ops: putfh dir; remove
8724 args.array_len = 3;
8725 args.array = argop;
8727 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8728 if (e.error) {
8729 nfs_rw_exit(&drp->r_rwlock);
8730 return (e.error);
8732 need_end_op = TRUE;
8734 /* putfh directory */
8735 argop[0].argop = OP_CPUTFH;
8736 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8738 /* remove */
8739 argop[1].argop = OP_CREMOVE;
8740 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8742 /* getattr (postop attrs for dir that contained removed dir) */
8743 argop[2].argop = OP_GETATTR;
8744 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8745 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8747 dinfo.di_time_call = gethrtime();
8748 doqueue = 1;
8749 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8751 PURGE_ATTRCACHE4(vp);
8753 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8754 if (e.error) {
8755 PURGE_ATTRCACHE4(dvp);
8758 if (needrecov) {
8759 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8760 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8761 if (!e.error)
8762 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8764 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8765 needrecov);
8766 need_end_op = FALSE;
8767 goto recov_retry;
8771 if (!e.error) {
8772 resp = &res;
8775 * Only return error if first 2 ops (OP_REMOVE or earlier)
8776 * failed.
8778 if (res.status != NFS4_OK && res.array_len <= 2) {
8779 e.error = geterrno4(res.status);
8780 PURGE_ATTRCACHE4(dvp);
8781 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8782 &recov_state, needrecov);
8783 need_end_op = FALSE;
8784 nfs4_purge_stale_fh(e.error, dvp, cr);
8786 * System V defines rmdir to return EEXIST, not
8787 * ENOTEMPTY if the directory is not empty. Over
8788 * the wire, the error is NFSERR_ENOTEMPTY which
8789 * geterrno4 maps to ENOTEMPTY.
8791 if (e.error == ENOTEMPTY)
8792 e.error = EEXIST;
8793 } else {
8794 resop = &res.array[1]; /* remove res */
8795 rm_res = &resop->nfs_resop4_u.opremove;
8797 if (res.status == NFS4_OK) {
8798 resop = &res.array[2]; /* dir attrs */
8799 dinfo.di_garp =
8800 &resop->nfs_resop4_u.opgetattr.ga_res;
8801 dinfo.di_cred = cr;
8802 dinfop = &dinfo;
8803 } else
8804 dinfop = NULL;
8806 /* Update dir attribute, readdir and dnlc caches */
8807 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8808 dinfop);
8810 /* destroy rddir cache for dir that was removed */
8811 if (VTOR4(vp)->r_dir != NULL)
8812 nfs4_purge_rddir_cache(vp);
8816 if (need_end_op)
8817 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8819 nfs_rw_exit(&drp->r_rwlock);
8821 if (resp)
8822 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8824 if (e.error == 0) {
8825 vnode_t *tvp;
8826 rnode4_t *trp;
8827 trp = VTOR4(vp);
8828 tvp = vp;
8829 if (IS_SHADOW(vp, trp))
8830 tvp = RTOV4(trp);
8831 vnevent_rmdir(tvp, dvp, nm, ct);
8834 VN_RELE(vp);
8836 return (e.error);
8839 /* ARGSUSED */
8840 static int
8841 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8842 caller_context_t *ct, int flags)
8844 int error;
8845 vnode_t *vp;
8846 rnode4_t *rp;
8847 char *contents;
8848 mntinfo4_t *mi = VTOMI4(dvp);
8850 if (nfs_zone() != mi->mi_zone)
8851 return (EPERM);
8852 if (!(mi->mi_flags & MI4_SYMLINK))
8853 return (EOPNOTSUPP);
8855 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8856 if (error)
8857 return (error);
8859 ASSERT(nfs4_consistent_type(vp));
8860 rp = VTOR4(vp);
8861 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8863 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8865 if (contents != NULL) {
8866 mutex_enter(&rp->r_statelock);
8867 if (rp->r_symlink.contents == NULL) {
8868 rp->r_symlink.len = strlen(tnm);
8869 bcopy(tnm, contents, rp->r_symlink.len);
8870 rp->r_symlink.contents = contents;
8871 rp->r_symlink.size = MAXPATHLEN;
8872 mutex_exit(&rp->r_statelock);
8873 } else {
8874 mutex_exit(&rp->r_statelock);
8875 kmem_free((void *)contents, MAXPATHLEN);
8879 VN_RELE(vp);
8881 return (error);
8886 * Read directory entries.
8887 * There are some weird things to look out for here. The uio_loffset
8888 * field is either 0 or it is the offset returned from a previous
8889 * readdir. It is an opaque value used by the server to find the
8890 * correct directory block to read. The count field is the number
8891 * of blocks to read on the server. This is advisory only, the server
8892 * may return only one block's worth of entries. Entries may be compressed
8893 * on the server.
8895 /* ARGSUSED */
8896 static int
8897 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8898 caller_context_t *ct, int flags)
8900 int error;
8901 uint_t count;
8902 rnode4_t *rp;
8903 rddir4_cache *rdc;
8904 rddir4_cache *rrdc;
8906 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8907 return (EIO);
8908 rp = VTOR4(vp);
8910 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8913 * Make sure that the directory cache is valid.
8915 if (rp->r_dir != NULL) {
8916 if (nfs_disable_rddir_cache != 0) {
8918 * Setting nfs_disable_rddir_cache in /etc/system
8919 * allows interoperability with servers that do not
8920 * properly update the attributes of directories.
8921 * Any cached information gets purged before an
8922 * access is made to it.
8924 nfs4_purge_rddir_cache(vp);
8927 error = nfs4_validate_caches(vp, cr);
8928 if (error)
8929 return (error);
8932 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8935 * Short circuit last readdir which always returns 0 bytes.
8936 * This can be done after the directory has been read through
8937 * completely at least once. This will set r_direof which
8938 * can be used to find the value of the last cookie.
8940 mutex_enter(&rp->r_statelock);
8941 if (rp->r_direof != NULL &&
8942 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8943 mutex_exit(&rp->r_statelock);
8944 #ifdef DEBUG
8945 nfs4_readdir_cache_shorts++;
8946 #endif
8947 if (eofp)
8948 *eofp = 1;
8949 return (0);
8953 * Look for a cache entry. Cache entries are identified
8954 * by the NFS cookie value and the byte count requested.
8956 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8959 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8961 if (rdc == NULL) {
8962 mutex_exit(&rp->r_statelock);
8963 return (EINTR);
8967 * Check to see if we need to fill this entry in.
8969 if (rdc->flags & RDDIRREQ) {
8970 rdc->flags &= ~RDDIRREQ;
8971 rdc->flags |= RDDIR;
8972 mutex_exit(&rp->r_statelock);
8975 * Do the readdir.
8977 nfs4readdir(vp, rdc, cr);
8980 * Reacquire the lock, so that we can continue
8982 mutex_enter(&rp->r_statelock);
8984 * The entry is now complete
8986 rdc->flags &= ~RDDIR;
8989 ASSERT(!(rdc->flags & RDDIR));
8992 * If an error occurred while attempting
8993 * to fill the cache entry, mark the entry invalid and
8994 * just return the error.
8996 if (rdc->error) {
8997 error = rdc->error;
8998 rdc->flags |= RDDIRREQ;
8999 rddir4_cache_rele(rp, rdc);
9000 mutex_exit(&rp->r_statelock);
9001 return (error);
9005 * The cache entry is complete and good,
9006 * copyout the dirent structs to the calling
9007 * thread.
9009 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9012 * If no error occurred during the copyout,
9013 * update the offset in the uio struct to
9014 * contain the value of the next NFS 4 cookie
9015 * and set the eof value appropriately.
9017 if (!error) {
9018 uiop->uio_loffset = rdc->nfs4_ncookie;
9019 if (eofp)
9020 *eofp = rdc->eof;
9024 * Decide whether to do readahead. Don't if we
9025 * have already read to the end of directory.
9027 if (rdc->eof) {
9029 * Make the entry the direof only if it is cached
9031 if (rdc->flags & RDDIRCACHED)
9032 rp->r_direof = rdc;
9033 rddir4_cache_rele(rp, rdc);
9034 mutex_exit(&rp->r_statelock);
9035 return (error);
9038 /* Determine if a readdir readahead should be done */
9039 if (!(rp->r_flags & R4LOOKUP)) {
9040 rddir4_cache_rele(rp, rdc);
9041 mutex_exit(&rp->r_statelock);
9042 return (error);
9046 * Now look for a readahead entry.
9048 * Check to see whether we found an entry for the readahead.
9049 * If so, we don't need to do anything further, so free the new
9050 * entry if one was allocated. Otherwise, allocate a new entry, add
9051 * it to the cache, and then initiate an asynchronous readdir
9052 * operation to fill it.
9054 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9057 * A readdir cache entry could not be obtained for the readahead. In
9058 * this case we skip the readahead and return.
9060 if (rrdc == NULL) {
9061 rddir4_cache_rele(rp, rdc);
9062 mutex_exit(&rp->r_statelock);
9063 return (error);
9067 * Check to see if we need to fill this entry in.
9069 if (rrdc->flags & RDDIRREQ) {
9070 rrdc->flags &= ~RDDIRREQ;
9071 rrdc->flags |= RDDIR;
9072 rddir4_cache_rele(rp, rdc);
9073 mutex_exit(&rp->r_statelock);
9074 #ifdef DEBUG
9075 nfs4_readdir_readahead++;
9076 #endif
9078 * Do the readdir.
9080 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9081 return (error);
9084 rddir4_cache_rele(rp, rrdc);
9085 rddir4_cache_rele(rp, rdc);
9086 mutex_exit(&rp->r_statelock);
9087 return (error);
9090 static int
9091 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9093 int error;
9094 rnode4_t *rp;
9096 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9098 rp = VTOR4(vp);
9101 * Obtain the readdir results for the caller.
9103 nfs4readdir(vp, rdc, cr);
9105 mutex_enter(&rp->r_statelock);
9107 * The entry is now complete
9109 rdc->flags &= ~RDDIR;
9111 error = rdc->error;
9112 if (error)
9113 rdc->flags |= RDDIRREQ;
9114 rddir4_cache_rele(rp, rdc);
9115 mutex_exit(&rp->r_statelock);
9117 return (error);
9121 * Read directory entries.
9122 * There are some weird things to look out for here. The uio_loffset
9123 * field is either 0 or it is the offset returned from a previous
9124 * readdir. It is an opaque value used by the server to find the
9125 * correct directory block to read. The count field is the number
9126 * of blocks to read on the server. This is advisory only, the server
9127 * may return only one block's worth of entries. Entries may be compressed
9128 * on the server.
9130 * Generates the following compound request:
9131 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9132 * must include a Lookupp as well. In this case, send:
9133 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9134 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9136 * Get complete attributes and filehandles for entries if this is the
9137 * first read of the directory. Otherwise, just get fileid's.
9139 static void
9140 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9142 COMPOUND4args_clnt args;
9143 COMPOUND4res_clnt res;
9144 READDIR4args *rargs;
9145 READDIR4res_clnt *rd_res;
9146 bitmap4 rd_bitsval;
9147 nfs_argop4 argop[5];
9148 nfs_resop4 *resop;
9149 rnode4_t *rp = VTOR4(vp);
9150 mntinfo4_t *mi = VTOMI4(vp);
9151 int doqueue;
9152 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9153 vnode_t *dvp;
9154 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9155 int num_ops, res_opcnt;
9156 bool_t needrecov = FALSE;
9157 nfs4_recov_state_t recov_state;
9158 hrtime_t t;
9159 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9161 ASSERT(nfs_zone() == mi->mi_zone);
9162 ASSERT(rdc->flags & RDDIR);
9163 ASSERT(rdc->entries == NULL);
9166 * If rp were a stub, it should have triggered and caused
9167 * a mount for us to get this far.
9169 ASSERT(!RP_ISSTUB(rp));
9171 num_ops = 2;
9172 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9174 * Since nfsv4 readdir may not return entries for "." and "..",
9175 * the client must recreate them:
9176 * To find the correct nodeid, do the following:
9177 * For current node, get nodeid from dnlc.
9178 * - if current node is rootvp, set pnodeid to nodeid.
9179 * - else if parent is in the dnlc, get its nodeid from there.
9180 * - else add LOOKUPP+GETATTR to compound.
9182 nodeid = rp->r_attr.va_nodeid;
9183 if (vp->v_flag & VROOT) {
9184 pnodeid = nodeid; /* root of mount point */
9185 } else {
9186 dvp = dnlc_lookup(vp, "..");
9187 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9188 /* parent in dnlc cache - no need for otw */
9189 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9190 } else {
9192 * parent not in dnlc cache,
9193 * do lookupp to get its id
9195 num_ops = 5;
9196 pnodeid = 0; /* set later by getattr parent */
9198 if (dvp)
9199 VN_RELE(dvp);
9202 recov_state.rs_flags = 0;
9203 recov_state.rs_num_retry_despite_err = 0;
9205 /* Save the original mount point security flavor */
9206 (void) save_mnt_secinfo(mi->mi_curr_serv);
9208 recov_retry:
9209 args.ctag = TAG_READDIR;
9211 args.array = argop;
9212 args.array_len = num_ops;
9214 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9215 &recov_state, NULL)) {
9217 * If readdir a node that is a stub for a crossed mount point,
9218 * keep the original secinfo flavor for the current file
9219 * system, not the crossed one.
9221 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9222 rdc->error = e.error;
9223 return;
9227 * Determine which attrs to request for dirents. This code
9228 * must be protected by nfs4_start/end_fop because of r_server
9229 * (which will change during failover recovery).
9232 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9234 * Get all vattr attrs plus filehandle and rdattr_error
9236 rd_bitsval = NFS4_VATTR_MASK |
9237 FATTR4_RDATTR_ERROR_MASK |
9238 FATTR4_FILEHANDLE_MASK;
9240 if (rp->r_flags & R4READDIRWATTR) {
9241 mutex_enter(&rp->r_statelock);
9242 rp->r_flags &= ~R4READDIRWATTR;
9243 mutex_exit(&rp->r_statelock);
9245 } else {
9246 servinfo4_t *svp = rp->r_server;
9249 * Already read directory. Use readdir with
9250 * no attrs (except for mounted_on_fileid) for updates.
9252 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9255 * request mounted on fileid if supported, else request
9256 * fileid. maybe we should verify that fileid is supported
9257 * and request something else if not.
9259 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9260 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9261 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9262 nfs_rw_exit(&svp->sv_lock);
9265 /* putfh directory fh */
9266 argop[0].argop = OP_CPUTFH;
9267 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9269 argop[1].argop = OP_READDIR;
9270 rargs = &argop[1].nfs_argop4_u.opreaddir;
9272 * 1 and 2 are reserved for client "." and ".." entry offset.
9273 * cookie 0 should be used over-the-wire to start reading at
9274 * the beginning of the directory excluding "." and "..".
9276 if (rdc->nfs4_cookie == 0 ||
9277 rdc->nfs4_cookie == 1 ||
9278 rdc->nfs4_cookie == 2) {
9279 rargs->cookie = (nfs_cookie4)0;
9280 rargs->cookieverf = 0;
9281 } else {
9282 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9283 mutex_enter(&rp->r_statelock);
9284 rargs->cookieverf = rp->r_cookieverf4;
9285 mutex_exit(&rp->r_statelock);
9287 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9288 rargs->maxcount = mi->mi_tsize;
9289 rargs->attr_request = rd_bitsval;
9290 rargs->rdc = rdc;
9291 rargs->dvp = vp;
9292 rargs->mi = mi;
9293 rargs->cr = cr;
9297 * If count < than the minimum required, we return no entries
9298 * and fail with EINVAL
9300 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9301 rdc->error = EINVAL;
9302 goto out;
9305 if (args.array_len == 5) {
9307 * Add lookupp and getattr for parent nodeid.
9309 argop[2].argop = OP_LOOKUPP;
9311 argop[3].argop = OP_GETFH;
9313 /* getattr parent */
9314 argop[4].argop = OP_GETATTR;
9315 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9316 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9319 doqueue = 1;
9321 if (mi->mi_io_kstats) {
9322 mutex_enter(&mi->mi_lock);
9323 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9324 mutex_exit(&mi->mi_lock);
9327 /* capture the time of this call */
9328 rargs->t = t = gethrtime();
9330 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9332 if (mi->mi_io_kstats) {
9333 mutex_enter(&mi->mi_lock);
9334 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9335 mutex_exit(&mi->mi_lock);
9338 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9341 * If RPC error occurred and it isn't an error that
9342 * triggers recovery, then go ahead and fail now.
9344 if (e.error != 0 && !needrecov) {
9345 rdc->error = e.error;
9346 goto out;
9349 if (needrecov) {
9350 bool_t abort;
9352 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9353 "nfs4readdir: initiating recovery.\n"));
9355 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9356 NULL, OP_READDIR, NULL, NULL, NULL);
9357 if (abort == FALSE) {
9358 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9359 &recov_state, needrecov);
9360 if (!e.error)
9361 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9362 if (rdc->entries != NULL) {
9363 kmem_free(rdc->entries, rdc->entlen);
9364 rdc->entries = NULL;
9366 goto recov_retry;
9369 if (e.error != 0) {
9370 rdc->error = e.error;
9371 goto out;
9374 /* fall through for res.status case */
9377 res_opcnt = res.array_len;
9380 * If compound failed first 2 ops (PUTFH+READDIR), then return
9381 * failure here. Subsequent ops are for filling out dot-dot
9382 * dirent, and if they fail, we still want to give the caller
9383 * the dirents returned by (the successful) READDIR op, so we need
9384 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9386 * One example where PUTFH+READDIR ops would succeed but
9387 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9388 * but lacks x. In this case, a POSIX server's fop_readdir
9389 * would succeed; however, fop_lookup(..) would fail since no
9390 * x perm. We need to come up with a non-vendor-specific way
9391 * for a POSIX server to return d_ino from dotdot's dirent if
9392 * client only requests mounted_on_fileid, and just say the
9393 * LOOKUPP succeeded and fill out the GETATTR. However, if
9394 * client requested any mandatory attrs, server would be required
9395 * to fail the GETATTR op because it can't call fop_lookup+fop_getattr
9396 * for dotdot.
9399 if (res.status) {
9400 if (res_opcnt <= 2) {
9401 e.error = geterrno4(res.status);
9402 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9403 &recov_state, needrecov);
9404 nfs4_purge_stale_fh(e.error, vp, cr);
9405 rdc->error = e.error;
9406 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9407 if (rdc->entries != NULL) {
9408 kmem_free(rdc->entries, rdc->entlen);
9409 rdc->entries = NULL;
9412 * If readdir a node that is a stub for a
9413 * crossed mount point, keep the original
9414 * secinfo flavor for the current file system,
9415 * not the crossed one.
9417 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9418 return;
9422 resop = &res.array[1]; /* readdir res */
9423 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9425 mutex_enter(&rp->r_statelock);
9426 rp->r_cookieverf4 = rd_res->cookieverf;
9427 mutex_exit(&rp->r_statelock);
9430 * For "." and ".." entries
9431 * e.g.
9432 * seek(cookie=0) -> "." entry with d_off = 1
9433 * seek(cookie=1) -> ".." entry with d_off = 2
9435 if (cookie == (nfs_cookie4) 0) {
9436 if (rd_res->dotp)
9437 rd_res->dotp->d_ino = nodeid;
9438 if (rd_res->dotdotp)
9439 rd_res->dotdotp->d_ino = pnodeid;
9441 if (cookie == (nfs_cookie4) 1) {
9442 if (rd_res->dotdotp)
9443 rd_res->dotdotp->d_ino = pnodeid;
9447 /* LOOKUPP+GETATTR attemped */
9448 if (args.array_len == 5 && rd_res->dotdotp) {
9449 if (res.status == NFS4_OK && res_opcnt == 5) {
9450 nfs_fh4 *fhp;
9451 nfs4_sharedfh_t *sfhp;
9452 vnode_t *pvp;
9453 nfs4_ga_res_t *garp;
9455 resop++; /* lookupp */
9456 resop++; /* getfh */
9457 fhp = &resop->nfs_resop4_u.opgetfh.object;
9459 resop++; /* getattr of parent */
9462 * First, take care of finishing the
9463 * readdir results.
9465 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9467 * The d_ino of .. must be the inode number
9468 * of the mounted filesystem.
9470 if (garp->n4g_va.va_mask & AT_NODEID)
9471 rd_res->dotdotp->d_ino =
9472 garp->n4g_va.va_nodeid;
9476 * Next, create the ".." dnlc entry
9478 sfhp = sfh4_get(fhp, mi);
9479 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9480 dnlc_update(vp, "..", pvp);
9481 VN_RELE(pvp);
9483 sfh4_rele(&sfhp);
9487 if (mi->mi_io_kstats) {
9488 mutex_enter(&mi->mi_lock);
9489 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9490 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9491 mutex_exit(&mi->mi_lock);
9494 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9496 out:
9498 * If readdir a node that is a stub for a crossed mount point,
9499 * keep the original secinfo flavor for the current file system,
9500 * not the crossed one.
9502 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9504 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9508 static int
9509 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9511 rnode4_t *rp = VTOR4(bp->b_vp);
9512 int count;
9513 int error;
9514 cred_t *cred_otw = NULL;
9515 offset_t offset;
9516 nfs4_open_stream_t *osp = NULL;
9517 bool_t first_time = TRUE; /* first time getting otw cred */
9518 bool_t last_time = FALSE; /* last time getting otw cred */
9520 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9522 DTRACE_IO1(start, struct buf *, bp);
9523 offset = ldbtob(bp->b_lblkno);
9525 if (bp->b_flags & B_READ) {
9526 read_again:
9528 * Releases the osp, if it is provided.
9529 * Puts a hold on the cred_otw and the new osp (if found).
9531 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9532 &first_time, &last_time);
9533 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9534 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9535 readahead, NULL);
9536 crfree(cred_otw);
9537 if (!error) {
9538 if (bp->b_resid) {
9540 * Didn't get it all because we hit EOF,
9541 * zero all the memory beyond the EOF.
9543 /* bzero(rdaddr + */
9544 bzero(bp->b_un.b_addr +
9545 bp->b_bcount - bp->b_resid, bp->b_resid);
9547 mutex_enter(&rp->r_statelock);
9548 if (bp->b_resid == bp->b_bcount &&
9549 offset >= rp->r_size) {
9551 * We didn't read anything at all as we are
9552 * past EOF. Return an error indicator back
9553 * but don't destroy the pages (yet).
9555 error = NFS_EOF;
9557 mutex_exit(&rp->r_statelock);
9558 } else if (error == EACCES && last_time == FALSE) {
9559 goto read_again;
9561 } else {
9562 if (!(rp->r_flags & R4STALE)) {
9563 write_again:
9565 * Releases the osp, if it is provided.
9566 * Puts a hold on the cred_otw and the new
9567 * osp (if found).
9569 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9570 &first_time, &last_time);
9571 mutex_enter(&rp->r_statelock);
9572 count = MIN(bp->b_bcount, rp->r_size - offset);
9573 mutex_exit(&rp->r_statelock);
9574 if (count < 0)
9575 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9576 #ifdef DEBUG
9577 if (count == 0) {
9578 zoneid_t zoneid = getzoneid();
9580 zcmn_err(zoneid, CE_WARN,
9581 "nfs4_bio: zero length write at %lld",
9582 offset);
9583 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9584 "b_bcount=%ld, file size=%lld",
9585 rp->r_flags, (long)bp->b_bcount,
9586 rp->r_size);
9587 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9588 if (nfs4_bio_do_stop)
9589 debug_enter("nfs4_bio");
9591 #endif
9592 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9593 count, cred_otw, stab_comm);
9594 if (error == EACCES && last_time == FALSE) {
9595 crfree(cred_otw);
9596 goto write_again;
9598 bp->b_error = error;
9599 if (error && error != EINTR &&
9600 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9602 * Don't print EDQUOT errors on the console.
9603 * Don't print asynchronous EACCES errors.
9604 * Don't print EFBIG errors.
9605 * Print all other write errors.
9607 if (error != EDQUOT && error != EFBIG &&
9608 (error != EACCES ||
9609 !(bp->b_flags & B_ASYNC)))
9610 nfs4_write_error(bp->b_vp,
9611 error, cred_otw);
9613 * Update r_error and r_flags as appropriate.
9614 * If the error was ESTALE, then mark the
9615 * rnode as not being writeable and save
9616 * the error status. Otherwise, save any
9617 * errors which occur from asynchronous
9618 * page invalidations. Any errors occurring
9619 * from other operations should be saved
9620 * by the caller.
9622 mutex_enter(&rp->r_statelock);
9623 if (error == ESTALE) {
9624 rp->r_flags |= R4STALE;
9625 if (!rp->r_error)
9626 rp->r_error = error;
9627 } else if (!rp->r_error &&
9628 (bp->b_flags &
9629 (B_INVAL|B_FORCE|B_ASYNC)) ==
9630 (B_INVAL|B_FORCE|B_ASYNC)) {
9631 rp->r_error = error;
9633 mutex_exit(&rp->r_statelock);
9635 crfree(cred_otw);
9636 } else {
9637 error = rp->r_error;
9639 * A close may have cleared r_error, if so,
9640 * propagate ESTALE error return properly
9642 if (error == 0)
9643 error = ESTALE;
9647 if (error != 0 && error != NFS_EOF)
9648 bp->b_flags |= B_ERROR;
9650 if (osp)
9651 open_stream_rele(osp, rp);
9653 DTRACE_IO1(done, struct buf *, bp);
9655 return (error);
9658 /* ARGSUSED */
9660 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9662 return (EREMOTE);
9665 /* ARGSUSED2 */
9667 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9669 rnode4_t *rp = VTOR4(vp);
9671 if (!write_lock) {
9672 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9673 return (V_WRITELOCK_FALSE);
9676 if ((rp->r_flags & R4DIRECTIO) ||
9677 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9678 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9679 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9680 return (V_WRITELOCK_FALSE);
9681 nfs_rw_exit(&rp->r_rwlock);
9684 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9685 return (V_WRITELOCK_TRUE);
9688 /* ARGSUSED */
9689 void
9690 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9692 rnode4_t *rp = VTOR4(vp);
9694 nfs_rw_exit(&rp->r_rwlock);
9697 /* ARGSUSED */
9698 static int
9699 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9701 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9702 return (EIO);
9705 * Because we stuff the readdir cookie into the offset field
9706 * someone may attempt to do an lseek with the cookie which
9707 * we want to succeed.
9709 if (vp->v_type == VDIR)
9710 return (0);
9711 if (*noffp < 0)
9712 return (EINVAL);
9713 return (0);
9718 * Return all the pages from [off..off+len) in file
9720 /* ARGSUSED */
9721 static int
9722 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9723 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9724 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9726 rnode4_t *rp;
9727 int error;
9728 mntinfo4_t *mi;
9730 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9731 return (EIO);
9732 rp = VTOR4(vp);
9733 if (IS_SHADOW(vp, rp))
9734 vp = RTOV4(rp);
9736 if (vp->v_flag & VNOMAP)
9737 return (ENOSYS);
9739 if (protp != NULL)
9740 *protp = PROT_ALL;
9743 * Now validate that the caches are up to date.
9745 if (error = nfs4_validate_caches(vp, cr))
9746 return (error);
9748 mi = VTOMI4(vp);
9749 retry:
9750 mutex_enter(&rp->r_statelock);
9753 * Don't create dirty pages faster than they
9754 * can be cleaned so that the system doesn't
9755 * get imbalanced. If the async queue is
9756 * maxed out, then wait for it to drain before
9757 * creating more dirty pages. Also, wait for
9758 * any threads doing pagewalks in the vop_getattr
9759 * entry points so that they don't block for
9760 * long periods.
9762 if (rw == S_CREATE) {
9763 while ((mi->mi_max_threads != 0 &&
9764 rp->r_awcount > 2 * mi->mi_max_threads) ||
9765 rp->r_gcount > 0)
9766 cv_wait(&rp->r_cv, &rp->r_statelock);
9770 * If we are getting called as a side effect of an nfs_write()
9771 * operation the local file size might not be extended yet.
9772 * In this case we want to be able to return pages of zeroes.
9774 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9775 NFS4_DEBUG(nfs4_pageio_debug,
9776 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9777 "len=%llu, size=%llu, attrsize =%llu", off,
9778 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9779 mutex_exit(&rp->r_statelock);
9780 return (EFAULT); /* beyond EOF */
9783 mutex_exit(&rp->r_statelock);
9785 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9786 pl, plsz, seg, addr, rw, cr);
9787 NFS4_DEBUG(nfs4_pageio_debug && error,
9788 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9789 error, off, (u_longlong_t)len));
9791 switch (error) {
9792 case NFS_EOF:
9793 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9794 goto retry;
9795 case ESTALE:
9796 nfs4_purge_stale_fh(error, vp, cr);
9799 return (error);
9803 * Called from pvn_getpages to get a particular page.
9805 /* ARGSUSED */
9806 static int
9807 nfs4_getapage(vnode_t *vp, uoff_t off, size_t len, uint_t *protp,
9808 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9809 enum seg_rw rw, cred_t *cr)
9811 rnode4_t *rp;
9812 uint_t bsize;
9813 struct buf *bp;
9814 page_t *pp;
9815 uoff_t lbn;
9816 uoff_t io_off;
9817 uoff_t blkoff;
9818 uoff_t rablkoff;
9819 size_t io_len;
9820 uint_t blksize;
9821 int error;
9822 int readahead;
9823 int readahead_issued = 0;
9824 int ra_window; /* readahead window */
9825 page_t *pagefound;
9826 page_t *savepp;
9828 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9829 return (EIO);
9831 rp = VTOR4(vp);
9832 ASSERT(!IS_SHADOW(vp, rp));
9833 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9835 reread:
9836 bp = NULL;
9837 pp = NULL;
9838 pagefound = NULL;
9840 if (pl != NULL)
9841 pl[0] = NULL;
9843 error = 0;
9844 lbn = off / bsize;
9845 blkoff = lbn * bsize;
9848 * Queueing up the readahead before doing the synchronous read
9849 * results in a significant increase in read throughput because
9850 * of the increased parallelism between the async threads and
9851 * the process context.
9853 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9854 rw != S_CREATE &&
9855 !(vp->v_flag & VNOCACHE)) {
9856 mutex_enter(&rp->r_statelock);
9859 * Calculate the number of readaheads to do.
9860 * a) No readaheads at offset = 0.
9861 * b) Do maximum(nfs4_nra) readaheads when the readahead
9862 * window is closed.
9863 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9864 * upon how far the readahead window is open or close.
9865 * d) No readaheads if rp->r_nextr is not within the scope
9866 * of the readahead window (random i/o).
9869 if (off == 0)
9870 readahead = 0;
9871 else if (blkoff == rp->r_nextr)
9872 readahead = nfs4_nra;
9873 else if (rp->r_nextr > blkoff &&
9874 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9875 <= (nfs4_nra - 1)))
9876 readahead = nfs4_nra - ra_window;
9877 else
9878 readahead = 0;
9880 rablkoff = rp->r_nextr;
9881 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9882 mutex_exit(&rp->r_statelock);
9883 if (nfs4_async_readahead(vp, rablkoff + bsize,
9884 addr + (rablkoff + bsize - off),
9885 seg, cr, nfs4_readahead) < 0) {
9886 mutex_enter(&rp->r_statelock);
9887 break;
9889 readahead--;
9890 rablkoff += bsize;
9892 * Indicate that we did a readahead so
9893 * readahead offset is not updated
9894 * by the synchronous read below.
9896 readahead_issued = 1;
9897 mutex_enter(&rp->r_statelock);
9899 * set readahead offset to
9900 * offset of last async readahead
9901 * request.
9903 rp->r_nextr = rablkoff;
9905 mutex_exit(&rp->r_statelock);
9908 again:
9909 if ((pagefound = page_exists(&vp->v_object, off)) == NULL) {
9910 if (pl == NULL) {
9911 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9912 nfs4_readahead);
9913 } else if (rw == S_CREATE) {
9915 * Block for this page is not allocated, or the offset
9916 * is beyond the current allocation size, or we're
9917 * allocating a swap slot and the page was not found,
9918 * so allocate it and return a zero page.
9920 if ((pp = page_create_va(&vp->v_object, off,
9921 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9922 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9923 io_len = PAGESIZE;
9924 mutex_enter(&rp->r_statelock);
9925 rp->r_nextr = off + PAGESIZE;
9926 mutex_exit(&rp->r_statelock);
9927 } else {
9929 * Need to go to server to get a block
9931 mutex_enter(&rp->r_statelock);
9932 if (blkoff < rp->r_size &&
9933 blkoff + bsize > rp->r_size) {
9935 * If less than a block left in
9936 * file read less than a block.
9938 if (rp->r_size <= off) {
9940 * Trying to access beyond EOF,
9941 * set up to get at least one page.
9943 blksize = off + PAGESIZE - blkoff;
9944 } else
9945 blksize = rp->r_size - blkoff;
9946 } else if ((off == 0) ||
9947 (off != rp->r_nextr && !readahead_issued)) {
9948 blksize = PAGESIZE;
9949 blkoff = off; /* block = page here */
9950 } else
9951 blksize = bsize;
9952 mutex_exit(&rp->r_statelock);
9954 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9955 &io_len, blkoff, blksize, 0);
9958 * Some other thread has entered the page,
9959 * so just use it.
9961 if (pp == NULL)
9962 goto again;
9965 * Now round the request size up to page boundaries.
9966 * This ensures that the entire page will be
9967 * initialized to zeroes if EOF is encountered.
9969 io_len = ptob(btopr(io_len));
9971 bp = pageio_setup(pp, io_len, vp, B_READ);
9972 ASSERT(bp != NULL);
9975 * pageio_setup should have set b_addr to 0. This
9976 * is correct since we want to do I/O on a page
9977 * boundary. bp_mapin will use this addr to calculate
9978 * an offset, and then set b_addr to the kernel virtual
9979 * address it allocated for us.
9981 ASSERT(bp->b_un.b_addr == 0);
9983 bp->b_edev = 0;
9984 bp->b_dev = 0;
9985 bp->b_lblkno = lbtodb(io_off);
9986 bp->b_file = vp;
9987 bp->b_offset = (offset_t)off;
9988 bp_mapin(bp);
9991 * If doing a write beyond what we believe is EOF,
9992 * don't bother trying to read the pages from the
9993 * server, we'll just zero the pages here. We
9994 * don't check that the rw flag is S_WRITE here
9995 * because some implementations may attempt a
9996 * read access to the buffer before copying data.
9998 mutex_enter(&rp->r_statelock);
9999 if (io_off >= rp->r_size && seg == segkmap) {
10000 mutex_exit(&rp->r_statelock);
10001 bzero(bp->b_un.b_addr, io_len);
10002 } else {
10003 mutex_exit(&rp->r_statelock);
10004 error = nfs4_bio(bp, NULL, cr, FALSE);
10008 * Unmap the buffer before freeing it.
10010 bp_mapout(bp);
10011 pageio_done(bp);
10013 savepp = pp;
10014 do {
10015 pp->p_fsdata = C_NOCOMMIT;
10016 } while ((pp = pp->p_next) != savepp);
10018 if (error == NFS_EOF) {
10020 * If doing a write system call just return
10021 * zeroed pages, else user tried to get pages
10022 * beyond EOF, return error. We don't check
10023 * that the rw flag is S_WRITE here because
10024 * some implementations may attempt a read
10025 * access to the buffer before copying data.
10027 if (seg == segkmap)
10028 error = 0;
10029 else
10030 error = EFAULT;
10033 if (!readahead_issued && !error) {
10034 mutex_enter(&rp->r_statelock);
10035 rp->r_nextr = io_off + io_len;
10036 mutex_exit(&rp->r_statelock);
10041 out:
10042 if (pl == NULL)
10043 return (error);
10045 if (error) {
10046 if (pp != NULL)
10047 pvn_read_done(pp, B_ERROR);
10048 return (error);
10051 if (pagefound) {
10052 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10055 * Page exists in the cache, acquire the appropriate lock.
10056 * If this fails, start all over again.
10058 if ((pp = page_lookup(&vp->v_object, off, se)) == NULL) {
10059 #ifdef DEBUG
10060 nfs4_lostpage++;
10061 #endif
10062 goto reread;
10064 pl[0] = pp;
10065 pl[1] = NULL;
10066 return (0);
10069 if (pp != NULL)
10070 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10072 return (error);
10075 static void
10076 nfs4_readahead(vnode_t *vp, uoff_t blkoff, caddr_t addr, struct seg *seg,
10077 cred_t *cr)
10079 int error;
10080 page_t *pp;
10081 uoff_t io_off;
10082 size_t io_len;
10083 struct buf *bp;
10084 uint_t bsize, blksize;
10085 rnode4_t *rp = VTOR4(vp);
10086 page_t *savepp;
10088 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10090 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10092 mutex_enter(&rp->r_statelock);
10093 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10095 * If less than a block left in file read less
10096 * than a block.
10098 blksize = rp->r_size - blkoff;
10099 } else
10100 blksize = bsize;
10101 mutex_exit(&rp->r_statelock);
10103 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10104 &io_off, &io_len, blkoff, blksize, 1);
10106 * The isra flag passed to the kluster function is 1, we may have
10107 * gotten a return value of NULL for a variety of reasons (# of free
10108 * pages < minfree, someone entered the page on the vnode etc). In all
10109 * cases, we want to punt on the readahead.
10111 if (pp == NULL)
10112 return;
10115 * Now round the request size up to page boundaries.
10116 * This ensures that the entire page will be
10117 * initialized to zeroes if EOF is encountered.
10119 io_len = ptob(btopr(io_len));
10121 bp = pageio_setup(pp, io_len, vp, B_READ);
10122 ASSERT(bp != NULL);
10125 * pageio_setup should have set b_addr to 0. This is correct since
10126 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10127 * to calculate an offset, and then set b_addr to the kernel virtual
10128 * address it allocated for us.
10130 ASSERT(bp->b_un.b_addr == 0);
10132 bp->b_edev = 0;
10133 bp->b_dev = 0;
10134 bp->b_lblkno = lbtodb(io_off);
10135 bp->b_file = vp;
10136 bp->b_offset = (offset_t)blkoff;
10137 bp_mapin(bp);
10140 * If doing a write beyond what we believe is EOF, don't bother trying
10141 * to read the pages from the server, we'll just zero the pages here.
10142 * We don't check that the rw flag is S_WRITE here because some
10143 * implementations may attempt a read access to the buffer before
10144 * copying data.
10146 mutex_enter(&rp->r_statelock);
10147 if (io_off >= rp->r_size && seg == segkmap) {
10148 mutex_exit(&rp->r_statelock);
10149 bzero(bp->b_un.b_addr, io_len);
10150 error = 0;
10151 } else {
10152 mutex_exit(&rp->r_statelock);
10153 error = nfs4_bio(bp, NULL, cr, TRUE);
10154 if (error == NFS_EOF)
10155 error = 0;
10159 * Unmap the buffer before freeing it.
10161 bp_mapout(bp);
10162 pageio_done(bp);
10164 savepp = pp;
10165 do {
10166 pp->p_fsdata = C_NOCOMMIT;
10167 } while ((pp = pp->p_next) != savepp);
10169 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10172 * In case of error set readahead offset
10173 * to the lowest offset.
10174 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10176 if (error && rp->r_nextr > io_off) {
10177 mutex_enter(&rp->r_statelock);
10178 if (rp->r_nextr > io_off)
10179 rp->r_nextr = io_off;
10180 mutex_exit(&rp->r_statelock);
10185 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10186 * If len == 0, do from off to EOF.
10188 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10189 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10190 * (from pageout).
10192 /* ARGSUSED */
10193 static int
10194 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10195 caller_context_t *ct)
10197 int error;
10198 rnode4_t *rp;
10200 ASSERT(cr != NULL);
10202 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10203 return (EIO);
10205 rp = VTOR4(vp);
10206 if (IS_SHADOW(vp, rp))
10207 vp = RTOV4(rp);
10210 * XXX - Why should this check be made here?
10212 if (vp->v_flag & VNOMAP)
10213 return (ENOSYS);
10215 if (len == 0 && !(flags & B_INVAL) &&
10216 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10217 return (0);
10219 mutex_enter(&rp->r_statelock);
10220 rp->r_count++;
10221 mutex_exit(&rp->r_statelock);
10222 error = nfs4_putpages(vp, off, len, flags, cr);
10223 mutex_enter(&rp->r_statelock);
10224 rp->r_count--;
10225 cv_broadcast(&rp->r_cv);
10226 mutex_exit(&rp->r_statelock);
10228 return (error);
10232 * Write out a single page, possibly klustering adjacent dirty pages.
10235 nfs4_putapage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
10236 int flags, cred_t *cr)
10238 uoff_t io_off;
10239 uoff_t lbn_off;
10240 uoff_t lbn;
10241 size_t io_len;
10242 uint_t bsize;
10243 int error;
10244 rnode4_t *rp;
10246 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10247 ASSERT(pp != NULL);
10248 ASSERT(cr != NULL);
10249 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10251 rp = VTOR4(vp);
10252 ASSERT(rp->r_count > 0);
10253 ASSERT(!IS_SHADOW(vp, rp));
10255 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10256 lbn = pp->p_offset / bsize;
10257 lbn_off = lbn * bsize;
10260 * Find a kluster that fits in one block, or in
10261 * one page if pages are bigger than blocks. If
10262 * there is less file space allocated than a whole
10263 * page, we'll shorten the i/o request below.
10265 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10266 roundup(bsize, PAGESIZE), flags);
10269 * pvn_write_kluster shouldn't have returned a page with offset
10270 * behind the original page we were given. Verify that.
10272 ASSERT((pp->p_offset / bsize) >= lbn);
10275 * Now pp will have the list of kept dirty pages marked for
10276 * write back. It will also handle invalidation and freeing
10277 * of pages that are not dirty. Check for page length rounding
10278 * problems.
10280 if (io_off + io_len > lbn_off + bsize) {
10281 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10282 io_len = lbn_off + bsize - io_off;
10285 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10286 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10287 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10288 * progress and the r_size has not been made consistent with the
10289 * new size of the file. When the uiomove() completes the r_size is
10290 * updated and the R4MODINPROGRESS flag is cleared.
10292 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10293 * consistent value of r_size. Without this handshaking, it is
10294 * possible that nfs4_bio() picks up the old value of r_size
10295 * before the uiomove() in writerp4() completes. This will result
10296 * in the write through nfs4_bio() being dropped.
10298 * More precisely, there is a window between the time the uiomove()
10299 * completes and the time the r_size is updated. If a fop_putpage()
10300 * operation intervenes in this window, the page will be picked up,
10301 * because it is dirty (it will be unlocked, unless it was
10302 * pagecreate'd). When the page is picked up as dirty, the dirty
10303 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10304 * checked. This will still be the old size. Therefore the page will
10305 * not be written out. When segmap_release() calls fop_putpage(),
10306 * the page will be found to be clean and the write will be dropped.
10308 if (rp->r_flags & R4MODINPROGRESS) {
10309 mutex_enter(&rp->r_statelock);
10310 if ((rp->r_flags & R4MODINPROGRESS) &&
10311 rp->r_modaddr + MAXBSIZE > io_off &&
10312 rp->r_modaddr < io_off + io_len) {
10313 page_t *plist;
10315 * A write is in progress for this region of the file.
10316 * If we did not detect R4MODINPROGRESS here then this
10317 * path through nfs_putapage() would eventually go to
10318 * nfs4_bio() and may not write out all of the data
10319 * in the pages. We end up losing data. So we decide
10320 * to set the modified bit on each page in the page
10321 * list and mark the rnode with R4DIRTY. This write
10322 * will be restarted at some later time.
10324 plist = pp;
10325 while (plist != NULL) {
10326 pp = plist;
10327 page_sub(&plist, pp);
10328 hat_setmod(pp);
10329 page_io_unlock(pp);
10330 page_unlock(pp);
10332 rp->r_flags |= R4DIRTY;
10333 mutex_exit(&rp->r_statelock);
10334 if (offp)
10335 *offp = io_off;
10336 if (lenp)
10337 *lenp = io_len;
10338 return (0);
10340 mutex_exit(&rp->r_statelock);
10343 if (flags & B_ASYNC) {
10344 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10345 nfs4_sync_putapage);
10346 } else
10347 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10349 if (offp)
10350 *offp = io_off;
10351 if (lenp)
10352 *lenp = io_len;
10353 return (error);
10356 static int
10357 nfs4_sync_putapage(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
10358 int flags, cred_t *cr)
10360 int error;
10361 rnode4_t *rp;
10363 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10365 flags |= B_WRITE;
10367 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10369 rp = VTOR4(vp);
10371 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10372 error == EACCES) &&
10373 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10374 if (!(rp->r_flags & R4OUTOFSPACE)) {
10375 mutex_enter(&rp->r_statelock);
10376 rp->r_flags |= R4OUTOFSPACE;
10377 mutex_exit(&rp->r_statelock);
10379 flags |= B_ERROR;
10380 pvn_write_done(pp, flags);
10382 * If this was not an async thread, then try again to
10383 * write out the pages, but this time, also destroy
10384 * them whether or not the write is successful. This
10385 * will prevent memory from filling up with these
10386 * pages and destroying them is the only alternative
10387 * if they can't be written out.
10389 * Don't do this if this is an async thread because
10390 * when the pages are unlocked in pvn_write_done,
10391 * some other thread could have come along, locked
10392 * them, and queued for an async thread. It would be
10393 * possible for all of the async threads to be tied
10394 * up waiting to lock the pages again and they would
10395 * all already be locked and waiting for an async
10396 * thread to handle them. Deadlock.
10398 if (!(flags & B_ASYNC)) {
10399 error = nfs4_putpage(vp, io_off, io_len,
10400 B_INVAL | B_FORCE, cr, NULL);
10402 } else {
10403 if (error)
10404 flags |= B_ERROR;
10405 else if (rp->r_flags & R4OUTOFSPACE) {
10406 mutex_enter(&rp->r_statelock);
10407 rp->r_flags &= ~R4OUTOFSPACE;
10408 mutex_exit(&rp->r_statelock);
10410 pvn_write_done(pp, flags);
10411 if (freemem < desfree)
10412 (void) nfs4_commit_vp(vp, 0, 0, cr,
10413 NFS4_WRITE_NOWAIT);
10416 return (error);
10419 #ifdef DEBUG
10420 int nfs4_force_open_before_mmap = 0;
10421 #endif
10423 /* ARGSUSED */
10424 static int
10425 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10426 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10427 caller_context_t *ct)
10429 struct segvn_crargs vn_a;
10430 int error = 0;
10431 rnode4_t *rp = VTOR4(vp);
10432 mntinfo4_t *mi = VTOMI4(vp);
10434 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10435 return (EIO);
10437 if (vp->v_flag & VNOMAP)
10438 return (ENOSYS);
10440 if (off < 0 || (off + len) < 0)
10441 return (ENXIO);
10443 if (vp->v_type != VREG)
10444 return (ENODEV);
10447 * If the file is delegated to the client don't do anything.
10448 * If the file is not delegated, then validate the data cache.
10450 mutex_enter(&rp->r_statev4_lock);
10451 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10452 mutex_exit(&rp->r_statev4_lock);
10453 error = nfs4_validate_caches(vp, cr);
10454 if (error)
10455 return (error);
10456 } else {
10457 mutex_exit(&rp->r_statev4_lock);
10461 * Check to see if the vnode is currently marked as not cachable.
10462 * This means portions of the file are locked (through fop_frlock).
10463 * In this case the map request must be refused. We use
10464 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10466 * Atomically increment r_inmap after acquiring r_rwlock. The
10467 * idea here is to acquire r_rwlock to block read/write and
10468 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10469 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10470 * and we can prevent the deadlock that would have occurred
10471 * when nfs4_addmap() would have acquired it out of order.
10473 * Since we are not protecting r_inmap by any lock, we do not
10474 * hold any lock when we decrement it. We atomically decrement
10475 * r_inmap after we release r_lkserlock.
10478 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10479 return (EINTR);
10480 atomic_inc_uint(&rp->r_inmap);
10481 nfs_rw_exit(&rp->r_rwlock);
10483 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10484 atomic_dec_uint(&rp->r_inmap);
10485 return (EINTR);
10488 if (vp->v_flag & VNOCACHE) {
10489 error = EAGAIN;
10490 goto done;
10494 * Don't allow concurrent locks and mapping if mandatory locking is
10495 * enabled.
10497 if (flk_has_remote_locks(vp)) {
10498 struct vattr va;
10499 va.va_mask = AT_MODE;
10500 error = nfs4getattr(vp, &va, cr);
10501 if (error != 0)
10502 goto done;
10503 if (MANDLOCK(vp, va.va_mode)) {
10504 error = EAGAIN;
10505 goto done;
10510 * It is possible that the rnode has a lost lock request that we
10511 * are still trying to recover, and that the request conflicts with
10512 * this map request.
10514 * An alternative approach would be for nfs4_safemap() to consider
10515 * queued lock requests when deciding whether to set or clear
10516 * VNOCACHE. This would require the frlock code path to call
10517 * nfs4_safemap() after enqueing a lost request.
10519 if (nfs4_map_lost_lock_conflict(vp)) {
10520 error = EAGAIN;
10521 goto done;
10524 as_rangelock(as);
10525 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10526 if (error != 0) {
10527 as_rangeunlock(as);
10528 goto done;
10531 if (vp->v_type == VREG) {
10533 * We need to retrieve the open stream
10535 nfs4_open_stream_t *osp = NULL;
10536 nfs4_open_owner_t *oop = NULL;
10538 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10539 if (oop != NULL) {
10540 /* returns with 'os_sync_lock' held */
10541 osp = find_open_stream(oop, rp);
10542 open_owner_rele(oop);
10544 if (osp == NULL) {
10545 #ifdef DEBUG
10546 if (nfs4_force_open_before_mmap) {
10547 error = EIO;
10548 goto done;
10550 #endif
10551 /* returns with 'os_sync_lock' held */
10552 error = open_and_get_osp(vp, cr, &osp);
10553 if (osp == NULL) {
10554 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10555 "nfs4_map: we tried to OPEN the file "
10556 "but again no osp, so fail with EIO"));
10557 goto done;
10561 if (osp->os_failed_reopen) {
10562 mutex_exit(&osp->os_sync_lock);
10563 open_stream_rele(osp, rp);
10564 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10565 "nfs4_map: os_failed_reopen set on "
10566 "osp %p, cr %p, rp %s", (void *)osp,
10567 (void *)cr, rnode4info(rp)));
10568 error = EIO;
10569 goto done;
10571 mutex_exit(&osp->os_sync_lock);
10572 open_stream_rele(osp, rp);
10575 vn_a.vp = vp;
10576 vn_a.offset = off;
10577 vn_a.type = (flags & MAP_TYPE);
10578 vn_a.prot = (uchar_t)prot;
10579 vn_a.maxprot = (uchar_t)maxprot;
10580 vn_a.flags = (flags & ~MAP_TYPE);
10581 vn_a.cred = cr;
10582 vn_a.amp = NULL;
10583 vn_a.szc = 0;
10584 vn_a.lgrp_mem_policy_flags = 0;
10586 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10587 as_rangeunlock(as);
10589 done:
10590 nfs_rw_exit(&rp->r_lkserlock);
10591 atomic_dec_uint(&rp->r_inmap);
10592 return (error);
10596 * We're most likely dealing with a kernel module that likes to READ
10597 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10598 * officially OPEN the file to create the necessary client state
10599 * for bookkeeping of os_mmap_read/write counts.
10601 * Since fop_map only passes in a pointer to the vnode rather than
10602 * a double pointer, we can't handle the case where nfs4open_otw()
10603 * returns a different vnode than the one passed into fop_map (since
10604 * fop_delmap will not see the vnode nfs4open_otw used). In this case,
10605 * we return NULL and let nfs4_map() fail. Note: the only case where
10606 * this should happen is if the file got removed and replaced with the
10607 * same name on the server (in addition to the fact that we're trying
10608 * to fop_map withouth fop_opening the file in the first place).
10610 static int
10611 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10613 rnode4_t *rp, *drp;
10614 vnode_t *dvp, *open_vp;
10615 char file_name[MAXNAMELEN];
10616 int just_created;
10617 nfs4_open_stream_t *osp;
10618 nfs4_open_owner_t *oop;
10619 int error;
10621 *ospp = NULL;
10622 open_vp = map_vp;
10624 rp = VTOR4(open_vp);
10625 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10626 return (error);
10627 drp = VTOR4(dvp);
10629 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10630 VN_RELE(dvp);
10631 return (EINTR);
10634 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10635 nfs_rw_exit(&drp->r_rwlock);
10636 VN_RELE(dvp);
10637 return (error);
10640 mutex_enter(&rp->r_statev4_lock);
10641 if (rp->created_v4) {
10642 rp->created_v4 = 0;
10643 mutex_exit(&rp->r_statev4_lock);
10645 dnlc_update(dvp, file_name, open_vp);
10646 /* This is needed so we don't bump the open ref count */
10647 just_created = 1;
10648 } else {
10649 mutex_exit(&rp->r_statev4_lock);
10650 just_created = 0;
10653 VN_HOLD(map_vp);
10655 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10656 just_created);
10657 if (error) {
10658 nfs_rw_exit(&drp->r_rwlock);
10659 VN_RELE(dvp);
10660 VN_RELE(map_vp);
10661 return (error);
10664 nfs_rw_exit(&drp->r_rwlock);
10665 VN_RELE(dvp);
10668 * If nfs4open_otw() returned a different vnode then "undo"
10669 * the open and return failure to the caller.
10671 if (!VN_CMP(open_vp, map_vp)) {
10672 nfs4_error_t e;
10674 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10675 "open returned a different vnode"));
10677 * If there's an error, ignore it,
10678 * and let fop_inactive handle it.
10680 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10681 CLOSE_NORM, 0, 0, 0);
10682 VN_RELE(map_vp);
10683 return (EIO);
10686 VN_RELE(map_vp);
10688 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10689 if (!oop) {
10690 nfs4_error_t e;
10692 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10693 "no open owner"));
10695 * If there's an error, ignore it,
10696 * and let fop_inactive handle it.
10698 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10699 CLOSE_NORM, 0, 0, 0);
10700 return (EIO);
10702 osp = find_open_stream(oop, rp);
10703 open_owner_rele(oop);
10704 *ospp = osp;
10705 return (0);
10709 * Please be aware that when this function is called, the address space write
10710 * a_lock is held. Do not put over the wire calls in this function.
10712 /* ARGSUSED */
10713 static int
10714 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10715 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10716 caller_context_t *ct)
10718 rnode4_t *rp;
10719 int error = 0;
10720 mntinfo4_t *mi;
10722 mi = VTOMI4(vp);
10723 rp = VTOR4(vp);
10725 if (nfs_zone() != mi->mi_zone)
10726 return (EIO);
10727 if (vp->v_flag & VNOMAP)
10728 return (ENOSYS);
10731 * Don't need to update the open stream first, since this
10732 * mmap can't add any additional share access that isn't
10733 * already contained in the open stream (for the case where we
10734 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10735 * take into account os_mmap_read[write] counts).
10737 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10739 if (vp->v_type == VREG) {
10741 * We need to retrieve the open stream and update the counts.
10742 * If there is no open stream here, something is wrong.
10744 nfs4_open_stream_t *osp = NULL;
10745 nfs4_open_owner_t *oop = NULL;
10747 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10748 if (oop != NULL) {
10749 /* returns with 'os_sync_lock' held */
10750 osp = find_open_stream(oop, rp);
10751 open_owner_rele(oop);
10753 if (osp == NULL) {
10754 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10755 "nfs4_addmap: we should have an osp"
10756 "but we don't, so fail with EIO"));
10757 error = EIO;
10758 goto out;
10761 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10762 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10765 * Update the map count in the open stream.
10766 * This is necessary in the case where we
10767 * open/mmap/close/, then the server reboots, and we
10768 * attempt to reopen. If the mmap doesn't add share
10769 * access then we send an invalid reopen with
10770 * access = NONE.
10772 * We need to specifically check each PROT_* so a mmap
10773 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10774 * read and write access. A simple comparison of prot
10775 * to ~PROT_WRITE to determine read access is insufficient
10776 * since prot can be |= with PROT_USER, etc.
10780 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10782 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10783 osp->os_mmap_write += btopr(len);
10784 if (maxprot & PROT_READ)
10785 osp->os_mmap_read += btopr(len);
10786 if (maxprot & PROT_EXEC)
10787 osp->os_mmap_read += btopr(len);
10789 * Ensure that os_mmap_read gets incremented, even if
10790 * maxprot were to look like PROT_NONE.
10792 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10793 !(maxprot & PROT_EXEC))
10794 osp->os_mmap_read += btopr(len);
10795 osp->os_mapcnt += btopr(len);
10796 mutex_exit(&osp->os_sync_lock);
10797 open_stream_rele(osp, rp);
10800 out:
10802 * If we got an error, then undo our
10803 * incrementing of 'r_mapcnt'.
10806 if (error) {
10807 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10808 ASSERT(rp->r_mapcnt >= 0);
10810 return (error);
10813 /* ARGSUSED */
10814 static int
10815 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10818 return (VTOR4(vp1) == VTOR4(vp2));
10821 /* ARGSUSED */
10822 static int
10823 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10824 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10825 caller_context_t *ct)
10827 int rc;
10828 uoff_t start, end;
10829 rnode4_t *rp;
10830 int error = 0, intr = INTR4(vp);
10831 nfs4_error_t e;
10833 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10834 return (EIO);
10836 /* check for valid cmd parameter */
10837 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10838 return (EINVAL);
10840 /* Verify l_type. */
10841 switch (bfp->l_type) {
10842 case F_RDLCK:
10843 if (cmd != F_GETLK && !(flag & FREAD))
10844 return (EBADF);
10845 break;
10846 case F_WRLCK:
10847 if (cmd != F_GETLK && !(flag & FWRITE))
10848 return (EBADF);
10849 break;
10850 case F_UNLCK:
10851 intr = 0;
10852 break;
10854 default:
10855 return (EINVAL);
10858 /* check the validity of the lock range */
10859 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10860 return (rc);
10861 if (rc = flk_check_lock_data(start, end, MAXEND))
10862 return (rc);
10865 * If the filesystem is mounted using local locking, pass the
10866 * request off to the local locking code.
10868 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10869 if (cmd == F_SETLK || cmd == F_SETLKW) {
10871 * For complete safety, we should be holding
10872 * r_lkserlock. However, we can't call
10873 * nfs4_safelock and then fs_frlock while
10874 * holding r_lkserlock, so just invoke
10875 * nfs4_safelock and expect that this will
10876 * catch enough of the cases.
10878 if (!nfs4_safelock(vp, bfp, cr))
10879 return (EAGAIN);
10881 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10884 rp = VTOR4(vp);
10887 * Check whether the given lock request can proceed, given the
10888 * current file mappings.
10890 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10891 return (EINTR);
10892 if (cmd == F_SETLK || cmd == F_SETLKW) {
10893 if (!nfs4_safelock(vp, bfp, cr)) {
10894 rc = EAGAIN;
10895 goto done;
10900 * Flush the cache after waiting for async I/O to finish. For new
10901 * locks, this is so that the process gets the latest bits from the
10902 * server. For unlocks, this is so that other clients see the
10903 * latest bits once the file has been unlocked. If currently dirty
10904 * pages can't be flushed, then don't allow a lock to be set. But
10905 * allow unlocks to succeed, to avoid having orphan locks on the
10906 * server.
10908 if (cmd != F_GETLK) {
10909 mutex_enter(&rp->r_statelock);
10910 while (rp->r_count > 0) {
10911 if (intr) {
10912 klwp_t *lwp = ttolwp(curthread);
10914 if (lwp != NULL)
10915 lwp->lwp_nostop++;
10916 if (cv_wait_sig(&rp->r_cv,
10917 &rp->r_statelock) == 0) {
10918 if (lwp != NULL)
10919 lwp->lwp_nostop--;
10920 rc = EINTR;
10921 break;
10923 if (lwp != NULL)
10924 lwp->lwp_nostop--;
10925 } else {
10926 cv_wait(&rp->r_cv, &rp->r_statelock);
10929 mutex_exit(&rp->r_statelock);
10930 if (rc != 0)
10931 goto done;
10932 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10933 if (error) {
10934 if (error == ENOSPC || error == EDQUOT) {
10935 mutex_enter(&rp->r_statelock);
10936 if (!rp->r_error)
10937 rp->r_error = error;
10938 mutex_exit(&rp->r_statelock);
10940 if (bfp->l_type != F_UNLCK) {
10941 rc = ENOLCK;
10942 goto done;
10948 * Call the lock manager to do the real work of contacting
10949 * the server and obtaining the lock.
10951 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10952 cr, &e, NULL, NULL);
10953 rc = e.error;
10955 if (rc == 0)
10956 nfs4_lockcompletion(vp, cmd);
10958 done:
10959 nfs_rw_exit(&rp->r_lkserlock);
10961 return (rc);
10965 * Free storage space associated with the specified vnode. The portion
10966 * to be freed is specified by bfp->l_start and bfp->l_len (already
10967 * normalized to a "whence" of 0).
10969 * This is an experimental facility whose continued existence is not
10970 * guaranteed. Currently, we only support the special case
10971 * of l_len == 0, meaning free to end of file.
10973 /* ARGSUSED */
10974 static int
10975 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10976 offset_t offset, cred_t *cr, caller_context_t *ct)
10978 int error;
10980 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10981 return (EIO);
10982 ASSERT(vp->v_type == VREG);
10983 if (cmd != F_FREESP)
10984 return (EINVAL);
10986 error = convoff(vp, bfp, 0, offset);
10987 if (!error) {
10988 ASSERT(bfp->l_start >= 0);
10989 if (bfp->l_len == 0) {
10990 struct vattr va;
10992 va.va_mask = AT_SIZE;
10993 va.va_size = bfp->l_start;
10994 error = nfs4setattr(vp, &va, 0, cr, NULL);
10996 if (error == 0 && bfp->l_start == 0)
10997 vnevent_truncate(vp, ct);
10998 } else
10999 error = EINVAL;
11002 return (error);
11005 /* ARGSUSED */
11007 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11009 rnode4_t *rp;
11010 rp = VTOR4(vp);
11012 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11013 vp = RTOV4(rp);
11015 *vpp = vp;
11016 return (0);
11020 * Setup and add an address space callback to do the work of the delmap call.
11021 * The callback will (and must be) deleted in the actual callback function.
11023 * This is done in order to take care of the problem that we have with holding
11024 * the address space's a_lock for a long period of time (e.g. if the NFS server
11025 * is down). Callbacks will be executed in the address space code while the
11026 * a_lock is not held. Holding the address space's a_lock causes things such
11027 * as ps and fork to hang because they are trying to acquire this lock as well.
11029 /* ARGSUSED */
11030 static int
11031 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11032 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11033 caller_context_t *ct)
11035 int caller_found;
11036 int error;
11037 rnode4_t *rp;
11038 nfs4_delmap_args_t *dmapp;
11039 nfs4_delmapcall_t *delmap_call;
11041 if (vp->v_flag & VNOMAP)
11042 return (ENOSYS);
11045 * A process may not change zones if it has NFS pages mmap'ed
11046 * in, so we can't legitimately get here from the wrong zone.
11048 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11050 rp = VTOR4(vp);
11053 * The way that the address space of this process deletes its mapping
11054 * of this file is via the following call chains:
11055 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11056 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11058 * With the use of address space callbacks we are allowed to drop the
11059 * address space lock, a_lock, while executing the NFS operations that
11060 * need to go over the wire. Returning EAGAIN to the caller of this
11061 * function is what drives the execution of the callback that we add
11062 * below. The callback will be executed by the address space code
11063 * after dropping the a_lock. When the callback is finished, since
11064 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11065 * is called again on the same segment to finish the rest of the work
11066 * that needs to happen during unmapping.
11068 * This action of calling back into the segment driver causes
11069 * nfs4_delmap() to get called again, but since the callback was
11070 * already executed at this point, it already did the work and there
11071 * is nothing left for us to do.
11073 * To Summarize:
11074 * - The first time nfs4_delmap is called by the current thread is when
11075 * we add the caller associated with this delmap to the delmap caller
11076 * list, add the callback, and return EAGAIN.
11077 * - The second time in this call chain when nfs4_delmap is called we
11078 * will find this caller in the delmap caller list and realize there
11079 * is no more work to do thus removing this caller from the list and
11080 * returning the error that was set in the callback execution.
11082 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11083 if (caller_found) {
11085 * 'error' is from the actual delmap operations. To avoid
11086 * hangs, we need to handle the return of EAGAIN differently
11087 * since this is what drives the callback execution.
11088 * In this case, we don't want to return EAGAIN and do the
11089 * callback execution because there are none to execute.
11091 if (error == EAGAIN)
11092 return (0);
11093 else
11094 return (error);
11097 /* current caller was not in the list */
11098 delmap_call = nfs4_init_delmapcall();
11100 mutex_enter(&rp->r_statelock);
11101 list_insert_tail(&rp->r_indelmap, delmap_call);
11102 mutex_exit(&rp->r_statelock);
11104 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11106 dmapp->vp = vp;
11107 dmapp->off = off;
11108 dmapp->addr = addr;
11109 dmapp->len = len;
11110 dmapp->prot = prot;
11111 dmapp->maxprot = maxprot;
11112 dmapp->flags = flags;
11113 dmapp->cr = cr;
11114 dmapp->caller = delmap_call;
11116 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11117 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11119 return (error ? error : EAGAIN);
11122 static nfs4_delmapcall_t *
11123 nfs4_init_delmapcall()
11125 nfs4_delmapcall_t *delmap_call;
11127 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11128 delmap_call->call_id = curthread;
11129 delmap_call->error = 0;
11131 return (delmap_call);
11134 static void
11135 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11137 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11141 * Searches for the current delmap caller (based on curthread) in the list of
11142 * callers. If it is found, we remove it and free the delmap caller.
11143 * Returns:
11144 * 0 if the caller wasn't found
11145 * 1 if the caller was found, removed and freed. *errp will be set
11146 * to what the result of the delmap was.
11148 static int
11149 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11151 nfs4_delmapcall_t *delmap_call;
11154 * If the list doesn't exist yet, we create it and return
11155 * that the caller wasn't found. No list = no callers.
11157 mutex_enter(&rp->r_statelock);
11158 if (!(rp->r_flags & R4DELMAPLIST)) {
11159 /* The list does not exist */
11160 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11161 offsetof(nfs4_delmapcall_t, call_node));
11162 rp->r_flags |= R4DELMAPLIST;
11163 mutex_exit(&rp->r_statelock);
11164 return (0);
11165 } else {
11166 /* The list exists so search it */
11167 for (delmap_call = list_head(&rp->r_indelmap);
11168 delmap_call != NULL;
11169 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11170 if (delmap_call->call_id == curthread) {
11171 /* current caller is in the list */
11172 *errp = delmap_call->error;
11173 list_remove(&rp->r_indelmap, delmap_call);
11174 mutex_exit(&rp->r_statelock);
11175 nfs4_free_delmapcall(delmap_call);
11176 return (1);
11180 mutex_exit(&rp->r_statelock);
11181 return (0);
11185 * Remove some pages from an mmap'd vnode. Just update the
11186 * count of pages. If doing close-to-open, then flush and
11187 * commit all of the pages associated with this file.
11188 * Otherwise, start an asynchronous page flush to write out
11189 * any dirty pages. This will also associate a credential
11190 * with the rnode which can be used to write the pages.
11192 /* ARGSUSED */
11193 static void
11194 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11196 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11197 rnode4_t *rp;
11198 mntinfo4_t *mi;
11199 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11201 rp = VTOR4(dmapp->vp);
11202 mi = VTOMI4(dmapp->vp);
11204 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11205 ASSERT(rp->r_mapcnt >= 0);
11208 * Initiate a page flush and potential commit if there are
11209 * pages, the file system was not mounted readonly, the segment
11210 * was mapped shared, and the pages themselves were writeable.
11212 if (nfs4_has_pages(dmapp->vp) &&
11213 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11214 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11215 mutex_enter(&rp->r_statelock);
11216 rp->r_flags |= R4DIRTY;
11217 mutex_exit(&rp->r_statelock);
11218 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11219 dmapp->len, dmapp->cr);
11220 if (!e.error) {
11221 mutex_enter(&rp->r_statelock);
11222 e.error = rp->r_error;
11223 rp->r_error = 0;
11224 mutex_exit(&rp->r_statelock);
11226 } else
11227 e.error = 0;
11229 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11230 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11231 B_INVAL, dmapp->cr, NULL);
11233 if (e.error) {
11234 e.stat = puterrno4(e.error);
11235 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11236 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11237 dmapp->caller->error = e.error;
11240 /* Check to see if we need to close the file */
11242 if (dmapp->vp->v_type == VREG) {
11243 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11244 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11246 if (e.error != 0 || e.stat != NFS4_OK) {
11248 * Since it is possible that e.error == 0 and
11249 * e.stat != NFS4_OK (and vice versa),
11250 * we do the proper checking in order to get both
11251 * e.error and e.stat reporting the correct info.
11253 if (e.stat == NFS4_OK)
11254 e.stat = puterrno4(e.error);
11255 if (e.error == 0)
11256 e.error = geterrno4(e.stat);
11258 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11259 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11260 dmapp->caller->error = e.error;
11264 (void) as_delete_callback(as, arg);
11265 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11269 static uint_t
11270 fattr4_maxfilesize_to_bits(uint64_t ll)
11272 uint_t l = 1;
11274 if (ll == 0) {
11275 return (0);
11278 if (ll & 0xffffffff00000000) {
11279 l += 32; ll >>= 32;
11281 if (ll & 0xffff0000) {
11282 l += 16; ll >>= 16;
11284 if (ll & 0xff00) {
11285 l += 8; ll >>= 8;
11287 if (ll & 0xf0) {
11288 l += 4; ll >>= 4;
11290 if (ll & 0xc) {
11291 l += 2; ll >>= 2;
11293 if (ll & 0x2) {
11294 l += 1;
11296 return (l);
11299 static int
11300 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11302 vnode_t *avp = NULL;
11303 int error;
11305 if ((error = nfs4lookup_xattr(vp, "", &avp,
11306 LOOKUP_XATTR, cr)) == 0)
11307 error = do_xattr_exists_check(avp, valp, cr);
11308 if (avp)
11309 VN_RELE(avp);
11311 return (error);
11314 /* ARGSUSED */
11316 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11317 caller_context_t *ct)
11319 int error;
11320 hrtime_t t;
11321 rnode4_t *rp;
11322 nfs4_ga_res_t gar;
11323 nfs4_ga_ext_res_t ger;
11325 gar.n4g_ext_res = &ger;
11327 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11328 return (EIO);
11329 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11330 *valp = MAXPATHLEN;
11331 return (0);
11333 if (cmd == _PC_ACL_ENABLED) {
11334 *valp = _ACL_ACE_ENABLED;
11335 return (0);
11338 rp = VTOR4(vp);
11339 if (cmd == _PC_XATTR_EXISTS) {
11341 * The existence of the xattr directory is not sufficient
11342 * for determining whether generic user attributes exists.
11343 * The attribute directory could only be a transient directory
11344 * used for Solaris sysattr support. Do a small readdir
11345 * to verify if the only entries are sysattrs or not.
11347 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11348 * is NULL. Once the xadir vp exists, we can create xattrs,
11349 * and we don't have any way to update the "base" object's
11350 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11351 * could help out.
11353 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11354 rp->r_xattr_dir == NULL) {
11355 return (nfs4_have_xattrs(vp, valp, cr));
11357 } else { /* OLD CODE */
11358 if (ATTRCACHE4_VALID(vp)) {
11359 mutex_enter(&rp->r_statelock);
11360 if (rp->r_pathconf.pc4_cache_valid) {
11361 error = 0;
11362 switch (cmd) {
11363 case _PC_FILESIZEBITS:
11364 *valp =
11365 rp->r_pathconf.pc4_filesizebits;
11366 break;
11367 case _PC_LINK_MAX:
11368 *valp =
11369 rp->r_pathconf.pc4_link_max;
11370 break;
11371 case _PC_NAME_MAX:
11372 *valp =
11373 rp->r_pathconf.pc4_name_max;
11374 break;
11375 case _PC_CHOWN_RESTRICTED:
11376 *valp =
11377 rp->r_pathconf.pc4_chown_restricted;
11378 break;
11379 case _PC_NO_TRUNC:
11380 *valp =
11381 rp->r_pathconf.pc4_no_trunc;
11382 break;
11383 default:
11384 error = EINVAL;
11385 break;
11387 mutex_exit(&rp->r_statelock);
11388 #ifdef DEBUG
11389 nfs4_pathconf_cache_hits++;
11390 #endif
11391 return (error);
11393 mutex_exit(&rp->r_statelock);
11396 #ifdef DEBUG
11397 nfs4_pathconf_cache_misses++;
11398 #endif
11400 t = gethrtime();
11402 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11404 if (error) {
11405 mutex_enter(&rp->r_statelock);
11406 rp->r_pathconf.pc4_cache_valid = FALSE;
11407 rp->r_pathconf.pc4_xattr_valid = FALSE;
11408 mutex_exit(&rp->r_statelock);
11409 return (error);
11412 /* interpret the max filesize */
11413 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11414 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11416 /* Store the attributes we just received */
11417 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11419 switch (cmd) {
11420 case _PC_FILESIZEBITS:
11421 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11422 break;
11423 case _PC_LINK_MAX:
11424 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11425 break;
11426 case _PC_NAME_MAX:
11427 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11428 break;
11429 case _PC_CHOWN_RESTRICTED:
11430 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11431 break;
11432 case _PC_NO_TRUNC:
11433 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11434 break;
11435 case _PC_XATTR_EXISTS:
11436 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11437 if (error = nfs4_have_xattrs(vp, valp, cr))
11438 return (error);
11440 break;
11441 default:
11442 return (EINVAL);
11445 return (0);
11449 * Called by async thread to do synchronous pageio. Do the i/o, wait
11450 * for it to complete, and cleanup the page list when done.
11452 static int
11453 nfs4_sync_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11454 int flags, cred_t *cr)
11456 int error;
11458 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11460 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11461 if (flags & B_READ)
11462 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11463 else
11464 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11465 return (error);
11468 /* ARGSUSED */
11469 static int
11470 nfs4_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11471 int flags, cred_t *cr, caller_context_t *ct)
11473 int error;
11474 rnode4_t *rp;
11476 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11477 return (EIO);
11479 if (pp == NULL)
11480 return (EINVAL);
11482 rp = VTOR4(vp);
11483 mutex_enter(&rp->r_statelock);
11484 rp->r_count++;
11485 mutex_exit(&rp->r_statelock);
11487 if (flags & B_ASYNC) {
11488 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11489 nfs4_sync_pageio);
11490 } else
11491 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11492 mutex_enter(&rp->r_statelock);
11493 rp->r_count--;
11494 cv_broadcast(&rp->r_cv);
11495 mutex_exit(&rp->r_statelock);
11496 return (error);
11499 /* ARGSUSED */
11500 static void
11501 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11502 caller_context_t *ct)
11504 int error;
11505 rnode4_t *rp;
11506 page_t *plist;
11507 page_t *pptr;
11508 offset3 offset;
11509 count3 len;
11510 k_sigset_t smask;
11513 * We should get called with fl equal to either B_FREE or
11514 * B_INVAL. Any other value is illegal.
11516 * The page that we are either supposed to free or destroy
11517 * should be exclusive locked and its io lock should not
11518 * be held.
11520 ASSERT(fl == B_FREE || fl == B_INVAL);
11521 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11523 rp = VTOR4(vp);
11526 * If the page doesn't need to be committed or we shouldn't
11527 * even bother attempting to commit it, then just make sure
11528 * that the p_fsdata byte is clear and then either free or
11529 * destroy the page as appropriate.
11531 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11532 pp->p_fsdata = C_NOCOMMIT;
11533 if (fl == B_FREE)
11534 page_free(pp, dn);
11535 else
11536 page_destroy(pp, dn);
11537 return;
11541 * If there is a page invalidation operation going on, then
11542 * if this is one of the pages being destroyed, then just
11543 * clear the p_fsdata byte and then either free or destroy
11544 * the page as appropriate.
11546 mutex_enter(&rp->r_statelock);
11547 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11548 mutex_exit(&rp->r_statelock);
11549 pp->p_fsdata = C_NOCOMMIT;
11550 if (fl == B_FREE)
11551 page_free(pp, dn);
11552 else
11553 page_destroy(pp, dn);
11554 return;
11558 * If we are freeing this page and someone else is already
11559 * waiting to do a commit, then just unlock the page and
11560 * return. That other thread will take care of commiting
11561 * this page. The page can be freed sometime after the
11562 * commit has finished. Otherwise, if the page is marked
11563 * as delay commit, then we may be getting called from
11564 * pvn_write_done, one page at a time. This could result
11565 * in one commit per page, so we end up doing lots of small
11566 * commits instead of fewer larger commits. This is bad,
11567 * we want do as few commits as possible.
11569 if (fl == B_FREE) {
11570 if (rp->r_flags & R4COMMITWAIT) {
11571 page_unlock(pp);
11572 mutex_exit(&rp->r_statelock);
11573 return;
11575 if (pp->p_fsdata == C_DELAYCOMMIT) {
11576 pp->p_fsdata = C_COMMIT;
11577 page_unlock(pp);
11578 mutex_exit(&rp->r_statelock);
11579 return;
11584 * Check to see if there is a signal which would prevent an
11585 * attempt to commit the pages from being successful. If so,
11586 * then don't bother with all of the work to gather pages and
11587 * generate the unsuccessful RPC. Just return from here and
11588 * let the page be committed at some later time.
11590 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11591 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11592 sigunintr(&smask);
11593 page_unlock(pp);
11594 mutex_exit(&rp->r_statelock);
11595 return;
11597 sigunintr(&smask);
11600 * We are starting to need to commit pages, so let's try
11601 * to commit as many as possible at once to reduce the
11602 * overhead.
11604 * Set the `commit inprogress' state bit. We must
11605 * first wait until any current one finishes. Then
11606 * we initialize the c_pages list with this page.
11608 while (rp->r_flags & R4COMMIT) {
11609 rp->r_flags |= R4COMMITWAIT;
11610 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11611 rp->r_flags &= ~R4COMMITWAIT;
11613 rp->r_flags |= R4COMMIT;
11614 mutex_exit(&rp->r_statelock);
11615 ASSERT(rp->r_commit.c_pages == NULL);
11616 rp->r_commit.c_pages = pp;
11617 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11618 rp->r_commit.c_commlen = PAGESIZE;
11621 * Gather together all other pages which can be committed.
11622 * They will all be chained off r_commit.c_pages.
11624 nfs4_get_commit(vp);
11627 * Clear the `commit inprogress' status and disconnect
11628 * the list of pages to be committed from the rnode.
11629 * At this same time, we also save the starting offset
11630 * and length of data to be committed on the server.
11632 plist = rp->r_commit.c_pages;
11633 rp->r_commit.c_pages = NULL;
11634 offset = rp->r_commit.c_commbase;
11635 len = rp->r_commit.c_commlen;
11636 mutex_enter(&rp->r_statelock);
11637 rp->r_flags &= ~R4COMMIT;
11638 cv_broadcast(&rp->r_commit.c_cv);
11639 mutex_exit(&rp->r_statelock);
11641 if (curproc == proc_pageout || curproc == proc_fsflush ||
11642 nfs_zone() != VTOMI4(vp)->mi_zone) {
11643 nfs4_async_commit(vp, plist, offset, len,
11644 cr, do_nfs4_async_commit);
11645 return;
11649 * Actually generate the COMMIT op over the wire operation.
11651 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11654 * If we got an error during the commit, just unlock all
11655 * of the pages. The pages will get retransmitted to the
11656 * server during a putpage operation.
11658 if (error) {
11659 while (plist != NULL) {
11660 pptr = plist;
11661 page_sub(&plist, pptr);
11662 page_unlock(pptr);
11664 return;
11668 * We've tried as hard as we can to commit the data to stable
11669 * storage on the server. We just unlock the rest of the pages
11670 * and clear the commit required state. They will be put
11671 * onto the tail of the cachelist if they are nolonger
11672 * mapped.
11674 while (plist != pp) {
11675 pptr = plist;
11676 page_sub(&plist, pptr);
11677 pptr->p_fsdata = C_NOCOMMIT;
11678 page_unlock(pptr);
11682 * It is possible that nfs4_commit didn't return error but
11683 * some other thread has modified the page we are going
11684 * to free/destroy.
11685 * In this case we need to rewrite the page. Do an explicit check
11686 * before attempting to free/destroy the page. If modified, needs to
11687 * be rewritten so unlock the page and return.
11689 if (hat_ismod(pp)) {
11690 pp->p_fsdata = C_NOCOMMIT;
11691 page_unlock(pp);
11692 return;
11696 * Now, as appropriate, either free or destroy the page
11697 * that we were called with.
11699 pp->p_fsdata = C_NOCOMMIT;
11700 if (fl == B_FREE)
11701 page_free(pp, dn);
11702 else
11703 page_destroy(pp, dn);
11707 * Commit requires that the current fh be the file written to.
11708 * The compound op structure is:
11709 * PUTFH(file), COMMIT
11711 static int
11712 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11714 COMPOUND4args_clnt args;
11715 COMPOUND4res_clnt res;
11716 COMMIT4res *cm_res;
11717 nfs_argop4 argop[2];
11718 nfs_resop4 *resop;
11719 int doqueue;
11720 mntinfo4_t *mi;
11721 rnode4_t *rp;
11722 cred_t *cred_otw = NULL;
11723 bool_t needrecov = FALSE;
11724 nfs4_recov_state_t recov_state;
11725 nfs4_open_stream_t *osp = NULL;
11726 bool_t first_time = TRUE; /* first time getting OTW cred */
11727 bool_t last_time = FALSE; /* last time getting OTW cred */
11728 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11730 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11732 rp = VTOR4(vp);
11734 mi = VTOMI4(vp);
11735 recov_state.rs_flags = 0;
11736 recov_state.rs_num_retry_despite_err = 0;
11737 get_commit_cred:
11739 * Releases the osp, if a valid open stream is provided.
11740 * Puts a hold on the cred_otw and the new osp (if found).
11742 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11743 &first_time, &last_time);
11744 args.ctag = TAG_COMMIT;
11745 recov_retry:
11747 * Commit ops: putfh file; commit
11749 args.array_len = 2;
11750 args.array = argop;
11752 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11753 &recov_state, NULL);
11754 if (e.error) {
11755 crfree(cred_otw);
11756 if (osp != NULL)
11757 open_stream_rele(osp, rp);
11758 return (e.error);
11761 /* putfh directory */
11762 argop[0].argop = OP_CPUTFH;
11763 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11765 /* commit */
11766 argop[1].argop = OP_COMMIT;
11767 argop[1].nfs_argop4_u.opcommit.offset = offset;
11768 argop[1].nfs_argop4_u.opcommit.count = count;
11770 doqueue = 1;
11771 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11773 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11774 if (!needrecov && e.error) {
11775 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11776 needrecov);
11777 crfree(cred_otw);
11778 if (e.error == EACCES && last_time == FALSE)
11779 goto get_commit_cred;
11780 if (osp != NULL)
11781 open_stream_rele(osp, rp);
11782 return (e.error);
11785 if (needrecov) {
11786 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11787 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11788 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11789 &recov_state, needrecov);
11790 if (!e.error)
11791 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11792 goto recov_retry;
11794 if (e.error) {
11795 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11796 &recov_state, needrecov);
11797 crfree(cred_otw);
11798 if (osp != NULL)
11799 open_stream_rele(osp, rp);
11800 return (e.error);
11802 /* fall through for res.status case */
11805 if (res.status) {
11806 e.error = geterrno4(res.status);
11807 if (e.error == EACCES && last_time == FALSE) {
11808 crfree(cred_otw);
11809 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11810 &recov_state, needrecov);
11811 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11812 goto get_commit_cred;
11815 * Can't do a nfs4_purge_stale_fh here because this
11816 * can cause a deadlock. nfs4_commit can
11817 * be called from nfs4_dispose which can be called
11818 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11819 * can call back to pvn_vplist_dirty.
11821 if (e.error == ESTALE) {
11822 mutex_enter(&rp->r_statelock);
11823 rp->r_flags |= R4STALE;
11824 if (!rp->r_error)
11825 rp->r_error = e.error;
11826 mutex_exit(&rp->r_statelock);
11827 PURGE_ATTRCACHE4(vp);
11828 } else {
11829 mutex_enter(&rp->r_statelock);
11830 if (!rp->r_error)
11831 rp->r_error = e.error;
11832 mutex_exit(&rp->r_statelock);
11834 } else {
11835 ASSERT(rp->r_flags & R4HAVEVERF);
11836 resop = &res.array[1]; /* commit res */
11837 cm_res = &resop->nfs_resop4_u.opcommit;
11838 mutex_enter(&rp->r_statelock);
11839 if (cm_res->writeverf == rp->r_writeverf) {
11840 mutex_exit(&rp->r_statelock);
11841 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11842 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11843 &recov_state, needrecov);
11844 crfree(cred_otw);
11845 if (osp != NULL)
11846 open_stream_rele(osp, rp);
11847 return (0);
11849 nfs4_set_mod(vp);
11850 rp->r_writeverf = cm_res->writeverf;
11851 mutex_exit(&rp->r_statelock);
11852 e.error = NFS_VERF_MISMATCH;
11855 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11856 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11857 crfree(cred_otw);
11858 if (osp != NULL)
11859 open_stream_rele(osp, rp);
11861 return (e.error);
11864 static void
11865 nfs4_set_mod(vnode_t *vp)
11867 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11869 /* make sure we're looking at the master vnode, not a shadow */
11870 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11874 * This function is used to gather a page list of the pages which
11875 * can be committed on the server.
11877 * The calling thread must have set R4COMMIT. This bit is used to
11878 * serialize access to the commit structure in the rnode. As long
11879 * as the thread has set R4COMMIT, then it can manipulate the commit
11880 * structure without requiring any other locks.
11882 * When this function is called from nfs4_dispose() the page passed
11883 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11884 * will skip it. This is not a problem since we initially add the
11885 * page to the r_commit page list.
11888 static void
11889 nfs4_get_commit(vnode_t *vp)
11891 rnode4_t *rp;
11892 page_t *pp;
11894 rp = VTOR4(vp);
11896 ASSERT(rp->r_flags & R4COMMIT);
11898 /* make sure we're looking at the master vnode, not a shadow */
11900 if (IS_SHADOW(vp, rp))
11901 vp = RTOV4(rp);
11903 vmobject_lock(&vp->v_object);
11906 * Step through all of the pages associated with this vnode
11907 * looking for pages which need to be committed.
11909 for (pp = vmobject_get_head(&vp->v_object);
11910 pp != NULL;
11911 pp = vmobject_get_next(&vp->v_object, pp)) {
11912 /* Skip marker pages. */
11913 if (PP_ISPVN_TAG(pp))
11914 continue;
11917 * First short-cut everything (without the page_lock)
11918 * and see if this page does not need to be committed
11919 * or is modified if so then we'll just skip it.
11921 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11922 continue;
11925 * Attempt to lock the page. If we can't, then
11926 * someone else is messing with it or we have been
11927 * called from nfs4_dispose and this is the page that
11928 * nfs4_dispose was called with.. anyway just skip it.
11930 if (!page_trylock(pp, SE_EXCL))
11931 continue;
11934 * Lets check again now that we have the page lock.
11936 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11937 page_unlock(pp);
11938 continue;
11941 /* this had better not be a free page */
11942 ASSERT(PP_ISFREE(pp) == 0);
11945 * The page needs to be committed and we locked it.
11946 * Update the base and length parameters and add it
11947 * to r_pages.
11949 if (rp->r_commit.c_pages == NULL) {
11950 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11951 rp->r_commit.c_commlen = PAGESIZE;
11952 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11953 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11954 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11955 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11956 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11957 <= pp->p_offset) {
11958 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11959 rp->r_commit.c_commbase + PAGESIZE;
11961 page_add(&rp->r_commit.c_pages, pp);
11964 vmobject_unlock(&vp->v_object);
11968 * This routine is used to gather together a page list of the pages
11969 * which are to be committed on the server. This routine must not
11970 * be called if the calling thread holds any locked pages.
11972 * The calling thread must have set R4COMMIT. This bit is used to
11973 * serialize access to the commit structure in the rnode. As long
11974 * as the thread has set R4COMMIT, then it can manipulate the commit
11975 * structure without requiring any other locks.
11977 static void
11978 nfs4_get_commit_range(vnode_t *vp, uoff_t soff, size_t len)
11981 rnode4_t *rp;
11982 page_t *pp;
11983 uoff_t end;
11984 uoff_t off;
11985 ASSERT(len != 0);
11986 rp = VTOR4(vp);
11987 ASSERT(rp->r_flags & R4COMMIT);
11989 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11991 /* make sure we're looking at the master vnode, not a shadow */
11993 if (IS_SHADOW(vp, rp))
11994 vp = RTOV4(rp);
11997 * If there are no pages associated with this vnode, then
11998 * just return.
12000 if (!vn_has_cached_data(vp))
12001 return;
12003 * Calculate the ending offset.
12005 end = soff + len;
12006 for (off = soff; off < end; off += PAGESIZE) {
12008 * Lookup each page by vp, offset.
12010 if ((pp = page_lookup_nowait(&vp->v_object, off, SE_EXCL)) == NULL)
12011 continue;
12013 * If this page does not need to be committed or is
12014 * modified, then just skip it.
12016 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12017 page_unlock(pp);
12018 continue;
12021 ASSERT(PP_ISFREE(pp) == 0);
12023 * The page needs to be committed and we locked it.
12024 * Update the base and length parameters and add it
12025 * to r_pages.
12027 if (rp->r_commit.c_pages == NULL) {
12028 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12029 rp->r_commit.c_commlen = PAGESIZE;
12030 } else {
12031 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12032 rp->r_commit.c_commbase + PAGESIZE;
12034 page_add(&rp->r_commit.c_pages, pp);
12039 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12040 * Flushes and commits data to the server.
12042 static int
12043 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12045 int error;
12046 verifier4 write_verf;
12047 rnode4_t *rp = VTOR4(vp);
12049 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12052 * Flush the data portion of the file and then commit any
12053 * portions which need to be committed. This may need to
12054 * be done twice if the server has changed state since
12055 * data was last written. The data will need to be
12056 * rewritten to the server and then a new commit done.
12058 * In fact, this may need to be done several times if the
12059 * server is having problems and crashing while we are
12060 * attempting to do this.
12063 top:
12065 * Do a flush based on the poff and plen arguments. This
12066 * will synchronously write out any modified pages in the
12067 * range specified by (poff, plen). This starts all of the
12068 * i/o operations which will be waited for in the next
12069 * call to nfs4_putpage
12072 mutex_enter(&rp->r_statelock);
12073 write_verf = rp->r_writeverf;
12074 mutex_exit(&rp->r_statelock);
12076 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12077 if (error == EAGAIN)
12078 error = 0;
12081 * Do a flush based on the poff and plen arguments. This
12082 * will synchronously write out any modified pages in the
12083 * range specified by (poff, plen) and wait until all of
12084 * the asynchronous i/o's in that range are done as well.
12086 if (!error)
12087 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12089 if (error)
12090 return (error);
12092 mutex_enter(&rp->r_statelock);
12093 if (rp->r_writeverf != write_verf) {
12094 mutex_exit(&rp->r_statelock);
12095 goto top;
12097 mutex_exit(&rp->r_statelock);
12100 * Now commit any pages which might need to be committed.
12101 * If the error, NFS_VERF_MISMATCH, is returned, then
12102 * start over with the flush operation.
12104 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12106 if (error == NFS_VERF_MISMATCH)
12107 goto top;
12109 return (error);
12113 * nfs4_commit_vp() will wait for other pending commits and
12114 * will either commit the whole file or a range, plen dictates
12115 * if we commit whole file. a value of zero indicates the whole
12116 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12118 static int
12119 nfs4_commit_vp(vnode_t *vp, uoff_t poff, size_t plen,
12120 cred_t *cr, int wait_on_writes)
12122 rnode4_t *rp;
12123 page_t *plist;
12124 offset3 offset;
12125 count3 len;
12127 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12129 rp = VTOR4(vp);
12132 * before we gather commitable pages make
12133 * sure there are no outstanding async writes
12135 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12136 mutex_enter(&rp->r_statelock);
12137 while (rp->r_count > 0) {
12138 cv_wait(&rp->r_cv, &rp->r_statelock);
12140 mutex_exit(&rp->r_statelock);
12144 * Set the `commit inprogress' state bit. We must
12145 * first wait until any current one finishes.
12147 mutex_enter(&rp->r_statelock);
12148 while (rp->r_flags & R4COMMIT) {
12149 rp->r_flags |= R4COMMITWAIT;
12150 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12151 rp->r_flags &= ~R4COMMITWAIT;
12153 rp->r_flags |= R4COMMIT;
12154 mutex_exit(&rp->r_statelock);
12157 * Gather all of the pages which need to be
12158 * committed.
12160 if (plen == 0)
12161 nfs4_get_commit(vp);
12162 else
12163 nfs4_get_commit_range(vp, poff, plen);
12166 * Clear the `commit inprogress' bit and disconnect the
12167 * page list which was gathered by nfs4_get_commit.
12169 plist = rp->r_commit.c_pages;
12170 rp->r_commit.c_pages = NULL;
12171 offset = rp->r_commit.c_commbase;
12172 len = rp->r_commit.c_commlen;
12173 mutex_enter(&rp->r_statelock);
12174 rp->r_flags &= ~R4COMMIT;
12175 cv_broadcast(&rp->r_commit.c_cv);
12176 mutex_exit(&rp->r_statelock);
12179 * If any pages need to be committed, commit them and
12180 * then unlock them so that they can be freed some
12181 * time later.
12183 if (plist == NULL)
12184 return (0);
12187 * No error occurred during the flush portion
12188 * of this operation, so now attempt to commit
12189 * the data to stable storage on the server.
12191 * This will unlock all of the pages on the list.
12193 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12196 static int
12197 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12198 cred_t *cr)
12200 int error;
12201 page_t *pp;
12203 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12205 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12208 * If we got an error, then just unlock all of the pages
12209 * on the list.
12211 if (error) {
12212 while (plist != NULL) {
12213 pp = plist;
12214 page_sub(&plist, pp);
12215 page_unlock(pp);
12217 return (error);
12220 * We've tried as hard as we can to commit the data to stable
12221 * storage on the server. We just unlock the pages and clear
12222 * the commit required state. They will get freed later.
12224 while (plist != NULL) {
12225 pp = plist;
12226 page_sub(&plist, pp);
12227 pp->p_fsdata = C_NOCOMMIT;
12228 page_unlock(pp);
12231 return (error);
12234 static void
12235 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12236 cred_t *cr)
12239 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12242 /*ARGSUSED*/
12243 static int
12244 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12245 caller_context_t *ct)
12247 int error = 0;
12248 mntinfo4_t *mi;
12249 vattr_t va;
12250 vsecattr_t nfsace4_vsap;
12252 mi = VTOMI4(vp);
12253 if (nfs_zone() != mi->mi_zone)
12254 return (EIO);
12255 if (mi->mi_flags & MI4_ACL) {
12256 /* if we have a delegation, return it */
12257 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12258 (void) nfs4delegreturn(VTOR4(vp),
12259 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12261 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12262 NFS4_ACL_SET);
12263 if (error) /* EINVAL */
12264 return (error);
12266 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12268 * These are aclent_t type entries.
12270 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12271 vp->v_type == VDIR, FALSE);
12272 if (error)
12273 return (error);
12274 } else {
12276 * These are ace_t type entries.
12278 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12279 FALSE);
12280 if (error)
12281 return (error);
12283 bzero(&va, sizeof (va));
12284 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12285 vs_ace4_destroy(&nfsace4_vsap);
12286 return (error);
12288 return (ENOSYS);
12291 /* ARGSUSED */
12293 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12294 caller_context_t *ct)
12296 int error;
12297 mntinfo4_t *mi;
12298 nfs4_ga_res_t gar;
12299 rnode4_t *rp = VTOR4(vp);
12301 mi = VTOMI4(vp);
12302 if (nfs_zone() != mi->mi_zone)
12303 return (EIO);
12305 bzero(&gar, sizeof (gar));
12306 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12309 * vsecattr->vsa_mask holds the original acl request mask.
12310 * This is needed when determining what to return.
12311 * (See: nfs4_create_getsecattr_return())
12313 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12314 if (error) /* EINVAL */
12315 return (error);
12318 * If this is a referral stub, don't try to go OTW for an ACL
12320 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12321 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12323 if (mi->mi_flags & MI4_ACL) {
12325 * Check if the data is cached and the cache is valid. If it
12326 * is we don't go over the wire.
12328 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12329 mutex_enter(&rp->r_statelock);
12330 if (rp->r_secattr != NULL) {
12331 error = nfs4_create_getsecattr_return(
12332 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12333 rp->r_attr.va_gid,
12334 vp->v_type == VDIR);
12335 if (!error) { /* error == 0 - Success! */
12336 mutex_exit(&rp->r_statelock);
12337 return (error);
12340 mutex_exit(&rp->r_statelock);
12344 * The getattr otw call will always get both the acl, in
12345 * the form of a list of nfsace4's, and the number of acl
12346 * entries; independent of the value of gar.n4g_va.va_mask.
12348 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12349 if (error) {
12350 vs_ace4_destroy(&gar.n4g_vsa);
12351 if (error == ENOTSUP || error == EOPNOTSUPP)
12352 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12353 return (error);
12356 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12358 * No error was returned, but according to the response
12359 * bitmap, neither was an acl.
12361 vs_ace4_destroy(&gar.n4g_vsa);
12362 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12363 return (error);
12367 * Update the cache with the ACL.
12369 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12371 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12372 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12373 vp->v_type == VDIR);
12374 vs_ace4_destroy(&gar.n4g_vsa);
12375 if ((error) && (vsecattr->vsa_mask &
12376 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12377 (error != EACCES)) {
12378 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12380 return (error);
12382 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12383 return (error);
12387 * The function returns:
12388 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12389 * - EINVAL if the passed in "acl_mask" is an invalid request.
12391 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12392 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12394 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12395 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12396 * - We have a count field set without the corresponding acl field set. (e.g. -
12397 * VSA_ACECNT is set, but VSA_ACE is not)
12399 static int
12400 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12402 /* Shortcut the masks that are always valid. */
12403 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12404 return (0);
12405 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12406 return (0);
12408 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12410 * We can't have any VSA_ACL type stuff in the mask now.
12412 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12413 VSA_DFACLCNT))
12414 return (EINVAL);
12416 if (op == NFS4_ACL_SET) {
12417 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12418 return (EINVAL);
12422 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12424 * We can't have any VSA_ACE type stuff in the mask now.
12426 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12427 return (EINVAL);
12429 if (op == NFS4_ACL_SET) {
12430 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12431 return (EINVAL);
12433 if ((acl_mask & VSA_DFACLCNT) &&
12434 !(acl_mask & VSA_DFACL))
12435 return (EINVAL);
12438 return (0);
12442 * The theory behind creating the correct getsecattr return is simply this:
12443 * "Don't return anything that the caller is not expecting to have to free."
12445 static int
12446 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12447 uid_t uid, gid_t gid, int isdir)
12449 int error = 0;
12450 /* Save the mask since the translators modify it. */
12451 uint_t orig_mask = vsap->vsa_mask;
12453 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12454 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12456 if (error)
12457 return (error);
12460 * If the caller only asked for the ace count (VSA_ACECNT)
12461 * don't give them the full acl (VSA_ACE), free it.
12463 if (!orig_mask & VSA_ACE) {
12464 if (vsap->vsa_aclentp != NULL) {
12465 kmem_free(vsap->vsa_aclentp,
12466 vsap->vsa_aclcnt * sizeof (ace_t));
12467 vsap->vsa_aclentp = NULL;
12470 vsap->vsa_mask = orig_mask;
12472 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12473 VSA_DFACLCNT)) {
12474 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12475 isdir, FALSE);
12477 if (error)
12478 return (error);
12481 * If the caller only asked for the acl count (VSA_ACLCNT)
12482 * and/or the default acl count (VSA_DFACLCNT) don't give them
12483 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12485 if (!orig_mask & VSA_ACL) {
12486 if (vsap->vsa_aclentp != NULL) {
12487 kmem_free(vsap->vsa_aclentp,
12488 vsap->vsa_aclcnt * sizeof (aclent_t));
12489 vsap->vsa_aclentp = NULL;
12493 if (!orig_mask & VSA_DFACL) {
12494 if (vsap->vsa_dfaclentp != NULL) {
12495 kmem_free(vsap->vsa_dfaclentp,
12496 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12497 vsap->vsa_dfaclentp = NULL;
12500 vsap->vsa_mask = orig_mask;
12502 return (0);
12505 /* ARGSUSED */
12507 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12508 caller_context_t *ct)
12510 int error;
12512 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12513 return (EIO);
12515 * check for valid cmd parameter
12517 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12518 return (EINVAL);
12521 * Check access permissions
12523 if ((cmd & F_SHARE) &&
12524 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12525 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12526 return (EBADF);
12529 * If the filesystem is mounted using local locking, pass the
12530 * request off to the local share code.
12532 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12533 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12535 switch (cmd) {
12536 case F_SHARE:
12537 case F_UNSHARE:
12539 * This will be properly implemented later,
12540 * see RFE: 4823948 .
12542 error = EAGAIN;
12543 break;
12545 case F_HASREMOTELOCKS:
12547 * NFS client can't store remote locks itself
12549 shr->s_access = 0;
12550 error = 0;
12551 break;
12553 default:
12554 error = EINVAL;
12555 break;
12558 return (error);
12562 * Common code called by directory ops to update the attrcache
12564 static int
12565 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12566 hrtime_t t, vnode_t *vp, cred_t *cr)
12568 int error = 0;
12570 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12572 if (status != NFS4_OK) {
12573 /* getattr not done or failed */
12574 PURGE_ATTRCACHE4(vp);
12575 return (error);
12578 if (garp) {
12579 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12580 } else {
12581 PURGE_ATTRCACHE4(vp);
12583 return (error);
12587 * Update directory caches for directory modification ops (link, rename, etc.)
12588 * When dinfo is NULL, manage dircaches in the old way.
12590 static void
12591 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12592 dirattr_info_t *dinfo)
12594 rnode4_t *drp = VTOR4(dvp);
12596 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12598 /* Purge rddir cache for dir since it changed */
12599 if (drp->r_dir != NULL)
12600 nfs4_purge_rddir_cache(dvp);
12603 * If caller provided dinfo, then use it to manage dir caches.
12605 if (dinfo != NULL) {
12606 if (vp != NULL) {
12607 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12608 if (!VTOR4(vp)->created_v4) {
12609 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12610 dnlc_update(dvp, nm, vp);
12611 } else {
12613 * XXX don't update if the created_v4 flag is
12614 * set
12616 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12617 NFS4_DEBUG(nfs4_client_state_debug,
12618 (CE_NOTE, "nfs4_update_dircaches: "
12619 "don't update dnlc: created_v4 flag"));
12623 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12624 dinfo->di_cred, FALSE, cinfo);
12626 return;
12630 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12631 * Since caller modified dir but didn't receive post-dirmod-op dir
12632 * attrs, the dir's attrs must be purged.
12634 * XXX this check and dnlc update/purge should really be atomic,
12635 * XXX but can't use rnode statelock because it'll deadlock in
12636 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12637 * XXX does occur.
12639 * XXX We also may want to check that atomic is true in the
12640 * XXX change_info struct. If it is not, the change_info may
12641 * XXX reflect changes by more than one clients which means that
12642 * XXX our cache may not be valid.
12644 PURGE_ATTRCACHE4(dvp);
12645 if (drp->r_change == cinfo->before) {
12646 /* no changes took place in the directory prior to our link */
12647 if (vp != NULL) {
12648 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12649 if (!VTOR4(vp)->created_v4) {
12650 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12651 dnlc_update(dvp, nm, vp);
12652 } else {
12654 * XXX dont' update if the created_v4 flag
12655 * is set
12657 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12658 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12659 "nfs4_update_dircaches: don't"
12660 " update dnlc: created_v4 flag"));
12663 } else {
12664 /* Another client modified directory - purge its dnlc cache */
12665 dnlc_purge_vp(dvp);
12670 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12671 * file.
12673 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12674 * file (ie: client recovery) and otherwise set to FALSE.
12676 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12677 * initiated) calling functions.
12679 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12680 * of resending a 'lost' open request.
12682 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12683 * server that hands out BAD_SEQID on open confirm.
12685 * Errors are returned via the nfs4_error_t parameter.
12687 void
12688 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12689 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12690 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12692 COMPOUND4args_clnt args;
12693 COMPOUND4res_clnt res;
12694 nfs_argop4 argop[2];
12695 nfs_resop4 *resop;
12696 int doqueue = 1;
12697 mntinfo4_t *mi;
12698 OPEN_CONFIRM4args *open_confirm_args;
12699 int needrecov;
12701 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12702 #if DEBUG
12703 mutex_enter(&oop->oo_lock);
12704 ASSERT(oop->oo_seqid_inuse);
12705 mutex_exit(&oop->oo_lock);
12706 #endif
12708 recov_retry_confirm:
12709 nfs4_error_zinit(ep);
12710 *retry_open = FALSE;
12712 if (resend)
12713 args.ctag = TAG_OPEN_CONFIRM_LOST;
12714 else
12715 args.ctag = TAG_OPEN_CONFIRM;
12717 args.array_len = 2;
12718 args.array = argop;
12720 /* putfh target fh */
12721 argop[0].argop = OP_CPUTFH;
12722 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12724 argop[1].argop = OP_OPEN_CONFIRM;
12725 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12727 (*seqid) += 1;
12728 open_confirm_args->seqid = *seqid;
12729 open_confirm_args->open_stateid = *stateid;
12731 mi = VTOMI4(vp);
12733 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12735 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12736 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12739 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12740 if (!needrecov && ep->error)
12741 return;
12743 if (needrecov) {
12744 bool_t abort = FALSE;
12746 if (reopening_file == FALSE) {
12747 nfs4_bseqid_entry_t *bsep = NULL;
12749 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12750 bsep = nfs4_create_bseqid_entry(oop, NULL,
12751 vp, 0, args.ctag,
12752 open_confirm_args->seqid);
12754 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12755 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12756 if (bsep) {
12757 kmem_free(bsep, sizeof (*bsep));
12758 if (num_bseqid_retryp &&
12759 --(*num_bseqid_retryp) == 0)
12760 abort = TRUE;
12763 if ((ep->error == ETIMEDOUT ||
12764 res.status == NFS4ERR_RESOURCE) &&
12765 abort == FALSE && resend == FALSE) {
12766 if (!ep->error)
12767 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12769 ddi_sleep(confirm_retry_sec);
12770 goto recov_retry_confirm;
12772 /* State may have changed so retry the entire OPEN op */
12773 if (abort == FALSE)
12774 *retry_open = TRUE;
12775 else
12776 *retry_open = FALSE;
12777 if (!ep->error)
12778 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12779 return;
12782 if (res.status) {
12783 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12784 return;
12787 resop = &res.array[1]; /* open confirm res */
12788 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12789 stateid, sizeof (*stateid));
12791 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12795 * Return the credentials associated with a client state object. The
12796 * caller is responsible for freeing the credentials.
12799 static cred_t *
12800 state_to_cred(nfs4_open_stream_t *osp)
12802 cred_t *cr;
12805 * It's ok to not lock the open stream and open owner to get
12806 * the oo_cred since this is only written once (upon creation)
12807 * and will not change.
12809 cr = osp->os_open_owner->oo_cred;
12810 crhold(cr);
12812 return (cr);
12816 * nfs4_find_sysid
12818 * Find the sysid for the knetconfig associated with the given mi.
12820 static struct lm_sysid *
12821 nfs4_find_sysid(mntinfo4_t *mi)
12823 ASSERT(nfs_zone() == mi->mi_zone);
12826 * Switch from RDMA knconf to original mount knconf
12828 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12829 mi->mi_curr_serv->sv_hostname, NULL));
12832 #ifdef DEBUG
12834 * Return a string version of the call type for easy reading.
12836 static char *
12837 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12839 switch (ctype) {
12840 case NFS4_LCK_CTYPE_NORM:
12841 return ("NORMAL");
12842 case NFS4_LCK_CTYPE_RECLAIM:
12843 return ("RECLAIM");
12844 case NFS4_LCK_CTYPE_RESEND:
12845 return ("RESEND");
12846 case NFS4_LCK_CTYPE_REINSTATE:
12847 return ("REINSTATE");
12848 default:
12849 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12850 "type %d", ctype);
12851 return ("");
12854 #endif
12857 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12858 * Unlock requests don't have an over-the-wire locktype, so we just return
12859 * something non-threatening.
12862 static nfs_lock_type4
12863 flk_to_locktype(int cmd, int l_type)
12865 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12867 switch (l_type) {
12868 case F_UNLCK:
12869 return (READ_LT);
12870 case F_RDLCK:
12871 if (cmd == F_SETLK)
12872 return (READ_LT);
12873 else
12874 return (READW_LT);
12875 case F_WRLCK:
12876 if (cmd == F_SETLK)
12877 return (WRITE_LT);
12878 else
12879 return (WRITEW_LT);
12881 panic("flk_to_locktype");
12882 /*NOTREACHED*/
12886 * Do some preliminary checks for nfs4frlock.
12888 static int
12889 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12890 uoff_t offset)
12892 int error = 0;
12895 * If we are setting a lock, check that the file is opened
12896 * with the correct mode.
12898 if (cmd == F_SETLK || cmd == F_SETLKW) {
12899 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12900 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12901 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12902 "nfs4frlock_validate_args: file was opened with "
12903 "incorrect mode"));
12904 return (EBADF);
12908 /* Convert the offset. It may need to be restored before returning. */
12909 if (error = convoff(vp, flk, 0, offset)) {
12910 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12911 "nfs4frlock_validate_args: convoff => error= %d\n",
12912 error));
12913 return (error);
12916 return (error);
12920 * Set the flock64's lm_sysid for nfs4frlock.
12922 static int
12923 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12925 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12927 /* Find the lm_sysid */
12928 *lspp = nfs4_find_sysid(VTOMI4(vp));
12930 if (*lspp == NULL) {
12931 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12932 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12933 return (ENOLCK);
12936 flk->l_sysid = lm_sysidt(*lspp);
12938 return (0);
12942 * Do the remaining preliminary setup for nfs4frlock.
12944 static void
12945 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12946 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12947 cred_t **cred_otw)
12950 * set tick_delay to the base delay time.
12951 * (NFS4_BASE_WAIT_TIME is in secs)
12954 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12957 * If lock is relative to EOF, we need the newest length of the
12958 * file. Therefore invalidate the ATTR_CACHE.
12961 *whencep = flk->l_whence;
12963 if (*whencep == 2) /* SEEK_END */
12964 PURGE_ATTRCACHE4(vp);
12966 recov_statep->rs_flags = 0;
12967 recov_statep->rs_num_retry_despite_err = 0;
12968 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12972 * Initialize and allocate the data structures necessary for
12973 * the nfs4frlock call.
12974 * Allocates argsp's op array.
12976 static void
12977 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
12978 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
12979 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
12980 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
12982 int argoplist_size;
12983 int num_ops = 2;
12985 *retry = FALSE;
12986 *did_start_fop = FALSE;
12987 *skip_get_err = FALSE;
12988 lost_rqstp->lr_op = 0;
12989 argoplist_size = num_ops * sizeof (nfs_argop4);
12990 /* fill array with zero */
12991 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
12993 *argspp = argsp;
12994 *respp = NULL;
12996 argsp->array_len = num_ops;
12997 argsp->array = *argopp;
12999 /* initialize in case of error; will get real value down below */
13000 argsp->ctag = TAG_NONE;
13002 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13003 *op_hintp = OH_LOCKU;
13004 else
13005 *op_hintp = OH_OTHER;
13009 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13010 * the proper nfs4_server_t for this instance of nfs4frlock.
13011 * Returns 0 (success) or an errno value.
13013 static int
13014 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13015 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13016 bool_t *did_start_fop, bool_t *startrecovp)
13018 int error = 0;
13019 rnode4_t *rp;
13021 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13023 if (ctype == NFS4_LCK_CTYPE_NORM) {
13024 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13025 recov_statep, startrecovp);
13026 if (error)
13027 return (error);
13028 *did_start_fop = TRUE;
13029 } else {
13030 *did_start_fop = FALSE;
13031 *startrecovp = FALSE;
13034 if (!error) {
13035 rp = VTOR4(vp);
13037 /* If the file failed recovery, just quit. */
13038 mutex_enter(&rp->r_statelock);
13039 if (rp->r_flags & R4RECOVERR) {
13040 error = EIO;
13042 mutex_exit(&rp->r_statelock);
13045 return (error);
13049 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13050 * resend nfs4frlock call is initiated by the recovery framework.
13051 * Acquires the lop and oop seqid synchronization.
13053 static void
13054 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13055 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13056 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13057 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13059 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13060 int error;
13062 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13063 (CE_NOTE,
13064 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13065 ASSERT(resend_rqstp != NULL);
13066 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13067 resend_rqstp->lr_op == OP_LOCKU);
13069 *oopp = resend_rqstp->lr_oop;
13070 if (resend_rqstp->lr_oop) {
13071 open_owner_hold(resend_rqstp->lr_oop);
13072 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13073 ASSERT(error == 0); /* recov thread always succeeds */
13076 /* Must resend this lost lock/locku request. */
13077 ASSERT(resend_rqstp->lr_lop != NULL);
13078 *lopp = resend_rqstp->lr_lop;
13079 lock_owner_hold(resend_rqstp->lr_lop);
13080 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13081 ASSERT(error == 0); /* recov thread always succeeds */
13083 *ospp = resend_rqstp->lr_osp;
13084 if (*ospp)
13085 open_stream_hold(resend_rqstp->lr_osp);
13087 if (resend_rqstp->lr_op == OP_LOCK) {
13088 LOCK4args *lock_args;
13090 argop->argop = OP_LOCK;
13091 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13092 lock_args->locktype = resend_rqstp->lr_locktype;
13093 lock_args->reclaim =
13094 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13095 lock_args->offset = resend_rqstp->lr_flk->l_start;
13096 lock_args->length = resend_rqstp->lr_flk->l_len;
13097 if (lock_args->length == 0)
13098 lock_args->length = ~lock_args->length;
13099 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13100 mi2clientid(mi), &lock_args->locker);
13102 switch (resend_rqstp->lr_ctype) {
13103 case NFS4_LCK_CTYPE_RESEND:
13104 argsp->ctag = TAG_LOCK_RESEND;
13105 break;
13106 case NFS4_LCK_CTYPE_REINSTATE:
13107 argsp->ctag = TAG_LOCK_REINSTATE;
13108 break;
13109 case NFS4_LCK_CTYPE_RECLAIM:
13110 argsp->ctag = TAG_LOCK_RECLAIM;
13111 break;
13112 default:
13113 argsp->ctag = TAG_LOCK_UNKNOWN;
13114 break;
13116 } else {
13117 LOCKU4args *locku_args;
13118 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13120 argop->argop = OP_LOCKU;
13121 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13122 locku_args->locktype = READ_LT;
13123 locku_args->seqid = lop->lock_seqid + 1;
13124 mutex_enter(&lop->lo_lock);
13125 locku_args->lock_stateid = lop->lock_stateid;
13126 mutex_exit(&lop->lo_lock);
13127 locku_args->offset = resend_rqstp->lr_flk->l_start;
13128 locku_args->length = resend_rqstp->lr_flk->l_len;
13129 if (locku_args->length == 0)
13130 locku_args->length = ~locku_args->length;
13132 switch (resend_rqstp->lr_ctype) {
13133 case NFS4_LCK_CTYPE_RESEND:
13134 argsp->ctag = TAG_LOCKU_RESEND;
13135 break;
13136 case NFS4_LCK_CTYPE_REINSTATE:
13137 argsp->ctag = TAG_LOCKU_REINSTATE;
13138 break;
13139 default:
13140 argsp->ctag = TAG_LOCK_UNKNOWN;
13141 break;
13147 * Setup the LOCKT4 arguments.
13149 static void
13150 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13151 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13152 rnode4_t *rp)
13154 LOCKT4args *lockt_args;
13156 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13157 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13158 argop->argop = OP_LOCKT;
13159 argsp->ctag = TAG_LOCKT;
13160 lockt_args = &argop->nfs_argop4_u.oplockt;
13163 * The locktype will be READ_LT unless it's
13164 * a write lock. We do this because the Solaris
13165 * system call allows the combination of
13166 * F_UNLCK and F_GETLK* and so in that case the
13167 * unlock is mapped to a read.
13169 if (flk->l_type == F_WRLCK)
13170 lockt_args->locktype = WRITE_LT;
13171 else
13172 lockt_args->locktype = READ_LT;
13174 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13175 /* set the lock owner4 args */
13176 nfs4_setlockowner_args(&lockt_args->owner, rp,
13177 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13178 flk->l_pid);
13179 lockt_args->offset = flk->l_start;
13180 lockt_args->length = flk->l_len;
13181 if (flk->l_len == 0)
13182 lockt_args->length = ~lockt_args->length;
13184 *lockt_argsp = lockt_args;
13188 * If the client is holding a delegation, and the open stream to be used
13189 * with this lock request is a delegation open stream, then re-open the stream.
13190 * Sets the nfs4_error_t to all zeros unless the open stream has already
13191 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13192 * means the caller should retry (like a recovery retry).
13194 static void
13195 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13197 open_delegation_type4 dt;
13198 bool_t reopen_needed, force;
13199 nfs4_open_stream_t *osp;
13200 open_claim_type4 oclaim;
13201 rnode4_t *rp = VTOR4(vp);
13202 mntinfo4_t *mi = VTOMI4(vp);
13204 ASSERT(nfs_zone() == mi->mi_zone);
13206 nfs4_error_zinit(ep);
13208 mutex_enter(&rp->r_statev4_lock);
13209 dt = rp->r_deleg_type;
13210 mutex_exit(&rp->r_statev4_lock);
13212 if (dt != OPEN_DELEGATE_NONE) {
13213 nfs4_open_owner_t *oop;
13215 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13216 if (!oop) {
13217 ep->stat = NFS4ERR_IO;
13218 return;
13220 /* returns with 'os_sync_lock' held */
13221 osp = find_open_stream(oop, rp);
13222 if (!osp) {
13223 open_owner_rele(oop);
13224 ep->stat = NFS4ERR_IO;
13225 return;
13228 if (osp->os_failed_reopen) {
13229 NFS4_DEBUG((nfs4_open_stream_debug ||
13230 nfs4_client_lock_debug), (CE_NOTE,
13231 "nfs4frlock_check_deleg: os_failed_reopen set "
13232 "for osp %p, cr %p, rp %s", (void *)osp,
13233 (void *)cr, rnode4info(rp)));
13234 mutex_exit(&osp->os_sync_lock);
13235 open_stream_rele(osp, rp);
13236 open_owner_rele(oop);
13237 ep->stat = NFS4ERR_IO;
13238 return;
13242 * Determine whether a reopen is needed. If this
13243 * is a delegation open stream, then send the open
13244 * to the server to give visibility to the open owner.
13245 * Even if it isn't a delegation open stream, we need
13246 * to check if the previous open CLAIM_DELEGATE_CUR
13247 * was sufficient.
13250 reopen_needed = osp->os_delegation ||
13251 ((lt == F_RDLCK &&
13252 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13253 (lt == F_WRLCK &&
13254 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13256 mutex_exit(&osp->os_sync_lock);
13257 open_owner_rele(oop);
13259 if (reopen_needed) {
13261 * Always use CLAIM_PREVIOUS after server reboot.
13262 * The server will reject CLAIM_DELEGATE_CUR if
13263 * it is used during the grace period.
13265 mutex_enter(&mi->mi_lock);
13266 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13267 oclaim = CLAIM_PREVIOUS;
13268 force = TRUE;
13269 } else {
13270 oclaim = CLAIM_DELEGATE_CUR;
13271 force = FALSE;
13273 mutex_exit(&mi->mi_lock);
13275 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13276 if (ep->error == EAGAIN) {
13277 nfs4_error_zinit(ep);
13278 ep->stat = NFS4ERR_DELAY;
13281 open_stream_rele(osp, rp);
13282 osp = NULL;
13287 * Setup the LOCKU4 arguments.
13288 * Returns errors via the nfs4_error_t.
13289 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13290 * over-the-wire. The caller must release the
13291 * reference on *lopp.
13292 * NFS4ERR_DELAY caller should retry (like recovery retry)
13293 * (other) unrecoverable error.
13295 static void
13296 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13297 LOCKU4args **locku_argsp, flock64_t *flk,
13298 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13299 vnode_t *vp, int flag, uoff_t offset, cred_t *cr,
13300 bool_t *skip_get_err, bool_t *go_otwp)
13302 nfs4_lock_owner_t *lop = NULL;
13303 LOCKU4args *locku_args;
13304 pid_t pid;
13305 bool_t is_spec = FALSE;
13306 rnode4_t *rp = VTOR4(vp);
13308 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13309 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13311 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13312 if (ep->error || ep->stat)
13313 return;
13315 argop->argop = OP_LOCKU;
13316 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13317 argsp->ctag = TAG_LOCKU_REINSTATE;
13318 else
13319 argsp->ctag = TAG_LOCKU;
13320 locku_args = &argop->nfs_argop4_u.oplocku;
13321 *locku_argsp = locku_args;
13323 /* locktype should be set to any legal value */
13324 locku_args->locktype = READ_LT;
13326 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13327 flk->l_pid;
13330 * Get the lock owner stateid. If no lock owner
13331 * exists, return success.
13333 lop = find_lock_owner(rp, pid, LOWN_ANY);
13334 *lopp = lop;
13335 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13336 is_spec = TRUE;
13337 if (!lop || is_spec) {
13339 * No lock owner so no locks to unlock.
13340 * Return success. If there was a failed
13341 * reclaim earlier, the lock might still be
13342 * registered with the local locking code,
13343 * so notify it of the unlock.
13345 * If the lockowner is using a special stateid,
13346 * then the original lock request (that created
13347 * this lockowner) was never successful, so we
13348 * have no lock to undo OTW.
13350 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13351 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13352 "(%ld) so return success", (long)pid));
13354 if (ctype == NFS4_LCK_CTYPE_NORM)
13355 flk->l_pid = curproc->p_pid;
13356 nfs4_register_lock_locally(vp, flk, flag, offset);
13358 * Release our hold and NULL out so final_cleanup
13359 * doesn't try to end a lock seqid sync we
13360 * never started.
13362 if (is_spec) {
13363 lock_owner_rele(lop);
13364 *lopp = NULL;
13366 *skip_get_err = TRUE;
13367 *go_otwp = FALSE;
13368 return;
13371 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13372 if (ep->error == EAGAIN) {
13373 lock_owner_rele(lop);
13374 *lopp = NULL;
13375 return;
13378 mutex_enter(&lop->lo_lock);
13379 locku_args->lock_stateid = lop->lock_stateid;
13380 mutex_exit(&lop->lo_lock);
13381 locku_args->seqid = lop->lock_seqid + 1;
13383 /* leave the ref count on lop, rele after RPC call */
13385 locku_args->offset = flk->l_start;
13386 locku_args->length = flk->l_len;
13387 if (flk->l_len == 0)
13388 locku_args->length = ~locku_args->length;
13390 *go_otwp = TRUE;
13394 * Setup the LOCK4 arguments.
13396 * Returns errors via the nfs4_error_t.
13397 * NFS4_OK no problems
13398 * NFS4ERR_DELAY caller should retry (like recovery retry)
13399 * (other) unrecoverable error
13401 static void
13402 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13403 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13404 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13405 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13407 LOCK4args *lock_args;
13408 nfs4_open_owner_t *oop = NULL;
13409 nfs4_open_stream_t *osp = NULL;
13410 nfs4_lock_owner_t *lop = NULL;
13411 pid_t pid;
13412 rnode4_t *rp = VTOR4(vp);
13414 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13416 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13417 if (ep->error || ep->stat != NFS4_OK)
13418 return;
13420 argop->argop = OP_LOCK;
13421 if (ctype == NFS4_LCK_CTYPE_NORM)
13422 argsp->ctag = TAG_LOCK;
13423 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13424 argsp->ctag = TAG_RELOCK;
13425 else
13426 argsp->ctag = TAG_LOCK_REINSTATE;
13427 lock_args = &argop->nfs_argop4_u.oplock;
13428 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13429 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13431 * Get the lock owner. If no lock owner exists,
13432 * create a 'temporary' one and grab the open seqid
13433 * synchronization (which puts a hold on the open
13434 * owner and open stream).
13435 * This also grabs the lock seqid synchronization.
13437 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13438 ep->stat =
13439 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13441 if (ep->stat != NFS4_OK)
13442 goto out;
13444 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13445 &lock_args->locker);
13447 lock_args->offset = flk->l_start;
13448 lock_args->length = flk->l_len;
13449 if (flk->l_len == 0)
13450 lock_args->length = ~lock_args->length;
13451 *lock_argsp = lock_args;
13452 out:
13453 *oopp = oop;
13454 *ospp = osp;
13455 *lopp = lop;
13459 * After we get the reply from the server, record the proper information
13460 * for possible resend lock requests.
13462 static void
13463 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13464 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13465 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13466 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13468 bool_t unlock = (flk->l_type == F_UNLCK);
13470 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13471 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13472 ctype == NFS4_LCK_CTYPE_REINSTATE);
13474 if (error != 0 && !unlock) {
13475 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13476 nfs4_client_lock_debug), (CE_NOTE,
13477 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13478 " for lop %p", (void *)lop));
13479 ASSERT(lop != NULL);
13480 mutex_enter(&lop->lo_lock);
13481 lop->lo_pending_rqsts = 1;
13482 mutex_exit(&lop->lo_lock);
13485 lost_rqstp->lr_putfirst = FALSE;
13486 lost_rqstp->lr_op = 0;
13489 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13490 * recovery purposes so that the lock request that was sent
13491 * can be saved and re-issued later. Ditto for EIO from a forced
13492 * unmount. This is done to have the client's local locking state
13493 * match the v4 server's state; that is, the request was
13494 * potentially received and accepted by the server but the client
13495 * thinks it was not.
13497 if (error == ETIMEDOUT || error == EINTR ||
13498 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13499 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13500 nfs4_client_lock_debug), (CE_NOTE,
13501 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13502 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13503 (void *)lop, (void *)oop, (void *)osp));
13504 if (unlock)
13505 lost_rqstp->lr_op = OP_LOCKU;
13506 else {
13507 lost_rqstp->lr_op = OP_LOCK;
13508 lost_rqstp->lr_locktype = locktype;
13511 * Objects are held and rele'd via the recovery code.
13512 * See nfs4_save_lost_rqst.
13514 lost_rqstp->lr_vp = vp;
13515 lost_rqstp->lr_dvp = NULL;
13516 lost_rqstp->lr_oop = oop;
13517 lost_rqstp->lr_osp = osp;
13518 lost_rqstp->lr_lop = lop;
13519 lost_rqstp->lr_cr = cr;
13520 switch (ctype) {
13521 case NFS4_LCK_CTYPE_NORM:
13522 flk->l_pid = ttoproc(curthread)->p_pid;
13523 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13524 break;
13525 case NFS4_LCK_CTYPE_REINSTATE:
13526 lost_rqstp->lr_putfirst = TRUE;
13527 lost_rqstp->lr_ctype = ctype;
13528 break;
13529 default:
13530 break;
13532 lost_rqstp->lr_flk = flk;
13537 * Update lop's seqid. Also update the seqid stored in a resend request,
13538 * if any. (Some recovery errors increment the seqid, and we may have to
13539 * send the resend request again.)
13542 static void
13543 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13544 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13546 if (lock_args) {
13547 if (lock_args->locker.new_lock_owner == TRUE)
13548 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13549 else {
13550 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13551 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13553 } else if (locku_args) {
13554 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13555 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13560 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13561 * COMPOUND4 args/res for calls that need to retry.
13562 * Switches the *cred_otwp to base_cr.
13564 static void
13565 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13566 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13567 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13568 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13569 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13571 nfs4_open_owner_t *oop = *oopp;
13572 nfs4_open_stream_t *osp = *ospp;
13573 nfs4_lock_owner_t *lop = *lopp;
13574 nfs_argop4 *argop = (*argspp)->array;
13576 if (*did_start_fop) {
13577 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13578 needrecov);
13579 *did_start_fop = FALSE;
13581 ASSERT((*argspp)->array_len == 2);
13582 if (argop[1].argop == OP_LOCK)
13583 nfs4args_lock_free(&argop[1]);
13584 else if (argop[1].argop == OP_LOCKT)
13585 nfs4args_lockt_free(&argop[1]);
13586 kmem_free(argop, 2 * sizeof (nfs_argop4));
13587 if (!error)
13588 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13589 *argspp = NULL;
13590 *respp = NULL;
13592 if (lop) {
13593 nfs4_end_lock_seqid_sync(lop);
13594 lock_owner_rele(lop);
13595 *lopp = NULL;
13598 /* need to free up the reference on osp for lock args */
13599 if (osp != NULL) {
13600 open_stream_rele(osp, VTOR4(vp));
13601 *ospp = NULL;
13604 /* need to free up the reference on oop for lock args */
13605 if (oop != NULL) {
13606 nfs4_end_open_seqid_sync(oop);
13607 open_owner_rele(oop);
13608 *oopp = NULL;
13611 crfree(*cred_otwp);
13612 *cred_otwp = base_cr;
13613 crhold(*cred_otwp);
13617 * Function to process the client's recovery for nfs4frlock.
13618 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13620 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13621 * COMPOUND4 args/res for calls that need to retry.
13623 * Note: the rp's r_lkserlock is *not* dropped during this path.
13625 static bool_t
13626 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13627 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13628 LOCK4args *lock_args, LOCKU4args *locku_args,
13629 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13630 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13631 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13632 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13634 nfs4_open_owner_t *oop = *oopp;
13635 nfs4_open_stream_t *osp = *ospp;
13636 nfs4_lock_owner_t *lop = *lopp;
13638 bool_t abort, retry;
13640 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13641 ASSERT((*argspp) != NULL);
13642 ASSERT((*respp) != NULL);
13643 if (lock_args || locku_args)
13644 ASSERT(lop != NULL);
13646 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13647 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13649 retry = TRUE;
13650 abort = FALSE;
13651 if (needrecov) {
13652 nfs4_bseqid_entry_t *bsep = NULL;
13653 nfs_opnum4 op;
13655 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13657 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13658 seqid4 seqid;
13660 if (lock_args) {
13661 if (lock_args->locker.new_lock_owner == TRUE)
13662 seqid = lock_args->locker.locker4_u.
13663 open_owner.open_seqid;
13664 else
13665 seqid = lock_args->locker.locker4_u.
13666 lock_owner.lock_seqid;
13667 } else if (locku_args) {
13668 seqid = locku_args->seqid;
13669 } else {
13670 seqid = 0;
13673 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13674 flk->l_pid, (*argspp)->ctag, seqid);
13677 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13678 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13679 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13680 NULL, op, bsep, NULL, NULL);
13682 if (bsep)
13683 kmem_free(bsep, sizeof (*bsep));
13687 * Return that we do not want to retry the request for 3 cases:
13688 * 1. If we received EINTR or are bailing out because of a forced
13689 * unmount, we came into this code path just for the sake of
13690 * initiating recovery, we now need to return the error.
13691 * 2. If we have aborted recovery.
13692 * 3. We received NFS4ERR_BAD_SEQID.
13694 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13695 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13696 retry = FALSE;
13698 if (*did_start_fop == TRUE) {
13699 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13700 needrecov);
13701 *did_start_fop = FALSE;
13704 if (retry == TRUE) {
13705 nfs_argop4 *argop;
13707 argop = (*argspp)->array;
13708 ASSERT((*argspp)->array_len == 2);
13710 if (argop[1].argop == OP_LOCK)
13711 nfs4args_lock_free(&argop[1]);
13712 else if (argop[1].argop == OP_LOCKT)
13713 nfs4args_lockt_free(&argop[1]);
13714 kmem_free(argop, 2 * sizeof (nfs_argop4));
13715 if (!ep->error)
13716 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13717 *respp = NULL;
13718 *argspp = NULL;
13721 if (lop != NULL) {
13722 nfs4_end_lock_seqid_sync(lop);
13723 lock_owner_rele(lop);
13726 *lopp = NULL;
13728 /* need to free up the reference on osp for lock args */
13729 if (osp != NULL) {
13730 open_stream_rele(osp, rp);
13731 *ospp = NULL;
13734 /* need to free up the reference on oop for lock args */
13735 if (oop != NULL) {
13736 nfs4_end_open_seqid_sync(oop);
13737 open_owner_rele(oop);
13738 *oopp = NULL;
13741 return (retry);
13745 * Handles the successful reply from the server for nfs4frlock.
13747 static void
13748 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13749 vnode_t *vp, int flag, uoff_t offset,
13750 nfs4_lost_rqst_t *resend_rqstp)
13752 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13753 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13754 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13755 if (ctype == NFS4_LCK_CTYPE_NORM) {
13756 flk->l_pid = ttoproc(curthread)->p_pid;
13758 * We do not register lost locks locally in
13759 * the 'resend' case since the user/application
13760 * doesn't think we have the lock.
13762 ASSERT(!resend_rqstp);
13763 nfs4_register_lock_locally(vp, flk, flag, offset);
13769 * Handle the DENIED reply from the server for nfs4frlock.
13770 * Returns TRUE if we should retry the request; FALSE otherwise.
13772 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13773 * COMPOUND4 args/res for calls that need to retry. Can also
13774 * drop and regrab the r_lkserlock.
13776 static bool_t
13777 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13778 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13779 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13780 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13781 nfs4_recov_state_t *recov_statep, int needrecov,
13782 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13783 clock_t *tick_delayp, short *whencep, int *errorp,
13784 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13785 bool_t *skip_get_err)
13787 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13789 if (lock_args) {
13790 nfs4_open_owner_t *oop = *oopp;
13791 nfs4_open_stream_t *osp = *ospp;
13792 nfs4_lock_owner_t *lop = *lopp;
13793 int intr;
13796 * Blocking lock needs to sleep and retry from the request.
13798 * Do not block and wait for 'resend' or 'reinstate'
13799 * lock requests, just return the error.
13801 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13803 if (cmd == F_SETLKW) {
13804 rnode4_t *rp = VTOR4(vp);
13805 nfs_argop4 *argop = (*argspp)->array;
13807 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13809 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13810 recov_statep, needrecov);
13811 *did_start_fop = FALSE;
13812 ASSERT((*argspp)->array_len == 2);
13813 if (argop[1].argop == OP_LOCK)
13814 nfs4args_lock_free(&argop[1]);
13815 else if (argop[1].argop == OP_LOCKT)
13816 nfs4args_lockt_free(&argop[1]);
13817 kmem_free(argop, 2 * sizeof (nfs_argop4));
13818 if (*respp)
13819 xdr_free(xdr_COMPOUND4res_clnt,
13820 (caddr_t)*respp);
13821 *argspp = NULL;
13822 *respp = NULL;
13823 nfs4_end_lock_seqid_sync(lop);
13824 lock_owner_rele(lop);
13825 *lopp = NULL;
13826 if (osp != NULL) {
13827 open_stream_rele(osp, rp);
13828 *ospp = NULL;
13830 if (oop != NULL) {
13831 nfs4_end_open_seqid_sync(oop);
13832 open_owner_rele(oop);
13833 *oopp = NULL;
13836 nfs_rw_exit(&rp->r_lkserlock);
13838 intr = nfs4_block_and_wait(tick_delayp, rp);
13840 if (intr) {
13841 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13842 RW_WRITER, FALSE);
13843 *errorp = EINTR;
13844 return (FALSE);
13847 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13848 RW_WRITER, FALSE);
13851 * Make sure we are still safe to lock with
13852 * regards to mmapping.
13854 if (!nfs4_safelock(vp, flk, cr)) {
13855 *errorp = EAGAIN;
13856 return (FALSE);
13859 return (TRUE);
13861 if (ctype == NFS4_LCK_CTYPE_NORM)
13862 *errorp = EAGAIN;
13863 *skip_get_err = TRUE;
13864 flk->l_whence = 0;
13865 *whencep = 0;
13866 return (FALSE);
13867 } else if (lockt_args) {
13868 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13869 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13871 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13872 flk, lockt_args);
13874 /* according to NLM code */
13875 *errorp = 0;
13876 *whencep = 0;
13877 *skip_get_err = TRUE;
13878 return (FALSE);
13880 return (FALSE);
13884 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13886 static void
13887 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13889 switch (resp->status) {
13890 case NFS4ERR_ACCESS:
13891 case NFS4ERR_ADMIN_REVOKED:
13892 case NFS4ERR_BADHANDLE:
13893 case NFS4ERR_BAD_RANGE:
13894 case NFS4ERR_BAD_SEQID:
13895 case NFS4ERR_BAD_STATEID:
13896 case NFS4ERR_BADXDR:
13897 case NFS4ERR_DEADLOCK:
13898 case NFS4ERR_DELAY:
13899 case NFS4ERR_EXPIRED:
13900 case NFS4ERR_FHEXPIRED:
13901 case NFS4ERR_GRACE:
13902 case NFS4ERR_INVAL:
13903 case NFS4ERR_ISDIR:
13904 case NFS4ERR_LEASE_MOVED:
13905 case NFS4ERR_LOCK_NOTSUPP:
13906 case NFS4ERR_LOCK_RANGE:
13907 case NFS4ERR_MOVED:
13908 case NFS4ERR_NOFILEHANDLE:
13909 case NFS4ERR_NO_GRACE:
13910 case NFS4ERR_OLD_STATEID:
13911 case NFS4ERR_OPENMODE:
13912 case NFS4ERR_RECLAIM_BAD:
13913 case NFS4ERR_RECLAIM_CONFLICT:
13914 case NFS4ERR_RESOURCE:
13915 case NFS4ERR_SERVERFAULT:
13916 case NFS4ERR_STALE:
13917 case NFS4ERR_STALE_CLIENTID:
13918 case NFS4ERR_STALE_STATEID:
13919 return;
13920 default:
13921 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13922 "nfs4frlock_results_default: got unrecognizable "
13923 "res.status %d", resp->status));
13924 *errorp = NFS4ERR_INVAL;
13929 * The lock request was successful, so update the client's state.
13931 static void
13932 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13933 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13934 vnode_t *vp, flock64_t *flk, cred_t *cr,
13935 nfs4_lost_rqst_t *resend_rqstp)
13937 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13939 if (lock_args) {
13940 LOCK4res *lock_res;
13942 lock_res = &resop->nfs_resop4_u.oplock;
13943 /* update the stateid with server's response */
13945 if (lock_args->locker.new_lock_owner == TRUE) {
13946 mutex_enter(&lop->lo_lock);
13947 lop->lo_just_created = NFS4_PERM_CREATED;
13948 mutex_exit(&lop->lo_lock);
13951 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13954 * If the lock was the result of a resending a lost
13955 * request, we've synched up the stateid and seqid
13956 * with the server, but now the server might be out of sync
13957 * with what the application thinks it has for locks.
13958 * Clean that up here. It's unclear whether we should do
13959 * this even if the filesystem has been forcibly unmounted.
13960 * For most servers, it's probably wasted effort, but
13961 * RFC 7530 lets servers require that unlocks exactly match
13962 * the locks that are held.
13964 if (resend_rqstp != NULL &&
13965 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13966 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13967 } else {
13968 flk->l_whence = 0;
13970 } else if (locku_args) {
13971 LOCKU4res *locku_res;
13973 locku_res = &resop->nfs_resop4_u.oplocku;
13975 /* Update the stateid with the server's response */
13976 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
13977 } else if (lockt_args) {
13978 /* Switch the lock type to express success, see fcntl */
13979 flk->l_type = F_UNLCK;
13980 flk->l_whence = 0;
13985 * Do final cleanup before exiting nfs4frlock.
13986 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13987 * COMPOUND4 args/res for calls that haven't already.
13989 static void
13990 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
13991 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
13992 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
13993 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13994 short whence, uoff_t offset, struct lm_sysid *ls,
13995 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
13996 bool_t did_start_fop, bool_t skip_get_err,
13997 cred_t *cred_otw, cred_t *cred)
13999 mntinfo4_t *mi = VTOMI4(vp);
14000 rnode4_t *rp = VTOR4(vp);
14001 int error = *errorp;
14002 nfs_argop4 *argop;
14003 int do_flush_pages = 0;
14005 ASSERT(nfs_zone() == mi->mi_zone);
14007 * The client recovery code wants the raw status information,
14008 * so don't map the NFS status code to an errno value for
14009 * non-normal call types.
14011 if (ctype == NFS4_LCK_CTYPE_NORM) {
14012 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14013 *errorp = geterrno4(resp->status);
14014 if (did_start_fop == TRUE)
14015 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14016 needrecov);
14019 * We've established a new lock on the server, so invalidate
14020 * the pages associated with the vnode to get the most up to
14021 * date pages from the server after acquiring the lock. We
14022 * want to be sure that the read operation gets the newest data.
14023 * N.B.
14024 * We used to do this in nfs4frlock_results_ok but that doesn't
14025 * work since fop_putpage can call nfs4_commit which calls
14026 * nfs4_start_fop. We flush the pages below after calling
14027 * nfs4_end_fop above
14028 * The flush of the page cache must be done after
14029 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14031 if (!error && resp && resp->status == NFS4_OK)
14032 do_flush_pages = 1;
14034 if (argsp) {
14035 ASSERT(argsp->array_len == 2);
14036 argop = argsp->array;
14037 if (argop[1].argop == OP_LOCK)
14038 nfs4args_lock_free(&argop[1]);
14039 else if (argop[1].argop == OP_LOCKT)
14040 nfs4args_lockt_free(&argop[1]);
14041 kmem_free(argop, 2 * sizeof (nfs_argop4));
14042 if (resp)
14043 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14046 /* free the reference on the lock owner */
14047 if (lop != NULL) {
14048 nfs4_end_lock_seqid_sync(lop);
14049 lock_owner_rele(lop);
14052 /* need to free up the reference on osp for lock args */
14053 if (osp != NULL)
14054 open_stream_rele(osp, rp);
14056 /* need to free up the reference on oop for lock args */
14057 if (oop != NULL) {
14058 nfs4_end_open_seqid_sync(oop);
14059 open_owner_rele(oop);
14062 if (do_flush_pages)
14063 nfs4_flush_pages(vp, cred);
14065 (void) convoff(vp, flk, whence, offset);
14067 lm_rel_sysid(ls);
14070 * Record debug information in the event we get EINVAL.
14072 mutex_enter(&mi->mi_lock);
14073 if (*errorp == EINVAL && (lock_args || locku_args) &&
14074 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14075 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14076 zcmn_err(getzoneid(), CE_NOTE,
14077 "%s operation failed with "
14078 "EINVAL probably since the server, %s,"
14079 " doesn't support POSIX style locking",
14080 lock_args ? "LOCK" : "LOCKU",
14081 mi->mi_curr_serv->sv_hostname);
14082 mi->mi_flags |= MI4_LOCK_DEBUG;
14085 mutex_exit(&mi->mi_lock);
14087 if (cred_otw)
14088 crfree(cred_otw);
14092 * This calls the server and the local locking code.
14094 * Client locks are registerred locally by oring the sysid with
14095 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14096 * We need to distinguish between the two to avoid collision in case one
14097 * machine is used as both client and server.
14099 * Blocking lock requests will continually retry to acquire the lock
14100 * forever.
14102 * The ctype is defined as follows:
14103 * NFS4_LCK_CTYPE_NORM: normal lock request.
14105 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14106 * recovery, get the pid from flk instead of curproc, and don't reregister
14107 * the lock locally.
14109 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14110 * that we will use the information passed in via resend_rqstp to setup the
14111 * lock/locku request. This resend is the exact same request as the 'lost
14112 * lock', and is initiated by the recovery framework. A successful resend
14113 * request can initiate one or more reinstate requests.
14115 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14116 * does not trigger additional reinstate requests. This lock call type is
14117 * set for setting the v4 server's locking state back to match what the
14118 * client's local locking state is in the event of a received 'lost lock'.
14120 * Errors are returned via the nfs4_error_t parameter.
14122 void
14123 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14124 int flag, uoff_t offset, cred_t *cr, nfs4_error_t *ep,
14125 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14127 COMPOUND4args_clnt args, *argsp = NULL;
14128 COMPOUND4res_clnt res, *resp = NULL;
14129 nfs_argop4 *argop;
14130 nfs_resop4 *resop;
14131 rnode4_t *rp;
14132 int doqueue = 1;
14133 clock_t tick_delay; /* delay in clock ticks */
14134 struct lm_sysid *ls;
14135 LOCK4args *lock_args = NULL;
14136 LOCKU4args *locku_args = NULL;
14137 LOCKT4args *lockt_args = NULL;
14138 nfs4_open_owner_t *oop = NULL;
14139 nfs4_open_stream_t *osp = NULL;
14140 nfs4_lock_owner_t *lop = NULL;
14141 bool_t needrecov = FALSE;
14142 nfs4_recov_state_t recov_state;
14143 short whence;
14144 nfs4_op_hint_t op_hint;
14145 nfs4_lost_rqst_t lost_rqst;
14146 bool_t retry = FALSE;
14147 bool_t did_start_fop = FALSE;
14148 bool_t skip_get_err = FALSE;
14149 cred_t *cred_otw = NULL;
14150 bool_t recovonly; /* just queue request */
14151 int frc_no_reclaim = 0;
14152 #ifdef DEBUG
14153 char *name;
14154 #endif
14156 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14158 #ifdef DEBUG
14159 name = fn_name(VTOSV(vp)->sv_name);
14160 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14161 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14162 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14163 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14164 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14165 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14166 resend_rqstp ? "TRUE" : "FALSE"));
14167 kmem_free(name, MAXNAMELEN);
14168 #endif
14170 nfs4_error_zinit(ep);
14171 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14172 if (ep->error)
14173 return;
14174 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14175 if (ep->error)
14176 return;
14177 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14178 vp, cr, &cred_otw);
14180 recov_retry:
14181 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14182 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14183 rp = VTOR4(vp);
14185 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14186 &did_start_fop, &recovonly);
14188 if (ep->error)
14189 goto out;
14191 if (recovonly) {
14193 * Leave the request for the recovery system to deal with.
14195 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14196 ASSERT(cmd != F_GETLK);
14197 ASSERT(flk->l_type == F_UNLCK);
14199 nfs4_error_init(ep, EINTR);
14200 needrecov = TRUE;
14201 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14202 if (lop != NULL) {
14203 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14204 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14205 (void) nfs4_start_recovery(ep,
14206 VTOMI4(vp), vp, NULL, NULL,
14207 (lost_rqst.lr_op == OP_LOCK ||
14208 lost_rqst.lr_op == OP_LOCKU) ?
14209 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14210 lock_owner_rele(lop);
14211 lop = NULL;
14213 flk->l_pid = curproc->p_pid;
14214 nfs4_register_lock_locally(vp, flk, flag, offset);
14215 goto out;
14218 /* putfh directory fh */
14219 argop[0].argop = OP_CPUTFH;
14220 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14223 * Set up the over-the-wire arguments and get references to the
14224 * open owner, etc.
14227 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14228 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14229 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14230 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14231 } else {
14232 bool_t go_otw = TRUE;
14234 ASSERT(resend_rqstp == NULL);
14236 switch (cmd) {
14237 case F_GETLK:
14238 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14239 &lockt_args, argsp, flk, rp);
14240 break;
14241 case F_SETLKW:
14242 case F_SETLK:
14243 if (flk->l_type == F_UNLCK)
14244 nfs4frlock_setup_locku_args(ctype,
14245 &argop[1], &locku_args, flk,
14246 &lop, ep, argsp,
14247 vp, flag, offset, cr,
14248 &skip_get_err, &go_otw);
14249 else
14250 nfs4frlock_setup_lock_args(ctype,
14251 &lock_args, &oop, &osp, &lop, &argop[1],
14252 argsp, flk, cmd, vp, cr, ep);
14254 if (ep->error)
14255 goto out;
14257 switch (ep->stat) {
14258 case NFS4_OK:
14259 break;
14260 case NFS4ERR_DELAY:
14261 /* recov thread never gets this error */
14262 ASSERT(resend_rqstp == NULL);
14263 ASSERT(did_start_fop);
14265 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14266 &recov_state, TRUE);
14267 did_start_fop = FALSE;
14268 if (argop[1].argop == OP_LOCK)
14269 nfs4args_lock_free(&argop[1]);
14270 else if (argop[1].argop == OP_LOCKT)
14271 nfs4args_lockt_free(&argop[1]);
14272 kmem_free(argop, 2 * sizeof (nfs_argop4));
14273 argsp = NULL;
14274 goto recov_retry;
14275 default:
14276 ep->error = EIO;
14277 goto out;
14279 break;
14280 default:
14281 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14282 "nfs4_frlock: invalid cmd %d", cmd));
14283 ep->error = EINVAL;
14284 goto out;
14287 if (!go_otw)
14288 goto out;
14291 /* XXX should we use the local reclock as a cache ? */
14293 * Unregister the lock with the local locking code before
14294 * contacting the server. This avoids a potential race where
14295 * another process gets notified that it has been granted a lock
14296 * before we can unregister ourselves locally.
14298 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14299 if (ctype == NFS4_LCK_CTYPE_NORM)
14300 flk->l_pid = ttoproc(curthread)->p_pid;
14301 nfs4_register_lock_locally(vp, flk, flag, offset);
14305 * Send the server the lock request. Continually loop with a delay
14306 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14308 resp = &res;
14310 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14311 (CE_NOTE,
14312 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14313 rnode4info(rp)));
14315 if (lock_args && frc_no_reclaim) {
14316 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14317 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14318 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14319 lock_args->reclaim = FALSE;
14320 if (did_reclaimp)
14321 *did_reclaimp = 0;
14325 * Do the OTW call.
14327 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14329 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14330 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14332 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14333 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14334 "nfs4frlock: needrecov %d", needrecov));
14336 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14337 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14338 args.ctag);
14341 * Check if one of these mutually exclusive error cases has
14342 * happened:
14343 * need to swap credentials due to access error
14344 * recovery is needed
14345 * different error (only known case is missing Kerberos ticket)
14348 if ((ep->error == EACCES ||
14349 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14350 cred_otw != cr) {
14351 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14352 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14353 cr, &cred_otw);
14354 goto recov_retry;
14357 if (needrecov) {
14359 * LOCKT requests don't need to recover from lost
14360 * requests since they don't create/modify state.
14362 if ((ep->error == EINTR ||
14363 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14364 lockt_args)
14365 goto out;
14367 * Do not attempt recovery for requests initiated by
14368 * the recovery framework. Let the framework redrive them.
14370 if (ctype != NFS4_LCK_CTYPE_NORM)
14371 goto out;
14372 else {
14373 ASSERT(resend_rqstp == NULL);
14376 nfs4frlock_save_lost_rqst(ctype, ep->error,
14377 flk_to_locktype(cmd, flk->l_type),
14378 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14380 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14381 &resp, lock_args, locku_args, &oop, &osp, &lop,
14382 rp, vp, &recov_state, op_hint, &did_start_fop,
14383 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14385 if (retry) {
14386 ASSERT(oop == NULL);
14387 ASSERT(osp == NULL);
14388 ASSERT(lop == NULL);
14389 goto recov_retry;
14391 goto out;
14395 * Bail out if have reached this point with ep->error set. Can
14396 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14397 * This happens if Kerberos ticket has expired or has been
14398 * destroyed.
14400 if (ep->error != 0)
14401 goto out;
14404 * Process the reply.
14406 switch (resp->status) {
14407 case NFS4_OK:
14408 resop = &resp->array[1];
14409 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14410 resend_rqstp);
14412 * Have a successful lock operation, now update state.
14414 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14415 resop, lop, vp, flk, cr, resend_rqstp);
14416 break;
14418 case NFS4ERR_DENIED:
14419 resop = &resp->array[1];
14420 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14421 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14422 &recov_state, needrecov, &argsp, &resp,
14423 &tick_delay, &whence, &ep->error, resop, cr,
14424 &did_start_fop, &skip_get_err);
14426 if (retry) {
14427 ASSERT(oop == NULL);
14428 ASSERT(osp == NULL);
14429 ASSERT(lop == NULL);
14430 goto recov_retry;
14432 break;
14434 * If the server won't let us reclaim, fall-back to trying to lock
14435 * the file from scratch. Code elsewhere will check the changeinfo
14436 * to ensure the file hasn't been changed.
14438 case NFS4ERR_NO_GRACE:
14439 if (lock_args && lock_args->reclaim == TRUE) {
14440 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14441 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14442 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14443 frc_no_reclaim = 1;
14444 /* clean up before retrying */
14445 needrecov = 0;
14446 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14447 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14448 &recov_state, op_hint, &did_start_fop, NULL, flk);
14449 goto recov_retry;
14451 /* FALLTHROUGH */
14453 default:
14454 nfs4frlock_results_default(resp, &ep->error);
14455 break;
14457 out:
14459 * Process and cleanup from error. Make interrupted unlock
14460 * requests look successful, since they will be handled by the
14461 * client recovery code.
14463 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14464 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14465 lock_args, locku_args, did_start_fop,
14466 skip_get_err, cred_otw, cr);
14468 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14469 (cmd == F_SETLK || cmd == F_SETLKW))
14470 ep->error = 0;
14474 * nfs4_safelock:
14476 * Return non-zero if the given lock request can be handled without
14477 * violating the constraints on concurrent mapping and locking.
14480 static int
14481 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14483 rnode4_t *rp = VTOR4(vp);
14484 struct vattr va;
14485 int error;
14487 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14488 ASSERT(rp->r_mapcnt >= 0);
14489 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14490 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14491 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14492 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14494 if (rp->r_mapcnt == 0)
14495 return (1); /* always safe if not mapped */
14498 * If the file is already mapped and there are locks, then they
14499 * should be all safe locks. So adding or removing a lock is safe
14500 * as long as the new request is safe (i.e., whole-file, meaning
14501 * length and starting offset are both zero).
14504 if (bfp->l_start != 0 || bfp->l_len != 0) {
14505 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14506 "cannot lock a memory mapped file unless locking the "
14507 "entire file: start %"PRIx64", len %"PRIx64,
14508 bfp->l_start, bfp->l_len));
14509 return (0);
14512 /* mandatory locking and mapping don't mix */
14513 va.va_mask = AT_MODE;
14514 error = fop_getattr(vp, &va, 0, cr, NULL);
14515 if (error != 0) {
14516 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14517 "getattr error %d", error));
14518 return (0); /* treat errors conservatively */
14520 if (MANDLOCK(vp, va.va_mode)) {
14521 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14522 "cannot mandatory lock and mmap a file"));
14523 return (0);
14526 return (1);
14531 * Register the lock locally within Solaris.
14532 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14533 * recording locks locally.
14535 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14536 * are registered locally.
14538 void
14539 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14540 uoff_t offset)
14542 int oldsysid;
14543 int error;
14544 #ifdef DEBUG
14545 char *name;
14546 #endif
14548 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14550 #ifdef DEBUG
14551 name = fn_name(VTOSV(vp)->sv_name);
14552 NFS4_DEBUG(nfs4_client_lock_debug,
14553 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14554 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14555 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14556 flk->l_sysid));
14557 kmem_free(name, MAXNAMELEN);
14558 #endif
14560 /* register the lock with local locking */
14561 oldsysid = flk->l_sysid;
14562 flk->l_sysid |= LM_SYSID_CLIENT;
14563 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14564 #ifdef DEBUG
14565 if (error != 0) {
14566 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14567 "nfs4_register_lock_locally: could not register with"
14568 " local locking"));
14569 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14570 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14571 error, (void *)vp, flk->l_pid, flk->l_sysid));
14572 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14573 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14574 flk->l_type, flk->l_start, flk->l_len));
14575 (void) reclock(vp, flk, 0, flag, offset, NULL);
14576 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14577 "blocked by pid %d sysid 0x%x type %d "
14578 "off 0x%" PRIx64 " len 0x%" PRIx64,
14579 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14580 flk->l_len));
14582 #endif
14583 flk->l_sysid = oldsysid;
14587 * nfs4_lockrelease:
14589 * Release any locks on the given vnode that are held by the current
14590 * process. Also removes the lock owner (if one exists) from the rnode's
14591 * list.
14593 static int
14594 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14596 flock64_t ld;
14597 int ret, error;
14598 rnode4_t *rp;
14599 nfs4_lock_owner_t *lop;
14600 nfs4_recov_state_t recov_state;
14601 mntinfo4_t *mi;
14602 bool_t possible_orphan = FALSE;
14603 bool_t recovonly;
14605 ASSERT((uintptr_t)vp > KERNELBASE);
14606 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14608 rp = VTOR4(vp);
14609 mi = VTOMI4(vp);
14612 * If we have not locked anything then we can
14613 * just return since we have no work to do.
14615 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14616 return (0);
14620 * We need to comprehend that another thread may
14621 * kick off recovery and the lock_owner we have stashed
14622 * in lop might be invalid so we should NOT cache it
14623 * locally!
14625 recov_state.rs_flags = 0;
14626 recov_state.rs_num_retry_despite_err = 0;
14627 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14628 &recovonly);
14629 if (error) {
14630 mutex_enter(&rp->r_statelock);
14631 rp->r_flags |= R4LODANGLERS;
14632 mutex_exit(&rp->r_statelock);
14633 return (error);
14636 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14639 * Check if the lock owner might have a lock (request was sent but
14640 * no response was received). Also check if there are any remote
14641 * locks on the file. (In theory we shouldn't have to make this
14642 * second check if there's no lock owner, but for now we'll be
14643 * conservative and do it anyway.) If either condition is true,
14644 * send an unlock for the entire file to the server.
14646 * Note that no explicit synchronization is needed here. At worst,
14647 * flk_has_remote_locks() will return a false positive, in which case
14648 * the unlock call wastes time but doesn't harm correctness.
14651 if (lop) {
14652 mutex_enter(&lop->lo_lock);
14653 possible_orphan = lop->lo_pending_rqsts;
14654 mutex_exit(&lop->lo_lock);
14655 lock_owner_rele(lop);
14658 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14660 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14661 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14662 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14663 (void *)lop));
14665 if (possible_orphan || flk_has_remote_locks(vp)) {
14666 ld.l_type = F_UNLCK; /* set to unlock entire file */
14667 ld.l_whence = 0; /* unlock from start of file */
14668 ld.l_start = 0;
14669 ld.l_len = 0; /* do entire file */
14671 ret = fop_frlock(vp, F_SETLK, &ld, flag, offset, NULL,
14672 cr, NULL);
14674 if (ret != 0) {
14676 * If fop_frlock fails, make sure we unregister
14677 * local locks before we continue.
14679 ld.l_pid = ttoproc(curthread)->p_pid;
14680 nfs4_register_lock_locally(vp, &ld, flag, offset);
14681 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14682 "nfs4_lockrelease: lock release error on vp"
14683 " %p: error %d.\n", (void *)vp, ret));
14687 recov_state.rs_flags = 0;
14688 recov_state.rs_num_retry_despite_err = 0;
14689 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14690 &recovonly);
14691 if (error) {
14692 mutex_enter(&rp->r_statelock);
14693 rp->r_flags |= R4LODANGLERS;
14694 mutex_exit(&rp->r_statelock);
14695 return (error);
14699 * So, here we're going to need to retrieve the lock-owner
14700 * again (in case recovery has done a switch-a-roo) and
14701 * remove it because we can.
14703 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14705 if (lop) {
14706 nfs4_rnode_remove_lock_owner(rp, lop);
14707 lock_owner_rele(lop);
14710 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14711 return (0);
14715 * Wait for 'tick_delay' clock ticks.
14716 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14717 * NOTE: lock_lease_time is in seconds.
14719 * XXX For future improvements, should implement a waiting queue scheme.
14721 static int
14722 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14724 long milliseconds_delay;
14725 time_t lock_lease_time;
14727 /* wait tick_delay clock ticks or siginteruptus */
14728 if (delay_sig(*tick_delay)) {
14729 return (EINTR);
14731 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14732 "reissue the lock request: blocked for %ld clock ticks: %ld "
14733 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14735 /* get the lease time */
14736 lock_lease_time = r2lease_time(rp);
14738 /* drv_hztousec converts ticks to microseconds */
14739 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14740 if (milliseconds_delay < lock_lease_time * 1000) {
14741 *tick_delay = 2 * *tick_delay;
14742 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14743 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14745 return (0);
14749 void
14750 nfs4_vnops_init(void)
14754 void
14755 nfs4_vnops_fini(void)
14760 * Return a reference to the directory (parent) vnode for a given vnode,
14761 * using the saved pathname information and the directory file handle. The
14762 * caller is responsible for disposing of the reference.
14763 * Returns zero or an errno value.
14765 * Caller should set need_start_op to FALSE if it is the recovery
14766 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14769 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14771 svnode_t *svnp;
14772 vnode_t *dvp = NULL;
14773 servinfo4_t *svp;
14774 nfs4_fname_t *mfname;
14775 int error;
14777 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14779 if (vp->v_flag & VROOT) {
14780 nfs4_sharedfh_t *sfh;
14781 nfs_fh4 fh;
14782 mntinfo4_t *mi;
14784 ASSERT(vp->v_type == VREG);
14786 mi = VTOMI4(vp);
14787 svp = mi->mi_curr_serv;
14788 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14789 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14790 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14791 sfh = sfh4_get(&fh, VTOMI4(vp));
14792 nfs_rw_exit(&svp->sv_lock);
14793 mfname = mi->mi_fname;
14794 fn_hold(mfname);
14795 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14796 sfh4_rele(&sfh);
14798 if (dvp->v_type == VNON)
14799 dvp->v_type = VDIR;
14800 *dvpp = dvp;
14801 return (0);
14804 svnp = VTOSV(vp);
14806 if (svnp == NULL) {
14807 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14808 "shadow node is NULL"));
14809 return (EINVAL);
14812 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14813 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14814 "shadow node name or dfh val == NULL"));
14815 return (EINVAL);
14818 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14819 (int)need_start_op);
14820 if (error != 0) {
14821 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14822 "nfs4_make_dotdot returned %d", error));
14823 return (error);
14825 if (!dvp) {
14826 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14827 "nfs4_make_dotdot returned a NULL dvp"));
14828 return (EIO);
14830 if (dvp->v_type == VNON)
14831 dvp->v_type = VDIR;
14832 ASSERT(dvp->v_type == VDIR);
14833 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14834 mutex_enter(&dvp->v_lock);
14835 dvp->v_flag |= V_XATTRDIR;
14836 mutex_exit(&dvp->v_lock);
14838 *dvpp = dvp;
14839 return (0);
14843 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14844 * length that fnamep can accept, including the trailing null.
14845 * Returns 0 if okay, returns an errno value if there was a problem.
14849 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14851 char *fn;
14852 int err = 0;
14853 servinfo4_t *svp;
14854 svnode_t *shvp;
14857 * If the file being opened has VROOT set, then this is
14858 * a "file" mount. sv_name will not be interesting, so
14859 * go back to the servinfo4 to get the original mount
14860 * path and strip off all but the final edge. Otherwise
14861 * just return the name from the shadow vnode.
14864 if (vp->v_flag & VROOT) {
14866 svp = VTOMI4(vp)->mi_curr_serv;
14867 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14869 fn = strrchr(svp->sv_path, '/');
14870 if (fn == NULL)
14871 err = EINVAL;
14872 else
14873 fn++;
14874 } else {
14875 shvp = VTOSV(vp);
14876 fn = fn_name(shvp->sv_name);
14879 if (err == 0)
14880 if (strlen(fn) < maxlen)
14881 (void) strcpy(fnamep, fn);
14882 else
14883 err = ENAMETOOLONG;
14885 if (vp->v_flag & VROOT)
14886 nfs_rw_exit(&svp->sv_lock);
14887 else
14888 kmem_free(fn, MAXNAMELEN);
14890 return (err);
14894 * Bookkeeping for a close that doesn't need to go over the wire.
14895 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14896 * it is left at 1.
14898 void
14899 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14901 rnode4_t *rp;
14902 mntinfo4_t *mi;
14904 mi = VTOMI4(vp);
14905 rp = VTOR4(vp);
14907 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14908 "rp=%p osp=%p", (void *)rp, (void *)osp));
14909 ASSERT(nfs_zone() == mi->mi_zone);
14910 ASSERT(mutex_owned(&osp->os_sync_lock));
14911 ASSERT(*have_lockp);
14913 if (!osp->os_valid ||
14914 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14915 return;
14919 * This removes the reference obtained at OPEN; ie,
14920 * when the open stream structure was created.
14922 * We don't have to worry about calling 'open_stream_rele'
14923 * since we our currently holding a reference to this
14924 * open stream which means the count can not go to 0 with
14925 * this decrement.
14927 ASSERT(osp->os_ref_count >= 2);
14928 osp->os_ref_count--;
14929 osp->os_valid = 0;
14930 mutex_exit(&osp->os_sync_lock);
14931 *have_lockp = 0;
14933 nfs4_dec_state_ref_count(mi);
14937 * Close all remaining open streams on the rnode. These open streams
14938 * could be here because:
14939 * - The close attempted at either close or delmap failed
14940 * - Some kernel entity did fop_open but never did fop_close
14941 * - Someone did mknod on a regular file but never opened it
14944 nfs4close_all(vnode_t *vp, cred_t *cr)
14946 nfs4_open_stream_t *osp;
14947 int error;
14948 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14949 rnode4_t *rp;
14951 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14953 error = 0;
14954 rp = VTOR4(vp);
14957 * At this point, all we know is that the last time
14958 * someone called vn_rele, the count was 1. Since then,
14959 * the vnode could have been re-activated. We want to
14960 * loop through the open streams and close each one, but
14961 * we have to be careful since once we release the rnode
14962 * hash bucket lock, someone else is free to come in and
14963 * re-activate the rnode and add new open streams. The
14964 * strategy is take the rnode hash bucket lock, verify that
14965 * the count is still 1, grab the open stream off the
14966 * head of the list and mark it invalid, then release the
14967 * rnode hash bucket lock and proceed with that open stream.
14968 * This is ok because nfs4close_one() will acquire the proper
14969 * open/create to close/destroy synchronization for open
14970 * streams, and will ensure that if someone has reopened
14971 * the open stream after we've dropped the hash bucket lock
14972 * then we'll just simply return without destroying the
14973 * open stream.
14974 * Repeat until the list is empty.
14977 for (;;) {
14979 /* make sure vnode hasn't been reactivated */
14980 rw_enter(&rp->r_hashq->r_lock, RW_READER);
14981 mutex_enter(&vp->v_lock);
14982 if (vp->v_count > 1) {
14983 mutex_exit(&vp->v_lock);
14984 rw_exit(&rp->r_hashq->r_lock);
14985 break;
14988 * Grabbing r_os_lock before releasing v_lock prevents
14989 * a window where the rnode/open stream could get
14990 * reactivated (and os_force_close set to 0) before we
14991 * had a chance to set os_force_close to 1.
14993 mutex_enter(&rp->r_os_lock);
14994 mutex_exit(&vp->v_lock);
14996 osp = list_head(&rp->r_open_streams);
14997 if (!osp) {
14998 /* nothing left to CLOSE OTW, so return */
14999 mutex_exit(&rp->r_os_lock);
15000 rw_exit(&rp->r_hashq->r_lock);
15001 break;
15004 mutex_enter(&rp->r_statev4_lock);
15005 /* the file can't still be mem mapped */
15006 ASSERT(rp->r_mapcnt == 0);
15007 if (rp->created_v4)
15008 rp->created_v4 = 0;
15009 mutex_exit(&rp->r_statev4_lock);
15012 * Grab a ref on this open stream; nfs4close_one
15013 * will mark it as invalid
15015 mutex_enter(&osp->os_sync_lock);
15016 osp->os_ref_count++;
15017 osp->os_force_close = 1;
15018 mutex_exit(&osp->os_sync_lock);
15019 mutex_exit(&rp->r_os_lock);
15020 rw_exit(&rp->r_hashq->r_lock);
15022 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15024 /* Update error if it isn't already non-zero */
15025 if (error == 0) {
15026 if (e.error)
15027 error = e.error;
15028 else if (e.stat)
15029 error = geterrno4(e.stat);
15032 #ifdef DEBUG
15033 nfs4close_all_cnt++;
15034 #endif
15035 /* Release the ref on osp acquired above. */
15036 open_stream_rele(osp, rp);
15038 /* Proceed to the next open stream, if any */
15040 return (error);
15044 * nfs4close_one - close one open stream for a file if needed.
15046 * "close_type" indicates which close path this is:
15047 * CLOSE_NORM: close initiated via fop_close.
15048 * CLOSE_DELMAP: close initiated via fop_delmap.
15049 * CLOSE_FORCE: close initiated via fop_inactive. This path forces
15050 * the close and release of client state for this open stream
15051 * (unless someone else has the open stream open).
15052 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15053 * (e.g., due to abort because of a signal).
15054 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15056 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15057 * recovery. Instead, the caller is expected to deal with retries.
15059 * The caller can either pass in the osp ('provided_osp') or not.
15061 * 'access_bits' represents the access we are closing/downgrading.
15063 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15064 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15065 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15067 * Errors are returned via the nfs4_error_t.
15069 void
15070 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15071 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15072 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15073 uint_t mmap_flags)
15075 nfs4_open_owner_t *oop;
15076 nfs4_open_stream_t *osp = NULL;
15077 int retry = 0;
15078 int num_retries = NFS4_NUM_RECOV_RETRIES;
15079 rnode4_t *rp;
15080 mntinfo4_t *mi;
15081 nfs4_recov_state_t recov_state;
15082 cred_t *cred_otw = NULL;
15083 bool_t recovonly = FALSE;
15084 int isrecov;
15085 int force_close;
15086 int close_failed = 0;
15087 int did_dec_count = 0;
15088 int did_start_op = 0;
15089 int did_force_recovlock = 0;
15090 int did_start_seqid_sync = 0;
15091 int have_sync_lock = 0;
15093 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15095 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15096 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15097 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15098 len, maxprot, mmap_flags, access_bits));
15100 nfs4_error_zinit(ep);
15101 rp = VTOR4(vp);
15102 mi = VTOMI4(vp);
15103 isrecov = (close_type == CLOSE_RESEND ||
15104 close_type == CLOSE_AFTER_RESEND);
15107 * First get the open owner.
15109 if (!provided_osp) {
15110 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15111 } else {
15112 oop = provided_osp->os_open_owner;
15113 ASSERT(oop != NULL);
15114 open_owner_hold(oop);
15117 if (!oop) {
15118 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15119 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15120 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15121 (void *)provided_osp, close_type));
15122 ep->error = EIO;
15123 goto out;
15126 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15127 recov_retry:
15128 osp = NULL;
15129 close_failed = 0;
15130 force_close = (close_type == CLOSE_FORCE);
15131 retry = 0;
15132 did_start_op = 0;
15133 did_force_recovlock = 0;
15134 did_start_seqid_sync = 0;
15135 have_sync_lock = 0;
15136 recovonly = FALSE;
15137 recov_state.rs_flags = 0;
15138 recov_state.rs_num_retry_despite_err = 0;
15141 * Second synchronize with recovery.
15143 if (!isrecov) {
15144 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15145 &recov_state, &recovonly);
15146 if (!ep->error) {
15147 did_start_op = 1;
15148 } else {
15149 close_failed = 1;
15151 * If we couldn't get start_fop, but have to
15152 * cleanup state, then at least acquire the
15153 * mi_recovlock so we can synchronize with
15154 * recovery.
15156 if (close_type == CLOSE_FORCE) {
15157 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15158 RW_READER, FALSE);
15159 did_force_recovlock = 1;
15160 } else
15161 goto out;
15166 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15167 * set 'recovonly' to TRUE since most likely this is due to
15168 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15169 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15170 * to retry, causing us to loop until recovery finishes. Plus we
15171 * don't need protection over the open seqid since we're not going
15172 * OTW, hence don't need to use the seqid.
15174 if (recovonly == FALSE) {
15175 /* need to grab the open owner sync before 'os_sync_lock' */
15176 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15177 if (ep->error == EAGAIN) {
15178 ASSERT(!isrecov);
15179 if (did_start_op)
15180 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15181 &recov_state, TRUE);
15182 if (did_force_recovlock)
15183 nfs_rw_exit(&mi->mi_recovlock);
15184 goto recov_retry;
15186 did_start_seqid_sync = 1;
15190 * Third get an open stream and acquire 'os_sync_lock' to
15191 * sychronize the opening/creating of an open stream with the
15192 * closing/destroying of an open stream.
15194 if (!provided_osp) {
15195 /* returns with 'os_sync_lock' held */
15196 osp = find_open_stream(oop, rp);
15197 if (!osp) {
15198 ep->error = EIO;
15199 goto out;
15201 } else {
15202 osp = provided_osp;
15203 open_stream_hold(osp);
15204 mutex_enter(&osp->os_sync_lock);
15206 have_sync_lock = 1;
15208 ASSERT(oop == osp->os_open_owner);
15211 * Fourth, do any special pre-OTW CLOSE processing
15212 * based on the specific close type.
15214 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15215 !did_dec_count) {
15216 ASSERT(osp->os_open_ref_count > 0);
15217 osp->os_open_ref_count--;
15218 did_dec_count = 1;
15219 if (osp->os_open_ref_count == 0)
15220 osp->os_final_close = 1;
15223 if (close_type == CLOSE_FORCE) {
15224 /* see if somebody reopened the open stream. */
15225 if (!osp->os_force_close) {
15226 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15227 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15228 "was reopened, vp %p", (void *)osp, (void *)vp));
15229 ep->error = 0;
15230 ep->stat = NFS4_OK;
15231 goto out;
15234 if (!osp->os_final_close && !did_dec_count) {
15235 osp->os_open_ref_count--;
15236 did_dec_count = 1;
15240 * We can't depend on os_open_ref_count being 0 due to the
15241 * way executables are opened (VN_RELE to match a fop_open).
15243 #ifdef NOTYET
15244 ASSERT(osp->os_open_ref_count == 0);
15245 #endif
15246 if (osp->os_open_ref_count != 0) {
15247 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15248 "nfs4close_one: should panic here on an "
15249 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15250 "since this is probably the exec problem."));
15252 osp->os_open_ref_count = 0;
15256 * There is the possibility that nfs4close_one()
15257 * for close_type == CLOSE_DELMAP couldn't find the
15258 * open stream, thus couldn't decrement its os_mapcnt;
15259 * therefore we can't use this ASSERT yet.
15261 #ifdef NOTYET
15262 ASSERT(osp->os_mapcnt == 0);
15263 #endif
15264 osp->os_mapcnt = 0;
15267 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15268 ASSERT(osp->os_mapcnt >= btopr(len));
15270 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15271 osp->os_mmap_write -= btopr(len);
15272 if (maxprot & PROT_READ)
15273 osp->os_mmap_read -= btopr(len);
15274 if (maxprot & PROT_EXEC)
15275 osp->os_mmap_read -= btopr(len);
15276 /* mirror the PROT_NONE check in nfs4_addmap() */
15277 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15278 !(maxprot & PROT_EXEC))
15279 osp->os_mmap_read -= btopr(len);
15280 osp->os_mapcnt -= btopr(len);
15281 did_dec_count = 1;
15284 if (recovonly) {
15285 nfs4_lost_rqst_t lost_rqst;
15287 /* request should not already be in recovery queue */
15288 ASSERT(lrp == NULL);
15289 nfs4_error_init(ep, EINTR);
15290 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15291 osp, cred_otw, vp);
15292 mutex_exit(&osp->os_sync_lock);
15293 have_sync_lock = 0;
15294 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15295 lost_rqst.lr_op == OP_CLOSE ?
15296 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15297 close_failed = 1;
15298 force_close = 0;
15299 goto close_cleanup;
15303 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15304 * we stopped operating on the open owner's <old oo_name, old seqid>
15305 * space, which means we stopped operating on the open stream
15306 * too. So don't go OTW (as the seqid is likely bad, and the
15307 * stateid could be stale, potentially triggering a false
15308 * setclientid), and just clean up the client's internal state.
15310 if (osp->os_orig_oo_name != oop->oo_name) {
15311 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15312 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15313 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15314 "oo_name %" PRIx64")",
15315 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15316 oop->oo_name));
15317 close_failed = 1;
15320 /* If the file failed recovery, just quit. */
15321 mutex_enter(&rp->r_statelock);
15322 if (rp->r_flags & R4RECOVERR) {
15323 close_failed = 1;
15325 mutex_exit(&rp->r_statelock);
15328 * If the force close path failed to obtain start_fop
15329 * then skip the OTW close and just remove the state.
15331 if (close_failed)
15332 goto close_cleanup;
15335 * Fifth, check to see if there are still mapped pages or other
15336 * opens using this open stream. If there are then we can't
15337 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15339 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15340 nfs4_lost_rqst_t new_lost_rqst;
15341 bool_t needrecov = FALSE;
15342 cred_t *odg_cred_otw = NULL;
15343 seqid4 open_dg_seqid = 0;
15345 if (osp->os_delegation) {
15347 * If this open stream was never OPENed OTW then we
15348 * surely can't DOWNGRADE it (especially since the
15349 * osp->open_stateid is really a delegation stateid
15350 * when os_delegation is 1).
15352 if (access_bits & FREAD)
15353 osp->os_share_acc_read--;
15354 if (access_bits & FWRITE)
15355 osp->os_share_acc_write--;
15356 osp->os_share_deny_none--;
15357 nfs4_error_zinit(ep);
15358 goto out;
15360 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15361 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15362 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15363 if (needrecov && !isrecov) {
15364 bool_t abort;
15365 nfs4_bseqid_entry_t *bsep = NULL;
15367 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15368 bsep = nfs4_create_bseqid_entry(oop, NULL,
15369 vp, 0,
15370 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15371 open_dg_seqid);
15373 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15374 oop, osp, odg_cred_otw, vp, access_bits, 0);
15375 mutex_exit(&osp->os_sync_lock);
15376 have_sync_lock = 0;
15377 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15378 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15379 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15380 bsep, NULL, NULL);
15381 if (odg_cred_otw)
15382 crfree(odg_cred_otw);
15383 if (bsep)
15384 kmem_free(bsep, sizeof (*bsep));
15386 if (abort == TRUE)
15387 goto out;
15389 if (did_start_seqid_sync) {
15390 nfs4_end_open_seqid_sync(oop);
15391 did_start_seqid_sync = 0;
15393 open_stream_rele(osp, rp);
15395 if (did_start_op)
15396 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15397 &recov_state, FALSE);
15398 if (did_force_recovlock)
15399 nfs_rw_exit(&mi->mi_recovlock);
15401 goto recov_retry;
15402 } else {
15403 if (odg_cred_otw)
15404 crfree(odg_cred_otw);
15406 goto out;
15410 * If this open stream was created as the results of an open
15411 * while holding a delegation, then just release it; no need
15412 * to do an OTW close. Otherwise do a "normal" OTW close.
15414 if (osp->os_delegation) {
15415 nfs4close_notw(vp, osp, &have_sync_lock);
15416 nfs4_error_zinit(ep);
15417 goto out;
15421 * If this stream is not valid, we're done.
15423 if (!osp->os_valid) {
15424 nfs4_error_zinit(ep);
15425 goto out;
15429 * Last open or mmap ref has vanished, need to do an OTW close.
15430 * First check to see if a close is still necessary.
15432 if (osp->os_failed_reopen) {
15433 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15434 "don't close OTW osp %p since reopen failed.",
15435 (void *)osp));
15437 * Reopen of the open stream failed, hence the
15438 * stateid of the open stream is invalid/stale, and
15439 * sending this OTW would incorrectly cause another
15440 * round of recovery. In this case, we need to set
15441 * the 'os_valid' bit to 0 so another thread doesn't
15442 * come in and re-open this open stream before
15443 * this "closing" thread cleans up state (decrementing
15444 * the nfs4_server_t's state_ref_count and decrementing
15445 * the os_ref_count).
15447 osp->os_valid = 0;
15449 * This removes the reference obtained at OPEN; ie,
15450 * when the open stream structure was created.
15452 * We don't have to worry about calling 'open_stream_rele'
15453 * since we our currently holding a reference to this
15454 * open stream which means the count can not go to 0 with
15455 * this decrement.
15457 ASSERT(osp->os_ref_count >= 2);
15458 osp->os_ref_count--;
15459 nfs4_error_zinit(ep);
15460 close_failed = 0;
15461 goto close_cleanup;
15464 ASSERT(osp->os_ref_count > 1);
15467 * Sixth, try the CLOSE OTW.
15469 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15470 close_type, ep, &have_sync_lock);
15472 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15474 * Let the recovery thread be responsible for
15475 * removing the state for CLOSE.
15477 close_failed = 1;
15478 force_close = 0;
15479 retry = 0;
15482 /* See if we need to retry with a different cred */
15483 if ((ep->error == EACCES ||
15484 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15485 cred_otw != cr) {
15486 crfree(cred_otw);
15487 cred_otw = cr;
15488 crhold(cred_otw);
15489 retry = 1;
15492 if (ep->error || ep->stat)
15493 close_failed = 1;
15495 if (retry && !isrecov && num_retries-- > 0) {
15496 if (have_sync_lock) {
15497 mutex_exit(&osp->os_sync_lock);
15498 have_sync_lock = 0;
15500 if (did_start_seqid_sync) {
15501 nfs4_end_open_seqid_sync(oop);
15502 did_start_seqid_sync = 0;
15504 open_stream_rele(osp, rp);
15506 if (did_start_op)
15507 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15508 &recov_state, FALSE);
15509 if (did_force_recovlock)
15510 nfs_rw_exit(&mi->mi_recovlock);
15511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15512 "nfs4close_one: need to retry the close "
15513 "operation"));
15514 goto recov_retry;
15516 close_cleanup:
15518 * Seventh and lastly, process our results.
15520 if (close_failed && force_close) {
15522 * It's ok to drop and regrab the 'os_sync_lock' since
15523 * nfs4close_notw() will recheck to make sure the
15524 * "close"/removal of state should happen.
15526 if (!have_sync_lock) {
15527 mutex_enter(&osp->os_sync_lock);
15528 have_sync_lock = 1;
15531 * This is last call, remove the ref on the open
15532 * stream created by open and clean everything up.
15534 osp->os_pending_close = 0;
15535 nfs4close_notw(vp, osp, &have_sync_lock);
15536 nfs4_error_zinit(ep);
15539 if (!close_failed) {
15540 if (have_sync_lock) {
15541 osp->os_pending_close = 0;
15542 mutex_exit(&osp->os_sync_lock);
15543 have_sync_lock = 0;
15544 } else {
15545 mutex_enter(&osp->os_sync_lock);
15546 osp->os_pending_close = 0;
15547 mutex_exit(&osp->os_sync_lock);
15549 if (did_start_op && recov_state.rs_sp != NULL) {
15550 mutex_enter(&recov_state.rs_sp->s_lock);
15551 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15552 mutex_exit(&recov_state.rs_sp->s_lock);
15553 } else {
15554 nfs4_dec_state_ref_count(mi);
15556 nfs4_error_zinit(ep);
15559 out:
15560 if (have_sync_lock)
15561 mutex_exit(&osp->os_sync_lock);
15562 if (did_start_op)
15563 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15564 recovonly ? TRUE : FALSE);
15565 if (did_force_recovlock)
15566 nfs_rw_exit(&mi->mi_recovlock);
15567 if (cred_otw)
15568 crfree(cred_otw);
15569 if (osp)
15570 open_stream_rele(osp, rp);
15571 if (oop) {
15572 if (did_start_seqid_sync)
15573 nfs4_end_open_seqid_sync(oop);
15574 open_owner_rele(oop);
15579 * Convert information returned by the server in the LOCK4denied
15580 * structure to the form required by fcntl.
15582 static void
15583 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15585 nfs4_lo_name_t *lo;
15587 #ifdef DEBUG
15588 if (denied_to_flk_debug) {
15589 lockt_denied_debug = lockt_denied;
15590 debug_enter("lockt_denied");
15592 #endif
15594 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15595 flk->l_whence = 0; /* aka SEEK_SET */
15596 flk->l_start = lockt_denied->offset;
15597 flk->l_len = lockt_denied->length;
15600 * If the blocking clientid matches our client id, then we can
15601 * interpret the lockowner (since we built it). If not, then
15602 * fabricate a sysid and pid. Note that the l_sysid field
15603 * in *flk already has the local sysid.
15606 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15608 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15609 lo = (nfs4_lo_name_t *)
15610 lockt_denied->owner.owner_val;
15612 flk->l_pid = lo->ln_pid;
15613 } else {
15614 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15615 "denied_to_flk: bad lock owner length\n"));
15617 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15619 } else {
15620 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15621 "denied_to_flk: foreign clientid\n"));
15624 * Construct a new sysid which should be different from
15625 * sysids of other systems.
15628 flk->l_sysid++;
15629 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15633 static pid_t
15634 lo_to_pid(lock_owner4 *lop)
15636 pid_t pid = 0;
15637 uchar_t *cp;
15638 int i;
15640 cp = (uchar_t *)&lop->clientid;
15642 for (i = 0; i < sizeof (lop->clientid); i++)
15643 pid += (pid_t)*cp++;
15645 cp = (uchar_t *)lop->owner_val;
15647 for (i = 0; i < lop->owner_len; i++)
15648 pid += (pid_t)*cp++;
15650 return (pid);
15654 * Given a lock pointer, returns the length of that lock.
15655 * "end" is the last locked offset the "l_len" covers from
15656 * the start of the lock.
15658 static off64_t
15659 lock_to_end(flock64_t *lock)
15661 off64_t lock_end;
15663 if (lock->l_len == 0)
15664 lock_end = (off64_t)MAXEND;
15665 else
15666 lock_end = lock->l_start + lock->l_len - 1;
15668 return (lock_end);
15672 * Given the end of a lock, it will return you the length "l_len" for that lock.
15674 static off64_t
15675 end_to_len(off64_t start, off64_t end)
15677 off64_t lock_len;
15679 ASSERT(end >= start);
15680 if (end == MAXEND)
15681 lock_len = 0;
15682 else
15683 lock_len = end - start + 1;
15685 return (lock_len);
15689 * On given end for a lock it determines if it is the last locked offset
15690 * or not, if so keeps it as is, else adds one to return the length for
15691 * valid start.
15693 static off64_t
15694 start_check(off64_t x)
15696 if (x == MAXEND)
15697 return (x);
15698 else
15699 return (x + 1);
15703 * See if these two locks overlap, and if so return 1;
15704 * otherwise, return 0.
15706 static int
15707 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15709 off64_t llfp_end, curfp_end;
15711 llfp_end = lock_to_end(llfp);
15712 curfp_end = lock_to_end(curfp);
15714 if (((llfp_end >= curfp->l_start) &&
15715 (llfp->l_start <= curfp->l_start)) ||
15716 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15717 return (1);
15718 return (0);
15722 * Determine what the intersecting lock region is, and add that to the
15723 * 'nl_llpp' locklist in increasing order (by l_start).
15725 static void
15726 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15727 locklist_t **nl_llpp, vnode_t *vp)
15729 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15730 off64_t lost_flp_end, local_flp_end, len, start;
15732 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15734 if (!locks_intersect(lost_flp, local_flp))
15735 return;
15737 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15738 "locks intersect"));
15740 lost_flp_end = lock_to_end(lost_flp);
15741 local_flp_end = lock_to_end(local_flp);
15743 /* Find the starting point of the intersecting region */
15744 if (local_flp->l_start > lost_flp->l_start)
15745 start = local_flp->l_start;
15746 else
15747 start = lost_flp->l_start;
15749 /* Find the lenght of the intersecting region */
15750 if (lost_flp_end < local_flp_end)
15751 len = end_to_len(start, lost_flp_end);
15752 else
15753 len = end_to_len(start, local_flp_end);
15756 * Prepare the flock structure for the intersection found and insert
15757 * it into the new list in increasing l_start order. This list contains
15758 * intersections of locks registered by the client with the local host
15759 * and the lost lock.
15760 * The lock type of this lock is the same as that of the local_flp.
15762 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15763 intersect_llp->ll_flock.l_start = start;
15764 intersect_llp->ll_flock.l_len = len;
15765 intersect_llp->ll_flock.l_type = local_flp->l_type;
15766 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15767 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15768 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15769 intersect_llp->ll_vp = vp;
15771 tmp_fllp = *nl_llpp;
15772 cur_fllp = NULL;
15773 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15774 intersect_llp->ll_flock.l_start) {
15775 cur_fllp = tmp_fllp;
15776 tmp_fllp = tmp_fllp->ll_next;
15778 if (cur_fllp == NULL) {
15779 /* first on the list */
15780 intersect_llp->ll_next = *nl_llpp;
15781 *nl_llpp = intersect_llp;
15782 } else {
15783 intersect_llp->ll_next = cur_fllp->ll_next;
15784 cur_fllp->ll_next = intersect_llp;
15787 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15788 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15789 intersect_llp->ll_flock.l_start,
15790 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15791 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15795 * Our local locking current state is potentially different than
15796 * what the NFSv4 server thinks we have due to a lost lock that was
15797 * resent and then received. We need to reset our "NFSv4" locking
15798 * state to match the current local locking state for this pid since
15799 * that is what the user/application sees as what the world is.
15801 * We cannot afford to drop the open/lock seqid sync since then we can
15802 * get confused about what the current local locking state "is" versus
15803 * "was".
15805 * If we are unable to fix up the locks, we send SIGLOST to the affected
15806 * process. This is not done if the filesystem has been forcibly
15807 * unmounted, in case the process has already exited and a new process
15808 * exists with the same pid.
15810 static void
15811 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15812 nfs4_lock_owner_t *lop)
15814 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15815 mntinfo4_t *mi = VTOMI4(vp);
15816 const int cmd = F_SETLK;
15817 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15818 flock64_t ul_fl;
15820 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15821 "nfs4_reinstitute_local_lock_state"));
15824 * Find active locks for this vp from the local locking code.
15825 * Scan through this list and find out the locks that intersect with
15826 * the lost lock. Once we find the lock that intersects, add the
15827 * intersection area as a new lock to a new list "ri_llp". The lock
15828 * type of the intersection region lock added to ri_llp is the same
15829 * as that found in the active lock list, "list". The intersecting
15830 * region locks are added to ri_llp in increasing l_start order.
15832 ASSERT(nfs_zone() == mi->mi_zone);
15834 locks = flk_active_locks_for_vp(vp);
15835 ri_llp = NULL;
15837 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15838 ASSERT(llp->ll_vp == vp);
15840 * Pick locks that belong to this pid/lockowner
15842 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15843 continue;
15845 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15849 * Now we have the list of intersections with the lost lock. These are
15850 * the locks that were/are active before the server replied to the
15851 * last/lost lock. Issue these locks to the server here. Playing these
15852 * locks to the server will re-establish our current local locking state
15853 * with the v4 server.
15854 * If we get an error, send SIGLOST to the application for that lock.
15857 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15858 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15859 "nfs4_reinstitute_local_lock_state: need to issue "
15860 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15861 llp->ll_flock.l_start,
15862 llp->ll_flock.l_start + llp->ll_flock.l_len,
15863 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15864 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15866 * No need to relock what we already have
15868 if (llp->ll_flock.l_type == lost_flp->l_type)
15869 continue;
15871 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15875 * Now keeping the start of the lost lock as our reference parse the
15876 * newly created ri_llp locklist to find the ranges that we have locked
15877 * with the v4 server but not in the current local locking. We need
15878 * to unlock these ranges.
15879 * These ranges can also be reffered to as those ranges, where the lost
15880 * lock does not overlap with the locks in the ri_llp but are locked
15881 * since the server replied to the lost lock.
15883 cur_start = lost_flp->l_start;
15884 lost_flp_end = lock_to_end(lost_flp);
15886 ul_fl.l_type = F_UNLCK;
15887 ul_fl.l_whence = 0; /* aka SEEK_SET */
15888 ul_fl.l_sysid = lost_flp->l_sysid;
15889 ul_fl.l_pid = lost_flp->l_pid;
15891 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15892 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15894 if (llp->ll_flock.l_start <= cur_start) {
15895 cur_start = start_check(llp_ll_flock_end);
15896 continue;
15898 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15899 "nfs4_reinstitute_local_lock_state: "
15900 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15901 cur_start, llp->ll_flock.l_start));
15903 ul_fl.l_start = cur_start;
15904 ul_fl.l_len = end_to_len(cur_start,
15905 (llp->ll_flock.l_start - 1));
15907 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15908 cur_start = start_check(llp_ll_flock_end);
15912 * In the case where the lost lock ends after all intersecting locks,
15913 * unlock the last part of the lost lock range.
15915 if (cur_start != start_check(lost_flp_end)) {
15916 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15917 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15918 "lost lock region [%"PRIx64" - %"PRIx64"]",
15919 cur_start, lost_flp->l_start + lost_flp->l_len));
15921 ul_fl.l_start = cur_start;
15923 * Is it an to-EOF lock? if so unlock till the end
15925 if (lost_flp->l_len == 0)
15926 ul_fl.l_len = 0;
15927 else
15928 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15930 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15933 if (locks != NULL)
15934 flk_free_locklist(locks);
15936 /* Free up our newly created locklist */
15937 for (llp = ri_llp; llp != NULL; ) {
15938 tmp_llp = llp->ll_next;
15939 kmem_free(llp, sizeof (locklist_t));
15940 llp = tmp_llp;
15944 * Now return back to the original calling nfs4frlock()
15945 * and let us naturally drop our seqid syncs.
15950 * Create a lost state record for the given lock reinstantiation request
15951 * and push it onto the lost state queue.
15953 static void
15954 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15955 nfs4_lock_owner_t *lop)
15957 nfs4_lost_rqst_t req;
15958 nfs_lock_type4 locktype;
15959 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15961 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15963 locktype = flk_to_locktype(cmd, flk->l_type);
15964 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15965 NULL, NULL, lop, flk, &req, cr, vp);
15966 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15967 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15968 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15969 NULL, NULL, NULL);