vm: replace vnode v_pages with a v_pagecache_list list_t
[unleashed.git] / kernel / fs / nfs / nfs4_vnops.c
blobf1bb51bf5aea07aa1e917594dd7f91083f5e7e12
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/vfs_opreg.h>
52 #include <sys/file.h>
53 #include <sys/filio.h>
54 #include <sys/uio.h>
55 #include <sys/buf.h>
56 #include <sys/mman.h>
57 #include <sys/pathname.h>
58 #include <sys/dirent.h>
59 #include <sys/debug.h>
60 #include <sys/vmsystm.h>
61 #include <sys/fcntl.h>
62 #include <sys/flock.h>
63 #include <sys/swap.h>
64 #include <sys/errno.h>
65 #include <sys/strsubr.h>
66 #include <sys/sysmacros.h>
67 #include <sys/kmem.h>
68 #include <sys/cmn_err.h>
69 #include <sys/pathconf.h>
70 #include <sys/utsname.h>
71 #include <sys/dnlc.h>
72 #include <sys/acl.h>
73 #include <sys/systeminfo.h>
74 #include <sys/policy.h>
75 #include <sys/sdt.h>
76 #include <sys/list.h>
77 #include <sys/stat.h>
78 #include <sys/zone.h>
80 #include <rpc/types.h>
81 #include <rpc/auth.h>
82 #include <rpc/clnt.h>
84 #include <nfs/nfs.h>
85 #include <nfs/nfs_clnt.h>
86 #include <nfs/nfs_acl.h>
87 #include <nfs/lm.h>
88 #include <nfs/nfs4.h>
89 #include <nfs/nfs4_kprot.h>
90 #include <nfs/rnode4.h>
91 #include <nfs/nfs4_clnt.h>
93 #include <vm/hat.h>
94 #include <vm/as.h>
95 #include <vm/page.h>
96 #include <vm/pvn.h>
97 #include <vm/seg.h>
98 #include <vm/seg_map.h>
99 #include <vm/seg_kpm.h>
100 #include <vm/seg_vn.h>
102 #include <sys/fs_subr.h>
104 #include <sys/ddi.h>
105 #include <sys/int_fmtio.h>
106 #include <sys/fs/autofs.h>
108 typedef struct {
109 nfs4_ga_res_t *di_garp;
110 cred_t *di_cred;
111 hrtime_t di_time_call;
112 } dirattr_info_t;
114 typedef enum nfs4_acl_op {
115 NFS4_ACL_GET,
116 NFS4_ACL_SET
117 } nfs4_acl_op_t;
119 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *);
121 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
122 char *, dirattr_info_t *);
124 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
125 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
126 nfs4_error_t *, int *);
127 static int nfs4_rdwrlbn(vnode_t *, page_t *, uoff_t, size_t, int,
128 cred_t *);
129 static int nfs4write(vnode_t *, caddr_t, uoff_t, int, cred_t *,
130 stable_how4 *);
131 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
132 cred_t *, bool_t, struct uio *);
133 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
134 vsecattr_t *);
135 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
136 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
137 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
138 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
139 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
140 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
141 int, vnode_t **, cred_t *);
142 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
143 cred_t *, int, int, enum createmode4, int);
144 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
145 caller_context_t *);
146 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
147 vnode_t *, char *, cred_t *, nfsstat4 *);
148 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
149 vnode_t *, char *, cred_t *, nfsstat4 *);
150 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
151 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
152 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
153 static int nfs4_getapage(vnode_t *, uoff_t, size_t, uint_t *,
154 page_t *[], size_t, struct seg *, caddr_t,
155 enum seg_rw, cred_t *);
156 static void nfs4_readahead(vnode_t *, uoff_t, caddr_t, struct seg *,
157 cred_t *);
158 static int nfs4_sync_putapage(vnode_t *, page_t *, uoff_t, size_t,
159 int, cred_t *);
160 static int nfs4_sync_pageio(vnode_t *, page_t *, uoff_t, size_t,
161 int, cred_t *);
162 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
163 static void nfs4_set_mod(vnode_t *);
164 static void nfs4_get_commit(vnode_t *);
165 static void nfs4_get_commit_range(vnode_t *, uoff_t, size_t);
166 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
167 static int nfs4_commit_vp(vnode_t *, uoff_t, size_t, cred_t *, int);
168 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
169 cred_t *);
170 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
171 cred_t *);
172 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
173 hrtime_t, vnode_t *, cred_t *);
174 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
175 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
176 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
177 uoff_t);
178 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
179 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
180 static cred_t *state_to_cred(nfs4_open_stream_t *);
181 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
182 static pid_t lo_to_pid(lock_owner4 *);
183 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
184 cred_t *, nfs4_lock_owner_t *);
185 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
186 nfs4_lock_owner_t *);
187 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
188 static void nfs4_delmap_callback(struct as *, void *, uint_t);
189 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
190 static nfs4_delmapcall_t *nfs4_init_delmapcall();
191 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
192 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
193 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
194 uid_t, gid_t, int);
197 * Routines that implement the setting of v4 args for the misc. ops
199 static void nfs4args_lock_free(nfs_argop4 *);
200 static void nfs4args_lockt_free(nfs_argop4 *);
201 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
202 int, rnode4_t *, cred_t *, bitmap4, int *,
203 nfs4_stateid_types_t *);
204 static void nfs4args_setattr_free(nfs_argop4 *);
205 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
206 bitmap4);
207 static void nfs4args_verify_free(nfs_argop4 *);
208 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
209 WRITE4args **, nfs4_stateid_types_t *);
212 * These are the vnode ops functions that implement the vnode interface to
213 * the networked file system. See more comments below at nfs4_vnodeops.
215 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
216 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
217 caller_context_t *);
218 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
219 caller_context_t *);
220 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
221 caller_context_t *);
222 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
223 caller_context_t *);
224 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
225 caller_context_t *);
226 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
227 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
228 caller_context_t *);
229 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
230 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
231 int, vnode_t **, cred_t *, int, caller_context_t *,
232 vsecattr_t *);
233 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
234 int);
235 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
236 caller_context_t *, int);
237 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
238 caller_context_t *, int);
239 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
240 cred_t *, caller_context_t *, int, vsecattr_t *);
241 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
242 caller_context_t *, int);
243 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
244 cred_t *, caller_context_t *, int);
245 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
246 caller_context_t *, int);
247 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
248 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
249 page_t *[], size_t, struct seg *, caddr_t,
250 enum seg_rw, cred_t *, caller_context_t *);
251 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
252 caller_context_t *);
253 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
254 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
255 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
256 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
257 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
258 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
259 struct flk_callback *, cred_t *, caller_context_t *);
260 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
261 cred_t *, caller_context_t *);
262 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
263 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
264 static int nfs4_pageio(vnode_t *, page_t *, uoff_t, size_t, int,
265 cred_t *, caller_context_t *);
266 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
267 caller_context_t *);
268 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
269 caller_context_t *);
271 * These vnode ops are required to be called from outside this source file,
272 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
273 * as static.
275 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
276 caller_context_t *);
277 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
278 int nfs4_lookup(vnode_t *, char *, vnode_t **,
279 struct pathname *, int, vnode_t *, cred_t *,
280 caller_context_t *, int *, pathname_t *);
281 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
282 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
283 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
284 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
285 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
286 caller_context_t *);
287 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
288 caller_context_t *);
289 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
290 caller_context_t *);
293 * Used for nfs4_commit_vp() to indicate if we should
294 * wait on pending writes.
296 #define NFS4_WRITE_NOWAIT 0
297 #define NFS4_WRITE_WAIT 1
299 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
302 * Error flags used to pass information about certain special errors
303 * which need to be handled specially.
305 #define NFS_EOF -98
306 #define NFS_VERF_MISMATCH -97
309 * Flags used to differentiate between which operation drove the
310 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
312 #define NFS4_CLOSE_OP 0x1
313 #define NFS4_DELMAP_OP 0x2
314 #define NFS4_INACTIVE_OP 0x3
316 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
318 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
319 #define ALIGN64(x, ptr, sz) \
320 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
321 if (x) { \
322 x = sizeof (uint64_t) - (x); \
323 sz -= (x); \
324 ptr += (x); \
327 #ifdef DEBUG
328 int nfs4_client_attr_debug = 0;
329 int nfs4_client_state_debug = 0;
330 int nfs4_client_shadow_debug = 0;
331 int nfs4_client_lock_debug = 0;
332 int nfs4_seqid_sync = 0;
333 int nfs4_client_map_debug = 0;
334 static int nfs4_pageio_debug = 0;
335 int nfs4_client_inactive_debug = 0;
336 int nfs4_client_recov_debug = 0;
337 int nfs4_client_failover_debug = 0;
338 int nfs4_client_call_debug = 0;
339 int nfs4_client_lookup_debug = 0;
340 int nfs4_client_zone_debug = 0;
341 int nfs4_lost_rqst_debug = 0;
342 int nfs4_rdattrerr_debug = 0;
343 int nfs4_open_stream_debug = 0;
345 int nfs4read_error_inject;
347 static int nfs4_create_misses = 0;
349 static int nfs4_readdir_cache_shorts = 0;
350 static int nfs4_readdir_readahead = 0;
352 static int nfs4_bio_do_stop = 0;
354 static int nfs4_lostpage = 0; /* number of times we lost original page */
356 int nfs4_mmap_debug = 0;
358 static int nfs4_pathconf_cache_hits = 0;
359 static int nfs4_pathconf_cache_misses = 0;
361 int nfs4close_all_cnt;
362 int nfs4close_one_debug = 0;
363 int nfs4close_notw_debug = 0;
365 int denied_to_flk_debug = 0;
366 void *lockt_denied_debug;
368 #endif
371 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
372 * or NFS4ERR_RESOURCE.
374 static int confirm_retry_sec = 30;
376 static int nfs4_lookup_neg_cache = 1;
379 * number of pages to read ahead
380 * optimized for 100 base-T.
382 static int nfs4_nra = 4;
384 static int nfs4_do_symlink_cache = 1;
386 static int nfs4_pathconf_disable_cache = 0;
389 * These are the vnode ops routines which implement the vnode interface to
390 * the networked file system. These routines just take their parameters,
391 * make them look networkish by putting the right info into interface structs,
392 * and then calling the appropriate remote routine(s) to do the work.
394 * Note on directory name lookup cacheing: If we detect a stale fhandle,
395 * we purge the directory cache relative to that vnode. This way, the
396 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
397 * more details on rnode locking.
400 struct vnodeops *nfs4_vnodeops;
402 const fs_operation_def_t nfs4_vnodeops_template[] = {
403 VOPNAME_OPEN, { .vop_open = nfs4_open },
404 VOPNAME_CLOSE, { .vop_close = nfs4_close },
405 VOPNAME_READ, { .vop_read = nfs4_read },
406 VOPNAME_WRITE, { .vop_write = nfs4_write },
407 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl },
408 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr },
409 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr },
410 VOPNAME_ACCESS, { .vop_access = nfs4_access },
411 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup },
412 VOPNAME_CREATE, { .vop_create = nfs4_create },
413 VOPNAME_REMOVE, { .vop_remove = nfs4_remove },
414 VOPNAME_LINK, { .vop_link = nfs4_link },
415 VOPNAME_RENAME, { .vop_rename = nfs4_rename },
416 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir },
417 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir },
418 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir },
419 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink },
420 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink },
421 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync },
422 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive },
423 VOPNAME_FID, { .vop_fid = nfs4_fid },
424 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock },
425 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock },
426 VOPNAME_SEEK, { .vop_seek = nfs4_seek },
427 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock },
428 VOPNAME_SPACE, { .vop_space = nfs4_space },
429 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp },
430 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage },
431 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage },
432 VOPNAME_MAP, { .vop_map = nfs4_map },
433 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap },
434 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap },
435 /* no separate nfs4_dump */
436 VOPNAME_DUMP, { .vop_dump = nfs_dump },
437 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf },
438 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio },
439 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose },
440 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr },
441 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr },
442 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock },
443 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
444 NULL, NULL
448 * The following are subroutines and definitions to set args or get res
449 * for the different nfsv4 ops
452 void
453 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
455 int i;
457 for (i = 0; i < arglen; i++) {
458 if (argop[i].argop == OP_LOOKUP) {
459 kmem_free(
460 argop[i].nfs_argop4_u.oplookup.
461 objname.utf8string_val,
462 argop[i].nfs_argop4_u.oplookup.
463 objname.utf8string_len);
468 static void
469 nfs4args_lock_free(nfs_argop4 *argop)
471 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
473 if (locker->new_lock_owner == TRUE) {
474 open_to_lock_owner4 *open_owner;
476 open_owner = &locker->locker4_u.open_owner;
477 if (open_owner->lock_owner.owner_val != NULL) {
478 kmem_free(open_owner->lock_owner.owner_val,
479 open_owner->lock_owner.owner_len);
484 static void
485 nfs4args_lockt_free(nfs_argop4 *argop)
487 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
489 if (lowner->owner_val != NULL) {
490 kmem_free(lowner->owner_val, lowner->owner_len);
494 static void
495 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
496 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
497 nfs4_stateid_types_t *sid_types)
499 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
500 mntinfo4_t *mi;
502 argop->argop = OP_SETATTR;
504 * The stateid is set to 0 if client is not modifying the size
505 * and otherwise to whatever nfs4_get_stateid() returns.
507 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
508 * state struct could be found for the process/file pair. We may
509 * want to change this in the future (by OPENing the file). See
510 * bug # 4474852.
512 if (vap->va_mask & AT_SIZE) {
514 ASSERT(rp != NULL);
515 mi = VTOMI4(RTOV4(rp));
517 argop->nfs_argop4_u.opsetattr.stateid =
518 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
519 OP_SETATTR, sid_types, FALSE);
520 } else {
521 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
522 sizeof (stateid4));
525 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
526 if (*error)
527 bzero(attr, sizeof (*attr));
530 static void
531 nfs4args_setattr_free(nfs_argop4 *argop)
533 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
536 static int
537 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
538 bitmap4 supp)
540 fattr4 *attr;
541 int error = 0;
543 argop->argop = op;
544 switch (op) {
545 case OP_VERIFY:
546 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
547 break;
548 case OP_NVERIFY:
549 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
550 break;
551 default:
552 return (EINVAL);
554 if (!error)
555 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
556 if (error)
557 bzero(attr, sizeof (*attr));
558 return (error);
561 static void
562 nfs4args_verify_free(nfs_argop4 *argop)
564 switch (argop->argop) {
565 case OP_VERIFY:
566 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
567 break;
568 case OP_NVERIFY:
569 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
570 break;
571 default:
572 break;
576 static void
577 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
578 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
580 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
581 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
583 argop->argop = OP_WRITE;
584 wargs->stable = stable;
585 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
586 mi, OP_WRITE, sid_tp);
587 wargs->mblk = NULL;
588 *wargs_pp = wargs;
591 void
592 nfs4args_copen_free(OPEN4cargs *open_args)
594 if (open_args->owner.owner_val) {
595 kmem_free(open_args->owner.owner_val,
596 open_args->owner.owner_len);
598 if ((open_args->opentype == OPEN4_CREATE) &&
599 (open_args->mode != EXCLUSIVE4)) {
600 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
605 * XXX: This is referenced in modstubs.s
607 struct vnodeops *
608 nfs4_getvnodeops(void)
610 return (nfs4_vnodeops);
614 * The OPEN operation opens a regular file.
616 /*ARGSUSED3*/
617 static int
618 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
620 vnode_t *dvp = NULL;
621 rnode4_t *rp, *drp;
622 int error;
623 int just_been_created;
624 char fn[MAXNAMELEN];
626 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
627 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
628 return (EIO);
629 rp = VTOR4(*vpp);
632 * Check to see if opening something besides a regular file;
633 * if so skip the OTW call
635 if ((*vpp)->v_type != VREG) {
636 error = nfs4_open_non_reg_file(vpp, flag, cr);
637 return (error);
641 * XXX - would like a check right here to know if the file is
642 * executable or not, so as to skip OTW
645 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
646 return (error);
648 drp = VTOR4(dvp);
649 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
650 return (EINTR);
652 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
653 nfs_rw_exit(&drp->r_rwlock);
654 return (error);
658 * See if this file has just been CREATEd.
659 * If so, clear the flag and update the dnlc, which was previously
660 * skipped in nfs4_create.
661 * XXX need better serilization on this.
662 * XXX move this into the nf4open_otw call, after we have
663 * XXX acquired the open owner seqid sync.
665 mutex_enter(&rp->r_statev4_lock);
666 if (rp->created_v4) {
667 rp->created_v4 = 0;
668 mutex_exit(&rp->r_statev4_lock);
670 dnlc_update(dvp, fn, *vpp);
671 /* This is needed so we don't bump the open ref count */
672 just_been_created = 1;
673 } else {
674 mutex_exit(&rp->r_statev4_lock);
675 just_been_created = 0;
679 * If caller specified O_TRUNC/FTRUNC, then be sure to set
680 * FWRITE (to drive successful setattr(size=0) after open)
682 if (flag & FTRUNC)
683 flag |= FWRITE;
685 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
686 just_been_created);
688 if (!error && !((*vpp)->v_flag & VROOT))
689 dnlc_update(dvp, fn, *vpp);
691 nfs_rw_exit(&drp->r_rwlock);
693 /* release the hold from vtodv */
694 VN_RELE(dvp);
696 /* exchange the shadow for the master vnode, if needed */
698 if (error == 0 && IS_SHADOW(*vpp, rp))
699 sv_exchange(vpp);
701 return (error);
705 * See if there's a "lost open" request to be saved and recovered.
707 static void
708 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
709 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
710 vnode_t *dvp, OPEN4cargs *open_args)
712 vfs_t *vfsp;
713 char *srccfp;
715 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
717 if (error != ETIMEDOUT && error != EINTR &&
718 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
719 lost_rqstp->lr_op = 0;
720 return;
723 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
724 "nfs4open_save_lost_rqst: error %d", error));
726 lost_rqstp->lr_op = OP_OPEN;
729 * The vp (if it is not NULL) and dvp are held and rele'd via
730 * the recovery code. See nfs4_save_lost_rqst.
732 lost_rqstp->lr_vp = vp;
733 lost_rqstp->lr_dvp = dvp;
734 lost_rqstp->lr_oop = oop;
735 lost_rqstp->lr_osp = NULL;
736 lost_rqstp->lr_lop = NULL;
737 lost_rqstp->lr_cr = cr;
738 lost_rqstp->lr_flk = NULL;
739 lost_rqstp->lr_oacc = open_args->share_access;
740 lost_rqstp->lr_odeny = open_args->share_deny;
741 lost_rqstp->lr_oclaim = open_args->claim;
742 if (open_args->claim == CLAIM_DELEGATE_CUR) {
743 lost_rqstp->lr_ostateid =
744 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
745 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
746 } else {
747 srccfp = open_args->open_claim4_u.cfile;
749 lost_rqstp->lr_ofile.utf8string_len = 0;
750 lost_rqstp->lr_ofile.utf8string_val = NULL;
751 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
752 lost_rqstp->lr_putfirst = FALSE;
755 struct nfs4_excl_time {
756 uint32 seconds;
757 uint32 nseconds;
761 * The OPEN operation creates and/or opens a regular file
763 * ARGSUSED
765 static int
766 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
767 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
768 enum createmode4 createmode, int file_just_been_created)
770 rnode4_t *rp;
771 rnode4_t *drp = VTOR4(dvp);
772 vnode_t *vp = NULL;
773 vnode_t *vpi = *vpp;
774 bool_t needrecov = FALSE;
776 int doqueue = 1;
778 COMPOUND4args_clnt args;
779 COMPOUND4res_clnt res;
780 nfs_argop4 *argop;
781 nfs_resop4 *resop;
782 int argoplist_size;
783 int idx_open, idx_fattr;
785 GETFH4res *gf_res = NULL;
786 OPEN4res *op_res = NULL;
787 nfs4_ga_res_t *garp;
788 fattr4 *attr = NULL;
789 struct nfs4_excl_time verf;
790 bool_t did_excl_setup = FALSE;
791 int created_osp;
793 OPEN4cargs *open_args;
794 nfs4_open_owner_t *oop = NULL;
795 nfs4_open_stream_t *osp = NULL;
796 seqid4 seqid = 0;
797 bool_t retry_open = FALSE;
798 nfs4_recov_state_t recov_state;
799 nfs4_lost_rqst_t lost_rqst;
800 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
801 hrtime_t t;
802 int acc = 0;
803 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
804 cred_t *ncr = NULL;
806 nfs4_sharedfh_t *otw_sfh;
807 nfs4_sharedfh_t *orig_sfh;
808 int fh_differs = 0;
809 int numops, setgid_flag;
810 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
813 * Make sure we properly deal with setting the right gid on
814 * a newly created file to reflect the parent's setgid bit
816 setgid_flag = 0;
817 if (create_flag && in_va) {
820 * If there is grpid mount flag used or
821 * the parent's directory has the setgid bit set
822 * _and_ the client was able to get a valid mapping
823 * for the parent dir's owner_group, we want to
824 * append NVERIFY(owner_group == dva.va_gid) and
825 * SETATTR to the CREATE compound.
827 mutex_enter(&drp->r_statelock);
828 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
829 drp->r_attr.va_mode & VSGID) &&
830 drp->r_attr.va_gid != GID_NOBODY) {
831 in_va->va_mask |= AT_GID;
832 in_va->va_gid = drp->r_attr.va_gid;
833 setgid_flag = 1;
835 mutex_exit(&drp->r_statelock);
839 * Normal/non-create compound:
840 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
842 * Open(create) compound no setgid:
843 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
844 * RESTOREFH + GETATTR
846 * Open(create) setgid:
847 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
848 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
849 * NVERIFY(grp) + SETATTR
851 if (setgid_flag) {
852 numops = 10;
853 idx_open = 1;
854 idx_fattr = 3;
855 } else if (create_flag) {
856 numops = 7;
857 idx_open = 2;
858 idx_fattr = 4;
859 } else {
860 numops = 4;
861 idx_open = 1;
862 idx_fattr = 3;
865 args.array_len = numops;
866 argoplist_size = numops * sizeof (nfs_argop4);
867 argop = kmem_alloc(argoplist_size, KM_SLEEP);
869 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
870 "open %s open flag 0x%x cred %p", file_name, open_flag,
871 (void *)cr));
873 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
874 if (create_flag) {
876 * We are to create a file. Initialize the passed in vnode
877 * pointer.
879 vpi = NULL;
880 } else {
882 * Check to see if the client owns a read delegation and is
883 * trying to open for write. If so, then return the delegation
884 * to avoid the server doing a cb_recall and returning DELAY.
885 * NB - we don't use the statev4_lock here because we'd have
886 * to drop the lock anyway and the result would be stale.
888 if ((open_flag & FWRITE) &&
889 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
890 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
893 * If the file has a delegation, then do an access check up
894 * front. This avoids having to an access check later after
895 * we've already done start_op, which could deadlock.
897 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
898 if (open_flag & FREAD &&
899 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
900 acc |= VREAD;
901 if (open_flag & FWRITE &&
902 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
903 acc |= VWRITE;
907 drp = VTOR4(dvp);
909 recov_state.rs_flags = 0;
910 recov_state.rs_num_retry_despite_err = 0;
911 cred_otw = cr;
913 recov_retry:
914 fh_differs = 0;
915 nfs4_error_zinit(&e);
917 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
918 if (e.error) {
919 if (ncr != NULL)
920 crfree(ncr);
921 kmem_free(argop, argoplist_size);
922 return (e.error);
925 args.ctag = TAG_OPEN;
926 args.array_len = numops;
927 args.array = argop;
929 /* putfh directory fh */
930 argop[0].argop = OP_CPUTFH;
931 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
933 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
934 argop[idx_open].argop = OP_COPEN;
935 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
936 open_args->claim = CLAIM_NULL;
938 /* name of file */
939 open_args->open_claim4_u.cfile = file_name;
940 open_args->owner.owner_len = 0;
941 open_args->owner.owner_val = NULL;
943 if (create_flag) {
944 /* CREATE a file */
945 open_args->opentype = OPEN4_CREATE;
946 open_args->mode = createmode;
947 if (createmode == EXCLUSIVE4) {
948 if (did_excl_setup == FALSE) {
949 verf.seconds = zone_get_hostid(NULL);
950 if (verf.seconds != 0)
951 verf.nseconds = newnum();
952 else {
953 timestruc_t now;
955 gethrestime(&now);
956 verf.seconds = now.tv_sec;
957 verf.nseconds = now.tv_nsec;
960 * Since the server will use this value for the
961 * mtime, make sure that it can't overflow. Zero
962 * out the MSB. The actual value does not matter
963 * here, only its uniqeness.
965 verf.seconds &= INT32_MAX;
966 did_excl_setup = TRUE;
969 /* Now copy over verifier to OPEN4args. */
970 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
971 } else {
972 int v_error;
973 bitmap4 supp_attrs;
974 servinfo4_t *svp;
976 attr = &open_args->createhow4_u.createattrs;
978 svp = drp->r_server;
979 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
980 supp_attrs = svp->sv_supp_attrs;
981 nfs_rw_exit(&svp->sv_lock);
983 /* GUARDED4 or UNCHECKED4 */
984 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
985 supp_attrs);
986 if (v_error) {
987 bzero(attr, sizeof (*attr));
988 nfs4args_copen_free(open_args);
989 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
990 &recov_state, FALSE);
991 if (ncr != NULL)
992 crfree(ncr);
993 kmem_free(argop, argoplist_size);
994 return (v_error);
997 } else {
998 /* NO CREATE */
999 open_args->opentype = OPEN4_NOCREATE;
1002 if (recov_state.rs_sp != NULL) {
1003 mutex_enter(&recov_state.rs_sp->s_lock);
1004 open_args->owner.clientid = recov_state.rs_sp->clientid;
1005 mutex_exit(&recov_state.rs_sp->s_lock);
1006 } else {
1007 /* XXX should we just fail here? */
1008 open_args->owner.clientid = 0;
1012 * This increments oop's ref count or creates a temporary 'just_created'
1013 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1014 * completes.
1016 mutex_enter(&VTOMI4(dvp)->mi_lock);
1018 /* See if a permanent or just created open owner exists */
1019 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1020 if (!oop) {
1022 * This open owner does not exist so create a temporary
1023 * just created one.
1025 oop = create_open_owner(cr, VTOMI4(dvp));
1026 ASSERT(oop != NULL);
1028 mutex_exit(&VTOMI4(dvp)->mi_lock);
1030 /* this length never changes, do alloc before seqid sync */
1031 open_args->owner.owner_len = sizeof (oop->oo_name);
1032 open_args->owner.owner_val =
1033 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1035 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1036 if (e.error == EAGAIN) {
1037 open_owner_rele(oop);
1038 nfs4args_copen_free(open_args);
1039 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1040 if (ncr != NULL) {
1041 crfree(ncr);
1042 ncr = NULL;
1044 goto recov_retry;
1047 /* Check to see if we need to do the OTW call */
1048 if (!create_flag) {
1049 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1050 file_just_been_created, &e.error, acc, &recov_state)) {
1053 * The OTW open is not necessary. Either
1054 * the open can succeed without it (eg.
1055 * delegation, error == 0) or the open
1056 * must fail due to an access failure
1057 * (error != 0). In either case, tidy
1058 * up and return.
1061 nfs4_end_open_seqid_sync(oop);
1062 open_owner_rele(oop);
1063 nfs4args_copen_free(open_args);
1064 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1065 if (ncr != NULL)
1066 crfree(ncr);
1067 kmem_free(argop, argoplist_size);
1068 return (e.error);
1072 bcopy(&oop->oo_name, open_args->owner.owner_val,
1073 open_args->owner.owner_len);
1075 seqid = nfs4_get_open_seqid(oop) + 1;
1076 open_args->seqid = seqid;
1077 open_args->share_access = 0;
1078 if (open_flag & FREAD)
1079 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1080 if (open_flag & FWRITE)
1081 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1082 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1087 * getfh w/sanity check for idx_open/idx_fattr
1089 ASSERT((idx_open + 1) == (idx_fattr - 1));
1090 argop[idx_open + 1].argop = OP_GETFH;
1092 /* getattr */
1093 argop[idx_fattr].argop = OP_GETATTR;
1094 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1095 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1097 if (setgid_flag) {
1098 vattr_t _v;
1099 servinfo4_t *svp;
1100 bitmap4 supp_attrs;
1102 svp = drp->r_server;
1103 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1104 supp_attrs = svp->sv_supp_attrs;
1105 nfs_rw_exit(&svp->sv_lock);
1108 * For setgid case, we need to:
1109 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1111 argop[4].argop = OP_SAVEFH;
1113 argop[5].argop = OP_CPUTFH;
1114 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1116 argop[6].argop = OP_GETATTR;
1117 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1118 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1120 argop[7].argop = OP_RESTOREFH;
1123 * nverify
1125 _v.va_mask = AT_GID;
1126 _v.va_gid = in_va->va_gid;
1127 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1128 supp_attrs))) {
1131 * setattr
1133 * We _know_ we're not messing with AT_SIZE or
1134 * AT_XTIME, so no need for stateid or flags.
1135 * Also we specify NULL rp since we're only
1136 * interested in setting owner_group attributes.
1138 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1139 supp_attrs, &e.error, 0);
1140 if (e.error)
1141 nfs4args_verify_free(&argop[8]);
1144 if (e.error) {
1146 * XXX - Revisit the last argument to nfs4_end_op()
1147 * once 5020486 is fixed.
1149 nfs4_end_open_seqid_sync(oop);
1150 open_owner_rele(oop);
1151 nfs4args_copen_free(open_args);
1152 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1153 if (ncr != NULL)
1154 crfree(ncr);
1155 kmem_free(argop, argoplist_size);
1156 return (e.error);
1158 } else if (create_flag) {
1159 argop[1].argop = OP_SAVEFH;
1161 argop[5].argop = OP_RESTOREFH;
1163 argop[6].argop = OP_GETATTR;
1164 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1165 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1168 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1169 "nfs4open_otw: %s call, nm %s, rp %s",
1170 needrecov ? "recov" : "first", file_name,
1171 rnode4info(VTOR4(dvp))));
1173 t = gethrtime();
1175 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1177 if (!e.error && nfs4_need_to_bump_seqid(&res))
1178 nfs4_set_open_seqid(seqid, oop, args.ctag);
1180 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1182 if (e.error || needrecov) {
1183 bool_t abort = FALSE;
1185 if (needrecov) {
1186 nfs4_bseqid_entry_t *bsep = NULL;
1188 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1189 cred_otw, vpi, dvp, open_args);
1191 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1192 bsep = nfs4_create_bseqid_entry(oop, NULL,
1193 vpi, 0, args.ctag, open_args->seqid);
1194 num_bseqid_retry--;
1197 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1198 NULL, lost_rqst.lr_op == OP_OPEN ?
1199 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1201 if (bsep)
1202 kmem_free(bsep, sizeof (*bsep));
1203 /* give up if we keep getting BAD_SEQID */
1204 if (num_bseqid_retry == 0)
1205 abort = TRUE;
1206 if (abort == TRUE && e.error == 0)
1207 e.error = geterrno4(res.status);
1209 nfs4_end_open_seqid_sync(oop);
1210 open_owner_rele(oop);
1211 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1212 nfs4args_copen_free(open_args);
1213 if (setgid_flag) {
1214 nfs4args_verify_free(&argop[8]);
1215 nfs4args_setattr_free(&argop[9]);
1217 if (!e.error)
1218 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1219 if (ncr != NULL) {
1220 crfree(ncr);
1221 ncr = NULL;
1223 if (!needrecov || abort == TRUE || e.error == EINTR ||
1224 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1225 kmem_free(argop, argoplist_size);
1226 return (e.error);
1228 goto recov_retry;
1232 * Will check and update lease after checking the rflag for
1233 * OPEN_CONFIRM in the successful OPEN call.
1235 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1238 * XXX what if we're crossing mount points from server1:/drp
1239 * to server2:/drp/rp.
1242 /* Signal our end of use of the open seqid */
1243 nfs4_end_open_seqid_sync(oop);
1246 * This will destroy the open owner if it was just created,
1247 * and no one else has put a reference on it.
1249 open_owner_rele(oop);
1250 if (create_flag && (createmode != EXCLUSIVE4) &&
1251 res.status == NFS4ERR_BADOWNER)
1252 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1254 e.error = geterrno4(res.status);
1255 nfs4args_copen_free(open_args);
1256 if (setgid_flag) {
1257 nfs4args_verify_free(&argop[8]);
1258 nfs4args_setattr_free(&argop[9]);
1260 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1261 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1263 * If the reply is NFS4ERR_ACCESS, it may be because
1264 * we are root (no root net access). If the real uid
1265 * is not root, then retry with the real uid instead.
1267 if (ncr != NULL) {
1268 crfree(ncr);
1269 ncr = NULL;
1271 if (res.status == NFS4ERR_ACCESS &&
1272 (ncr = crnetadjust(cred_otw)) != NULL) {
1273 cred_otw = ncr;
1274 goto recov_retry;
1276 kmem_free(argop, argoplist_size);
1277 return (e.error);
1280 resop = &res.array[idx_open]; /* open res */
1281 op_res = &resop->nfs_resop4_u.opopen;
1283 #ifdef DEBUG
1285 * verify attrset bitmap
1287 if (create_flag &&
1288 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1289 /* make sure attrset returned is what we asked for */
1290 /* XXX Ignore this 'error' for now */
1291 if (attr->attrmask != op_res->attrset)
1292 /* EMPTY */;
1294 #endif
1296 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1297 mutex_enter(&VTOMI4(dvp)->mi_lock);
1298 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1299 mutex_exit(&VTOMI4(dvp)->mi_lock);
1302 resop = &res.array[idx_open + 1]; /* getfh res */
1303 gf_res = &resop->nfs_resop4_u.opgetfh;
1305 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1308 * The open stateid has been updated on the server but not
1309 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1310 * flush_pages->fop_putpage->...->nfs4write where we will issue an OTW
1311 * WRITE call. That, however, will use the old stateid, so go ahead
1312 * and upate the open stateid now, before any call to makenfs4node.
1314 if (vpi) {
1315 nfs4_open_stream_t *tmp_osp;
1316 rnode4_t *tmp_rp = VTOR4(vpi);
1318 tmp_osp = find_open_stream(oop, tmp_rp);
1319 if (tmp_osp) {
1320 tmp_osp->open_stateid = op_res->stateid;
1321 mutex_exit(&tmp_osp->os_sync_lock);
1322 open_stream_rele(tmp_osp, tmp_rp);
1326 * We must determine if the file handle given by the otw open
1327 * is the same as the file handle which was passed in with
1328 * *vpp. This case can be reached if the file we are trying
1329 * to open has been removed and another file has been created
1330 * having the same file name. The passed in vnode is released
1331 * later.
1333 orig_sfh = VTOR4(vpi)->r_fh;
1334 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1337 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1339 if (create_flag || fh_differs) {
1340 int rnode_err = 0;
1342 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1343 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1345 if (e.error)
1346 PURGE_ATTRCACHE4(vp);
1348 * For the newly created vp case, make sure the rnode
1349 * isn't bad before using it.
1351 mutex_enter(&(VTOR4(vp))->r_statelock);
1352 if (VTOR4(vp)->r_flags & R4RECOVERR)
1353 rnode_err = EIO;
1354 mutex_exit(&(VTOR4(vp))->r_statelock);
1356 if (rnode_err) {
1357 nfs4_end_open_seqid_sync(oop);
1358 nfs4args_copen_free(open_args);
1359 if (setgid_flag) {
1360 nfs4args_verify_free(&argop[8]);
1361 nfs4args_setattr_free(&argop[9]);
1363 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1364 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1365 needrecov);
1366 open_owner_rele(oop);
1367 VN_RELE(vp);
1368 if (ncr != NULL)
1369 crfree(ncr);
1370 sfh4_rele(&otw_sfh);
1371 kmem_free(argop, argoplist_size);
1372 return (EIO);
1374 } else {
1375 vp = vpi;
1377 sfh4_rele(&otw_sfh);
1380 * It seems odd to get a full set of attrs and then not update
1381 * the object's attrcache in the non-create case. Create case uses
1382 * the attrs since makenfs4node checks to see if the attrs need to
1383 * be updated (and then updates them). The non-create case should
1384 * update attrs also.
1386 if (! create_flag && ! fh_differs && !e.error) {
1387 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1390 nfs4_error_zinit(&e);
1391 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1392 /* This does not do recovery for vp explicitly. */
1393 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1394 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1396 if (e.error || e.stat) {
1397 nfs4_end_open_seqid_sync(oop);
1398 nfs4args_copen_free(open_args);
1399 if (setgid_flag) {
1400 nfs4args_verify_free(&argop[8]);
1401 nfs4args_setattr_free(&argop[9]);
1403 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1404 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1405 needrecov);
1406 open_owner_rele(oop);
1407 if (create_flag || fh_differs) {
1408 /* rele the makenfs4node */
1409 VN_RELE(vp);
1411 if (ncr != NULL) {
1412 crfree(ncr);
1413 ncr = NULL;
1415 if (retry_open == TRUE) {
1416 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1417 "nfs4open_otw: retry the open since OPEN "
1418 "CONFIRM failed with error %d stat %d",
1419 e.error, e.stat));
1420 if (create_flag && createmode == GUARDED4) {
1421 NFS4_DEBUG(nfs4_client_recov_debug,
1422 (CE_NOTE, "nfs4open_otw: switch "
1423 "createmode from GUARDED4 to "
1424 "UNCHECKED4"));
1425 createmode = UNCHECKED4;
1427 goto recov_retry;
1429 if (!e.error) {
1430 if (create_flag && (createmode != EXCLUSIVE4) &&
1431 e.stat == NFS4ERR_BADOWNER)
1432 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1434 e.error = geterrno4(e.stat);
1436 kmem_free(argop, argoplist_size);
1437 return (e.error);
1441 rp = VTOR4(vp);
1443 mutex_enter(&rp->r_statev4_lock);
1444 if (create_flag)
1445 rp->created_v4 = 1;
1446 mutex_exit(&rp->r_statev4_lock);
1448 mutex_enter(&oop->oo_lock);
1449 /* Doesn't matter if 'oo_just_created' already was set as this */
1450 oop->oo_just_created = NFS4_PERM_CREATED;
1451 if (oop->oo_cred_otw)
1452 crfree(oop->oo_cred_otw);
1453 oop->oo_cred_otw = cred_otw;
1454 crhold(oop->oo_cred_otw);
1455 mutex_exit(&oop->oo_lock);
1457 /* returns with 'os_sync_lock' held */
1458 osp = find_or_create_open_stream(oop, rp, &created_osp);
1459 if (!osp) {
1460 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1461 "nfs4open_otw: failed to create an open stream"));
1462 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1463 "signal our end of use of the open seqid"));
1465 nfs4_end_open_seqid_sync(oop);
1466 open_owner_rele(oop);
1467 nfs4args_copen_free(open_args);
1468 if (setgid_flag) {
1469 nfs4args_verify_free(&argop[8]);
1470 nfs4args_setattr_free(&argop[9]);
1472 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1473 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1474 if (create_flag || fh_differs)
1475 VN_RELE(vp);
1476 if (ncr != NULL)
1477 crfree(ncr);
1479 kmem_free(argop, argoplist_size);
1480 return (EINVAL);
1484 osp->open_stateid = op_res->stateid;
1486 if (open_flag & FREAD)
1487 osp->os_share_acc_read++;
1488 if (open_flag & FWRITE)
1489 osp->os_share_acc_write++;
1490 osp->os_share_deny_none++;
1493 * Need to reset this bitfield for the possible case where we were
1494 * going to OTW CLOSE the file, got a non-recoverable error, and before
1495 * we could retry the CLOSE, OPENed the file again.
1497 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1498 osp->os_final_close = 0;
1499 osp->os_force_close = 0;
1500 #ifdef DEBUG
1501 if (osp->os_failed_reopen)
1502 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1503 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1504 (void *)osp, (void *)cr, rnode4info(rp)));
1505 #endif
1506 osp->os_failed_reopen = 0;
1508 mutex_exit(&osp->os_sync_lock);
1510 nfs4_end_open_seqid_sync(oop);
1512 if (created_osp && recov_state.rs_sp != NULL) {
1513 mutex_enter(&recov_state.rs_sp->s_lock);
1514 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1515 mutex_exit(&recov_state.rs_sp->s_lock);
1518 /* get rid of our reference to find oop */
1519 open_owner_rele(oop);
1521 open_stream_rele(osp, rp);
1523 /* accept delegation, if any */
1524 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1526 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1528 if (createmode == EXCLUSIVE4 &&
1529 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1530 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1531 " EXCLUSIVE4: sending a SETATTR"));
1533 * If doing an exclusive create, then generate
1534 * a SETATTR to set the initial attributes.
1535 * Try to set the mtime and the atime to the
1536 * server's current time. It is somewhat
1537 * expected that these fields will be used to
1538 * store the exclusive create cookie. If not,
1539 * server implementors will need to know that
1540 * a SETATTR will follow an exclusive create
1541 * and the cookie should be destroyed if
1542 * appropriate.
1544 * The AT_GID and AT_SIZE bits are turned off
1545 * so that the SETATTR request will not attempt
1546 * to process these. The gid will be set
1547 * separately if appropriate. The size is turned
1548 * off because it is assumed that a new file will
1549 * be created empty and if the file wasn't empty,
1550 * then the exclusive create will have failed
1551 * because the file must have existed already.
1552 * Therefore, no truncate operation is needed.
1554 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1555 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1557 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1558 if (e.error) {
1560 * Couldn't correct the attributes of
1561 * the newly created file and the
1562 * attributes are wrong. Remove the
1563 * file and return an error to the
1564 * application.
1566 /* XXX will this take care of client state ? */
1567 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1568 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1569 " remove file", e.error));
1570 VN_RELE(vp);
1571 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1573 * Since we've reled the vnode and removed
1574 * the file we now need to return the error.
1575 * At this point we don't want to update the
1576 * dircaches, call nfs4_waitfor_purge_complete
1577 * or set vpp to vp so we need to skip these
1578 * as well.
1580 goto skip_update_dircaches;
1585 * If we created or found the correct vnode, due to create_flag or
1586 * fh_differs being set, then update directory cache attribute, readdir
1587 * and dnlc caches.
1589 if (create_flag || fh_differs) {
1590 dirattr_info_t dinfo, *dinfop;
1593 * Make sure getattr succeeded before using results.
1594 * note: op 7 is getattr(dir) for both flavors of
1595 * open(create).
1597 if (create_flag && res.status == NFS4_OK) {
1598 dinfo.di_time_call = t;
1599 dinfo.di_cred = cr;
1600 dinfo.di_garp =
1601 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1602 dinfop = &dinfo;
1603 } else {
1604 dinfop = NULL;
1607 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1608 dinfop);
1612 * If the page cache for this file was flushed from actions
1613 * above, it was done asynchronously and if that is true,
1614 * there is a need to wait here for it to complete. This must
1615 * be done outside of start_fop/end_fop.
1617 (void) nfs4_waitfor_purge_complete(vp);
1620 * It is implicit that we are in the open case (create_flag == 0) since
1621 * fh_differs can only be set to a non-zero value in the open case.
1623 if (fh_differs != 0 && vpi != NULL)
1624 VN_RELE(vpi);
1627 * Be sure to set *vpp to the correct value before returning.
1629 *vpp = vp;
1631 skip_update_dircaches:
1633 nfs4args_copen_free(open_args);
1634 if (setgid_flag) {
1635 nfs4args_verify_free(&argop[8]);
1636 nfs4args_setattr_free(&argop[9]);
1638 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1640 if (ncr)
1641 crfree(ncr);
1642 kmem_free(argop, argoplist_size);
1643 return (e.error);
1647 * Reopen an open instance. cf. nfs4open_otw().
1649 * Errors are returned by the nfs4_error_t parameter.
1650 * - ep->error contains an errno value or zero.
1651 * - if it is zero, ep->stat is set to an NFS status code, if any.
1652 * If the file could not be reopened, but the caller should continue, the
1653 * file is marked dead and no error values are returned. If the caller
1654 * should stop recovering open files and start over, either the ep->error
1655 * value or ep->stat will indicate an error (either something that requires
1656 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1657 * filehandles) may be handled silently by this routine.
1658 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1659 * will be started, so the caller should not do it.
1661 * Gotos:
1662 * - kill_file : reopen failed in such a fashion to constitute marking the
1663 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1664 * is for cases where recovery is not possible.
1665 * - failed_reopen : same as above, except that the file has already been
1666 * marked dead, so no need to do it again.
1667 * - bailout : reopen failed but we are able to recover and retry the reopen -
1668 * either within this function immediately or via the calling function.
1671 void
1672 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1673 open_claim_type4 claim, bool_t frc_use_claim_previous,
1674 bool_t is_recov)
1676 COMPOUND4args_clnt args;
1677 COMPOUND4res_clnt res;
1678 nfs_argop4 argop[4];
1679 nfs_resop4 *resop;
1680 OPEN4res *op_res = NULL;
1681 OPEN4cargs *open_args;
1682 GETFH4res *gf_res;
1683 rnode4_t *rp = VTOR4(vp);
1684 int doqueue = 1;
1685 cred_t *cr = NULL, *cred_otw = NULL;
1686 nfs4_open_owner_t *oop = NULL;
1687 seqid4 seqid;
1688 nfs4_ga_res_t *garp;
1689 char fn[MAXNAMELEN];
1690 nfs4_recov_state_t recov = {NULL, 0};
1691 nfs4_lost_rqst_t lost_rqst;
1692 mntinfo4_t *mi = VTOMI4(vp);
1693 bool_t abort;
1694 char *failed_msg = "";
1695 int fh_different;
1696 hrtime_t t;
1697 nfs4_bseqid_entry_t *bsep = NULL;
1699 ASSERT(nfs4_consistent_type(vp));
1700 ASSERT(nfs_zone() == mi->mi_zone);
1702 nfs4_error_zinit(ep);
1704 /* this is the cred used to find the open owner */
1705 cr = state_to_cred(osp);
1706 if (cr == NULL) {
1707 failed_msg = "Couldn't reopen: no cred";
1708 goto kill_file;
1710 /* use this cred for OTW operations */
1711 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1713 top:
1714 nfs4_error_zinit(ep);
1716 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1717 /* File system has been unmounted, quit */
1718 ep->error = EIO;
1719 failed_msg = "Couldn't reopen: file system has been unmounted";
1720 goto kill_file;
1723 oop = osp->os_open_owner;
1725 ASSERT(oop != NULL);
1726 if (oop == NULL) { /* be defensive in non-DEBUG */
1727 failed_msg = "can't reopen: no open owner";
1728 goto kill_file;
1730 open_owner_hold(oop);
1732 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1733 if (ep->error) {
1734 open_owner_rele(oop);
1735 oop = NULL;
1736 goto bailout;
1740 * If the rnode has a delegation and the delegation has been
1741 * recovered and the server didn't request a recall and the caller
1742 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1743 * recovery) and the rnode hasn't been marked dead, then install
1744 * the delegation stateid in the open stream. Otherwise, proceed
1745 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1747 mutex_enter(&rp->r_statev4_lock);
1748 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1749 !rp->r_deleg_return_pending &&
1750 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1751 !rp->r_deleg_needs_recall &&
1752 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1753 !(rp->r_flags & R4RECOVERR)) {
1754 mutex_enter(&osp->os_sync_lock);
1755 osp->os_delegation = 1;
1756 osp->open_stateid = rp->r_deleg_stateid;
1757 mutex_exit(&osp->os_sync_lock);
1758 mutex_exit(&rp->r_statev4_lock);
1759 goto bailout;
1761 mutex_exit(&rp->r_statev4_lock);
1764 * If the file failed recovery, just quit. This failure need not
1765 * affect other reopens, so don't return an error.
1767 mutex_enter(&rp->r_statelock);
1768 if (rp->r_flags & R4RECOVERR) {
1769 mutex_exit(&rp->r_statelock);
1770 ep->error = 0;
1771 goto failed_reopen;
1773 mutex_exit(&rp->r_statelock);
1776 * argop is empty here
1778 * PUTFH, OPEN, GETATTR
1780 args.ctag = TAG_REOPEN;
1781 args.array_len = 4;
1782 args.array = argop;
1784 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1785 "nfs4_reopen: file is type %d, id %s",
1786 vp->v_type, rnode4info(VTOR4(vp))));
1788 argop[0].argop = OP_CPUTFH;
1790 if (claim != CLAIM_PREVIOUS) {
1792 * if this is a file mount then
1793 * use the mntinfo parentfh
1795 argop[0].nfs_argop4_u.opcputfh.sfh =
1796 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1797 VTOSV(vp)->sv_dfh;
1798 } else {
1799 /* putfh fh to reopen */
1800 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1803 argop[1].argop = OP_COPEN;
1804 open_args = &argop[1].nfs_argop4_u.opcopen;
1805 open_args->claim = claim;
1807 if (claim == CLAIM_NULL) {
1809 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1810 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1811 "failed for vp 0x%p for CLAIM_NULL with %m",
1812 (void *)vp);
1813 failed_msg = "Couldn't reopen: vtoname failed for "
1814 "CLAIM_NULL";
1815 /* nothing allocated yet */
1816 goto kill_file;
1819 open_args->open_claim4_u.cfile = fn;
1820 } else if (claim == CLAIM_PREVIOUS) {
1823 * We have two cases to deal with here:
1824 * 1) We're being called to reopen files in order to satisfy
1825 * a lock operation request which requires us to explicitly
1826 * reopen files which were opened under a delegation. If
1827 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1828 * that case, frc_use_claim_previous is TRUE and we must
1829 * use the rnode's current delegation type (r_deleg_type).
1830 * 2) We're reopening files during some form of recovery.
1831 * In this case, frc_use_claim_previous is FALSE and we
1832 * use the delegation type appropriate for recovery
1833 * (r_deleg_needs_recovery).
1835 mutex_enter(&rp->r_statev4_lock);
1836 open_args->open_claim4_u.delegate_type =
1837 frc_use_claim_previous ?
1838 rp->r_deleg_type :
1839 rp->r_deleg_needs_recovery;
1840 mutex_exit(&rp->r_statev4_lock);
1842 } else if (claim == CLAIM_DELEGATE_CUR) {
1844 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1845 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1846 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1847 "with %m", (void *)vp);
1848 failed_msg = "Couldn't reopen: vtoname failed for "
1849 "CLAIM_DELEGATE_CUR";
1850 /* nothing allocated yet */
1851 goto kill_file;
1854 mutex_enter(&rp->r_statev4_lock);
1855 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1856 rp->r_deleg_stateid;
1857 mutex_exit(&rp->r_statev4_lock);
1859 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1861 open_args->opentype = OPEN4_NOCREATE;
1862 open_args->owner.clientid = mi2clientid(mi);
1863 open_args->owner.owner_len = sizeof (oop->oo_name);
1864 open_args->owner.owner_val =
1865 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1866 bcopy(&oop->oo_name, open_args->owner.owner_val,
1867 open_args->owner.owner_len);
1868 open_args->share_access = 0;
1869 open_args->share_deny = 0;
1871 mutex_enter(&osp->os_sync_lock);
1872 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1873 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1874 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1875 (void *)osp, (void *)rp, osp->os_share_acc_read,
1876 osp->os_share_acc_write, osp->os_open_ref_count,
1877 osp->os_mmap_read, osp->os_mmap_write, claim));
1879 if (osp->os_share_acc_read || osp->os_mmap_read)
1880 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1881 if (osp->os_share_acc_write || osp->os_mmap_write)
1882 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1883 if (osp->os_share_deny_read)
1884 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1885 if (osp->os_share_deny_write)
1886 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1887 mutex_exit(&osp->os_sync_lock);
1889 seqid = nfs4_get_open_seqid(oop) + 1;
1890 open_args->seqid = seqid;
1892 /* Construct the getfh part of the compound */
1893 argop[2].argop = OP_GETFH;
1895 /* Construct the getattr part of the compound */
1896 argop[3].argop = OP_GETATTR;
1897 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1898 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1900 t = gethrtime();
1902 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1904 if (ep->error) {
1905 if (!is_recov && !frc_use_claim_previous &&
1906 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1907 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1908 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1909 cred_otw, vp, NULL, open_args);
1910 abort = nfs4_start_recovery(ep,
1911 VTOMI4(vp), vp, NULL, NULL,
1912 lost_rqst.lr_op == OP_OPEN ?
1913 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1914 nfs4args_copen_free(open_args);
1915 goto bailout;
1918 nfs4args_copen_free(open_args);
1920 if (ep->error == EACCES && cred_otw != cr) {
1921 crfree(cred_otw);
1922 cred_otw = cr;
1923 crhold(cred_otw);
1924 nfs4_end_open_seqid_sync(oop);
1925 open_owner_rele(oop);
1926 oop = NULL;
1927 goto top;
1929 if (ep->error == ETIMEDOUT)
1930 goto bailout;
1931 failed_msg = "Couldn't reopen: rpc error";
1932 goto kill_file;
1935 if (nfs4_need_to_bump_seqid(&res))
1936 nfs4_set_open_seqid(seqid, oop, args.ctag);
1938 switch (res.status) {
1939 case NFS4_OK:
1940 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1941 mutex_enter(&rp->r_statelock);
1942 rp->r_delay_interval = 0;
1943 mutex_exit(&rp->r_statelock);
1945 break;
1946 case NFS4ERR_BAD_SEQID:
1947 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1948 args.ctag, open_args->seqid);
1950 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1951 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1952 NULL, OP_OPEN, bsep, NULL, NULL);
1954 nfs4args_copen_free(open_args);
1955 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1956 nfs4_end_open_seqid_sync(oop);
1957 open_owner_rele(oop);
1958 oop = NULL;
1959 kmem_free(bsep, sizeof (*bsep));
1961 goto kill_file;
1962 case NFS4ERR_NO_GRACE:
1963 nfs4args_copen_free(open_args);
1964 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1965 nfs4_end_open_seqid_sync(oop);
1966 open_owner_rele(oop);
1967 oop = NULL;
1968 if (claim == CLAIM_PREVIOUS) {
1970 * Retry as a plain open. We don't need to worry about
1971 * checking the changeinfo: it is acceptable for a
1972 * client to re-open a file and continue processing
1973 * (in the absence of locks).
1975 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1976 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1977 "will retry as CLAIM_NULL"));
1978 claim = CLAIM_NULL;
1979 nfs4_mi_kstat_inc_no_grace(mi);
1980 goto top;
1982 failed_msg =
1983 "Couldn't reopen: tried reclaim outside grace period. ";
1984 goto kill_file;
1985 case NFS4ERR_GRACE:
1986 nfs4_set_grace_wait(mi);
1987 nfs4args_copen_free(open_args);
1988 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1989 nfs4_end_open_seqid_sync(oop);
1990 open_owner_rele(oop);
1991 oop = NULL;
1992 ep->error = nfs4_wait_for_grace(mi, &recov);
1993 if (ep->error != 0)
1994 goto bailout;
1995 goto top;
1996 case NFS4ERR_DELAY:
1997 nfs4_set_delay_wait(vp);
1998 nfs4args_copen_free(open_args);
1999 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2000 nfs4_end_open_seqid_sync(oop);
2001 open_owner_rele(oop);
2002 oop = NULL;
2003 ep->error = nfs4_wait_for_delay(vp, &recov);
2004 nfs4_mi_kstat_inc_delay(mi);
2005 if (ep->error != 0)
2006 goto bailout;
2007 goto top;
2008 case NFS4ERR_FHEXPIRED:
2009 /* recover filehandle and retry */
2010 abort = nfs4_start_recovery(ep,
2011 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2012 nfs4args_copen_free(open_args);
2013 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2014 nfs4_end_open_seqid_sync(oop);
2015 open_owner_rele(oop);
2016 oop = NULL;
2017 if (abort == FALSE)
2018 goto top;
2019 failed_msg = "Couldn't reopen: recovery aborted";
2020 goto kill_file;
2021 case NFS4ERR_RESOURCE:
2022 case NFS4ERR_STALE_CLIENTID:
2023 case NFS4ERR_WRONGSEC:
2024 case NFS4ERR_EXPIRED:
2026 * Do not mark the file dead and let the calling
2027 * function initiate recovery.
2029 nfs4args_copen_free(open_args);
2030 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2031 nfs4_end_open_seqid_sync(oop);
2032 open_owner_rele(oop);
2033 oop = NULL;
2034 goto bailout;
2035 case NFS4ERR_ACCESS:
2036 if (cred_otw != cr) {
2037 crfree(cred_otw);
2038 cred_otw = cr;
2039 crhold(cred_otw);
2040 nfs4args_copen_free(open_args);
2041 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2042 nfs4_end_open_seqid_sync(oop);
2043 open_owner_rele(oop);
2044 oop = NULL;
2045 goto top;
2047 /* fall through */
2048 default:
2049 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2050 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2051 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2052 rnode4info(VTOR4(vp))));
2053 failed_msg = "Couldn't reopen: NFSv4 error";
2054 nfs4args_copen_free(open_args);
2055 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2056 goto kill_file;
2059 resop = &res.array[1]; /* open res */
2060 op_res = &resop->nfs_resop4_u.opopen;
2062 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2065 * Check if the path we reopened really is the same
2066 * file. We could end up in a situation where the file
2067 * was removed and a new file created with the same name.
2069 resop = &res.array[2];
2070 gf_res = &resop->nfs_resop4_u.opgetfh;
2071 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2072 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2073 if (fh_different) {
2074 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2075 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2076 /* Oops, we don't have the same file */
2077 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2078 failed_msg = "Couldn't reopen: Persistent "
2079 "file handle changed";
2080 else
2081 failed_msg = "Couldn't reopen: Volatile "
2082 "(no expire on open) file handle changed";
2084 nfs4args_copen_free(open_args);
2085 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2086 nfs_rw_exit(&mi->mi_fh_lock);
2087 goto kill_file;
2089 } else {
2091 * We have volatile file handles that don't compare.
2092 * If the fids are the same then we assume that the
2093 * file handle expired but the rnode still refers to
2094 * the same file object.
2096 * First check that we have fids or not.
2097 * If we don't we have a dumb server so we will
2098 * just assume every thing is ok for now.
2100 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2101 rp->r_attr.va_mask & AT_NODEID &&
2102 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2104 * We have fids, but they don't
2105 * compare. So kill the file.
2107 failed_msg =
2108 "Couldn't reopen: file handle changed"
2109 " due to mismatched fids";
2110 nfs4args_copen_free(open_args);
2111 (void) xdr_free(xdr_COMPOUND4res_clnt,
2112 (caddr_t)&res);
2113 nfs_rw_exit(&mi->mi_fh_lock);
2114 goto kill_file;
2115 } else {
2117 * We have volatile file handles that refers
2118 * to the same file (at least they have the
2119 * same fid) or we don't have fids so we
2120 * can't tell. :(. We'll be a kind and accepting
2121 * client so we'll update the rnode's file
2122 * handle with the otw handle.
2124 * We need to drop mi->mi_fh_lock since
2125 * sh4_update acquires it. Since there is
2126 * only one recovery thread there is no
2127 * race.
2129 nfs_rw_exit(&mi->mi_fh_lock);
2130 sfh4_update(rp->r_fh, &gf_res->object);
2133 } else {
2134 nfs_rw_exit(&mi->mi_fh_lock);
2137 ASSERT(nfs4_consistent_type(vp));
2140 * If the server wanted an OPEN_CONFIRM but that fails, just start
2141 * over. Presumably if there is a persistent error it will show up
2142 * when we resend the OPEN.
2144 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2145 bool_t retry_open = FALSE;
2147 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2148 cred_otw, is_recov, &retry_open,
2149 oop, FALSE, ep, NULL);
2150 if (ep->error || ep->stat) {
2151 nfs4args_copen_free(open_args);
2152 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2153 nfs4_end_open_seqid_sync(oop);
2154 open_owner_rele(oop);
2155 oop = NULL;
2156 goto top;
2160 mutex_enter(&osp->os_sync_lock);
2161 osp->open_stateid = op_res->stateid;
2162 osp->os_delegation = 0;
2164 * Need to reset this bitfield for the possible case where we were
2165 * going to OTW CLOSE the file, got a non-recoverable error, and before
2166 * we could retry the CLOSE, OPENed the file again.
2168 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2169 osp->os_final_close = 0;
2170 osp->os_force_close = 0;
2171 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2172 osp->os_dc_openacc = open_args->share_access;
2173 mutex_exit(&osp->os_sync_lock);
2175 nfs4_end_open_seqid_sync(oop);
2177 /* accept delegation, if any */
2178 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2180 nfs4args_copen_free(open_args);
2182 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2184 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2186 ASSERT(nfs4_consistent_type(vp));
2188 open_owner_rele(oop);
2189 crfree(cr);
2190 crfree(cred_otw);
2191 return;
2193 kill_file:
2194 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2195 failed_reopen:
2196 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2197 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2198 (void *)osp, (void *)cr, rnode4info(rp)));
2199 mutex_enter(&osp->os_sync_lock);
2200 osp->os_failed_reopen = 1;
2201 mutex_exit(&osp->os_sync_lock);
2202 bailout:
2203 if (oop != NULL) {
2204 nfs4_end_open_seqid_sync(oop);
2205 open_owner_rele(oop);
2207 if (cr != NULL)
2208 crfree(cr);
2209 if (cred_otw != NULL)
2210 crfree(cred_otw);
2213 /* for . and .. OPENs */
2214 /* ARGSUSED */
2215 static int
2216 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2218 rnode4_t *rp;
2219 nfs4_ga_res_t gar;
2221 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2224 * If close-to-open consistency checking is turned off or
2225 * if there is no cached data, we can avoid
2226 * the over the wire getattr. Otherwise, force a
2227 * call to the server to get fresh attributes and to
2228 * check caches. This is required for close-to-open
2229 * consistency.
2231 rp = VTOR4(*vpp);
2232 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2233 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2234 return (0);
2236 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2240 * CLOSE a file
2242 /* ARGSUSED */
2243 static int
2244 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2245 caller_context_t *ct)
2247 rnode4_t *rp;
2248 int error = 0;
2249 int r_error = 0;
2250 int n4error = 0;
2251 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2254 * Remove client state for this (lockowner, file) pair.
2255 * Issue otw v4 call to have the server do the same.
2258 rp = VTOR4(vp);
2261 * zone_enter(2) prevents processes from changing zones with NFS files
2262 * open; if we happen to get here from the wrong zone we can't do
2263 * anything over the wire.
2265 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2267 * We could attempt to clean up locks, except we're sure
2268 * that the current process didn't acquire any locks on
2269 * the file: any attempt to lock a file belong to another zone
2270 * will fail, and one can't lock an NFS file and then change
2271 * zones, as that fails too.
2273 * Returning an error here is the sane thing to do. A
2274 * subsequent call to VN_RELE() which translates to a
2275 * nfs4_inactive() will clean up state: if the zone of the
2276 * vnode's origin is still alive and kicking, the inactive
2277 * thread will handle the request (from the correct zone), and
2278 * everything (minus the OTW close call) should be OK. If the
2279 * zone is going away nfs4_async_inactive() will throw away
2280 * delegations, open streams and cached pages inline.
2282 return (EIO);
2286 * If we are using local locking for this filesystem, then
2287 * release all of the SYSV style record locks. Otherwise,
2288 * we are doing network locking and we need to release all
2289 * of the network locks. All of the locks held by this
2290 * process on this file are released no matter what the
2291 * incoming reference count is.
2293 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2294 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2295 cleanshares(vp, ttoproc(curthread)->p_pid);
2296 } else
2297 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2299 if (e.error) {
2300 struct lm_sysid *lmsid;
2301 lmsid = nfs4_find_sysid(VTOMI4(vp));
2302 if (lmsid == NULL) {
2303 DTRACE_PROBE2(unknown__sysid, int, e.error,
2304 vnode_t *, vp);
2305 } else {
2306 cleanlocks(vp, ttoproc(curthread)->p_pid,
2307 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2309 lm_rel_sysid(lmsid);
2311 return (e.error);
2314 if (count > 1)
2315 return (0);
2318 * If the file has been `unlinked', then purge the
2319 * DNLC so that this vnode will get reycled quicker
2320 * and the .nfs* file on the server will get removed.
2322 if (rp->r_unldvp != NULL)
2323 dnlc_purge_vp(vp);
2326 * If the file was open for write and there are pages,
2327 * do a synchronous flush and commit of all of the
2328 * dirty and uncommitted pages.
2330 ASSERT(!e.error);
2331 if ((flag & FWRITE) && nfs4_has_pages(vp))
2332 error = nfs4_putpage_commit(vp, 0, 0, cr);
2334 mutex_enter(&rp->r_statelock);
2335 r_error = rp->r_error;
2336 rp->r_error = 0;
2337 mutex_exit(&rp->r_statelock);
2340 * If this file type is one for which no explicit 'open' was
2341 * done, then bail now (ie. no need for protocol 'close'). If
2342 * there was an error w/the vm subsystem, return _that_ error,
2343 * otherwise, return any errors that may've been reported via
2344 * the rnode.
2346 if (vp->v_type != VREG)
2347 return (error ? error : r_error);
2350 * The sync putpage commit may have failed above, but since
2351 * we're working w/a regular file, we need to do the protocol
2352 * 'close' (nfs4close_one will figure out if an otw close is
2353 * needed or not). Report any errors _after_ doing the protocol
2354 * 'close'.
2356 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2357 n4error = e.error ? e.error : geterrno4(e.stat);
2360 * Error reporting prio (Hi -> Lo)
2362 * i) nfs4_putpage_commit (error)
2363 * ii) rnode's (r_error)
2364 * iii) nfs4close_one (n4error)
2366 return (error ? error : (r_error ? r_error : n4error));
2370 * Initialize *lost_rqstp.
2373 static void
2374 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2375 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2376 vnode_t *vp)
2378 if (error != ETIMEDOUT && error != EINTR &&
2379 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2380 lost_rqstp->lr_op = 0;
2381 return;
2384 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2385 "nfs4close_save_lost_rqst: error %d", error));
2387 lost_rqstp->lr_op = OP_CLOSE;
2389 * The vp is held and rele'd via the recovery code.
2390 * See nfs4_save_lost_rqst.
2392 lost_rqstp->lr_vp = vp;
2393 lost_rqstp->lr_dvp = NULL;
2394 lost_rqstp->lr_oop = oop;
2395 lost_rqstp->lr_osp = osp;
2396 ASSERT(osp != NULL);
2397 ASSERT(mutex_owned(&osp->os_sync_lock));
2398 osp->os_pending_close = 1;
2399 lost_rqstp->lr_lop = NULL;
2400 lost_rqstp->lr_cr = cr;
2401 lost_rqstp->lr_flk = NULL;
2402 lost_rqstp->lr_putfirst = FALSE;
2406 * Assumes you already have the open seqid sync grabbed as well as the
2407 * 'os_sync_lock'. Note: this will release the open seqid sync and
2408 * 'os_sync_lock' if client recovery starts. Calling functions have to
2409 * be prepared to handle this.
2411 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2412 * was needed and was started, and that the calling function should retry
2413 * this function; otherwise it is returned as 0.
2415 * Errors are returned via the nfs4_error_t parameter.
2417 static void
2418 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2419 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2420 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2422 COMPOUND4args_clnt args;
2423 COMPOUND4res_clnt res;
2424 CLOSE4args *close_args;
2425 nfs_resop4 *resop;
2426 nfs_argop4 argop[3];
2427 int doqueue = 1;
2428 mntinfo4_t *mi;
2429 seqid4 seqid;
2430 vnode_t *vp;
2431 bool_t needrecov = FALSE;
2432 nfs4_lost_rqst_t lost_rqst;
2433 hrtime_t t;
2435 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2437 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2439 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2441 /* Only set this to 1 if recovery is started */
2442 *recov = 0;
2444 /* do the OTW call to close the file */
2446 if (close_type == CLOSE_RESEND)
2447 args.ctag = TAG_CLOSE_LOST;
2448 else if (close_type == CLOSE_AFTER_RESEND)
2449 args.ctag = TAG_CLOSE_UNDO;
2450 else
2451 args.ctag = TAG_CLOSE;
2453 args.array_len = 3;
2454 args.array = argop;
2456 vp = RTOV4(rp);
2458 mi = VTOMI4(vp);
2460 /* putfh target fh */
2461 argop[0].argop = OP_CPUTFH;
2462 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2464 argop[1].argop = OP_GETATTR;
2465 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2466 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2468 argop[2].argop = OP_CLOSE;
2469 close_args = &argop[2].nfs_argop4_u.opclose;
2471 seqid = nfs4_get_open_seqid(oop) + 1;
2473 close_args->seqid = seqid;
2474 close_args->open_stateid = osp->open_stateid;
2476 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2477 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2478 rnode4info(rp)));
2480 t = gethrtime();
2482 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2484 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2485 nfs4_set_open_seqid(seqid, oop, args.ctag);
2488 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2489 if (ep->error && !needrecov) {
2491 * if there was an error and no recovery is to be done
2492 * then then set up the file to flush its cache if
2493 * needed for the next caller.
2495 mutex_enter(&rp->r_statelock);
2496 PURGE_ATTRCACHE4_LOCKED(rp);
2497 rp->r_flags &= ~R4WRITEMODIFIED;
2498 mutex_exit(&rp->r_statelock);
2499 return;
2502 if (needrecov) {
2503 bool_t abort;
2504 nfs4_bseqid_entry_t *bsep = NULL;
2506 if (close_type != CLOSE_RESEND)
2507 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2508 osp, cred_otw, vp);
2510 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2511 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2512 0, args.ctag, close_args->seqid);
2514 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2515 "nfs4close_otw: initiating recovery. error %d "
2516 "res.status %d", ep->error, res.status));
2519 * Drop the 'os_sync_lock' here so we don't hit
2520 * a potential recursive mutex_enter via an
2521 * 'open_stream_hold()'.
2523 mutex_exit(&osp->os_sync_lock);
2524 *have_sync_lockp = 0;
2525 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2526 (close_type != CLOSE_RESEND &&
2527 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2528 OP_CLOSE, bsep, NULL, NULL);
2530 /* drop open seq sync, and let the calling function regrab it */
2531 nfs4_end_open_seqid_sync(oop);
2532 *did_start_seqid_syncp = 0;
2534 if (bsep)
2535 kmem_free(bsep, sizeof (*bsep));
2537 * For signals, the caller wants to quit, so don't say to
2538 * retry. For forced unmount, if it's a user thread, it
2539 * wants to quit. If it's a recovery thread, the retry
2540 * will happen higher-up on the call stack. Either way,
2541 * don't say to retry.
2543 if (abort == FALSE && ep->error != EINTR &&
2544 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2545 close_type != CLOSE_RESEND &&
2546 close_type != CLOSE_AFTER_RESEND)
2547 *recov = 1;
2548 else
2549 *recov = 0;
2551 if (!ep->error)
2552 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2553 return;
2556 if (res.status) {
2557 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2558 return;
2561 mutex_enter(&rp->r_statev4_lock);
2562 rp->created_v4 = 0;
2563 mutex_exit(&rp->r_statev4_lock);
2565 resop = &res.array[2];
2566 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2567 osp->os_valid = 0;
2570 * This removes the reference obtained at OPEN; ie, when the
2571 * open stream structure was created.
2573 * We don't have to worry about calling 'open_stream_rele'
2574 * since we our currently holding a reference to the open
2575 * stream which means the count cannot go to 0 with this
2576 * decrement.
2578 ASSERT(osp->os_ref_count >= 2);
2579 osp->os_ref_count--;
2581 if (ep->error == 0) {
2583 * Avoid a deadlock with the r_serial thread waiting for
2584 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be
2585 * held by us. We will wait in nfs4_attr_cache() for the
2586 * completion of the r_serial thread.
2588 mutex_exit(&osp->os_sync_lock);
2589 *have_sync_lockp = 0;
2591 nfs4_attr_cache(vp,
2592 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2593 t, cred_otw, TRUE, NULL);
2596 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2597 " returning %d", ep->error));
2599 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2602 /* ARGSUSED */
2603 static int
2604 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2605 caller_context_t *ct)
2607 rnode4_t *rp;
2608 uoff_t off;
2609 offset_t diff;
2610 uint_t on;
2611 uint_t n;
2612 caddr_t base;
2613 uint_t flags;
2614 int error;
2615 mntinfo4_t *mi;
2617 rp = VTOR4(vp);
2619 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2621 if (IS_SHADOW(vp, rp))
2622 vp = RTOV4(rp);
2624 if (vp->v_type != VREG)
2625 return (EISDIR);
2627 mi = VTOMI4(vp);
2629 if (nfs_zone() != mi->mi_zone)
2630 return (EIO);
2632 if (uiop->uio_resid == 0)
2633 return (0);
2635 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2636 return (EINVAL);
2638 mutex_enter(&rp->r_statelock);
2639 if (rp->r_flags & R4RECOVERRP)
2640 error = (rp->r_error ? rp->r_error : EIO);
2641 else
2642 error = 0;
2643 mutex_exit(&rp->r_statelock);
2644 if (error)
2645 return (error);
2648 * Bypass VM if caching has been disabled (e.g., locking) or if
2649 * using client-side direct I/O and the file is not mmap'd and
2650 * there are no cached pages.
2652 if ((vp->v_flag & VNOCACHE) ||
2653 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2654 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2655 size_t resid = 0;
2657 return (nfs4read(vp, NULL, uiop->uio_loffset,
2658 uiop->uio_resid, &resid, cr, FALSE, uiop));
2661 error = 0;
2663 do {
2664 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2665 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2666 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2668 if (error = nfs4_validate_caches(vp, cr))
2669 break;
2671 mutex_enter(&rp->r_statelock);
2672 while (rp->r_flags & R4INCACHEPURGE) {
2673 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2674 mutex_exit(&rp->r_statelock);
2675 return (EINTR);
2678 diff = rp->r_size - uiop->uio_loffset;
2679 mutex_exit(&rp->r_statelock);
2680 if (diff <= 0)
2681 break;
2682 if (diff < n)
2683 n = (uint_t)diff;
2685 if (vpm_enable) {
2687 * Copy data.
2689 error = vpm_data_copy(vp, off + on, n, uiop,
2690 1, NULL, 0, S_READ);
2691 } else {
2692 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2693 S_READ);
2695 error = uiomove(base + on, n, UIO_READ, uiop);
2698 if (!error) {
2700 * If read a whole block or read to eof,
2701 * won't need this buffer again soon.
2703 mutex_enter(&rp->r_statelock);
2704 if (n + on == MAXBSIZE ||
2705 uiop->uio_loffset == rp->r_size)
2706 flags = SM_DONTNEED;
2707 else
2708 flags = 0;
2709 mutex_exit(&rp->r_statelock);
2710 if (vpm_enable) {
2711 error = vpm_sync_pages(vp, off, n, flags);
2712 } else {
2713 error = segmap_release(segkmap, base, flags);
2715 } else {
2716 if (vpm_enable) {
2717 (void) vpm_sync_pages(vp, off, n, 0);
2718 } else {
2719 (void) segmap_release(segkmap, base, 0);
2722 } while (!error && uiop->uio_resid > 0);
2724 return (error);
2727 /* ARGSUSED */
2728 static int
2729 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2730 caller_context_t *ct)
2732 rlim64_t limit = uiop->uio_llimit;
2733 rnode4_t *rp;
2734 uoff_t off;
2735 caddr_t base;
2736 uint_t flags;
2737 int remainder;
2738 size_t n;
2739 int on;
2740 int error;
2741 int resid;
2742 uoff_t offset;
2743 mntinfo4_t *mi;
2744 uint_t bsize;
2746 rp = VTOR4(vp);
2748 if (IS_SHADOW(vp, rp))
2749 vp = RTOV4(rp);
2751 if (vp->v_type != VREG)
2752 return (EISDIR);
2754 mi = VTOMI4(vp);
2756 if (nfs_zone() != mi->mi_zone)
2757 return (EIO);
2759 if (uiop->uio_resid == 0)
2760 return (0);
2762 mutex_enter(&rp->r_statelock);
2763 if (rp->r_flags & R4RECOVERRP)
2764 error = (rp->r_error ? rp->r_error : EIO);
2765 else
2766 error = 0;
2767 mutex_exit(&rp->r_statelock);
2768 if (error)
2769 return (error);
2771 if (ioflag & FAPPEND) {
2772 struct vattr va;
2775 * Must serialize if appending.
2777 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2778 nfs_rw_exit(&rp->r_rwlock);
2779 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2780 INTR4(vp)))
2781 return (EINTR);
2784 va.va_mask = AT_SIZE;
2785 error = nfs4getattr(vp, &va, cr);
2786 if (error)
2787 return (error);
2788 uiop->uio_loffset = va.va_size;
2791 offset = uiop->uio_loffset + uiop->uio_resid;
2793 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2794 return (EINVAL);
2796 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2797 limit = MAXOFFSET_T;
2800 * Check to make sure that the process will not exceed
2801 * its limit on file size. It is okay to write up to
2802 * the limit, but not beyond. Thus, the write which
2803 * reaches the limit will be short and the next write
2804 * will return an error.
2806 remainder = 0;
2807 if (offset > uiop->uio_llimit) {
2808 remainder = offset - uiop->uio_llimit;
2809 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2810 if (uiop->uio_resid <= 0) {
2811 proc_t *p = ttoproc(curthread);
2813 uiop->uio_resid += remainder;
2814 mutex_enter(&p->p_lock);
2815 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2816 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2817 mutex_exit(&p->p_lock);
2818 return (EFBIG);
2822 /* update the change attribute, if we have a write delegation */
2824 mutex_enter(&rp->r_statev4_lock);
2825 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2826 rp->r_deleg_change++;
2828 mutex_exit(&rp->r_statev4_lock);
2830 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2831 return (EINTR);
2834 * Bypass VM if caching has been disabled (e.g., locking) or if
2835 * using client-side direct I/O and the file is not mmap'd and
2836 * there are no cached pages.
2838 if ((vp->v_flag & VNOCACHE) ||
2839 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2840 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2841 size_t bufsize;
2842 int count;
2843 uoff_t org_offset;
2844 stable_how4 stab_comm;
2845 nfs4_fwrite:
2846 if (rp->r_flags & R4STALE) {
2847 resid = uiop->uio_resid;
2848 offset = uiop->uio_loffset;
2849 error = rp->r_error;
2851 * A close may have cleared r_error, if so,
2852 * propagate ESTALE error return properly
2854 if (error == 0)
2855 error = ESTALE;
2856 goto bottom;
2859 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2860 base = kmem_alloc(bufsize, KM_SLEEP);
2861 do {
2862 if (ioflag & FDSYNC)
2863 stab_comm = DATA_SYNC4;
2864 else
2865 stab_comm = FILE_SYNC4;
2866 resid = uiop->uio_resid;
2867 offset = uiop->uio_loffset;
2868 count = MIN(uiop->uio_resid, bufsize);
2869 org_offset = uiop->uio_loffset;
2870 error = uiomove(base, count, UIO_WRITE, uiop);
2871 if (!error) {
2872 error = nfs4write(vp, base, org_offset,
2873 count, cr, &stab_comm);
2874 if (!error) {
2875 mutex_enter(&rp->r_statelock);
2876 if (rp->r_size < uiop->uio_loffset)
2877 rp->r_size = uiop->uio_loffset;
2878 mutex_exit(&rp->r_statelock);
2881 } while (!error && uiop->uio_resid > 0);
2882 kmem_free(base, bufsize);
2883 goto bottom;
2886 bsize = vp->v_vfsp->vfs_bsize;
2888 do {
2889 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2890 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2891 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2893 resid = uiop->uio_resid;
2894 offset = uiop->uio_loffset;
2896 if (rp->r_flags & R4STALE) {
2897 error = rp->r_error;
2899 * A close may have cleared r_error, if so,
2900 * propagate ESTALE error return properly
2902 if (error == 0)
2903 error = ESTALE;
2904 break;
2908 * Don't create dirty pages faster than they
2909 * can be cleaned so that the system doesn't
2910 * get imbalanced. If the async queue is
2911 * maxed out, then wait for it to drain before
2912 * creating more dirty pages. Also, wait for
2913 * any threads doing pagewalks in the vop_getattr
2914 * entry points so that they don't block for
2915 * long periods.
2917 mutex_enter(&rp->r_statelock);
2918 while ((mi->mi_max_threads != 0 &&
2919 rp->r_awcount > 2 * mi->mi_max_threads) ||
2920 rp->r_gcount > 0) {
2921 if (INTR4(vp)) {
2922 klwp_t *lwp = ttolwp(curthread);
2924 if (lwp != NULL)
2925 lwp->lwp_nostop++;
2926 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2927 mutex_exit(&rp->r_statelock);
2928 if (lwp != NULL)
2929 lwp->lwp_nostop--;
2930 error = EINTR;
2931 goto bottom;
2933 if (lwp != NULL)
2934 lwp->lwp_nostop--;
2935 } else
2936 cv_wait(&rp->r_cv, &rp->r_statelock);
2938 mutex_exit(&rp->r_statelock);
2941 * Touch the page and fault it in if it is not in core
2942 * before segmap_getmapflt or vpm_data_copy can lock it.
2943 * This is to avoid the deadlock if the buffer is mapped
2944 * to the same file through mmap which we want to write.
2946 uio_prefaultpages((long)n, uiop);
2948 if (vpm_enable) {
2950 * It will use kpm mappings, so no need to
2951 * pass an address.
2953 error = writerp4(rp, NULL, n, uiop, 0);
2954 } else {
2955 if (segmap_kpm) {
2956 int pon = uiop->uio_loffset & PAGEOFFSET;
2957 size_t pn = MIN(PAGESIZE - pon,
2958 uiop->uio_resid);
2959 int pagecreate;
2961 mutex_enter(&rp->r_statelock);
2962 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2963 uiop->uio_loffset + pn >= rp->r_size);
2964 mutex_exit(&rp->r_statelock);
2966 base = segmap_getmapflt(segkmap, vp, off + on,
2967 pn, !pagecreate, S_WRITE);
2969 error = writerp4(rp, base + pon, n, uiop,
2970 pagecreate);
2972 } else {
2973 base = segmap_getmapflt(segkmap, vp, off + on,
2974 n, 0, S_READ);
2975 error = writerp4(rp, base + on, n, uiop, 0);
2979 if (!error) {
2980 if (mi->mi_flags & MI4_NOAC)
2981 flags = SM_WRITE;
2982 else if ((uiop->uio_loffset % bsize) == 0 ||
2983 IS_SWAPVP(vp)) {
2985 * Have written a whole block.
2986 * Start an asynchronous write
2987 * and mark the buffer to
2988 * indicate that it won't be
2989 * needed again soon.
2991 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2992 } else
2993 flags = 0;
2994 if ((ioflag & (FSYNC|FDSYNC)) ||
2995 (rp->r_flags & R4OUTOFSPACE)) {
2996 flags &= ~SM_ASYNC;
2997 flags |= SM_WRITE;
2999 if (vpm_enable) {
3000 error = vpm_sync_pages(vp, off, n, flags);
3001 } else {
3002 error = segmap_release(segkmap, base, flags);
3004 } else {
3005 if (vpm_enable) {
3006 (void) vpm_sync_pages(vp, off, n, 0);
3007 } else {
3008 (void) segmap_release(segkmap, base, 0);
3011 * In the event that we got an access error while
3012 * faulting in a page for a write-only file just
3013 * force a write.
3015 if (error == EACCES)
3016 goto nfs4_fwrite;
3018 } while (!error && uiop->uio_resid > 0);
3020 bottom:
3021 if (error) {
3022 uiop->uio_resid = resid + remainder;
3023 uiop->uio_loffset = offset;
3024 } else {
3025 uiop->uio_resid += remainder;
3027 mutex_enter(&rp->r_statev4_lock);
3028 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3029 gethrestime(&rp->r_attr.va_mtime);
3030 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3032 mutex_exit(&rp->r_statev4_lock);
3035 nfs_rw_exit(&rp->r_lkserlock);
3037 return (error);
3041 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3043 static int
3044 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, uoff_t off, size_t len,
3045 int flags, cred_t *cr)
3047 struct buf *bp;
3048 int error;
3049 page_t *savepp;
3050 uchar_t fsdata;
3051 stable_how4 stab_comm;
3053 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3054 bp = pageio_setup(pp, len, vp, flags);
3055 ASSERT(bp != NULL);
3058 * pageio_setup should have set b_addr to 0. This
3059 * is correct since we want to do I/O on a page
3060 * boundary. bp_mapin will use this addr to calculate
3061 * an offset, and then set b_addr to the kernel virtual
3062 * address it allocated for us.
3064 ASSERT(bp->b_un.b_addr == 0);
3066 bp->b_edev = 0;
3067 bp->b_dev = 0;
3068 bp->b_lblkno = lbtodb(off);
3069 bp->b_file = vp;
3070 bp->b_offset = (offset_t)off;
3071 bp_mapin(bp);
3073 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3074 freemem > desfree)
3075 stab_comm = UNSTABLE4;
3076 else
3077 stab_comm = FILE_SYNC4;
3079 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3081 bp_mapout(bp);
3082 pageio_done(bp);
3084 if (stab_comm == UNSTABLE4)
3085 fsdata = C_DELAYCOMMIT;
3086 else
3087 fsdata = C_NOCOMMIT;
3089 savepp = pp;
3090 do {
3091 pp->p_fsdata = fsdata;
3092 } while ((pp = pp->p_next) != savepp);
3094 return (error);
3099 static int
3100 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3102 nfs4_open_owner_t *oop;
3103 nfs4_open_stream_t *osp;
3104 rnode4_t *rp = VTOR4(vp);
3105 mntinfo4_t *mi = VTOMI4(vp);
3106 int reopen_needed;
3108 ASSERT(nfs_zone() == mi->mi_zone);
3111 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3112 if (!oop)
3113 return (EIO);
3115 /* returns with 'os_sync_lock' held */
3116 osp = find_open_stream(oop, rp);
3117 if (!osp) {
3118 open_owner_rele(oop);
3119 return (EIO);
3122 if (osp->os_failed_reopen) {
3123 mutex_exit(&osp->os_sync_lock);
3124 open_stream_rele(osp, rp);
3125 open_owner_rele(oop);
3126 return (EIO);
3130 * Determine whether a reopen is needed. If this
3131 * is a delegation open stream, then the os_delegation bit
3132 * should be set.
3135 reopen_needed = osp->os_delegation;
3137 mutex_exit(&osp->os_sync_lock);
3138 open_owner_rele(oop);
3140 if (reopen_needed) {
3141 nfs4_error_zinit(ep);
3142 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3143 mutex_enter(&osp->os_sync_lock);
3144 if (ep->error || ep->stat || osp->os_failed_reopen) {
3145 mutex_exit(&osp->os_sync_lock);
3146 open_stream_rele(osp, rp);
3147 return (EIO);
3149 mutex_exit(&osp->os_sync_lock);
3151 open_stream_rele(osp, rp);
3153 return (0);
3157 * Write to file. Writes to remote server in largest size
3158 * chunks that the server can handle. Write is synchronous.
3160 static int
3161 nfs4write(vnode_t *vp, caddr_t base, uoff_t offset, int count, cred_t *cr,
3162 stable_how4 *stab_comm)
3164 mntinfo4_t *mi;
3165 COMPOUND4args_clnt args;
3166 COMPOUND4res_clnt res;
3167 WRITE4args *wargs;
3168 WRITE4res *wres;
3169 nfs_argop4 argop[2];
3170 nfs_resop4 *resop;
3171 int tsize;
3172 stable_how4 stable;
3173 rnode4_t *rp;
3174 int doqueue = 1;
3175 bool_t needrecov;
3176 nfs4_recov_state_t recov_state;
3177 nfs4_stateid_types_t sid_types;
3178 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3179 int recov;
3181 rp = VTOR4(vp);
3182 mi = VTOMI4(vp);
3184 ASSERT(nfs_zone() == mi->mi_zone);
3186 stable = *stab_comm;
3187 *stab_comm = FILE_SYNC4;
3189 needrecov = FALSE;
3190 recov_state.rs_flags = 0;
3191 recov_state.rs_num_retry_despite_err = 0;
3192 nfs4_init_stateid_types(&sid_types);
3194 /* Is curthread the recovery thread? */
3195 mutex_enter(&mi->mi_lock);
3196 recov = (mi->mi_recovthread == curthread);
3197 mutex_exit(&mi->mi_lock);
3199 recov_retry:
3200 args.ctag = TAG_WRITE;
3201 args.array_len = 2;
3202 args.array = argop;
3204 if (!recov) {
3205 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3206 &recov_state, NULL);
3207 if (e.error)
3208 return (e.error);
3211 /* 0. putfh target fh */
3212 argop[0].argop = OP_CPUTFH;
3213 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3215 /* 1. write */
3216 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3218 do {
3220 wargs->offset = (offset4)offset;
3221 wargs->data_val = base;
3223 if (mi->mi_io_kstats) {
3224 mutex_enter(&mi->mi_lock);
3225 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3226 mutex_exit(&mi->mi_lock);
3229 if ((vp->v_flag & VNOCACHE) ||
3230 (rp->r_flags & R4DIRECTIO) ||
3231 (mi->mi_flags & MI4_DIRECTIO))
3232 tsize = MIN(mi->mi_stsize, count);
3233 else
3234 tsize = MIN(mi->mi_curwrite, count);
3235 wargs->data_len = (uint_t)tsize;
3236 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3238 if (mi->mi_io_kstats) {
3239 mutex_enter(&mi->mi_lock);
3240 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3241 mutex_exit(&mi->mi_lock);
3244 if (!recov) {
3245 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3246 if (e.error && !needrecov) {
3247 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3248 &recov_state, needrecov);
3249 return (e.error);
3251 } else {
3252 if (e.error)
3253 return (e.error);
3257 * Do handling of OLD_STATEID outside
3258 * of the normal recovery framework.
3260 * If write receives a BAD stateid error while using a
3261 * delegation stateid, retry using the open stateid (if it
3262 * exists). If it doesn't have an open stateid, reopen the
3263 * file first, then retry.
3265 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3266 sid_types.cur_sid_type != SPEC_SID) {
3267 nfs4_save_stateid(&wargs->stateid, &sid_types);
3268 if (!recov)
3269 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3270 &recov_state, needrecov);
3271 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3272 goto recov_retry;
3273 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3274 sid_types.cur_sid_type == DEL_SID) {
3275 nfs4_save_stateid(&wargs->stateid, &sid_types);
3276 mutex_enter(&rp->r_statev4_lock);
3277 rp->r_deleg_return_pending = TRUE;
3278 mutex_exit(&rp->r_statev4_lock);
3279 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3280 if (!recov)
3281 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3282 &recov_state, needrecov);
3283 (void) xdr_free(xdr_COMPOUND4res_clnt,
3284 (caddr_t)&res);
3285 return (EIO);
3287 if (!recov)
3288 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3289 &recov_state, needrecov);
3290 /* hold needed for nfs4delegreturn_thread */
3291 VN_HOLD(vp);
3292 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3293 NFS4_DR_DISCARD), FALSE);
3294 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3295 goto recov_retry;
3298 if (needrecov) {
3299 bool_t abort;
3301 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3302 "nfs4write: client got error %d, res.status %d"
3303 ", so start recovery", e.error, res.status));
3305 abort = nfs4_start_recovery(&e,
3306 VTOMI4(vp), vp, NULL, &wargs->stateid,
3307 NULL, OP_WRITE, NULL, NULL, NULL);
3308 if (!e.error) {
3309 e.error = geterrno4(res.status);
3310 (void) xdr_free(xdr_COMPOUND4res_clnt,
3311 (caddr_t)&res);
3313 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3314 &recov_state, needrecov);
3315 if (abort == FALSE)
3316 goto recov_retry;
3317 return (e.error);
3320 if (res.status) {
3321 e.error = geterrno4(res.status);
3322 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3323 if (!recov)
3324 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3325 &recov_state, needrecov);
3326 return (e.error);
3329 resop = &res.array[1]; /* write res */
3330 wres = &resop->nfs_resop4_u.opwrite;
3332 if ((int)wres->count > tsize) {
3333 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3335 zcmn_err(getzoneid(), CE_WARN,
3336 "nfs4write: server wrote %u, requested was %u",
3337 (int)wres->count, tsize);
3338 if (!recov)
3339 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3340 &recov_state, needrecov);
3341 return (EIO);
3343 if (wres->committed == UNSTABLE4) {
3344 *stab_comm = UNSTABLE4;
3345 if (wargs->stable == DATA_SYNC4 ||
3346 wargs->stable == FILE_SYNC4) {
3347 (void) xdr_free(xdr_COMPOUND4res_clnt,
3348 (caddr_t)&res);
3349 zcmn_err(getzoneid(), CE_WARN,
3350 "nfs4write: server %s did not commit "
3351 "to stable storage",
3352 rp->r_server->sv_hostname);
3353 if (!recov)
3354 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3355 OH_WRITE, &recov_state, needrecov);
3356 return (EIO);
3360 tsize = (int)wres->count;
3361 count -= tsize;
3362 base += tsize;
3363 offset += tsize;
3364 if (mi->mi_io_kstats) {
3365 mutex_enter(&mi->mi_lock);
3366 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3367 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3368 tsize;
3369 mutex_exit(&mi->mi_lock);
3371 lwp_stat_update(LWP_STAT_OUBLK, 1);
3372 mutex_enter(&rp->r_statelock);
3373 if (rp->r_flags & R4HAVEVERF) {
3374 if (rp->r_writeverf != wres->writeverf) {
3375 nfs4_set_mod(vp);
3376 rp->r_writeverf = wres->writeverf;
3378 } else {
3379 rp->r_writeverf = wres->writeverf;
3380 rp->r_flags |= R4HAVEVERF;
3382 PURGE_ATTRCACHE4_LOCKED(rp);
3383 rp->r_flags |= R4WRITEMODIFIED;
3384 gethrestime(&rp->r_attr.va_mtime);
3385 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3386 mutex_exit(&rp->r_statelock);
3387 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3388 } while (count);
3390 if (!recov)
3391 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3392 needrecov);
3394 return (e.error);
3398 * Read from a file. Reads data in largest chunks our interface can handle.
3400 static int
3401 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3402 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3404 mntinfo4_t *mi;
3405 COMPOUND4args_clnt args;
3406 COMPOUND4res_clnt res;
3407 READ4args *rargs;
3408 nfs_argop4 argop[2];
3409 int tsize;
3410 int doqueue;
3411 rnode4_t *rp;
3412 int data_len;
3413 bool_t is_eof;
3414 bool_t needrecov = FALSE;
3415 nfs4_recov_state_t recov_state;
3416 nfs4_stateid_types_t sid_types;
3417 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3419 rp = VTOR4(vp);
3420 mi = VTOMI4(vp);
3421 doqueue = 1;
3423 ASSERT(nfs_zone() == mi->mi_zone);
3425 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3427 args.array_len = 2;
3428 args.array = argop;
3430 nfs4_init_stateid_types(&sid_types);
3432 recov_state.rs_flags = 0;
3433 recov_state.rs_num_retry_despite_err = 0;
3435 recov_retry:
3436 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3437 &recov_state, NULL);
3438 if (e.error)
3439 return (e.error);
3441 /* putfh target fh */
3442 argop[0].argop = OP_CPUTFH;
3443 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3445 /* read */
3446 argop[1].argop = OP_READ;
3447 rargs = &argop[1].nfs_argop4_u.opread;
3448 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3449 OP_READ, &sid_types, async);
3451 do {
3452 if (mi->mi_io_kstats) {
3453 mutex_enter(&mi->mi_lock);
3454 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3455 mutex_exit(&mi->mi_lock);
3458 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3459 "nfs4read: %s call, rp %s",
3460 needrecov ? "recov" : "first",
3461 rnode4info(rp)));
3463 if ((vp->v_flag & VNOCACHE) ||
3464 (rp->r_flags & R4DIRECTIO) ||
3465 (mi->mi_flags & MI4_DIRECTIO))
3466 tsize = MIN(mi->mi_tsize, count);
3467 else
3468 tsize = MIN(mi->mi_curread, count);
3470 rargs->offset = (offset4)offset;
3471 rargs->count = (count4)tsize;
3472 rargs->res_data_val_alt = NULL;
3473 rargs->res_mblk = NULL;
3474 rargs->res_uiop = NULL;
3475 rargs->res_maxsize = 0;
3476 rargs->wlist = NULL;
3478 if (uiop)
3479 rargs->res_uiop = uiop;
3480 else
3481 rargs->res_data_val_alt = base;
3482 rargs->res_maxsize = tsize;
3484 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3485 #ifdef DEBUG
3486 if (nfs4read_error_inject) {
3487 res.status = nfs4read_error_inject;
3488 nfs4read_error_inject = 0;
3490 #endif
3492 if (mi->mi_io_kstats) {
3493 mutex_enter(&mi->mi_lock);
3494 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3495 mutex_exit(&mi->mi_lock);
3498 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3499 if (e.error != 0 && !needrecov) {
3500 nfs4_end_fop(mi, vp, NULL, OH_READ,
3501 &recov_state, needrecov);
3502 return (e.error);
3506 * Do proper retry for OLD and BAD stateid errors outside
3507 * of the normal recovery framework. There are two differences
3508 * between async and sync reads. The first is that we allow
3509 * retry on BAD_STATEID for async reads, but not sync reads.
3510 * The second is that we mark the file dead for a failed
3511 * attempt with a special stateid for sync reads, but just
3512 * return EIO for async reads.
3514 * If a sync read receives a BAD stateid error while using a
3515 * delegation stateid, retry using the open stateid (if it
3516 * exists). If it doesn't have an open stateid, reopen the
3517 * file first, then retry.
3519 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3520 res.status == NFS4ERR_BAD_STATEID) && async) {
3521 nfs4_end_fop(mi, vp, NULL, OH_READ,
3522 &recov_state, needrecov);
3523 if (sid_types.cur_sid_type == SPEC_SID) {
3524 (void) xdr_free(xdr_COMPOUND4res_clnt,
3525 (caddr_t)&res);
3526 return (EIO);
3528 nfs4_save_stateid(&rargs->stateid, &sid_types);
3529 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3530 goto recov_retry;
3531 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3532 !async && sid_types.cur_sid_type != SPEC_SID) {
3533 nfs4_save_stateid(&rargs->stateid, &sid_types);
3534 nfs4_end_fop(mi, vp, NULL, OH_READ,
3535 &recov_state, needrecov);
3536 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3537 goto recov_retry;
3538 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3539 sid_types.cur_sid_type == DEL_SID) {
3540 nfs4_save_stateid(&rargs->stateid, &sid_types);
3541 mutex_enter(&rp->r_statev4_lock);
3542 rp->r_deleg_return_pending = TRUE;
3543 mutex_exit(&rp->r_statev4_lock);
3544 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3545 nfs4_end_fop(mi, vp, NULL, OH_READ,
3546 &recov_state, needrecov);
3547 (void) xdr_free(xdr_COMPOUND4res_clnt,
3548 (caddr_t)&res);
3549 return (EIO);
3551 nfs4_end_fop(mi, vp, NULL, OH_READ,
3552 &recov_state, needrecov);
3553 /* hold needed for nfs4delegreturn_thread */
3554 VN_HOLD(vp);
3555 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3556 NFS4_DR_DISCARD), FALSE);
3557 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3558 goto recov_retry;
3560 if (needrecov) {
3561 bool_t abort;
3563 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3564 "nfs4read: initiating recovery\n"));
3565 abort = nfs4_start_recovery(&e,
3566 mi, vp, NULL, &rargs->stateid,
3567 NULL, OP_READ, NULL, NULL, NULL);
3568 nfs4_end_fop(mi, vp, NULL, OH_READ,
3569 &recov_state, needrecov);
3571 * Do not retry if we got OLD_STATEID using a special
3572 * stateid. This avoids looping with a broken server.
3574 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3575 sid_types.cur_sid_type == SPEC_SID)
3576 abort = TRUE;
3578 if (abort == FALSE) {
3580 * Need to retry all possible stateids in
3581 * case the recovery error wasn't stateid
3582 * related or the stateids have become
3583 * stale (server reboot).
3585 nfs4_init_stateid_types(&sid_types);
3586 (void) xdr_free(xdr_COMPOUND4res_clnt,
3587 (caddr_t)&res);
3588 goto recov_retry;
3591 if (!e.error) {
3592 e.error = geterrno4(res.status);
3593 (void) xdr_free(xdr_COMPOUND4res_clnt,
3594 (caddr_t)&res);
3596 return (e.error);
3599 if (res.status) {
3600 e.error = geterrno4(res.status);
3601 nfs4_end_fop(mi, vp, NULL, OH_READ,
3602 &recov_state, needrecov);
3603 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3604 return (e.error);
3607 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3608 count -= data_len;
3609 if (base)
3610 base += data_len;
3611 offset += data_len;
3612 if (mi->mi_io_kstats) {
3613 mutex_enter(&mi->mi_lock);
3614 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3615 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3616 mutex_exit(&mi->mi_lock);
3618 lwp_stat_update(LWP_STAT_INBLK, 1);
3619 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3620 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3622 } while (count && !is_eof);
3624 *residp = count;
3626 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3628 return (e.error);
3631 /* ARGSUSED */
3632 static int
3633 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3634 caller_context_t *ct)
3636 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3637 return (EIO);
3638 switch (cmd) {
3639 case _FIODIRECTIO:
3640 return (nfs4_directio(vp, (int)arg, cr));
3641 default:
3642 return (ENOTTY);
3646 /* ARGSUSED */
3648 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3649 caller_context_t *ct)
3651 int error;
3652 rnode4_t *rp = VTOR4(vp);
3654 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3655 return (EIO);
3657 * If it has been specified that the return value will
3658 * just be used as a hint, and we are only being asked
3659 * for size, fsid or rdevid, then return the client's
3660 * notion of these values without checking to make sure
3661 * that the attribute cache is up to date.
3662 * The whole point is to avoid an over the wire GETATTR
3663 * call.
3665 if (flags & ATTR_HINT) {
3666 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3667 mutex_enter(&rp->r_statelock);
3668 if (vap->va_mask & AT_SIZE)
3669 vap->va_size = rp->r_size;
3670 if (vap->va_mask & AT_FSID)
3671 vap->va_fsid = rp->r_attr.va_fsid;
3672 if (vap->va_mask & AT_RDEV)
3673 vap->va_rdev = rp->r_attr.va_rdev;
3674 mutex_exit(&rp->r_statelock);
3675 return (0);
3680 * Only need to flush pages if asking for the mtime
3681 * and if there any dirty pages or any outstanding
3682 * asynchronous (write) requests for this file.
3684 if (vap->va_mask & AT_MTIME) {
3685 rp = VTOR4(vp);
3686 if (nfs4_has_pages(vp)) {
3687 mutex_enter(&rp->r_statev4_lock);
3688 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3689 mutex_exit(&rp->r_statev4_lock);
3690 if (rp->r_flags & R4DIRTY ||
3691 rp->r_awcount > 0) {
3692 mutex_enter(&rp->r_statelock);
3693 rp->r_gcount++;
3694 mutex_exit(&rp->r_statelock);
3695 error =
3696 nfs4_putpage(vp, 0,
3697 0, 0, cr, NULL);
3698 mutex_enter(&rp->r_statelock);
3699 if (error && (error == ENOSPC ||
3700 error == EDQUOT)) {
3701 if (!rp->r_error)
3702 rp->r_error = error;
3704 if (--rp->r_gcount == 0)
3705 cv_broadcast(&rp->r_cv);
3706 mutex_exit(&rp->r_statelock);
3708 } else {
3709 mutex_exit(&rp->r_statev4_lock);
3713 return (nfs4getattr(vp, vap, cr));
3717 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3720 * If these are the only two bits cleared
3721 * on the server then return 0 (OK) else
3722 * return 1 (BAD).
3724 on_client &= ~(S_ISUID|S_ISGID);
3725 if (on_client == from_server)
3726 return (0);
3727 else
3728 return (1);
3731 /*ARGSUSED4*/
3732 static int
3733 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3734 caller_context_t *ct)
3736 int error;
3738 if (vap->va_mask & AT_NOSET)
3739 return (EINVAL);
3741 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3742 return (EIO);
3745 * Don't call secpolicy_vnode_setattr, the client cannot
3746 * use its cached attributes to make security decisions
3747 * as the server may be faking mode bits or mapping uid/gid.
3748 * Always just let the server to the checking.
3749 * If we provide the ability to remove basic priviledges
3750 * to setattr (e.g. basic without chmod) then we will
3751 * need to add a check here before calling the server.
3753 error = nfs4setattr(vp, vap, flags, cr, NULL);
3755 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3756 vnevent_truncate(vp, ct);
3758 return (error);
3762 * To replace the "guarded" version 3 setattr, we use two types of compound
3763 * setattr requests:
3764 * 1. The "normal" setattr, used when the size of the file isn't being
3765 * changed - { Putfh <fh>; Setattr; Getattr }/
3766 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3767 * with only ctime as the argument. If the server ctime differs from
3768 * what is cached on the client, the verify will fail, but we would
3769 * already have the ctime from the preceding getattr, so just set it
3770 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3771 * Setattr; Getattr }.
3773 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3774 * this setattr and NULL if they are not.
3776 static int
3777 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3778 vsecattr_t *vsap)
3780 COMPOUND4args_clnt args;
3781 COMPOUND4res_clnt res, *resp = NULL;
3782 nfs4_ga_res_t *garp = NULL;
3783 int numops = 3; /* { Putfh; Setattr; Getattr } */
3784 nfs_argop4 argop[5];
3785 int verify_argop = -1;
3786 int setattr_argop = 1;
3787 nfs_resop4 *resop;
3788 vattr_t va;
3789 rnode4_t *rp;
3790 int doqueue = 1;
3791 uint_t mask = vap->va_mask;
3792 mode_t omode;
3793 vsecattr_t *vsp;
3794 timestruc_t ctime;
3795 bool_t needrecov = FALSE;
3796 nfs4_recov_state_t recov_state;
3797 nfs4_stateid_types_t sid_types;
3798 stateid4 stateid;
3799 hrtime_t t;
3800 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3801 servinfo4_t *svp;
3802 bitmap4 supp_attrs;
3804 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3805 rp = VTOR4(vp);
3806 nfs4_init_stateid_types(&sid_types);
3809 * Only need to flush pages if there are any pages and
3810 * if the file is marked as dirty in some fashion. The
3811 * file must be flushed so that we can accurately
3812 * determine the size of the file and the cached data
3813 * after the SETATTR returns. A file is considered to
3814 * be dirty if it is either marked with R4DIRTY, has
3815 * outstanding i/o's active, or is mmap'd. In this
3816 * last case, we can't tell whether there are dirty
3817 * pages, so we flush just to be sure.
3819 if (nfs4_has_pages(vp) &&
3820 ((rp->r_flags & R4DIRTY) ||
3821 rp->r_count > 0 ||
3822 rp->r_mapcnt > 0)) {
3823 ASSERT(vp->v_type != VCHR);
3824 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3825 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3826 mutex_enter(&rp->r_statelock);
3827 if (!rp->r_error)
3828 rp->r_error = e.error;
3829 mutex_exit(&rp->r_statelock);
3833 if (mask & AT_SIZE) {
3835 * Verification setattr compound for non-deleg AT_SIZE:
3836 * { Putfh; Getattr; Verify; Setattr; Getattr }
3837 * Set ctime local here (outside the do_again label)
3838 * so that subsequent retries (after failed VERIFY)
3839 * will use ctime from GETATTR results (from failed
3840 * verify compound) as VERIFY arg.
3841 * If file has delegation, then VERIFY(time_metadata)
3842 * is of little added value, so don't bother.
3844 mutex_enter(&rp->r_statev4_lock);
3845 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3846 rp->r_deleg_return_pending) {
3847 numops = 5;
3848 ctime = rp->r_attr.va_ctime;
3850 mutex_exit(&rp->r_statev4_lock);
3853 recov_state.rs_flags = 0;
3854 recov_state.rs_num_retry_despite_err = 0;
3856 args.ctag = TAG_SETATTR;
3857 do_again:
3858 recov_retry:
3859 setattr_argop = numops - 2;
3861 args.array = argop;
3862 args.array_len = numops;
3864 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3865 if (e.error)
3866 return (e.error);
3869 /* putfh target fh */
3870 argop[0].argop = OP_CPUTFH;
3871 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3873 if (numops == 5) {
3875 * We only care about the ctime, but need to get mtime
3876 * and size for proper cache update.
3878 /* getattr */
3879 argop[1].argop = OP_GETATTR;
3880 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3881 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3883 /* verify - set later in loop */
3884 verify_argop = 2;
3887 /* setattr */
3888 svp = rp->r_server;
3889 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3890 supp_attrs = svp->sv_supp_attrs;
3891 nfs_rw_exit(&svp->sv_lock);
3893 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3894 supp_attrs, &e.error, &sid_types);
3895 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3896 if (e.error) {
3897 /* req time field(s) overflow - return immediately */
3898 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3899 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3900 opsetattr.obj_attributes);
3901 return (e.error);
3903 omode = rp->r_attr.va_mode;
3905 /* getattr */
3906 argop[numops-1].argop = OP_GETATTR;
3907 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3909 * If we are setting the ACL (indicated only by vsap != NULL), request
3910 * the ACL in this getattr. The ACL returned from this getattr will be
3911 * used in updating the ACL cache.
3913 if (vsap != NULL)
3914 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3915 FATTR4_ACL_MASK;
3916 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3919 * setattr iterates if the object size is set and the cached ctime
3920 * does not match the file ctime. In that case, verify the ctime first.
3923 do {
3924 if (verify_argop != -1) {
3926 * Verify that the ctime match before doing setattr.
3928 va.va_mask = AT_CTIME;
3929 va.va_ctime = ctime;
3930 svp = rp->r_server;
3931 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3932 supp_attrs = svp->sv_supp_attrs;
3933 nfs_rw_exit(&svp->sv_lock);
3934 e.error = nfs4args_verify(&argop[verify_argop], &va,
3935 OP_VERIFY, supp_attrs);
3936 if (e.error) {
3937 /* req time field(s) overflow - return */
3938 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3939 needrecov);
3940 break;
3944 doqueue = 1;
3946 t = gethrtime();
3948 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3951 * Purge the access cache and ACL cache if changing either the
3952 * owner of the file, the group owner, or the mode. These may
3953 * change the access permissions of the file, so purge old
3954 * information and start over again.
3956 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3957 (void) nfs4_access_purge_rp(rp);
3958 if (rp->r_secattr != NULL) {
3959 mutex_enter(&rp->r_statelock);
3960 vsp = rp->r_secattr;
3961 rp->r_secattr = NULL;
3962 mutex_exit(&rp->r_statelock);
3963 if (vsp != NULL)
3964 nfs4_acl_free_cache(vsp);
3969 * If res.array_len == numops, then everything succeeded,
3970 * except for possibly the final getattr. If only the
3971 * last getattr failed, give up, and don't try recovery.
3973 if (res.array_len == numops) {
3974 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3975 needrecov);
3976 if (! e.error)
3977 resp = &res;
3978 break;
3982 * if either rpc call failed or completely succeeded - done
3984 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3985 if (e.error) {
3986 PURGE_ATTRCACHE4(vp);
3987 if (!needrecov) {
3988 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3989 needrecov);
3990 break;
3995 * Do proper retry for OLD_STATEID outside of the normal
3996 * recovery framework.
3998 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3999 sid_types.cur_sid_type != SPEC_SID &&
4000 sid_types.cur_sid_type != NO_SID) {
4001 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4002 needrecov);
4003 nfs4_save_stateid(&stateid, &sid_types);
4004 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4005 opsetattr.obj_attributes);
4006 if (verify_argop != -1) {
4007 nfs4args_verify_free(&argop[verify_argop]);
4008 verify_argop = -1;
4010 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4011 goto recov_retry;
4014 if (needrecov) {
4015 bool_t abort;
4017 abort = nfs4_start_recovery(&e,
4018 VTOMI4(vp), vp, NULL, NULL, NULL,
4019 OP_SETATTR, NULL, NULL, NULL);
4020 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4021 needrecov);
4023 * Do not retry if we failed with OLD_STATEID using
4024 * a special stateid. This is done to avoid looping
4025 * with a broken server.
4027 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4028 (sid_types.cur_sid_type == SPEC_SID ||
4029 sid_types.cur_sid_type == NO_SID))
4030 abort = TRUE;
4031 if (!e.error) {
4032 if (res.status == NFS4ERR_BADOWNER)
4033 nfs4_log_badowner(VTOMI4(vp),
4034 OP_SETATTR);
4036 e.error = geterrno4(res.status);
4037 (void) xdr_free(xdr_COMPOUND4res_clnt,
4038 (caddr_t)&res);
4040 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4041 opsetattr.obj_attributes);
4042 if (verify_argop != -1) {
4043 nfs4args_verify_free(&argop[verify_argop]);
4044 verify_argop = -1;
4046 if (abort == FALSE) {
4048 * Need to retry all possible stateids in
4049 * case the recovery error wasn't stateid
4050 * related or the stateids have become
4051 * stale (server reboot).
4053 nfs4_init_stateid_types(&sid_types);
4054 goto recov_retry;
4056 return (e.error);
4060 * Need to call nfs4_end_op before nfs4getattr to
4061 * avoid potential nfs4_start_op deadlock. See RFE
4062 * 4777612. Calls to nfs4_invalidate_pages() and
4063 * nfs4_purge_stale_fh() might also generate over the
4064 * wire calls which my cause nfs4_start_op() deadlock.
4066 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4069 * Check to update lease.
4071 resp = &res;
4072 if (res.status == NFS4_OK) {
4073 break;
4077 * Check if verify failed to see if try again
4079 if ((verify_argop == -1) || (res.array_len != 3)) {
4081 * can't continue...
4083 if (res.status == NFS4ERR_BADOWNER)
4084 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4086 e.error = geterrno4(res.status);
4087 } else {
4089 * When the verify request fails, the client ctime is
4090 * not in sync with the server. This is the same as
4091 * the version 3 "not synchronized" error, and we
4092 * handle it in a similar manner (XXX do we need to???).
4093 * Use the ctime returned in the first getattr for
4094 * the input to the next verify.
4095 * If we couldn't get the attributes, then we give up
4096 * because we can't complete the operation as required.
4098 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4100 if (e.error) {
4101 PURGE_ATTRCACHE4(vp);
4102 nfs4_purge_stale_fh(e.error, vp, cr);
4103 } else {
4105 * retry with a new verify value
4107 ctime = garp->n4g_va.va_ctime;
4108 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4109 resp = NULL;
4111 if (!e.error) {
4112 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4113 opsetattr.obj_attributes);
4114 if (verify_argop != -1) {
4115 nfs4args_verify_free(&argop[verify_argop]);
4116 verify_argop = -1;
4118 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4119 goto do_again;
4121 } while (!e.error);
4123 if (e.error) {
4125 * If we are here, rfs4call has an irrecoverable error - return
4127 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4128 opsetattr.obj_attributes);
4129 if (verify_argop != -1) {
4130 nfs4args_verify_free(&argop[verify_argop]);
4131 verify_argop = -1;
4133 if (resp)
4134 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4135 return (e.error);
4141 * If changing the size of the file, invalidate
4142 * any local cached data which is no longer part
4143 * of the file. We also possibly invalidate the
4144 * last page in the file. We could use
4145 * pvn_vpzero(), but this would mark the page as
4146 * modified and require it to be written back to
4147 * the server for no particularly good reason.
4148 * This way, if we access it, then we bring it
4149 * back in. A read should be cheaper than a
4150 * write.
4152 if (mask & AT_SIZE) {
4153 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4156 /* either no error or one of the postop getattr failed */
4159 * XXX Perform a simplified version of wcc checking. Instead of
4160 * have another getattr to get pre-op, just purge cache if
4161 * any of the ops prior to and including the getattr failed.
4162 * If the getattr succeeded then update the attrcache accordingly.
4165 garp = NULL;
4166 if (res.status == NFS4_OK) {
4168 * Last getattr
4170 resop = &res.array[numops - 1];
4171 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4174 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4175 * rather than filling it. See the function itself for details.
4177 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4178 if (garp != NULL) {
4179 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4180 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4181 vs_ace4_destroy(&garp->n4g_vsa);
4182 } else {
4183 if (vsap != NULL) {
4185 * The ACL was supposed to be set and to be
4186 * returned in the last getattr of this
4187 * compound, but for some reason the getattr
4188 * result doesn't contain the ACL. In this
4189 * case, purge the ACL cache.
4191 if (rp->r_secattr != NULL) {
4192 mutex_enter(&rp->r_statelock);
4193 vsp = rp->r_secattr;
4194 rp->r_secattr = NULL;
4195 mutex_exit(&rp->r_statelock);
4196 if (vsp != NULL)
4197 nfs4_acl_free_cache(vsp);
4203 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4205 * Set the size, rather than relying on getting it updated
4206 * via a GETATTR. With delegations the client tries to
4207 * suppress GETATTR calls.
4209 mutex_enter(&rp->r_statelock);
4210 rp->r_size = vap->va_size;
4211 mutex_exit(&rp->r_statelock);
4215 * Can free up request args and res
4217 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4218 opsetattr.obj_attributes);
4219 if (verify_argop != -1) {
4220 nfs4args_verify_free(&argop[verify_argop]);
4221 verify_argop = -1;
4223 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4226 * Some servers will change the mode to clear the setuid
4227 * and setgid bits when changing the uid or gid. The
4228 * client needs to compensate appropriately.
4230 if (mask & (AT_UID | AT_GID)) {
4231 int terror, do_setattr;
4233 do_setattr = 0;
4234 va.va_mask = AT_MODE;
4235 terror = nfs4getattr(vp, &va, cr);
4236 if (!terror &&
4237 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4238 (!(mask & AT_MODE) && va.va_mode != omode))) {
4239 va.va_mask = AT_MODE;
4240 if (mask & AT_MODE) {
4242 * We asked the mode to be changed and what
4243 * we just got from the server in getattr is
4244 * not what we wanted it to be, so set it now.
4246 va.va_mode = vap->va_mode;
4247 do_setattr = 1;
4248 } else {
4250 * We did not ask the mode to be changed,
4251 * Check to see that the server just cleared
4252 * I_SUID and I_GUID from it. If not then
4253 * set mode to omode with UID/GID cleared.
4255 if (nfs4_compare_modes(va.va_mode, omode)) {
4256 omode &= ~(S_ISUID|S_ISGID);
4257 va.va_mode = omode;
4258 do_setattr = 1;
4262 if (do_setattr)
4263 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4267 return (e.error);
4270 /* ARGSUSED */
4271 static int
4272 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4274 COMPOUND4args_clnt args;
4275 COMPOUND4res_clnt res;
4276 int doqueue;
4277 uint32_t acc, resacc, argacc;
4278 rnode4_t *rp;
4279 cred_t *cred, *ncr, *ncrfree = NULL;
4280 nfs4_access_type_t cacc;
4281 int num_ops;
4282 nfs_argop4 argop[3];
4283 nfs_resop4 *resop;
4284 bool_t needrecov = FALSE, do_getattr;
4285 nfs4_recov_state_t recov_state;
4286 int rpc_error;
4287 hrtime_t t;
4288 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4289 mntinfo4_t *mi = VTOMI4(vp);
4291 if (nfs_zone() != mi->mi_zone)
4292 return (EIO);
4294 acc = 0;
4295 if (mode & VREAD)
4296 acc |= ACCESS4_READ;
4297 if (mode & VWRITE) {
4298 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4299 return (EROFS);
4300 if (vp->v_type == VDIR)
4301 acc |= ACCESS4_DELETE;
4302 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4304 if (mode & VEXEC) {
4305 if (vp->v_type == VDIR)
4306 acc |= ACCESS4_LOOKUP;
4307 else
4308 acc |= ACCESS4_EXECUTE;
4311 if (VTOR4(vp)->r_acache != NULL) {
4312 e.error = nfs4_validate_caches(vp, cr);
4313 if (e.error)
4314 return (e.error);
4317 rp = VTOR4(vp);
4318 if (vp->v_type == VDIR)
4319 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4320 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4321 else
4322 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4323 ACCESS4_EXECUTE;
4324 recov_state.rs_flags = 0;
4325 recov_state.rs_num_retry_despite_err = 0;
4327 cred = cr;
4329 * ncr and ncrfree both initially
4330 * point to the memory area returned
4331 * by crnetadjust();
4332 * ncrfree not NULL when exiting means
4333 * that we need to release it
4335 ncr = crnetadjust(cred);
4336 ncrfree = ncr;
4338 tryagain:
4339 cacc = nfs4_access_check(rp, acc, cred);
4340 if (cacc == NFS4_ACCESS_ALLOWED) {
4341 if (ncrfree != NULL)
4342 crfree(ncrfree);
4343 return (0);
4345 if (cacc == NFS4_ACCESS_DENIED) {
4347 * If the cred can be adjusted, try again
4348 * with the new cred.
4350 if (ncr != NULL) {
4351 cred = ncr;
4352 ncr = NULL;
4353 goto tryagain;
4355 if (ncrfree != NULL)
4356 crfree(ncrfree);
4357 return (EACCES);
4360 recov_retry:
4362 * Don't take with r_statev4_lock here. r_deleg_type could
4363 * change as soon as lock is released. Since it is an int,
4364 * there is no atomicity issue.
4366 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4367 num_ops = do_getattr ? 3 : 2;
4369 args.ctag = TAG_ACCESS;
4371 args.array_len = num_ops;
4372 args.array = argop;
4374 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4375 &recov_state, NULL)) {
4376 if (ncrfree != NULL)
4377 crfree(ncrfree);
4378 return (e.error);
4381 /* putfh target fh */
4382 argop[0].argop = OP_CPUTFH;
4383 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4385 /* access */
4386 argop[1].argop = OP_ACCESS;
4387 argop[1].nfs_argop4_u.opaccess.access = argacc;
4389 /* getattr */
4390 if (do_getattr) {
4391 argop[2].argop = OP_GETATTR;
4392 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4393 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4396 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4397 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4398 rnode4info(VTOR4(vp))));
4400 doqueue = 1;
4401 t = gethrtime();
4402 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4403 rpc_error = e.error;
4405 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4406 if (needrecov) {
4407 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4408 "nfs4_access: initiating recovery\n"));
4410 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4411 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4412 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4413 &recov_state, needrecov);
4414 if (!e.error)
4415 (void) xdr_free(xdr_COMPOUND4res_clnt,
4416 (caddr_t)&res);
4417 goto recov_retry;
4420 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4422 if (e.error)
4423 goto out;
4425 if (res.status) {
4426 e.error = geterrno4(res.status);
4428 * This might generate over the wire calls throught
4429 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4430 * here to avoid a deadlock.
4432 nfs4_purge_stale_fh(e.error, vp, cr);
4433 goto out;
4435 resop = &res.array[1]; /* access res */
4437 resacc = resop->nfs_resop4_u.opaccess.access;
4439 if (do_getattr) {
4440 resop++; /* getattr res */
4441 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4442 t, cr, FALSE, NULL);
4445 if (!e.error) {
4446 nfs4_access_cache(rp, argacc, resacc, cred);
4448 * we just cached results with cred; if cred is the
4449 * adjusted credentials from crnetadjust, we do not want
4450 * to release them before exiting: hence setting ncrfree
4451 * to NULL
4453 if (cred != cr)
4454 ncrfree = NULL;
4455 /* XXX check the supported bits too? */
4456 if ((acc & resacc) != acc) {
4458 * The following code implements the semantic
4459 * that a setuid root program has *at least* the
4460 * permissions of the user that is running the
4461 * program. See rfs3call() for more portions
4462 * of the implementation of this functionality.
4464 /* XXX-LP */
4465 if (ncr != NULL) {
4466 (void) xdr_free(xdr_COMPOUND4res_clnt,
4467 (caddr_t)&res);
4468 cred = ncr;
4469 ncr = NULL;
4470 goto tryagain;
4472 e.error = EACCES;
4476 out:
4477 if (!rpc_error)
4478 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4480 if (ncrfree != NULL)
4481 crfree(ncrfree);
4483 return (e.error);
4486 /* ARGSUSED */
4487 static int
4488 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4490 COMPOUND4args_clnt args;
4491 COMPOUND4res_clnt res;
4492 int doqueue;
4493 rnode4_t *rp;
4494 nfs_argop4 argop[3];
4495 nfs_resop4 *resop;
4496 READLINK4res *lr_res;
4497 nfs4_ga_res_t *garp;
4498 uint_t len;
4499 char *linkdata;
4500 bool_t needrecov = FALSE;
4501 nfs4_recov_state_t recov_state;
4502 hrtime_t t;
4503 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4505 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4506 return (EIO);
4508 * Can't readlink anything other than a symbolic link.
4510 if (vp->v_type != VLNK)
4511 return (EINVAL);
4513 rp = VTOR4(vp);
4514 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4515 e.error = nfs4_validate_caches(vp, cr);
4516 if (e.error)
4517 return (e.error);
4518 mutex_enter(&rp->r_statelock);
4519 if (rp->r_symlink.contents != NULL) {
4520 e.error = uiomove(rp->r_symlink.contents,
4521 rp->r_symlink.len, UIO_READ, uiop);
4522 mutex_exit(&rp->r_statelock);
4523 return (e.error);
4525 mutex_exit(&rp->r_statelock);
4527 recov_state.rs_flags = 0;
4528 recov_state.rs_num_retry_despite_err = 0;
4530 recov_retry:
4531 args.array_len = 3;
4532 args.array = argop;
4533 args.ctag = TAG_READLINK;
4535 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4536 if (e.error) {
4537 return (e.error);
4540 /* 0. putfh symlink fh */
4541 argop[0].argop = OP_CPUTFH;
4542 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4544 /* 1. readlink */
4545 argop[1].argop = OP_READLINK;
4547 /* 2. getattr */
4548 argop[2].argop = OP_GETATTR;
4549 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4550 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4552 doqueue = 1;
4554 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4555 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4556 rnode4info(VTOR4(vp))));
4558 t = gethrtime();
4560 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4562 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4563 if (needrecov) {
4564 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4565 "nfs4_readlink: initiating recovery\n"));
4567 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4568 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4569 if (!e.error)
4570 (void) xdr_free(xdr_COMPOUND4res_clnt,
4571 (caddr_t)&res);
4573 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4574 needrecov);
4575 goto recov_retry;
4579 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4581 if (e.error)
4582 return (e.error);
4585 * There is an path in the code below which calls
4586 * nfs4_purge_stale_fh(), which may generate otw calls through
4587 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4588 * here to avoid nfs4_start_op() deadlock.
4591 if (res.status && (res.array_len < args.array_len)) {
4593 * either Putfh or Link failed
4595 e.error = geterrno4(res.status);
4596 nfs4_purge_stale_fh(e.error, vp, cr);
4597 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4598 return (e.error);
4601 resop = &res.array[1]; /* readlink res */
4602 lr_res = &resop->nfs_resop4_u.opreadlink;
4605 * treat symlink names as data
4607 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4608 if (linkdata != NULL) {
4609 int uio_len = len - 1;
4610 /* len includes null byte, which we won't uiomove */
4611 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4612 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4613 mutex_enter(&rp->r_statelock);
4614 if (rp->r_symlink.contents == NULL) {
4615 rp->r_symlink.contents = linkdata;
4616 rp->r_symlink.len = uio_len;
4617 rp->r_symlink.size = len;
4618 mutex_exit(&rp->r_statelock);
4619 } else {
4620 mutex_exit(&rp->r_statelock);
4621 kmem_free(linkdata, len);
4623 } else {
4624 kmem_free(linkdata, len);
4627 if (res.status == NFS4_OK) {
4628 resop++; /* getattr res */
4629 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4631 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4633 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4636 * The over the wire error for attempting to readlink something
4637 * other than a symbolic link is ENXIO. However, we need to
4638 * return EINVAL instead of ENXIO, so we map it here.
4640 return (e.error == ENXIO ? EINVAL : e.error);
4644 * Flush local dirty pages to stable storage on the server.
4646 * If FNODSYNC is specified, then there is nothing to do because
4647 * metadata changes are not cached on the client before being
4648 * sent to the server.
4650 /* ARGSUSED */
4651 static int
4652 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4654 int error;
4656 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4657 return (0);
4658 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4659 return (EIO);
4660 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4661 if (!error)
4662 error = VTOR4(vp)->r_error;
4663 return (error);
4667 * Weirdness: if the file was removed or the target of a rename
4668 * operation while it was open, it got renamed instead. Here we
4669 * remove the renamed file.
4671 /* ARGSUSED */
4672 void
4673 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4675 rnode4_t *rp;
4677 ASSERT(vp != DNLC_NO_VNODE);
4679 rp = VTOR4(vp);
4681 if (IS_SHADOW(vp, rp)) {
4682 sv_inactive(vp);
4683 return;
4687 * If this is coming from the wrong zone, we let someone in the right
4688 * zone take care of it asynchronously. We can get here due to
4689 * VN_RELE() being called from pageout() or fsflush(). This call may
4690 * potentially turn into an expensive no-op if, for instance, v_count
4691 * gets incremented in the meantime, but it's still correct.
4693 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4694 nfs4_async_inactive(vp, cr);
4695 return;
4699 * Some of the cleanup steps might require over-the-wire
4700 * operations. Since fop_inactive can get called as a result of
4701 * other over-the-wire operations (e.g., an attribute cache update
4702 * can lead to a DNLC purge), doing those steps now would lead to a
4703 * nested call to the recovery framework, which can deadlock. So
4704 * do any over-the-wire cleanups asynchronously, in a separate
4705 * thread.
4708 mutex_enter(&rp->r_os_lock);
4709 mutex_enter(&rp->r_statelock);
4710 mutex_enter(&rp->r_statev4_lock);
4712 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4713 mutex_exit(&rp->r_statev4_lock);
4714 mutex_exit(&rp->r_statelock);
4715 mutex_exit(&rp->r_os_lock);
4716 nfs4_async_inactive(vp, cr);
4717 return;
4720 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4721 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4722 mutex_exit(&rp->r_statev4_lock);
4723 mutex_exit(&rp->r_statelock);
4724 mutex_exit(&rp->r_os_lock);
4725 nfs4_async_inactive(vp, cr);
4726 return;
4729 if (rp->r_unldvp != NULL) {
4730 mutex_exit(&rp->r_statev4_lock);
4731 mutex_exit(&rp->r_statelock);
4732 mutex_exit(&rp->r_os_lock);
4733 nfs4_async_inactive(vp, cr);
4734 return;
4736 mutex_exit(&rp->r_statev4_lock);
4737 mutex_exit(&rp->r_statelock);
4738 mutex_exit(&rp->r_os_lock);
4740 rp4_addfree(rp, cr);
4744 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4745 * various bits of state. The caller must not refer to vp after this call.
4748 void
4749 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4751 rnode4_t *rp = VTOR4(vp);
4752 nfs4_recov_state_t recov_state;
4753 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4754 vnode_t *unldvp;
4755 char *unlname;
4756 cred_t *unlcred;
4757 COMPOUND4args_clnt args;
4758 COMPOUND4res_clnt res, *resp;
4759 nfs_argop4 argop[2];
4760 int doqueue;
4761 #ifdef DEBUG
4762 char *name;
4763 #endif
4765 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4766 ASSERT(!IS_SHADOW(vp, rp));
4768 #ifdef DEBUG
4769 name = fn_name(VTOSV(vp)->sv_name);
4770 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4771 "release vnode %s", name));
4772 kmem_free(name, MAXNAMELEN);
4773 #endif
4775 if (vp->v_type == VREG) {
4776 bool_t recov_failed = FALSE;
4778 e.error = nfs4close_all(vp, cr);
4779 if (e.error) {
4780 /* Check to see if recovery failed */
4781 mutex_enter(&(VTOMI4(vp)->mi_lock));
4782 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4783 recov_failed = TRUE;
4784 mutex_exit(&(VTOMI4(vp)->mi_lock));
4785 if (!recov_failed) {
4786 mutex_enter(&rp->r_statelock);
4787 if (rp->r_flags & R4RECOVERR)
4788 recov_failed = TRUE;
4789 mutex_exit(&rp->r_statelock);
4791 if (recov_failed) {
4792 NFS4_DEBUG(nfs4_client_recov_debug,
4793 (CE_NOTE, "nfs4_inactive_otw: "
4794 "close failed (recovery failure)"));
4799 redo:
4800 if (rp->r_unldvp == NULL) {
4801 rp4_addfree(rp, cr);
4802 return;
4806 * Save the vnode pointer for the directory where the
4807 * unlinked-open file got renamed, then set it to NULL
4808 * to prevent another thread from getting here before
4809 * we're done with the remove. While we have the
4810 * statelock, make local copies of the pertinent rnode
4811 * fields. If we weren't to do this in an atomic way, the
4812 * the unl* fields could become inconsistent with respect
4813 * to each other due to a race condition between this
4814 * code and nfs_remove(). See bug report 1034328.
4816 mutex_enter(&rp->r_statelock);
4817 if (rp->r_unldvp == NULL) {
4818 mutex_exit(&rp->r_statelock);
4819 rp4_addfree(rp, cr);
4820 return;
4823 unldvp = rp->r_unldvp;
4824 rp->r_unldvp = NULL;
4825 unlname = rp->r_unlname;
4826 rp->r_unlname = NULL;
4827 unlcred = rp->r_unlcred;
4828 rp->r_unlcred = NULL;
4829 mutex_exit(&rp->r_statelock);
4832 * If there are any dirty pages left, then flush
4833 * them. This is unfortunate because they just
4834 * may get thrown away during the remove operation,
4835 * but we have to do this for correctness.
4837 if (nfs4_has_pages(vp) &&
4838 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4839 ASSERT(vp->v_type != VCHR);
4840 e.error = nfs4_putpage(vp, 0, 0, 0, cr, NULL);
4841 if (e.error) {
4842 mutex_enter(&rp->r_statelock);
4843 if (!rp->r_error)
4844 rp->r_error = e.error;
4845 mutex_exit(&rp->r_statelock);
4849 recov_state.rs_flags = 0;
4850 recov_state.rs_num_retry_despite_err = 0;
4851 recov_retry_remove:
4853 * Do the remove operation on the renamed file
4855 args.ctag = TAG_INACTIVE;
4858 * Remove ops: putfh dir; remove
4860 args.array_len = 2;
4861 args.array = argop;
4863 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4864 if (e.error) {
4865 kmem_free(unlname, MAXNAMELEN);
4866 crfree(unlcred);
4867 VN_RELE(unldvp);
4869 * Try again; this time around r_unldvp will be NULL, so we'll
4870 * just call rp4_addfree() and return.
4872 goto redo;
4875 /* putfh directory */
4876 argop[0].argop = OP_CPUTFH;
4877 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4879 /* remove */
4880 argop[1].argop = OP_CREMOVE;
4881 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4883 doqueue = 1;
4884 resp = &res;
4886 #if 0 /* notyet */
4888 * Can't do this yet. We may be being called from
4889 * dnlc_purge_XXX while that routine is holding a
4890 * mutex lock to the nc_rele list. The calls to
4891 * nfs3_cache_wcc_data may result in calls to
4892 * dnlc_purge_XXX. This will result in a deadlock.
4894 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4895 if (e.error) {
4896 PURGE_ATTRCACHE4(unldvp);
4897 resp = NULL;
4898 } else if (res.status) {
4899 e.error = geterrno4(res.status);
4900 PURGE_ATTRCACHE4(unldvp);
4902 * This code is inactive right now
4903 * but if made active there should
4904 * be a nfs4_end_op() call before
4905 * nfs4_purge_stale_fh to avoid start_op()
4906 * deadlock. See BugId: 4948726
4908 nfs4_purge_stale_fh(error, unldvp, cr);
4909 } else {
4910 nfs_resop4 *resop;
4911 REMOVE4res *rm_res;
4913 resop = &res.array[1];
4914 rm_res = &resop->nfs_resop4_u.opremove;
4916 * Update directory cache attribute,
4917 * readdir and dnlc caches.
4919 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4921 #else
4922 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4924 PURGE_ATTRCACHE4(unldvp);
4925 #endif
4927 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4928 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4929 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4930 if (!e.error)
4931 (void) xdr_free(xdr_COMPOUND4res_clnt,
4932 (caddr_t)&res);
4933 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4934 &recov_state, TRUE);
4935 goto recov_retry_remove;
4938 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4941 * Release stuff held for the remove
4943 VN_RELE(unldvp);
4944 if (!e.error && resp)
4945 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4947 kmem_free(unlname, MAXNAMELEN);
4948 crfree(unlcred);
4949 goto redo;
4953 * Remote file system operations having to do with directory manipulation.
4955 /* ARGSUSED3 */
4957 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4958 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4959 int *direntflags, pathname_t *realpnp)
4961 int error;
4962 vnode_t *vp, *avp = NULL;
4963 rnode4_t *drp;
4965 *vpp = NULL;
4966 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4967 return (EPERM);
4969 * if LOOKUP_XATTR, must replace dvp (object) with
4970 * object's attrdir before continuing with lookup
4972 if (flags & LOOKUP_XATTR) {
4973 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4974 if (error)
4975 return (error);
4977 dvp = avp;
4980 * If lookup is for "", just return dvp now. The attrdir
4981 * has already been activated (from nfs4lookup_xattr), and
4982 * the caller will RELE the original dvp -- not
4983 * the attrdir. So, set vpp and return.
4984 * Currently, when the LOOKUP_XATTR flag is
4985 * passed to fop_lookup, the name is always empty, and
4986 * shortcircuiting here avoids 3 unneeded lock/unlock
4987 * pairs.
4989 * If a non-empty name was provided, then it is the
4990 * attribute name, and it will be looked up below.
4992 if (*nm == '\0') {
4993 *vpp = dvp;
4994 return (0);
4998 * The vfs layer never sends a name when asking for the
4999 * attrdir, so we should never get here (unless of course
5000 * name is passed at some time in future -- at which time
5001 * we'll blow up here).
5003 ASSERT(0);
5006 drp = VTOR4(dvp);
5007 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5008 return (EINTR);
5010 error = nfs4lookup(dvp, nm, vpp, cr, 0);
5011 nfs_rw_exit(&drp->r_rwlock);
5014 * If vnode is a device, create special vnode.
5016 if (!error && ISVDEV((*vpp)->v_type)) {
5017 vp = *vpp;
5018 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5019 VN_RELE(vp);
5022 return (error);
5025 /* ARGSUSED */
5026 static int
5027 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5029 int error;
5030 rnode4_t *drp;
5031 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5032 mntinfo4_t *mi;
5034 mi = VTOMI4(dvp);
5035 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5036 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5037 return (EINVAL);
5039 drp = VTOR4(dvp);
5040 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5041 return (EINTR);
5043 mutex_enter(&drp->r_statelock);
5045 * If the server doesn't support xattrs just return EINVAL
5047 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5048 mutex_exit(&drp->r_statelock);
5049 nfs_rw_exit(&drp->r_rwlock);
5050 return (EINVAL);
5054 * If there is a cached xattr directory entry,
5055 * use it as long as the attributes are valid. If the
5056 * attributes are not valid, take the simple approach and
5057 * free the cached value and re-fetch a new value.
5059 * We don't negative entry cache for now, if we did we
5060 * would need to check if the file has changed on every
5061 * lookup. But xattrs don't exist very often and failing
5062 * an openattr is not much more expensive than and NVERIFY or GETATTR
5063 * so do an openattr over the wire for now.
5065 if (drp->r_xattr_dir != NULL) {
5066 if (ATTRCACHE4_VALID(dvp)) {
5067 VN_HOLD(drp->r_xattr_dir);
5068 *vpp = drp->r_xattr_dir;
5069 mutex_exit(&drp->r_statelock);
5070 nfs_rw_exit(&drp->r_rwlock);
5071 return (0);
5073 VN_RELE(drp->r_xattr_dir);
5074 drp->r_xattr_dir = NULL;
5076 mutex_exit(&drp->r_statelock);
5078 error = nfs4openattr(dvp, vpp, cflag, cr);
5080 nfs_rw_exit(&drp->r_rwlock);
5082 return (error);
5085 static int
5086 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5088 int error;
5089 rnode4_t *drp;
5091 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5094 * If lookup is for "", just return dvp. Don't need
5095 * to send it over the wire, look it up in the dnlc,
5096 * or perform any access checks.
5098 if (*nm == '\0') {
5099 VN_HOLD(dvp);
5100 *vpp = dvp;
5101 return (0);
5105 * Can't do lookups in non-directories.
5107 if (dvp->v_type != VDIR)
5108 return (ENOTDIR);
5111 * If lookup is for ".", just return dvp. Don't need
5112 * to send it over the wire or look it up in the dnlc,
5113 * just need to check access.
5115 if (nm[0] == '.' && nm[1] == '\0') {
5116 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5117 if (error)
5118 return (error);
5119 VN_HOLD(dvp);
5120 *vpp = dvp;
5121 return (0);
5124 drp = VTOR4(dvp);
5125 if (!(drp->r_flags & R4LOOKUP)) {
5126 mutex_enter(&drp->r_statelock);
5127 drp->r_flags |= R4LOOKUP;
5128 mutex_exit(&drp->r_statelock);
5131 *vpp = NULL;
5133 * Lookup this name in the DNLC. If there is no entry
5134 * lookup over the wire.
5136 if (!skipdnlc)
5137 *vpp = dnlc_lookup(dvp, nm);
5138 if (*vpp == NULL) {
5140 * We need to go over the wire to lookup the name.
5142 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5146 * We hit on the dnlc
5148 if (*vpp != DNLC_NO_VNODE ||
5149 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5151 * But our attrs may not be valid.
5153 if (ATTRCACHE4_VALID(dvp)) {
5154 error = nfs4_waitfor_purge_complete(dvp);
5155 if (error) {
5156 VN_RELE(*vpp);
5157 *vpp = NULL;
5158 return (error);
5162 * If after the purge completes, check to make sure
5163 * our attrs are still valid.
5165 if (ATTRCACHE4_VALID(dvp)) {
5167 * If we waited for a purge we may have
5168 * lost our vnode so look it up again.
5170 VN_RELE(*vpp);
5171 *vpp = dnlc_lookup(dvp, nm);
5172 if (*vpp == NULL)
5173 return (nfs4lookupnew_otw(dvp,
5174 nm, vpp, cr));
5177 * The access cache should almost always hit
5179 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5181 if (error) {
5182 VN_RELE(*vpp);
5183 *vpp = NULL;
5184 return (error);
5186 if (*vpp == DNLC_NO_VNODE) {
5187 VN_RELE(*vpp);
5188 *vpp = NULL;
5189 return (ENOENT);
5191 return (0);
5196 ASSERT(*vpp != NULL);
5199 * We may have gotten here we have one of the following cases:
5200 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5201 * need to validate them.
5202 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5203 * must validate.
5205 * Go to the server and check if the directory has changed, if
5206 * it hasn't we are done and can use the dnlc entry.
5208 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5212 * Go to the server and check if the directory has changed, if
5213 * it hasn't we are done and can use the dnlc entry. If it
5214 * has changed we get a new copy of its attributes and check
5215 * the access for VEXEC, then relookup the filename and
5216 * get its filehandle and attributes.
5218 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5219 * if the NVERIFY failed we must
5220 * purge the caches
5221 * cache new attributes (will set r_time_attr_inval)
5222 * cache new access
5223 * recheck VEXEC access
5224 * add name to dnlc, possibly negative
5225 * if LOOKUP succeeded
5226 * cache new attributes
5227 * else
5228 * set a new r_time_attr_inval for dvp
5229 * check to make sure we have access
5231 * The vpp returned is the vnode passed in if the directory is valid,
5232 * a new vnode if successful lookup, or NULL on error.
5234 static int
5235 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5237 COMPOUND4args_clnt args;
5238 COMPOUND4res_clnt res;
5239 fattr4 *ver_fattr;
5240 fattr4_change dchange;
5241 int32_t *ptr;
5242 int argoplist_size = 7 * sizeof (nfs_argop4);
5243 nfs_argop4 *argop;
5244 int doqueue;
5245 mntinfo4_t *mi;
5246 nfs4_recov_state_t recov_state;
5247 hrtime_t t;
5248 int isdotdot;
5249 vnode_t *nvp;
5250 nfs_fh4 *fhp;
5251 nfs4_sharedfh_t *sfhp;
5252 nfs4_access_type_t cacc;
5253 rnode4_t *nrp;
5254 rnode4_t *drp = VTOR4(dvp);
5255 nfs4_ga_res_t *garp = NULL;
5256 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5258 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5259 ASSERT(nm != NULL);
5260 ASSERT(nm[0] != '\0');
5261 ASSERT(dvp->v_type == VDIR);
5262 ASSERT(nm[0] != '.' || nm[1] != '\0');
5263 ASSERT(*vpp != NULL);
5265 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5266 isdotdot = 1;
5267 args.ctag = TAG_LOOKUP_VPARENT;
5268 } else {
5270 * If dvp were a stub, it should have triggered and caused
5271 * a mount for us to get this far.
5273 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5275 isdotdot = 0;
5276 args.ctag = TAG_LOOKUP_VALID;
5279 mi = VTOMI4(dvp);
5280 recov_state.rs_flags = 0;
5281 recov_state.rs_num_retry_despite_err = 0;
5283 nvp = NULL;
5285 /* Save the original mount point security information */
5286 (void) save_mnt_secinfo(mi->mi_curr_serv);
5288 recov_retry:
5289 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5290 &recov_state, NULL);
5291 if (e.error) {
5292 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5293 VN_RELE(*vpp);
5294 *vpp = NULL;
5295 return (e.error);
5298 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5300 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5301 args.array_len = 7;
5302 args.array = argop;
5304 /* 0. putfh file */
5305 argop[0].argop = OP_CPUTFH;
5306 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5308 /* 1. nverify the change info */
5309 argop[1].argop = OP_NVERIFY;
5310 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5311 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5312 ver_fattr->attrlist4 = (char *)&dchange;
5313 ptr = (int32_t *)&dchange;
5314 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5315 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5317 /* 2. getattr directory */
5318 argop[2].argop = OP_GETATTR;
5319 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5320 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5322 /* 3. access directory */
5323 argop[3].argop = OP_ACCESS;
5324 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5325 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5327 /* 4. lookup name */
5328 if (isdotdot) {
5329 argop[4].argop = OP_LOOKUPP;
5330 } else {
5331 argop[4].argop = OP_CLOOKUP;
5332 argop[4].nfs_argop4_u.opclookup.cname = nm;
5335 /* 5. resulting file handle */
5336 argop[5].argop = OP_GETFH;
5338 /* 6. resulting file attributes */
5339 argop[6].argop = OP_GETATTR;
5340 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5341 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5343 doqueue = 1;
5344 t = gethrtime();
5346 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5348 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5349 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5350 if (e.error != 0 && *vpp != NULL)
5351 VN_RELE(*vpp);
5352 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5353 &recov_state, FALSE);
5354 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5355 kmem_free(argop, argoplist_size);
5356 return (e.error);
5359 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5361 * For WRONGSEC of a non-dotdot case, send secinfo directly
5362 * from this thread, do not go thru the recovery thread since
5363 * we need the nm information.
5365 * Not doing dotdot case because there is no specification
5366 * for (PUTFH, SECINFO "..") yet.
5368 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5369 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5370 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5371 &recov_state, FALSE);
5372 else
5373 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5374 &recov_state, TRUE);
5375 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5376 kmem_free(argop, argoplist_size);
5377 if (!e.error)
5378 goto recov_retry;
5379 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5380 VN_RELE(*vpp);
5381 *vpp = NULL;
5382 return (e.error);
5385 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5386 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5387 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5388 &recov_state, TRUE);
5390 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5391 kmem_free(argop, argoplist_size);
5392 goto recov_retry;
5396 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5398 if (e.error || res.array_len == 0) {
5400 * If e.error isn't set, then reply has no ops (or we couldn't
5401 * be here). The only legal way to reply without an op array
5402 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5403 * be in the reply for all other status values.
5405 * For valid replies without an ops array, return ENOTSUP
5406 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5407 * return EIO -- don't trust status.
5409 if (e.error == 0)
5410 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5411 ENOTSUP : EIO;
5412 VN_RELE(*vpp);
5413 *vpp = NULL;
5414 kmem_free(argop, argoplist_size);
5415 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5416 return (e.error);
5419 if (res.status != NFS4ERR_SAME) {
5420 e.error = geterrno4(res.status);
5423 * The NVERIFY "failed" so the directory has changed
5424 * First make sure PUTFH succeeded and NVERIFY "failed"
5425 * cleanly.
5427 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5428 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5429 nfs4_purge_stale_fh(e.error, dvp, cr);
5430 VN_RELE(*vpp);
5431 *vpp = NULL;
5432 goto exit;
5436 * We know the NVERIFY "failed" so we must:
5437 * purge the caches (access and indirectly dnlc if needed)
5439 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5441 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5442 nfs4_purge_stale_fh(e.error, dvp, cr);
5443 VN_RELE(*vpp);
5444 *vpp = NULL;
5445 goto exit;
5449 * Install new cached attributes for the directory
5451 nfs4_attr_cache(dvp,
5452 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5453 t, cr, FALSE, NULL);
5455 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5456 nfs4_purge_stale_fh(e.error, dvp, cr);
5457 VN_RELE(*vpp);
5458 *vpp = NULL;
5459 e.error = geterrno4(res.status);
5460 goto exit;
5464 * Now we know the directory is valid,
5465 * cache new directory access
5467 nfs4_access_cache(drp,
5468 args.array[3].nfs_argop4_u.opaccess.access,
5469 res.array[3].nfs_resop4_u.opaccess.access, cr);
5472 * recheck VEXEC access
5474 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5475 if (cacc != NFS4_ACCESS_ALLOWED) {
5477 * Directory permissions might have been revoked
5479 if (cacc == NFS4_ACCESS_DENIED) {
5480 e.error = EACCES;
5481 VN_RELE(*vpp);
5482 *vpp = NULL;
5483 goto exit;
5487 * Somehow we must not have asked for enough
5488 * so try a singleton ACCESS, should never happen.
5490 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5491 if (e.error) {
5492 VN_RELE(*vpp);
5493 *vpp = NULL;
5494 goto exit;
5498 e.error = geterrno4(res.status);
5499 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5501 * The lookup failed, probably no entry
5503 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5504 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5505 } else {
5507 * Might be some other error, so remove
5508 * the dnlc entry to make sure we start all
5509 * over again, next time.
5511 dnlc_remove(dvp, nm);
5513 VN_RELE(*vpp);
5514 *vpp = NULL;
5515 goto exit;
5518 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5520 * The file exists but we can't get its fh for
5521 * some unknown reason. Remove it from the dnlc
5522 * and error out to be safe.
5524 dnlc_remove(dvp, nm);
5525 VN_RELE(*vpp);
5526 *vpp = NULL;
5527 goto exit;
5529 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5530 if (fhp->nfs_fh4_len == 0) {
5532 * The file exists but a bogus fh
5533 * some unknown reason. Remove it from the dnlc
5534 * and error out to be safe.
5536 e.error = ENOENT;
5537 dnlc_remove(dvp, nm);
5538 VN_RELE(*vpp);
5539 *vpp = NULL;
5540 goto exit;
5542 sfhp = sfh4_get(fhp, mi);
5544 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5545 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5548 * Make the new rnode
5550 if (isdotdot) {
5551 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5552 if (e.error) {
5553 sfh4_rele(&sfhp);
5554 VN_RELE(*vpp);
5555 *vpp = NULL;
5556 goto exit;
5559 * XXX if nfs4_make_dotdot uses an existing rnode
5560 * XXX it doesn't update the attributes.
5561 * XXX for now just save them again to save an OTW
5563 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5564 } else {
5565 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5566 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5568 * If v_type == VNON, then garp was NULL because
5569 * the last op in the compound failed and makenfs4node
5570 * could not find the vnode for sfhp. It created
5571 * a new vnode, so we have nothing to purge here.
5573 if (nvp->v_type == VNON) {
5574 vattr_t vattr;
5576 vattr.va_mask = AT_TYPE;
5578 * N.B. We've already called nfs4_end_fop above.
5580 e.error = nfs4getattr(nvp, &vattr, cr);
5581 if (e.error) {
5582 sfh4_rele(&sfhp);
5583 VN_RELE(*vpp);
5584 *vpp = NULL;
5585 VN_RELE(nvp);
5586 goto exit;
5588 nvp->v_type = vattr.va_type;
5591 sfh4_rele(&sfhp);
5593 nrp = VTOR4(nvp);
5594 mutex_enter(&nrp->r_statev4_lock);
5595 if (!nrp->created_v4) {
5596 mutex_exit(&nrp->r_statev4_lock);
5597 dnlc_update(dvp, nm, nvp);
5598 } else
5599 mutex_exit(&nrp->r_statev4_lock);
5601 VN_RELE(*vpp);
5602 *vpp = nvp;
5603 } else {
5604 hrtime_t now;
5605 hrtime_t delta = 0;
5607 e.error = 0;
5610 * Because the NVERIFY "succeeded" we know that the
5611 * directory attributes are still valid
5612 * so update r_time_attr_inval
5614 now = gethrtime();
5615 mutex_enter(&drp->r_statelock);
5616 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5617 delta = now - drp->r_time_attr_saved;
5618 if (delta < mi->mi_acdirmin)
5619 delta = mi->mi_acdirmin;
5620 else if (delta > mi->mi_acdirmax)
5621 delta = mi->mi_acdirmax;
5623 drp->r_time_attr_inval = now + delta;
5624 mutex_exit(&drp->r_statelock);
5625 dnlc_update(dvp, nm, *vpp);
5628 * Even though we have a valid directory attr cache
5629 * and dnlc entry, we may not have access.
5630 * This should almost always hit the cache.
5632 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5633 if (e.error) {
5634 VN_RELE(*vpp);
5635 *vpp = NULL;
5638 if (*vpp == DNLC_NO_VNODE) {
5639 VN_RELE(*vpp);
5640 *vpp = NULL;
5641 e.error = ENOENT;
5645 exit:
5646 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5647 kmem_free(argop, argoplist_size);
5648 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5649 return (e.error);
5653 * We need to go over the wire to lookup the name, but
5654 * while we are there verify the directory has not
5655 * changed but if it has, get new attributes and check access
5657 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5658 * NVERIFY GETATTR ACCESS
5660 * With the results:
5661 * if the NVERIFY failed we must purge the caches, add new attributes,
5662 * and cache new access.
5663 * set a new r_time_attr_inval
5664 * add name to dnlc, possibly negative
5665 * if LOOKUP succeeded
5666 * cache new attributes
5668 static int
5669 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5671 COMPOUND4args_clnt args;
5672 COMPOUND4res_clnt res;
5673 fattr4 *ver_fattr;
5674 fattr4_change dchange;
5675 int32_t *ptr;
5676 nfs4_ga_res_t *garp = NULL;
5677 int argoplist_size = 9 * sizeof (nfs_argop4);
5678 nfs_argop4 *argop;
5679 int doqueue;
5680 mntinfo4_t *mi;
5681 nfs4_recov_state_t recov_state;
5682 hrtime_t t;
5683 int isdotdot;
5684 vnode_t *nvp;
5685 nfs_fh4 *fhp;
5686 nfs4_sharedfh_t *sfhp;
5687 nfs4_access_type_t cacc;
5688 rnode4_t *nrp;
5689 rnode4_t *drp = VTOR4(dvp);
5690 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5692 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5693 ASSERT(nm != NULL);
5694 ASSERT(nm[0] != '\0');
5695 ASSERT(dvp->v_type == VDIR);
5696 ASSERT(nm[0] != '.' || nm[1] != '\0');
5697 ASSERT(*vpp == NULL);
5699 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5700 isdotdot = 1;
5701 args.ctag = TAG_LOOKUP_PARENT;
5702 } else {
5704 * If dvp were a stub, it should have triggered and caused
5705 * a mount for us to get this far.
5707 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5709 isdotdot = 0;
5710 args.ctag = TAG_LOOKUP;
5713 mi = VTOMI4(dvp);
5714 recov_state.rs_flags = 0;
5715 recov_state.rs_num_retry_despite_err = 0;
5717 nvp = NULL;
5719 /* Save the original mount point security information */
5720 (void) save_mnt_secinfo(mi->mi_curr_serv);
5722 recov_retry:
5723 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5724 &recov_state, NULL);
5725 if (e.error) {
5726 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5727 return (e.error);
5730 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5732 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5733 args.array_len = 9;
5734 args.array = argop;
5736 /* 0. putfh file */
5737 argop[0].argop = OP_CPUTFH;
5738 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5740 /* 1. savefh for the nverify */
5741 argop[1].argop = OP_SAVEFH;
5743 /* 2. lookup name */
5744 if (isdotdot) {
5745 argop[2].argop = OP_LOOKUPP;
5746 } else {
5747 argop[2].argop = OP_CLOOKUP;
5748 argop[2].nfs_argop4_u.opclookup.cname = nm;
5751 /* 3. resulting file handle */
5752 argop[3].argop = OP_GETFH;
5754 /* 4. resulting file attributes */
5755 argop[4].argop = OP_GETATTR;
5756 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5757 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5759 /* 5. restorefh back the directory for the nverify */
5760 argop[5].argop = OP_RESTOREFH;
5762 /* 6. nverify the change info */
5763 argop[6].argop = OP_NVERIFY;
5764 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5765 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5766 ver_fattr->attrlist4 = (char *)&dchange;
5767 ptr = (int32_t *)&dchange;
5768 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5769 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5771 /* 7. getattr directory */
5772 argop[7].argop = OP_GETATTR;
5773 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5774 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5776 /* 8. access directory */
5777 argop[8].argop = OP_ACCESS;
5778 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5779 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5781 doqueue = 1;
5782 t = gethrtime();
5784 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5786 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5787 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5788 if (e.error != 0 && *vpp != NULL)
5789 VN_RELE(*vpp);
5790 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5791 &recov_state, FALSE);
5792 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5793 kmem_free(argop, argoplist_size);
5794 return (e.error);
5797 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5799 * For WRONGSEC of a non-dotdot case, send secinfo directly
5800 * from this thread, do not go thru the recovery thread since
5801 * we need the nm information.
5803 * Not doing dotdot case because there is no specification
5804 * for (PUTFH, SECINFO "..") yet.
5806 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5807 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5809 &recov_state, FALSE);
5810 else
5811 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5812 &recov_state, TRUE);
5813 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5814 kmem_free(argop, argoplist_size);
5815 if (!e.error)
5816 goto recov_retry;
5817 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5818 return (e.error);
5821 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5822 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5823 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5824 &recov_state, TRUE);
5826 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5827 kmem_free(argop, argoplist_size);
5828 goto recov_retry;
5832 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5834 if (e.error || res.array_len == 0) {
5836 * If e.error isn't set, then reply has no ops (or we couldn't
5837 * be here). The only legal way to reply without an op array
5838 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5839 * be in the reply for all other status values.
5841 * For valid replies without an ops array, return ENOTSUP
5842 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5843 * return EIO -- don't trust status.
5845 if (e.error == 0)
5846 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5847 ENOTSUP : EIO;
5849 kmem_free(argop, argoplist_size);
5850 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5851 return (e.error);
5854 e.error = geterrno4(res.status);
5857 * The PUTFH and SAVEFH may have failed.
5859 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5860 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5861 nfs4_purge_stale_fh(e.error, dvp, cr);
5862 goto exit;
5866 * Check if the file exists, if it does delay entering
5867 * into the dnlc until after we update the directory
5868 * attributes so we don't cause it to get purged immediately.
5870 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5872 * The lookup failed, probably no entry
5874 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5875 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5876 goto exit;
5879 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5881 * The file exists but we can't get its fh for
5882 * some unknown reason. Error out to be safe.
5884 goto exit;
5887 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5888 if (fhp->nfs_fh4_len == 0) {
5890 * The file exists but a bogus fh
5891 * some unknown reason. Error out to be safe.
5893 e.error = EIO;
5894 goto exit;
5896 sfhp = sfh4_get(fhp, mi);
5898 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5899 sfh4_rele(&sfhp);
5900 goto exit;
5902 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5905 * The RESTOREFH may have failed
5907 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5908 sfh4_rele(&sfhp);
5909 e.error = EIO;
5910 goto exit;
5913 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5915 * First make sure the NVERIFY failed as we expected,
5916 * if it didn't then be conservative and error out
5917 * as we can't trust the directory.
5919 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5920 sfh4_rele(&sfhp);
5921 e.error = EIO;
5922 goto exit;
5926 * We know the NVERIFY "failed" so the directory has changed,
5927 * so we must:
5928 * purge the caches (access and indirectly dnlc if needed)
5930 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5932 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5933 sfh4_rele(&sfhp);
5934 goto exit;
5936 nfs4_attr_cache(dvp,
5937 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5938 t, cr, FALSE, NULL);
5940 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5941 nfs4_purge_stale_fh(e.error, dvp, cr);
5942 sfh4_rele(&sfhp);
5943 e.error = geterrno4(res.status);
5944 goto exit;
5948 * Now we know the directory is valid,
5949 * cache new directory access
5951 nfs4_access_cache(drp,
5952 args.array[8].nfs_argop4_u.opaccess.access,
5953 res.array[8].nfs_resop4_u.opaccess.access, cr);
5956 * recheck VEXEC access
5958 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5959 if (cacc != NFS4_ACCESS_ALLOWED) {
5961 * Directory permissions might have been revoked
5963 if (cacc == NFS4_ACCESS_DENIED) {
5964 sfh4_rele(&sfhp);
5965 e.error = EACCES;
5966 goto exit;
5970 * Somehow we must not have asked for enough
5971 * so try a singleton ACCESS should never happen
5973 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5974 if (e.error) {
5975 sfh4_rele(&sfhp);
5976 goto exit;
5980 e.error = geterrno4(res.status);
5981 } else {
5982 hrtime_t now;
5983 hrtime_t delta = 0;
5985 e.error = 0;
5988 * Because the NVERIFY "succeeded" we know that the
5989 * directory attributes are still valid
5990 * so update r_time_attr_inval
5992 now = gethrtime();
5993 mutex_enter(&drp->r_statelock);
5994 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5995 delta = now - drp->r_time_attr_saved;
5996 if (delta < mi->mi_acdirmin)
5997 delta = mi->mi_acdirmin;
5998 else if (delta > mi->mi_acdirmax)
5999 delta = mi->mi_acdirmax;
6001 drp->r_time_attr_inval = now + delta;
6002 mutex_exit(&drp->r_statelock);
6005 * Even though we have a valid directory attr cache,
6006 * we may not have access.
6007 * This should almost always hit the cache.
6009 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
6010 if (e.error) {
6011 sfh4_rele(&sfhp);
6012 goto exit;
6017 * Now we have successfully completed the lookup, if the
6018 * directory has changed we now have the valid attributes.
6019 * We also know we have directory access.
6020 * Create the new rnode and insert it in the dnlc.
6022 if (isdotdot) {
6023 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6024 if (e.error) {
6025 sfh4_rele(&sfhp);
6026 goto exit;
6029 * XXX if nfs4_make_dotdot uses an existing rnode
6030 * XXX it doesn't update the attributes.
6031 * XXX for now just save them again to save an OTW
6033 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6034 } else {
6035 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6036 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6038 sfh4_rele(&sfhp);
6040 nrp = VTOR4(nvp);
6041 mutex_enter(&nrp->r_statev4_lock);
6042 if (!nrp->created_v4) {
6043 mutex_exit(&nrp->r_statev4_lock);
6044 dnlc_update(dvp, nm, nvp);
6045 } else
6046 mutex_exit(&nrp->r_statev4_lock);
6048 *vpp = nvp;
6050 exit:
6051 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6052 kmem_free(argop, argoplist_size);
6053 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6054 return (e.error);
6057 #ifdef DEBUG
6058 void
6059 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6061 uint_t i, len;
6062 zoneid_t zoneid = getzoneid();
6063 char *s;
6065 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6066 for (i = 0; i < argcnt; i++) {
6067 nfs_argop4 *op = &argbase[i];
6068 switch (op->argop) {
6069 case OP_CPUTFH:
6070 case OP_PUTFH:
6071 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6072 break;
6073 case OP_PUTROOTFH:
6074 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6075 break;
6076 case OP_CLOOKUP:
6077 s = op->nfs_argop4_u.opclookup.cname;
6078 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6079 break;
6080 case OP_LOOKUP:
6081 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6082 &len, NULL);
6083 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6084 kmem_free(s, len);
6085 break;
6086 case OP_LOOKUPP:
6087 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6088 break;
6089 case OP_GETFH:
6090 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6091 break;
6092 case OP_GETATTR:
6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6094 break;
6095 case OP_OPENATTR:
6096 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6097 break;
6098 default:
6099 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6100 op->argop);
6101 break;
6105 #endif
6108 * nfs4lookup_setup - constructs a multi-lookup compound request.
6110 * Given the path "nm1/nm2/.../nmn", the following compound requests
6111 * may be created:
6113 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6114 * is faster, for now.
6116 * l4_getattrs indicates the type of compound requested.
6118 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6120 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6122 * total number of ops is n + 1.
6124 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6125 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6126 * before the last component, and only get attributes
6127 * for the last component. Note that the second-to-last
6128 * pathname component is XATTR_RPATH, which does NOT go
6129 * over-the-wire as a lookup.
6131 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6132 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6134 * and total number of ops is n + 5.
6136 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6137 * attribute directory: create lookups plus an OPENATTR
6138 * replacing the last lookup. Note that the last pathname
6139 * component is XATTR_RPATH, which does NOT go over-the-wire
6140 * as a lookup.
6142 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6143 * Openattr; Getfh; Getattr }
6145 * and total number of ops is n + 5.
6147 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6148 * nodes too.
6150 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6151 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6153 * and total number of ops is 3*n + 1.
6155 * All cases: returns the index in the arg array of the final LOOKUP op, or
6156 * -1 if no LOOKUPs were used.
6159 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6161 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6162 nfs_argop4 *argbase, *argop;
6163 int arglen, argcnt;
6164 int n = 1; /* number of components */
6165 int nga = 1; /* number of Getattr's in request */
6166 char c = '\0', *s, *p;
6167 int lookup_idx = -1;
6168 int argoplist_size;
6170 /* set lookuparg response result to 0 */
6171 lookupargp->resp->status = NFS4_OK;
6173 /* skip leading "/" or "." e.g. ".//./" if there is */
6174 for (; ; nm++) {
6175 if (*nm != '/' && *nm != '.')
6176 break;
6178 /* ".." is counted as 1 component */
6179 if (*nm == '.' && *(nm + 1) != '/')
6180 break;
6184 * Find n = number of components - nm must be null terminated
6185 * Skip "." components.
6187 if (*nm != '\0')
6188 for (n = 1, s = nm; *s != '\0'; s++) {
6189 if ((*s == '/') && (*(s + 1) != '/') &&
6190 (*(s + 1) != '\0') &&
6191 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6192 *(s + 2) == '\0')))
6193 n++;
6195 else
6196 n = 0;
6199 * nga is number of components that need Getfh+Getattr
6201 switch (l4_getattrs) {
6202 case LKP4_NO_ATTRIBUTES:
6203 nga = 0;
6204 break;
6205 case LKP4_ALL_ATTRIBUTES:
6206 nga = n;
6208 * Always have at least 1 getfh, getattr pair
6210 if (nga == 0)
6211 nga++;
6212 break;
6213 case LKP4_LAST_ATTRDIR:
6214 case LKP4_LAST_NAMED_ATTR:
6215 nga = n+1;
6216 break;
6220 * If change to use the filehandle attr instead of getfh
6221 * the following line can be deleted.
6223 nga *= 2;
6226 * calculate number of ops in request as
6227 * header + trailer + lookups + getattrs
6229 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6231 argoplist_size = arglen * sizeof (nfs_argop4);
6232 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6233 lookupargp->argsp->array = argop;
6235 argcnt = lookupargp->header_len;
6236 argop += argcnt;
6239 * loop and create a lookup op and possibly getattr/getfh for
6240 * each component. Skip "." components.
6242 for (s = nm; *s != '\0'; s = p) {
6244 * Set up a pathname struct for each component if needed
6246 while (*s == '/')
6247 s++;
6248 if (*s == '\0')
6249 break;
6251 for (p = s; (*p != '/') && (*p != '\0'); p++)
6253 c = *p;
6254 *p = '\0';
6256 if (s[0] == '.' && s[1] == '\0') {
6257 *p = c;
6258 continue;
6260 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6261 strcmp(s, XATTR_RPATH) == 0) {
6262 /* getfh XXX may not be needed in future */
6263 argop->argop = OP_GETFH;
6264 argop++;
6265 argcnt++;
6267 /* getattr */
6268 argop->argop = OP_GETATTR;
6269 argop->nfs_argop4_u.opgetattr.attr_request =
6270 lookupargp->ga_bits;
6271 argop->nfs_argop4_u.opgetattr.mi =
6272 lookupargp->mi;
6273 argop++;
6274 argcnt++;
6276 /* openattr */
6277 argop->argop = OP_OPENATTR;
6278 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6279 strcmp(s, XATTR_RPATH) == 0) {
6280 /* openattr */
6281 argop->argop = OP_OPENATTR;
6282 argop++;
6283 argcnt++;
6285 /* getfh XXX may not be needed in future */
6286 argop->argop = OP_GETFH;
6287 argop++;
6288 argcnt++;
6290 /* getattr */
6291 argop->argop = OP_GETATTR;
6292 argop->nfs_argop4_u.opgetattr.attr_request =
6293 lookupargp->ga_bits;
6294 argop->nfs_argop4_u.opgetattr.mi =
6295 lookupargp->mi;
6296 argop++;
6297 argcnt++;
6298 *p = c;
6299 continue;
6300 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6301 /* lookupp */
6302 argop->argop = OP_LOOKUPP;
6303 } else {
6304 /* lookup */
6305 argop->argop = OP_LOOKUP;
6306 (void) str_to_utf8(s,
6307 &argop->nfs_argop4_u.oplookup.objname);
6309 lookup_idx = argcnt;
6310 argop++;
6311 argcnt++;
6313 *p = c;
6315 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6316 /* getfh XXX may not be needed in future */
6317 argop->argop = OP_GETFH;
6318 argop++;
6319 argcnt++;
6321 /* getattr */
6322 argop->argop = OP_GETATTR;
6323 argop->nfs_argop4_u.opgetattr.attr_request =
6324 lookupargp->ga_bits;
6325 argop->nfs_argop4_u.opgetattr.mi =
6326 lookupargp->mi;
6327 argop++;
6328 argcnt++;
6332 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6333 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6334 if (needgetfh) {
6335 /* stick in a post-lookup getfh */
6336 argop->argop = OP_GETFH;
6337 argcnt++;
6338 argop++;
6340 /* post-lookup getattr */
6341 argop->argop = OP_GETATTR;
6342 argop->nfs_argop4_u.opgetattr.attr_request =
6343 lookupargp->ga_bits;
6344 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6345 argcnt++;
6347 argcnt += lookupargp->trailer_len; /* actual op count */
6348 lookupargp->argsp->array_len = argcnt;
6349 lookupargp->arglen = arglen;
6351 #ifdef DEBUG
6352 if (nfs4_client_lookup_debug)
6353 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6354 #endif
6356 return (lookup_idx);
6359 static int
6360 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6362 COMPOUND4args_clnt args;
6363 COMPOUND4res_clnt res;
6364 GETFH4res *gf_res = NULL;
6365 nfs_argop4 argop[4];
6366 nfs_resop4 *resop = NULL;
6367 nfs4_sharedfh_t *sfhp;
6368 hrtime_t t;
6369 nfs4_error_t e;
6371 rnode4_t *drp;
6372 int doqueue = 1;
6373 vnode_t *vp;
6374 int needrecov = 0;
6375 nfs4_recov_state_t recov_state;
6377 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6379 *avp = NULL;
6380 recov_state.rs_flags = 0;
6381 recov_state.rs_num_retry_despite_err = 0;
6383 recov_retry:
6384 /* COMPOUND: putfh, openattr, getfh, getattr */
6385 args.array_len = 4;
6386 args.array = argop;
6387 args.ctag = TAG_OPENATTR;
6389 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6390 if (e.error)
6391 return (e.error);
6393 drp = VTOR4(dvp);
6395 /* putfh */
6396 argop[0].argop = OP_CPUTFH;
6397 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6399 /* openattr */
6400 argop[1].argop = OP_OPENATTR;
6401 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6403 /* getfh */
6404 argop[2].argop = OP_GETFH;
6406 /* getattr */
6407 argop[3].argop = OP_GETATTR;
6408 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6409 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6411 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6412 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6413 rnode4info(drp)));
6415 t = gethrtime();
6417 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6419 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6420 if (needrecov) {
6421 bool_t abort;
6423 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6424 "nfs4openattr: initiating recovery\n"));
6426 abort = nfs4_start_recovery(&e,
6427 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6428 OP_OPENATTR, NULL, NULL, NULL);
6429 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6430 if (!e.error) {
6431 e.error = geterrno4(res.status);
6432 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6434 if (abort == FALSE)
6435 goto recov_retry;
6436 return (e.error);
6439 if (e.error) {
6440 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6441 return (e.error);
6444 if (res.status) {
6446 * If OTW errro is NOTSUPP, then it should be
6447 * translated to EINVAL. All Solaris file system
6448 * implementations return EINVAL to the syscall layer
6449 * when the attrdir cannot be created due to an
6450 * implementation restriction or noxattr mount option.
6452 if (res.status == NFS4ERR_NOTSUPP) {
6453 mutex_enter(&drp->r_statelock);
6454 if (drp->r_xattr_dir)
6455 VN_RELE(drp->r_xattr_dir);
6456 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6457 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6458 mutex_exit(&drp->r_statelock);
6460 e.error = EINVAL;
6461 } else {
6462 e.error = geterrno4(res.status);
6465 if (e.error) {
6466 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6467 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6468 needrecov);
6469 return (e.error);
6473 resop = &res.array[0]; /* putfh res */
6474 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6476 resop = &res.array[1]; /* openattr res */
6477 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6479 resop = &res.array[2]; /* getfh res */
6480 gf_res = &resop->nfs_resop4_u.opgetfh;
6481 if (gf_res->object.nfs_fh4_len == 0) {
6482 *avp = NULL;
6483 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6484 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6485 return (ENOENT);
6488 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6489 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6490 dvp->v_vfsp, t, cr, dvp,
6491 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6492 sfh4_rele(&sfhp);
6494 if (e.error)
6495 PURGE_ATTRCACHE4(vp);
6497 mutex_enter(&vp->v_lock);
6498 vp->v_flag |= V_XATTRDIR;
6499 mutex_exit(&vp->v_lock);
6501 *avp = vp;
6503 mutex_enter(&drp->r_statelock);
6504 if (drp->r_xattr_dir)
6505 VN_RELE(drp->r_xattr_dir);
6506 VN_HOLD(vp);
6507 drp->r_xattr_dir = vp;
6510 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6511 * NULL. xattrs could be created at any time, and we have no
6512 * way to update pc4_xattr_exists in the base object if/when
6513 * it happens.
6515 drp->r_pathconf.pc4_xattr_valid = 0;
6517 mutex_exit(&drp->r_statelock);
6519 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6521 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6523 return (0);
6526 /* ARGSUSED */
6527 static int
6528 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6529 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6530 vsecattr_t *vsecp)
6532 int error;
6533 vnode_t *vp = NULL;
6534 rnode4_t *rp;
6535 struct vattr vattr;
6536 rnode4_t *drp;
6537 vnode_t *tempvp;
6538 enum createmode4 createmode;
6539 bool_t must_trunc = FALSE;
6540 int truncating = 0;
6542 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6543 return (EPERM);
6544 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6545 return (EINVAL);
6548 /* . and .. have special meaning in the protocol, reject them. */
6550 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6551 return (EISDIR);
6553 drp = VTOR4(dvp);
6555 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6556 return (EINTR);
6558 top:
6560 * We make a copy of the attributes because the caller does not
6561 * expect us to change what va points to.
6563 vattr = *va;
6566 * If the pathname is "", then dvp is the root vnode of
6567 * a remote file mounted over a local directory.
6568 * All that needs to be done is access
6569 * checking and truncation. Note that we avoid doing
6570 * open w/ create because the parent directory might
6571 * be in pseudo-fs and the open would fail.
6573 if (*nm == '\0') {
6574 error = 0;
6575 VN_HOLD(dvp);
6576 vp = dvp;
6577 must_trunc = TRUE;
6578 } else {
6580 * We need to go over the wire, just to be sure whether the
6581 * file exists or not. Using the DNLC can be dangerous in
6582 * this case when making a decision regarding existence.
6584 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6587 if (exclusive)
6588 createmode = EXCLUSIVE4;
6589 else
6590 createmode = GUARDED4;
6593 * error would be set if the file does not exist on the
6594 * server, so lets go create it.
6596 if (error) {
6597 goto create_otw;
6601 * File does exist on the server
6603 if (exclusive == EXCL)
6604 error = EEXIST;
6605 else if (vp->v_type == VDIR && (mode & VWRITE))
6606 error = EISDIR;
6607 else {
6609 * If vnode is a device, create special vnode.
6611 if (ISVDEV(vp->v_type)) {
6612 tempvp = vp;
6613 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6614 VN_RELE(tempvp);
6616 if (!(error = fop_access(vp, mode, 0, cr, ct))) {
6617 if ((vattr.va_mask & AT_SIZE) &&
6618 vp->v_type == VREG) {
6619 rp = VTOR4(vp);
6621 * Check here for large file handled
6622 * by LF-unaware process (as
6623 * ufs_create() does)
6625 if (!(flags & FOFFMAX)) {
6626 mutex_enter(&rp->r_statelock);
6627 if (rp->r_size > MAXOFF32_T)
6628 error = EOVERFLOW;
6629 mutex_exit(&rp->r_statelock);
6632 /* if error is set then we need to return */
6633 if (error) {
6634 nfs_rw_exit(&drp->r_rwlock);
6635 VN_RELE(vp);
6636 return (error);
6639 if (must_trunc) {
6640 vattr.va_mask = AT_SIZE;
6641 error = nfs4setattr(vp, &vattr, 0, cr,
6642 NULL);
6643 } else {
6645 * we know we have a regular file that already
6646 * exists and we may end up truncating the file
6647 * as a result of the open_otw, so flush out
6648 * any dirty pages for this file first.
6650 if (nfs4_has_pages(vp) &&
6651 ((rp->r_flags & R4DIRTY) ||
6652 rp->r_count > 0 ||
6653 rp->r_mapcnt > 0)) {
6654 error = nfs4_putpage(vp,
6655 (offset_t)0, 0, 0, cr, ct);
6656 if (error && (error == ENOSPC ||
6657 error == EDQUOT)) {
6658 mutex_enter(
6659 &rp->r_statelock);
6660 if (!rp->r_error)
6661 rp->r_error =
6662 error;
6663 mutex_exit(
6664 &rp->r_statelock);
6667 vattr.va_mask = (AT_SIZE |
6668 AT_TYPE | AT_MODE);
6669 vattr.va_type = VREG;
6670 createmode = UNCHECKED4;
6671 truncating = 1;
6672 goto create_otw;
6677 nfs_rw_exit(&drp->r_rwlock);
6678 if (error) {
6679 VN_RELE(vp);
6680 } else {
6681 vnode_t *tvp;
6682 rnode4_t *trp;
6683 tvp = vp;
6684 if (vp->v_type == VREG) {
6685 trp = VTOR4(vp);
6686 if (IS_SHADOW(vp, trp))
6687 tvp = RTOV4(trp);
6690 if (must_trunc) {
6692 * existing file got truncated, notify.
6694 vnevent_create(tvp, ct);
6697 *vpp = vp;
6699 return (error);
6701 create_otw:
6702 dnlc_remove(dvp, nm);
6704 ASSERT(vattr.va_mask & AT_TYPE);
6707 * If not a regular file let nfs4mknod() handle it.
6709 if (vattr.va_type != VREG) {
6710 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6711 nfs_rw_exit(&drp->r_rwlock);
6712 return (error);
6716 * It _is_ a regular file.
6718 ASSERT(vattr.va_mask & AT_MODE);
6719 if (MANDMODE(vattr.va_mode)) {
6720 nfs_rw_exit(&drp->r_rwlock);
6721 return (EACCES);
6725 * If this happens to be a mknod of a regular file, then flags will
6726 * have neither FREAD or FWRITE. However, we must set at least one
6727 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6728 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6729 * set (based on openmode specified by app).
6731 if ((flags & (FREAD|FWRITE)) == 0)
6732 flags |= (FREAD|FWRITE);
6734 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6736 if (vp != NULL) {
6737 /* if create was successful, throw away the file's pages */
6738 if (!error && (vattr.va_mask & AT_SIZE))
6739 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6740 cr);
6741 /* release the lookup hold */
6742 VN_RELE(vp);
6743 vp = NULL;
6747 * validate that we opened a regular file. This handles a misbehaving
6748 * server that returns an incorrect FH.
6750 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6751 error = EISDIR;
6752 VN_RELE(*vpp);
6756 * If this is not an exclusive create, then the CREATE
6757 * request will be made with the GUARDED mode set. This
6758 * means that the server will return EEXIST if the file
6759 * exists. The file could exist because of a retransmitted
6760 * request. In this case, we recover by starting over and
6761 * checking to see whether the file exists. This second
6762 * time through it should and a CREATE request will not be
6763 * sent.
6765 * This handles the problem of a dangling CREATE request
6766 * which contains attributes which indicate that the file
6767 * should be truncated. This retransmitted request could
6768 * possibly truncate valid data in the file if not caught
6769 * by the duplicate request mechanism on the server or if
6770 * not caught by other means. The scenario is:
6772 * Client transmits CREATE request with size = 0
6773 * Client times out, retransmits request.
6774 * Response to the first request arrives from the server
6775 * and the client proceeds on.
6776 * Client writes data to the file.
6777 * The server now processes retransmitted CREATE request
6778 * and truncates file.
6780 * The use of the GUARDED CREATE request prevents this from
6781 * happening because the retransmitted CREATE would fail
6782 * with EEXIST and would not truncate the file.
6784 if (error == EEXIST && exclusive == NONEXCL) {
6785 #ifdef DEBUG
6786 nfs4_create_misses++;
6787 #endif
6788 goto top;
6790 nfs_rw_exit(&drp->r_rwlock);
6791 if (truncating && !error && *vpp) {
6792 vnode_t *tvp;
6793 rnode4_t *trp;
6795 * existing file got truncated, notify.
6797 tvp = *vpp;
6798 trp = VTOR4(tvp);
6799 if (IS_SHADOW(tvp, trp))
6800 tvp = RTOV4(trp);
6801 vnevent_create(tvp, ct);
6803 return (error);
6807 * Create compound (for mkdir, mknod, symlink):
6808 * { Putfh <dfh>; Create; Getfh; Getattr }
6809 * It's okay if setattr failed to set gid - this is not considered
6810 * an error, but purge attrs in that case.
6812 static int
6813 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6814 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6816 int need_end_op = FALSE;
6817 COMPOUND4args_clnt args;
6818 COMPOUND4res_clnt res, *resp = NULL;
6819 nfs_argop4 *argop;
6820 nfs_resop4 *resop;
6821 int doqueue;
6822 mntinfo4_t *mi;
6823 rnode4_t *drp = VTOR4(dvp);
6824 change_info4 *cinfo;
6825 GETFH4res *gf_res;
6826 struct vattr vattr;
6827 vnode_t *vp;
6828 fattr4 *crattr;
6829 bool_t needrecov = FALSE;
6830 nfs4_recov_state_t recov_state;
6831 nfs4_sharedfh_t *sfhp = NULL;
6832 hrtime_t t;
6833 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6834 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6835 dirattr_info_t dinfo, *dinfop;
6836 servinfo4_t *svp;
6837 bitmap4 supp_attrs;
6839 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6840 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6842 mi = VTOMI4(dvp);
6845 * Make sure we properly deal with setting the right gid
6846 * on a new directory to reflect the parent's setgid bit
6848 setgid_flag = 0;
6849 if (type == NF4DIR) {
6850 struct vattr dva;
6852 va->va_mode &= ~VSGID;
6853 dva.va_mask = AT_MODE | AT_GID;
6854 if (fop_getattr(dvp, &dva, 0, cr, NULL) == 0) {
6857 * If the parent's directory has the setgid bit set
6858 * _and_ the client was able to get a valid mapping
6859 * for the parent dir's owner_group, we want to
6860 * append NVERIFY(owner_group == dva.va_gid) and
6861 * SETTATTR to the CREATE compound.
6863 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6864 setgid_flag = 1;
6865 va->va_mode |= VSGID;
6866 if (dva.va_gid != GID_NOBODY) {
6867 va->va_mask |= AT_GID;
6868 va->va_gid = dva.va_gid;
6875 * Create ops:
6876 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6877 * 5:restorefh(dir) 6:getattr(dir)
6879 * if (setgid)
6880 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6881 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6882 * 8:nverify 9:setattr
6884 if (setgid_flag) {
6885 numops = 10;
6886 idx_create = 1;
6887 idx_fattr = 3;
6888 } else {
6889 numops = 7;
6890 idx_create = 2;
6891 idx_fattr = 4;
6894 ASSERT(nfs_zone() == mi->mi_zone);
6895 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6896 return (EINTR);
6898 recov_state.rs_flags = 0;
6899 recov_state.rs_num_retry_despite_err = 0;
6901 argoplist_size = numops * sizeof (nfs_argop4);
6902 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6904 recov_retry:
6905 if (type == NF4LNK)
6906 args.ctag = TAG_SYMLINK;
6907 else if (type == NF4DIR)
6908 args.ctag = TAG_MKDIR;
6909 else
6910 args.ctag = TAG_MKNOD;
6912 args.array_len = numops;
6913 args.array = argop;
6915 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6916 nfs_rw_exit(&drp->r_rwlock);
6917 kmem_free(argop, argoplist_size);
6918 return (e.error);
6920 need_end_op = TRUE;
6923 /* 0: putfh directory */
6924 argop[0].argop = OP_CPUTFH;
6925 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6927 /* 1/2: Create object */
6928 argop[idx_create].argop = OP_CCREATE;
6929 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6930 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6931 if (type == NF4LNK) {
6933 * symlink, treat name as data
6935 ASSERT(data != NULL);
6936 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6937 (char *)data;
6939 if (type == NF4BLK || type == NF4CHR) {
6940 ASSERT(data != NULL);
6941 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6942 *((specdata4 *)data);
6945 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6947 svp = drp->r_server;
6948 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6949 supp_attrs = svp->sv_supp_attrs;
6950 nfs_rw_exit(&svp->sv_lock);
6952 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6953 nfs_rw_exit(&drp->r_rwlock);
6954 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6955 e.error = EINVAL;
6956 kmem_free(argop, argoplist_size);
6957 return (e.error);
6960 /* 2/3: getfh fh of created object */
6961 ASSERT(idx_create + 1 == idx_fattr - 1);
6962 argop[idx_create + 1].argop = OP_GETFH;
6964 /* 3/4: getattr of new object */
6965 argop[idx_fattr].argop = OP_GETATTR;
6966 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6967 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6969 if (setgid_flag) {
6970 vattr_t _v;
6972 argop[4].argop = OP_SAVEFH;
6974 argop[5].argop = OP_CPUTFH;
6975 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6977 argop[6].argop = OP_GETATTR;
6978 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6979 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6981 argop[7].argop = OP_RESTOREFH;
6984 * nverify
6986 * XXX - Revisit the last argument to nfs4_end_op()
6987 * once 5020486 is fixed.
6989 _v.va_mask = AT_GID;
6990 _v.va_gid = va->va_gid;
6991 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6992 supp_attrs)) {
6993 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6994 nfs_rw_exit(&drp->r_rwlock);
6995 nfs4_fattr4_free(crattr);
6996 kmem_free(argop, argoplist_size);
6997 return (e.error);
7001 * setattr
7003 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
7004 * so no need for stateid or flags. Also we specify NULL
7005 * rp since we're only interested in setting owner_group
7006 * attributes.
7008 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
7009 &e.error, 0);
7011 if (e.error) {
7012 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7013 nfs_rw_exit(&drp->r_rwlock);
7014 nfs4_fattr4_free(crattr);
7015 nfs4args_verify_free(&argop[8]);
7016 kmem_free(argop, argoplist_size);
7017 return (e.error);
7019 } else {
7020 argop[1].argop = OP_SAVEFH;
7022 argop[5].argop = OP_RESTOREFH;
7024 argop[6].argop = OP_GETATTR;
7025 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7026 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7029 dnlc_remove(dvp, nm);
7031 doqueue = 1;
7032 t = gethrtime();
7033 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7035 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7036 if (e.error) {
7037 PURGE_ATTRCACHE4(dvp);
7038 if (!needrecov)
7039 goto out;
7042 if (needrecov) {
7043 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7044 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7045 nfs4_end_op(mi, dvp, NULL, &recov_state,
7046 needrecov);
7047 need_end_op = FALSE;
7048 nfs4_fattr4_free(crattr);
7049 if (setgid_flag) {
7050 nfs4args_verify_free(&argop[8]);
7051 nfs4args_setattr_free(&argop[9]);
7053 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7054 goto recov_retry;
7058 resp = &res;
7060 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7062 if (res.status == NFS4ERR_BADOWNER)
7063 nfs4_log_badowner(mi, OP_CREATE);
7065 e.error = geterrno4(res.status);
7068 * This check is left over from when create was implemented
7069 * using a setattr op (instead of createattrs). If the
7070 * putfh/create/getfh failed, the error was returned. If
7071 * setattr/getattr failed, we keep going.
7073 * It might be better to get rid of the GETFH also, and just
7074 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7075 * Then if any of the operations failed, we could return the
7076 * error now, and remove much of the error code below.
7078 if (res.array_len <= idx_fattr) {
7080 * Either Putfh, Create or Getfh failed.
7082 PURGE_ATTRCACHE4(dvp);
7084 * nfs4_purge_stale_fh() may generate otw calls through
7085 * nfs4_invalidate_pages. Hence the need to call
7086 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7088 nfs4_end_op(mi, dvp, NULL, &recov_state,
7089 needrecov);
7090 need_end_op = FALSE;
7091 nfs4_purge_stale_fh(e.error, dvp, cr);
7092 goto out;
7096 resop = &res.array[idx_create]; /* create res */
7097 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7099 resop = &res.array[idx_create + 1]; /* getfh res */
7100 gf_res = &resop->nfs_resop4_u.opgetfh;
7102 sfhp = sfh4_get(&gf_res->object, mi);
7103 if (e.error) {
7104 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7105 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7106 if (vp->v_type == VNON) {
7107 vattr.va_mask = AT_TYPE;
7109 * Need to call nfs4_end_op before nfs4getattr to avoid
7110 * potential nfs4_start_op deadlock. See RFE 4777612.
7112 nfs4_end_op(mi, dvp, NULL, &recov_state,
7113 needrecov);
7114 need_end_op = FALSE;
7115 e.error = nfs4getattr(vp, &vattr, cr);
7116 if (e.error) {
7117 VN_RELE(vp);
7118 *vpp = NULL;
7119 goto out;
7121 vp->v_type = vattr.va_type;
7123 e.error = 0;
7124 } else {
7125 *vpp = vp = makenfs4node(sfhp,
7126 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7127 dvp->v_vfsp, t, cr,
7128 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7132 * If compound succeeded, then update dir attrs
7134 if (res.status == NFS4_OK) {
7135 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7136 dinfo.di_cred = cr;
7137 dinfo.di_time_call = t;
7138 dinfop = &dinfo;
7139 } else
7140 dinfop = NULL;
7142 /* Update directory cache attribute, readdir and dnlc caches */
7143 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7145 out:
7146 if (sfhp != NULL)
7147 sfh4_rele(&sfhp);
7148 nfs_rw_exit(&drp->r_rwlock);
7149 nfs4_fattr4_free(crattr);
7150 if (setgid_flag) {
7151 nfs4args_verify_free(&argop[8]);
7152 nfs4args_setattr_free(&argop[9]);
7154 if (resp)
7155 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7156 if (need_end_op)
7157 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7159 kmem_free(argop, argoplist_size);
7160 return (e.error);
7163 /* ARGSUSED */
7164 static int
7165 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7166 int mode, vnode_t **vpp, cred_t *cr)
7168 int error;
7169 vnode_t *vp;
7170 nfs_ftype4 type;
7171 specdata4 spec, *specp = NULL;
7173 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7175 switch (va->va_type) {
7176 case VCHR:
7177 case VBLK:
7178 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7179 spec.specdata1 = getmajor(va->va_rdev);
7180 spec.specdata2 = getminor(va->va_rdev);
7181 specp = &spec;
7182 break;
7184 case VFIFO:
7185 type = NF4FIFO;
7186 break;
7187 case VSOCK:
7188 type = NF4SOCK;
7189 break;
7191 default:
7192 return (EINVAL);
7195 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7196 if (error) {
7197 return (error);
7201 * This might not be needed any more; special case to deal
7202 * with problematic v2/v3 servers. Since create was unable
7203 * to set group correctly, not sure what hope setattr has.
7205 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7206 va->va_mask = AT_GID;
7207 (void) nfs4setattr(vp, va, 0, cr, NULL);
7211 * If vnode is a device create special vnode
7213 if (ISVDEV(vp->v_type)) {
7214 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7215 VN_RELE(vp);
7216 } else {
7217 *vpp = vp;
7219 return (error);
7223 * Remove requires that the current fh be the target directory.
7224 * After the operation, the current fh is unchanged.
7225 * The compound op structure is:
7226 * PUTFH(targetdir), REMOVE
7228 * Weirdness: if the vnode to be removed is open
7229 * we rename it instead of removing it and nfs_inactive
7230 * will remove the new name.
7232 /* ARGSUSED */
7233 static int
7234 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7236 COMPOUND4args_clnt args;
7237 COMPOUND4res_clnt res, *resp = NULL;
7238 REMOVE4res *rm_res;
7239 nfs_argop4 argop[3];
7240 nfs_resop4 *resop;
7241 vnode_t *vp;
7242 char *tmpname;
7243 int doqueue;
7244 mntinfo4_t *mi;
7245 rnode4_t *rp;
7246 rnode4_t *drp;
7247 int needrecov = 0;
7248 nfs4_recov_state_t recov_state;
7249 int isopen;
7250 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7251 dirattr_info_t dinfo;
7253 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7254 return (EPERM);
7255 drp = VTOR4(dvp);
7256 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7257 return (EINTR);
7259 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7260 if (e.error) {
7261 nfs_rw_exit(&drp->r_rwlock);
7262 return (e.error);
7265 if (vp->v_type == VDIR) {
7266 VN_RELE(vp);
7267 nfs_rw_exit(&drp->r_rwlock);
7268 return (EISDIR);
7272 * First just remove the entry from the name cache, as it
7273 * is most likely the only entry for this vp.
7275 dnlc_remove(dvp, nm);
7277 rp = VTOR4(vp);
7280 * For regular file types, check to see if the file is open by looking
7281 * at the open streams.
7282 * For all other types, check the reference count on the vnode. Since
7283 * they are not opened OTW they never have an open stream.
7285 * If the file is open, rename it to .nfsXXXX.
7287 if (vp->v_type != VREG) {
7289 * If the file has a v_count > 1 then there may be more than one
7290 * entry in the name cache due multiple links or an open file,
7291 * but we don't have the real reference count so flush all
7292 * possible entries.
7294 if (vp->v_count > 1)
7295 dnlc_purge_vp(vp);
7298 * Now we have the real reference count.
7300 isopen = vp->v_count > 1;
7301 } else {
7302 mutex_enter(&rp->r_os_lock);
7303 isopen = list_head(&rp->r_open_streams) != NULL;
7304 mutex_exit(&rp->r_os_lock);
7307 mutex_enter(&rp->r_statelock);
7308 if (isopen &&
7309 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7310 mutex_exit(&rp->r_statelock);
7311 tmpname = newname();
7312 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7313 if (e.error)
7314 kmem_free(tmpname, MAXNAMELEN);
7315 else {
7316 mutex_enter(&rp->r_statelock);
7317 if (rp->r_unldvp == NULL) {
7318 VN_HOLD(dvp);
7319 rp->r_unldvp = dvp;
7320 if (rp->r_unlcred != NULL)
7321 crfree(rp->r_unlcred);
7322 crhold(cr);
7323 rp->r_unlcred = cr;
7324 rp->r_unlname = tmpname;
7325 } else {
7326 kmem_free(rp->r_unlname, MAXNAMELEN);
7327 rp->r_unlname = tmpname;
7329 mutex_exit(&rp->r_statelock);
7331 VN_RELE(vp);
7332 nfs_rw_exit(&drp->r_rwlock);
7333 return (e.error);
7336 * Actually remove the file/dir
7338 mutex_exit(&rp->r_statelock);
7341 * We need to flush any dirty pages which happen to
7342 * be hanging around before removing the file.
7343 * This shouldn't happen very often since in NFSv4
7344 * we should be close to open consistent.
7346 if (nfs4_has_pages(vp) &&
7347 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7348 e.error = nfs4_putpage(vp, 0, 0, 0, cr, ct);
7349 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7350 mutex_enter(&rp->r_statelock);
7351 if (!rp->r_error)
7352 rp->r_error = e.error;
7353 mutex_exit(&rp->r_statelock);
7357 mi = VTOMI4(dvp);
7359 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7360 recov_state.rs_flags = 0;
7361 recov_state.rs_num_retry_despite_err = 0;
7363 recov_retry:
7365 * Remove ops: putfh dir; remove
7367 args.ctag = TAG_REMOVE;
7368 args.array_len = 3;
7369 args.array = argop;
7371 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7372 if (e.error) {
7373 nfs_rw_exit(&drp->r_rwlock);
7374 VN_RELE(vp);
7375 return (e.error);
7378 /* putfh directory */
7379 argop[0].argop = OP_CPUTFH;
7380 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7382 /* remove */
7383 argop[1].argop = OP_CREMOVE;
7384 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7386 /* getattr dir */
7387 argop[2].argop = OP_GETATTR;
7388 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7389 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7391 doqueue = 1;
7392 dinfo.di_time_call = gethrtime();
7393 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7395 PURGE_ATTRCACHE4(vp);
7397 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7398 if (e.error)
7399 PURGE_ATTRCACHE4(dvp);
7401 if (needrecov) {
7402 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7403 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7404 if (!e.error)
7405 (void) xdr_free(xdr_COMPOUND4res_clnt,
7406 (caddr_t)&res);
7407 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7408 needrecov);
7409 goto recov_retry;
7414 * Matching nfs4_end_op() for start_op() above.
7415 * There is a path in the code below which calls
7416 * nfs4_purge_stale_fh(), which may generate otw calls through
7417 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7418 * here to avoid nfs4_start_op() deadlock.
7420 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7422 if (!e.error) {
7423 resp = &res;
7425 if (res.status) {
7426 e.error = geterrno4(res.status);
7427 PURGE_ATTRCACHE4(dvp);
7428 nfs4_purge_stale_fh(e.error, dvp, cr);
7429 } else {
7430 resop = &res.array[1]; /* remove res */
7431 rm_res = &resop->nfs_resop4_u.opremove;
7433 dinfo.di_garp =
7434 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7435 dinfo.di_cred = cr;
7437 /* Update directory attr, readdir and dnlc caches */
7438 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7439 &dinfo);
7442 nfs_rw_exit(&drp->r_rwlock);
7443 if (resp)
7444 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7446 if (e.error == 0) {
7447 vnode_t *tvp;
7448 rnode4_t *trp;
7449 trp = VTOR4(vp);
7450 tvp = vp;
7451 if (IS_SHADOW(vp, trp))
7452 tvp = RTOV4(trp);
7453 vnevent_remove(tvp, dvp, nm, ct);
7455 VN_RELE(vp);
7456 return (e.error);
7460 * Link requires that the current fh be the target directory and the
7461 * saved fh be the source fh. After the operation, the current fh is unchanged.
7462 * Thus the compound op structure is:
7463 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7464 * GETATTR(file)
7466 /* ARGSUSED */
7467 static int
7468 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7469 caller_context_t *ct, int flags)
7471 COMPOUND4args_clnt args;
7472 COMPOUND4res_clnt res, *resp = NULL;
7473 LINK4res *ln_res;
7474 int argoplist_size = 7 * sizeof (nfs_argop4);
7475 nfs_argop4 *argop;
7476 nfs_resop4 *resop;
7477 vnode_t *realvp, *nvp;
7478 int doqueue;
7479 mntinfo4_t *mi;
7480 rnode4_t *tdrp;
7481 bool_t needrecov = FALSE;
7482 nfs4_recov_state_t recov_state;
7483 hrtime_t t;
7484 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7485 dirattr_info_t dinfo;
7487 ASSERT(*tnm != '\0');
7488 ASSERT(tdvp->v_type == VDIR);
7489 ASSERT(nfs4_consistent_type(tdvp));
7490 ASSERT(nfs4_consistent_type(svp));
7492 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7493 return (EPERM);
7494 if (fop_realvp(svp, &realvp, ct) == 0) {
7495 svp = realvp;
7496 ASSERT(nfs4_consistent_type(svp));
7499 tdrp = VTOR4(tdvp);
7500 mi = VTOMI4(svp);
7502 if (!(mi->mi_flags & MI4_LINK)) {
7503 return (EOPNOTSUPP);
7505 recov_state.rs_flags = 0;
7506 recov_state.rs_num_retry_despite_err = 0;
7508 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7509 return (EINTR);
7511 recov_retry:
7512 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7514 args.ctag = TAG_LINK;
7517 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7518 * restorefh; getattr(fl)
7520 args.array_len = 7;
7521 args.array = argop;
7523 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7524 if (e.error) {
7525 kmem_free(argop, argoplist_size);
7526 nfs_rw_exit(&tdrp->r_rwlock);
7527 return (e.error);
7530 /* 0. putfh file */
7531 argop[0].argop = OP_CPUTFH;
7532 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7534 /* 1. save current fh to free up the space for the dir */
7535 argop[1].argop = OP_SAVEFH;
7537 /* 2. putfh targetdir */
7538 argop[2].argop = OP_CPUTFH;
7539 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7541 /* 3. link: current_fh is targetdir, saved_fh is source */
7542 argop[3].argop = OP_CLINK;
7543 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7545 /* 4. Get attributes of dir */
7546 argop[4].argop = OP_GETATTR;
7547 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7548 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7550 /* 5. If link was successful, restore current vp to file */
7551 argop[5].argop = OP_RESTOREFH;
7553 /* 6. Get attributes of linked object */
7554 argop[6].argop = OP_GETATTR;
7555 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7556 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7558 dnlc_remove(tdvp, tnm);
7560 doqueue = 1;
7561 t = gethrtime();
7563 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7565 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7566 if (e.error != 0 && !needrecov) {
7567 PURGE_ATTRCACHE4(tdvp);
7568 PURGE_ATTRCACHE4(svp);
7569 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7570 goto out;
7573 if (needrecov) {
7574 bool_t abort;
7576 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7577 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7578 if (abort == FALSE) {
7579 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7580 needrecov);
7581 kmem_free(argop, argoplist_size);
7582 if (!e.error)
7583 (void) xdr_free(xdr_COMPOUND4res_clnt,
7584 (caddr_t)&res);
7585 goto recov_retry;
7586 } else {
7587 if (e.error != 0) {
7588 PURGE_ATTRCACHE4(tdvp);
7589 PURGE_ATTRCACHE4(svp);
7590 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7591 &recov_state, needrecov);
7592 goto out;
7594 /* fall through for res.status case */
7598 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7600 resp = &res;
7601 if (res.status) {
7602 /* If link succeeded, then don't return error */
7603 e.error = geterrno4(res.status);
7604 if (res.array_len <= 4) {
7606 * Either Putfh, Savefh, Putfh dir, or Link failed
7608 PURGE_ATTRCACHE4(svp);
7609 PURGE_ATTRCACHE4(tdvp);
7610 if (e.error == EOPNOTSUPP) {
7611 mutex_enter(&mi->mi_lock);
7612 mi->mi_flags &= ~MI4_LINK;
7613 mutex_exit(&mi->mi_lock);
7615 /* XXX-LP */
7616 if (e.error == EISDIR && crgetuid(cr) != 0)
7617 e.error = EPERM;
7618 goto out;
7622 /* either no error or one of the postop getattr failed */
7625 * XXX - if LINK succeeded, but no attrs were returned for link
7626 * file, purge its cache.
7628 * XXX Perform a simplified version of wcc checking. Instead of
7629 * have another getattr to get pre-op, just purge cache if
7630 * any of the ops prior to and including the getattr failed.
7631 * If the getattr succeeded then update the attrcache accordingly.
7635 * update cache with link file postattrs.
7636 * Note: at this point resop points to link res.
7638 resop = &res.array[3]; /* link res */
7639 ln_res = &resop->nfs_resop4_u.oplink;
7640 if (res.status == NFS4_OK)
7641 e.error = nfs4_update_attrcache(res.status,
7642 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7643 t, svp, cr);
7646 * Call makenfs4node to create the new shadow vp for tnm.
7647 * We pass NULL attrs because we just cached attrs for
7648 * the src object. All we're trying to accomplish is to
7649 * to create the new shadow vnode.
7651 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7652 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7654 /* Update target cache attribute, readdir and dnlc caches */
7655 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7656 dinfo.di_time_call = t;
7657 dinfo.di_cred = cr;
7659 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7660 ASSERT(nfs4_consistent_type(tdvp));
7661 ASSERT(nfs4_consistent_type(svp));
7662 ASSERT(nfs4_consistent_type(nvp));
7663 VN_RELE(nvp);
7665 if (!e.error) {
7666 vnode_t *tvp;
7667 rnode4_t *trp;
7669 * Notify the source file of this link operation.
7671 trp = VTOR4(svp);
7672 tvp = svp;
7673 if (IS_SHADOW(svp, trp))
7674 tvp = RTOV4(trp);
7675 vnevent_link(tvp, ct);
7677 out:
7678 kmem_free(argop, argoplist_size);
7679 if (resp)
7680 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7682 nfs_rw_exit(&tdrp->r_rwlock);
7684 return (e.error);
7687 /* ARGSUSED */
7688 static int
7689 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7690 caller_context_t *ct, int flags)
7692 vnode_t *realvp;
7694 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7695 return (EPERM);
7696 if (fop_realvp(ndvp, &realvp, ct) == 0)
7697 ndvp = realvp;
7699 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7703 * nfs4rename does the real work of renaming in NFS Version 4.
7705 * A file handle is considered volatile for renaming purposes if either
7706 * of the volatile bits are turned on. However, the compound may differ
7707 * based on the likelihood of the filehandle to change during rename.
7709 static int
7710 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7711 caller_context_t *ct)
7713 int error;
7714 mntinfo4_t *mi;
7715 vnode_t *nvp = NULL;
7716 vnode_t *ovp = NULL;
7717 char *tmpname = NULL;
7718 rnode4_t *rp;
7719 rnode4_t *odrp;
7720 rnode4_t *ndrp;
7721 int did_link = 0;
7722 int do_link = 1;
7723 nfsstat4 stat = NFS4_OK;
7725 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7726 ASSERT(nfs4_consistent_type(odvp));
7727 ASSERT(nfs4_consistent_type(ndvp));
7729 if (onm[0] == '.' && (onm[1] == '\0' ||
7730 (onm[1] == '.' && onm[2] == '\0')))
7731 return (EINVAL);
7733 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7734 (nnm[1] == '.' && nnm[2] == '\0')))
7735 return (EINVAL);
7737 odrp = VTOR4(odvp);
7738 ndrp = VTOR4(ndvp);
7739 if ((intptr_t)odrp < (intptr_t)ndrp) {
7740 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7741 return (EINTR);
7742 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7743 nfs_rw_exit(&odrp->r_rwlock);
7744 return (EINTR);
7746 } else {
7747 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7748 return (EINTR);
7749 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7750 nfs_rw_exit(&ndrp->r_rwlock);
7751 return (EINTR);
7756 * Lookup the target file. If it exists, it needs to be
7757 * checked to see whether it is a mount point and whether
7758 * it is active (open).
7760 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7761 if (!error) {
7762 int isactive;
7764 ASSERT(nfs4_consistent_type(nvp));
7766 * If this file has been mounted on, then just
7767 * return busy because renaming to it would remove
7768 * the mounted file system from the name space.
7770 if (vn_ismntpt(nvp)) {
7771 VN_RELE(nvp);
7772 nfs_rw_exit(&odrp->r_rwlock);
7773 nfs_rw_exit(&ndrp->r_rwlock);
7774 return (EBUSY);
7778 * First just remove the entry from the name cache, as it
7779 * is most likely the only entry for this vp.
7781 dnlc_remove(ndvp, nnm);
7783 rp = VTOR4(nvp);
7785 if (nvp->v_type != VREG) {
7787 * Purge the name cache of all references to this vnode
7788 * so that we can check the reference count to infer
7789 * whether it is active or not.
7791 if (nvp->v_count > 1)
7792 dnlc_purge_vp(nvp);
7794 isactive = nvp->v_count > 1;
7795 } else {
7796 mutex_enter(&rp->r_os_lock);
7797 isactive = list_head(&rp->r_open_streams) != NULL;
7798 mutex_exit(&rp->r_os_lock);
7802 * If the vnode is active and is not a directory,
7803 * arrange to rename it to a
7804 * temporary file so that it will continue to be
7805 * accessible. This implements the "unlink-open-file"
7806 * semantics for the target of a rename operation.
7807 * Before doing this though, make sure that the
7808 * source and target files are not already the same.
7810 if (isactive && nvp->v_type != VDIR) {
7812 * Lookup the source name.
7814 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7817 * The source name *should* already exist.
7819 if (error) {
7820 VN_RELE(nvp);
7821 nfs_rw_exit(&odrp->r_rwlock);
7822 nfs_rw_exit(&ndrp->r_rwlock);
7823 return (error);
7826 ASSERT(nfs4_consistent_type(ovp));
7829 * Compare the two vnodes. If they are the same,
7830 * just release all held vnodes and return success.
7832 if (VN_CMP(ovp, nvp)) {
7833 VN_RELE(ovp);
7834 VN_RELE(nvp);
7835 nfs_rw_exit(&odrp->r_rwlock);
7836 nfs_rw_exit(&ndrp->r_rwlock);
7837 return (0);
7841 * Can't mix and match directories and non-
7842 * directories in rename operations. We already
7843 * know that the target is not a directory. If
7844 * the source is a directory, return an error.
7846 if (ovp->v_type == VDIR) {
7847 VN_RELE(ovp);
7848 VN_RELE(nvp);
7849 nfs_rw_exit(&odrp->r_rwlock);
7850 nfs_rw_exit(&ndrp->r_rwlock);
7851 return (ENOTDIR);
7853 link_call:
7855 * The target file exists, is not the same as
7856 * the source file, and is active. We first
7857 * try to Link it to a temporary filename to
7858 * avoid having the server removing the file
7859 * completely (which could cause data loss to
7860 * the user's POV in the event the Rename fails
7861 * -- see bug 1165874).
7864 * The do_link and did_link booleans are
7865 * introduced in the event we get NFS4ERR_FILE_OPEN
7866 * returned for the Rename. Some servers can
7867 * not Rename over an Open file, so they return
7868 * this error. The client needs to Remove the
7869 * newly created Link and do two Renames, just
7870 * as if the server didn't support LINK.
7872 tmpname = newname();
7873 error = 0;
7875 if (do_link) {
7876 error = nfs4_link(ndvp, nvp, tmpname, cr,
7877 NULL, 0);
7879 if (error == EOPNOTSUPP || !do_link) {
7880 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7881 cr, NULL, 0);
7882 did_link = 0;
7883 } else {
7884 did_link = 1;
7886 if (error) {
7887 kmem_free(tmpname, MAXNAMELEN);
7888 VN_RELE(ovp);
7889 VN_RELE(nvp);
7890 nfs_rw_exit(&odrp->r_rwlock);
7891 nfs_rw_exit(&ndrp->r_rwlock);
7892 return (error);
7895 mutex_enter(&rp->r_statelock);
7896 if (rp->r_unldvp == NULL) {
7897 VN_HOLD(ndvp);
7898 rp->r_unldvp = ndvp;
7899 if (rp->r_unlcred != NULL)
7900 crfree(rp->r_unlcred);
7901 crhold(cr);
7902 rp->r_unlcred = cr;
7903 rp->r_unlname = tmpname;
7904 } else {
7905 if (rp->r_unlname)
7906 kmem_free(rp->r_unlname, MAXNAMELEN);
7907 rp->r_unlname = tmpname;
7909 mutex_exit(&rp->r_statelock);
7912 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7914 ASSERT(nfs4_consistent_type(nvp));
7917 if (ovp == NULL) {
7919 * When renaming directories to be a subdirectory of a
7920 * different parent, the dnlc entry for ".." will no
7921 * longer be valid, so it must be removed.
7923 * We do a lookup here to determine whether we are renaming
7924 * a directory and we need to check if we are renaming
7925 * an unlinked file. This might have already been done
7926 * in previous code, so we check ovp == NULL to avoid
7927 * doing it twice.
7929 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7931 * The source name *should* already exist.
7933 if (error) {
7934 nfs_rw_exit(&odrp->r_rwlock);
7935 nfs_rw_exit(&ndrp->r_rwlock);
7936 if (nvp) {
7937 VN_RELE(nvp);
7939 return (error);
7941 ASSERT(ovp != NULL);
7942 ASSERT(nfs4_consistent_type(ovp));
7946 * Is the object being renamed a dir, and if so, is
7947 * it being renamed to a child of itself? The underlying
7948 * fs should ultimately return EINVAL for this case;
7949 * however, buggy beta non-Solaris NFSv4 servers at
7950 * interop testing events have allowed this behavior,
7951 * and it caused our client to panic due to a recursive
7952 * mutex_enter in fn_move.
7954 * The tedious locking in fn_move could be changed to
7955 * deal with this case, and the client could avoid the
7956 * panic; however, the client would just confuse itself
7957 * later and misbehave. A better way to handle the broken
7958 * server is to detect this condition and return EINVAL
7959 * without ever sending the the bogus rename to the server.
7960 * We know the rename is invalid -- just fail it now.
7962 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7963 VN_RELE(ovp);
7964 nfs_rw_exit(&odrp->r_rwlock);
7965 nfs_rw_exit(&ndrp->r_rwlock);
7966 if (nvp) {
7967 VN_RELE(nvp);
7969 return (EINVAL);
7972 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7975 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7976 * possible for the filehandle to change due to the rename.
7977 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7978 * the fh will not change because of the rename, but we still need
7979 * to update its rnode entry with the new name for
7980 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7981 * has no effect on these for now, but for future improvements,
7982 * we might want to use it too to simplify handling of files
7983 * that are open with that flag on. (XXX)
7985 mi = VTOMI4(odvp);
7986 if (NFS4_VOLATILE_FH(mi))
7987 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7988 &stat);
7989 else
7990 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7991 &stat);
7993 ASSERT(nfs4_consistent_type(odvp));
7994 ASSERT(nfs4_consistent_type(ndvp));
7995 ASSERT(nfs4_consistent_type(ovp));
7997 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7998 do_link = 0;
8000 * Before the 'link_call' code, we did a nfs4_lookup
8001 * that puts a VN_HOLD on nvp. After the nfs4_link
8002 * call we call VN_RELE to match that hold. We need
8003 * to place an additional VN_HOLD here since we will
8004 * be hitting that VN_RELE again.
8006 VN_HOLD(nvp);
8008 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
8010 /* Undo the unlinked file naming stuff we just did */
8011 mutex_enter(&rp->r_statelock);
8012 if (rp->r_unldvp) {
8013 VN_RELE(ndvp);
8014 rp->r_unldvp = NULL;
8015 if (rp->r_unlcred != NULL)
8016 crfree(rp->r_unlcred);
8017 rp->r_unlcred = NULL;
8018 /* rp->r_unlanme points to tmpname */
8019 if (rp->r_unlname)
8020 kmem_free(rp->r_unlname, MAXNAMELEN);
8021 rp->r_unlname = NULL;
8023 mutex_exit(&rp->r_statelock);
8025 if (nvp) {
8026 VN_RELE(nvp);
8028 goto link_call;
8031 if (error) {
8032 VN_RELE(ovp);
8033 nfs_rw_exit(&odrp->r_rwlock);
8034 nfs_rw_exit(&ndrp->r_rwlock);
8035 if (nvp) {
8036 VN_RELE(nvp);
8038 return (error);
8042 * when renaming directories to be a subdirectory of a
8043 * different parent, the dnlc entry for ".." will no
8044 * longer be valid, so it must be removed
8046 rp = VTOR4(ovp);
8047 if (ndvp != odvp) {
8048 if (ovp->v_type == VDIR) {
8049 dnlc_remove(ovp, "..");
8050 if (rp->r_dir != NULL)
8051 nfs4_purge_rddir_cache(ovp);
8056 * If we are renaming the unlinked file, update the
8057 * r_unldvp and r_unlname as needed.
8059 mutex_enter(&rp->r_statelock);
8060 if (rp->r_unldvp != NULL) {
8061 if (strcmp(rp->r_unlname, onm) == 0) {
8062 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8063 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8064 if (ndvp != rp->r_unldvp) {
8065 VN_RELE(rp->r_unldvp);
8066 rp->r_unldvp = ndvp;
8067 VN_HOLD(ndvp);
8071 mutex_exit(&rp->r_statelock);
8074 * Notify the rename vnevents to source vnode, and to the target
8075 * vnode if it already existed.
8077 if (error == 0) {
8078 vnode_t *tvp;
8079 rnode4_t *trp;
8081 * Notify the vnode. Each links is represented by
8082 * a different vnode, in nfsv4.
8084 if (nvp) {
8085 trp = VTOR4(nvp);
8086 tvp = nvp;
8087 if (IS_SHADOW(nvp, trp))
8088 tvp = RTOV4(trp);
8089 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8093 * if the source and destination directory are not the
8094 * same notify the destination directory.
8096 if (VTOR4(odvp) != VTOR4(ndvp)) {
8097 trp = VTOR4(ndvp);
8098 tvp = ndvp;
8099 if (IS_SHADOW(ndvp, trp))
8100 tvp = RTOV4(trp);
8101 vnevent_rename_dest_dir(tvp, ct);
8104 trp = VTOR4(ovp);
8105 tvp = ovp;
8106 if (IS_SHADOW(ovp, trp))
8107 tvp = RTOV4(trp);
8108 vnevent_rename_src(tvp, odvp, onm, ct);
8111 if (nvp) {
8112 VN_RELE(nvp);
8114 VN_RELE(ovp);
8116 nfs_rw_exit(&odrp->r_rwlock);
8117 nfs_rw_exit(&ndrp->r_rwlock);
8119 return (error);
8123 * When the parent directory has changed, sv_dfh must be updated
8125 static void
8126 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8128 svnode_t *sv = VTOSV(vp);
8129 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8130 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8132 sfh4_hold(new_dfh);
8133 sv->sv_dfh = new_dfh;
8134 sfh4_rele(&old_dfh);
8138 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8139 * when it is known that the filehandle is persistent through rename.
8141 * Rename requires that the current fh be the target directory and the
8142 * saved fh be the source directory. After the operation, the current fh
8143 * is unchanged.
8144 * The compound op structure for persistent fh rename is:
8145 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8146 * Rather than bother with the directory postop args, we'll simply
8147 * update that a change occurred in the cache, so no post-op getattrs.
8149 static int
8150 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8151 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8153 COMPOUND4args_clnt args;
8154 COMPOUND4res_clnt res, *resp = NULL;
8155 nfs_argop4 *argop;
8156 nfs_resop4 *resop;
8157 int doqueue, argoplist_size;
8158 mntinfo4_t *mi;
8159 rnode4_t *odrp = VTOR4(odvp);
8160 rnode4_t *ndrp = VTOR4(ndvp);
8161 RENAME4res *rn_res;
8162 bool_t needrecov;
8163 nfs4_recov_state_t recov_state;
8164 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8165 dirattr_info_t dinfo, *dinfop;
8167 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8169 recov_state.rs_flags = 0;
8170 recov_state.rs_num_retry_despite_err = 0;
8173 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8175 * If source/target are different dirs, then append putfh(src); getattr
8177 args.array_len = (odvp == ndvp) ? 5 : 7;
8178 argoplist_size = args.array_len * sizeof (nfs_argop4);
8179 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8181 recov_retry:
8182 *statp = NFS4_OK;
8184 /* No need to Lookup the file, persistent fh */
8185 args.ctag = TAG_RENAME;
8187 mi = VTOMI4(odvp);
8188 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8189 if (e.error) {
8190 kmem_free(argop, argoplist_size);
8191 return (e.error);
8194 /* 0: putfh source directory */
8195 argop[0].argop = OP_CPUTFH;
8196 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8198 /* 1: Save source fh to free up current for target */
8199 argop[1].argop = OP_SAVEFH;
8201 /* 2: putfh targetdir */
8202 argop[2].argop = OP_CPUTFH;
8203 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8205 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8206 argop[3].argop = OP_CRENAME;
8207 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8208 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8210 /* 4: getattr (targetdir) */
8211 argop[4].argop = OP_GETATTR;
8212 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8213 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8215 if (ndvp != odvp) {
8217 /* 5: putfh (sourcedir) */
8218 argop[5].argop = OP_CPUTFH;
8219 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8221 /* 6: getattr (sourcedir) */
8222 argop[6].argop = OP_GETATTR;
8223 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8224 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8227 dnlc_remove(odvp, onm);
8228 dnlc_remove(ndvp, nnm);
8230 doqueue = 1;
8231 dinfo.di_time_call = gethrtime();
8232 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8234 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8235 if (e.error) {
8236 PURGE_ATTRCACHE4(odvp);
8237 PURGE_ATTRCACHE4(ndvp);
8238 } else {
8239 *statp = res.status;
8242 if (needrecov) {
8243 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8244 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8245 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8246 if (!e.error)
8247 (void) xdr_free(xdr_COMPOUND4res_clnt,
8248 (caddr_t)&res);
8249 goto recov_retry;
8253 if (!e.error) {
8254 resp = &res;
8256 * as long as OP_RENAME
8258 if (res.status != NFS4_OK && res.array_len <= 4) {
8259 e.error = geterrno4(res.status);
8260 PURGE_ATTRCACHE4(odvp);
8261 PURGE_ATTRCACHE4(ndvp);
8263 * System V defines rename to return EEXIST, not
8264 * ENOTEMPTY if the target directory is not empty.
8265 * Over the wire, the error is NFSERR_ENOTEMPTY
8266 * which geterrno4 maps to ENOTEMPTY.
8268 if (e.error == ENOTEMPTY)
8269 e.error = EEXIST;
8270 } else {
8272 resop = &res.array[3]; /* rename res */
8273 rn_res = &resop->nfs_resop4_u.oprename;
8275 if (res.status == NFS4_OK) {
8277 * Update target attribute, readdir and dnlc
8278 * caches.
8280 dinfo.di_garp =
8281 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8282 dinfo.di_cred = cr;
8283 dinfop = &dinfo;
8284 } else
8285 dinfop = NULL;
8287 nfs4_update_dircaches(&rn_res->target_cinfo,
8288 ndvp, NULL, NULL, dinfop);
8291 * Update source attribute, readdir and dnlc caches
8294 if (ndvp != odvp) {
8295 update_parentdir_sfh(renvp, ndvp);
8297 if (dinfop)
8298 dinfo.di_garp =
8299 &(res.array[6].nfs_resop4_u.
8300 opgetattr.ga_res);
8302 nfs4_update_dircaches(&rn_res->source_cinfo,
8303 odvp, NULL, NULL, dinfop);
8306 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8307 nnm);
8311 if (resp)
8312 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8313 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8314 kmem_free(argop, argoplist_size);
8316 return (e.error);
8320 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8321 * it is possible for the filehandle to change due to the rename.
8323 * The compound req in this case includes a post-rename lookup and getattr
8324 * to ensure that we have the correct fh and attributes for the object.
8326 * Rename requires that the current fh be the target directory and the
8327 * saved fh be the source directory. After the operation, the current fh
8328 * is unchanged.
8330 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8331 * update the filehandle for the renamed object. We also get the old
8332 * filehandle for historical reasons; this should be taken out sometime.
8333 * This results in a rather cumbersome compound...
8335 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8336 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8339 static int
8340 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8341 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8343 COMPOUND4args_clnt args;
8344 COMPOUND4res_clnt res, *resp = NULL;
8345 int argoplist_size;
8346 nfs_argop4 *argop;
8347 nfs_resop4 *resop;
8348 int doqueue;
8349 mntinfo4_t *mi;
8350 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8351 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8352 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8353 RENAME4res *rn_res;
8354 GETFH4res *ngf_res;
8355 bool_t needrecov;
8356 nfs4_recov_state_t recov_state;
8357 hrtime_t t;
8358 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8359 dirattr_info_t dinfo, *dinfop = &dinfo;
8361 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8363 recov_state.rs_flags = 0;
8364 recov_state.rs_num_retry_despite_err = 0;
8366 recov_retry:
8367 *statp = NFS4_OK;
8370 * There is a window between the RPC and updating the path and
8371 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8372 * code, so that it doesn't try to use the old path during that
8373 * window.
8375 mutex_enter(&orp->r_statelock);
8376 while (orp->r_flags & R4RECEXPFH) {
8377 klwp_t *lwp = ttolwp(curthread);
8379 if (lwp != NULL)
8380 lwp->lwp_nostop++;
8381 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8382 mutex_exit(&orp->r_statelock);
8383 if (lwp != NULL)
8384 lwp->lwp_nostop--;
8385 return (EINTR);
8387 if (lwp != NULL)
8388 lwp->lwp_nostop--;
8390 orp->r_flags |= R4RECEXPFH;
8391 mutex_exit(&orp->r_statelock);
8393 mi = VTOMI4(odvp);
8395 args.ctag = TAG_RENAME_VFH;
8396 args.array_len = (odvp == ndvp) ? 10 : 12;
8397 argoplist_size = args.array_len * sizeof (nfs_argop4);
8398 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8401 * Rename ops:
8402 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8403 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8404 * LOOKUP(trgt), GETFH(new), GETATTR,
8406 * if (odvp != ndvp)
8407 * add putfh(sourcedir), getattr(sourcedir) }
8409 args.array = argop;
8411 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8412 &recov_state, NULL);
8413 if (e.error) {
8414 kmem_free(argop, argoplist_size);
8415 mutex_enter(&orp->r_statelock);
8416 orp->r_flags &= ~R4RECEXPFH;
8417 cv_broadcast(&orp->r_cv);
8418 mutex_exit(&orp->r_statelock);
8419 return (e.error);
8422 /* 0: putfh source directory */
8423 argop[0].argop = OP_CPUTFH;
8424 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8426 /* 1: Save source fh to free up current for target */
8427 argop[1].argop = OP_SAVEFH;
8429 /* 2: Lookup pre-rename fh of renamed object */
8430 argop[2].argop = OP_CLOOKUP;
8431 argop[2].nfs_argop4_u.opclookup.cname = onm;
8433 /* 3: getfh fh of renamed object (before rename) */
8434 argop[3].argop = OP_GETFH;
8436 /* 4: putfh targetdir */
8437 argop[4].argop = OP_CPUTFH;
8438 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8440 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8441 argop[5].argop = OP_CRENAME;
8442 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8443 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8445 /* 6: getattr of target dir (post op attrs) */
8446 argop[6].argop = OP_GETATTR;
8447 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8448 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8450 /* 7: Lookup post-rename fh of renamed object */
8451 argop[7].argop = OP_CLOOKUP;
8452 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8454 /* 8: getfh fh of renamed object (after rename) */
8455 argop[8].argop = OP_GETFH;
8457 /* 9: getattr of renamed object */
8458 argop[9].argop = OP_GETATTR;
8459 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8460 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8463 * If source/target dirs are different, then get new post-op
8464 * attrs for source dir also.
8466 if (ndvp != odvp) {
8467 /* 10: putfh (sourcedir) */
8468 argop[10].argop = OP_CPUTFH;
8469 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8471 /* 11: getattr (sourcedir) */
8472 argop[11].argop = OP_GETATTR;
8473 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8474 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8477 dnlc_remove(odvp, onm);
8478 dnlc_remove(ndvp, nnm);
8480 doqueue = 1;
8481 t = gethrtime();
8482 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8484 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8485 if (e.error) {
8486 PURGE_ATTRCACHE4(odvp);
8487 PURGE_ATTRCACHE4(ndvp);
8488 if (!needrecov) {
8489 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8490 &recov_state, needrecov);
8491 goto out;
8493 } else {
8494 *statp = res.status;
8497 if (needrecov) {
8498 bool_t abort;
8500 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8501 OP_RENAME, NULL, NULL, NULL);
8502 if (abort == FALSE) {
8503 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8504 &recov_state, needrecov);
8505 kmem_free(argop, argoplist_size);
8506 if (!e.error)
8507 (void) xdr_free(xdr_COMPOUND4res_clnt,
8508 (caddr_t)&res);
8509 mutex_enter(&orp->r_statelock);
8510 orp->r_flags &= ~R4RECEXPFH;
8511 cv_broadcast(&orp->r_cv);
8512 mutex_exit(&orp->r_statelock);
8513 goto recov_retry;
8514 } else {
8515 if (e.error != 0) {
8516 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8517 &recov_state, needrecov);
8518 goto out;
8520 /* fall through for res.status case */
8524 resp = &res;
8526 * If OP_RENAME (or any prev op) failed, then return an error.
8527 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8529 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8531 * Error in an op other than last Getattr
8533 e.error = geterrno4(res.status);
8534 PURGE_ATTRCACHE4(odvp);
8535 PURGE_ATTRCACHE4(ndvp);
8537 * System V defines rename to return EEXIST, not
8538 * ENOTEMPTY if the target directory is not empty.
8539 * Over the wire, the error is NFSERR_ENOTEMPTY
8540 * which geterrno4 maps to ENOTEMPTY.
8542 if (e.error == ENOTEMPTY)
8543 e.error = EEXIST;
8544 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8545 needrecov);
8546 goto out;
8549 /* rename results */
8550 rn_res = &res.array[5].nfs_resop4_u.oprename;
8552 if (res.status == NFS4_OK) {
8553 /* Update target attribute, readdir and dnlc caches */
8554 dinfo.di_garp =
8555 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8556 dinfo.di_cred = cr;
8557 dinfo.di_time_call = t;
8558 } else
8559 dinfop = NULL;
8561 /* Update source cache attribute, readdir and dnlc caches */
8562 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8564 /* Update source cache attribute, readdir and dnlc caches */
8565 if (ndvp != odvp) {
8566 update_parentdir_sfh(ovp, ndvp);
8569 * If dinfop is non-NULL, then compound succeded, so
8570 * set di_garp to attrs for source dir. dinfop is only
8571 * set to NULL when compound fails.
8573 if (dinfop)
8574 dinfo.di_garp =
8575 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8576 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8577 dinfop);
8581 * Update the rnode with the new component name and args,
8582 * and if the file handle changed, also update it with the new fh.
8583 * This is only necessary if the target object has an rnode
8584 * entry and there is no need to create one for it.
8586 resop = &res.array[8]; /* getfh new res */
8587 ngf_res = &resop->nfs_resop4_u.opgetfh;
8590 * Update the path and filehandle for the renamed object.
8592 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8594 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8596 if (res.status == NFS4_OK) {
8597 resop++; /* getattr res */
8598 e.error = nfs4_update_attrcache(res.status,
8599 &resop->nfs_resop4_u.opgetattr.ga_res,
8600 t, ovp, cr);
8603 out:
8604 kmem_free(argop, argoplist_size);
8605 if (resp)
8606 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8607 mutex_enter(&orp->r_statelock);
8608 orp->r_flags &= ~R4RECEXPFH;
8609 cv_broadcast(&orp->r_cv);
8610 mutex_exit(&orp->r_statelock);
8612 return (e.error);
8615 /* ARGSUSED */
8616 static int
8617 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8618 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8620 int error;
8621 vnode_t *vp;
8623 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8624 return (EPERM);
8626 * As ".." has special meaning and rather than send a mkdir
8627 * over the wire to just let the server freak out, we just
8628 * short circuit it here and return EEXIST
8630 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8631 return (EEXIST);
8634 * Decision to get the right gid and setgid bit of the
8635 * new directory is now made in call_nfs4_create_req.
8637 va->va_mask |= AT_MODE;
8638 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8639 if (error)
8640 return (error);
8642 *vpp = vp;
8643 return (0);
8648 * rmdir is using the same remove v4 op as does remove.
8649 * Remove requires that the current fh be the target directory.
8650 * After the operation, the current fh is unchanged.
8651 * The compound op structure is:
8652 * PUTFH(targetdir), REMOVE
8654 /*ARGSUSED4*/
8655 static int
8656 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8657 caller_context_t *ct, int flags)
8659 int need_end_op = FALSE;
8660 COMPOUND4args_clnt args;
8661 COMPOUND4res_clnt res, *resp = NULL;
8662 REMOVE4res *rm_res;
8663 nfs_argop4 argop[3];
8664 nfs_resop4 *resop;
8665 vnode_t *vp;
8666 int doqueue;
8667 mntinfo4_t *mi;
8668 rnode4_t *drp;
8669 bool_t needrecov = FALSE;
8670 nfs4_recov_state_t recov_state;
8671 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8672 dirattr_info_t dinfo, *dinfop;
8674 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8675 return (EPERM);
8677 * As ".." has special meaning and rather than send a rmdir
8678 * over the wire to just let the server freak out, we just
8679 * short circuit it here and return EEXIST
8681 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8682 return (EEXIST);
8684 drp = VTOR4(dvp);
8685 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8686 return (EINTR);
8689 * Attempt to prevent a rmdir(".") from succeeding.
8691 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8692 if (e.error) {
8693 nfs_rw_exit(&drp->r_rwlock);
8694 return (e.error);
8696 if (vp == cdir) {
8697 VN_RELE(vp);
8698 nfs_rw_exit(&drp->r_rwlock);
8699 return (EINVAL);
8703 * Since nfsv4 remove op works on both files and directories,
8704 * check that the removed object is indeed a directory.
8706 if (vp->v_type != VDIR) {
8707 VN_RELE(vp);
8708 nfs_rw_exit(&drp->r_rwlock);
8709 return (ENOTDIR);
8713 * First just remove the entry from the name cache, as it
8714 * is most likely an entry for this vp.
8716 dnlc_remove(dvp, nm);
8719 * If there vnode reference count is greater than one, then
8720 * there may be additional references in the DNLC which will
8721 * need to be purged. First, trying removing the entry for
8722 * the parent directory and see if that removes the additional
8723 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8724 * to completely remove any references to the directory which
8725 * might still exist in the DNLC.
8727 if (vp->v_count > 1) {
8728 dnlc_remove(vp, "..");
8729 if (vp->v_count > 1)
8730 dnlc_purge_vp(vp);
8733 mi = VTOMI4(dvp);
8734 recov_state.rs_flags = 0;
8735 recov_state.rs_num_retry_despite_err = 0;
8737 recov_retry:
8738 args.ctag = TAG_RMDIR;
8741 * Rmdir ops: putfh dir; remove
8743 args.array_len = 3;
8744 args.array = argop;
8746 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8747 if (e.error) {
8748 nfs_rw_exit(&drp->r_rwlock);
8749 return (e.error);
8751 need_end_op = TRUE;
8753 /* putfh directory */
8754 argop[0].argop = OP_CPUTFH;
8755 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8757 /* remove */
8758 argop[1].argop = OP_CREMOVE;
8759 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8761 /* getattr (postop attrs for dir that contained removed dir) */
8762 argop[2].argop = OP_GETATTR;
8763 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8764 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8766 dinfo.di_time_call = gethrtime();
8767 doqueue = 1;
8768 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8770 PURGE_ATTRCACHE4(vp);
8772 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8773 if (e.error) {
8774 PURGE_ATTRCACHE4(dvp);
8777 if (needrecov) {
8778 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8779 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8780 if (!e.error)
8781 (void) xdr_free(xdr_COMPOUND4res_clnt,
8782 (caddr_t)&res);
8784 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8785 needrecov);
8786 need_end_op = FALSE;
8787 goto recov_retry;
8791 if (!e.error) {
8792 resp = &res;
8795 * Only return error if first 2 ops (OP_REMOVE or earlier)
8796 * failed.
8798 if (res.status != NFS4_OK && res.array_len <= 2) {
8799 e.error = geterrno4(res.status);
8800 PURGE_ATTRCACHE4(dvp);
8801 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8802 &recov_state, needrecov);
8803 need_end_op = FALSE;
8804 nfs4_purge_stale_fh(e.error, dvp, cr);
8806 * System V defines rmdir to return EEXIST, not
8807 * ENOTEMPTY if the directory is not empty. Over
8808 * the wire, the error is NFSERR_ENOTEMPTY which
8809 * geterrno4 maps to ENOTEMPTY.
8811 if (e.error == ENOTEMPTY)
8812 e.error = EEXIST;
8813 } else {
8814 resop = &res.array[1]; /* remove res */
8815 rm_res = &resop->nfs_resop4_u.opremove;
8817 if (res.status == NFS4_OK) {
8818 resop = &res.array[2]; /* dir attrs */
8819 dinfo.di_garp =
8820 &resop->nfs_resop4_u.opgetattr.ga_res;
8821 dinfo.di_cred = cr;
8822 dinfop = &dinfo;
8823 } else
8824 dinfop = NULL;
8826 /* Update dir attribute, readdir and dnlc caches */
8827 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8828 dinfop);
8830 /* destroy rddir cache for dir that was removed */
8831 if (VTOR4(vp)->r_dir != NULL)
8832 nfs4_purge_rddir_cache(vp);
8836 if (need_end_op)
8837 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8839 nfs_rw_exit(&drp->r_rwlock);
8841 if (resp)
8842 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8844 if (e.error == 0) {
8845 vnode_t *tvp;
8846 rnode4_t *trp;
8847 trp = VTOR4(vp);
8848 tvp = vp;
8849 if (IS_SHADOW(vp, trp))
8850 tvp = RTOV4(trp);
8851 vnevent_rmdir(tvp, dvp, nm, ct);
8854 VN_RELE(vp);
8856 return (e.error);
8859 /* ARGSUSED */
8860 static int
8861 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8862 caller_context_t *ct, int flags)
8864 int error;
8865 vnode_t *vp;
8866 rnode4_t *rp;
8867 char *contents;
8868 mntinfo4_t *mi = VTOMI4(dvp);
8870 if (nfs_zone() != mi->mi_zone)
8871 return (EPERM);
8872 if (!(mi->mi_flags & MI4_SYMLINK))
8873 return (EOPNOTSUPP);
8875 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8876 if (error)
8877 return (error);
8879 ASSERT(nfs4_consistent_type(vp));
8880 rp = VTOR4(vp);
8881 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8883 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8885 if (contents != NULL) {
8886 mutex_enter(&rp->r_statelock);
8887 if (rp->r_symlink.contents == NULL) {
8888 rp->r_symlink.len = strlen(tnm);
8889 bcopy(tnm, contents, rp->r_symlink.len);
8890 rp->r_symlink.contents = contents;
8891 rp->r_symlink.size = MAXPATHLEN;
8892 mutex_exit(&rp->r_statelock);
8893 } else {
8894 mutex_exit(&rp->r_statelock);
8895 kmem_free((void *)contents, MAXPATHLEN);
8899 VN_RELE(vp);
8901 return (error);
8906 * Read directory entries.
8907 * There are some weird things to look out for here. The uio_loffset
8908 * field is either 0 or it is the offset returned from a previous
8909 * readdir. It is an opaque value used by the server to find the
8910 * correct directory block to read. The count field is the number
8911 * of blocks to read on the server. This is advisory only, the server
8912 * may return only one block's worth of entries. Entries may be compressed
8913 * on the server.
8915 /* ARGSUSED */
8916 static int
8917 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8918 caller_context_t *ct, int flags)
8920 int error;
8921 uint_t count;
8922 rnode4_t *rp;
8923 rddir4_cache *rdc;
8924 rddir4_cache *rrdc;
8926 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8927 return (EIO);
8928 rp = VTOR4(vp);
8930 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8933 * Make sure that the directory cache is valid.
8935 if (rp->r_dir != NULL) {
8936 if (nfs_disable_rddir_cache != 0) {
8938 * Setting nfs_disable_rddir_cache in /etc/system
8939 * allows interoperability with servers that do not
8940 * properly update the attributes of directories.
8941 * Any cached information gets purged before an
8942 * access is made to it.
8944 nfs4_purge_rddir_cache(vp);
8947 error = nfs4_validate_caches(vp, cr);
8948 if (error)
8949 return (error);
8952 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8955 * Short circuit last readdir which always returns 0 bytes.
8956 * This can be done after the directory has been read through
8957 * completely at least once. This will set r_direof which
8958 * can be used to find the value of the last cookie.
8960 mutex_enter(&rp->r_statelock);
8961 if (rp->r_direof != NULL &&
8962 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8963 mutex_exit(&rp->r_statelock);
8964 #ifdef DEBUG
8965 nfs4_readdir_cache_shorts++;
8966 #endif
8967 if (eofp)
8968 *eofp = 1;
8969 return (0);
8973 * Look for a cache entry. Cache entries are identified
8974 * by the NFS cookie value and the byte count requested.
8976 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8979 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8981 if (rdc == NULL) {
8982 mutex_exit(&rp->r_statelock);
8983 return (EINTR);
8987 * Check to see if we need to fill this entry in.
8989 if (rdc->flags & RDDIRREQ) {
8990 rdc->flags &= ~RDDIRREQ;
8991 rdc->flags |= RDDIR;
8992 mutex_exit(&rp->r_statelock);
8995 * Do the readdir.
8997 nfs4readdir(vp, rdc, cr);
9000 * Reacquire the lock, so that we can continue
9002 mutex_enter(&rp->r_statelock);
9004 * The entry is now complete
9006 rdc->flags &= ~RDDIR;
9009 ASSERT(!(rdc->flags & RDDIR));
9012 * If an error occurred while attempting
9013 * to fill the cache entry, mark the entry invalid and
9014 * just return the error.
9016 if (rdc->error) {
9017 error = rdc->error;
9018 rdc->flags |= RDDIRREQ;
9019 rddir4_cache_rele(rp, rdc);
9020 mutex_exit(&rp->r_statelock);
9021 return (error);
9025 * The cache entry is complete and good,
9026 * copyout the dirent structs to the calling
9027 * thread.
9029 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9032 * If no error occurred during the copyout,
9033 * update the offset in the uio struct to
9034 * contain the value of the next NFS 4 cookie
9035 * and set the eof value appropriately.
9037 if (!error) {
9038 uiop->uio_loffset = rdc->nfs4_ncookie;
9039 if (eofp)
9040 *eofp = rdc->eof;
9044 * Decide whether to do readahead. Don't if we
9045 * have already read to the end of directory.
9047 if (rdc->eof) {
9049 * Make the entry the direof only if it is cached
9051 if (rdc->flags & RDDIRCACHED)
9052 rp->r_direof = rdc;
9053 rddir4_cache_rele(rp, rdc);
9054 mutex_exit(&rp->r_statelock);
9055 return (error);
9058 /* Determine if a readdir readahead should be done */
9059 if (!(rp->r_flags & R4LOOKUP)) {
9060 rddir4_cache_rele(rp, rdc);
9061 mutex_exit(&rp->r_statelock);
9062 return (error);
9066 * Now look for a readahead entry.
9068 * Check to see whether we found an entry for the readahead.
9069 * If so, we don't need to do anything further, so free the new
9070 * entry if one was allocated. Otherwise, allocate a new entry, add
9071 * it to the cache, and then initiate an asynchronous readdir
9072 * operation to fill it.
9074 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9077 * A readdir cache entry could not be obtained for the readahead. In
9078 * this case we skip the readahead and return.
9080 if (rrdc == NULL) {
9081 rddir4_cache_rele(rp, rdc);
9082 mutex_exit(&rp->r_statelock);
9083 return (error);
9087 * Check to see if we need to fill this entry in.
9089 if (rrdc->flags & RDDIRREQ) {
9090 rrdc->flags &= ~RDDIRREQ;
9091 rrdc->flags |= RDDIR;
9092 rddir4_cache_rele(rp, rdc);
9093 mutex_exit(&rp->r_statelock);
9094 #ifdef DEBUG
9095 nfs4_readdir_readahead++;
9096 #endif
9098 * Do the readdir.
9100 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9101 return (error);
9104 rddir4_cache_rele(rp, rrdc);
9105 rddir4_cache_rele(rp, rdc);
9106 mutex_exit(&rp->r_statelock);
9107 return (error);
9110 static int
9111 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9113 int error;
9114 rnode4_t *rp;
9116 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9118 rp = VTOR4(vp);
9121 * Obtain the readdir results for the caller.
9123 nfs4readdir(vp, rdc, cr);
9125 mutex_enter(&rp->r_statelock);
9127 * The entry is now complete
9129 rdc->flags &= ~RDDIR;
9131 error = rdc->error;
9132 if (error)
9133 rdc->flags |= RDDIRREQ;
9134 rddir4_cache_rele(rp, rdc);
9135 mutex_exit(&rp->r_statelock);
9137 return (error);
9141 * Read directory entries.
9142 * There are some weird things to look out for here. The uio_loffset
9143 * field is either 0 or it is the offset returned from a previous
9144 * readdir. It is an opaque value used by the server to find the
9145 * correct directory block to read. The count field is the number
9146 * of blocks to read on the server. This is advisory only, the server
9147 * may return only one block's worth of entries. Entries may be compressed
9148 * on the server.
9150 * Generates the following compound request:
9151 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9152 * must include a Lookupp as well. In this case, send:
9153 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9154 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9156 * Get complete attributes and filehandles for entries if this is the
9157 * first read of the directory. Otherwise, just get fileid's.
9159 static void
9160 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9162 COMPOUND4args_clnt args;
9163 COMPOUND4res_clnt res;
9164 READDIR4args *rargs;
9165 READDIR4res_clnt *rd_res;
9166 bitmap4 rd_bitsval;
9167 nfs_argop4 argop[5];
9168 nfs_resop4 *resop;
9169 rnode4_t *rp = VTOR4(vp);
9170 mntinfo4_t *mi = VTOMI4(vp);
9171 int doqueue;
9172 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9173 vnode_t *dvp;
9174 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9175 int num_ops, res_opcnt;
9176 bool_t needrecov = FALSE;
9177 nfs4_recov_state_t recov_state;
9178 hrtime_t t;
9179 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9181 ASSERT(nfs_zone() == mi->mi_zone);
9182 ASSERT(rdc->flags & RDDIR);
9183 ASSERT(rdc->entries == NULL);
9186 * If rp were a stub, it should have triggered and caused
9187 * a mount for us to get this far.
9189 ASSERT(!RP_ISSTUB(rp));
9191 num_ops = 2;
9192 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9194 * Since nfsv4 readdir may not return entries for "." and "..",
9195 * the client must recreate them:
9196 * To find the correct nodeid, do the following:
9197 * For current node, get nodeid from dnlc.
9198 * - if current node is rootvp, set pnodeid to nodeid.
9199 * - else if parent is in the dnlc, get its nodeid from there.
9200 * - else add LOOKUPP+GETATTR to compound.
9202 nodeid = rp->r_attr.va_nodeid;
9203 if (vp->v_flag & VROOT) {
9204 pnodeid = nodeid; /* root of mount point */
9205 } else {
9206 dvp = dnlc_lookup(vp, "..");
9207 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9208 /* parent in dnlc cache - no need for otw */
9209 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9210 } else {
9212 * parent not in dnlc cache,
9213 * do lookupp to get its id
9215 num_ops = 5;
9216 pnodeid = 0; /* set later by getattr parent */
9218 if (dvp)
9219 VN_RELE(dvp);
9222 recov_state.rs_flags = 0;
9223 recov_state.rs_num_retry_despite_err = 0;
9225 /* Save the original mount point security flavor */
9226 (void) save_mnt_secinfo(mi->mi_curr_serv);
9228 recov_retry:
9229 args.ctag = TAG_READDIR;
9231 args.array = argop;
9232 args.array_len = num_ops;
9234 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9235 &recov_state, NULL)) {
9237 * If readdir a node that is a stub for a crossed mount point,
9238 * keep the original secinfo flavor for the current file
9239 * system, not the crossed one.
9241 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9242 rdc->error = e.error;
9243 return;
9247 * Determine which attrs to request for dirents. This code
9248 * must be protected by nfs4_start/end_fop because of r_server
9249 * (which will change during failover recovery).
9252 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9254 * Get all vattr attrs plus filehandle and rdattr_error
9256 rd_bitsval = NFS4_VATTR_MASK |
9257 FATTR4_RDATTR_ERROR_MASK |
9258 FATTR4_FILEHANDLE_MASK;
9260 if (rp->r_flags & R4READDIRWATTR) {
9261 mutex_enter(&rp->r_statelock);
9262 rp->r_flags &= ~R4READDIRWATTR;
9263 mutex_exit(&rp->r_statelock);
9265 } else {
9266 servinfo4_t *svp = rp->r_server;
9269 * Already read directory. Use readdir with
9270 * no attrs (except for mounted_on_fileid) for updates.
9272 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9275 * request mounted on fileid if supported, else request
9276 * fileid. maybe we should verify that fileid is supported
9277 * and request something else if not.
9279 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9280 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9281 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9282 nfs_rw_exit(&svp->sv_lock);
9285 /* putfh directory fh */
9286 argop[0].argop = OP_CPUTFH;
9287 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9289 argop[1].argop = OP_READDIR;
9290 rargs = &argop[1].nfs_argop4_u.opreaddir;
9292 * 1 and 2 are reserved for client "." and ".." entry offset.
9293 * cookie 0 should be used over-the-wire to start reading at
9294 * the beginning of the directory excluding "." and "..".
9296 if (rdc->nfs4_cookie == 0 ||
9297 rdc->nfs4_cookie == 1 ||
9298 rdc->nfs4_cookie == 2) {
9299 rargs->cookie = (nfs_cookie4)0;
9300 rargs->cookieverf = 0;
9301 } else {
9302 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9303 mutex_enter(&rp->r_statelock);
9304 rargs->cookieverf = rp->r_cookieverf4;
9305 mutex_exit(&rp->r_statelock);
9307 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9308 rargs->maxcount = mi->mi_tsize;
9309 rargs->attr_request = rd_bitsval;
9310 rargs->rdc = rdc;
9311 rargs->dvp = vp;
9312 rargs->mi = mi;
9313 rargs->cr = cr;
9317 * If count < than the minimum required, we return no entries
9318 * and fail with EINVAL
9320 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9321 rdc->error = EINVAL;
9322 goto out;
9325 if (args.array_len == 5) {
9327 * Add lookupp and getattr for parent nodeid.
9329 argop[2].argop = OP_LOOKUPP;
9331 argop[3].argop = OP_GETFH;
9333 /* getattr parent */
9334 argop[4].argop = OP_GETATTR;
9335 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9336 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9339 doqueue = 1;
9341 if (mi->mi_io_kstats) {
9342 mutex_enter(&mi->mi_lock);
9343 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9344 mutex_exit(&mi->mi_lock);
9347 /* capture the time of this call */
9348 rargs->t = t = gethrtime();
9350 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9352 if (mi->mi_io_kstats) {
9353 mutex_enter(&mi->mi_lock);
9354 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9355 mutex_exit(&mi->mi_lock);
9358 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9361 * If RPC error occurred and it isn't an error that
9362 * triggers recovery, then go ahead and fail now.
9364 if (e.error != 0 && !needrecov) {
9365 rdc->error = e.error;
9366 goto out;
9369 if (needrecov) {
9370 bool_t abort;
9372 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9373 "nfs4readdir: initiating recovery.\n"));
9375 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9376 NULL, OP_READDIR, NULL, NULL, NULL);
9377 if (abort == FALSE) {
9378 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9379 &recov_state, needrecov);
9380 if (!e.error)
9381 (void) xdr_free(xdr_COMPOUND4res_clnt,
9382 (caddr_t)&res);
9383 if (rdc->entries != NULL) {
9384 kmem_free(rdc->entries, rdc->entlen);
9385 rdc->entries = NULL;
9387 goto recov_retry;
9390 if (e.error != 0) {
9391 rdc->error = e.error;
9392 goto out;
9395 /* fall through for res.status case */
9398 res_opcnt = res.array_len;
9401 * If compound failed first 2 ops (PUTFH+READDIR), then return
9402 * failure here. Subsequent ops are for filling out dot-dot
9403 * dirent, and if they fail, we still want to give the caller
9404 * the dirents returned by (the successful) READDIR op, so we need
9405 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9407 * One example where PUTFH+READDIR ops would succeed but
9408 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9409 * but lacks x. In this case, a POSIX server's fop_readdir
9410 * would succeed; however, fop_lookup(..) would fail since no
9411 * x perm. We need to come up with a non-vendor-specific way
9412 * for a POSIX server to return d_ino from dotdot's dirent if
9413 * client only requests mounted_on_fileid, and just say the
9414 * LOOKUPP succeeded and fill out the GETATTR. However, if
9415 * client requested any mandatory attrs, server would be required
9416 * to fail the GETATTR op because it can't call fop_lookup+fop_getattr
9417 * for dotdot.
9420 if (res.status) {
9421 if (res_opcnt <= 2) {
9422 e.error = geterrno4(res.status);
9423 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9424 &recov_state, needrecov);
9425 nfs4_purge_stale_fh(e.error, vp, cr);
9426 rdc->error = e.error;
9427 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9428 if (rdc->entries != NULL) {
9429 kmem_free(rdc->entries, rdc->entlen);
9430 rdc->entries = NULL;
9433 * If readdir a node that is a stub for a
9434 * crossed mount point, keep the original
9435 * secinfo flavor for the current file system,
9436 * not the crossed one.
9438 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9439 return;
9443 resop = &res.array[1]; /* readdir res */
9444 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9446 mutex_enter(&rp->r_statelock);
9447 rp->r_cookieverf4 = rd_res->cookieverf;
9448 mutex_exit(&rp->r_statelock);
9451 * For "." and ".." entries
9452 * e.g.
9453 * seek(cookie=0) -> "." entry with d_off = 1
9454 * seek(cookie=1) -> ".." entry with d_off = 2
9456 if (cookie == (nfs_cookie4) 0) {
9457 if (rd_res->dotp)
9458 rd_res->dotp->d_ino = nodeid;
9459 if (rd_res->dotdotp)
9460 rd_res->dotdotp->d_ino = pnodeid;
9462 if (cookie == (nfs_cookie4) 1) {
9463 if (rd_res->dotdotp)
9464 rd_res->dotdotp->d_ino = pnodeid;
9468 /* LOOKUPP+GETATTR attemped */
9469 if (args.array_len == 5 && rd_res->dotdotp) {
9470 if (res.status == NFS4_OK && res_opcnt == 5) {
9471 nfs_fh4 *fhp;
9472 nfs4_sharedfh_t *sfhp;
9473 vnode_t *pvp;
9474 nfs4_ga_res_t *garp;
9476 resop++; /* lookupp */
9477 resop++; /* getfh */
9478 fhp = &resop->nfs_resop4_u.opgetfh.object;
9480 resop++; /* getattr of parent */
9483 * First, take care of finishing the
9484 * readdir results.
9486 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9488 * The d_ino of .. must be the inode number
9489 * of the mounted filesystem.
9491 if (garp->n4g_va.va_mask & AT_NODEID)
9492 rd_res->dotdotp->d_ino =
9493 garp->n4g_va.va_nodeid;
9497 * Next, create the ".." dnlc entry
9499 sfhp = sfh4_get(fhp, mi);
9500 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9501 dnlc_update(vp, "..", pvp);
9502 VN_RELE(pvp);
9504 sfh4_rele(&sfhp);
9508 if (mi->mi_io_kstats) {
9509 mutex_enter(&mi->mi_lock);
9510 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9511 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9512 mutex_exit(&mi->mi_lock);
9515 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9517 out:
9519 * If readdir a node that is a stub for a crossed mount point,
9520 * keep the original secinfo flavor for the current file system,
9521 * not the crossed one.
9523 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9525 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9529 static int
9530 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9532 rnode4_t *rp = VTOR4(bp->b_vp);
9533 int count;
9534 int error;
9535 cred_t *cred_otw = NULL;
9536 offset_t offset;
9537 nfs4_open_stream_t *osp = NULL;
9538 bool_t first_time = TRUE; /* first time getting otw cred */
9539 bool_t last_time = FALSE; /* last time getting otw cred */
9541 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9543 DTRACE_IO1(start, struct buf *, bp);
9544 offset = ldbtob(bp->b_lblkno);
9546 if (bp->b_flags & B_READ) {
9547 read_again:
9549 * Releases the osp, if it is provided.
9550 * Puts a hold on the cred_otw and the new osp (if found).
9552 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9553 &first_time, &last_time);
9554 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9555 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9556 readahead, NULL);
9557 crfree(cred_otw);
9558 if (!error) {
9559 if (bp->b_resid) {
9561 * Didn't get it all because we hit EOF,
9562 * zero all the memory beyond the EOF.
9564 /* bzero(rdaddr + */
9565 bzero(bp->b_un.b_addr +
9566 bp->b_bcount - bp->b_resid, bp->b_resid);
9568 mutex_enter(&rp->r_statelock);
9569 if (bp->b_resid == bp->b_bcount &&
9570 offset >= rp->r_size) {
9572 * We didn't read anything at all as we are
9573 * past EOF. Return an error indicator back
9574 * but don't destroy the pages (yet).
9576 error = NFS_EOF;
9578 mutex_exit(&rp->r_statelock);
9579 } else if (error == EACCES && last_time == FALSE) {
9580 goto read_again;
9582 } else {
9583 if (!(rp->r_flags & R4STALE)) {
9584 write_again:
9586 * Releases the osp, if it is provided.
9587 * Puts a hold on the cred_otw and the new
9588 * osp (if found).
9590 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9591 &first_time, &last_time);
9592 mutex_enter(&rp->r_statelock);
9593 count = MIN(bp->b_bcount, rp->r_size - offset);
9594 mutex_exit(&rp->r_statelock);
9595 if (count < 0)
9596 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9597 #ifdef DEBUG
9598 if (count == 0) {
9599 zoneid_t zoneid = getzoneid();
9601 zcmn_err(zoneid, CE_WARN,
9602 "nfs4_bio: zero length write at %lld",
9603 offset);
9604 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9605 "b_bcount=%ld, file size=%lld",
9606 rp->r_flags, (long)bp->b_bcount,
9607 rp->r_size);
9608 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9609 if (nfs4_bio_do_stop)
9610 debug_enter("nfs4_bio");
9612 #endif
9613 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9614 count, cred_otw, stab_comm);
9615 if (error == EACCES && last_time == FALSE) {
9616 crfree(cred_otw);
9617 goto write_again;
9619 bp->b_error = error;
9620 if (error && error != EINTR &&
9621 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9623 * Don't print EDQUOT errors on the console.
9624 * Don't print asynchronous EACCES errors.
9625 * Don't print EFBIG errors.
9626 * Print all other write errors.
9628 if (error != EDQUOT && error != EFBIG &&
9629 (error != EACCES ||
9630 !(bp->b_flags & B_ASYNC)))
9631 nfs4_write_error(bp->b_vp,
9632 error, cred_otw);
9634 * Update r_error and r_flags as appropriate.
9635 * If the error was ESTALE, then mark the
9636 * rnode as not being writeable and save
9637 * the error status. Otherwise, save any
9638 * errors which occur from asynchronous
9639 * page invalidations. Any errors occurring
9640 * from other operations should be saved
9641 * by the caller.
9643 mutex_enter(&rp->r_statelock);
9644 if (error == ESTALE) {
9645 rp->r_flags |= R4STALE;
9646 if (!rp->r_error)
9647 rp->r_error = error;
9648 } else if (!rp->r_error &&
9649 (bp->b_flags &
9650 (B_INVAL|B_FORCE|B_ASYNC)) ==
9651 (B_INVAL|B_FORCE|B_ASYNC)) {
9652 rp->r_error = error;
9654 mutex_exit(&rp->r_statelock);
9656 crfree(cred_otw);
9657 } else {
9658 error = rp->r_error;
9660 * A close may have cleared r_error, if so,
9661 * propagate ESTALE error return properly
9663 if (error == 0)
9664 error = ESTALE;
9668 if (error != 0 && error != NFS_EOF)
9669 bp->b_flags |= B_ERROR;
9671 if (osp)
9672 open_stream_rele(osp, rp);
9674 DTRACE_IO1(done, struct buf *, bp);
9676 return (error);
9679 /* ARGSUSED */
9681 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9683 return (EREMOTE);
9686 /* ARGSUSED2 */
9688 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9690 rnode4_t *rp = VTOR4(vp);
9692 if (!write_lock) {
9693 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9694 return (V_WRITELOCK_FALSE);
9697 if ((rp->r_flags & R4DIRECTIO) ||
9698 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9699 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9700 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9701 return (V_WRITELOCK_FALSE);
9702 nfs_rw_exit(&rp->r_rwlock);
9705 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9706 return (V_WRITELOCK_TRUE);
9709 /* ARGSUSED */
9710 void
9711 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9713 rnode4_t *rp = VTOR4(vp);
9715 nfs_rw_exit(&rp->r_rwlock);
9718 /* ARGSUSED */
9719 static int
9720 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9722 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9723 return (EIO);
9726 * Because we stuff the readdir cookie into the offset field
9727 * someone may attempt to do an lseek with the cookie which
9728 * we want to succeed.
9730 if (vp->v_type == VDIR)
9731 return (0);
9732 if (*noffp < 0)
9733 return (EINVAL);
9734 return (0);
9739 * Return all the pages from [off..off+len) in file
9741 /* ARGSUSED */
9742 static int
9743 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9744 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9745 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9747 rnode4_t *rp;
9748 int error;
9749 mntinfo4_t *mi;
9751 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9752 return (EIO);
9753 rp = VTOR4(vp);
9754 if (IS_SHADOW(vp, rp))
9755 vp = RTOV4(rp);
9757 if (vp->v_flag & VNOMAP)
9758 return (ENOSYS);
9760 if (protp != NULL)
9761 *protp = PROT_ALL;
9764 * Now validate that the caches are up to date.
9766 if (error = nfs4_validate_caches(vp, cr))
9767 return (error);
9769 mi = VTOMI4(vp);
9770 retry:
9771 mutex_enter(&rp->r_statelock);
9774 * Don't create dirty pages faster than they
9775 * can be cleaned so that the system doesn't
9776 * get imbalanced. If the async queue is
9777 * maxed out, then wait for it to drain before
9778 * creating more dirty pages. Also, wait for
9779 * any threads doing pagewalks in the vop_getattr
9780 * entry points so that they don't block for
9781 * long periods.
9783 if (rw == S_CREATE) {
9784 while ((mi->mi_max_threads != 0 &&
9785 rp->r_awcount > 2 * mi->mi_max_threads) ||
9786 rp->r_gcount > 0)
9787 cv_wait(&rp->r_cv, &rp->r_statelock);
9791 * If we are getting called as a side effect of an nfs_write()
9792 * operation the local file size might not be extended yet.
9793 * In this case we want to be able to return pages of zeroes.
9795 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9796 NFS4_DEBUG(nfs4_pageio_debug,
9797 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9798 "len=%llu, size=%llu, attrsize =%llu", off,
9799 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9800 mutex_exit(&rp->r_statelock);
9801 return (EFAULT); /* beyond EOF */
9804 mutex_exit(&rp->r_statelock);
9806 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9807 pl, plsz, seg, addr, rw, cr);
9808 NFS4_DEBUG(nfs4_pageio_debug && error,
9809 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9810 error, off, (u_longlong_t)len));
9812 switch (error) {
9813 case NFS_EOF:
9814 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9815 goto retry;
9816 case ESTALE:
9817 nfs4_purge_stale_fh(error, vp, cr);
9820 return (error);
9824 * Called from pvn_getpages to get a particular page.
9826 /* ARGSUSED */
9827 static int
9828 nfs4_getapage(vnode_t *vp, uoff_t off, size_t len, uint_t *protp,
9829 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9830 enum seg_rw rw, cred_t *cr)
9832 rnode4_t *rp;
9833 uint_t bsize;
9834 struct buf *bp;
9835 page_t *pp;
9836 uoff_t lbn;
9837 uoff_t io_off;
9838 uoff_t blkoff;
9839 uoff_t rablkoff;
9840 size_t io_len;
9841 uint_t blksize;
9842 int error;
9843 int readahead;
9844 int readahead_issued = 0;
9845 int ra_window; /* readahead window */
9846 page_t *pagefound;
9847 page_t *savepp;
9849 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9850 return (EIO);
9852 rp = VTOR4(vp);
9853 ASSERT(!IS_SHADOW(vp, rp));
9854 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9856 reread:
9857 bp = NULL;
9858 pp = NULL;
9859 pagefound = NULL;
9861 if (pl != NULL)
9862 pl[0] = NULL;
9864 error = 0;
9865 lbn = off / bsize;
9866 blkoff = lbn * bsize;
9869 * Queueing up the readahead before doing the synchronous read
9870 * results in a significant increase in read throughput because
9871 * of the increased parallelism between the async threads and
9872 * the process context.
9874 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9875 rw != S_CREATE &&
9876 !(vp->v_flag & VNOCACHE)) {
9877 mutex_enter(&rp->r_statelock);
9880 * Calculate the number of readaheads to do.
9881 * a) No readaheads at offset = 0.
9882 * b) Do maximum(nfs4_nra) readaheads when the readahead
9883 * window is closed.
9884 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9885 * upon how far the readahead window is open or close.
9886 * d) No readaheads if rp->r_nextr is not within the scope
9887 * of the readahead window (random i/o).
9890 if (off == 0)
9891 readahead = 0;
9892 else if (blkoff == rp->r_nextr)
9893 readahead = nfs4_nra;
9894 else if (rp->r_nextr > blkoff &&
9895 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9896 <= (nfs4_nra - 1)))
9897 readahead = nfs4_nra - ra_window;
9898 else
9899 readahead = 0;
9901 rablkoff = rp->r_nextr;
9902 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9903 mutex_exit(&rp->r_statelock);
9904 if (nfs4_async_readahead(vp, rablkoff + bsize,
9905 addr + (rablkoff + bsize - off),
9906 seg, cr, nfs4_readahead) < 0) {
9907 mutex_enter(&rp->r_statelock);
9908 break;
9910 readahead--;
9911 rablkoff += bsize;
9913 * Indicate that we did a readahead so
9914 * readahead offset is not updated
9915 * by the synchronous read below.
9917 readahead_issued = 1;
9918 mutex_enter(&rp->r_statelock);
9920 * set readahead offset to
9921 * offset of last async readahead
9922 * request.
9924 rp->r_nextr = rablkoff;
9926 mutex_exit(&rp->r_statelock);
9929 again:
9930 if ((pagefound = page_exists(vp, off)) == NULL) {
9931 if (pl == NULL) {
9932 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9933 nfs4_readahead);
9934 } else if (rw == S_CREATE) {
9936 * Block for this page is not allocated, or the offset
9937 * is beyond the current allocation size, or we're
9938 * allocating a swap slot and the page was not found,
9939 * so allocate it and return a zero page.
9941 if ((pp = page_create_va(vp, off,
9942 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9943 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9944 io_len = PAGESIZE;
9945 mutex_enter(&rp->r_statelock);
9946 rp->r_nextr = off + PAGESIZE;
9947 mutex_exit(&rp->r_statelock);
9948 } else {
9950 * Need to go to server to get a block
9952 mutex_enter(&rp->r_statelock);
9953 if (blkoff < rp->r_size &&
9954 blkoff + bsize > rp->r_size) {
9956 * If less than a block left in
9957 * file read less than a block.
9959 if (rp->r_size <= off) {
9961 * Trying to access beyond EOF,
9962 * set up to get at least one page.
9964 blksize = off + PAGESIZE - blkoff;
9965 } else
9966 blksize = rp->r_size - blkoff;
9967 } else if ((off == 0) ||
9968 (off != rp->r_nextr && !readahead_issued)) {
9969 blksize = PAGESIZE;
9970 blkoff = off; /* block = page here */
9971 } else
9972 blksize = bsize;
9973 mutex_exit(&rp->r_statelock);
9975 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9976 &io_len, blkoff, blksize, 0);
9979 * Some other thread has entered the page,
9980 * so just use it.
9982 if (pp == NULL)
9983 goto again;
9986 * Now round the request size up to page boundaries.
9987 * This ensures that the entire page will be
9988 * initialized to zeroes if EOF is encountered.
9990 io_len = ptob(btopr(io_len));
9992 bp = pageio_setup(pp, io_len, vp, B_READ);
9993 ASSERT(bp != NULL);
9996 * pageio_setup should have set b_addr to 0. This
9997 * is correct since we want to do I/O on a page
9998 * boundary. bp_mapin will use this addr to calculate
9999 * an offset, and then set b_addr to the kernel virtual
10000 * address it allocated for us.
10002 ASSERT(bp->b_un.b_addr == 0);
10004 bp->b_edev = 0;
10005 bp->b_dev = 0;
10006 bp->b_lblkno = lbtodb(io_off);
10007 bp->b_file = vp;
10008 bp->b_offset = (offset_t)off;
10009 bp_mapin(bp);
10012 * If doing a write beyond what we believe is EOF,
10013 * don't bother trying to read the pages from the
10014 * server, we'll just zero the pages here. We
10015 * don't check that the rw flag is S_WRITE here
10016 * because some implementations may attempt a
10017 * read access to the buffer before copying data.
10019 mutex_enter(&rp->r_statelock);
10020 if (io_off >= rp->r_size && seg == segkmap) {
10021 mutex_exit(&rp->r_statelock);
10022 bzero(bp->b_un.b_addr, io_len);
10023 } else {
10024 mutex_exit(&rp->r_statelock);
10025 error = nfs4_bio(bp, NULL, cr, FALSE);
10029 * Unmap the buffer before freeing it.
10031 bp_mapout(bp);
10032 pageio_done(bp);
10034 savepp = pp;
10035 do {
10036 pp->p_fsdata = C_NOCOMMIT;
10037 } while ((pp = pp->p_next) != savepp);
10039 if (error == NFS_EOF) {
10041 * If doing a write system call just return
10042 * zeroed pages, else user tried to get pages
10043 * beyond EOF, return error. We don't check
10044 * that the rw flag is S_WRITE here because
10045 * some implementations may attempt a read
10046 * access to the buffer before copying data.
10048 if (seg == segkmap)
10049 error = 0;
10050 else
10051 error = EFAULT;
10054 if (!readahead_issued && !error) {
10055 mutex_enter(&rp->r_statelock);
10056 rp->r_nextr = io_off + io_len;
10057 mutex_exit(&rp->r_statelock);
10062 out:
10063 if (pl == NULL)
10064 return (error);
10066 if (error) {
10067 if (pp != NULL)
10068 pvn_read_done(pp, B_ERROR);
10069 return (error);
10072 if (pagefound) {
10073 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10076 * Page exists in the cache, acquire the appropriate lock.
10077 * If this fails, start all over again.
10079 if ((pp = page_lookup(vp, off, se)) == NULL) {
10080 #ifdef DEBUG
10081 nfs4_lostpage++;
10082 #endif
10083 goto reread;
10085 pl[0] = pp;
10086 pl[1] = NULL;
10087 return (0);
10090 if (pp != NULL)
10091 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10093 return (error);
10096 static void
10097 nfs4_readahead(vnode_t *vp, uoff_t blkoff, caddr_t addr, struct seg *seg,
10098 cred_t *cr)
10100 int error;
10101 page_t *pp;
10102 uoff_t io_off;
10103 size_t io_len;
10104 struct buf *bp;
10105 uint_t bsize, blksize;
10106 rnode4_t *rp = VTOR4(vp);
10107 page_t *savepp;
10109 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10111 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10113 mutex_enter(&rp->r_statelock);
10114 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10116 * If less than a block left in file read less
10117 * than a block.
10119 blksize = rp->r_size - blkoff;
10120 } else
10121 blksize = bsize;
10122 mutex_exit(&rp->r_statelock);
10124 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10125 &io_off, &io_len, blkoff, blksize, 1);
10127 * The isra flag passed to the kluster function is 1, we may have
10128 * gotten a return value of NULL for a variety of reasons (# of free
10129 * pages < minfree, someone entered the page on the vnode etc). In all
10130 * cases, we want to punt on the readahead.
10132 if (pp == NULL)
10133 return;
10136 * Now round the request size up to page boundaries.
10137 * This ensures that the entire page will be
10138 * initialized to zeroes if EOF is encountered.
10140 io_len = ptob(btopr(io_len));
10142 bp = pageio_setup(pp, io_len, vp, B_READ);
10143 ASSERT(bp != NULL);
10146 * pageio_setup should have set b_addr to 0. This is correct since
10147 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10148 * to calculate an offset, and then set b_addr to the kernel virtual
10149 * address it allocated for us.
10151 ASSERT(bp->b_un.b_addr == 0);
10153 bp->b_edev = 0;
10154 bp->b_dev = 0;
10155 bp->b_lblkno = lbtodb(io_off);
10156 bp->b_file = vp;
10157 bp->b_offset = (offset_t)blkoff;
10158 bp_mapin(bp);
10161 * If doing a write beyond what we believe is EOF, don't bother trying
10162 * to read the pages from the server, we'll just zero the pages here.
10163 * We don't check that the rw flag is S_WRITE here because some
10164 * implementations may attempt a read access to the buffer before
10165 * copying data.
10167 mutex_enter(&rp->r_statelock);
10168 if (io_off >= rp->r_size && seg == segkmap) {
10169 mutex_exit(&rp->r_statelock);
10170 bzero(bp->b_un.b_addr, io_len);
10171 error = 0;
10172 } else {
10173 mutex_exit(&rp->r_statelock);
10174 error = nfs4_bio(bp, NULL, cr, TRUE);
10175 if (error == NFS_EOF)
10176 error = 0;
10180 * Unmap the buffer before freeing it.
10182 bp_mapout(bp);
10183 pageio_done(bp);
10185 savepp = pp;
10186 do {
10187 pp->p_fsdata = C_NOCOMMIT;
10188 } while ((pp = pp->p_next) != savepp);
10190 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10193 * In case of error set readahead offset
10194 * to the lowest offset.
10195 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10197 if (error && rp->r_nextr > io_off) {
10198 mutex_enter(&rp->r_statelock);
10199 if (rp->r_nextr > io_off)
10200 rp->r_nextr = io_off;
10201 mutex_exit(&rp->r_statelock);
10206 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10207 * If len == 0, do from off to EOF.
10209 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10210 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10211 * (from pageout).
10213 /* ARGSUSED */
10214 static int
10215 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10216 caller_context_t *ct)
10218 int error;
10219 rnode4_t *rp;
10221 ASSERT(cr != NULL);
10223 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10224 return (EIO);
10226 rp = VTOR4(vp);
10227 if (IS_SHADOW(vp, rp))
10228 vp = RTOV4(rp);
10231 * XXX - Why should this check be made here?
10233 if (vp->v_flag & VNOMAP)
10234 return (ENOSYS);
10236 if (len == 0 && !(flags & B_INVAL) &&
10237 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10238 return (0);
10240 mutex_enter(&rp->r_statelock);
10241 rp->r_count++;
10242 mutex_exit(&rp->r_statelock);
10243 error = nfs4_putpages(vp, off, len, flags, cr);
10244 mutex_enter(&rp->r_statelock);
10245 rp->r_count--;
10246 cv_broadcast(&rp->r_cv);
10247 mutex_exit(&rp->r_statelock);
10249 return (error);
10253 * Write out a single page, possibly klustering adjacent dirty pages.
10256 nfs4_putapage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
10257 int flags, cred_t *cr)
10259 uoff_t io_off;
10260 uoff_t lbn_off;
10261 uoff_t lbn;
10262 size_t io_len;
10263 uint_t bsize;
10264 int error;
10265 rnode4_t *rp;
10267 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10268 ASSERT(pp != NULL);
10269 ASSERT(cr != NULL);
10270 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10272 rp = VTOR4(vp);
10273 ASSERT(rp->r_count > 0);
10274 ASSERT(!IS_SHADOW(vp, rp));
10276 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10277 lbn = pp->p_offset / bsize;
10278 lbn_off = lbn * bsize;
10281 * Find a kluster that fits in one block, or in
10282 * one page if pages are bigger than blocks. If
10283 * there is less file space allocated than a whole
10284 * page, we'll shorten the i/o request below.
10286 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10287 roundup(bsize, PAGESIZE), flags);
10290 * pvn_write_kluster shouldn't have returned a page with offset
10291 * behind the original page we were given. Verify that.
10293 ASSERT((pp->p_offset / bsize) >= lbn);
10296 * Now pp will have the list of kept dirty pages marked for
10297 * write back. It will also handle invalidation and freeing
10298 * of pages that are not dirty. Check for page length rounding
10299 * problems.
10301 if (io_off + io_len > lbn_off + bsize) {
10302 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10303 io_len = lbn_off + bsize - io_off;
10306 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10307 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10308 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10309 * progress and the r_size has not been made consistent with the
10310 * new size of the file. When the uiomove() completes the r_size is
10311 * updated and the R4MODINPROGRESS flag is cleared.
10313 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10314 * consistent value of r_size. Without this handshaking, it is
10315 * possible that nfs4_bio() picks up the old value of r_size
10316 * before the uiomove() in writerp4() completes. This will result
10317 * in the write through nfs4_bio() being dropped.
10319 * More precisely, there is a window between the time the uiomove()
10320 * completes and the time the r_size is updated. If a fop_putpage()
10321 * operation intervenes in this window, the page will be picked up,
10322 * because it is dirty (it will be unlocked, unless it was
10323 * pagecreate'd). When the page is picked up as dirty, the dirty
10324 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10325 * checked. This will still be the old size. Therefore the page will
10326 * not be written out. When segmap_release() calls fop_putpage(),
10327 * the page will be found to be clean and the write will be dropped.
10329 if (rp->r_flags & R4MODINPROGRESS) {
10330 mutex_enter(&rp->r_statelock);
10331 if ((rp->r_flags & R4MODINPROGRESS) &&
10332 rp->r_modaddr + MAXBSIZE > io_off &&
10333 rp->r_modaddr < io_off + io_len) {
10334 page_t *plist;
10336 * A write is in progress for this region of the file.
10337 * If we did not detect R4MODINPROGRESS here then this
10338 * path through nfs_putapage() would eventually go to
10339 * nfs4_bio() and may not write out all of the data
10340 * in the pages. We end up losing data. So we decide
10341 * to set the modified bit on each page in the page
10342 * list and mark the rnode with R4DIRTY. This write
10343 * will be restarted at some later time.
10345 plist = pp;
10346 while (plist != NULL) {
10347 pp = plist;
10348 page_sub(&plist, pp);
10349 hat_setmod(pp);
10350 page_io_unlock(pp);
10351 page_unlock(pp);
10353 rp->r_flags |= R4DIRTY;
10354 mutex_exit(&rp->r_statelock);
10355 if (offp)
10356 *offp = io_off;
10357 if (lenp)
10358 *lenp = io_len;
10359 return (0);
10361 mutex_exit(&rp->r_statelock);
10364 if (flags & B_ASYNC) {
10365 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10366 nfs4_sync_putapage);
10367 } else
10368 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10370 if (offp)
10371 *offp = io_off;
10372 if (lenp)
10373 *lenp = io_len;
10374 return (error);
10377 static int
10378 nfs4_sync_putapage(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
10379 int flags, cred_t *cr)
10381 int error;
10382 rnode4_t *rp;
10384 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10386 flags |= B_WRITE;
10388 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10390 rp = VTOR4(vp);
10392 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10393 error == EACCES) &&
10394 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10395 if (!(rp->r_flags & R4OUTOFSPACE)) {
10396 mutex_enter(&rp->r_statelock);
10397 rp->r_flags |= R4OUTOFSPACE;
10398 mutex_exit(&rp->r_statelock);
10400 flags |= B_ERROR;
10401 pvn_write_done(pp, flags);
10403 * If this was not an async thread, then try again to
10404 * write out the pages, but this time, also destroy
10405 * them whether or not the write is successful. This
10406 * will prevent memory from filling up with these
10407 * pages and destroying them is the only alternative
10408 * if they can't be written out.
10410 * Don't do this if this is an async thread because
10411 * when the pages are unlocked in pvn_write_done,
10412 * some other thread could have come along, locked
10413 * them, and queued for an async thread. It would be
10414 * possible for all of the async threads to be tied
10415 * up waiting to lock the pages again and they would
10416 * all already be locked and waiting for an async
10417 * thread to handle them. Deadlock.
10419 if (!(flags & B_ASYNC)) {
10420 error = nfs4_putpage(vp, io_off, io_len,
10421 B_INVAL | B_FORCE, cr, NULL);
10423 } else {
10424 if (error)
10425 flags |= B_ERROR;
10426 else if (rp->r_flags & R4OUTOFSPACE) {
10427 mutex_enter(&rp->r_statelock);
10428 rp->r_flags &= ~R4OUTOFSPACE;
10429 mutex_exit(&rp->r_statelock);
10431 pvn_write_done(pp, flags);
10432 if (freemem < desfree)
10433 (void) nfs4_commit_vp(vp, 0, 0, cr,
10434 NFS4_WRITE_NOWAIT);
10437 return (error);
10440 #ifdef DEBUG
10441 int nfs4_force_open_before_mmap = 0;
10442 #endif
10444 /* ARGSUSED */
10445 static int
10446 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10447 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10448 caller_context_t *ct)
10450 struct segvn_crargs vn_a;
10451 int error = 0;
10452 rnode4_t *rp = VTOR4(vp);
10453 mntinfo4_t *mi = VTOMI4(vp);
10455 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10456 return (EIO);
10458 if (vp->v_flag & VNOMAP)
10459 return (ENOSYS);
10461 if (off < 0 || (off + len) < 0)
10462 return (ENXIO);
10464 if (vp->v_type != VREG)
10465 return (ENODEV);
10468 * If the file is delegated to the client don't do anything.
10469 * If the file is not delegated, then validate the data cache.
10471 mutex_enter(&rp->r_statev4_lock);
10472 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10473 mutex_exit(&rp->r_statev4_lock);
10474 error = nfs4_validate_caches(vp, cr);
10475 if (error)
10476 return (error);
10477 } else {
10478 mutex_exit(&rp->r_statev4_lock);
10482 * Check to see if the vnode is currently marked as not cachable.
10483 * This means portions of the file are locked (through fop_frlock).
10484 * In this case the map request must be refused. We use
10485 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10487 * Atomically increment r_inmap after acquiring r_rwlock. The
10488 * idea here is to acquire r_rwlock to block read/write and
10489 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10490 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10491 * and we can prevent the deadlock that would have occurred
10492 * when nfs4_addmap() would have acquired it out of order.
10494 * Since we are not protecting r_inmap by any lock, we do not
10495 * hold any lock when we decrement it. We atomically decrement
10496 * r_inmap after we release r_lkserlock.
10499 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10500 return (EINTR);
10501 atomic_inc_uint(&rp->r_inmap);
10502 nfs_rw_exit(&rp->r_rwlock);
10504 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10505 atomic_dec_uint(&rp->r_inmap);
10506 return (EINTR);
10509 if (vp->v_flag & VNOCACHE) {
10510 error = EAGAIN;
10511 goto done;
10515 * Don't allow concurrent locks and mapping if mandatory locking is
10516 * enabled.
10518 if (flk_has_remote_locks(vp)) {
10519 struct vattr va;
10520 va.va_mask = AT_MODE;
10521 error = nfs4getattr(vp, &va, cr);
10522 if (error != 0)
10523 goto done;
10524 if (MANDLOCK(vp, va.va_mode)) {
10525 error = EAGAIN;
10526 goto done;
10531 * It is possible that the rnode has a lost lock request that we
10532 * are still trying to recover, and that the request conflicts with
10533 * this map request.
10535 * An alternative approach would be for nfs4_safemap() to consider
10536 * queued lock requests when deciding whether to set or clear
10537 * VNOCACHE. This would require the frlock code path to call
10538 * nfs4_safemap() after enqueing a lost request.
10540 if (nfs4_map_lost_lock_conflict(vp)) {
10541 error = EAGAIN;
10542 goto done;
10545 as_rangelock(as);
10546 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10547 if (error != 0) {
10548 as_rangeunlock(as);
10549 goto done;
10552 if (vp->v_type == VREG) {
10554 * We need to retrieve the open stream
10556 nfs4_open_stream_t *osp = NULL;
10557 nfs4_open_owner_t *oop = NULL;
10559 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10560 if (oop != NULL) {
10561 /* returns with 'os_sync_lock' held */
10562 osp = find_open_stream(oop, rp);
10563 open_owner_rele(oop);
10565 if (osp == NULL) {
10566 #ifdef DEBUG
10567 if (nfs4_force_open_before_mmap) {
10568 error = EIO;
10569 goto done;
10571 #endif
10572 /* returns with 'os_sync_lock' held */
10573 error = open_and_get_osp(vp, cr, &osp);
10574 if (osp == NULL) {
10575 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10576 "nfs4_map: we tried to OPEN the file "
10577 "but again no osp, so fail with EIO"));
10578 goto done;
10582 if (osp->os_failed_reopen) {
10583 mutex_exit(&osp->os_sync_lock);
10584 open_stream_rele(osp, rp);
10585 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10586 "nfs4_map: os_failed_reopen set on "
10587 "osp %p, cr %p, rp %s", (void *)osp,
10588 (void *)cr, rnode4info(rp)));
10589 error = EIO;
10590 goto done;
10592 mutex_exit(&osp->os_sync_lock);
10593 open_stream_rele(osp, rp);
10596 vn_a.vp = vp;
10597 vn_a.offset = off;
10598 vn_a.type = (flags & MAP_TYPE);
10599 vn_a.prot = (uchar_t)prot;
10600 vn_a.maxprot = (uchar_t)maxprot;
10601 vn_a.flags = (flags & ~MAP_TYPE);
10602 vn_a.cred = cr;
10603 vn_a.amp = NULL;
10604 vn_a.szc = 0;
10605 vn_a.lgrp_mem_policy_flags = 0;
10607 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10608 as_rangeunlock(as);
10610 done:
10611 nfs_rw_exit(&rp->r_lkserlock);
10612 atomic_dec_uint(&rp->r_inmap);
10613 return (error);
10617 * We're most likely dealing with a kernel module that likes to READ
10618 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10619 * officially OPEN the file to create the necessary client state
10620 * for bookkeeping of os_mmap_read/write counts.
10622 * Since fop_map only passes in a pointer to the vnode rather than
10623 * a double pointer, we can't handle the case where nfs4open_otw()
10624 * returns a different vnode than the one passed into fop_map (since
10625 * fop_delmap will not see the vnode nfs4open_otw used). In this case,
10626 * we return NULL and let nfs4_map() fail. Note: the only case where
10627 * this should happen is if the file got removed and replaced with the
10628 * same name on the server (in addition to the fact that we're trying
10629 * to fop_map withouth fop_opening the file in the first place).
10631 static int
10632 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10634 rnode4_t *rp, *drp;
10635 vnode_t *dvp, *open_vp;
10636 char file_name[MAXNAMELEN];
10637 int just_created;
10638 nfs4_open_stream_t *osp;
10639 nfs4_open_owner_t *oop;
10640 int error;
10642 *ospp = NULL;
10643 open_vp = map_vp;
10645 rp = VTOR4(open_vp);
10646 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10647 return (error);
10648 drp = VTOR4(dvp);
10650 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10651 VN_RELE(dvp);
10652 return (EINTR);
10655 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10656 nfs_rw_exit(&drp->r_rwlock);
10657 VN_RELE(dvp);
10658 return (error);
10661 mutex_enter(&rp->r_statev4_lock);
10662 if (rp->created_v4) {
10663 rp->created_v4 = 0;
10664 mutex_exit(&rp->r_statev4_lock);
10666 dnlc_update(dvp, file_name, open_vp);
10667 /* This is needed so we don't bump the open ref count */
10668 just_created = 1;
10669 } else {
10670 mutex_exit(&rp->r_statev4_lock);
10671 just_created = 0;
10674 VN_HOLD(map_vp);
10676 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10677 just_created);
10678 if (error) {
10679 nfs_rw_exit(&drp->r_rwlock);
10680 VN_RELE(dvp);
10681 VN_RELE(map_vp);
10682 return (error);
10685 nfs_rw_exit(&drp->r_rwlock);
10686 VN_RELE(dvp);
10689 * If nfs4open_otw() returned a different vnode then "undo"
10690 * the open and return failure to the caller.
10692 if (!VN_CMP(open_vp, map_vp)) {
10693 nfs4_error_t e;
10695 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10696 "open returned a different vnode"));
10698 * If there's an error, ignore it,
10699 * and let fop_inactive handle it.
10701 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10702 CLOSE_NORM, 0, 0, 0);
10703 VN_RELE(map_vp);
10704 return (EIO);
10707 VN_RELE(map_vp);
10709 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10710 if (!oop) {
10711 nfs4_error_t e;
10713 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10714 "no open owner"));
10716 * If there's an error, ignore it,
10717 * and let fop_inactive handle it.
10719 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10720 CLOSE_NORM, 0, 0, 0);
10721 return (EIO);
10723 osp = find_open_stream(oop, rp);
10724 open_owner_rele(oop);
10725 *ospp = osp;
10726 return (0);
10730 * Please be aware that when this function is called, the address space write
10731 * a_lock is held. Do not put over the wire calls in this function.
10733 /* ARGSUSED */
10734 static int
10735 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10736 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10737 caller_context_t *ct)
10739 rnode4_t *rp;
10740 int error = 0;
10741 mntinfo4_t *mi;
10743 mi = VTOMI4(vp);
10744 rp = VTOR4(vp);
10746 if (nfs_zone() != mi->mi_zone)
10747 return (EIO);
10748 if (vp->v_flag & VNOMAP)
10749 return (ENOSYS);
10752 * Don't need to update the open stream first, since this
10753 * mmap can't add any additional share access that isn't
10754 * already contained in the open stream (for the case where we
10755 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10756 * take into account os_mmap_read[write] counts).
10758 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10760 if (vp->v_type == VREG) {
10762 * We need to retrieve the open stream and update the counts.
10763 * If there is no open stream here, something is wrong.
10765 nfs4_open_stream_t *osp = NULL;
10766 nfs4_open_owner_t *oop = NULL;
10768 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10769 if (oop != NULL) {
10770 /* returns with 'os_sync_lock' held */
10771 osp = find_open_stream(oop, rp);
10772 open_owner_rele(oop);
10774 if (osp == NULL) {
10775 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10776 "nfs4_addmap: we should have an osp"
10777 "but we don't, so fail with EIO"));
10778 error = EIO;
10779 goto out;
10782 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10783 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10786 * Update the map count in the open stream.
10787 * This is necessary in the case where we
10788 * open/mmap/close/, then the server reboots, and we
10789 * attempt to reopen. If the mmap doesn't add share
10790 * access then we send an invalid reopen with
10791 * access = NONE.
10793 * We need to specifically check each PROT_* so a mmap
10794 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10795 * read and write access. A simple comparison of prot
10796 * to ~PROT_WRITE to determine read access is insufficient
10797 * since prot can be |= with PROT_USER, etc.
10801 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10803 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10804 osp->os_mmap_write += btopr(len);
10805 if (maxprot & PROT_READ)
10806 osp->os_mmap_read += btopr(len);
10807 if (maxprot & PROT_EXEC)
10808 osp->os_mmap_read += btopr(len);
10810 * Ensure that os_mmap_read gets incremented, even if
10811 * maxprot were to look like PROT_NONE.
10813 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10814 !(maxprot & PROT_EXEC))
10815 osp->os_mmap_read += btopr(len);
10816 osp->os_mapcnt += btopr(len);
10817 mutex_exit(&osp->os_sync_lock);
10818 open_stream_rele(osp, rp);
10821 out:
10823 * If we got an error, then undo our
10824 * incrementing of 'r_mapcnt'.
10827 if (error) {
10828 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10829 ASSERT(rp->r_mapcnt >= 0);
10831 return (error);
10834 /* ARGSUSED */
10835 static int
10836 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10839 return (VTOR4(vp1) == VTOR4(vp2));
10842 /* ARGSUSED */
10843 static int
10844 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10845 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10846 caller_context_t *ct)
10848 int rc;
10849 uoff_t start, end;
10850 rnode4_t *rp;
10851 int error = 0, intr = INTR4(vp);
10852 nfs4_error_t e;
10854 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10855 return (EIO);
10857 /* check for valid cmd parameter */
10858 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10859 return (EINVAL);
10861 /* Verify l_type. */
10862 switch (bfp->l_type) {
10863 case F_RDLCK:
10864 if (cmd != F_GETLK && !(flag & FREAD))
10865 return (EBADF);
10866 break;
10867 case F_WRLCK:
10868 if (cmd != F_GETLK && !(flag & FWRITE))
10869 return (EBADF);
10870 break;
10871 case F_UNLCK:
10872 intr = 0;
10873 break;
10875 default:
10876 return (EINVAL);
10879 /* check the validity of the lock range */
10880 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10881 return (rc);
10882 if (rc = flk_check_lock_data(start, end, MAXEND))
10883 return (rc);
10886 * If the filesystem is mounted using local locking, pass the
10887 * request off to the local locking code.
10889 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10890 if (cmd == F_SETLK || cmd == F_SETLKW) {
10892 * For complete safety, we should be holding
10893 * r_lkserlock. However, we can't call
10894 * nfs4_safelock and then fs_frlock while
10895 * holding r_lkserlock, so just invoke
10896 * nfs4_safelock and expect that this will
10897 * catch enough of the cases.
10899 if (!nfs4_safelock(vp, bfp, cr))
10900 return (EAGAIN);
10902 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10905 rp = VTOR4(vp);
10908 * Check whether the given lock request can proceed, given the
10909 * current file mappings.
10911 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10912 return (EINTR);
10913 if (cmd == F_SETLK || cmd == F_SETLKW) {
10914 if (!nfs4_safelock(vp, bfp, cr)) {
10915 rc = EAGAIN;
10916 goto done;
10921 * Flush the cache after waiting for async I/O to finish. For new
10922 * locks, this is so that the process gets the latest bits from the
10923 * server. For unlocks, this is so that other clients see the
10924 * latest bits once the file has been unlocked. If currently dirty
10925 * pages can't be flushed, then don't allow a lock to be set. But
10926 * allow unlocks to succeed, to avoid having orphan locks on the
10927 * server.
10929 if (cmd != F_GETLK) {
10930 mutex_enter(&rp->r_statelock);
10931 while (rp->r_count > 0) {
10932 if (intr) {
10933 klwp_t *lwp = ttolwp(curthread);
10935 if (lwp != NULL)
10936 lwp->lwp_nostop++;
10937 if (cv_wait_sig(&rp->r_cv,
10938 &rp->r_statelock) == 0) {
10939 if (lwp != NULL)
10940 lwp->lwp_nostop--;
10941 rc = EINTR;
10942 break;
10944 if (lwp != NULL)
10945 lwp->lwp_nostop--;
10946 } else {
10947 cv_wait(&rp->r_cv, &rp->r_statelock);
10950 mutex_exit(&rp->r_statelock);
10951 if (rc != 0)
10952 goto done;
10953 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10954 if (error) {
10955 if (error == ENOSPC || error == EDQUOT) {
10956 mutex_enter(&rp->r_statelock);
10957 if (!rp->r_error)
10958 rp->r_error = error;
10959 mutex_exit(&rp->r_statelock);
10961 if (bfp->l_type != F_UNLCK) {
10962 rc = ENOLCK;
10963 goto done;
10969 * Call the lock manager to do the real work of contacting
10970 * the server and obtaining the lock.
10972 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10973 cr, &e, NULL, NULL);
10974 rc = e.error;
10976 if (rc == 0)
10977 nfs4_lockcompletion(vp, cmd);
10979 done:
10980 nfs_rw_exit(&rp->r_lkserlock);
10982 return (rc);
10986 * Free storage space associated with the specified vnode. The portion
10987 * to be freed is specified by bfp->l_start and bfp->l_len (already
10988 * normalized to a "whence" of 0).
10990 * This is an experimental facility whose continued existence is not
10991 * guaranteed. Currently, we only support the special case
10992 * of l_len == 0, meaning free to end of file.
10994 /* ARGSUSED */
10995 static int
10996 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10997 offset_t offset, cred_t *cr, caller_context_t *ct)
10999 int error;
11001 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11002 return (EIO);
11003 ASSERT(vp->v_type == VREG);
11004 if (cmd != F_FREESP)
11005 return (EINVAL);
11007 error = convoff(vp, bfp, 0, offset);
11008 if (!error) {
11009 ASSERT(bfp->l_start >= 0);
11010 if (bfp->l_len == 0) {
11011 struct vattr va;
11013 va.va_mask = AT_SIZE;
11014 va.va_size = bfp->l_start;
11015 error = nfs4setattr(vp, &va, 0, cr, NULL);
11017 if (error == 0 && bfp->l_start == 0)
11018 vnevent_truncate(vp, ct);
11019 } else
11020 error = EINVAL;
11023 return (error);
11026 /* ARGSUSED */
11028 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11030 rnode4_t *rp;
11031 rp = VTOR4(vp);
11033 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11034 vp = RTOV4(rp);
11036 *vpp = vp;
11037 return (0);
11041 * Setup and add an address space callback to do the work of the delmap call.
11042 * The callback will (and must be) deleted in the actual callback function.
11044 * This is done in order to take care of the problem that we have with holding
11045 * the address space's a_lock for a long period of time (e.g. if the NFS server
11046 * is down). Callbacks will be executed in the address space code while the
11047 * a_lock is not held. Holding the address space's a_lock causes things such
11048 * as ps and fork to hang because they are trying to acquire this lock as well.
11050 /* ARGSUSED */
11051 static int
11052 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11053 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11054 caller_context_t *ct)
11056 int caller_found;
11057 int error;
11058 rnode4_t *rp;
11059 nfs4_delmap_args_t *dmapp;
11060 nfs4_delmapcall_t *delmap_call;
11062 if (vp->v_flag & VNOMAP)
11063 return (ENOSYS);
11066 * A process may not change zones if it has NFS pages mmap'ed
11067 * in, so we can't legitimately get here from the wrong zone.
11069 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11071 rp = VTOR4(vp);
11074 * The way that the address space of this process deletes its mapping
11075 * of this file is via the following call chains:
11076 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11077 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11079 * With the use of address space callbacks we are allowed to drop the
11080 * address space lock, a_lock, while executing the NFS operations that
11081 * need to go over the wire. Returning EAGAIN to the caller of this
11082 * function is what drives the execution of the callback that we add
11083 * below. The callback will be executed by the address space code
11084 * after dropping the a_lock. When the callback is finished, since
11085 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11086 * is called again on the same segment to finish the rest of the work
11087 * that needs to happen during unmapping.
11089 * This action of calling back into the segment driver causes
11090 * nfs4_delmap() to get called again, but since the callback was
11091 * already executed at this point, it already did the work and there
11092 * is nothing left for us to do.
11094 * To Summarize:
11095 * - The first time nfs4_delmap is called by the current thread is when
11096 * we add the caller associated with this delmap to the delmap caller
11097 * list, add the callback, and return EAGAIN.
11098 * - The second time in this call chain when nfs4_delmap is called we
11099 * will find this caller in the delmap caller list and realize there
11100 * is no more work to do thus removing this caller from the list and
11101 * returning the error that was set in the callback execution.
11103 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11104 if (caller_found) {
11106 * 'error' is from the actual delmap operations. To avoid
11107 * hangs, we need to handle the return of EAGAIN differently
11108 * since this is what drives the callback execution.
11109 * In this case, we don't want to return EAGAIN and do the
11110 * callback execution because there are none to execute.
11112 if (error == EAGAIN)
11113 return (0);
11114 else
11115 return (error);
11118 /* current caller was not in the list */
11119 delmap_call = nfs4_init_delmapcall();
11121 mutex_enter(&rp->r_statelock);
11122 list_insert_tail(&rp->r_indelmap, delmap_call);
11123 mutex_exit(&rp->r_statelock);
11125 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11127 dmapp->vp = vp;
11128 dmapp->off = off;
11129 dmapp->addr = addr;
11130 dmapp->len = len;
11131 dmapp->prot = prot;
11132 dmapp->maxprot = maxprot;
11133 dmapp->flags = flags;
11134 dmapp->cr = cr;
11135 dmapp->caller = delmap_call;
11137 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11138 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11140 return (error ? error : EAGAIN);
11143 static nfs4_delmapcall_t *
11144 nfs4_init_delmapcall()
11146 nfs4_delmapcall_t *delmap_call;
11148 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11149 delmap_call->call_id = curthread;
11150 delmap_call->error = 0;
11152 return (delmap_call);
11155 static void
11156 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11158 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11162 * Searches for the current delmap caller (based on curthread) in the list of
11163 * callers. If it is found, we remove it and free the delmap caller.
11164 * Returns:
11165 * 0 if the caller wasn't found
11166 * 1 if the caller was found, removed and freed. *errp will be set
11167 * to what the result of the delmap was.
11169 static int
11170 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11172 nfs4_delmapcall_t *delmap_call;
11175 * If the list doesn't exist yet, we create it and return
11176 * that the caller wasn't found. No list = no callers.
11178 mutex_enter(&rp->r_statelock);
11179 if (!(rp->r_flags & R4DELMAPLIST)) {
11180 /* The list does not exist */
11181 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11182 offsetof(nfs4_delmapcall_t, call_node));
11183 rp->r_flags |= R4DELMAPLIST;
11184 mutex_exit(&rp->r_statelock);
11185 return (0);
11186 } else {
11187 /* The list exists so search it */
11188 for (delmap_call = list_head(&rp->r_indelmap);
11189 delmap_call != NULL;
11190 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11191 if (delmap_call->call_id == curthread) {
11192 /* current caller is in the list */
11193 *errp = delmap_call->error;
11194 list_remove(&rp->r_indelmap, delmap_call);
11195 mutex_exit(&rp->r_statelock);
11196 nfs4_free_delmapcall(delmap_call);
11197 return (1);
11201 mutex_exit(&rp->r_statelock);
11202 return (0);
11206 * Remove some pages from an mmap'd vnode. Just update the
11207 * count of pages. If doing close-to-open, then flush and
11208 * commit all of the pages associated with this file.
11209 * Otherwise, start an asynchronous page flush to write out
11210 * any dirty pages. This will also associate a credential
11211 * with the rnode which can be used to write the pages.
11213 /* ARGSUSED */
11214 static void
11215 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11217 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11218 rnode4_t *rp;
11219 mntinfo4_t *mi;
11220 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11222 rp = VTOR4(dmapp->vp);
11223 mi = VTOMI4(dmapp->vp);
11225 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11226 ASSERT(rp->r_mapcnt >= 0);
11229 * Initiate a page flush and potential commit if there are
11230 * pages, the file system was not mounted readonly, the segment
11231 * was mapped shared, and the pages themselves were writeable.
11233 if (nfs4_has_pages(dmapp->vp) &&
11234 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11235 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11236 mutex_enter(&rp->r_statelock);
11237 rp->r_flags |= R4DIRTY;
11238 mutex_exit(&rp->r_statelock);
11239 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11240 dmapp->len, dmapp->cr);
11241 if (!e.error) {
11242 mutex_enter(&rp->r_statelock);
11243 e.error = rp->r_error;
11244 rp->r_error = 0;
11245 mutex_exit(&rp->r_statelock);
11247 } else
11248 e.error = 0;
11250 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11251 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11252 B_INVAL, dmapp->cr, NULL);
11254 if (e.error) {
11255 e.stat = puterrno4(e.error);
11256 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11257 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11258 dmapp->caller->error = e.error;
11261 /* Check to see if we need to close the file */
11263 if (dmapp->vp->v_type == VREG) {
11264 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11265 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11267 if (e.error != 0 || e.stat != NFS4_OK) {
11269 * Since it is possible that e.error == 0 and
11270 * e.stat != NFS4_OK (and vice versa),
11271 * we do the proper checking in order to get both
11272 * e.error and e.stat reporting the correct info.
11274 if (e.stat == NFS4_OK)
11275 e.stat = puterrno4(e.error);
11276 if (e.error == 0)
11277 e.error = geterrno4(e.stat);
11279 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11280 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11281 dmapp->caller->error = e.error;
11285 (void) as_delete_callback(as, arg);
11286 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11290 static uint_t
11291 fattr4_maxfilesize_to_bits(uint64_t ll)
11293 uint_t l = 1;
11295 if (ll == 0) {
11296 return (0);
11299 if (ll & 0xffffffff00000000) {
11300 l += 32; ll >>= 32;
11302 if (ll & 0xffff0000) {
11303 l += 16; ll >>= 16;
11305 if (ll & 0xff00) {
11306 l += 8; ll >>= 8;
11308 if (ll & 0xf0) {
11309 l += 4; ll >>= 4;
11311 if (ll & 0xc) {
11312 l += 2; ll >>= 2;
11314 if (ll & 0x2) {
11315 l += 1;
11317 return (l);
11320 static int
11321 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11323 vnode_t *avp = NULL;
11324 int error;
11326 if ((error = nfs4lookup_xattr(vp, "", &avp,
11327 LOOKUP_XATTR, cr)) == 0)
11328 error = do_xattr_exists_check(avp, valp, cr);
11329 if (avp)
11330 VN_RELE(avp);
11332 return (error);
11335 /* ARGSUSED */
11337 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11338 caller_context_t *ct)
11340 int error;
11341 hrtime_t t;
11342 rnode4_t *rp;
11343 nfs4_ga_res_t gar;
11344 nfs4_ga_ext_res_t ger;
11346 gar.n4g_ext_res = &ger;
11348 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11349 return (EIO);
11350 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11351 *valp = MAXPATHLEN;
11352 return (0);
11354 if (cmd == _PC_ACL_ENABLED) {
11355 *valp = _ACL_ACE_ENABLED;
11356 return (0);
11359 rp = VTOR4(vp);
11360 if (cmd == _PC_XATTR_EXISTS) {
11362 * The existence of the xattr directory is not sufficient
11363 * for determining whether generic user attributes exists.
11364 * The attribute directory could only be a transient directory
11365 * used for Solaris sysattr support. Do a small readdir
11366 * to verify if the only entries are sysattrs or not.
11368 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11369 * is NULL. Once the xadir vp exists, we can create xattrs,
11370 * and we don't have any way to update the "base" object's
11371 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11372 * could help out.
11374 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11375 rp->r_xattr_dir == NULL) {
11376 return (nfs4_have_xattrs(vp, valp, cr));
11378 } else { /* OLD CODE */
11379 if (ATTRCACHE4_VALID(vp)) {
11380 mutex_enter(&rp->r_statelock);
11381 if (rp->r_pathconf.pc4_cache_valid) {
11382 error = 0;
11383 switch (cmd) {
11384 case _PC_FILESIZEBITS:
11385 *valp =
11386 rp->r_pathconf.pc4_filesizebits;
11387 break;
11388 case _PC_LINK_MAX:
11389 *valp =
11390 rp->r_pathconf.pc4_link_max;
11391 break;
11392 case _PC_NAME_MAX:
11393 *valp =
11394 rp->r_pathconf.pc4_name_max;
11395 break;
11396 case _PC_CHOWN_RESTRICTED:
11397 *valp =
11398 rp->r_pathconf.pc4_chown_restricted;
11399 break;
11400 case _PC_NO_TRUNC:
11401 *valp =
11402 rp->r_pathconf.pc4_no_trunc;
11403 break;
11404 default:
11405 error = EINVAL;
11406 break;
11408 mutex_exit(&rp->r_statelock);
11409 #ifdef DEBUG
11410 nfs4_pathconf_cache_hits++;
11411 #endif
11412 return (error);
11414 mutex_exit(&rp->r_statelock);
11417 #ifdef DEBUG
11418 nfs4_pathconf_cache_misses++;
11419 #endif
11421 t = gethrtime();
11423 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11425 if (error) {
11426 mutex_enter(&rp->r_statelock);
11427 rp->r_pathconf.pc4_cache_valid = FALSE;
11428 rp->r_pathconf.pc4_xattr_valid = FALSE;
11429 mutex_exit(&rp->r_statelock);
11430 return (error);
11433 /* interpret the max filesize */
11434 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11435 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11437 /* Store the attributes we just received */
11438 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11440 switch (cmd) {
11441 case _PC_FILESIZEBITS:
11442 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11443 break;
11444 case _PC_LINK_MAX:
11445 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11446 break;
11447 case _PC_NAME_MAX:
11448 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11449 break;
11450 case _PC_CHOWN_RESTRICTED:
11451 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11452 break;
11453 case _PC_NO_TRUNC:
11454 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11455 break;
11456 case _PC_XATTR_EXISTS:
11457 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11458 if (error = nfs4_have_xattrs(vp, valp, cr))
11459 return (error);
11461 break;
11462 default:
11463 return (EINVAL);
11466 return (0);
11470 * Called by async thread to do synchronous pageio. Do the i/o, wait
11471 * for it to complete, and cleanup the page list when done.
11473 static int
11474 nfs4_sync_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11475 int flags, cred_t *cr)
11477 int error;
11479 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11481 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11482 if (flags & B_READ)
11483 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11484 else
11485 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11486 return (error);
11489 /* ARGSUSED */
11490 static int
11491 nfs4_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11492 int flags, cred_t *cr, caller_context_t *ct)
11494 int error;
11495 rnode4_t *rp;
11497 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11498 return (EIO);
11500 if (pp == NULL)
11501 return (EINVAL);
11503 rp = VTOR4(vp);
11504 mutex_enter(&rp->r_statelock);
11505 rp->r_count++;
11506 mutex_exit(&rp->r_statelock);
11508 if (flags & B_ASYNC) {
11509 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11510 nfs4_sync_pageio);
11511 } else
11512 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11513 mutex_enter(&rp->r_statelock);
11514 rp->r_count--;
11515 cv_broadcast(&rp->r_cv);
11516 mutex_exit(&rp->r_statelock);
11517 return (error);
11520 /* ARGSUSED */
11521 static void
11522 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11523 caller_context_t *ct)
11525 int error;
11526 rnode4_t *rp;
11527 page_t *plist;
11528 page_t *pptr;
11529 offset3 offset;
11530 count3 len;
11531 k_sigset_t smask;
11534 * We should get called with fl equal to either B_FREE or
11535 * B_INVAL. Any other value is illegal.
11537 * The page that we are either supposed to free or destroy
11538 * should be exclusive locked and its io lock should not
11539 * be held.
11541 ASSERT(fl == B_FREE || fl == B_INVAL);
11542 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11544 rp = VTOR4(vp);
11547 * If the page doesn't need to be committed or we shouldn't
11548 * even bother attempting to commit it, then just make sure
11549 * that the p_fsdata byte is clear and then either free or
11550 * destroy the page as appropriate.
11552 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11553 pp->p_fsdata = C_NOCOMMIT;
11554 if (fl == B_FREE)
11555 page_free(pp, dn);
11556 else
11557 page_destroy(pp, dn);
11558 return;
11562 * If there is a page invalidation operation going on, then
11563 * if this is one of the pages being destroyed, then just
11564 * clear the p_fsdata byte and then either free or destroy
11565 * the page as appropriate.
11567 mutex_enter(&rp->r_statelock);
11568 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11569 mutex_exit(&rp->r_statelock);
11570 pp->p_fsdata = C_NOCOMMIT;
11571 if (fl == B_FREE)
11572 page_free(pp, dn);
11573 else
11574 page_destroy(pp, dn);
11575 return;
11579 * If we are freeing this page and someone else is already
11580 * waiting to do a commit, then just unlock the page and
11581 * return. That other thread will take care of commiting
11582 * this page. The page can be freed sometime after the
11583 * commit has finished. Otherwise, if the page is marked
11584 * as delay commit, then we may be getting called from
11585 * pvn_write_done, one page at a time. This could result
11586 * in one commit per page, so we end up doing lots of small
11587 * commits instead of fewer larger commits. This is bad,
11588 * we want do as few commits as possible.
11590 if (fl == B_FREE) {
11591 if (rp->r_flags & R4COMMITWAIT) {
11592 page_unlock(pp);
11593 mutex_exit(&rp->r_statelock);
11594 return;
11596 if (pp->p_fsdata == C_DELAYCOMMIT) {
11597 pp->p_fsdata = C_COMMIT;
11598 page_unlock(pp);
11599 mutex_exit(&rp->r_statelock);
11600 return;
11605 * Check to see if there is a signal which would prevent an
11606 * attempt to commit the pages from being successful. If so,
11607 * then don't bother with all of the work to gather pages and
11608 * generate the unsuccessful RPC. Just return from here and
11609 * let the page be committed at some later time.
11611 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11612 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11613 sigunintr(&smask);
11614 page_unlock(pp);
11615 mutex_exit(&rp->r_statelock);
11616 return;
11618 sigunintr(&smask);
11621 * We are starting to need to commit pages, so let's try
11622 * to commit as many as possible at once to reduce the
11623 * overhead.
11625 * Set the `commit inprogress' state bit. We must
11626 * first wait until any current one finishes. Then
11627 * we initialize the c_pages list with this page.
11629 while (rp->r_flags & R4COMMIT) {
11630 rp->r_flags |= R4COMMITWAIT;
11631 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11632 rp->r_flags &= ~R4COMMITWAIT;
11634 rp->r_flags |= R4COMMIT;
11635 mutex_exit(&rp->r_statelock);
11636 ASSERT(rp->r_commit.c_pages == NULL);
11637 rp->r_commit.c_pages = pp;
11638 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11639 rp->r_commit.c_commlen = PAGESIZE;
11642 * Gather together all other pages which can be committed.
11643 * They will all be chained off r_commit.c_pages.
11645 nfs4_get_commit(vp);
11648 * Clear the `commit inprogress' status and disconnect
11649 * the list of pages to be committed from the rnode.
11650 * At this same time, we also save the starting offset
11651 * and length of data to be committed on the server.
11653 plist = rp->r_commit.c_pages;
11654 rp->r_commit.c_pages = NULL;
11655 offset = rp->r_commit.c_commbase;
11656 len = rp->r_commit.c_commlen;
11657 mutex_enter(&rp->r_statelock);
11658 rp->r_flags &= ~R4COMMIT;
11659 cv_broadcast(&rp->r_commit.c_cv);
11660 mutex_exit(&rp->r_statelock);
11662 if (curproc == proc_pageout || curproc == proc_fsflush ||
11663 nfs_zone() != VTOMI4(vp)->mi_zone) {
11664 nfs4_async_commit(vp, plist, offset, len,
11665 cr, do_nfs4_async_commit);
11666 return;
11670 * Actually generate the COMMIT op over the wire operation.
11672 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11675 * If we got an error during the commit, just unlock all
11676 * of the pages. The pages will get retransmitted to the
11677 * server during a putpage operation.
11679 if (error) {
11680 while (plist != NULL) {
11681 pptr = plist;
11682 page_sub(&plist, pptr);
11683 page_unlock(pptr);
11685 return;
11689 * We've tried as hard as we can to commit the data to stable
11690 * storage on the server. We just unlock the rest of the pages
11691 * and clear the commit required state. They will be put
11692 * onto the tail of the cachelist if they are nolonger
11693 * mapped.
11695 while (plist != pp) {
11696 pptr = plist;
11697 page_sub(&plist, pptr);
11698 pptr->p_fsdata = C_NOCOMMIT;
11699 page_unlock(pptr);
11703 * It is possible that nfs4_commit didn't return error but
11704 * some other thread has modified the page we are going
11705 * to free/destroy.
11706 * In this case we need to rewrite the page. Do an explicit check
11707 * before attempting to free/destroy the page. If modified, needs to
11708 * be rewritten so unlock the page and return.
11710 if (hat_ismod(pp)) {
11711 pp->p_fsdata = C_NOCOMMIT;
11712 page_unlock(pp);
11713 return;
11717 * Now, as appropriate, either free or destroy the page
11718 * that we were called with.
11720 pp->p_fsdata = C_NOCOMMIT;
11721 if (fl == B_FREE)
11722 page_free(pp, dn);
11723 else
11724 page_destroy(pp, dn);
11728 * Commit requires that the current fh be the file written to.
11729 * The compound op structure is:
11730 * PUTFH(file), COMMIT
11732 static int
11733 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11735 COMPOUND4args_clnt args;
11736 COMPOUND4res_clnt res;
11737 COMMIT4res *cm_res;
11738 nfs_argop4 argop[2];
11739 nfs_resop4 *resop;
11740 int doqueue;
11741 mntinfo4_t *mi;
11742 rnode4_t *rp;
11743 cred_t *cred_otw = NULL;
11744 bool_t needrecov = FALSE;
11745 nfs4_recov_state_t recov_state;
11746 nfs4_open_stream_t *osp = NULL;
11747 bool_t first_time = TRUE; /* first time getting OTW cred */
11748 bool_t last_time = FALSE; /* last time getting OTW cred */
11749 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11751 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11753 rp = VTOR4(vp);
11755 mi = VTOMI4(vp);
11756 recov_state.rs_flags = 0;
11757 recov_state.rs_num_retry_despite_err = 0;
11758 get_commit_cred:
11760 * Releases the osp, if a valid open stream is provided.
11761 * Puts a hold on the cred_otw and the new osp (if found).
11763 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11764 &first_time, &last_time);
11765 args.ctag = TAG_COMMIT;
11766 recov_retry:
11768 * Commit ops: putfh file; commit
11770 args.array_len = 2;
11771 args.array = argop;
11773 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11774 &recov_state, NULL);
11775 if (e.error) {
11776 crfree(cred_otw);
11777 if (osp != NULL)
11778 open_stream_rele(osp, rp);
11779 return (e.error);
11782 /* putfh directory */
11783 argop[0].argop = OP_CPUTFH;
11784 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11786 /* commit */
11787 argop[1].argop = OP_COMMIT;
11788 argop[1].nfs_argop4_u.opcommit.offset = offset;
11789 argop[1].nfs_argop4_u.opcommit.count = count;
11791 doqueue = 1;
11792 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11794 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11795 if (!needrecov && e.error) {
11796 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11797 needrecov);
11798 crfree(cred_otw);
11799 if (e.error == EACCES && last_time == FALSE)
11800 goto get_commit_cred;
11801 if (osp != NULL)
11802 open_stream_rele(osp, rp);
11803 return (e.error);
11806 if (needrecov) {
11807 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11808 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11809 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11810 &recov_state, needrecov);
11811 if (!e.error)
11812 (void) xdr_free(xdr_COMPOUND4res_clnt,
11813 (caddr_t)&res);
11814 goto recov_retry;
11816 if (e.error) {
11817 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11818 &recov_state, needrecov);
11819 crfree(cred_otw);
11820 if (osp != NULL)
11821 open_stream_rele(osp, rp);
11822 return (e.error);
11824 /* fall through for res.status case */
11827 if (res.status) {
11828 e.error = geterrno4(res.status);
11829 if (e.error == EACCES && last_time == FALSE) {
11830 crfree(cred_otw);
11831 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11832 &recov_state, needrecov);
11833 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11834 goto get_commit_cred;
11837 * Can't do a nfs4_purge_stale_fh here because this
11838 * can cause a deadlock. nfs4_commit can
11839 * be called from nfs4_dispose which can be called
11840 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11841 * can call back to pvn_vplist_dirty.
11843 if (e.error == ESTALE) {
11844 mutex_enter(&rp->r_statelock);
11845 rp->r_flags |= R4STALE;
11846 if (!rp->r_error)
11847 rp->r_error = e.error;
11848 mutex_exit(&rp->r_statelock);
11849 PURGE_ATTRCACHE4(vp);
11850 } else {
11851 mutex_enter(&rp->r_statelock);
11852 if (!rp->r_error)
11853 rp->r_error = e.error;
11854 mutex_exit(&rp->r_statelock);
11856 } else {
11857 ASSERT(rp->r_flags & R4HAVEVERF);
11858 resop = &res.array[1]; /* commit res */
11859 cm_res = &resop->nfs_resop4_u.opcommit;
11860 mutex_enter(&rp->r_statelock);
11861 if (cm_res->writeverf == rp->r_writeverf) {
11862 mutex_exit(&rp->r_statelock);
11863 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11864 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11865 &recov_state, needrecov);
11866 crfree(cred_otw);
11867 if (osp != NULL)
11868 open_stream_rele(osp, rp);
11869 return (0);
11871 nfs4_set_mod(vp);
11872 rp->r_writeverf = cm_res->writeverf;
11873 mutex_exit(&rp->r_statelock);
11874 e.error = NFS_VERF_MISMATCH;
11877 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11878 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11879 crfree(cred_otw);
11880 if (osp != NULL)
11881 open_stream_rele(osp, rp);
11883 return (e.error);
11886 static void
11887 nfs4_set_mod(vnode_t *vp)
11889 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11891 /* make sure we're looking at the master vnode, not a shadow */
11892 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11896 * This function is used to gather a page list of the pages which
11897 * can be committed on the server.
11899 * The calling thread must have set R4COMMIT. This bit is used to
11900 * serialize access to the commit structure in the rnode. As long
11901 * as the thread has set R4COMMIT, then it can manipulate the commit
11902 * structure without requiring any other locks.
11904 * When this function is called from nfs4_dispose() the page passed
11905 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11906 * will skip it. This is not a problem since we initially add the
11907 * page to the r_commit page list.
11910 static void
11911 nfs4_get_commit(vnode_t *vp)
11913 rnode4_t *rp;
11914 page_t *pp;
11915 kmutex_t *vphm;
11917 rp = VTOR4(vp);
11919 ASSERT(rp->r_flags & R4COMMIT);
11921 /* make sure we're looking at the master vnode, not a shadow */
11923 if (IS_SHADOW(vp, rp))
11924 vp = RTOV4(rp);
11926 vphm = page_vnode_mutex(vp);
11927 mutex_enter(vphm);
11930 * Step through all of the pages associated with this vnode
11931 * looking for pages which need to be committed.
11933 for (pp = vnode_get_head(vp);
11934 pp != NULL;
11935 pp = vnode_get_next(vp, pp)) {
11936 /* Skip marker pages. */
11937 if (PP_ISPVN_TAG(pp))
11938 continue;
11941 * First short-cut everything (without the page_lock)
11942 * and see if this page does not need to be committed
11943 * or is modified if so then we'll just skip it.
11945 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11946 continue;
11949 * Attempt to lock the page. If we can't, then
11950 * someone else is messing with it or we have been
11951 * called from nfs4_dispose and this is the page that
11952 * nfs4_dispose was called with.. anyway just skip it.
11954 if (!page_trylock(pp, SE_EXCL))
11955 continue;
11958 * Lets check again now that we have the page lock.
11960 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11961 page_unlock(pp);
11962 continue;
11965 /* this had better not be a free page */
11966 ASSERT(PP_ISFREE(pp) == 0);
11969 * The page needs to be committed and we locked it.
11970 * Update the base and length parameters and add it
11971 * to r_pages.
11973 if (rp->r_commit.c_pages == NULL) {
11974 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11975 rp->r_commit.c_commlen = PAGESIZE;
11976 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11977 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11978 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11979 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11980 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11981 <= pp->p_offset) {
11982 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11983 rp->r_commit.c_commbase + PAGESIZE;
11985 page_add(&rp->r_commit.c_pages, pp);
11988 mutex_exit(vphm);
11992 * This routine is used to gather together a page list of the pages
11993 * which are to be committed on the server. This routine must not
11994 * be called if the calling thread holds any locked pages.
11996 * The calling thread must have set R4COMMIT. This bit is used to
11997 * serialize access to the commit structure in the rnode. As long
11998 * as the thread has set R4COMMIT, then it can manipulate the commit
11999 * structure without requiring any other locks.
12001 static void
12002 nfs4_get_commit_range(vnode_t *vp, uoff_t soff, size_t len)
12005 rnode4_t *rp;
12006 page_t *pp;
12007 uoff_t end;
12008 uoff_t off;
12009 ASSERT(len != 0);
12010 rp = VTOR4(vp);
12011 ASSERT(rp->r_flags & R4COMMIT);
12013 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12015 /* make sure we're looking at the master vnode, not a shadow */
12017 if (IS_SHADOW(vp, rp))
12018 vp = RTOV4(rp);
12021 * If there are no pages associated with this vnode, then
12022 * just return.
12024 if (!vn_has_cached_data(vp))
12025 return;
12027 * Calculate the ending offset.
12029 end = soff + len;
12030 for (off = soff; off < end; off += PAGESIZE) {
12032 * Lookup each page by vp, offset.
12034 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12035 continue;
12037 * If this page does not need to be committed or is
12038 * modified, then just skip it.
12040 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12041 page_unlock(pp);
12042 continue;
12045 ASSERT(PP_ISFREE(pp) == 0);
12047 * The page needs to be committed and we locked it.
12048 * Update the base and length parameters and add it
12049 * to r_pages.
12051 if (rp->r_commit.c_pages == NULL) {
12052 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12053 rp->r_commit.c_commlen = PAGESIZE;
12054 } else {
12055 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12056 rp->r_commit.c_commbase + PAGESIZE;
12058 page_add(&rp->r_commit.c_pages, pp);
12063 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12064 * Flushes and commits data to the server.
12066 static int
12067 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12069 int error;
12070 verifier4 write_verf;
12071 rnode4_t *rp = VTOR4(vp);
12073 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12076 * Flush the data portion of the file and then commit any
12077 * portions which need to be committed. This may need to
12078 * be done twice if the server has changed state since
12079 * data was last written. The data will need to be
12080 * rewritten to the server and then a new commit done.
12082 * In fact, this may need to be done several times if the
12083 * server is having problems and crashing while we are
12084 * attempting to do this.
12087 top:
12089 * Do a flush based on the poff and plen arguments. This
12090 * will synchronously write out any modified pages in the
12091 * range specified by (poff, plen). This starts all of the
12092 * i/o operations which will be waited for in the next
12093 * call to nfs4_putpage
12096 mutex_enter(&rp->r_statelock);
12097 write_verf = rp->r_writeverf;
12098 mutex_exit(&rp->r_statelock);
12100 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12101 if (error == EAGAIN)
12102 error = 0;
12105 * Do a flush based on the poff and plen arguments. This
12106 * will synchronously write out any modified pages in the
12107 * range specified by (poff, plen) and wait until all of
12108 * the asynchronous i/o's in that range are done as well.
12110 if (!error)
12111 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12113 if (error)
12114 return (error);
12116 mutex_enter(&rp->r_statelock);
12117 if (rp->r_writeverf != write_verf) {
12118 mutex_exit(&rp->r_statelock);
12119 goto top;
12121 mutex_exit(&rp->r_statelock);
12124 * Now commit any pages which might need to be committed.
12125 * If the error, NFS_VERF_MISMATCH, is returned, then
12126 * start over with the flush operation.
12128 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12130 if (error == NFS_VERF_MISMATCH)
12131 goto top;
12133 return (error);
12137 * nfs4_commit_vp() will wait for other pending commits and
12138 * will either commit the whole file or a range, plen dictates
12139 * if we commit whole file. a value of zero indicates the whole
12140 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12142 static int
12143 nfs4_commit_vp(vnode_t *vp, uoff_t poff, size_t plen,
12144 cred_t *cr, int wait_on_writes)
12146 rnode4_t *rp;
12147 page_t *plist;
12148 offset3 offset;
12149 count3 len;
12151 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12153 rp = VTOR4(vp);
12156 * before we gather commitable pages make
12157 * sure there are no outstanding async writes
12159 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12160 mutex_enter(&rp->r_statelock);
12161 while (rp->r_count > 0) {
12162 cv_wait(&rp->r_cv, &rp->r_statelock);
12164 mutex_exit(&rp->r_statelock);
12168 * Set the `commit inprogress' state bit. We must
12169 * first wait until any current one finishes.
12171 mutex_enter(&rp->r_statelock);
12172 while (rp->r_flags & R4COMMIT) {
12173 rp->r_flags |= R4COMMITWAIT;
12174 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12175 rp->r_flags &= ~R4COMMITWAIT;
12177 rp->r_flags |= R4COMMIT;
12178 mutex_exit(&rp->r_statelock);
12181 * Gather all of the pages which need to be
12182 * committed.
12184 if (plen == 0)
12185 nfs4_get_commit(vp);
12186 else
12187 nfs4_get_commit_range(vp, poff, plen);
12190 * Clear the `commit inprogress' bit and disconnect the
12191 * page list which was gathered by nfs4_get_commit.
12193 plist = rp->r_commit.c_pages;
12194 rp->r_commit.c_pages = NULL;
12195 offset = rp->r_commit.c_commbase;
12196 len = rp->r_commit.c_commlen;
12197 mutex_enter(&rp->r_statelock);
12198 rp->r_flags &= ~R4COMMIT;
12199 cv_broadcast(&rp->r_commit.c_cv);
12200 mutex_exit(&rp->r_statelock);
12203 * If any pages need to be committed, commit them and
12204 * then unlock them so that they can be freed some
12205 * time later.
12207 if (plist == NULL)
12208 return (0);
12211 * No error occurred during the flush portion
12212 * of this operation, so now attempt to commit
12213 * the data to stable storage on the server.
12215 * This will unlock all of the pages on the list.
12217 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12220 static int
12221 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12222 cred_t *cr)
12224 int error;
12225 page_t *pp;
12227 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12229 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12232 * If we got an error, then just unlock all of the pages
12233 * on the list.
12235 if (error) {
12236 while (plist != NULL) {
12237 pp = plist;
12238 page_sub(&plist, pp);
12239 page_unlock(pp);
12241 return (error);
12244 * We've tried as hard as we can to commit the data to stable
12245 * storage on the server. We just unlock the pages and clear
12246 * the commit required state. They will get freed later.
12248 while (plist != NULL) {
12249 pp = plist;
12250 page_sub(&plist, pp);
12251 pp->p_fsdata = C_NOCOMMIT;
12252 page_unlock(pp);
12255 return (error);
12258 static void
12259 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12260 cred_t *cr)
12263 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12266 /*ARGSUSED*/
12267 static int
12268 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12269 caller_context_t *ct)
12271 int error = 0;
12272 mntinfo4_t *mi;
12273 vattr_t va;
12274 vsecattr_t nfsace4_vsap;
12276 mi = VTOMI4(vp);
12277 if (nfs_zone() != mi->mi_zone)
12278 return (EIO);
12279 if (mi->mi_flags & MI4_ACL) {
12280 /* if we have a delegation, return it */
12281 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12282 (void) nfs4delegreturn(VTOR4(vp),
12283 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12285 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12286 NFS4_ACL_SET);
12287 if (error) /* EINVAL */
12288 return (error);
12290 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12292 * These are aclent_t type entries.
12294 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12295 vp->v_type == VDIR, FALSE);
12296 if (error)
12297 return (error);
12298 } else {
12300 * These are ace_t type entries.
12302 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12303 FALSE);
12304 if (error)
12305 return (error);
12307 bzero(&va, sizeof (va));
12308 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12309 vs_ace4_destroy(&nfsace4_vsap);
12310 return (error);
12312 return (ENOSYS);
12315 /* ARGSUSED */
12317 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12318 caller_context_t *ct)
12320 int error;
12321 mntinfo4_t *mi;
12322 nfs4_ga_res_t gar;
12323 rnode4_t *rp = VTOR4(vp);
12325 mi = VTOMI4(vp);
12326 if (nfs_zone() != mi->mi_zone)
12327 return (EIO);
12329 bzero(&gar, sizeof (gar));
12330 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12333 * vsecattr->vsa_mask holds the original acl request mask.
12334 * This is needed when determining what to return.
12335 * (See: nfs4_create_getsecattr_return())
12337 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12338 if (error) /* EINVAL */
12339 return (error);
12342 * If this is a referral stub, don't try to go OTW for an ACL
12344 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12345 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12347 if (mi->mi_flags & MI4_ACL) {
12349 * Check if the data is cached and the cache is valid. If it
12350 * is we don't go over the wire.
12352 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12353 mutex_enter(&rp->r_statelock);
12354 if (rp->r_secattr != NULL) {
12355 error = nfs4_create_getsecattr_return(
12356 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12357 rp->r_attr.va_gid,
12358 vp->v_type == VDIR);
12359 if (!error) { /* error == 0 - Success! */
12360 mutex_exit(&rp->r_statelock);
12361 return (error);
12364 mutex_exit(&rp->r_statelock);
12368 * The getattr otw call will always get both the acl, in
12369 * the form of a list of nfsace4's, and the number of acl
12370 * entries; independent of the value of gar.n4g_va.va_mask.
12372 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12373 if (error) {
12374 vs_ace4_destroy(&gar.n4g_vsa);
12375 if (error == ENOTSUP || error == EOPNOTSUPP)
12376 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12377 return (error);
12380 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12382 * No error was returned, but according to the response
12383 * bitmap, neither was an acl.
12385 vs_ace4_destroy(&gar.n4g_vsa);
12386 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12387 return (error);
12391 * Update the cache with the ACL.
12393 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12395 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12396 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12397 vp->v_type == VDIR);
12398 vs_ace4_destroy(&gar.n4g_vsa);
12399 if ((error) && (vsecattr->vsa_mask &
12400 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12401 (error != EACCES)) {
12402 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12404 return (error);
12406 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12407 return (error);
12411 * The function returns:
12412 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12413 * - EINVAL if the passed in "acl_mask" is an invalid request.
12415 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12416 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12418 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12419 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12420 * - We have a count field set without the corresponding acl field set. (e.g. -
12421 * VSA_ACECNT is set, but VSA_ACE is not)
12423 static int
12424 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12426 /* Shortcut the masks that are always valid. */
12427 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12428 return (0);
12429 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12430 return (0);
12432 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12434 * We can't have any VSA_ACL type stuff in the mask now.
12436 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12437 VSA_DFACLCNT))
12438 return (EINVAL);
12440 if (op == NFS4_ACL_SET) {
12441 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12442 return (EINVAL);
12446 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12448 * We can't have any VSA_ACE type stuff in the mask now.
12450 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12451 return (EINVAL);
12453 if (op == NFS4_ACL_SET) {
12454 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12455 return (EINVAL);
12457 if ((acl_mask & VSA_DFACLCNT) &&
12458 !(acl_mask & VSA_DFACL))
12459 return (EINVAL);
12462 return (0);
12466 * The theory behind creating the correct getsecattr return is simply this:
12467 * "Don't return anything that the caller is not expecting to have to free."
12469 static int
12470 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12471 uid_t uid, gid_t gid, int isdir)
12473 int error = 0;
12474 /* Save the mask since the translators modify it. */
12475 uint_t orig_mask = vsap->vsa_mask;
12477 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12478 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12480 if (error)
12481 return (error);
12484 * If the caller only asked for the ace count (VSA_ACECNT)
12485 * don't give them the full acl (VSA_ACE), free it.
12487 if (!orig_mask & VSA_ACE) {
12488 if (vsap->vsa_aclentp != NULL) {
12489 kmem_free(vsap->vsa_aclentp,
12490 vsap->vsa_aclcnt * sizeof (ace_t));
12491 vsap->vsa_aclentp = NULL;
12494 vsap->vsa_mask = orig_mask;
12496 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12497 VSA_DFACLCNT)) {
12498 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12499 isdir, FALSE);
12501 if (error)
12502 return (error);
12505 * If the caller only asked for the acl count (VSA_ACLCNT)
12506 * and/or the default acl count (VSA_DFACLCNT) don't give them
12507 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12509 if (!orig_mask & VSA_ACL) {
12510 if (vsap->vsa_aclentp != NULL) {
12511 kmem_free(vsap->vsa_aclentp,
12512 vsap->vsa_aclcnt * sizeof (aclent_t));
12513 vsap->vsa_aclentp = NULL;
12517 if (!orig_mask & VSA_DFACL) {
12518 if (vsap->vsa_dfaclentp != NULL) {
12519 kmem_free(vsap->vsa_dfaclentp,
12520 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12521 vsap->vsa_dfaclentp = NULL;
12524 vsap->vsa_mask = orig_mask;
12526 return (0);
12529 /* ARGSUSED */
12531 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12532 caller_context_t *ct)
12534 int error;
12536 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12537 return (EIO);
12539 * check for valid cmd parameter
12541 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12542 return (EINVAL);
12545 * Check access permissions
12547 if ((cmd & F_SHARE) &&
12548 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12549 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12550 return (EBADF);
12553 * If the filesystem is mounted using local locking, pass the
12554 * request off to the local share code.
12556 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12557 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12559 switch (cmd) {
12560 case F_SHARE:
12561 case F_UNSHARE:
12563 * This will be properly implemented later,
12564 * see RFE: 4823948 .
12566 error = EAGAIN;
12567 break;
12569 case F_HASREMOTELOCKS:
12571 * NFS client can't store remote locks itself
12573 shr->s_access = 0;
12574 error = 0;
12575 break;
12577 default:
12578 error = EINVAL;
12579 break;
12582 return (error);
12586 * Common code called by directory ops to update the attrcache
12588 static int
12589 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12590 hrtime_t t, vnode_t *vp, cred_t *cr)
12592 int error = 0;
12594 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12596 if (status != NFS4_OK) {
12597 /* getattr not done or failed */
12598 PURGE_ATTRCACHE4(vp);
12599 return (error);
12602 if (garp) {
12603 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12604 } else {
12605 PURGE_ATTRCACHE4(vp);
12607 return (error);
12611 * Update directory caches for directory modification ops (link, rename, etc.)
12612 * When dinfo is NULL, manage dircaches in the old way.
12614 static void
12615 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12616 dirattr_info_t *dinfo)
12618 rnode4_t *drp = VTOR4(dvp);
12620 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12622 /* Purge rddir cache for dir since it changed */
12623 if (drp->r_dir != NULL)
12624 nfs4_purge_rddir_cache(dvp);
12627 * If caller provided dinfo, then use it to manage dir caches.
12629 if (dinfo != NULL) {
12630 if (vp != NULL) {
12631 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12632 if (!VTOR4(vp)->created_v4) {
12633 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12634 dnlc_update(dvp, nm, vp);
12635 } else {
12637 * XXX don't update if the created_v4 flag is
12638 * set
12640 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12641 NFS4_DEBUG(nfs4_client_state_debug,
12642 (CE_NOTE, "nfs4_update_dircaches: "
12643 "don't update dnlc: created_v4 flag"));
12647 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12648 dinfo->di_cred, FALSE, cinfo);
12650 return;
12654 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12655 * Since caller modified dir but didn't receive post-dirmod-op dir
12656 * attrs, the dir's attrs must be purged.
12658 * XXX this check and dnlc update/purge should really be atomic,
12659 * XXX but can't use rnode statelock because it'll deadlock in
12660 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12661 * XXX does occur.
12663 * XXX We also may want to check that atomic is true in the
12664 * XXX change_info struct. If it is not, the change_info may
12665 * XXX reflect changes by more than one clients which means that
12666 * XXX our cache may not be valid.
12668 PURGE_ATTRCACHE4(dvp);
12669 if (drp->r_change == cinfo->before) {
12670 /* no changes took place in the directory prior to our link */
12671 if (vp != NULL) {
12672 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12673 if (!VTOR4(vp)->created_v4) {
12674 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12675 dnlc_update(dvp, nm, vp);
12676 } else {
12678 * XXX dont' update if the created_v4 flag
12679 * is set
12681 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12682 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12683 "nfs4_update_dircaches: don't"
12684 " update dnlc: created_v4 flag"));
12687 } else {
12688 /* Another client modified directory - purge its dnlc cache */
12689 dnlc_purge_vp(dvp);
12694 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12695 * file.
12697 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12698 * file (ie: client recovery) and otherwise set to FALSE.
12700 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12701 * initiated) calling functions.
12703 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12704 * of resending a 'lost' open request.
12706 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12707 * server that hands out BAD_SEQID on open confirm.
12709 * Errors are returned via the nfs4_error_t parameter.
12711 void
12712 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12713 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12714 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12716 COMPOUND4args_clnt args;
12717 COMPOUND4res_clnt res;
12718 nfs_argop4 argop[2];
12719 nfs_resop4 *resop;
12720 int doqueue = 1;
12721 mntinfo4_t *mi;
12722 OPEN_CONFIRM4args *open_confirm_args;
12723 int needrecov;
12725 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12726 #if DEBUG
12727 mutex_enter(&oop->oo_lock);
12728 ASSERT(oop->oo_seqid_inuse);
12729 mutex_exit(&oop->oo_lock);
12730 #endif
12732 recov_retry_confirm:
12733 nfs4_error_zinit(ep);
12734 *retry_open = FALSE;
12736 if (resend)
12737 args.ctag = TAG_OPEN_CONFIRM_LOST;
12738 else
12739 args.ctag = TAG_OPEN_CONFIRM;
12741 args.array_len = 2;
12742 args.array = argop;
12744 /* putfh target fh */
12745 argop[0].argop = OP_CPUTFH;
12746 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12748 argop[1].argop = OP_OPEN_CONFIRM;
12749 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12751 (*seqid) += 1;
12752 open_confirm_args->seqid = *seqid;
12753 open_confirm_args->open_stateid = *stateid;
12755 mi = VTOMI4(vp);
12757 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12759 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12760 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12763 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12764 if (!needrecov && ep->error)
12765 return;
12767 if (needrecov) {
12768 bool_t abort = FALSE;
12770 if (reopening_file == FALSE) {
12771 nfs4_bseqid_entry_t *bsep = NULL;
12773 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12774 bsep = nfs4_create_bseqid_entry(oop, NULL,
12775 vp, 0, args.ctag,
12776 open_confirm_args->seqid);
12778 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12779 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12780 if (bsep) {
12781 kmem_free(bsep, sizeof (*bsep));
12782 if (num_bseqid_retryp &&
12783 --(*num_bseqid_retryp) == 0)
12784 abort = TRUE;
12787 if ((ep->error == ETIMEDOUT ||
12788 res.status == NFS4ERR_RESOURCE) &&
12789 abort == FALSE && resend == FALSE) {
12790 if (!ep->error)
12791 (void) xdr_free(xdr_COMPOUND4res_clnt,
12792 (caddr_t)&res);
12794 delay(SEC_TO_TICK(confirm_retry_sec));
12795 goto recov_retry_confirm;
12797 /* State may have changed so retry the entire OPEN op */
12798 if (abort == FALSE)
12799 *retry_open = TRUE;
12800 else
12801 *retry_open = FALSE;
12802 if (!ep->error)
12803 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12804 return;
12807 if (res.status) {
12808 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12809 return;
12812 resop = &res.array[1]; /* open confirm res */
12813 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12814 stateid, sizeof (*stateid));
12816 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12820 * Return the credentials associated with a client state object. The
12821 * caller is responsible for freeing the credentials.
12824 static cred_t *
12825 state_to_cred(nfs4_open_stream_t *osp)
12827 cred_t *cr;
12830 * It's ok to not lock the open stream and open owner to get
12831 * the oo_cred since this is only written once (upon creation)
12832 * and will not change.
12834 cr = osp->os_open_owner->oo_cred;
12835 crhold(cr);
12837 return (cr);
12841 * nfs4_find_sysid
12843 * Find the sysid for the knetconfig associated with the given mi.
12845 static struct lm_sysid *
12846 nfs4_find_sysid(mntinfo4_t *mi)
12848 ASSERT(nfs_zone() == mi->mi_zone);
12851 * Switch from RDMA knconf to original mount knconf
12853 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12854 mi->mi_curr_serv->sv_hostname, NULL));
12857 #ifdef DEBUG
12859 * Return a string version of the call type for easy reading.
12861 static char *
12862 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12864 switch (ctype) {
12865 case NFS4_LCK_CTYPE_NORM:
12866 return ("NORMAL");
12867 case NFS4_LCK_CTYPE_RECLAIM:
12868 return ("RECLAIM");
12869 case NFS4_LCK_CTYPE_RESEND:
12870 return ("RESEND");
12871 case NFS4_LCK_CTYPE_REINSTATE:
12872 return ("REINSTATE");
12873 default:
12874 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12875 "type %d", ctype);
12876 return ("");
12879 #endif
12882 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12883 * Unlock requests don't have an over-the-wire locktype, so we just return
12884 * something non-threatening.
12887 static nfs_lock_type4
12888 flk_to_locktype(int cmd, int l_type)
12890 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12892 switch (l_type) {
12893 case F_UNLCK:
12894 return (READ_LT);
12895 case F_RDLCK:
12896 if (cmd == F_SETLK)
12897 return (READ_LT);
12898 else
12899 return (READW_LT);
12900 case F_WRLCK:
12901 if (cmd == F_SETLK)
12902 return (WRITE_LT);
12903 else
12904 return (WRITEW_LT);
12906 panic("flk_to_locktype");
12907 /*NOTREACHED*/
12911 * Do some preliminary checks for nfs4frlock.
12913 static int
12914 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12915 uoff_t offset)
12917 int error = 0;
12920 * If we are setting a lock, check that the file is opened
12921 * with the correct mode.
12923 if (cmd == F_SETLK || cmd == F_SETLKW) {
12924 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12925 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12926 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12927 "nfs4frlock_validate_args: file was opened with "
12928 "incorrect mode"));
12929 return (EBADF);
12933 /* Convert the offset. It may need to be restored before returning. */
12934 if (error = convoff(vp, flk, 0, offset)) {
12935 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12936 "nfs4frlock_validate_args: convoff => error= %d\n",
12937 error));
12938 return (error);
12941 return (error);
12945 * Set the flock64's lm_sysid for nfs4frlock.
12947 static int
12948 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12950 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12952 /* Find the lm_sysid */
12953 *lspp = nfs4_find_sysid(VTOMI4(vp));
12955 if (*lspp == NULL) {
12956 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12957 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12958 return (ENOLCK);
12961 flk->l_sysid = lm_sysidt(*lspp);
12963 return (0);
12967 * Do the remaining preliminary setup for nfs4frlock.
12969 static void
12970 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12971 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12972 cred_t **cred_otw)
12975 * set tick_delay to the base delay time.
12976 * (NFS4_BASE_WAIT_TIME is in secs)
12979 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12982 * If lock is relative to EOF, we need the newest length of the
12983 * file. Therefore invalidate the ATTR_CACHE.
12986 *whencep = flk->l_whence;
12988 if (*whencep == 2) /* SEEK_END */
12989 PURGE_ATTRCACHE4(vp);
12991 recov_statep->rs_flags = 0;
12992 recov_statep->rs_num_retry_despite_err = 0;
12993 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12997 * Initialize and allocate the data structures necessary for
12998 * the nfs4frlock call.
12999 * Allocates argsp's op array.
13001 static void
13002 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
13003 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13004 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13005 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13007 int argoplist_size;
13008 int num_ops = 2;
13010 *retry = FALSE;
13011 *did_start_fop = FALSE;
13012 *skip_get_err = FALSE;
13013 lost_rqstp->lr_op = 0;
13014 argoplist_size = num_ops * sizeof (nfs_argop4);
13015 /* fill array with zero */
13016 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13018 *argspp = argsp;
13019 *respp = NULL;
13021 argsp->array_len = num_ops;
13022 argsp->array = *argopp;
13024 /* initialize in case of error; will get real value down below */
13025 argsp->ctag = TAG_NONE;
13027 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13028 *op_hintp = OH_LOCKU;
13029 else
13030 *op_hintp = OH_OTHER;
13034 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13035 * the proper nfs4_server_t for this instance of nfs4frlock.
13036 * Returns 0 (success) or an errno value.
13038 static int
13039 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13040 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13041 bool_t *did_start_fop, bool_t *startrecovp)
13043 int error = 0;
13044 rnode4_t *rp;
13046 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13048 if (ctype == NFS4_LCK_CTYPE_NORM) {
13049 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13050 recov_statep, startrecovp);
13051 if (error)
13052 return (error);
13053 *did_start_fop = TRUE;
13054 } else {
13055 *did_start_fop = FALSE;
13056 *startrecovp = FALSE;
13059 if (!error) {
13060 rp = VTOR4(vp);
13062 /* If the file failed recovery, just quit. */
13063 mutex_enter(&rp->r_statelock);
13064 if (rp->r_flags & R4RECOVERR) {
13065 error = EIO;
13067 mutex_exit(&rp->r_statelock);
13070 return (error);
13074 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13075 * resend nfs4frlock call is initiated by the recovery framework.
13076 * Acquires the lop and oop seqid synchronization.
13078 static void
13079 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13080 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13081 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13082 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13084 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13085 int error;
13087 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13088 (CE_NOTE,
13089 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13090 ASSERT(resend_rqstp != NULL);
13091 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13092 resend_rqstp->lr_op == OP_LOCKU);
13094 *oopp = resend_rqstp->lr_oop;
13095 if (resend_rqstp->lr_oop) {
13096 open_owner_hold(resend_rqstp->lr_oop);
13097 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13098 ASSERT(error == 0); /* recov thread always succeeds */
13101 /* Must resend this lost lock/locku request. */
13102 ASSERT(resend_rqstp->lr_lop != NULL);
13103 *lopp = resend_rqstp->lr_lop;
13104 lock_owner_hold(resend_rqstp->lr_lop);
13105 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13106 ASSERT(error == 0); /* recov thread always succeeds */
13108 *ospp = resend_rqstp->lr_osp;
13109 if (*ospp)
13110 open_stream_hold(resend_rqstp->lr_osp);
13112 if (resend_rqstp->lr_op == OP_LOCK) {
13113 LOCK4args *lock_args;
13115 argop->argop = OP_LOCK;
13116 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13117 lock_args->locktype = resend_rqstp->lr_locktype;
13118 lock_args->reclaim =
13119 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13120 lock_args->offset = resend_rqstp->lr_flk->l_start;
13121 lock_args->length = resend_rqstp->lr_flk->l_len;
13122 if (lock_args->length == 0)
13123 lock_args->length = ~lock_args->length;
13124 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13125 mi2clientid(mi), &lock_args->locker);
13127 switch (resend_rqstp->lr_ctype) {
13128 case NFS4_LCK_CTYPE_RESEND:
13129 argsp->ctag = TAG_LOCK_RESEND;
13130 break;
13131 case NFS4_LCK_CTYPE_REINSTATE:
13132 argsp->ctag = TAG_LOCK_REINSTATE;
13133 break;
13134 case NFS4_LCK_CTYPE_RECLAIM:
13135 argsp->ctag = TAG_LOCK_RECLAIM;
13136 break;
13137 default:
13138 argsp->ctag = TAG_LOCK_UNKNOWN;
13139 break;
13141 } else {
13142 LOCKU4args *locku_args;
13143 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13145 argop->argop = OP_LOCKU;
13146 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13147 locku_args->locktype = READ_LT;
13148 locku_args->seqid = lop->lock_seqid + 1;
13149 mutex_enter(&lop->lo_lock);
13150 locku_args->lock_stateid = lop->lock_stateid;
13151 mutex_exit(&lop->lo_lock);
13152 locku_args->offset = resend_rqstp->lr_flk->l_start;
13153 locku_args->length = resend_rqstp->lr_flk->l_len;
13154 if (locku_args->length == 0)
13155 locku_args->length = ~locku_args->length;
13157 switch (resend_rqstp->lr_ctype) {
13158 case NFS4_LCK_CTYPE_RESEND:
13159 argsp->ctag = TAG_LOCKU_RESEND;
13160 break;
13161 case NFS4_LCK_CTYPE_REINSTATE:
13162 argsp->ctag = TAG_LOCKU_REINSTATE;
13163 break;
13164 default:
13165 argsp->ctag = TAG_LOCK_UNKNOWN;
13166 break;
13172 * Setup the LOCKT4 arguments.
13174 static void
13175 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13176 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13177 rnode4_t *rp)
13179 LOCKT4args *lockt_args;
13181 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13182 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13183 argop->argop = OP_LOCKT;
13184 argsp->ctag = TAG_LOCKT;
13185 lockt_args = &argop->nfs_argop4_u.oplockt;
13188 * The locktype will be READ_LT unless it's
13189 * a write lock. We do this because the Solaris
13190 * system call allows the combination of
13191 * F_UNLCK and F_GETLK* and so in that case the
13192 * unlock is mapped to a read.
13194 if (flk->l_type == F_WRLCK)
13195 lockt_args->locktype = WRITE_LT;
13196 else
13197 lockt_args->locktype = READ_LT;
13199 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13200 /* set the lock owner4 args */
13201 nfs4_setlockowner_args(&lockt_args->owner, rp,
13202 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13203 flk->l_pid);
13204 lockt_args->offset = flk->l_start;
13205 lockt_args->length = flk->l_len;
13206 if (flk->l_len == 0)
13207 lockt_args->length = ~lockt_args->length;
13209 *lockt_argsp = lockt_args;
13213 * If the client is holding a delegation, and the open stream to be used
13214 * with this lock request is a delegation open stream, then re-open the stream.
13215 * Sets the nfs4_error_t to all zeros unless the open stream has already
13216 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13217 * means the caller should retry (like a recovery retry).
13219 static void
13220 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13222 open_delegation_type4 dt;
13223 bool_t reopen_needed, force;
13224 nfs4_open_stream_t *osp;
13225 open_claim_type4 oclaim;
13226 rnode4_t *rp = VTOR4(vp);
13227 mntinfo4_t *mi = VTOMI4(vp);
13229 ASSERT(nfs_zone() == mi->mi_zone);
13231 nfs4_error_zinit(ep);
13233 mutex_enter(&rp->r_statev4_lock);
13234 dt = rp->r_deleg_type;
13235 mutex_exit(&rp->r_statev4_lock);
13237 if (dt != OPEN_DELEGATE_NONE) {
13238 nfs4_open_owner_t *oop;
13240 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13241 if (!oop) {
13242 ep->stat = NFS4ERR_IO;
13243 return;
13245 /* returns with 'os_sync_lock' held */
13246 osp = find_open_stream(oop, rp);
13247 if (!osp) {
13248 open_owner_rele(oop);
13249 ep->stat = NFS4ERR_IO;
13250 return;
13253 if (osp->os_failed_reopen) {
13254 NFS4_DEBUG((nfs4_open_stream_debug ||
13255 nfs4_client_lock_debug), (CE_NOTE,
13256 "nfs4frlock_check_deleg: os_failed_reopen set "
13257 "for osp %p, cr %p, rp %s", (void *)osp,
13258 (void *)cr, rnode4info(rp)));
13259 mutex_exit(&osp->os_sync_lock);
13260 open_stream_rele(osp, rp);
13261 open_owner_rele(oop);
13262 ep->stat = NFS4ERR_IO;
13263 return;
13267 * Determine whether a reopen is needed. If this
13268 * is a delegation open stream, then send the open
13269 * to the server to give visibility to the open owner.
13270 * Even if it isn't a delegation open stream, we need
13271 * to check if the previous open CLAIM_DELEGATE_CUR
13272 * was sufficient.
13275 reopen_needed = osp->os_delegation ||
13276 ((lt == F_RDLCK &&
13277 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13278 (lt == F_WRLCK &&
13279 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13281 mutex_exit(&osp->os_sync_lock);
13282 open_owner_rele(oop);
13284 if (reopen_needed) {
13286 * Always use CLAIM_PREVIOUS after server reboot.
13287 * The server will reject CLAIM_DELEGATE_CUR if
13288 * it is used during the grace period.
13290 mutex_enter(&mi->mi_lock);
13291 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13292 oclaim = CLAIM_PREVIOUS;
13293 force = TRUE;
13294 } else {
13295 oclaim = CLAIM_DELEGATE_CUR;
13296 force = FALSE;
13298 mutex_exit(&mi->mi_lock);
13300 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13301 if (ep->error == EAGAIN) {
13302 nfs4_error_zinit(ep);
13303 ep->stat = NFS4ERR_DELAY;
13306 open_stream_rele(osp, rp);
13307 osp = NULL;
13312 * Setup the LOCKU4 arguments.
13313 * Returns errors via the nfs4_error_t.
13314 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13315 * over-the-wire. The caller must release the
13316 * reference on *lopp.
13317 * NFS4ERR_DELAY caller should retry (like recovery retry)
13318 * (other) unrecoverable error.
13320 static void
13321 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13322 LOCKU4args **locku_argsp, flock64_t *flk,
13323 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13324 vnode_t *vp, int flag, uoff_t offset, cred_t *cr,
13325 bool_t *skip_get_err, bool_t *go_otwp)
13327 nfs4_lock_owner_t *lop = NULL;
13328 LOCKU4args *locku_args;
13329 pid_t pid;
13330 bool_t is_spec = FALSE;
13331 rnode4_t *rp = VTOR4(vp);
13333 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13334 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13336 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13337 if (ep->error || ep->stat)
13338 return;
13340 argop->argop = OP_LOCKU;
13341 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13342 argsp->ctag = TAG_LOCKU_REINSTATE;
13343 else
13344 argsp->ctag = TAG_LOCKU;
13345 locku_args = &argop->nfs_argop4_u.oplocku;
13346 *locku_argsp = locku_args;
13348 /* locktype should be set to any legal value */
13349 locku_args->locktype = READ_LT;
13351 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13352 flk->l_pid;
13355 * Get the lock owner stateid. If no lock owner
13356 * exists, return success.
13358 lop = find_lock_owner(rp, pid, LOWN_ANY);
13359 *lopp = lop;
13360 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13361 is_spec = TRUE;
13362 if (!lop || is_spec) {
13364 * No lock owner so no locks to unlock.
13365 * Return success. If there was a failed
13366 * reclaim earlier, the lock might still be
13367 * registered with the local locking code,
13368 * so notify it of the unlock.
13370 * If the lockowner is using a special stateid,
13371 * then the original lock request (that created
13372 * this lockowner) was never successful, so we
13373 * have no lock to undo OTW.
13375 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13376 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13377 "(%ld) so return success", (long)pid));
13379 if (ctype == NFS4_LCK_CTYPE_NORM)
13380 flk->l_pid = curproc->p_pid;
13381 nfs4_register_lock_locally(vp, flk, flag, offset);
13383 * Release our hold and NULL out so final_cleanup
13384 * doesn't try to end a lock seqid sync we
13385 * never started.
13387 if (is_spec) {
13388 lock_owner_rele(lop);
13389 *lopp = NULL;
13391 *skip_get_err = TRUE;
13392 *go_otwp = FALSE;
13393 return;
13396 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13397 if (ep->error == EAGAIN) {
13398 lock_owner_rele(lop);
13399 *lopp = NULL;
13400 return;
13403 mutex_enter(&lop->lo_lock);
13404 locku_args->lock_stateid = lop->lock_stateid;
13405 mutex_exit(&lop->lo_lock);
13406 locku_args->seqid = lop->lock_seqid + 1;
13408 /* leave the ref count on lop, rele after RPC call */
13410 locku_args->offset = flk->l_start;
13411 locku_args->length = flk->l_len;
13412 if (flk->l_len == 0)
13413 locku_args->length = ~locku_args->length;
13415 *go_otwp = TRUE;
13419 * Setup the LOCK4 arguments.
13421 * Returns errors via the nfs4_error_t.
13422 * NFS4_OK no problems
13423 * NFS4ERR_DELAY caller should retry (like recovery retry)
13424 * (other) unrecoverable error
13426 static void
13427 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13428 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13429 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13430 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13432 LOCK4args *lock_args;
13433 nfs4_open_owner_t *oop = NULL;
13434 nfs4_open_stream_t *osp = NULL;
13435 nfs4_lock_owner_t *lop = NULL;
13436 pid_t pid;
13437 rnode4_t *rp = VTOR4(vp);
13439 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13441 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13442 if (ep->error || ep->stat != NFS4_OK)
13443 return;
13445 argop->argop = OP_LOCK;
13446 if (ctype == NFS4_LCK_CTYPE_NORM)
13447 argsp->ctag = TAG_LOCK;
13448 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13449 argsp->ctag = TAG_RELOCK;
13450 else
13451 argsp->ctag = TAG_LOCK_REINSTATE;
13452 lock_args = &argop->nfs_argop4_u.oplock;
13453 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13454 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13456 * Get the lock owner. If no lock owner exists,
13457 * create a 'temporary' one and grab the open seqid
13458 * synchronization (which puts a hold on the open
13459 * owner and open stream).
13460 * This also grabs the lock seqid synchronization.
13462 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13463 ep->stat =
13464 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13466 if (ep->stat != NFS4_OK)
13467 goto out;
13469 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13470 &lock_args->locker);
13472 lock_args->offset = flk->l_start;
13473 lock_args->length = flk->l_len;
13474 if (flk->l_len == 0)
13475 lock_args->length = ~lock_args->length;
13476 *lock_argsp = lock_args;
13477 out:
13478 *oopp = oop;
13479 *ospp = osp;
13480 *lopp = lop;
13484 * After we get the reply from the server, record the proper information
13485 * for possible resend lock requests.
13487 static void
13488 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13489 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13490 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13491 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13493 bool_t unlock = (flk->l_type == F_UNLCK);
13495 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13496 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13497 ctype == NFS4_LCK_CTYPE_REINSTATE);
13499 if (error != 0 && !unlock) {
13500 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13501 nfs4_client_lock_debug), (CE_NOTE,
13502 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13503 " for lop %p", (void *)lop));
13504 ASSERT(lop != NULL);
13505 mutex_enter(&lop->lo_lock);
13506 lop->lo_pending_rqsts = 1;
13507 mutex_exit(&lop->lo_lock);
13510 lost_rqstp->lr_putfirst = FALSE;
13511 lost_rqstp->lr_op = 0;
13514 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13515 * recovery purposes so that the lock request that was sent
13516 * can be saved and re-issued later. Ditto for EIO from a forced
13517 * unmount. This is done to have the client's local locking state
13518 * match the v4 server's state; that is, the request was
13519 * potentially received and accepted by the server but the client
13520 * thinks it was not.
13522 if (error == ETIMEDOUT || error == EINTR ||
13523 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13524 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13525 nfs4_client_lock_debug), (CE_NOTE,
13526 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13527 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13528 (void *)lop, (void *)oop, (void *)osp));
13529 if (unlock)
13530 lost_rqstp->lr_op = OP_LOCKU;
13531 else {
13532 lost_rqstp->lr_op = OP_LOCK;
13533 lost_rqstp->lr_locktype = locktype;
13536 * Objects are held and rele'd via the recovery code.
13537 * See nfs4_save_lost_rqst.
13539 lost_rqstp->lr_vp = vp;
13540 lost_rqstp->lr_dvp = NULL;
13541 lost_rqstp->lr_oop = oop;
13542 lost_rqstp->lr_osp = osp;
13543 lost_rqstp->lr_lop = lop;
13544 lost_rqstp->lr_cr = cr;
13545 switch (ctype) {
13546 case NFS4_LCK_CTYPE_NORM:
13547 flk->l_pid = ttoproc(curthread)->p_pid;
13548 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13549 break;
13550 case NFS4_LCK_CTYPE_REINSTATE:
13551 lost_rqstp->lr_putfirst = TRUE;
13552 lost_rqstp->lr_ctype = ctype;
13553 break;
13554 default:
13555 break;
13557 lost_rqstp->lr_flk = flk;
13562 * Update lop's seqid. Also update the seqid stored in a resend request,
13563 * if any. (Some recovery errors increment the seqid, and we may have to
13564 * send the resend request again.)
13567 static void
13568 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13569 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13571 if (lock_args) {
13572 if (lock_args->locker.new_lock_owner == TRUE)
13573 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13574 else {
13575 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13576 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13578 } else if (locku_args) {
13579 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13580 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13585 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13586 * COMPOUND4 args/res for calls that need to retry.
13587 * Switches the *cred_otwp to base_cr.
13589 static void
13590 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13591 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13592 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13593 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13594 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13596 nfs4_open_owner_t *oop = *oopp;
13597 nfs4_open_stream_t *osp = *ospp;
13598 nfs4_lock_owner_t *lop = *lopp;
13599 nfs_argop4 *argop = (*argspp)->array;
13601 if (*did_start_fop) {
13602 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13603 needrecov);
13604 *did_start_fop = FALSE;
13606 ASSERT((*argspp)->array_len == 2);
13607 if (argop[1].argop == OP_LOCK)
13608 nfs4args_lock_free(&argop[1]);
13609 else if (argop[1].argop == OP_LOCKT)
13610 nfs4args_lockt_free(&argop[1]);
13611 kmem_free(argop, 2 * sizeof (nfs_argop4));
13612 if (!error)
13613 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13614 *argspp = NULL;
13615 *respp = NULL;
13617 if (lop) {
13618 nfs4_end_lock_seqid_sync(lop);
13619 lock_owner_rele(lop);
13620 *lopp = NULL;
13623 /* need to free up the reference on osp for lock args */
13624 if (osp != NULL) {
13625 open_stream_rele(osp, VTOR4(vp));
13626 *ospp = NULL;
13629 /* need to free up the reference on oop for lock args */
13630 if (oop != NULL) {
13631 nfs4_end_open_seqid_sync(oop);
13632 open_owner_rele(oop);
13633 *oopp = NULL;
13636 crfree(*cred_otwp);
13637 *cred_otwp = base_cr;
13638 crhold(*cred_otwp);
13642 * Function to process the client's recovery for nfs4frlock.
13643 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13645 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13646 * COMPOUND4 args/res for calls that need to retry.
13648 * Note: the rp's r_lkserlock is *not* dropped during this path.
13650 static bool_t
13651 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13652 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13653 LOCK4args *lock_args, LOCKU4args *locku_args,
13654 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13655 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13656 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13657 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13659 nfs4_open_owner_t *oop = *oopp;
13660 nfs4_open_stream_t *osp = *ospp;
13661 nfs4_lock_owner_t *lop = *lopp;
13663 bool_t abort, retry;
13665 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13666 ASSERT((*argspp) != NULL);
13667 ASSERT((*respp) != NULL);
13668 if (lock_args || locku_args)
13669 ASSERT(lop != NULL);
13671 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13672 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13674 retry = TRUE;
13675 abort = FALSE;
13676 if (needrecov) {
13677 nfs4_bseqid_entry_t *bsep = NULL;
13678 nfs_opnum4 op;
13680 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13682 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13683 seqid4 seqid;
13685 if (lock_args) {
13686 if (lock_args->locker.new_lock_owner == TRUE)
13687 seqid = lock_args->locker.locker4_u.
13688 open_owner.open_seqid;
13689 else
13690 seqid = lock_args->locker.locker4_u.
13691 lock_owner.lock_seqid;
13692 } else if (locku_args) {
13693 seqid = locku_args->seqid;
13694 } else {
13695 seqid = 0;
13698 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13699 flk->l_pid, (*argspp)->ctag, seqid);
13702 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13703 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13704 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13705 NULL, op, bsep, NULL, NULL);
13707 if (bsep)
13708 kmem_free(bsep, sizeof (*bsep));
13712 * Return that we do not want to retry the request for 3 cases:
13713 * 1. If we received EINTR or are bailing out because of a forced
13714 * unmount, we came into this code path just for the sake of
13715 * initiating recovery, we now need to return the error.
13716 * 2. If we have aborted recovery.
13717 * 3. We received NFS4ERR_BAD_SEQID.
13719 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13720 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13721 retry = FALSE;
13723 if (*did_start_fop == TRUE) {
13724 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13725 needrecov);
13726 *did_start_fop = FALSE;
13729 if (retry == TRUE) {
13730 nfs_argop4 *argop;
13732 argop = (*argspp)->array;
13733 ASSERT((*argspp)->array_len == 2);
13735 if (argop[1].argop == OP_LOCK)
13736 nfs4args_lock_free(&argop[1]);
13737 else if (argop[1].argop == OP_LOCKT)
13738 nfs4args_lockt_free(&argop[1]);
13739 kmem_free(argop, 2 * sizeof (nfs_argop4));
13740 if (!ep->error)
13741 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13742 *respp = NULL;
13743 *argspp = NULL;
13746 if (lop != NULL) {
13747 nfs4_end_lock_seqid_sync(lop);
13748 lock_owner_rele(lop);
13751 *lopp = NULL;
13753 /* need to free up the reference on osp for lock args */
13754 if (osp != NULL) {
13755 open_stream_rele(osp, rp);
13756 *ospp = NULL;
13759 /* need to free up the reference on oop for lock args */
13760 if (oop != NULL) {
13761 nfs4_end_open_seqid_sync(oop);
13762 open_owner_rele(oop);
13763 *oopp = NULL;
13766 return (retry);
13770 * Handles the successful reply from the server for nfs4frlock.
13772 static void
13773 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13774 vnode_t *vp, int flag, uoff_t offset,
13775 nfs4_lost_rqst_t *resend_rqstp)
13777 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13778 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13779 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13780 if (ctype == NFS4_LCK_CTYPE_NORM) {
13781 flk->l_pid = ttoproc(curthread)->p_pid;
13783 * We do not register lost locks locally in
13784 * the 'resend' case since the user/application
13785 * doesn't think we have the lock.
13787 ASSERT(!resend_rqstp);
13788 nfs4_register_lock_locally(vp, flk, flag, offset);
13794 * Handle the DENIED reply from the server for nfs4frlock.
13795 * Returns TRUE if we should retry the request; FALSE otherwise.
13797 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13798 * COMPOUND4 args/res for calls that need to retry. Can also
13799 * drop and regrab the r_lkserlock.
13801 static bool_t
13802 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13803 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13804 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13805 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13806 nfs4_recov_state_t *recov_statep, int needrecov,
13807 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13808 clock_t *tick_delayp, short *whencep, int *errorp,
13809 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13810 bool_t *skip_get_err)
13812 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13814 if (lock_args) {
13815 nfs4_open_owner_t *oop = *oopp;
13816 nfs4_open_stream_t *osp = *ospp;
13817 nfs4_lock_owner_t *lop = *lopp;
13818 int intr;
13821 * Blocking lock needs to sleep and retry from the request.
13823 * Do not block and wait for 'resend' or 'reinstate'
13824 * lock requests, just return the error.
13826 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13828 if (cmd == F_SETLKW) {
13829 rnode4_t *rp = VTOR4(vp);
13830 nfs_argop4 *argop = (*argspp)->array;
13832 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13834 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13835 recov_statep, needrecov);
13836 *did_start_fop = FALSE;
13837 ASSERT((*argspp)->array_len == 2);
13838 if (argop[1].argop == OP_LOCK)
13839 nfs4args_lock_free(&argop[1]);
13840 else if (argop[1].argop == OP_LOCKT)
13841 nfs4args_lockt_free(&argop[1]);
13842 kmem_free(argop, 2 * sizeof (nfs_argop4));
13843 if (*respp)
13844 (void) xdr_free(xdr_COMPOUND4res_clnt,
13845 (caddr_t)*respp);
13846 *argspp = NULL;
13847 *respp = NULL;
13848 nfs4_end_lock_seqid_sync(lop);
13849 lock_owner_rele(lop);
13850 *lopp = NULL;
13851 if (osp != NULL) {
13852 open_stream_rele(osp, rp);
13853 *ospp = NULL;
13855 if (oop != NULL) {
13856 nfs4_end_open_seqid_sync(oop);
13857 open_owner_rele(oop);
13858 *oopp = NULL;
13861 nfs_rw_exit(&rp->r_lkserlock);
13863 intr = nfs4_block_and_wait(tick_delayp, rp);
13865 if (intr) {
13866 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13867 RW_WRITER, FALSE);
13868 *errorp = EINTR;
13869 return (FALSE);
13872 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13873 RW_WRITER, FALSE);
13876 * Make sure we are still safe to lock with
13877 * regards to mmapping.
13879 if (!nfs4_safelock(vp, flk, cr)) {
13880 *errorp = EAGAIN;
13881 return (FALSE);
13884 return (TRUE);
13886 if (ctype == NFS4_LCK_CTYPE_NORM)
13887 *errorp = EAGAIN;
13888 *skip_get_err = TRUE;
13889 flk->l_whence = 0;
13890 *whencep = 0;
13891 return (FALSE);
13892 } else if (lockt_args) {
13893 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13894 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13896 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13897 flk, lockt_args);
13899 /* according to NLM code */
13900 *errorp = 0;
13901 *whencep = 0;
13902 *skip_get_err = TRUE;
13903 return (FALSE);
13905 return (FALSE);
13909 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13911 static void
13912 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13914 switch (resp->status) {
13915 case NFS4ERR_ACCESS:
13916 case NFS4ERR_ADMIN_REVOKED:
13917 case NFS4ERR_BADHANDLE:
13918 case NFS4ERR_BAD_RANGE:
13919 case NFS4ERR_BAD_SEQID:
13920 case NFS4ERR_BAD_STATEID:
13921 case NFS4ERR_BADXDR:
13922 case NFS4ERR_DEADLOCK:
13923 case NFS4ERR_DELAY:
13924 case NFS4ERR_EXPIRED:
13925 case NFS4ERR_FHEXPIRED:
13926 case NFS4ERR_GRACE:
13927 case NFS4ERR_INVAL:
13928 case NFS4ERR_ISDIR:
13929 case NFS4ERR_LEASE_MOVED:
13930 case NFS4ERR_LOCK_NOTSUPP:
13931 case NFS4ERR_LOCK_RANGE:
13932 case NFS4ERR_MOVED:
13933 case NFS4ERR_NOFILEHANDLE:
13934 case NFS4ERR_NO_GRACE:
13935 case NFS4ERR_OLD_STATEID:
13936 case NFS4ERR_OPENMODE:
13937 case NFS4ERR_RECLAIM_BAD:
13938 case NFS4ERR_RECLAIM_CONFLICT:
13939 case NFS4ERR_RESOURCE:
13940 case NFS4ERR_SERVERFAULT:
13941 case NFS4ERR_STALE:
13942 case NFS4ERR_STALE_CLIENTID:
13943 case NFS4ERR_STALE_STATEID:
13944 return;
13945 default:
13946 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13947 "nfs4frlock_results_default: got unrecognizable "
13948 "res.status %d", resp->status));
13949 *errorp = NFS4ERR_INVAL;
13954 * The lock request was successful, so update the client's state.
13956 static void
13957 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13958 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13959 vnode_t *vp, flock64_t *flk, cred_t *cr,
13960 nfs4_lost_rqst_t *resend_rqstp)
13962 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13964 if (lock_args) {
13965 LOCK4res *lock_res;
13967 lock_res = &resop->nfs_resop4_u.oplock;
13968 /* update the stateid with server's response */
13970 if (lock_args->locker.new_lock_owner == TRUE) {
13971 mutex_enter(&lop->lo_lock);
13972 lop->lo_just_created = NFS4_PERM_CREATED;
13973 mutex_exit(&lop->lo_lock);
13976 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13979 * If the lock was the result of a resending a lost
13980 * request, we've synched up the stateid and seqid
13981 * with the server, but now the server might be out of sync
13982 * with what the application thinks it has for locks.
13983 * Clean that up here. It's unclear whether we should do
13984 * this even if the filesystem has been forcibly unmounted.
13985 * For most servers, it's probably wasted effort, but
13986 * RFC 7530 lets servers require that unlocks exactly match
13987 * the locks that are held.
13989 if (resend_rqstp != NULL &&
13990 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13991 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13992 } else {
13993 flk->l_whence = 0;
13995 } else if (locku_args) {
13996 LOCKU4res *locku_res;
13998 locku_res = &resop->nfs_resop4_u.oplocku;
14000 /* Update the stateid with the server's response */
14001 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14002 } else if (lockt_args) {
14003 /* Switch the lock type to express success, see fcntl */
14004 flk->l_type = F_UNLCK;
14005 flk->l_whence = 0;
14010 * Do final cleanup before exiting nfs4frlock.
14011 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14012 * COMPOUND4 args/res for calls that haven't already.
14014 static void
14015 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14016 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14017 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14018 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14019 short whence, uoff_t offset, struct lm_sysid *ls,
14020 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14021 bool_t did_start_fop, bool_t skip_get_err,
14022 cred_t *cred_otw, cred_t *cred)
14024 mntinfo4_t *mi = VTOMI4(vp);
14025 rnode4_t *rp = VTOR4(vp);
14026 int error = *errorp;
14027 nfs_argop4 *argop;
14028 int do_flush_pages = 0;
14030 ASSERT(nfs_zone() == mi->mi_zone);
14032 * The client recovery code wants the raw status information,
14033 * so don't map the NFS status code to an errno value for
14034 * non-normal call types.
14036 if (ctype == NFS4_LCK_CTYPE_NORM) {
14037 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14038 *errorp = geterrno4(resp->status);
14039 if (did_start_fop == TRUE)
14040 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14041 needrecov);
14044 * We've established a new lock on the server, so invalidate
14045 * the pages associated with the vnode to get the most up to
14046 * date pages from the server after acquiring the lock. We
14047 * want to be sure that the read operation gets the newest data.
14048 * N.B.
14049 * We used to do this in nfs4frlock_results_ok but that doesn't
14050 * work since fop_putpage can call nfs4_commit which calls
14051 * nfs4_start_fop. We flush the pages below after calling
14052 * nfs4_end_fop above
14053 * The flush of the page cache must be done after
14054 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14056 if (!error && resp && resp->status == NFS4_OK)
14057 do_flush_pages = 1;
14059 if (argsp) {
14060 ASSERT(argsp->array_len == 2);
14061 argop = argsp->array;
14062 if (argop[1].argop == OP_LOCK)
14063 nfs4args_lock_free(&argop[1]);
14064 else if (argop[1].argop == OP_LOCKT)
14065 nfs4args_lockt_free(&argop[1]);
14066 kmem_free(argop, 2 * sizeof (nfs_argop4));
14067 if (resp)
14068 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14071 /* free the reference on the lock owner */
14072 if (lop != NULL) {
14073 nfs4_end_lock_seqid_sync(lop);
14074 lock_owner_rele(lop);
14077 /* need to free up the reference on osp for lock args */
14078 if (osp != NULL)
14079 open_stream_rele(osp, rp);
14081 /* need to free up the reference on oop for lock args */
14082 if (oop != NULL) {
14083 nfs4_end_open_seqid_sync(oop);
14084 open_owner_rele(oop);
14087 if (do_flush_pages)
14088 nfs4_flush_pages(vp, cred);
14090 (void) convoff(vp, flk, whence, offset);
14092 lm_rel_sysid(ls);
14095 * Record debug information in the event we get EINVAL.
14097 mutex_enter(&mi->mi_lock);
14098 if (*errorp == EINVAL && (lock_args || locku_args) &&
14099 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14100 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14101 zcmn_err(getzoneid(), CE_NOTE,
14102 "%s operation failed with "
14103 "EINVAL probably since the server, %s,"
14104 " doesn't support POSIX style locking",
14105 lock_args ? "LOCK" : "LOCKU",
14106 mi->mi_curr_serv->sv_hostname);
14107 mi->mi_flags |= MI4_LOCK_DEBUG;
14110 mutex_exit(&mi->mi_lock);
14112 if (cred_otw)
14113 crfree(cred_otw);
14117 * This calls the server and the local locking code.
14119 * Client locks are registerred locally by oring the sysid with
14120 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14121 * We need to distinguish between the two to avoid collision in case one
14122 * machine is used as both client and server.
14124 * Blocking lock requests will continually retry to acquire the lock
14125 * forever.
14127 * The ctype is defined as follows:
14128 * NFS4_LCK_CTYPE_NORM: normal lock request.
14130 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14131 * recovery, get the pid from flk instead of curproc, and don't reregister
14132 * the lock locally.
14134 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14135 * that we will use the information passed in via resend_rqstp to setup the
14136 * lock/locku request. This resend is the exact same request as the 'lost
14137 * lock', and is initiated by the recovery framework. A successful resend
14138 * request can initiate one or more reinstate requests.
14140 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14141 * does not trigger additional reinstate requests. This lock call type is
14142 * set for setting the v4 server's locking state back to match what the
14143 * client's local locking state is in the event of a received 'lost lock'.
14145 * Errors are returned via the nfs4_error_t parameter.
14147 void
14148 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14149 int flag, uoff_t offset, cred_t *cr, nfs4_error_t *ep,
14150 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14152 COMPOUND4args_clnt args, *argsp = NULL;
14153 COMPOUND4res_clnt res, *resp = NULL;
14154 nfs_argop4 *argop;
14155 nfs_resop4 *resop;
14156 rnode4_t *rp;
14157 int doqueue = 1;
14158 clock_t tick_delay; /* delay in clock ticks */
14159 struct lm_sysid *ls;
14160 LOCK4args *lock_args = NULL;
14161 LOCKU4args *locku_args = NULL;
14162 LOCKT4args *lockt_args = NULL;
14163 nfs4_open_owner_t *oop = NULL;
14164 nfs4_open_stream_t *osp = NULL;
14165 nfs4_lock_owner_t *lop = NULL;
14166 bool_t needrecov = FALSE;
14167 nfs4_recov_state_t recov_state;
14168 short whence;
14169 nfs4_op_hint_t op_hint;
14170 nfs4_lost_rqst_t lost_rqst;
14171 bool_t retry = FALSE;
14172 bool_t did_start_fop = FALSE;
14173 bool_t skip_get_err = FALSE;
14174 cred_t *cred_otw = NULL;
14175 bool_t recovonly; /* just queue request */
14176 int frc_no_reclaim = 0;
14177 #ifdef DEBUG
14178 char *name;
14179 #endif
14181 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14183 #ifdef DEBUG
14184 name = fn_name(VTOSV(vp)->sv_name);
14185 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14186 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14187 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14188 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14189 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14190 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14191 resend_rqstp ? "TRUE" : "FALSE"));
14192 kmem_free(name, MAXNAMELEN);
14193 #endif
14195 nfs4_error_zinit(ep);
14196 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14197 if (ep->error)
14198 return;
14199 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14200 if (ep->error)
14201 return;
14202 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14203 vp, cr, &cred_otw);
14205 recov_retry:
14206 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14207 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14208 rp = VTOR4(vp);
14210 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14211 &did_start_fop, &recovonly);
14213 if (ep->error)
14214 goto out;
14216 if (recovonly) {
14218 * Leave the request for the recovery system to deal with.
14220 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14221 ASSERT(cmd != F_GETLK);
14222 ASSERT(flk->l_type == F_UNLCK);
14224 nfs4_error_init(ep, EINTR);
14225 needrecov = TRUE;
14226 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14227 if (lop != NULL) {
14228 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14229 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14230 (void) nfs4_start_recovery(ep,
14231 VTOMI4(vp), vp, NULL, NULL,
14232 (lost_rqst.lr_op == OP_LOCK ||
14233 lost_rqst.lr_op == OP_LOCKU) ?
14234 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14235 lock_owner_rele(lop);
14236 lop = NULL;
14238 flk->l_pid = curproc->p_pid;
14239 nfs4_register_lock_locally(vp, flk, flag, offset);
14240 goto out;
14243 /* putfh directory fh */
14244 argop[0].argop = OP_CPUTFH;
14245 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14248 * Set up the over-the-wire arguments and get references to the
14249 * open owner, etc.
14252 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14253 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14254 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14255 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14256 } else {
14257 bool_t go_otw = TRUE;
14259 ASSERT(resend_rqstp == NULL);
14261 switch (cmd) {
14262 case F_GETLK:
14263 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14264 &lockt_args, argsp, flk, rp);
14265 break;
14266 case F_SETLKW:
14267 case F_SETLK:
14268 if (flk->l_type == F_UNLCK)
14269 nfs4frlock_setup_locku_args(ctype,
14270 &argop[1], &locku_args, flk,
14271 &lop, ep, argsp,
14272 vp, flag, offset, cr,
14273 &skip_get_err, &go_otw);
14274 else
14275 nfs4frlock_setup_lock_args(ctype,
14276 &lock_args, &oop, &osp, &lop, &argop[1],
14277 argsp, flk, cmd, vp, cr, ep);
14279 if (ep->error)
14280 goto out;
14282 switch (ep->stat) {
14283 case NFS4_OK:
14284 break;
14285 case NFS4ERR_DELAY:
14286 /* recov thread never gets this error */
14287 ASSERT(resend_rqstp == NULL);
14288 ASSERT(did_start_fop);
14290 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14291 &recov_state, TRUE);
14292 did_start_fop = FALSE;
14293 if (argop[1].argop == OP_LOCK)
14294 nfs4args_lock_free(&argop[1]);
14295 else if (argop[1].argop == OP_LOCKT)
14296 nfs4args_lockt_free(&argop[1]);
14297 kmem_free(argop, 2 * sizeof (nfs_argop4));
14298 argsp = NULL;
14299 goto recov_retry;
14300 default:
14301 ep->error = EIO;
14302 goto out;
14304 break;
14305 default:
14306 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14307 "nfs4_frlock: invalid cmd %d", cmd));
14308 ep->error = EINVAL;
14309 goto out;
14312 if (!go_otw)
14313 goto out;
14316 /* XXX should we use the local reclock as a cache ? */
14318 * Unregister the lock with the local locking code before
14319 * contacting the server. This avoids a potential race where
14320 * another process gets notified that it has been granted a lock
14321 * before we can unregister ourselves locally.
14323 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14324 if (ctype == NFS4_LCK_CTYPE_NORM)
14325 flk->l_pid = ttoproc(curthread)->p_pid;
14326 nfs4_register_lock_locally(vp, flk, flag, offset);
14330 * Send the server the lock request. Continually loop with a delay
14331 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14333 resp = &res;
14335 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14336 (CE_NOTE,
14337 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14338 rnode4info(rp)));
14340 if (lock_args && frc_no_reclaim) {
14341 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14342 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14343 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14344 lock_args->reclaim = FALSE;
14345 if (did_reclaimp)
14346 *did_reclaimp = 0;
14350 * Do the OTW call.
14352 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14354 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14355 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14357 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14358 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14359 "nfs4frlock: needrecov %d", needrecov));
14361 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14362 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14363 args.ctag);
14366 * Check if one of these mutually exclusive error cases has
14367 * happened:
14368 * need to swap credentials due to access error
14369 * recovery is needed
14370 * different error (only known case is missing Kerberos ticket)
14373 if ((ep->error == EACCES ||
14374 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14375 cred_otw != cr) {
14376 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14377 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14378 cr, &cred_otw);
14379 goto recov_retry;
14382 if (needrecov) {
14384 * LOCKT requests don't need to recover from lost
14385 * requests since they don't create/modify state.
14387 if ((ep->error == EINTR ||
14388 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14389 lockt_args)
14390 goto out;
14392 * Do not attempt recovery for requests initiated by
14393 * the recovery framework. Let the framework redrive them.
14395 if (ctype != NFS4_LCK_CTYPE_NORM)
14396 goto out;
14397 else {
14398 ASSERT(resend_rqstp == NULL);
14401 nfs4frlock_save_lost_rqst(ctype, ep->error,
14402 flk_to_locktype(cmd, flk->l_type),
14403 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14405 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14406 &resp, lock_args, locku_args, &oop, &osp, &lop,
14407 rp, vp, &recov_state, op_hint, &did_start_fop,
14408 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14410 if (retry) {
14411 ASSERT(oop == NULL);
14412 ASSERT(osp == NULL);
14413 ASSERT(lop == NULL);
14414 goto recov_retry;
14416 goto out;
14420 * Bail out if have reached this point with ep->error set. Can
14421 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14422 * This happens if Kerberos ticket has expired or has been
14423 * destroyed.
14425 if (ep->error != 0)
14426 goto out;
14429 * Process the reply.
14431 switch (resp->status) {
14432 case NFS4_OK:
14433 resop = &resp->array[1];
14434 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14435 resend_rqstp);
14437 * Have a successful lock operation, now update state.
14439 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14440 resop, lop, vp, flk, cr, resend_rqstp);
14441 break;
14443 case NFS4ERR_DENIED:
14444 resop = &resp->array[1];
14445 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14446 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14447 &recov_state, needrecov, &argsp, &resp,
14448 &tick_delay, &whence, &ep->error, resop, cr,
14449 &did_start_fop, &skip_get_err);
14451 if (retry) {
14452 ASSERT(oop == NULL);
14453 ASSERT(osp == NULL);
14454 ASSERT(lop == NULL);
14455 goto recov_retry;
14457 break;
14459 * If the server won't let us reclaim, fall-back to trying to lock
14460 * the file from scratch. Code elsewhere will check the changeinfo
14461 * to ensure the file hasn't been changed.
14463 case NFS4ERR_NO_GRACE:
14464 if (lock_args && lock_args->reclaim == TRUE) {
14465 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14466 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14467 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14468 frc_no_reclaim = 1;
14469 /* clean up before retrying */
14470 needrecov = 0;
14471 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14472 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14473 &recov_state, op_hint, &did_start_fop, NULL, flk);
14474 goto recov_retry;
14476 /* FALLTHROUGH */
14478 default:
14479 nfs4frlock_results_default(resp, &ep->error);
14480 break;
14482 out:
14484 * Process and cleanup from error. Make interrupted unlock
14485 * requests look successful, since they will be handled by the
14486 * client recovery code.
14488 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14489 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14490 lock_args, locku_args, did_start_fop,
14491 skip_get_err, cred_otw, cr);
14493 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14494 (cmd == F_SETLK || cmd == F_SETLKW))
14495 ep->error = 0;
14499 * nfs4_safelock:
14501 * Return non-zero if the given lock request can be handled without
14502 * violating the constraints on concurrent mapping and locking.
14505 static int
14506 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14508 rnode4_t *rp = VTOR4(vp);
14509 struct vattr va;
14510 int error;
14512 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14513 ASSERT(rp->r_mapcnt >= 0);
14514 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14515 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14516 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14517 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14519 if (rp->r_mapcnt == 0)
14520 return (1); /* always safe if not mapped */
14523 * If the file is already mapped and there are locks, then they
14524 * should be all safe locks. So adding or removing a lock is safe
14525 * as long as the new request is safe (i.e., whole-file, meaning
14526 * length and starting offset are both zero).
14529 if (bfp->l_start != 0 || bfp->l_len != 0) {
14530 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14531 "cannot lock a memory mapped file unless locking the "
14532 "entire file: start %"PRIx64", len %"PRIx64,
14533 bfp->l_start, bfp->l_len));
14534 return (0);
14537 /* mandatory locking and mapping don't mix */
14538 va.va_mask = AT_MODE;
14539 error = fop_getattr(vp, &va, 0, cr, NULL);
14540 if (error != 0) {
14541 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14542 "getattr error %d", error));
14543 return (0); /* treat errors conservatively */
14545 if (MANDLOCK(vp, va.va_mode)) {
14546 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14547 "cannot mandatory lock and mmap a file"));
14548 return (0);
14551 return (1);
14556 * Register the lock locally within Solaris.
14557 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14558 * recording locks locally.
14560 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14561 * are registered locally.
14563 void
14564 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14565 uoff_t offset)
14567 int oldsysid;
14568 int error;
14569 #ifdef DEBUG
14570 char *name;
14571 #endif
14573 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14575 #ifdef DEBUG
14576 name = fn_name(VTOSV(vp)->sv_name);
14577 NFS4_DEBUG(nfs4_client_lock_debug,
14578 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14579 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14580 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14581 flk->l_sysid));
14582 kmem_free(name, MAXNAMELEN);
14583 #endif
14585 /* register the lock with local locking */
14586 oldsysid = flk->l_sysid;
14587 flk->l_sysid |= LM_SYSID_CLIENT;
14588 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14589 #ifdef DEBUG
14590 if (error != 0) {
14591 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14592 "nfs4_register_lock_locally: could not register with"
14593 " local locking"));
14594 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14595 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14596 error, (void *)vp, flk->l_pid, flk->l_sysid));
14597 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14598 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14599 flk->l_type, flk->l_start, flk->l_len));
14600 (void) reclock(vp, flk, 0, flag, offset, NULL);
14601 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14602 "blocked by pid %d sysid 0x%x type %d "
14603 "off 0x%" PRIx64 " len 0x%" PRIx64,
14604 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14605 flk->l_len));
14607 #endif
14608 flk->l_sysid = oldsysid;
14612 * nfs4_lockrelease:
14614 * Release any locks on the given vnode that are held by the current
14615 * process. Also removes the lock owner (if one exists) from the rnode's
14616 * list.
14618 static int
14619 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14621 flock64_t ld;
14622 int ret, error;
14623 rnode4_t *rp;
14624 nfs4_lock_owner_t *lop;
14625 nfs4_recov_state_t recov_state;
14626 mntinfo4_t *mi;
14627 bool_t possible_orphan = FALSE;
14628 bool_t recovonly;
14630 ASSERT((uintptr_t)vp > KERNELBASE);
14631 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14633 rp = VTOR4(vp);
14634 mi = VTOMI4(vp);
14637 * If we have not locked anything then we can
14638 * just return since we have no work to do.
14640 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14641 return (0);
14645 * We need to comprehend that another thread may
14646 * kick off recovery and the lock_owner we have stashed
14647 * in lop might be invalid so we should NOT cache it
14648 * locally!
14650 recov_state.rs_flags = 0;
14651 recov_state.rs_num_retry_despite_err = 0;
14652 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14653 &recovonly);
14654 if (error) {
14655 mutex_enter(&rp->r_statelock);
14656 rp->r_flags |= R4LODANGLERS;
14657 mutex_exit(&rp->r_statelock);
14658 return (error);
14661 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14664 * Check if the lock owner might have a lock (request was sent but
14665 * no response was received). Also check if there are any remote
14666 * locks on the file. (In theory we shouldn't have to make this
14667 * second check if there's no lock owner, but for now we'll be
14668 * conservative and do it anyway.) If either condition is true,
14669 * send an unlock for the entire file to the server.
14671 * Note that no explicit synchronization is needed here. At worst,
14672 * flk_has_remote_locks() will return a false positive, in which case
14673 * the unlock call wastes time but doesn't harm correctness.
14676 if (lop) {
14677 mutex_enter(&lop->lo_lock);
14678 possible_orphan = lop->lo_pending_rqsts;
14679 mutex_exit(&lop->lo_lock);
14680 lock_owner_rele(lop);
14683 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14685 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14686 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14687 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14688 (void *)lop));
14690 if (possible_orphan || flk_has_remote_locks(vp)) {
14691 ld.l_type = F_UNLCK; /* set to unlock entire file */
14692 ld.l_whence = 0; /* unlock from start of file */
14693 ld.l_start = 0;
14694 ld.l_len = 0; /* do entire file */
14696 ret = fop_frlock(vp, F_SETLK, &ld, flag, offset, NULL,
14697 cr, NULL);
14699 if (ret != 0) {
14701 * If fop_frlock fails, make sure we unregister
14702 * local locks before we continue.
14704 ld.l_pid = ttoproc(curthread)->p_pid;
14705 nfs4_register_lock_locally(vp, &ld, flag, offset);
14706 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14707 "nfs4_lockrelease: lock release error on vp"
14708 " %p: error %d.\n", (void *)vp, ret));
14712 recov_state.rs_flags = 0;
14713 recov_state.rs_num_retry_despite_err = 0;
14714 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14715 &recovonly);
14716 if (error) {
14717 mutex_enter(&rp->r_statelock);
14718 rp->r_flags |= R4LODANGLERS;
14719 mutex_exit(&rp->r_statelock);
14720 return (error);
14724 * So, here we're going to need to retrieve the lock-owner
14725 * again (in case recovery has done a switch-a-roo) and
14726 * remove it because we can.
14728 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14730 if (lop) {
14731 nfs4_rnode_remove_lock_owner(rp, lop);
14732 lock_owner_rele(lop);
14735 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14736 return (0);
14740 * Wait for 'tick_delay' clock ticks.
14741 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14742 * NOTE: lock_lease_time is in seconds.
14744 * XXX For future improvements, should implement a waiting queue scheme.
14746 static int
14747 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14749 long milliseconds_delay;
14750 time_t lock_lease_time;
14752 /* wait tick_delay clock ticks or siginteruptus */
14753 if (delay_sig(*tick_delay)) {
14754 return (EINTR);
14756 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14757 "reissue the lock request: blocked for %ld clock ticks: %ld "
14758 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14760 /* get the lease time */
14761 lock_lease_time = r2lease_time(rp);
14763 /* drv_hztousec converts ticks to microseconds */
14764 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14765 if (milliseconds_delay < lock_lease_time * 1000) {
14766 *tick_delay = 2 * *tick_delay;
14767 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14768 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14770 return (0);
14774 void
14775 nfs4_vnops_init(void)
14779 void
14780 nfs4_vnops_fini(void)
14785 * Return a reference to the directory (parent) vnode for a given vnode,
14786 * using the saved pathname information and the directory file handle. The
14787 * caller is responsible for disposing of the reference.
14788 * Returns zero or an errno value.
14790 * Caller should set need_start_op to FALSE if it is the recovery
14791 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14794 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14796 svnode_t *svnp;
14797 vnode_t *dvp = NULL;
14798 servinfo4_t *svp;
14799 nfs4_fname_t *mfname;
14800 int error;
14802 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14804 if (vp->v_flag & VROOT) {
14805 nfs4_sharedfh_t *sfh;
14806 nfs_fh4 fh;
14807 mntinfo4_t *mi;
14809 ASSERT(vp->v_type == VREG);
14811 mi = VTOMI4(vp);
14812 svp = mi->mi_curr_serv;
14813 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14814 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14815 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14816 sfh = sfh4_get(&fh, VTOMI4(vp));
14817 nfs_rw_exit(&svp->sv_lock);
14818 mfname = mi->mi_fname;
14819 fn_hold(mfname);
14820 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14821 sfh4_rele(&sfh);
14823 if (dvp->v_type == VNON)
14824 dvp->v_type = VDIR;
14825 *dvpp = dvp;
14826 return (0);
14829 svnp = VTOSV(vp);
14831 if (svnp == NULL) {
14832 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14833 "shadow node is NULL"));
14834 return (EINVAL);
14837 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14838 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14839 "shadow node name or dfh val == NULL"));
14840 return (EINVAL);
14843 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14844 (int)need_start_op);
14845 if (error != 0) {
14846 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14847 "nfs4_make_dotdot returned %d", error));
14848 return (error);
14850 if (!dvp) {
14851 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14852 "nfs4_make_dotdot returned a NULL dvp"));
14853 return (EIO);
14855 if (dvp->v_type == VNON)
14856 dvp->v_type = VDIR;
14857 ASSERT(dvp->v_type == VDIR);
14858 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14859 mutex_enter(&dvp->v_lock);
14860 dvp->v_flag |= V_XATTRDIR;
14861 mutex_exit(&dvp->v_lock);
14863 *dvpp = dvp;
14864 return (0);
14868 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14869 * length that fnamep can accept, including the trailing null.
14870 * Returns 0 if okay, returns an errno value if there was a problem.
14874 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14876 char *fn;
14877 int err = 0;
14878 servinfo4_t *svp;
14879 svnode_t *shvp;
14882 * If the file being opened has VROOT set, then this is
14883 * a "file" mount. sv_name will not be interesting, so
14884 * go back to the servinfo4 to get the original mount
14885 * path and strip off all but the final edge. Otherwise
14886 * just return the name from the shadow vnode.
14889 if (vp->v_flag & VROOT) {
14891 svp = VTOMI4(vp)->mi_curr_serv;
14892 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14894 fn = strrchr(svp->sv_path, '/');
14895 if (fn == NULL)
14896 err = EINVAL;
14897 else
14898 fn++;
14899 } else {
14900 shvp = VTOSV(vp);
14901 fn = fn_name(shvp->sv_name);
14904 if (err == 0)
14905 if (strlen(fn) < maxlen)
14906 (void) strcpy(fnamep, fn);
14907 else
14908 err = ENAMETOOLONG;
14910 if (vp->v_flag & VROOT)
14911 nfs_rw_exit(&svp->sv_lock);
14912 else
14913 kmem_free(fn, MAXNAMELEN);
14915 return (err);
14919 * Bookkeeping for a close that doesn't need to go over the wire.
14920 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14921 * it is left at 1.
14923 void
14924 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14926 rnode4_t *rp;
14927 mntinfo4_t *mi;
14929 mi = VTOMI4(vp);
14930 rp = VTOR4(vp);
14932 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14933 "rp=%p osp=%p", (void *)rp, (void *)osp));
14934 ASSERT(nfs_zone() == mi->mi_zone);
14935 ASSERT(mutex_owned(&osp->os_sync_lock));
14936 ASSERT(*have_lockp);
14938 if (!osp->os_valid ||
14939 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14940 return;
14944 * This removes the reference obtained at OPEN; ie,
14945 * when the open stream structure was created.
14947 * We don't have to worry about calling 'open_stream_rele'
14948 * since we our currently holding a reference to this
14949 * open stream which means the count can not go to 0 with
14950 * this decrement.
14952 ASSERT(osp->os_ref_count >= 2);
14953 osp->os_ref_count--;
14954 osp->os_valid = 0;
14955 mutex_exit(&osp->os_sync_lock);
14956 *have_lockp = 0;
14958 nfs4_dec_state_ref_count(mi);
14962 * Close all remaining open streams on the rnode. These open streams
14963 * could be here because:
14964 * - The close attempted at either close or delmap failed
14965 * - Some kernel entity did fop_open but never did fop_close
14966 * - Someone did mknod on a regular file but never opened it
14969 nfs4close_all(vnode_t *vp, cred_t *cr)
14971 nfs4_open_stream_t *osp;
14972 int error;
14973 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14974 rnode4_t *rp;
14976 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14978 error = 0;
14979 rp = VTOR4(vp);
14982 * At this point, all we know is that the last time
14983 * someone called vn_rele, the count was 1. Since then,
14984 * the vnode could have been re-activated. We want to
14985 * loop through the open streams and close each one, but
14986 * we have to be careful since once we release the rnode
14987 * hash bucket lock, someone else is free to come in and
14988 * re-activate the rnode and add new open streams. The
14989 * strategy is take the rnode hash bucket lock, verify that
14990 * the count is still 1, grab the open stream off the
14991 * head of the list and mark it invalid, then release the
14992 * rnode hash bucket lock and proceed with that open stream.
14993 * This is ok because nfs4close_one() will acquire the proper
14994 * open/create to close/destroy synchronization for open
14995 * streams, and will ensure that if someone has reopened
14996 * the open stream after we've dropped the hash bucket lock
14997 * then we'll just simply return without destroying the
14998 * open stream.
14999 * Repeat until the list is empty.
15002 for (;;) {
15004 /* make sure vnode hasn't been reactivated */
15005 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15006 mutex_enter(&vp->v_lock);
15007 if (vp->v_count > 1) {
15008 mutex_exit(&vp->v_lock);
15009 rw_exit(&rp->r_hashq->r_lock);
15010 break;
15013 * Grabbing r_os_lock before releasing v_lock prevents
15014 * a window where the rnode/open stream could get
15015 * reactivated (and os_force_close set to 0) before we
15016 * had a chance to set os_force_close to 1.
15018 mutex_enter(&rp->r_os_lock);
15019 mutex_exit(&vp->v_lock);
15021 osp = list_head(&rp->r_open_streams);
15022 if (!osp) {
15023 /* nothing left to CLOSE OTW, so return */
15024 mutex_exit(&rp->r_os_lock);
15025 rw_exit(&rp->r_hashq->r_lock);
15026 break;
15029 mutex_enter(&rp->r_statev4_lock);
15030 /* the file can't still be mem mapped */
15031 ASSERT(rp->r_mapcnt == 0);
15032 if (rp->created_v4)
15033 rp->created_v4 = 0;
15034 mutex_exit(&rp->r_statev4_lock);
15037 * Grab a ref on this open stream; nfs4close_one
15038 * will mark it as invalid
15040 mutex_enter(&osp->os_sync_lock);
15041 osp->os_ref_count++;
15042 osp->os_force_close = 1;
15043 mutex_exit(&osp->os_sync_lock);
15044 mutex_exit(&rp->r_os_lock);
15045 rw_exit(&rp->r_hashq->r_lock);
15047 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15049 /* Update error if it isn't already non-zero */
15050 if (error == 0) {
15051 if (e.error)
15052 error = e.error;
15053 else if (e.stat)
15054 error = geterrno4(e.stat);
15057 #ifdef DEBUG
15058 nfs4close_all_cnt++;
15059 #endif
15060 /* Release the ref on osp acquired above. */
15061 open_stream_rele(osp, rp);
15063 /* Proceed to the next open stream, if any */
15065 return (error);
15069 * nfs4close_one - close one open stream for a file if needed.
15071 * "close_type" indicates which close path this is:
15072 * CLOSE_NORM: close initiated via fop_close.
15073 * CLOSE_DELMAP: close initiated via fop_delmap.
15074 * CLOSE_FORCE: close initiated via fop_inactive. This path forces
15075 * the close and release of client state for this open stream
15076 * (unless someone else has the open stream open).
15077 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15078 * (e.g., due to abort because of a signal).
15079 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15081 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15082 * recovery. Instead, the caller is expected to deal with retries.
15084 * The caller can either pass in the osp ('provided_osp') or not.
15086 * 'access_bits' represents the access we are closing/downgrading.
15088 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15089 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15090 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15092 * Errors are returned via the nfs4_error_t.
15094 void
15095 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15096 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15097 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15098 uint_t mmap_flags)
15100 nfs4_open_owner_t *oop;
15101 nfs4_open_stream_t *osp = NULL;
15102 int retry = 0;
15103 int num_retries = NFS4_NUM_RECOV_RETRIES;
15104 rnode4_t *rp;
15105 mntinfo4_t *mi;
15106 nfs4_recov_state_t recov_state;
15107 cred_t *cred_otw = NULL;
15108 bool_t recovonly = FALSE;
15109 int isrecov;
15110 int force_close;
15111 int close_failed = 0;
15112 int did_dec_count = 0;
15113 int did_start_op = 0;
15114 int did_force_recovlock = 0;
15115 int did_start_seqid_sync = 0;
15116 int have_sync_lock = 0;
15118 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15120 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15121 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15122 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15123 len, maxprot, mmap_flags, access_bits));
15125 nfs4_error_zinit(ep);
15126 rp = VTOR4(vp);
15127 mi = VTOMI4(vp);
15128 isrecov = (close_type == CLOSE_RESEND ||
15129 close_type == CLOSE_AFTER_RESEND);
15132 * First get the open owner.
15134 if (!provided_osp) {
15135 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15136 } else {
15137 oop = provided_osp->os_open_owner;
15138 ASSERT(oop != NULL);
15139 open_owner_hold(oop);
15142 if (!oop) {
15143 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15144 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15145 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15146 (void *)provided_osp, close_type));
15147 ep->error = EIO;
15148 goto out;
15151 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15152 recov_retry:
15153 osp = NULL;
15154 close_failed = 0;
15155 force_close = (close_type == CLOSE_FORCE);
15156 retry = 0;
15157 did_start_op = 0;
15158 did_force_recovlock = 0;
15159 did_start_seqid_sync = 0;
15160 have_sync_lock = 0;
15161 recovonly = FALSE;
15162 recov_state.rs_flags = 0;
15163 recov_state.rs_num_retry_despite_err = 0;
15166 * Second synchronize with recovery.
15168 if (!isrecov) {
15169 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15170 &recov_state, &recovonly);
15171 if (!ep->error) {
15172 did_start_op = 1;
15173 } else {
15174 close_failed = 1;
15176 * If we couldn't get start_fop, but have to
15177 * cleanup state, then at least acquire the
15178 * mi_recovlock so we can synchronize with
15179 * recovery.
15181 if (close_type == CLOSE_FORCE) {
15182 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15183 RW_READER, FALSE);
15184 did_force_recovlock = 1;
15185 } else
15186 goto out;
15191 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15192 * set 'recovonly' to TRUE since most likely this is due to
15193 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15194 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15195 * to retry, causing us to loop until recovery finishes. Plus we
15196 * don't need protection over the open seqid since we're not going
15197 * OTW, hence don't need to use the seqid.
15199 if (recovonly == FALSE) {
15200 /* need to grab the open owner sync before 'os_sync_lock' */
15201 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15202 if (ep->error == EAGAIN) {
15203 ASSERT(!isrecov);
15204 if (did_start_op)
15205 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15206 &recov_state, TRUE);
15207 if (did_force_recovlock)
15208 nfs_rw_exit(&mi->mi_recovlock);
15209 goto recov_retry;
15211 did_start_seqid_sync = 1;
15215 * Third get an open stream and acquire 'os_sync_lock' to
15216 * sychronize the opening/creating of an open stream with the
15217 * closing/destroying of an open stream.
15219 if (!provided_osp) {
15220 /* returns with 'os_sync_lock' held */
15221 osp = find_open_stream(oop, rp);
15222 if (!osp) {
15223 ep->error = EIO;
15224 goto out;
15226 } else {
15227 osp = provided_osp;
15228 open_stream_hold(osp);
15229 mutex_enter(&osp->os_sync_lock);
15231 have_sync_lock = 1;
15233 ASSERT(oop == osp->os_open_owner);
15236 * Fourth, do any special pre-OTW CLOSE processing
15237 * based on the specific close type.
15239 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15240 !did_dec_count) {
15241 ASSERT(osp->os_open_ref_count > 0);
15242 osp->os_open_ref_count--;
15243 did_dec_count = 1;
15244 if (osp->os_open_ref_count == 0)
15245 osp->os_final_close = 1;
15248 if (close_type == CLOSE_FORCE) {
15249 /* see if somebody reopened the open stream. */
15250 if (!osp->os_force_close) {
15251 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15252 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15253 "was reopened, vp %p", (void *)osp, (void *)vp));
15254 ep->error = 0;
15255 ep->stat = NFS4_OK;
15256 goto out;
15259 if (!osp->os_final_close && !did_dec_count) {
15260 osp->os_open_ref_count--;
15261 did_dec_count = 1;
15265 * We can't depend on os_open_ref_count being 0 due to the
15266 * way executables are opened (VN_RELE to match a fop_open).
15268 #ifdef NOTYET
15269 ASSERT(osp->os_open_ref_count == 0);
15270 #endif
15271 if (osp->os_open_ref_count != 0) {
15272 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15273 "nfs4close_one: should panic here on an "
15274 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15275 "since this is probably the exec problem."));
15277 osp->os_open_ref_count = 0;
15281 * There is the possibility that nfs4close_one()
15282 * for close_type == CLOSE_DELMAP couldn't find the
15283 * open stream, thus couldn't decrement its os_mapcnt;
15284 * therefore we can't use this ASSERT yet.
15286 #ifdef NOTYET
15287 ASSERT(osp->os_mapcnt == 0);
15288 #endif
15289 osp->os_mapcnt = 0;
15292 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15293 ASSERT(osp->os_mapcnt >= btopr(len));
15295 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15296 osp->os_mmap_write -= btopr(len);
15297 if (maxprot & PROT_READ)
15298 osp->os_mmap_read -= btopr(len);
15299 if (maxprot & PROT_EXEC)
15300 osp->os_mmap_read -= btopr(len);
15301 /* mirror the PROT_NONE check in nfs4_addmap() */
15302 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15303 !(maxprot & PROT_EXEC))
15304 osp->os_mmap_read -= btopr(len);
15305 osp->os_mapcnt -= btopr(len);
15306 did_dec_count = 1;
15309 if (recovonly) {
15310 nfs4_lost_rqst_t lost_rqst;
15312 /* request should not already be in recovery queue */
15313 ASSERT(lrp == NULL);
15314 nfs4_error_init(ep, EINTR);
15315 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15316 osp, cred_otw, vp);
15317 mutex_exit(&osp->os_sync_lock);
15318 have_sync_lock = 0;
15319 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15320 lost_rqst.lr_op == OP_CLOSE ?
15321 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15322 close_failed = 1;
15323 force_close = 0;
15324 goto close_cleanup;
15328 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15329 * we stopped operating on the open owner's <old oo_name, old seqid>
15330 * space, which means we stopped operating on the open stream
15331 * too. So don't go OTW (as the seqid is likely bad, and the
15332 * stateid could be stale, potentially triggering a false
15333 * setclientid), and just clean up the client's internal state.
15335 if (osp->os_orig_oo_name != oop->oo_name) {
15336 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15337 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15338 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15339 "oo_name %" PRIx64")",
15340 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15341 oop->oo_name));
15342 close_failed = 1;
15345 /* If the file failed recovery, just quit. */
15346 mutex_enter(&rp->r_statelock);
15347 if (rp->r_flags & R4RECOVERR) {
15348 close_failed = 1;
15350 mutex_exit(&rp->r_statelock);
15353 * If the force close path failed to obtain start_fop
15354 * then skip the OTW close and just remove the state.
15356 if (close_failed)
15357 goto close_cleanup;
15360 * Fifth, check to see if there are still mapped pages or other
15361 * opens using this open stream. If there are then we can't
15362 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15364 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15365 nfs4_lost_rqst_t new_lost_rqst;
15366 bool_t needrecov = FALSE;
15367 cred_t *odg_cred_otw = NULL;
15368 seqid4 open_dg_seqid = 0;
15370 if (osp->os_delegation) {
15372 * If this open stream was never OPENed OTW then we
15373 * surely can't DOWNGRADE it (especially since the
15374 * osp->open_stateid is really a delegation stateid
15375 * when os_delegation is 1).
15377 if (access_bits & FREAD)
15378 osp->os_share_acc_read--;
15379 if (access_bits & FWRITE)
15380 osp->os_share_acc_write--;
15381 osp->os_share_deny_none--;
15382 nfs4_error_zinit(ep);
15383 goto out;
15385 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15386 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15387 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15388 if (needrecov && !isrecov) {
15389 bool_t abort;
15390 nfs4_bseqid_entry_t *bsep = NULL;
15392 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15393 bsep = nfs4_create_bseqid_entry(oop, NULL,
15394 vp, 0,
15395 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15396 open_dg_seqid);
15398 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15399 oop, osp, odg_cred_otw, vp, access_bits, 0);
15400 mutex_exit(&osp->os_sync_lock);
15401 have_sync_lock = 0;
15402 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15403 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15404 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15405 bsep, NULL, NULL);
15406 if (odg_cred_otw)
15407 crfree(odg_cred_otw);
15408 if (bsep)
15409 kmem_free(bsep, sizeof (*bsep));
15411 if (abort == TRUE)
15412 goto out;
15414 if (did_start_seqid_sync) {
15415 nfs4_end_open_seqid_sync(oop);
15416 did_start_seqid_sync = 0;
15418 open_stream_rele(osp, rp);
15420 if (did_start_op)
15421 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15422 &recov_state, FALSE);
15423 if (did_force_recovlock)
15424 nfs_rw_exit(&mi->mi_recovlock);
15426 goto recov_retry;
15427 } else {
15428 if (odg_cred_otw)
15429 crfree(odg_cred_otw);
15431 goto out;
15435 * If this open stream was created as the results of an open
15436 * while holding a delegation, then just release it; no need
15437 * to do an OTW close. Otherwise do a "normal" OTW close.
15439 if (osp->os_delegation) {
15440 nfs4close_notw(vp, osp, &have_sync_lock);
15441 nfs4_error_zinit(ep);
15442 goto out;
15446 * If this stream is not valid, we're done.
15448 if (!osp->os_valid) {
15449 nfs4_error_zinit(ep);
15450 goto out;
15454 * Last open or mmap ref has vanished, need to do an OTW close.
15455 * First check to see if a close is still necessary.
15457 if (osp->os_failed_reopen) {
15458 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15459 "don't close OTW osp %p since reopen failed.",
15460 (void *)osp));
15462 * Reopen of the open stream failed, hence the
15463 * stateid of the open stream is invalid/stale, and
15464 * sending this OTW would incorrectly cause another
15465 * round of recovery. In this case, we need to set
15466 * the 'os_valid' bit to 0 so another thread doesn't
15467 * come in and re-open this open stream before
15468 * this "closing" thread cleans up state (decrementing
15469 * the nfs4_server_t's state_ref_count and decrementing
15470 * the os_ref_count).
15472 osp->os_valid = 0;
15474 * This removes the reference obtained at OPEN; ie,
15475 * when the open stream structure was created.
15477 * We don't have to worry about calling 'open_stream_rele'
15478 * since we our currently holding a reference to this
15479 * open stream which means the count can not go to 0 with
15480 * this decrement.
15482 ASSERT(osp->os_ref_count >= 2);
15483 osp->os_ref_count--;
15484 nfs4_error_zinit(ep);
15485 close_failed = 0;
15486 goto close_cleanup;
15489 ASSERT(osp->os_ref_count > 1);
15492 * Sixth, try the CLOSE OTW.
15494 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15495 close_type, ep, &have_sync_lock);
15497 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15499 * Let the recovery thread be responsible for
15500 * removing the state for CLOSE.
15502 close_failed = 1;
15503 force_close = 0;
15504 retry = 0;
15507 /* See if we need to retry with a different cred */
15508 if ((ep->error == EACCES ||
15509 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15510 cred_otw != cr) {
15511 crfree(cred_otw);
15512 cred_otw = cr;
15513 crhold(cred_otw);
15514 retry = 1;
15517 if (ep->error || ep->stat)
15518 close_failed = 1;
15520 if (retry && !isrecov && num_retries-- > 0) {
15521 if (have_sync_lock) {
15522 mutex_exit(&osp->os_sync_lock);
15523 have_sync_lock = 0;
15525 if (did_start_seqid_sync) {
15526 nfs4_end_open_seqid_sync(oop);
15527 did_start_seqid_sync = 0;
15529 open_stream_rele(osp, rp);
15531 if (did_start_op)
15532 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15533 &recov_state, FALSE);
15534 if (did_force_recovlock)
15535 nfs_rw_exit(&mi->mi_recovlock);
15536 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15537 "nfs4close_one: need to retry the close "
15538 "operation"));
15539 goto recov_retry;
15541 close_cleanup:
15543 * Seventh and lastly, process our results.
15545 if (close_failed && force_close) {
15547 * It's ok to drop and regrab the 'os_sync_lock' since
15548 * nfs4close_notw() will recheck to make sure the
15549 * "close"/removal of state should happen.
15551 if (!have_sync_lock) {
15552 mutex_enter(&osp->os_sync_lock);
15553 have_sync_lock = 1;
15556 * This is last call, remove the ref on the open
15557 * stream created by open and clean everything up.
15559 osp->os_pending_close = 0;
15560 nfs4close_notw(vp, osp, &have_sync_lock);
15561 nfs4_error_zinit(ep);
15564 if (!close_failed) {
15565 if (have_sync_lock) {
15566 osp->os_pending_close = 0;
15567 mutex_exit(&osp->os_sync_lock);
15568 have_sync_lock = 0;
15569 } else {
15570 mutex_enter(&osp->os_sync_lock);
15571 osp->os_pending_close = 0;
15572 mutex_exit(&osp->os_sync_lock);
15574 if (did_start_op && recov_state.rs_sp != NULL) {
15575 mutex_enter(&recov_state.rs_sp->s_lock);
15576 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15577 mutex_exit(&recov_state.rs_sp->s_lock);
15578 } else {
15579 nfs4_dec_state_ref_count(mi);
15581 nfs4_error_zinit(ep);
15584 out:
15585 if (have_sync_lock)
15586 mutex_exit(&osp->os_sync_lock);
15587 if (did_start_op)
15588 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15589 recovonly ? TRUE : FALSE);
15590 if (did_force_recovlock)
15591 nfs_rw_exit(&mi->mi_recovlock);
15592 if (cred_otw)
15593 crfree(cred_otw);
15594 if (osp)
15595 open_stream_rele(osp, rp);
15596 if (oop) {
15597 if (did_start_seqid_sync)
15598 nfs4_end_open_seqid_sync(oop);
15599 open_owner_rele(oop);
15604 * Convert information returned by the server in the LOCK4denied
15605 * structure to the form required by fcntl.
15607 static void
15608 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15610 nfs4_lo_name_t *lo;
15612 #ifdef DEBUG
15613 if (denied_to_flk_debug) {
15614 lockt_denied_debug = lockt_denied;
15615 debug_enter("lockt_denied");
15617 #endif
15619 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15620 flk->l_whence = 0; /* aka SEEK_SET */
15621 flk->l_start = lockt_denied->offset;
15622 flk->l_len = lockt_denied->length;
15625 * If the blocking clientid matches our client id, then we can
15626 * interpret the lockowner (since we built it). If not, then
15627 * fabricate a sysid and pid. Note that the l_sysid field
15628 * in *flk already has the local sysid.
15631 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15633 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15634 lo = (nfs4_lo_name_t *)
15635 lockt_denied->owner.owner_val;
15637 flk->l_pid = lo->ln_pid;
15638 } else {
15639 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15640 "denied_to_flk: bad lock owner length\n"));
15642 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15644 } else {
15645 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15646 "denied_to_flk: foreign clientid\n"));
15649 * Construct a new sysid which should be different from
15650 * sysids of other systems.
15653 flk->l_sysid++;
15654 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15658 static pid_t
15659 lo_to_pid(lock_owner4 *lop)
15661 pid_t pid = 0;
15662 uchar_t *cp;
15663 int i;
15665 cp = (uchar_t *)&lop->clientid;
15667 for (i = 0; i < sizeof (lop->clientid); i++)
15668 pid += (pid_t)*cp++;
15670 cp = (uchar_t *)lop->owner_val;
15672 for (i = 0; i < lop->owner_len; i++)
15673 pid += (pid_t)*cp++;
15675 return (pid);
15679 * Given a lock pointer, returns the length of that lock.
15680 * "end" is the last locked offset the "l_len" covers from
15681 * the start of the lock.
15683 static off64_t
15684 lock_to_end(flock64_t *lock)
15686 off64_t lock_end;
15688 if (lock->l_len == 0)
15689 lock_end = (off64_t)MAXEND;
15690 else
15691 lock_end = lock->l_start + lock->l_len - 1;
15693 return (lock_end);
15697 * Given the end of a lock, it will return you the length "l_len" for that lock.
15699 static off64_t
15700 end_to_len(off64_t start, off64_t end)
15702 off64_t lock_len;
15704 ASSERT(end >= start);
15705 if (end == MAXEND)
15706 lock_len = 0;
15707 else
15708 lock_len = end - start + 1;
15710 return (lock_len);
15714 * On given end for a lock it determines if it is the last locked offset
15715 * or not, if so keeps it as is, else adds one to return the length for
15716 * valid start.
15718 static off64_t
15719 start_check(off64_t x)
15721 if (x == MAXEND)
15722 return (x);
15723 else
15724 return (x + 1);
15728 * See if these two locks overlap, and if so return 1;
15729 * otherwise, return 0.
15731 static int
15732 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15734 off64_t llfp_end, curfp_end;
15736 llfp_end = lock_to_end(llfp);
15737 curfp_end = lock_to_end(curfp);
15739 if (((llfp_end >= curfp->l_start) &&
15740 (llfp->l_start <= curfp->l_start)) ||
15741 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15742 return (1);
15743 return (0);
15747 * Determine what the intersecting lock region is, and add that to the
15748 * 'nl_llpp' locklist in increasing order (by l_start).
15750 static void
15751 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15752 locklist_t **nl_llpp, vnode_t *vp)
15754 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15755 off64_t lost_flp_end, local_flp_end, len, start;
15757 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15759 if (!locks_intersect(lost_flp, local_flp))
15760 return;
15762 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15763 "locks intersect"));
15765 lost_flp_end = lock_to_end(lost_flp);
15766 local_flp_end = lock_to_end(local_flp);
15768 /* Find the starting point of the intersecting region */
15769 if (local_flp->l_start > lost_flp->l_start)
15770 start = local_flp->l_start;
15771 else
15772 start = lost_flp->l_start;
15774 /* Find the lenght of the intersecting region */
15775 if (lost_flp_end < local_flp_end)
15776 len = end_to_len(start, lost_flp_end);
15777 else
15778 len = end_to_len(start, local_flp_end);
15781 * Prepare the flock structure for the intersection found and insert
15782 * it into the new list in increasing l_start order. This list contains
15783 * intersections of locks registered by the client with the local host
15784 * and the lost lock.
15785 * The lock type of this lock is the same as that of the local_flp.
15787 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15788 intersect_llp->ll_flock.l_start = start;
15789 intersect_llp->ll_flock.l_len = len;
15790 intersect_llp->ll_flock.l_type = local_flp->l_type;
15791 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15792 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15793 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15794 intersect_llp->ll_vp = vp;
15796 tmp_fllp = *nl_llpp;
15797 cur_fllp = NULL;
15798 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15799 intersect_llp->ll_flock.l_start) {
15800 cur_fllp = tmp_fllp;
15801 tmp_fllp = tmp_fllp->ll_next;
15803 if (cur_fllp == NULL) {
15804 /* first on the list */
15805 intersect_llp->ll_next = *nl_llpp;
15806 *nl_llpp = intersect_llp;
15807 } else {
15808 intersect_llp->ll_next = cur_fllp->ll_next;
15809 cur_fllp->ll_next = intersect_llp;
15812 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15813 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15814 intersect_llp->ll_flock.l_start,
15815 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15816 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15820 * Our local locking current state is potentially different than
15821 * what the NFSv4 server thinks we have due to a lost lock that was
15822 * resent and then received. We need to reset our "NFSv4" locking
15823 * state to match the current local locking state for this pid since
15824 * that is what the user/application sees as what the world is.
15826 * We cannot afford to drop the open/lock seqid sync since then we can
15827 * get confused about what the current local locking state "is" versus
15828 * "was".
15830 * If we are unable to fix up the locks, we send SIGLOST to the affected
15831 * process. This is not done if the filesystem has been forcibly
15832 * unmounted, in case the process has already exited and a new process
15833 * exists with the same pid.
15835 static void
15836 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15837 nfs4_lock_owner_t *lop)
15839 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15840 mntinfo4_t *mi = VTOMI4(vp);
15841 const int cmd = F_SETLK;
15842 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15843 flock64_t ul_fl;
15845 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15846 "nfs4_reinstitute_local_lock_state"));
15849 * Find active locks for this vp from the local locking code.
15850 * Scan through this list and find out the locks that intersect with
15851 * the lost lock. Once we find the lock that intersects, add the
15852 * intersection area as a new lock to a new list "ri_llp". The lock
15853 * type of the intersection region lock added to ri_llp is the same
15854 * as that found in the active lock list, "list". The intersecting
15855 * region locks are added to ri_llp in increasing l_start order.
15857 ASSERT(nfs_zone() == mi->mi_zone);
15859 locks = flk_active_locks_for_vp(vp);
15860 ri_llp = NULL;
15862 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15863 ASSERT(llp->ll_vp == vp);
15865 * Pick locks that belong to this pid/lockowner
15867 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15868 continue;
15870 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15874 * Now we have the list of intersections with the lost lock. These are
15875 * the locks that were/are active before the server replied to the
15876 * last/lost lock. Issue these locks to the server here. Playing these
15877 * locks to the server will re-establish our current local locking state
15878 * with the v4 server.
15879 * If we get an error, send SIGLOST to the application for that lock.
15882 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15883 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15884 "nfs4_reinstitute_local_lock_state: need to issue "
15885 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15886 llp->ll_flock.l_start,
15887 llp->ll_flock.l_start + llp->ll_flock.l_len,
15888 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15889 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15891 * No need to relock what we already have
15893 if (llp->ll_flock.l_type == lost_flp->l_type)
15894 continue;
15896 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15900 * Now keeping the start of the lost lock as our reference parse the
15901 * newly created ri_llp locklist to find the ranges that we have locked
15902 * with the v4 server but not in the current local locking. We need
15903 * to unlock these ranges.
15904 * These ranges can also be reffered to as those ranges, where the lost
15905 * lock does not overlap with the locks in the ri_llp but are locked
15906 * since the server replied to the lost lock.
15908 cur_start = lost_flp->l_start;
15909 lost_flp_end = lock_to_end(lost_flp);
15911 ul_fl.l_type = F_UNLCK;
15912 ul_fl.l_whence = 0; /* aka SEEK_SET */
15913 ul_fl.l_sysid = lost_flp->l_sysid;
15914 ul_fl.l_pid = lost_flp->l_pid;
15916 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15917 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15919 if (llp->ll_flock.l_start <= cur_start) {
15920 cur_start = start_check(llp_ll_flock_end);
15921 continue;
15923 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15924 "nfs4_reinstitute_local_lock_state: "
15925 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15926 cur_start, llp->ll_flock.l_start));
15928 ul_fl.l_start = cur_start;
15929 ul_fl.l_len = end_to_len(cur_start,
15930 (llp->ll_flock.l_start - 1));
15932 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15933 cur_start = start_check(llp_ll_flock_end);
15937 * In the case where the lost lock ends after all intersecting locks,
15938 * unlock the last part of the lost lock range.
15940 if (cur_start != start_check(lost_flp_end)) {
15941 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15942 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15943 "lost lock region [%"PRIx64" - %"PRIx64"]",
15944 cur_start, lost_flp->l_start + lost_flp->l_len));
15946 ul_fl.l_start = cur_start;
15948 * Is it an to-EOF lock? if so unlock till the end
15950 if (lost_flp->l_len == 0)
15951 ul_fl.l_len = 0;
15952 else
15953 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15955 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15958 if (locks != NULL)
15959 flk_free_locklist(locks);
15961 /* Free up our newly created locklist */
15962 for (llp = ri_llp; llp != NULL; ) {
15963 tmp_llp = llp->ll_next;
15964 kmem_free(llp, sizeof (locklist_t));
15965 llp = tmp_llp;
15969 * Now return back to the original calling nfs4frlock()
15970 * and let us naturally drop our seqid syncs.
15975 * Create a lost state record for the given lock reinstantiation request
15976 * and push it onto the lost state queue.
15978 static void
15979 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15980 nfs4_lock_owner_t *lop)
15982 nfs4_lost_rqst_t req;
15983 nfs_lock_type4 locktype;
15984 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15986 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15988 locktype = flk_to_locktype(cmd, flk->l_type);
15989 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15990 NULL, NULL, lop, flk, &req, cr, vp);
15991 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15992 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15993 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15994 NULL, NULL, NULL);