Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / kernel / fs / nfs / nfs4_vnops.c
blob270247fa1a6e69139b8af88197c71395474e6bc7
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2016 STRATO AG. All rights reserved.
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
32 * Use is subject to license terms.
36 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
37 * All Rights Reserved
41 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/cred.h>
48 #include <sys/time.h>
49 #include <sys/vnode.h>
50 #include <sys/vfs.h>
51 #include <sys/file.h>
52 #include <sys/filio.h>
53 #include <sys/uio.h>
54 #include <sys/buf.h>
55 #include <sys/mman.h>
56 #include <sys/pathname.h>
57 #include <sys/dirent.h>
58 #include <sys/debug.h>
59 #include <sys/vmsystm.h>
60 #include <sys/fcntl.h>
61 #include <sys/flock.h>
62 #include <sys/swap.h>
63 #include <sys/errno.h>
64 #include <sys/strsubr.h>
65 #include <sys/sysmacros.h>
66 #include <sys/kmem.h>
67 #include <sys/cmn_err.h>
68 #include <sys/pathconf.h>
69 #include <sys/utsname.h>
70 #include <sys/dnlc.h>
71 #include <sys/acl.h>
72 #include <sys/systeminfo.h>
73 #include <sys/policy.h>
74 #include <sys/sdt.h>
75 #include <sys/list.h>
76 #include <sys/stat.h>
77 #include <sys/zone.h>
79 #include <rpc/types.h>
80 #include <rpc/auth.h>
81 #include <rpc/clnt.h>
83 #include <nfs/nfs.h>
84 #include <nfs/nfs_clnt.h>
85 #include <nfs/nfs_acl.h>
86 #include <nfs/lm.h>
87 #include <nfs/nfs4.h>
88 #include <nfs/nfs4_kprot.h>
89 #include <nfs/rnode4.h>
90 #include <nfs/nfs4_clnt.h>
92 #include <vm/hat.h>
93 #include <vm/as.h>
94 #include <vm/page.h>
95 #include <vm/pvn.h>
96 #include <vm/seg.h>
97 #include <vm/seg_map.h>
98 #include <vm/seg_kpm.h>
99 #include <vm/seg_vn.h>
101 #include <sys/fs_subr.h>
103 #include <sys/ddi.h>
104 #include <sys/int_fmtio.h>
105 #include <sys/fs/autofs.h>
107 typedef struct {
108 nfs4_ga_res_t *di_garp;
109 cred_t *di_cred;
110 hrtime_t di_time_call;
111 } dirattr_info_t;
113 typedef enum nfs4_acl_op {
114 NFS4_ACL_GET,
115 NFS4_ACL_SET
116 } nfs4_acl_op_t;
118 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *);
120 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
121 char *, dirattr_info_t *);
123 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
124 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
125 nfs4_error_t *, int *);
126 static int nfs4_rdwrlbn(vnode_t *, page_t *, uoff_t, size_t, int,
127 cred_t *);
128 static int nfs4write(vnode_t *, caddr_t, uoff_t, int, cred_t *,
129 stable_how4 *);
130 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
131 cred_t *, bool_t, struct uio *);
132 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
133 vsecattr_t *);
134 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
135 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
136 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
137 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
138 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
139 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
140 int, vnode_t **, cred_t *);
141 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
142 cred_t *, int, int, enum createmode4, int);
143 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
144 caller_context_t *);
145 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
146 vnode_t *, char *, cred_t *, nfsstat4 *);
147 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
148 vnode_t *, char *, cred_t *, nfsstat4 *);
149 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
150 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
151 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
152 static int nfs4_getapage(vnode_t *, uoff_t, size_t, uint_t *,
153 page_t *[], size_t, struct seg *, caddr_t,
154 enum seg_rw, cred_t *);
155 static void nfs4_readahead(vnode_t *, uoff_t, caddr_t, struct seg *,
156 cred_t *);
157 static int nfs4_sync_putapage(vnode_t *, page_t *, uoff_t, size_t,
158 int, cred_t *);
159 static int nfs4_sync_pageio(vnode_t *, page_t *, uoff_t, size_t,
160 int, cred_t *);
161 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
162 static void nfs4_set_mod(vnode_t *);
163 static void nfs4_get_commit(vnode_t *);
164 static void nfs4_get_commit_range(vnode_t *, uoff_t, size_t);
165 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
166 static int nfs4_commit_vp(vnode_t *, uoff_t, size_t, cred_t *, int);
167 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
168 cred_t *);
169 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
170 cred_t *);
171 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
172 hrtime_t, vnode_t *, cred_t *);
173 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
174 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
175 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
176 uoff_t);
177 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
178 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
179 static cred_t *state_to_cred(nfs4_open_stream_t *);
180 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
181 static pid_t lo_to_pid(lock_owner4 *);
182 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
183 cred_t *, nfs4_lock_owner_t *);
184 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
185 nfs4_lock_owner_t *);
186 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
187 static void nfs4_delmap_callback(struct as *, void *, uint_t);
188 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
189 static nfs4_delmapcall_t *nfs4_init_delmapcall();
190 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
191 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
192 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
193 uid_t, gid_t, int);
196 * Routines that implement the setting of v4 args for the misc. ops
198 static void nfs4args_lock_free(nfs_argop4 *);
199 static void nfs4args_lockt_free(nfs_argop4 *);
200 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
201 int, rnode4_t *, cred_t *, bitmap4, int *,
202 nfs4_stateid_types_t *);
203 static void nfs4args_setattr_free(nfs_argop4 *);
204 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
205 bitmap4);
206 static void nfs4args_verify_free(nfs_argop4 *);
207 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
208 WRITE4args **, nfs4_stateid_types_t *);
211 * These are the vnode ops functions that implement the vnode interface to
212 * the networked file system. See more comments below at nfs4_vnodeops.
214 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
215 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
216 caller_context_t *);
217 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
218 caller_context_t *);
219 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
220 caller_context_t *);
221 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
222 caller_context_t *);
223 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
224 caller_context_t *);
225 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
226 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
227 caller_context_t *);
228 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
229 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
230 int, vnode_t **, cred_t *, int, caller_context_t *,
231 vsecattr_t *);
232 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
233 int);
234 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
235 caller_context_t *, int);
236 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
237 caller_context_t *, int);
238 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
239 cred_t *, caller_context_t *, int, vsecattr_t *);
240 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
241 caller_context_t *, int);
242 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
243 cred_t *, caller_context_t *, int);
244 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
245 caller_context_t *, int);
246 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
247 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
248 page_t *[], size_t, struct seg *, caddr_t,
249 enum seg_rw, cred_t *, caller_context_t *);
250 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
251 caller_context_t *);
252 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
253 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
254 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
255 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
256 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
257 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
258 struct flk_callback *, cred_t *, caller_context_t *);
259 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
260 cred_t *, caller_context_t *);
261 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
262 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
263 static int nfs4_pageio(vnode_t *, page_t *, uoff_t, size_t, int,
264 cred_t *, caller_context_t *);
265 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
266 caller_context_t *);
267 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
268 caller_context_t *);
270 * These vnode ops are required to be called from outside this source file,
271 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
272 * as static.
274 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
275 caller_context_t *);
276 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
277 int nfs4_lookup(vnode_t *, char *, vnode_t **,
278 struct pathname *, int, vnode_t *, cred_t *,
279 caller_context_t *, int *, pathname_t *);
280 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
281 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
282 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
283 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
284 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
285 caller_context_t *);
286 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
287 caller_context_t *);
288 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
289 caller_context_t *);
292 * Used for nfs4_commit_vp() to indicate if we should
293 * wait on pending writes.
295 #define NFS4_WRITE_NOWAIT 0
296 #define NFS4_WRITE_WAIT 1
298 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
301 * Error flags used to pass information about certain special errors
302 * which need to be handled specially.
304 #define NFS_EOF -98
305 #define NFS_VERF_MISMATCH -97
308 * Flags used to differentiate between which operation drove the
309 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
311 #define NFS4_CLOSE_OP 0x1
312 #define NFS4_DELMAP_OP 0x2
313 #define NFS4_INACTIVE_OP 0x3
315 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
317 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
318 #define ALIGN64(x, ptr, sz) \
319 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
320 if (x) { \
321 x = sizeof (uint64_t) - (x); \
322 sz -= (x); \
323 ptr += (x); \
326 #ifdef DEBUG
327 int nfs4_client_attr_debug = 0;
328 int nfs4_client_state_debug = 0;
329 int nfs4_client_shadow_debug = 0;
330 int nfs4_client_lock_debug = 0;
331 int nfs4_seqid_sync = 0;
332 int nfs4_client_map_debug = 0;
333 static int nfs4_pageio_debug = 0;
334 int nfs4_client_inactive_debug = 0;
335 int nfs4_client_recov_debug = 0;
336 int nfs4_client_failover_debug = 0;
337 int nfs4_client_call_debug = 0;
338 int nfs4_client_lookup_debug = 0;
339 int nfs4_client_zone_debug = 0;
340 int nfs4_lost_rqst_debug = 0;
341 int nfs4_rdattrerr_debug = 0;
342 int nfs4_open_stream_debug = 0;
344 int nfs4read_error_inject;
346 static int nfs4_create_misses = 0;
348 static int nfs4_readdir_cache_shorts = 0;
349 static int nfs4_readdir_readahead = 0;
351 static int nfs4_bio_do_stop = 0;
353 static int nfs4_lostpage = 0; /* number of times we lost original page */
355 int nfs4_mmap_debug = 0;
357 static int nfs4_pathconf_cache_hits = 0;
358 static int nfs4_pathconf_cache_misses = 0;
360 int nfs4close_all_cnt;
361 int nfs4close_one_debug = 0;
362 int nfs4close_notw_debug = 0;
364 int denied_to_flk_debug = 0;
365 void *lockt_denied_debug;
367 #endif
370 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
371 * or NFS4ERR_RESOURCE.
373 static int confirm_retry_sec = 30;
375 static int nfs4_lookup_neg_cache = 1;
378 * number of pages to read ahead
379 * optimized for 100 base-T.
381 static int nfs4_nra = 4;
383 static int nfs4_do_symlink_cache = 1;
385 static int nfs4_pathconf_disable_cache = 0;
388 * These are the vnode ops routines which implement the vnode interface to
389 * the networked file system. These routines just take their parameters,
390 * make them look networkish by putting the right info into interface structs,
391 * and then calling the appropriate remote routine(s) to do the work.
393 * Note on directory name lookup cacheing: If we detect a stale fhandle,
394 * we purge the directory cache relative to that vnode. This way, the
395 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
396 * more details on rnode locking.
399 const struct vnodeops nfs4_vnodeops = {
400 .vnop_name = "nfs4",
401 .vop_open = nfs4_open,
402 .vop_close = nfs4_close,
403 .vop_read = nfs4_read,
404 .vop_write = nfs4_write,
405 .vop_ioctl = nfs4_ioctl,
406 .vop_getattr = nfs4_getattr,
407 .vop_setattr = nfs4_setattr,
408 .vop_access = nfs4_access,
409 .vop_lookup = nfs4_lookup,
410 .vop_create = nfs4_create,
411 .vop_remove = nfs4_remove,
412 .vop_link = nfs4_link,
413 .vop_rename = nfs4_rename,
414 .vop_mkdir = nfs4_mkdir,
415 .vop_rmdir = nfs4_rmdir,
416 .vop_readdir = nfs4_readdir,
417 .vop_symlink = nfs4_symlink,
418 .vop_readlink = nfs4_readlink,
419 .vop_fsync = nfs4_fsync,
420 .vop_inactive = nfs4_inactive,
421 .vop_fid = nfs4_fid,
422 .vop_rwlock = nfs4_rwlock,
423 .vop_rwunlock = nfs4_rwunlock,
424 .vop_seek = nfs4_seek,
425 .vop_frlock = nfs4_frlock,
426 .vop_space = nfs4_space,
427 .vop_realvp = nfs4_realvp,
428 .vop_getpage = nfs4_getpage,
429 .vop_putpage = nfs4_putpage,
430 .vop_map = nfs4_map,
431 .vop_addmap = nfs4_addmap,
432 .vop_delmap = nfs4_delmap,
433 /* no separate nfs4_dump */
434 .vop_dump = nfs_dump,
435 .vop_pathconf = nfs4_pathconf,
436 .vop_pageio = nfs4_pageio,
437 .vop_dispose = nfs4_dispose,
438 .vop_setsecattr = nfs4_setsecattr,
439 .vop_getsecattr = nfs4_getsecattr,
440 .vop_shrlock = nfs4_shrlock,
441 .vop_vnevent = fs_vnevent_support,
445 * The following are subroutines and definitions to set args or get res
446 * for the different nfsv4 ops
449 void
450 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
452 int i;
454 for (i = 0; i < arglen; i++) {
455 if (argop[i].argop == OP_LOOKUP) {
456 kmem_free(
457 argop[i].nfs_argop4_u.oplookup.
458 objname.utf8string_val,
459 argop[i].nfs_argop4_u.oplookup.
460 objname.utf8string_len);
465 static void
466 nfs4args_lock_free(nfs_argop4 *argop)
468 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
470 if (locker->new_lock_owner == TRUE) {
471 open_to_lock_owner4 *open_owner;
473 open_owner = &locker->locker4_u.open_owner;
474 if (open_owner->lock_owner.owner_val != NULL) {
475 kmem_free(open_owner->lock_owner.owner_val,
476 open_owner->lock_owner.owner_len);
481 static void
482 nfs4args_lockt_free(nfs_argop4 *argop)
484 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
486 if (lowner->owner_val != NULL) {
487 kmem_free(lowner->owner_val, lowner->owner_len);
491 static void
492 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
493 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
494 nfs4_stateid_types_t *sid_types)
496 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
497 mntinfo4_t *mi;
499 argop->argop = OP_SETATTR;
501 * The stateid is set to 0 if client is not modifying the size
502 * and otherwise to whatever nfs4_get_stateid() returns.
504 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
505 * state struct could be found for the process/file pair. We may
506 * want to change this in the future (by OPENing the file). See
507 * bug # 4474852.
509 if (vap->va_mask & VATTR_SIZE) {
511 ASSERT(rp != NULL);
512 mi = VTOMI4(RTOV4(rp));
514 argop->nfs_argop4_u.opsetattr.stateid =
515 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
516 OP_SETATTR, sid_types, FALSE);
517 } else {
518 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
519 sizeof (stateid4));
522 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
523 if (*error)
524 bzero(attr, sizeof (*attr));
527 static void
528 nfs4args_setattr_free(nfs_argop4 *argop)
530 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
533 static int
534 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
535 bitmap4 supp)
537 fattr4 *attr;
538 int error = 0;
540 argop->argop = op;
541 switch (op) {
542 case OP_VERIFY:
543 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
544 break;
545 case OP_NVERIFY:
546 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
547 break;
548 default:
549 return (EINVAL);
551 if (!error)
552 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
553 if (error)
554 bzero(attr, sizeof (*attr));
555 return (error);
558 static void
559 nfs4args_verify_free(nfs_argop4 *argop)
561 switch (argop->argop) {
562 case OP_VERIFY:
563 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
564 break;
565 case OP_NVERIFY:
566 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
567 break;
568 default:
569 break;
573 static void
574 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
575 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
577 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
578 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
580 argop->argop = OP_WRITE;
581 wargs->stable = stable;
582 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
583 mi, OP_WRITE, sid_tp);
584 wargs->mblk = NULL;
585 *wargs_pp = wargs;
588 void
589 nfs4args_copen_free(OPEN4cargs *open_args)
591 if (open_args->owner.owner_val) {
592 kmem_free(open_args->owner.owner_val,
593 open_args->owner.owner_len);
595 if ((open_args->opentype == OPEN4_CREATE) &&
596 (open_args->mode != EXCLUSIVE4)) {
597 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
602 * XXX: This is referenced in modstubs.s
604 const struct vnodeops *
605 nfs4_getvnodeops(void)
607 return (&nfs4_vnodeops);
611 * The OPEN operation opens a regular file.
613 /*ARGSUSED3*/
614 static int
615 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
617 vnode_t *dvp = NULL;
618 rnode4_t *rp, *drp;
619 int error;
620 int just_been_created;
621 char fn[MAXNAMELEN];
623 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
624 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
625 return (EIO);
626 rp = VTOR4(*vpp);
629 * Check to see if opening something besides a regular file;
630 * if so skip the OTW call
632 if ((*vpp)->v_type != VREG) {
633 error = nfs4_open_non_reg_file(vpp, flag, cr);
634 return (error);
638 * XXX - would like a check right here to know if the file is
639 * executable or not, so as to skip OTW
642 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
643 return (error);
645 drp = VTOR4(dvp);
646 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
647 return (EINTR);
649 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
650 nfs_rw_exit(&drp->r_rwlock);
651 return (error);
655 * See if this file has just been CREATEd.
656 * If so, clear the flag and update the dnlc, which was previously
657 * skipped in nfs4_create.
658 * XXX need better serilization on this.
659 * XXX move this into the nf4open_otw call, after we have
660 * XXX acquired the open owner seqid sync.
662 mutex_enter(&rp->r_statev4_lock);
663 if (rp->created_v4) {
664 rp->created_v4 = 0;
665 mutex_exit(&rp->r_statev4_lock);
667 dnlc_update(dvp, fn, *vpp);
668 /* This is needed so we don't bump the open ref count */
669 just_been_created = 1;
670 } else {
671 mutex_exit(&rp->r_statev4_lock);
672 just_been_created = 0;
676 * If caller specified O_TRUNC/FTRUNC, then be sure to set
677 * FWRITE (to drive successful setattr(size=0) after open)
679 if (flag & FTRUNC)
680 flag |= FWRITE;
682 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
683 just_been_created);
685 if (!error && !((*vpp)->v_flag & VROOT))
686 dnlc_update(dvp, fn, *vpp);
688 nfs_rw_exit(&drp->r_rwlock);
690 /* release the hold from vtodv */
691 VN_RELE(dvp);
693 /* exchange the shadow for the master vnode, if needed */
695 if (error == 0 && IS_SHADOW(*vpp, rp))
696 sv_exchange(vpp);
698 return (error);
702 * See if there's a "lost open" request to be saved and recovered.
704 static void
705 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
706 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
707 vnode_t *dvp, OPEN4cargs *open_args)
709 vfs_t *vfsp;
710 char *srccfp;
712 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
714 if (error != ETIMEDOUT && error != EINTR &&
715 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
716 lost_rqstp->lr_op = 0;
717 return;
720 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
721 "nfs4open_save_lost_rqst: error %d", error));
723 lost_rqstp->lr_op = OP_OPEN;
726 * The vp (if it is not NULL) and dvp are held and rele'd via
727 * the recovery code. See nfs4_save_lost_rqst.
729 lost_rqstp->lr_vp = vp;
730 lost_rqstp->lr_dvp = dvp;
731 lost_rqstp->lr_oop = oop;
732 lost_rqstp->lr_osp = NULL;
733 lost_rqstp->lr_lop = NULL;
734 lost_rqstp->lr_cr = cr;
735 lost_rqstp->lr_flk = NULL;
736 lost_rqstp->lr_oacc = open_args->share_access;
737 lost_rqstp->lr_odeny = open_args->share_deny;
738 lost_rqstp->lr_oclaim = open_args->claim;
739 if (open_args->claim == CLAIM_DELEGATE_CUR) {
740 lost_rqstp->lr_ostateid =
741 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
742 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
743 } else {
744 srccfp = open_args->open_claim4_u.cfile;
746 lost_rqstp->lr_ofile.utf8string_len = 0;
747 lost_rqstp->lr_ofile.utf8string_val = NULL;
748 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
749 lost_rqstp->lr_putfirst = FALSE;
752 struct nfs4_excl_time {
753 uint32 seconds;
754 uint32 nseconds;
758 * The OPEN operation creates and/or opens a regular file
760 * ARGSUSED
762 static int
763 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
764 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
765 enum createmode4 createmode, int file_just_been_created)
767 rnode4_t *rp;
768 rnode4_t *drp = VTOR4(dvp);
769 vnode_t *vp = NULL;
770 vnode_t *vpi = *vpp;
771 bool_t needrecov = FALSE;
773 int doqueue = 1;
775 COMPOUND4args_clnt args;
776 COMPOUND4res_clnt res;
777 nfs_argop4 *argop;
778 nfs_resop4 *resop;
779 int argoplist_size;
780 int idx_open, idx_fattr;
782 GETFH4res *gf_res = NULL;
783 OPEN4res *op_res = NULL;
784 nfs4_ga_res_t *garp;
785 fattr4 *attr = NULL;
786 struct nfs4_excl_time verf;
787 bool_t did_excl_setup = FALSE;
788 int created_osp;
790 OPEN4cargs *open_args;
791 nfs4_open_owner_t *oop = NULL;
792 nfs4_open_stream_t *osp = NULL;
793 seqid4 seqid = 0;
794 bool_t retry_open = FALSE;
795 nfs4_recov_state_t recov_state;
796 nfs4_lost_rqst_t lost_rqst;
797 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
798 hrtime_t t;
799 int acc = 0;
800 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
801 cred_t *ncr = NULL;
803 nfs4_sharedfh_t *otw_sfh;
804 nfs4_sharedfh_t *orig_sfh;
805 int fh_differs = 0;
806 int numops, setgid_flag;
807 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
810 * Make sure we properly deal with setting the right gid on
811 * a newly created file to reflect the parent's setgid bit
813 setgid_flag = 0;
814 if (create_flag && in_va) {
817 * If there is grpid mount flag used or
818 * the parent's directory has the setgid bit set
819 * _and_ the client was able to get a valid mapping
820 * for the parent dir's owner_group, we want to
821 * append NVERIFY(owner_group == dva.va_gid) and
822 * SETATTR to the CREATE compound.
824 mutex_enter(&drp->r_statelock);
825 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
826 drp->r_attr.va_mode & VSGID) &&
827 drp->r_attr.va_gid != GID_NOBODY) {
828 in_va->va_mask |= VATTR_GID;
829 in_va->va_gid = drp->r_attr.va_gid;
830 setgid_flag = 1;
832 mutex_exit(&drp->r_statelock);
836 * Normal/non-create compound:
837 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
839 * Open(create) compound no setgid:
840 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
841 * RESTOREFH + GETATTR
843 * Open(create) setgid:
844 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
845 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
846 * NVERIFY(grp) + SETATTR
848 if (setgid_flag) {
849 numops = 10;
850 idx_open = 1;
851 idx_fattr = 3;
852 } else if (create_flag) {
853 numops = 7;
854 idx_open = 2;
855 idx_fattr = 4;
856 } else {
857 numops = 4;
858 idx_open = 1;
859 idx_fattr = 3;
862 args.array_len = numops;
863 argoplist_size = numops * sizeof (nfs_argop4);
864 argop = kmem_alloc(argoplist_size, KM_SLEEP);
866 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
867 "open %s open flag 0x%x cred %p", file_name, open_flag,
868 (void *)cr));
870 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
871 if (create_flag) {
873 * We are to create a file. Initialize the passed in vnode
874 * pointer.
876 vpi = NULL;
877 } else {
879 * Check to see if the client owns a read delegation and is
880 * trying to open for write. If so, then return the delegation
881 * to avoid the server doing a cb_recall and returning DELAY.
882 * NB - we don't use the statev4_lock here because we'd have
883 * to drop the lock anyway and the result would be stale.
885 if ((open_flag & FWRITE) &&
886 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
887 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
890 * If the file has a delegation, then do an access check up
891 * front. This avoids having to an access check later after
892 * we've already done start_op, which could deadlock.
894 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
895 if (open_flag & FREAD &&
896 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
897 acc |= VREAD;
898 if (open_flag & FWRITE &&
899 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
900 acc |= VWRITE;
904 drp = VTOR4(dvp);
906 recov_state.rs_flags = 0;
907 recov_state.rs_num_retry_despite_err = 0;
908 cred_otw = cr;
910 recov_retry:
911 fh_differs = 0;
912 nfs4_error_zinit(&e);
914 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
915 if (e.error) {
916 if (ncr != NULL)
917 crfree(ncr);
918 kmem_free(argop, argoplist_size);
919 return (e.error);
922 args.ctag = TAG_OPEN;
923 args.array_len = numops;
924 args.array = argop;
926 /* putfh directory fh */
927 argop[0].argop = OP_CPUTFH;
928 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
930 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
931 argop[idx_open].argop = OP_COPEN;
932 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
933 open_args->claim = CLAIM_NULL;
935 /* name of file */
936 open_args->open_claim4_u.cfile = file_name;
937 open_args->owner.owner_len = 0;
938 open_args->owner.owner_val = NULL;
940 if (create_flag) {
941 /* CREATE a file */
942 open_args->opentype = OPEN4_CREATE;
943 open_args->mode = createmode;
944 if (createmode == EXCLUSIVE4) {
945 if (did_excl_setup == FALSE) {
946 verf.seconds = zone_get_hostid(NULL);
947 if (verf.seconds != 0)
948 verf.nseconds = newnum();
949 else {
950 timestruc_t now;
952 gethrestime(&now);
953 verf.seconds = now.tv_sec;
954 verf.nseconds = now.tv_nsec;
957 * Since the server will use this value for the
958 * mtime, make sure that it can't overflow. Zero
959 * out the MSB. The actual value does not matter
960 * here, only its uniqeness.
962 verf.seconds &= INT32_MAX;
963 did_excl_setup = TRUE;
966 /* Now copy over verifier to OPEN4args. */
967 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
968 } else {
969 int v_error;
970 bitmap4 supp_attrs;
971 servinfo4_t *svp;
973 attr = &open_args->createhow4_u.createattrs;
975 svp = drp->r_server;
976 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
977 supp_attrs = svp->sv_supp_attrs;
978 nfs_rw_exit(&svp->sv_lock);
980 /* GUARDED4 or UNCHECKED4 */
981 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
982 supp_attrs);
983 if (v_error) {
984 bzero(attr, sizeof (*attr));
985 nfs4args_copen_free(open_args);
986 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
987 &recov_state, FALSE);
988 if (ncr != NULL)
989 crfree(ncr);
990 kmem_free(argop, argoplist_size);
991 return (v_error);
994 } else {
995 /* NO CREATE */
996 open_args->opentype = OPEN4_NOCREATE;
999 if (recov_state.rs_sp != NULL) {
1000 mutex_enter(&recov_state.rs_sp->s_lock);
1001 open_args->owner.clientid = recov_state.rs_sp->clientid;
1002 mutex_exit(&recov_state.rs_sp->s_lock);
1003 } else {
1004 /* XXX should we just fail here? */
1005 open_args->owner.clientid = 0;
1009 * This increments oop's ref count or creates a temporary 'just_created'
1010 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1011 * completes.
1013 mutex_enter(&VTOMI4(dvp)->mi_lock);
1015 /* See if a permanent or just created open owner exists */
1016 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1017 if (!oop) {
1019 * This open owner does not exist so create a temporary
1020 * just created one.
1022 oop = create_open_owner(cr, VTOMI4(dvp));
1023 ASSERT(oop != NULL);
1025 mutex_exit(&VTOMI4(dvp)->mi_lock);
1027 /* this length never changes, do alloc before seqid sync */
1028 open_args->owner.owner_len = sizeof (oop->oo_name);
1029 open_args->owner.owner_val =
1030 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1032 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1033 if (e.error == EAGAIN) {
1034 open_owner_rele(oop);
1035 nfs4args_copen_free(open_args);
1036 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1037 if (ncr != NULL) {
1038 crfree(ncr);
1039 ncr = NULL;
1041 goto recov_retry;
1044 /* Check to see if we need to do the OTW call */
1045 if (!create_flag) {
1046 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1047 file_just_been_created, &e.error, acc, &recov_state)) {
1050 * The OTW open is not necessary. Either
1051 * the open can succeed without it (eg.
1052 * delegation, error == 0) or the open
1053 * must fail due to an access failure
1054 * (error != 0). In either case, tidy
1055 * up and return.
1058 nfs4_end_open_seqid_sync(oop);
1059 open_owner_rele(oop);
1060 nfs4args_copen_free(open_args);
1061 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1062 if (ncr != NULL)
1063 crfree(ncr);
1064 kmem_free(argop, argoplist_size);
1065 return (e.error);
1069 bcopy(&oop->oo_name, open_args->owner.owner_val,
1070 open_args->owner.owner_len);
1072 seqid = nfs4_get_open_seqid(oop) + 1;
1073 open_args->seqid = seqid;
1074 open_args->share_access = 0;
1075 if (open_flag & FREAD)
1076 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1077 if (open_flag & FWRITE)
1078 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1079 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1084 * getfh w/sanity check for idx_open/idx_fattr
1086 ASSERT((idx_open + 1) == (idx_fattr - 1));
1087 argop[idx_open + 1].argop = OP_GETFH;
1089 /* getattr */
1090 argop[idx_fattr].argop = OP_GETATTR;
1091 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1092 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1094 if (setgid_flag) {
1095 vattr_t _v;
1096 servinfo4_t *svp;
1097 bitmap4 supp_attrs;
1099 svp = drp->r_server;
1100 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1101 supp_attrs = svp->sv_supp_attrs;
1102 nfs_rw_exit(&svp->sv_lock);
1105 * For setgid case, we need to:
1106 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1108 argop[4].argop = OP_SAVEFH;
1110 argop[5].argop = OP_CPUTFH;
1111 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1113 argop[6].argop = OP_GETATTR;
1114 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1115 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1117 argop[7].argop = OP_RESTOREFH;
1120 * nverify
1122 _v.va_mask = VATTR_GID;
1123 _v.va_gid = in_va->va_gid;
1124 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1125 supp_attrs))) {
1128 * setattr
1130 * We _know_ we're not messing with VATTR_SIZE or
1131 * VATTR_XTIME, so no need for stateid or flags.
1132 * Also we specify NULL rp since we're only
1133 * interested in setting owner_group attributes.
1135 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1136 supp_attrs, &e.error, 0);
1137 if (e.error)
1138 nfs4args_verify_free(&argop[8]);
1141 if (e.error) {
1143 * XXX - Revisit the last argument to nfs4_end_op()
1144 * once 5020486 is fixed.
1146 nfs4_end_open_seqid_sync(oop);
1147 open_owner_rele(oop);
1148 nfs4args_copen_free(open_args);
1149 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1150 if (ncr != NULL)
1151 crfree(ncr);
1152 kmem_free(argop, argoplist_size);
1153 return (e.error);
1155 } else if (create_flag) {
1156 argop[1].argop = OP_SAVEFH;
1158 argop[5].argop = OP_RESTOREFH;
1160 argop[6].argop = OP_GETATTR;
1161 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1162 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1165 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1166 "nfs4open_otw: %s call, nm %s, rp %s",
1167 needrecov ? "recov" : "first", file_name,
1168 rnode4info(VTOR4(dvp))));
1170 t = gethrtime();
1172 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1174 if (!e.error && nfs4_need_to_bump_seqid(&res))
1175 nfs4_set_open_seqid(seqid, oop, args.ctag);
1177 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1179 if (e.error || needrecov) {
1180 bool_t abort = FALSE;
1182 if (needrecov) {
1183 nfs4_bseqid_entry_t *bsep = NULL;
1185 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1186 cred_otw, vpi, dvp, open_args);
1188 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1189 bsep = nfs4_create_bseqid_entry(oop, NULL,
1190 vpi, 0, args.ctag, open_args->seqid);
1191 num_bseqid_retry--;
1194 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1195 NULL, lost_rqst.lr_op == OP_OPEN ?
1196 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1198 if (bsep)
1199 kmem_free(bsep, sizeof (*bsep));
1200 /* give up if we keep getting BAD_SEQID */
1201 if (num_bseqid_retry == 0)
1202 abort = TRUE;
1203 if (abort == TRUE && e.error == 0)
1204 e.error = geterrno4(res.status);
1206 nfs4_end_open_seqid_sync(oop);
1207 open_owner_rele(oop);
1208 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1209 nfs4args_copen_free(open_args);
1210 if (setgid_flag) {
1211 nfs4args_verify_free(&argop[8]);
1212 nfs4args_setattr_free(&argop[9]);
1214 if (!e.error)
1215 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1216 if (ncr != NULL) {
1217 crfree(ncr);
1218 ncr = NULL;
1220 if (!needrecov || abort == TRUE || e.error == EINTR ||
1221 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1222 kmem_free(argop, argoplist_size);
1223 return (e.error);
1225 goto recov_retry;
1229 * Will check and update lease after checking the rflag for
1230 * OPEN_CONFIRM in the successful OPEN call.
1232 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1235 * XXX what if we're crossing mount points from server1:/drp
1236 * to server2:/drp/rp.
1239 /* Signal our end of use of the open seqid */
1240 nfs4_end_open_seqid_sync(oop);
1243 * This will destroy the open owner if it was just created,
1244 * and no one else has put a reference on it.
1246 open_owner_rele(oop);
1247 if (create_flag && (createmode != EXCLUSIVE4) &&
1248 res.status == NFS4ERR_BADOWNER)
1249 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1251 e.error = geterrno4(res.status);
1252 nfs4args_copen_free(open_args);
1253 if (setgid_flag) {
1254 nfs4args_verify_free(&argop[8]);
1255 nfs4args_setattr_free(&argop[9]);
1257 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1258 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1260 * If the reply is NFS4ERR_ACCESS, it may be because
1261 * we are root (no root net access). If the real uid
1262 * is not root, then retry with the real uid instead.
1264 if (ncr != NULL) {
1265 crfree(ncr);
1266 ncr = NULL;
1268 if (res.status == NFS4ERR_ACCESS &&
1269 (ncr = crnetadjust(cred_otw)) != NULL) {
1270 cred_otw = ncr;
1271 goto recov_retry;
1273 kmem_free(argop, argoplist_size);
1274 return (e.error);
1277 resop = &res.array[idx_open]; /* open res */
1278 op_res = &resop->nfs_resop4_u.opopen;
1280 #ifdef DEBUG
1282 * verify attrset bitmap
1284 if (create_flag &&
1285 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1286 /* make sure attrset returned is what we asked for */
1287 /* XXX Ignore this 'error' for now */
1288 if (attr->attrmask != op_res->attrset)
1289 /* EMPTY */;
1291 #endif
1293 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1294 mutex_enter(&VTOMI4(dvp)->mi_lock);
1295 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1296 mutex_exit(&VTOMI4(dvp)->mi_lock);
1299 resop = &res.array[idx_open + 1]; /* getfh res */
1300 gf_res = &resop->nfs_resop4_u.opgetfh;
1302 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1305 * The open stateid has been updated on the server but not
1306 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1307 * flush_pages->fop_putpage->...->nfs4write where we will issue an OTW
1308 * WRITE call. That, however, will use the old stateid, so go ahead
1309 * and upate the open stateid now, before any call to makenfs4node.
1311 if (vpi) {
1312 nfs4_open_stream_t *tmp_osp;
1313 rnode4_t *tmp_rp = VTOR4(vpi);
1315 tmp_osp = find_open_stream(oop, tmp_rp);
1316 if (tmp_osp) {
1317 tmp_osp->open_stateid = op_res->stateid;
1318 mutex_exit(&tmp_osp->os_sync_lock);
1319 open_stream_rele(tmp_osp, tmp_rp);
1323 * We must determine if the file handle given by the otw open
1324 * is the same as the file handle which was passed in with
1325 * *vpp. This case can be reached if the file we are trying
1326 * to open has been removed and another file has been created
1327 * having the same file name. The passed in vnode is released
1328 * later.
1330 orig_sfh = VTOR4(vpi)->r_fh;
1331 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1334 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1336 if (create_flag || fh_differs) {
1337 int rnode_err = 0;
1339 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1340 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1342 if (e.error)
1343 PURGE_ATTRCACHE4(vp);
1345 * For the newly created vp case, make sure the rnode
1346 * isn't bad before using it.
1348 mutex_enter(&(VTOR4(vp))->r_statelock);
1349 if (VTOR4(vp)->r_flags & R4RECOVERR)
1350 rnode_err = EIO;
1351 mutex_exit(&(VTOR4(vp))->r_statelock);
1353 if (rnode_err) {
1354 nfs4_end_open_seqid_sync(oop);
1355 nfs4args_copen_free(open_args);
1356 if (setgid_flag) {
1357 nfs4args_verify_free(&argop[8]);
1358 nfs4args_setattr_free(&argop[9]);
1360 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1361 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1362 needrecov);
1363 open_owner_rele(oop);
1364 VN_RELE(vp);
1365 if (ncr != NULL)
1366 crfree(ncr);
1367 sfh4_rele(&otw_sfh);
1368 kmem_free(argop, argoplist_size);
1369 return (EIO);
1371 } else {
1372 vp = vpi;
1374 sfh4_rele(&otw_sfh);
1377 * It seems odd to get a full set of attrs and then not update
1378 * the object's attrcache in the non-create case. Create case uses
1379 * the attrs since makenfs4node checks to see if the attrs need to
1380 * be updated (and then updates them). The non-create case should
1381 * update attrs also.
1383 if (! create_flag && ! fh_differs && !e.error) {
1384 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1387 nfs4_error_zinit(&e);
1388 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1389 /* This does not do recovery for vp explicitly. */
1390 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1391 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1393 if (e.error || e.stat) {
1394 nfs4_end_open_seqid_sync(oop);
1395 nfs4args_copen_free(open_args);
1396 if (setgid_flag) {
1397 nfs4args_verify_free(&argop[8]);
1398 nfs4args_setattr_free(&argop[9]);
1400 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1401 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1402 needrecov);
1403 open_owner_rele(oop);
1404 if (create_flag || fh_differs) {
1405 /* rele the makenfs4node */
1406 VN_RELE(vp);
1408 if (ncr != NULL) {
1409 crfree(ncr);
1410 ncr = NULL;
1412 if (retry_open == TRUE) {
1413 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1414 "nfs4open_otw: retry the open since OPEN "
1415 "CONFIRM failed with error %d stat %d",
1416 e.error, e.stat));
1417 if (create_flag && createmode == GUARDED4) {
1418 NFS4_DEBUG(nfs4_client_recov_debug,
1419 (CE_NOTE, "nfs4open_otw: switch "
1420 "createmode from GUARDED4 to "
1421 "UNCHECKED4"));
1422 createmode = UNCHECKED4;
1424 goto recov_retry;
1426 if (!e.error) {
1427 if (create_flag && (createmode != EXCLUSIVE4) &&
1428 e.stat == NFS4ERR_BADOWNER)
1429 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1431 e.error = geterrno4(e.stat);
1433 kmem_free(argop, argoplist_size);
1434 return (e.error);
1438 rp = VTOR4(vp);
1440 mutex_enter(&rp->r_statev4_lock);
1441 if (create_flag)
1442 rp->created_v4 = 1;
1443 mutex_exit(&rp->r_statev4_lock);
1445 mutex_enter(&oop->oo_lock);
1446 /* Doesn't matter if 'oo_just_created' already was set as this */
1447 oop->oo_just_created = NFS4_PERM_CREATED;
1448 if (oop->oo_cred_otw)
1449 crfree(oop->oo_cred_otw);
1450 oop->oo_cred_otw = cred_otw;
1451 crhold(oop->oo_cred_otw);
1452 mutex_exit(&oop->oo_lock);
1454 /* returns with 'os_sync_lock' held */
1455 osp = find_or_create_open_stream(oop, rp, &created_osp);
1456 if (!osp) {
1457 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1458 "nfs4open_otw: failed to create an open stream"));
1459 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1460 "signal our end of use of the open seqid"));
1462 nfs4_end_open_seqid_sync(oop);
1463 open_owner_rele(oop);
1464 nfs4args_copen_free(open_args);
1465 if (setgid_flag) {
1466 nfs4args_verify_free(&argop[8]);
1467 nfs4args_setattr_free(&argop[9]);
1469 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1470 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1471 if (create_flag || fh_differs)
1472 VN_RELE(vp);
1473 if (ncr != NULL)
1474 crfree(ncr);
1476 kmem_free(argop, argoplist_size);
1477 return (EINVAL);
1481 osp->open_stateid = op_res->stateid;
1483 if (open_flag & FREAD)
1484 osp->os_share_acc_read++;
1485 if (open_flag & FWRITE)
1486 osp->os_share_acc_write++;
1487 osp->os_share_deny_none++;
1490 * Need to reset this bitfield for the possible case where we were
1491 * going to OTW CLOSE the file, got a non-recoverable error, and before
1492 * we could retry the CLOSE, OPENed the file again.
1494 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1495 osp->os_final_close = 0;
1496 osp->os_force_close = 0;
1497 #ifdef DEBUG
1498 if (osp->os_failed_reopen)
1499 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1500 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1501 (void *)osp, (void *)cr, rnode4info(rp)));
1502 #endif
1503 osp->os_failed_reopen = 0;
1505 mutex_exit(&osp->os_sync_lock);
1507 nfs4_end_open_seqid_sync(oop);
1509 if (created_osp && recov_state.rs_sp != NULL) {
1510 mutex_enter(&recov_state.rs_sp->s_lock);
1511 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1512 mutex_exit(&recov_state.rs_sp->s_lock);
1515 /* get rid of our reference to find oop */
1516 open_owner_rele(oop);
1518 open_stream_rele(osp, rp);
1520 /* accept delegation, if any */
1521 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1523 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1525 if (createmode == EXCLUSIVE4 &&
1526 (in_va->va_mask & ~(VATTR_GID | VATTR_SIZE))) {
1527 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1528 " EXCLUSIVE4: sending a SETATTR"));
1530 * If doing an exclusive create, then generate
1531 * a SETATTR to set the initial attributes.
1532 * Try to set the mtime and the atime to the
1533 * server's current time. It is somewhat
1534 * expected that these fields will be used to
1535 * store the exclusive create cookie. If not,
1536 * server implementors will need to know that
1537 * a SETATTR will follow an exclusive create
1538 * and the cookie should be destroyed if
1539 * appropriate.
1541 * The VATTR_GID and VATTR_SIZE bits are turned off
1542 * so that the SETATTR request will not attempt
1543 * to process these. The gid will be set
1544 * separately if appropriate. The size is turned
1545 * off because it is assumed that a new file will
1546 * be created empty and if the file wasn't empty,
1547 * then the exclusive create will have failed
1548 * because the file must have existed already.
1549 * Therefore, no truncate operation is needed.
1551 in_va->va_mask &= ~(VATTR_GID | VATTR_SIZE);
1552 in_va->va_mask |= (VATTR_MTIME | VATTR_ATIME);
1554 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1555 if (e.error) {
1557 * Couldn't correct the attributes of
1558 * the newly created file and the
1559 * attributes are wrong. Remove the
1560 * file and return an error to the
1561 * application.
1563 /* XXX will this take care of client state ? */
1564 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1565 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1566 " remove file", e.error));
1567 VN_RELE(vp);
1568 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1570 * Since we've reled the vnode and removed
1571 * the file we now need to return the error.
1572 * At this point we don't want to update the
1573 * dircaches, call nfs4_waitfor_purge_complete
1574 * or set vpp to vp so we need to skip these
1575 * as well.
1577 goto skip_update_dircaches;
1582 * If we created or found the correct vnode, due to create_flag or
1583 * fh_differs being set, then update directory cache attribute, readdir
1584 * and dnlc caches.
1586 if (create_flag || fh_differs) {
1587 dirattr_info_t dinfo, *dinfop;
1590 * Make sure getattr succeeded before using results.
1591 * note: op 7 is getattr(dir) for both flavors of
1592 * open(create).
1594 if (create_flag && res.status == NFS4_OK) {
1595 dinfo.di_time_call = t;
1596 dinfo.di_cred = cr;
1597 dinfo.di_garp =
1598 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1599 dinfop = &dinfo;
1600 } else {
1601 dinfop = NULL;
1604 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1605 dinfop);
1609 * If the page cache for this file was flushed from actions
1610 * above, it was done asynchronously and if that is true,
1611 * there is a need to wait here for it to complete. This must
1612 * be done outside of start_fop/end_fop.
1614 (void) nfs4_waitfor_purge_complete(vp);
1617 * It is implicit that we are in the open case (create_flag == 0) since
1618 * fh_differs can only be set to a non-zero value in the open case.
1620 if (fh_differs != 0 && vpi != NULL)
1621 VN_RELE(vpi);
1624 * Be sure to set *vpp to the correct value before returning.
1626 *vpp = vp;
1628 skip_update_dircaches:
1630 nfs4args_copen_free(open_args);
1631 if (setgid_flag) {
1632 nfs4args_verify_free(&argop[8]);
1633 nfs4args_setattr_free(&argop[9]);
1635 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1637 if (ncr)
1638 crfree(ncr);
1639 kmem_free(argop, argoplist_size);
1640 return (e.error);
1644 * Reopen an open instance. cf. nfs4open_otw().
1646 * Errors are returned by the nfs4_error_t parameter.
1647 * - ep->error contains an errno value or zero.
1648 * - if it is zero, ep->stat is set to an NFS status code, if any.
1649 * If the file could not be reopened, but the caller should continue, the
1650 * file is marked dead and no error values are returned. If the caller
1651 * should stop recovering open files and start over, either the ep->error
1652 * value or ep->stat will indicate an error (either something that requires
1653 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1654 * filehandles) may be handled silently by this routine.
1655 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1656 * will be started, so the caller should not do it.
1658 * Gotos:
1659 * - kill_file : reopen failed in such a fashion to constitute marking the
1660 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1661 * is for cases where recovery is not possible.
1662 * - failed_reopen : same as above, except that the file has already been
1663 * marked dead, so no need to do it again.
1664 * - bailout : reopen failed but we are able to recover and retry the reopen -
1665 * either within this function immediately or via the calling function.
1668 void
1669 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1670 open_claim_type4 claim, bool_t frc_use_claim_previous,
1671 bool_t is_recov)
1673 COMPOUND4args_clnt args;
1674 COMPOUND4res_clnt res;
1675 nfs_argop4 argop[4];
1676 nfs_resop4 *resop;
1677 OPEN4res *op_res = NULL;
1678 OPEN4cargs *open_args;
1679 GETFH4res *gf_res;
1680 rnode4_t *rp = VTOR4(vp);
1681 int doqueue = 1;
1682 cred_t *cr = NULL, *cred_otw = NULL;
1683 nfs4_open_owner_t *oop = NULL;
1684 seqid4 seqid;
1685 nfs4_ga_res_t *garp;
1686 char fn[MAXNAMELEN];
1687 nfs4_recov_state_t recov = {NULL, 0};
1688 nfs4_lost_rqst_t lost_rqst;
1689 mntinfo4_t *mi = VTOMI4(vp);
1690 bool_t abort;
1691 char *failed_msg = "";
1692 int fh_different;
1693 hrtime_t t;
1694 nfs4_bseqid_entry_t *bsep = NULL;
1696 ASSERT(nfs4_consistent_type(vp));
1697 ASSERT(nfs_zone() == mi->mi_zone);
1699 nfs4_error_zinit(ep);
1701 /* this is the cred used to find the open owner */
1702 cr = state_to_cred(osp);
1703 if (cr == NULL) {
1704 failed_msg = "Couldn't reopen: no cred";
1705 goto kill_file;
1707 /* use this cred for OTW operations */
1708 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1710 top:
1711 nfs4_error_zinit(ep);
1713 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1714 /* File system has been unmounted, quit */
1715 ep->error = EIO;
1716 failed_msg = "Couldn't reopen: file system has been unmounted";
1717 goto kill_file;
1720 oop = osp->os_open_owner;
1722 ASSERT(oop != NULL);
1723 if (oop == NULL) { /* be defensive in non-DEBUG */
1724 failed_msg = "can't reopen: no open owner";
1725 goto kill_file;
1727 open_owner_hold(oop);
1729 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1730 if (ep->error) {
1731 open_owner_rele(oop);
1732 oop = NULL;
1733 goto bailout;
1737 * If the rnode has a delegation and the delegation has been
1738 * recovered and the server didn't request a recall and the caller
1739 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1740 * recovery) and the rnode hasn't been marked dead, then install
1741 * the delegation stateid in the open stream. Otherwise, proceed
1742 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1744 mutex_enter(&rp->r_statev4_lock);
1745 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1746 !rp->r_deleg_return_pending &&
1747 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1748 !rp->r_deleg_needs_recall &&
1749 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1750 !(rp->r_flags & R4RECOVERR)) {
1751 mutex_enter(&osp->os_sync_lock);
1752 osp->os_delegation = 1;
1753 osp->open_stateid = rp->r_deleg_stateid;
1754 mutex_exit(&osp->os_sync_lock);
1755 mutex_exit(&rp->r_statev4_lock);
1756 goto bailout;
1758 mutex_exit(&rp->r_statev4_lock);
1761 * If the file failed recovery, just quit. This failure need not
1762 * affect other reopens, so don't return an error.
1764 mutex_enter(&rp->r_statelock);
1765 if (rp->r_flags & R4RECOVERR) {
1766 mutex_exit(&rp->r_statelock);
1767 ep->error = 0;
1768 goto failed_reopen;
1770 mutex_exit(&rp->r_statelock);
1773 * argop is empty here
1775 * PUTFH, OPEN, GETATTR
1777 args.ctag = TAG_REOPEN;
1778 args.array_len = 4;
1779 args.array = argop;
1781 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1782 "nfs4_reopen: file is type %d, id %s",
1783 vp->v_type, rnode4info(VTOR4(vp))));
1785 argop[0].argop = OP_CPUTFH;
1787 if (claim != CLAIM_PREVIOUS) {
1789 * if this is a file mount then
1790 * use the mntinfo parentfh
1792 argop[0].nfs_argop4_u.opcputfh.sfh =
1793 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1794 VTOSV(vp)->sv_dfh;
1795 } else {
1796 /* putfh fh to reopen */
1797 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1800 argop[1].argop = OP_COPEN;
1801 open_args = &argop[1].nfs_argop4_u.opcopen;
1802 open_args->claim = claim;
1804 if (claim == CLAIM_NULL) {
1806 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1807 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1808 "failed for vp 0x%p for CLAIM_NULL with %m",
1809 (void *)vp);
1810 failed_msg = "Couldn't reopen: vtoname failed for "
1811 "CLAIM_NULL";
1812 /* nothing allocated yet */
1813 goto kill_file;
1816 open_args->open_claim4_u.cfile = fn;
1817 } else if (claim == CLAIM_PREVIOUS) {
1820 * We have two cases to deal with here:
1821 * 1) We're being called to reopen files in order to satisfy
1822 * a lock operation request which requires us to explicitly
1823 * reopen files which were opened under a delegation. If
1824 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1825 * that case, frc_use_claim_previous is TRUE and we must
1826 * use the rnode's current delegation type (r_deleg_type).
1827 * 2) We're reopening files during some form of recovery.
1828 * In this case, frc_use_claim_previous is FALSE and we
1829 * use the delegation type appropriate for recovery
1830 * (r_deleg_needs_recovery).
1832 mutex_enter(&rp->r_statev4_lock);
1833 open_args->open_claim4_u.delegate_type =
1834 frc_use_claim_previous ?
1835 rp->r_deleg_type :
1836 rp->r_deleg_needs_recovery;
1837 mutex_exit(&rp->r_statev4_lock);
1839 } else if (claim == CLAIM_DELEGATE_CUR) {
1841 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1842 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1843 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1844 "with %m", (void *)vp);
1845 failed_msg = "Couldn't reopen: vtoname failed for "
1846 "CLAIM_DELEGATE_CUR";
1847 /* nothing allocated yet */
1848 goto kill_file;
1851 mutex_enter(&rp->r_statev4_lock);
1852 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1853 rp->r_deleg_stateid;
1854 mutex_exit(&rp->r_statev4_lock);
1856 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1858 open_args->opentype = OPEN4_NOCREATE;
1859 open_args->owner.clientid = mi2clientid(mi);
1860 open_args->owner.owner_len = sizeof (oop->oo_name);
1861 open_args->owner.owner_val =
1862 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1863 bcopy(&oop->oo_name, open_args->owner.owner_val,
1864 open_args->owner.owner_len);
1865 open_args->share_access = 0;
1866 open_args->share_deny = 0;
1868 mutex_enter(&osp->os_sync_lock);
1869 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1870 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1871 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1872 (void *)osp, (void *)rp, osp->os_share_acc_read,
1873 osp->os_share_acc_write, osp->os_open_ref_count,
1874 osp->os_mmap_read, osp->os_mmap_write, claim));
1876 if (osp->os_share_acc_read || osp->os_mmap_read)
1877 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1878 if (osp->os_share_acc_write || osp->os_mmap_write)
1879 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1880 if (osp->os_share_deny_read)
1881 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1882 if (osp->os_share_deny_write)
1883 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1884 mutex_exit(&osp->os_sync_lock);
1886 seqid = nfs4_get_open_seqid(oop) + 1;
1887 open_args->seqid = seqid;
1889 /* Construct the getfh part of the compound */
1890 argop[2].argop = OP_GETFH;
1892 /* Construct the getattr part of the compound */
1893 argop[3].argop = OP_GETATTR;
1894 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1895 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1897 t = gethrtime();
1899 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1901 if (ep->error) {
1902 if (!is_recov && !frc_use_claim_previous &&
1903 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1904 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1905 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1906 cred_otw, vp, NULL, open_args);
1907 abort = nfs4_start_recovery(ep,
1908 VTOMI4(vp), vp, NULL, NULL,
1909 lost_rqst.lr_op == OP_OPEN ?
1910 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1911 nfs4args_copen_free(open_args);
1912 goto bailout;
1915 nfs4args_copen_free(open_args);
1917 if (ep->error == EACCES && cred_otw != cr) {
1918 crfree(cred_otw);
1919 cred_otw = cr;
1920 crhold(cred_otw);
1921 nfs4_end_open_seqid_sync(oop);
1922 open_owner_rele(oop);
1923 oop = NULL;
1924 goto top;
1926 if (ep->error == ETIMEDOUT)
1927 goto bailout;
1928 failed_msg = "Couldn't reopen: rpc error";
1929 goto kill_file;
1932 if (nfs4_need_to_bump_seqid(&res))
1933 nfs4_set_open_seqid(seqid, oop, args.ctag);
1935 switch (res.status) {
1936 case NFS4_OK:
1937 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1938 mutex_enter(&rp->r_statelock);
1939 rp->r_delay_interval = 0;
1940 mutex_exit(&rp->r_statelock);
1942 break;
1943 case NFS4ERR_BAD_SEQID:
1944 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1945 args.ctag, open_args->seqid);
1947 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1948 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1949 NULL, OP_OPEN, bsep, NULL, NULL);
1951 nfs4args_copen_free(open_args);
1952 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1953 nfs4_end_open_seqid_sync(oop);
1954 open_owner_rele(oop);
1955 oop = NULL;
1956 kmem_free(bsep, sizeof (*bsep));
1958 goto kill_file;
1959 case NFS4ERR_NO_GRACE:
1960 nfs4args_copen_free(open_args);
1961 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1962 nfs4_end_open_seqid_sync(oop);
1963 open_owner_rele(oop);
1964 oop = NULL;
1965 if (claim == CLAIM_PREVIOUS) {
1967 * Retry as a plain open. We don't need to worry about
1968 * checking the changeinfo: it is acceptable for a
1969 * client to re-open a file and continue processing
1970 * (in the absence of locks).
1972 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1973 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1974 "will retry as CLAIM_NULL"));
1975 claim = CLAIM_NULL;
1976 nfs4_mi_kstat_inc_no_grace(mi);
1977 goto top;
1979 failed_msg =
1980 "Couldn't reopen: tried reclaim outside grace period. ";
1981 goto kill_file;
1982 case NFS4ERR_GRACE:
1983 nfs4_set_grace_wait(mi);
1984 nfs4args_copen_free(open_args);
1985 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1986 nfs4_end_open_seqid_sync(oop);
1987 open_owner_rele(oop);
1988 oop = NULL;
1989 ep->error = nfs4_wait_for_grace(mi, &recov);
1990 if (ep->error != 0)
1991 goto bailout;
1992 goto top;
1993 case NFS4ERR_DELAY:
1994 nfs4_set_delay_wait(vp);
1995 nfs4args_copen_free(open_args);
1996 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1997 nfs4_end_open_seqid_sync(oop);
1998 open_owner_rele(oop);
1999 oop = NULL;
2000 ep->error = nfs4_wait_for_delay(vp, &recov);
2001 nfs4_mi_kstat_inc_delay(mi);
2002 if (ep->error != 0)
2003 goto bailout;
2004 goto top;
2005 case NFS4ERR_FHEXPIRED:
2006 /* recover filehandle and retry */
2007 abort = nfs4_start_recovery(ep,
2008 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2009 nfs4args_copen_free(open_args);
2010 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2011 nfs4_end_open_seqid_sync(oop);
2012 open_owner_rele(oop);
2013 oop = NULL;
2014 if (abort == FALSE)
2015 goto top;
2016 failed_msg = "Couldn't reopen: recovery aborted";
2017 goto kill_file;
2018 case NFS4ERR_RESOURCE:
2019 case NFS4ERR_STALE_CLIENTID:
2020 case NFS4ERR_WRONGSEC:
2021 case NFS4ERR_EXPIRED:
2023 * Do not mark the file dead and let the calling
2024 * function initiate recovery.
2026 nfs4args_copen_free(open_args);
2027 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2028 nfs4_end_open_seqid_sync(oop);
2029 open_owner_rele(oop);
2030 oop = NULL;
2031 goto bailout;
2032 case NFS4ERR_ACCESS:
2033 if (cred_otw != cr) {
2034 crfree(cred_otw);
2035 cred_otw = cr;
2036 crhold(cred_otw);
2037 nfs4args_copen_free(open_args);
2038 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2039 nfs4_end_open_seqid_sync(oop);
2040 open_owner_rele(oop);
2041 oop = NULL;
2042 goto top;
2044 /* fall through */
2045 default:
2046 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2047 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2048 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2049 rnode4info(VTOR4(vp))));
2050 failed_msg = "Couldn't reopen: NFSv4 error";
2051 nfs4args_copen_free(open_args);
2052 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2053 goto kill_file;
2056 resop = &res.array[1]; /* open res */
2057 op_res = &resop->nfs_resop4_u.opopen;
2059 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2062 * Check if the path we reopened really is the same
2063 * file. We could end up in a situation where the file
2064 * was removed and a new file created with the same name.
2066 resop = &res.array[2];
2067 gf_res = &resop->nfs_resop4_u.opgetfh;
2068 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2069 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2070 if (fh_different) {
2071 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2072 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2073 /* Oops, we don't have the same file */
2074 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2075 failed_msg = "Couldn't reopen: Persistent "
2076 "file handle changed";
2077 else
2078 failed_msg = "Couldn't reopen: Volatile "
2079 "(no expire on open) file handle changed";
2081 nfs4args_copen_free(open_args);
2082 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2083 nfs_rw_exit(&mi->mi_fh_lock);
2084 goto kill_file;
2086 } else {
2088 * We have volatile file handles that don't compare.
2089 * If the fids are the same then we assume that the
2090 * file handle expired but the rnode still refers to
2091 * the same file object.
2093 * First check that we have fids or not.
2094 * If we don't we have a dumb server so we will
2095 * just assume every thing is ok for now.
2097 if (!ep->error && garp->n4g_va.va_mask & VATTR_NODEID &&
2098 rp->r_attr.va_mask & VATTR_NODEID &&
2099 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2101 * We have fids, but they don't
2102 * compare. So kill the file.
2104 failed_msg =
2105 "Couldn't reopen: file handle changed"
2106 " due to mismatched fids";
2107 nfs4args_copen_free(open_args);
2108 xdr_free(xdr_COMPOUND4res_clnt,
2109 (caddr_t)&res);
2110 nfs_rw_exit(&mi->mi_fh_lock);
2111 goto kill_file;
2112 } else {
2114 * We have volatile file handles that refers
2115 * to the same file (at least they have the
2116 * same fid) or we don't have fids so we
2117 * can't tell. :(. We'll be a kind and accepting
2118 * client so we'll update the rnode's file
2119 * handle with the otw handle.
2121 * We need to drop mi->mi_fh_lock since
2122 * sh4_update acquires it. Since there is
2123 * only one recovery thread there is no
2124 * race.
2126 nfs_rw_exit(&mi->mi_fh_lock);
2127 sfh4_update(rp->r_fh, &gf_res->object);
2130 } else {
2131 nfs_rw_exit(&mi->mi_fh_lock);
2134 ASSERT(nfs4_consistent_type(vp));
2137 * If the server wanted an OPEN_CONFIRM but that fails, just start
2138 * over. Presumably if there is a persistent error it will show up
2139 * when we resend the OPEN.
2141 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2142 bool_t retry_open = FALSE;
2144 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2145 cred_otw, is_recov, &retry_open,
2146 oop, FALSE, ep, NULL);
2147 if (ep->error || ep->stat) {
2148 nfs4args_copen_free(open_args);
2149 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2150 nfs4_end_open_seqid_sync(oop);
2151 open_owner_rele(oop);
2152 oop = NULL;
2153 goto top;
2157 mutex_enter(&osp->os_sync_lock);
2158 osp->open_stateid = op_res->stateid;
2159 osp->os_delegation = 0;
2161 * Need to reset this bitfield for the possible case where we were
2162 * going to OTW CLOSE the file, got a non-recoverable error, and before
2163 * we could retry the CLOSE, OPENed the file again.
2165 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2166 osp->os_final_close = 0;
2167 osp->os_force_close = 0;
2168 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2169 osp->os_dc_openacc = open_args->share_access;
2170 mutex_exit(&osp->os_sync_lock);
2172 nfs4_end_open_seqid_sync(oop);
2174 /* accept delegation, if any */
2175 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2177 nfs4args_copen_free(open_args);
2179 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2181 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2183 ASSERT(nfs4_consistent_type(vp));
2185 open_owner_rele(oop);
2186 crfree(cr);
2187 crfree(cred_otw);
2188 return;
2190 kill_file:
2191 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2192 failed_reopen:
2193 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2194 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2195 (void *)osp, (void *)cr, rnode4info(rp)));
2196 mutex_enter(&osp->os_sync_lock);
2197 osp->os_failed_reopen = 1;
2198 mutex_exit(&osp->os_sync_lock);
2199 bailout:
2200 if (oop != NULL) {
2201 nfs4_end_open_seqid_sync(oop);
2202 open_owner_rele(oop);
2204 if (cr != NULL)
2205 crfree(cr);
2206 if (cred_otw != NULL)
2207 crfree(cred_otw);
2210 /* for . and .. OPENs */
2211 /* ARGSUSED */
2212 static int
2213 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2215 rnode4_t *rp;
2216 nfs4_ga_res_t gar;
2218 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2221 * If close-to-open consistency checking is turned off or
2222 * if there is no cached data, we can avoid
2223 * the over the wire getattr. Otherwise, force a
2224 * call to the server to get fresh attributes and to
2225 * check caches. This is required for close-to-open
2226 * consistency.
2228 rp = VTOR4(*vpp);
2229 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2230 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2231 return (0);
2233 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2237 * CLOSE a file
2239 /* ARGSUSED */
2240 static int
2241 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2242 caller_context_t *ct)
2244 rnode4_t *rp;
2245 int error = 0;
2246 int r_error = 0;
2247 int n4error = 0;
2248 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2251 * Remove client state for this (lockowner, file) pair.
2252 * Issue otw v4 call to have the server do the same.
2255 rp = VTOR4(vp);
2258 * zone_enter(2) prevents processes from changing zones with NFS files
2259 * open; if we happen to get here from the wrong zone we can't do
2260 * anything over the wire.
2262 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2264 * We could attempt to clean up locks, except we're sure
2265 * that the current process didn't acquire any locks on
2266 * the file: any attempt to lock a file belong to another zone
2267 * will fail, and one can't lock an NFS file and then change
2268 * zones, as that fails too.
2270 * Returning an error here is the sane thing to do. A
2271 * subsequent call to VN_RELE() which translates to a
2272 * nfs4_inactive() will clean up state: if the zone of the
2273 * vnode's origin is still alive and kicking, the inactive
2274 * thread will handle the request (from the correct zone), and
2275 * everything (minus the OTW close call) should be OK. If the
2276 * zone is going away nfs4_async_inactive() will throw away
2277 * delegations, open streams and cached pages inline.
2279 return (EIO);
2283 * If we are using local locking for this filesystem, then
2284 * release all of the SYSV style record locks. Otherwise,
2285 * we are doing network locking and we need to release all
2286 * of the network locks. All of the locks held by this
2287 * process on this file are released no matter what the
2288 * incoming reference count is.
2290 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2291 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2292 cleanshares(vp, ttoproc(curthread)->p_pid);
2293 } else
2294 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2296 if (e.error) {
2297 struct lm_sysid *lmsid;
2298 lmsid = nfs4_find_sysid(VTOMI4(vp));
2299 if (lmsid == NULL) {
2300 DTRACE_PROBE2(unknown__sysid, int, e.error,
2301 vnode_t *, vp);
2302 } else {
2303 cleanlocks(vp, ttoproc(curthread)->p_pid,
2304 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2306 lm_rel_sysid(lmsid);
2308 return (e.error);
2311 if (count > 1)
2312 return (0);
2315 * If the file has been `unlinked', then purge the
2316 * DNLC so that this vnode will get reycled quicker
2317 * and the .nfs* file on the server will get removed.
2319 if (rp->r_unldvp != NULL)
2320 dnlc_purge_vp(vp);
2323 * If the file was open for write and there are pages,
2324 * do a synchronous flush and commit of all of the
2325 * dirty and uncommitted pages.
2327 ASSERT(!e.error);
2328 if ((flag & FWRITE) && nfs4_has_pages(vp))
2329 error = nfs4_putpage_commit(vp, 0, 0, cr);
2331 mutex_enter(&rp->r_statelock);
2332 r_error = rp->r_error;
2333 rp->r_error = 0;
2334 mutex_exit(&rp->r_statelock);
2337 * If this file type is one for which no explicit 'open' was
2338 * done, then bail now (ie. no need for protocol 'close'). If
2339 * there was an error w/the vm subsystem, return _that_ error,
2340 * otherwise, return any errors that may've been reported via
2341 * the rnode.
2343 if (vp->v_type != VREG)
2344 return (error ? error : r_error);
2347 * The sync putpage commit may have failed above, but since
2348 * we're working w/a regular file, we need to do the protocol
2349 * 'close' (nfs4close_one will figure out if an otw close is
2350 * needed or not). Report any errors _after_ doing the protocol
2351 * 'close'.
2353 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2354 n4error = e.error ? e.error : geterrno4(e.stat);
2357 * Error reporting prio (Hi -> Lo)
2359 * i) nfs4_putpage_commit (error)
2360 * ii) rnode's (r_error)
2361 * iii) nfs4close_one (n4error)
2363 return (error ? error : (r_error ? r_error : n4error));
2367 * Initialize *lost_rqstp.
2370 static void
2371 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2372 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2373 vnode_t *vp)
2375 if (error != ETIMEDOUT && error != EINTR &&
2376 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2377 lost_rqstp->lr_op = 0;
2378 return;
2381 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2382 "nfs4close_save_lost_rqst: error %d", error));
2384 lost_rqstp->lr_op = OP_CLOSE;
2386 * The vp is held and rele'd via the recovery code.
2387 * See nfs4_save_lost_rqst.
2389 lost_rqstp->lr_vp = vp;
2390 lost_rqstp->lr_dvp = NULL;
2391 lost_rqstp->lr_oop = oop;
2392 lost_rqstp->lr_osp = osp;
2393 ASSERT(osp != NULL);
2394 ASSERT(mutex_owned(&osp->os_sync_lock));
2395 osp->os_pending_close = 1;
2396 lost_rqstp->lr_lop = NULL;
2397 lost_rqstp->lr_cr = cr;
2398 lost_rqstp->lr_flk = NULL;
2399 lost_rqstp->lr_putfirst = FALSE;
2403 * Assumes you already have the open seqid sync grabbed as well as the
2404 * 'os_sync_lock'. Note: this will release the open seqid sync and
2405 * 'os_sync_lock' if client recovery starts. Calling functions have to
2406 * be prepared to handle this.
2408 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2409 * was needed and was started, and that the calling function should retry
2410 * this function; otherwise it is returned as 0.
2412 * Errors are returned via the nfs4_error_t parameter.
2414 static void
2415 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2416 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2417 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2419 COMPOUND4args_clnt args;
2420 COMPOUND4res_clnt res;
2421 CLOSE4args *close_args;
2422 nfs_resop4 *resop;
2423 nfs_argop4 argop[3];
2424 int doqueue = 1;
2425 mntinfo4_t *mi;
2426 seqid4 seqid;
2427 vnode_t *vp;
2428 bool_t needrecov = FALSE;
2429 nfs4_lost_rqst_t lost_rqst;
2430 hrtime_t t;
2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2434 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2436 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2438 /* Only set this to 1 if recovery is started */
2439 *recov = 0;
2441 /* do the OTW call to close the file */
2443 if (close_type == CLOSE_RESEND)
2444 args.ctag = TAG_CLOSE_LOST;
2445 else if (close_type == CLOSE_AFTER_RESEND)
2446 args.ctag = TAG_CLOSE_UNDO;
2447 else
2448 args.ctag = TAG_CLOSE;
2450 args.array_len = 3;
2451 args.array = argop;
2453 vp = RTOV4(rp);
2455 mi = VTOMI4(vp);
2457 /* putfh target fh */
2458 argop[0].argop = OP_CPUTFH;
2459 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2461 argop[1].argop = OP_GETATTR;
2462 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2463 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2465 argop[2].argop = OP_CLOSE;
2466 close_args = &argop[2].nfs_argop4_u.opclose;
2468 seqid = nfs4_get_open_seqid(oop) + 1;
2470 close_args->seqid = seqid;
2471 close_args->open_stateid = osp->open_stateid;
2473 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2474 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2475 rnode4info(rp)));
2477 t = gethrtime();
2479 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2481 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2482 nfs4_set_open_seqid(seqid, oop, args.ctag);
2485 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2486 if (ep->error && !needrecov) {
2488 * if there was an error and no recovery is to be done
2489 * then then set up the file to flush its cache if
2490 * needed for the next caller.
2492 mutex_enter(&rp->r_statelock);
2493 PURGE_ATTRCACHE4_LOCKED(rp);
2494 rp->r_flags &= ~R4WRITEMODIFIED;
2495 mutex_exit(&rp->r_statelock);
2496 return;
2499 if (needrecov) {
2500 bool_t abort;
2501 nfs4_bseqid_entry_t *bsep = NULL;
2503 if (close_type != CLOSE_RESEND)
2504 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2505 osp, cred_otw, vp);
2507 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2508 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2509 0, args.ctag, close_args->seqid);
2511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2512 "nfs4close_otw: initiating recovery. error %d "
2513 "res.status %d", ep->error, res.status));
2516 * Drop the 'os_sync_lock' here so we don't hit
2517 * a potential recursive mutex_enter via an
2518 * 'open_stream_hold()'.
2520 mutex_exit(&osp->os_sync_lock);
2521 *have_sync_lockp = 0;
2522 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2523 (close_type != CLOSE_RESEND &&
2524 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2525 OP_CLOSE, bsep, NULL, NULL);
2527 /* drop open seq sync, and let the calling function regrab it */
2528 nfs4_end_open_seqid_sync(oop);
2529 *did_start_seqid_syncp = 0;
2531 if (bsep)
2532 kmem_free(bsep, sizeof (*bsep));
2534 * For signals, the caller wants to quit, so don't say to
2535 * retry. For forced unmount, if it's a user thread, it
2536 * wants to quit. If it's a recovery thread, the retry
2537 * will happen higher-up on the call stack. Either way,
2538 * don't say to retry.
2540 if (abort == FALSE && ep->error != EINTR &&
2541 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2542 close_type != CLOSE_RESEND &&
2543 close_type != CLOSE_AFTER_RESEND)
2544 *recov = 1;
2545 else
2546 *recov = 0;
2548 if (!ep->error)
2549 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2550 return;
2553 if (res.status) {
2554 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2555 return;
2558 mutex_enter(&rp->r_statev4_lock);
2559 rp->created_v4 = 0;
2560 mutex_exit(&rp->r_statev4_lock);
2562 resop = &res.array[2];
2563 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2564 osp->os_valid = 0;
2567 * This removes the reference obtained at OPEN; ie, when the
2568 * open stream structure was created.
2570 * We don't have to worry about calling 'open_stream_rele'
2571 * since we our currently holding a reference to the open
2572 * stream which means the count cannot go to 0 with this
2573 * decrement.
2575 ASSERT(osp->os_ref_count >= 2);
2576 osp->os_ref_count--;
2578 if (ep->error == 0) {
2580 * Avoid a deadlock with the r_serial thread waiting for
2581 * os_sync_lock in nfs4_get_otw_cred_by_osp() which might be
2582 * held by us. We will wait in nfs4_attr_cache() for the
2583 * completion of the r_serial thread.
2585 mutex_exit(&osp->os_sync_lock);
2586 *have_sync_lockp = 0;
2588 nfs4_attr_cache(vp,
2589 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2590 t, cred_otw, TRUE, NULL);
2593 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2594 " returning %d", ep->error));
2596 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2599 /* ARGSUSED */
2600 static int
2601 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2602 caller_context_t *ct)
2604 rnode4_t *rp;
2605 uoff_t off;
2606 offset_t diff;
2607 uint_t on;
2608 uint_t n;
2609 caddr_t base;
2610 uint_t flags;
2611 int error;
2612 mntinfo4_t *mi;
2614 rp = VTOR4(vp);
2616 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2618 if (IS_SHADOW(vp, rp))
2619 vp = RTOV4(rp);
2621 if (vp->v_type != VREG)
2622 return (EISDIR);
2624 mi = VTOMI4(vp);
2626 if (nfs_zone() != mi->mi_zone)
2627 return (EIO);
2629 if (uiop->uio_resid == 0)
2630 return (0);
2632 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2633 return (EINVAL);
2635 mutex_enter(&rp->r_statelock);
2636 if (rp->r_flags & R4RECOVERRP)
2637 error = (rp->r_error ? rp->r_error : EIO);
2638 else
2639 error = 0;
2640 mutex_exit(&rp->r_statelock);
2641 if (error)
2642 return (error);
2645 * Bypass VM if caching has been disabled (e.g., locking) or if
2646 * using client-side direct I/O and the file is not mmap'd and
2647 * there are no cached pages.
2649 if ((vp->v_flag & VNOCACHE) ||
2650 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2651 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2652 size_t resid = 0;
2654 return (nfs4read(vp, NULL, uiop->uio_loffset,
2655 uiop->uio_resid, &resid, cr, FALSE, uiop));
2658 error = 0;
2660 do {
2661 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2662 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2663 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2665 if (error = nfs4_validate_caches(vp, cr))
2666 break;
2668 mutex_enter(&rp->r_statelock);
2669 while (rp->r_flags & R4INCACHEPURGE) {
2670 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2671 mutex_exit(&rp->r_statelock);
2672 return (EINTR);
2675 diff = rp->r_size - uiop->uio_loffset;
2676 mutex_exit(&rp->r_statelock);
2677 if (diff <= 0)
2678 break;
2679 if (diff < n)
2680 n = (uint_t)diff;
2682 if (vpm_enable) {
2684 * Copy data.
2686 error = vpm_data_copy(vp, off + on, n, uiop,
2687 1, NULL, 0, S_READ);
2688 } else {
2689 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2690 S_READ);
2692 error = uiomove(base + on, n, UIO_READ, uiop);
2695 if (!error) {
2697 * If read a whole block or read to eof,
2698 * won't need this buffer again soon.
2700 mutex_enter(&rp->r_statelock);
2701 if (n + on == MAXBSIZE ||
2702 uiop->uio_loffset == rp->r_size)
2703 flags = SM_DONTNEED;
2704 else
2705 flags = 0;
2706 mutex_exit(&rp->r_statelock);
2707 if (vpm_enable) {
2708 error = vpm_sync_pages(vp, off, n, flags);
2709 } else {
2710 error = segmap_release(segkmap, base, flags);
2712 } else {
2713 if (vpm_enable) {
2714 (void) vpm_sync_pages(vp, off, n, 0);
2715 } else {
2716 (void) segmap_release(segkmap, base, 0);
2719 } while (!error && uiop->uio_resid > 0);
2721 return (error);
2724 /* ARGSUSED */
2725 static int
2726 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2727 caller_context_t *ct)
2729 rlim_t limit = uiop->uio_llimit;
2730 rnode4_t *rp;
2731 uoff_t off;
2732 caddr_t base;
2733 uint_t flags;
2734 int remainder;
2735 size_t n;
2736 int on;
2737 int error;
2738 int resid;
2739 uoff_t offset;
2740 mntinfo4_t *mi;
2741 uint_t bsize;
2743 rp = VTOR4(vp);
2745 if (IS_SHADOW(vp, rp))
2746 vp = RTOV4(rp);
2748 if (vp->v_type != VREG)
2749 return (EISDIR);
2751 mi = VTOMI4(vp);
2753 if (nfs_zone() != mi->mi_zone)
2754 return (EIO);
2756 if (uiop->uio_resid == 0)
2757 return (0);
2759 mutex_enter(&rp->r_statelock);
2760 if (rp->r_flags & R4RECOVERRP)
2761 error = (rp->r_error ? rp->r_error : EIO);
2762 else
2763 error = 0;
2764 mutex_exit(&rp->r_statelock);
2765 if (error)
2766 return (error);
2768 if (ioflag & FAPPEND) {
2769 struct vattr va;
2772 * Must serialize if appending.
2774 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2775 nfs_rw_exit(&rp->r_rwlock);
2776 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2777 INTR4(vp)))
2778 return (EINTR);
2781 va.va_mask = VATTR_SIZE;
2782 error = nfs4getattr(vp, &va, cr);
2783 if (error)
2784 return (error);
2785 uiop->uio_loffset = va.va_size;
2788 offset = uiop->uio_loffset + uiop->uio_resid;
2790 if (uiop->uio_loffset < 0 || offset < 0)
2791 return (EINVAL);
2793 if (limit == RLIM_INFINITY || limit > MAXOFFSET_T)
2794 limit = MAXOFFSET_T;
2797 * Check to make sure that the process will not exceed
2798 * its limit on file size. It is okay to write up to
2799 * the limit, but not beyond. Thus, the write which
2800 * reaches the limit will be short and the next write
2801 * will return an error.
2803 remainder = 0;
2804 if (offset > uiop->uio_llimit) {
2805 remainder = offset - uiop->uio_llimit;
2806 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2807 if (uiop->uio_resid <= 0) {
2808 proc_t *p = ttoproc(curthread);
2810 uiop->uio_resid += remainder;
2811 mutex_enter(&p->p_lock);
2812 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2813 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2814 mutex_exit(&p->p_lock);
2815 return (EFBIG);
2819 /* update the change attribute, if we have a write delegation */
2821 mutex_enter(&rp->r_statev4_lock);
2822 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2823 rp->r_deleg_change++;
2825 mutex_exit(&rp->r_statev4_lock);
2827 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2828 return (EINTR);
2831 * Bypass VM if caching has been disabled (e.g., locking) or if
2832 * using client-side direct I/O and the file is not mmap'd and
2833 * there are no cached pages.
2835 if ((vp->v_flag & VNOCACHE) ||
2836 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2837 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2838 size_t bufsize;
2839 int count;
2840 uoff_t org_offset;
2841 stable_how4 stab_comm;
2842 nfs4_fwrite:
2843 if (rp->r_flags & R4STALE) {
2844 resid = uiop->uio_resid;
2845 offset = uiop->uio_loffset;
2846 error = rp->r_error;
2848 * A close may have cleared r_error, if so,
2849 * propagate ESTALE error return properly
2851 if (error == 0)
2852 error = ESTALE;
2853 goto bottom;
2856 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2857 base = kmem_alloc(bufsize, KM_SLEEP);
2858 do {
2859 if (ioflag & FDSYNC)
2860 stab_comm = DATA_SYNC4;
2861 else
2862 stab_comm = FILE_SYNC4;
2863 resid = uiop->uio_resid;
2864 offset = uiop->uio_loffset;
2865 count = MIN(uiop->uio_resid, bufsize);
2866 org_offset = uiop->uio_loffset;
2867 error = uiomove(base, count, UIO_WRITE, uiop);
2868 if (!error) {
2869 error = nfs4write(vp, base, org_offset,
2870 count, cr, &stab_comm);
2871 if (!error) {
2872 mutex_enter(&rp->r_statelock);
2873 if (rp->r_size < uiop->uio_loffset)
2874 rp->r_size = uiop->uio_loffset;
2875 mutex_exit(&rp->r_statelock);
2878 } while (!error && uiop->uio_resid > 0);
2879 kmem_free(base, bufsize);
2880 goto bottom;
2883 bsize = vp->v_vfsp->vfs_bsize;
2885 do {
2886 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2887 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2888 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2890 resid = uiop->uio_resid;
2891 offset = uiop->uio_loffset;
2893 if (rp->r_flags & R4STALE) {
2894 error = rp->r_error;
2896 * A close may have cleared r_error, if so,
2897 * propagate ESTALE error return properly
2899 if (error == 0)
2900 error = ESTALE;
2901 break;
2905 * Don't create dirty pages faster than they
2906 * can be cleaned so that the system doesn't
2907 * get imbalanced. If the async queue is
2908 * maxed out, then wait for it to drain before
2909 * creating more dirty pages. Also, wait for
2910 * any threads doing pagewalks in the vop_getattr
2911 * entry points so that they don't block for
2912 * long periods.
2914 mutex_enter(&rp->r_statelock);
2915 while ((mi->mi_max_threads != 0 &&
2916 rp->r_awcount > 2 * mi->mi_max_threads) ||
2917 rp->r_gcount > 0) {
2918 if (INTR4(vp)) {
2919 klwp_t *lwp = ttolwp(curthread);
2921 if (lwp != NULL)
2922 lwp->lwp_nostop++;
2923 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2924 mutex_exit(&rp->r_statelock);
2925 if (lwp != NULL)
2926 lwp->lwp_nostop--;
2927 error = EINTR;
2928 goto bottom;
2930 if (lwp != NULL)
2931 lwp->lwp_nostop--;
2932 } else
2933 cv_wait(&rp->r_cv, &rp->r_statelock);
2935 mutex_exit(&rp->r_statelock);
2938 * Touch the page and fault it in if it is not in core
2939 * before segmap_getmapflt or vpm_data_copy can lock it.
2940 * This is to avoid the deadlock if the buffer is mapped
2941 * to the same file through mmap which we want to write.
2943 uio_prefaultpages((long)n, uiop);
2945 if (vpm_enable) {
2947 * It will use kpm mappings, so no need to
2948 * pass an address.
2950 error = writerp4(rp, NULL, n, uiop, 0);
2951 } else {
2952 if (segmap_kpm) {
2953 int pon = uiop->uio_loffset & PAGEOFFSET;
2954 size_t pn = MIN(PAGESIZE - pon,
2955 uiop->uio_resid);
2956 int pagecreate;
2958 mutex_enter(&rp->r_statelock);
2959 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2960 uiop->uio_loffset + pn >= rp->r_size);
2961 mutex_exit(&rp->r_statelock);
2963 base = segmap_getmapflt(segkmap, vp, off + on,
2964 pn, !pagecreate, S_WRITE);
2966 error = writerp4(rp, base + pon, n, uiop,
2967 pagecreate);
2969 } else {
2970 base = segmap_getmapflt(segkmap, vp, off + on,
2971 n, 0, S_READ);
2972 error = writerp4(rp, base + on, n, uiop, 0);
2976 if (!error) {
2977 if (mi->mi_flags & MI4_NOAC)
2978 flags = SM_WRITE;
2979 else if ((uiop->uio_loffset % bsize) == 0 ||
2980 IS_SWAPVP(vp)) {
2982 * Have written a whole block.
2983 * Start an asynchronous write
2984 * and mark the buffer to
2985 * indicate that it won't be
2986 * needed again soon.
2988 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2989 } else
2990 flags = 0;
2991 if ((ioflag & (FSYNC|FDSYNC)) ||
2992 (rp->r_flags & R4OUTOFSPACE)) {
2993 flags &= ~SM_ASYNC;
2994 flags |= SM_WRITE;
2996 if (vpm_enable) {
2997 error = vpm_sync_pages(vp, off, n, flags);
2998 } else {
2999 error = segmap_release(segkmap, base, flags);
3001 } else {
3002 if (vpm_enable) {
3003 (void) vpm_sync_pages(vp, off, n, 0);
3004 } else {
3005 (void) segmap_release(segkmap, base, 0);
3008 * In the event that we got an access error while
3009 * faulting in a page for a write-only file just
3010 * force a write.
3012 if (error == EACCES)
3013 goto nfs4_fwrite;
3015 } while (!error && uiop->uio_resid > 0);
3017 bottom:
3018 if (error) {
3019 uiop->uio_resid = resid + remainder;
3020 uiop->uio_loffset = offset;
3021 } else {
3022 uiop->uio_resid += remainder;
3024 mutex_enter(&rp->r_statev4_lock);
3025 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3026 gethrestime(&rp->r_attr.va_mtime);
3027 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3029 mutex_exit(&rp->r_statev4_lock);
3032 nfs_rw_exit(&rp->r_lkserlock);
3034 return (error);
3038 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3040 static int
3041 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, uoff_t off, size_t len,
3042 int flags, cred_t *cr)
3044 struct buf *bp;
3045 int error;
3046 page_t *savepp;
3047 uchar_t fsdata;
3048 stable_how4 stab_comm;
3050 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3051 bp = pageio_setup(pp, len, vp, flags);
3052 ASSERT(bp != NULL);
3055 * pageio_setup should have set b_addr to 0. This
3056 * is correct since we want to do I/O on a page
3057 * boundary. bp_mapin will use this addr to calculate
3058 * an offset, and then set b_addr to the kernel virtual
3059 * address it allocated for us.
3061 ASSERT(bp->b_un.b_addr == 0);
3063 bp->b_edev = 0;
3064 bp->b_dev = 0;
3065 bp->b_lblkno = lbtodb(off);
3066 bp->b_file = vp;
3067 bp->b_offset = (offset_t)off;
3068 bp_mapin(bp);
3070 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3071 freemem > desfree)
3072 stab_comm = UNSTABLE4;
3073 else
3074 stab_comm = FILE_SYNC4;
3076 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3078 bp_mapout(bp);
3079 pageio_done(bp);
3081 if (stab_comm == UNSTABLE4)
3082 fsdata = C_DELAYCOMMIT;
3083 else
3084 fsdata = C_NOCOMMIT;
3086 savepp = pp;
3087 do {
3088 pp->p_fsdata = fsdata;
3089 } while ((pp = pp->p_next) != savepp);
3091 return (error);
3096 static int
3097 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3099 nfs4_open_owner_t *oop;
3100 nfs4_open_stream_t *osp;
3101 rnode4_t *rp = VTOR4(vp);
3102 mntinfo4_t *mi = VTOMI4(vp);
3103 int reopen_needed;
3105 ASSERT(nfs_zone() == mi->mi_zone);
3108 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3109 if (!oop)
3110 return (EIO);
3112 /* returns with 'os_sync_lock' held */
3113 osp = find_open_stream(oop, rp);
3114 if (!osp) {
3115 open_owner_rele(oop);
3116 return (EIO);
3119 if (osp->os_failed_reopen) {
3120 mutex_exit(&osp->os_sync_lock);
3121 open_stream_rele(osp, rp);
3122 open_owner_rele(oop);
3123 return (EIO);
3127 * Determine whether a reopen is needed. If this
3128 * is a delegation open stream, then the os_delegation bit
3129 * should be set.
3132 reopen_needed = osp->os_delegation;
3134 mutex_exit(&osp->os_sync_lock);
3135 open_owner_rele(oop);
3137 if (reopen_needed) {
3138 nfs4_error_zinit(ep);
3139 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3140 mutex_enter(&osp->os_sync_lock);
3141 if (ep->error || ep->stat || osp->os_failed_reopen) {
3142 mutex_exit(&osp->os_sync_lock);
3143 open_stream_rele(osp, rp);
3144 return (EIO);
3146 mutex_exit(&osp->os_sync_lock);
3148 open_stream_rele(osp, rp);
3150 return (0);
3154 * Write to file. Writes to remote server in largest size
3155 * chunks that the server can handle. Write is synchronous.
3157 static int
3158 nfs4write(vnode_t *vp, caddr_t base, uoff_t offset, int count, cred_t *cr,
3159 stable_how4 *stab_comm)
3161 mntinfo4_t *mi;
3162 COMPOUND4args_clnt args;
3163 COMPOUND4res_clnt res;
3164 WRITE4args *wargs;
3165 WRITE4res *wres;
3166 nfs_argop4 argop[2];
3167 nfs_resop4 *resop;
3168 int tsize;
3169 stable_how4 stable;
3170 rnode4_t *rp;
3171 int doqueue = 1;
3172 bool_t needrecov;
3173 nfs4_recov_state_t recov_state;
3174 nfs4_stateid_types_t sid_types;
3175 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3176 int recov;
3178 rp = VTOR4(vp);
3179 mi = VTOMI4(vp);
3181 ASSERT(nfs_zone() == mi->mi_zone);
3183 stable = *stab_comm;
3184 *stab_comm = FILE_SYNC4;
3186 needrecov = FALSE;
3187 recov_state.rs_flags = 0;
3188 recov_state.rs_num_retry_despite_err = 0;
3189 nfs4_init_stateid_types(&sid_types);
3191 /* Is curthread the recovery thread? */
3192 mutex_enter(&mi->mi_lock);
3193 recov = (mi->mi_recovthread == curthread);
3194 mutex_exit(&mi->mi_lock);
3196 recov_retry:
3197 args.ctag = TAG_WRITE;
3198 args.array_len = 2;
3199 args.array = argop;
3201 if (!recov) {
3202 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3203 &recov_state, NULL);
3204 if (e.error)
3205 return (e.error);
3208 /* 0. putfh target fh */
3209 argop[0].argop = OP_CPUTFH;
3210 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3212 /* 1. write */
3213 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3215 do {
3217 wargs->offset = (offset4)offset;
3218 wargs->data_val = base;
3220 if (mi->mi_io_kstats) {
3221 mutex_enter(&mi->mi_lock);
3222 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3223 mutex_exit(&mi->mi_lock);
3226 if ((vp->v_flag & VNOCACHE) ||
3227 (rp->r_flags & R4DIRECTIO) ||
3228 (mi->mi_flags & MI4_DIRECTIO))
3229 tsize = MIN(mi->mi_stsize, count);
3230 else
3231 tsize = MIN(mi->mi_curwrite, count);
3232 wargs->data_len = (uint_t)tsize;
3233 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3235 if (mi->mi_io_kstats) {
3236 mutex_enter(&mi->mi_lock);
3237 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3238 mutex_exit(&mi->mi_lock);
3241 if (!recov) {
3242 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3243 if (e.error && !needrecov) {
3244 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3245 &recov_state, needrecov);
3246 return (e.error);
3248 } else {
3249 if (e.error)
3250 return (e.error);
3254 * Do handling of OLD_STATEID outside
3255 * of the normal recovery framework.
3257 * If write receives a BAD stateid error while using a
3258 * delegation stateid, retry using the open stateid (if it
3259 * exists). If it doesn't have an open stateid, reopen the
3260 * file first, then retry.
3262 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3263 sid_types.cur_sid_type != SPEC_SID) {
3264 nfs4_save_stateid(&wargs->stateid, &sid_types);
3265 if (!recov)
3266 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3267 &recov_state, needrecov);
3268 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3269 goto recov_retry;
3270 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3271 sid_types.cur_sid_type == DEL_SID) {
3272 nfs4_save_stateid(&wargs->stateid, &sid_types);
3273 mutex_enter(&rp->r_statev4_lock);
3274 rp->r_deleg_return_pending = TRUE;
3275 mutex_exit(&rp->r_statev4_lock);
3276 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3277 if (!recov)
3278 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3279 &recov_state, needrecov);
3280 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3281 return (EIO);
3283 if (!recov)
3284 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3285 &recov_state, needrecov);
3286 /* hold needed for nfs4delegreturn_thread */
3287 VN_HOLD(vp);
3288 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3289 NFS4_DR_DISCARD), FALSE);
3290 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3291 goto recov_retry;
3294 if (needrecov) {
3295 bool_t abort;
3297 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3298 "nfs4write: client got error %d, res.status %d"
3299 ", so start recovery", e.error, res.status));
3301 abort = nfs4_start_recovery(&e,
3302 VTOMI4(vp), vp, NULL, &wargs->stateid,
3303 NULL, OP_WRITE, NULL, NULL, NULL);
3304 if (!e.error) {
3305 e.error = geterrno4(res.status);
3306 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3308 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3309 &recov_state, needrecov);
3310 if (abort == FALSE)
3311 goto recov_retry;
3312 return (e.error);
3315 if (res.status) {
3316 e.error = geterrno4(res.status);
3317 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3318 if (!recov)
3319 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3320 &recov_state, needrecov);
3321 return (e.error);
3324 resop = &res.array[1]; /* write res */
3325 wres = &resop->nfs_resop4_u.opwrite;
3327 if ((int)wres->count > tsize) {
3328 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3330 zcmn_err(getzoneid(), CE_WARN,
3331 "nfs4write: server wrote %u, requested was %u",
3332 (int)wres->count, tsize);
3333 if (!recov)
3334 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3335 &recov_state, needrecov);
3336 return (EIO);
3338 if (wres->committed == UNSTABLE4) {
3339 *stab_comm = UNSTABLE4;
3340 if (wargs->stable == DATA_SYNC4 ||
3341 wargs->stable == FILE_SYNC4) {
3342 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3343 zcmn_err(getzoneid(), CE_WARN,
3344 "nfs4write: server %s did not commit "
3345 "to stable storage",
3346 rp->r_server->sv_hostname);
3347 if (!recov)
3348 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3349 OH_WRITE, &recov_state, needrecov);
3350 return (EIO);
3354 tsize = (int)wres->count;
3355 count -= tsize;
3356 base += tsize;
3357 offset += tsize;
3358 if (mi->mi_io_kstats) {
3359 mutex_enter(&mi->mi_lock);
3360 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3361 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3362 tsize;
3363 mutex_exit(&mi->mi_lock);
3365 lwp_stat_update(LWP_STAT_OUBLK, 1);
3366 mutex_enter(&rp->r_statelock);
3367 if (rp->r_flags & R4HAVEVERF) {
3368 if (rp->r_writeverf != wres->writeverf) {
3369 nfs4_set_mod(vp);
3370 rp->r_writeverf = wres->writeverf;
3372 } else {
3373 rp->r_writeverf = wres->writeverf;
3374 rp->r_flags |= R4HAVEVERF;
3376 PURGE_ATTRCACHE4_LOCKED(rp);
3377 rp->r_flags |= R4WRITEMODIFIED;
3378 gethrestime(&rp->r_attr.va_mtime);
3379 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3380 mutex_exit(&rp->r_statelock);
3381 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3382 } while (count);
3384 if (!recov)
3385 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3386 needrecov);
3388 return (e.error);
3392 * Read from a file. Reads data in largest chunks our interface can handle.
3394 static int
3395 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3396 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3398 mntinfo4_t *mi;
3399 COMPOUND4args_clnt args;
3400 COMPOUND4res_clnt res;
3401 READ4args *rargs;
3402 nfs_argop4 argop[2];
3403 int tsize;
3404 int doqueue;
3405 rnode4_t *rp;
3406 int data_len;
3407 bool_t is_eof;
3408 bool_t needrecov = FALSE;
3409 nfs4_recov_state_t recov_state;
3410 nfs4_stateid_types_t sid_types;
3411 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3413 rp = VTOR4(vp);
3414 mi = VTOMI4(vp);
3415 doqueue = 1;
3417 ASSERT(nfs_zone() == mi->mi_zone);
3419 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3421 args.array_len = 2;
3422 args.array = argop;
3424 nfs4_init_stateid_types(&sid_types);
3426 recov_state.rs_flags = 0;
3427 recov_state.rs_num_retry_despite_err = 0;
3429 recov_retry:
3430 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3431 &recov_state, NULL);
3432 if (e.error)
3433 return (e.error);
3435 /* putfh target fh */
3436 argop[0].argop = OP_CPUTFH;
3437 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3439 /* read */
3440 argop[1].argop = OP_READ;
3441 rargs = &argop[1].nfs_argop4_u.opread;
3442 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3443 OP_READ, &sid_types, async);
3445 do {
3446 if (mi->mi_io_kstats) {
3447 mutex_enter(&mi->mi_lock);
3448 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3449 mutex_exit(&mi->mi_lock);
3452 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3453 "nfs4read: %s call, rp %s",
3454 needrecov ? "recov" : "first",
3455 rnode4info(rp)));
3457 if ((vp->v_flag & VNOCACHE) ||
3458 (rp->r_flags & R4DIRECTIO) ||
3459 (mi->mi_flags & MI4_DIRECTIO))
3460 tsize = MIN(mi->mi_tsize, count);
3461 else
3462 tsize = MIN(mi->mi_curread, count);
3464 rargs->offset = (offset4)offset;
3465 rargs->count = (count4)tsize;
3466 rargs->res_data_val_alt = NULL;
3467 rargs->res_mblk = NULL;
3468 rargs->res_uiop = NULL;
3469 rargs->res_maxsize = 0;
3470 rargs->wlist = NULL;
3472 if (uiop)
3473 rargs->res_uiop = uiop;
3474 else
3475 rargs->res_data_val_alt = base;
3476 rargs->res_maxsize = tsize;
3478 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3479 #ifdef DEBUG
3480 if (nfs4read_error_inject) {
3481 res.status = nfs4read_error_inject;
3482 nfs4read_error_inject = 0;
3484 #endif
3486 if (mi->mi_io_kstats) {
3487 mutex_enter(&mi->mi_lock);
3488 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3489 mutex_exit(&mi->mi_lock);
3492 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3493 if (e.error != 0 && !needrecov) {
3494 nfs4_end_fop(mi, vp, NULL, OH_READ,
3495 &recov_state, needrecov);
3496 return (e.error);
3500 * Do proper retry for OLD and BAD stateid errors outside
3501 * of the normal recovery framework. There are two differences
3502 * between async and sync reads. The first is that we allow
3503 * retry on BAD_STATEID for async reads, but not sync reads.
3504 * The second is that we mark the file dead for a failed
3505 * attempt with a special stateid for sync reads, but just
3506 * return EIO for async reads.
3508 * If a sync read receives a BAD stateid error while using a
3509 * delegation stateid, retry using the open stateid (if it
3510 * exists). If it doesn't have an open stateid, reopen the
3511 * file first, then retry.
3513 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3514 res.status == NFS4ERR_BAD_STATEID) && async) {
3515 nfs4_end_fop(mi, vp, NULL, OH_READ,
3516 &recov_state, needrecov);
3517 if (sid_types.cur_sid_type == SPEC_SID) {
3518 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3519 return (EIO);
3521 nfs4_save_stateid(&rargs->stateid, &sid_types);
3522 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3523 goto recov_retry;
3524 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3525 !async && sid_types.cur_sid_type != SPEC_SID) {
3526 nfs4_save_stateid(&rargs->stateid, &sid_types);
3527 nfs4_end_fop(mi, vp, NULL, OH_READ,
3528 &recov_state, needrecov);
3529 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3530 goto recov_retry;
3531 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3532 sid_types.cur_sid_type == DEL_SID) {
3533 nfs4_save_stateid(&rargs->stateid, &sid_types);
3534 mutex_enter(&rp->r_statev4_lock);
3535 rp->r_deleg_return_pending = TRUE;
3536 mutex_exit(&rp->r_statev4_lock);
3537 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3538 nfs4_end_fop(mi, vp, NULL, OH_READ,
3539 &recov_state, needrecov);
3540 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3541 return (EIO);
3543 nfs4_end_fop(mi, vp, NULL, OH_READ,
3544 &recov_state, needrecov);
3545 /* hold needed for nfs4delegreturn_thread */
3546 VN_HOLD(vp);
3547 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3548 NFS4_DR_DISCARD), FALSE);
3549 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3550 goto recov_retry;
3552 if (needrecov) {
3553 bool_t abort;
3555 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3556 "nfs4read: initiating recovery\n"));
3557 abort = nfs4_start_recovery(&e,
3558 mi, vp, NULL, &rargs->stateid,
3559 NULL, OP_READ, NULL, NULL, NULL);
3560 nfs4_end_fop(mi, vp, NULL, OH_READ,
3561 &recov_state, needrecov);
3563 * Do not retry if we got OLD_STATEID using a special
3564 * stateid. This avoids looping with a broken server.
3566 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3567 sid_types.cur_sid_type == SPEC_SID)
3568 abort = TRUE;
3570 if (abort == FALSE) {
3572 * Need to retry all possible stateids in
3573 * case the recovery error wasn't stateid
3574 * related or the stateids have become
3575 * stale (server reboot).
3577 nfs4_init_stateid_types(&sid_types);
3578 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3579 goto recov_retry;
3582 if (!e.error) {
3583 e.error = geterrno4(res.status);
3584 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3586 return (e.error);
3589 if (res.status) {
3590 e.error = geterrno4(res.status);
3591 nfs4_end_fop(mi, vp, NULL, OH_READ,
3592 &recov_state, needrecov);
3593 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3594 return (e.error);
3597 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3598 count -= data_len;
3599 if (base)
3600 base += data_len;
3601 offset += data_len;
3602 if (mi->mi_io_kstats) {
3603 mutex_enter(&mi->mi_lock);
3604 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3605 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3606 mutex_exit(&mi->mi_lock);
3608 lwp_stat_update(LWP_STAT_INBLK, 1);
3609 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3610 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3612 } while (count && !is_eof);
3614 *residp = count;
3616 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3618 return (e.error);
3621 /* ARGSUSED */
3622 static int
3623 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3624 caller_context_t *ct)
3626 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3627 return (EIO);
3628 switch (cmd) {
3629 case _FIODIRECTIO:
3630 return (nfs4_directio(vp, (int)arg, cr));
3631 default:
3632 return (ENOTTY);
3636 /* ARGSUSED */
3638 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3639 caller_context_t *ct)
3641 int error;
3642 rnode4_t *rp = VTOR4(vp);
3644 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3645 return (EIO);
3647 * If it has been specified that the return value will
3648 * just be used as a hint, and we are only being asked
3649 * for size, fsid or rdevid, then return the client's
3650 * notion of these values without checking to make sure
3651 * that the attribute cache is up to date.
3652 * The whole point is to avoid an over the wire GETATTR
3653 * call.
3655 if (flags & ATTR_HINT) {
3656 if (!(vap->va_mask & ~(VATTR_SIZE | VATTR_FSID | VATTR_RDEV))) {
3657 mutex_enter(&rp->r_statelock);
3658 if (vap->va_mask & VATTR_SIZE)
3659 vap->va_size = rp->r_size;
3660 if (vap->va_mask & VATTR_FSID)
3661 vap->va_fsid = rp->r_attr.va_fsid;
3662 if (vap->va_mask & VATTR_RDEV)
3663 vap->va_rdev = rp->r_attr.va_rdev;
3664 mutex_exit(&rp->r_statelock);
3665 return (0);
3670 * Only need to flush pages if asking for the mtime
3671 * and if there any dirty pages or any outstanding
3672 * asynchronous (write) requests for this file.
3674 if (vap->va_mask & VATTR_MTIME) {
3675 rp = VTOR4(vp);
3676 if (nfs4_has_pages(vp)) {
3677 mutex_enter(&rp->r_statev4_lock);
3678 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3679 mutex_exit(&rp->r_statev4_lock);
3680 if (rp->r_flags & R4DIRTY ||
3681 rp->r_awcount > 0) {
3682 mutex_enter(&rp->r_statelock);
3683 rp->r_gcount++;
3684 mutex_exit(&rp->r_statelock);
3685 error =
3686 nfs4_putpage(vp, 0,
3687 0, 0, cr, NULL);
3688 mutex_enter(&rp->r_statelock);
3689 if (error && (error == ENOSPC ||
3690 error == EDQUOT)) {
3691 if (!rp->r_error)
3692 rp->r_error = error;
3694 if (--rp->r_gcount == 0)
3695 cv_broadcast(&rp->r_cv);
3696 mutex_exit(&rp->r_statelock);
3698 } else {
3699 mutex_exit(&rp->r_statev4_lock);
3703 return (nfs4getattr(vp, vap, cr));
3707 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3710 * If these are the only two bits cleared
3711 * on the server then return 0 (OK) else
3712 * return 1 (BAD).
3714 on_client &= ~(S_ISUID|S_ISGID);
3715 if (on_client == from_server)
3716 return (0);
3717 else
3718 return (1);
3721 /*ARGSUSED4*/
3722 static int
3723 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3724 caller_context_t *ct)
3726 int error;
3728 if (vap->va_mask & VATTR_NOSET)
3729 return (EINVAL);
3731 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3732 return (EIO);
3735 * Don't call secpolicy_vnode_setattr, the client cannot
3736 * use its cached attributes to make security decisions
3737 * as the server may be faking mode bits or mapping uid/gid.
3738 * Always just let the server to the checking.
3739 * If we provide the ability to remove basic priviledges
3740 * to setattr (e.g. basic without chmod) then we will
3741 * need to add a check here before calling the server.
3743 error = nfs4setattr(vp, vap, flags, cr, NULL);
3745 if (error == 0 && (vap->va_mask & VATTR_SIZE) && vap->va_size == 0)
3746 vnevent_truncate(vp, ct);
3748 return (error);
3752 * To replace the "guarded" version 3 setattr, we use two types of compound
3753 * setattr requests:
3754 * 1. The "normal" setattr, used when the size of the file isn't being
3755 * changed - { Putfh <fh>; Setattr; Getattr }/
3756 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3757 * with only ctime as the argument. If the server ctime differs from
3758 * what is cached on the client, the verify will fail, but we would
3759 * already have the ctime from the preceding getattr, so just set it
3760 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3761 * Setattr; Getattr }.
3763 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3764 * this setattr and NULL if they are not.
3766 static int
3767 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3768 vsecattr_t *vsap)
3770 COMPOUND4args_clnt args;
3771 COMPOUND4res_clnt res, *resp = NULL;
3772 nfs4_ga_res_t *garp = NULL;
3773 int numops = 3; /* { Putfh; Setattr; Getattr } */
3774 nfs_argop4 argop[5];
3775 int verify_argop = -1;
3776 int setattr_argop = 1;
3777 nfs_resop4 *resop;
3778 vattr_t va;
3779 rnode4_t *rp;
3780 int doqueue = 1;
3781 uint_t mask = vap->va_mask;
3782 mode_t omode;
3783 vsecattr_t *vsp;
3784 timestruc_t ctime;
3785 bool_t needrecov = FALSE;
3786 nfs4_recov_state_t recov_state;
3787 nfs4_stateid_types_t sid_types;
3788 stateid4 stateid;
3789 hrtime_t t;
3790 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3791 servinfo4_t *svp;
3792 bitmap4 supp_attrs;
3794 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3795 rp = VTOR4(vp);
3796 nfs4_init_stateid_types(&sid_types);
3799 * Only need to flush pages if there are any pages and
3800 * if the file is marked as dirty in some fashion. The
3801 * file must be flushed so that we can accurately
3802 * determine the size of the file and the cached data
3803 * after the SETATTR returns. A file is considered to
3804 * be dirty if it is either marked with R4DIRTY, has
3805 * outstanding i/o's active, or is mmap'd. In this
3806 * last case, we can't tell whether there are dirty
3807 * pages, so we flush just to be sure.
3809 if (nfs4_has_pages(vp) &&
3810 ((rp->r_flags & R4DIRTY) ||
3811 rp->r_count > 0 ||
3812 rp->r_mapcnt > 0)) {
3813 ASSERT(vp->v_type != VCHR);
3814 e.error = nfs4_putpage(vp, 0, 0, 0, cr, NULL);
3815 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3816 mutex_enter(&rp->r_statelock);
3817 if (!rp->r_error)
3818 rp->r_error = e.error;
3819 mutex_exit(&rp->r_statelock);
3823 if (mask & VATTR_SIZE) {
3825 * Verification setattr compound for non-deleg VATTR_SIZE:
3826 * { Putfh; Getattr; Verify; Setattr; Getattr }
3827 * Set ctime local here (outside the do_again label)
3828 * so that subsequent retries (after failed VERIFY)
3829 * will use ctime from GETATTR results (from failed
3830 * verify compound) as VERIFY arg.
3831 * If file has delegation, then VERIFY(time_metadata)
3832 * is of little added value, so don't bother.
3834 mutex_enter(&rp->r_statev4_lock);
3835 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3836 rp->r_deleg_return_pending) {
3837 numops = 5;
3838 ctime = rp->r_attr.va_ctime;
3840 mutex_exit(&rp->r_statev4_lock);
3843 recov_state.rs_flags = 0;
3844 recov_state.rs_num_retry_despite_err = 0;
3846 args.ctag = TAG_SETATTR;
3847 do_again:
3848 recov_retry:
3849 setattr_argop = numops - 2;
3851 args.array = argop;
3852 args.array_len = numops;
3854 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3855 if (e.error)
3856 return (e.error);
3859 /* putfh target fh */
3860 argop[0].argop = OP_CPUTFH;
3861 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3863 if (numops == 5) {
3865 * We only care about the ctime, but need to get mtime
3866 * and size for proper cache update.
3868 /* getattr */
3869 argop[1].argop = OP_GETATTR;
3870 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3871 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3873 /* verify - set later in loop */
3874 verify_argop = 2;
3877 /* setattr */
3878 svp = rp->r_server;
3879 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3880 supp_attrs = svp->sv_supp_attrs;
3881 nfs_rw_exit(&svp->sv_lock);
3883 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3884 supp_attrs, &e.error, &sid_types);
3885 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3886 if (e.error) {
3887 /* req time field(s) overflow - return immediately */
3888 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3889 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3890 opsetattr.obj_attributes);
3891 return (e.error);
3893 omode = rp->r_attr.va_mode;
3895 /* getattr */
3896 argop[numops-1].argop = OP_GETATTR;
3897 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3899 * If we are setting the ACL (indicated only by vsap != NULL), request
3900 * the ACL in this getattr. The ACL returned from this getattr will be
3901 * used in updating the ACL cache.
3903 if (vsap != NULL)
3904 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3905 FATTR4_ACL_MASK;
3906 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3909 * setattr iterates if the object size is set and the cached ctime
3910 * does not match the file ctime. In that case, verify the ctime first.
3913 do {
3914 if (verify_argop != -1) {
3916 * Verify that the ctime match before doing setattr.
3918 va.va_mask = VATTR_CTIME;
3919 va.va_ctime = ctime;
3920 svp = rp->r_server;
3921 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3922 supp_attrs = svp->sv_supp_attrs;
3923 nfs_rw_exit(&svp->sv_lock);
3924 e.error = nfs4args_verify(&argop[verify_argop], &va,
3925 OP_VERIFY, supp_attrs);
3926 if (e.error) {
3927 /* req time field(s) overflow - return */
3928 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3929 needrecov);
3930 break;
3934 doqueue = 1;
3936 t = gethrtime();
3938 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3941 * Purge the access cache and ACL cache if changing either the
3942 * owner of the file, the group owner, or the mode. These may
3943 * change the access permissions of the file, so purge old
3944 * information and start over again.
3946 if (mask & (VATTR_UID | VATTR_GID | VATTR_MODE)) {
3947 (void) nfs4_access_purge_rp(rp);
3948 if (rp->r_secattr != NULL) {
3949 mutex_enter(&rp->r_statelock);
3950 vsp = rp->r_secattr;
3951 rp->r_secattr = NULL;
3952 mutex_exit(&rp->r_statelock);
3953 if (vsp != NULL)
3954 nfs4_acl_free_cache(vsp);
3959 * If res.array_len == numops, then everything succeeded,
3960 * except for possibly the final getattr. If only the
3961 * last getattr failed, give up, and don't try recovery.
3963 if (res.array_len == numops) {
3964 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3965 needrecov);
3966 if (! e.error)
3967 resp = &res;
3968 break;
3972 * if either rpc call failed or completely succeeded - done
3974 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3975 if (e.error) {
3976 PURGE_ATTRCACHE4(vp);
3977 if (!needrecov) {
3978 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3979 needrecov);
3980 break;
3985 * Do proper retry for OLD_STATEID outside of the normal
3986 * recovery framework.
3988 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3989 sid_types.cur_sid_type != SPEC_SID &&
3990 sid_types.cur_sid_type != NO_SID) {
3991 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3992 needrecov);
3993 nfs4_save_stateid(&stateid, &sid_types);
3994 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3995 opsetattr.obj_attributes);
3996 if (verify_argop != -1) {
3997 nfs4args_verify_free(&argop[verify_argop]);
3998 verify_argop = -1;
4000 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4001 goto recov_retry;
4004 if (needrecov) {
4005 bool_t abort;
4007 abort = nfs4_start_recovery(&e,
4008 VTOMI4(vp), vp, NULL, NULL, NULL,
4009 OP_SETATTR, NULL, NULL, NULL);
4010 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4011 needrecov);
4013 * Do not retry if we failed with OLD_STATEID using
4014 * a special stateid. This is done to avoid looping
4015 * with a broken server.
4017 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4018 (sid_types.cur_sid_type == SPEC_SID ||
4019 sid_types.cur_sid_type == NO_SID))
4020 abort = TRUE;
4021 if (!e.error) {
4022 if (res.status == NFS4ERR_BADOWNER)
4023 nfs4_log_badowner(VTOMI4(vp),
4024 OP_SETATTR);
4026 e.error = geterrno4(res.status);
4027 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4029 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4030 opsetattr.obj_attributes);
4031 if (verify_argop != -1) {
4032 nfs4args_verify_free(&argop[verify_argop]);
4033 verify_argop = -1;
4035 if (abort == FALSE) {
4037 * Need to retry all possible stateids in
4038 * case the recovery error wasn't stateid
4039 * related or the stateids have become
4040 * stale (server reboot).
4042 nfs4_init_stateid_types(&sid_types);
4043 goto recov_retry;
4045 return (e.error);
4049 * Need to call nfs4_end_op before nfs4getattr to
4050 * avoid potential nfs4_start_op deadlock. See RFE
4051 * 4777612. Calls to nfs4_invalidate_pages() and
4052 * nfs4_purge_stale_fh() might also generate over the
4053 * wire calls which my cause nfs4_start_op() deadlock.
4055 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4058 * Check to update lease.
4060 resp = &res;
4061 if (res.status == NFS4_OK) {
4062 break;
4066 * Check if verify failed to see if try again
4068 if ((verify_argop == -1) || (res.array_len != 3)) {
4070 * can't continue...
4072 if (res.status == NFS4ERR_BADOWNER)
4073 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4075 e.error = geterrno4(res.status);
4076 } else {
4078 * When the verify request fails, the client ctime is
4079 * not in sync with the server. This is the same as
4080 * the version 3 "not synchronized" error, and we
4081 * handle it in a similar manner (XXX do we need to???).
4082 * Use the ctime returned in the first getattr for
4083 * the input to the next verify.
4084 * If we couldn't get the attributes, then we give up
4085 * because we can't complete the operation as required.
4087 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4089 if (e.error) {
4090 PURGE_ATTRCACHE4(vp);
4091 nfs4_purge_stale_fh(e.error, vp, cr);
4092 } else {
4094 * retry with a new verify value
4096 ctime = garp->n4g_va.va_ctime;
4097 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4098 resp = NULL;
4100 if (!e.error) {
4101 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4102 opsetattr.obj_attributes);
4103 if (verify_argop != -1) {
4104 nfs4args_verify_free(&argop[verify_argop]);
4105 verify_argop = -1;
4107 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4108 goto do_again;
4110 } while (!e.error);
4112 if (e.error) {
4114 * If we are here, rfs4call has an irrecoverable error - return
4116 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4117 opsetattr.obj_attributes);
4118 if (verify_argop != -1) {
4119 nfs4args_verify_free(&argop[verify_argop]);
4120 verify_argop = -1;
4122 if (resp)
4123 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4124 return (e.error);
4130 * If changing the size of the file, invalidate
4131 * any local cached data which is no longer part
4132 * of the file. We also possibly invalidate the
4133 * last page in the file. We could use
4134 * pvn_vpzero(), but this would mark the page as
4135 * modified and require it to be written back to
4136 * the server for no particularly good reason.
4137 * This way, if we access it, then we bring it
4138 * back in. A read should be cheaper than a
4139 * write.
4141 if (mask & VATTR_SIZE) {
4142 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4145 /* either no error or one of the postop getattr failed */
4148 * XXX Perform a simplified version of wcc checking. Instead of
4149 * have another getattr to get pre-op, just purge cache if
4150 * any of the ops prior to and including the getattr failed.
4151 * If the getattr succeeded then update the attrcache accordingly.
4154 garp = NULL;
4155 if (res.status == NFS4_OK) {
4157 * Last getattr
4159 resop = &res.array[numops - 1];
4160 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4163 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4164 * rather than filling it. See the function itself for details.
4166 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4167 if (garp != NULL) {
4168 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4169 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4170 vs_ace4_destroy(&garp->n4g_vsa);
4171 } else {
4172 if (vsap != NULL) {
4174 * The ACL was supposed to be set and to be
4175 * returned in the last getattr of this
4176 * compound, but for some reason the getattr
4177 * result doesn't contain the ACL. In this
4178 * case, purge the ACL cache.
4180 if (rp->r_secattr != NULL) {
4181 mutex_enter(&rp->r_statelock);
4182 vsp = rp->r_secattr;
4183 rp->r_secattr = NULL;
4184 mutex_exit(&rp->r_statelock);
4185 if (vsp != NULL)
4186 nfs4_acl_free_cache(vsp);
4192 if (res.status == NFS4_OK && (mask & VATTR_SIZE)) {
4194 * Set the size, rather than relying on getting it updated
4195 * via a GETATTR. With delegations the client tries to
4196 * suppress GETATTR calls.
4198 mutex_enter(&rp->r_statelock);
4199 rp->r_size = vap->va_size;
4200 mutex_exit(&rp->r_statelock);
4204 * Can free up request args and res
4206 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4207 opsetattr.obj_attributes);
4208 if (verify_argop != -1) {
4209 nfs4args_verify_free(&argop[verify_argop]);
4210 verify_argop = -1;
4212 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4215 * Some servers will change the mode to clear the setuid
4216 * and setgid bits when changing the uid or gid. The
4217 * client needs to compensate appropriately.
4219 if (mask & (VATTR_UID | VATTR_GID)) {
4220 int terror, do_setattr;
4222 do_setattr = 0;
4223 va.va_mask = VATTR_MODE;
4224 terror = nfs4getattr(vp, &va, cr);
4225 if (!terror &&
4226 (((mask & VATTR_MODE) && va.va_mode != vap->va_mode) ||
4227 (!(mask & VATTR_MODE) && va.va_mode != omode))) {
4228 va.va_mask = VATTR_MODE;
4229 if (mask & VATTR_MODE) {
4231 * We asked the mode to be changed and what
4232 * we just got from the server in getattr is
4233 * not what we wanted it to be, so set it now.
4235 va.va_mode = vap->va_mode;
4236 do_setattr = 1;
4237 } else {
4239 * We did not ask the mode to be changed,
4240 * Check to see that the server just cleared
4241 * I_SUID and I_GUID from it. If not then
4242 * set mode to omode with UID/GID cleared.
4244 if (nfs4_compare_modes(va.va_mode, omode)) {
4245 omode &= ~(S_ISUID|S_ISGID);
4246 va.va_mode = omode;
4247 do_setattr = 1;
4251 if (do_setattr)
4252 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4256 return (e.error);
4259 /* ARGSUSED */
4260 static int
4261 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4263 COMPOUND4args_clnt args;
4264 COMPOUND4res_clnt res;
4265 int doqueue;
4266 uint32_t acc, resacc, argacc;
4267 rnode4_t *rp;
4268 cred_t *cred, *ncr, *ncrfree = NULL;
4269 nfs4_access_type_t cacc;
4270 int num_ops;
4271 nfs_argop4 argop[3];
4272 nfs_resop4 *resop;
4273 bool_t needrecov = FALSE, do_getattr;
4274 nfs4_recov_state_t recov_state;
4275 int rpc_error;
4276 hrtime_t t;
4277 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4278 mntinfo4_t *mi = VTOMI4(vp);
4280 if (nfs_zone() != mi->mi_zone)
4281 return (EIO);
4283 acc = 0;
4284 if (mode & VREAD)
4285 acc |= ACCESS4_READ;
4286 if (mode & VWRITE) {
4287 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4288 return (EROFS);
4289 if (vp->v_type == VDIR)
4290 acc |= ACCESS4_DELETE;
4291 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4293 if (mode & VEXEC) {
4294 if (vp->v_type == VDIR)
4295 acc |= ACCESS4_LOOKUP;
4296 else
4297 acc |= ACCESS4_EXECUTE;
4300 if (VTOR4(vp)->r_acache != NULL) {
4301 e.error = nfs4_validate_caches(vp, cr);
4302 if (e.error)
4303 return (e.error);
4306 rp = VTOR4(vp);
4307 if (vp->v_type == VDIR)
4308 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4309 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4310 else
4311 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4312 ACCESS4_EXECUTE;
4313 recov_state.rs_flags = 0;
4314 recov_state.rs_num_retry_despite_err = 0;
4316 cred = cr;
4318 * ncr and ncrfree both initially
4319 * point to the memory area returned
4320 * by crnetadjust();
4321 * ncrfree not NULL when exiting means
4322 * that we need to release it
4324 ncr = crnetadjust(cred);
4325 ncrfree = ncr;
4327 tryagain:
4328 cacc = nfs4_access_check(rp, acc, cred);
4329 if (cacc == NFS4_ACCESS_ALLOWED) {
4330 if (ncrfree != NULL)
4331 crfree(ncrfree);
4332 return (0);
4334 if (cacc == NFS4_ACCESS_DENIED) {
4336 * If the cred can be adjusted, try again
4337 * with the new cred.
4339 if (ncr != NULL) {
4340 cred = ncr;
4341 ncr = NULL;
4342 goto tryagain;
4344 if (ncrfree != NULL)
4345 crfree(ncrfree);
4346 return (EACCES);
4349 recov_retry:
4351 * Don't take with r_statev4_lock here. r_deleg_type could
4352 * change as soon as lock is released. Since it is an int,
4353 * there is no atomicity issue.
4355 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4356 num_ops = do_getattr ? 3 : 2;
4358 args.ctag = TAG_ACCESS;
4360 args.array_len = num_ops;
4361 args.array = argop;
4363 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4364 &recov_state, NULL)) {
4365 if (ncrfree != NULL)
4366 crfree(ncrfree);
4367 return (e.error);
4370 /* putfh target fh */
4371 argop[0].argop = OP_CPUTFH;
4372 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4374 /* access */
4375 argop[1].argop = OP_ACCESS;
4376 argop[1].nfs_argop4_u.opaccess.access = argacc;
4378 /* getattr */
4379 if (do_getattr) {
4380 argop[2].argop = OP_GETATTR;
4381 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4382 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4385 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4386 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4387 rnode4info(VTOR4(vp))));
4389 doqueue = 1;
4390 t = gethrtime();
4391 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4392 rpc_error = e.error;
4394 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4395 if (needrecov) {
4396 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4397 "nfs4_access: initiating recovery\n"));
4399 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4400 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4401 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4402 &recov_state, needrecov);
4403 if (!e.error)
4404 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4405 goto recov_retry;
4408 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4410 if (e.error)
4411 goto out;
4413 if (res.status) {
4414 e.error = geterrno4(res.status);
4416 * This might generate over the wire calls throught
4417 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4418 * here to avoid a deadlock.
4420 nfs4_purge_stale_fh(e.error, vp, cr);
4421 goto out;
4423 resop = &res.array[1]; /* access res */
4425 resacc = resop->nfs_resop4_u.opaccess.access;
4427 if (do_getattr) {
4428 resop++; /* getattr res */
4429 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4430 t, cr, FALSE, NULL);
4433 if (!e.error) {
4434 nfs4_access_cache(rp, argacc, resacc, cred);
4436 * we just cached results with cred; if cred is the
4437 * adjusted credentials from crnetadjust, we do not want
4438 * to release them before exiting: hence setting ncrfree
4439 * to NULL
4441 if (cred != cr)
4442 ncrfree = NULL;
4443 /* XXX check the supported bits too? */
4444 if ((acc & resacc) != acc) {
4446 * The following code implements the semantic
4447 * that a setuid root program has *at least* the
4448 * permissions of the user that is running the
4449 * program. See rfs3call() for more portions
4450 * of the implementation of this functionality.
4452 /* XXX-LP */
4453 if (ncr != NULL) {
4454 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4455 cred = ncr;
4456 ncr = NULL;
4457 goto tryagain;
4459 e.error = EACCES;
4463 out:
4464 if (!rpc_error)
4465 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4467 if (ncrfree != NULL)
4468 crfree(ncrfree);
4470 return (e.error);
4473 /* ARGSUSED */
4474 static int
4475 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4477 COMPOUND4args_clnt args;
4478 COMPOUND4res_clnt res;
4479 int doqueue;
4480 rnode4_t *rp;
4481 nfs_argop4 argop[3];
4482 nfs_resop4 *resop;
4483 READLINK4res *lr_res;
4484 nfs4_ga_res_t *garp;
4485 uint_t len;
4486 char *linkdata;
4487 bool_t needrecov = FALSE;
4488 nfs4_recov_state_t recov_state;
4489 hrtime_t t;
4490 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4492 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4493 return (EIO);
4495 * Can't readlink anything other than a symbolic link.
4497 if (vp->v_type != VLNK)
4498 return (EINVAL);
4500 rp = VTOR4(vp);
4501 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4502 e.error = nfs4_validate_caches(vp, cr);
4503 if (e.error)
4504 return (e.error);
4505 mutex_enter(&rp->r_statelock);
4506 if (rp->r_symlink.contents != NULL) {
4507 e.error = uiomove(rp->r_symlink.contents,
4508 rp->r_symlink.len, UIO_READ, uiop);
4509 mutex_exit(&rp->r_statelock);
4510 return (e.error);
4512 mutex_exit(&rp->r_statelock);
4514 recov_state.rs_flags = 0;
4515 recov_state.rs_num_retry_despite_err = 0;
4517 recov_retry:
4518 args.array_len = 3;
4519 args.array = argop;
4520 args.ctag = TAG_READLINK;
4522 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4523 if (e.error) {
4524 return (e.error);
4527 /* 0. putfh symlink fh */
4528 argop[0].argop = OP_CPUTFH;
4529 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4531 /* 1. readlink */
4532 argop[1].argop = OP_READLINK;
4534 /* 2. getattr */
4535 argop[2].argop = OP_GETATTR;
4536 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4537 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4539 doqueue = 1;
4541 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4542 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4543 rnode4info(VTOR4(vp))));
4545 t = gethrtime();
4547 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4549 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4550 if (needrecov) {
4551 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4552 "nfs4_readlink: initiating recovery\n"));
4554 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4555 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4556 if (!e.error)
4557 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4559 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4560 needrecov);
4561 goto recov_retry;
4565 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4567 if (e.error)
4568 return (e.error);
4571 * There is an path in the code below which calls
4572 * nfs4_purge_stale_fh(), which may generate otw calls through
4573 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4574 * here to avoid nfs4_start_op() deadlock.
4577 if (res.status && (res.array_len < args.array_len)) {
4579 * either Putfh or Link failed
4581 e.error = geterrno4(res.status);
4582 nfs4_purge_stale_fh(e.error, vp, cr);
4583 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4584 return (e.error);
4587 resop = &res.array[1]; /* readlink res */
4588 lr_res = &resop->nfs_resop4_u.opreadlink;
4591 * treat symlink names as data
4593 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4594 if (linkdata != NULL) {
4595 int uio_len = len - 1;
4596 /* len includes null byte, which we won't uiomove */
4597 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4598 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4599 mutex_enter(&rp->r_statelock);
4600 if (rp->r_symlink.contents == NULL) {
4601 rp->r_symlink.contents = linkdata;
4602 rp->r_symlink.len = uio_len;
4603 rp->r_symlink.size = len;
4604 mutex_exit(&rp->r_statelock);
4605 } else {
4606 mutex_exit(&rp->r_statelock);
4607 kmem_free(linkdata, len);
4609 } else {
4610 kmem_free(linkdata, len);
4613 if (res.status == NFS4_OK) {
4614 resop++; /* getattr res */
4615 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4617 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4619 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4622 * The over the wire error for attempting to readlink something
4623 * other than a symbolic link is ENXIO. However, we need to
4624 * return EINVAL instead of ENXIO, so we map it here.
4626 return (e.error == ENXIO ? EINVAL : e.error);
4630 * Flush local dirty pages to stable storage on the server.
4632 * If FNODSYNC is specified, then there is nothing to do because
4633 * metadata changes are not cached on the client before being
4634 * sent to the server.
4636 /* ARGSUSED */
4637 static int
4638 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4640 int error;
4642 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4643 return (0);
4644 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4645 return (EIO);
4646 error = nfs4_putpage_commit(vp, 0, 0, cr);
4647 if (!error)
4648 error = VTOR4(vp)->r_error;
4649 return (error);
4653 * Weirdness: if the file was removed or the target of a rename
4654 * operation while it was open, it got renamed instead. Here we
4655 * remove the renamed file.
4657 /* ARGSUSED */
4658 void
4659 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4661 rnode4_t *rp;
4663 ASSERT(vp != DNLC_NO_VNODE);
4665 rp = VTOR4(vp);
4667 if (IS_SHADOW(vp, rp)) {
4668 sv_inactive(vp);
4669 return;
4673 * If this is coming from the wrong zone, we let someone in the right
4674 * zone take care of it asynchronously. We can get here due to
4675 * VN_RELE() being called from pageout() or fsflush(). This call may
4676 * potentially turn into an expensive no-op if, for instance, v_count
4677 * gets incremented in the meantime, but it's still correct.
4679 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4680 nfs4_async_inactive(vp, cr);
4681 return;
4685 * Some of the cleanup steps might require over-the-wire
4686 * operations. Since fop_inactive can get called as a result of
4687 * other over-the-wire operations (e.g., an attribute cache update
4688 * can lead to a DNLC purge), doing those steps now would lead to a
4689 * nested call to the recovery framework, which can deadlock. So
4690 * do any over-the-wire cleanups asynchronously, in a separate
4691 * thread.
4694 mutex_enter(&rp->r_os_lock);
4695 mutex_enter(&rp->r_statelock);
4696 mutex_enter(&rp->r_statev4_lock);
4698 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4699 mutex_exit(&rp->r_statev4_lock);
4700 mutex_exit(&rp->r_statelock);
4701 mutex_exit(&rp->r_os_lock);
4702 nfs4_async_inactive(vp, cr);
4703 return;
4706 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4707 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4708 mutex_exit(&rp->r_statev4_lock);
4709 mutex_exit(&rp->r_statelock);
4710 mutex_exit(&rp->r_os_lock);
4711 nfs4_async_inactive(vp, cr);
4712 return;
4715 if (rp->r_unldvp != NULL) {
4716 mutex_exit(&rp->r_statev4_lock);
4717 mutex_exit(&rp->r_statelock);
4718 mutex_exit(&rp->r_os_lock);
4719 nfs4_async_inactive(vp, cr);
4720 return;
4722 mutex_exit(&rp->r_statev4_lock);
4723 mutex_exit(&rp->r_statelock);
4724 mutex_exit(&rp->r_os_lock);
4726 rp4_addfree(rp, cr);
4730 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4731 * various bits of state. The caller must not refer to vp after this call.
4734 void
4735 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4737 rnode4_t *rp = VTOR4(vp);
4738 nfs4_recov_state_t recov_state;
4739 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4740 vnode_t *unldvp;
4741 char *unlname;
4742 cred_t *unlcred;
4743 COMPOUND4args_clnt args;
4744 COMPOUND4res_clnt res, *resp;
4745 nfs_argop4 argop[2];
4746 int doqueue;
4747 #ifdef DEBUG
4748 char *name;
4749 #endif
4751 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4752 ASSERT(!IS_SHADOW(vp, rp));
4754 #ifdef DEBUG
4755 name = fn_name(VTOSV(vp)->sv_name);
4756 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4757 "release vnode %s", name));
4758 kmem_free(name, MAXNAMELEN);
4759 #endif
4761 if (vp->v_type == VREG) {
4762 bool_t recov_failed = FALSE;
4764 e.error = nfs4close_all(vp, cr);
4765 if (e.error) {
4766 /* Check to see if recovery failed */
4767 mutex_enter(&(VTOMI4(vp)->mi_lock));
4768 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4769 recov_failed = TRUE;
4770 mutex_exit(&(VTOMI4(vp)->mi_lock));
4771 if (!recov_failed) {
4772 mutex_enter(&rp->r_statelock);
4773 if (rp->r_flags & R4RECOVERR)
4774 recov_failed = TRUE;
4775 mutex_exit(&rp->r_statelock);
4777 if (recov_failed) {
4778 NFS4_DEBUG(nfs4_client_recov_debug,
4779 (CE_NOTE, "nfs4_inactive_otw: "
4780 "close failed (recovery failure)"));
4785 redo:
4786 if (rp->r_unldvp == NULL) {
4787 rp4_addfree(rp, cr);
4788 return;
4792 * Save the vnode pointer for the directory where the
4793 * unlinked-open file got renamed, then set it to NULL
4794 * to prevent another thread from getting here before
4795 * we're done with the remove. While we have the
4796 * statelock, make local copies of the pertinent rnode
4797 * fields. If we weren't to do this in an atomic way, the
4798 * the unl* fields could become inconsistent with respect
4799 * to each other due to a race condition between this
4800 * code and nfs_remove(). See bug report 1034328.
4802 mutex_enter(&rp->r_statelock);
4803 if (rp->r_unldvp == NULL) {
4804 mutex_exit(&rp->r_statelock);
4805 rp4_addfree(rp, cr);
4806 return;
4809 unldvp = rp->r_unldvp;
4810 rp->r_unldvp = NULL;
4811 unlname = rp->r_unlname;
4812 rp->r_unlname = NULL;
4813 unlcred = rp->r_unlcred;
4814 rp->r_unlcred = NULL;
4815 mutex_exit(&rp->r_statelock);
4818 * If there are any dirty pages left, then flush
4819 * them. This is unfortunate because they just
4820 * may get thrown away during the remove operation,
4821 * but we have to do this for correctness.
4823 if (nfs4_has_pages(vp) &&
4824 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4825 ASSERT(vp->v_type != VCHR);
4826 e.error = nfs4_putpage(vp, 0, 0, 0, cr, NULL);
4827 if (e.error) {
4828 mutex_enter(&rp->r_statelock);
4829 if (!rp->r_error)
4830 rp->r_error = e.error;
4831 mutex_exit(&rp->r_statelock);
4835 recov_state.rs_flags = 0;
4836 recov_state.rs_num_retry_despite_err = 0;
4837 recov_retry_remove:
4839 * Do the remove operation on the renamed file
4841 args.ctag = TAG_INACTIVE;
4844 * Remove ops: putfh dir; remove
4846 args.array_len = 2;
4847 args.array = argop;
4849 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4850 if (e.error) {
4851 kmem_free(unlname, MAXNAMELEN);
4852 crfree(unlcred);
4853 VN_RELE(unldvp);
4855 * Try again; this time around r_unldvp will be NULL, so we'll
4856 * just call rp4_addfree() and return.
4858 goto redo;
4861 /* putfh directory */
4862 argop[0].argop = OP_CPUTFH;
4863 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4865 /* remove */
4866 argop[1].argop = OP_CREMOVE;
4867 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4869 doqueue = 1;
4870 resp = &res;
4872 #if 0 /* notyet */
4874 * Can't do this yet. We may be being called from
4875 * dnlc_purge_XXX while that routine is holding a
4876 * mutex lock to the nc_rele list. The calls to
4877 * nfs3_cache_wcc_data may result in calls to
4878 * dnlc_purge_XXX. This will result in a deadlock.
4880 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4881 if (e.error) {
4882 PURGE_ATTRCACHE4(unldvp);
4883 resp = NULL;
4884 } else if (res.status) {
4885 e.error = geterrno4(res.status);
4886 PURGE_ATTRCACHE4(unldvp);
4888 * This code is inactive right now
4889 * but if made active there should
4890 * be a nfs4_end_op() call before
4891 * nfs4_purge_stale_fh to avoid start_op()
4892 * deadlock. See BugId: 4948726
4894 nfs4_purge_stale_fh(error, unldvp, cr);
4895 } else {
4896 nfs_resop4 *resop;
4897 REMOVE4res *rm_res;
4899 resop = &res.array[1];
4900 rm_res = &resop->nfs_resop4_u.opremove;
4902 * Update directory cache attribute,
4903 * readdir and dnlc caches.
4905 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4907 #else
4908 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4910 PURGE_ATTRCACHE4(unldvp);
4911 #endif
4913 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4914 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4915 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4916 if (!e.error)
4917 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4918 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4919 &recov_state, TRUE);
4920 goto recov_retry_remove;
4923 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4926 * Release stuff held for the remove
4928 VN_RELE(unldvp);
4929 if (!e.error && resp)
4930 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4932 kmem_free(unlname, MAXNAMELEN);
4933 crfree(unlcred);
4934 goto redo;
4938 * Remote file system operations having to do with directory manipulation.
4940 /* ARGSUSED3 */
4942 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4943 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4944 int *direntflags, pathname_t *realpnp)
4946 int error;
4947 vnode_t *vp, *avp = NULL;
4948 rnode4_t *drp;
4950 *vpp = NULL;
4951 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4952 return (EPERM);
4954 * if LOOKUP_XATTR, must replace dvp (object) with
4955 * object's attrdir before continuing with lookup
4957 if (flags & LOOKUP_XATTR) {
4958 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4959 if (error)
4960 return (error);
4962 dvp = avp;
4965 * If lookup is for "", just return dvp now. The attrdir
4966 * has already been activated (from nfs4lookup_xattr), and
4967 * the caller will RELE the original dvp -- not
4968 * the attrdir. So, set vpp and return.
4969 * Currently, when the LOOKUP_XATTR flag is
4970 * passed to fop_lookup, the name is always empty, and
4971 * shortcircuiting here avoids 3 unneeded lock/unlock
4972 * pairs.
4974 * If a non-empty name was provided, then it is the
4975 * attribute name, and it will be looked up below.
4977 if (*nm == '\0') {
4978 *vpp = dvp;
4979 return (0);
4983 * The vfs layer never sends a name when asking for the
4984 * attrdir, so we should never get here (unless of course
4985 * name is passed at some time in future -- at which time
4986 * we'll blow up here).
4988 ASSERT(0);
4991 drp = VTOR4(dvp);
4992 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4993 return (EINTR);
4995 error = nfs4lookup(dvp, nm, vpp, cr, 0);
4996 nfs_rw_exit(&drp->r_rwlock);
4999 * If vnode is a device, create special vnode.
5001 if (!error && ISVDEV((*vpp)->v_type)) {
5002 vp = *vpp;
5003 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5004 VN_RELE(vp);
5007 return (error);
5010 /* ARGSUSED */
5011 static int
5012 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5014 int error;
5015 rnode4_t *drp;
5016 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5017 mntinfo4_t *mi;
5019 mi = VTOMI4(dvp);
5020 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5021 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5022 return (EINVAL);
5024 drp = VTOR4(dvp);
5025 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5026 return (EINTR);
5028 mutex_enter(&drp->r_statelock);
5030 * If the server doesn't support xattrs just return EINVAL
5032 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5033 mutex_exit(&drp->r_statelock);
5034 nfs_rw_exit(&drp->r_rwlock);
5035 return (EINVAL);
5039 * If there is a cached xattr directory entry,
5040 * use it as long as the attributes are valid. If the
5041 * attributes are not valid, take the simple approach and
5042 * free the cached value and re-fetch a new value.
5044 * We don't negative entry cache for now, if we did we
5045 * would need to check if the file has changed on every
5046 * lookup. But xattrs don't exist very often and failing
5047 * an openattr is not much more expensive than and NVERIFY or GETATTR
5048 * so do an openattr over the wire for now.
5050 if (drp->r_xattr_dir != NULL) {
5051 if (ATTRCACHE4_VALID(dvp)) {
5052 VN_HOLD(drp->r_xattr_dir);
5053 *vpp = drp->r_xattr_dir;
5054 mutex_exit(&drp->r_statelock);
5055 nfs_rw_exit(&drp->r_rwlock);
5056 return (0);
5058 VN_RELE(drp->r_xattr_dir);
5059 drp->r_xattr_dir = NULL;
5061 mutex_exit(&drp->r_statelock);
5063 error = nfs4openattr(dvp, vpp, cflag, cr);
5065 nfs_rw_exit(&drp->r_rwlock);
5067 return (error);
5070 static int
5071 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5073 int error;
5074 rnode4_t *drp;
5076 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5079 * If lookup is for "", just return dvp. Don't need
5080 * to send it over the wire, look it up in the dnlc,
5081 * or perform any access checks.
5083 if (*nm == '\0') {
5084 VN_HOLD(dvp);
5085 *vpp = dvp;
5086 return (0);
5090 * Can't do lookups in non-directories.
5092 if (dvp->v_type != VDIR)
5093 return (ENOTDIR);
5096 * If lookup is for ".", just return dvp. Don't need
5097 * to send it over the wire or look it up in the dnlc,
5098 * just need to check access.
5100 if (nm[0] == '.' && nm[1] == '\0') {
5101 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5102 if (error)
5103 return (error);
5104 VN_HOLD(dvp);
5105 *vpp = dvp;
5106 return (0);
5109 drp = VTOR4(dvp);
5110 if (!(drp->r_flags & R4LOOKUP)) {
5111 mutex_enter(&drp->r_statelock);
5112 drp->r_flags |= R4LOOKUP;
5113 mutex_exit(&drp->r_statelock);
5116 *vpp = NULL;
5118 * Lookup this name in the DNLC. If there is no entry
5119 * lookup over the wire.
5121 if (!skipdnlc)
5122 *vpp = dnlc_lookup(dvp, nm);
5123 if (*vpp == NULL) {
5125 * We need to go over the wire to lookup the name.
5127 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5131 * We hit on the dnlc
5133 if (*vpp != DNLC_NO_VNODE ||
5134 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5136 * But our attrs may not be valid.
5138 if (ATTRCACHE4_VALID(dvp)) {
5139 error = nfs4_waitfor_purge_complete(dvp);
5140 if (error) {
5141 VN_RELE(*vpp);
5142 *vpp = NULL;
5143 return (error);
5147 * If after the purge completes, check to make sure
5148 * our attrs are still valid.
5150 if (ATTRCACHE4_VALID(dvp)) {
5152 * If we waited for a purge we may have
5153 * lost our vnode so look it up again.
5155 VN_RELE(*vpp);
5156 *vpp = dnlc_lookup(dvp, nm);
5157 if (*vpp == NULL)
5158 return (nfs4lookupnew_otw(dvp,
5159 nm, vpp, cr));
5162 * The access cache should almost always hit
5164 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5166 if (error) {
5167 VN_RELE(*vpp);
5168 *vpp = NULL;
5169 return (error);
5171 if (*vpp == DNLC_NO_VNODE) {
5172 VN_RELE(*vpp);
5173 *vpp = NULL;
5174 return (ENOENT);
5176 return (0);
5181 ASSERT(*vpp != NULL);
5184 * We may have gotten here we have one of the following cases:
5185 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5186 * need to validate them.
5187 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5188 * must validate.
5190 * Go to the server and check if the directory has changed, if
5191 * it hasn't we are done and can use the dnlc entry.
5193 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5197 * Go to the server and check if the directory has changed, if
5198 * it hasn't we are done and can use the dnlc entry. If it
5199 * has changed we get a new copy of its attributes and check
5200 * the access for VEXEC, then relookup the filename and
5201 * get its filehandle and attributes.
5203 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5204 * if the NVERIFY failed we must
5205 * purge the caches
5206 * cache new attributes (will set r_time_attr_inval)
5207 * cache new access
5208 * recheck VEXEC access
5209 * add name to dnlc, possibly negative
5210 * if LOOKUP succeeded
5211 * cache new attributes
5212 * else
5213 * set a new r_time_attr_inval for dvp
5214 * check to make sure we have access
5216 * The vpp returned is the vnode passed in if the directory is valid,
5217 * a new vnode if successful lookup, or NULL on error.
5219 static int
5220 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5222 COMPOUND4args_clnt args;
5223 COMPOUND4res_clnt res;
5224 fattr4 *ver_fattr;
5225 fattr4_change dchange;
5226 int32_t *ptr;
5227 int argoplist_size = 7 * sizeof (nfs_argop4);
5228 nfs_argop4 *argop;
5229 int doqueue;
5230 mntinfo4_t *mi;
5231 nfs4_recov_state_t recov_state;
5232 hrtime_t t;
5233 int isdotdot;
5234 vnode_t *nvp;
5235 nfs_fh4 *fhp;
5236 nfs4_sharedfh_t *sfhp;
5237 nfs4_access_type_t cacc;
5238 rnode4_t *nrp;
5239 rnode4_t *drp = VTOR4(dvp);
5240 nfs4_ga_res_t *garp = NULL;
5241 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5243 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5244 ASSERT(nm != NULL);
5245 ASSERT(nm[0] != '\0');
5246 ASSERT(dvp->v_type == VDIR);
5247 ASSERT(nm[0] != '.' || nm[1] != '\0');
5248 ASSERT(*vpp != NULL);
5250 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5251 isdotdot = 1;
5252 args.ctag = TAG_LOOKUP_VPARENT;
5253 } else {
5255 * If dvp were a stub, it should have triggered and caused
5256 * a mount for us to get this far.
5258 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5260 isdotdot = 0;
5261 args.ctag = TAG_LOOKUP_VALID;
5264 mi = VTOMI4(dvp);
5265 recov_state.rs_flags = 0;
5266 recov_state.rs_num_retry_despite_err = 0;
5268 nvp = NULL;
5270 /* Save the original mount point security information */
5271 (void) save_mnt_secinfo(mi->mi_curr_serv);
5273 recov_retry:
5274 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5275 &recov_state, NULL);
5276 if (e.error) {
5277 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5278 VN_RELE(*vpp);
5279 *vpp = NULL;
5280 return (e.error);
5283 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5285 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5286 args.array_len = 7;
5287 args.array = argop;
5289 /* 0. putfh file */
5290 argop[0].argop = OP_CPUTFH;
5291 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5293 /* 1. nverify the change info */
5294 argop[1].argop = OP_NVERIFY;
5295 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5296 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5297 ver_fattr->attrlist4 = (char *)&dchange;
5298 ptr = (int32_t *)&dchange;
5299 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5300 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5302 /* 2. getattr directory */
5303 argop[2].argop = OP_GETATTR;
5304 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5305 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5307 /* 3. access directory */
5308 argop[3].argop = OP_ACCESS;
5309 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5310 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5312 /* 4. lookup name */
5313 if (isdotdot) {
5314 argop[4].argop = OP_LOOKUPP;
5315 } else {
5316 argop[4].argop = OP_CLOOKUP;
5317 argop[4].nfs_argop4_u.opclookup.cname = nm;
5320 /* 5. resulting file handle */
5321 argop[5].argop = OP_GETFH;
5323 /* 6. resulting file attributes */
5324 argop[6].argop = OP_GETATTR;
5325 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5326 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5328 doqueue = 1;
5329 t = gethrtime();
5331 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5333 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5334 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5335 if (e.error != 0 && *vpp != NULL)
5336 VN_RELE(*vpp);
5337 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5338 &recov_state, FALSE);
5339 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5340 kmem_free(argop, argoplist_size);
5341 return (e.error);
5344 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5346 * For WRONGSEC of a non-dotdot case, send secinfo directly
5347 * from this thread, do not go thru the recovery thread since
5348 * we need the nm information.
5350 * Not doing dotdot case because there is no specification
5351 * for (PUTFH, SECINFO "..") yet.
5353 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5354 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5355 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5356 &recov_state, FALSE);
5357 else
5358 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5359 &recov_state, TRUE);
5360 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5361 kmem_free(argop, argoplist_size);
5362 if (!e.error)
5363 goto recov_retry;
5364 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5365 VN_RELE(*vpp);
5366 *vpp = NULL;
5367 return (e.error);
5370 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5371 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5372 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5373 &recov_state, TRUE);
5375 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5376 kmem_free(argop, argoplist_size);
5377 goto recov_retry;
5381 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5383 if (e.error || res.array_len == 0) {
5385 * If e.error isn't set, then reply has no ops (or we couldn't
5386 * be here). The only legal way to reply without an op array
5387 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5388 * be in the reply for all other status values.
5390 * For valid replies without an ops array, return ENOTSUP
5391 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5392 * return EIO -- don't trust status.
5394 if (e.error == 0)
5395 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5396 ENOTSUP : EIO;
5397 VN_RELE(*vpp);
5398 *vpp = NULL;
5399 kmem_free(argop, argoplist_size);
5400 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5401 return (e.error);
5404 if (res.status != NFS4ERR_SAME) {
5405 e.error = geterrno4(res.status);
5408 * The NVERIFY "failed" so the directory has changed
5409 * First make sure PUTFH succeeded and NVERIFY "failed"
5410 * cleanly.
5412 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5413 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5414 nfs4_purge_stale_fh(e.error, dvp, cr);
5415 VN_RELE(*vpp);
5416 *vpp = NULL;
5417 goto exit;
5421 * We know the NVERIFY "failed" so we must:
5422 * purge the caches (access and indirectly dnlc if needed)
5424 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5426 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5427 nfs4_purge_stale_fh(e.error, dvp, cr);
5428 VN_RELE(*vpp);
5429 *vpp = NULL;
5430 goto exit;
5434 * Install new cached attributes for the directory
5436 nfs4_attr_cache(dvp,
5437 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5438 t, cr, FALSE, NULL);
5440 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5441 nfs4_purge_stale_fh(e.error, dvp, cr);
5442 VN_RELE(*vpp);
5443 *vpp = NULL;
5444 e.error = geterrno4(res.status);
5445 goto exit;
5449 * Now we know the directory is valid,
5450 * cache new directory access
5452 nfs4_access_cache(drp,
5453 args.array[3].nfs_argop4_u.opaccess.access,
5454 res.array[3].nfs_resop4_u.opaccess.access, cr);
5457 * recheck VEXEC access
5459 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5460 if (cacc != NFS4_ACCESS_ALLOWED) {
5462 * Directory permissions might have been revoked
5464 if (cacc == NFS4_ACCESS_DENIED) {
5465 e.error = EACCES;
5466 VN_RELE(*vpp);
5467 *vpp = NULL;
5468 goto exit;
5472 * Somehow we must not have asked for enough
5473 * so try a singleton ACCESS, should never happen.
5475 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5476 if (e.error) {
5477 VN_RELE(*vpp);
5478 *vpp = NULL;
5479 goto exit;
5483 e.error = geterrno4(res.status);
5484 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5486 * The lookup failed, probably no entry
5488 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5489 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5490 } else {
5492 * Might be some other error, so remove
5493 * the dnlc entry to make sure we start all
5494 * over again, next time.
5496 dnlc_remove(dvp, nm);
5498 VN_RELE(*vpp);
5499 *vpp = NULL;
5500 goto exit;
5503 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5505 * The file exists but we can't get its fh for
5506 * some unknown reason. Remove it from the dnlc
5507 * and error out to be safe.
5509 dnlc_remove(dvp, nm);
5510 VN_RELE(*vpp);
5511 *vpp = NULL;
5512 goto exit;
5514 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5515 if (fhp->nfs_fh4_len == 0) {
5517 * The file exists but a bogus fh
5518 * some unknown reason. Remove it from the dnlc
5519 * and error out to be safe.
5521 e.error = ENOENT;
5522 dnlc_remove(dvp, nm);
5523 VN_RELE(*vpp);
5524 *vpp = NULL;
5525 goto exit;
5527 sfhp = sfh4_get(fhp, mi);
5529 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5530 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5533 * Make the new rnode
5535 if (isdotdot) {
5536 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5537 if (e.error) {
5538 sfh4_rele(&sfhp);
5539 VN_RELE(*vpp);
5540 *vpp = NULL;
5541 goto exit;
5544 * XXX if nfs4_make_dotdot uses an existing rnode
5545 * XXX it doesn't update the attributes.
5546 * XXX for now just save them again to save an OTW
5548 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5549 } else {
5550 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5551 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5553 * If v_type == VNON, then garp was NULL because
5554 * the last op in the compound failed and makenfs4node
5555 * could not find the vnode for sfhp. It created
5556 * a new vnode, so we have nothing to purge here.
5558 if (nvp->v_type == VNON) {
5559 vattr_t vattr;
5561 vattr.va_mask = VATTR_TYPE;
5563 * N.B. We've already called nfs4_end_fop above.
5565 e.error = nfs4getattr(nvp, &vattr, cr);
5566 if (e.error) {
5567 sfh4_rele(&sfhp);
5568 VN_RELE(*vpp);
5569 *vpp = NULL;
5570 VN_RELE(nvp);
5571 goto exit;
5573 nvp->v_type = vattr.va_type;
5576 sfh4_rele(&sfhp);
5578 nrp = VTOR4(nvp);
5579 mutex_enter(&nrp->r_statev4_lock);
5580 if (!nrp->created_v4) {
5581 mutex_exit(&nrp->r_statev4_lock);
5582 dnlc_update(dvp, nm, nvp);
5583 } else
5584 mutex_exit(&nrp->r_statev4_lock);
5586 VN_RELE(*vpp);
5587 *vpp = nvp;
5588 } else {
5589 hrtime_t now;
5590 hrtime_t delta = 0;
5592 e.error = 0;
5595 * Because the NVERIFY "succeeded" we know that the
5596 * directory attributes are still valid
5597 * so update r_time_attr_inval
5599 now = gethrtime();
5600 mutex_enter(&drp->r_statelock);
5601 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5602 delta = now - drp->r_time_attr_saved;
5603 if (delta < mi->mi_acdirmin)
5604 delta = mi->mi_acdirmin;
5605 else if (delta > mi->mi_acdirmax)
5606 delta = mi->mi_acdirmax;
5608 drp->r_time_attr_inval = now + delta;
5609 mutex_exit(&drp->r_statelock);
5610 dnlc_update(dvp, nm, *vpp);
5613 * Even though we have a valid directory attr cache
5614 * and dnlc entry, we may not have access.
5615 * This should almost always hit the cache.
5617 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5618 if (e.error) {
5619 VN_RELE(*vpp);
5620 *vpp = NULL;
5623 if (*vpp == DNLC_NO_VNODE) {
5624 VN_RELE(*vpp);
5625 *vpp = NULL;
5626 e.error = ENOENT;
5630 exit:
5631 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5632 kmem_free(argop, argoplist_size);
5633 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5634 return (e.error);
5638 * We need to go over the wire to lookup the name, but
5639 * while we are there verify the directory has not
5640 * changed but if it has, get new attributes and check access
5642 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5643 * NVERIFY GETATTR ACCESS
5645 * With the results:
5646 * if the NVERIFY failed we must purge the caches, add new attributes,
5647 * and cache new access.
5648 * set a new r_time_attr_inval
5649 * add name to dnlc, possibly negative
5650 * if LOOKUP succeeded
5651 * cache new attributes
5653 static int
5654 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5656 COMPOUND4args_clnt args;
5657 COMPOUND4res_clnt res;
5658 fattr4 *ver_fattr;
5659 fattr4_change dchange;
5660 int32_t *ptr;
5661 nfs4_ga_res_t *garp = NULL;
5662 int argoplist_size = 9 * sizeof (nfs_argop4);
5663 nfs_argop4 *argop;
5664 int doqueue;
5665 mntinfo4_t *mi;
5666 nfs4_recov_state_t recov_state;
5667 hrtime_t t;
5668 int isdotdot;
5669 vnode_t *nvp;
5670 nfs_fh4 *fhp;
5671 nfs4_sharedfh_t *sfhp;
5672 nfs4_access_type_t cacc;
5673 rnode4_t *nrp;
5674 rnode4_t *drp = VTOR4(dvp);
5675 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5677 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5678 ASSERT(nm != NULL);
5679 ASSERT(nm[0] != '\0');
5680 ASSERT(dvp->v_type == VDIR);
5681 ASSERT(nm[0] != '.' || nm[1] != '\0');
5682 ASSERT(*vpp == NULL);
5684 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5685 isdotdot = 1;
5686 args.ctag = TAG_LOOKUP_PARENT;
5687 } else {
5689 * If dvp were a stub, it should have triggered and caused
5690 * a mount for us to get this far.
5692 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5694 isdotdot = 0;
5695 args.ctag = TAG_LOOKUP;
5698 mi = VTOMI4(dvp);
5699 recov_state.rs_flags = 0;
5700 recov_state.rs_num_retry_despite_err = 0;
5702 nvp = NULL;
5704 /* Save the original mount point security information */
5705 (void) save_mnt_secinfo(mi->mi_curr_serv);
5707 recov_retry:
5708 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5709 &recov_state, NULL);
5710 if (e.error) {
5711 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5712 return (e.error);
5715 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5717 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5718 args.array_len = 9;
5719 args.array = argop;
5721 /* 0. putfh file */
5722 argop[0].argop = OP_CPUTFH;
5723 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5725 /* 1. savefh for the nverify */
5726 argop[1].argop = OP_SAVEFH;
5728 /* 2. lookup name */
5729 if (isdotdot) {
5730 argop[2].argop = OP_LOOKUPP;
5731 } else {
5732 argop[2].argop = OP_CLOOKUP;
5733 argop[2].nfs_argop4_u.opclookup.cname = nm;
5736 /* 3. resulting file handle */
5737 argop[3].argop = OP_GETFH;
5739 /* 4. resulting file attributes */
5740 argop[4].argop = OP_GETATTR;
5741 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5742 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5744 /* 5. restorefh back the directory for the nverify */
5745 argop[5].argop = OP_RESTOREFH;
5747 /* 6. nverify the change info */
5748 argop[6].argop = OP_NVERIFY;
5749 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5750 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5751 ver_fattr->attrlist4 = (char *)&dchange;
5752 ptr = (int32_t *)&dchange;
5753 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5754 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5756 /* 7. getattr directory */
5757 argop[7].argop = OP_GETATTR;
5758 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5759 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5761 /* 8. access directory */
5762 argop[8].argop = OP_ACCESS;
5763 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5764 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5766 doqueue = 1;
5767 t = gethrtime();
5769 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5771 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5772 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5773 if (e.error != 0 && *vpp != NULL)
5774 VN_RELE(*vpp);
5775 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5776 &recov_state, FALSE);
5777 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5778 kmem_free(argop, argoplist_size);
5779 return (e.error);
5782 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5784 * For WRONGSEC of a non-dotdot case, send secinfo directly
5785 * from this thread, do not go thru the recovery thread since
5786 * we need the nm information.
5788 * Not doing dotdot case because there is no specification
5789 * for (PUTFH, SECINFO "..") yet.
5791 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5792 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5793 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5794 &recov_state, FALSE);
5795 else
5796 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5797 &recov_state, TRUE);
5798 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5799 kmem_free(argop, argoplist_size);
5800 if (!e.error)
5801 goto recov_retry;
5802 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5803 return (e.error);
5806 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5807 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5809 &recov_state, TRUE);
5811 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5812 kmem_free(argop, argoplist_size);
5813 goto recov_retry;
5817 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5819 if (e.error || res.array_len == 0) {
5821 * If e.error isn't set, then reply has no ops (or we couldn't
5822 * be here). The only legal way to reply without an op array
5823 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5824 * be in the reply for all other status values.
5826 * For valid replies without an ops array, return ENOTSUP
5827 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5828 * return EIO -- don't trust status.
5830 if (e.error == 0)
5831 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5832 ENOTSUP : EIO;
5834 kmem_free(argop, argoplist_size);
5835 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5836 return (e.error);
5839 e.error = geterrno4(res.status);
5842 * The PUTFH and SAVEFH may have failed.
5844 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5845 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5846 nfs4_purge_stale_fh(e.error, dvp, cr);
5847 goto exit;
5851 * Check if the file exists, if it does delay entering
5852 * into the dnlc until after we update the directory
5853 * attributes so we don't cause it to get purged immediately.
5855 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5857 * The lookup failed, probably no entry
5859 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5860 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5861 goto exit;
5864 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5866 * The file exists but we can't get its fh for
5867 * some unknown reason. Error out to be safe.
5869 goto exit;
5872 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5873 if (fhp->nfs_fh4_len == 0) {
5875 * The file exists but a bogus fh
5876 * some unknown reason. Error out to be safe.
5878 e.error = EIO;
5879 goto exit;
5881 sfhp = sfh4_get(fhp, mi);
5883 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5884 sfh4_rele(&sfhp);
5885 goto exit;
5887 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5890 * The RESTOREFH may have failed
5892 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5893 sfh4_rele(&sfhp);
5894 e.error = EIO;
5895 goto exit;
5898 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5900 * First make sure the NVERIFY failed as we expected,
5901 * if it didn't then be conservative and error out
5902 * as we can't trust the directory.
5904 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5905 sfh4_rele(&sfhp);
5906 e.error = EIO;
5907 goto exit;
5911 * We know the NVERIFY "failed" so the directory has changed,
5912 * so we must:
5913 * purge the caches (access and indirectly dnlc if needed)
5915 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5917 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5918 sfh4_rele(&sfhp);
5919 goto exit;
5921 nfs4_attr_cache(dvp,
5922 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5923 t, cr, FALSE, NULL);
5925 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5926 nfs4_purge_stale_fh(e.error, dvp, cr);
5927 sfh4_rele(&sfhp);
5928 e.error = geterrno4(res.status);
5929 goto exit;
5933 * Now we know the directory is valid,
5934 * cache new directory access
5936 nfs4_access_cache(drp,
5937 args.array[8].nfs_argop4_u.opaccess.access,
5938 res.array[8].nfs_resop4_u.opaccess.access, cr);
5941 * recheck VEXEC access
5943 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5944 if (cacc != NFS4_ACCESS_ALLOWED) {
5946 * Directory permissions might have been revoked
5948 if (cacc == NFS4_ACCESS_DENIED) {
5949 sfh4_rele(&sfhp);
5950 e.error = EACCES;
5951 goto exit;
5955 * Somehow we must not have asked for enough
5956 * so try a singleton ACCESS should never happen
5958 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5959 if (e.error) {
5960 sfh4_rele(&sfhp);
5961 goto exit;
5965 e.error = geterrno4(res.status);
5966 } else {
5967 hrtime_t now;
5968 hrtime_t delta = 0;
5970 e.error = 0;
5973 * Because the NVERIFY "succeeded" we know that the
5974 * directory attributes are still valid
5975 * so update r_time_attr_inval
5977 now = gethrtime();
5978 mutex_enter(&drp->r_statelock);
5979 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5980 delta = now - drp->r_time_attr_saved;
5981 if (delta < mi->mi_acdirmin)
5982 delta = mi->mi_acdirmin;
5983 else if (delta > mi->mi_acdirmax)
5984 delta = mi->mi_acdirmax;
5986 drp->r_time_attr_inval = now + delta;
5987 mutex_exit(&drp->r_statelock);
5990 * Even though we have a valid directory attr cache,
5991 * we may not have access.
5992 * This should almost always hit the cache.
5994 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5995 if (e.error) {
5996 sfh4_rele(&sfhp);
5997 goto exit;
6002 * Now we have successfully completed the lookup, if the
6003 * directory has changed we now have the valid attributes.
6004 * We also know we have directory access.
6005 * Create the new rnode and insert it in the dnlc.
6007 if (isdotdot) {
6008 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6009 if (e.error) {
6010 sfh4_rele(&sfhp);
6011 goto exit;
6014 * XXX if nfs4_make_dotdot uses an existing rnode
6015 * XXX it doesn't update the attributes.
6016 * XXX for now just save them again to save an OTW
6018 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6019 } else {
6020 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6021 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6023 sfh4_rele(&sfhp);
6025 nrp = VTOR4(nvp);
6026 mutex_enter(&nrp->r_statev4_lock);
6027 if (!nrp->created_v4) {
6028 mutex_exit(&nrp->r_statev4_lock);
6029 dnlc_update(dvp, nm, nvp);
6030 } else
6031 mutex_exit(&nrp->r_statev4_lock);
6033 *vpp = nvp;
6035 exit:
6036 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6037 kmem_free(argop, argoplist_size);
6038 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6039 return (e.error);
6042 #ifdef DEBUG
6043 void
6044 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6046 uint_t i, len;
6047 zoneid_t zoneid = getzoneid();
6048 char *s;
6050 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6051 for (i = 0; i < argcnt; i++) {
6052 nfs_argop4 *op = &argbase[i];
6053 switch (op->argop) {
6054 case OP_CPUTFH:
6055 case OP_PUTFH:
6056 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6057 break;
6058 case OP_PUTROOTFH:
6059 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6060 break;
6061 case OP_CLOOKUP:
6062 s = op->nfs_argop4_u.opclookup.cname;
6063 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6064 break;
6065 case OP_LOOKUP:
6066 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6067 &len, NULL);
6068 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6069 kmem_free(s, len);
6070 break;
6071 case OP_LOOKUPP:
6072 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6073 break;
6074 case OP_GETFH:
6075 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6076 break;
6077 case OP_GETATTR:
6078 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6079 break;
6080 case OP_OPENATTR:
6081 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6082 break;
6083 default:
6084 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6085 op->argop);
6086 break;
6090 #endif
6093 * nfs4lookup_setup - constructs a multi-lookup compound request.
6095 * Given the path "nm1/nm2/.../nmn", the following compound requests
6096 * may be created:
6098 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6099 * is faster, for now.
6101 * l4_getattrs indicates the type of compound requested.
6103 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6105 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6107 * total number of ops is n + 1.
6109 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6110 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6111 * before the last component, and only get attributes
6112 * for the last component. Note that the second-to-last
6113 * pathname component is XATTR_RPATH, which does NOT go
6114 * over-the-wire as a lookup.
6116 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6117 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6119 * and total number of ops is n + 5.
6121 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6122 * attribute directory: create lookups plus an OPENATTR
6123 * replacing the last lookup. Note that the last pathname
6124 * component is XATTR_RPATH, which does NOT go over-the-wire
6125 * as a lookup.
6127 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6128 * Openattr; Getfh; Getattr }
6130 * and total number of ops is n + 5.
6132 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6133 * nodes too.
6135 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6136 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6138 * and total number of ops is 3*n + 1.
6140 * All cases: returns the index in the arg array of the final LOOKUP op, or
6141 * -1 if no LOOKUPs were used.
6144 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6146 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6147 nfs_argop4 *argbase, *argop;
6148 int arglen, argcnt;
6149 int n = 1; /* number of components */
6150 int nga = 1; /* number of Getattr's in request */
6151 char c = '\0', *s, *p;
6152 int lookup_idx = -1;
6153 int argoplist_size;
6155 /* set lookuparg response result to 0 */
6156 lookupargp->resp->status = NFS4_OK;
6158 /* skip leading "/" or "." e.g. ".//./" if there is */
6159 for (; ; nm++) {
6160 if (*nm != '/' && *nm != '.')
6161 break;
6163 /* ".." is counted as 1 component */
6164 if (*nm == '.' && *(nm + 1) != '/')
6165 break;
6169 * Find n = number of components - nm must be null terminated
6170 * Skip "." components.
6172 if (*nm != '\0')
6173 for (n = 1, s = nm; *s != '\0'; s++) {
6174 if ((*s == '/') && (*(s + 1) != '/') &&
6175 (*(s + 1) != '\0') &&
6176 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6177 *(s + 2) == '\0')))
6178 n++;
6180 else
6181 n = 0;
6184 * nga is number of components that need Getfh+Getattr
6186 switch (l4_getattrs) {
6187 case LKP4_NO_ATTRIBUTES:
6188 nga = 0;
6189 break;
6190 case LKP4_ALL_ATTRIBUTES:
6191 nga = n;
6193 * Always have at least 1 getfh, getattr pair
6195 if (nga == 0)
6196 nga++;
6197 break;
6198 case LKP4_LAST_ATTRDIR:
6199 case LKP4_LAST_NAMED_ATTR:
6200 nga = n+1;
6201 break;
6205 * If change to use the filehandle attr instead of getfh
6206 * the following line can be deleted.
6208 nga *= 2;
6211 * calculate number of ops in request as
6212 * header + trailer + lookups + getattrs
6214 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6216 argoplist_size = arglen * sizeof (nfs_argop4);
6217 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6218 lookupargp->argsp->array = argop;
6220 argcnt = lookupargp->header_len;
6221 argop += argcnt;
6224 * loop and create a lookup op and possibly getattr/getfh for
6225 * each component. Skip "." components.
6227 for (s = nm; *s != '\0'; s = p) {
6229 * Set up a pathname struct for each component if needed
6231 while (*s == '/')
6232 s++;
6233 if (*s == '\0')
6234 break;
6236 for (p = s; (*p != '/') && (*p != '\0'); p++)
6238 c = *p;
6239 *p = '\0';
6241 if (s[0] == '.' && s[1] == '\0') {
6242 *p = c;
6243 continue;
6245 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6246 strcmp(s, XATTR_RPATH) == 0) {
6247 /* getfh XXX may not be needed in future */
6248 argop->argop = OP_GETFH;
6249 argop++;
6250 argcnt++;
6252 /* getattr */
6253 argop->argop = OP_GETATTR;
6254 argop->nfs_argop4_u.opgetattr.attr_request =
6255 lookupargp->ga_bits;
6256 argop->nfs_argop4_u.opgetattr.mi =
6257 lookupargp->mi;
6258 argop++;
6259 argcnt++;
6261 /* openattr */
6262 argop->argop = OP_OPENATTR;
6263 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6264 strcmp(s, XATTR_RPATH) == 0) {
6265 /* openattr */
6266 argop->argop = OP_OPENATTR;
6267 argop++;
6268 argcnt++;
6270 /* getfh XXX may not be needed in future */
6271 argop->argop = OP_GETFH;
6272 argop++;
6273 argcnt++;
6275 /* getattr */
6276 argop->argop = OP_GETATTR;
6277 argop->nfs_argop4_u.opgetattr.attr_request =
6278 lookupargp->ga_bits;
6279 argop->nfs_argop4_u.opgetattr.mi =
6280 lookupargp->mi;
6281 argop++;
6282 argcnt++;
6283 *p = c;
6284 continue;
6285 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6286 /* lookupp */
6287 argop->argop = OP_LOOKUPP;
6288 } else {
6289 /* lookup */
6290 argop->argop = OP_LOOKUP;
6291 (void) str_to_utf8(s,
6292 &argop->nfs_argop4_u.oplookup.objname);
6294 lookup_idx = argcnt;
6295 argop++;
6296 argcnt++;
6298 *p = c;
6300 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6301 /* getfh XXX may not be needed in future */
6302 argop->argop = OP_GETFH;
6303 argop++;
6304 argcnt++;
6306 /* getattr */
6307 argop->argop = OP_GETATTR;
6308 argop->nfs_argop4_u.opgetattr.attr_request =
6309 lookupargp->ga_bits;
6310 argop->nfs_argop4_u.opgetattr.mi =
6311 lookupargp->mi;
6312 argop++;
6313 argcnt++;
6317 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6318 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6319 if (needgetfh) {
6320 /* stick in a post-lookup getfh */
6321 argop->argop = OP_GETFH;
6322 argcnt++;
6323 argop++;
6325 /* post-lookup getattr */
6326 argop->argop = OP_GETATTR;
6327 argop->nfs_argop4_u.opgetattr.attr_request =
6328 lookupargp->ga_bits;
6329 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6330 argcnt++;
6332 argcnt += lookupargp->trailer_len; /* actual op count */
6333 lookupargp->argsp->array_len = argcnt;
6334 lookupargp->arglen = arglen;
6336 #ifdef DEBUG
6337 if (nfs4_client_lookup_debug)
6338 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6339 #endif
6341 return (lookup_idx);
6344 static int
6345 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6347 COMPOUND4args_clnt args;
6348 COMPOUND4res_clnt res;
6349 GETFH4res *gf_res = NULL;
6350 nfs_argop4 argop[4];
6351 nfs_resop4 *resop = NULL;
6352 nfs4_sharedfh_t *sfhp;
6353 hrtime_t t;
6354 nfs4_error_t e;
6356 rnode4_t *drp;
6357 int doqueue = 1;
6358 vnode_t *vp;
6359 int needrecov = 0;
6360 nfs4_recov_state_t recov_state;
6362 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6364 *avp = NULL;
6365 recov_state.rs_flags = 0;
6366 recov_state.rs_num_retry_despite_err = 0;
6368 recov_retry:
6369 /* COMPOUND: putfh, openattr, getfh, getattr */
6370 args.array_len = 4;
6371 args.array = argop;
6372 args.ctag = TAG_OPENATTR;
6374 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6375 if (e.error)
6376 return (e.error);
6378 drp = VTOR4(dvp);
6380 /* putfh */
6381 argop[0].argop = OP_CPUTFH;
6382 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6384 /* openattr */
6385 argop[1].argop = OP_OPENATTR;
6386 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6388 /* getfh */
6389 argop[2].argop = OP_GETFH;
6391 /* getattr */
6392 argop[3].argop = OP_GETATTR;
6393 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6394 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6396 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6397 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6398 rnode4info(drp)));
6400 t = gethrtime();
6402 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6404 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6405 if (needrecov) {
6406 bool_t abort;
6408 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6409 "nfs4openattr: initiating recovery\n"));
6411 abort = nfs4_start_recovery(&e,
6412 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6413 OP_OPENATTR, NULL, NULL, NULL);
6414 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6415 if (!e.error) {
6416 e.error = geterrno4(res.status);
6417 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6419 if (abort == FALSE)
6420 goto recov_retry;
6421 return (e.error);
6424 if (e.error) {
6425 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6426 return (e.error);
6429 if (res.status) {
6431 * If OTW errro is NOTSUPP, then it should be
6432 * translated to EINVAL. All Solaris file system
6433 * implementations return EINVAL to the syscall layer
6434 * when the attrdir cannot be created due to an
6435 * implementation restriction or noxattr mount option.
6437 if (res.status == NFS4ERR_NOTSUPP) {
6438 mutex_enter(&drp->r_statelock);
6439 if (drp->r_xattr_dir)
6440 VN_RELE(drp->r_xattr_dir);
6441 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6442 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6443 mutex_exit(&drp->r_statelock);
6445 e.error = EINVAL;
6446 } else {
6447 e.error = geterrno4(res.status);
6450 if (e.error) {
6451 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6452 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6453 needrecov);
6454 return (e.error);
6458 resop = &res.array[0]; /* putfh res */
6459 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6461 resop = &res.array[1]; /* openattr res */
6462 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6464 resop = &res.array[2]; /* getfh res */
6465 gf_res = &resop->nfs_resop4_u.opgetfh;
6466 if (gf_res->object.nfs_fh4_len == 0) {
6467 *avp = NULL;
6468 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6469 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6470 return (ENOENT);
6473 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6474 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6475 dvp->v_vfsp, t, cr, dvp,
6476 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6477 sfh4_rele(&sfhp);
6479 if (e.error)
6480 PURGE_ATTRCACHE4(vp);
6482 mutex_enter(&vp->v_lock);
6483 vp->v_flag |= V_XATTRDIR;
6484 mutex_exit(&vp->v_lock);
6486 *avp = vp;
6488 mutex_enter(&drp->r_statelock);
6489 if (drp->r_xattr_dir)
6490 VN_RELE(drp->r_xattr_dir);
6491 VN_HOLD(vp);
6492 drp->r_xattr_dir = vp;
6495 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6496 * NULL. xattrs could be created at any time, and we have no
6497 * way to update pc4_xattr_exists in the base object if/when
6498 * it happens.
6500 drp->r_pathconf.pc4_xattr_valid = 0;
6502 mutex_exit(&drp->r_statelock);
6504 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6506 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6508 return (0);
6511 /* ARGSUSED */
6512 static int
6513 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6514 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6515 vsecattr_t *vsecp)
6517 int error;
6518 vnode_t *vp = NULL;
6519 rnode4_t *rp;
6520 struct vattr vattr;
6521 rnode4_t *drp;
6522 vnode_t *tempvp;
6523 enum createmode4 createmode;
6524 bool_t must_trunc = FALSE;
6525 int truncating = 0;
6527 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6528 return (EPERM);
6529 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6530 return (EINVAL);
6533 /* . and .. have special meaning in the protocol, reject them. */
6535 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6536 return (EISDIR);
6538 drp = VTOR4(dvp);
6540 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6541 return (EINTR);
6543 top:
6545 * We make a copy of the attributes because the caller does not
6546 * expect us to change what va points to.
6548 vattr = *va;
6551 * If the pathname is "", then dvp is the root vnode of
6552 * a remote file mounted over a local directory.
6553 * All that needs to be done is access
6554 * checking and truncation. Note that we avoid doing
6555 * open w/ create because the parent directory might
6556 * be in pseudo-fs and the open would fail.
6558 if (*nm == '\0') {
6559 error = 0;
6560 VN_HOLD(dvp);
6561 vp = dvp;
6562 must_trunc = TRUE;
6563 } else {
6565 * We need to go over the wire, just to be sure whether the
6566 * file exists or not. Using the DNLC can be dangerous in
6567 * this case when making a decision regarding existence.
6569 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6572 if (exclusive)
6573 createmode = EXCLUSIVE4;
6574 else
6575 createmode = GUARDED4;
6578 * error would be set if the file does not exist on the
6579 * server, so lets go create it.
6581 if (error) {
6582 goto create_otw;
6586 * File does exist on the server
6588 if (exclusive == EXCL)
6589 error = EEXIST;
6590 else if (vp->v_type == VDIR && (mode & VWRITE))
6591 error = EISDIR;
6592 else {
6594 * If vnode is a device, create special vnode.
6596 if (ISVDEV(vp->v_type)) {
6597 tempvp = vp;
6598 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6599 VN_RELE(tempvp);
6601 if (!(error = fop_access(vp, mode, 0, cr, ct))) {
6602 if ((vattr.va_mask & VATTR_SIZE) &&
6603 vp->v_type == VREG) {
6604 rp = VTOR4(vp);
6606 if (must_trunc) {
6607 vattr.va_mask = VATTR_SIZE;
6608 error = nfs4setattr(vp, &vattr, 0, cr,
6609 NULL);
6610 } else {
6612 * we know we have a regular file that already
6613 * exists and we may end up truncating the file
6614 * as a result of the open_otw, so flush out
6615 * any dirty pages for this file first.
6617 if (nfs4_has_pages(vp) &&
6618 ((rp->r_flags & R4DIRTY) ||
6619 rp->r_count > 0 ||
6620 rp->r_mapcnt > 0)) {
6621 error = nfs4_putpage(vp,
6622 0, 0, 0, cr, ct);
6623 if (error && (error == ENOSPC ||
6624 error == EDQUOT)) {
6625 mutex_enter(
6626 &rp->r_statelock);
6627 if (!rp->r_error)
6628 rp->r_error =
6629 error;
6630 mutex_exit(
6631 &rp->r_statelock);
6634 vattr.va_mask = (VATTR_SIZE |
6635 VATTR_TYPE | VATTR_MODE);
6636 vattr.va_type = VREG;
6637 createmode = UNCHECKED4;
6638 truncating = 1;
6639 goto create_otw;
6644 nfs_rw_exit(&drp->r_rwlock);
6645 if (error) {
6646 VN_RELE(vp);
6647 } else {
6648 vnode_t *tvp;
6649 rnode4_t *trp;
6650 tvp = vp;
6651 if (vp->v_type == VREG) {
6652 trp = VTOR4(vp);
6653 if (IS_SHADOW(vp, trp))
6654 tvp = RTOV4(trp);
6657 if (must_trunc) {
6659 * existing file got truncated, notify.
6661 vnevent_create(tvp, ct);
6664 *vpp = vp;
6666 return (error);
6668 create_otw:
6669 dnlc_remove(dvp, nm);
6671 ASSERT(vattr.va_mask & VATTR_TYPE);
6674 * If not a regular file let nfs4mknod() handle it.
6676 if (vattr.va_type != VREG) {
6677 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6678 nfs_rw_exit(&drp->r_rwlock);
6679 return (error);
6683 * It _is_ a regular file.
6685 ASSERT(vattr.va_mask & VATTR_MODE);
6686 if (MANDMODE(vattr.va_mode)) {
6687 nfs_rw_exit(&drp->r_rwlock);
6688 return (EACCES);
6692 * If this happens to be a mknod of a regular file, then flags will
6693 * have neither FREAD or FWRITE. However, we must set at least one
6694 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6695 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6696 * set (based on openmode specified by app).
6698 if ((flags & (FREAD|FWRITE)) == 0)
6699 flags |= (FREAD|FWRITE);
6701 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6703 if (vp != NULL) {
6704 /* if create was successful, throw away the file's pages */
6705 if (!error && (vattr.va_mask & VATTR_SIZE))
6706 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6707 cr);
6708 /* release the lookup hold */
6709 VN_RELE(vp);
6710 vp = NULL;
6714 * validate that we opened a regular file. This handles a misbehaving
6715 * server that returns an incorrect FH.
6717 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6718 error = EISDIR;
6719 VN_RELE(*vpp);
6723 * If this is not an exclusive create, then the CREATE
6724 * request will be made with the GUARDED mode set. This
6725 * means that the server will return EEXIST if the file
6726 * exists. The file could exist because of a retransmitted
6727 * request. In this case, we recover by starting over and
6728 * checking to see whether the file exists. This second
6729 * time through it should and a CREATE request will not be
6730 * sent.
6732 * This handles the problem of a dangling CREATE request
6733 * which contains attributes which indicate that the file
6734 * should be truncated. This retransmitted request could
6735 * possibly truncate valid data in the file if not caught
6736 * by the duplicate request mechanism on the server or if
6737 * not caught by other means. The scenario is:
6739 * Client transmits CREATE request with size = 0
6740 * Client times out, retransmits request.
6741 * Response to the first request arrives from the server
6742 * and the client proceeds on.
6743 * Client writes data to the file.
6744 * The server now processes retransmitted CREATE request
6745 * and truncates file.
6747 * The use of the GUARDED CREATE request prevents this from
6748 * happening because the retransmitted CREATE would fail
6749 * with EEXIST and would not truncate the file.
6751 if (error == EEXIST && exclusive == NONEXCL) {
6752 #ifdef DEBUG
6753 nfs4_create_misses++;
6754 #endif
6755 goto top;
6757 nfs_rw_exit(&drp->r_rwlock);
6758 if (truncating && !error && *vpp) {
6759 vnode_t *tvp;
6760 rnode4_t *trp;
6762 * existing file got truncated, notify.
6764 tvp = *vpp;
6765 trp = VTOR4(tvp);
6766 if (IS_SHADOW(tvp, trp))
6767 tvp = RTOV4(trp);
6768 vnevent_create(tvp, ct);
6770 return (error);
6774 * Create compound (for mkdir, mknod, symlink):
6775 * { Putfh <dfh>; Create; Getfh; Getattr }
6776 * It's okay if setattr failed to set gid - this is not considered
6777 * an error, but purge attrs in that case.
6779 static int
6780 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6781 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6783 int need_end_op = FALSE;
6784 COMPOUND4args_clnt args;
6785 COMPOUND4res_clnt res, *resp = NULL;
6786 nfs_argop4 *argop;
6787 nfs_resop4 *resop;
6788 int doqueue;
6789 mntinfo4_t *mi;
6790 rnode4_t *drp = VTOR4(dvp);
6791 change_info4 *cinfo;
6792 GETFH4res *gf_res;
6793 struct vattr vattr;
6794 vnode_t *vp;
6795 fattr4 *crattr;
6796 bool_t needrecov = FALSE;
6797 nfs4_recov_state_t recov_state;
6798 nfs4_sharedfh_t *sfhp = NULL;
6799 hrtime_t t;
6800 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6801 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6802 dirattr_info_t dinfo, *dinfop;
6803 servinfo4_t *svp;
6804 bitmap4 supp_attrs;
6806 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6807 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6809 mi = VTOMI4(dvp);
6812 * Make sure we properly deal with setting the right gid
6813 * on a new directory to reflect the parent's setgid bit
6815 setgid_flag = 0;
6816 if (type == NF4DIR) {
6817 struct vattr dva;
6819 va->va_mode &= ~VSGID;
6820 dva.va_mask = VATTR_MODE | VATTR_GID;
6821 if (fop_getattr(dvp, &dva, 0, cr, NULL) == 0) {
6824 * If the parent's directory has the setgid bit set
6825 * _and_ the client was able to get a valid mapping
6826 * for the parent dir's owner_group, we want to
6827 * append NVERIFY(owner_group == dva.va_gid) and
6828 * SETTATTR to the CREATE compound.
6830 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6831 setgid_flag = 1;
6832 va->va_mode |= VSGID;
6833 if (dva.va_gid != GID_NOBODY) {
6834 va->va_mask |= VATTR_GID;
6835 va->va_gid = dva.va_gid;
6842 * Create ops:
6843 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6844 * 5:restorefh(dir) 6:getattr(dir)
6846 * if (setgid)
6847 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6848 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6849 * 8:nverify 9:setattr
6851 if (setgid_flag) {
6852 numops = 10;
6853 idx_create = 1;
6854 idx_fattr = 3;
6855 } else {
6856 numops = 7;
6857 idx_create = 2;
6858 idx_fattr = 4;
6861 ASSERT(nfs_zone() == mi->mi_zone);
6862 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6863 return (EINTR);
6865 recov_state.rs_flags = 0;
6866 recov_state.rs_num_retry_despite_err = 0;
6868 argoplist_size = numops * sizeof (nfs_argop4);
6869 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6871 recov_retry:
6872 if (type == NF4LNK)
6873 args.ctag = TAG_SYMLINK;
6874 else if (type == NF4DIR)
6875 args.ctag = TAG_MKDIR;
6876 else
6877 args.ctag = TAG_MKNOD;
6879 args.array_len = numops;
6880 args.array = argop;
6882 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6883 nfs_rw_exit(&drp->r_rwlock);
6884 kmem_free(argop, argoplist_size);
6885 return (e.error);
6887 need_end_op = TRUE;
6890 /* 0: putfh directory */
6891 argop[0].argop = OP_CPUTFH;
6892 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6894 /* 1/2: Create object */
6895 argop[idx_create].argop = OP_CCREATE;
6896 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6897 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6898 if (type == NF4LNK) {
6900 * symlink, treat name as data
6902 ASSERT(data != NULL);
6903 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6904 (char *)data;
6906 if (type == NF4BLK || type == NF4CHR) {
6907 ASSERT(data != NULL);
6908 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6909 *((specdata4 *)data);
6912 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6914 svp = drp->r_server;
6915 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6916 supp_attrs = svp->sv_supp_attrs;
6917 nfs_rw_exit(&svp->sv_lock);
6919 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6920 nfs_rw_exit(&drp->r_rwlock);
6921 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6922 e.error = EINVAL;
6923 kmem_free(argop, argoplist_size);
6924 return (e.error);
6927 /* 2/3: getfh fh of created object */
6928 ASSERT(idx_create + 1 == idx_fattr - 1);
6929 argop[idx_create + 1].argop = OP_GETFH;
6931 /* 3/4: getattr of new object */
6932 argop[idx_fattr].argop = OP_GETATTR;
6933 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6934 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6936 if (setgid_flag) {
6937 vattr_t _v;
6939 argop[4].argop = OP_SAVEFH;
6941 argop[5].argop = OP_CPUTFH;
6942 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6944 argop[6].argop = OP_GETATTR;
6945 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6946 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6948 argop[7].argop = OP_RESTOREFH;
6951 * nverify
6953 * XXX - Revisit the last argument to nfs4_end_op()
6954 * once 5020486 is fixed.
6956 _v.va_mask = VATTR_GID;
6957 _v.va_gid = va->va_gid;
6958 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6959 supp_attrs)) {
6960 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6961 nfs_rw_exit(&drp->r_rwlock);
6962 nfs4_fattr4_free(crattr);
6963 kmem_free(argop, argoplist_size);
6964 return (e.error);
6968 * setattr
6970 * We _know_ we're not messing with VATTR_SIZE or
6971 * VATTR_XTIME, so no need for stateid or flags. Also we
6972 * specify NULL rp since we're only interested in setting
6973 * owner_group attributes.
6975 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
6976 &e.error, 0);
6978 if (e.error) {
6979 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6980 nfs_rw_exit(&drp->r_rwlock);
6981 nfs4_fattr4_free(crattr);
6982 nfs4args_verify_free(&argop[8]);
6983 kmem_free(argop, argoplist_size);
6984 return (e.error);
6986 } else {
6987 argop[1].argop = OP_SAVEFH;
6989 argop[5].argop = OP_RESTOREFH;
6991 argop[6].argop = OP_GETATTR;
6992 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6993 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6996 dnlc_remove(dvp, nm);
6998 doqueue = 1;
6999 t = gethrtime();
7000 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7002 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7003 if (e.error) {
7004 PURGE_ATTRCACHE4(dvp);
7005 if (!needrecov)
7006 goto out;
7009 if (needrecov) {
7010 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7011 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7012 nfs4_end_op(mi, dvp, NULL, &recov_state,
7013 needrecov);
7014 need_end_op = FALSE;
7015 nfs4_fattr4_free(crattr);
7016 if (setgid_flag) {
7017 nfs4args_verify_free(&argop[8]);
7018 nfs4args_setattr_free(&argop[9]);
7020 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7021 goto recov_retry;
7025 resp = &res;
7027 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7029 if (res.status == NFS4ERR_BADOWNER)
7030 nfs4_log_badowner(mi, OP_CREATE);
7032 e.error = geterrno4(res.status);
7035 * This check is left over from when create was implemented
7036 * using a setattr op (instead of createattrs). If the
7037 * putfh/create/getfh failed, the error was returned. If
7038 * setattr/getattr failed, we keep going.
7040 * It might be better to get rid of the GETFH also, and just
7041 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7042 * Then if any of the operations failed, we could return the
7043 * error now, and remove much of the error code below.
7045 if (res.array_len <= idx_fattr) {
7047 * Either Putfh, Create or Getfh failed.
7049 PURGE_ATTRCACHE4(dvp);
7051 * nfs4_purge_stale_fh() may generate otw calls through
7052 * nfs4_invalidate_pages. Hence the need to call
7053 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7055 nfs4_end_op(mi, dvp, NULL, &recov_state,
7056 needrecov);
7057 need_end_op = FALSE;
7058 nfs4_purge_stale_fh(e.error, dvp, cr);
7059 goto out;
7063 resop = &res.array[idx_create]; /* create res */
7064 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7066 resop = &res.array[idx_create + 1]; /* getfh res */
7067 gf_res = &resop->nfs_resop4_u.opgetfh;
7069 sfhp = sfh4_get(&gf_res->object, mi);
7070 if (e.error) {
7071 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7072 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7073 if (vp->v_type == VNON) {
7074 vattr.va_mask = VATTR_TYPE;
7076 * Need to call nfs4_end_op before nfs4getattr to avoid
7077 * potential nfs4_start_op deadlock. See RFE 4777612.
7079 nfs4_end_op(mi, dvp, NULL, &recov_state,
7080 needrecov);
7081 need_end_op = FALSE;
7082 e.error = nfs4getattr(vp, &vattr, cr);
7083 if (e.error) {
7084 VN_RELE(vp);
7085 *vpp = NULL;
7086 goto out;
7088 vp->v_type = vattr.va_type;
7090 e.error = 0;
7091 } else {
7092 *vpp = vp = makenfs4node(sfhp,
7093 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7094 dvp->v_vfsp, t, cr,
7095 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7099 * If compound succeeded, then update dir attrs
7101 if (res.status == NFS4_OK) {
7102 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7103 dinfo.di_cred = cr;
7104 dinfo.di_time_call = t;
7105 dinfop = &dinfo;
7106 } else
7107 dinfop = NULL;
7109 /* Update directory cache attribute, readdir and dnlc caches */
7110 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7112 out:
7113 if (sfhp != NULL)
7114 sfh4_rele(&sfhp);
7115 nfs_rw_exit(&drp->r_rwlock);
7116 nfs4_fattr4_free(crattr);
7117 if (setgid_flag) {
7118 nfs4args_verify_free(&argop[8]);
7119 nfs4args_setattr_free(&argop[9]);
7121 if (resp)
7122 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7123 if (need_end_op)
7124 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7126 kmem_free(argop, argoplist_size);
7127 return (e.error);
7130 /* ARGSUSED */
7131 static int
7132 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7133 int mode, vnode_t **vpp, cred_t *cr)
7135 int error;
7136 vnode_t *vp;
7137 nfs_ftype4 type;
7138 specdata4 spec, *specp = NULL;
7140 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7142 switch (va->va_type) {
7143 case VCHR:
7144 case VBLK:
7145 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7146 spec.specdata1 = getmajor(va->va_rdev);
7147 spec.specdata2 = getminor(va->va_rdev);
7148 specp = &spec;
7149 break;
7151 case VFIFO:
7152 type = NF4FIFO;
7153 break;
7154 case VSOCK:
7155 type = NF4SOCK;
7156 break;
7158 default:
7159 return (EINVAL);
7162 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7163 if (error) {
7164 return (error);
7168 * This might not be needed any more; special case to deal
7169 * with problematic v2/v3 servers. Since create was unable
7170 * to set group correctly, not sure what hope setattr has.
7172 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7173 va->va_mask = VATTR_GID;
7174 (void) nfs4setattr(vp, va, 0, cr, NULL);
7178 * If vnode is a device create special vnode
7180 if (ISVDEV(vp->v_type)) {
7181 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7182 VN_RELE(vp);
7183 } else {
7184 *vpp = vp;
7186 return (error);
7190 * Remove requires that the current fh be the target directory.
7191 * After the operation, the current fh is unchanged.
7192 * The compound op structure is:
7193 * PUTFH(targetdir), REMOVE
7195 * Weirdness: if the vnode to be removed is open
7196 * we rename it instead of removing it and nfs_inactive
7197 * will remove the new name.
7199 /* ARGSUSED */
7200 static int
7201 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7203 COMPOUND4args_clnt args;
7204 COMPOUND4res_clnt res, *resp = NULL;
7205 REMOVE4res *rm_res;
7206 nfs_argop4 argop[3];
7207 nfs_resop4 *resop;
7208 vnode_t *vp;
7209 char *tmpname;
7210 int doqueue;
7211 mntinfo4_t *mi;
7212 rnode4_t *rp;
7213 rnode4_t *drp;
7214 int needrecov = 0;
7215 nfs4_recov_state_t recov_state;
7216 int isopen;
7217 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7218 dirattr_info_t dinfo;
7220 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7221 return (EPERM);
7222 drp = VTOR4(dvp);
7223 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7224 return (EINTR);
7226 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7227 if (e.error) {
7228 nfs_rw_exit(&drp->r_rwlock);
7229 return (e.error);
7232 if (vp->v_type == VDIR) {
7233 VN_RELE(vp);
7234 nfs_rw_exit(&drp->r_rwlock);
7235 return (EISDIR);
7239 * First just remove the entry from the name cache, as it
7240 * is most likely the only entry for this vp.
7242 dnlc_remove(dvp, nm);
7244 rp = VTOR4(vp);
7247 * For regular file types, check to see if the file is open by looking
7248 * at the open streams.
7249 * For all other types, check the reference count on the vnode. Since
7250 * they are not opened OTW they never have an open stream.
7252 * If the file is open, rename it to .nfsXXXX.
7254 if (vp->v_type != VREG) {
7256 * If the file has a v_count > 1 then there may be more than one
7257 * entry in the name cache due multiple links or an open file,
7258 * but we don't have the real reference count so flush all
7259 * possible entries.
7261 if (vp->v_count > 1)
7262 dnlc_purge_vp(vp);
7265 * Now we have the real reference count.
7267 isopen = vp->v_count > 1;
7268 } else {
7269 mutex_enter(&rp->r_os_lock);
7270 isopen = list_head(&rp->r_open_streams) != NULL;
7271 mutex_exit(&rp->r_os_lock);
7274 mutex_enter(&rp->r_statelock);
7275 if (isopen &&
7276 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7277 mutex_exit(&rp->r_statelock);
7278 tmpname = newname();
7279 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7280 if (e.error)
7281 kmem_free(tmpname, MAXNAMELEN);
7282 else {
7283 mutex_enter(&rp->r_statelock);
7284 if (rp->r_unldvp == NULL) {
7285 VN_HOLD(dvp);
7286 rp->r_unldvp = dvp;
7287 if (rp->r_unlcred != NULL)
7288 crfree(rp->r_unlcred);
7289 crhold(cr);
7290 rp->r_unlcred = cr;
7291 rp->r_unlname = tmpname;
7292 } else {
7293 kmem_free(rp->r_unlname, MAXNAMELEN);
7294 rp->r_unlname = tmpname;
7296 mutex_exit(&rp->r_statelock);
7298 VN_RELE(vp);
7299 nfs_rw_exit(&drp->r_rwlock);
7300 return (e.error);
7303 * Actually remove the file/dir
7305 mutex_exit(&rp->r_statelock);
7308 * We need to flush any dirty pages which happen to
7309 * be hanging around before removing the file.
7310 * This shouldn't happen very often since in NFSv4
7311 * we should be close to open consistent.
7313 if (nfs4_has_pages(vp) &&
7314 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7315 e.error = nfs4_putpage(vp, 0, 0, 0, cr, ct);
7316 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7317 mutex_enter(&rp->r_statelock);
7318 if (!rp->r_error)
7319 rp->r_error = e.error;
7320 mutex_exit(&rp->r_statelock);
7324 mi = VTOMI4(dvp);
7326 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7327 recov_state.rs_flags = 0;
7328 recov_state.rs_num_retry_despite_err = 0;
7330 recov_retry:
7332 * Remove ops: putfh dir; remove
7334 args.ctag = TAG_REMOVE;
7335 args.array_len = 3;
7336 args.array = argop;
7338 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7339 if (e.error) {
7340 nfs_rw_exit(&drp->r_rwlock);
7341 VN_RELE(vp);
7342 return (e.error);
7345 /* putfh directory */
7346 argop[0].argop = OP_CPUTFH;
7347 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7349 /* remove */
7350 argop[1].argop = OP_CREMOVE;
7351 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7353 /* getattr dir */
7354 argop[2].argop = OP_GETATTR;
7355 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7356 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7358 doqueue = 1;
7359 dinfo.di_time_call = gethrtime();
7360 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7362 PURGE_ATTRCACHE4(vp);
7364 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7365 if (e.error)
7366 PURGE_ATTRCACHE4(dvp);
7368 if (needrecov) {
7369 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7370 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7371 if (!e.error)
7372 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7373 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7374 needrecov);
7375 goto recov_retry;
7380 * Matching nfs4_end_op() for start_op() above.
7381 * There is a path in the code below which calls
7382 * nfs4_purge_stale_fh(), which may generate otw calls through
7383 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7384 * here to avoid nfs4_start_op() deadlock.
7386 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7388 if (!e.error) {
7389 resp = &res;
7391 if (res.status) {
7392 e.error = geterrno4(res.status);
7393 PURGE_ATTRCACHE4(dvp);
7394 nfs4_purge_stale_fh(e.error, dvp, cr);
7395 } else {
7396 resop = &res.array[1]; /* remove res */
7397 rm_res = &resop->nfs_resop4_u.opremove;
7399 dinfo.di_garp =
7400 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7401 dinfo.di_cred = cr;
7403 /* Update directory attr, readdir and dnlc caches */
7404 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7405 &dinfo);
7408 nfs_rw_exit(&drp->r_rwlock);
7409 if (resp)
7410 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7412 if (e.error == 0) {
7413 vnode_t *tvp;
7414 rnode4_t *trp;
7415 trp = VTOR4(vp);
7416 tvp = vp;
7417 if (IS_SHADOW(vp, trp))
7418 tvp = RTOV4(trp);
7419 vnevent_remove(tvp, dvp, nm, ct);
7421 VN_RELE(vp);
7422 return (e.error);
7426 * Link requires that the current fh be the target directory and the
7427 * saved fh be the source fh. After the operation, the current fh is unchanged.
7428 * Thus the compound op structure is:
7429 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7430 * GETATTR(file)
7432 /* ARGSUSED */
7433 static int
7434 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7435 caller_context_t *ct, int flags)
7437 COMPOUND4args_clnt args;
7438 COMPOUND4res_clnt res, *resp = NULL;
7439 LINK4res *ln_res;
7440 int argoplist_size = 7 * sizeof (nfs_argop4);
7441 nfs_argop4 *argop;
7442 nfs_resop4 *resop;
7443 vnode_t *realvp, *nvp;
7444 int doqueue;
7445 mntinfo4_t *mi;
7446 rnode4_t *tdrp;
7447 bool_t needrecov = FALSE;
7448 nfs4_recov_state_t recov_state;
7449 hrtime_t t;
7450 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7451 dirattr_info_t dinfo;
7453 ASSERT(*tnm != '\0');
7454 ASSERT(tdvp->v_type == VDIR);
7455 ASSERT(nfs4_consistent_type(tdvp));
7456 ASSERT(nfs4_consistent_type(svp));
7458 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7459 return (EPERM);
7460 if (fop_realvp(svp, &realvp, ct) == 0) {
7461 svp = realvp;
7462 ASSERT(nfs4_consistent_type(svp));
7465 tdrp = VTOR4(tdvp);
7466 mi = VTOMI4(svp);
7468 if (!(mi->mi_flags & MI4_LINK)) {
7469 return (EOPNOTSUPP);
7471 recov_state.rs_flags = 0;
7472 recov_state.rs_num_retry_despite_err = 0;
7474 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7475 return (EINTR);
7477 recov_retry:
7478 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7480 args.ctag = TAG_LINK;
7483 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7484 * restorefh; getattr(fl)
7486 args.array_len = 7;
7487 args.array = argop;
7489 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7490 if (e.error) {
7491 kmem_free(argop, argoplist_size);
7492 nfs_rw_exit(&tdrp->r_rwlock);
7493 return (e.error);
7496 /* 0. putfh file */
7497 argop[0].argop = OP_CPUTFH;
7498 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7500 /* 1. save current fh to free up the space for the dir */
7501 argop[1].argop = OP_SAVEFH;
7503 /* 2. putfh targetdir */
7504 argop[2].argop = OP_CPUTFH;
7505 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7507 /* 3. link: current_fh is targetdir, saved_fh is source */
7508 argop[3].argop = OP_CLINK;
7509 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7511 /* 4. Get attributes of dir */
7512 argop[4].argop = OP_GETATTR;
7513 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7514 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7516 /* 5. If link was successful, restore current vp to file */
7517 argop[5].argop = OP_RESTOREFH;
7519 /* 6. Get attributes of linked object */
7520 argop[6].argop = OP_GETATTR;
7521 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7522 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7524 dnlc_remove(tdvp, tnm);
7526 doqueue = 1;
7527 t = gethrtime();
7529 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7531 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7532 if (e.error != 0 && !needrecov) {
7533 PURGE_ATTRCACHE4(tdvp);
7534 PURGE_ATTRCACHE4(svp);
7535 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7536 goto out;
7539 if (needrecov) {
7540 bool_t abort;
7542 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7543 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7544 if (abort == FALSE) {
7545 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7546 needrecov);
7547 kmem_free(argop, argoplist_size);
7548 if (!e.error)
7549 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7550 goto recov_retry;
7551 } else {
7552 if (e.error != 0) {
7553 PURGE_ATTRCACHE4(tdvp);
7554 PURGE_ATTRCACHE4(svp);
7555 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7556 &recov_state, needrecov);
7557 goto out;
7559 /* fall through for res.status case */
7563 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7565 resp = &res;
7566 if (res.status) {
7567 /* If link succeeded, then don't return error */
7568 e.error = geterrno4(res.status);
7569 if (res.array_len <= 4) {
7571 * Either Putfh, Savefh, Putfh dir, or Link failed
7573 PURGE_ATTRCACHE4(svp);
7574 PURGE_ATTRCACHE4(tdvp);
7575 if (e.error == EOPNOTSUPP) {
7576 mutex_enter(&mi->mi_lock);
7577 mi->mi_flags &= ~MI4_LINK;
7578 mutex_exit(&mi->mi_lock);
7580 /* XXX-LP */
7581 if (e.error == EISDIR && crgetuid(cr) != 0)
7582 e.error = EPERM;
7583 goto out;
7587 /* either no error or one of the postop getattr failed */
7590 * XXX - if LINK succeeded, but no attrs were returned for link
7591 * file, purge its cache.
7593 * XXX Perform a simplified version of wcc checking. Instead of
7594 * have another getattr to get pre-op, just purge cache if
7595 * any of the ops prior to and including the getattr failed.
7596 * If the getattr succeeded then update the attrcache accordingly.
7600 * update cache with link file postattrs.
7601 * Note: at this point resop points to link res.
7603 resop = &res.array[3]; /* link res */
7604 ln_res = &resop->nfs_resop4_u.oplink;
7605 if (res.status == NFS4_OK)
7606 e.error = nfs4_update_attrcache(res.status,
7607 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7608 t, svp, cr);
7611 * Call makenfs4node to create the new shadow vp for tnm.
7612 * We pass NULL attrs because we just cached attrs for
7613 * the src object. All we're trying to accomplish is to
7614 * to create the new shadow vnode.
7616 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7617 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7619 /* Update target cache attribute, readdir and dnlc caches */
7620 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7621 dinfo.di_time_call = t;
7622 dinfo.di_cred = cr;
7624 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7625 ASSERT(nfs4_consistent_type(tdvp));
7626 ASSERT(nfs4_consistent_type(svp));
7627 ASSERT(nfs4_consistent_type(nvp));
7628 VN_RELE(nvp);
7630 if (!e.error) {
7631 vnode_t *tvp;
7632 rnode4_t *trp;
7634 * Notify the source file of this link operation.
7636 trp = VTOR4(svp);
7637 tvp = svp;
7638 if (IS_SHADOW(svp, trp))
7639 tvp = RTOV4(trp);
7640 vnevent_link(tvp, ct);
7642 out:
7643 kmem_free(argop, argoplist_size);
7644 if (resp)
7645 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7647 nfs_rw_exit(&tdrp->r_rwlock);
7649 return (e.error);
7652 /* ARGSUSED */
7653 static int
7654 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7655 caller_context_t *ct, int flags)
7657 vnode_t *realvp;
7659 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7660 return (EPERM);
7661 if (fop_realvp(ndvp, &realvp, ct) == 0)
7662 ndvp = realvp;
7664 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7668 * nfs4rename does the real work of renaming in NFS Version 4.
7670 * A file handle is considered volatile for renaming purposes if either
7671 * of the volatile bits are turned on. However, the compound may differ
7672 * based on the likelihood of the filehandle to change during rename.
7674 static int
7675 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7676 caller_context_t *ct)
7678 int error;
7679 mntinfo4_t *mi;
7680 vnode_t *nvp = NULL;
7681 vnode_t *ovp = NULL;
7682 char *tmpname = NULL;
7683 rnode4_t *rp;
7684 rnode4_t *odrp;
7685 rnode4_t *ndrp;
7686 int did_link = 0;
7687 int do_link = 1;
7688 nfsstat4 stat = NFS4_OK;
7690 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7691 ASSERT(nfs4_consistent_type(odvp));
7692 ASSERT(nfs4_consistent_type(ndvp));
7694 if (onm[0] == '.' && (onm[1] == '\0' ||
7695 (onm[1] == '.' && onm[2] == '\0')))
7696 return (EINVAL);
7698 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7699 (nnm[1] == '.' && nnm[2] == '\0')))
7700 return (EINVAL);
7702 odrp = VTOR4(odvp);
7703 ndrp = VTOR4(ndvp);
7704 if ((intptr_t)odrp < (intptr_t)ndrp) {
7705 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7706 return (EINTR);
7707 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7708 nfs_rw_exit(&odrp->r_rwlock);
7709 return (EINTR);
7711 } else {
7712 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7713 return (EINTR);
7714 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7715 nfs_rw_exit(&ndrp->r_rwlock);
7716 return (EINTR);
7721 * Lookup the target file. If it exists, it needs to be
7722 * checked to see whether it is a mount point and whether
7723 * it is active (open).
7725 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7726 if (!error) {
7727 int isactive;
7729 ASSERT(nfs4_consistent_type(nvp));
7731 * If this file has been mounted on, then just
7732 * return busy because renaming to it would remove
7733 * the mounted file system from the name space.
7735 if (vn_ismntpt(nvp)) {
7736 VN_RELE(nvp);
7737 nfs_rw_exit(&odrp->r_rwlock);
7738 nfs_rw_exit(&ndrp->r_rwlock);
7739 return (EBUSY);
7743 * First just remove the entry from the name cache, as it
7744 * is most likely the only entry for this vp.
7746 dnlc_remove(ndvp, nnm);
7748 rp = VTOR4(nvp);
7750 if (nvp->v_type != VREG) {
7752 * Purge the name cache of all references to this vnode
7753 * so that we can check the reference count to infer
7754 * whether it is active or not.
7756 if (nvp->v_count > 1)
7757 dnlc_purge_vp(nvp);
7759 isactive = nvp->v_count > 1;
7760 } else {
7761 mutex_enter(&rp->r_os_lock);
7762 isactive = list_head(&rp->r_open_streams) != NULL;
7763 mutex_exit(&rp->r_os_lock);
7767 * If the vnode is active and is not a directory,
7768 * arrange to rename it to a
7769 * temporary file so that it will continue to be
7770 * accessible. This implements the "unlink-open-file"
7771 * semantics for the target of a rename operation.
7772 * Before doing this though, make sure that the
7773 * source and target files are not already the same.
7775 if (isactive && nvp->v_type != VDIR) {
7777 * Lookup the source name.
7779 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7782 * The source name *should* already exist.
7784 if (error) {
7785 VN_RELE(nvp);
7786 nfs_rw_exit(&odrp->r_rwlock);
7787 nfs_rw_exit(&ndrp->r_rwlock);
7788 return (error);
7791 ASSERT(nfs4_consistent_type(ovp));
7794 * Compare the two vnodes. If they are the same,
7795 * just release all held vnodes and return success.
7797 if (VN_CMP(ovp, nvp)) {
7798 VN_RELE(ovp);
7799 VN_RELE(nvp);
7800 nfs_rw_exit(&odrp->r_rwlock);
7801 nfs_rw_exit(&ndrp->r_rwlock);
7802 return (0);
7806 * Can't mix and match directories and non-
7807 * directories in rename operations. We already
7808 * know that the target is not a directory. If
7809 * the source is a directory, return an error.
7811 if (ovp->v_type == VDIR) {
7812 VN_RELE(ovp);
7813 VN_RELE(nvp);
7814 nfs_rw_exit(&odrp->r_rwlock);
7815 nfs_rw_exit(&ndrp->r_rwlock);
7816 return (ENOTDIR);
7818 link_call:
7820 * The target file exists, is not the same as
7821 * the source file, and is active. We first
7822 * try to Link it to a temporary filename to
7823 * avoid having the server removing the file
7824 * completely (which could cause data loss to
7825 * the user's POV in the event the Rename fails
7826 * -- see bug 1165874).
7829 * The do_link and did_link booleans are
7830 * introduced in the event we get NFS4ERR_FILE_OPEN
7831 * returned for the Rename. Some servers can
7832 * not Rename over an Open file, so they return
7833 * this error. The client needs to Remove the
7834 * newly created Link and do two Renames, just
7835 * as if the server didn't support LINK.
7837 tmpname = newname();
7838 error = 0;
7840 if (do_link) {
7841 error = nfs4_link(ndvp, nvp, tmpname, cr,
7842 NULL, 0);
7844 if (error == EOPNOTSUPP || !do_link) {
7845 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7846 cr, NULL, 0);
7847 did_link = 0;
7848 } else {
7849 did_link = 1;
7851 if (error) {
7852 kmem_free(tmpname, MAXNAMELEN);
7853 VN_RELE(ovp);
7854 VN_RELE(nvp);
7855 nfs_rw_exit(&odrp->r_rwlock);
7856 nfs_rw_exit(&ndrp->r_rwlock);
7857 return (error);
7860 mutex_enter(&rp->r_statelock);
7861 if (rp->r_unldvp == NULL) {
7862 VN_HOLD(ndvp);
7863 rp->r_unldvp = ndvp;
7864 if (rp->r_unlcred != NULL)
7865 crfree(rp->r_unlcred);
7866 crhold(cr);
7867 rp->r_unlcred = cr;
7868 rp->r_unlname = tmpname;
7869 } else {
7870 if (rp->r_unlname)
7871 kmem_free(rp->r_unlname, MAXNAMELEN);
7872 rp->r_unlname = tmpname;
7874 mutex_exit(&rp->r_statelock);
7877 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7879 ASSERT(nfs4_consistent_type(nvp));
7882 if (ovp == NULL) {
7884 * When renaming directories to be a subdirectory of a
7885 * different parent, the dnlc entry for ".." will no
7886 * longer be valid, so it must be removed.
7888 * We do a lookup here to determine whether we are renaming
7889 * a directory and we need to check if we are renaming
7890 * an unlinked file. This might have already been done
7891 * in previous code, so we check ovp == NULL to avoid
7892 * doing it twice.
7894 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7896 * The source name *should* already exist.
7898 if (error) {
7899 nfs_rw_exit(&odrp->r_rwlock);
7900 nfs_rw_exit(&ndrp->r_rwlock);
7901 if (nvp) {
7902 VN_RELE(nvp);
7904 return (error);
7906 ASSERT(ovp != NULL);
7907 ASSERT(nfs4_consistent_type(ovp));
7911 * Is the object being renamed a dir, and if so, is
7912 * it being renamed to a child of itself? The underlying
7913 * fs should ultimately return EINVAL for this case;
7914 * however, buggy beta non-Solaris NFSv4 servers at
7915 * interop testing events have allowed this behavior,
7916 * and it caused our client to panic due to a recursive
7917 * mutex_enter in fn_move.
7919 * The tedious locking in fn_move could be changed to
7920 * deal with this case, and the client could avoid the
7921 * panic; however, the client would just confuse itself
7922 * later and misbehave. A better way to handle the broken
7923 * server is to detect this condition and return EINVAL
7924 * without ever sending the the bogus rename to the server.
7925 * We know the rename is invalid -- just fail it now.
7927 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7928 VN_RELE(ovp);
7929 nfs_rw_exit(&odrp->r_rwlock);
7930 nfs_rw_exit(&ndrp->r_rwlock);
7931 if (nvp) {
7932 VN_RELE(nvp);
7934 return (EINVAL);
7937 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7940 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7941 * possible for the filehandle to change due to the rename.
7942 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7943 * the fh will not change because of the rename, but we still need
7944 * to update its rnode entry with the new name for
7945 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7946 * has no effect on these for now, but for future improvements,
7947 * we might want to use it too to simplify handling of files
7948 * that are open with that flag on. (XXX)
7950 mi = VTOMI4(odvp);
7951 if (NFS4_VOLATILE_FH(mi))
7952 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7953 &stat);
7954 else
7955 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7956 &stat);
7958 ASSERT(nfs4_consistent_type(odvp));
7959 ASSERT(nfs4_consistent_type(ndvp));
7960 ASSERT(nfs4_consistent_type(ovp));
7962 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7963 do_link = 0;
7965 * Before the 'link_call' code, we did a nfs4_lookup
7966 * that puts a VN_HOLD on nvp. After the nfs4_link
7967 * call we call VN_RELE to match that hold. We need
7968 * to place an additional VN_HOLD here since we will
7969 * be hitting that VN_RELE again.
7971 VN_HOLD(nvp);
7973 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
7975 /* Undo the unlinked file naming stuff we just did */
7976 mutex_enter(&rp->r_statelock);
7977 if (rp->r_unldvp) {
7978 VN_RELE(ndvp);
7979 rp->r_unldvp = NULL;
7980 if (rp->r_unlcred != NULL)
7981 crfree(rp->r_unlcred);
7982 rp->r_unlcred = NULL;
7983 /* rp->r_unlanme points to tmpname */
7984 if (rp->r_unlname)
7985 kmem_free(rp->r_unlname, MAXNAMELEN);
7986 rp->r_unlname = NULL;
7988 mutex_exit(&rp->r_statelock);
7990 if (nvp) {
7991 VN_RELE(nvp);
7993 goto link_call;
7996 if (error) {
7997 VN_RELE(ovp);
7998 nfs_rw_exit(&odrp->r_rwlock);
7999 nfs_rw_exit(&ndrp->r_rwlock);
8000 if (nvp) {
8001 VN_RELE(nvp);
8003 return (error);
8007 * when renaming directories to be a subdirectory of a
8008 * different parent, the dnlc entry for ".." will no
8009 * longer be valid, so it must be removed
8011 rp = VTOR4(ovp);
8012 if (ndvp != odvp) {
8013 if (ovp->v_type == VDIR) {
8014 dnlc_remove(ovp, "..");
8015 if (rp->r_dir != NULL)
8016 nfs4_purge_rddir_cache(ovp);
8021 * If we are renaming the unlinked file, update the
8022 * r_unldvp and r_unlname as needed.
8024 mutex_enter(&rp->r_statelock);
8025 if (rp->r_unldvp != NULL) {
8026 if (strcmp(rp->r_unlname, onm) == 0) {
8027 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8028 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8029 if (ndvp != rp->r_unldvp) {
8030 VN_RELE(rp->r_unldvp);
8031 rp->r_unldvp = ndvp;
8032 VN_HOLD(ndvp);
8036 mutex_exit(&rp->r_statelock);
8039 * Notify the rename vnevents to source vnode, and to the target
8040 * vnode if it already existed.
8042 if (error == 0) {
8043 vnode_t *tvp;
8044 rnode4_t *trp;
8046 * Notify the vnode. Each links is represented by
8047 * a different vnode, in nfsv4.
8049 if (nvp) {
8050 trp = VTOR4(nvp);
8051 tvp = nvp;
8052 if (IS_SHADOW(nvp, trp))
8053 tvp = RTOV4(trp);
8054 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8058 * if the source and destination directory are not the
8059 * same notify the destination directory.
8061 if (VTOR4(odvp) != VTOR4(ndvp)) {
8062 trp = VTOR4(ndvp);
8063 tvp = ndvp;
8064 if (IS_SHADOW(ndvp, trp))
8065 tvp = RTOV4(trp);
8066 vnevent_rename_dest_dir(tvp, ct);
8069 trp = VTOR4(ovp);
8070 tvp = ovp;
8071 if (IS_SHADOW(ovp, trp))
8072 tvp = RTOV4(trp);
8073 vnevent_rename_src(tvp, odvp, onm, ct);
8076 if (nvp) {
8077 VN_RELE(nvp);
8079 VN_RELE(ovp);
8081 nfs_rw_exit(&odrp->r_rwlock);
8082 nfs_rw_exit(&ndrp->r_rwlock);
8084 return (error);
8088 * When the parent directory has changed, sv_dfh must be updated
8090 static void
8091 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8093 svnode_t *sv = VTOSV(vp);
8094 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8095 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8097 sfh4_hold(new_dfh);
8098 sv->sv_dfh = new_dfh;
8099 sfh4_rele(&old_dfh);
8103 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8104 * when it is known that the filehandle is persistent through rename.
8106 * Rename requires that the current fh be the target directory and the
8107 * saved fh be the source directory. After the operation, the current fh
8108 * is unchanged.
8109 * The compound op structure for persistent fh rename is:
8110 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8111 * Rather than bother with the directory postop args, we'll simply
8112 * update that a change occurred in the cache, so no post-op getattrs.
8114 static int
8115 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8116 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8118 COMPOUND4args_clnt args;
8119 COMPOUND4res_clnt res, *resp = NULL;
8120 nfs_argop4 *argop;
8121 nfs_resop4 *resop;
8122 int doqueue, argoplist_size;
8123 mntinfo4_t *mi;
8124 rnode4_t *odrp = VTOR4(odvp);
8125 rnode4_t *ndrp = VTOR4(ndvp);
8126 RENAME4res *rn_res;
8127 bool_t needrecov;
8128 nfs4_recov_state_t recov_state;
8129 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8130 dirattr_info_t dinfo, *dinfop;
8132 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8134 recov_state.rs_flags = 0;
8135 recov_state.rs_num_retry_despite_err = 0;
8138 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8140 * If source/target are different dirs, then append putfh(src); getattr
8142 args.array_len = (odvp == ndvp) ? 5 : 7;
8143 argoplist_size = args.array_len * sizeof (nfs_argop4);
8144 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8146 recov_retry:
8147 *statp = NFS4_OK;
8149 /* No need to Lookup the file, persistent fh */
8150 args.ctag = TAG_RENAME;
8152 mi = VTOMI4(odvp);
8153 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8154 if (e.error) {
8155 kmem_free(argop, argoplist_size);
8156 return (e.error);
8159 /* 0: putfh source directory */
8160 argop[0].argop = OP_CPUTFH;
8161 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8163 /* 1: Save source fh to free up current for target */
8164 argop[1].argop = OP_SAVEFH;
8166 /* 2: putfh targetdir */
8167 argop[2].argop = OP_CPUTFH;
8168 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8170 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8171 argop[3].argop = OP_CRENAME;
8172 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8173 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8175 /* 4: getattr (targetdir) */
8176 argop[4].argop = OP_GETATTR;
8177 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8178 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8180 if (ndvp != odvp) {
8182 /* 5: putfh (sourcedir) */
8183 argop[5].argop = OP_CPUTFH;
8184 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8186 /* 6: getattr (sourcedir) */
8187 argop[6].argop = OP_GETATTR;
8188 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8189 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8192 dnlc_remove(odvp, onm);
8193 dnlc_remove(ndvp, nnm);
8195 doqueue = 1;
8196 dinfo.di_time_call = gethrtime();
8197 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8199 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8200 if (e.error) {
8201 PURGE_ATTRCACHE4(odvp);
8202 PURGE_ATTRCACHE4(ndvp);
8203 } else {
8204 *statp = res.status;
8207 if (needrecov) {
8208 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8209 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8210 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8211 if (!e.error)
8212 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8213 goto recov_retry;
8217 if (!e.error) {
8218 resp = &res;
8220 * as long as OP_RENAME
8222 if (res.status != NFS4_OK && res.array_len <= 4) {
8223 e.error = geterrno4(res.status);
8224 PURGE_ATTRCACHE4(odvp);
8225 PURGE_ATTRCACHE4(ndvp);
8227 * System V defines rename to return EEXIST, not
8228 * ENOTEMPTY if the target directory is not empty.
8229 * Over the wire, the error is NFSERR_ENOTEMPTY
8230 * which geterrno4 maps to ENOTEMPTY.
8232 if (e.error == ENOTEMPTY)
8233 e.error = EEXIST;
8234 } else {
8236 resop = &res.array[3]; /* rename res */
8237 rn_res = &resop->nfs_resop4_u.oprename;
8239 if (res.status == NFS4_OK) {
8241 * Update target attribute, readdir and dnlc
8242 * caches.
8244 dinfo.di_garp =
8245 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8246 dinfo.di_cred = cr;
8247 dinfop = &dinfo;
8248 } else
8249 dinfop = NULL;
8251 nfs4_update_dircaches(&rn_res->target_cinfo,
8252 ndvp, NULL, NULL, dinfop);
8255 * Update source attribute, readdir and dnlc caches
8258 if (ndvp != odvp) {
8259 update_parentdir_sfh(renvp, ndvp);
8261 if (dinfop)
8262 dinfo.di_garp =
8263 &(res.array[6].nfs_resop4_u.
8264 opgetattr.ga_res);
8266 nfs4_update_dircaches(&rn_res->source_cinfo,
8267 odvp, NULL, NULL, dinfop);
8270 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8271 nnm);
8275 if (resp)
8276 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8277 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8278 kmem_free(argop, argoplist_size);
8280 return (e.error);
8284 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8285 * it is possible for the filehandle to change due to the rename.
8287 * The compound req in this case includes a post-rename lookup and getattr
8288 * to ensure that we have the correct fh and attributes for the object.
8290 * Rename requires that the current fh be the target directory and the
8291 * saved fh be the source directory. After the operation, the current fh
8292 * is unchanged.
8294 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8295 * update the filehandle for the renamed object. We also get the old
8296 * filehandle for historical reasons; this should be taken out sometime.
8297 * This results in a rather cumbersome compound...
8299 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8300 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8303 static int
8304 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8305 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8307 COMPOUND4args_clnt args;
8308 COMPOUND4res_clnt res, *resp = NULL;
8309 int argoplist_size;
8310 nfs_argop4 *argop;
8311 nfs_resop4 *resop;
8312 int doqueue;
8313 mntinfo4_t *mi;
8314 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8315 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8316 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8317 RENAME4res *rn_res;
8318 GETFH4res *ngf_res;
8319 bool_t needrecov;
8320 nfs4_recov_state_t recov_state;
8321 hrtime_t t;
8322 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8323 dirattr_info_t dinfo, *dinfop = &dinfo;
8325 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8327 recov_state.rs_flags = 0;
8328 recov_state.rs_num_retry_despite_err = 0;
8330 recov_retry:
8331 *statp = NFS4_OK;
8334 * There is a window between the RPC and updating the path and
8335 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8336 * code, so that it doesn't try to use the old path during that
8337 * window.
8339 mutex_enter(&orp->r_statelock);
8340 while (orp->r_flags & R4RECEXPFH) {
8341 klwp_t *lwp = ttolwp(curthread);
8343 if (lwp != NULL)
8344 lwp->lwp_nostop++;
8345 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8346 mutex_exit(&orp->r_statelock);
8347 if (lwp != NULL)
8348 lwp->lwp_nostop--;
8349 return (EINTR);
8351 if (lwp != NULL)
8352 lwp->lwp_nostop--;
8354 orp->r_flags |= R4RECEXPFH;
8355 mutex_exit(&orp->r_statelock);
8357 mi = VTOMI4(odvp);
8359 args.ctag = TAG_RENAME_VFH;
8360 args.array_len = (odvp == ndvp) ? 10 : 12;
8361 argoplist_size = args.array_len * sizeof (nfs_argop4);
8362 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8365 * Rename ops:
8366 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8367 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8368 * LOOKUP(trgt), GETFH(new), GETATTR,
8370 * if (odvp != ndvp)
8371 * add putfh(sourcedir), getattr(sourcedir) }
8373 args.array = argop;
8375 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8376 &recov_state, NULL);
8377 if (e.error) {
8378 kmem_free(argop, argoplist_size);
8379 mutex_enter(&orp->r_statelock);
8380 orp->r_flags &= ~R4RECEXPFH;
8381 cv_broadcast(&orp->r_cv);
8382 mutex_exit(&orp->r_statelock);
8383 return (e.error);
8386 /* 0: putfh source directory */
8387 argop[0].argop = OP_CPUTFH;
8388 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8390 /* 1: Save source fh to free up current for target */
8391 argop[1].argop = OP_SAVEFH;
8393 /* 2: Lookup pre-rename fh of renamed object */
8394 argop[2].argop = OP_CLOOKUP;
8395 argop[2].nfs_argop4_u.opclookup.cname = onm;
8397 /* 3: getfh fh of renamed object (before rename) */
8398 argop[3].argop = OP_GETFH;
8400 /* 4: putfh targetdir */
8401 argop[4].argop = OP_CPUTFH;
8402 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8404 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8405 argop[5].argop = OP_CRENAME;
8406 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8407 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8409 /* 6: getattr of target dir (post op attrs) */
8410 argop[6].argop = OP_GETATTR;
8411 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8412 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8414 /* 7: Lookup post-rename fh of renamed object */
8415 argop[7].argop = OP_CLOOKUP;
8416 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8418 /* 8: getfh fh of renamed object (after rename) */
8419 argop[8].argop = OP_GETFH;
8421 /* 9: getattr of renamed object */
8422 argop[9].argop = OP_GETATTR;
8423 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8424 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8427 * If source/target dirs are different, then get new post-op
8428 * attrs for source dir also.
8430 if (ndvp != odvp) {
8431 /* 10: putfh (sourcedir) */
8432 argop[10].argop = OP_CPUTFH;
8433 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8435 /* 11: getattr (sourcedir) */
8436 argop[11].argop = OP_GETATTR;
8437 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8438 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8441 dnlc_remove(odvp, onm);
8442 dnlc_remove(ndvp, nnm);
8444 doqueue = 1;
8445 t = gethrtime();
8446 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8448 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8449 if (e.error) {
8450 PURGE_ATTRCACHE4(odvp);
8451 PURGE_ATTRCACHE4(ndvp);
8452 if (!needrecov) {
8453 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8454 &recov_state, needrecov);
8455 goto out;
8457 } else {
8458 *statp = res.status;
8461 if (needrecov) {
8462 bool_t abort;
8464 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8465 OP_RENAME, NULL, NULL, NULL);
8466 if (abort == FALSE) {
8467 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8468 &recov_state, needrecov);
8469 kmem_free(argop, argoplist_size);
8470 if (!e.error)
8471 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8472 mutex_enter(&orp->r_statelock);
8473 orp->r_flags &= ~R4RECEXPFH;
8474 cv_broadcast(&orp->r_cv);
8475 mutex_exit(&orp->r_statelock);
8476 goto recov_retry;
8477 } else {
8478 if (e.error != 0) {
8479 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8480 &recov_state, needrecov);
8481 goto out;
8483 /* fall through for res.status case */
8487 resp = &res;
8489 * If OP_RENAME (or any prev op) failed, then return an error.
8490 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8492 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8494 * Error in an op other than last Getattr
8496 e.error = geterrno4(res.status);
8497 PURGE_ATTRCACHE4(odvp);
8498 PURGE_ATTRCACHE4(ndvp);
8500 * System V defines rename to return EEXIST, not
8501 * ENOTEMPTY if the target directory is not empty.
8502 * Over the wire, the error is NFSERR_ENOTEMPTY
8503 * which geterrno4 maps to ENOTEMPTY.
8505 if (e.error == ENOTEMPTY)
8506 e.error = EEXIST;
8507 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8508 needrecov);
8509 goto out;
8512 /* rename results */
8513 rn_res = &res.array[5].nfs_resop4_u.oprename;
8515 if (res.status == NFS4_OK) {
8516 /* Update target attribute, readdir and dnlc caches */
8517 dinfo.di_garp =
8518 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8519 dinfo.di_cred = cr;
8520 dinfo.di_time_call = t;
8521 } else
8522 dinfop = NULL;
8524 /* Update source cache attribute, readdir and dnlc caches */
8525 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8527 /* Update source cache attribute, readdir and dnlc caches */
8528 if (ndvp != odvp) {
8529 update_parentdir_sfh(ovp, ndvp);
8532 * If dinfop is non-NULL, then compound succeded, so
8533 * set di_garp to attrs for source dir. dinfop is only
8534 * set to NULL when compound fails.
8536 if (dinfop)
8537 dinfo.di_garp =
8538 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8539 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8540 dinfop);
8544 * Update the rnode with the new component name and args,
8545 * and if the file handle changed, also update it with the new fh.
8546 * This is only necessary if the target object has an rnode
8547 * entry and there is no need to create one for it.
8549 resop = &res.array[8]; /* getfh new res */
8550 ngf_res = &resop->nfs_resop4_u.opgetfh;
8553 * Update the path and filehandle for the renamed object.
8555 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8557 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8559 if (res.status == NFS4_OK) {
8560 resop++; /* getattr res */
8561 e.error = nfs4_update_attrcache(res.status,
8562 &resop->nfs_resop4_u.opgetattr.ga_res,
8563 t, ovp, cr);
8566 out:
8567 kmem_free(argop, argoplist_size);
8568 if (resp)
8569 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8570 mutex_enter(&orp->r_statelock);
8571 orp->r_flags &= ~R4RECEXPFH;
8572 cv_broadcast(&orp->r_cv);
8573 mutex_exit(&orp->r_statelock);
8575 return (e.error);
8578 /* ARGSUSED */
8579 static int
8580 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8581 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8583 int error;
8584 vnode_t *vp;
8586 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8587 return (EPERM);
8589 * As ".." has special meaning and rather than send a mkdir
8590 * over the wire to just let the server freak out, we just
8591 * short circuit it here and return EEXIST
8593 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8594 return (EEXIST);
8597 * Decision to get the right gid and setgid bit of the
8598 * new directory is now made in call_nfs4_create_req.
8600 va->va_mask |= VATTR_MODE;
8601 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8602 if (error)
8603 return (error);
8605 *vpp = vp;
8606 return (0);
8611 * rmdir is using the same remove v4 op as does remove.
8612 * Remove requires that the current fh be the target directory.
8613 * After the operation, the current fh is unchanged.
8614 * The compound op structure is:
8615 * PUTFH(targetdir), REMOVE
8617 /*ARGSUSED4*/
8618 static int
8619 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8620 caller_context_t *ct, int flags)
8622 int need_end_op = FALSE;
8623 COMPOUND4args_clnt args;
8624 COMPOUND4res_clnt res, *resp = NULL;
8625 REMOVE4res *rm_res;
8626 nfs_argop4 argop[3];
8627 nfs_resop4 *resop;
8628 vnode_t *vp;
8629 int doqueue;
8630 mntinfo4_t *mi;
8631 rnode4_t *drp;
8632 bool_t needrecov = FALSE;
8633 nfs4_recov_state_t recov_state;
8634 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8635 dirattr_info_t dinfo, *dinfop;
8637 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8638 return (EPERM);
8640 * As ".." has special meaning and rather than send a rmdir
8641 * over the wire to just let the server freak out, we just
8642 * short circuit it here and return EEXIST
8644 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8645 return (EEXIST);
8647 drp = VTOR4(dvp);
8648 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8649 return (EINTR);
8652 * Attempt to prevent a rmdir(".") from succeeding.
8654 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8655 if (e.error) {
8656 nfs_rw_exit(&drp->r_rwlock);
8657 return (e.error);
8659 if (vp == cdir) {
8660 VN_RELE(vp);
8661 nfs_rw_exit(&drp->r_rwlock);
8662 return (EINVAL);
8666 * Since nfsv4 remove op works on both files and directories,
8667 * check that the removed object is indeed a directory.
8669 if (vp->v_type != VDIR) {
8670 VN_RELE(vp);
8671 nfs_rw_exit(&drp->r_rwlock);
8672 return (ENOTDIR);
8676 * First just remove the entry from the name cache, as it
8677 * is most likely an entry for this vp.
8679 dnlc_remove(dvp, nm);
8682 * If there vnode reference count is greater than one, then
8683 * there may be additional references in the DNLC which will
8684 * need to be purged. First, trying removing the entry for
8685 * the parent directory and see if that removes the additional
8686 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8687 * to completely remove any references to the directory which
8688 * might still exist in the DNLC.
8690 if (vp->v_count > 1) {
8691 dnlc_remove(vp, "..");
8692 if (vp->v_count > 1)
8693 dnlc_purge_vp(vp);
8696 mi = VTOMI4(dvp);
8697 recov_state.rs_flags = 0;
8698 recov_state.rs_num_retry_despite_err = 0;
8700 recov_retry:
8701 args.ctag = TAG_RMDIR;
8704 * Rmdir ops: putfh dir; remove
8706 args.array_len = 3;
8707 args.array = argop;
8709 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8710 if (e.error) {
8711 nfs_rw_exit(&drp->r_rwlock);
8712 return (e.error);
8714 need_end_op = TRUE;
8716 /* putfh directory */
8717 argop[0].argop = OP_CPUTFH;
8718 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8720 /* remove */
8721 argop[1].argop = OP_CREMOVE;
8722 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8724 /* getattr (postop attrs for dir that contained removed dir) */
8725 argop[2].argop = OP_GETATTR;
8726 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8727 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8729 dinfo.di_time_call = gethrtime();
8730 doqueue = 1;
8731 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8733 PURGE_ATTRCACHE4(vp);
8735 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8736 if (e.error) {
8737 PURGE_ATTRCACHE4(dvp);
8740 if (needrecov) {
8741 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8742 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8743 if (!e.error)
8744 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8746 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8747 needrecov);
8748 need_end_op = FALSE;
8749 goto recov_retry;
8753 if (!e.error) {
8754 resp = &res;
8757 * Only return error if first 2 ops (OP_REMOVE or earlier)
8758 * failed.
8760 if (res.status != NFS4_OK && res.array_len <= 2) {
8761 e.error = geterrno4(res.status);
8762 PURGE_ATTRCACHE4(dvp);
8763 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8764 &recov_state, needrecov);
8765 need_end_op = FALSE;
8766 nfs4_purge_stale_fh(e.error, dvp, cr);
8768 * System V defines rmdir to return EEXIST, not
8769 * ENOTEMPTY if the directory is not empty. Over
8770 * the wire, the error is NFSERR_ENOTEMPTY which
8771 * geterrno4 maps to ENOTEMPTY.
8773 if (e.error == ENOTEMPTY)
8774 e.error = EEXIST;
8775 } else {
8776 resop = &res.array[1]; /* remove res */
8777 rm_res = &resop->nfs_resop4_u.opremove;
8779 if (res.status == NFS4_OK) {
8780 resop = &res.array[2]; /* dir attrs */
8781 dinfo.di_garp =
8782 &resop->nfs_resop4_u.opgetattr.ga_res;
8783 dinfo.di_cred = cr;
8784 dinfop = &dinfo;
8785 } else
8786 dinfop = NULL;
8788 /* Update dir attribute, readdir and dnlc caches */
8789 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8790 dinfop);
8792 /* destroy rddir cache for dir that was removed */
8793 if (VTOR4(vp)->r_dir != NULL)
8794 nfs4_purge_rddir_cache(vp);
8798 if (need_end_op)
8799 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8801 nfs_rw_exit(&drp->r_rwlock);
8803 if (resp)
8804 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8806 if (e.error == 0) {
8807 vnode_t *tvp;
8808 rnode4_t *trp;
8809 trp = VTOR4(vp);
8810 tvp = vp;
8811 if (IS_SHADOW(vp, trp))
8812 tvp = RTOV4(trp);
8813 vnevent_rmdir(tvp, dvp, nm, ct);
8816 VN_RELE(vp);
8818 return (e.error);
8821 /* ARGSUSED */
8822 static int
8823 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8824 caller_context_t *ct, int flags)
8826 int error;
8827 vnode_t *vp;
8828 rnode4_t *rp;
8829 char *contents;
8830 mntinfo4_t *mi = VTOMI4(dvp);
8832 if (nfs_zone() != mi->mi_zone)
8833 return (EPERM);
8834 if (!(mi->mi_flags & MI4_SYMLINK))
8835 return (EOPNOTSUPP);
8837 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8838 if (error)
8839 return (error);
8841 ASSERT(nfs4_consistent_type(vp));
8842 rp = VTOR4(vp);
8843 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8845 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8847 if (contents != NULL) {
8848 mutex_enter(&rp->r_statelock);
8849 if (rp->r_symlink.contents == NULL) {
8850 rp->r_symlink.len = strlen(tnm);
8851 bcopy(tnm, contents, rp->r_symlink.len);
8852 rp->r_symlink.contents = contents;
8853 rp->r_symlink.size = MAXPATHLEN;
8854 mutex_exit(&rp->r_statelock);
8855 } else {
8856 mutex_exit(&rp->r_statelock);
8857 kmem_free((void *)contents, MAXPATHLEN);
8861 VN_RELE(vp);
8863 return (error);
8868 * Read directory entries.
8869 * There are some weird things to look out for here. The uio_loffset
8870 * field is either 0 or it is the offset returned from a previous
8871 * readdir. It is an opaque value used by the server to find the
8872 * correct directory block to read. The count field is the number
8873 * of blocks to read on the server. This is advisory only, the server
8874 * may return only one block's worth of entries. Entries may be compressed
8875 * on the server.
8877 /* ARGSUSED */
8878 static int
8879 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8880 caller_context_t *ct, int flags)
8882 int error;
8883 uint_t count;
8884 rnode4_t *rp;
8885 rddir4_cache *rdc;
8886 rddir4_cache *rrdc;
8888 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8889 return (EIO);
8890 rp = VTOR4(vp);
8892 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8895 * Make sure that the directory cache is valid.
8897 if (rp->r_dir != NULL) {
8898 if (nfs_disable_rddir_cache != 0) {
8900 * Setting nfs_disable_rddir_cache in /etc/system
8901 * allows interoperability with servers that do not
8902 * properly update the attributes of directories.
8903 * Any cached information gets purged before an
8904 * access is made to it.
8906 nfs4_purge_rddir_cache(vp);
8909 error = nfs4_validate_caches(vp, cr);
8910 if (error)
8911 return (error);
8914 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8917 * Short circuit last readdir which always returns 0 bytes.
8918 * This can be done after the directory has been read through
8919 * completely at least once. This will set r_direof which
8920 * can be used to find the value of the last cookie.
8922 mutex_enter(&rp->r_statelock);
8923 if (rp->r_direof != NULL &&
8924 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8925 mutex_exit(&rp->r_statelock);
8926 #ifdef DEBUG
8927 nfs4_readdir_cache_shorts++;
8928 #endif
8929 if (eofp)
8930 *eofp = 1;
8931 return (0);
8935 * Look for a cache entry. Cache entries are identified
8936 * by the NFS cookie value and the byte count requested.
8938 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8941 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8943 if (rdc == NULL) {
8944 mutex_exit(&rp->r_statelock);
8945 return (EINTR);
8949 * Check to see if we need to fill this entry in.
8951 if (rdc->flags & RDDIRREQ) {
8952 rdc->flags &= ~RDDIRREQ;
8953 rdc->flags |= RDDIR;
8954 mutex_exit(&rp->r_statelock);
8957 * Do the readdir.
8959 nfs4readdir(vp, rdc, cr);
8962 * Reacquire the lock, so that we can continue
8964 mutex_enter(&rp->r_statelock);
8966 * The entry is now complete
8968 rdc->flags &= ~RDDIR;
8971 ASSERT(!(rdc->flags & RDDIR));
8974 * If an error occurred while attempting
8975 * to fill the cache entry, mark the entry invalid and
8976 * just return the error.
8978 if (rdc->error) {
8979 error = rdc->error;
8980 rdc->flags |= RDDIRREQ;
8981 rddir4_cache_rele(rp, rdc);
8982 mutex_exit(&rp->r_statelock);
8983 return (error);
8987 * The cache entry is complete and good,
8988 * copyout the dirent structs to the calling
8989 * thread.
8991 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
8994 * If no error occurred during the copyout,
8995 * update the offset in the uio struct to
8996 * contain the value of the next NFS 4 cookie
8997 * and set the eof value appropriately.
8999 if (!error) {
9000 uiop->uio_loffset = rdc->nfs4_ncookie;
9001 if (eofp)
9002 *eofp = rdc->eof;
9006 * Decide whether to do readahead. Don't if we
9007 * have already read to the end of directory.
9009 if (rdc->eof) {
9011 * Make the entry the direof only if it is cached
9013 if (rdc->flags & RDDIRCACHED)
9014 rp->r_direof = rdc;
9015 rddir4_cache_rele(rp, rdc);
9016 mutex_exit(&rp->r_statelock);
9017 return (error);
9020 /* Determine if a readdir readahead should be done */
9021 if (!(rp->r_flags & R4LOOKUP)) {
9022 rddir4_cache_rele(rp, rdc);
9023 mutex_exit(&rp->r_statelock);
9024 return (error);
9028 * Now look for a readahead entry.
9030 * Check to see whether we found an entry for the readahead.
9031 * If so, we don't need to do anything further, so free the new
9032 * entry if one was allocated. Otherwise, allocate a new entry, add
9033 * it to the cache, and then initiate an asynchronous readdir
9034 * operation to fill it.
9036 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9039 * A readdir cache entry could not be obtained for the readahead. In
9040 * this case we skip the readahead and return.
9042 if (rrdc == NULL) {
9043 rddir4_cache_rele(rp, rdc);
9044 mutex_exit(&rp->r_statelock);
9045 return (error);
9049 * Check to see if we need to fill this entry in.
9051 if (rrdc->flags & RDDIRREQ) {
9052 rrdc->flags &= ~RDDIRREQ;
9053 rrdc->flags |= RDDIR;
9054 rddir4_cache_rele(rp, rdc);
9055 mutex_exit(&rp->r_statelock);
9056 #ifdef DEBUG
9057 nfs4_readdir_readahead++;
9058 #endif
9060 * Do the readdir.
9062 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9063 return (error);
9066 rddir4_cache_rele(rp, rrdc);
9067 rddir4_cache_rele(rp, rdc);
9068 mutex_exit(&rp->r_statelock);
9069 return (error);
9072 static int
9073 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9075 int error;
9076 rnode4_t *rp;
9078 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9080 rp = VTOR4(vp);
9083 * Obtain the readdir results for the caller.
9085 nfs4readdir(vp, rdc, cr);
9087 mutex_enter(&rp->r_statelock);
9089 * The entry is now complete
9091 rdc->flags &= ~RDDIR;
9093 error = rdc->error;
9094 if (error)
9095 rdc->flags |= RDDIRREQ;
9096 rddir4_cache_rele(rp, rdc);
9097 mutex_exit(&rp->r_statelock);
9099 return (error);
9103 * Read directory entries.
9104 * There are some weird things to look out for here. The uio_loffset
9105 * field is either 0 or it is the offset returned from a previous
9106 * readdir. It is an opaque value used by the server to find the
9107 * correct directory block to read. The count field is the number
9108 * of blocks to read on the server. This is advisory only, the server
9109 * may return only one block's worth of entries. Entries may be compressed
9110 * on the server.
9112 * Generates the following compound request:
9113 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9114 * must include a Lookupp as well. In this case, send:
9115 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9116 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9118 * Get complete attributes and filehandles for entries if this is the
9119 * first read of the directory. Otherwise, just get fileid's.
9121 static void
9122 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9124 COMPOUND4args_clnt args;
9125 COMPOUND4res_clnt res;
9126 READDIR4args *rargs;
9127 READDIR4res_clnt *rd_res;
9128 bitmap4 rd_bitsval;
9129 nfs_argop4 argop[5];
9130 nfs_resop4 *resop;
9131 rnode4_t *rp = VTOR4(vp);
9132 mntinfo4_t *mi = VTOMI4(vp);
9133 int doqueue;
9134 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9135 vnode_t *dvp;
9136 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9137 int num_ops, res_opcnt;
9138 bool_t needrecov = FALSE;
9139 nfs4_recov_state_t recov_state;
9140 hrtime_t t;
9141 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9143 ASSERT(nfs_zone() == mi->mi_zone);
9144 ASSERT(rdc->flags & RDDIR);
9145 ASSERT(rdc->entries == NULL);
9148 * If rp were a stub, it should have triggered and caused
9149 * a mount for us to get this far.
9151 ASSERT(!RP_ISSTUB(rp));
9153 num_ops = 2;
9154 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9156 * Since nfsv4 readdir may not return entries for "." and "..",
9157 * the client must recreate them:
9158 * To find the correct nodeid, do the following:
9159 * For current node, get nodeid from dnlc.
9160 * - if current node is rootvp, set pnodeid to nodeid.
9161 * - else if parent is in the dnlc, get its nodeid from there.
9162 * - else add LOOKUPP+GETATTR to compound.
9164 nodeid = rp->r_attr.va_nodeid;
9165 if (vp->v_flag & VROOT) {
9166 pnodeid = nodeid; /* root of mount point */
9167 } else {
9168 dvp = dnlc_lookup(vp, "..");
9169 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9170 /* parent in dnlc cache - no need for otw */
9171 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9172 } else {
9174 * parent not in dnlc cache,
9175 * do lookupp to get its id
9177 num_ops = 5;
9178 pnodeid = 0; /* set later by getattr parent */
9180 if (dvp)
9181 VN_RELE(dvp);
9184 recov_state.rs_flags = 0;
9185 recov_state.rs_num_retry_despite_err = 0;
9187 /* Save the original mount point security flavor */
9188 (void) save_mnt_secinfo(mi->mi_curr_serv);
9190 recov_retry:
9191 args.ctag = TAG_READDIR;
9193 args.array = argop;
9194 args.array_len = num_ops;
9196 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9197 &recov_state, NULL)) {
9199 * If readdir a node that is a stub for a crossed mount point,
9200 * keep the original secinfo flavor for the current file
9201 * system, not the crossed one.
9203 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9204 rdc->error = e.error;
9205 return;
9209 * Determine which attrs to request for dirents. This code
9210 * must be protected by nfs4_start/end_fop because of r_server
9211 * (which will change during failover recovery).
9214 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9216 * Get all vattr attrs plus filehandle and rdattr_error
9218 rd_bitsval = NFS4_VATTR_MASK |
9219 FATTR4_RDATTR_ERROR_MASK |
9220 FATTR4_FILEHANDLE_MASK;
9222 if (rp->r_flags & R4READDIRWATTR) {
9223 mutex_enter(&rp->r_statelock);
9224 rp->r_flags &= ~R4READDIRWATTR;
9225 mutex_exit(&rp->r_statelock);
9227 } else {
9228 servinfo4_t *svp = rp->r_server;
9231 * Already read directory. Use readdir with
9232 * no attrs (except for mounted_on_fileid) for updates.
9234 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9237 * request mounted on fileid if supported, else request
9238 * fileid. maybe we should verify that fileid is supported
9239 * and request something else if not.
9241 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9242 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9243 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9244 nfs_rw_exit(&svp->sv_lock);
9247 /* putfh directory fh */
9248 argop[0].argop = OP_CPUTFH;
9249 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9251 argop[1].argop = OP_READDIR;
9252 rargs = &argop[1].nfs_argop4_u.opreaddir;
9254 * 1 and 2 are reserved for client "." and ".." entry offset.
9255 * cookie 0 should be used over-the-wire to start reading at
9256 * the beginning of the directory excluding "." and "..".
9258 if (rdc->nfs4_cookie == 0 ||
9259 rdc->nfs4_cookie == 1 ||
9260 rdc->nfs4_cookie == 2) {
9261 rargs->cookie = (nfs_cookie4)0;
9262 rargs->cookieverf = 0;
9263 } else {
9264 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9265 mutex_enter(&rp->r_statelock);
9266 rargs->cookieverf = rp->r_cookieverf4;
9267 mutex_exit(&rp->r_statelock);
9269 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9270 rargs->maxcount = mi->mi_tsize;
9271 rargs->attr_request = rd_bitsval;
9272 rargs->rdc = rdc;
9273 rargs->dvp = vp;
9274 rargs->mi = mi;
9275 rargs->cr = cr;
9279 * If count < than the minimum required, we return no entries
9280 * and fail with EINVAL
9282 if (rargs->dircount < (DIRENT_RECLEN(1) + DIRENT_RECLEN(2))) {
9283 rdc->error = EINVAL;
9284 goto out;
9287 if (args.array_len == 5) {
9289 * Add lookupp and getattr for parent nodeid.
9291 argop[2].argop = OP_LOOKUPP;
9293 argop[3].argop = OP_GETFH;
9295 /* getattr parent */
9296 argop[4].argop = OP_GETATTR;
9297 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9298 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9301 doqueue = 1;
9303 if (mi->mi_io_kstats) {
9304 mutex_enter(&mi->mi_lock);
9305 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9306 mutex_exit(&mi->mi_lock);
9309 /* capture the time of this call */
9310 rargs->t = t = gethrtime();
9312 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9314 if (mi->mi_io_kstats) {
9315 mutex_enter(&mi->mi_lock);
9316 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9317 mutex_exit(&mi->mi_lock);
9320 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9323 * If RPC error occurred and it isn't an error that
9324 * triggers recovery, then go ahead and fail now.
9326 if (e.error != 0 && !needrecov) {
9327 rdc->error = e.error;
9328 goto out;
9331 if (needrecov) {
9332 bool_t abort;
9334 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9335 "nfs4readdir: initiating recovery.\n"));
9337 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9338 NULL, OP_READDIR, NULL, NULL, NULL);
9339 if (abort == FALSE) {
9340 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9341 &recov_state, needrecov);
9342 if (!e.error)
9343 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9344 if (rdc->entries != NULL) {
9345 kmem_free(rdc->entries, rdc->entlen);
9346 rdc->entries = NULL;
9348 goto recov_retry;
9351 if (e.error != 0) {
9352 rdc->error = e.error;
9353 goto out;
9356 /* fall through for res.status case */
9359 res_opcnt = res.array_len;
9362 * If compound failed first 2 ops (PUTFH+READDIR), then return
9363 * failure here. Subsequent ops are for filling out dot-dot
9364 * dirent, and if they fail, we still want to give the caller
9365 * the dirents returned by (the successful) READDIR op, so we need
9366 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9368 * One example where PUTFH+READDIR ops would succeed but
9369 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9370 * but lacks x. In this case, a POSIX server's fop_readdir
9371 * would succeed; however, fop_lookup(..) would fail since no
9372 * x perm. We need to come up with a non-vendor-specific way
9373 * for a POSIX server to return d_ino from dotdot's dirent if
9374 * client only requests mounted_on_fileid, and just say the
9375 * LOOKUPP succeeded and fill out the GETATTR. However, if
9376 * client requested any mandatory attrs, server would be required
9377 * to fail the GETATTR op because it can't call fop_lookup+fop_getattr
9378 * for dotdot.
9381 if (res.status) {
9382 if (res_opcnt <= 2) {
9383 e.error = geterrno4(res.status);
9384 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9385 &recov_state, needrecov);
9386 nfs4_purge_stale_fh(e.error, vp, cr);
9387 rdc->error = e.error;
9388 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9389 if (rdc->entries != NULL) {
9390 kmem_free(rdc->entries, rdc->entlen);
9391 rdc->entries = NULL;
9394 * If readdir a node that is a stub for a
9395 * crossed mount point, keep the original
9396 * secinfo flavor for the current file system,
9397 * not the crossed one.
9399 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9400 return;
9404 resop = &res.array[1]; /* readdir res */
9405 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9407 mutex_enter(&rp->r_statelock);
9408 rp->r_cookieverf4 = rd_res->cookieverf;
9409 mutex_exit(&rp->r_statelock);
9412 * For "." and ".." entries
9413 * e.g.
9414 * seek(cookie=0) -> "." entry with d_off = 1
9415 * seek(cookie=1) -> ".." entry with d_off = 2
9417 if (cookie == (nfs_cookie4) 0) {
9418 if (rd_res->dotp)
9419 rd_res->dotp->d_ino = nodeid;
9420 if (rd_res->dotdotp)
9421 rd_res->dotdotp->d_ino = pnodeid;
9423 if (cookie == (nfs_cookie4) 1) {
9424 if (rd_res->dotdotp)
9425 rd_res->dotdotp->d_ino = pnodeid;
9429 /* LOOKUPP+GETATTR attemped */
9430 if (args.array_len == 5 && rd_res->dotdotp) {
9431 if (res.status == NFS4_OK && res_opcnt == 5) {
9432 nfs_fh4 *fhp;
9433 nfs4_sharedfh_t *sfhp;
9434 vnode_t *pvp;
9435 nfs4_ga_res_t *garp;
9437 resop++; /* lookupp */
9438 resop++; /* getfh */
9439 fhp = &resop->nfs_resop4_u.opgetfh.object;
9441 resop++; /* getattr of parent */
9444 * First, take care of finishing the
9445 * readdir results.
9447 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9449 * The d_ino of .. must be the inode number
9450 * of the mounted filesystem.
9452 if (garp->n4g_va.va_mask & VATTR_NODEID)
9453 rd_res->dotdotp->d_ino =
9454 garp->n4g_va.va_nodeid;
9458 * Next, create the ".." dnlc entry
9460 sfhp = sfh4_get(fhp, mi);
9461 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9462 dnlc_update(vp, "..", pvp);
9463 VN_RELE(pvp);
9465 sfh4_rele(&sfhp);
9469 if (mi->mi_io_kstats) {
9470 mutex_enter(&mi->mi_lock);
9471 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9472 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9473 mutex_exit(&mi->mi_lock);
9476 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9478 out:
9480 * If readdir a node that is a stub for a crossed mount point,
9481 * keep the original secinfo flavor for the current file system,
9482 * not the crossed one.
9484 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9486 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9490 static int
9491 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9493 rnode4_t *rp = VTOR4(bp->b_vp);
9494 int count;
9495 int error;
9496 cred_t *cred_otw = NULL;
9497 offset_t offset;
9498 nfs4_open_stream_t *osp = NULL;
9499 bool_t first_time = TRUE; /* first time getting otw cred */
9500 bool_t last_time = FALSE; /* last time getting otw cred */
9502 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9504 DTRACE_IO1(start, struct buf *, bp);
9505 offset = ldbtob(bp->b_lblkno);
9507 if (bp->b_flags & B_READ) {
9508 read_again:
9510 * Releases the osp, if it is provided.
9511 * Puts a hold on the cred_otw and the new osp (if found).
9513 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9514 &first_time, &last_time);
9515 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9516 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9517 readahead, NULL);
9518 crfree(cred_otw);
9519 if (!error) {
9520 if (bp->b_resid) {
9522 * Didn't get it all because we hit EOF,
9523 * zero all the memory beyond the EOF.
9525 /* bzero(rdaddr + */
9526 bzero(bp->b_un.b_addr +
9527 bp->b_bcount - bp->b_resid, bp->b_resid);
9529 mutex_enter(&rp->r_statelock);
9530 if (bp->b_resid == bp->b_bcount &&
9531 offset >= rp->r_size) {
9533 * We didn't read anything at all as we are
9534 * past EOF. Return an error indicator back
9535 * but don't destroy the pages (yet).
9537 error = NFS_EOF;
9539 mutex_exit(&rp->r_statelock);
9540 } else if (error == EACCES && last_time == FALSE) {
9541 goto read_again;
9543 } else {
9544 if (!(rp->r_flags & R4STALE)) {
9545 write_again:
9547 * Releases the osp, if it is provided.
9548 * Puts a hold on the cred_otw and the new
9549 * osp (if found).
9551 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9552 &first_time, &last_time);
9553 mutex_enter(&rp->r_statelock);
9554 count = MIN(bp->b_bcount, rp->r_size - offset);
9555 mutex_exit(&rp->r_statelock);
9556 if (count < 0)
9557 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9558 #ifdef DEBUG
9559 if (count == 0) {
9560 zoneid_t zoneid = getzoneid();
9562 zcmn_err(zoneid, CE_WARN,
9563 "nfs4_bio: zero length write at %lld",
9564 offset);
9565 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9566 "b_bcount=%ld, file size=%lld",
9567 rp->r_flags, (long)bp->b_bcount,
9568 rp->r_size);
9569 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9570 if (nfs4_bio_do_stop)
9571 debug_enter("nfs4_bio");
9573 #endif
9574 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9575 count, cred_otw, stab_comm);
9576 if (error == EACCES && last_time == FALSE) {
9577 crfree(cred_otw);
9578 goto write_again;
9580 bp->b_error = error;
9581 if (error && error != EINTR &&
9582 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9584 * Don't print EDQUOT errors on the console.
9585 * Don't print asynchronous EACCES errors.
9586 * Don't print EFBIG errors.
9587 * Print all other write errors.
9589 if (error != EDQUOT && error != EFBIG &&
9590 (error != EACCES ||
9591 !(bp->b_flags & B_ASYNC)))
9592 nfs4_write_error(bp->b_vp,
9593 error, cred_otw);
9595 * Update r_error and r_flags as appropriate.
9596 * If the error was ESTALE, then mark the
9597 * rnode as not being writeable and save
9598 * the error status. Otherwise, save any
9599 * errors which occur from asynchronous
9600 * page invalidations. Any errors occurring
9601 * from other operations should be saved
9602 * by the caller.
9604 mutex_enter(&rp->r_statelock);
9605 if (error == ESTALE) {
9606 rp->r_flags |= R4STALE;
9607 if (!rp->r_error)
9608 rp->r_error = error;
9609 } else if (!rp->r_error &&
9610 (bp->b_flags &
9611 (B_INVAL|B_FORCE|B_ASYNC)) ==
9612 (B_INVAL|B_FORCE|B_ASYNC)) {
9613 rp->r_error = error;
9615 mutex_exit(&rp->r_statelock);
9617 crfree(cred_otw);
9618 } else {
9619 error = rp->r_error;
9621 * A close may have cleared r_error, if so,
9622 * propagate ESTALE error return properly
9624 if (error == 0)
9625 error = ESTALE;
9629 if (error != 0 && error != NFS_EOF)
9630 bp->b_flags |= B_ERROR;
9632 if (osp)
9633 open_stream_rele(osp, rp);
9635 DTRACE_IO1(done, struct buf *, bp);
9637 return (error);
9640 /* ARGSUSED */
9642 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9644 return (EREMOTE);
9647 /* ARGSUSED2 */
9649 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9651 rnode4_t *rp = VTOR4(vp);
9653 if (!write_lock) {
9654 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9655 return (V_WRITELOCK_FALSE);
9658 if ((rp->r_flags & R4DIRECTIO) ||
9659 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9660 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9661 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9662 return (V_WRITELOCK_FALSE);
9663 nfs_rw_exit(&rp->r_rwlock);
9666 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9667 return (V_WRITELOCK_TRUE);
9670 /* ARGSUSED */
9671 void
9672 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9674 rnode4_t *rp = VTOR4(vp);
9676 nfs_rw_exit(&rp->r_rwlock);
9679 /* ARGSUSED */
9680 static int
9681 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9683 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9684 return (EIO);
9687 * Because we stuff the readdir cookie into the offset field
9688 * someone may attempt to do an lseek with the cookie which
9689 * we want to succeed.
9691 if (vp->v_type == VDIR)
9692 return (0);
9693 if (*noffp < 0)
9694 return (EINVAL);
9695 return (0);
9700 * Return all the pages from [off..off+len) in file
9702 /* ARGSUSED */
9703 static int
9704 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9705 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9706 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9708 rnode4_t *rp;
9709 int error;
9710 mntinfo4_t *mi;
9712 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9713 return (EIO);
9714 rp = VTOR4(vp);
9715 if (IS_SHADOW(vp, rp))
9716 vp = RTOV4(rp);
9718 if (vp->v_flag & VNOMAP)
9719 return (ENOSYS);
9721 if (protp != NULL)
9722 *protp = PROT_ALL;
9725 * Now validate that the caches are up to date.
9727 if (error = nfs4_validate_caches(vp, cr))
9728 return (error);
9730 mi = VTOMI4(vp);
9731 retry:
9732 mutex_enter(&rp->r_statelock);
9735 * Don't create dirty pages faster than they
9736 * can be cleaned so that the system doesn't
9737 * get imbalanced. If the async queue is
9738 * maxed out, then wait for it to drain before
9739 * creating more dirty pages. Also, wait for
9740 * any threads doing pagewalks in the vop_getattr
9741 * entry points so that they don't block for
9742 * long periods.
9744 if (rw == S_CREATE) {
9745 while ((mi->mi_max_threads != 0 &&
9746 rp->r_awcount > 2 * mi->mi_max_threads) ||
9747 rp->r_gcount > 0)
9748 cv_wait(&rp->r_cv, &rp->r_statelock);
9752 * If we are getting called as a side effect of an nfs_write()
9753 * operation the local file size might not be extended yet.
9754 * In this case we want to be able to return pages of zeroes.
9756 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9757 NFS4_DEBUG(nfs4_pageio_debug,
9758 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9759 "len=%llu, size=%llu, attrsize =%llu", off,
9760 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9761 mutex_exit(&rp->r_statelock);
9762 return (EFAULT); /* beyond EOF */
9765 mutex_exit(&rp->r_statelock);
9767 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9768 pl, plsz, seg, addr, rw, cr);
9769 NFS4_DEBUG(nfs4_pageio_debug && error,
9770 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9771 error, off, (u_longlong_t)len));
9773 switch (error) {
9774 case NFS_EOF:
9775 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9776 goto retry;
9777 case ESTALE:
9778 nfs4_purge_stale_fh(error, vp, cr);
9781 return (error);
9785 * Called from pvn_getpages to get a particular page.
9787 /* ARGSUSED */
9788 static int
9789 nfs4_getapage(vnode_t *vp, uoff_t off, size_t len, uint_t *protp,
9790 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9791 enum seg_rw rw, cred_t *cr)
9793 rnode4_t *rp;
9794 uint_t bsize;
9795 struct buf *bp;
9796 page_t *pp;
9797 uoff_t lbn;
9798 uoff_t io_off;
9799 uoff_t blkoff;
9800 uoff_t rablkoff;
9801 size_t io_len;
9802 uint_t blksize;
9803 int error;
9804 int readahead;
9805 int readahead_issued = 0;
9806 int ra_window; /* readahead window */
9807 page_t *pagefound;
9808 page_t *savepp;
9810 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9811 return (EIO);
9813 rp = VTOR4(vp);
9814 ASSERT(!IS_SHADOW(vp, rp));
9815 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9817 reread:
9818 bp = NULL;
9819 pp = NULL;
9820 pagefound = NULL;
9822 if (pl != NULL)
9823 pl[0] = NULL;
9825 error = 0;
9826 lbn = off / bsize;
9827 blkoff = lbn * bsize;
9830 * Queueing up the readahead before doing the synchronous read
9831 * results in a significant increase in read throughput because
9832 * of the increased parallelism between the async threads and
9833 * the process context.
9835 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9836 rw != S_CREATE &&
9837 !(vp->v_flag & VNOCACHE)) {
9838 mutex_enter(&rp->r_statelock);
9841 * Calculate the number of readaheads to do.
9842 * a) No readaheads at offset = 0.
9843 * b) Do maximum(nfs4_nra) readaheads when the readahead
9844 * window is closed.
9845 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9846 * upon how far the readahead window is open or close.
9847 * d) No readaheads if rp->r_nextr is not within the scope
9848 * of the readahead window (random i/o).
9851 if (off == 0)
9852 readahead = 0;
9853 else if (blkoff == rp->r_nextr)
9854 readahead = nfs4_nra;
9855 else if (rp->r_nextr > blkoff &&
9856 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9857 <= (nfs4_nra - 1)))
9858 readahead = nfs4_nra - ra_window;
9859 else
9860 readahead = 0;
9862 rablkoff = rp->r_nextr;
9863 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9864 mutex_exit(&rp->r_statelock);
9865 if (nfs4_async_readahead(vp, rablkoff + bsize,
9866 addr + (rablkoff + bsize - off),
9867 seg, cr, nfs4_readahead) < 0) {
9868 mutex_enter(&rp->r_statelock);
9869 break;
9871 readahead--;
9872 rablkoff += bsize;
9874 * Indicate that we did a readahead so
9875 * readahead offset is not updated
9876 * by the synchronous read below.
9878 readahead_issued = 1;
9879 mutex_enter(&rp->r_statelock);
9881 * set readahead offset to
9882 * offset of last async readahead
9883 * request.
9885 rp->r_nextr = rablkoff;
9887 mutex_exit(&rp->r_statelock);
9890 again:
9891 if ((pagefound = page_exists(&vp->v_object, off)) == NULL) {
9892 if (pl == NULL) {
9893 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9894 nfs4_readahead);
9895 } else if (rw == S_CREATE) {
9897 * Block for this page is not allocated, or the offset
9898 * is beyond the current allocation size, or we're
9899 * allocating a swap slot and the page was not found,
9900 * so allocate it and return a zero page.
9902 if ((pp = page_create_va(&vp->v_object, off,
9903 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9904 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9905 io_len = PAGESIZE;
9906 mutex_enter(&rp->r_statelock);
9907 rp->r_nextr = off + PAGESIZE;
9908 mutex_exit(&rp->r_statelock);
9909 } else {
9911 * Need to go to server to get a block
9913 mutex_enter(&rp->r_statelock);
9914 if (blkoff < rp->r_size &&
9915 blkoff + bsize > rp->r_size) {
9917 * If less than a block left in
9918 * file read less than a block.
9920 if (rp->r_size <= off) {
9922 * Trying to access beyond EOF,
9923 * set up to get at least one page.
9925 blksize = off + PAGESIZE - blkoff;
9926 } else
9927 blksize = rp->r_size - blkoff;
9928 } else if ((off == 0) ||
9929 (off != rp->r_nextr && !readahead_issued)) {
9930 blksize = PAGESIZE;
9931 blkoff = off; /* block = page here */
9932 } else
9933 blksize = bsize;
9934 mutex_exit(&rp->r_statelock);
9936 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9937 &io_len, blkoff, blksize, 0);
9940 * Some other thread has entered the page,
9941 * so just use it.
9943 if (pp == NULL)
9944 goto again;
9947 * Now round the request size up to page boundaries.
9948 * This ensures that the entire page will be
9949 * initialized to zeroes if EOF is encountered.
9951 io_len = ptob(btopr(io_len));
9953 bp = pageio_setup(pp, io_len, vp, B_READ);
9954 ASSERT(bp != NULL);
9957 * pageio_setup should have set b_addr to 0. This
9958 * is correct since we want to do I/O on a page
9959 * boundary. bp_mapin will use this addr to calculate
9960 * an offset, and then set b_addr to the kernel virtual
9961 * address it allocated for us.
9963 ASSERT(bp->b_un.b_addr == 0);
9965 bp->b_edev = 0;
9966 bp->b_dev = 0;
9967 bp->b_lblkno = lbtodb(io_off);
9968 bp->b_file = vp;
9969 bp->b_offset = (offset_t)off;
9970 bp_mapin(bp);
9973 * If doing a write beyond what we believe is EOF,
9974 * don't bother trying to read the pages from the
9975 * server, we'll just zero the pages here. We
9976 * don't check that the rw flag is S_WRITE here
9977 * because some implementations may attempt a
9978 * read access to the buffer before copying data.
9980 mutex_enter(&rp->r_statelock);
9981 if (io_off >= rp->r_size && seg == segkmap) {
9982 mutex_exit(&rp->r_statelock);
9983 bzero(bp->b_un.b_addr, io_len);
9984 } else {
9985 mutex_exit(&rp->r_statelock);
9986 error = nfs4_bio(bp, NULL, cr, FALSE);
9990 * Unmap the buffer before freeing it.
9992 bp_mapout(bp);
9993 pageio_done(bp);
9995 savepp = pp;
9996 do {
9997 pp->p_fsdata = C_NOCOMMIT;
9998 } while ((pp = pp->p_next) != savepp);
10000 if (error == NFS_EOF) {
10002 * If doing a write system call just return
10003 * zeroed pages, else user tried to get pages
10004 * beyond EOF, return error. We don't check
10005 * that the rw flag is S_WRITE here because
10006 * some implementations may attempt a read
10007 * access to the buffer before copying data.
10009 if (seg == segkmap)
10010 error = 0;
10011 else
10012 error = EFAULT;
10015 if (!readahead_issued && !error) {
10016 mutex_enter(&rp->r_statelock);
10017 rp->r_nextr = io_off + io_len;
10018 mutex_exit(&rp->r_statelock);
10023 out:
10024 if (pl == NULL)
10025 return (error);
10027 if (error) {
10028 if (pp != NULL)
10029 pvn_read_done(pp, B_ERROR);
10030 return (error);
10033 if (pagefound) {
10034 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10037 * Page exists in the cache, acquire the appropriate lock.
10038 * If this fails, start all over again.
10040 if ((pp = page_lookup(&vp->v_object, off, se)) == NULL) {
10041 #ifdef DEBUG
10042 nfs4_lostpage++;
10043 #endif
10044 goto reread;
10046 pl[0] = pp;
10047 pl[1] = NULL;
10048 return (0);
10051 if (pp != NULL)
10052 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10054 return (error);
10057 static void
10058 nfs4_readahead(vnode_t *vp, uoff_t blkoff, caddr_t addr, struct seg *seg,
10059 cred_t *cr)
10061 int error;
10062 page_t *pp;
10063 uoff_t io_off;
10064 size_t io_len;
10065 struct buf *bp;
10066 uint_t bsize, blksize;
10067 rnode4_t *rp = VTOR4(vp);
10068 page_t *savepp;
10070 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10072 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10074 mutex_enter(&rp->r_statelock);
10075 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10077 * If less than a block left in file read less
10078 * than a block.
10080 blksize = rp->r_size - blkoff;
10081 } else
10082 blksize = bsize;
10083 mutex_exit(&rp->r_statelock);
10085 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10086 &io_off, &io_len, blkoff, blksize, 1);
10088 * The isra flag passed to the kluster function is 1, we may have
10089 * gotten a return value of NULL for a variety of reasons (# of free
10090 * pages < minfree, someone entered the page on the vnode etc). In all
10091 * cases, we want to punt on the readahead.
10093 if (pp == NULL)
10094 return;
10097 * Now round the request size up to page boundaries.
10098 * This ensures that the entire page will be
10099 * initialized to zeroes if EOF is encountered.
10101 io_len = ptob(btopr(io_len));
10103 bp = pageio_setup(pp, io_len, vp, B_READ);
10104 ASSERT(bp != NULL);
10107 * pageio_setup should have set b_addr to 0. This is correct since
10108 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10109 * to calculate an offset, and then set b_addr to the kernel virtual
10110 * address it allocated for us.
10112 ASSERT(bp->b_un.b_addr == 0);
10114 bp->b_edev = 0;
10115 bp->b_dev = 0;
10116 bp->b_lblkno = lbtodb(io_off);
10117 bp->b_file = vp;
10118 bp->b_offset = (offset_t)blkoff;
10119 bp_mapin(bp);
10122 * If doing a write beyond what we believe is EOF, don't bother trying
10123 * to read the pages from the server, we'll just zero the pages here.
10124 * We don't check that the rw flag is S_WRITE here because some
10125 * implementations may attempt a read access to the buffer before
10126 * copying data.
10128 mutex_enter(&rp->r_statelock);
10129 if (io_off >= rp->r_size && seg == segkmap) {
10130 mutex_exit(&rp->r_statelock);
10131 bzero(bp->b_un.b_addr, io_len);
10132 error = 0;
10133 } else {
10134 mutex_exit(&rp->r_statelock);
10135 error = nfs4_bio(bp, NULL, cr, TRUE);
10136 if (error == NFS_EOF)
10137 error = 0;
10141 * Unmap the buffer before freeing it.
10143 bp_mapout(bp);
10144 pageio_done(bp);
10146 savepp = pp;
10147 do {
10148 pp->p_fsdata = C_NOCOMMIT;
10149 } while ((pp = pp->p_next) != savepp);
10151 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10154 * In case of error set readahead offset
10155 * to the lowest offset.
10156 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10158 if (error && rp->r_nextr > io_off) {
10159 mutex_enter(&rp->r_statelock);
10160 if (rp->r_nextr > io_off)
10161 rp->r_nextr = io_off;
10162 mutex_exit(&rp->r_statelock);
10167 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10168 * If len == 0, do from off to EOF.
10170 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10171 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10172 * (from pageout).
10174 /* ARGSUSED */
10175 static int
10176 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10177 caller_context_t *ct)
10179 int error;
10180 rnode4_t *rp;
10182 ASSERT(cr != NULL);
10184 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10185 return (EIO);
10187 rp = VTOR4(vp);
10188 if (IS_SHADOW(vp, rp))
10189 vp = RTOV4(rp);
10192 * XXX - Why should this check be made here?
10194 if (vp->v_flag & VNOMAP)
10195 return (ENOSYS);
10197 if (len == 0 && !(flags & B_INVAL) &&
10198 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10199 return (0);
10201 mutex_enter(&rp->r_statelock);
10202 rp->r_count++;
10203 mutex_exit(&rp->r_statelock);
10204 error = nfs4_putpages(vp, off, len, flags, cr);
10205 mutex_enter(&rp->r_statelock);
10206 rp->r_count--;
10207 cv_broadcast(&rp->r_cv);
10208 mutex_exit(&rp->r_statelock);
10210 return (error);
10214 * Write out a single page, possibly klustering adjacent dirty pages.
10217 nfs4_putapage(vnode_t *vp, page_t *pp, uoff_t *offp, size_t *lenp,
10218 int flags, cred_t *cr)
10220 uoff_t io_off;
10221 uoff_t lbn_off;
10222 uoff_t lbn;
10223 size_t io_len;
10224 uint_t bsize;
10225 int error;
10226 rnode4_t *rp;
10228 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10229 ASSERT(pp != NULL);
10230 ASSERT(cr != NULL);
10231 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10233 rp = VTOR4(vp);
10234 ASSERT(rp->r_count > 0);
10235 ASSERT(!IS_SHADOW(vp, rp));
10237 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10238 lbn = pp->p_offset / bsize;
10239 lbn_off = lbn * bsize;
10242 * Find a kluster that fits in one block, or in
10243 * one page if pages are bigger than blocks. If
10244 * there is less file space allocated than a whole
10245 * page, we'll shorten the i/o request below.
10247 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10248 roundup(bsize, PAGESIZE), flags);
10251 * pvn_write_kluster shouldn't have returned a page with offset
10252 * behind the original page we were given. Verify that.
10254 ASSERT((pp->p_offset / bsize) >= lbn);
10257 * Now pp will have the list of kept dirty pages marked for
10258 * write back. It will also handle invalidation and freeing
10259 * of pages that are not dirty. Check for page length rounding
10260 * problems.
10262 if (io_off + io_len > lbn_off + bsize) {
10263 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10264 io_len = lbn_off + bsize - io_off;
10267 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10268 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10269 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10270 * progress and the r_size has not been made consistent with the
10271 * new size of the file. When the uiomove() completes the r_size is
10272 * updated and the R4MODINPROGRESS flag is cleared.
10274 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10275 * consistent value of r_size. Without this handshaking, it is
10276 * possible that nfs4_bio() picks up the old value of r_size
10277 * before the uiomove() in writerp4() completes. This will result
10278 * in the write through nfs4_bio() being dropped.
10280 * More precisely, there is a window between the time the uiomove()
10281 * completes and the time the r_size is updated. If a fop_putpage()
10282 * operation intervenes in this window, the page will be picked up,
10283 * because it is dirty (it will be unlocked, unless it was
10284 * pagecreate'd). When the page is picked up as dirty, the dirty
10285 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10286 * checked. This will still be the old size. Therefore the page will
10287 * not be written out. When segmap_release() calls fop_putpage(),
10288 * the page will be found to be clean and the write will be dropped.
10290 if (rp->r_flags & R4MODINPROGRESS) {
10291 mutex_enter(&rp->r_statelock);
10292 if ((rp->r_flags & R4MODINPROGRESS) &&
10293 rp->r_modaddr + MAXBSIZE > io_off &&
10294 rp->r_modaddr < io_off + io_len) {
10295 page_t *plist;
10297 * A write is in progress for this region of the file.
10298 * If we did not detect R4MODINPROGRESS here then this
10299 * path through nfs_putapage() would eventually go to
10300 * nfs4_bio() and may not write out all of the data
10301 * in the pages. We end up losing data. So we decide
10302 * to set the modified bit on each page in the page
10303 * list and mark the rnode with R4DIRTY. This write
10304 * will be restarted at some later time.
10306 plist = pp;
10307 while (plist != NULL) {
10308 pp = plist;
10309 page_sub(&plist, pp);
10310 hat_setmod(pp);
10311 page_io_unlock(pp);
10312 page_unlock(pp);
10314 rp->r_flags |= R4DIRTY;
10315 mutex_exit(&rp->r_statelock);
10316 if (offp)
10317 *offp = io_off;
10318 if (lenp)
10319 *lenp = io_len;
10320 return (0);
10322 mutex_exit(&rp->r_statelock);
10325 if (flags & B_ASYNC) {
10326 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10327 nfs4_sync_putapage);
10328 } else
10329 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10331 if (offp)
10332 *offp = io_off;
10333 if (lenp)
10334 *lenp = io_len;
10335 return (error);
10338 static int
10339 nfs4_sync_putapage(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
10340 int flags, cred_t *cr)
10342 int error;
10343 rnode4_t *rp;
10345 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10347 flags |= B_WRITE;
10349 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10351 rp = VTOR4(vp);
10353 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10354 error == EACCES) &&
10355 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10356 if (!(rp->r_flags & R4OUTOFSPACE)) {
10357 mutex_enter(&rp->r_statelock);
10358 rp->r_flags |= R4OUTOFSPACE;
10359 mutex_exit(&rp->r_statelock);
10361 flags |= B_ERROR;
10362 pvn_write_done(pp, flags);
10364 * If this was not an async thread, then try again to
10365 * write out the pages, but this time, also destroy
10366 * them whether or not the write is successful. This
10367 * will prevent memory from filling up with these
10368 * pages and destroying them is the only alternative
10369 * if they can't be written out.
10371 * Don't do this if this is an async thread because
10372 * when the pages are unlocked in pvn_write_done,
10373 * some other thread could have come along, locked
10374 * them, and queued for an async thread. It would be
10375 * possible for all of the async threads to be tied
10376 * up waiting to lock the pages again and they would
10377 * all already be locked and waiting for an async
10378 * thread to handle them. Deadlock.
10380 if (!(flags & B_ASYNC)) {
10381 error = nfs4_putpage(vp, io_off, io_len,
10382 B_INVAL | B_FORCE, cr, NULL);
10384 } else {
10385 if (error)
10386 flags |= B_ERROR;
10387 else if (rp->r_flags & R4OUTOFSPACE) {
10388 mutex_enter(&rp->r_statelock);
10389 rp->r_flags &= ~R4OUTOFSPACE;
10390 mutex_exit(&rp->r_statelock);
10392 pvn_write_done(pp, flags);
10393 if (freemem < desfree)
10394 (void) nfs4_commit_vp(vp, 0, 0, cr,
10395 NFS4_WRITE_NOWAIT);
10398 return (error);
10401 #ifdef DEBUG
10402 int nfs4_force_open_before_mmap = 0;
10403 #endif
10405 /* ARGSUSED */
10406 static int
10407 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10408 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10409 caller_context_t *ct)
10411 struct segvn_crargs vn_a;
10412 int error = 0;
10413 rnode4_t *rp = VTOR4(vp);
10414 mntinfo4_t *mi = VTOMI4(vp);
10416 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10417 return (EIO);
10419 if (vp->v_flag & VNOMAP)
10420 return (ENOSYS);
10422 if (off < 0 || (off + len) < 0)
10423 return (ENXIO);
10425 if (vp->v_type != VREG)
10426 return (ENODEV);
10429 * If the file is delegated to the client don't do anything.
10430 * If the file is not delegated, then validate the data cache.
10432 mutex_enter(&rp->r_statev4_lock);
10433 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10434 mutex_exit(&rp->r_statev4_lock);
10435 error = nfs4_validate_caches(vp, cr);
10436 if (error)
10437 return (error);
10438 } else {
10439 mutex_exit(&rp->r_statev4_lock);
10443 * Check to see if the vnode is currently marked as not cachable.
10444 * This means portions of the file are locked (through fop_frlock).
10445 * In this case the map request must be refused. We use
10446 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10448 * Atomically increment r_inmap after acquiring r_rwlock. The
10449 * idea here is to acquire r_rwlock to block read/write and
10450 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10451 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10452 * and we can prevent the deadlock that would have occurred
10453 * when nfs4_addmap() would have acquired it out of order.
10455 * Since we are not protecting r_inmap by any lock, we do not
10456 * hold any lock when we decrement it. We atomically decrement
10457 * r_inmap after we release r_lkserlock.
10460 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10461 return (EINTR);
10462 atomic_inc_uint(&rp->r_inmap);
10463 nfs_rw_exit(&rp->r_rwlock);
10465 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10466 atomic_dec_uint(&rp->r_inmap);
10467 return (EINTR);
10470 if (vp->v_flag & VNOCACHE) {
10471 error = EAGAIN;
10472 goto done;
10476 * Don't allow concurrent locks and mapping if mandatory locking is
10477 * enabled.
10479 if (flk_has_remote_locks(vp)) {
10480 struct vattr va;
10481 va.va_mask = VATTR_MODE;
10482 error = nfs4getattr(vp, &va, cr);
10483 if (error != 0)
10484 goto done;
10485 if (MANDLOCK(vp, va.va_mode)) {
10486 error = EAGAIN;
10487 goto done;
10492 * It is possible that the rnode has a lost lock request that we
10493 * are still trying to recover, and that the request conflicts with
10494 * this map request.
10496 * An alternative approach would be for nfs4_safemap() to consider
10497 * queued lock requests when deciding whether to set or clear
10498 * VNOCACHE. This would require the frlock code path to call
10499 * nfs4_safemap() after enqueing a lost request.
10501 if (nfs4_map_lost_lock_conflict(vp)) {
10502 error = EAGAIN;
10503 goto done;
10506 as_rangelock(as);
10507 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10508 if (error != 0) {
10509 as_rangeunlock(as);
10510 goto done;
10513 if (vp->v_type == VREG) {
10515 * We need to retrieve the open stream
10517 nfs4_open_stream_t *osp = NULL;
10518 nfs4_open_owner_t *oop = NULL;
10520 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10521 if (oop != NULL) {
10522 /* returns with 'os_sync_lock' held */
10523 osp = find_open_stream(oop, rp);
10524 open_owner_rele(oop);
10526 if (osp == NULL) {
10527 #ifdef DEBUG
10528 if (nfs4_force_open_before_mmap) {
10529 error = EIO;
10530 goto done;
10532 #endif
10533 /* returns with 'os_sync_lock' held */
10534 error = open_and_get_osp(vp, cr, &osp);
10535 if (osp == NULL) {
10536 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10537 "nfs4_map: we tried to OPEN the file "
10538 "but again no osp, so fail with EIO"));
10539 goto done;
10543 if (osp->os_failed_reopen) {
10544 mutex_exit(&osp->os_sync_lock);
10545 open_stream_rele(osp, rp);
10546 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10547 "nfs4_map: os_failed_reopen set on "
10548 "osp %p, cr %p, rp %s", (void *)osp,
10549 (void *)cr, rnode4info(rp)));
10550 error = EIO;
10551 goto done;
10553 mutex_exit(&osp->os_sync_lock);
10554 open_stream_rele(osp, rp);
10557 vn_a.vp = vp;
10558 vn_a.offset = off;
10559 vn_a.type = (flags & MAP_TYPE);
10560 vn_a.prot = (uchar_t)prot;
10561 vn_a.maxprot = (uchar_t)maxprot;
10562 vn_a.flags = (flags & ~MAP_TYPE);
10563 vn_a.cred = cr;
10564 vn_a.amp = NULL;
10565 vn_a.szc = 0;
10566 vn_a.lgrp_mem_policy_flags = 0;
10568 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10569 as_rangeunlock(as);
10571 done:
10572 nfs_rw_exit(&rp->r_lkserlock);
10573 atomic_dec_uint(&rp->r_inmap);
10574 return (error);
10578 * We're most likely dealing with a kernel module that likes to READ
10579 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10580 * officially OPEN the file to create the necessary client state
10581 * for bookkeeping of os_mmap_read/write counts.
10583 * Since fop_map only passes in a pointer to the vnode rather than
10584 * a double pointer, we can't handle the case where nfs4open_otw()
10585 * returns a different vnode than the one passed into fop_map (since
10586 * fop_delmap will not see the vnode nfs4open_otw used). In this case,
10587 * we return NULL and let nfs4_map() fail. Note: the only case where
10588 * this should happen is if the file got removed and replaced with the
10589 * same name on the server (in addition to the fact that we're trying
10590 * to fop_map withouth fop_opening the file in the first place).
10592 static int
10593 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10595 rnode4_t *rp, *drp;
10596 vnode_t *dvp, *open_vp;
10597 char file_name[MAXNAMELEN];
10598 int just_created;
10599 nfs4_open_stream_t *osp;
10600 nfs4_open_owner_t *oop;
10601 int error;
10603 *ospp = NULL;
10604 open_vp = map_vp;
10606 rp = VTOR4(open_vp);
10607 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10608 return (error);
10609 drp = VTOR4(dvp);
10611 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10612 VN_RELE(dvp);
10613 return (EINTR);
10616 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10617 nfs_rw_exit(&drp->r_rwlock);
10618 VN_RELE(dvp);
10619 return (error);
10622 mutex_enter(&rp->r_statev4_lock);
10623 if (rp->created_v4) {
10624 rp->created_v4 = 0;
10625 mutex_exit(&rp->r_statev4_lock);
10627 dnlc_update(dvp, file_name, open_vp);
10628 /* This is needed so we don't bump the open ref count */
10629 just_created = 1;
10630 } else {
10631 mutex_exit(&rp->r_statev4_lock);
10632 just_created = 0;
10635 VN_HOLD(map_vp);
10637 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10638 just_created);
10639 if (error) {
10640 nfs_rw_exit(&drp->r_rwlock);
10641 VN_RELE(dvp);
10642 VN_RELE(map_vp);
10643 return (error);
10646 nfs_rw_exit(&drp->r_rwlock);
10647 VN_RELE(dvp);
10650 * If nfs4open_otw() returned a different vnode then "undo"
10651 * the open and return failure to the caller.
10653 if (!VN_CMP(open_vp, map_vp)) {
10654 nfs4_error_t e;
10656 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10657 "open returned a different vnode"));
10659 * If there's an error, ignore it,
10660 * and let fop_inactive handle it.
10662 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10663 CLOSE_NORM, 0, 0, 0);
10664 VN_RELE(map_vp);
10665 return (EIO);
10668 VN_RELE(map_vp);
10670 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10671 if (!oop) {
10672 nfs4_error_t e;
10674 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10675 "no open owner"));
10677 * If there's an error, ignore it,
10678 * and let fop_inactive handle it.
10680 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10681 CLOSE_NORM, 0, 0, 0);
10682 return (EIO);
10684 osp = find_open_stream(oop, rp);
10685 open_owner_rele(oop);
10686 *ospp = osp;
10687 return (0);
10691 * Please be aware that when this function is called, the address space write
10692 * a_lock is held. Do not put over the wire calls in this function.
10694 /* ARGSUSED */
10695 static int
10696 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10697 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10698 caller_context_t *ct)
10700 rnode4_t *rp;
10701 int error = 0;
10702 mntinfo4_t *mi;
10704 mi = VTOMI4(vp);
10705 rp = VTOR4(vp);
10707 if (nfs_zone() != mi->mi_zone)
10708 return (EIO);
10709 if (vp->v_flag & VNOMAP)
10710 return (ENOSYS);
10713 * Don't need to update the open stream first, since this
10714 * mmap can't add any additional share access that isn't
10715 * already contained in the open stream (for the case where we
10716 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10717 * take into account os_mmap_read[write] counts).
10719 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10721 if (vp->v_type == VREG) {
10723 * We need to retrieve the open stream and update the counts.
10724 * If there is no open stream here, something is wrong.
10726 nfs4_open_stream_t *osp = NULL;
10727 nfs4_open_owner_t *oop = NULL;
10729 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10730 if (oop != NULL) {
10731 /* returns with 'os_sync_lock' held */
10732 osp = find_open_stream(oop, rp);
10733 open_owner_rele(oop);
10735 if (osp == NULL) {
10736 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10737 "nfs4_addmap: we should have an osp"
10738 "but we don't, so fail with EIO"));
10739 error = EIO;
10740 goto out;
10743 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10744 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10747 * Update the map count in the open stream.
10748 * This is necessary in the case where we
10749 * open/mmap/close/, then the server reboots, and we
10750 * attempt to reopen. If the mmap doesn't add share
10751 * access then we send an invalid reopen with
10752 * access = NONE.
10754 * We need to specifically check each PROT_* so a mmap
10755 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10756 * read and write access. A simple comparison of prot
10757 * to ~PROT_WRITE to determine read access is insufficient
10758 * since prot can be |= with PROT_USER, etc.
10762 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10764 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10765 osp->os_mmap_write += btopr(len);
10766 if (maxprot & PROT_READ)
10767 osp->os_mmap_read += btopr(len);
10768 if (maxprot & PROT_EXEC)
10769 osp->os_mmap_read += btopr(len);
10771 * Ensure that os_mmap_read gets incremented, even if
10772 * maxprot were to look like PROT_NONE.
10774 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10775 !(maxprot & PROT_EXEC))
10776 osp->os_mmap_read += btopr(len);
10777 osp->os_mapcnt += btopr(len);
10778 mutex_exit(&osp->os_sync_lock);
10779 open_stream_rele(osp, rp);
10782 out:
10784 * If we got an error, then undo our
10785 * incrementing of 'r_mapcnt'.
10788 if (error) {
10789 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10790 ASSERT(rp->r_mapcnt >= 0);
10792 return (error);
10795 /* ARGSUSED */
10796 static int
10797 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10800 return (VTOR4(vp1) == VTOR4(vp2));
10803 /* ARGSUSED */
10804 static int
10805 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10806 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10807 caller_context_t *ct)
10809 int rc;
10810 uoff_t start, end;
10811 rnode4_t *rp;
10812 int error = 0, intr = INTR4(vp);
10813 nfs4_error_t e;
10815 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10816 return (EIO);
10818 /* check for valid cmd parameter */
10819 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10820 return (EINVAL);
10822 /* Verify l_type. */
10823 switch (bfp->l_type) {
10824 case F_RDLCK:
10825 if (cmd != F_GETLK && !(flag & FREAD))
10826 return (EBADF);
10827 break;
10828 case F_WRLCK:
10829 if (cmd != F_GETLK && !(flag & FWRITE))
10830 return (EBADF);
10831 break;
10832 case F_UNLCK:
10833 intr = 0;
10834 break;
10836 default:
10837 return (EINVAL);
10840 /* check the validity of the lock range */
10841 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10842 return (rc);
10843 if (rc = flk_check_lock_data(start, end, MAXEND))
10844 return (rc);
10847 * If the filesystem is mounted using local locking, pass the
10848 * request off to the local locking code.
10850 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10851 if (cmd == F_SETLK || cmd == F_SETLKW) {
10853 * For complete safety, we should be holding
10854 * r_lkserlock. However, we can't call
10855 * nfs4_safelock and then fs_frlock while
10856 * holding r_lkserlock, so just invoke
10857 * nfs4_safelock and expect that this will
10858 * catch enough of the cases.
10860 if (!nfs4_safelock(vp, bfp, cr))
10861 return (EAGAIN);
10863 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10866 rp = VTOR4(vp);
10869 * Check whether the given lock request can proceed, given the
10870 * current file mappings.
10872 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10873 return (EINTR);
10874 if (cmd == F_SETLK || cmd == F_SETLKW) {
10875 if (!nfs4_safelock(vp, bfp, cr)) {
10876 rc = EAGAIN;
10877 goto done;
10882 * Flush the cache after waiting for async I/O to finish. For new
10883 * locks, this is so that the process gets the latest bits from the
10884 * server. For unlocks, this is so that other clients see the
10885 * latest bits once the file has been unlocked. If currently dirty
10886 * pages can't be flushed, then don't allow a lock to be set. But
10887 * allow unlocks to succeed, to avoid having orphan locks on the
10888 * server.
10890 if (cmd != F_GETLK) {
10891 mutex_enter(&rp->r_statelock);
10892 while (rp->r_count > 0) {
10893 if (intr) {
10894 klwp_t *lwp = ttolwp(curthread);
10896 if (lwp != NULL)
10897 lwp->lwp_nostop++;
10898 if (cv_wait_sig(&rp->r_cv,
10899 &rp->r_statelock) == 0) {
10900 if (lwp != NULL)
10901 lwp->lwp_nostop--;
10902 rc = EINTR;
10903 break;
10905 if (lwp != NULL)
10906 lwp->lwp_nostop--;
10907 } else {
10908 cv_wait(&rp->r_cv, &rp->r_statelock);
10911 mutex_exit(&rp->r_statelock);
10912 if (rc != 0)
10913 goto done;
10914 error = nfs4_putpage(vp, 0, 0, B_INVAL, cr, ct);
10915 if (error) {
10916 if (error == ENOSPC || error == EDQUOT) {
10917 mutex_enter(&rp->r_statelock);
10918 if (!rp->r_error)
10919 rp->r_error = error;
10920 mutex_exit(&rp->r_statelock);
10922 if (bfp->l_type != F_UNLCK) {
10923 rc = ENOLCK;
10924 goto done;
10930 * Call the lock manager to do the real work of contacting
10931 * the server and obtaining the lock.
10933 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10934 cr, &e, NULL, NULL);
10935 rc = e.error;
10937 if (rc == 0)
10938 nfs4_lockcompletion(vp, cmd);
10940 done:
10941 nfs_rw_exit(&rp->r_lkserlock);
10943 return (rc);
10947 * Free storage space associated with the specified vnode. The portion
10948 * to be freed is specified by bfp->l_start and bfp->l_len (already
10949 * normalized to a "whence" of 0).
10951 * This is an experimental facility whose continued existence is not
10952 * guaranteed. Currently, we only support the special case
10953 * of l_len == 0, meaning free to end of file.
10955 /* ARGSUSED */
10956 static int
10957 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10958 offset_t offset, cred_t *cr, caller_context_t *ct)
10960 int error;
10962 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10963 return (EIO);
10964 ASSERT(vp->v_type == VREG);
10965 if (cmd != F_FREESP)
10966 return (EINVAL);
10968 error = convoff(vp, bfp, 0, offset);
10969 if (!error) {
10970 ASSERT(bfp->l_start >= 0);
10971 if (bfp->l_len == 0) {
10972 struct vattr va;
10974 va.va_mask = VATTR_SIZE;
10975 va.va_size = bfp->l_start;
10976 error = nfs4setattr(vp, &va, 0, cr, NULL);
10978 if (error == 0 && bfp->l_start == 0)
10979 vnevent_truncate(vp, ct);
10980 } else
10981 error = EINVAL;
10984 return (error);
10987 /* ARGSUSED */
10989 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
10991 rnode4_t *rp;
10992 rp = VTOR4(vp);
10994 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
10995 vp = RTOV4(rp);
10997 *vpp = vp;
10998 return (0);
11002 * Setup and add an address space callback to do the work of the delmap call.
11003 * The callback will (and must be) deleted in the actual callback function.
11005 * This is done in order to take care of the problem that we have with holding
11006 * the address space's a_lock for a long period of time (e.g. if the NFS server
11007 * is down). Callbacks will be executed in the address space code while the
11008 * a_lock is not held. Holding the address space's a_lock causes things such
11009 * as ps and fork to hang because they are trying to acquire this lock as well.
11011 /* ARGSUSED */
11012 static int
11013 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11014 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11015 caller_context_t *ct)
11017 int caller_found;
11018 int error;
11019 rnode4_t *rp;
11020 nfs4_delmap_args_t *dmapp;
11021 nfs4_delmapcall_t *delmap_call;
11023 if (vp->v_flag & VNOMAP)
11024 return (ENOSYS);
11027 * A process may not change zones if it has NFS pages mmap'ed
11028 * in, so we can't legitimately get here from the wrong zone.
11030 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11032 rp = VTOR4(vp);
11035 * The way that the address space of this process deletes its mapping
11036 * of this file is via the following call chains:
11037 * - as_free()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11038 * - as_unmap()->segop_unmap()/segvn_unmap()->fop_delmap()/nfs4_delmap()
11040 * With the use of address space callbacks we are allowed to drop the
11041 * address space lock, a_lock, while executing the NFS operations that
11042 * need to go over the wire. Returning EAGAIN to the caller of this
11043 * function is what drives the execution of the callback that we add
11044 * below. The callback will be executed by the address space code
11045 * after dropping the a_lock. When the callback is finished, since
11046 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11047 * is called again on the same segment to finish the rest of the work
11048 * that needs to happen during unmapping.
11050 * This action of calling back into the segment driver causes
11051 * nfs4_delmap() to get called again, but since the callback was
11052 * already executed at this point, it already did the work and there
11053 * is nothing left for us to do.
11055 * To Summarize:
11056 * - The first time nfs4_delmap is called by the current thread is when
11057 * we add the caller associated with this delmap to the delmap caller
11058 * list, add the callback, and return EAGAIN.
11059 * - The second time in this call chain when nfs4_delmap is called we
11060 * will find this caller in the delmap caller list and realize there
11061 * is no more work to do thus removing this caller from the list and
11062 * returning the error that was set in the callback execution.
11064 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11065 if (caller_found) {
11067 * 'error' is from the actual delmap operations. To avoid
11068 * hangs, we need to handle the return of EAGAIN differently
11069 * since this is what drives the callback execution.
11070 * In this case, we don't want to return EAGAIN and do the
11071 * callback execution because there are none to execute.
11073 if (error == EAGAIN)
11074 return (0);
11075 else
11076 return (error);
11079 /* current caller was not in the list */
11080 delmap_call = nfs4_init_delmapcall();
11082 mutex_enter(&rp->r_statelock);
11083 list_insert_tail(&rp->r_indelmap, delmap_call);
11084 mutex_exit(&rp->r_statelock);
11086 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11088 dmapp->vp = vp;
11089 dmapp->off = off;
11090 dmapp->addr = addr;
11091 dmapp->len = len;
11092 dmapp->prot = prot;
11093 dmapp->maxprot = maxprot;
11094 dmapp->flags = flags;
11095 dmapp->cr = cr;
11096 dmapp->caller = delmap_call;
11098 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11099 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11101 return (error ? error : EAGAIN);
11104 static nfs4_delmapcall_t *
11105 nfs4_init_delmapcall()
11107 nfs4_delmapcall_t *delmap_call;
11109 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11110 delmap_call->call_id = curthread;
11111 delmap_call->error = 0;
11113 return (delmap_call);
11116 static void
11117 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11119 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11123 * Searches for the current delmap caller (based on curthread) in the list of
11124 * callers. If it is found, we remove it and free the delmap caller.
11125 * Returns:
11126 * 0 if the caller wasn't found
11127 * 1 if the caller was found, removed and freed. *errp will be set
11128 * to what the result of the delmap was.
11130 static int
11131 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11133 nfs4_delmapcall_t *delmap_call;
11136 * If the list doesn't exist yet, we create it and return
11137 * that the caller wasn't found. No list = no callers.
11139 mutex_enter(&rp->r_statelock);
11140 if (!(rp->r_flags & R4DELMAPLIST)) {
11141 /* The list does not exist */
11142 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11143 offsetof(nfs4_delmapcall_t, call_node));
11144 rp->r_flags |= R4DELMAPLIST;
11145 mutex_exit(&rp->r_statelock);
11146 return (0);
11147 } else {
11148 /* The list exists so search it */
11149 for (delmap_call = list_head(&rp->r_indelmap);
11150 delmap_call != NULL;
11151 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11152 if (delmap_call->call_id == curthread) {
11153 /* current caller is in the list */
11154 *errp = delmap_call->error;
11155 list_remove(&rp->r_indelmap, delmap_call);
11156 mutex_exit(&rp->r_statelock);
11157 nfs4_free_delmapcall(delmap_call);
11158 return (1);
11162 mutex_exit(&rp->r_statelock);
11163 return (0);
11167 * Remove some pages from an mmap'd vnode. Just update the
11168 * count of pages. If doing close-to-open, then flush and
11169 * commit all of the pages associated with this file.
11170 * Otherwise, start an asynchronous page flush to write out
11171 * any dirty pages. This will also associate a credential
11172 * with the rnode which can be used to write the pages.
11174 /* ARGSUSED */
11175 static void
11176 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11178 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11179 rnode4_t *rp;
11180 mntinfo4_t *mi;
11181 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11183 rp = VTOR4(dmapp->vp);
11184 mi = VTOMI4(dmapp->vp);
11186 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11187 ASSERT(rp->r_mapcnt >= 0);
11190 * Initiate a page flush and potential commit if there are
11191 * pages, the file system was not mounted readonly, the segment
11192 * was mapped shared, and the pages themselves were writeable.
11194 if (nfs4_has_pages(dmapp->vp) &&
11195 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11196 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11197 mutex_enter(&rp->r_statelock);
11198 rp->r_flags |= R4DIRTY;
11199 mutex_exit(&rp->r_statelock);
11200 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11201 dmapp->len, dmapp->cr);
11202 if (!e.error) {
11203 mutex_enter(&rp->r_statelock);
11204 e.error = rp->r_error;
11205 rp->r_error = 0;
11206 mutex_exit(&rp->r_statelock);
11208 } else
11209 e.error = 0;
11211 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11212 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11213 B_INVAL, dmapp->cr, NULL);
11215 if (e.error) {
11216 e.stat = puterrno4(e.error);
11217 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11218 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11219 dmapp->caller->error = e.error;
11222 /* Check to see if we need to close the file */
11224 if (dmapp->vp->v_type == VREG) {
11225 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11226 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11228 if (e.error != 0 || e.stat != NFS4_OK) {
11230 * Since it is possible that e.error == 0 and
11231 * e.stat != NFS4_OK (and vice versa),
11232 * we do the proper checking in order to get both
11233 * e.error and e.stat reporting the correct info.
11235 if (e.stat == NFS4_OK)
11236 e.stat = puterrno4(e.error);
11237 if (e.error == 0)
11238 e.error = geterrno4(e.stat);
11240 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11241 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11242 dmapp->caller->error = e.error;
11246 (void) as_delete_callback(as, arg);
11247 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11251 static uint_t
11252 fattr4_maxfilesize_to_bits(uint64_t ll)
11254 uint_t l = 1;
11256 if (ll == 0) {
11257 return (0);
11260 if (ll & 0xffffffff00000000) {
11261 l += 32; ll >>= 32;
11263 if (ll & 0xffff0000) {
11264 l += 16; ll >>= 16;
11266 if (ll & 0xff00) {
11267 l += 8; ll >>= 8;
11269 if (ll & 0xf0) {
11270 l += 4; ll >>= 4;
11272 if (ll & 0xc) {
11273 l += 2; ll >>= 2;
11275 if (ll & 0x2) {
11276 l += 1;
11278 return (l);
11281 static int
11282 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11284 vnode_t *avp = NULL;
11285 int error;
11287 if ((error = nfs4lookup_xattr(vp, "", &avp,
11288 LOOKUP_XATTR, cr)) == 0)
11289 error = do_xattr_exists_check(avp, valp, cr);
11290 if (avp)
11291 VN_RELE(avp);
11293 return (error);
11296 /* ARGSUSED */
11298 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11299 caller_context_t *ct)
11301 int error;
11302 hrtime_t t;
11303 rnode4_t *rp;
11304 nfs4_ga_res_t gar;
11305 nfs4_ga_ext_res_t ger;
11307 gar.n4g_ext_res = &ger;
11309 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11310 return (EIO);
11311 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11312 *valp = MAXPATHLEN;
11313 return (0);
11315 if (cmd == _PC_ACL_ENABLED) {
11316 *valp = _ACL_ACE_ENABLED;
11317 return (0);
11320 rp = VTOR4(vp);
11321 if (cmd == _PC_XATTR_EXISTS) {
11323 * The existence of the xattr directory is not sufficient
11324 * for determining whether generic user attributes exists.
11325 * The attribute directory could only be a transient directory
11326 * used for Solaris sysattr support. Do a small readdir
11327 * to verify if the only entries are sysattrs or not.
11329 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11330 * is NULL. Once the xadir vp exists, we can create xattrs,
11331 * and we don't have any way to update the "base" object's
11332 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11333 * could help out.
11335 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11336 rp->r_xattr_dir == NULL) {
11337 return (nfs4_have_xattrs(vp, valp, cr));
11339 } else { /* OLD CODE */
11340 if (ATTRCACHE4_VALID(vp)) {
11341 mutex_enter(&rp->r_statelock);
11342 if (rp->r_pathconf.pc4_cache_valid) {
11343 error = 0;
11344 switch (cmd) {
11345 case _PC_FILESIZEBITS:
11346 *valp =
11347 rp->r_pathconf.pc4_filesizebits;
11348 break;
11349 case _PC_LINK_MAX:
11350 *valp =
11351 rp->r_pathconf.pc4_link_max;
11352 break;
11353 case _PC_NAME_MAX:
11354 *valp =
11355 rp->r_pathconf.pc4_name_max;
11356 break;
11357 case _PC_CHOWN_RESTRICTED:
11358 *valp =
11359 rp->r_pathconf.pc4_chown_restricted;
11360 break;
11361 case _PC_NO_TRUNC:
11362 *valp =
11363 rp->r_pathconf.pc4_no_trunc;
11364 break;
11365 default:
11366 error = EINVAL;
11367 break;
11369 mutex_exit(&rp->r_statelock);
11370 #ifdef DEBUG
11371 nfs4_pathconf_cache_hits++;
11372 #endif
11373 return (error);
11375 mutex_exit(&rp->r_statelock);
11378 #ifdef DEBUG
11379 nfs4_pathconf_cache_misses++;
11380 #endif
11382 t = gethrtime();
11384 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11386 if (error) {
11387 mutex_enter(&rp->r_statelock);
11388 rp->r_pathconf.pc4_cache_valid = FALSE;
11389 rp->r_pathconf.pc4_xattr_valid = FALSE;
11390 mutex_exit(&rp->r_statelock);
11391 return (error);
11394 /* interpret the max filesize */
11395 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11396 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11398 /* Store the attributes we just received */
11399 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11401 switch (cmd) {
11402 case _PC_FILESIZEBITS:
11403 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11404 break;
11405 case _PC_LINK_MAX:
11406 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11407 break;
11408 case _PC_NAME_MAX:
11409 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11410 break;
11411 case _PC_CHOWN_RESTRICTED:
11412 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11413 break;
11414 case _PC_NO_TRUNC:
11415 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11416 break;
11417 case _PC_XATTR_EXISTS:
11418 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11419 if (error = nfs4_have_xattrs(vp, valp, cr))
11420 return (error);
11422 break;
11423 default:
11424 return (EINVAL);
11427 return (0);
11431 * Called by async thread to do synchronous pageio. Do the i/o, wait
11432 * for it to complete, and cleanup the page list when done.
11434 static int
11435 nfs4_sync_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11436 int flags, cred_t *cr)
11438 int error;
11440 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11442 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11443 if (flags & B_READ)
11444 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11445 else
11446 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11447 return (error);
11450 /* ARGSUSED */
11451 static int
11452 nfs4_pageio(vnode_t *vp, page_t *pp, uoff_t io_off, size_t io_len,
11453 int flags, cred_t *cr, caller_context_t *ct)
11455 int error;
11456 rnode4_t *rp;
11458 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11459 return (EIO);
11461 if (pp == NULL)
11462 return (EINVAL);
11464 rp = VTOR4(vp);
11465 mutex_enter(&rp->r_statelock);
11466 rp->r_count++;
11467 mutex_exit(&rp->r_statelock);
11469 if (flags & B_ASYNC) {
11470 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11471 nfs4_sync_pageio);
11472 } else
11473 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11474 mutex_enter(&rp->r_statelock);
11475 rp->r_count--;
11476 cv_broadcast(&rp->r_cv);
11477 mutex_exit(&rp->r_statelock);
11478 return (error);
11481 /* ARGSUSED */
11482 static void
11483 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11484 caller_context_t *ct)
11486 int error;
11487 rnode4_t *rp;
11488 page_t *plist;
11489 page_t *pptr;
11490 offset3 offset;
11491 count3 len;
11492 k_sigset_t smask;
11495 * We should get called with fl equal to either B_FREE or
11496 * B_INVAL. Any other value is illegal.
11498 * The page that we are either supposed to free or destroy
11499 * should be exclusive locked and its io lock should not
11500 * be held.
11502 ASSERT(fl == B_FREE || fl == B_INVAL);
11503 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11505 rp = VTOR4(vp);
11508 * If the page doesn't need to be committed or we shouldn't
11509 * even bother attempting to commit it, then just make sure
11510 * that the p_fsdata byte is clear and then either free or
11511 * destroy the page as appropriate.
11513 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11514 pp->p_fsdata = C_NOCOMMIT;
11515 if (fl == B_FREE)
11516 page_free(pp, dn);
11517 else
11518 page_destroy(pp, dn);
11519 return;
11523 * If there is a page invalidation operation going on, then
11524 * if this is one of the pages being destroyed, then just
11525 * clear the p_fsdata byte and then either free or destroy
11526 * the page as appropriate.
11528 mutex_enter(&rp->r_statelock);
11529 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11530 mutex_exit(&rp->r_statelock);
11531 pp->p_fsdata = C_NOCOMMIT;
11532 if (fl == B_FREE)
11533 page_free(pp, dn);
11534 else
11535 page_destroy(pp, dn);
11536 return;
11540 * If we are freeing this page and someone else is already
11541 * waiting to do a commit, then just unlock the page and
11542 * return. That other thread will take care of commiting
11543 * this page. The page can be freed sometime after the
11544 * commit has finished. Otherwise, if the page is marked
11545 * as delay commit, then we may be getting called from
11546 * pvn_write_done, one page at a time. This could result
11547 * in one commit per page, so we end up doing lots of small
11548 * commits instead of fewer larger commits. This is bad,
11549 * we want do as few commits as possible.
11551 if (fl == B_FREE) {
11552 if (rp->r_flags & R4COMMITWAIT) {
11553 page_unlock(pp);
11554 mutex_exit(&rp->r_statelock);
11555 return;
11557 if (pp->p_fsdata == C_DELAYCOMMIT) {
11558 pp->p_fsdata = C_COMMIT;
11559 page_unlock(pp);
11560 mutex_exit(&rp->r_statelock);
11561 return;
11566 * Check to see if there is a signal which would prevent an
11567 * attempt to commit the pages from being successful. If so,
11568 * then don't bother with all of the work to gather pages and
11569 * generate the unsuccessful RPC. Just return from here and
11570 * let the page be committed at some later time.
11572 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11573 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11574 sigunintr(&smask);
11575 page_unlock(pp);
11576 mutex_exit(&rp->r_statelock);
11577 return;
11579 sigunintr(&smask);
11582 * We are starting to need to commit pages, so let's try
11583 * to commit as many as possible at once to reduce the
11584 * overhead.
11586 * Set the `commit inprogress' state bit. We must
11587 * first wait until any current one finishes. Then
11588 * we initialize the c_pages list with this page.
11590 while (rp->r_flags & R4COMMIT) {
11591 rp->r_flags |= R4COMMITWAIT;
11592 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11593 rp->r_flags &= ~R4COMMITWAIT;
11595 rp->r_flags |= R4COMMIT;
11596 mutex_exit(&rp->r_statelock);
11597 ASSERT(rp->r_commit.c_pages == NULL);
11598 rp->r_commit.c_pages = pp;
11599 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11600 rp->r_commit.c_commlen = PAGESIZE;
11603 * Gather together all other pages which can be committed.
11604 * They will all be chained off r_commit.c_pages.
11606 nfs4_get_commit(vp);
11609 * Clear the `commit inprogress' status and disconnect
11610 * the list of pages to be committed from the rnode.
11611 * At this same time, we also save the starting offset
11612 * and length of data to be committed on the server.
11614 plist = rp->r_commit.c_pages;
11615 rp->r_commit.c_pages = NULL;
11616 offset = rp->r_commit.c_commbase;
11617 len = rp->r_commit.c_commlen;
11618 mutex_enter(&rp->r_statelock);
11619 rp->r_flags &= ~R4COMMIT;
11620 cv_broadcast(&rp->r_commit.c_cv);
11621 mutex_exit(&rp->r_statelock);
11623 if (curproc == proc_pageout || curproc == proc_fsflush ||
11624 nfs_zone() != VTOMI4(vp)->mi_zone) {
11625 nfs4_async_commit(vp, plist, offset, len,
11626 cr, do_nfs4_async_commit);
11627 return;
11631 * Actually generate the COMMIT op over the wire operation.
11633 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11636 * If we got an error during the commit, just unlock all
11637 * of the pages. The pages will get retransmitted to the
11638 * server during a putpage operation.
11640 if (error) {
11641 while (plist != NULL) {
11642 pptr = plist;
11643 page_sub(&plist, pptr);
11644 page_unlock(pptr);
11646 return;
11650 * We've tried as hard as we can to commit the data to stable
11651 * storage on the server. We just unlock the rest of the pages
11652 * and clear the commit required state. They will be put
11653 * onto the tail of the cachelist if they are nolonger
11654 * mapped.
11656 while (plist != pp) {
11657 pptr = plist;
11658 page_sub(&plist, pptr);
11659 pptr->p_fsdata = C_NOCOMMIT;
11660 page_unlock(pptr);
11664 * It is possible that nfs4_commit didn't return error but
11665 * some other thread has modified the page we are going
11666 * to free/destroy.
11667 * In this case we need to rewrite the page. Do an explicit check
11668 * before attempting to free/destroy the page. If modified, needs to
11669 * be rewritten so unlock the page and return.
11671 if (hat_ismod(pp)) {
11672 pp->p_fsdata = C_NOCOMMIT;
11673 page_unlock(pp);
11674 return;
11678 * Now, as appropriate, either free or destroy the page
11679 * that we were called with.
11681 pp->p_fsdata = C_NOCOMMIT;
11682 if (fl == B_FREE)
11683 page_free(pp, dn);
11684 else
11685 page_destroy(pp, dn);
11689 * Commit requires that the current fh be the file written to.
11690 * The compound op structure is:
11691 * PUTFH(file), COMMIT
11693 static int
11694 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11696 COMPOUND4args_clnt args;
11697 COMPOUND4res_clnt res;
11698 COMMIT4res *cm_res;
11699 nfs_argop4 argop[2];
11700 nfs_resop4 *resop;
11701 int doqueue;
11702 mntinfo4_t *mi;
11703 rnode4_t *rp;
11704 cred_t *cred_otw = NULL;
11705 bool_t needrecov = FALSE;
11706 nfs4_recov_state_t recov_state;
11707 nfs4_open_stream_t *osp = NULL;
11708 bool_t first_time = TRUE; /* first time getting OTW cred */
11709 bool_t last_time = FALSE; /* last time getting OTW cred */
11710 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11712 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11714 rp = VTOR4(vp);
11716 mi = VTOMI4(vp);
11717 recov_state.rs_flags = 0;
11718 recov_state.rs_num_retry_despite_err = 0;
11719 get_commit_cred:
11721 * Releases the osp, if a valid open stream is provided.
11722 * Puts a hold on the cred_otw and the new osp (if found).
11724 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11725 &first_time, &last_time);
11726 args.ctag = TAG_COMMIT;
11727 recov_retry:
11729 * Commit ops: putfh file; commit
11731 args.array_len = 2;
11732 args.array = argop;
11734 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11735 &recov_state, NULL);
11736 if (e.error) {
11737 crfree(cred_otw);
11738 if (osp != NULL)
11739 open_stream_rele(osp, rp);
11740 return (e.error);
11743 /* putfh directory */
11744 argop[0].argop = OP_CPUTFH;
11745 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11747 /* commit */
11748 argop[1].argop = OP_COMMIT;
11749 argop[1].nfs_argop4_u.opcommit.offset = offset;
11750 argop[1].nfs_argop4_u.opcommit.count = count;
11752 doqueue = 1;
11753 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11755 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11756 if (!needrecov && e.error) {
11757 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11758 needrecov);
11759 crfree(cred_otw);
11760 if (e.error == EACCES && last_time == FALSE)
11761 goto get_commit_cred;
11762 if (osp != NULL)
11763 open_stream_rele(osp, rp);
11764 return (e.error);
11767 if (needrecov) {
11768 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11769 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11770 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11771 &recov_state, needrecov);
11772 if (!e.error)
11773 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11774 goto recov_retry;
11776 if (e.error) {
11777 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11778 &recov_state, needrecov);
11779 crfree(cred_otw);
11780 if (osp != NULL)
11781 open_stream_rele(osp, rp);
11782 return (e.error);
11784 /* fall through for res.status case */
11787 if (res.status) {
11788 e.error = geterrno4(res.status);
11789 if (e.error == EACCES && last_time == FALSE) {
11790 crfree(cred_otw);
11791 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11792 &recov_state, needrecov);
11793 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11794 goto get_commit_cred;
11797 * Can't do a nfs4_purge_stale_fh here because this
11798 * can cause a deadlock. nfs4_commit can
11799 * be called from nfs4_dispose which can be called
11800 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11801 * can call back to pvn_vplist_dirty.
11803 if (e.error == ESTALE) {
11804 mutex_enter(&rp->r_statelock);
11805 rp->r_flags |= R4STALE;
11806 if (!rp->r_error)
11807 rp->r_error = e.error;
11808 mutex_exit(&rp->r_statelock);
11809 PURGE_ATTRCACHE4(vp);
11810 } else {
11811 mutex_enter(&rp->r_statelock);
11812 if (!rp->r_error)
11813 rp->r_error = e.error;
11814 mutex_exit(&rp->r_statelock);
11816 } else {
11817 ASSERT(rp->r_flags & R4HAVEVERF);
11818 resop = &res.array[1]; /* commit res */
11819 cm_res = &resop->nfs_resop4_u.opcommit;
11820 mutex_enter(&rp->r_statelock);
11821 if (cm_res->writeverf == rp->r_writeverf) {
11822 mutex_exit(&rp->r_statelock);
11823 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11824 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11825 &recov_state, needrecov);
11826 crfree(cred_otw);
11827 if (osp != NULL)
11828 open_stream_rele(osp, rp);
11829 return (0);
11831 nfs4_set_mod(vp);
11832 rp->r_writeverf = cm_res->writeverf;
11833 mutex_exit(&rp->r_statelock);
11834 e.error = NFS_VERF_MISMATCH;
11837 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11838 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11839 crfree(cred_otw);
11840 if (osp != NULL)
11841 open_stream_rele(osp, rp);
11843 return (e.error);
11846 static void
11847 nfs4_set_mod(vnode_t *vp)
11849 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11851 /* make sure we're looking at the master vnode, not a shadow */
11852 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11856 * This function is used to gather a page list of the pages which
11857 * can be committed on the server.
11859 * The calling thread must have set R4COMMIT. This bit is used to
11860 * serialize access to the commit structure in the rnode. As long
11861 * as the thread has set R4COMMIT, then it can manipulate the commit
11862 * structure without requiring any other locks.
11864 * When this function is called from nfs4_dispose() the page passed
11865 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11866 * will skip it. This is not a problem since we initially add the
11867 * page to the r_commit page list.
11870 static void
11871 nfs4_get_commit(vnode_t *vp)
11873 rnode4_t *rp;
11874 page_t *pp;
11876 rp = VTOR4(vp);
11878 ASSERT(rp->r_flags & R4COMMIT);
11880 /* make sure we're looking at the master vnode, not a shadow */
11882 if (IS_SHADOW(vp, rp))
11883 vp = RTOV4(rp);
11885 vmobject_lock(&vp->v_object);
11888 * Step through all of the pages associated with this vnode
11889 * looking for pages which need to be committed.
11891 for (pp = vmobject_get_head(&vp->v_object);
11892 pp != NULL;
11893 pp = vmobject_get_next(&vp->v_object, pp)) {
11894 /* Skip marker pages. */
11895 if (PP_ISPVN_TAG(pp))
11896 continue;
11899 * First short-cut everything (without the page_lock)
11900 * and see if this page does not need to be committed
11901 * or is modified if so then we'll just skip it.
11903 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11904 continue;
11907 * Attempt to lock the page. If we can't, then
11908 * someone else is messing with it or we have been
11909 * called from nfs4_dispose and this is the page that
11910 * nfs4_dispose was called with.. anyway just skip it.
11912 if (!page_trylock(pp, SE_EXCL))
11913 continue;
11916 * Lets check again now that we have the page lock.
11918 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11919 page_unlock(pp);
11920 continue;
11923 /* this had better not be a free page */
11924 ASSERT(PP_ISFREE(pp) == 0);
11927 * The page needs to be committed and we locked it.
11928 * Update the base and length parameters and add it
11929 * to r_pages.
11931 if (rp->r_commit.c_pages == NULL) {
11932 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11933 rp->r_commit.c_commlen = PAGESIZE;
11934 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11935 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11936 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11937 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11938 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11939 <= pp->p_offset) {
11940 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11941 rp->r_commit.c_commbase + PAGESIZE;
11943 page_add(&rp->r_commit.c_pages, pp);
11946 vmobject_unlock(&vp->v_object);
11950 * This routine is used to gather together a page list of the pages
11951 * which are to be committed on the server. This routine must not
11952 * be called if the calling thread holds any locked pages.
11954 * The calling thread must have set R4COMMIT. This bit is used to
11955 * serialize access to the commit structure in the rnode. As long
11956 * as the thread has set R4COMMIT, then it can manipulate the commit
11957 * structure without requiring any other locks.
11959 static void
11960 nfs4_get_commit_range(vnode_t *vp, uoff_t soff, size_t len)
11963 rnode4_t *rp;
11964 page_t *pp;
11965 uoff_t end;
11966 uoff_t off;
11967 ASSERT(len != 0);
11968 rp = VTOR4(vp);
11969 ASSERT(rp->r_flags & R4COMMIT);
11971 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11973 /* make sure we're looking at the master vnode, not a shadow */
11975 if (IS_SHADOW(vp, rp))
11976 vp = RTOV4(rp);
11979 * If there are no pages associated with this vnode, then
11980 * just return.
11982 if (!vn_has_cached_data(vp))
11983 return;
11985 * Calculate the ending offset.
11987 end = soff + len;
11988 for (off = soff; off < end; off += PAGESIZE) {
11990 * Lookup each page by vp, offset.
11992 if ((pp = page_lookup_nowait(&vp->v_object, off, SE_EXCL)) == NULL)
11993 continue;
11995 * If this page does not need to be committed or is
11996 * modified, then just skip it.
11998 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11999 page_unlock(pp);
12000 continue;
12003 ASSERT(PP_ISFREE(pp) == 0);
12005 * The page needs to be committed and we locked it.
12006 * Update the base and length parameters and add it
12007 * to r_pages.
12009 if (rp->r_commit.c_pages == NULL) {
12010 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12011 rp->r_commit.c_commlen = PAGESIZE;
12012 } else {
12013 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12014 rp->r_commit.c_commbase + PAGESIZE;
12016 page_add(&rp->r_commit.c_pages, pp);
12021 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12022 * Flushes and commits data to the server.
12024 static int
12025 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12027 int error;
12028 verifier4 write_verf;
12029 rnode4_t *rp = VTOR4(vp);
12031 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12034 * Flush the data portion of the file and then commit any
12035 * portions which need to be committed. This may need to
12036 * be done twice if the server has changed state since
12037 * data was last written. The data will need to be
12038 * rewritten to the server and then a new commit done.
12040 * In fact, this may need to be done several times if the
12041 * server is having problems and crashing while we are
12042 * attempting to do this.
12045 top:
12047 * Do a flush based on the poff and plen arguments. This
12048 * will synchronously write out any modified pages in the
12049 * range specified by (poff, plen). This starts all of the
12050 * i/o operations which will be waited for in the next
12051 * call to nfs4_putpage
12054 mutex_enter(&rp->r_statelock);
12055 write_verf = rp->r_writeverf;
12056 mutex_exit(&rp->r_statelock);
12058 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12059 if (error == EAGAIN)
12060 error = 0;
12063 * Do a flush based on the poff and plen arguments. This
12064 * will synchronously write out any modified pages in the
12065 * range specified by (poff, plen) and wait until all of
12066 * the asynchronous i/o's in that range are done as well.
12068 if (!error)
12069 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12071 if (error)
12072 return (error);
12074 mutex_enter(&rp->r_statelock);
12075 if (rp->r_writeverf != write_verf) {
12076 mutex_exit(&rp->r_statelock);
12077 goto top;
12079 mutex_exit(&rp->r_statelock);
12082 * Now commit any pages which might need to be committed.
12083 * If the error, NFS_VERF_MISMATCH, is returned, then
12084 * start over with the flush operation.
12086 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12088 if (error == NFS_VERF_MISMATCH)
12089 goto top;
12091 return (error);
12095 * nfs4_commit_vp() will wait for other pending commits and
12096 * will either commit the whole file or a range, plen dictates
12097 * if we commit whole file. a value of zero indicates the whole
12098 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12100 static int
12101 nfs4_commit_vp(vnode_t *vp, uoff_t poff, size_t plen,
12102 cred_t *cr, int wait_on_writes)
12104 rnode4_t *rp;
12105 page_t *plist;
12106 offset3 offset;
12107 count3 len;
12109 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12111 rp = VTOR4(vp);
12114 * before we gather commitable pages make
12115 * sure there are no outstanding async writes
12117 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12118 mutex_enter(&rp->r_statelock);
12119 while (rp->r_count > 0) {
12120 cv_wait(&rp->r_cv, &rp->r_statelock);
12122 mutex_exit(&rp->r_statelock);
12126 * Set the `commit inprogress' state bit. We must
12127 * first wait until any current one finishes.
12129 mutex_enter(&rp->r_statelock);
12130 while (rp->r_flags & R4COMMIT) {
12131 rp->r_flags |= R4COMMITWAIT;
12132 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12133 rp->r_flags &= ~R4COMMITWAIT;
12135 rp->r_flags |= R4COMMIT;
12136 mutex_exit(&rp->r_statelock);
12139 * Gather all of the pages which need to be
12140 * committed.
12142 if (plen == 0)
12143 nfs4_get_commit(vp);
12144 else
12145 nfs4_get_commit_range(vp, poff, plen);
12148 * Clear the `commit inprogress' bit and disconnect the
12149 * page list which was gathered by nfs4_get_commit.
12151 plist = rp->r_commit.c_pages;
12152 rp->r_commit.c_pages = NULL;
12153 offset = rp->r_commit.c_commbase;
12154 len = rp->r_commit.c_commlen;
12155 mutex_enter(&rp->r_statelock);
12156 rp->r_flags &= ~R4COMMIT;
12157 cv_broadcast(&rp->r_commit.c_cv);
12158 mutex_exit(&rp->r_statelock);
12161 * If any pages need to be committed, commit them and
12162 * then unlock them so that they can be freed some
12163 * time later.
12165 if (plist == NULL)
12166 return (0);
12169 * No error occurred during the flush portion
12170 * of this operation, so now attempt to commit
12171 * the data to stable storage on the server.
12173 * This will unlock all of the pages on the list.
12175 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12178 static int
12179 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12180 cred_t *cr)
12182 int error;
12183 page_t *pp;
12185 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12187 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12190 * If we got an error, then just unlock all of the pages
12191 * on the list.
12193 if (error) {
12194 while (plist != NULL) {
12195 pp = plist;
12196 page_sub(&plist, pp);
12197 page_unlock(pp);
12199 return (error);
12202 * We've tried as hard as we can to commit the data to stable
12203 * storage on the server. We just unlock the pages and clear
12204 * the commit required state. They will get freed later.
12206 while (plist != NULL) {
12207 pp = plist;
12208 page_sub(&plist, pp);
12209 pp->p_fsdata = C_NOCOMMIT;
12210 page_unlock(pp);
12213 return (error);
12216 static void
12217 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12218 cred_t *cr)
12221 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12224 /*ARGSUSED*/
12225 static int
12226 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12227 caller_context_t *ct)
12229 int error = 0;
12230 mntinfo4_t *mi;
12231 vattr_t va;
12232 vsecattr_t nfsace4_vsap;
12234 mi = VTOMI4(vp);
12235 if (nfs_zone() != mi->mi_zone)
12236 return (EIO);
12237 if (mi->mi_flags & MI4_ACL) {
12238 /* if we have a delegation, return it */
12239 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12240 (void) nfs4delegreturn(VTOR4(vp),
12241 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12243 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12244 NFS4_ACL_SET);
12245 if (error) /* EINVAL */
12246 return (error);
12248 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12250 * These are aclent_t type entries.
12252 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12253 vp->v_type == VDIR, FALSE);
12254 if (error)
12255 return (error);
12256 } else {
12258 * These are ace_t type entries.
12260 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12261 FALSE);
12262 if (error)
12263 return (error);
12265 bzero(&va, sizeof (va));
12266 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12267 vs_ace4_destroy(&nfsace4_vsap);
12268 return (error);
12270 return (ENOSYS);
12273 /* ARGSUSED */
12275 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12276 caller_context_t *ct)
12278 int error;
12279 mntinfo4_t *mi;
12280 nfs4_ga_res_t gar;
12281 rnode4_t *rp = VTOR4(vp);
12283 mi = VTOMI4(vp);
12284 if (nfs_zone() != mi->mi_zone)
12285 return (EIO);
12287 bzero(&gar, sizeof (gar));
12288 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12291 * vsecattr->vsa_mask holds the original acl request mask.
12292 * This is needed when determining what to return.
12293 * (See: nfs4_create_getsecattr_return())
12295 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12296 if (error) /* EINVAL */
12297 return (error);
12300 * If this is a referral stub, don't try to go OTW for an ACL
12302 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12303 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12305 if (mi->mi_flags & MI4_ACL) {
12307 * Check if the data is cached and the cache is valid. If it
12308 * is we don't go over the wire.
12310 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12311 mutex_enter(&rp->r_statelock);
12312 if (rp->r_secattr != NULL) {
12313 error = nfs4_create_getsecattr_return(
12314 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12315 rp->r_attr.va_gid,
12316 vp->v_type == VDIR);
12317 if (!error) { /* error == 0 - Success! */
12318 mutex_exit(&rp->r_statelock);
12319 return (error);
12322 mutex_exit(&rp->r_statelock);
12326 * The getattr otw call will always get both the acl, in
12327 * the form of a list of nfsace4's, and the number of acl
12328 * entries; independent of the value of gar.n4g_va.va_mask.
12330 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12331 if (error) {
12332 vs_ace4_destroy(&gar.n4g_vsa);
12333 if (error == ENOTSUP || error == EOPNOTSUPP)
12334 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12335 return (error);
12338 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12340 * No error was returned, but according to the response
12341 * bitmap, neither was an acl.
12343 vs_ace4_destroy(&gar.n4g_vsa);
12344 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12345 return (error);
12349 * Update the cache with the ACL.
12351 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12353 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12354 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12355 vp->v_type == VDIR);
12356 vs_ace4_destroy(&gar.n4g_vsa);
12357 if ((error) && (vsecattr->vsa_mask &
12358 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12359 (error != EACCES)) {
12360 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12362 return (error);
12364 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12365 return (error);
12369 * The function returns:
12370 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12371 * - EINVAL if the passed in "acl_mask" is an invalid request.
12373 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12374 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12376 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12377 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12378 * - We have a count field set without the corresponding acl field set. (e.g. -
12379 * VSA_ACECNT is set, but VSA_ACE is not)
12381 static int
12382 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12384 /* Shortcut the masks that are always valid. */
12385 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12386 return (0);
12387 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12388 return (0);
12390 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12392 * We can't have any VSA_ACL type stuff in the mask now.
12394 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12395 VSA_DFACLCNT))
12396 return (EINVAL);
12398 if (op == NFS4_ACL_SET) {
12399 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12400 return (EINVAL);
12404 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12406 * We can't have any VSA_ACE type stuff in the mask now.
12408 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12409 return (EINVAL);
12411 if (op == NFS4_ACL_SET) {
12412 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12413 return (EINVAL);
12415 if ((acl_mask & VSA_DFACLCNT) &&
12416 !(acl_mask & VSA_DFACL))
12417 return (EINVAL);
12420 return (0);
12424 * The theory behind creating the correct getsecattr return is simply this:
12425 * "Don't return anything that the caller is not expecting to have to free."
12427 static int
12428 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12429 uid_t uid, gid_t gid, int isdir)
12431 int error = 0;
12432 /* Save the mask since the translators modify it. */
12433 uint_t orig_mask = vsap->vsa_mask;
12435 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12436 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12438 if (error)
12439 return (error);
12442 * If the caller only asked for the ace count (VSA_ACECNT)
12443 * don't give them the full acl (VSA_ACE), free it.
12445 if (!orig_mask & VSA_ACE) {
12446 if (vsap->vsa_aclentp != NULL) {
12447 kmem_free(vsap->vsa_aclentp,
12448 vsap->vsa_aclcnt * sizeof (ace_t));
12449 vsap->vsa_aclentp = NULL;
12452 vsap->vsa_mask = orig_mask;
12454 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12455 VSA_DFACLCNT)) {
12456 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12457 isdir, FALSE);
12459 if (error)
12460 return (error);
12463 * If the caller only asked for the acl count (VSA_ACLCNT)
12464 * and/or the default acl count (VSA_DFACLCNT) don't give them
12465 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12467 if (!orig_mask & VSA_ACL) {
12468 if (vsap->vsa_aclentp != NULL) {
12469 kmem_free(vsap->vsa_aclentp,
12470 vsap->vsa_aclcnt * sizeof (aclent_t));
12471 vsap->vsa_aclentp = NULL;
12475 if (!orig_mask & VSA_DFACL) {
12476 if (vsap->vsa_dfaclentp != NULL) {
12477 kmem_free(vsap->vsa_dfaclentp,
12478 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12479 vsap->vsa_dfaclentp = NULL;
12482 vsap->vsa_mask = orig_mask;
12484 return (0);
12487 /* ARGSUSED */
12489 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12490 caller_context_t *ct)
12492 int error;
12494 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12495 return (EIO);
12497 * check for valid cmd parameter
12499 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12500 return (EINVAL);
12503 * Check access permissions
12505 if ((cmd & F_SHARE) &&
12506 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12507 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12508 return (EBADF);
12511 * If the filesystem is mounted using local locking, pass the
12512 * request off to the local share code.
12514 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12515 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12517 switch (cmd) {
12518 case F_SHARE:
12519 case F_UNSHARE:
12521 * This will be properly implemented later,
12522 * see RFE: 4823948 .
12524 error = EAGAIN;
12525 break;
12527 case F_HASREMOTELOCKS:
12529 * NFS client can't store remote locks itself
12531 shr->s_access = 0;
12532 error = 0;
12533 break;
12535 default:
12536 error = EINVAL;
12537 break;
12540 return (error);
12544 * Common code called by directory ops to update the attrcache
12546 static int
12547 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12548 hrtime_t t, vnode_t *vp, cred_t *cr)
12550 int error = 0;
12552 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12554 if (status != NFS4_OK) {
12555 /* getattr not done or failed */
12556 PURGE_ATTRCACHE4(vp);
12557 return (error);
12560 if (garp) {
12561 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12562 } else {
12563 PURGE_ATTRCACHE4(vp);
12565 return (error);
12569 * Update directory caches for directory modification ops (link, rename, etc.)
12570 * When dinfo is NULL, manage dircaches in the old way.
12572 static void
12573 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12574 dirattr_info_t *dinfo)
12576 rnode4_t *drp = VTOR4(dvp);
12578 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12580 /* Purge rddir cache for dir since it changed */
12581 if (drp->r_dir != NULL)
12582 nfs4_purge_rddir_cache(dvp);
12585 * If caller provided dinfo, then use it to manage dir caches.
12587 if (dinfo != NULL) {
12588 if (vp != NULL) {
12589 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12590 if (!VTOR4(vp)->created_v4) {
12591 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12592 dnlc_update(dvp, nm, vp);
12593 } else {
12595 * XXX don't update if the created_v4 flag is
12596 * set
12598 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12599 NFS4_DEBUG(nfs4_client_state_debug,
12600 (CE_NOTE, "nfs4_update_dircaches: "
12601 "don't update dnlc: created_v4 flag"));
12605 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12606 dinfo->di_cred, FALSE, cinfo);
12608 return;
12612 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12613 * Since caller modified dir but didn't receive post-dirmod-op dir
12614 * attrs, the dir's attrs must be purged.
12616 * XXX this check and dnlc update/purge should really be atomic,
12617 * XXX but can't use rnode statelock because it'll deadlock in
12618 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12619 * XXX does occur.
12621 * XXX We also may want to check that atomic is true in the
12622 * XXX change_info struct. If it is not, the change_info may
12623 * XXX reflect changes by more than one clients which means that
12624 * XXX our cache may not be valid.
12626 PURGE_ATTRCACHE4(dvp);
12627 if (drp->r_change == cinfo->before) {
12628 /* no changes took place in the directory prior to our link */
12629 if (vp != NULL) {
12630 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12631 if (!VTOR4(vp)->created_v4) {
12632 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12633 dnlc_update(dvp, nm, vp);
12634 } else {
12636 * XXX dont' update if the created_v4 flag
12637 * is set
12639 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12640 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12641 "nfs4_update_dircaches: don't"
12642 " update dnlc: created_v4 flag"));
12645 } else {
12646 /* Another client modified directory - purge its dnlc cache */
12647 dnlc_purge_vp(dvp);
12652 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12653 * file.
12655 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12656 * file (ie: client recovery) and otherwise set to FALSE.
12658 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12659 * initiated) calling functions.
12661 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12662 * of resending a 'lost' open request.
12664 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12665 * server that hands out BAD_SEQID on open confirm.
12667 * Errors are returned via the nfs4_error_t parameter.
12669 void
12670 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12671 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12672 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12674 COMPOUND4args_clnt args;
12675 COMPOUND4res_clnt res;
12676 nfs_argop4 argop[2];
12677 nfs_resop4 *resop;
12678 int doqueue = 1;
12679 mntinfo4_t *mi;
12680 OPEN_CONFIRM4args *open_confirm_args;
12681 int needrecov;
12683 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12684 #if DEBUG
12685 mutex_enter(&oop->oo_lock);
12686 ASSERT(oop->oo_seqid_inuse);
12687 mutex_exit(&oop->oo_lock);
12688 #endif
12690 recov_retry_confirm:
12691 nfs4_error_zinit(ep);
12692 *retry_open = FALSE;
12694 if (resend)
12695 args.ctag = TAG_OPEN_CONFIRM_LOST;
12696 else
12697 args.ctag = TAG_OPEN_CONFIRM;
12699 args.array_len = 2;
12700 args.array = argop;
12702 /* putfh target fh */
12703 argop[0].argop = OP_CPUTFH;
12704 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12706 argop[1].argop = OP_OPEN_CONFIRM;
12707 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12709 (*seqid) += 1;
12710 open_confirm_args->seqid = *seqid;
12711 open_confirm_args->open_stateid = *stateid;
12713 mi = VTOMI4(vp);
12715 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12717 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12718 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12721 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12722 if (!needrecov && ep->error)
12723 return;
12725 if (needrecov) {
12726 bool_t abort = FALSE;
12728 if (reopening_file == FALSE) {
12729 nfs4_bseqid_entry_t *bsep = NULL;
12731 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12732 bsep = nfs4_create_bseqid_entry(oop, NULL,
12733 vp, 0, args.ctag,
12734 open_confirm_args->seqid);
12736 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12737 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12738 if (bsep) {
12739 kmem_free(bsep, sizeof (*bsep));
12740 if (num_bseqid_retryp &&
12741 --(*num_bseqid_retryp) == 0)
12742 abort = TRUE;
12745 if ((ep->error == ETIMEDOUT ||
12746 res.status == NFS4ERR_RESOURCE) &&
12747 abort == FALSE && resend == FALSE) {
12748 if (!ep->error)
12749 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12751 ddi_sleep(confirm_retry_sec);
12752 goto recov_retry_confirm;
12754 /* State may have changed so retry the entire OPEN op */
12755 if (abort == FALSE)
12756 *retry_open = TRUE;
12757 else
12758 *retry_open = FALSE;
12759 if (!ep->error)
12760 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12761 return;
12764 if (res.status) {
12765 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12766 return;
12769 resop = &res.array[1]; /* open confirm res */
12770 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12771 stateid, sizeof (*stateid));
12773 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12777 * Return the credentials associated with a client state object. The
12778 * caller is responsible for freeing the credentials.
12781 static cred_t *
12782 state_to_cred(nfs4_open_stream_t *osp)
12784 cred_t *cr;
12787 * It's ok to not lock the open stream and open owner to get
12788 * the oo_cred since this is only written once (upon creation)
12789 * and will not change.
12791 cr = osp->os_open_owner->oo_cred;
12792 crhold(cr);
12794 return (cr);
12798 * nfs4_find_sysid
12800 * Find the sysid for the knetconfig associated with the given mi.
12802 static struct lm_sysid *
12803 nfs4_find_sysid(mntinfo4_t *mi)
12805 ASSERT(nfs_zone() == mi->mi_zone);
12808 * Switch from RDMA knconf to original mount knconf
12810 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12811 mi->mi_curr_serv->sv_hostname, NULL));
12814 #ifdef DEBUG
12816 * Return a string version of the call type for easy reading.
12818 static char *
12819 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12821 switch (ctype) {
12822 case NFS4_LCK_CTYPE_NORM:
12823 return ("NORMAL");
12824 case NFS4_LCK_CTYPE_RECLAIM:
12825 return ("RECLAIM");
12826 case NFS4_LCK_CTYPE_RESEND:
12827 return ("RESEND");
12828 case NFS4_LCK_CTYPE_REINSTATE:
12829 return ("REINSTATE");
12830 default:
12831 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12832 "type %d", ctype);
12833 return ("");
12836 #endif
12839 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12840 * Unlock requests don't have an over-the-wire locktype, so we just return
12841 * something non-threatening.
12844 static nfs_lock_type4
12845 flk_to_locktype(int cmd, int l_type)
12847 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12849 switch (l_type) {
12850 case F_UNLCK:
12851 return (READ_LT);
12852 case F_RDLCK:
12853 if (cmd == F_SETLK)
12854 return (READ_LT);
12855 else
12856 return (READW_LT);
12857 case F_WRLCK:
12858 if (cmd == F_SETLK)
12859 return (WRITE_LT);
12860 else
12861 return (WRITEW_LT);
12863 panic("flk_to_locktype");
12864 /*NOTREACHED*/
12868 * Do some preliminary checks for nfs4frlock.
12870 static int
12871 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12872 uoff_t offset)
12874 int error = 0;
12877 * If we are setting a lock, check that the file is opened
12878 * with the correct mode.
12880 if (cmd == F_SETLK || cmd == F_SETLKW) {
12881 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12882 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12883 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12884 "nfs4frlock_validate_args: file was opened with "
12885 "incorrect mode"));
12886 return (EBADF);
12890 /* Convert the offset. It may need to be restored before returning. */
12891 if (error = convoff(vp, flk, 0, offset)) {
12892 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12893 "nfs4frlock_validate_args: convoff => error= %d\n",
12894 error));
12895 return (error);
12898 return (error);
12902 * Set the flock64's lm_sysid for nfs4frlock.
12904 static int
12905 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12907 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12909 /* Find the lm_sysid */
12910 *lspp = nfs4_find_sysid(VTOMI4(vp));
12912 if (*lspp == NULL) {
12913 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12914 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12915 return (ENOLCK);
12918 flk->l_sysid = lm_sysidt(*lspp);
12920 return (0);
12924 * Do the remaining preliminary setup for nfs4frlock.
12926 static void
12927 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12928 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12929 cred_t **cred_otw)
12932 * set tick_delay to the base delay time.
12933 * (NFS4_BASE_WAIT_TIME is in secs)
12936 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12939 * If lock is relative to EOF, we need the newest length of the
12940 * file. Therefore invalidate the ATTR_CACHE.
12943 *whencep = flk->l_whence;
12945 if (*whencep == 2) /* SEEK_END */
12946 PURGE_ATTRCACHE4(vp);
12948 recov_statep->rs_flags = 0;
12949 recov_statep->rs_num_retry_despite_err = 0;
12950 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12954 * Initialize and allocate the data structures necessary for
12955 * the nfs4frlock call.
12956 * Allocates argsp's op array.
12958 static void
12959 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
12960 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
12961 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
12962 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
12964 int argoplist_size;
12965 int num_ops = 2;
12967 *retry = FALSE;
12968 *did_start_fop = FALSE;
12969 *skip_get_err = FALSE;
12970 lost_rqstp->lr_op = 0;
12971 argoplist_size = num_ops * sizeof (nfs_argop4);
12972 /* fill array with zero */
12973 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
12975 *argspp = argsp;
12976 *respp = NULL;
12978 argsp->array_len = num_ops;
12979 argsp->array = *argopp;
12981 /* initialize in case of error; will get real value down below */
12982 argsp->ctag = TAG_NONE;
12984 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
12985 *op_hintp = OH_LOCKU;
12986 else
12987 *op_hintp = OH_OTHER;
12991 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
12992 * the proper nfs4_server_t for this instance of nfs4frlock.
12993 * Returns 0 (success) or an errno value.
12995 static int
12996 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
12997 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
12998 bool_t *did_start_fop, bool_t *startrecovp)
13000 int error = 0;
13001 rnode4_t *rp;
13003 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13005 if (ctype == NFS4_LCK_CTYPE_NORM) {
13006 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13007 recov_statep, startrecovp);
13008 if (error)
13009 return (error);
13010 *did_start_fop = TRUE;
13011 } else {
13012 *did_start_fop = FALSE;
13013 *startrecovp = FALSE;
13016 if (!error) {
13017 rp = VTOR4(vp);
13019 /* If the file failed recovery, just quit. */
13020 mutex_enter(&rp->r_statelock);
13021 if (rp->r_flags & R4RECOVERR) {
13022 error = EIO;
13024 mutex_exit(&rp->r_statelock);
13027 return (error);
13031 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13032 * resend nfs4frlock call is initiated by the recovery framework.
13033 * Acquires the lop and oop seqid synchronization.
13035 static void
13036 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13037 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13038 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13039 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13041 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13042 int error;
13044 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13045 (CE_NOTE,
13046 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13047 ASSERT(resend_rqstp != NULL);
13048 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13049 resend_rqstp->lr_op == OP_LOCKU);
13051 *oopp = resend_rqstp->lr_oop;
13052 if (resend_rqstp->lr_oop) {
13053 open_owner_hold(resend_rqstp->lr_oop);
13054 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13055 ASSERT(error == 0); /* recov thread always succeeds */
13058 /* Must resend this lost lock/locku request. */
13059 ASSERT(resend_rqstp->lr_lop != NULL);
13060 *lopp = resend_rqstp->lr_lop;
13061 lock_owner_hold(resend_rqstp->lr_lop);
13062 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13063 ASSERT(error == 0); /* recov thread always succeeds */
13065 *ospp = resend_rqstp->lr_osp;
13066 if (*ospp)
13067 open_stream_hold(resend_rqstp->lr_osp);
13069 if (resend_rqstp->lr_op == OP_LOCK) {
13070 LOCK4args *lock_args;
13072 argop->argop = OP_LOCK;
13073 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13074 lock_args->locktype = resend_rqstp->lr_locktype;
13075 lock_args->reclaim =
13076 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13077 lock_args->offset = resend_rqstp->lr_flk->l_start;
13078 lock_args->length = resend_rqstp->lr_flk->l_len;
13079 if (lock_args->length == 0)
13080 lock_args->length = ~lock_args->length;
13081 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13082 mi2clientid(mi), &lock_args->locker);
13084 switch (resend_rqstp->lr_ctype) {
13085 case NFS4_LCK_CTYPE_RESEND:
13086 argsp->ctag = TAG_LOCK_RESEND;
13087 break;
13088 case NFS4_LCK_CTYPE_REINSTATE:
13089 argsp->ctag = TAG_LOCK_REINSTATE;
13090 break;
13091 case NFS4_LCK_CTYPE_RECLAIM:
13092 argsp->ctag = TAG_LOCK_RECLAIM;
13093 break;
13094 default:
13095 argsp->ctag = TAG_LOCK_UNKNOWN;
13096 break;
13098 } else {
13099 LOCKU4args *locku_args;
13100 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13102 argop->argop = OP_LOCKU;
13103 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13104 locku_args->locktype = READ_LT;
13105 locku_args->seqid = lop->lock_seqid + 1;
13106 mutex_enter(&lop->lo_lock);
13107 locku_args->lock_stateid = lop->lock_stateid;
13108 mutex_exit(&lop->lo_lock);
13109 locku_args->offset = resend_rqstp->lr_flk->l_start;
13110 locku_args->length = resend_rqstp->lr_flk->l_len;
13111 if (locku_args->length == 0)
13112 locku_args->length = ~locku_args->length;
13114 switch (resend_rqstp->lr_ctype) {
13115 case NFS4_LCK_CTYPE_RESEND:
13116 argsp->ctag = TAG_LOCKU_RESEND;
13117 break;
13118 case NFS4_LCK_CTYPE_REINSTATE:
13119 argsp->ctag = TAG_LOCKU_REINSTATE;
13120 break;
13121 default:
13122 argsp->ctag = TAG_LOCK_UNKNOWN;
13123 break;
13129 * Setup the LOCKT4 arguments.
13131 static void
13132 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13133 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13134 rnode4_t *rp)
13136 LOCKT4args *lockt_args;
13138 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13139 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13140 argop->argop = OP_LOCKT;
13141 argsp->ctag = TAG_LOCKT;
13142 lockt_args = &argop->nfs_argop4_u.oplockt;
13145 * The locktype will be READ_LT unless it's
13146 * a write lock. We do this because the Solaris
13147 * system call allows the combination of
13148 * F_UNLCK and F_GETLK* and so in that case the
13149 * unlock is mapped to a read.
13151 if (flk->l_type == F_WRLCK)
13152 lockt_args->locktype = WRITE_LT;
13153 else
13154 lockt_args->locktype = READ_LT;
13156 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13157 /* set the lock owner4 args */
13158 nfs4_setlockowner_args(&lockt_args->owner, rp,
13159 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13160 flk->l_pid);
13161 lockt_args->offset = flk->l_start;
13162 lockt_args->length = flk->l_len;
13163 if (flk->l_len == 0)
13164 lockt_args->length = ~lockt_args->length;
13166 *lockt_argsp = lockt_args;
13170 * If the client is holding a delegation, and the open stream to be used
13171 * with this lock request is a delegation open stream, then re-open the stream.
13172 * Sets the nfs4_error_t to all zeros unless the open stream has already
13173 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13174 * means the caller should retry (like a recovery retry).
13176 static void
13177 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13179 open_delegation_type4 dt;
13180 bool_t reopen_needed, force;
13181 nfs4_open_stream_t *osp;
13182 open_claim_type4 oclaim;
13183 rnode4_t *rp = VTOR4(vp);
13184 mntinfo4_t *mi = VTOMI4(vp);
13186 ASSERT(nfs_zone() == mi->mi_zone);
13188 nfs4_error_zinit(ep);
13190 mutex_enter(&rp->r_statev4_lock);
13191 dt = rp->r_deleg_type;
13192 mutex_exit(&rp->r_statev4_lock);
13194 if (dt != OPEN_DELEGATE_NONE) {
13195 nfs4_open_owner_t *oop;
13197 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13198 if (!oop) {
13199 ep->stat = NFS4ERR_IO;
13200 return;
13202 /* returns with 'os_sync_lock' held */
13203 osp = find_open_stream(oop, rp);
13204 if (!osp) {
13205 open_owner_rele(oop);
13206 ep->stat = NFS4ERR_IO;
13207 return;
13210 if (osp->os_failed_reopen) {
13211 NFS4_DEBUG((nfs4_open_stream_debug ||
13212 nfs4_client_lock_debug), (CE_NOTE,
13213 "nfs4frlock_check_deleg: os_failed_reopen set "
13214 "for osp %p, cr %p, rp %s", (void *)osp,
13215 (void *)cr, rnode4info(rp)));
13216 mutex_exit(&osp->os_sync_lock);
13217 open_stream_rele(osp, rp);
13218 open_owner_rele(oop);
13219 ep->stat = NFS4ERR_IO;
13220 return;
13224 * Determine whether a reopen is needed. If this
13225 * is a delegation open stream, then send the open
13226 * to the server to give visibility to the open owner.
13227 * Even if it isn't a delegation open stream, we need
13228 * to check if the previous open CLAIM_DELEGATE_CUR
13229 * was sufficient.
13232 reopen_needed = osp->os_delegation ||
13233 ((lt == F_RDLCK &&
13234 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13235 (lt == F_WRLCK &&
13236 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13238 mutex_exit(&osp->os_sync_lock);
13239 open_owner_rele(oop);
13241 if (reopen_needed) {
13243 * Always use CLAIM_PREVIOUS after server reboot.
13244 * The server will reject CLAIM_DELEGATE_CUR if
13245 * it is used during the grace period.
13247 mutex_enter(&mi->mi_lock);
13248 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13249 oclaim = CLAIM_PREVIOUS;
13250 force = TRUE;
13251 } else {
13252 oclaim = CLAIM_DELEGATE_CUR;
13253 force = FALSE;
13255 mutex_exit(&mi->mi_lock);
13257 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13258 if (ep->error == EAGAIN) {
13259 nfs4_error_zinit(ep);
13260 ep->stat = NFS4ERR_DELAY;
13263 open_stream_rele(osp, rp);
13264 osp = NULL;
13269 * Setup the LOCKU4 arguments.
13270 * Returns errors via the nfs4_error_t.
13271 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13272 * over-the-wire. The caller must release the
13273 * reference on *lopp.
13274 * NFS4ERR_DELAY caller should retry (like recovery retry)
13275 * (other) unrecoverable error.
13277 static void
13278 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13279 LOCKU4args **locku_argsp, flock64_t *flk,
13280 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13281 vnode_t *vp, int flag, uoff_t offset, cred_t *cr,
13282 bool_t *skip_get_err, bool_t *go_otwp)
13284 nfs4_lock_owner_t *lop = NULL;
13285 LOCKU4args *locku_args;
13286 pid_t pid;
13287 bool_t is_spec = FALSE;
13288 rnode4_t *rp = VTOR4(vp);
13290 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13291 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13293 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13294 if (ep->error || ep->stat)
13295 return;
13297 argop->argop = OP_LOCKU;
13298 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13299 argsp->ctag = TAG_LOCKU_REINSTATE;
13300 else
13301 argsp->ctag = TAG_LOCKU;
13302 locku_args = &argop->nfs_argop4_u.oplocku;
13303 *locku_argsp = locku_args;
13305 /* locktype should be set to any legal value */
13306 locku_args->locktype = READ_LT;
13308 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13309 flk->l_pid;
13312 * Get the lock owner stateid. If no lock owner
13313 * exists, return success.
13315 lop = find_lock_owner(rp, pid, LOWN_ANY);
13316 *lopp = lop;
13317 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13318 is_spec = TRUE;
13319 if (!lop || is_spec) {
13321 * No lock owner so no locks to unlock.
13322 * Return success. If there was a failed
13323 * reclaim earlier, the lock might still be
13324 * registered with the local locking code,
13325 * so notify it of the unlock.
13327 * If the lockowner is using a special stateid,
13328 * then the original lock request (that created
13329 * this lockowner) was never successful, so we
13330 * have no lock to undo OTW.
13332 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13333 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13334 "(%ld) so return success", (long)pid));
13336 if (ctype == NFS4_LCK_CTYPE_NORM)
13337 flk->l_pid = curproc->p_pid;
13338 nfs4_register_lock_locally(vp, flk, flag, offset);
13340 * Release our hold and NULL out so final_cleanup
13341 * doesn't try to end a lock seqid sync we
13342 * never started.
13344 if (is_spec) {
13345 lock_owner_rele(lop);
13346 *lopp = NULL;
13348 *skip_get_err = TRUE;
13349 *go_otwp = FALSE;
13350 return;
13353 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13354 if (ep->error == EAGAIN) {
13355 lock_owner_rele(lop);
13356 *lopp = NULL;
13357 return;
13360 mutex_enter(&lop->lo_lock);
13361 locku_args->lock_stateid = lop->lock_stateid;
13362 mutex_exit(&lop->lo_lock);
13363 locku_args->seqid = lop->lock_seqid + 1;
13365 /* leave the ref count on lop, rele after RPC call */
13367 locku_args->offset = flk->l_start;
13368 locku_args->length = flk->l_len;
13369 if (flk->l_len == 0)
13370 locku_args->length = ~locku_args->length;
13372 *go_otwp = TRUE;
13376 * Setup the LOCK4 arguments.
13378 * Returns errors via the nfs4_error_t.
13379 * NFS4_OK no problems
13380 * NFS4ERR_DELAY caller should retry (like recovery retry)
13381 * (other) unrecoverable error
13383 static void
13384 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13385 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13386 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13387 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13389 LOCK4args *lock_args;
13390 nfs4_open_owner_t *oop = NULL;
13391 nfs4_open_stream_t *osp = NULL;
13392 nfs4_lock_owner_t *lop = NULL;
13393 pid_t pid;
13394 rnode4_t *rp = VTOR4(vp);
13396 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13398 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13399 if (ep->error || ep->stat != NFS4_OK)
13400 return;
13402 argop->argop = OP_LOCK;
13403 if (ctype == NFS4_LCK_CTYPE_NORM)
13404 argsp->ctag = TAG_LOCK;
13405 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13406 argsp->ctag = TAG_RELOCK;
13407 else
13408 argsp->ctag = TAG_LOCK_REINSTATE;
13409 lock_args = &argop->nfs_argop4_u.oplock;
13410 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13411 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13413 * Get the lock owner. If no lock owner exists,
13414 * create a 'temporary' one and grab the open seqid
13415 * synchronization (which puts a hold on the open
13416 * owner and open stream).
13417 * This also grabs the lock seqid synchronization.
13419 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13420 ep->stat =
13421 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13423 if (ep->stat != NFS4_OK)
13424 goto out;
13426 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13427 &lock_args->locker);
13429 lock_args->offset = flk->l_start;
13430 lock_args->length = flk->l_len;
13431 if (flk->l_len == 0)
13432 lock_args->length = ~lock_args->length;
13433 *lock_argsp = lock_args;
13434 out:
13435 *oopp = oop;
13436 *ospp = osp;
13437 *lopp = lop;
13441 * After we get the reply from the server, record the proper information
13442 * for possible resend lock requests.
13444 static void
13445 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13446 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13447 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13448 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13450 bool_t unlock = (flk->l_type == F_UNLCK);
13452 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13453 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13454 ctype == NFS4_LCK_CTYPE_REINSTATE);
13456 if (error != 0 && !unlock) {
13457 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13458 nfs4_client_lock_debug), (CE_NOTE,
13459 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13460 " for lop %p", (void *)lop));
13461 ASSERT(lop != NULL);
13462 mutex_enter(&lop->lo_lock);
13463 lop->lo_pending_rqsts = 1;
13464 mutex_exit(&lop->lo_lock);
13467 lost_rqstp->lr_putfirst = FALSE;
13468 lost_rqstp->lr_op = 0;
13471 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13472 * recovery purposes so that the lock request that was sent
13473 * can be saved and re-issued later. Ditto for EIO from a forced
13474 * unmount. This is done to have the client's local locking state
13475 * match the v4 server's state; that is, the request was
13476 * potentially received and accepted by the server but the client
13477 * thinks it was not.
13479 if (error == ETIMEDOUT || error == EINTR ||
13480 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13481 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13482 nfs4_client_lock_debug), (CE_NOTE,
13483 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13484 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13485 (void *)lop, (void *)oop, (void *)osp));
13486 if (unlock)
13487 lost_rqstp->lr_op = OP_LOCKU;
13488 else {
13489 lost_rqstp->lr_op = OP_LOCK;
13490 lost_rqstp->lr_locktype = locktype;
13493 * Objects are held and rele'd via the recovery code.
13494 * See nfs4_save_lost_rqst.
13496 lost_rqstp->lr_vp = vp;
13497 lost_rqstp->lr_dvp = NULL;
13498 lost_rqstp->lr_oop = oop;
13499 lost_rqstp->lr_osp = osp;
13500 lost_rqstp->lr_lop = lop;
13501 lost_rqstp->lr_cr = cr;
13502 switch (ctype) {
13503 case NFS4_LCK_CTYPE_NORM:
13504 flk->l_pid = ttoproc(curthread)->p_pid;
13505 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13506 break;
13507 case NFS4_LCK_CTYPE_REINSTATE:
13508 lost_rqstp->lr_putfirst = TRUE;
13509 lost_rqstp->lr_ctype = ctype;
13510 break;
13511 default:
13512 break;
13514 lost_rqstp->lr_flk = flk;
13519 * Update lop's seqid. Also update the seqid stored in a resend request,
13520 * if any. (Some recovery errors increment the seqid, and we may have to
13521 * send the resend request again.)
13524 static void
13525 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13526 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13528 if (lock_args) {
13529 if (lock_args->locker.new_lock_owner == TRUE)
13530 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13531 else {
13532 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13533 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13535 } else if (locku_args) {
13536 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13537 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13542 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13543 * COMPOUND4 args/res for calls that need to retry.
13544 * Switches the *cred_otwp to base_cr.
13546 static void
13547 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13548 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13549 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13550 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13551 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13553 nfs4_open_owner_t *oop = *oopp;
13554 nfs4_open_stream_t *osp = *ospp;
13555 nfs4_lock_owner_t *lop = *lopp;
13556 nfs_argop4 *argop = (*argspp)->array;
13558 if (*did_start_fop) {
13559 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13560 needrecov);
13561 *did_start_fop = FALSE;
13563 ASSERT((*argspp)->array_len == 2);
13564 if (argop[1].argop == OP_LOCK)
13565 nfs4args_lock_free(&argop[1]);
13566 else if (argop[1].argop == OP_LOCKT)
13567 nfs4args_lockt_free(&argop[1]);
13568 kmem_free(argop, 2 * sizeof (nfs_argop4));
13569 if (!error)
13570 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13571 *argspp = NULL;
13572 *respp = NULL;
13574 if (lop) {
13575 nfs4_end_lock_seqid_sync(lop);
13576 lock_owner_rele(lop);
13577 *lopp = NULL;
13580 /* need to free up the reference on osp for lock args */
13581 if (osp != NULL) {
13582 open_stream_rele(osp, VTOR4(vp));
13583 *ospp = NULL;
13586 /* need to free up the reference on oop for lock args */
13587 if (oop != NULL) {
13588 nfs4_end_open_seqid_sync(oop);
13589 open_owner_rele(oop);
13590 *oopp = NULL;
13593 crfree(*cred_otwp);
13594 *cred_otwp = base_cr;
13595 crhold(*cred_otwp);
13599 * Function to process the client's recovery for nfs4frlock.
13600 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13602 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13603 * COMPOUND4 args/res for calls that need to retry.
13605 * Note: the rp's r_lkserlock is *not* dropped during this path.
13607 static bool_t
13608 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13609 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13610 LOCK4args *lock_args, LOCKU4args *locku_args,
13611 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13612 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13613 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13614 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13616 nfs4_open_owner_t *oop = *oopp;
13617 nfs4_open_stream_t *osp = *ospp;
13618 nfs4_lock_owner_t *lop = *lopp;
13620 bool_t abort, retry;
13622 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13623 ASSERT((*argspp) != NULL);
13624 ASSERT((*respp) != NULL);
13625 if (lock_args || locku_args)
13626 ASSERT(lop != NULL);
13628 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13629 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13631 retry = TRUE;
13632 abort = FALSE;
13633 if (needrecov) {
13634 nfs4_bseqid_entry_t *bsep = NULL;
13635 nfs_opnum4 op;
13637 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13639 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13640 seqid4 seqid;
13642 if (lock_args) {
13643 if (lock_args->locker.new_lock_owner == TRUE)
13644 seqid = lock_args->locker.locker4_u.
13645 open_owner.open_seqid;
13646 else
13647 seqid = lock_args->locker.locker4_u.
13648 lock_owner.lock_seqid;
13649 } else if (locku_args) {
13650 seqid = locku_args->seqid;
13651 } else {
13652 seqid = 0;
13655 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13656 flk->l_pid, (*argspp)->ctag, seqid);
13659 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13660 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13661 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13662 NULL, op, bsep, NULL, NULL);
13664 if (bsep)
13665 kmem_free(bsep, sizeof (*bsep));
13669 * Return that we do not want to retry the request for 3 cases:
13670 * 1. If we received EINTR or are bailing out because of a forced
13671 * unmount, we came into this code path just for the sake of
13672 * initiating recovery, we now need to return the error.
13673 * 2. If we have aborted recovery.
13674 * 3. We received NFS4ERR_BAD_SEQID.
13676 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13677 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13678 retry = FALSE;
13680 if (*did_start_fop == TRUE) {
13681 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13682 needrecov);
13683 *did_start_fop = FALSE;
13686 if (retry == TRUE) {
13687 nfs_argop4 *argop;
13689 argop = (*argspp)->array;
13690 ASSERT((*argspp)->array_len == 2);
13692 if (argop[1].argop == OP_LOCK)
13693 nfs4args_lock_free(&argop[1]);
13694 else if (argop[1].argop == OP_LOCKT)
13695 nfs4args_lockt_free(&argop[1]);
13696 kmem_free(argop, 2 * sizeof (nfs_argop4));
13697 if (!ep->error)
13698 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13699 *respp = NULL;
13700 *argspp = NULL;
13703 if (lop != NULL) {
13704 nfs4_end_lock_seqid_sync(lop);
13705 lock_owner_rele(lop);
13708 *lopp = NULL;
13710 /* need to free up the reference on osp for lock args */
13711 if (osp != NULL) {
13712 open_stream_rele(osp, rp);
13713 *ospp = NULL;
13716 /* need to free up the reference on oop for lock args */
13717 if (oop != NULL) {
13718 nfs4_end_open_seqid_sync(oop);
13719 open_owner_rele(oop);
13720 *oopp = NULL;
13723 return (retry);
13727 * Handles the successful reply from the server for nfs4frlock.
13729 static void
13730 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13731 vnode_t *vp, int flag, uoff_t offset,
13732 nfs4_lost_rqst_t *resend_rqstp)
13734 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13735 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13736 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13737 if (ctype == NFS4_LCK_CTYPE_NORM) {
13738 flk->l_pid = ttoproc(curthread)->p_pid;
13740 * We do not register lost locks locally in
13741 * the 'resend' case since the user/application
13742 * doesn't think we have the lock.
13744 ASSERT(!resend_rqstp);
13745 nfs4_register_lock_locally(vp, flk, flag, offset);
13751 * Handle the DENIED reply from the server for nfs4frlock.
13752 * Returns TRUE if we should retry the request; FALSE otherwise.
13754 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13755 * COMPOUND4 args/res for calls that need to retry. Can also
13756 * drop and regrab the r_lkserlock.
13758 static bool_t
13759 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13760 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13761 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13762 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13763 nfs4_recov_state_t *recov_statep, int needrecov,
13764 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13765 clock_t *tick_delayp, short *whencep, int *errorp,
13766 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13767 bool_t *skip_get_err)
13769 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13771 if (lock_args) {
13772 nfs4_open_owner_t *oop = *oopp;
13773 nfs4_open_stream_t *osp = *ospp;
13774 nfs4_lock_owner_t *lop = *lopp;
13775 int intr;
13778 * Blocking lock needs to sleep and retry from the request.
13780 * Do not block and wait for 'resend' or 'reinstate'
13781 * lock requests, just return the error.
13783 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13785 if (cmd == F_SETLKW) {
13786 rnode4_t *rp = VTOR4(vp);
13787 nfs_argop4 *argop = (*argspp)->array;
13789 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13791 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13792 recov_statep, needrecov);
13793 *did_start_fop = FALSE;
13794 ASSERT((*argspp)->array_len == 2);
13795 if (argop[1].argop == OP_LOCK)
13796 nfs4args_lock_free(&argop[1]);
13797 else if (argop[1].argop == OP_LOCKT)
13798 nfs4args_lockt_free(&argop[1]);
13799 kmem_free(argop, 2 * sizeof (nfs_argop4));
13800 if (*respp)
13801 xdr_free(xdr_COMPOUND4res_clnt,
13802 (caddr_t)*respp);
13803 *argspp = NULL;
13804 *respp = NULL;
13805 nfs4_end_lock_seqid_sync(lop);
13806 lock_owner_rele(lop);
13807 *lopp = NULL;
13808 if (osp != NULL) {
13809 open_stream_rele(osp, rp);
13810 *ospp = NULL;
13812 if (oop != NULL) {
13813 nfs4_end_open_seqid_sync(oop);
13814 open_owner_rele(oop);
13815 *oopp = NULL;
13818 nfs_rw_exit(&rp->r_lkserlock);
13820 intr = nfs4_block_and_wait(tick_delayp, rp);
13822 if (intr) {
13823 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13824 RW_WRITER, FALSE);
13825 *errorp = EINTR;
13826 return (FALSE);
13829 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13830 RW_WRITER, FALSE);
13833 * Make sure we are still safe to lock with
13834 * regards to mmapping.
13836 if (!nfs4_safelock(vp, flk, cr)) {
13837 *errorp = EAGAIN;
13838 return (FALSE);
13841 return (TRUE);
13843 if (ctype == NFS4_LCK_CTYPE_NORM)
13844 *errorp = EAGAIN;
13845 *skip_get_err = TRUE;
13846 flk->l_whence = 0;
13847 *whencep = 0;
13848 return (FALSE);
13849 } else if (lockt_args) {
13850 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13851 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13853 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13854 flk, lockt_args);
13856 /* according to NLM code */
13857 *errorp = 0;
13858 *whencep = 0;
13859 *skip_get_err = TRUE;
13860 return (FALSE);
13862 return (FALSE);
13866 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13868 static void
13869 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13871 switch (resp->status) {
13872 case NFS4ERR_ACCESS:
13873 case NFS4ERR_ADMIN_REVOKED:
13874 case NFS4ERR_BADHANDLE:
13875 case NFS4ERR_BAD_RANGE:
13876 case NFS4ERR_BAD_SEQID:
13877 case NFS4ERR_BAD_STATEID:
13878 case NFS4ERR_BADXDR:
13879 case NFS4ERR_DEADLOCK:
13880 case NFS4ERR_DELAY:
13881 case NFS4ERR_EXPIRED:
13882 case NFS4ERR_FHEXPIRED:
13883 case NFS4ERR_GRACE:
13884 case NFS4ERR_INVAL:
13885 case NFS4ERR_ISDIR:
13886 case NFS4ERR_LEASE_MOVED:
13887 case NFS4ERR_LOCK_NOTSUPP:
13888 case NFS4ERR_LOCK_RANGE:
13889 case NFS4ERR_MOVED:
13890 case NFS4ERR_NOFILEHANDLE:
13891 case NFS4ERR_NO_GRACE:
13892 case NFS4ERR_OLD_STATEID:
13893 case NFS4ERR_OPENMODE:
13894 case NFS4ERR_RECLAIM_BAD:
13895 case NFS4ERR_RECLAIM_CONFLICT:
13896 case NFS4ERR_RESOURCE:
13897 case NFS4ERR_SERVERFAULT:
13898 case NFS4ERR_STALE:
13899 case NFS4ERR_STALE_CLIENTID:
13900 case NFS4ERR_STALE_STATEID:
13901 return;
13902 default:
13903 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13904 "nfs4frlock_results_default: got unrecognizable "
13905 "res.status %d", resp->status));
13906 *errorp = NFS4ERR_INVAL;
13911 * The lock request was successful, so update the client's state.
13913 static void
13914 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13915 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13916 vnode_t *vp, flock64_t *flk, cred_t *cr,
13917 nfs4_lost_rqst_t *resend_rqstp)
13919 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13921 if (lock_args) {
13922 LOCK4res *lock_res;
13924 lock_res = &resop->nfs_resop4_u.oplock;
13925 /* update the stateid with server's response */
13927 if (lock_args->locker.new_lock_owner == TRUE) {
13928 mutex_enter(&lop->lo_lock);
13929 lop->lo_just_created = NFS4_PERM_CREATED;
13930 mutex_exit(&lop->lo_lock);
13933 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13936 * If the lock was the result of a resending a lost
13937 * request, we've synched up the stateid and seqid
13938 * with the server, but now the server might be out of sync
13939 * with what the application thinks it has for locks.
13940 * Clean that up here. It's unclear whether we should do
13941 * this even if the filesystem has been forcibly unmounted.
13942 * For most servers, it's probably wasted effort, but
13943 * RFC 7530 lets servers require that unlocks exactly match
13944 * the locks that are held.
13946 if (resend_rqstp != NULL &&
13947 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13948 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13949 } else {
13950 flk->l_whence = 0;
13952 } else if (locku_args) {
13953 LOCKU4res *locku_res;
13955 locku_res = &resop->nfs_resop4_u.oplocku;
13957 /* Update the stateid with the server's response */
13958 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
13959 } else if (lockt_args) {
13960 /* Switch the lock type to express success, see fcntl */
13961 flk->l_type = F_UNLCK;
13962 flk->l_whence = 0;
13967 * Do final cleanup before exiting nfs4frlock.
13968 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13969 * COMPOUND4 args/res for calls that haven't already.
13971 static void
13972 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
13973 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
13974 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
13975 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13976 short whence, uoff_t offset, struct lm_sysid *ls,
13977 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
13978 bool_t did_start_fop, bool_t skip_get_err,
13979 cred_t *cred_otw, cred_t *cred)
13981 mntinfo4_t *mi = VTOMI4(vp);
13982 rnode4_t *rp = VTOR4(vp);
13983 int error = *errorp;
13984 nfs_argop4 *argop;
13985 int do_flush_pages = 0;
13987 ASSERT(nfs_zone() == mi->mi_zone);
13989 * The client recovery code wants the raw status information,
13990 * so don't map the NFS status code to an errno value for
13991 * non-normal call types.
13993 if (ctype == NFS4_LCK_CTYPE_NORM) {
13994 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
13995 *errorp = geterrno4(resp->status);
13996 if (did_start_fop == TRUE)
13997 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
13998 needrecov);
14001 * We've established a new lock on the server, so invalidate
14002 * the pages associated with the vnode to get the most up to
14003 * date pages from the server after acquiring the lock. We
14004 * want to be sure that the read operation gets the newest data.
14005 * N.B.
14006 * We used to do this in nfs4frlock_results_ok but that doesn't
14007 * work since fop_putpage can call nfs4_commit which calls
14008 * nfs4_start_fop. We flush the pages below after calling
14009 * nfs4_end_fop above
14010 * The flush of the page cache must be done after
14011 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14013 if (!error && resp && resp->status == NFS4_OK)
14014 do_flush_pages = 1;
14016 if (argsp) {
14017 ASSERT(argsp->array_len == 2);
14018 argop = argsp->array;
14019 if (argop[1].argop == OP_LOCK)
14020 nfs4args_lock_free(&argop[1]);
14021 else if (argop[1].argop == OP_LOCKT)
14022 nfs4args_lockt_free(&argop[1]);
14023 kmem_free(argop, 2 * sizeof (nfs_argop4));
14024 if (resp)
14025 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14028 /* free the reference on the lock owner */
14029 if (lop != NULL) {
14030 nfs4_end_lock_seqid_sync(lop);
14031 lock_owner_rele(lop);
14034 /* need to free up the reference on osp for lock args */
14035 if (osp != NULL)
14036 open_stream_rele(osp, rp);
14038 /* need to free up the reference on oop for lock args */
14039 if (oop != NULL) {
14040 nfs4_end_open_seqid_sync(oop);
14041 open_owner_rele(oop);
14044 if (do_flush_pages)
14045 nfs4_flush_pages(vp, cred);
14047 (void) convoff(vp, flk, whence, offset);
14049 lm_rel_sysid(ls);
14052 * Record debug information in the event we get EINVAL.
14054 mutex_enter(&mi->mi_lock);
14055 if (*errorp == EINVAL && (lock_args || locku_args) &&
14056 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14057 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14058 zcmn_err(getzoneid(), CE_NOTE,
14059 "%s operation failed with "
14060 "EINVAL probably since the server, %s,"
14061 " doesn't support POSIX style locking",
14062 lock_args ? "LOCK" : "LOCKU",
14063 mi->mi_curr_serv->sv_hostname);
14064 mi->mi_flags |= MI4_LOCK_DEBUG;
14067 mutex_exit(&mi->mi_lock);
14069 if (cred_otw)
14070 crfree(cred_otw);
14074 * This calls the server and the local locking code.
14076 * Client locks are registerred locally by oring the sysid with
14077 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14078 * We need to distinguish between the two to avoid collision in case one
14079 * machine is used as both client and server.
14081 * Blocking lock requests will continually retry to acquire the lock
14082 * forever.
14084 * The ctype is defined as follows:
14085 * NFS4_LCK_CTYPE_NORM: normal lock request.
14087 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14088 * recovery, get the pid from flk instead of curproc, and don't reregister
14089 * the lock locally.
14091 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14092 * that we will use the information passed in via resend_rqstp to setup the
14093 * lock/locku request. This resend is the exact same request as the 'lost
14094 * lock', and is initiated by the recovery framework. A successful resend
14095 * request can initiate one or more reinstate requests.
14097 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14098 * does not trigger additional reinstate requests. This lock call type is
14099 * set for setting the v4 server's locking state back to match what the
14100 * client's local locking state is in the event of a received 'lost lock'.
14102 * Errors are returned via the nfs4_error_t parameter.
14104 void
14105 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14106 int flag, uoff_t offset, cred_t *cr, nfs4_error_t *ep,
14107 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14109 COMPOUND4args_clnt args, *argsp = NULL;
14110 COMPOUND4res_clnt res, *resp = NULL;
14111 nfs_argop4 *argop;
14112 nfs_resop4 *resop;
14113 rnode4_t *rp;
14114 int doqueue = 1;
14115 clock_t tick_delay; /* delay in clock ticks */
14116 struct lm_sysid *ls;
14117 LOCK4args *lock_args = NULL;
14118 LOCKU4args *locku_args = NULL;
14119 LOCKT4args *lockt_args = NULL;
14120 nfs4_open_owner_t *oop = NULL;
14121 nfs4_open_stream_t *osp = NULL;
14122 nfs4_lock_owner_t *lop = NULL;
14123 bool_t needrecov = FALSE;
14124 nfs4_recov_state_t recov_state;
14125 short whence;
14126 nfs4_op_hint_t op_hint;
14127 nfs4_lost_rqst_t lost_rqst;
14128 bool_t retry = FALSE;
14129 bool_t did_start_fop = FALSE;
14130 bool_t skip_get_err = FALSE;
14131 cred_t *cred_otw = NULL;
14132 bool_t recovonly; /* just queue request */
14133 int frc_no_reclaim = 0;
14134 #ifdef DEBUG
14135 char *name;
14136 #endif
14138 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14140 #ifdef DEBUG
14141 name = fn_name(VTOSV(vp)->sv_name);
14142 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14143 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14144 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14145 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14146 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14147 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14148 resend_rqstp ? "TRUE" : "FALSE"));
14149 kmem_free(name, MAXNAMELEN);
14150 #endif
14152 nfs4_error_zinit(ep);
14153 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14154 if (ep->error)
14155 return;
14156 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14157 if (ep->error)
14158 return;
14159 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14160 vp, cr, &cred_otw);
14162 recov_retry:
14163 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14164 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14165 rp = VTOR4(vp);
14167 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14168 &did_start_fop, &recovonly);
14170 if (ep->error)
14171 goto out;
14173 if (recovonly) {
14175 * Leave the request for the recovery system to deal with.
14177 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14178 ASSERT(cmd != F_GETLK);
14179 ASSERT(flk->l_type == F_UNLCK);
14181 nfs4_error_init(ep, EINTR);
14182 needrecov = TRUE;
14183 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14184 if (lop != NULL) {
14185 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14186 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14187 (void) nfs4_start_recovery(ep,
14188 VTOMI4(vp), vp, NULL, NULL,
14189 (lost_rqst.lr_op == OP_LOCK ||
14190 lost_rqst.lr_op == OP_LOCKU) ?
14191 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14192 lock_owner_rele(lop);
14193 lop = NULL;
14195 flk->l_pid = curproc->p_pid;
14196 nfs4_register_lock_locally(vp, flk, flag, offset);
14197 goto out;
14200 /* putfh directory fh */
14201 argop[0].argop = OP_CPUTFH;
14202 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14205 * Set up the over-the-wire arguments and get references to the
14206 * open owner, etc.
14209 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14210 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14211 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14212 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14213 } else {
14214 bool_t go_otw = TRUE;
14216 ASSERT(resend_rqstp == NULL);
14218 switch (cmd) {
14219 case F_GETLK:
14220 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14221 &lockt_args, argsp, flk, rp);
14222 break;
14223 case F_SETLKW:
14224 case F_SETLK:
14225 if (flk->l_type == F_UNLCK)
14226 nfs4frlock_setup_locku_args(ctype,
14227 &argop[1], &locku_args, flk,
14228 &lop, ep, argsp,
14229 vp, flag, offset, cr,
14230 &skip_get_err, &go_otw);
14231 else
14232 nfs4frlock_setup_lock_args(ctype,
14233 &lock_args, &oop, &osp, &lop, &argop[1],
14234 argsp, flk, cmd, vp, cr, ep);
14236 if (ep->error)
14237 goto out;
14239 switch (ep->stat) {
14240 case NFS4_OK:
14241 break;
14242 case NFS4ERR_DELAY:
14243 /* recov thread never gets this error */
14244 ASSERT(resend_rqstp == NULL);
14245 ASSERT(did_start_fop);
14247 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14248 &recov_state, TRUE);
14249 did_start_fop = FALSE;
14250 if (argop[1].argop == OP_LOCK)
14251 nfs4args_lock_free(&argop[1]);
14252 else if (argop[1].argop == OP_LOCKT)
14253 nfs4args_lockt_free(&argop[1]);
14254 kmem_free(argop, 2 * sizeof (nfs_argop4));
14255 argsp = NULL;
14256 goto recov_retry;
14257 default:
14258 ep->error = EIO;
14259 goto out;
14261 break;
14262 default:
14263 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14264 "nfs4_frlock: invalid cmd %d", cmd));
14265 ep->error = EINVAL;
14266 goto out;
14269 if (!go_otw)
14270 goto out;
14273 /* XXX should we use the local reclock as a cache ? */
14275 * Unregister the lock with the local locking code before
14276 * contacting the server. This avoids a potential race where
14277 * another process gets notified that it has been granted a lock
14278 * before we can unregister ourselves locally.
14280 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14281 if (ctype == NFS4_LCK_CTYPE_NORM)
14282 flk->l_pid = ttoproc(curthread)->p_pid;
14283 nfs4_register_lock_locally(vp, flk, flag, offset);
14287 * Send the server the lock request. Continually loop with a delay
14288 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14290 resp = &res;
14292 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14293 (CE_NOTE,
14294 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14295 rnode4info(rp)));
14297 if (lock_args && frc_no_reclaim) {
14298 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14299 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14300 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14301 lock_args->reclaim = FALSE;
14302 if (did_reclaimp)
14303 *did_reclaimp = 0;
14307 * Do the OTW call.
14309 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14311 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14312 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14314 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14315 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14316 "nfs4frlock: needrecov %d", needrecov));
14318 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14319 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14320 args.ctag);
14323 * Check if one of these mutually exclusive error cases has
14324 * happened:
14325 * need to swap credentials due to access error
14326 * recovery is needed
14327 * different error (only known case is missing Kerberos ticket)
14330 if ((ep->error == EACCES ||
14331 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14332 cred_otw != cr) {
14333 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14334 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14335 cr, &cred_otw);
14336 goto recov_retry;
14339 if (needrecov) {
14341 * LOCKT requests don't need to recover from lost
14342 * requests since they don't create/modify state.
14344 if ((ep->error == EINTR ||
14345 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14346 lockt_args)
14347 goto out;
14349 * Do not attempt recovery for requests initiated by
14350 * the recovery framework. Let the framework redrive them.
14352 if (ctype != NFS4_LCK_CTYPE_NORM)
14353 goto out;
14354 else {
14355 ASSERT(resend_rqstp == NULL);
14358 nfs4frlock_save_lost_rqst(ctype, ep->error,
14359 flk_to_locktype(cmd, flk->l_type),
14360 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14362 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14363 &resp, lock_args, locku_args, &oop, &osp, &lop,
14364 rp, vp, &recov_state, op_hint, &did_start_fop,
14365 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14367 if (retry) {
14368 ASSERT(oop == NULL);
14369 ASSERT(osp == NULL);
14370 ASSERT(lop == NULL);
14371 goto recov_retry;
14373 goto out;
14377 * Bail out if have reached this point with ep->error set. Can
14378 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14379 * This happens if Kerberos ticket has expired or has been
14380 * destroyed.
14382 if (ep->error != 0)
14383 goto out;
14386 * Process the reply.
14388 switch (resp->status) {
14389 case NFS4_OK:
14390 resop = &resp->array[1];
14391 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14392 resend_rqstp);
14394 * Have a successful lock operation, now update state.
14396 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14397 resop, lop, vp, flk, cr, resend_rqstp);
14398 break;
14400 case NFS4ERR_DENIED:
14401 resop = &resp->array[1];
14402 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14403 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14404 &recov_state, needrecov, &argsp, &resp,
14405 &tick_delay, &whence, &ep->error, resop, cr,
14406 &did_start_fop, &skip_get_err);
14408 if (retry) {
14409 ASSERT(oop == NULL);
14410 ASSERT(osp == NULL);
14411 ASSERT(lop == NULL);
14412 goto recov_retry;
14414 break;
14416 * If the server won't let us reclaim, fall-back to trying to lock
14417 * the file from scratch. Code elsewhere will check the changeinfo
14418 * to ensure the file hasn't been changed.
14420 case NFS4ERR_NO_GRACE:
14421 if (lock_args && lock_args->reclaim == TRUE) {
14422 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14423 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14424 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14425 frc_no_reclaim = 1;
14426 /* clean up before retrying */
14427 needrecov = 0;
14428 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14429 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14430 &recov_state, op_hint, &did_start_fop, NULL, flk);
14431 goto recov_retry;
14433 /* FALLTHROUGH */
14435 default:
14436 nfs4frlock_results_default(resp, &ep->error);
14437 break;
14439 out:
14441 * Process and cleanup from error. Make interrupted unlock
14442 * requests look successful, since they will be handled by the
14443 * client recovery code.
14445 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14446 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14447 lock_args, locku_args, did_start_fop,
14448 skip_get_err, cred_otw, cr);
14450 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14451 (cmd == F_SETLK || cmd == F_SETLKW))
14452 ep->error = 0;
14456 * nfs4_safelock:
14458 * Return non-zero if the given lock request can be handled without
14459 * violating the constraints on concurrent mapping and locking.
14462 static int
14463 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14465 rnode4_t *rp = VTOR4(vp);
14466 struct vattr va;
14467 int error;
14469 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14470 ASSERT(rp->r_mapcnt >= 0);
14471 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14472 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14473 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14474 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14476 if (rp->r_mapcnt == 0)
14477 return (1); /* always safe if not mapped */
14480 * If the file is already mapped and there are locks, then they
14481 * should be all safe locks. So adding or removing a lock is safe
14482 * as long as the new request is safe (i.e., whole-file, meaning
14483 * length and starting offset are both zero).
14486 if (bfp->l_start != 0 || bfp->l_len != 0) {
14487 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14488 "cannot lock a memory mapped file unless locking the "
14489 "entire file: start %"PRIx64", len %"PRIx64,
14490 bfp->l_start, bfp->l_len));
14491 return (0);
14494 /* mandatory locking and mapping don't mix */
14495 va.va_mask = VATTR_MODE;
14496 error = fop_getattr(vp, &va, 0, cr, NULL);
14497 if (error != 0) {
14498 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14499 "getattr error %d", error));
14500 return (0); /* treat errors conservatively */
14502 if (MANDLOCK(vp, va.va_mode)) {
14503 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14504 "cannot mandatory lock and mmap a file"));
14505 return (0);
14508 return (1);
14513 * Register the lock locally within Solaris.
14514 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14515 * recording locks locally.
14517 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14518 * are registered locally.
14520 void
14521 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14522 uoff_t offset)
14524 int oldsysid;
14525 int error;
14526 #ifdef DEBUG
14527 char *name;
14528 #endif
14530 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14532 #ifdef DEBUG
14533 name = fn_name(VTOSV(vp)->sv_name);
14534 NFS4_DEBUG(nfs4_client_lock_debug,
14535 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14536 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14537 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14538 flk->l_sysid));
14539 kmem_free(name, MAXNAMELEN);
14540 #endif
14542 /* register the lock with local locking */
14543 oldsysid = flk->l_sysid;
14544 flk->l_sysid |= LM_SYSID_CLIENT;
14545 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14546 #ifdef DEBUG
14547 if (error != 0) {
14548 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14549 "nfs4_register_lock_locally: could not register with"
14550 " local locking"));
14551 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14552 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14553 error, (void *)vp, flk->l_pid, flk->l_sysid));
14554 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14555 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14556 flk->l_type, flk->l_start, flk->l_len));
14557 (void) reclock(vp, flk, 0, flag, offset, NULL);
14558 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14559 "blocked by pid %d sysid 0x%x type %d "
14560 "off 0x%" PRIx64 " len 0x%" PRIx64,
14561 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14562 flk->l_len));
14564 #endif
14565 flk->l_sysid = oldsysid;
14569 * nfs4_lockrelease:
14571 * Release any locks on the given vnode that are held by the current
14572 * process. Also removes the lock owner (if one exists) from the rnode's
14573 * list.
14575 static int
14576 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14578 flock64_t ld;
14579 int ret, error;
14580 rnode4_t *rp;
14581 nfs4_lock_owner_t *lop;
14582 nfs4_recov_state_t recov_state;
14583 mntinfo4_t *mi;
14584 bool_t possible_orphan = FALSE;
14585 bool_t recovonly;
14587 ASSERT((uintptr_t)vp > KERNELBASE);
14588 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14590 rp = VTOR4(vp);
14591 mi = VTOMI4(vp);
14594 * If we have not locked anything then we can
14595 * just return since we have no work to do.
14597 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14598 return (0);
14602 * We need to comprehend that another thread may
14603 * kick off recovery and the lock_owner we have stashed
14604 * in lop might be invalid so we should NOT cache it
14605 * locally!
14607 recov_state.rs_flags = 0;
14608 recov_state.rs_num_retry_despite_err = 0;
14609 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14610 &recovonly);
14611 if (error) {
14612 mutex_enter(&rp->r_statelock);
14613 rp->r_flags |= R4LODANGLERS;
14614 mutex_exit(&rp->r_statelock);
14615 return (error);
14618 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14621 * Check if the lock owner might have a lock (request was sent but
14622 * no response was received). Also check if there are any remote
14623 * locks on the file. (In theory we shouldn't have to make this
14624 * second check if there's no lock owner, but for now we'll be
14625 * conservative and do it anyway.) If either condition is true,
14626 * send an unlock for the entire file to the server.
14628 * Note that no explicit synchronization is needed here. At worst,
14629 * flk_has_remote_locks() will return a false positive, in which case
14630 * the unlock call wastes time but doesn't harm correctness.
14633 if (lop) {
14634 mutex_enter(&lop->lo_lock);
14635 possible_orphan = lop->lo_pending_rqsts;
14636 mutex_exit(&lop->lo_lock);
14637 lock_owner_rele(lop);
14640 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14642 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14643 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14644 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14645 (void *)lop));
14647 if (possible_orphan || flk_has_remote_locks(vp)) {
14648 ld.l_type = F_UNLCK; /* set to unlock entire file */
14649 ld.l_whence = 0; /* unlock from start of file */
14650 ld.l_start = 0;
14651 ld.l_len = 0; /* do entire file */
14653 ret = fop_frlock(vp, F_SETLK, &ld, flag, offset, NULL,
14654 cr, NULL);
14656 if (ret != 0) {
14658 * If fop_frlock fails, make sure we unregister
14659 * local locks before we continue.
14661 ld.l_pid = ttoproc(curthread)->p_pid;
14662 nfs4_register_lock_locally(vp, &ld, flag, offset);
14663 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14664 "nfs4_lockrelease: lock release error on vp"
14665 " %p: error %d.\n", (void *)vp, ret));
14669 recov_state.rs_flags = 0;
14670 recov_state.rs_num_retry_despite_err = 0;
14671 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14672 &recovonly);
14673 if (error) {
14674 mutex_enter(&rp->r_statelock);
14675 rp->r_flags |= R4LODANGLERS;
14676 mutex_exit(&rp->r_statelock);
14677 return (error);
14681 * So, here we're going to need to retrieve the lock-owner
14682 * again (in case recovery has done a switch-a-roo) and
14683 * remove it because we can.
14685 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14687 if (lop) {
14688 nfs4_rnode_remove_lock_owner(rp, lop);
14689 lock_owner_rele(lop);
14692 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14693 return (0);
14697 * Wait for 'tick_delay' clock ticks.
14698 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14699 * NOTE: lock_lease_time is in seconds.
14701 * XXX For future improvements, should implement a waiting queue scheme.
14703 static int
14704 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14706 long milliseconds_delay;
14707 time_t lock_lease_time;
14709 /* wait tick_delay clock ticks or siginteruptus */
14710 if (delay_sig(*tick_delay)) {
14711 return (EINTR);
14713 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14714 "reissue the lock request: blocked for %ld clock ticks: %ld "
14715 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14717 /* get the lease time */
14718 lock_lease_time = r2lease_time(rp);
14720 /* drv_hztousec converts ticks to microseconds */
14721 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14722 if (milliseconds_delay < lock_lease_time * 1000) {
14723 *tick_delay = 2 * *tick_delay;
14724 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14725 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14727 return (0);
14731 void
14732 nfs4_vnops_init(void)
14736 void
14737 nfs4_vnops_fini(void)
14742 * Return a reference to the directory (parent) vnode for a given vnode,
14743 * using the saved pathname information and the directory file handle. The
14744 * caller is responsible for disposing of the reference.
14745 * Returns zero or an errno value.
14747 * Caller should set need_start_op to FALSE if it is the recovery
14748 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14751 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14753 svnode_t *svnp;
14754 vnode_t *dvp = NULL;
14755 servinfo4_t *svp;
14756 nfs4_fname_t *mfname;
14757 int error;
14759 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14761 if (vp->v_flag & VROOT) {
14762 nfs4_sharedfh_t *sfh;
14763 nfs_fh4 fh;
14764 mntinfo4_t *mi;
14766 ASSERT(vp->v_type == VREG);
14768 mi = VTOMI4(vp);
14769 svp = mi->mi_curr_serv;
14770 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14771 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14772 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14773 sfh = sfh4_get(&fh, VTOMI4(vp));
14774 nfs_rw_exit(&svp->sv_lock);
14775 mfname = mi->mi_fname;
14776 fn_hold(mfname);
14777 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14778 sfh4_rele(&sfh);
14780 if (dvp->v_type == VNON)
14781 dvp->v_type = VDIR;
14782 *dvpp = dvp;
14783 return (0);
14786 svnp = VTOSV(vp);
14788 if (svnp == NULL) {
14789 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14790 "shadow node is NULL"));
14791 return (EINVAL);
14794 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14795 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14796 "shadow node name or dfh val == NULL"));
14797 return (EINVAL);
14800 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14801 (int)need_start_op);
14802 if (error != 0) {
14803 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14804 "nfs4_make_dotdot returned %d", error));
14805 return (error);
14807 if (!dvp) {
14808 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14809 "nfs4_make_dotdot returned a NULL dvp"));
14810 return (EIO);
14812 if (dvp->v_type == VNON)
14813 dvp->v_type = VDIR;
14814 ASSERT(dvp->v_type == VDIR);
14815 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14816 mutex_enter(&dvp->v_lock);
14817 dvp->v_flag |= V_XATTRDIR;
14818 mutex_exit(&dvp->v_lock);
14820 *dvpp = dvp;
14821 return (0);
14825 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14826 * length that fnamep can accept, including the trailing null.
14827 * Returns 0 if okay, returns an errno value if there was a problem.
14831 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14833 char *fn;
14834 int err = 0;
14835 servinfo4_t *svp;
14836 svnode_t *shvp;
14839 * If the file being opened has VROOT set, then this is
14840 * a "file" mount. sv_name will not be interesting, so
14841 * go back to the servinfo4 to get the original mount
14842 * path and strip off all but the final edge. Otherwise
14843 * just return the name from the shadow vnode.
14846 if (vp->v_flag & VROOT) {
14848 svp = VTOMI4(vp)->mi_curr_serv;
14849 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14851 fn = strrchr(svp->sv_path, '/');
14852 if (fn == NULL)
14853 err = EINVAL;
14854 else
14855 fn++;
14856 } else {
14857 shvp = VTOSV(vp);
14858 fn = fn_name(shvp->sv_name);
14861 if (err == 0)
14862 if (strlen(fn) < maxlen)
14863 (void) strcpy(fnamep, fn);
14864 else
14865 err = ENAMETOOLONG;
14867 if (vp->v_flag & VROOT)
14868 nfs_rw_exit(&svp->sv_lock);
14869 else
14870 kmem_free(fn, MAXNAMELEN);
14872 return (err);
14876 * Bookkeeping for a close that doesn't need to go over the wire.
14877 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14878 * it is left at 1.
14880 void
14881 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14883 rnode4_t *rp;
14884 mntinfo4_t *mi;
14886 mi = VTOMI4(vp);
14887 rp = VTOR4(vp);
14889 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14890 "rp=%p osp=%p", (void *)rp, (void *)osp));
14891 ASSERT(nfs_zone() == mi->mi_zone);
14892 ASSERT(mutex_owned(&osp->os_sync_lock));
14893 ASSERT(*have_lockp);
14895 if (!osp->os_valid ||
14896 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14897 return;
14901 * This removes the reference obtained at OPEN; ie,
14902 * when the open stream structure was created.
14904 * We don't have to worry about calling 'open_stream_rele'
14905 * since we our currently holding a reference to this
14906 * open stream which means the count can not go to 0 with
14907 * this decrement.
14909 ASSERT(osp->os_ref_count >= 2);
14910 osp->os_ref_count--;
14911 osp->os_valid = 0;
14912 mutex_exit(&osp->os_sync_lock);
14913 *have_lockp = 0;
14915 nfs4_dec_state_ref_count(mi);
14919 * Close all remaining open streams on the rnode. These open streams
14920 * could be here because:
14921 * - The close attempted at either close or delmap failed
14922 * - Some kernel entity did fop_open but never did fop_close
14923 * - Someone did mknod on a regular file but never opened it
14926 nfs4close_all(vnode_t *vp, cred_t *cr)
14928 nfs4_open_stream_t *osp;
14929 int error;
14930 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14931 rnode4_t *rp;
14933 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14935 error = 0;
14936 rp = VTOR4(vp);
14939 * At this point, all we know is that the last time
14940 * someone called vn_rele, the count was 1. Since then,
14941 * the vnode could have been re-activated. We want to
14942 * loop through the open streams and close each one, but
14943 * we have to be careful since once we release the rnode
14944 * hash bucket lock, someone else is free to come in and
14945 * re-activate the rnode and add new open streams. The
14946 * strategy is take the rnode hash bucket lock, verify that
14947 * the count is still 1, grab the open stream off the
14948 * head of the list and mark it invalid, then release the
14949 * rnode hash bucket lock and proceed with that open stream.
14950 * This is ok because nfs4close_one() will acquire the proper
14951 * open/create to close/destroy synchronization for open
14952 * streams, and will ensure that if someone has reopened
14953 * the open stream after we've dropped the hash bucket lock
14954 * then we'll just simply return without destroying the
14955 * open stream.
14956 * Repeat until the list is empty.
14959 for (;;) {
14961 /* make sure vnode hasn't been reactivated */
14962 rw_enter(&rp->r_hashq->r_lock, RW_READER);
14963 mutex_enter(&vp->v_lock);
14964 if (vp->v_count > 1) {
14965 mutex_exit(&vp->v_lock);
14966 rw_exit(&rp->r_hashq->r_lock);
14967 break;
14970 * Grabbing r_os_lock before releasing v_lock prevents
14971 * a window where the rnode/open stream could get
14972 * reactivated (and os_force_close set to 0) before we
14973 * had a chance to set os_force_close to 1.
14975 mutex_enter(&rp->r_os_lock);
14976 mutex_exit(&vp->v_lock);
14978 osp = list_head(&rp->r_open_streams);
14979 if (!osp) {
14980 /* nothing left to CLOSE OTW, so return */
14981 mutex_exit(&rp->r_os_lock);
14982 rw_exit(&rp->r_hashq->r_lock);
14983 break;
14986 mutex_enter(&rp->r_statev4_lock);
14987 /* the file can't still be mem mapped */
14988 ASSERT(rp->r_mapcnt == 0);
14989 if (rp->created_v4)
14990 rp->created_v4 = 0;
14991 mutex_exit(&rp->r_statev4_lock);
14994 * Grab a ref on this open stream; nfs4close_one
14995 * will mark it as invalid
14997 mutex_enter(&osp->os_sync_lock);
14998 osp->os_ref_count++;
14999 osp->os_force_close = 1;
15000 mutex_exit(&osp->os_sync_lock);
15001 mutex_exit(&rp->r_os_lock);
15002 rw_exit(&rp->r_hashq->r_lock);
15004 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15006 /* Update error if it isn't already non-zero */
15007 if (error == 0) {
15008 if (e.error)
15009 error = e.error;
15010 else if (e.stat)
15011 error = geterrno4(e.stat);
15014 #ifdef DEBUG
15015 nfs4close_all_cnt++;
15016 #endif
15017 /* Release the ref on osp acquired above. */
15018 open_stream_rele(osp, rp);
15020 /* Proceed to the next open stream, if any */
15022 return (error);
15026 * nfs4close_one - close one open stream for a file if needed.
15028 * "close_type" indicates which close path this is:
15029 * CLOSE_NORM: close initiated via fop_close.
15030 * CLOSE_DELMAP: close initiated via fop_delmap.
15031 * CLOSE_FORCE: close initiated via fop_inactive. This path forces
15032 * the close and release of client state for this open stream
15033 * (unless someone else has the open stream open).
15034 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15035 * (e.g., due to abort because of a signal).
15036 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15038 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15039 * recovery. Instead, the caller is expected to deal with retries.
15041 * The caller can either pass in the osp ('provided_osp') or not.
15043 * 'access_bits' represents the access we are closing/downgrading.
15045 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15046 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15047 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15049 * Errors are returned via the nfs4_error_t.
15051 void
15052 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15053 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15054 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15055 uint_t mmap_flags)
15057 nfs4_open_owner_t *oop;
15058 nfs4_open_stream_t *osp = NULL;
15059 int retry = 0;
15060 int num_retries = NFS4_NUM_RECOV_RETRIES;
15061 rnode4_t *rp;
15062 mntinfo4_t *mi;
15063 nfs4_recov_state_t recov_state;
15064 cred_t *cred_otw = NULL;
15065 bool_t recovonly = FALSE;
15066 int isrecov;
15067 int force_close;
15068 int close_failed = 0;
15069 int did_dec_count = 0;
15070 int did_start_op = 0;
15071 int did_force_recovlock = 0;
15072 int did_start_seqid_sync = 0;
15073 int have_sync_lock = 0;
15075 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15077 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15078 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15079 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15080 len, maxprot, mmap_flags, access_bits));
15082 nfs4_error_zinit(ep);
15083 rp = VTOR4(vp);
15084 mi = VTOMI4(vp);
15085 isrecov = (close_type == CLOSE_RESEND ||
15086 close_type == CLOSE_AFTER_RESEND);
15089 * First get the open owner.
15091 if (!provided_osp) {
15092 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15093 } else {
15094 oop = provided_osp->os_open_owner;
15095 ASSERT(oop != NULL);
15096 open_owner_hold(oop);
15099 if (!oop) {
15100 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15101 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15102 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15103 (void *)provided_osp, close_type));
15104 ep->error = EIO;
15105 goto out;
15108 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15109 recov_retry:
15110 osp = NULL;
15111 close_failed = 0;
15112 force_close = (close_type == CLOSE_FORCE);
15113 retry = 0;
15114 did_start_op = 0;
15115 did_force_recovlock = 0;
15116 did_start_seqid_sync = 0;
15117 have_sync_lock = 0;
15118 recovonly = FALSE;
15119 recov_state.rs_flags = 0;
15120 recov_state.rs_num_retry_despite_err = 0;
15123 * Second synchronize with recovery.
15125 if (!isrecov) {
15126 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15127 &recov_state, &recovonly);
15128 if (!ep->error) {
15129 did_start_op = 1;
15130 } else {
15131 close_failed = 1;
15133 * If we couldn't get start_fop, but have to
15134 * cleanup state, then at least acquire the
15135 * mi_recovlock so we can synchronize with
15136 * recovery.
15138 if (close_type == CLOSE_FORCE) {
15139 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15140 RW_READER, FALSE);
15141 did_force_recovlock = 1;
15142 } else
15143 goto out;
15148 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15149 * set 'recovonly' to TRUE since most likely this is due to
15150 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15151 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15152 * to retry, causing us to loop until recovery finishes. Plus we
15153 * don't need protection over the open seqid since we're not going
15154 * OTW, hence don't need to use the seqid.
15156 if (recovonly == FALSE) {
15157 /* need to grab the open owner sync before 'os_sync_lock' */
15158 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15159 if (ep->error == EAGAIN) {
15160 ASSERT(!isrecov);
15161 if (did_start_op)
15162 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15163 &recov_state, TRUE);
15164 if (did_force_recovlock)
15165 nfs_rw_exit(&mi->mi_recovlock);
15166 goto recov_retry;
15168 did_start_seqid_sync = 1;
15172 * Third get an open stream and acquire 'os_sync_lock' to
15173 * sychronize the opening/creating of an open stream with the
15174 * closing/destroying of an open stream.
15176 if (!provided_osp) {
15177 /* returns with 'os_sync_lock' held */
15178 osp = find_open_stream(oop, rp);
15179 if (!osp) {
15180 ep->error = EIO;
15181 goto out;
15183 } else {
15184 osp = provided_osp;
15185 open_stream_hold(osp);
15186 mutex_enter(&osp->os_sync_lock);
15188 have_sync_lock = 1;
15190 ASSERT(oop == osp->os_open_owner);
15193 * Fourth, do any special pre-OTW CLOSE processing
15194 * based on the specific close type.
15196 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15197 !did_dec_count) {
15198 ASSERT(osp->os_open_ref_count > 0);
15199 osp->os_open_ref_count--;
15200 did_dec_count = 1;
15201 if (osp->os_open_ref_count == 0)
15202 osp->os_final_close = 1;
15205 if (close_type == CLOSE_FORCE) {
15206 /* see if somebody reopened the open stream. */
15207 if (!osp->os_force_close) {
15208 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15209 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15210 "was reopened, vp %p", (void *)osp, (void *)vp));
15211 ep->error = 0;
15212 ep->stat = NFS4_OK;
15213 goto out;
15216 if (!osp->os_final_close && !did_dec_count) {
15217 osp->os_open_ref_count--;
15218 did_dec_count = 1;
15222 * We can't depend on os_open_ref_count being 0 due to the
15223 * way executables are opened (VN_RELE to match a fop_open).
15225 #ifdef NOTYET
15226 ASSERT(osp->os_open_ref_count == 0);
15227 #endif
15228 if (osp->os_open_ref_count != 0) {
15229 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15230 "nfs4close_one: should panic here on an "
15231 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15232 "since this is probably the exec problem."));
15234 osp->os_open_ref_count = 0;
15238 * There is the possibility that nfs4close_one()
15239 * for close_type == CLOSE_DELMAP couldn't find the
15240 * open stream, thus couldn't decrement its os_mapcnt;
15241 * therefore we can't use this ASSERT yet.
15243 #ifdef NOTYET
15244 ASSERT(osp->os_mapcnt == 0);
15245 #endif
15246 osp->os_mapcnt = 0;
15249 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15250 ASSERT(osp->os_mapcnt >= btopr(len));
15252 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15253 osp->os_mmap_write -= btopr(len);
15254 if (maxprot & PROT_READ)
15255 osp->os_mmap_read -= btopr(len);
15256 if (maxprot & PROT_EXEC)
15257 osp->os_mmap_read -= btopr(len);
15258 /* mirror the PROT_NONE check in nfs4_addmap() */
15259 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15260 !(maxprot & PROT_EXEC))
15261 osp->os_mmap_read -= btopr(len);
15262 osp->os_mapcnt -= btopr(len);
15263 did_dec_count = 1;
15266 if (recovonly) {
15267 nfs4_lost_rqst_t lost_rqst;
15269 /* request should not already be in recovery queue */
15270 ASSERT(lrp == NULL);
15271 nfs4_error_init(ep, EINTR);
15272 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15273 osp, cred_otw, vp);
15274 mutex_exit(&osp->os_sync_lock);
15275 have_sync_lock = 0;
15276 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15277 lost_rqst.lr_op == OP_CLOSE ?
15278 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15279 close_failed = 1;
15280 force_close = 0;
15281 goto close_cleanup;
15285 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15286 * we stopped operating on the open owner's <old oo_name, old seqid>
15287 * space, which means we stopped operating on the open stream
15288 * too. So don't go OTW (as the seqid is likely bad, and the
15289 * stateid could be stale, potentially triggering a false
15290 * setclientid), and just clean up the client's internal state.
15292 if (osp->os_orig_oo_name != oop->oo_name) {
15293 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15294 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15295 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15296 "oo_name %" PRIx64")",
15297 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15298 oop->oo_name));
15299 close_failed = 1;
15302 /* If the file failed recovery, just quit. */
15303 mutex_enter(&rp->r_statelock);
15304 if (rp->r_flags & R4RECOVERR) {
15305 close_failed = 1;
15307 mutex_exit(&rp->r_statelock);
15310 * If the force close path failed to obtain start_fop
15311 * then skip the OTW close and just remove the state.
15313 if (close_failed)
15314 goto close_cleanup;
15317 * Fifth, check to see if there are still mapped pages or other
15318 * opens using this open stream. If there are then we can't
15319 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15321 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15322 nfs4_lost_rqst_t new_lost_rqst;
15323 bool_t needrecov = FALSE;
15324 cred_t *odg_cred_otw = NULL;
15325 seqid4 open_dg_seqid = 0;
15327 if (osp->os_delegation) {
15329 * If this open stream was never OPENed OTW then we
15330 * surely can't DOWNGRADE it (especially since the
15331 * osp->open_stateid is really a delegation stateid
15332 * when os_delegation is 1).
15334 if (access_bits & FREAD)
15335 osp->os_share_acc_read--;
15336 if (access_bits & FWRITE)
15337 osp->os_share_acc_write--;
15338 osp->os_share_deny_none--;
15339 nfs4_error_zinit(ep);
15340 goto out;
15342 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15343 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15344 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15345 if (needrecov && !isrecov) {
15346 bool_t abort;
15347 nfs4_bseqid_entry_t *bsep = NULL;
15349 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15350 bsep = nfs4_create_bseqid_entry(oop, NULL,
15351 vp, 0,
15352 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15353 open_dg_seqid);
15355 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15356 oop, osp, odg_cred_otw, vp, access_bits, 0);
15357 mutex_exit(&osp->os_sync_lock);
15358 have_sync_lock = 0;
15359 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15360 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15361 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15362 bsep, NULL, NULL);
15363 if (odg_cred_otw)
15364 crfree(odg_cred_otw);
15365 if (bsep)
15366 kmem_free(bsep, sizeof (*bsep));
15368 if (abort == TRUE)
15369 goto out;
15371 if (did_start_seqid_sync) {
15372 nfs4_end_open_seqid_sync(oop);
15373 did_start_seqid_sync = 0;
15375 open_stream_rele(osp, rp);
15377 if (did_start_op)
15378 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15379 &recov_state, FALSE);
15380 if (did_force_recovlock)
15381 nfs_rw_exit(&mi->mi_recovlock);
15383 goto recov_retry;
15384 } else {
15385 if (odg_cred_otw)
15386 crfree(odg_cred_otw);
15388 goto out;
15392 * If this open stream was created as the results of an open
15393 * while holding a delegation, then just release it; no need
15394 * to do an OTW close. Otherwise do a "normal" OTW close.
15396 if (osp->os_delegation) {
15397 nfs4close_notw(vp, osp, &have_sync_lock);
15398 nfs4_error_zinit(ep);
15399 goto out;
15403 * If this stream is not valid, we're done.
15405 if (!osp->os_valid) {
15406 nfs4_error_zinit(ep);
15407 goto out;
15411 * Last open or mmap ref has vanished, need to do an OTW close.
15412 * First check to see if a close is still necessary.
15414 if (osp->os_failed_reopen) {
15415 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15416 "don't close OTW osp %p since reopen failed.",
15417 (void *)osp));
15419 * Reopen of the open stream failed, hence the
15420 * stateid of the open stream is invalid/stale, and
15421 * sending this OTW would incorrectly cause another
15422 * round of recovery. In this case, we need to set
15423 * the 'os_valid' bit to 0 so another thread doesn't
15424 * come in and re-open this open stream before
15425 * this "closing" thread cleans up state (decrementing
15426 * the nfs4_server_t's state_ref_count and decrementing
15427 * the os_ref_count).
15429 osp->os_valid = 0;
15431 * This removes the reference obtained at OPEN; ie,
15432 * when the open stream structure was created.
15434 * We don't have to worry about calling 'open_stream_rele'
15435 * since we our currently holding a reference to this
15436 * open stream which means the count can not go to 0 with
15437 * this decrement.
15439 ASSERT(osp->os_ref_count >= 2);
15440 osp->os_ref_count--;
15441 nfs4_error_zinit(ep);
15442 close_failed = 0;
15443 goto close_cleanup;
15446 ASSERT(osp->os_ref_count > 1);
15449 * Sixth, try the CLOSE OTW.
15451 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15452 close_type, ep, &have_sync_lock);
15454 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15456 * Let the recovery thread be responsible for
15457 * removing the state for CLOSE.
15459 close_failed = 1;
15460 force_close = 0;
15461 retry = 0;
15464 /* See if we need to retry with a different cred */
15465 if ((ep->error == EACCES ||
15466 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15467 cred_otw != cr) {
15468 crfree(cred_otw);
15469 cred_otw = cr;
15470 crhold(cred_otw);
15471 retry = 1;
15474 if (ep->error || ep->stat)
15475 close_failed = 1;
15477 if (retry && !isrecov && num_retries-- > 0) {
15478 if (have_sync_lock) {
15479 mutex_exit(&osp->os_sync_lock);
15480 have_sync_lock = 0;
15482 if (did_start_seqid_sync) {
15483 nfs4_end_open_seqid_sync(oop);
15484 did_start_seqid_sync = 0;
15486 open_stream_rele(osp, rp);
15488 if (did_start_op)
15489 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15490 &recov_state, FALSE);
15491 if (did_force_recovlock)
15492 nfs_rw_exit(&mi->mi_recovlock);
15493 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15494 "nfs4close_one: need to retry the close "
15495 "operation"));
15496 goto recov_retry;
15498 close_cleanup:
15500 * Seventh and lastly, process our results.
15502 if (close_failed && force_close) {
15504 * It's ok to drop and regrab the 'os_sync_lock' since
15505 * nfs4close_notw() will recheck to make sure the
15506 * "close"/removal of state should happen.
15508 if (!have_sync_lock) {
15509 mutex_enter(&osp->os_sync_lock);
15510 have_sync_lock = 1;
15513 * This is last call, remove the ref on the open
15514 * stream created by open and clean everything up.
15516 osp->os_pending_close = 0;
15517 nfs4close_notw(vp, osp, &have_sync_lock);
15518 nfs4_error_zinit(ep);
15521 if (!close_failed) {
15522 if (have_sync_lock) {
15523 osp->os_pending_close = 0;
15524 mutex_exit(&osp->os_sync_lock);
15525 have_sync_lock = 0;
15526 } else {
15527 mutex_enter(&osp->os_sync_lock);
15528 osp->os_pending_close = 0;
15529 mutex_exit(&osp->os_sync_lock);
15531 if (did_start_op && recov_state.rs_sp != NULL) {
15532 mutex_enter(&recov_state.rs_sp->s_lock);
15533 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15534 mutex_exit(&recov_state.rs_sp->s_lock);
15535 } else {
15536 nfs4_dec_state_ref_count(mi);
15538 nfs4_error_zinit(ep);
15541 out:
15542 if (have_sync_lock)
15543 mutex_exit(&osp->os_sync_lock);
15544 if (did_start_op)
15545 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15546 recovonly ? TRUE : FALSE);
15547 if (did_force_recovlock)
15548 nfs_rw_exit(&mi->mi_recovlock);
15549 if (cred_otw)
15550 crfree(cred_otw);
15551 if (osp)
15552 open_stream_rele(osp, rp);
15553 if (oop) {
15554 if (did_start_seqid_sync)
15555 nfs4_end_open_seqid_sync(oop);
15556 open_owner_rele(oop);
15561 * Convert information returned by the server in the LOCK4denied
15562 * structure to the form required by fcntl.
15564 static void
15565 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15567 nfs4_lo_name_t *lo;
15569 #ifdef DEBUG
15570 if (denied_to_flk_debug) {
15571 lockt_denied_debug = lockt_denied;
15572 debug_enter("lockt_denied");
15574 #endif
15576 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15577 flk->l_whence = 0; /* aka SEEK_SET */
15578 flk->l_start = lockt_denied->offset;
15579 flk->l_len = lockt_denied->length;
15582 * If the blocking clientid matches our client id, then we can
15583 * interpret the lockowner (since we built it). If not, then
15584 * fabricate a sysid and pid. Note that the l_sysid field
15585 * in *flk already has the local sysid.
15588 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15590 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15591 lo = (nfs4_lo_name_t *)
15592 lockt_denied->owner.owner_val;
15594 flk->l_pid = lo->ln_pid;
15595 } else {
15596 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15597 "denied_to_flk: bad lock owner length\n"));
15599 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15601 } else {
15602 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15603 "denied_to_flk: foreign clientid\n"));
15606 * Construct a new sysid which should be different from
15607 * sysids of other systems.
15610 flk->l_sysid++;
15611 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15615 static pid_t
15616 lo_to_pid(lock_owner4 *lop)
15618 pid_t pid = 0;
15619 uchar_t *cp;
15620 int i;
15622 cp = (uchar_t *)&lop->clientid;
15624 for (i = 0; i < sizeof (lop->clientid); i++)
15625 pid += (pid_t)*cp++;
15627 cp = (uchar_t *)lop->owner_val;
15629 for (i = 0; i < lop->owner_len; i++)
15630 pid += (pid_t)*cp++;
15632 return (pid);
15636 * Given a lock pointer, returns the length of that lock.
15637 * "end" is the last locked offset the "l_len" covers from
15638 * the start of the lock.
15640 static off64_t
15641 lock_to_end(flock64_t *lock)
15643 off64_t lock_end;
15645 if (lock->l_len == 0)
15646 lock_end = (off64_t)MAXEND;
15647 else
15648 lock_end = lock->l_start + lock->l_len - 1;
15650 return (lock_end);
15654 * Given the end of a lock, it will return you the length "l_len" for that lock.
15656 static off64_t
15657 end_to_len(off64_t start, off64_t end)
15659 off64_t lock_len;
15661 ASSERT(end >= start);
15662 if (end == MAXEND)
15663 lock_len = 0;
15664 else
15665 lock_len = end - start + 1;
15667 return (lock_len);
15671 * On given end for a lock it determines if it is the last locked offset
15672 * or not, if so keeps it as is, else adds one to return the length for
15673 * valid start.
15675 static off64_t
15676 start_check(off64_t x)
15678 if (x == MAXEND)
15679 return (x);
15680 else
15681 return (x + 1);
15685 * See if these two locks overlap, and if so return 1;
15686 * otherwise, return 0.
15688 static int
15689 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15691 off64_t llfp_end, curfp_end;
15693 llfp_end = lock_to_end(llfp);
15694 curfp_end = lock_to_end(curfp);
15696 if (((llfp_end >= curfp->l_start) &&
15697 (llfp->l_start <= curfp->l_start)) ||
15698 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15699 return (1);
15700 return (0);
15704 * Determine what the intersecting lock region is, and add that to the
15705 * 'nl_llpp' locklist in increasing order (by l_start).
15707 static void
15708 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15709 locklist_t **nl_llpp, vnode_t *vp)
15711 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15712 off64_t lost_flp_end, local_flp_end, len, start;
15714 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15716 if (!locks_intersect(lost_flp, local_flp))
15717 return;
15719 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15720 "locks intersect"));
15722 lost_flp_end = lock_to_end(lost_flp);
15723 local_flp_end = lock_to_end(local_flp);
15725 /* Find the starting point of the intersecting region */
15726 if (local_flp->l_start > lost_flp->l_start)
15727 start = local_flp->l_start;
15728 else
15729 start = lost_flp->l_start;
15731 /* Find the lenght of the intersecting region */
15732 if (lost_flp_end < local_flp_end)
15733 len = end_to_len(start, lost_flp_end);
15734 else
15735 len = end_to_len(start, local_flp_end);
15738 * Prepare the flock structure for the intersection found and insert
15739 * it into the new list in increasing l_start order. This list contains
15740 * intersections of locks registered by the client with the local host
15741 * and the lost lock.
15742 * The lock type of this lock is the same as that of the local_flp.
15744 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15745 intersect_llp->ll_flock.l_start = start;
15746 intersect_llp->ll_flock.l_len = len;
15747 intersect_llp->ll_flock.l_type = local_flp->l_type;
15748 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15749 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15750 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15751 intersect_llp->ll_vp = vp;
15753 tmp_fllp = *nl_llpp;
15754 cur_fllp = NULL;
15755 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15756 intersect_llp->ll_flock.l_start) {
15757 cur_fllp = tmp_fllp;
15758 tmp_fllp = tmp_fllp->ll_next;
15760 if (cur_fllp == NULL) {
15761 /* first on the list */
15762 intersect_llp->ll_next = *nl_llpp;
15763 *nl_llpp = intersect_llp;
15764 } else {
15765 intersect_llp->ll_next = cur_fllp->ll_next;
15766 cur_fllp->ll_next = intersect_llp;
15769 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15770 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15771 intersect_llp->ll_flock.l_start,
15772 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15773 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15777 * Our local locking current state is potentially different than
15778 * what the NFSv4 server thinks we have due to a lost lock that was
15779 * resent and then received. We need to reset our "NFSv4" locking
15780 * state to match the current local locking state for this pid since
15781 * that is what the user/application sees as what the world is.
15783 * We cannot afford to drop the open/lock seqid sync since then we can
15784 * get confused about what the current local locking state "is" versus
15785 * "was".
15787 * If we are unable to fix up the locks, we send SIGLOST to the affected
15788 * process. This is not done if the filesystem has been forcibly
15789 * unmounted, in case the process has already exited and a new process
15790 * exists with the same pid.
15792 static void
15793 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15794 nfs4_lock_owner_t *lop)
15796 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15797 mntinfo4_t *mi = VTOMI4(vp);
15798 const int cmd = F_SETLK;
15799 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15800 flock64_t ul_fl;
15802 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15803 "nfs4_reinstitute_local_lock_state"));
15806 * Find active locks for this vp from the local locking code.
15807 * Scan through this list and find out the locks that intersect with
15808 * the lost lock. Once we find the lock that intersects, add the
15809 * intersection area as a new lock to a new list "ri_llp". The lock
15810 * type of the intersection region lock added to ri_llp is the same
15811 * as that found in the active lock list, "list". The intersecting
15812 * region locks are added to ri_llp in increasing l_start order.
15814 ASSERT(nfs_zone() == mi->mi_zone);
15816 locks = flk_active_locks_for_vp(vp);
15817 ri_llp = NULL;
15819 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15820 ASSERT(llp->ll_vp == vp);
15822 * Pick locks that belong to this pid/lockowner
15824 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15825 continue;
15827 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15831 * Now we have the list of intersections with the lost lock. These are
15832 * the locks that were/are active before the server replied to the
15833 * last/lost lock. Issue these locks to the server here. Playing these
15834 * locks to the server will re-establish our current local locking state
15835 * with the v4 server.
15836 * If we get an error, send SIGLOST to the application for that lock.
15839 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15840 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15841 "nfs4_reinstitute_local_lock_state: need to issue "
15842 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15843 llp->ll_flock.l_start,
15844 llp->ll_flock.l_start + llp->ll_flock.l_len,
15845 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15846 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15848 * No need to relock what we already have
15850 if (llp->ll_flock.l_type == lost_flp->l_type)
15851 continue;
15853 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15857 * Now keeping the start of the lost lock as our reference parse the
15858 * newly created ri_llp locklist to find the ranges that we have locked
15859 * with the v4 server but not in the current local locking. We need
15860 * to unlock these ranges.
15861 * These ranges can also be reffered to as those ranges, where the lost
15862 * lock does not overlap with the locks in the ri_llp but are locked
15863 * since the server replied to the lost lock.
15865 cur_start = lost_flp->l_start;
15866 lost_flp_end = lock_to_end(lost_flp);
15868 ul_fl.l_type = F_UNLCK;
15869 ul_fl.l_whence = 0; /* aka SEEK_SET */
15870 ul_fl.l_sysid = lost_flp->l_sysid;
15871 ul_fl.l_pid = lost_flp->l_pid;
15873 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15874 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15876 if (llp->ll_flock.l_start <= cur_start) {
15877 cur_start = start_check(llp_ll_flock_end);
15878 continue;
15880 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15881 "nfs4_reinstitute_local_lock_state: "
15882 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15883 cur_start, llp->ll_flock.l_start));
15885 ul_fl.l_start = cur_start;
15886 ul_fl.l_len = end_to_len(cur_start,
15887 (llp->ll_flock.l_start - 1));
15889 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15890 cur_start = start_check(llp_ll_flock_end);
15894 * In the case where the lost lock ends after all intersecting locks,
15895 * unlock the last part of the lost lock range.
15897 if (cur_start != start_check(lost_flp_end)) {
15898 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15899 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15900 "lost lock region [%"PRIx64" - %"PRIx64"]",
15901 cur_start, lost_flp->l_start + lost_flp->l_len));
15903 ul_fl.l_start = cur_start;
15905 * Is it an to-EOF lock? if so unlock till the end
15907 if (lost_flp->l_len == 0)
15908 ul_fl.l_len = 0;
15909 else
15910 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15912 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15915 if (locks != NULL)
15916 flk_free_locklist(locks);
15918 /* Free up our newly created locklist */
15919 for (llp = ri_llp; llp != NULL; ) {
15920 tmp_llp = llp->ll_next;
15921 kmem_free(llp, sizeof (locklist_t));
15922 llp = tmp_llp;
15926 * Now return back to the original calling nfs4frlock()
15927 * and let us naturally drop our seqid syncs.
15932 * Create a lost state record for the given lock reinstantiation request
15933 * and push it onto the lost state queue.
15935 static void
15936 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15937 nfs4_lock_owner_t *lop)
15939 nfs4_lost_rqst_t req;
15940 nfs_lock_type4 locktype;
15941 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15943 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15945 locktype = flk_to_locktype(cmd, flk->l_type);
15946 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15947 NULL, NULL, lop, flk, &req, cr, vp);
15948 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15949 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15950 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15951 NULL, NULL, NULL);