1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 */
25
26 /*
27 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 /*
32 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
33 * All Rights Reserved
34 */
35
36 /*
37 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
38 */
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/cred.h>
44 #include <sys/time.h>
45 #include <sys/vnode.h>
46 #include <sys/vfs.h>
47 #include <sys/vfs_opreg.h>
48 #include <sys/file.h>
49 #include <sys/filio.h>
50 #include <sys/uio.h>
51 #include <sys/buf.h>
52 #include <sys/mman.h>
53 #include <sys/pathname.h>
54 #include <sys/dirent.h>
55 #include <sys/debug.h>
56 #include <sys/vmsystm.h>
57 #include <sys/fcntl.h>
58 #include <sys/flock.h>
59 #include <sys/swap.h>
60 #include <sys/errno.h>
61 #include <sys/strsubr.h>
62 #include <sys/sysmacros.h>
63 #include <sys/kmem.h>
64 #include <sys/cmn_err.h>
65 #include <sys/pathconf.h>
66 #include <sys/utsname.h>
67 #include <sys/dnlc.h>
68 #include <sys/acl.h>
69 #include <sys/systeminfo.h>
70 #include <sys/policy.h>
71 #include <sys/sdt.h>
72 #include <sys/list.h>
73 #include <sys/stat.h>
74 #include <sys/zone.h>
75
76 #include <rpc/types.h>
77 #include <rpc/auth.h>
78 #include <rpc/clnt.h>
79
80 #include <nfs/nfs.h>
81 #include <nfs/nfs_clnt.h>
82 #include <nfs/nfs_acl.h>
83 #include <nfs/lm.h>
84 #include <nfs/nfs4.h>
85 #include <nfs/nfs4_kprot.h>
86 #include <nfs/rnode4.h>
87 #include <nfs/nfs4_clnt.h>
88
89 #include <vm/hat.h>
90 #include <vm/as.h>
91 #include <vm/page.h>
92 #include <vm/pvn.h>
93 #include <vm/seg.h>
94 #include <vm/seg_map.h>
95 #include <vm/seg_kpm.h>
96 #include <vm/seg_vn.h>
97
98 #include <fs/fs_subr.h>
99
100 #include <sys/ddi.h>
101 #include <sys/int_fmtio.h>
102 #include <sys/fs/autofs.h>
103
104 typedef struct {
105 nfs4_ga_res_t *di_garp;
106 cred_t *di_cred;
107 hrtime_t di_time_call;
108 } dirattr_info_t;
109
110 typedef enum nfs4_acl_op {
111 NFS4_ACL_GET,
112 NFS4_ACL_SET
113 } nfs4_acl_op_t;
114
115 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *mi);
116
117 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
118 char *, dirattr_info_t *);
119
120 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
121 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
122 nfs4_error_t *, int *);
123 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
124 cred_t *);
125 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
126 stable_how4 *);
127 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
128 cred_t *, bool_t, struct uio *);
129 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
130 vsecattr_t *);
131 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
132 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
133 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
134 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
135 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
136 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
137 int, vnode_t **, cred_t *);
138 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
139 cred_t *, int, int, enum createmode4, int);
140 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
141 caller_context_t *);
142 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
143 vnode_t *, char *, cred_t *, nfsstat4 *);
144 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
145 vnode_t *, char *, cred_t *, nfsstat4 *);
146 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
147 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
148 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
149 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
150 page_t *[], size_t, struct seg *, caddr_t,
151 enum seg_rw, cred_t *);
152 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
153 cred_t *);
154 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
155 int, cred_t *);
156 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
157 int, cred_t *);
158 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
159 static void nfs4_set_mod(vnode_t *);
160 static void nfs4_get_commit(vnode_t *);
161 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
162 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
163 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
164 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
165 cred_t *);
166 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
167 cred_t *);
168 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
169 hrtime_t, vnode_t *, cred_t *);
170 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
171 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
172 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
173 u_offset_t);
174 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
175 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
176 static cred_t *state_to_cred(nfs4_open_stream_t *);
177 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
178 static pid_t lo_to_pid(lock_owner4 *);
179 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
180 cred_t *, nfs4_lock_owner_t *);
181 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
182 nfs4_lock_owner_t *);
183 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
184 static void nfs4_delmap_callback(struct as *, void *, uint_t);
185 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
186 static nfs4_delmapcall_t *nfs4_init_delmapcall();
187 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
188 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
189 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
190 uid_t, gid_t, int);
191
192 /*
193 * Routines that implement the setting of v4 args for the misc. ops
194 */
195 static void nfs4args_lock_free(nfs_argop4 *);
196 static void nfs4args_lockt_free(nfs_argop4 *);
197 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
198 int, rnode4_t *, cred_t *, bitmap4, int *,
199 nfs4_stateid_types_t *);
200 static void nfs4args_setattr_free(nfs_argop4 *);
201 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
202 bitmap4);
203 static void nfs4args_verify_free(nfs_argop4 *);
204 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
205 WRITE4args **, nfs4_stateid_types_t *);
206
207 /*
208 * These are the vnode ops functions that implement the vnode interface to
209 * the networked file system. See more comments below at nfs4_vnodeops.
210 */
211 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
212 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
213 caller_context_t *);
214 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
215 caller_context_t *);
216 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
217 caller_context_t *);
218 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
219 caller_context_t *);
220 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
221 caller_context_t *);
222 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
223 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
224 caller_context_t *);
225 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
226 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
227 int, vnode_t **, cred_t *, int, caller_context_t *,
228 vsecattr_t *);
229 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
230 int);
231 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
232 caller_context_t *, int);
233 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
234 caller_context_t *, int);
235 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
236 cred_t *, caller_context_t *, int, vsecattr_t *);
237 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
238 caller_context_t *, int);
239 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
240 cred_t *, caller_context_t *, int);
241 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
242 caller_context_t *, int);
243 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
244 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
245 page_t *[], size_t, struct seg *, caddr_t,
246 enum seg_rw, cred_t *, caller_context_t *);
247 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
248 caller_context_t *);
249 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
250 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
251 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
252 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
253 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
254 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
255 struct flk_callback *, cred_t *, caller_context_t *);
256 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
257 cred_t *, caller_context_t *);
258 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
259 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
260 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
261 cred_t *, caller_context_t *);
262 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
263 caller_context_t *);
264 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
265 caller_context_t *);
266 /*
267 * These vnode ops are required to be called from outside this source file,
268 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
269 * as static.
270 */
271 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
272 caller_context_t *);
273 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
274 int nfs4_lookup(vnode_t *, char *, vnode_t **,
275 struct pathname *, int, vnode_t *, cred_t *,
276 caller_context_t *, int *, pathname_t *);
277 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
278 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
279 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
280 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
281 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
282 caller_context_t *);
283 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
284 caller_context_t *);
285 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
286 caller_context_t *);
287
288 /*
289 * Used for nfs4_commit_vp() to indicate if we should
290 * wait on pending writes.
291 */
292 #define NFS4_WRITE_NOWAIT 0
293 #define NFS4_WRITE_WAIT 1
294
295 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
296
297 /*
298 * Error flags used to pass information about certain special errors
299 * which need to be handled specially.
300 */
301 #define NFS_EOF -98
302 #define NFS_VERF_MISMATCH -97
303
304 /*
305 * Flags used to differentiate between which operation drove the
306 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
307 */
308 #define NFS4_CLOSE_OP 0x1
309 #define NFS4_DELMAP_OP 0x2
310 #define NFS4_INACTIVE_OP 0x3
311
312 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
313
314 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
315 #define ALIGN64(x, ptr, sz) \
316 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
317 if (x) { \
318 x = sizeof (uint64_t) - (x); \
319 sz -= (x); \
320 ptr += (x); \
321 }
322
323 #ifdef DEBUG
324 int nfs4_client_attr_debug = 0;
325 int nfs4_client_state_debug = 0;
326 int nfs4_client_shadow_debug = 0;
327 int nfs4_client_lock_debug = 0;
328 int nfs4_seqid_sync = 0;
329 int nfs4_client_map_debug = 0;
330 static int nfs4_pageio_debug = 0;
331 int nfs4_client_inactive_debug = 0;
332 int nfs4_client_recov_debug = 0;
333 int nfs4_client_failover_debug = 0;
334 int nfs4_client_call_debug = 0;
335 int nfs4_client_lookup_debug = 0;
336 int nfs4_client_zone_debug = 0;
337 int nfs4_lost_rqst_debug = 0;
338 int nfs4_rdattrerr_debug = 0;
339 int nfs4_open_stream_debug = 0;
340
341 int nfs4read_error_inject;
342
343 static int nfs4_create_misses = 0;
344
345 static int nfs4_readdir_cache_shorts = 0;
346 static int nfs4_readdir_readahead = 0;
347
348 static int nfs4_bio_do_stop = 0;
349
350 static int nfs4_lostpage = 0; /* number of times we lost original page */
351
352 int nfs4_mmap_debug = 0;
353
354 static int nfs4_pathconf_cache_hits = 0;
355 static int nfs4_pathconf_cache_misses = 0;
356
357 int nfs4close_all_cnt;
358 int nfs4close_one_debug = 0;
359 int nfs4close_notw_debug = 0;
360
361 int denied_to_flk_debug = 0;
362 void *lockt_denied_debug;
363
364 #endif
365
366 /*
367 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
368 * or NFS4ERR_RESOURCE.
369 */
370 static int confirm_retry_sec = 30;
371
372 static int nfs4_lookup_neg_cache = 1;
373
374 /*
375 * number of pages to read ahead
376 * optimized for 100 base-T.
377 */
378 static int nfs4_nra = 4;
379
380 static int nfs4_do_symlink_cache = 1;
381
382 static int nfs4_pathconf_disable_cache = 0;
383
384 /*
385 * These are the vnode ops routines which implement the vnode interface to
386 * the networked file system. These routines just take their parameters,
387 * make them look networkish by putting the right info into interface structs,
388 * and then calling the appropriate remote routine(s) to do the work.
389 *
390 * Note on directory name lookup cacheing: If we detect a stale fhandle,
391 * we purge the directory cache relative to that vnode. This way, the
392 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
393 * more details on rnode locking.
394 */
395
396 struct vnodeops *nfs4_vnodeops;
397
398 const fs_operation_def_t nfs4_vnodeops_template[] = {
399 VOPNAME_OPEN, { .vop_open = nfs4_open },
400 VOPNAME_CLOSE, { .vop_close = nfs4_close },
401 VOPNAME_READ, { .vop_read = nfs4_read },
402 VOPNAME_WRITE, { .vop_write = nfs4_write },
403 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl },
404 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr },
405 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr },
406 VOPNAME_ACCESS, { .vop_access = nfs4_access },
407 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup },
408 VOPNAME_CREATE, { .vop_create = nfs4_create },
409 VOPNAME_REMOVE, { .vop_remove = nfs4_remove },
410 VOPNAME_LINK, { .vop_link = nfs4_link },
411 VOPNAME_RENAME, { .vop_rename = nfs4_rename },
412 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir },
413 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir },
414 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir },
415 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink },
416 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink },
417 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync },
418 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive },
419 VOPNAME_FID, { .vop_fid = nfs4_fid },
420 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock },
421 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock },
422 VOPNAME_SEEK, { .vop_seek = nfs4_seek },
423 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock },
424 VOPNAME_SPACE, { .vop_space = nfs4_space },
425 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp },
426 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage },
427 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage },
428 VOPNAME_MAP, { .vop_map = nfs4_map },
429 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap },
430 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap },
431 /* no separate nfs4_dump */
432 VOPNAME_DUMP, { .vop_dump = nfs_dump },
433 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf },
434 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio },
435 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose },
436 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr },
437 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr },
438 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock },
439 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
440 NULL, NULL
441 };
442
443 /*
444 * The following are subroutines and definitions to set args or get res
445 * for the different nfsv4 ops
446 */
447
448 void
449 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
450 {
451 int i;
452
453 for (i = 0; i < arglen; i++) {
454 if (argop[i].argop == OP_LOOKUP) {
455 kmem_free(
456 argop[i].nfs_argop4_u.oplookup.
457 objname.utf8string_val,
458 argop[i].nfs_argop4_u.oplookup.
459 objname.utf8string_len);
460 }
461 }
462 }
463
464 static void
465 nfs4args_lock_free(nfs_argop4 *argop)
466 {
467 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
468
469 if (locker->new_lock_owner == TRUE) {
470 open_to_lock_owner4 *open_owner;
471
472 open_owner = &locker->locker4_u.open_owner;
473 if (open_owner->lock_owner.owner_val != NULL) {
474 kmem_free(open_owner->lock_owner.owner_val,
475 open_owner->lock_owner.owner_len);
476 }
477 }
478 }
479
480 static void
481 nfs4args_lockt_free(nfs_argop4 *argop)
482 {
483 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
484
485 if (lowner->owner_val != NULL) {
486 kmem_free(lowner->owner_val, lowner->owner_len);
487 }
488 }
489
490 static void
491 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
492 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
493 nfs4_stateid_types_t *sid_types)
494 {
495 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
496 mntinfo4_t *mi;
497
498 argop->argop = OP_SETATTR;
499 /*
500 * The stateid is set to 0 if client is not modifying the size
501 * and otherwise to whatever nfs4_get_stateid() returns.
502 *
503 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
504 * state struct could be found for the process/file pair. We may
505 * want to change this in the future (by OPENing the file). See
506 * bug # 4474852.
507 */
508 if (vap->va_mask & AT_SIZE) {
509
510 ASSERT(rp != NULL);
511 mi = VTOMI4(RTOV4(rp));
512
513 argop->nfs_argop4_u.opsetattr.stateid =
514 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
515 OP_SETATTR, sid_types, FALSE);
516 } else {
517 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
518 sizeof (stateid4));
519 }
520
521 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
522 if (*error)
523 bzero(attr, sizeof (*attr));
524 }
525
526 static void
527 nfs4args_setattr_free(nfs_argop4 *argop)
528 {
529 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
530 }
531
532 static int
533 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
534 bitmap4 supp)
535 {
536 fattr4 *attr;
537 int error = 0;
538
539 argop->argop = op;
540 switch (op) {
541 case OP_VERIFY:
542 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
543 break;
544 case OP_NVERIFY:
545 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
546 break;
547 default:
548 return (EINVAL);
549 }
550 if (!error)
551 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
552 if (error)
553 bzero(attr, sizeof (*attr));
554 return (error);
555 }
556
557 static void
558 nfs4args_verify_free(nfs_argop4 *argop)
559 {
560 switch (argop->argop) {
561 case OP_VERIFY:
562 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
563 break;
564 case OP_NVERIFY:
565 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
566 break;
567 default:
568 break;
569 }
570 }
571
572 static void
573 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
574 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
575 {
576 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
577 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
578
579 argop->argop = OP_WRITE;
580 wargs->stable = stable;
581 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
582 mi, OP_WRITE, sid_tp);
583 wargs->mblk = NULL;
584 *wargs_pp = wargs;
585 }
586
587 void
588 nfs4args_copen_free(OPEN4cargs *open_args)
589 {
590 if (open_args->owner.owner_val) {
591 kmem_free(open_args->owner.owner_val,
592 open_args->owner.owner_len);
593 }
594 if ((open_args->opentype == OPEN4_CREATE) &&
595 (open_args->mode != EXCLUSIVE4)) {
596 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
597 }
598 }
599
600 /*
601 * XXX: This is referenced in modstubs.s
602 */
603 struct vnodeops *
604 nfs4_getvnodeops(void)
605 {
606 return (nfs4_vnodeops);
607 }
608
609 /*
610 * The OPEN operation opens a regular file.
611 */
612 /*ARGSUSED3*/
613 static int
614 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
615 {
616 vnode_t *dvp = NULL;
617 rnode4_t *rp, *drp;
618 int error;
619 int just_been_created;
620 char fn[MAXNAMELEN];
621
622 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
623 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
624 return (EIO);
625 rp = VTOR4(*vpp);
626
627 /*
628 * Check to see if opening something besides a regular file;
629 * if so skip the OTW call
630 */
631 if ((*vpp)->v_type != VREG) {
632 error = nfs4_open_non_reg_file(vpp, flag, cr);
633 return (error);
634 }
635
636 /*
637 * XXX - would like a check right here to know if the file is
638 * executable or not, so as to skip OTW
639 */
640
641 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
642 return (error);
643
644 drp = VTOR4(dvp);
645 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
646 return (EINTR);
647
648 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
649 nfs_rw_exit(&drp->r_rwlock);
650 return (error);
651 }
652
653 /*
654 * See if this file has just been CREATEd.
655 * If so, clear the flag and update the dnlc, which was previously
656 * skipped in nfs4_create.
657 * XXX need better serilization on this.
658 * XXX move this into the nf4open_otw call, after we have
659 * XXX acquired the open owner seqid sync.
660 */
661 mutex_enter(&rp->r_statev4_lock);
662 if (rp->created_v4) {
663 rp->created_v4 = 0;
664 mutex_exit(&rp->r_statev4_lock);
665
666 dnlc_update(dvp, fn, *vpp);
667 /* This is needed so we don't bump the open ref count */
668 just_been_created = 1;
669 } else {
670 mutex_exit(&rp->r_statev4_lock);
671 just_been_created = 0;
672 }
673
674 /*
675 * If caller specified O_TRUNC/FTRUNC, then be sure to set
676 * FWRITE (to drive successful setattr(size=0) after open)
677 */
678 if (flag & FTRUNC)
679 flag |= FWRITE;
680
681 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
682 just_been_created);
683
684 if (!error && !((*vpp)->v_flag & VROOT))
685 dnlc_update(dvp, fn, *vpp);
686
687 nfs_rw_exit(&drp->r_rwlock);
688
689 /* release the hold from vtodv */
690 VN_RELE(dvp);
691
692 /* exchange the shadow for the master vnode, if needed */
693
694 if (error == 0 && IS_SHADOW(*vpp, rp))
695 sv_exchange(vpp);
696
697 return (error);
698 }
699
700 /*
701 * See if there's a "lost open" request to be saved and recovered.
702 */
703 static void
704 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
705 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
706 vnode_t *dvp, OPEN4cargs *open_args)
707 {
708 vfs_t *vfsp;
709 char *srccfp;
710
711 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
712
713 if (error != ETIMEDOUT && error != EINTR &&
714 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
715 lost_rqstp->lr_op = 0;
716 return;
717 }
718
719 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
720 "nfs4open_save_lost_rqst: error %d", error));
721
722 lost_rqstp->lr_op = OP_OPEN;
723
724 /*
725 * The vp (if it is not NULL) and dvp are held and rele'd via
726 * the recovery code. See nfs4_save_lost_rqst.
727 */
728 lost_rqstp->lr_vp = vp;
729 lost_rqstp->lr_dvp = dvp;
730 lost_rqstp->lr_oop = oop;
731 lost_rqstp->lr_osp = NULL;
732 lost_rqstp->lr_lop = NULL;
733 lost_rqstp->lr_cr = cr;
734 lost_rqstp->lr_flk = NULL;
735 lost_rqstp->lr_oacc = open_args->share_access;
736 lost_rqstp->lr_odeny = open_args->share_deny;
737 lost_rqstp->lr_oclaim = open_args->claim;
738 if (open_args->claim == CLAIM_DELEGATE_CUR) {
739 lost_rqstp->lr_ostateid =
740 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
741 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
742 } else {
743 srccfp = open_args->open_claim4_u.cfile;
744 }
745 lost_rqstp->lr_ofile.utf8string_len = 0;
746 lost_rqstp->lr_ofile.utf8string_val = NULL;
747 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
748 lost_rqstp->lr_putfirst = FALSE;
749 }
750
751 struct nfs4_excl_time {
752 uint32 seconds;
753 uint32 nseconds;
754 };
755
756 /*
757 * The OPEN operation creates and/or opens a regular file
758 *
759 * ARGSUSED
760 */
761 static int
762 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
763 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
764 enum createmode4 createmode, int file_just_been_created)
765 {
766 rnode4_t *rp;
767 rnode4_t *drp = VTOR4(dvp);
768 vnode_t *vp = NULL;
769 vnode_t *vpi = *vpp;
770 bool_t needrecov = FALSE;
771
772 int doqueue = 1;
773
774 COMPOUND4args_clnt args;
775 COMPOUND4res_clnt res;
776 nfs_argop4 *argop;
777 nfs_resop4 *resop;
778 int argoplist_size;
779 int idx_open, idx_fattr;
780
781 GETFH4res *gf_res = NULL;
782 OPEN4res *op_res = NULL;
783 nfs4_ga_res_t *garp;
784 fattr4 *attr = NULL;
785 struct nfs4_excl_time verf;
786 bool_t did_excl_setup = FALSE;
787 int created_osp;
788
789 OPEN4cargs *open_args;
790 nfs4_open_owner_t *oop = NULL;
791 nfs4_open_stream_t *osp = NULL;
792 seqid4 seqid = 0;
793 bool_t retry_open = FALSE;
794 nfs4_recov_state_t recov_state;
795 nfs4_lost_rqst_t lost_rqst;
796 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
797 hrtime_t t;
798 int acc = 0;
799 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
800 cred_t *ncr = NULL;
801
802 nfs4_sharedfh_t *otw_sfh;
803 nfs4_sharedfh_t *orig_sfh;
804 int fh_differs = 0;
805 int numops, setgid_flag;
806 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
807
808 /*
809 * Make sure we properly deal with setting the right gid on
810 * a newly created file to reflect the parent's setgid bit
811 */
812 setgid_flag = 0;
813 if (create_flag && in_va) {
814
815 /*
816 * If there is grpid mount flag used or
817 * the parent's directory has the setgid bit set
818 * _and_ the client was able to get a valid mapping
819 * for the parent dir's owner_group, we want to
820 * append NVERIFY(owner_group == dva.va_gid) and
821 * SETATTR to the CREATE compound.
822 */
823 mutex_enter(&drp->r_statelock);
824 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
825 drp->r_attr.va_mode & VSGID) &&
826 drp->r_attr.va_gid != GID_NOBODY) {
827 in_va->va_mask |= AT_GID;
828 in_va->va_gid = drp->r_attr.va_gid;
829 setgid_flag = 1;
830 }
831 mutex_exit(&drp->r_statelock);
832 }
833
834 /*
835 * Normal/non-create compound:
836 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
837 *
838 * Open(create) compound no setgid:
839 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
840 * RESTOREFH + GETATTR
841 *
842 * Open(create) setgid:
843 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
844 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
845 * NVERIFY(grp) + SETATTR
846 */
847 if (setgid_flag) {
848 numops = 10;
849 idx_open = 1;
850 idx_fattr = 3;
851 } else if (create_flag) {
852 numops = 7;
853 idx_open = 2;
854 idx_fattr = 4;
855 } else {
856 numops = 4;
857 idx_open = 1;
858 idx_fattr = 3;
859 }
860
861 args.array_len = numops;
862 argoplist_size = numops * sizeof (nfs_argop4);
863 argop = kmem_alloc(argoplist_size, KM_SLEEP);
864
865 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
866 "open %s open flag 0x%x cred %p", file_name, open_flag,
867 (void *)cr));
868
869 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
870 if (create_flag) {
871 /*
872 * We are to create a file. Initialize the passed in vnode
873 * pointer.
874 */
875 vpi = NULL;
876 } else {
877 /*
878 * Check to see if the client owns a read delegation and is
879 * trying to open for write. If so, then return the delegation
880 * to avoid the server doing a cb_recall and returning DELAY.
881 * NB - we don't use the statev4_lock here because we'd have
882 * to drop the lock anyway and the result would be stale.
883 */
884 if ((open_flag & FWRITE) &&
885 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
886 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
887
888 /*
889 * If the file has a delegation, then do an access check up
890 * front. This avoids having to an access check later after
891 * we've already done start_op, which could deadlock.
892 */
893 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
894 if (open_flag & FREAD &&
895 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
896 acc |= VREAD;
897 if (open_flag & FWRITE &&
898 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
899 acc |= VWRITE;
900 }
901 }
902
903 drp = VTOR4(dvp);
904
905 recov_state.rs_flags = 0;
906 recov_state.rs_num_retry_despite_err = 0;
907 cred_otw = cr;
908
909 recov_retry:
910 fh_differs = 0;
911 nfs4_error_zinit(&e);
912
913 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
914 if (e.error) {
915 if (ncr != NULL)
916 crfree(ncr);
917 kmem_free(argop, argoplist_size);
918 return (e.error);
919 }
920
921 args.ctag = TAG_OPEN;
922 args.array_len = numops;
923 args.array = argop;
924
925 /* putfh directory fh */
926 argop[0].argop = OP_CPUTFH;
927 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
928
929 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
930 argop[idx_open].argop = OP_COPEN;
931 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
932 open_args->claim = CLAIM_NULL;
933
934 /* name of file */
935 open_args->open_claim4_u.cfile = file_name;
936 open_args->owner.owner_len = 0;
937 open_args->owner.owner_val = NULL;
938
939 if (create_flag) {
940 /* CREATE a file */
941 open_args->opentype = OPEN4_CREATE;
942 open_args->mode = createmode;
943 if (createmode == EXCLUSIVE4) {
944 if (did_excl_setup == FALSE) {
945 verf.seconds = zone_get_hostid(NULL);
946 if (verf.seconds != 0)
947 verf.nseconds = newnum();
948 else {
949 timestruc_t now;
950
951 gethrestime(&now);
952 verf.seconds = now.tv_sec;
953 verf.nseconds = now.tv_nsec;
954 }
955 /*
956 * Since the server will use this value for the
957 * mtime, make sure that it can't overflow. Zero
958 * out the MSB. The actual value does not matter
959 * here, only its uniqeness.
960 */
961 verf.seconds &= INT32_MAX;
962 did_excl_setup = TRUE;
963 }
964
965 /* Now copy over verifier to OPEN4args. */
966 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
967 } else {
968 int v_error;
969 bitmap4 supp_attrs;
970 servinfo4_t *svp;
971
972 attr = &open_args->createhow4_u.createattrs;
973
974 svp = drp->r_server;
975 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
976 supp_attrs = svp->sv_supp_attrs;
977 nfs_rw_exit(&svp->sv_lock);
978
979 /* GUARDED4 or UNCHECKED4 */
980 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
981 supp_attrs);
982 if (v_error) {
983 bzero(attr, sizeof (*attr));
984 nfs4args_copen_free(open_args);
985 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
986 &recov_state, FALSE);
987 if (ncr != NULL)
988 crfree(ncr);
989 kmem_free(argop, argoplist_size);
990 return (v_error);
991 }
992 }
993 } else {
994 /* NO CREATE */
995 open_args->opentype = OPEN4_NOCREATE;
996 }
997
998 if (recov_state.rs_sp != NULL) {
999 mutex_enter(&recov_state.rs_sp->s_lock);
1000 open_args->owner.clientid = recov_state.rs_sp->clientid;
1001 mutex_exit(&recov_state.rs_sp->s_lock);
1002 } else {
1003 /* XXX should we just fail here? */
1004 open_args->owner.clientid = 0;
1005 }
1006
1007 /*
1008 * This increments oop's ref count or creates a temporary 'just_created'
1009 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1010 * completes.
1011 */
1012 mutex_enter(&VTOMI4(dvp)->mi_lock);
1013
1014 /* See if a permanent or just created open owner exists */
1015 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1016 if (!oop) {
1017 /*
1018 * This open owner does not exist so create a temporary
1019 * just created one.
1020 */
1021 oop = create_open_owner(cr, VTOMI4(dvp));
1022 ASSERT(oop != NULL);
1023 }
1024 mutex_exit(&VTOMI4(dvp)->mi_lock);
1025
1026 /* this length never changes, do alloc before seqid sync */
1027 open_args->owner.owner_len = sizeof (oop->oo_name);
1028 open_args->owner.owner_val =
1029 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1030
1031 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1032 if (e.error == EAGAIN) {
1033 open_owner_rele(oop);
1034 nfs4args_copen_free(open_args);
1035 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1036 if (ncr != NULL) {
1037 crfree(ncr);
1038 ncr = NULL;
1039 }
1040 goto recov_retry;
1041 }
1042
1043 /* Check to see if we need to do the OTW call */
1044 if (!create_flag) {
1045 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1046 file_just_been_created, &e.error, acc, &recov_state)) {
1047
1048 /*
1049 * The OTW open is not necessary. Either
1050 * the open can succeed without it (eg.
1051 * delegation, error == 0) or the open
1052 * must fail due to an access failure
1053 * (error != 0). In either case, tidy
1054 * up and return.
1055 */
1056
1057 nfs4_end_open_seqid_sync(oop);
1058 open_owner_rele(oop);
1059 nfs4args_copen_free(open_args);
1060 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1061 if (ncr != NULL)
1062 crfree(ncr);
1063 kmem_free(argop, argoplist_size);
1064 return (e.error);
1065 }
1066 }
1067
1068 bcopy(&oop->oo_name, open_args->owner.owner_val,
1069 open_args->owner.owner_len);
1070
1071 seqid = nfs4_get_open_seqid(oop) + 1;
1072 open_args->seqid = seqid;
1073 open_args->share_access = 0;
1074 if (open_flag & FREAD)
1075 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1076 if (open_flag & FWRITE)
1077 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1078 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1079
1080
1081
1082 /*
1083 * getfh w/sanity check for idx_open/idx_fattr
1084 */
1085 ASSERT((idx_open + 1) == (idx_fattr - 1));
1086 argop[idx_open + 1].argop = OP_GETFH;
1087
1088 /* getattr */
1089 argop[idx_fattr].argop = OP_GETATTR;
1090 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1091 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1092
1093 if (setgid_flag) {
1094 vattr_t _v;
1095 servinfo4_t *svp;
1096 bitmap4 supp_attrs;
1097
1098 svp = drp->r_server;
1099 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1100 supp_attrs = svp->sv_supp_attrs;
1101 nfs_rw_exit(&svp->sv_lock);
1102
1103 /*
1104 * For setgid case, we need to:
1105 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1106 */
1107 argop[4].argop = OP_SAVEFH;
1108
1109 argop[5].argop = OP_CPUTFH;
1110 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1111
1112 argop[6].argop = OP_GETATTR;
1113 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1114 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1115
1116 argop[7].argop = OP_RESTOREFH;
1117
1118 /*
1119 * nverify
1120 */
1121 _v.va_mask = AT_GID;
1122 _v.va_gid = in_va->va_gid;
1123 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1124 supp_attrs))) {
1125
1126 /*
1127 * setattr
1128 *
1129 * We _know_ we're not messing with AT_SIZE or
1130 * AT_XTIME, so no need for stateid or flags.
1131 * Also we specify NULL rp since we're only
1132 * interested in setting owner_group attributes.
1133 */
1134 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1135 supp_attrs, &e.error, 0);
1136 if (e.error)
1137 nfs4args_verify_free(&argop[8]);
1138 }
1139
1140 if (e.error) {
1141 /*
1142 * XXX - Revisit the last argument to nfs4_end_op()
1143 * once 5020486 is fixed.
1144 */
1145 nfs4_end_open_seqid_sync(oop);
1146 open_owner_rele(oop);
1147 nfs4args_copen_free(open_args);
1148 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1149 if (ncr != NULL)
1150 crfree(ncr);
1151 kmem_free(argop, argoplist_size);
1152 return (e.error);
1153 }
1154 } else if (create_flag) {
1155 argop[1].argop = OP_SAVEFH;
1156
1157 argop[5].argop = OP_RESTOREFH;
1158
1159 argop[6].argop = OP_GETATTR;
1160 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1161 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1162 }
1163
1164 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1165 "nfs4open_otw: %s call, nm %s, rp %s",
1166 needrecov ? "recov" : "first", file_name,
1167 rnode4info(VTOR4(dvp))));
1168
1169 t = gethrtime();
1170
1171 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1172
1173 if (!e.error && nfs4_need_to_bump_seqid(&res))
1174 nfs4_set_open_seqid(seqid, oop, args.ctag);
1175
1176 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1177
1178 if (e.error || needrecov) {
1179 bool_t abort = FALSE;
1180
1181 if (needrecov) {
1182 nfs4_bseqid_entry_t *bsep = NULL;
1183
1184 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1185 cred_otw, vpi, dvp, open_args);
1186
1187 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1188 bsep = nfs4_create_bseqid_entry(oop, NULL,
1189 vpi, 0, args.ctag, open_args->seqid);
1190 num_bseqid_retry--;
1191 }
1192
1193 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1194 NULL, lost_rqst.lr_op == OP_OPEN ?
1195 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1196
1197 if (bsep)
1198 kmem_free(bsep, sizeof (*bsep));
1199 /* give up if we keep getting BAD_SEQID */
1200 if (num_bseqid_retry == 0)
1201 abort = TRUE;
1202 if (abort == TRUE && e.error == 0)
1203 e.error = geterrno4(res.status);
1204 }
1205 nfs4_end_open_seqid_sync(oop);
1206 open_owner_rele(oop);
1207 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1208 nfs4args_copen_free(open_args);
1209 if (setgid_flag) {
1210 nfs4args_verify_free(&argop[8]);
1211 nfs4args_setattr_free(&argop[9]);
1212 }
1213 if (!e.error)
1214 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1215 if (ncr != NULL) {
1216 crfree(ncr);
1217 ncr = NULL;
1218 }
1219 if (!needrecov || abort == TRUE || e.error == EINTR ||
1220 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1221 kmem_free(argop, argoplist_size);
1222 return (e.error);
1223 }
1224 goto recov_retry;
1225 }
1226
1227 /*
1228 * Will check and update lease after checking the rflag for
1229 * OPEN_CONFIRM in the successful OPEN call.
1230 */
1231 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1232
1233 /*
1234 * XXX what if we're crossing mount points from server1:/drp
1235 * to server2:/drp/rp.
1236 */
1237
1238 /* Signal our end of use of the open seqid */
1239 nfs4_end_open_seqid_sync(oop);
1240
1241 /*
1242 * This will destroy the open owner if it was just created,
1243 * and no one else has put a reference on it.
1244 */
1245 open_owner_rele(oop);
1246 if (create_flag && (createmode != EXCLUSIVE4) &&
1247 res.status == NFS4ERR_BADOWNER)
1248 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1249
1250 e.error = geterrno4(res.status);
1251 nfs4args_copen_free(open_args);
1252 if (setgid_flag) {
1253 nfs4args_verify_free(&argop[8]);
1254 nfs4args_setattr_free(&argop[9]);
1255 }
1256 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1257 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1258 /*
1259 * If the reply is NFS4ERR_ACCESS, it may be because
1260 * we are root (no root net access). If the real uid
1261 * is not root, then retry with the real uid instead.
1262 */
1263 if (ncr != NULL) {
1264 crfree(ncr);
1265 ncr = NULL;
1266 }
1267 if (res.status == NFS4ERR_ACCESS &&
1268 (ncr = crnetadjust(cred_otw)) != NULL) {
1269 cred_otw = ncr;
1270 goto recov_retry;
1271 }
1272 kmem_free(argop, argoplist_size);
1273 return (e.error);
1274 }
1275
1276 resop = &res.array[idx_open]; /* open res */
1277 op_res = &resop->nfs_resop4_u.opopen;
1278
1279 #ifdef DEBUG
1280 /*
1281 * verify attrset bitmap
1282 */
1283 if (create_flag &&
1284 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1285 /* make sure attrset returned is what we asked for */
1286 /* XXX Ignore this 'error' for now */
1287 if (attr->attrmask != op_res->attrset)
1288 /* EMPTY */;
1289 }
1290 #endif
1291
1292 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1293 mutex_enter(&VTOMI4(dvp)->mi_lock);
1294 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1295 mutex_exit(&VTOMI4(dvp)->mi_lock);
1296 }
1297
1298 resop = &res.array[idx_open + 1]; /* getfh res */
1299 gf_res = &resop->nfs_resop4_u.opgetfh;
1300
1301 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1302
1303 /*
1304 * The open stateid has been updated on the server but not
1305 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1306 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1307 * WRITE call. That, however, will use the old stateid, so go ahead
1308 * and upate the open stateid now, before any call to makenfs4node.
1309 */
1310 if (vpi) {
1311 nfs4_open_stream_t *tmp_osp;
1312 rnode4_t *tmp_rp = VTOR4(vpi);
1313
1314 tmp_osp = find_open_stream(oop, tmp_rp);
1315 if (tmp_osp) {
1316 tmp_osp->open_stateid = op_res->stateid;
1317 mutex_exit(&tmp_osp->os_sync_lock);
1318 open_stream_rele(tmp_osp, tmp_rp);
1319 }
1320
1321 /*
1322 * We must determine if the file handle given by the otw open
1323 * is the same as the file handle which was passed in with
1324 * *vpp. This case can be reached if the file we are trying
1325 * to open has been removed and another file has been created
1326 * having the same file name. The passed in vnode is released
1327 * later.
1328 */
1329 orig_sfh = VTOR4(vpi)->r_fh;
1330 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1331 }
1332
1333 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1334
1335 if (create_flag || fh_differs) {
1336 int rnode_err = 0;
1337
1338 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1339 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1340
1341 if (e.error)
1342 PURGE_ATTRCACHE4(vp);
1343 /*
1344 * For the newly created vp case, make sure the rnode
1345 * isn't bad before using it.
1346 */
1347 mutex_enter(&(VTOR4(vp))->r_statelock);
1348 if (VTOR4(vp)->r_flags & R4RECOVERR)
1349 rnode_err = EIO;
1350 mutex_exit(&(VTOR4(vp))->r_statelock);
1351
1352 if (rnode_err) {
1353 nfs4_end_open_seqid_sync(oop);
1354 nfs4args_copen_free(open_args);
1355 if (setgid_flag) {
1356 nfs4args_verify_free(&argop[8]);
1357 nfs4args_setattr_free(&argop[9]);
1358 }
1359 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1360 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1361 needrecov);
1362 open_owner_rele(oop);
1363 VN_RELE(vp);
1364 if (ncr != NULL)
1365 crfree(ncr);
1366 sfh4_rele(&otw_sfh);
1367 kmem_free(argop, argoplist_size);
1368 return (EIO);
1369 }
1370 } else {
1371 vp = vpi;
1372 }
1373 sfh4_rele(&otw_sfh);
1374
1375 /*
1376 * It seems odd to get a full set of attrs and then not update
1377 * the object's attrcache in the non-create case. Create case uses
1378 * the attrs since makenfs4node checks to see if the attrs need to
1379 * be updated (and then updates them). The non-create case should
1380 * update attrs also.
1381 */
1382 if (! create_flag && ! fh_differs && !e.error) {
1383 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1384 }
1385
1386 nfs4_error_zinit(&e);
1387 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1388 /* This does not do recovery for vp explicitly. */
1389 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1390 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1391
1392 if (e.error || e.stat) {
1393 nfs4_end_open_seqid_sync(oop);
1394 nfs4args_copen_free(open_args);
1395 if (setgid_flag) {
1396 nfs4args_verify_free(&argop[8]);
1397 nfs4args_setattr_free(&argop[9]);
1398 }
1399 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1400 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1401 needrecov);
1402 open_owner_rele(oop);
1403 if (create_flag || fh_differs) {
1404 /* rele the makenfs4node */
1405 VN_RELE(vp);
1406 }
1407 if (ncr != NULL) {
1408 crfree(ncr);
1409 ncr = NULL;
1410 }
1411 if (retry_open == TRUE) {
1412 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1413 "nfs4open_otw: retry the open since OPEN "
1414 "CONFIRM failed with error %d stat %d",
1415 e.error, e.stat));
1416 if (create_flag && createmode == GUARDED4) {
1417 NFS4_DEBUG(nfs4_client_recov_debug,
1418 (CE_NOTE, "nfs4open_otw: switch "
1419 "createmode from GUARDED4 to "
1420 "UNCHECKED4"));
1421 createmode = UNCHECKED4;
1422 }
1423 goto recov_retry;
1424 }
1425 if (!e.error) {
1426 if (create_flag && (createmode != EXCLUSIVE4) &&
1427 e.stat == NFS4ERR_BADOWNER)
1428 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1429
1430 e.error = geterrno4(e.stat);
1431 }
1432 kmem_free(argop, argoplist_size);
1433 return (e.error);
1434 }
1435 }
1436
1437 rp = VTOR4(vp);
1438
1439 mutex_enter(&rp->r_statev4_lock);
1440 if (create_flag)
1441 rp->created_v4 = 1;
1442 mutex_exit(&rp->r_statev4_lock);
1443
1444 mutex_enter(&oop->oo_lock);
1445 /* Doesn't matter if 'oo_just_created' already was set as this */
1446 oop->oo_just_created = NFS4_PERM_CREATED;
1447 if (oop->oo_cred_otw)
1448 crfree(oop->oo_cred_otw);
1449 oop->oo_cred_otw = cred_otw;
1450 crhold(oop->oo_cred_otw);
1451 mutex_exit(&oop->oo_lock);
1452
1453 /* returns with 'os_sync_lock' held */
1454 osp = find_or_create_open_stream(oop, rp, &created_osp);
1455 if (!osp) {
1456 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1457 "nfs4open_otw: failed to create an open stream"));
1458 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1459 "signal our end of use of the open seqid"));
1460
1461 nfs4_end_open_seqid_sync(oop);
1462 open_owner_rele(oop);
1463 nfs4args_copen_free(open_args);
1464 if (setgid_flag) {
1465 nfs4args_verify_free(&argop[8]);
1466 nfs4args_setattr_free(&argop[9]);
1467 }
1468 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1469 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1470 if (create_flag || fh_differs)
1471 VN_RELE(vp);
1472 if (ncr != NULL)
1473 crfree(ncr);
1474
1475 kmem_free(argop, argoplist_size);
1476 return (EINVAL);
1477
1478 }
1479
1480 osp->open_stateid = op_res->stateid;
1481
1482 if (open_flag & FREAD)
1483 osp->os_share_acc_read++;
1484 if (open_flag & FWRITE)
1485 osp->os_share_acc_write++;
1486 osp->os_share_deny_none++;
1487
1488 /*
1489 * Need to reset this bitfield for the possible case where we were
1490 * going to OTW CLOSE the file, got a non-recoverable error, and before
1491 * we could retry the CLOSE, OPENed the file again.
1492 */
1493 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1494 osp->os_final_close = 0;
1495 osp->os_force_close = 0;
1496 #ifdef DEBUG
1497 if (osp->os_failed_reopen)
1498 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1499 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1500 (void *)osp, (void *)cr, rnode4info(rp)));
1501 #endif
1502 osp->os_failed_reopen = 0;
1503
1504 mutex_exit(&osp->os_sync_lock);
1505
1506 nfs4_end_open_seqid_sync(oop);
1507
1508 if (created_osp && recov_state.rs_sp != NULL) {
1509 mutex_enter(&recov_state.rs_sp->s_lock);
1510 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1511 mutex_exit(&recov_state.rs_sp->s_lock);
1512 }
1513
1514 /* get rid of our reference to find oop */
1515 open_owner_rele(oop);
1516
1517 open_stream_rele(osp, rp);
1518
1519 /* accept delegation, if any */
1520 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1521
1522 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1523
1524 if (createmode == EXCLUSIVE4 &&
1525 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1526 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1527 " EXCLUSIVE4: sending a SETATTR"));
1528 /*
1529 * If doing an exclusive create, then generate
1530 * a SETATTR to set the initial attributes.
1531 * Try to set the mtime and the atime to the
1532 * server's current time. It is somewhat
1533 * expected that these fields will be used to
1534 * store the exclusive create cookie. If not,
1535 * server implementors will need to know that
1536 * a SETATTR will follow an exclusive create
1537 * and the cookie should be destroyed if
1538 * appropriate.
1539 *
1540 * The AT_GID and AT_SIZE bits are turned off
1541 * so that the SETATTR request will not attempt
1542 * to process these. The gid will be set
1543 * separately if appropriate. The size is turned
1544 * off because it is assumed that a new file will
1545 * be created empty and if the file wasn't empty,
1546 * then the exclusive create will have failed
1547 * because the file must have existed already.
1548 * Therefore, no truncate operation is needed.
1549 */
1550 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1551 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1552
1553 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1554 if (e.error) {
1555 /*
1556 * Couldn't correct the attributes of
1557 * the newly created file and the
1558 * attributes are wrong. Remove the
1559 * file and return an error to the
1560 * application.
1561 */
1562 /* XXX will this take care of client state ? */
1563 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1564 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1565 " remove file", e.error));
1566 VN_RELE(vp);
1567 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1568 /*
1569 * Since we've reled the vnode and removed
1570 * the file we now need to return the error.
1571 * At this point we don't want to update the
1572 * dircaches, call nfs4_waitfor_purge_complete
1573 * or set vpp to vp so we need to skip these
1574 * as well.
1575 */
1576 goto skip_update_dircaches;
1577 }
1578 }
1579
1580 /*
1581 * If we created or found the correct vnode, due to create_flag or
1582 * fh_differs being set, then update directory cache attribute, readdir
1583 * and dnlc caches.
1584 */
1585 if (create_flag || fh_differs) {
1586 dirattr_info_t dinfo, *dinfop;
1587
1588 /*
1589 * Make sure getattr succeeded before using results.
1590 * note: op 7 is getattr(dir) for both flavors of
1591 * open(create).
1592 */
1593 if (create_flag && res.status == NFS4_OK) {
1594 dinfo.di_time_call = t;
1595 dinfo.di_cred = cr;
1596 dinfo.di_garp =
1597 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1598 dinfop = &dinfo;
1599 } else {
1600 dinfop = NULL;
1601 }
1602
1603 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1604 dinfop);
1605 }
1606
1607 /*
1608 * If the page cache for this file was flushed from actions
1609 * above, it was done asynchronously and if that is true,
1610 * there is a need to wait here for it to complete. This must
1611 * be done outside of start_fop/end_fop.
1612 */
1613 (void) nfs4_waitfor_purge_complete(vp);
1614
1615 /*
1616 * It is implicit that we are in the open case (create_flag == 0) since
1617 * fh_differs can only be set to a non-zero value in the open case.
1618 */
1619 if (fh_differs != 0 && vpi != NULL)
1620 VN_RELE(vpi);
1621
1622 /*
1623 * Be sure to set *vpp to the correct value before returning.
1624 */
1625 *vpp = vp;
1626
1627 skip_update_dircaches:
1628
1629 nfs4args_copen_free(open_args);
1630 if (setgid_flag) {
1631 nfs4args_verify_free(&argop[8]);
1632 nfs4args_setattr_free(&argop[9]);
1633 }
1634 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1635
1636 if (ncr)
1637 crfree(ncr);
1638 kmem_free(argop, argoplist_size);
1639 return (e.error);
1640 }
1641
1642 /*
1643 * Reopen an open instance. cf. nfs4open_otw().
1644 *
1645 * Errors are returned by the nfs4_error_t parameter.
1646 * - ep->error contains an errno value or zero.
1647 * - if it is zero, ep->stat is set to an NFS status code, if any.
1648 * If the file could not be reopened, but the caller should continue, the
1649 * file is marked dead and no error values are returned. If the caller
1650 * should stop recovering open files and start over, either the ep->error
1651 * value or ep->stat will indicate an error (either something that requires
1652 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1653 * filehandles) may be handled silently by this routine.
1654 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1655 * will be started, so the caller should not do it.
1656 *
1657 * Gotos:
1658 * - kill_file : reopen failed in such a fashion to constitute marking the
1659 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1660 * is for cases where recovery is not possible.
1661 * - failed_reopen : same as above, except that the file has already been
1662 * marked dead, so no need to do it again.
1663 * - bailout : reopen failed but we are able to recover and retry the reopen -
1664 * either within this function immediately or via the calling function.
1665 */
1666
1667 void
1668 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1669 open_claim_type4 claim, bool_t frc_use_claim_previous,
1670 bool_t is_recov)
1671 {
1672 COMPOUND4args_clnt args;
1673 COMPOUND4res_clnt res;
1674 nfs_argop4 argop[4];
1675 nfs_resop4 *resop;
1676 OPEN4res *op_res = NULL;
1677 OPEN4cargs *open_args;
1678 GETFH4res *gf_res;
1679 rnode4_t *rp = VTOR4(vp);
1680 int doqueue = 1;
1681 cred_t *cr = NULL, *cred_otw = NULL;
1682 nfs4_open_owner_t *oop = NULL;
1683 seqid4 seqid;
1684 nfs4_ga_res_t *garp;
1685 char fn[MAXNAMELEN];
1686 nfs4_recov_state_t recov = {NULL, 0};
1687 nfs4_lost_rqst_t lost_rqst;
1688 mntinfo4_t *mi = VTOMI4(vp);
1689 bool_t abort;
1690 char *failed_msg = "";
1691 int fh_different;
1692 hrtime_t t;
1693 nfs4_bseqid_entry_t *bsep = NULL;
1694
1695 ASSERT(nfs4_consistent_type(vp));
1696 ASSERT(nfs_zone() == mi->mi_zone);
1697
1698 nfs4_error_zinit(ep);
1699
1700 /* this is the cred used to find the open owner */
1701 cr = state_to_cred(osp);
1702 if (cr == NULL) {
1703 failed_msg = "Couldn't reopen: no cred";
1704 goto kill_file;
1705 }
1706 /* use this cred for OTW operations */
1707 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1708
1709 top:
1710 nfs4_error_zinit(ep);
1711
1712 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1713 /* File system has been unmounted, quit */
1714 ep->error = EIO;
1715 failed_msg = "Couldn't reopen: file system has been unmounted";
1716 goto kill_file;
1717 }
1718
1719 oop = osp->os_open_owner;
1720
1721 ASSERT(oop != NULL);
1722 if (oop == NULL) { /* be defensive in non-DEBUG */
1723 failed_msg = "can't reopen: no open owner";
1724 goto kill_file;
1725 }
1726 open_owner_hold(oop);
1727
1728 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1729 if (ep->error) {
1730 open_owner_rele(oop);
1731 oop = NULL;
1732 goto bailout;
1733 }
1734
1735 /*
1736 * If the rnode has a delegation and the delegation has been
1737 * recovered and the server didn't request a recall and the caller
1738 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1739 * recovery) and the rnode hasn't been marked dead, then install
1740 * the delegation stateid in the open stream. Otherwise, proceed
1741 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1742 */
1743 mutex_enter(&rp->r_statev4_lock);
1744 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1745 !rp->r_deleg_return_pending &&
1746 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1747 !rp->r_deleg_needs_recall &&
1748 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1749 !(rp->r_flags & R4RECOVERR)) {
1750 mutex_enter(&osp->os_sync_lock);
1751 osp->os_delegation = 1;
1752 osp->open_stateid = rp->r_deleg_stateid;
1753 mutex_exit(&osp->os_sync_lock);
1754 mutex_exit(&rp->r_statev4_lock);
1755 goto bailout;
1756 }
1757 mutex_exit(&rp->r_statev4_lock);
1758
1759 /*
1760 * If the file failed recovery, just quit. This failure need not
1761 * affect other reopens, so don't return an error.
1762 */
1763 mutex_enter(&rp->r_statelock);
1764 if (rp->r_flags & R4RECOVERR) {
1765 mutex_exit(&rp->r_statelock);
1766 ep->error = 0;
1767 goto failed_reopen;
1768 }
1769 mutex_exit(&rp->r_statelock);
1770
1771 /*
1772 * argop is empty here
1773 *
1774 * PUTFH, OPEN, GETATTR
1775 */
1776 args.ctag = TAG_REOPEN;
1777 args.array_len = 4;
1778 args.array = argop;
1779
1780 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1781 "nfs4_reopen: file is type %d, id %s",
1782 vp->v_type, rnode4info(VTOR4(vp))));
1783
1784 argop[0].argop = OP_CPUTFH;
1785
1786 if (claim != CLAIM_PREVIOUS) {
1787 /*
1788 * if this is a file mount then
1789 * use the mntinfo parentfh
1790 */
1791 argop[0].nfs_argop4_u.opcputfh.sfh =
1792 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1793 VTOSV(vp)->sv_dfh;
1794 } else {
1795 /* putfh fh to reopen */
1796 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1797 }
1798
1799 argop[1].argop = OP_COPEN;
1800 open_args = &argop[1].nfs_argop4_u.opcopen;
1801 open_args->claim = claim;
1802
1803 if (claim == CLAIM_NULL) {
1804
1805 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1806 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1807 "failed for vp 0x%p for CLAIM_NULL with %m",
1808 (void *)vp);
1809 failed_msg = "Couldn't reopen: vtoname failed for "
1810 "CLAIM_NULL";
1811 /* nothing allocated yet */
1812 goto kill_file;
1813 }
1814
1815 open_args->open_claim4_u.cfile = fn;
1816 } else if (claim == CLAIM_PREVIOUS) {
1817
1818 /*
1819 * We have two cases to deal with here:
1820 * 1) We're being called to reopen files in order to satisfy
1821 * a lock operation request which requires us to explicitly
1822 * reopen files which were opened under a delegation. If
1823 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1824 * that case, frc_use_claim_previous is TRUE and we must
1825 * use the rnode's current delegation type (r_deleg_type).
1826 * 2) We're reopening files during some form of recovery.
1827 * In this case, frc_use_claim_previous is FALSE and we
1828 * use the delegation type appropriate for recovery
1829 * (r_deleg_needs_recovery).
1830 */
1831 mutex_enter(&rp->r_statev4_lock);
1832 open_args->open_claim4_u.delegate_type =
1833 frc_use_claim_previous ?
1834 rp->r_deleg_type :
1835 rp->r_deleg_needs_recovery;
1836 mutex_exit(&rp->r_statev4_lock);
1837
1838 } else if (claim == CLAIM_DELEGATE_CUR) {
1839
1840 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1841 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1842 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1843 "with %m", (void *)vp);
1844 failed_msg = "Couldn't reopen: vtoname failed for "
1845 "CLAIM_DELEGATE_CUR";
1846 /* nothing allocated yet */
1847 goto kill_file;
1848 }
1849
1850 mutex_enter(&rp->r_statev4_lock);
1851 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1852 rp->r_deleg_stateid;
1853 mutex_exit(&rp->r_statev4_lock);
1854
1855 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1856 }
1857 open_args->opentype = OPEN4_NOCREATE;
1858 open_args->owner.clientid = mi2clientid(mi);
1859 open_args->owner.owner_len = sizeof (oop->oo_name);
1860 open_args->owner.owner_val =
1861 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1862 bcopy(&oop->oo_name, open_args->owner.owner_val,
1863 open_args->owner.owner_len);
1864 open_args->share_access = 0;
1865 open_args->share_deny = 0;
1866
1867 mutex_enter(&osp->os_sync_lock);
1868 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1869 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1870 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1871 (void *)osp, (void *)rp, osp->os_share_acc_read,
1872 osp->os_share_acc_write, osp->os_open_ref_count,
1873 osp->os_mmap_read, osp->os_mmap_write, claim));
1874
1875 if (osp->os_share_acc_read || osp->os_mmap_read)
1876 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1877 if (osp->os_share_acc_write || osp->os_mmap_write)
1878 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1879 if (osp->os_share_deny_read)
1880 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1881 if (osp->os_share_deny_write)
1882 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1883 mutex_exit(&osp->os_sync_lock);
1884
1885 seqid = nfs4_get_open_seqid(oop) + 1;
1886 open_args->seqid = seqid;
1887
1888 /* Construct the getfh part of the compound */
1889 argop[2].argop = OP_GETFH;
1890
1891 /* Construct the getattr part of the compound */
1892 argop[3].argop = OP_GETATTR;
1893 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1894 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1895
1896 t = gethrtime();
1897
1898 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1899
1900 if (ep->error) {
1901 if (!is_recov && !frc_use_claim_previous &&
1902 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1903 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1904 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1905 cred_otw, vp, NULL, open_args);
1906 abort = nfs4_start_recovery(ep,
1907 VTOMI4(vp), vp, NULL, NULL,
1908 lost_rqst.lr_op == OP_OPEN ?
1909 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1910 nfs4args_copen_free(open_args);
1911 goto bailout;
1912 }
1913
1914 nfs4args_copen_free(open_args);
1915
1916 if (ep->error == EACCES && cred_otw != cr) {
1917 crfree(cred_otw);
1918 cred_otw = cr;
1919 crhold(cred_otw);
1920 nfs4_end_open_seqid_sync(oop);
1921 open_owner_rele(oop);
1922 oop = NULL;
1923 goto top;
1924 }
1925 if (ep->error == ETIMEDOUT)
1926 goto bailout;
1927 failed_msg = "Couldn't reopen: rpc error";
1928 goto kill_file;
1929 }
1930
1931 if (nfs4_need_to_bump_seqid(&res))
1932 nfs4_set_open_seqid(seqid, oop, args.ctag);
1933
1934 switch (res.status) {
1935 case NFS4_OK:
1936 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1937 mutex_enter(&rp->r_statelock);
1938 rp->r_delay_interval = 0;
1939 mutex_exit(&rp->r_statelock);
1940 }
1941 break;
1942 case NFS4ERR_BAD_SEQID:
1943 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1944 args.ctag, open_args->seqid);
1945
1946 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1947 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1948 NULL, OP_OPEN, bsep, NULL, NULL);
1949
1950 nfs4args_copen_free(open_args);
1951 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1952 nfs4_end_open_seqid_sync(oop);
1953 open_owner_rele(oop);
1954 oop = NULL;
1955 kmem_free(bsep, sizeof (*bsep));
1956
1957 goto kill_file;
1958 case NFS4ERR_NO_GRACE:
1959 nfs4args_copen_free(open_args);
1960 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1961 nfs4_end_open_seqid_sync(oop);
1962 open_owner_rele(oop);
1963 oop = NULL;
1964 if (claim == CLAIM_PREVIOUS) {
1965 /*
1966 * Retry as a plain open. We don't need to worry about
1967 * checking the changeinfo: it is acceptable for a
1968 * client to re-open a file and continue processing
1969 * (in the absence of locks).
1970 */
1971 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1972 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1973 "will retry as CLAIM_NULL"));
1974 claim = CLAIM_NULL;
1975 nfs4_mi_kstat_inc_no_grace(mi);
1976 goto top;
1977 }
1978 failed_msg =
1979 "Couldn't reopen: tried reclaim outside grace period. ";
1980 goto kill_file;
1981 case NFS4ERR_GRACE:
1982 nfs4_set_grace_wait(mi);
1983 nfs4args_copen_free(open_args);
1984 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1985 nfs4_end_open_seqid_sync(oop);
1986 open_owner_rele(oop);
1987 oop = NULL;
1988 ep->error = nfs4_wait_for_grace(mi, &recov);
1989 if (ep->error != 0)
1990 goto bailout;
1991 goto top;
1992 case NFS4ERR_DELAY:
1993 nfs4_set_delay_wait(vp);
1994 nfs4args_copen_free(open_args);
1995 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1996 nfs4_end_open_seqid_sync(oop);
1997 open_owner_rele(oop);
1998 oop = NULL;
1999 ep->error = nfs4_wait_for_delay(vp, &recov);
2000 nfs4_mi_kstat_inc_delay(mi);
2001 if (ep->error != 0)
2002 goto bailout;
2003 goto top;
2004 case NFS4ERR_FHEXPIRED:
2005 /* recover filehandle and retry */
2006 abort = nfs4_start_recovery(ep,
2007 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2008 nfs4args_copen_free(open_args);
2009 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2010 nfs4_end_open_seqid_sync(oop);
2011 open_owner_rele(oop);
2012 oop = NULL;
2013 if (abort == FALSE)
2014 goto top;
2015 failed_msg = "Couldn't reopen: recovery aborted";
2016 goto kill_file;
2017 case NFS4ERR_RESOURCE:
2018 case NFS4ERR_STALE_CLIENTID:
2019 case NFS4ERR_WRONGSEC:
2020 case NFS4ERR_EXPIRED:
2021 /*
2022 * Do not mark the file dead and let the calling
2023 * function initiate recovery.
2024 */
2025 nfs4args_copen_free(open_args);
2026 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2027 nfs4_end_open_seqid_sync(oop);
2028 open_owner_rele(oop);
2029 oop = NULL;
2030 goto bailout;
2031 case NFS4ERR_ACCESS:
2032 if (cred_otw != cr) {
2033 crfree(cred_otw);
2034 cred_otw = cr;
2035 crhold(cred_otw);
2036 nfs4args_copen_free(open_args);
2037 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2038 nfs4_end_open_seqid_sync(oop);
2039 open_owner_rele(oop);
2040 oop = NULL;
2041 goto top;
2042 }
2043 /* fall through */
2044 default:
2045 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2046 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2047 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2048 rnode4info(VTOR4(vp))));
2049 failed_msg = "Couldn't reopen: NFSv4 error";
2050 nfs4args_copen_free(open_args);
2051 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2052 goto kill_file;
2053 }
2054
2055 resop = &res.array[1]; /* open res */
2056 op_res = &resop->nfs_resop4_u.opopen;
2057
2058 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2059
2060 /*
2061 * Check if the path we reopened really is the same
2062 * file. We could end up in a situation where the file
2063 * was removed and a new file created with the same name.
2064 */
2065 resop = &res.array[2];
2066 gf_res = &resop->nfs_resop4_u.opgetfh;
2067 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2068 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2069 if (fh_different) {
2070 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2071 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2072 /* Oops, we don't have the same file */
2073 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2074 failed_msg = "Couldn't reopen: Persistent "
2075 "file handle changed";
2076 else
2077 failed_msg = "Couldn't reopen: Volatile "
2078 "(no expire on open) file handle changed";
2079
2080 nfs4args_copen_free(open_args);
2081 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2082 nfs_rw_exit(&mi->mi_fh_lock);
2083 goto kill_file;
2084
2085 } else {
2086 /*
2087 * We have volatile file handles that don't compare.
2088 * If the fids are the same then we assume that the
2089 * file handle expired but the rnode still refers to
2090 * the same file object.
2091 *
2092 * First check that we have fids or not.
2093 * If we don't we have a dumb server so we will
2094 * just assume every thing is ok for now.
2095 */
2096 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2097 rp->r_attr.va_mask & AT_NODEID &&
2098 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2099 /*
2100 * We have fids, but they don't
2101 * compare. So kill the file.
2102 */
2103 failed_msg =
2104 "Couldn't reopen: file handle changed"
2105 " due to mismatched fids";
2106 nfs4args_copen_free(open_args);
2107 (void) xdr_free(xdr_COMPOUND4res_clnt,
2108 (caddr_t)&res);
2109 nfs_rw_exit(&mi->mi_fh_lock);
2110 goto kill_file;
2111 } else {
2112 /*
2113 * We have volatile file handles that refers
2114 * to the same file (at least they have the
2115 * same fid) or we don't have fids so we
2116 * can't tell. :(. We'll be a kind and accepting
2117 * client so we'll update the rnode's file
2118 * handle with the otw handle.
2119 *
2120 * We need to drop mi->mi_fh_lock since
2121 * sh4_update acquires it. Since there is
2122 * only one recovery thread there is no
2123 * race.
2124 */
2125 nfs_rw_exit(&mi->mi_fh_lock);
2126 sfh4_update(rp->r_fh, &gf_res->object);
2127 }
2128 }
2129 } else {
2130 nfs_rw_exit(&mi->mi_fh_lock);
2131 }
2132
2133 ASSERT(nfs4_consistent_type(vp));
2134
2135 /*
2136 * If the server wanted an OPEN_CONFIRM but that fails, just start
2137 * over. Presumably if there is a persistent error it will show up
2138 * when we resend the OPEN.
2139 */
2140 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2141 bool_t retry_open = FALSE;
2142
2143 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2144 cred_otw, is_recov, &retry_open,
2145 oop, FALSE, ep, NULL);
2146 if (ep->error || ep->stat) {
2147 nfs4args_copen_free(open_args);
2148 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2149 nfs4_end_open_seqid_sync(oop);
2150 open_owner_rele(oop);
2151 oop = NULL;
2152 goto top;
2153 }
2154 }
2155
2156 mutex_enter(&osp->os_sync_lock);
2157 osp->open_stateid = op_res->stateid;
2158 osp->os_delegation = 0;
2159 /*
2160 * Need to reset this bitfield for the possible case where we were
2161 * going to OTW CLOSE the file, got a non-recoverable error, and before
2162 * we could retry the CLOSE, OPENed the file again.
2163 */
2164 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2165 osp->os_final_close = 0;
2166 osp->os_force_close = 0;
2167 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2168 osp->os_dc_openacc = open_args->share_access;
2169 mutex_exit(&osp->os_sync_lock);
2170
2171 nfs4_end_open_seqid_sync(oop);
2172
2173 /* accept delegation, if any */
2174 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2175
2176 nfs4args_copen_free(open_args);
2177
2178 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2179
2180 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2181
2182 ASSERT(nfs4_consistent_type(vp));
2183
2184 open_owner_rele(oop);
2185 crfree(cr);
2186 crfree(cred_otw);
2187 return;
2188
2189 kill_file:
2190 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2191 failed_reopen:
2192 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2193 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2194 (void *)osp, (void *)cr, rnode4info(rp)));
2195 mutex_enter(&osp->os_sync_lock);
2196 osp->os_failed_reopen = 1;
2197 mutex_exit(&osp->os_sync_lock);
2198 bailout:
2199 if (oop != NULL) {
2200 nfs4_end_open_seqid_sync(oop);
2201 open_owner_rele(oop);
2202 }
2203 if (cr != NULL)
2204 crfree(cr);
2205 if (cred_otw != NULL)
2206 crfree(cred_otw);
2207 }
2208
2209 /* for . and .. OPENs */
2210 /* ARGSUSED */
2211 static int
2212 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2213 {
2214 rnode4_t *rp;
2215 nfs4_ga_res_t gar;
2216
2217 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2218
2219 /*
2220 * If close-to-open consistency checking is turned off or
2221 * if there is no cached data, we can avoid
2222 * the over the wire getattr. Otherwise, force a
2223 * call to the server to get fresh attributes and to
2224 * check caches. This is required for close-to-open
2225 * consistency.
2226 */
2227 rp = VTOR4(*vpp);
2228 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2229 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2230 return (0);
2231
2232 gar.n4g_va.va_mask = AT_ALL;
2233 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2234 }
2235
2236 /*
2237 * CLOSE a file
2238 */
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)
2243 {
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 };
2249
2250 /*
2251 * Remove client state for this (lockowner, file) pair.
2252 * Issue otw v4 call to have the server do the same.
2253 */
2254
2255 rp = VTOR4(vp);
2256
2257 /*
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.
2261 */
2262 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2263 /*
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.
2269 *
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.
2278 */
2279 return (EIO);
2280 }
2281
2282 /*
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.
2289 */
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);
2295
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));
2305
2306 lm_rel_sysid(lmsid);
2307 }
2308 return (e.error);
2309 }
2310
2311 if (count > 1)
2312 return (0);
2313
2314 /*
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.
2318 */
2319 if (rp->r_unldvp != NULL)
2320 dnlc_purge_vp(vp);
2321
2322 /*
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.
2326 */
2327 ASSERT(!e.error);
2328 if ((flag & FWRITE) && nfs4_has_pages(vp))
2329 error = nfs4_putpage_commit(vp, 0, 0, cr);
2330
2331 mutex_enter(&rp->r_statelock);
2332 r_error = rp->r_error;
2333 rp->r_error = 0;
2334 mutex_exit(&rp->r_statelock);
2335
2336 /*
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.
2342 */
2343 if (vp->v_type != VREG)
2344 return (error ? error : r_error);
2345
2346 /*
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'.
2352 */
2353 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2354 n4error = e.error ? e.error : geterrno4(e.stat);
2355
2356 /*
2357 * Error reporting prio (Hi -> Lo)
2358 *
2359 * i) nfs4_putpage_commit (error)
2360 * ii) rnode's (r_error)
2361 * iii) nfs4close_one (n4error)
2362 */
2363 return (error ? error : (r_error ? r_error : n4error));
2364 }
2365
2366 /*
2367 * Initialize *lost_rqstp.
2368 */
2369
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)
2374 {
2375 if (error != ETIMEDOUT && error != EINTR &&
2376 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2377 lost_rqstp->lr_op = 0;
2378 return;
2379 }
2380
2381 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2382 "nfs4close_save_lost_rqst: error %d", error));
2383
2384 lost_rqstp->lr_op = OP_CLOSE;
2385 /*
2386 * The vp is held and rele'd via the recovery code.
2387 * See nfs4_save_lost_rqst.
2388 */
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;
2400 }
2401
2402 /*
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.
2407 *
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.
2411 *
2412 * Errors are returned via the nfs4_error_t parameter.
2413 */
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)
2418 {
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;
2431
2432 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2433
2434 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2435
2436 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2437
2438 /* Only set this to 1 if recovery is started */
2439 *recov = 0;
2440
2441 /* do the OTW call to close the file */
2442
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;
2449
2450 args.array_len = 3;
2451 args.array = argop;
2452
2453 vp = RTOV4(rp);
2454
2455 mi = VTOMI4(vp);
2456
2457 /* putfh target fh */
2458 argop[0].argop = OP_CPUTFH;
2459 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2460
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;
2464
2465 argop[2].argop = OP_CLOSE;
2466 close_args = &argop[2].nfs_argop4_u.opclose;
2467
2468 seqid = nfs4_get_open_seqid(oop) + 1;
2469
2470 close_args->seqid = seqid;
2471 close_args->open_stateid = osp->open_stateid;
2472
2473 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2474 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2475 rnode4info(rp)));
2476
2477 t = gethrtime();
2478
2479 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2480
2481 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2482 nfs4_set_open_seqid(seqid, oop, args.ctag);
2483 }
2484
2485 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2486 if (ep->error && !needrecov) {
2487 /*
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.
2491 */
2492 mutex_enter(&rp->r_statelock);
2493 PURGE_ATTRCACHE4_LOCKED(rp);
2494 rp->r_flags &= ~R4WRITEMODIFIED;
2495 mutex_exit(&rp->r_statelock);
2496 return;
2497 }
2498
2499 if (needrecov) {
2500 bool_t abort;
2501 nfs4_bseqid_entry_t *bsep = NULL;
2502
2503 if (close_type != CLOSE_RESEND)
2504 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2505 osp, cred_otw, vp);
2506
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);
2510
2511 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2512 "nfs4close_otw: initiating recovery. error %d "
2513 "res.status %d", ep->error, res.status));
2514
2515 /*
2516 * Drop the 'os_sync_lock' here so we don't hit
2517 * a potential recursive mutex_enter via an
2518 * 'open_stream_hold()'.
2519 */
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);
2526
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;
2530
2531 if (bsep)
2532 kmem_free(bsep, sizeof (*bsep));
2533 /*
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.
2539 */
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;
2547
2548 if (!ep->error)
2549 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2550 return;
2551 }
2552
2553 if (res.status) {
2554 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2555 return;
2556 }
2557
2558 mutex_enter(&rp->r_statev4_lock);
2559 rp->created_v4 = 0;
2560 mutex_exit(&rp->r_statev4_lock);
2561
2562 resop = &res.array[2];
2563 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2564 osp->os_valid = 0;
2565
2566 /*
2567 * This removes the reference obtained at OPEN; ie, when the
2568 * open stream structure was created.
2569 *
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.
2574 */
2575 ASSERT(osp->os_ref_count >= 2);
2576 osp->os_ref_count--;
2577
2578 if (!ep->error)
2579 nfs4_attr_cache(vp,
2580 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2581 t, cred_otw, TRUE, NULL);
2582
2583 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2584 " returning %d", ep->error));
2585
2586 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2587 }
2588
2589 /* ARGSUSED */
2590 static int
2591 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2592 caller_context_t *ct)
2593 {
2594 rnode4_t *rp;
2595 u_offset_t off;
2596 offset_t diff;
2597 uint_t on;
2598 uint_t n;
2599 caddr_t base;
2600 uint_t flags;
2601 int error;
2602 mntinfo4_t *mi;
2603
2604 rp = VTOR4(vp);
2605
2606 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2607
2608 if (IS_SHADOW(vp, rp))
2609 vp = RTOV4(rp);
2610
2611 if (vp->v_type != VREG)
2612 return (EISDIR);
2613
2614 mi = VTOMI4(vp);
2615
2616 if (nfs_zone() != mi->mi_zone)
2617 return (EIO);
2618
2619 if (uiop->uio_resid == 0)
2620 return (0);
2621
2622 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2623 return (EINVAL);
2624
2625 mutex_enter(&rp->r_statelock);
2626 if (rp->r_flags & R4RECOVERRP)
2627 error = (rp->r_error ? rp->r_error : EIO);
2628 else
2629 error = 0;
2630 mutex_exit(&rp->r_statelock);
2631 if (error)
2632 return (error);
2633
2634 /*
2635 * Bypass VM if caching has been disabled (e.g., locking) or if
2636 * using client-side direct I/O and the file is not mmap'd and
2637 * there are no cached pages.
2638 */
2639 if ((vp->v_flag & VNOCACHE) ||
2640 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2641 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2642 size_t resid = 0;
2643
2644 return (nfs4read(vp, NULL, uiop->uio_loffset,
2645 uiop->uio_resid, &resid, cr, FALSE, uiop));
2646 }
2647
2648 error = 0;
2649
2650 do {
2651 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2652 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2653 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2654
2655 if (error = nfs4_validate_caches(vp, cr))
2656 break;
2657
2658 mutex_enter(&rp->r_statelock);
2659 while (rp->r_flags & R4INCACHEPURGE) {
2660 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2661 mutex_exit(&rp->r_statelock);
2662 return (EINTR);
2663 }
2664 }
2665 diff = rp->r_size - uiop->uio_loffset;
2666 mutex_exit(&rp->r_statelock);
2667 if (diff <= 0)
2668 break;
2669 if (diff < n)
2670 n = (uint_t)diff;
2671
2672 if (vpm_enable) {
2673 /*
2674 * Copy data.
2675 */
2676 error = vpm_data_copy(vp, off + on, n, uiop,
2677 1, NULL, 0, S_READ);
2678 } else {
2679 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2680 S_READ);
2681
2682 error = uiomove(base + on, n, UIO_READ, uiop);
2683 }
2684
2685 if (!error) {
2686 /*
2687 * If read a whole block or read to eof,
2688 * won't need this buffer again soon.
2689 */
2690 mutex_enter(&rp->r_statelock);
2691 if (n + on == MAXBSIZE ||
2692 uiop->uio_loffset == rp->r_size)
2693 flags = SM_DONTNEED;
2694 else
2695 flags = 0;
2696 mutex_exit(&rp->r_statelock);
2697 if (vpm_enable) {
2698 error = vpm_sync_pages(vp, off, n, flags);
2699 } else {
2700 error = segmap_release(segkmap, base, flags);
2701 }
2702 } else {
2703 if (vpm_enable) {
2704 (void) vpm_sync_pages(vp, off, n, 0);
2705 } else {
2706 (void) segmap_release(segkmap, base, 0);
2707 }
2708 }
2709 } while (!error && uiop->uio_resid > 0);
2710
2711 return (error);
2712 }
2713
2714 /* ARGSUSED */
2715 static int
2716 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2717 caller_context_t *ct)
2718 {
2719 rlim64_t limit = uiop->uio_llimit;
2720 rnode4_t *rp;
2721 u_offset_t off;
2722 caddr_t base;
2723 uint_t flags;
2724 int remainder;
2725 size_t n;
2726 int on;
2727 int error;
2728 int resid;
2729 u_offset_t offset;
2730 mntinfo4_t *mi;
2731 uint_t bsize;
2732
2733 rp = VTOR4(vp);
2734
2735 if (IS_SHADOW(vp, rp))
2736 vp = RTOV4(rp);
2737
2738 if (vp->v_type != VREG)
2739 return (EISDIR);
2740
2741 mi = VTOMI4(vp);
2742
2743 if (nfs_zone() != mi->mi_zone)
2744 return (EIO);
2745
2746 if (uiop->uio_resid == 0)
2747 return (0);
2748
2749 mutex_enter(&rp->r_statelock);
2750 if (rp->r_flags & R4RECOVERRP)
2751 error = (rp->r_error ? rp->r_error : EIO);
2752 else
2753 error = 0;
2754 mutex_exit(&rp->r_statelock);
2755 if (error)
2756 return (error);
2757
2758 if (ioflag & FAPPEND) {
2759 struct vattr va;
2760
2761 /*
2762 * Must serialize if appending.
2763 */
2764 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2765 nfs_rw_exit(&rp->r_rwlock);
2766 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2767 INTR4(vp)))
2768 return (EINTR);
2769 }
2770
2771 va.va_mask = AT_SIZE;
2772 error = nfs4getattr(vp, &va, cr);
2773 if (error)
2774 return (error);
2775 uiop->uio_loffset = va.va_size;
2776 }
2777
2778 offset = uiop->uio_loffset + uiop->uio_resid;
2779
2780 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2781 return (EINVAL);
2782
2783 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2784 limit = MAXOFFSET_T;
2785
2786 /*
2787 * Check to make sure that the process will not exceed
2788 * its limit on file size. It is okay to write up to
2789 * the limit, but not beyond. Thus, the write which
2790 * reaches the limit will be short and the next write
2791 * will return an error.
2792 */
2793 remainder = 0;
2794 if (offset > uiop->uio_llimit) {
2795 remainder = offset - uiop->uio_llimit;
2796 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2797 if (uiop->uio_resid <= 0) {
2798 proc_t *p = ttoproc(curthread);
2799
2800 uiop->uio_resid += remainder;
2801 mutex_enter(&p->p_lock);
2802 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2803 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2804 mutex_exit(&p->p_lock);
2805 return (EFBIG);
2806 }
2807 }
2808
2809 /* update the change attribute, if we have a write delegation */
2810
2811 mutex_enter(&rp->r_statev4_lock);
2812 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2813 rp->r_deleg_change++;
2814
2815 mutex_exit(&rp->r_statev4_lock);
2816
2817 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2818 return (EINTR);
2819
2820 /*
2821 * Bypass VM if caching has been disabled (e.g., locking) or if
2822 * using client-side direct I/O and the file is not mmap'd and
2823 * there are no cached pages.
2824 */
2825 if ((vp->v_flag & VNOCACHE) ||
2826 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2827 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2828 size_t bufsize;
2829 int count;
2830 u_offset_t org_offset;
2831 stable_how4 stab_comm;
2832 nfs4_fwrite:
2833 if (rp->r_flags & R4STALE) {
2834 resid = uiop->uio_resid;
2835 offset = uiop->uio_loffset;
2836 error = rp->r_error;
2837 /*
2838 * A close may have cleared r_error, if so,
2839 * propagate ESTALE error return properly
2840 */
2841 if (error == 0)
2842 error = ESTALE;
2843 goto bottom;
2844 }
2845
2846 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2847 base = kmem_alloc(bufsize, KM_SLEEP);
2848 do {
2849 if (ioflag & FDSYNC)
2850 stab_comm = DATA_SYNC4;
2851 else
2852 stab_comm = FILE_SYNC4;
2853 resid = uiop->uio_resid;
2854 offset = uiop->uio_loffset;
2855 count = MIN(uiop->uio_resid, bufsize);
2856 org_offset = uiop->uio_loffset;
2857 error = uiomove(base, count, UIO_WRITE, uiop);
2858 if (!error) {
2859 error = nfs4write(vp, base, org_offset,
2860 count, cr, &stab_comm);
2861 if (!error) {
2862 mutex_enter(&rp->r_statelock);
2863 if (rp->r_size < uiop->uio_loffset)
2864 rp->r_size = uiop->uio_loffset;
2865 mutex_exit(&rp->r_statelock);
2866 }
2867 }
2868 } while (!error && uiop->uio_resid > 0);
2869 kmem_free(base, bufsize);
2870 goto bottom;
2871 }
2872
2873 bsize = vp->v_vfsp->vfs_bsize;
2874
2875 do {
2876 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2877 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2878 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2879
2880 resid = uiop->uio_resid;
2881 offset = uiop->uio_loffset;
2882
2883 if (rp->r_flags & R4STALE) {
2884 error = rp->r_error;
2885 /*
2886 * A close may have cleared r_error, if so,
2887 * propagate ESTALE error return properly
2888 */
2889 if (error == 0)
2890 error = ESTALE;
2891 break;
2892 }
2893
2894 /*
2895 * Don't create dirty pages faster than they
2896 * can be cleaned so that the system doesn't
2897 * get imbalanced. If the async queue is
2898 * maxed out, then wait for it to drain before
2899 * creating more dirty pages. Also, wait for
2900 * any threads doing pagewalks in the vop_getattr
2901 * entry points so that they don't block for
2902 * long periods.
2903 */
2904 mutex_enter(&rp->r_statelock);
2905 while ((mi->mi_max_threads != 0 &&
2906 rp->r_awcount > 2 * mi->mi_max_threads) ||
2907 rp->r_gcount > 0) {
2908 if (INTR4(vp)) {
2909 klwp_t *lwp = ttolwp(curthread);
2910
2911 if (lwp != NULL)
2912 lwp->lwp_nostop++;
2913 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2914 mutex_exit(&rp->r_statelock);
2915 if (lwp != NULL)
2916 lwp->lwp_nostop--;
2917 error = EINTR;
2918 goto bottom;
2919 }
2920 if (lwp != NULL)
2921 lwp->lwp_nostop--;
2922 } else
2923 cv_wait(&rp->r_cv, &rp->r_statelock);
2924 }
2925 mutex_exit(&rp->r_statelock);
2926
2927 /*
2928 * Touch the page and fault it in if it is not in core
2929 * before segmap_getmapflt or vpm_data_copy can lock it.
2930 * This is to avoid the deadlock if the buffer is mapped
2931 * to the same file through mmap which we want to write.
2932 */
2933 uio_prefaultpages((long)n, uiop);
2934
2935 if (vpm_enable) {
2936 /*
2937 * It will use kpm mappings, so no need to
2938 * pass an address.
2939 */
2940 error = writerp4(rp, NULL, n, uiop, 0);
2941 } else {
2942 if (segmap_kpm) {
2943 int pon = uiop->uio_loffset & PAGEOFFSET;
2944 size_t pn = MIN(PAGESIZE - pon,
2945 uiop->uio_resid);
2946 int pagecreate;
2947
2948 mutex_enter(&rp->r_statelock);
2949 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2950 uiop->uio_loffset + pn >= rp->r_size);
2951 mutex_exit(&rp->r_statelock);
2952
2953 base = segmap_getmapflt(segkmap, vp, off + on,
2954 pn, !pagecreate, S_WRITE);
2955
2956 error = writerp4(rp, base + pon, n, uiop,
2957 pagecreate);
2958
2959 } else {
2960 base = segmap_getmapflt(segkmap, vp, off + on,
2961 n, 0, S_READ);
2962 error = writerp4(rp, base + on, n, uiop, 0);
2963 }
2964 }
2965
2966 if (!error) {
2967 if (mi->mi_flags & MI4_NOAC)
2968 flags = SM_WRITE;
2969 else if ((uiop->uio_loffset % bsize) == 0 ||
2970 IS_SWAPVP(vp)) {
2971 /*
2972 * Have written a whole block.
2973 * Start an asynchronous write
2974 * and mark the buffer to
2975 * indicate that it won't be
2976 * needed again soon.
2977 */
2978 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
2979 } else
2980 flags = 0;
2981 if ((ioflag & (FSYNC|FDSYNC)) ||
2982 (rp->r_flags & R4OUTOFSPACE)) {
2983 flags &= ~SM_ASYNC;
2984 flags |= SM_WRITE;
2985 }
2986 if (vpm_enable) {
2987 error = vpm_sync_pages(vp, off, n, flags);
2988 } else {
2989 error = segmap_release(segkmap, base, flags);
2990 }
2991 } else {
2992 if (vpm_enable) {
2993 (void) vpm_sync_pages(vp, off, n, 0);
2994 } else {
2995 (void) segmap_release(segkmap, base, 0);
2996 }
2997 /*
2998 * In the event that we got an access error while
2999 * faulting in a page for a write-only file just
3000 * force a write.
3001 */
3002 if (error == EACCES)
3003 goto nfs4_fwrite;
3004 }
3005 } while (!error && uiop->uio_resid > 0);
3006
3007 bottom:
3008 if (error) {
3009 uiop->uio_resid = resid + remainder;
3010 uiop->uio_loffset = offset;
3011 } else {
3012 uiop->uio_resid += remainder;
3013
3014 mutex_enter(&rp->r_statev4_lock);
3015 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3016 gethrestime(&rp->r_attr.va_mtime);
3017 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3018 }
3019 mutex_exit(&rp->r_statev4_lock);
3020 }
3021
3022 nfs_rw_exit(&rp->r_lkserlock);
3023
3024 return (error);
3025 }
3026
3027 /*
3028 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3029 */
3030 static int
3031 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3032 int flags, cred_t *cr)
3033 {
3034 struct buf *bp;
3035 int error;
3036 page_t *savepp;
3037 uchar_t fsdata;
3038 stable_how4 stab_comm;
3039
3040 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3041 bp = pageio_setup(pp, len, vp, flags);
3042 ASSERT(bp != NULL);
3043
3044 /*
3045 * pageio_setup should have set b_addr to 0. This
3046 * is correct since we want to do I/O on a page
3047 * boundary. bp_mapin will use this addr to calculate
3048 * an offset, and then set b_addr to the kernel virtual
3049 * address it allocated for us.
3050 */
3051 ASSERT(bp->b_un.b_addr == 0);
3052
3053 bp->b_edev = 0;
3054 bp->b_dev = 0;
3055 bp->b_lblkno = lbtodb(off);
3056 bp->b_file = vp;
3057 bp->b_offset = (offset_t)off;
3058 bp_mapin(bp);
3059
3060 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3061 freemem > desfree)
3062 stab_comm = UNSTABLE4;
3063 else
3064 stab_comm = FILE_SYNC4;
3065
3066 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3067
3068 bp_mapout(bp);
3069 pageio_done(bp);
3070
3071 if (stab_comm == UNSTABLE4)
3072 fsdata = C_DELAYCOMMIT;
3073 else
3074 fsdata = C_NOCOMMIT;
3075
3076 savepp = pp;
3077 do {
3078 pp->p_fsdata = fsdata;
3079 } while ((pp = pp->p_next) != savepp);
3080
3081 return (error);
3082 }
3083
3084 /*
3085 */
3086 static int
3087 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3088 {
3089 nfs4_open_owner_t *oop;
3090 nfs4_open_stream_t *osp;
3091 rnode4_t *rp = VTOR4(vp);
3092 mntinfo4_t *mi = VTOMI4(vp);
3093 int reopen_needed;
3094
3095 ASSERT(nfs_zone() == mi->mi_zone);
3096
3097
3098 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3099 if (!oop)
3100 return (EIO);
3101
3102 /* returns with 'os_sync_lock' held */
3103 osp = find_open_stream(oop, rp);
3104 if (!osp) {
3105 open_owner_rele(oop);
3106 return (EIO);
3107 }
3108
3109 if (osp->os_failed_reopen) {
3110 mutex_exit(&osp->os_sync_lock);
3111 open_stream_rele(osp, rp);
3112 open_owner_rele(oop);
3113 return (EIO);
3114 }
3115
3116 /*
3117 * Determine whether a reopen is needed. If this
3118 * is a delegation open stream, then the os_delegation bit
3119 * should be set.
3120 */
3121
3122 reopen_needed = osp->os_delegation;
3123
3124 mutex_exit(&osp->os_sync_lock);
3125 open_owner_rele(oop);
3126
3127 if (reopen_needed) {
3128 nfs4_error_zinit(ep);
3129 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3130 mutex_enter(&osp->os_sync_lock);
3131 if (ep->error || ep->stat || osp->os_failed_reopen) {
3132 mutex_exit(&osp->os_sync_lock);
3133 open_stream_rele(osp, rp);
3134 return (EIO);
3135 }
3136 mutex_exit(&osp->os_sync_lock);
3137 }
3138 open_stream_rele(osp, rp);
3139
3140 return (0);
3141 }
3142
3143 /*
3144 * Write to file. Writes to remote server in largest size
3145 * chunks that the server can handle. Write is synchronous.
3146 */
3147 static int
3148 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3149 stable_how4 *stab_comm)
3150 {
3151 mntinfo4_t *mi;
3152 COMPOUND4args_clnt args;
3153 COMPOUND4res_clnt res;
3154 WRITE4args *wargs;
3155 WRITE4res *wres;
3156 nfs_argop4 argop[2];
3157 nfs_resop4 *resop;
3158 int tsize;
3159 stable_how4 stable;
3160 rnode4_t *rp;
3161 int doqueue = 1;
3162 bool_t needrecov;
3163 nfs4_recov_state_t recov_state;
3164 nfs4_stateid_types_t sid_types;
3165 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3166 int recov;
3167
3168 rp = VTOR4(vp);
3169 mi = VTOMI4(vp);
3170
3171 ASSERT(nfs_zone() == mi->mi_zone);
3172
3173 stable = *stab_comm;
3174 *stab_comm = FILE_SYNC4;
3175
3176 needrecov = FALSE;
3177 recov_state.rs_flags = 0;
3178 recov_state.rs_num_retry_despite_err = 0;
3179 nfs4_init_stateid_types(&sid_types);
3180
3181 /* Is curthread the recovery thread? */
3182 mutex_enter(&mi->mi_lock);
3183 recov = (mi->mi_recovthread == curthread);
3184 mutex_exit(&mi->mi_lock);
3185
3186 recov_retry:
3187 args.ctag = TAG_WRITE;
3188 args.array_len = 2;
3189 args.array = argop;
3190
3191 if (!recov) {
3192 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3193 &recov_state, NULL);
3194 if (e.error)
3195 return (e.error);
3196 }
3197
3198 /* 0. putfh target fh */
3199 argop[0].argop = OP_CPUTFH;
3200 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3201
3202 /* 1. write */
3203 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3204
3205 do {
3206
3207 wargs->offset = (offset4)offset;
3208 wargs->data_val = base;
3209
3210 if (mi->mi_io_kstats) {
3211 mutex_enter(&mi->mi_lock);
3212 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3213 mutex_exit(&mi->mi_lock);
3214 }
3215
3216 if ((vp->v_flag & VNOCACHE) ||
3217 (rp->r_flags & R4DIRECTIO) ||
3218 (mi->mi_flags & MI4_DIRECTIO))
3219 tsize = MIN(mi->mi_stsize, count);
3220 else
3221 tsize = MIN(mi->mi_curwrite, count);
3222 wargs->data_len = (uint_t)tsize;
3223 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3224
3225 if (mi->mi_io_kstats) {
3226 mutex_enter(&mi->mi_lock);
3227 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3228 mutex_exit(&mi->mi_lock);
3229 }
3230
3231 if (!recov) {
3232 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3233 if (e.error && !needrecov) {
3234 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3235 &recov_state, needrecov);
3236 return (e.error);
3237 }
3238 } else {
3239 if (e.error)
3240 return (e.error);
3241 }
3242
3243 /*
3244 * Do handling of OLD_STATEID outside
3245 * of the normal recovery framework.
3246 *
3247 * If write receives a BAD stateid error while using a
3248 * delegation stateid, retry using the open stateid (if it
3249 * exists). If it doesn't have an open stateid, reopen the
3250 * file first, then retry.
3251 */
3252 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3253 sid_types.cur_sid_type != SPEC_SID) {
3254 nfs4_save_stateid(&wargs->stateid, &sid_types);
3255 if (!recov)
3256 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3257 &recov_state, needrecov);
3258 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3259 goto recov_retry;
3260 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3261 sid_types.cur_sid_type == DEL_SID) {
3262 nfs4_save_stateid(&wargs->stateid, &sid_types);
3263 mutex_enter(&rp->r_statev4_lock);
3264 rp->r_deleg_return_pending = TRUE;
3265 mutex_exit(&rp->r_statev4_lock);
3266 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3267 if (!recov)
3268 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3269 &recov_state, needrecov);
3270 (void) xdr_free(xdr_COMPOUND4res_clnt,
3271 (caddr_t)&res);
3272 return (EIO);
3273 }
3274 if (!recov)
3275 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3276 &recov_state, needrecov);
3277 /* hold needed for nfs4delegreturn_thread */
3278 VN_HOLD(vp);
3279 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3280 NFS4_DR_DISCARD), FALSE);
3281 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3282 goto recov_retry;
3283 }
3284
3285 if (needrecov) {
3286 bool_t abort;
3287
3288 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3289 "nfs4write: client got error %d, res.status %d"
3290 ", so start recovery", e.error, res.status));
3291
3292 abort = nfs4_start_recovery(&e,
3293 VTOMI4(vp), vp, NULL, &wargs->stateid,
3294 NULL, OP_WRITE, NULL, NULL, NULL);
3295 if (!e.error) {
3296 e.error = geterrno4(res.status);
3297 (void) xdr_free(xdr_COMPOUND4res_clnt,
3298 (caddr_t)&res);
3299 }
3300 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3301 &recov_state, needrecov);
3302 if (abort == FALSE)
3303 goto recov_retry;
3304 return (e.error);
3305 }
3306
3307 if (res.status) {
3308 e.error = geterrno4(res.status);
3309 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3310 if (!recov)
3311 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3312 &recov_state, needrecov);
3313 return (e.error);
3314 }
3315
3316 resop = &res.array[1]; /* write res */
3317 wres = &resop->nfs_resop4_u.opwrite;
3318
3319 if ((int)wres->count > tsize) {
3320 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3321
3322 zcmn_err(getzoneid(), CE_WARN,
3323 "nfs4write: server wrote %u, requested was %u",
3324 (int)wres->count, tsize);
3325 if (!recov)
3326 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3327 &recov_state, needrecov);
3328 return (EIO);
3329 }
3330 if (wres->committed == UNSTABLE4) {
3331 *stab_comm = UNSTABLE4;
3332 if (wargs->stable == DATA_SYNC4 ||
3333 wargs->stable == FILE_SYNC4) {
3334 (void) xdr_free(xdr_COMPOUND4res_clnt,
3335 (caddr_t)&res);
3336 zcmn_err(getzoneid(), CE_WARN,
3337 "nfs4write: server %s did not commit "
3338 "to stable storage",
3339 rp->r_server->sv_hostname);
3340 if (!recov)
3341 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3342 OH_WRITE, &recov_state, needrecov);
3343 return (EIO);
3344 }
3345 }
3346
3347 tsize = (int)wres->count;
3348 count -= tsize;
3349 base += tsize;
3350 offset += tsize;
3351 if (mi->mi_io_kstats) {
3352 mutex_enter(&mi->mi_lock);
3353 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3354 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3355 tsize;
3356 mutex_exit(&mi->mi_lock);
3357 }
3358 lwp_stat_update(LWP_STAT_OUBLK, 1);
3359 mutex_enter(&rp->r_statelock);
3360 if (rp->r_flags & R4HAVEVERF) {
3361 if (rp->r_writeverf != wres->writeverf) {
3362 nfs4_set_mod(vp);
3363 rp->r_writeverf = wres->writeverf;
3364 }
3365 } else {
3366 rp->r_writeverf = wres->writeverf;
3367 rp->r_flags |= R4HAVEVERF;
3368 }
3369 PURGE_ATTRCACHE4_LOCKED(rp);
3370 rp->r_flags |= R4WRITEMODIFIED;
3371 gethrestime(&rp->r_attr.va_mtime);
3372 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3373 mutex_exit(&rp->r_statelock);
3374 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3375 } while (count);
3376
3377 if (!recov)
3378 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3379 needrecov);
3380
3381 return (e.error);
3382 }
3383
3384 /*
3385 * Read from a file. Reads data in largest chunks our interface can handle.
3386 */
3387 static int
3388 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3389 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3390 {
3391 mntinfo4_t *mi;
3392 COMPOUND4args_clnt args;
3393 COMPOUND4res_clnt res;
3394 READ4args *rargs;
3395 nfs_argop4 argop[2];
3396 int tsize;
3397 int doqueue;
3398 rnode4_t *rp;
3399 int data_len;
3400 bool_t is_eof;
3401 bool_t needrecov = FALSE;
3402 nfs4_recov_state_t recov_state;
3403 nfs4_stateid_types_t sid_types;
3404 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3405
3406 rp = VTOR4(vp);
3407 mi = VTOMI4(vp);
3408 doqueue = 1;
3409
3410 ASSERT(nfs_zone() == mi->mi_zone);
3411
3412 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3413
3414 args.array_len = 2;
3415 args.array = argop;
3416
3417 nfs4_init_stateid_types(&sid_types);
3418
3419 recov_state.rs_flags = 0;
3420 recov_state.rs_num_retry_despite_err = 0;
3421
3422 recov_retry:
3423 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3424 &recov_state, NULL);
3425 if (e.error)
3426 return (e.error);
3427
3428 /* putfh target fh */
3429 argop[0].argop = OP_CPUTFH;
3430 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3431
3432 /* read */
3433 argop[1].argop = OP_READ;
3434 rargs = &argop[1].nfs_argop4_u.opread;
3435 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3436 OP_READ, &sid_types, async);
3437
3438 do {
3439 if (mi->mi_io_kstats) {
3440 mutex_enter(&mi->mi_lock);
3441 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3442 mutex_exit(&mi->mi_lock);
3443 }
3444
3445 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3446 "nfs4read: %s call, rp %s",
3447 needrecov ? "recov" : "first",
3448 rnode4info(rp)));
3449
3450 if ((vp->v_flag & VNOCACHE) ||
3451 (rp->r_flags & R4DIRECTIO) ||
3452 (mi->mi_flags & MI4_DIRECTIO))
3453 tsize = MIN(mi->mi_tsize, count);
3454 else
3455 tsize = MIN(mi->mi_curread, count);
3456
3457 rargs->offset = (offset4)offset;
3458 rargs->count = (count4)tsize;
3459 rargs->res_data_val_alt = NULL;
3460 rargs->res_mblk = NULL;
3461 rargs->res_uiop = NULL;
3462 rargs->res_maxsize = 0;
3463 rargs->wlist = NULL;
3464
3465 if (uiop)
3466 rargs->res_uiop = uiop;
3467 else
3468 rargs->res_data_val_alt = base;
3469 rargs->res_maxsize = tsize;
3470
3471 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3472 #ifdef DEBUG
3473 if (nfs4read_error_inject) {
3474 res.status = nfs4read_error_inject;
3475 nfs4read_error_inject = 0;
3476 }
3477 #endif
3478
3479 if (mi->mi_io_kstats) {
3480 mutex_enter(&mi->mi_lock);
3481 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3482 mutex_exit(&mi->mi_lock);
3483 }
3484
3485 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3486 if (e.error != 0 && !needrecov) {
3487 nfs4_end_fop(mi, vp, NULL, OH_READ,
3488 &recov_state, needrecov);
3489 return (e.error);
3490 }
3491
3492 /*
3493 * Do proper retry for OLD and BAD stateid errors outside
3494 * of the normal recovery framework. There are two differences
3495 * between async and sync reads. The first is that we allow
3496 * retry on BAD_STATEID for async reads, but not sync reads.
3497 * The second is that we mark the file dead for a failed
3498 * attempt with a special stateid for sync reads, but just
3499 * return EIO for async reads.
3500 *
3501 * If a sync read receives a BAD stateid error while using a
3502 * delegation stateid, retry using the open stateid (if it
3503 * exists). If it doesn't have an open stateid, reopen the
3504 * file first, then retry.
3505 */
3506 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3507 res.status == NFS4ERR_BAD_STATEID) && async) {
3508 nfs4_end_fop(mi, vp, NULL, OH_READ,
3509 &recov_state, needrecov);
3510 if (sid_types.cur_sid_type == SPEC_SID) {
3511 (void) xdr_free(xdr_COMPOUND4res_clnt,
3512 (caddr_t)&res);
3513 return (EIO);
3514 }
3515 nfs4_save_stateid(&rargs->stateid, &sid_types);
3516 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3517 goto recov_retry;
3518 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3519 !async && sid_types.cur_sid_type != SPEC_SID) {
3520 nfs4_save_stateid(&rargs->stateid, &sid_types);
3521 nfs4_end_fop(mi, vp, NULL, OH_READ,
3522 &recov_state, needrecov);
3523 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3524 goto recov_retry;
3525 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3526 sid_types.cur_sid_type == DEL_SID) {
3527 nfs4_save_stateid(&rargs->stateid, &sid_types);
3528 mutex_enter(&rp->r_statev4_lock);
3529 rp->r_deleg_return_pending = TRUE;
3530 mutex_exit(&rp->r_statev4_lock);
3531 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3532 nfs4_end_fop(mi, vp, NULL, OH_READ,
3533 &recov_state, needrecov);
3534 (void) xdr_free(xdr_COMPOUND4res_clnt,
3535 (caddr_t)&res);
3536 return (EIO);
3537 }
3538 nfs4_end_fop(mi, vp, NULL, OH_READ,
3539 &recov_state, needrecov);
3540 /* hold needed for nfs4delegreturn_thread */
3541 VN_HOLD(vp);
3542 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3543 NFS4_DR_DISCARD), FALSE);
3544 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3545 goto recov_retry;
3546 }
3547 if (needrecov) {
3548 bool_t abort;
3549
3550 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3551 "nfs4read: initiating recovery\n"));
3552 abort = nfs4_start_recovery(&e,
3553 mi, vp, NULL, &rargs->stateid,
3554 NULL, OP_READ, NULL, NULL, NULL);
3555 nfs4_end_fop(mi, vp, NULL, OH_READ,
3556 &recov_state, needrecov);
3557 /*
3558 * Do not retry if we got OLD_STATEID using a special
3559 * stateid. This avoids looping with a broken server.
3560 */
3561 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3562 sid_types.cur_sid_type == SPEC_SID)
3563 abort = TRUE;
3564
3565 if (abort == FALSE) {
3566 /*
3567 * Need to retry all possible stateids in
3568 * case the recovery error wasn't stateid
3569 * related or the stateids have become
3570 * stale (server reboot).
3571 */
3572 nfs4_init_stateid_types(&sid_types);
3573 (void) xdr_free(xdr_COMPOUND4res_clnt,
3574 (caddr_t)&res);
3575 goto recov_retry;
3576 }
3577
3578 if (!e.error) {
3579 e.error = geterrno4(res.status);
3580 (void) xdr_free(xdr_COMPOUND4res_clnt,
3581 (caddr_t)&res);
3582 }
3583 return (e.error);
3584 }
3585
3586 if (res.status) {
3587 e.error = geterrno4(res.status);
3588 nfs4_end_fop(mi, vp, NULL, OH_READ,
3589 &recov_state, needrecov);
3590 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3591 return (e.error);
3592 }
3593
3594 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3595 count -= data_len;
3596 if (base)
3597 base += data_len;
3598 offset += data_len;
3599 if (mi->mi_io_kstats) {
3600 mutex_enter(&mi->mi_lock);
3601 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3602 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3603 mutex_exit(&mi->mi_lock);
3604 }
3605 lwp_stat_update(LWP_STAT_INBLK, 1);
3606 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3607 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3608
3609 } while (count && !is_eof);
3610
3611 *residp = count;
3612
3613 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3614
3615 return (e.error);
3616 }
3617
3618 /* ARGSUSED */
3619 static int
3620 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3621 caller_context_t *ct)
3622 {
3623 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3624 return (EIO);
3625 switch (cmd) {
3626 case _FIODIRECTIO:
3627 return (nfs4_directio(vp, (int)arg, cr));
3628 default:
3629 return (ENOTTY);
3630 }
3631 }
3632
3633 /* ARGSUSED */
3634 int
3635 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3636 caller_context_t *ct)
3637 {
3638 int error;
3639 rnode4_t *rp = VTOR4(vp);
3640
3641 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3642 return (EIO);
3643 /*
3644 * If it has been specified that the return value will
3645 * just be used as a hint, and we are only being asked
3646 * for size, fsid or rdevid, then return the client's
3647 * notion of these values without checking to make sure
3648 * that the attribute cache is up to date.
3649 * The whole point is to avoid an over the wire GETATTR
3650 * call.
3651 */
3652 if (flags & ATTR_HINT) {
3653 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3654 mutex_enter(&rp->r_statelock);
3655 if (vap->va_mask & AT_SIZE)
3656 vap->va_size = rp->r_size;
3657 if (vap->va_mask & AT_FSID)
3658 vap->va_fsid = rp->r_attr.va_fsid;
3659 if (vap->va_mask & AT_RDEV)
3660 vap->va_rdev = rp->r_attr.va_rdev;
3661 mutex_exit(&rp->r_statelock);
3662 return (0);
3663 }
3664 }
3665
3666 /*
3667 * Only need to flush pages if asking for the mtime
3668 * and if there any dirty pages or any outstanding
3669 * asynchronous (write) requests for this file.
3670 */
3671 if (vap->va_mask & AT_MTIME) {
3672 rp = VTOR4(vp);
3673 if (nfs4_has_pages(vp)) {
3674 mutex_enter(&rp->r_statev4_lock);
3675 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3676 mutex_exit(&rp->r_statev4_lock);
3677 if (rp->r_flags & R4DIRTY ||
3678 rp->r_awcount > 0) {
3679 mutex_enter(&rp->r_statelock);
3680 rp->r_gcount++;
3681 mutex_exit(&rp->r_statelock);
3682 error =
3683 nfs4_putpage(vp, (u_offset_t)0,
3684 0, 0, cr, NULL);
3685 mutex_enter(&rp->r_statelock);
3686 if (error && (error == ENOSPC ||
3687 error == EDQUOT)) {
3688 if (!rp->r_error)
3689 rp->r_error = error;
3690 }
3691 if (--rp->r_gcount == 0)
3692 cv_broadcast(&rp->r_cv);
3693 mutex_exit(&rp->r_statelock);
3694 }
3695 } else {
3696 mutex_exit(&rp->r_statev4_lock);
3697 }
3698 }
3699 }
3700 return (nfs4getattr(vp, vap, cr));
3701 }
3702
3703 int
3704 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3705 {
3706 /*
3707 * If these are the only two bits cleared
3708 * on the server then return 0 (OK) else
3709 * return 1 (BAD).
3710 */
3711 on_client &= ~(S_ISUID|S_ISGID);
3712 if (on_client == from_server)
3713 return (0);
3714 else
3715 return (1);
3716 }
3717
3718 /*ARGSUSED4*/
3719 static int
3720 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3721 caller_context_t *ct)
3722 {
3723 int error;
3724
3725 if (vap->va_mask & AT_NOSET)
3726 return (EINVAL);
3727
3728 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3729 return (EIO);
3730
3731 /*
3732 * Don't call secpolicy_vnode_setattr, the client cannot
3733 * use its cached attributes to make security decisions
3734 * as the server may be faking mode bits or mapping uid/gid.
3735 * Always just let the server to the checking.
3736 * If we provide the ability to remove basic priviledges
3737 * to setattr (e.g. basic without chmod) then we will
3738 * need to add a check here before calling the server.
3739 */
3740 error = nfs4setattr(vp, vap, flags, cr, NULL);
3741
3742 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3743 vnevent_truncate(vp, ct);
3744
3745 return (error);
3746 }
3747
3748 /*
3749 * To replace the "guarded" version 3 setattr, we use two types of compound
3750 * setattr requests:
3751 * 1. The "normal" setattr, used when the size of the file isn't being
3752 * changed - { Putfh <fh>; Setattr; Getattr }/
3753 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3754 * with only ctime as the argument. If the server ctime differs from
3755 * what is cached on the client, the verify will fail, but we would
3756 * already have the ctime from the preceding getattr, so just set it
3757 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3758 * Setattr; Getattr }.
3759 *
3760 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3761 * this setattr and NULL if they are not.
3762 */
3763 static int
3764 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3765 vsecattr_t *vsap)
3766 {
3767 COMPOUND4args_clnt args;
3768 COMPOUND4res_clnt res, *resp = NULL;
3769 nfs4_ga_res_t *garp = NULL;
3770 int numops = 3; /* { Putfh; Setattr; Getattr } */
3771 nfs_argop4 argop[5];
3772 int verify_argop = -1;
3773 int setattr_argop = 1;
3774 nfs_resop4 *resop;
3775 vattr_t va;
3776 rnode4_t *rp;
3777 int doqueue = 1;
3778 uint_t mask = vap->va_mask;
3779 mode_t omode;
3780 vsecattr_t *vsp;
3781 timestruc_t ctime;
3782 bool_t needrecov = FALSE;
3783 nfs4_recov_state_t recov_state;
3784 nfs4_stateid_types_t sid_types;
3785 stateid4 stateid;
3786 hrtime_t t;
3787 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3788 servinfo4_t *svp;
3789 bitmap4 supp_attrs;
3790
3791 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3792 rp = VTOR4(vp);
3793 nfs4_init_stateid_types(&sid_types);
3794
3795 /*
3796 * Only need to flush pages if there are any pages and
3797 * if the file is marked as dirty in some fashion. The
3798 * file must be flushed so that we can accurately
3799 * determine the size of the file and the cached data
3800 * after the SETATTR returns. A file is considered to
3801 * be dirty if it is either marked with R4DIRTY, has
3802 * outstanding i/o's active, or is mmap'd. In this
3803 * last case, we can't tell whether there are dirty
3804 * pages, so we flush just to be sure.
3805 */
3806 if (nfs4_has_pages(vp) &&
3807 ((rp->r_flags & R4DIRTY) ||
3808 rp->r_count > 0 ||
3809 rp->r_mapcnt > 0)) {
3810 ASSERT(vp->v_type != VCHR);
3811 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3812 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3813 mutex_enter(&rp->r_statelock);
3814 if (!rp->r_error)
3815 rp->r_error = e.error;
3816 mutex_exit(&rp->r_statelock);
3817 }
3818 }
3819
3820 if (mask & AT_SIZE) {
3821 /*
3822 * Verification setattr compound for non-deleg AT_SIZE:
3823 * { Putfh; Getattr; Verify; Setattr; Getattr }
3824 * Set ctime local here (outside the do_again label)
3825 * so that subsequent retries (after failed VERIFY)
3826 * will use ctime from GETATTR results (from failed
3827 * verify compound) as VERIFY arg.
3828 * If file has delegation, then VERIFY(time_metadata)
3829 * is of little added value, so don't bother.
3830 */
3831 mutex_enter(&rp->r_statev4_lock);
3832 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3833 rp->r_deleg_return_pending) {
3834 numops = 5;
3835 ctime = rp->r_attr.va_ctime;
3836 }
3837 mutex_exit(&rp->r_statev4_lock);
3838 }
3839
3840 recov_state.rs_flags = 0;
3841 recov_state.rs_num_retry_despite_err = 0;
3842
3843 args.ctag = TAG_SETATTR;
3844 do_again:
3845 recov_retry:
3846 setattr_argop = numops - 2;
3847
3848 args.array = argop;
3849 args.array_len = numops;
3850
3851 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3852 if (e.error)
3853 return (e.error);
3854
3855
3856 /* putfh target fh */
3857 argop[0].argop = OP_CPUTFH;
3858 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3859
3860 if (numops == 5) {
3861 /*
3862 * We only care about the ctime, but need to get mtime
3863 * and size for proper cache update.
3864 */
3865 /* getattr */
3866 argop[1].argop = OP_GETATTR;
3867 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3868 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3869
3870 /* verify - set later in loop */
3871 verify_argop = 2;
3872 }
3873
3874 /* setattr */
3875 svp = rp->r_server;
3876 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3877 supp_attrs = svp->sv_supp_attrs;
3878 nfs_rw_exit(&svp->sv_lock);
3879
3880 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3881 supp_attrs, &e.error, &sid_types);
3882 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3883 if (e.error) {
3884 /* req time field(s) overflow - return immediately */
3885 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3886 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3887 opsetattr.obj_attributes);
3888 return (e.error);
3889 }
3890 omode = rp->r_attr.va_mode;
3891
3892 /* getattr */
3893 argop[numops-1].argop = OP_GETATTR;
3894 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3895 /*
3896 * If we are setting the ACL (indicated only by vsap != NULL), request
3897 * the ACL in this getattr. The ACL returned from this getattr will be
3898 * used in updating the ACL cache.
3899 */
3900 if (vsap != NULL)
3901 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3902 FATTR4_ACL_MASK;
3903 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3904
3905 /*
3906 * setattr iterates if the object size is set and the cached ctime
3907 * does not match the file ctime. In that case, verify the ctime first.
3908 */
3909
3910 do {
3911 if (verify_argop != -1) {
3912 /*
3913 * Verify that the ctime match before doing setattr.
3914 */
3915 va.va_mask = AT_CTIME;
3916 va.va_ctime = ctime;
3917 svp = rp->r_server;
3918 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3919 supp_attrs = svp->sv_supp_attrs;
3920 nfs_rw_exit(&svp->sv_lock);
3921 e.error = nfs4args_verify(&argop[verify_argop], &va,
3922 OP_VERIFY, supp_attrs);
3923 if (e.error) {
3924 /* req time field(s) overflow - return */
3925 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3926 needrecov);
3927 break;
3928 }
3929 }
3930
3931 doqueue = 1;
3932
3933 t = gethrtime();
3934
3935 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3936
3937 /*
3938 * Purge the access cache and ACL cache if changing either the
3939 * owner of the file, the group owner, or the mode. These may
3940 * change the access permissions of the file, so purge old
3941 * information and start over again.
3942 */
3943 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3944 (void) nfs4_access_purge_rp(rp);
3945 if (rp->r_secattr != NULL) {
3946 mutex_enter(&rp->r_statelock);
3947 vsp = rp->r_secattr;
3948 rp->r_secattr = NULL;
3949 mutex_exit(&rp->r_statelock);
3950 if (vsp != NULL)
3951 nfs4_acl_free_cache(vsp);
3952 }
3953 }
3954
3955 /*
3956 * If res.array_len == numops, then everything succeeded,
3957 * except for possibly the final getattr. If only the
3958 * last getattr failed, give up, and don't try recovery.
3959 */
3960 if (res.array_len == numops) {
3961 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3962 needrecov);
3963 if (! e.error)
3964 resp = &res;
3965 break;
3966 }
3967
3968 /*
3969 * if either rpc call failed or completely succeeded - done
3970 */
3971 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3972 if (e.error) {
3973 PURGE_ATTRCACHE4(vp);
3974 if (!needrecov) {
3975 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3976 needrecov);
3977 break;
3978 }
3979 }
3980
3981 /*
3982 * Do proper retry for OLD_STATEID outside of the normal
3983 * recovery framework.
3984 */
3985 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3986 sid_types.cur_sid_type != SPEC_SID &&
3987 sid_types.cur_sid_type != NO_SID) {
3988 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3989 needrecov);
3990 nfs4_save_stateid(&stateid, &sid_types);
3991 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3992 opsetattr.obj_attributes);
3993 if (verify_argop != -1) {
3994 nfs4args_verify_free(&argop[verify_argop]);
3995 verify_argop = -1;
3996 }
3997 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3998 goto recov_retry;
3999 }
4000
4001 if (needrecov) {
4002 bool_t abort;
4003
4004 abort = nfs4_start_recovery(&e,
4005 VTOMI4(vp), vp, NULL, NULL, NULL,
4006 OP_SETATTR, NULL, NULL, NULL);
4007 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4008 needrecov);
4009 /*
4010 * Do not retry if we failed with OLD_STATEID using
4011 * a special stateid. This is done to avoid looping
4012 * with a broken server.
4013 */
4014 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4015 (sid_types.cur_sid_type == SPEC_SID ||
4016 sid_types.cur_sid_type == NO_SID))
4017 abort = TRUE;
4018 if (!e.error) {
4019 if (res.status == NFS4ERR_BADOWNER)
4020 nfs4_log_badowner(VTOMI4(vp),
4021 OP_SETATTR);
4022
4023 e.error = geterrno4(res.status);
4024 (void) xdr_free(xdr_COMPOUND4res_clnt,
4025 (caddr_t)&res);
4026 }
4027 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4028 opsetattr.obj_attributes);
4029 if (verify_argop != -1) {
4030 nfs4args_verify_free(&argop[verify_argop]);
4031 verify_argop = -1;
4032 }
4033 if (abort == FALSE) {
4034 /*
4035 * Need to retry all possible stateids in
4036 * case the recovery error wasn't stateid
4037 * related or the stateids have become
4038 * stale (server reboot).
4039 */
4040 nfs4_init_stateid_types(&sid_types);
4041 goto recov_retry;
4042 }
4043 return (e.error);
4044 }
4045
4046 /*
4047 * Need to call nfs4_end_op before nfs4getattr to
4048 * avoid potential nfs4_start_op deadlock. See RFE
4049 * 4777612. Calls to nfs4_invalidate_pages() and
4050 * nfs4_purge_stale_fh() might also generate over the
4051 * wire calls which my cause nfs4_start_op() deadlock.
4052 */
4053 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4054
4055 /*
4056 * Check to update lease.
4057 */
4058 resp = &res;
4059 if (res.status == NFS4_OK) {
4060 break;
4061 }
4062
4063 /*
4064 * Check if verify failed to see if try again
4065 */
4066 if ((verify_argop == -1) || (res.array_len != 3)) {
4067 /*
4068 * can't continue...
4069 */
4070 if (res.status == NFS4ERR_BADOWNER)
4071 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4072
4073 e.error = geterrno4(res.status);
4074 } else {
4075 /*
4076 * When the verify request fails, the client ctime is
4077 * not in sync with the server. This is the same as
4078 * the version 3 "not synchronized" error, and we
4079 * handle it in a similar manner (XXX do we need to???).
4080 * Use the ctime returned in the first getattr for
4081 * the input to the next verify.
4082 * If we couldn't get the attributes, then we give up
4083 * because we can't complete the operation as required.
4084 */
4085 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4086 }
4087 if (e.error) {
4088 PURGE_ATTRCACHE4(vp);
4089 nfs4_purge_stale_fh(e.error, vp, cr);
4090 } else {
4091 /*
4092 * retry with a new verify value
4093 */
4094 ctime = garp->n4g_va.va_ctime;
4095 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4096 resp = NULL;
4097 }
4098 if (!e.error) {
4099 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4100 opsetattr.obj_attributes);
4101 if (verify_argop != -1) {
4102 nfs4args_verify_free(&argop[verify_argop]);
4103 verify_argop = -1;
4104 }
4105 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4106 goto do_again;
4107 }
4108 } while (!e.error);
4109
4110 if (e.error) {
4111 /*
4112 * If we are here, rfs4call has an irrecoverable error - return
4113 */
4114 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4115 opsetattr.obj_attributes);
4116 if (verify_argop != -1) {
4117 nfs4args_verify_free(&argop[verify_argop]);
4118 verify_argop = -1;
4119 }
4120 if (resp)
4121 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4122 return (e.error);
4123 }
4124
4125
4126
4127 /*
4128 * If changing the size of the file, invalidate
4129 * any local cached data which is no longer part
4130 * of the file. We also possibly invalidate the
4131 * last page in the file. We could use
4132 * pvn_vpzero(), but this would mark the page as
4133 * modified and require it to be written back to
4134 * the server for no particularly good reason.
4135 * This way, if we access it, then we bring it
4136 * back in. A read should be cheaper than a
4137 * write.
4138 */
4139 if (mask & AT_SIZE) {
4140 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4141 }
4142
4143 /* either no error or one of the postop getattr failed */
4144
4145 /*
4146 * XXX Perform a simplified version of wcc checking. Instead of
4147 * have another getattr to get pre-op, just purge cache if
4148 * any of the ops prior to and including the getattr failed.
4149 * If the getattr succeeded then update the attrcache accordingly.
4150 */
4151
4152 garp = NULL;
4153 if (res.status == NFS4_OK) {
4154 /*
4155 * Last getattr
4156 */
4157 resop = &res.array[numops - 1];
4158 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4159 }
4160 /*
4161 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4162 * rather than filling it. See the function itself for details.
4163 */
4164 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4165 if (garp != NULL) {
4166 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4167 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4168 vs_ace4_destroy(&garp->n4g_vsa);
4169 } else {
4170 if (vsap != NULL) {
4171 /*
4172 * The ACL was supposed to be set and to be
4173 * returned in the last getattr of this
4174 * compound, but for some reason the getattr
4175 * result doesn't contain the ACL. In this
4176 * case, purge the ACL cache.
4177 */
4178 if (rp->r_secattr != NULL) {
4179 mutex_enter(&rp->r_statelock);
4180 vsp = rp->r_secattr;
4181 rp->r_secattr = NULL;
4182 mutex_exit(&rp->r_statelock);
4183 if (vsp != NULL)
4184 nfs4_acl_free_cache(vsp);
4185 }
4186 }
4187 }
4188 }
4189
4190 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4191 /*
4192 * Set the size, rather than relying on getting it updated
4193 * via a GETATTR. With delegations the client tries to
4194 * suppress GETATTR calls.
4195 */
4196 mutex_enter(&rp->r_statelock);
4197 rp->r_size = vap->va_size;
4198 mutex_exit(&rp->r_statelock);
4199 }
4200
4201 /*
4202 * Can free up request args and res
4203 */
4204 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4205 opsetattr.obj_attributes);
4206 if (verify_argop != -1) {
4207 nfs4args_verify_free(&argop[verify_argop]);
4208 verify_argop = -1;
4209 }
4210 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4211
4212 /*
4213 * Some servers will change the mode to clear the setuid
4214 * and setgid bits when changing the uid or gid. The
4215 * client needs to compensate appropriately.
4216 */
4217 if (mask & (AT_UID | AT_GID)) {
4218 int terror, do_setattr;
4219
4220 do_setattr = 0;
4221 va.va_mask = AT_MODE;
4222 terror = nfs4getattr(vp, &va, cr);
4223 if (!terror &&
4224 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4225 (!(mask & AT_MODE) && va.va_mode != omode))) {
4226 va.va_mask = AT_MODE;
4227 if (mask & AT_MODE) {
4228 /*
4229 * We asked the mode to be changed and what
4230 * we just got from the server in getattr is
4231 * not what we wanted it to be, so set it now.
4232 */
4233 va.va_mode = vap->va_mode;
4234 do_setattr = 1;
4235 } else {
4236 /*
4237 * We did not ask the mode to be changed,
4238 * Check to see that the server just cleared
4239 * I_SUID and I_GUID from it. If not then
4240 * set mode to omode with UID/GID cleared.
4241 */
4242 if (nfs4_compare_modes(va.va_mode, omode)) {
4243 omode &= ~(S_ISUID|S_ISGID);
4244 va.va_mode = omode;
4245 do_setattr = 1;
4246 }
4247 }
4248
4249 if (do_setattr)
4250 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4251 }
4252 }
4253
4254 return (e.error);
4255 }
4256
4257 /* ARGSUSED */
4258 static int
4259 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4260 {
4261 COMPOUND4args_clnt args;
4262 COMPOUND4res_clnt res;
4263 int doqueue;
4264 uint32_t acc, resacc, argacc;
4265 rnode4_t *rp;
4266 cred_t *cred, *ncr, *ncrfree = NULL;
4267 nfs4_access_type_t cacc;
4268 int num_ops;
4269 nfs_argop4 argop[3];
4270 nfs_resop4 *resop;
4271 bool_t needrecov = FALSE, do_getattr;
4272 nfs4_recov_state_t recov_state;
4273 int rpc_error;
4274 hrtime_t t;
4275 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4276 mntinfo4_t *mi = VTOMI4(vp);
4277
4278 if (nfs_zone() != mi->mi_zone)
4279 return (EIO);
4280
4281 acc = 0;
4282 if (mode & VREAD)
4283 acc |= ACCESS4_READ;
4284 if (mode & VWRITE) {
4285 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4286 return (EROFS);
4287 if (vp->v_type == VDIR)
4288 acc |= ACCESS4_DELETE;
4289 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4290 }
4291 if (mode & VEXEC) {
4292 if (vp->v_type == VDIR)
4293 acc |= ACCESS4_LOOKUP;
4294 else
4295 acc |= ACCESS4_EXECUTE;
4296 }
4297
4298 if (VTOR4(vp)->r_acache != NULL) {
4299 e.error = nfs4_validate_caches(vp, cr);
4300 if (e.error)
4301 return (e.error);
4302 }
4303
4304 rp = VTOR4(vp);
4305 if (vp->v_type == VDIR)
4306 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4307 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4308 else
4309 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4310 ACCESS4_EXECUTE;
4311 recov_state.rs_flags = 0;
4312 recov_state.rs_num_retry_despite_err = 0;
4313
4314 cred = cr;
4315 /*
4316 * ncr and ncrfree both initially
4317 * point to the memory area returned
4318 * by crnetadjust();
4319 * ncrfree not NULL when exiting means
4320 * that we need to release it
4321 */
4322 ncr = crnetadjust(cred);
4323 ncrfree = ncr;
4324
4325 tryagain:
4326 cacc = nfs4_access_check(rp, acc, cred);
4327 if (cacc == NFS4_ACCESS_ALLOWED) {
4328 if (ncrfree != NULL)
4329 crfree(ncrfree);
4330 return (0);
4331 }
4332 if (cacc == NFS4_ACCESS_DENIED) {
4333 /*
4334 * If the cred can be adjusted, try again
4335 * with the new cred.
4336 */
4337 if (ncr != NULL) {
4338 cred = ncr;
4339 ncr = NULL;
4340 goto tryagain;
4341 }
4342 if (ncrfree != NULL)
4343 crfree(ncrfree);
4344 return (EACCES);
4345 }
4346
4347 recov_retry:
4348 /*
4349 * Don't take with r_statev4_lock here. r_deleg_type could
4350 * change as soon as lock is released. Since it is an int,
4351 * there is no atomicity issue.
4352 */
4353 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4354 num_ops = do_getattr ? 3 : 2;
4355
4356 args.ctag = TAG_ACCESS;
4357
4358 args.array_len = num_ops;
4359 args.array = argop;
4360
4361 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4362 &recov_state, NULL)) {
4363 if (ncrfree != NULL)
4364 crfree(ncrfree);
4365 return (e.error);
4366 }
4367
4368 /* putfh target fh */
4369 argop[0].argop = OP_CPUTFH;
4370 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4371
4372 /* access */
4373 argop[1].argop = OP_ACCESS;
4374 argop[1].nfs_argop4_u.opaccess.access = argacc;
4375
4376 /* getattr */
4377 if (do_getattr) {
4378 argop[2].argop = OP_GETATTR;
4379 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4380 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4381 }
4382
4383 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4384 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4385 rnode4info(VTOR4(vp))));
4386
4387 doqueue = 1;
4388 t = gethrtime();
4389 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4390 rpc_error = e.error;
4391
4392 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4393 if (needrecov) {
4394 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4395 "nfs4_access: initiating recovery\n"));
4396
4397 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4398 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4399 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4400 &recov_state, needrecov);
4401 if (!e.error)
4402 (void) xdr_free(xdr_COMPOUND4res_clnt,
4403 (caddr_t)&res);
4404 goto recov_retry;
4405 }
4406 }
4407 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4408
4409 if (e.error)
4410 goto out;
4411
4412 if (res.status) {
4413 e.error = geterrno4(res.status);
4414 /*
4415 * This might generate over the wire calls throught
4416 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4417 * here to avoid a deadlock.
4418 */
4419 nfs4_purge_stale_fh(e.error, vp, cr);
4420 goto out;
4421 }
4422 resop = &res.array[1]; /* access res */
4423
4424 resacc = resop->nfs_resop4_u.opaccess.access;
4425
4426 if (do_getattr) {
4427 resop++; /* getattr res */
4428 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4429 t, cr, FALSE, NULL);
4430 }
4431
4432 if (!e.error) {
4433 nfs4_access_cache(rp, argacc, resacc, cred);
4434 /*
4435 * we just cached results with cred; if cred is the
4436 * adjusted credentials from crnetadjust, we do not want
4437 * to release them before exiting: hence setting ncrfree
4438 * to NULL
4439 */
4440 if (cred != cr)
4441 ncrfree = NULL;
4442 /* XXX check the supported bits too? */
4443 if ((acc & resacc) != acc) {
4444 /*
4445 * The following code implements the semantic
4446 * that a setuid root program has *at least* the
4447 * permissions of the user that is running the
4448 * program. See rfs3call() for more portions
4449 * of the implementation of this functionality.
4450 */
4451 /* XXX-LP */
4452 if (ncr != NULL) {
4453 (void) xdr_free(xdr_COMPOUND4res_clnt,
4454 (caddr_t)&res);
4455 cred = ncr;
4456 ncr = NULL;
4457 goto tryagain;
4458 }
4459 e.error = EACCES;
4460 }
4461 }
4462
4463 out:
4464 if (!rpc_error)
4465 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4466
4467 if (ncrfree != NULL)
4468 crfree(ncrfree);
4469
4470 return (e.error);
4471 }
4472
4473 /* ARGSUSED */
4474 static int
4475 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4476 {
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 };
4491
4492 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4493 return (EIO);
4494 /*
4495 * Can't readlink anything other than a symbolic link.
4496 */
4497 if (vp->v_type != VLNK)
4498 return (EINVAL);
4499
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);
4511 }
4512 mutex_exit(&rp->r_statelock);
4513 }
4514 recov_state.rs_flags = 0;
4515 recov_state.rs_num_retry_despite_err = 0;
4516
4517 recov_retry:
4518 args.array_len = 3;
4519 args.array = argop;
4520 args.ctag = TAG_READLINK;
4521
4522 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4523 if (e.error) {
4524 return (e.error);
4525 }
4526
4527 /* 0. putfh symlink fh */
4528 argop[0].argop = OP_CPUTFH;
4529 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4530
4531 /* 1. readlink */
4532 argop[1].argop = OP_READLINK;
4533
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);
4538
4539 doqueue = 1;
4540
4541 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4542 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4543 rnode4info(VTOR4(vp))));
4544
4545 t = gethrtime();
4546
4547 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4548
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"));
4553
4554 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4555 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4556 if (!e.error)
4557 (void) xdr_free(xdr_COMPOUND4res_clnt,
4558 (caddr_t)&res);
4559
4560 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4561 needrecov);
4562 goto recov_retry;
4563 }
4564 }
4565
4566 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4567
4568 if (e.error)
4569 return (e.error);
4570
4571 /*
4572 * There is an path in the code below which calls
4573 * nfs4_purge_stale_fh(), which may generate otw calls through
4574 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4575 * here to avoid nfs4_start_op() deadlock.
4576 */
4577
4578 if (res.status && (res.array_len < args.array_len)) {
4579 /*
4580 * either Putfh or Link failed
4581 */
4582 e.error = geterrno4(res.status);
4583 nfs4_purge_stale_fh(e.error, vp, cr);
4584 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4585 return (e.error);
4586 }
4587
4588 resop = &res.array[1]; /* readlink res */
4589 lr_res = &resop->nfs_resop4_u.opreadlink;
4590
4591 /*
4592 * treat symlink names as data
4593 */
4594 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4595 if (linkdata != NULL) {
4596 int uio_len = len - 1;
4597 /* len includes null byte, which we won't uiomove */
4598 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4599 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4600 mutex_enter(&rp->r_statelock);
4601 if (rp->r_symlink.contents == NULL) {
4602 rp->r_symlink.contents = linkdata;
4603 rp->r_symlink.len = uio_len;
4604 rp->r_symlink.size = len;
4605 mutex_exit(&rp->r_statelock);
4606 } else {
4607 mutex_exit(&rp->r_statelock);
4608 kmem_free(linkdata, len);
4609 }
4610 } else {
4611 kmem_free(linkdata, len);
4612 }
4613 }
4614 if (res.status == NFS4_OK) {
4615 resop++; /* getattr res */
4616 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4617 }
4618 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4619
4620 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4621
4622 /*
4623 * The over the wire error for attempting to readlink something
4624 * other than a symbolic link is ENXIO. However, we need to
4625 * return EINVAL instead of ENXIO, so we map it here.
4626 */
4627 return (e.error == ENXIO ? EINVAL : e.error);
4628 }
4629
4630 /*
4631 * Flush local dirty pages to stable storage on the server.
4632 *
4633 * If FNODSYNC is specified, then there is nothing to do because
4634 * metadata changes are not cached on the client before being
4635 * sent to the server.
4636 */
4637 /* ARGSUSED */
4638 static int
4639 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4640 {
4641 int error;
4642
4643 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4644 return (0);
4645 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4646 return (EIO);
4647 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4648 if (!error)
4649 error = VTOR4(vp)->r_error;
4650 return (error);
4651 }
4652
4653 /*
4654 * Weirdness: if the file was removed or the target of a rename
4655 * operation while it was open, it got renamed instead. Here we
4656 * remove the renamed file.
4657 */
4658 /* ARGSUSED */
4659 void
4660 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4661 {
4662 rnode4_t *rp;
4663
4664 ASSERT(vp != DNLC_NO_VNODE);
4665
4666 rp = VTOR4(vp);
4667
4668 if (IS_SHADOW(vp, rp)) {
4669 sv_inactive(vp);
4670 return;
4671 }
4672
4673 /*
4674 * If this is coming from the wrong zone, we let someone in the right
4675 * zone take care of it asynchronously. We can get here due to
4676 * VN_RELE() being called from pageout() or fsflush(). This call may
4677 * potentially turn into an expensive no-op if, for instance, v_count
4678 * gets incremented in the meantime, but it's still correct.
4679 */
4680 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4681 nfs4_async_inactive(vp, cr);
4682 return;
4683 }
4684
4685 /*
4686 * Some of the cleanup steps might require over-the-wire
4687 * operations. Since VOP_INACTIVE can get called as a result of
4688 * other over-the-wire operations (e.g., an attribute cache update
4689 * can lead to a DNLC purge), doing those steps now would lead to a
4690 * nested call to the recovery framework, which can deadlock. So
4691 * do any over-the-wire cleanups asynchronously, in a separate
4692 * thread.
4693 */
4694
4695 mutex_enter(&rp->r_os_lock);
4696 mutex_enter(&rp->r_statelock);
4697 mutex_enter(&rp->r_statev4_lock);
4698
4699 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4700 mutex_exit(&rp->r_statev4_lock);
4701 mutex_exit(&rp->r_statelock);
4702 mutex_exit(&rp->r_os_lock);
4703 nfs4_async_inactive(vp, cr);
4704 return;
4705 }
4706
4707 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4708 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4709 mutex_exit(&rp->r_statev4_lock);
4710 mutex_exit(&rp->r_statelock);
4711 mutex_exit(&rp->r_os_lock);
4712 nfs4_async_inactive(vp, cr);
4713 return;
4714 }
4715
4716 if (rp->r_unldvp != NULL) {
4717 mutex_exit(&rp->r_statev4_lock);
4718 mutex_exit(&rp->r_statelock);
4719 mutex_exit(&rp->r_os_lock);
4720 nfs4_async_inactive(vp, cr);
4721 return;
4722 }
4723 mutex_exit(&rp->r_statev4_lock);
4724 mutex_exit(&rp->r_statelock);
4725 mutex_exit(&rp->r_os_lock);
4726
4727 rp4_addfree(rp, cr);
4728 }
4729
4730 /*
4731 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4732 * various bits of state. The caller must not refer to vp after this call.
4733 */
4734
4735 void
4736 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4737 {
4738 rnode4_t *rp = VTOR4(vp);
4739 nfs4_recov_state_t recov_state;
4740 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4741 vnode_t *unldvp;
4742 char *unlname;
4743 cred_t *unlcred;
4744 COMPOUND4args_clnt args;
4745 COMPOUND4res_clnt res, *resp;
4746 nfs_argop4 argop[2];
4747 int doqueue;
4748 #ifdef DEBUG
4749 char *name;
4750 #endif
4751
4752 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4753 ASSERT(!IS_SHADOW(vp, rp));
4754
4755 #ifdef DEBUG
4756 name = fn_name(VTOSV(vp)->sv_name);
4757 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4758 "release vnode %s", name));
4759 kmem_free(name, MAXNAMELEN);
4760 #endif
4761
4762 if (vp->v_type == VREG) {
4763 bool_t recov_failed = FALSE;
4764
4765 e.error = nfs4close_all(vp, cr);
4766 if (e.error) {
4767 /* Check to see if recovery failed */
4768 mutex_enter(&(VTOMI4(vp)->mi_lock));
4769 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4770 recov_failed = TRUE;
4771 mutex_exit(&(VTOMI4(vp)->mi_lock));
4772 if (!recov_failed) {
4773 mutex_enter(&rp->r_statelock);
4774 if (rp->r_flags & R4RECOVERR)
4775 recov_failed = TRUE;
4776 mutex_exit(&rp->r_statelock);
4777 }
4778 if (recov_failed) {
4779 NFS4_DEBUG(nfs4_client_recov_debug,
4780 (CE_NOTE, "nfs4_inactive_otw: "
4781 "close failed (recovery failure)"));
4782 }
4783 }
4784 }
4785
4786 redo:
4787 if (rp->r_unldvp == NULL) {
4788 rp4_addfree(rp, cr);
4789 return;
4790 }
4791
4792 /*
4793 * Save the vnode pointer for the directory where the
4794 * unlinked-open file got renamed, then set it to NULL
4795 * to prevent another thread from getting here before
4796 * we're done with the remove. While we have the
4797 * statelock, make local copies of the pertinent rnode
4798 * fields. If we weren't to do this in an atomic way, the
4799 * the unl* fields could become inconsistent with respect
4800 * to each other due to a race condition between this
4801 * code and nfs_remove(). See bug report 1034328.
4802 */
4803 mutex_enter(&rp->r_statelock);
4804 if (rp->r_unldvp == NULL) {
4805 mutex_exit(&rp->r_statelock);
4806 rp4_addfree(rp, cr);
4807 return;
4808 }
4809
4810 unldvp = rp->r_unldvp;
4811 rp->r_unldvp = NULL;
4812 unlname = rp->r_unlname;
4813 rp->r_unlname = NULL;
4814 unlcred = rp->r_unlcred;
4815 rp->r_unlcred = NULL;
4816 mutex_exit(&rp->r_statelock);
4817
4818 /*
4819 * If there are any dirty pages left, then flush
4820 * them. This is unfortunate because they just
4821 * may get thrown away during the remove operation,
4822 * but we have to do this for correctness.
4823 */
4824 if (nfs4_has_pages(vp) &&
4825 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4826 ASSERT(vp->v_type != VCHR);
4827 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4828 if (e.error) {
4829 mutex_enter(&rp->r_statelock);
4830 if (!rp->r_error)
4831 rp->r_error = e.error;
4832 mutex_exit(&rp->r_statelock);
4833 }
4834 }
4835
4836 recov_state.rs_flags = 0;
4837 recov_state.rs_num_retry_despite_err = 0;
4838 recov_retry_remove:
4839 /*
4840 * Do the remove operation on the renamed file
4841 */
4842 args.ctag = TAG_INACTIVE;
4843
4844 /*
4845 * Remove ops: putfh dir; remove
4846 */
4847 args.array_len = 2;
4848 args.array = argop;
4849
4850 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4851 if (e.error) {
4852 kmem_free(unlname, MAXNAMELEN);
4853 crfree(unlcred);
4854 VN_RELE(unldvp);
4855 /*
4856 * Try again; this time around r_unldvp will be NULL, so we'll
4857 * just call rp4_addfree() and return.
4858 */
4859 goto redo;
4860 }
4861
4862 /* putfh directory */
4863 argop[0].argop = OP_CPUTFH;
4864 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4865
4866 /* remove */
4867 argop[1].argop = OP_CREMOVE;
4868 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4869
4870 doqueue = 1;
4871 resp = &res;
4872
4873 #if 0 /* notyet */
4874 /*
4875 * Can't do this yet. We may be being called from
4876 * dnlc_purge_XXX while that routine is holding a
4877 * mutex lock to the nc_rele list. The calls to
4878 * nfs3_cache_wcc_data may result in calls to
4879 * dnlc_purge_XXX. This will result in a deadlock.
4880 */
4881 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4882 if (e.error) {
4883 PURGE_ATTRCACHE4(unldvp);
4884 resp = NULL;
4885 } else if (res.status) {
4886 e.error = geterrno4(res.status);
4887 PURGE_ATTRCACHE4(unldvp);
4888 /*
4889 * This code is inactive right now
4890 * but if made active there should
4891 * be a nfs4_end_op() call before
4892 * nfs4_purge_stale_fh to avoid start_op()
4893 * deadlock. See BugId: 4948726
4894 */
4895 nfs4_purge_stale_fh(error, unldvp, cr);
4896 } else {
4897 nfs_resop4 *resop;
4898 REMOVE4res *rm_res;
4899
4900 resop = &res.array[1];
4901 rm_res = &resop->nfs_resop4_u.opremove;
4902 /*
4903 * Update directory cache attribute,
4904 * readdir and dnlc caches.
4905 */
4906 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4907 }
4908 #else
4909 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4910
4911 PURGE_ATTRCACHE4(unldvp);
4912 #endif
4913
4914 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4915 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4916 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4917 if (!e.error)
4918 (void) xdr_free(xdr_COMPOUND4res_clnt,
4919 (caddr_t)&res);
4920 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4921 &recov_state, TRUE);
4922 goto recov_retry_remove;
4923 }
4924 }
4925 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4926
4927 /*
4928 * Release stuff held for the remove
4929 */
4930 VN_RELE(unldvp);
4931 if (!e.error && resp)
4932 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4933
4934 kmem_free(unlname, MAXNAMELEN);
4935 crfree(unlcred);
4936 goto redo;
4937 }
4938
4939 /*
4940 * Remote file system operations having to do with directory manipulation.
4941 */
4942 /* ARGSUSED3 */
4943 int
4944 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4945 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4946 int *direntflags, pathname_t *realpnp)
4947 {
4948 int error;
4949 vnode_t *vp, *avp = NULL;
4950 rnode4_t *drp;
4951
4952 *vpp = NULL;
4953 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4954 return (EPERM);
4955 /*
4956 * if LOOKUP_XATTR, must replace dvp (object) with
4957 * object's attrdir before continuing with lookup
4958 */
4959 if (flags & LOOKUP_XATTR) {
4960 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4961 if (error)
4962 return (error);
4963
4964 dvp = avp;
4965
4966 /*
4967 * If lookup is for "", just return dvp now. The attrdir
4968 * has already been activated (from nfs4lookup_xattr), and
4969 * the caller will RELE the original dvp -- not
4970 * the attrdir. So, set vpp and return.
4971 * Currently, when the LOOKUP_XATTR flag is
4972 * passed to VOP_LOOKUP, the name is always empty, and
4973 * shortcircuiting here avoids 3 unneeded lock/unlock
4974 * pairs.
4975 *
4976 * If a non-empty name was provided, then it is the
4977 * attribute name, and it will be looked up below.
4978 */
4979 if (*nm == '\0') {
4980 *vpp = dvp;
4981 return (0);
4982 }
4983
4984 /*
4985 * The vfs layer never sends a name when asking for the
4986 * attrdir, so we should never get here (unless of course
4987 * name is passed at some time in future -- at which time
4988 * we'll blow up here).
4989 */
4990 ASSERT(0);
4991 }
4992
4993 drp = VTOR4(dvp);
4994 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
4995 return (EINTR);
4996
4997 error = nfs4lookup(dvp, nm, vpp, cr, 0);
4998 nfs_rw_exit(&drp->r_rwlock);
4999
5000 /*
5001 * If vnode is a device, create special vnode.
5002 */
5003 if (!error && ISVDEV((*vpp)->v_type)) {
5004 vp = *vpp;
5005 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5006 VN_RELE(vp);
5007 }
5008
5009 return (error);
5010 }
5011
5012 /* ARGSUSED */
5013 static int
5014 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5015 {
5016 int error;
5017 rnode4_t *drp;
5018 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5019 mntinfo4_t *mi;
5020
5021 mi = VTOMI4(dvp);
5022 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5023 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5024 return (EINVAL);
5025
5026 drp = VTOR4(dvp);
5027 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5028 return (EINTR);
5029
5030 mutex_enter(&drp->r_statelock);
5031 /*
5032 * If the server doesn't support xattrs just return EINVAL
5033 */
5034 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5035 mutex_exit(&drp->r_statelock);
5036 nfs_rw_exit(&drp->r_rwlock);
5037 return (EINVAL);
5038 }
5039
5040 /*
5041 * If there is a cached xattr directory entry,
5042 * use it as long as the attributes are valid. If the
5043 * attributes are not valid, take the simple approach and
5044 * free the cached value and re-fetch a new value.
5045 *
5046 * We don't negative entry cache for now, if we did we
5047 * would need to check if the file has changed on every
5048 * lookup. But xattrs don't exist very often and failing
5049 * an openattr is not much more expensive than and NVERIFY or GETATTR
5050 * so do an openattr over the wire for now.
5051 */
5052 if (drp->r_xattr_dir != NULL) {
5053 if (ATTRCACHE4_VALID(dvp)) {
5054 VN_HOLD(drp->r_xattr_dir);
5055 *vpp = drp->r_xattr_dir;
5056 mutex_exit(&drp->r_statelock);
5057 nfs_rw_exit(&drp->r_rwlock);
5058 return (0);
5059 }
5060 VN_RELE(drp->r_xattr_dir);
5061 drp->r_xattr_dir = NULL;
5062 }
5063 mutex_exit(&drp->r_statelock);
5064
5065 error = nfs4openattr(dvp, vpp, cflag, cr);
5066
5067 nfs_rw_exit(&drp->r_rwlock);
5068
5069 return (error);
5070 }
5071
5072 static int
5073 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5074 {
5075 int error;
5076 rnode4_t *drp;
5077
5078 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5079
5080 /*
5081 * If lookup is for "", just return dvp. Don't need
5082 * to send it over the wire, look it up in the dnlc,
5083 * or perform any access checks.
5084 */
5085 if (*nm == '\0') {
5086 VN_HOLD(dvp);
5087 *vpp = dvp;
5088 return (0);
5089 }
5090
5091 /*
5092 * Can't do lookups in non-directories.
5093 */
5094 if (dvp->v_type != VDIR)
5095 return (ENOTDIR);
5096
5097 /*
5098 * If lookup is for ".", just return dvp. Don't need
5099 * to send it over the wire or look it up in the dnlc,
5100 * just need to check access.
5101 */
5102 if (nm[0] == '.' && nm[1] == '\0') {
5103 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5104 if (error)
5105 return (error);
5106 VN_HOLD(dvp);
5107 *vpp = dvp;
5108 return (0);
5109 }
5110
5111 drp = VTOR4(dvp);
5112 if (!(drp->r_flags & R4LOOKUP)) {
5113 mutex_enter(&drp->r_statelock);
5114 drp->r_flags |= R4LOOKUP;
5115 mutex_exit(&drp->r_statelock);
5116 }
5117
5118 *vpp = NULL;
5119 /*
5120 * Lookup this name in the DNLC. If there is no entry
5121 * lookup over the wire.
5122 */
5123 if (!skipdnlc)
5124 *vpp = dnlc_lookup(dvp, nm);
5125 if (*vpp == NULL) {
5126 /*
5127 * We need to go over the wire to lookup the name.
5128 */
5129 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5130 }
5131
5132 /*
5133 * We hit on the dnlc
5134 */
5135 if (*vpp != DNLC_NO_VNODE ||
5136 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5137 /*
5138 * But our attrs may not be valid.
5139 */
5140 if (ATTRCACHE4_VALID(dvp)) {
5141 error = nfs4_waitfor_purge_complete(dvp);
5142 if (error) {
5143 VN_RELE(*vpp);
5144 *vpp = NULL;
5145 return (error);
5146 }
5147
5148 /*
5149 * If after the purge completes, check to make sure
5150 * our attrs are still valid.
5151 */
5152 if (ATTRCACHE4_VALID(dvp)) {
5153 /*
5154 * If we waited for a purge we may have
5155 * lost our vnode so look it up again.
5156 */
5157 VN_RELE(*vpp);
5158 *vpp = dnlc_lookup(dvp, nm);
5159 if (*vpp == NULL)
5160 return (nfs4lookupnew_otw(dvp,
5161 nm, vpp, cr));
5162
5163 /*
5164 * The access cache should almost always hit
5165 */
5166 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5167
5168 if (error) {
5169 VN_RELE(*vpp);
5170 *vpp = NULL;
5171 return (error);
5172 }
5173 if (*vpp == DNLC_NO_VNODE) {
5174 VN_RELE(*vpp);
5175 *vpp = NULL;
5176 return (ENOENT);
5177 }
5178 return (0);
5179 }
5180 }
5181 }
5182
5183 ASSERT(*vpp != NULL);
5184
5185 /*
5186 * We may have gotten here we have one of the following cases:
5187 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5188 * need to validate them.
5189 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5190 * must validate.
5191 *
5192 * Go to the server and check if the directory has changed, if
5193 * it hasn't we are done and can use the dnlc entry.
5194 */
5195 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5196 }
5197
5198 /*
5199 * Go to the server and check if the directory has changed, if
5200 * it hasn't we are done and can use the dnlc entry. If it
5201 * has changed we get a new copy of its attributes and check
5202 * the access for VEXEC, then relookup the filename and
5203 * get its filehandle and attributes.
5204 *
5205 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5206 * if the NVERIFY failed we must
5207 * purge the caches
5208 * cache new attributes (will set r_time_attr_inval)
5209 * cache new access
5210 * recheck VEXEC access
5211 * add name to dnlc, possibly negative
5212 * if LOOKUP succeeded
5213 * cache new attributes
5214 * else
5215 * set a new r_time_attr_inval for dvp
5216 * check to make sure we have access
5217 *
5218 * The vpp returned is the vnode passed in if the directory is valid,
5219 * a new vnode if successful lookup, or NULL on error.
5220 */
5221 static int
5222 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5223 {
5224 COMPOUND4args_clnt args;
5225 COMPOUND4res_clnt res;
5226 fattr4 *ver_fattr;
5227 fattr4_change dchange;
5228 int32_t *ptr;
5229 int argoplist_size = 7 * sizeof (nfs_argop4);
5230 nfs_argop4 *argop;
5231 int doqueue;
5232 mntinfo4_t *mi;
5233 nfs4_recov_state_t recov_state;
5234 hrtime_t t;
5235 int isdotdot;
5236 vnode_t *nvp;
5237 nfs_fh4 *fhp;
5238 nfs4_sharedfh_t *sfhp;
5239 nfs4_access_type_t cacc;
5240 rnode4_t *nrp;
5241 rnode4_t *drp = VTOR4(dvp);
5242 nfs4_ga_res_t *garp = NULL;
5243 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5244
5245 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5246 ASSERT(nm != NULL);
5247 ASSERT(nm[0] != '\0');
5248 ASSERT(dvp->v_type == VDIR);
5249 ASSERT(nm[0] != '.' || nm[1] != '\0');
5250 ASSERT(*vpp != NULL);
5251
5252 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5253 isdotdot = 1;
5254 args.ctag = TAG_LOOKUP_VPARENT;
5255 } else {
5256 /*
5257 * If dvp were a stub, it should have triggered and caused
5258 * a mount for us to get this far.
5259 */
5260 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5261
5262 isdotdot = 0;
5263 args.ctag = TAG_LOOKUP_VALID;
5264 }
5265
5266 mi = VTOMI4(dvp);
5267 recov_state.rs_flags = 0;
5268 recov_state.rs_num_retry_despite_err = 0;
5269
5270 nvp = NULL;
5271
5272 /* Save the original mount point security information */
5273 (void) save_mnt_secinfo(mi->mi_curr_serv);
5274
5275 recov_retry:
5276 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5277 &recov_state, NULL);
5278 if (e.error) {
5279 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5280 VN_RELE(*vpp);
5281 *vpp = NULL;
5282 return (e.error);
5283 }
5284
5285 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5286
5287 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5288 args.array_len = 7;
5289 args.array = argop;
5290
5291 /* 0. putfh file */
5292 argop[0].argop = OP_CPUTFH;
5293 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5294
5295 /* 1. nverify the change info */
5296 argop[1].argop = OP_NVERIFY;
5297 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5298 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5299 ver_fattr->attrlist4 = (char *)&dchange;
5300 ptr = (int32_t *)&dchange;
5301 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5302 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5303
5304 /* 2. getattr directory */
5305 argop[2].argop = OP_GETATTR;
5306 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5307 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5308
5309 /* 3. access directory */
5310 argop[3].argop = OP_ACCESS;
5311 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5312 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5313
5314 /* 4. lookup name */
5315 if (isdotdot) {
5316 argop[4].argop = OP_LOOKUPP;
5317 } else {
5318 argop[4].argop = OP_CLOOKUP;
5319 argop[4].nfs_argop4_u.opclookup.cname = nm;
5320 }
5321
5322 /* 5. resulting file handle */
5323 argop[5].argop = OP_GETFH;
5324
5325 /* 6. resulting file attributes */
5326 argop[6].argop = OP_GETATTR;
5327 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5328 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5329
5330 doqueue = 1;
5331 t = gethrtime();
5332
5333 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5334
5335 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5336 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5337 if (e.error != 0 && *vpp != NULL)
5338 VN_RELE(*vpp);
5339 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5340 &recov_state, FALSE);
5341 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5342 kmem_free(argop, argoplist_size);
5343 return (e.error);
5344 }
5345
5346 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5347 /*
5348 * For WRONGSEC of a non-dotdot case, send secinfo directly
5349 * from this thread, do not go thru the recovery thread since
5350 * we need the nm information.
5351 *
5352 * Not doing dotdot case because there is no specification
5353 * for (PUTFH, SECINFO "..") yet.
5354 */
5355 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5356 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5357 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5358 &recov_state, FALSE);
5359 else
5360 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5361 &recov_state, TRUE);
5362 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5363 kmem_free(argop, argoplist_size);
5364 if (!e.error)
5365 goto recov_retry;
5366 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5367 VN_RELE(*vpp);
5368 *vpp = NULL;
5369 return (e.error);
5370 }
5371
5372 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5373 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5374 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5375 &recov_state, TRUE);
5376
5377 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5378 kmem_free(argop, argoplist_size);
5379 goto recov_retry;
5380 }
5381 }
5382
5383 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5384
5385 if (e.error || res.array_len == 0) {
5386 /*
5387 * If e.error isn't set, then reply has no ops (or we couldn't
5388 * be here). The only legal way to reply without an op array
5389 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5390 * be in the reply for all other status values.
5391 *
5392 * For valid replies without an ops array, return ENOTSUP
5393 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5394 * return EIO -- don't trust status.
5395 */
5396 if (e.error == 0)
5397 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5398 ENOTSUP : EIO;
5399 VN_RELE(*vpp);
5400 *vpp = NULL;
5401 kmem_free(argop, argoplist_size);
5402 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5403 return (e.error);
5404 }
5405
5406 if (res.status != NFS4ERR_SAME) {
5407 e.error = geterrno4(res.status);
5408
5409 /*
5410 * The NVERIFY "failed" so the directory has changed
5411 * First make sure PUTFH succeeded and NVERIFY "failed"
5412 * cleanly.
5413 */
5414 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5415 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5416 nfs4_purge_stale_fh(e.error, dvp, cr);
5417 VN_RELE(*vpp);
5418 *vpp = NULL;
5419 goto exit;
5420 }
5421
5422 /*
5423 * We know the NVERIFY "failed" so we must:
5424 * purge the caches (access and indirectly dnlc if needed)
5425 */
5426 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5427
5428 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5429 nfs4_purge_stale_fh(e.error, dvp, cr);
5430 VN_RELE(*vpp);
5431 *vpp = NULL;
5432 goto exit;
5433 }
5434
5435 /*
5436 * Install new cached attributes for the directory
5437 */
5438 nfs4_attr_cache(dvp,
5439 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5440 t, cr, FALSE, NULL);
5441
5442 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5443 nfs4_purge_stale_fh(e.error, dvp, cr);
5444 VN_RELE(*vpp);
5445 *vpp = NULL;
5446 e.error = geterrno4(res.status);
5447 goto exit;
5448 }
5449
5450 /*
5451 * Now we know the directory is valid,
5452 * cache new directory access
5453 */
5454 nfs4_access_cache(drp,
5455 args.array[3].nfs_argop4_u.opaccess.access,
5456 res.array[3].nfs_resop4_u.opaccess.access, cr);
5457
5458 /*
5459 * recheck VEXEC access
5460 */
5461 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5462 if (cacc != NFS4_ACCESS_ALLOWED) {
5463 /*
5464 * Directory permissions might have been revoked
5465 */
5466 if (cacc == NFS4_ACCESS_DENIED) {
5467 e.error = EACCES;
5468 VN_RELE(*vpp);
5469 *vpp = NULL;
5470 goto exit;
5471 }
5472
5473 /*
5474 * Somehow we must not have asked for enough
5475 * so try a singleton ACCESS, should never happen.
5476 */
5477 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5478 if (e.error) {
5479 VN_RELE(*vpp);
5480 *vpp = NULL;
5481 goto exit;
5482 }
5483 }
5484
5485 e.error = geterrno4(res.status);
5486 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5487 /*
5488 * The lookup failed, probably no entry
5489 */
5490 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5491 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5492 } else {
5493 /*
5494 * Might be some other error, so remove
5495 * the dnlc entry to make sure we start all
5496 * over again, next time.
5497 */
5498 dnlc_remove(dvp, nm);
5499 }
5500 VN_RELE(*vpp);
5501 *vpp = NULL;
5502 goto exit;
5503 }
5504
5505 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5506 /*
5507 * The file exists but we can't get its fh for
5508 * some unknown reason. Remove it from the dnlc
5509 * and error out to be safe.
5510 */
5511 dnlc_remove(dvp, nm);
5512 VN_RELE(*vpp);
5513 *vpp = NULL;
5514 goto exit;
5515 }
5516 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5517 if (fhp->nfs_fh4_len == 0) {
5518 /*
5519 * The file exists but a bogus fh
5520 * some unknown reason. Remove it from the dnlc
5521 * and error out to be safe.
5522 */
5523 e.error = ENOENT;
5524 dnlc_remove(dvp, nm);
5525 VN_RELE(*vpp);
5526 *vpp = NULL;
5527 goto exit;
5528 }
5529 sfhp = sfh4_get(fhp, mi);
5530
5531 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5532 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5533
5534 /*
5535 * Make the new rnode
5536 */
5537 if (isdotdot) {
5538 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5539 if (e.error) {
5540 sfh4_rele(&sfhp);
5541 VN_RELE(*vpp);
5542 *vpp = NULL;
5543 goto exit;
5544 }
5545 /*
5546 * XXX if nfs4_make_dotdot uses an existing rnode
5547 * XXX it doesn't update the attributes.
5548 * XXX for now just save them again to save an OTW
5549 */
5550 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5551 } else {
5552 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5553 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5554 /*
5555 * If v_type == VNON, then garp was NULL because
5556 * the last op in the compound failed and makenfs4node
5557 * could not find the vnode for sfhp. It created
5558 * a new vnode, so we have nothing to purge here.
5559 */
5560 if (nvp->v_type == VNON) {
5561 vattr_t vattr;
5562
5563 vattr.va_mask = AT_TYPE;
5564 /*
5565 * N.B. We've already called nfs4_end_fop above.
5566 */
5567 e.error = nfs4getattr(nvp, &vattr, cr);
5568 if (e.error) {
5569 sfh4_rele(&sfhp);
5570 VN_RELE(*vpp);
5571 *vpp = NULL;
5572 VN_RELE(nvp);
5573 goto exit;
5574 }
5575 nvp->v_type = vattr.va_type;
5576 }
5577 }
5578 sfh4_rele(&sfhp);
5579
5580 nrp = VTOR4(nvp);
5581 mutex_enter(&nrp->r_statev4_lock);
5582 if (!nrp->created_v4) {
5583 mutex_exit(&nrp->r_statev4_lock);
5584 dnlc_update(dvp, nm, nvp);
5585 } else
5586 mutex_exit(&nrp->r_statev4_lock);
5587
5588 VN_RELE(*vpp);
5589 *vpp = nvp;
5590 } else {
5591 hrtime_t now;
5592 hrtime_t delta = 0;
5593
5594 e.error = 0;
5595
5596 /*
5597 * Because the NVERIFY "succeeded" we know that the
5598 * directory attributes are still valid
5599 * so update r_time_attr_inval
5600 */
5601 now = gethrtime();
5602 mutex_enter(&drp->r_statelock);
5603 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5604 delta = now - drp->r_time_attr_saved;
5605 if (delta < mi->mi_acdirmin)
5606 delta = mi->mi_acdirmin;
5607 else if (delta > mi->mi_acdirmax)
5608 delta = mi->mi_acdirmax;
5609 }
5610 drp->r_time_attr_inval = now + delta;
5611 mutex_exit(&drp->r_statelock);
5612 dnlc_update(dvp, nm, *vpp);
5613
5614 /*
5615 * Even though we have a valid directory attr cache
5616 * and dnlc entry, we may not have access.
5617 * This should almost always hit the cache.
5618 */
5619 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5620 if (e.error) {
5621 VN_RELE(*vpp);
5622 *vpp = NULL;
5623 }
5624
5625 if (*vpp == DNLC_NO_VNODE) {
5626 VN_RELE(*vpp);
5627 *vpp = NULL;
5628 e.error = ENOENT;
5629 }
5630 }
5631
5632 exit:
5633 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5634 kmem_free(argop, argoplist_size);
5635 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5636 return (e.error);
5637 }
5638
5639 /*
5640 * We need to go over the wire to lookup the name, but
5641 * while we are there verify the directory has not
5642 * changed but if it has, get new attributes and check access
5643 *
5644 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5645 * NVERIFY GETATTR ACCESS
5646 *
5647 * With the results:
5648 * if the NVERIFY failed we must purge the caches, add new attributes,
5649 * and cache new access.
5650 * set a new r_time_attr_inval
5651 * add name to dnlc, possibly negative
5652 * if LOOKUP succeeded
5653 * cache new attributes
5654 */
5655 static int
5656 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5657 {
5658 COMPOUND4args_clnt args;
5659 COMPOUND4res_clnt res;
5660 fattr4 *ver_fattr;
5661 fattr4_change dchange;
5662 int32_t *ptr;
5663 nfs4_ga_res_t *garp = NULL;
5664 int argoplist_size = 9 * sizeof (nfs_argop4);
5665 nfs_argop4 *argop;
5666 int doqueue;
5667 mntinfo4_t *mi;
5668 nfs4_recov_state_t recov_state;
5669 hrtime_t t;
5670 int isdotdot;
5671 vnode_t *nvp;
5672 nfs_fh4 *fhp;
5673 nfs4_sharedfh_t *sfhp;
5674 nfs4_access_type_t cacc;
5675 rnode4_t *nrp;
5676 rnode4_t *drp = VTOR4(dvp);
5677 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5678
5679 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5680 ASSERT(nm != NULL);
5681 ASSERT(nm[0] != '\0');
5682 ASSERT(dvp->v_type == VDIR);
5683 ASSERT(nm[0] != '.' || nm[1] != '\0');
5684 ASSERT(*vpp == NULL);
5685
5686 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5687 isdotdot = 1;
5688 args.ctag = TAG_LOOKUP_PARENT;
5689 } else {
5690 /*
5691 * If dvp were a stub, it should have triggered and caused
5692 * a mount for us to get this far.
5693 */
5694 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5695
5696 isdotdot = 0;
5697 args.ctag = TAG_LOOKUP;
5698 }
5699
5700 mi = VTOMI4(dvp);
5701 recov_state.rs_flags = 0;
5702 recov_state.rs_num_retry_despite_err = 0;
5703
5704 nvp = NULL;
5705
5706 /* Save the original mount point security information */
5707 (void) save_mnt_secinfo(mi->mi_curr_serv);
5708
5709 recov_retry:
5710 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5711 &recov_state, NULL);
5712 if (e.error) {
5713 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5714 return (e.error);
5715 }
5716
5717 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5718
5719 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5720 args.array_len = 9;
5721 args.array = argop;
5722
5723 /* 0. putfh file */
5724 argop[0].argop = OP_CPUTFH;
5725 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5726
5727 /* 1. savefh for the nverify */
5728 argop[1].argop = OP_SAVEFH;
5729
5730 /* 2. lookup name */
5731 if (isdotdot) {
5732 argop[2].argop = OP_LOOKUPP;
5733 } else {
5734 argop[2].argop = OP_CLOOKUP;
5735 argop[2].nfs_argop4_u.opclookup.cname = nm;
5736 }
5737
5738 /* 3. resulting file handle */
5739 argop[3].argop = OP_GETFH;
5740
5741 /* 4. resulting file attributes */
5742 argop[4].argop = OP_GETATTR;
5743 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5744 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5745
5746 /* 5. restorefh back the directory for the nverify */
5747 argop[5].argop = OP_RESTOREFH;
5748
5749 /* 6. nverify the change info */
5750 argop[6].argop = OP_NVERIFY;
5751 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5752 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5753 ver_fattr->attrlist4 = (char *)&dchange;
5754 ptr = (int32_t *)&dchange;
5755 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5756 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5757
5758 /* 7. getattr directory */
5759 argop[7].argop = OP_GETATTR;
5760 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5761 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5762
5763 /* 8. access directory */
5764 argop[8].argop = OP_ACCESS;
5765 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5766 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5767
5768 doqueue = 1;
5769 t = gethrtime();
5770
5771 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5772
5773 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5774 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5775 if (e.error != 0 && *vpp != NULL)
5776 VN_RELE(*vpp);
5777 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5778 &recov_state, FALSE);
5779 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5780 kmem_free(argop, argoplist_size);
5781 return (e.error);
5782 }
5783
5784 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5785 /*
5786 * For WRONGSEC of a non-dotdot case, send secinfo directly
5787 * from this thread, do not go thru the recovery thread since
5788 * we need the nm information.
5789 *
5790 * Not doing dotdot case because there is no specification
5791 * for (PUTFH, SECINFO "..") yet.
5792 */
5793 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5794 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5795 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5796 &recov_state, FALSE);
5797 else
5798 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5799 &recov_state, TRUE);
5800 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5801 kmem_free(argop, argoplist_size);
5802 if (!e.error)
5803 goto recov_retry;
5804 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5805 return (e.error);
5806 }
5807
5808 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5809 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5810 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5811 &recov_state, TRUE);
5812
5813 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5814 kmem_free(argop, argoplist_size);
5815 goto recov_retry;
5816 }
5817 }
5818
5819 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5820
5821 if (e.error || res.array_len == 0) {
5822 /*
5823 * If e.error isn't set, then reply has no ops (or we couldn't
5824 * be here). The only legal way to reply without an op array
5825 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5826 * be in the reply for all other status values.
5827 *
5828 * For valid replies without an ops array, return ENOTSUP
5829 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5830 * return EIO -- don't trust status.
5831 */
5832 if (e.error == 0)
5833 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5834 ENOTSUP : EIO;
5835
5836 kmem_free(argop, argoplist_size);
5837 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5838 return (e.error);
5839 }
5840
5841 e.error = geterrno4(res.status);
5842
5843 /*
5844 * The PUTFH and SAVEFH may have failed.
5845 */
5846 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5847 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5848 nfs4_purge_stale_fh(e.error, dvp, cr);
5849 goto exit;
5850 }
5851
5852 /*
5853 * Check if the file exists, if it does delay entering
5854 * into the dnlc until after we update the directory
5855 * attributes so we don't cause it to get purged immediately.
5856 */
5857 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5858 /*
5859 * The lookup failed, probably no entry
5860 */
5861 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5862 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5863 goto exit;
5864 }
5865
5866 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5867 /*
5868 * The file exists but we can't get its fh for
5869 * some unknown reason. Error out to be safe.
5870 */
5871 goto exit;
5872 }
5873
5874 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5875 if (fhp->nfs_fh4_len == 0) {
5876 /*
5877 * The file exists but a bogus fh
5878 * some unknown reason. Error out to be safe.
5879 */
5880 e.error = EIO;
5881 goto exit;
5882 }
5883 sfhp = sfh4_get(fhp, mi);
5884
5885 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5886 sfh4_rele(&sfhp);
5887 goto exit;
5888 }
5889 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5890
5891 /*
5892 * The RESTOREFH may have failed
5893 */
5894 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5895 sfh4_rele(&sfhp);
5896 e.error = EIO;
5897 goto exit;
5898 }
5899
5900 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5901 /*
5902 * First make sure the NVERIFY failed as we expected,
5903 * if it didn't then be conservative and error out
5904 * as we can't trust the directory.
5905 */
5906 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5907 sfh4_rele(&sfhp);
5908 e.error = EIO;
5909 goto exit;
5910 }
5911
5912 /*
5913 * We know the NVERIFY "failed" so the directory has changed,
5914 * so we must:
5915 * purge the caches (access and indirectly dnlc if needed)
5916 */
5917 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5918
5919 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5920 sfh4_rele(&sfhp);
5921 goto exit;
5922 }
5923 nfs4_attr_cache(dvp,
5924 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5925 t, cr, FALSE, NULL);
5926
5927 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5928 nfs4_purge_stale_fh(e.error, dvp, cr);
5929 sfh4_rele(&sfhp);
5930 e.error = geterrno4(res.status);
5931 goto exit;
5932 }
5933
5934 /*
5935 * Now we know the directory is valid,
5936 * cache new directory access
5937 */
5938 nfs4_access_cache(drp,
5939 args.array[8].nfs_argop4_u.opaccess.access,
5940 res.array[8].nfs_resop4_u.opaccess.access, cr);
5941
5942 /*
5943 * recheck VEXEC access
5944 */
5945 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5946 if (cacc != NFS4_ACCESS_ALLOWED) {
5947 /*
5948 * Directory permissions might have been revoked
5949 */
5950 if (cacc == NFS4_ACCESS_DENIED) {
5951 sfh4_rele(&sfhp);
5952 e.error = EACCES;
5953 goto exit;
5954 }
5955
5956 /*
5957 * Somehow we must not have asked for enough
5958 * so try a singleton ACCESS should never happen
5959 */
5960 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5961 if (e.error) {
5962 sfh4_rele(&sfhp);
5963 goto exit;
5964 }
5965 }
5966
5967 e.error = geterrno4(res.status);
5968 } else {
5969 hrtime_t now;
5970 hrtime_t delta = 0;
5971
5972 e.error = 0;
5973
5974 /*
5975 * Because the NVERIFY "succeeded" we know that the
5976 * directory attributes are still valid
5977 * so update r_time_attr_inval
5978 */
5979 now = gethrtime();
5980 mutex_enter(&drp->r_statelock);
5981 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5982 delta = now - drp->r_time_attr_saved;
5983 if (delta < mi->mi_acdirmin)
5984 delta = mi->mi_acdirmin;
5985 else if (delta > mi->mi_acdirmax)
5986 delta = mi->mi_acdirmax;
5987 }
5988 drp->r_time_attr_inval = now + delta;
5989 mutex_exit(&drp->r_statelock);
5990
5991 /*
5992 * Even though we have a valid directory attr cache,
5993 * we may not have access.
5994 * This should almost always hit the cache.
5995 */
5996 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5997 if (e.error) {
5998 sfh4_rele(&sfhp);
5999 goto exit;
6000 }
6001 }
6002
6003 /*
6004 * Now we have successfully completed the lookup, if the
6005 * directory has changed we now have the valid attributes.
6006 * We also know we have directory access.
6007 * Create the new rnode and insert it in the dnlc.
6008 */
6009 if (isdotdot) {
6010 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6011 if (e.error) {
6012 sfh4_rele(&sfhp);
6013 goto exit;
6014 }
6015 /*
6016 * XXX if nfs4_make_dotdot uses an existing rnode
6017 * XXX it doesn't update the attributes.
6018 * XXX for now just save them again to save an OTW
6019 */
6020 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6021 } else {
6022 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6023 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6024 }
6025 sfh4_rele(&sfhp);
6026
6027 nrp = VTOR4(nvp);
6028 mutex_enter(&nrp->r_statev4_lock);
6029 if (!nrp->created_v4) {
6030 mutex_exit(&nrp->r_statev4_lock);
6031 dnlc_update(dvp, nm, nvp);
6032 } else
6033 mutex_exit(&nrp->r_statev4_lock);
6034
6035 *vpp = nvp;
6036
6037 exit:
6038 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6039 kmem_free(argop, argoplist_size);
6040 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6041 return (e.error);
6042 }
6043
6044 #ifdef DEBUG
6045 void
6046 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6047 {
6048 uint_t i, len;
6049 zoneid_t zoneid = getzoneid();
6050 char *s;
6051
6052 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6053 for (i = 0; i < argcnt; i++) {
6054 nfs_argop4 *op = &argbase[i];
6055 switch (op->argop) {
6056 case OP_CPUTFH:
6057 case OP_PUTFH:
6058 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6059 break;
6060 case OP_PUTROOTFH:
6061 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6062 break;
6063 case OP_CLOOKUP:
6064 s = op->nfs_argop4_u.opclookup.cname;
6065 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6066 break;
6067 case OP_LOOKUP:
6068 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6069 &len, NULL);
6070 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6071 kmem_free(s, len);
6072 break;
6073 case OP_LOOKUPP:
6074 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6075 break;
6076 case OP_GETFH:
6077 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6078 break;
6079 case OP_GETATTR:
6080 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6081 break;
6082 case OP_OPENATTR:
6083 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6084 break;
6085 default:
6086 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6087 op->argop);
6088 break;
6089 }
6090 }
6091 }
6092 #endif
6093
6094 /*
6095 * nfs4lookup_setup - constructs a multi-lookup compound request.
6096 *
6097 * Given the path "nm1/nm2/.../nmn", the following compound requests
6098 * may be created:
6099 *
6100 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6101 * is faster, for now.
6102 *
6103 * l4_getattrs indicates the type of compound requested.
6104 *
6105 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6106 *
6107 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6108 *
6109 * total number of ops is n + 1.
6110 *
6111 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6112 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6113 * before the last component, and only get attributes
6114 * for the last component. Note that the second-to-last
6115 * pathname component is XATTR_RPATH, which does NOT go
6116 * over-the-wire as a lookup.
6117 *
6118 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6119 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6120 *
6121 * and total number of ops is n + 5.
6122 *
6123 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6124 * attribute directory: create lookups plus an OPENATTR
6125 * replacing the last lookup. Note that the last pathname
6126 * component is XATTR_RPATH, which does NOT go over-the-wire
6127 * as a lookup.
6128 *
6129 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6130 * Openattr; Getfh; Getattr }
6131 *
6132 * and total number of ops is n + 5.
6133 *
6134 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6135 * nodes too.
6136 *
6137 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6138 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6139 *
6140 * and total number of ops is 3*n + 1.
6141 *
6142 * All cases: returns the index in the arg array of the final LOOKUP op, or
6143 * -1 if no LOOKUPs were used.
6144 */
6145 int
6146 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6147 {
6148 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6149 nfs_argop4 *argbase, *argop;
6150 int arglen, argcnt;
6151 int n = 1; /* number of components */
6152 int nga = 1; /* number of Getattr's in request */
6153 char c = '\0', *s, *p;
6154 int lookup_idx = -1;
6155 int argoplist_size;
6156
6157 /* set lookuparg response result to 0 */
6158 lookupargp->resp->status = NFS4_OK;
6159
6160 /* skip leading "/" or "." e.g. ".//./" if there is */
6161 for (; ; nm++) {
6162 if (*nm != '/' && *nm != '.')
6163 break;
6164
6165 /* ".." is counted as 1 component */
6166 if (*nm == '.' && *(nm + 1) != '/')
6167 break;
6168 }
6169
6170 /*
6171 * Find n = number of components - nm must be null terminated
6172 * Skip "." components.
6173 */
6174 if (*nm != '\0')
6175 for (n = 1, s = nm; *s != '\0'; s++) {
6176 if ((*s == '/') && (*(s + 1) != '/') &&
6177 (*(s + 1) != '\0') &&
6178 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6179 *(s + 2) == '\0')))
6180 n++;
6181 }
6182 else
6183 n = 0;
6184
6185 /*
6186 * nga is number of components that need Getfh+Getattr
6187 */
6188 switch (l4_getattrs) {
6189 case LKP4_NO_ATTRIBUTES:
6190 nga = 0;
6191 break;
6192 case LKP4_ALL_ATTRIBUTES:
6193 nga = n;
6194 /*
6195 * Always have at least 1 getfh, getattr pair
6196 */
6197 if (nga == 0)
6198 nga++;
6199 break;
6200 case LKP4_LAST_ATTRDIR:
6201 case LKP4_LAST_NAMED_ATTR:
6202 nga = n+1;
6203 break;
6204 }
6205
6206 /*
6207 * If change to use the filehandle attr instead of getfh
6208 * the following line can be deleted.
6209 */
6210 nga *= 2;
6211
6212 /*
6213 * calculate number of ops in request as
6214 * header + trailer + lookups + getattrs
6215 */
6216 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6217
6218 argoplist_size = arglen * sizeof (nfs_argop4);
6219 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6220 lookupargp->argsp->array = argop;
6221
6222 argcnt = lookupargp->header_len;
6223 argop += argcnt;
6224
6225 /*
6226 * loop and create a lookup op and possibly getattr/getfh for
6227 * each component. Skip "." components.
6228 */
6229 for (s = nm; *s != '\0'; s = p) {
6230 /*
6231 * Set up a pathname struct for each component if needed
6232 */
6233 while (*s == '/')
6234 s++;
6235 if (*s == '\0')
6236 break;
6237
6238 for (p = s; (*p != '/') && (*p != '\0'); p++)
6239 ;
6240 c = *p;
6241 *p = '\0';
6242
6243 if (s[0] == '.' && s[1] == '\0') {
6244 *p = c;
6245 continue;
6246 }
6247 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6248 strcmp(s, XATTR_RPATH) == 0) {
6249 /* getfh XXX may not be needed in future */
6250 argop->argop = OP_GETFH;
6251 argop++;
6252 argcnt++;
6253
6254 /* getattr */
6255 argop->argop = OP_GETATTR;
6256 argop->nfs_argop4_u.opgetattr.attr_request =
6257 lookupargp->ga_bits;
6258 argop->nfs_argop4_u.opgetattr.mi =
6259 lookupargp->mi;
6260 argop++;
6261 argcnt++;
6262
6263 /* openattr */
6264 argop->argop = OP_OPENATTR;
6265 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6266 strcmp(s, XATTR_RPATH) == 0) {
6267 /* openattr */
6268 argop->argop = OP_OPENATTR;
6269 argop++;
6270 argcnt++;
6271
6272 /* getfh XXX may not be needed in future */
6273 argop->argop = OP_GETFH;
6274 argop++;
6275 argcnt++;
6276
6277 /* getattr */
6278 argop->argop = OP_GETATTR;
6279 argop->nfs_argop4_u.opgetattr.attr_request =
6280 lookupargp->ga_bits;
6281 argop->nfs_argop4_u.opgetattr.mi =
6282 lookupargp->mi;
6283 argop++;
6284 argcnt++;
6285 *p = c;
6286 continue;
6287 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6288 /* lookupp */
6289 argop->argop = OP_LOOKUPP;
6290 } else {
6291 /* lookup */
6292 argop->argop = OP_LOOKUP;
6293 (void) str_to_utf8(s,
6294 &argop->nfs_argop4_u.oplookup.objname);
6295 }
6296 lookup_idx = argcnt;
6297 argop++;
6298 argcnt++;
6299
6300 *p = c;
6301
6302 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6303 /* getfh XXX may not be needed in future */
6304 argop->argop = OP_GETFH;
6305 argop++;
6306 argcnt++;
6307
6308 /* getattr */
6309 argop->argop = OP_GETATTR;
6310 argop->nfs_argop4_u.opgetattr.attr_request =
6311 lookupargp->ga_bits;
6312 argop->nfs_argop4_u.opgetattr.mi =
6313 lookupargp->mi;
6314 argop++;
6315 argcnt++;
6316 }
6317 }
6318
6319 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6320 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6321 if (needgetfh) {
6322 /* stick in a post-lookup getfh */
6323 argop->argop = OP_GETFH;
6324 argcnt++;
6325 argop++;
6326 }
6327 /* post-lookup getattr */
6328 argop->argop = OP_GETATTR;
6329 argop->nfs_argop4_u.opgetattr.attr_request =
6330 lookupargp->ga_bits;
6331 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6332 argcnt++;
6333 }
6334 argcnt += lookupargp->trailer_len; /* actual op count */
6335 lookupargp->argsp->array_len = argcnt;
6336 lookupargp->arglen = arglen;
6337
6338 #ifdef DEBUG
6339 if (nfs4_client_lookup_debug)
6340 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6341 #endif
6342
6343 return (lookup_idx);
6344 }
6345
6346 static int
6347 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6348 {
6349 COMPOUND4args_clnt args;
6350 COMPOUND4res_clnt res;
6351 GETFH4res *gf_res = NULL;
6352 nfs_argop4 argop[4];
6353 nfs_resop4 *resop = NULL;
6354 nfs4_sharedfh_t *sfhp;
6355 hrtime_t t;
6356 nfs4_error_t e;
6357
6358 rnode4_t *drp;
6359 int doqueue = 1;
6360 vnode_t *vp;
6361 int needrecov = 0;
6362 nfs4_recov_state_t recov_state;
6363
6364 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6365
6366 *avp = NULL;
6367 recov_state.rs_flags = 0;
6368 recov_state.rs_num_retry_despite_err = 0;
6369
6370 recov_retry:
6371 /* COMPOUND: putfh, openattr, getfh, getattr */
6372 args.array_len = 4;
6373 args.array = argop;
6374 args.ctag = TAG_OPENATTR;
6375
6376 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6377 if (e.error)
6378 return (e.error);
6379
6380 drp = VTOR4(dvp);
6381
6382 /* putfh */
6383 argop[0].argop = OP_CPUTFH;
6384 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6385
6386 /* openattr */
6387 argop[1].argop = OP_OPENATTR;
6388 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6389
6390 /* getfh */
6391 argop[2].argop = OP_GETFH;
6392
6393 /* getattr */
6394 argop[3].argop = OP_GETATTR;
6395 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6396 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6397
6398 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6399 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6400 rnode4info(drp)));
6401
6402 t = gethrtime();
6403
6404 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6405
6406 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6407 if (needrecov) {
6408 bool_t abort;
6409
6410 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6411 "nfs4openattr: initiating recovery\n"));
6412
6413 abort = nfs4_start_recovery(&e,
6414 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6415 OP_OPENATTR, NULL, NULL, NULL);
6416 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6417 if (!e.error) {
6418 e.error = geterrno4(res.status);
6419 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6420 }
6421 if (abort == FALSE)
6422 goto recov_retry;
6423 return (e.error);
6424 }
6425
6426 if (e.error) {
6427 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6428 return (e.error);
6429 }
6430
6431 if (res.status) {
6432 /*
6433 * If OTW errro is NOTSUPP, then it should be
6434 * translated to EINVAL. All Solaris file system
6435 * implementations return EINVAL to the syscall layer
6436 * when the attrdir cannot be created due to an
6437 * implementation restriction or noxattr mount option.
6438 */
6439 if (res.status == NFS4ERR_NOTSUPP) {
6440 mutex_enter(&drp->r_statelock);
6441 if (drp->r_xattr_dir)
6442 VN_RELE(drp->r_xattr_dir);
6443 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6444 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6445 mutex_exit(&drp->r_statelock);
6446
6447 e.error = EINVAL;
6448 } else {
6449 e.error = geterrno4(res.status);
6450 }
6451
6452 if (e.error) {
6453 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6454 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6455 needrecov);
6456 return (e.error);
6457 }
6458 }
6459
6460 resop = &res.array[0]; /* putfh res */
6461 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6462
6463 resop = &res.array[1]; /* openattr res */
6464 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6465
6466 resop = &res.array[2]; /* getfh res */
6467 gf_res = &resop->nfs_resop4_u.opgetfh;
6468 if (gf_res->object.nfs_fh4_len == 0) {
6469 *avp = NULL;
6470 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6471 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6472 return (ENOENT);
6473 }
6474
6475 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6476 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6477 dvp->v_vfsp, t, cr, dvp,
6478 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6479 sfh4_rele(&sfhp);
6480
6481 if (e.error)
6482 PURGE_ATTRCACHE4(vp);
6483
6484 mutex_enter(&vp->v_lock);
6485 vp->v_flag |= V_XATTRDIR;
6486 mutex_exit(&vp->v_lock);
6487
6488 *avp = vp;
6489
6490 mutex_enter(&drp->r_statelock);
6491 if (drp->r_xattr_dir)
6492 VN_RELE(drp->r_xattr_dir);
6493 VN_HOLD(vp);
6494 drp->r_xattr_dir = vp;
6495
6496 /*
6497 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6498 * NULL. xattrs could be created at any time, and we have no
6499 * way to update pc4_xattr_exists in the base object if/when
6500 * it happens.
6501 */
6502 drp->r_pathconf.pc4_xattr_valid = 0;
6503
6504 mutex_exit(&drp->r_statelock);
6505
6506 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6507
6508 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6509
6510 return (0);
6511 }
6512
6513 /* ARGSUSED */
6514 static int
6515 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6516 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6517 vsecattr_t *vsecp)
6518 {
6519 int error;
6520 vnode_t *vp = NULL;
6521 rnode4_t *rp;
6522 struct vattr vattr;
6523 rnode4_t *drp;
6524 vnode_t *tempvp;
6525 enum createmode4 createmode;
6526 bool_t must_trunc = FALSE;
6527 int truncating = 0;
6528
6529 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6530 return (EPERM);
6531 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6532 return (EINVAL);
6533 }
6534
6535 /* . and .. have special meaning in the protocol, reject them. */
6536
6537 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6538 return (EISDIR);
6539
6540 drp = VTOR4(dvp);
6541
6542 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6543 return (EINTR);
6544
6545 top:
6546 /*
6547 * We make a copy of the attributes because the caller does not
6548 * expect us to change what va points to.
6549 */
6550 vattr = *va;
6551
6552 /*
6553 * If the pathname is "", then dvp is the root vnode of
6554 * a remote file mounted over a local directory.
6555 * All that needs to be done is access
6556 * checking and truncation. Note that we avoid doing
6557 * open w/ create because the parent directory might
6558 * be in pseudo-fs and the open would fail.
6559 */
6560 if (*nm == '\0') {
6561 error = 0;
6562 VN_HOLD(dvp);
6563 vp = dvp;
6564 must_trunc = TRUE;
6565 } else {
6566 /*
6567 * We need to go over the wire, just to be sure whether the
6568 * file exists or not. Using the DNLC can be dangerous in
6569 * this case when making a decision regarding existence.
6570 */
6571 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6572 }
6573
6574 if (exclusive)
6575 createmode = EXCLUSIVE4;
6576 else
6577 createmode = GUARDED4;
6578
6579 /*
6580 * error would be set if the file does not exist on the
6581 * server, so lets go create it.
6582 */
6583 if (error) {
6584 goto create_otw;
6585 }
6586
6587 /*
6588 * File does exist on the server
6589 */
6590 if (exclusive == EXCL)
6591 error = EEXIST;
6592 else if (vp->v_type == VDIR && (mode & VWRITE))
6593 error = EISDIR;
6594 else {
6595 /*
6596 * If vnode is a device, create special vnode.
6597 */
6598 if (ISVDEV(vp->v_type)) {
6599 tempvp = vp;
6600 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6601 VN_RELE(tempvp);
6602 }
6603 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6604 if ((vattr.va_mask & AT_SIZE) &&
6605 vp->v_type == VREG) {
6606 rp = VTOR4(vp);
6607 /*
6608 * Check here for large file handled
6609 * by LF-unaware process (as
6610 * ufs_create() does)
6611 */
6612 if (!(flags & FOFFMAX)) {
6613 mutex_enter(&rp->r_statelock);
6614 if (rp->r_size > MAXOFF32_T)
6615 error = EOVERFLOW;
6616 mutex_exit(&rp->r_statelock);
6617 }
6618
6619 /* if error is set then we need to return */
6620 if (error) {
6621 nfs_rw_exit(&drp->r_rwlock);
6622 VN_RELE(vp);
6623 return (error);
6624 }
6625
6626 if (must_trunc) {
6627 vattr.va_mask = AT_SIZE;
6628 error = nfs4setattr(vp, &vattr, 0, cr,
6629 NULL);
6630 } else {
6631 /*
6632 * we know we have a regular file that already
6633 * exists and we may end up truncating the file
6634 * as a result of the open_otw, so flush out
6635 * any dirty pages for this file first.
6636 */
6637 if (nfs4_has_pages(vp) &&
6638 ((rp->r_flags & R4DIRTY) ||
6639 rp->r_count > 0 ||
6640 rp->r_mapcnt > 0)) {
6641 error = nfs4_putpage(vp,
6642 (offset_t)0, 0, 0, cr, ct);
6643 if (error && (error == ENOSPC ||
6644 error == EDQUOT)) {
6645 mutex_enter(
6646 &rp->r_statelock);
6647 if (!rp->r_error)
6648 rp->r_error =
6649 error;
6650 mutex_exit(
6651 &rp->r_statelock);
6652 }
6653 }
6654 vattr.va_mask = (AT_SIZE |
6655 AT_TYPE | AT_MODE);
6656 vattr.va_type = VREG;
6657 createmode = UNCHECKED4;
6658 truncating = 1;
6659 goto create_otw;
6660 }
6661 }
6662 }
6663 }
6664 nfs_rw_exit(&drp->r_rwlock);
6665 if (error) {
6666 VN_RELE(vp);
6667 } else {
6668 vnode_t *tvp;
6669 rnode4_t *trp;
6670 tvp = vp;
6671 if (vp->v_type == VREG) {
6672 trp = VTOR4(vp);
6673 if (IS_SHADOW(vp, trp))
6674 tvp = RTOV4(trp);
6675 }
6676
6677 if (must_trunc) {
6678 /*
6679 * existing file got truncated, notify.
6680 */
6681 vnevent_create(tvp, ct);
6682 }
6683
6684 *vpp = vp;
6685 }
6686 return (error);
6687
6688 create_otw:
6689 dnlc_remove(dvp, nm);
6690
6691 ASSERT(vattr.va_mask & AT_TYPE);
6692
6693 /*
6694 * If not a regular file let nfs4mknod() handle it.
6695 */
6696 if (vattr.va_type != VREG) {
6697 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6698 nfs_rw_exit(&drp->r_rwlock);
6699 return (error);
6700 }
6701
6702 /*
6703 * It _is_ a regular file.
6704 */
6705 ASSERT(vattr.va_mask & AT_MODE);
6706 if (MANDMODE(vattr.va_mode)) {
6707 nfs_rw_exit(&drp->r_rwlock);
6708 return (EACCES);
6709 }
6710
6711 /*
6712 * If this happens to be a mknod of a regular file, then flags will
6713 * have neither FREAD or FWRITE. However, we must set at least one
6714 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6715 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6716 * set (based on openmode specified by app).
6717 */
6718 if ((flags & (FREAD|FWRITE)) == 0)
6719 flags |= (FREAD|FWRITE);
6720
6721 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6722
6723 if (vp != NULL) {
6724 /* if create was successful, throw away the file's pages */
6725 if (!error && (vattr.va_mask & AT_SIZE))
6726 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6727 cr);
6728 /* release the lookup hold */
6729 VN_RELE(vp);
6730 vp = NULL;
6731 }
6732
6733 /*
6734 * validate that we opened a regular file. This handles a misbehaving
6735 * server that returns an incorrect FH.
6736 */
6737 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6738 error = EISDIR;
6739 VN_RELE(*vpp);
6740 }
6741
6742 /*
6743 * If this is not an exclusive create, then the CREATE
6744 * request will be made with the GUARDED mode set. This
6745 * means that the server will return EEXIST if the file
6746 * exists. The file could exist because of a retransmitted
6747 * request. In this case, we recover by starting over and
6748 * checking to see whether the file exists. This second
6749 * time through it should and a CREATE request will not be
6750 * sent.
6751 *
6752 * This handles the problem of a dangling CREATE request
6753 * which contains attributes which indicate that the file
6754 * should be truncated. This retransmitted request could
6755 * possibly truncate valid data in the file if not caught
6756 * by the duplicate request mechanism on the server or if
6757 * not caught by other means. The scenario is:
6758 *
6759 * Client transmits CREATE request with size = 0
6760 * Client times out, retransmits request.
6761 * Response to the first request arrives from the server
6762 * and the client proceeds on.
6763 * Client writes data to the file.
6764 * The server now processes retransmitted CREATE request
6765 * and truncates file.
6766 *
6767 * The use of the GUARDED CREATE request prevents this from
6768 * happening because the retransmitted CREATE would fail
6769 * with EEXIST and would not truncate the file.
6770 */
6771 if (error == EEXIST && exclusive == NONEXCL) {
6772 #ifdef DEBUG
6773 nfs4_create_misses++;
6774 #endif
6775 goto top;
6776 }
6777 nfs_rw_exit(&drp->r_rwlock);
6778 if (truncating && !error && *vpp) {
6779 vnode_t *tvp;
6780 rnode4_t *trp;
6781 /*
6782 * existing file got truncated, notify.
6783 */
6784 tvp = *vpp;
6785 trp = VTOR4(tvp);
6786 if (IS_SHADOW(tvp, trp))
6787 tvp = RTOV4(trp);
6788 vnevent_create(tvp, ct);
6789 }
6790 return (error);
6791 }
6792
6793 /*
6794 * Create compound (for mkdir, mknod, symlink):
6795 * { Putfh <dfh>; Create; Getfh; Getattr }
6796 * It's okay if setattr failed to set gid - this is not considered
6797 * an error, but purge attrs in that case.
6798 */
6799 static int
6800 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6801 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6802 {
6803 int need_end_op = FALSE;
6804 COMPOUND4args_clnt args;
6805 COMPOUND4res_clnt res, *resp = NULL;
6806 nfs_argop4 *argop;
6807 nfs_resop4 *resop;
6808 int doqueue;
6809 mntinfo4_t *mi;
6810 rnode4_t *drp = VTOR4(dvp);
6811 change_info4 *cinfo;
6812 GETFH4res *gf_res;
6813 struct vattr vattr;
6814 vnode_t *vp;
6815 fattr4 *crattr;
6816 bool_t needrecov = FALSE;
6817 nfs4_recov_state_t recov_state;
6818 nfs4_sharedfh_t *sfhp = NULL;
6819 hrtime_t t;
6820 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6821 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6822 dirattr_info_t dinfo, *dinfop;
6823 servinfo4_t *svp;
6824 bitmap4 supp_attrs;
6825
6826 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6827 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6828
6829 mi = VTOMI4(dvp);
6830
6831 /*
6832 * Make sure we properly deal with setting the right gid
6833 * on a new directory to reflect the parent's setgid bit
6834 */
6835 setgid_flag = 0;
6836 if (type == NF4DIR) {
6837 struct vattr dva;
6838
6839 va->va_mode &= ~VSGID;
6840 dva.va_mask = AT_MODE | AT_GID;
6841 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6842
6843 /*
6844 * If the parent's directory has the setgid bit set
6845 * _and_ the client was able to get a valid mapping
6846 * for the parent dir's owner_group, we want to
6847 * append NVERIFY(owner_group == dva.va_gid) and
6848 * SETTATTR to the CREATE compound.
6849 */
6850 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6851 setgid_flag = 1;
6852 va->va_mode |= VSGID;
6853 if (dva.va_gid != GID_NOBODY) {
6854 va->va_mask |= AT_GID;
6855 va->va_gid = dva.va_gid;
6856 }
6857 }
6858 }
6859 }
6860
6861 /*
6862 * Create ops:
6863 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6864 * 5:restorefh(dir) 6:getattr(dir)
6865 *
6866 * if (setgid)
6867 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6868 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6869 * 8:nverify 9:setattr
6870 */
6871 if (setgid_flag) {
6872 numops = 10;
6873 idx_create = 1;
6874 idx_fattr = 3;
6875 } else {
6876 numops = 7;
6877 idx_create = 2;
6878 idx_fattr = 4;
6879 }
6880
6881 ASSERT(nfs_zone() == mi->mi_zone);
6882 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6883 return (EINTR);
6884 }
6885 recov_state.rs_flags = 0;
6886 recov_state.rs_num_retry_despite_err = 0;
6887
6888 argoplist_size = numops * sizeof (nfs_argop4);
6889 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6890
6891 recov_retry:
6892 if (type == NF4LNK)
6893 args.ctag = TAG_SYMLINK;
6894 else if (type == NF4DIR)
6895 args.ctag = TAG_MKDIR;
6896 else
6897 args.ctag = TAG_MKNOD;
6898
6899 args.array_len = numops;
6900 args.array = argop;
6901
6902 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6903 nfs_rw_exit(&drp->r_rwlock);
6904 kmem_free(argop, argoplist_size);
6905 return (e.error);
6906 }
6907 need_end_op = TRUE;
6908
6909
6910 /* 0: putfh directory */
6911 argop[0].argop = OP_CPUTFH;
6912 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6913
6914 /* 1/2: Create object */
6915 argop[idx_create].argop = OP_CCREATE;
6916 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6917 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6918 if (type == NF4LNK) {
6919 /*
6920 * symlink, treat name as data
6921 */
6922 ASSERT(data != NULL);
6923 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6924 (char *)data;
6925 }
6926 if (type == NF4BLK || type == NF4CHR) {
6927 ASSERT(data != NULL);
6928 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6929 *((specdata4 *)data);
6930 }
6931
6932 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6933
6934 svp = drp->r_server;
6935 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6936 supp_attrs = svp->sv_supp_attrs;
6937 nfs_rw_exit(&svp->sv_lock);
6938
6939 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6940 nfs_rw_exit(&drp->r_rwlock);
6941 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6942 e.error = EINVAL;
6943 kmem_free(argop, argoplist_size);
6944 return (e.error);
6945 }
6946
6947 /* 2/3: getfh fh of created object */
6948 ASSERT(idx_create + 1 == idx_fattr - 1);
6949 argop[idx_create + 1].argop = OP_GETFH;
6950
6951 /* 3/4: getattr of new object */
6952 argop[idx_fattr].argop = OP_GETATTR;
6953 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6954 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6955
6956 if (setgid_flag) {
6957 vattr_t _v;
6958
6959 argop[4].argop = OP_SAVEFH;
6960
6961 argop[5].argop = OP_CPUTFH;
6962 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6963
6964 argop[6].argop = OP_GETATTR;
6965 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6966 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6967
6968 argop[7].argop = OP_RESTOREFH;
6969
6970 /*
6971 * nverify
6972 *
6973 * XXX - Revisit the last argument to nfs4_end_op()
6974 * once 5020486 is fixed.
6975 */
6976 _v.va_mask = AT_GID;
6977 _v.va_gid = va->va_gid;
6978 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6979 supp_attrs)) {
6980 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6981 nfs_rw_exit(&drp->r_rwlock);
6982 nfs4_fattr4_free(crattr);
6983 kmem_free(argop, argoplist_size);
6984 return (e.error);
6985 }
6986
6987 /*
6988 * setattr
6989 *
6990 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
6991 * so no need for stateid or flags. Also we specify NULL
6992 * rp since we're only interested in setting owner_group
6993 * attributes.
6994 */
6995 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
6996 &e.error, 0);
6997
6998 if (e.error) {
6999 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7000 nfs_rw_exit(&drp->r_rwlock);
7001 nfs4_fattr4_free(crattr);
7002 nfs4args_verify_free(&argop[8]);
7003 kmem_free(argop, argoplist_size);
7004 return (e.error);
7005 }
7006 } else {
7007 argop[1].argop = OP_SAVEFH;
7008
7009 argop[5].argop = OP_RESTOREFH;
7010
7011 argop[6].argop = OP_GETATTR;
7012 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7013 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7014 }
7015
7016 dnlc_remove(dvp, nm);
7017
7018 doqueue = 1;
7019 t = gethrtime();
7020 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7021
7022 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7023 if (e.error) {
7024 PURGE_ATTRCACHE4(dvp);
7025 if (!needrecov)
7026 goto out;
7027 }
7028
7029 if (needrecov) {
7030 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7031 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7032 nfs4_end_op(mi, dvp, NULL, &recov_state,
7033 needrecov);
7034 need_end_op = FALSE;
7035 nfs4_fattr4_free(crattr);
7036 if (setgid_flag) {
7037 nfs4args_verify_free(&argop[8]);
7038 nfs4args_setattr_free(&argop[9]);
7039 }
7040 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7041 goto recov_retry;
7042 }
7043 }
7044
7045 resp = &res;
7046
7047 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7048
7049 if (res.status == NFS4ERR_BADOWNER)
7050 nfs4_log_badowner(mi, OP_CREATE);
7051
7052 e.error = geterrno4(res.status);
7053
7054 /*
7055 * This check is left over from when create was implemented
7056 * using a setattr op (instead of createattrs). If the
7057 * putfh/create/getfh failed, the error was returned. If
7058 * setattr/getattr failed, we keep going.
7059 *
7060 * It might be better to get rid of the GETFH also, and just
7061 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7062 * Then if any of the operations failed, we could return the
7063 * error now, and remove much of the error code below.
7064 */
7065 if (res.array_len <= idx_fattr) {
7066 /*
7067 * Either Putfh, Create or Getfh failed.
7068 */
7069 PURGE_ATTRCACHE4(dvp);
7070 /*
7071 * nfs4_purge_stale_fh() may generate otw calls through
7072 * nfs4_invalidate_pages. Hence the need to call
7073 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7074 */
7075 nfs4_end_op(mi, dvp, NULL, &recov_state,
7076 needrecov);
7077 need_end_op = FALSE;
7078 nfs4_purge_stale_fh(e.error, dvp, cr);
7079 goto out;
7080 }
7081 }
7082
7083 resop = &res.array[idx_create]; /* create res */
7084 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7085
7086 resop = &res.array[idx_create + 1]; /* getfh res */
7087 gf_res = &resop->nfs_resop4_u.opgetfh;
7088
7089 sfhp = sfh4_get(&gf_res->object, mi);
7090 if (e.error) {
7091 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7092 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7093 if (vp->v_type == VNON) {
7094 vattr.va_mask = AT_TYPE;
7095 /*
7096 * Need to call nfs4_end_op before nfs4getattr to avoid
7097 * potential nfs4_start_op deadlock. See RFE 4777612.
7098 */
7099 nfs4_end_op(mi, dvp, NULL, &recov_state,
7100 needrecov);
7101 need_end_op = FALSE;
7102 e.error = nfs4getattr(vp, &vattr, cr);
7103 if (e.error) {
7104 VN_RELE(vp);
7105 *vpp = NULL;
7106 goto out;
7107 }
7108 vp->v_type = vattr.va_type;
7109 }
7110 e.error = 0;
7111 } else {
7112 *vpp = vp = makenfs4node(sfhp,
7113 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7114 dvp->v_vfsp, t, cr,
7115 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7116 }
7117
7118 /*
7119 * If compound succeeded, then update dir attrs
7120 */
7121 if (res.status == NFS4_OK) {
7122 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7123 dinfo.di_cred = cr;
7124 dinfo.di_time_call = t;
7125 dinfop = &dinfo;
7126 } else
7127 dinfop = NULL;
7128
7129 /* Update directory cache attribute, readdir and dnlc caches */
7130 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7131
7132 out:
7133 if (sfhp != NULL)
7134 sfh4_rele(&sfhp);
7135 nfs_rw_exit(&drp->r_rwlock);
7136 nfs4_fattr4_free(crattr);
7137 if (setgid_flag) {
7138 nfs4args_verify_free(&argop[8]);
7139 nfs4args_setattr_free(&argop[9]);
7140 }
7141 if (resp)
7142 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7143 if (need_end_op)
7144 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7145
7146 kmem_free(argop, argoplist_size);
7147 return (e.error);
7148 }
7149
7150 /* ARGSUSED */
7151 static int
7152 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7153 int mode, vnode_t **vpp, cred_t *cr)
7154 {
7155 int error;
7156 vnode_t *vp;
7157 nfs_ftype4 type;
7158 specdata4 spec, *specp = NULL;
7159
7160 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7161
7162 switch (va->va_type) {
7163 case VCHR:
7164 case VBLK:
7165 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7166 spec.specdata1 = getmajor(va->va_rdev);
7167 spec.specdata2 = getminor(va->va_rdev);
7168 specp = &spec;
7169 break;
7170
7171 case VFIFO:
7172 type = NF4FIFO;
7173 break;
7174 case VSOCK:
7175 type = NF4SOCK;
7176 break;
7177
7178 default:
7179 return (EINVAL);
7180 }
7181
7182 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7183 if (error) {
7184 return (error);
7185 }
7186
7187 /*
7188 * This might not be needed any more; special case to deal
7189 * with problematic v2/v3 servers. Since create was unable
7190 * to set group correctly, not sure what hope setattr has.
7191 */
7192 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7193 va->va_mask = AT_GID;
7194 (void) nfs4setattr(vp, va, 0, cr, NULL);
7195 }
7196
7197 /*
7198 * If vnode is a device create special vnode
7199 */
7200 if (ISVDEV(vp->v_type)) {
7201 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7202 VN_RELE(vp);
7203 } else {
7204 *vpp = vp;
7205 }
7206 return (error);
7207 }
7208
7209 /*
7210 * Remove requires that the current fh be the target directory.
7211 * After the operation, the current fh is unchanged.
7212 * The compound op structure is:
7213 * PUTFH(targetdir), REMOVE
7214 *
7215 * Weirdness: if the vnode to be removed is open
7216 * we rename it instead of removing it and nfs_inactive
7217 * will remove the new name.
7218 */
7219 /* ARGSUSED */
7220 static int
7221 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7222 {
7223 COMPOUND4args_clnt args;
7224 COMPOUND4res_clnt res, *resp = NULL;
7225 REMOVE4res *rm_res;
7226 nfs_argop4 argop[3];
7227 nfs_resop4 *resop;
7228 vnode_t *vp;
7229 char *tmpname;
7230 int doqueue;
7231 mntinfo4_t *mi;
7232 rnode4_t *rp;
7233 rnode4_t *drp;
7234 int needrecov = 0;
7235 nfs4_recov_state_t recov_state;
7236 int isopen;
7237 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7238 dirattr_info_t dinfo;
7239
7240 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7241 return (EPERM);
7242 drp = VTOR4(dvp);
7243 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7244 return (EINTR);
7245
7246 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7247 if (e.error) {
7248 nfs_rw_exit(&drp->r_rwlock);
7249 return (e.error);
7250 }
7251
7252 if (vp->v_type == VDIR) {
7253 VN_RELE(vp);
7254 nfs_rw_exit(&drp->r_rwlock);
7255 return (EISDIR);
7256 }
7257
7258 /*
7259 * First just remove the entry from the name cache, as it
7260 * is most likely the only entry for this vp.
7261 */
7262 dnlc_remove(dvp, nm);
7263
7264 rp = VTOR4(vp);
7265
7266 /*
7267 * For regular file types, check to see if the file is open by looking
7268 * at the open streams.
7269 * For all other types, check the reference count on the vnode. Since
7270 * they are not opened OTW they never have an open stream.
7271 *
7272 * If the file is open, rename it to .nfsXXXX.
7273 */
7274 if (vp->v_type != VREG) {
7275 /*
7276 * If the file has a v_count > 1 then there may be more than one
7277 * entry in the name cache due multiple links or an open file,
7278 * but we don't have the real reference count so flush all
7279 * possible entries.
7280 */
7281 if (vp->v_count > 1)
7282 dnlc_purge_vp(vp);
7283
7284 /*
7285 * Now we have the real reference count.
7286 */
7287 isopen = vp->v_count > 1;
7288 } else {
7289 mutex_enter(&rp->r_os_lock);
7290 isopen = list_head(&rp->r_open_streams) != NULL;
7291 mutex_exit(&rp->r_os_lock);
7292 }
7293
7294 mutex_enter(&rp->r_statelock);
7295 if (isopen &&
7296 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7297 mutex_exit(&rp->r_statelock);
7298 tmpname = newname();
7299 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7300 if (e.error)
7301 kmem_free(tmpname, MAXNAMELEN);
7302 else {
7303 mutex_enter(&rp->r_statelock);
7304 if (rp->r_unldvp == NULL) {
7305 VN_HOLD(dvp);
7306 rp->r_unldvp = dvp;
7307 if (rp->r_unlcred != NULL)
7308 crfree(rp->r_unlcred);
7309 crhold(cr);
7310 rp->r_unlcred = cr;
7311 rp->r_unlname = tmpname;
7312 } else {
7313 kmem_free(rp->r_unlname, MAXNAMELEN);
7314 rp->r_unlname = tmpname;
7315 }
7316 mutex_exit(&rp->r_statelock);
7317 }
7318 VN_RELE(vp);
7319 nfs_rw_exit(&drp->r_rwlock);
7320 return (e.error);
7321 }
7322 /*
7323 * Actually remove the file/dir
7324 */
7325 mutex_exit(&rp->r_statelock);
7326
7327 /*
7328 * We need to flush any dirty pages which happen to
7329 * be hanging around before removing the file.
7330 * This shouldn't happen very often since in NFSv4
7331 * we should be close to open consistent.
7332 */
7333 if (nfs4_has_pages(vp) &&
7334 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7335 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7336 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7337 mutex_enter(&rp->r_statelock);
7338 if (!rp->r_error)
7339 rp->r_error = e.error;
7340 mutex_exit(&rp->r_statelock);
7341 }
7342 }
7343
7344 mi = VTOMI4(dvp);
7345
7346 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7347 recov_state.rs_flags = 0;
7348 recov_state.rs_num_retry_despite_err = 0;
7349
7350 recov_retry:
7351 /*
7352 * Remove ops: putfh dir; remove
7353 */
7354 args.ctag = TAG_REMOVE;
7355 args.array_len = 3;
7356 args.array = argop;
7357
7358 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7359 if (e.error) {
7360 nfs_rw_exit(&drp->r_rwlock);
7361 VN_RELE(vp);
7362 return (e.error);
7363 }
7364
7365 /* putfh directory */
7366 argop[0].argop = OP_CPUTFH;
7367 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7368
7369 /* remove */
7370 argop[1].argop = OP_CREMOVE;
7371 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7372
7373 /* getattr dir */
7374 argop[2].argop = OP_GETATTR;
7375 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7376 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7377
7378 doqueue = 1;
7379 dinfo.di_time_call = gethrtime();
7380 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7381
7382 PURGE_ATTRCACHE4(vp);
7383
7384 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7385 if (e.error)
7386 PURGE_ATTRCACHE4(dvp);
7387
7388 if (needrecov) {
7389 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7390 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7391 if (!e.error)
7392 (void) xdr_free(xdr_COMPOUND4res_clnt,
7393 (caddr_t)&res);
7394 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7395 needrecov);
7396 goto recov_retry;
7397 }
7398 }
7399
7400 /*
7401 * Matching nfs4_end_op() for start_op() above.
7402 * There is a path in the code below which calls
7403 * nfs4_purge_stale_fh(), which may generate otw calls through
7404 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7405 * here to avoid nfs4_start_op() deadlock.
7406 */
7407 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7408
7409 if (!e.error) {
7410 resp = &res;
7411
7412 if (res.status) {
7413 e.error = geterrno4(res.status);
7414 PURGE_ATTRCACHE4(dvp);
7415 nfs4_purge_stale_fh(e.error, dvp, cr);
7416 } else {
7417 resop = &res.array[1]; /* remove res */
7418 rm_res = &resop->nfs_resop4_u.opremove;
7419
7420 dinfo.di_garp =
7421 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7422 dinfo.di_cred = cr;
7423
7424 /* Update directory attr, readdir and dnlc caches */
7425 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7426 &dinfo);
7427 }
7428 }
7429 nfs_rw_exit(&drp->r_rwlock);
7430 if (resp)
7431 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7432
7433 if (e.error == 0) {
7434 vnode_t *tvp;
7435 rnode4_t *trp;
7436 trp = VTOR4(vp);
7437 tvp = vp;
7438 if (IS_SHADOW(vp, trp))
7439 tvp = RTOV4(trp);
7440 vnevent_remove(tvp, dvp, nm, ct);
7441 }
7442 VN_RELE(vp);
7443 return (e.error);
7444 }
7445
7446 /*
7447 * Link requires that the current fh be the target directory and the
7448 * saved fh be the source fh. After the operation, the current fh is unchanged.
7449 * Thus the compound op structure is:
7450 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7451 * GETATTR(file)
7452 */
7453 /* ARGSUSED */
7454 static int
7455 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7456 caller_context_t *ct, int flags)
7457 {
7458 COMPOUND4args_clnt args;
7459 COMPOUND4res_clnt res, *resp = NULL;
7460 LINK4res *ln_res;
7461 int argoplist_size = 7 * sizeof (nfs_argop4);
7462 nfs_argop4 *argop;
7463 nfs_resop4 *resop;
7464 vnode_t *realvp, *nvp;
7465 int doqueue;
7466 mntinfo4_t *mi;
7467 rnode4_t *tdrp;
7468 bool_t needrecov = FALSE;
7469 nfs4_recov_state_t recov_state;
7470 hrtime_t t;
7471 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7472 dirattr_info_t dinfo;
7473
7474 ASSERT(*tnm != '\0');
7475 ASSERT(tdvp->v_type == VDIR);
7476 ASSERT(nfs4_consistent_type(tdvp));
7477 ASSERT(nfs4_consistent_type(svp));
7478
7479 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7480 return (EPERM);
7481 if (VOP_REALVP(svp, &realvp, ct) == 0) {
7482 svp = realvp;
7483 ASSERT(nfs4_consistent_type(svp));
7484 }
7485
7486 tdrp = VTOR4(tdvp);
7487 mi = VTOMI4(svp);
7488
7489 if (!(mi->mi_flags & MI4_LINK)) {
7490 return (EOPNOTSUPP);
7491 }
7492 recov_state.rs_flags = 0;
7493 recov_state.rs_num_retry_despite_err = 0;
7494
7495 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7496 return (EINTR);
7497
7498 recov_retry:
7499 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7500
7501 args.ctag = TAG_LINK;
7502
7503 /*
7504 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7505 * restorefh; getattr(fl)
7506 */
7507 args.array_len = 7;
7508 args.array = argop;
7509
7510 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7511 if (e.error) {
7512 kmem_free(argop, argoplist_size);
7513 nfs_rw_exit(&tdrp->r_rwlock);
7514 return (e.error);
7515 }
7516
7517 /* 0. putfh file */
7518 argop[0].argop = OP_CPUTFH;
7519 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7520
7521 /* 1. save current fh to free up the space for the dir */
7522 argop[1].argop = OP_SAVEFH;
7523
7524 /* 2. putfh targetdir */
7525 argop[2].argop = OP_CPUTFH;
7526 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7527
7528 /* 3. link: current_fh is targetdir, saved_fh is source */
7529 argop[3].argop = OP_CLINK;
7530 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7531
7532 /* 4. Get attributes of dir */
7533 argop[4].argop = OP_GETATTR;
7534 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7535 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7536
7537 /* 5. If link was successful, restore current vp to file */
7538 argop[5].argop = OP_RESTOREFH;
7539
7540 /* 6. Get attributes of linked object */
7541 argop[6].argop = OP_GETATTR;
7542 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7543 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7544
7545 dnlc_remove(tdvp, tnm);
7546
7547 doqueue = 1;
7548 t = gethrtime();
7549
7550 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7551
7552 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7553 if (e.error != 0 && !needrecov) {
7554 PURGE_ATTRCACHE4(tdvp);
7555 PURGE_ATTRCACHE4(svp);
7556 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7557 goto out;
7558 }
7559
7560 if (needrecov) {
7561 bool_t abort;
7562
7563 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7564 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7565 if (abort == FALSE) {
7566 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7567 needrecov);
7568 kmem_free(argop, argoplist_size);
7569 if (!e.error)
7570 (void) xdr_free(xdr_COMPOUND4res_clnt,
7571 (caddr_t)&res);
7572 goto recov_retry;
7573 } else {
7574 if (e.error != 0) {
7575 PURGE_ATTRCACHE4(tdvp);
7576 PURGE_ATTRCACHE4(svp);
7577 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7578 &recov_state, needrecov);
7579 goto out;
7580 }
7581 /* fall through for res.status case */
7582 }
7583 }
7584
7585 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7586
7587 resp = &res;
7588 if (res.status) {
7589 /* If link succeeded, then don't return error */
7590 e.error = geterrno4(res.status);
7591 if (res.array_len <= 4) {
7592 /*
7593 * Either Putfh, Savefh, Putfh dir, or Link failed
7594 */
7595 PURGE_ATTRCACHE4(svp);
7596 PURGE_ATTRCACHE4(tdvp);
7597 if (e.error == EOPNOTSUPP) {
7598 mutex_enter(&mi->mi_lock);
7599 mi->mi_flags &= ~MI4_LINK;
7600 mutex_exit(&mi->mi_lock);
7601 }
7602 /* Remap EISDIR to EPERM for non-root user for SVVS */
7603 /* XXX-LP */
7604 if (e.error == EISDIR && crgetuid(cr) != 0)
7605 e.error = EPERM;
7606 goto out;
7607 }
7608 }
7609
7610 /* either no error or one of the postop getattr failed */
7611
7612 /*
7613 * XXX - if LINK succeeded, but no attrs were returned for link
7614 * file, purge its cache.
7615 *
7616 * XXX Perform a simplified version of wcc checking. Instead of
7617 * have another getattr to get pre-op, just purge cache if
7618 * any of the ops prior to and including the getattr failed.
7619 * If the getattr succeeded then update the attrcache accordingly.
7620 */
7621
7622 /*
7623 * update cache with link file postattrs.
7624 * Note: at this point resop points to link res.
7625 */
7626 resop = &res.array[3]; /* link res */
7627 ln_res = &resop->nfs_resop4_u.oplink;
7628 if (res.status == NFS4_OK)
7629 e.error = nfs4_update_attrcache(res.status,
7630 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7631 t, svp, cr);
7632
7633 /*
7634 * Call makenfs4node to create the new shadow vp for tnm.
7635 * We pass NULL attrs because we just cached attrs for
7636 * the src object. All we're trying to accomplish is to
7637 * to create the new shadow vnode.
7638 */
7639 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7640 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7641
7642 /* Update target cache attribute, readdir and dnlc caches */
7643 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7644 dinfo.di_time_call = t;
7645 dinfo.di_cred = cr;
7646
7647 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7648 ASSERT(nfs4_consistent_type(tdvp));
7649 ASSERT(nfs4_consistent_type(svp));
7650 ASSERT(nfs4_consistent_type(nvp));
7651 VN_RELE(nvp);
7652
7653 if (!e.error) {
7654 vnode_t *tvp;
7655 rnode4_t *trp;
7656 /*
7657 * Notify the source file of this link operation.
7658 */
7659 trp = VTOR4(svp);
7660 tvp = svp;
7661 if (IS_SHADOW(svp, trp))
7662 tvp = RTOV4(trp);
7663 vnevent_link(tvp, ct);
7664 }
7665 out:
7666 kmem_free(argop, argoplist_size);
7667 if (resp)
7668 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7669
7670 nfs_rw_exit(&tdrp->r_rwlock);
7671
7672 return (e.error);
7673 }
7674
7675 /* ARGSUSED */
7676 static int
7677 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7678 caller_context_t *ct, int flags)
7679 {
7680 vnode_t *realvp;
7681
7682 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7683 return (EPERM);
7684 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7685 ndvp = realvp;
7686
7687 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7688 }
7689
7690 /*
7691 * nfs4rename does the real work of renaming in NFS Version 4.
7692 *
7693 * A file handle is considered volatile for renaming purposes if either
7694 * of the volatile bits are turned on. However, the compound may differ
7695 * based on the likelihood of the filehandle to change during rename.
7696 */
7697 static int
7698 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7699 caller_context_t *ct)
7700 {
7701 int error;
7702 mntinfo4_t *mi;
7703 vnode_t *nvp = NULL;
7704 vnode_t *ovp = NULL;
7705 char *tmpname = NULL;
7706 rnode4_t *rp;
7707 rnode4_t *odrp;
7708 rnode4_t *ndrp;
7709 int did_link = 0;
7710 int do_link = 1;
7711 nfsstat4 stat = NFS4_OK;
7712
7713 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7714 ASSERT(nfs4_consistent_type(odvp));
7715 ASSERT(nfs4_consistent_type(ndvp));
7716
7717 if (onm[0] == '.' && (onm[1] == '\0' ||
7718 (onm[1] == '.' && onm[2] == '\0')))
7719 return (EINVAL);
7720
7721 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7722 (nnm[1] == '.' && nnm[2] == '\0')))
7723 return (EINVAL);
7724
7725 odrp = VTOR4(odvp);
7726 ndrp = VTOR4(ndvp);
7727 if ((intptr_t)odrp < (intptr_t)ndrp) {
7728 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7729 return (EINTR);
7730 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7731 nfs_rw_exit(&odrp->r_rwlock);
7732 return (EINTR);
7733 }
7734 } else {
7735 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7736 return (EINTR);
7737 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7738 nfs_rw_exit(&ndrp->r_rwlock);
7739 return (EINTR);
7740 }
7741 }
7742
7743 /*
7744 * Lookup the target file. If it exists, it needs to be
7745 * checked to see whether it is a mount point and whether
7746 * it is active (open).
7747 */
7748 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7749 if (!error) {
7750 int isactive;
7751
7752 ASSERT(nfs4_consistent_type(nvp));
7753 /*
7754 * If this file has been mounted on, then just
7755 * return busy because renaming to it would remove
7756 * the mounted file system from the name space.
7757 */
7758 if (vn_ismntpt(nvp)) {
7759 VN_RELE(nvp);
7760 nfs_rw_exit(&odrp->r_rwlock);
7761 nfs_rw_exit(&ndrp->r_rwlock);
7762 return (EBUSY);
7763 }
7764
7765 /*
7766 * First just remove the entry from the name cache, as it
7767 * is most likely the only entry for this vp.
7768 */
7769 dnlc_remove(ndvp, nnm);
7770
7771 rp = VTOR4(nvp);
7772
7773 if (nvp->v_type != VREG) {
7774 /*
7775 * Purge the name cache of all references to this vnode
7776 * so that we can check the reference count to infer
7777 * whether it is active or not.
7778 */
7779 if (nvp->v_count > 1)
7780 dnlc_purge_vp(nvp);
7781
7782 isactive = nvp->v_count > 1;
7783 } else {
7784 mutex_enter(&rp->r_os_lock);
7785 isactive = list_head(&rp->r_open_streams) != NULL;
7786 mutex_exit(&rp->r_os_lock);
7787 }
7788
7789 /*
7790 * If the vnode is active and is not a directory,
7791 * arrange to rename it to a
7792 * temporary file so that it will continue to be
7793 * accessible. This implements the "unlink-open-file"
7794 * semantics for the target of a rename operation.
7795 * Before doing this though, make sure that the
7796 * source and target files are not already the same.
7797 */
7798 if (isactive && nvp->v_type != VDIR) {
7799 /*
7800 * Lookup the source name.
7801 */
7802 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7803
7804 /*
7805 * The source name *should* already exist.
7806 */
7807 if (error) {
7808 VN_RELE(nvp);
7809 nfs_rw_exit(&odrp->r_rwlock);
7810 nfs_rw_exit(&ndrp->r_rwlock);
7811 return (error);
7812 }
7813
7814 ASSERT(nfs4_consistent_type(ovp));
7815
7816 /*
7817 * Compare the two vnodes. If they are the same,
7818 * just release all held vnodes and return success.
7819 */
7820 if (VN_CMP(ovp, nvp)) {
7821 VN_RELE(ovp);
7822 VN_RELE(nvp);
7823 nfs_rw_exit(&odrp->r_rwlock);
7824 nfs_rw_exit(&ndrp->r_rwlock);
7825 return (0);
7826 }
7827
7828 /*
7829 * Can't mix and match directories and non-
7830 * directories in rename operations. We already
7831 * know that the target is not a directory. If
7832 * the source is a directory, return an error.
7833 */
7834 if (ovp->v_type == VDIR) {
7835 VN_RELE(ovp);
7836 VN_RELE(nvp);
7837 nfs_rw_exit(&odrp->r_rwlock);
7838 nfs_rw_exit(&ndrp->r_rwlock);
7839 return (ENOTDIR);
7840 }
7841 link_call:
7842 /*
7843 * The target file exists, is not the same as
7844 * the source file, and is active. We first
7845 * try to Link it to a temporary filename to
7846 * avoid having the server removing the file
7847 * completely (which could cause data loss to
7848 * the user's POV in the event the Rename fails
7849 * -- see bug 1165874).
7850 */
7851 /*
7852 * The do_link and did_link booleans are
7853 * introduced in the event we get NFS4ERR_FILE_OPEN
7854 * returned for the Rename. Some servers can
7855 * not Rename over an Open file, so they return
7856 * this error. The client needs to Remove the
7857 * newly created Link and do two Renames, just
7858 * as if the server didn't support LINK.
7859 */
7860 tmpname = newname();
7861 error = 0;
7862
7863 if (do_link) {
7864 error = nfs4_link(ndvp, nvp, tmpname, cr,
7865 NULL, 0);
7866 }
7867 if (error == EOPNOTSUPP || !do_link) {
7868 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7869 cr, NULL, 0);
7870 did_link = 0;
7871 } else {
7872 did_link = 1;
7873 }
7874 if (error) {
7875 kmem_free(tmpname, MAXNAMELEN);
7876 VN_RELE(ovp);
7877 VN_RELE(nvp);
7878 nfs_rw_exit(&odrp->r_rwlock);
7879 nfs_rw_exit(&ndrp->r_rwlock);
7880 return (error);
7881 }
7882
7883 mutex_enter(&rp->r_statelock);
7884 if (rp->r_unldvp == NULL) {
7885 VN_HOLD(ndvp);
7886 rp->r_unldvp = ndvp;
7887 if (rp->r_unlcred != NULL)
7888 crfree(rp->r_unlcred);
7889 crhold(cr);
7890 rp->r_unlcred = cr;
7891 rp->r_unlname = tmpname;
7892 } else {
7893 if (rp->r_unlname)
7894 kmem_free(rp->r_unlname, MAXNAMELEN);
7895 rp->r_unlname = tmpname;
7896 }
7897 mutex_exit(&rp->r_statelock);
7898 }
7899
7900 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7901
7902 ASSERT(nfs4_consistent_type(nvp));
7903 }
7904
7905 if (ovp == NULL) {
7906 /*
7907 * When renaming directories to be a subdirectory of a
7908 * different parent, the dnlc entry for ".." will no
7909 * longer be valid, so it must be removed.
7910 *
7911 * We do a lookup here to determine whether we are renaming
7912 * a directory and we need to check if we are renaming
7913 * an unlinked file. This might have already been done
7914 * in previous code, so we check ovp == NULL to avoid
7915 * doing it twice.
7916 */
7917 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7918 /*
7919 * The source name *should* already exist.
7920 */
7921 if (error) {
7922 nfs_rw_exit(&odrp->r_rwlock);
7923 nfs_rw_exit(&ndrp->r_rwlock);
7924 if (nvp) {
7925 VN_RELE(nvp);
7926 }
7927 return (error);
7928 }
7929 ASSERT(ovp != NULL);
7930 ASSERT(nfs4_consistent_type(ovp));
7931 }
7932
7933 /*
7934 * Is the object being renamed a dir, and if so, is
7935 * it being renamed to a child of itself? The underlying
7936 * fs should ultimately return EINVAL for this case;
7937 * however, buggy beta non-Solaris NFSv4 servers at
7938 * interop testing events have allowed this behavior,
7939 * and it caused our client to panic due to a recursive
7940 * mutex_enter in fn_move.
7941 *
7942 * The tedious locking in fn_move could be changed to
7943 * deal with this case, and the client could avoid the
7944 * panic; however, the client would just confuse itself
7945 * later and misbehave. A better way to handle the broken
7946 * server is to detect this condition and return EINVAL
7947 * without ever sending the the bogus rename to the server.
7948 * We know the rename is invalid -- just fail it now.
7949 */
7950 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7951 VN_RELE(ovp);
7952 nfs_rw_exit(&odrp->r_rwlock);
7953 nfs_rw_exit(&ndrp->r_rwlock);
7954 if (nvp) {
7955 VN_RELE(nvp);
7956 }
7957 return (EINVAL);
7958 }
7959
7960 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7961
7962 /*
7963 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7964 * possible for the filehandle to change due to the rename.
7965 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7966 * the fh will not change because of the rename, but we still need
7967 * to update its rnode entry with the new name for
7968 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7969 * has no effect on these for now, but for future improvements,
7970 * we might want to use it too to simplify handling of files
7971 * that are open with that flag on. (XXX)
7972 */
7973 mi = VTOMI4(odvp);
7974 if (NFS4_VOLATILE_FH(mi))
7975 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7976 &stat);
7977 else
7978 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7979 &stat);
7980
7981 ASSERT(nfs4_consistent_type(odvp));
7982 ASSERT(nfs4_consistent_type(ndvp));
7983 ASSERT(nfs4_consistent_type(ovp));
7984
7985 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7986 do_link = 0;
7987 /*
7988 * Before the 'link_call' code, we did a nfs4_lookup
7989 * that puts a VN_HOLD on nvp. After the nfs4_link
7990 * call we call VN_RELE to match that hold. We need
7991 * to place an additional VN_HOLD here since we will
7992 * be hitting that VN_RELE again.
7993 */
7994 VN_HOLD(nvp);
7995
7996 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
7997
7998 /* Undo the unlinked file naming stuff we just did */
7999 mutex_enter(&rp->r_statelock);
8000 if (rp->r_unldvp) {
8001 VN_RELE(ndvp);
8002 rp->r_unldvp = NULL;
8003 if (rp->r_unlcred != NULL)
8004 crfree(rp->r_unlcred);
8005 rp->r_unlcred = NULL;
8006 /* rp->r_unlanme points to tmpname */
8007 if (rp->r_unlname)
8008 kmem_free(rp->r_unlname, MAXNAMELEN);
8009 rp->r_unlname = NULL;
8010 }
8011 mutex_exit(&rp->r_statelock);
8012
8013 if (nvp) {
8014 VN_RELE(nvp);
8015 }
8016 goto link_call;
8017 }
8018
8019 if (error) {
8020 VN_RELE(ovp);
8021 nfs_rw_exit(&odrp->r_rwlock);
8022 nfs_rw_exit(&ndrp->r_rwlock);
8023 if (nvp) {
8024 VN_RELE(nvp);
8025 }
8026 return (error);
8027 }
8028
8029 /*
8030 * when renaming directories to be a subdirectory of a
8031 * different parent, the dnlc entry for ".." will no
8032 * longer be valid, so it must be removed
8033 */
8034 rp = VTOR4(ovp);
8035 if (ndvp != odvp) {
8036 if (ovp->v_type == VDIR) {
8037 dnlc_remove(ovp, "..");
8038 if (rp->r_dir != NULL)
8039 nfs4_purge_rddir_cache(ovp);
8040 }
8041 }
8042
8043 /*
8044 * If we are renaming the unlinked file, update the
8045 * r_unldvp and r_unlname as needed.
8046 */
8047 mutex_enter(&rp->r_statelock);
8048 if (rp->r_unldvp != NULL) {
8049 if (strcmp(rp->r_unlname, onm) == 0) {
8050 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8051 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8052 if (ndvp != rp->r_unldvp) {
8053 VN_RELE(rp->r_unldvp);
8054 rp->r_unldvp = ndvp;
8055 VN_HOLD(ndvp);
8056 }
8057 }
8058 }
8059 mutex_exit(&rp->r_statelock);
8060
8061 /*
8062 * Notify the rename vnevents to source vnode, and to the target
8063 * vnode if it already existed.
8064 */
8065 if (error == 0) {
8066 vnode_t *tvp;
8067 rnode4_t *trp;
8068 /*
8069 * Notify the vnode. Each links is represented by
8070 * a different vnode, in nfsv4.
8071 */
8072 if (nvp) {
8073 trp = VTOR4(nvp);
8074 tvp = nvp;
8075 if (IS_SHADOW(nvp, trp))
8076 tvp = RTOV4(trp);
8077 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8078 }
8079
8080 /*
8081 * if the source and destination directory are not the
8082 * same notify the destination directory.
8083 */
8084 if (VTOR4(odvp) != VTOR4(ndvp)) {
8085 trp = VTOR4(ndvp);
8086 tvp = ndvp;
8087 if (IS_SHADOW(ndvp, trp))
8088 tvp = RTOV4(trp);
8089 vnevent_rename_dest_dir(tvp, ct);
8090 }
8091
8092 trp = VTOR4(ovp);
8093 tvp = ovp;
8094 if (IS_SHADOW(ovp, trp))
8095 tvp = RTOV4(trp);
8096 vnevent_rename_src(tvp, odvp, onm, ct);
8097 }
8098
8099 if (nvp) {
8100 VN_RELE(nvp);
8101 }
8102 VN_RELE(ovp);
8103
8104 nfs_rw_exit(&odrp->r_rwlock);
8105 nfs_rw_exit(&ndrp->r_rwlock);
8106
8107 return (error);
8108 }
8109
8110 /*
8111 * When the parent directory has changed, sv_dfh must be updated
8112 */
8113 static void
8114 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8115 {
8116 svnode_t *sv = VTOSV(vp);
8117 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8118 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8119
8120 sfh4_hold(new_dfh);
8121 sv->sv_dfh = new_dfh;
8122 sfh4_rele(&old_dfh);
8123 }
8124
8125 /*
8126 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8127 * when it is known that the filehandle is persistent through rename.
8128 *
8129 * Rename requires that the current fh be the target directory and the
8130 * saved fh be the source directory. After the operation, the current fh
8131 * is unchanged.
8132 * The compound op structure for persistent fh rename is:
8133 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8134 * Rather than bother with the directory postop args, we'll simply
8135 * update that a change occurred in the cache, so no post-op getattrs.
8136 */
8137 static int
8138 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8139 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8140 {
8141 COMPOUND4args_clnt args;
8142 COMPOUND4res_clnt res, *resp = NULL;
8143 nfs_argop4 *argop;
8144 nfs_resop4 *resop;
8145 int doqueue, argoplist_size;
8146 mntinfo4_t *mi;
8147 rnode4_t *odrp = VTOR4(odvp);
8148 rnode4_t *ndrp = VTOR4(ndvp);
8149 RENAME4res *rn_res;
8150 bool_t needrecov;
8151 nfs4_recov_state_t recov_state;
8152 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8153 dirattr_info_t dinfo, *dinfop;
8154
8155 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8156
8157 recov_state.rs_flags = 0;
8158 recov_state.rs_num_retry_despite_err = 0;
8159
8160 /*
8161 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8162 *
8163 * If source/target are different dirs, then append putfh(src); getattr
8164 */
8165 args.array_len = (odvp == ndvp) ? 5 : 7;
8166 argoplist_size = args.array_len * sizeof (nfs_argop4);
8167 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8168
8169 recov_retry:
8170 *statp = NFS4_OK;
8171
8172 /* No need to Lookup the file, persistent fh */
8173 args.ctag = TAG_RENAME;
8174
8175 mi = VTOMI4(odvp);
8176 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8177 if (e.error) {
8178 kmem_free(argop, argoplist_size);
8179 return (e.error);
8180 }
8181
8182 /* 0: putfh source directory */
8183 argop[0].argop = OP_CPUTFH;
8184 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8185
8186 /* 1: Save source fh to free up current for target */
8187 argop[1].argop = OP_SAVEFH;
8188
8189 /* 2: putfh targetdir */
8190 argop[2].argop = OP_CPUTFH;
8191 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8192
8193 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8194 argop[3].argop = OP_CRENAME;
8195 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8196 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8197
8198 /* 4: getattr (targetdir) */
8199 argop[4].argop = OP_GETATTR;
8200 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8201 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8202
8203 if (ndvp != odvp) {
8204
8205 /* 5: putfh (sourcedir) */
8206 argop[5].argop = OP_CPUTFH;
8207 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8208
8209 /* 6: getattr (sourcedir) */
8210 argop[6].argop = OP_GETATTR;
8211 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8212 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8213 }
8214
8215 dnlc_remove(odvp, onm);
8216 dnlc_remove(ndvp, nnm);
8217
8218 doqueue = 1;
8219 dinfo.di_time_call = gethrtime();
8220 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8221
8222 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8223 if (e.error) {
8224 PURGE_ATTRCACHE4(odvp);
8225 PURGE_ATTRCACHE4(ndvp);
8226 } else {
8227 *statp = res.status;
8228 }
8229
8230 if (needrecov) {
8231 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8232 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8233 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8234 if (!e.error)
8235 (void) xdr_free(xdr_COMPOUND4res_clnt,
8236 (caddr_t)&res);
8237 goto recov_retry;
8238 }
8239 }
8240
8241 if (!e.error) {
8242 resp = &res;
8243 /*
8244 * as long as OP_RENAME
8245 */
8246 if (res.status != NFS4_OK && res.array_len <= 4) {
8247 e.error = geterrno4(res.status);
8248 PURGE_ATTRCACHE4(odvp);
8249 PURGE_ATTRCACHE4(ndvp);
8250 /*
8251 * System V defines rename to return EEXIST, not
8252 * ENOTEMPTY if the target directory is not empty.
8253 * Over the wire, the error is NFSERR_ENOTEMPTY
8254 * which geterrno4 maps to ENOTEMPTY.
8255 */
8256 if (e.error == ENOTEMPTY)
8257 e.error = EEXIST;
8258 } else {
8259
8260 resop = &res.array[3]; /* rename res */
8261 rn_res = &resop->nfs_resop4_u.oprename;
8262
8263 if (res.status == NFS4_OK) {
8264 /*
8265 * Update target attribute, readdir and dnlc
8266 * caches.
8267 */
8268 dinfo.di_garp =
8269 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8270 dinfo.di_cred = cr;
8271 dinfop = &dinfo;
8272 } else
8273 dinfop = NULL;
8274
8275 nfs4_update_dircaches(&rn_res->target_cinfo,
8276 ndvp, NULL, NULL, dinfop);
8277
8278 /*
8279 * Update source attribute, readdir and dnlc caches
8280 *
8281 */
8282 if (ndvp != odvp) {
8283 update_parentdir_sfh(renvp, ndvp);
8284
8285 if (dinfop)
8286 dinfo.di_garp =
8287 &(res.array[6].nfs_resop4_u.
8288 opgetattr.ga_res);
8289
8290 nfs4_update_dircaches(&rn_res->source_cinfo,
8291 odvp, NULL, NULL, dinfop);
8292 }
8293
8294 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8295 nnm);
8296 }
8297 }
8298
8299 if (resp)
8300 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8301 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8302 kmem_free(argop, argoplist_size);
8303
8304 return (e.error);
8305 }
8306
8307 /*
8308 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8309 * it is possible for the filehandle to change due to the rename.
8310 *
8311 * The compound req in this case includes a post-rename lookup and getattr
8312 * to ensure that we have the correct fh and attributes for the object.
8313 *
8314 * Rename requires that the current fh be the target directory and the
8315 * saved fh be the source directory. After the operation, the current fh
8316 * is unchanged.
8317 *
8318 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8319 * update the filehandle for the renamed object. We also get the old
8320 * filehandle for historical reasons; this should be taken out sometime.
8321 * This results in a rather cumbersome compound...
8322 *
8323 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8324 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8325 *
8326 */
8327 static int
8328 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8329 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8330 {
8331 COMPOUND4args_clnt args;
8332 COMPOUND4res_clnt res, *resp = NULL;
8333 int argoplist_size;
8334 nfs_argop4 *argop;
8335 nfs_resop4 *resop;
8336 int doqueue;
8337 mntinfo4_t *mi;
8338 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8339 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8340 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8341 RENAME4res *rn_res;
8342 GETFH4res *ngf_res;
8343 bool_t needrecov;
8344 nfs4_recov_state_t recov_state;
8345 hrtime_t t;
8346 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8347 dirattr_info_t dinfo, *dinfop = &dinfo;
8348
8349 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8350
8351 recov_state.rs_flags = 0;
8352 recov_state.rs_num_retry_despite_err = 0;
8353
8354 recov_retry:
8355 *statp = NFS4_OK;
8356
8357 /*
8358 * There is a window between the RPC and updating the path and
8359 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8360 * code, so that it doesn't try to use the old path during that
8361 * window.
8362 */
8363 mutex_enter(&orp->r_statelock);
8364 while (orp->r_flags & R4RECEXPFH) {
8365 klwp_t *lwp = ttolwp(curthread);
8366
8367 if (lwp != NULL)
8368 lwp->lwp_nostop++;
8369 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8370 mutex_exit(&orp->r_statelock);
8371 if (lwp != NULL)
8372 lwp->lwp_nostop--;
8373 return (EINTR);
8374 }
8375 if (lwp != NULL)
8376 lwp->lwp_nostop--;
8377 }
8378 orp->r_flags |= R4RECEXPFH;
8379 mutex_exit(&orp->r_statelock);
8380
8381 mi = VTOMI4(odvp);
8382
8383 args.ctag = TAG_RENAME_VFH;
8384 args.array_len = (odvp == ndvp) ? 10 : 12;
8385 argoplist_size = args.array_len * sizeof (nfs_argop4);
8386 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8387
8388 /*
8389 * Rename ops:
8390 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8391 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8392 * LOOKUP(trgt), GETFH(new), GETATTR,
8393 *
8394 * if (odvp != ndvp)
8395 * add putfh(sourcedir), getattr(sourcedir) }
8396 */
8397 args.array = argop;
8398
8399 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8400 &recov_state, NULL);
8401 if (e.error) {
8402 kmem_free(argop, argoplist_size);
8403 mutex_enter(&orp->r_statelock);
8404 orp->r_flags &= ~R4RECEXPFH;
8405 cv_broadcast(&orp->r_cv);
8406 mutex_exit(&orp->r_statelock);
8407 return (e.error);
8408 }
8409
8410 /* 0: putfh source directory */
8411 argop[0].argop = OP_CPUTFH;
8412 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8413
8414 /* 1: Save source fh to free up current for target */
8415 argop[1].argop = OP_SAVEFH;
8416
8417 /* 2: Lookup pre-rename fh of renamed object */
8418 argop[2].argop = OP_CLOOKUP;
8419 argop[2].nfs_argop4_u.opclookup.cname = onm;
8420
8421 /* 3: getfh fh of renamed object (before rename) */
8422 argop[3].argop = OP_GETFH;
8423
8424 /* 4: putfh targetdir */
8425 argop[4].argop = OP_CPUTFH;
8426 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8427
8428 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8429 argop[5].argop = OP_CRENAME;
8430 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8431 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8432
8433 /* 6: getattr of target dir (post op attrs) */
8434 argop[6].argop = OP_GETATTR;
8435 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8436 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8437
8438 /* 7: Lookup post-rename fh of renamed object */
8439 argop[7].argop = OP_CLOOKUP;
8440 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8441
8442 /* 8: getfh fh of renamed object (after rename) */
8443 argop[8].argop = OP_GETFH;
8444
8445 /* 9: getattr of renamed object */
8446 argop[9].argop = OP_GETATTR;
8447 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8448 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8449
8450 /*
8451 * If source/target dirs are different, then get new post-op
8452 * attrs for source dir also.
8453 */
8454 if (ndvp != odvp) {
8455 /* 10: putfh (sourcedir) */
8456 argop[10].argop = OP_CPUTFH;
8457 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8458
8459 /* 11: getattr (sourcedir) */
8460 argop[11].argop = OP_GETATTR;
8461 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8462 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8463 }
8464
8465 dnlc_remove(odvp, onm);
8466 dnlc_remove(ndvp, nnm);
8467
8468 doqueue = 1;
8469 t = gethrtime();
8470 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8471
8472 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8473 if (e.error) {
8474 PURGE_ATTRCACHE4(odvp);
8475 PURGE_ATTRCACHE4(ndvp);
8476 if (!needrecov) {
8477 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8478 &recov_state, needrecov);
8479 goto out;
8480 }
8481 } else {
8482 *statp = res.status;
8483 }
8484
8485 if (needrecov) {
8486 bool_t abort;
8487
8488 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8489 OP_RENAME, NULL, NULL, NULL);
8490 if (abort == FALSE) {
8491 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8492 &recov_state, needrecov);
8493 kmem_free(argop, argoplist_size);
8494 if (!e.error)
8495 (void) xdr_free(xdr_COMPOUND4res_clnt,
8496 (caddr_t)&res);
8497 mutex_enter(&orp->r_statelock);
8498 orp->r_flags &= ~R4RECEXPFH;
8499 cv_broadcast(&orp->r_cv);
8500 mutex_exit(&orp->r_statelock);
8501 goto recov_retry;
8502 } else {
8503 if (e.error != 0) {
8504 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8505 &recov_state, needrecov);
8506 goto out;
8507 }
8508 /* fall through for res.status case */
8509 }
8510 }
8511
8512 resp = &res;
8513 /*
8514 * If OP_RENAME (or any prev op) failed, then return an error.
8515 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8516 */
8517 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8518 /*
8519 * Error in an op other than last Getattr
8520 */
8521 e.error = geterrno4(res.status);
8522 PURGE_ATTRCACHE4(odvp);
8523 PURGE_ATTRCACHE4(ndvp);
8524 /*
8525 * System V defines rename to return EEXIST, not
8526 * ENOTEMPTY if the target directory is not empty.
8527 * Over the wire, the error is NFSERR_ENOTEMPTY
8528 * which geterrno4 maps to ENOTEMPTY.
8529 */
8530 if (e.error == ENOTEMPTY)
8531 e.error = EEXIST;
8532 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8533 needrecov);
8534 goto out;
8535 }
8536
8537 /* rename results */
8538 rn_res = &res.array[5].nfs_resop4_u.oprename;
8539
8540 if (res.status == NFS4_OK) {
8541 /* Update target attribute, readdir and dnlc caches */
8542 dinfo.di_garp =
8543 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8544 dinfo.di_cred = cr;
8545 dinfo.di_time_call = t;
8546 } else
8547 dinfop = NULL;
8548
8549 /* Update source cache attribute, readdir and dnlc caches */
8550 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8551
8552 /* Update source cache attribute, readdir and dnlc caches */
8553 if (ndvp != odvp) {
8554 update_parentdir_sfh(ovp, ndvp);
8555
8556 /*
8557 * If dinfop is non-NULL, then compound succeded, so
8558 * set di_garp to attrs for source dir. dinfop is only
8559 * set to NULL when compound fails.
8560 */
8561 if (dinfop)
8562 dinfo.di_garp =
8563 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8564 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8565 dinfop);
8566 }
8567
8568 /*
8569 * Update the rnode with the new component name and args,
8570 * and if the file handle changed, also update it with the new fh.
8571 * This is only necessary if the target object has an rnode
8572 * entry and there is no need to create one for it.
8573 */
8574 resop = &res.array[8]; /* getfh new res */
8575 ngf_res = &resop->nfs_resop4_u.opgetfh;
8576
8577 /*
8578 * Update the path and filehandle for the renamed object.
8579 */
8580 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8581
8582 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8583
8584 if (res.status == NFS4_OK) {
8585 resop++; /* getattr res */
8586 e.error = nfs4_update_attrcache(res.status,
8587 &resop->nfs_resop4_u.opgetattr.ga_res,
8588 t, ovp, cr);
8589 }
8590
8591 out:
8592 kmem_free(argop, argoplist_size);
8593 if (resp)
8594 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8595 mutex_enter(&orp->r_statelock);
8596 orp->r_flags &= ~R4RECEXPFH;
8597 cv_broadcast(&orp->r_cv);
8598 mutex_exit(&orp->r_statelock);
8599
8600 return (e.error);
8601 }
8602
8603 /* ARGSUSED */
8604 static int
8605 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8606 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8607 {
8608 int error;
8609 vnode_t *vp;
8610
8611 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8612 return (EPERM);
8613 /*
8614 * As ".." has special meaning and rather than send a mkdir
8615 * over the wire to just let the server freak out, we just
8616 * short circuit it here and return EEXIST
8617 */
8618 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8619 return (EEXIST);
8620
8621 /*
8622 * Decision to get the right gid and setgid bit of the
8623 * new directory is now made in call_nfs4_create_req.
8624 */
8625 va->va_mask |= AT_MODE;
8626 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8627 if (error)
8628 return (error);
8629
8630 *vpp = vp;
8631 return (0);
8632 }
8633
8634
8635 /*
8636 * rmdir is using the same remove v4 op as does remove.
8637 * Remove requires that the current fh be the target directory.
8638 * After the operation, the current fh is unchanged.
8639 * The compound op structure is:
8640 * PUTFH(targetdir), REMOVE
8641 */
8642 /*ARGSUSED4*/
8643 static int
8644 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8645 caller_context_t *ct, int flags)
8646 {
8647 int need_end_op = FALSE;
8648 COMPOUND4args_clnt args;
8649 COMPOUND4res_clnt res, *resp = NULL;
8650 REMOVE4res *rm_res;
8651 nfs_argop4 argop[3];
8652 nfs_resop4 *resop;
8653 vnode_t *vp;
8654 int doqueue;
8655 mntinfo4_t *mi;
8656 rnode4_t *drp;
8657 bool_t needrecov = FALSE;
8658 nfs4_recov_state_t recov_state;
8659 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8660 dirattr_info_t dinfo, *dinfop;
8661
8662 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8663 return (EPERM);
8664 /*
8665 * As ".." has special meaning and rather than send a rmdir
8666 * over the wire to just let the server freak out, we just
8667 * short circuit it here and return EEXIST
8668 */
8669 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8670 return (EEXIST);
8671
8672 drp = VTOR4(dvp);
8673 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8674 return (EINTR);
8675
8676 /*
8677 * Attempt to prevent a rmdir(".") from succeeding.
8678 */
8679 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8680 if (e.error) {
8681 nfs_rw_exit(&drp->r_rwlock);
8682 return (e.error);
8683 }
8684 if (vp == cdir) {
8685 VN_RELE(vp);
8686 nfs_rw_exit(&drp->r_rwlock);
8687 return (EINVAL);
8688 }
8689
8690 /*
8691 * Since nfsv4 remove op works on both files and directories,
8692 * check that the removed object is indeed a directory.
8693 */
8694 if (vp->v_type != VDIR) {
8695 VN_RELE(vp);
8696 nfs_rw_exit(&drp->r_rwlock);
8697 return (ENOTDIR);
8698 }
8699
8700 /*
8701 * First just remove the entry from the name cache, as it
8702 * is most likely an entry for this vp.
8703 */
8704 dnlc_remove(dvp, nm);
8705
8706 /*
8707 * If there vnode reference count is greater than one, then
8708 * there may be additional references in the DNLC which will
8709 * need to be purged. First, trying removing the entry for
8710 * the parent directory and see if that removes the additional
8711 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8712 * to completely remove any references to the directory which
8713 * might still exist in the DNLC.
8714 */
8715 if (vp->v_count > 1) {
8716 dnlc_remove(vp, "..");
8717 if (vp->v_count > 1)
8718 dnlc_purge_vp(vp);
8719 }
8720
8721 mi = VTOMI4(dvp);
8722 recov_state.rs_flags = 0;
8723 recov_state.rs_num_retry_despite_err = 0;
8724
8725 recov_retry:
8726 args.ctag = TAG_RMDIR;
8727
8728 /*
8729 * Rmdir ops: putfh dir; remove
8730 */
8731 args.array_len = 3;
8732 args.array = argop;
8733
8734 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8735 if (e.error) {
8736 nfs_rw_exit(&drp->r_rwlock);
8737 return (e.error);
8738 }
8739 need_end_op = TRUE;
8740
8741 /* putfh directory */
8742 argop[0].argop = OP_CPUTFH;
8743 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8744
8745 /* remove */
8746 argop[1].argop = OP_CREMOVE;
8747 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8748
8749 /* getattr (postop attrs for dir that contained removed dir) */
8750 argop[2].argop = OP_GETATTR;
8751 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8752 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8753
8754 dinfo.di_time_call = gethrtime();
8755 doqueue = 1;
8756 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8757
8758 PURGE_ATTRCACHE4(vp);
8759
8760 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8761 if (e.error) {
8762 PURGE_ATTRCACHE4(dvp);
8763 }
8764
8765 if (needrecov) {
8766 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8767 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8768 if (!e.error)
8769 (void) xdr_free(xdr_COMPOUND4res_clnt,
8770 (caddr_t)&res);
8771
8772 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8773 needrecov);
8774 need_end_op = FALSE;
8775 goto recov_retry;
8776 }
8777 }
8778
8779 if (!e.error) {
8780 resp = &res;
8781
8782 /*
8783 * Only return error if first 2 ops (OP_REMOVE or earlier)
8784 * failed.
8785 */
8786 if (res.status != NFS4_OK && res.array_len <= 2) {
8787 e.error = geterrno4(res.status);
8788 PURGE_ATTRCACHE4(dvp);
8789 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8790 &recov_state, needrecov);
8791 need_end_op = FALSE;
8792 nfs4_purge_stale_fh(e.error, dvp, cr);
8793 /*
8794 * System V defines rmdir to return EEXIST, not
8795 * ENOTEMPTY if the directory is not empty. Over
8796 * the wire, the error is NFSERR_ENOTEMPTY which
8797 * geterrno4 maps to ENOTEMPTY.
8798 */
8799 if (e.error == ENOTEMPTY)
8800 e.error = EEXIST;
8801 } else {
8802 resop = &res.array[1]; /* remove res */
8803 rm_res = &resop->nfs_resop4_u.opremove;
8804
8805 if (res.status == NFS4_OK) {
8806 resop = &res.array[2]; /* dir attrs */
8807 dinfo.di_garp =
8808 &resop->nfs_resop4_u.opgetattr.ga_res;
8809 dinfo.di_cred = cr;
8810 dinfop = &dinfo;
8811 } else
8812 dinfop = NULL;
8813
8814 /* Update dir attribute, readdir and dnlc caches */
8815 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8816 dinfop);
8817
8818 /* destroy rddir cache for dir that was removed */
8819 if (VTOR4(vp)->r_dir != NULL)
8820 nfs4_purge_rddir_cache(vp);
8821 }
8822 }
8823
8824 if (need_end_op)
8825 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8826
8827 nfs_rw_exit(&drp->r_rwlock);
8828
8829 if (resp)
8830 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8831
8832 if (e.error == 0) {
8833 vnode_t *tvp;
8834 rnode4_t *trp;
8835 trp = VTOR4(vp);
8836 tvp = vp;
8837 if (IS_SHADOW(vp, trp))
8838 tvp = RTOV4(trp);
8839 vnevent_rmdir(tvp, dvp, nm, ct);
8840 }
8841
8842 VN_RELE(vp);
8843
8844 return (e.error);
8845 }
8846
8847 /* ARGSUSED */
8848 static int
8849 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8850 caller_context_t *ct, int flags)
8851 {
8852 int error;
8853 vnode_t *vp;
8854 rnode4_t *rp;
8855 char *contents;
8856 mntinfo4_t *mi = VTOMI4(dvp);
8857
8858 if (nfs_zone() != mi->mi_zone)
8859 return (EPERM);
8860 if (!(mi->mi_flags & MI4_SYMLINK))
8861 return (EOPNOTSUPP);
8862
8863 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8864 if (error)
8865 return (error);
8866
8867 ASSERT(nfs4_consistent_type(vp));
8868 rp = VTOR4(vp);
8869 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8870
8871 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8872
8873 if (contents != NULL) {
8874 mutex_enter(&rp->r_statelock);
8875 if (rp->r_symlink.contents == NULL) {
8876 rp->r_symlink.len = strlen(tnm);
8877 bcopy(tnm, contents, rp->r_symlink.len);
8878 rp->r_symlink.contents = contents;
8879 rp->r_symlink.size = MAXPATHLEN;
8880 mutex_exit(&rp->r_statelock);
8881 } else {
8882 mutex_exit(&rp->r_statelock);
8883 kmem_free((void *)contents, MAXPATHLEN);
8884 }
8885 }
8886 }
8887 VN_RELE(vp);
8888
8889 return (error);
8890 }
8891
8892
8893 /*
8894 * Read directory entries.
8895 * There are some weird things to look out for here. The uio_loffset
8896 * field is either 0 or it is the offset returned from a previous
8897 * readdir. It is an opaque value used by the server to find the
8898 * correct directory block to read. The count field is the number
8899 * of blocks to read on the server. This is advisory only, the server
8900 * may return only one block's worth of entries. Entries may be compressed
8901 * on the server.
8902 */
8903 /* ARGSUSED */
8904 static int
8905 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8906 caller_context_t *ct, int flags)
8907 {
8908 int error;
8909 uint_t count;
8910 rnode4_t *rp;
8911 rddir4_cache *rdc;
8912 rddir4_cache *rrdc;
8913
8914 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8915 return (EIO);
8916 rp = VTOR4(vp);
8917
8918 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8919
8920 /*
8921 * Make sure that the directory cache is valid.
8922 */
8923 if (rp->r_dir != NULL) {
8924 if (nfs_disable_rddir_cache != 0) {
8925 /*
8926 * Setting nfs_disable_rddir_cache in /etc/system
8927 * allows interoperability with servers that do not
8928 * properly update the attributes of directories.
8929 * Any cached information gets purged before an
8930 * access is made to it.
8931 */
8932 nfs4_purge_rddir_cache(vp);
8933 }
8934
8935 error = nfs4_validate_caches(vp, cr);
8936 if (error)
8937 return (error);
8938 }
8939
8940 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8941
8942 /*
8943 * Short circuit last readdir which always returns 0 bytes.
8944 * This can be done after the directory has been read through
8945 * completely at least once. This will set r_direof which
8946 * can be used to find the value of the last cookie.
8947 */
8948 mutex_enter(&rp->r_statelock);
8949 if (rp->r_direof != NULL &&
8950 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8951 mutex_exit(&rp->r_statelock);
8952 #ifdef DEBUG
8953 nfs4_readdir_cache_shorts++;
8954 #endif
8955 if (eofp)
8956 *eofp = 1;
8957 return (0);
8958 }
8959
8960 /*
8961 * Look for a cache entry. Cache entries are identified
8962 * by the NFS cookie value and the byte count requested.
8963 */
8964 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8965
8966 /*
8967 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8968 */
8969 if (rdc == NULL) {
8970 mutex_exit(&rp->r_statelock);
8971 return (EINTR);
8972 }
8973
8974 /*
8975 * Check to see if we need to fill this entry in.
8976 */
8977 if (rdc->flags & RDDIRREQ) {
8978 rdc->flags &= ~RDDIRREQ;
8979 rdc->flags |= RDDIR;
8980 mutex_exit(&rp->r_statelock);
8981
8982 /*
8983 * Do the readdir.
8984 */
8985 nfs4readdir(vp, rdc, cr);
8986
8987 /*
8988 * Reacquire the lock, so that we can continue
8989 */
8990 mutex_enter(&rp->r_statelock);
8991 /*
8992 * The entry is now complete
8993 */
8994 rdc->flags &= ~RDDIR;
8995 }
8996
8997 ASSERT(!(rdc->flags & RDDIR));
8998
8999 /*
9000 * If an error occurred while attempting
9001 * to fill the cache entry, mark the entry invalid and
9002 * just return the error.
9003 */
9004 if (rdc->error) {
9005 error = rdc->error;
9006 rdc->flags |= RDDIRREQ;
9007 rddir4_cache_rele(rp, rdc);
9008 mutex_exit(&rp->r_statelock);
9009 return (error);
9010 }
9011
9012 /*
9013 * The cache entry is complete and good,
9014 * copyout the dirent structs to the calling
9015 * thread.
9016 */
9017 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9018
9019 /*
9020 * If no error occurred during the copyout,
9021 * update the offset in the uio struct to
9022 * contain the value of the next NFS 4 cookie
9023 * and set the eof value appropriately.
9024 */
9025 if (!error) {
9026 uiop->uio_loffset = rdc->nfs4_ncookie;
9027 if (eofp)
9028 *eofp = rdc->eof;
9029 }
9030
9031 /*
9032 * Decide whether to do readahead. Don't if we
9033 * have already read to the end of directory.
9034 */
9035 if (rdc->eof) {
9036 /*
9037 * Make the entry the direof only if it is cached
9038 */
9039 if (rdc->flags & RDDIRCACHED)
9040 rp->r_direof = rdc;
9041 rddir4_cache_rele(rp, rdc);
9042 mutex_exit(&rp->r_statelock);
9043 return (error);
9044 }
9045
9046 /* Determine if a readdir readahead should be done */
9047 if (!(rp->r_flags & R4LOOKUP)) {
9048 rddir4_cache_rele(rp, rdc);
9049 mutex_exit(&rp->r_statelock);
9050 return (error);
9051 }
9052
9053 /*
9054 * Now look for a readahead entry.
9055 *
9056 * Check to see whether we found an entry for the readahead.
9057 * If so, we don't need to do anything further, so free the new
9058 * entry if one was allocated. Otherwise, allocate a new entry, add
9059 * it to the cache, and then initiate an asynchronous readdir
9060 * operation to fill it.
9061 */
9062 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9063
9064 /*
9065 * A readdir cache entry could not be obtained for the readahead. In
9066 * this case we skip the readahead and return.
9067 */
9068 if (rrdc == NULL) {
9069 rddir4_cache_rele(rp, rdc);
9070 mutex_exit(&rp->r_statelock);
9071 return (error);
9072 }
9073
9074 /*
9075 * Check to see if we need to fill this entry in.
9076 */
9077 if (rrdc->flags & RDDIRREQ) {
9078 rrdc->flags &= ~RDDIRREQ;
9079 rrdc->flags |= RDDIR;
9080 rddir4_cache_rele(rp, rdc);
9081 mutex_exit(&rp->r_statelock);
9082 #ifdef DEBUG
9083 nfs4_readdir_readahead++;
9084 #endif
9085 /*
9086 * Do the readdir.
9087 */
9088 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9089 return (error);
9090 }
9091
9092 rddir4_cache_rele(rp, rrdc);
9093 rddir4_cache_rele(rp, rdc);
9094 mutex_exit(&rp->r_statelock);
9095 return (error);
9096 }
9097
9098 static int
9099 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9100 {
9101 int error;
9102 rnode4_t *rp;
9103
9104 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9105
9106 rp = VTOR4(vp);
9107
9108 /*
9109 * Obtain the readdir results for the caller.
9110 */
9111 nfs4readdir(vp, rdc, cr);
9112
9113 mutex_enter(&rp->r_statelock);
9114 /*
9115 * The entry is now complete
9116 */
9117 rdc->flags &= ~RDDIR;
9118
9119 error = rdc->error;
9120 if (error)
9121 rdc->flags |= RDDIRREQ;
9122 rddir4_cache_rele(rp, rdc);
9123 mutex_exit(&rp->r_statelock);
9124
9125 return (error);
9126 }
9127
9128 /*
9129 * Read directory entries.
9130 * There are some weird things to look out for here. The uio_loffset
9131 * field is either 0 or it is the offset returned from a previous
9132 * readdir. It is an opaque value used by the server to find the
9133 * correct directory block to read. The count field is the number
9134 * of blocks to read on the server. This is advisory only, the server
9135 * may return only one block's worth of entries. Entries may be compressed
9136 * on the server.
9137 *
9138 * Generates the following compound request:
9139 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9140 * must include a Lookupp as well. In this case, send:
9141 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9142 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9143 *
9144 * Get complete attributes and filehandles for entries if this is the
9145 * first read of the directory. Otherwise, just get fileid's.
9146 */
9147 static void
9148 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9149 {
9150 COMPOUND4args_clnt args;
9151 COMPOUND4res_clnt res;
9152 READDIR4args *rargs;
9153 READDIR4res_clnt *rd_res;
9154 bitmap4 rd_bitsval;
9155 nfs_argop4 argop[5];
9156 nfs_resop4 *resop;
9157 rnode4_t *rp = VTOR4(vp);
9158 mntinfo4_t *mi = VTOMI4(vp);
9159 int doqueue;
9160 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9161 vnode_t *dvp;
9162 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9163 int num_ops, res_opcnt;
9164 bool_t needrecov = FALSE;
9165 nfs4_recov_state_t recov_state;
9166 hrtime_t t;
9167 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9168
9169 ASSERT(nfs_zone() == mi->mi_zone);
9170 ASSERT(rdc->flags & RDDIR);
9171 ASSERT(rdc->entries == NULL);
9172
9173 /*
9174 * If rp were a stub, it should have triggered and caused
9175 * a mount for us to get this far.
9176 */
9177 ASSERT(!RP_ISSTUB(rp));
9178
9179 num_ops = 2;
9180 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9181 /*
9182 * Since nfsv4 readdir may not return entries for "." and "..",
9183 * the client must recreate them:
9184 * To find the correct nodeid, do the following:
9185 * For current node, get nodeid from dnlc.
9186 * - if current node is rootvp, set pnodeid to nodeid.
9187 * - else if parent is in the dnlc, get its nodeid from there.
9188 * - else add LOOKUPP+GETATTR to compound.
9189 */
9190 nodeid = rp->r_attr.va_nodeid;
9191 if (vp->v_flag & VROOT) {
9192 pnodeid = nodeid; /* root of mount point */
9193 } else {
9194 dvp = dnlc_lookup(vp, "..");
9195 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9196 /* parent in dnlc cache - no need for otw */
9197 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9198 } else {
9199 /*
9200 * parent not in dnlc cache,
9201 * do lookupp to get its id
9202 */
9203 num_ops = 5;
9204 pnodeid = 0; /* set later by getattr parent */
9205 }
9206 if (dvp)
9207 VN_RELE(dvp);
9208 }
9209 }
9210 recov_state.rs_flags = 0;
9211 recov_state.rs_num_retry_despite_err = 0;
9212
9213 /* Save the original mount point security flavor */
9214 (void) save_mnt_secinfo(mi->mi_curr_serv);
9215
9216 recov_retry:
9217 args.ctag = TAG_READDIR;
9218
9219 args.array = argop;
9220 args.array_len = num_ops;
9221
9222 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9223 &recov_state, NULL)) {
9224 /*
9225 * If readdir a node that is a stub for a crossed mount point,
9226 * keep the original secinfo flavor for the current file
9227 * system, not the crossed one.
9228 */
9229 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9230 rdc->error = e.error;
9231 return;
9232 }
9233
9234 /*
9235 * Determine which attrs to request for dirents. This code
9236 * must be protected by nfs4_start/end_fop because of r_server
9237 * (which will change during failover recovery).
9238 *
9239 */
9240 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9241 /*
9242 * Get all vattr attrs plus filehandle and rdattr_error
9243 */
9244 rd_bitsval = NFS4_VATTR_MASK |
9245 FATTR4_RDATTR_ERROR_MASK |
9246 FATTR4_FILEHANDLE_MASK;
9247
9248 if (rp->r_flags & R4READDIRWATTR) {
9249 mutex_enter(&rp->r_statelock);
9250 rp->r_flags &= ~R4READDIRWATTR;
9251 mutex_exit(&rp->r_statelock);
9252 }
9253 } else {
9254 servinfo4_t *svp = rp->r_server;
9255
9256 /*
9257 * Already read directory. Use readdir with
9258 * no attrs (except for mounted_on_fileid) for updates.
9259 */
9260 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9261
9262 /*
9263 * request mounted on fileid if supported, else request
9264 * fileid. maybe we should verify that fileid is supported
9265 * and request something else if not.
9266 */
9267 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9268 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9269 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9270 nfs_rw_exit(&svp->sv_lock);
9271 }
9272
9273 /* putfh directory fh */
9274 argop[0].argop = OP_CPUTFH;
9275 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9276
9277 argop[1].argop = OP_READDIR;
9278 rargs = &argop[1].nfs_argop4_u.opreaddir;
9279 /*
9280 * 1 and 2 are reserved for client "." and ".." entry offset.
9281 * cookie 0 should be used over-the-wire to start reading at
9282 * the beginning of the directory excluding "." and "..".
9283 */
9284 if (rdc->nfs4_cookie == 0 ||
9285 rdc->nfs4_cookie == 1 ||
9286 rdc->nfs4_cookie == 2) {
9287 rargs->cookie = (nfs_cookie4)0;
9288 rargs->cookieverf = 0;
9289 } else {
9290 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9291 mutex_enter(&rp->r_statelock);
9292 rargs->cookieverf = rp->r_cookieverf4;
9293 mutex_exit(&rp->r_statelock);
9294 }
9295 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9296 rargs->maxcount = mi->mi_tsize;
9297 rargs->attr_request = rd_bitsval;
9298 rargs->rdc = rdc;
9299 rargs->dvp = vp;
9300 rargs->mi = mi;
9301 rargs->cr = cr;
9302
9303
9304 /*
9305 * If count < than the minimum required, we return no entries
9306 * and fail with EINVAL
9307 */
9308 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9309 rdc->error = EINVAL;
9310 goto out;
9311 }
9312
9313 if (args.array_len == 5) {
9314 /*
9315 * Add lookupp and getattr for parent nodeid.
9316 */
9317 argop[2].argop = OP_LOOKUPP;
9318
9319 argop[3].argop = OP_GETFH;
9320
9321 /* getattr parent */
9322 argop[4].argop = OP_GETATTR;
9323 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9324 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9325 }
9326
9327 doqueue = 1;
9328
9329 if (mi->mi_io_kstats) {
9330 mutex_enter(&mi->mi_lock);
9331 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9332 mutex_exit(&mi->mi_lock);
9333 }
9334
9335 /* capture the time of this call */
9336 rargs->t = t = gethrtime();
9337
9338 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9339
9340 if (mi->mi_io_kstats) {
9341 mutex_enter(&mi->mi_lock);
9342 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9343 mutex_exit(&mi->mi_lock);
9344 }
9345
9346 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9347
9348 /*
9349 * If RPC error occurred and it isn't an error that
9350 * triggers recovery, then go ahead and fail now.
9351 */
9352 if (e.error != 0 && !needrecov) {
9353 rdc->error = e.error;
9354 goto out;
9355 }
9356
9357 if (needrecov) {
9358 bool_t abort;
9359
9360 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9361 "nfs4readdir: initiating recovery.\n"));
9362
9363 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9364 NULL, OP_READDIR, NULL, NULL, NULL);
9365 if (abort == FALSE) {
9366 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9367 &recov_state, needrecov);
9368 if (!e.error)
9369 (void) xdr_free(xdr_COMPOUND4res_clnt,
9370 (caddr_t)&res);
9371 if (rdc->entries != NULL) {
9372 kmem_free(rdc->entries, rdc->entlen);
9373 rdc->entries = NULL;
9374 }
9375 goto recov_retry;
9376 }
9377
9378 if (e.error != 0) {
9379 rdc->error = e.error;
9380 goto out;
9381 }
9382
9383 /* fall through for res.status case */
9384 }
9385
9386 res_opcnt = res.array_len;
9387
9388 /*
9389 * If compound failed first 2 ops (PUTFH+READDIR), then return
9390 * failure here. Subsequent ops are for filling out dot-dot
9391 * dirent, and if they fail, we still want to give the caller
9392 * the dirents returned by (the successful) READDIR op, so we need
9393 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9394 *
9395 * One example where PUTFH+READDIR ops would succeed but
9396 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9397 * but lacks x. In this case, a POSIX server's VOP_READDIR
9398 * would succeed; however, VOP_LOOKUP(..) would fail since no
9399 * x perm. We need to come up with a non-vendor-specific way
9400 * for a POSIX server to return d_ino from dotdot's dirent if
9401 * client only requests mounted_on_fileid, and just say the
9402 * LOOKUPP succeeded and fill out the GETATTR. However, if
9403 * client requested any mandatory attrs, server would be required
9404 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9405 * for dotdot.
9406 */
9407
9408 if (res.status) {
9409 if (res_opcnt <= 2) {
9410 e.error = geterrno4(res.status);
9411 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9412 &recov_state, needrecov);
9413 nfs4_purge_stale_fh(e.error, vp, cr);
9414 rdc->error = e.error;
9415 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9416 if (rdc->entries != NULL) {
9417 kmem_free(rdc->entries, rdc->entlen);
9418 rdc->entries = NULL;
9419 }
9420 /*
9421 * If readdir a node that is a stub for a
9422 * crossed mount point, keep the original
9423 * secinfo flavor for the current file system,
9424 * not the crossed one.
9425 */
9426 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9427 return;
9428 }
9429 }
9430
9431 resop = &res.array[1]; /* readdir res */
9432 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9433
9434 mutex_enter(&rp->r_statelock);
9435 rp->r_cookieverf4 = rd_res->cookieverf;
9436 mutex_exit(&rp->r_statelock);
9437
9438 /*
9439 * For "." and ".." entries
9440 * e.g.
9441 * seek(cookie=0) -> "." entry with d_off = 1
9442 * seek(cookie=1) -> ".." entry with d_off = 2
9443 */
9444 if (cookie == (nfs_cookie4) 0) {
9445 if (rd_res->dotp)
9446 rd_res->dotp->d_ino = nodeid;
9447 if (rd_res->dotdotp)
9448 rd_res->dotdotp->d_ino = pnodeid;
9449 }
9450 if (cookie == (nfs_cookie4) 1) {
9451 if (rd_res->dotdotp)
9452 rd_res->dotdotp->d_ino = pnodeid;
9453 }
9454
9455
9456 /* LOOKUPP+GETATTR attemped */
9457 if (args.array_len == 5 && rd_res->dotdotp) {
9458 if (res.status == NFS4_OK && res_opcnt == 5) {
9459 nfs_fh4 *fhp;
9460 nfs4_sharedfh_t *sfhp;
9461 vnode_t *pvp;
9462 nfs4_ga_res_t *garp;
9463
9464 resop++; /* lookupp */
9465 resop++; /* getfh */
9466 fhp = &resop->nfs_resop4_u.opgetfh.object;
9467
9468 resop++; /* getattr of parent */
9469
9470 /*
9471 * First, take care of finishing the
9472 * readdir results.
9473 */
9474 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9475 /*
9476 * The d_ino of .. must be the inode number
9477 * of the mounted filesystem.
9478 */
9479 if (garp->n4g_va.va_mask & AT_NODEID)
9480 rd_res->dotdotp->d_ino =
9481 garp->n4g_va.va_nodeid;
9482
9483
9484 /*
9485 * Next, create the ".." dnlc entry
9486 */
9487 sfhp = sfh4_get(fhp, mi);
9488 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9489 dnlc_update(vp, "..", pvp);
9490 VN_RELE(pvp);
9491 }
9492 sfh4_rele(&sfhp);
9493 }
9494 }
9495
9496 if (mi->mi_io_kstats) {
9497 mutex_enter(&mi->mi_lock);
9498 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9499 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9500 mutex_exit(&mi->mi_lock);
9501 }
9502
9503 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9504
9505 out:
9506 /*
9507 * If readdir a node that is a stub for a crossed mount point,
9508 * keep the original secinfo flavor for the current file system,
9509 * not the crossed one.
9510 */
9511 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9512
9513 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9514 }
9515
9516
9517 static int
9518 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9519 {
9520 rnode4_t *rp = VTOR4(bp->b_vp);
9521 int count;
9522 int error;
9523 cred_t *cred_otw = NULL;
9524 offset_t offset;
9525 nfs4_open_stream_t *osp = NULL;
9526 bool_t first_time = TRUE; /* first time getting otw cred */
9527 bool_t last_time = FALSE; /* last time getting otw cred */
9528
9529 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9530
9531 DTRACE_IO1(start, struct buf *, bp);
9532 offset = ldbtob(bp->b_lblkno);
9533
9534 if (bp->b_flags & B_READ) {
9535 read_again:
9536 /*
9537 * Releases the osp, if it is provided.
9538 * Puts a hold on the cred_otw and the new osp (if found).
9539 */
9540 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9541 &first_time, &last_time);
9542 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9543 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9544 readahead, NULL);
9545 crfree(cred_otw);
9546 if (!error) {
9547 if (bp->b_resid) {
9548 /*
9549 * Didn't get it all because we hit EOF,
9550 * zero all the memory beyond the EOF.
9551 */
9552 /* bzero(rdaddr + */
9553 bzero(bp->b_un.b_addr +
9554 bp->b_bcount - bp->b_resid, bp->b_resid);
9555 }
9556 mutex_enter(&rp->r_statelock);
9557 if (bp->b_resid == bp->b_bcount &&
9558 offset >= rp->r_size) {
9559 /*
9560 * We didn't read anything at all as we are
9561 * past EOF. Return an error indicator back
9562 * but don't destroy the pages (yet).
9563 */
9564 error = NFS_EOF;
9565 }
9566 mutex_exit(&rp->r_statelock);
9567 } else if (error == EACCES && last_time == FALSE) {
9568 goto read_again;
9569 }
9570 } else {
9571 if (!(rp->r_flags & R4STALE)) {
9572 write_again:
9573 /*
9574 * Releases the osp, if it is provided.
9575 * Puts a hold on the cred_otw and the new
9576 * osp (if found).
9577 */
9578 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9579 &first_time, &last_time);
9580 mutex_enter(&rp->r_statelock);
9581 count = MIN(bp->b_bcount, rp->r_size - offset);
9582 mutex_exit(&rp->r_statelock);
9583 if (count < 0)
9584 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9585 #ifdef DEBUG
9586 if (count == 0) {
9587 zoneid_t zoneid = getzoneid();
9588
9589 zcmn_err(zoneid, CE_WARN,
9590 "nfs4_bio: zero length write at %lld",
9591 offset);
9592 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9593 "b_bcount=%ld, file size=%lld",
9594 rp->r_flags, (long)bp->b_bcount,
9595 rp->r_size);
9596 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9597 if (nfs4_bio_do_stop)
9598 debug_enter("nfs4_bio");
9599 }
9600 #endif
9601 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9602 count, cred_otw, stab_comm);
9603 if (error == EACCES && last_time == FALSE) {
9604 crfree(cred_otw);
9605 goto write_again;
9606 }
9607 bp->b_error = error;
9608 if (error && error != EINTR &&
9609 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9610 /*
9611 * Don't print EDQUOT errors on the console.
9612 * Don't print asynchronous EACCES errors.
9613 * Don't print EFBIG errors.
9614 * Print all other write errors.
9615 */
9616 if (error != EDQUOT && error != EFBIG &&
9617 (error != EACCES ||
9618 !(bp->b_flags & B_ASYNC)))
9619 nfs4_write_error(bp->b_vp,
9620 error, cred_otw);
9621 /*
9622 * Update r_error and r_flags as appropriate.
9623 * If the error was ESTALE, then mark the
9624 * rnode as not being writeable and save
9625 * the error status. Otherwise, save any
9626 * errors which occur from asynchronous
9627 * page invalidations. Any errors occurring
9628 * from other operations should be saved
9629 * by the caller.
9630 */
9631 mutex_enter(&rp->r_statelock);
9632 if (error == ESTALE) {
9633 rp->r_flags |= R4STALE;
9634 if (!rp->r_error)
9635 rp->r_error = error;
9636 } else if (!rp->r_error &&
9637 (bp->b_flags &
9638 (B_INVAL|B_FORCE|B_ASYNC)) ==
9639 (B_INVAL|B_FORCE|B_ASYNC)) {
9640 rp->r_error = error;
9641 }
9642 mutex_exit(&rp->r_statelock);
9643 }
9644 crfree(cred_otw);
9645 } else {
9646 error = rp->r_error;
9647 /*
9648 * A close may have cleared r_error, if so,
9649 * propagate ESTALE error return properly
9650 */
9651 if (error == 0)
9652 error = ESTALE;
9653 }
9654 }
9655
9656 if (error != 0 && error != NFS_EOF)
9657 bp->b_flags |= B_ERROR;
9658
9659 if (osp)
9660 open_stream_rele(osp, rp);
9661
9662 DTRACE_IO1(done, struct buf *, bp);
9663
9664 return (error);
9665 }
9666
9667 /* ARGSUSED */
9668 int
9669 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9670 {
9671 return (EREMOTE);
9672 }
9673
9674 /* ARGSUSED2 */
9675 int
9676 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9677 {
9678 rnode4_t *rp = VTOR4(vp);
9679
9680 if (!write_lock) {
9681 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9682 return (V_WRITELOCK_FALSE);
9683 }
9684
9685 if ((rp->r_flags & R4DIRECTIO) ||
9686 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9687 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9688 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9689 return (V_WRITELOCK_FALSE);
9690 nfs_rw_exit(&rp->r_rwlock);
9691 }
9692
9693 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9694 return (V_WRITELOCK_TRUE);
9695 }
9696
9697 /* ARGSUSED */
9698 void
9699 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9700 {
9701 rnode4_t *rp = VTOR4(vp);
9702
9703 nfs_rw_exit(&rp->r_rwlock);
9704 }
9705
9706 /* ARGSUSED */
9707 static int
9708 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9709 {
9710 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9711 return (EIO);
9712
9713 /*
9714 * Because we stuff the readdir cookie into the offset field
9715 * someone may attempt to do an lseek with the cookie which
9716 * we want to succeed.
9717 */
9718 if (vp->v_type == VDIR)
9719 return (0);
9720 if (*noffp < 0)
9721 return (EINVAL);
9722 return (0);
9723 }
9724
9725
9726 /*
9727 * Return all the pages from [off..off+len) in file
9728 */
9729 /* ARGSUSED */
9730 static int
9731 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9732 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9733 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9734 {
9735 rnode4_t *rp;
9736 int error;
9737 mntinfo4_t *mi;
9738
9739 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9740 return (EIO);
9741 rp = VTOR4(vp);
9742 if (IS_SHADOW(vp, rp))
9743 vp = RTOV4(rp);
9744
9745 if (vp->v_flag & VNOMAP)
9746 return (ENOSYS);
9747
9748 if (protp != NULL)
9749 *protp = PROT_ALL;
9750
9751 /*
9752 * Now validate that the caches are up to date.
9753 */
9754 if (error = nfs4_validate_caches(vp, cr))
9755 return (error);
9756
9757 mi = VTOMI4(vp);
9758 retry:
9759 mutex_enter(&rp->r_statelock);
9760
9761 /*
9762 * Don't create dirty pages faster than they
9763 * can be cleaned so that the system doesn't
9764 * get imbalanced. If the async queue is
9765 * maxed out, then wait for it to drain before
9766 * creating more dirty pages. Also, wait for
9767 * any threads doing pagewalks in the vop_getattr
9768 * entry points so that they don't block for
9769 * long periods.
9770 */
9771 if (rw == S_CREATE) {
9772 while ((mi->mi_max_threads != 0 &&
9773 rp->r_awcount > 2 * mi->mi_max_threads) ||
9774 rp->r_gcount > 0)
9775 cv_wait(&rp->r_cv, &rp->r_statelock);
9776 }
9777
9778 /*
9779 * If we are getting called as a side effect of an nfs_write()
9780 * operation the local file size might not be extended yet.
9781 * In this case we want to be able to return pages of zeroes.
9782 */
9783 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9784 NFS4_DEBUG(nfs4_pageio_debug,
9785 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9786 "len=%llu, size=%llu, attrsize =%llu", off,
9787 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9788 mutex_exit(&rp->r_statelock);
9789 return (EFAULT); /* beyond EOF */
9790 }
9791
9792 mutex_exit(&rp->r_statelock);
9793
9794 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9795 pl, plsz, seg, addr, rw, cr);
9796 NFS4_DEBUG(nfs4_pageio_debug && error,
9797 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9798 error, off, (u_longlong_t)len));
9799
9800 switch (error) {
9801 case NFS_EOF:
9802 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9803 goto retry;
9804 case ESTALE:
9805 nfs4_purge_stale_fh(error, vp, cr);
9806 }
9807
9808 return (error);
9809 }
9810
9811 /*
9812 * Called from pvn_getpages to get a particular page.
9813 */
9814 /* ARGSUSED */
9815 static int
9816 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9817 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9818 enum seg_rw rw, cred_t *cr)
9819 {
9820 rnode4_t *rp;
9821 uint_t bsize;
9822 struct buf *bp;
9823 page_t *pp;
9824 u_offset_t lbn;
9825 u_offset_t io_off;
9826 u_offset_t blkoff;
9827 u_offset_t rablkoff;
9828 size_t io_len;
9829 uint_t blksize;
9830 int error;
9831 int readahead;
9832 int readahead_issued = 0;
9833 int ra_window; /* readahead window */
9834 page_t *pagefound;
9835 page_t *savepp;
9836
9837 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9838 return (EIO);
9839
9840 rp = VTOR4(vp);
9841 ASSERT(!IS_SHADOW(vp, rp));
9842 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9843
9844 reread:
9845 bp = NULL;
9846 pp = NULL;
9847 pagefound = NULL;
9848
9849 if (pl != NULL)
9850 pl[0] = NULL;
9851
9852 error = 0;
9853 lbn = off / bsize;
9854 blkoff = lbn * bsize;
9855
9856 /*
9857 * Queueing up the readahead before doing the synchronous read
9858 * results in a significant increase in read throughput because
9859 * of the increased parallelism between the async threads and
9860 * the process context.
9861 */
9862 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9863 rw != S_CREATE &&
9864 !(vp->v_flag & VNOCACHE)) {
9865 mutex_enter(&rp->r_statelock);
9866
9867 /*
9868 * Calculate the number of readaheads to do.
9869 * a) No readaheads at offset = 0.
9870 * b) Do maximum(nfs4_nra) readaheads when the readahead
9871 * window is closed.
9872 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9873 * upon how far the readahead window is open or close.
9874 * d) No readaheads if rp->r_nextr is not within the scope
9875 * of the readahead window (random i/o).
9876 */
9877
9878 if (off == 0)
9879 readahead = 0;
9880 else if (blkoff == rp->r_nextr)
9881 readahead = nfs4_nra;
9882 else if (rp->r_nextr > blkoff &&
9883 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9884 <= (nfs4_nra - 1)))
9885 readahead = nfs4_nra - ra_window;
9886 else
9887 readahead = 0;
9888
9889 rablkoff = rp->r_nextr;
9890 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9891 mutex_exit(&rp->r_statelock);
9892 if (nfs4_async_readahead(vp, rablkoff + bsize,
9893 addr + (rablkoff + bsize - off),
9894 seg, cr, nfs4_readahead) < 0) {
9895 mutex_enter(&rp->r_statelock);
9896 break;
9897 }
9898 readahead--;
9899 rablkoff += bsize;
9900 /*
9901 * Indicate that we did a readahead so
9902 * readahead offset is not updated
9903 * by the synchronous read below.
9904 */
9905 readahead_issued = 1;
9906 mutex_enter(&rp->r_statelock);
9907 /*
9908 * set readahead offset to
9909 * offset of last async readahead
9910 * request.
9911 */
9912 rp->r_nextr = rablkoff;
9913 }
9914 mutex_exit(&rp->r_statelock);
9915 }
9916
9917 again:
9918 if ((pagefound = page_exists(vp, off)) == NULL) {
9919 if (pl == NULL) {
9920 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9921 nfs4_readahead);
9922 } else if (rw == S_CREATE) {
9923 /*
9924 * Block for this page is not allocated, or the offset
9925 * is beyond the current allocation size, or we're
9926 * allocating a swap slot and the page was not found,
9927 * so allocate it and return a zero page.
9928 */
9929 if ((pp = page_create_va(vp, off,
9930 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9931 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9932 io_len = PAGESIZE;
9933 mutex_enter(&rp->r_statelock);
9934 rp->r_nextr = off + PAGESIZE;
9935 mutex_exit(&rp->r_statelock);
9936 } else {
9937 /*
9938 * Need to go to server to get a block
9939 */
9940 mutex_enter(&rp->r_statelock);
9941 if (blkoff < rp->r_size &&
9942 blkoff + bsize > rp->r_size) {
9943 /*
9944 * If less than a block left in
9945 * file read less than a block.
9946 */
9947 if (rp->r_size <= off) {
9948 /*
9949 * Trying to access beyond EOF,
9950 * set up to get at least one page.
9951 */
9952 blksize = off + PAGESIZE - blkoff;
9953 } else
9954 blksize = rp->r_size - blkoff;
9955 } else if ((off == 0) ||
9956 (off != rp->r_nextr && !readahead_issued)) {
9957 blksize = PAGESIZE;
9958 blkoff = off; /* block = page here */
9959 } else
9960 blksize = bsize;
9961 mutex_exit(&rp->r_statelock);
9962
9963 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9964 &io_len, blkoff, blksize, 0);
9965
9966 /*
9967 * Some other thread has entered the page,
9968 * so just use it.
9969 */
9970 if (pp == NULL)
9971 goto again;
9972
9973 /*
9974 * Now round the request size up to page boundaries.
9975 * This ensures that the entire page will be
9976 * initialized to zeroes if EOF is encountered.
9977 */
9978 io_len = ptob(btopr(io_len));
9979
9980 bp = pageio_setup(pp, io_len, vp, B_READ);
9981 ASSERT(bp != NULL);
9982
9983 /*
9984 * pageio_setup should have set b_addr to 0. This
9985 * is correct since we want to do I/O on a page
9986 * boundary. bp_mapin will use this addr to calculate
9987 * an offset, and then set b_addr to the kernel virtual
9988 * address it allocated for us.
9989 */
9990 ASSERT(bp->b_un.b_addr == 0);
9991
9992 bp->b_edev = 0;
9993 bp->b_dev = 0;
9994 bp->b_lblkno = lbtodb(io_off);
9995 bp->b_file = vp;
9996 bp->b_offset = (offset_t)off;
9997 bp_mapin(bp);
9998
9999 /*
10000 * If doing a write beyond what we believe is EOF,
10001 * don't bother trying to read the pages from the
10002 * server, we'll just zero the pages here. We
10003 * don't check that the rw flag is S_WRITE here
10004 * because some implementations may attempt a
10005 * read access to the buffer before copying data.
10006 */
10007 mutex_enter(&rp->r_statelock);
10008 if (io_off >= rp->r_size && seg == segkmap) {
10009 mutex_exit(&rp->r_statelock);
10010 bzero(bp->b_un.b_addr, io_len);
10011 } else {
10012 mutex_exit(&rp->r_statelock);
10013 error = nfs4_bio(bp, NULL, cr, FALSE);
10014 }
10015
10016 /*
10017 * Unmap the buffer before freeing it.
10018 */
10019 bp_mapout(bp);
10020 pageio_done(bp);
10021
10022 savepp = pp;
10023 do {
10024 pp->p_fsdata = C_NOCOMMIT;
10025 } while ((pp = pp->p_next) != savepp);
10026
10027 if (error == NFS_EOF) {
10028 /*
10029 * If doing a write system call just return
10030 * zeroed pages, else user tried to get pages
10031 * beyond EOF, return error. We don't check
10032 * that the rw flag is S_WRITE here because
10033 * some implementations may attempt a read
10034 * access to the buffer before copying data.
10035 */
10036 if (seg == segkmap)
10037 error = 0;
10038 else
10039 error = EFAULT;
10040 }
10041
10042 if (!readahead_issued && !error) {
10043 mutex_enter(&rp->r_statelock);
10044 rp->r_nextr = io_off + io_len;
10045 mutex_exit(&rp->r_statelock);
10046 }
10047 }
10048 }
10049
10050 out:
10051 if (pl == NULL)
10052 return (error);
10053
10054 if (error) {
10055 if (pp != NULL)
10056 pvn_read_done(pp, B_ERROR);
10057 return (error);
10058 }
10059
10060 if (pagefound) {
10061 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10062
10063 /*
10064 * Page exists in the cache, acquire the appropriate lock.
10065 * If this fails, start all over again.
10066 */
10067 if ((pp = page_lookup(vp, off, se)) == NULL) {
10068 #ifdef DEBUG
10069 nfs4_lostpage++;
10070 #endif
10071 goto reread;
10072 }
10073 pl[0] = pp;
10074 pl[1] = NULL;
10075 return (0);
10076 }
10077
10078 if (pp != NULL)
10079 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10080
10081 return (error);
10082 }
10083
10084 static void
10085 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10086 cred_t *cr)
10087 {
10088 int error;
10089 page_t *pp;
10090 u_offset_t io_off;
10091 size_t io_len;
10092 struct buf *bp;
10093 uint_t bsize, blksize;
10094 rnode4_t *rp = VTOR4(vp);
10095 page_t *savepp;
10096
10097 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10098
10099 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10100
10101 mutex_enter(&rp->r_statelock);
10102 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10103 /*
10104 * If less than a block left in file read less
10105 * than a block.
10106 */
10107 blksize = rp->r_size - blkoff;
10108 } else
10109 blksize = bsize;
10110 mutex_exit(&rp->r_statelock);
10111
10112 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10113 &io_off, &io_len, blkoff, blksize, 1);
10114 /*
10115 * The isra flag passed to the kluster function is 1, we may have
10116 * gotten a return value of NULL for a variety of reasons (# of free
10117 * pages < minfree, someone entered the page on the vnode etc). In all
10118 * cases, we want to punt on the readahead.
10119 */
10120 if (pp == NULL)
10121 return;
10122
10123 /*
10124 * Now round the request size up to page boundaries.
10125 * This ensures that the entire page will be
10126 * initialized to zeroes if EOF is encountered.
10127 */
10128 io_len = ptob(btopr(io_len));
10129
10130 bp = pageio_setup(pp, io_len, vp, B_READ);
10131 ASSERT(bp != NULL);
10132
10133 /*
10134 * pageio_setup should have set b_addr to 0. This is correct since
10135 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10136 * to calculate an offset, and then set b_addr to the kernel virtual
10137 * address it allocated for us.
10138 */
10139 ASSERT(bp->b_un.b_addr == 0);
10140
10141 bp->b_edev = 0;
10142 bp->b_dev = 0;
10143 bp->b_lblkno = lbtodb(io_off);
10144 bp->b_file = vp;
10145 bp->b_offset = (offset_t)blkoff;
10146 bp_mapin(bp);
10147
10148 /*
10149 * If doing a write beyond what we believe is EOF, don't bother trying
10150 * to read the pages from the server, we'll just zero the pages here.
10151 * We don't check that the rw flag is S_WRITE here because some
10152 * implementations may attempt a read access to the buffer before
10153 * copying data.
10154 */
10155 mutex_enter(&rp->r_statelock);
10156 if (io_off >= rp->r_size && seg == segkmap) {
10157 mutex_exit(&rp->r_statelock);
10158 bzero(bp->b_un.b_addr, io_len);
10159 error = 0;
10160 } else {
10161 mutex_exit(&rp->r_statelock);
10162 error = nfs4_bio(bp, NULL, cr, TRUE);
10163 if (error == NFS_EOF)
10164 error = 0;
10165 }
10166
10167 /*
10168 * Unmap the buffer before freeing it.
10169 */
10170 bp_mapout(bp);
10171 pageio_done(bp);
10172
10173 savepp = pp;
10174 do {
10175 pp->p_fsdata = C_NOCOMMIT;
10176 } while ((pp = pp->p_next) != savepp);
10177
10178 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10179
10180 /*
10181 * In case of error set readahead offset
10182 * to the lowest offset.
10183 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10184 */
10185 if (error && rp->r_nextr > io_off) {
10186 mutex_enter(&rp->r_statelock);
10187 if (rp->r_nextr > io_off)
10188 rp->r_nextr = io_off;
10189 mutex_exit(&rp->r_statelock);
10190 }
10191 }
10192
10193 /*
10194 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10195 * If len == 0, do from off to EOF.
10196 *
10197 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10198 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10199 * (from pageout).
10200 */
10201 /* ARGSUSED */
10202 static int
10203 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10204 caller_context_t *ct)
10205 {
10206 int error;
10207 rnode4_t *rp;
10208
10209 ASSERT(cr != NULL);
10210
10211 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10212 return (EIO);
10213
10214 rp = VTOR4(vp);
10215 if (IS_SHADOW(vp, rp))
10216 vp = RTOV4(rp);
10217
10218 /*
10219 * XXX - Why should this check be made here?
10220 */
10221 if (vp->v_flag & VNOMAP)
10222 return (ENOSYS);
10223
10224 if (len == 0 && !(flags & B_INVAL) &&
10225 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10226 return (0);
10227
10228 mutex_enter(&rp->r_statelock);
10229 rp->r_count++;
10230 mutex_exit(&rp->r_statelock);
10231 error = nfs4_putpages(vp, off, len, flags, cr);
10232 mutex_enter(&rp->r_statelock);
10233 rp->r_count--;
10234 cv_broadcast(&rp->r_cv);
10235 mutex_exit(&rp->r_statelock);
10236
10237 return (error);
10238 }
10239
10240 /*
10241 * Write out a single page, possibly klustering adjacent dirty pages.
10242 */
10243 int
10244 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10245 int flags, cred_t *cr)
10246 {
10247 u_offset_t io_off;
10248 u_offset_t lbn_off;
10249 u_offset_t lbn;
10250 size_t io_len;
10251 uint_t bsize;
10252 int error;
10253 rnode4_t *rp;
10254
10255 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10256 ASSERT(pp != NULL);
10257 ASSERT(cr != NULL);
10258 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10259
10260 rp = VTOR4(vp);
10261 ASSERT(rp->r_count > 0);
10262 ASSERT(!IS_SHADOW(vp, rp));
10263
10264 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10265 lbn = pp->p_offset / bsize;
10266 lbn_off = lbn * bsize;
10267
10268 /*
10269 * Find a kluster that fits in one block, or in
10270 * one page if pages are bigger than blocks. If
10271 * there is less file space allocated than a whole
10272 * page, we'll shorten the i/o request below.
10273 */
10274 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10275 roundup(bsize, PAGESIZE), flags);
10276
10277 /*
10278 * pvn_write_kluster shouldn't have returned a page with offset
10279 * behind the original page we were given. Verify that.
10280 */
10281 ASSERT((pp->p_offset / bsize) >= lbn);
10282
10283 /*
10284 * Now pp will have the list of kept dirty pages marked for
10285 * write back. It will also handle invalidation and freeing
10286 * of pages that are not dirty. Check for page length rounding
10287 * problems.
10288 */
10289 if (io_off + io_len > lbn_off + bsize) {
10290 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10291 io_len = lbn_off + bsize - io_off;
10292 }
10293 /*
10294 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10295 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10296 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10297 * progress and the r_size has not been made consistent with the
10298 * new size of the file. When the uiomove() completes the r_size is
10299 * updated and the R4MODINPROGRESS flag is cleared.
10300 *
10301 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10302 * consistent value of r_size. Without this handshaking, it is
10303 * possible that nfs4_bio() picks up the old value of r_size
10304 * before the uiomove() in writerp4() completes. This will result
10305 * in the write through nfs4_bio() being dropped.
10306 *
10307 * More precisely, there is a window between the time the uiomove()
10308 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10309 * operation intervenes in this window, the page will be picked up,
10310 * because it is dirty (it will be unlocked, unless it was
10311 * pagecreate'd). When the page is picked up as dirty, the dirty
10312 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10313 * checked. This will still be the old size. Therefore the page will
10314 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10315 * the page will be found to be clean and the write will be dropped.
10316 */
10317 if (rp->r_flags & R4MODINPROGRESS) {
10318 mutex_enter(&rp->r_statelock);
10319 if ((rp->r_flags & R4MODINPROGRESS) &&
10320 rp->r_modaddr + MAXBSIZE > io_off &&
10321 rp->r_modaddr < io_off + io_len) {
10322 page_t *plist;
10323 /*
10324 * A write is in progress for this region of the file.
10325 * If we did not detect R4MODINPROGRESS here then this
10326 * path through nfs_putapage() would eventually go to
10327 * nfs4_bio() and may not write out all of the data
10328 * in the pages. We end up losing data. So we decide
10329 * to set the modified bit on each page in the page
10330 * list and mark the rnode with R4DIRTY. This write
10331 * will be restarted at some later time.
10332 */
10333 plist = pp;
10334 while (plist != NULL) {
10335 pp = plist;
10336 page_sub(&plist, pp);
10337 hat_setmod(pp);
10338 page_io_unlock(pp);
10339 page_unlock(pp);
10340 }
10341 rp->r_flags |= R4DIRTY;
10342 mutex_exit(&rp->r_statelock);
10343 if (offp)
10344 *offp = io_off;
10345 if (lenp)
10346 *lenp = io_len;
10347 return (0);
10348 }
10349 mutex_exit(&rp->r_statelock);
10350 }
10351
10352 if (flags & B_ASYNC) {
10353 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10354 nfs4_sync_putapage);
10355 } else
10356 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10357
10358 if (offp)
10359 *offp = io_off;
10360 if (lenp)
10361 *lenp = io_len;
10362 return (error);
10363 }
10364
10365 static int
10366 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10367 int flags, cred_t *cr)
10368 {
10369 int error;
10370 rnode4_t *rp;
10371
10372 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10373
10374 flags |= B_WRITE;
10375
10376 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10377
10378 rp = VTOR4(vp);
10379
10380 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10381 error == EACCES) &&
10382 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10383 if (!(rp->r_flags & R4OUTOFSPACE)) {
10384 mutex_enter(&rp->r_statelock);
10385 rp->r_flags |= R4OUTOFSPACE;
10386 mutex_exit(&rp->r_statelock);
10387 }
10388 flags |= B_ERROR;
10389 pvn_write_done(pp, flags);
10390 /*
10391 * If this was not an async thread, then try again to
10392 * write out the pages, but this time, also destroy
10393 * them whether or not the write is successful. This
10394 * will prevent memory from filling up with these
10395 * pages and destroying them is the only alternative
10396 * if they can't be written out.
10397 *
10398 * Don't do this if this is an async thread because
10399 * when the pages are unlocked in pvn_write_done,
10400 * some other thread could have come along, locked
10401 * them, and queued for an async thread. It would be
10402 * possible for all of the async threads to be tied
10403 * up waiting to lock the pages again and they would
10404 * all already be locked and waiting for an async
10405 * thread to handle them. Deadlock.
10406 */
10407 if (!(flags & B_ASYNC)) {
10408 error = nfs4_putpage(vp, io_off, io_len,
10409 B_INVAL | B_FORCE, cr, NULL);
10410 }
10411 } else {
10412 if (error)
10413 flags |= B_ERROR;
10414 else if (rp->r_flags & R4OUTOFSPACE) {
10415 mutex_enter(&rp->r_statelock);
10416 rp->r_flags &= ~R4OUTOFSPACE;
10417 mutex_exit(&rp->r_statelock);
10418 }
10419 pvn_write_done(pp, flags);
10420 if (freemem < desfree)
10421 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10422 NFS4_WRITE_NOWAIT);
10423 }
10424
10425 return (error);
10426 }
10427
10428 #ifdef DEBUG
10429 int nfs4_force_open_before_mmap = 0;
10430 #endif
10431
10432 /* ARGSUSED */
10433 static int
10434 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10435 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10436 caller_context_t *ct)
10437 {
10438 struct segvn_crargs vn_a;
10439 int error = 0;
10440 rnode4_t *rp = VTOR4(vp);
10441 mntinfo4_t *mi = VTOMI4(vp);
10442
10443 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10444 return (EIO);
10445
10446 if (vp->v_flag & VNOMAP)
10447 return (ENOSYS);
10448
10449 if (off < 0 || (off + len) < 0)
10450 return (ENXIO);
10451
10452 if (vp->v_type != VREG)
10453 return (ENODEV);
10454
10455 /*
10456 * If the file is delegated to the client don't do anything.
10457 * If the file is not delegated, then validate the data cache.
10458 */
10459 mutex_enter(&rp->r_statev4_lock);
10460 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10461 mutex_exit(&rp->r_statev4_lock);
10462 error = nfs4_validate_caches(vp, cr);
10463 if (error)
10464 return (error);
10465 } else {
10466 mutex_exit(&rp->r_statev4_lock);
10467 }
10468
10469 /*
10470 * Check to see if the vnode is currently marked as not cachable.
10471 * This means portions of the file are locked (through VOP_FRLOCK).
10472 * In this case the map request must be refused. We use
10473 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10474 *
10475 * Atomically increment r_inmap after acquiring r_rwlock. The
10476 * idea here is to acquire r_rwlock to block read/write and
10477 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10478 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10479 * and we can prevent the deadlock that would have occurred
10480 * when nfs4_addmap() would have acquired it out of order.
10481 *
10482 * Since we are not protecting r_inmap by any lock, we do not
10483 * hold any lock when we decrement it. We atomically decrement
10484 * r_inmap after we release r_lkserlock.
10485 */
10486
10487 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10488 return (EINTR);
10489 atomic_inc_uint(&rp->r_inmap);
10490 nfs_rw_exit(&rp->r_rwlock);
10491
10492 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10493 atomic_dec_uint(&rp->r_inmap);
10494 return (EINTR);
10495 }
10496
10497
10498 if (vp->v_flag & VNOCACHE) {
10499 error = EAGAIN;
10500 goto done;
10501 }
10502
10503 /*
10504 * Don't allow concurrent locks and mapping if mandatory locking is
10505 * enabled.
10506 */
10507 if (flk_has_remote_locks(vp)) {
10508 struct vattr va;
10509 va.va_mask = AT_MODE;
10510 error = nfs4getattr(vp, &va, cr);
10511 if (error != 0)
10512 goto done;
10513 if (MANDLOCK(vp, va.va_mode)) {
10514 error = EAGAIN;
10515 goto done;
10516 }
10517 }
10518
10519 /*
10520 * It is possible that the rnode has a lost lock request that we
10521 * are still trying to recover, and that the request conflicts with
10522 * this map request.
10523 *
10524 * An alternative approach would be for nfs4_safemap() to consider
10525 * queued lock requests when deciding whether to set or clear
10526 * VNOCACHE. This would require the frlock code path to call
10527 * nfs4_safemap() after enqueing a lost request.
10528 */
10529 if (nfs4_map_lost_lock_conflict(vp)) {
10530 error = EAGAIN;
10531 goto done;
10532 }
10533
10534 as_rangelock(as);
10535 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10536 if (error != 0) {
10537 as_rangeunlock(as);
10538 goto done;
10539 }
10540
10541 if (vp->v_type == VREG) {
10542 /*
10543 * We need to retrieve the open stream
10544 */
10545 nfs4_open_stream_t *osp = NULL;
10546 nfs4_open_owner_t *oop = NULL;
10547
10548 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10549 if (oop != NULL) {
10550 /* returns with 'os_sync_lock' held */
10551 osp = find_open_stream(oop, rp);
10552 open_owner_rele(oop);
10553 }
10554 if (osp == NULL) {
10555 #ifdef DEBUG
10556 if (nfs4_force_open_before_mmap) {
10557 error = EIO;
10558 goto done;
10559 }
10560 #endif
10561 /* returns with 'os_sync_lock' held */
10562 error = open_and_get_osp(vp, cr, &osp);
10563 if (osp == NULL) {
10564 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10565 "nfs4_map: we tried to OPEN the file "
10566 "but again no osp, so fail with EIO"));
10567 goto done;
10568 }
10569 }
10570
10571 if (osp->os_failed_reopen) {
10572 mutex_exit(&osp->os_sync_lock);
10573 open_stream_rele(osp, rp);
10574 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10575 "nfs4_map: os_failed_reopen set on "
10576 "osp %p, cr %p, rp %s", (void *)osp,
10577 (void *)cr, rnode4info(rp)));
10578 error = EIO;
10579 goto done;
10580 }
10581 mutex_exit(&osp->os_sync_lock);
10582 open_stream_rele(osp, rp);
10583 }
10584
10585 vn_a.vp = vp;
10586 vn_a.offset = off;
10587 vn_a.type = (flags & MAP_TYPE);
10588 vn_a.prot = (uchar_t)prot;
10589 vn_a.maxprot = (uchar_t)maxprot;
10590 vn_a.flags = (flags & ~MAP_TYPE);
10591 vn_a.cred = cr;
10592 vn_a.amp = NULL;
10593 vn_a.szc = 0;
10594 vn_a.lgrp_mem_policy_flags = 0;
10595
10596 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10597 as_rangeunlock(as);
10598
10599 done:
10600 nfs_rw_exit(&rp->r_lkserlock);
10601 atomic_dec_uint(&rp->r_inmap);
10602 return (error);
10603 }
10604
10605 /*
10606 * We're most likely dealing with a kernel module that likes to READ
10607 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10608 * officially OPEN the file to create the necessary client state
10609 * for bookkeeping of os_mmap_read/write counts.
10610 *
10611 * Since VOP_MAP only passes in a pointer to the vnode rather than
10612 * a double pointer, we can't handle the case where nfs4open_otw()
10613 * returns a different vnode than the one passed into VOP_MAP (since
10614 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case,
10615 * we return NULL and let nfs4_map() fail. Note: the only case where
10616 * this should happen is if the file got removed and replaced with the
10617 * same name on the server (in addition to the fact that we're trying
10618 * to VOP_MAP withouth VOP_OPENing the file in the first place).
10619 */
10620 static int
10621 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10622 {
10623 rnode4_t *rp, *drp;
10624 vnode_t *dvp, *open_vp;
10625 char file_name[MAXNAMELEN];
10626 int just_created;
10627 nfs4_open_stream_t *osp;
10628 nfs4_open_owner_t *oop;
10629 int error;
10630
10631 *ospp = NULL;
10632 open_vp = map_vp;
10633
10634 rp = VTOR4(open_vp);
10635 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10636 return (error);
10637 drp = VTOR4(dvp);
10638
10639 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10640 VN_RELE(dvp);
10641 return (EINTR);
10642 }
10643
10644 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10645 nfs_rw_exit(&drp->r_rwlock);
10646 VN_RELE(dvp);
10647 return (error);
10648 }
10649
10650 mutex_enter(&rp->r_statev4_lock);
10651 if (rp->created_v4) {
10652 rp->created_v4 = 0;
10653 mutex_exit(&rp->r_statev4_lock);
10654
10655 dnlc_update(dvp, file_name, open_vp);
10656 /* This is needed so we don't bump the open ref count */
10657 just_created = 1;
10658 } else {
10659 mutex_exit(&rp->r_statev4_lock);
10660 just_created = 0;
10661 }
10662
10663 VN_HOLD(map_vp);
10664
10665 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10666 just_created);
10667 if (error) {
10668 nfs_rw_exit(&drp->r_rwlock);
10669 VN_RELE(dvp);
10670 VN_RELE(map_vp);
10671 return (error);
10672 }
10673
10674 nfs_rw_exit(&drp->r_rwlock);
10675 VN_RELE(dvp);
10676
10677 /*
10678 * If nfs4open_otw() returned a different vnode then "undo"
10679 * the open and return failure to the caller.
10680 */
10681 if (!VN_CMP(open_vp, map_vp)) {
10682 nfs4_error_t e;
10683
10684 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10685 "open returned a different vnode"));
10686 /*
10687 * If there's an error, ignore it,
10688 * and let VOP_INACTIVE handle it.
10689 */
10690 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10691 CLOSE_NORM, 0, 0, 0);
10692 VN_RELE(map_vp);
10693 return (EIO);
10694 }
10695
10696 VN_RELE(map_vp);
10697
10698 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10699 if (!oop) {
10700 nfs4_error_t e;
10701
10702 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10703 "no open owner"));
10704 /*
10705 * If there's an error, ignore it,
10706 * and let VOP_INACTIVE handle it.
10707 */
10708 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10709 CLOSE_NORM, 0, 0, 0);
10710 return (EIO);
10711 }
10712 osp = find_open_stream(oop, rp);
10713 open_owner_rele(oop);
10714 *ospp = osp;
10715 return (0);
10716 }
10717
10718 /*
10719 * Please be aware that when this function is called, the address space write
10720 * a_lock is held. Do not put over the wire calls in this function.
10721 */
10722 /* ARGSUSED */
10723 static int
10724 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10725 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10726 caller_context_t *ct)
10727 {
10728 rnode4_t *rp;
10729 int error = 0;
10730 mntinfo4_t *mi;
10731
10732 mi = VTOMI4(vp);
10733 rp = VTOR4(vp);
10734
10735 if (nfs_zone() != mi->mi_zone)
10736 return (EIO);
10737 if (vp->v_flag & VNOMAP)
10738 return (ENOSYS);
10739
10740 /*
10741 * Don't need to update the open stream first, since this
10742 * mmap can't add any additional share access that isn't
10743 * already contained in the open stream (for the case where we
10744 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10745 * take into account os_mmap_read[write] counts).
10746 */
10747 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10748
10749 if (vp->v_type == VREG) {
10750 /*
10751 * We need to retrieve the open stream and update the counts.
10752 * If there is no open stream here, something is wrong.
10753 */
10754 nfs4_open_stream_t *osp = NULL;
10755 nfs4_open_owner_t *oop = NULL;
10756
10757 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10758 if (oop != NULL) {
10759 /* returns with 'os_sync_lock' held */
10760 osp = find_open_stream(oop, rp);
10761 open_owner_rele(oop);
10762 }
10763 if (osp == NULL) {
10764 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10765 "nfs4_addmap: we should have an osp"
10766 "but we don't, so fail with EIO"));
10767 error = EIO;
10768 goto out;
10769 }
10770
10771 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10772 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10773
10774 /*
10775 * Update the map count in the open stream.
10776 * This is necessary in the case where we
10777 * open/mmap/close/, then the server reboots, and we
10778 * attempt to reopen. If the mmap doesn't add share
10779 * access then we send an invalid reopen with
10780 * access = NONE.
10781 *
10782 * We need to specifically check each PROT_* so a mmap
10783 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10784 * read and write access. A simple comparison of prot
10785 * to ~PROT_WRITE to determine read access is insufficient
10786 * since prot can be |= with PROT_USER, etc.
10787 */
10788
10789 /*
10790 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10791 */
10792 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10793 osp->os_mmap_write += btopr(len);
10794 if (maxprot & PROT_READ)
10795 osp->os_mmap_read += btopr(len);
10796 if (maxprot & PROT_EXEC)
10797 osp->os_mmap_read += btopr(len);
10798 /*
10799 * Ensure that os_mmap_read gets incremented, even if
10800 * maxprot were to look like PROT_NONE.
10801 */
10802 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10803 !(maxprot & PROT_EXEC))
10804 osp->os_mmap_read += btopr(len);
10805 osp->os_mapcnt += btopr(len);
10806 mutex_exit(&osp->os_sync_lock);
10807 open_stream_rele(osp, rp);
10808 }
10809
10810 out:
10811 /*
10812 * If we got an error, then undo our
10813 * incrementing of 'r_mapcnt'.
10814 */
10815
10816 if (error) {
10817 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10818 ASSERT(rp->r_mapcnt >= 0);
10819 }
10820 return (error);
10821 }
10822
10823 /* ARGSUSED */
10824 static int
10825 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10826 {
10827
10828 return (VTOR4(vp1) == VTOR4(vp2));
10829 }
10830
10831 /* ARGSUSED */
10832 static int
10833 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10834 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10835 caller_context_t *ct)
10836 {
10837 int rc;
10838 u_offset_t start, end;
10839 rnode4_t *rp;
10840 int error = 0, intr = INTR4(vp);
10841 nfs4_error_t e;
10842
10843 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10844 return (EIO);
10845
10846 /* check for valid cmd parameter */
10847 if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
10848 return (EINVAL);
10849
10850 /* Verify l_type. */
10851 switch (bfp->l_type) {
10852 case F_RDLCK:
10853 if (cmd != F_GETLK && !(flag & FREAD))
10854 return (EBADF);
10855 break;
10856 case F_WRLCK:
10857 if (cmd != F_GETLK && !(flag & FWRITE))
10858 return (EBADF);
10859 break;
10860 case F_UNLCK:
10861 intr = 0;
10862 break;
10863
10864 default:
10865 return (EINVAL);
10866 }
10867
10868 /* check the validity of the lock range */
10869 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10870 return (rc);
10871 if (rc = flk_check_lock_data(start, end, MAXEND))
10872 return (rc);
10873
10874 /*
10875 * If the filesystem is mounted using local locking, pass the
10876 * request off to the local locking code.
10877 */
10878 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10879 if (cmd == F_SETLK || cmd == F_SETLKW) {
10880 /*
10881 * For complete safety, we should be holding
10882 * r_lkserlock. However, we can't call
10883 * nfs4_safelock and then fs_frlock while
10884 * holding r_lkserlock, so just invoke
10885 * nfs4_safelock and expect that this will
10886 * catch enough of the cases.
10887 */
10888 if (!nfs4_safelock(vp, bfp, cr))
10889 return (EAGAIN);
10890 }
10891 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10892 }
10893
10894 rp = VTOR4(vp);
10895
10896 /*
10897 * Check whether the given lock request can proceed, given the
10898 * current file mappings.
10899 */
10900 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10901 return (EINTR);
10902 if (cmd == F_SETLK || cmd == F_SETLKW) {
10903 if (!nfs4_safelock(vp, bfp, cr)) {
10904 rc = EAGAIN;
10905 goto done;
10906 }
10907 }
10908
10909 /*
10910 * Flush the cache after waiting for async I/O to finish. For new
10911 * locks, this is so that the process gets the latest bits from the
10912 * server. For unlocks, this is so that other clients see the
10913 * latest bits once the file has been unlocked. If currently dirty
10914 * pages can't be flushed, then don't allow a lock to be set. But
10915 * allow unlocks to succeed, to avoid having orphan locks on the
10916 * server.
10917 */
10918 if (cmd != F_GETLK) {
10919 mutex_enter(&rp->r_statelock);
10920 while (rp->r_count > 0) {
10921 if (intr) {
10922 klwp_t *lwp = ttolwp(curthread);
10923
10924 if (lwp != NULL)
10925 lwp->lwp_nostop++;
10926 if (cv_wait_sig(&rp->r_cv,
10927 &rp->r_statelock) == 0) {
10928 if (lwp != NULL)
10929 lwp->lwp_nostop--;
10930 rc = EINTR;
10931 break;
10932 }
10933 if (lwp != NULL)
10934 lwp->lwp_nostop--;
10935 } else
10936 cv_wait(&rp->r_cv, &rp->r_statelock);
10937 }
10938 mutex_exit(&rp->r_statelock);
10939 if (rc != 0)
10940 goto done;
10941 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10942 if (error) {
10943 if (error == ENOSPC || error == EDQUOT) {
10944 mutex_enter(&rp->r_statelock);
10945 if (!rp->r_error)
10946 rp->r_error = error;
10947 mutex_exit(&rp->r_statelock);
10948 }
10949 if (bfp->l_type != F_UNLCK) {
10950 rc = ENOLCK;
10951 goto done;
10952 }
10953 }
10954 }
10955
10956 /*
10957 * Call the lock manager to do the real work of contacting
10958 * the server and obtaining the lock.
10959 */
10960 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10961 cr, &e, NULL, NULL);
10962 rc = e.error;
10963
10964 if (rc == 0)
10965 nfs4_lockcompletion(vp, cmd);
10966
10967 done:
10968 nfs_rw_exit(&rp->r_lkserlock);
10969
10970 return (rc);
10971 }
10972
10973 /*
10974 * Free storage space associated with the specified vnode. The portion
10975 * to be freed is specified by bfp->l_start and bfp->l_len (already
10976 * normalized to a "whence" of 0).
10977 *
10978 * This is an experimental facility whose continued existence is not
10979 * guaranteed. Currently, we only support the special case
10980 * of l_len == 0, meaning free to end of file.
10981 */
10982 /* ARGSUSED */
10983 static int
10984 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10985 offset_t offset, cred_t *cr, caller_context_t *ct)
10986 {
10987 int error;
10988
10989 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10990 return (EIO);
10991 ASSERT(vp->v_type == VREG);
10992 if (cmd != F_FREESP)
10993 return (EINVAL);
10994
10995 error = convoff(vp, bfp, 0, offset);
10996 if (!error) {
10997 ASSERT(bfp->l_start >= 0);
10998 if (bfp->l_len == 0) {
10999 struct vattr va;
11000
11001 va.va_mask = AT_SIZE;
11002 va.va_size = bfp->l_start;
11003 error = nfs4setattr(vp, &va, 0, cr, NULL);
11004
11005 if (error == 0 && bfp->l_start == 0)
11006 vnevent_truncate(vp, ct);
11007 } else
11008 error = EINVAL;
11009 }
11010
11011 return (error);
11012 }
11013
11014 /* ARGSUSED */
11015 int
11016 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11017 {
11018 rnode4_t *rp;
11019 rp = VTOR4(vp);
11020
11021 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11022 vp = RTOV4(rp);
11023 }
11024 *vpp = vp;
11025 return (0);
11026 }
11027
11028 /*
11029 * Setup and add an address space callback to do the work of the delmap call.
11030 * The callback will (and must be) deleted in the actual callback function.
11031 *
11032 * This is done in order to take care of the problem that we have with holding
11033 * the address space's a_lock for a long period of time (e.g. if the NFS server
11034 * is down). Callbacks will be executed in the address space code while the
11035 * a_lock is not held. Holding the address space's a_lock causes things such
11036 * as ps and fork to hang because they are trying to acquire this lock as well.
11037 */
11038 /* ARGSUSED */
11039 static int
11040 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11041 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11042 caller_context_t *ct)
11043 {
11044 int caller_found;
11045 int error;
11046 rnode4_t *rp;
11047 nfs4_delmap_args_t *dmapp;
11048 nfs4_delmapcall_t *delmap_call;
11049
11050 if (vp->v_flag & VNOMAP)
11051 return (ENOSYS);
11052
11053 /*
11054 * A process may not change zones if it has NFS pages mmap'ed
11055 * in, so we can't legitimately get here from the wrong zone.
11056 */
11057 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11058
11059 rp = VTOR4(vp);
11060
11061 /*
11062 * The way that the address space of this process deletes its mapping
11063 * of this file is via the following call chains:
11064 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11065 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11066 *
11067 * With the use of address space callbacks we are allowed to drop the
11068 * address space lock, a_lock, while executing the NFS operations that
11069 * need to go over the wire. Returning EAGAIN to the caller of this
11070 * function is what drives the execution of the callback that we add
11071 * below. The callback will be executed by the address space code
11072 * after dropping the a_lock. When the callback is finished, since
11073 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11074 * is called again on the same segment to finish the rest of the work
11075 * that needs to happen during unmapping.
11076 *
11077 * This action of calling back into the segment driver causes
11078 * nfs4_delmap() to get called again, but since the callback was
11079 * already executed at this point, it already did the work and there
11080 * is nothing left for us to do.
11081 *
11082 * To Summarize:
11083 * - The first time nfs4_delmap is called by the current thread is when
11084 * we add the caller associated with this delmap to the delmap caller
11085 * list, add the callback, and return EAGAIN.
11086 * - The second time in this call chain when nfs4_delmap is called we
11087 * will find this caller in the delmap caller list and realize there
11088 * is no more work to do thus removing this caller from the list and
11089 * returning the error that was set in the callback execution.
11090 */
11091 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11092 if (caller_found) {
11093 /*
11094 * 'error' is from the actual delmap operations. To avoid
11095 * hangs, we need to handle the return of EAGAIN differently
11096 * since this is what drives the callback execution.
11097 * In this case, we don't want to return EAGAIN and do the
11098 * callback execution because there are none to execute.
11099 */
11100 if (error == EAGAIN)
11101 return (0);
11102 else
11103 return (error);
11104 }
11105
11106 /* current caller was not in the list */
11107 delmap_call = nfs4_init_delmapcall();
11108
11109 mutex_enter(&rp->r_statelock);
11110 list_insert_tail(&rp->r_indelmap, delmap_call);
11111 mutex_exit(&rp->r_statelock);
11112
11113 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11114
11115 dmapp->vp = vp;
11116 dmapp->off = off;
11117 dmapp->addr = addr;
11118 dmapp->len = len;
11119 dmapp->prot = prot;
11120 dmapp->maxprot = maxprot;
11121 dmapp->flags = flags;
11122 dmapp->cr = cr;
11123 dmapp->caller = delmap_call;
11124
11125 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11126 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11127
11128 return (error ? error : EAGAIN);
11129 }
11130
11131 static nfs4_delmapcall_t *
11132 nfs4_init_delmapcall()
11133 {
11134 nfs4_delmapcall_t *delmap_call;
11135
11136 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11137 delmap_call->call_id = curthread;
11138 delmap_call->error = 0;
11139
11140 return (delmap_call);
11141 }
11142
11143 static void
11144 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11145 {
11146 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11147 }
11148
11149 /*
11150 * Searches for the current delmap caller (based on curthread) in the list of
11151 * callers. If it is found, we remove it and free the delmap caller.
11152 * Returns:
11153 * 0 if the caller wasn't found
11154 * 1 if the caller was found, removed and freed. *errp will be set
11155 * to what the result of the delmap was.
11156 */
11157 static int
11158 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11159 {
11160 nfs4_delmapcall_t *delmap_call;
11161
11162 /*
11163 * If the list doesn't exist yet, we create it and return
11164 * that the caller wasn't found. No list = no callers.
11165 */
11166 mutex_enter(&rp->r_statelock);
11167 if (!(rp->r_flags & R4DELMAPLIST)) {
11168 /* The list does not exist */
11169 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11170 offsetof(nfs4_delmapcall_t, call_node));
11171 rp->r_flags |= R4DELMAPLIST;
11172 mutex_exit(&rp->r_statelock);
11173 return (0);
11174 } else {
11175 /* The list exists so search it */
11176 for (delmap_call = list_head(&rp->r_indelmap);
11177 delmap_call != NULL;
11178 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11179 if (delmap_call->call_id == curthread) {
11180 /* current caller is in the list */
11181 *errp = delmap_call->error;
11182 list_remove(&rp->r_indelmap, delmap_call);
11183 mutex_exit(&rp->r_statelock);
11184 nfs4_free_delmapcall(delmap_call);
11185 return (1);
11186 }
11187 }
11188 }
11189 mutex_exit(&rp->r_statelock);
11190 return (0);
11191 }
11192
11193 /*
11194 * Remove some pages from an mmap'd vnode. Just update the
11195 * count of pages. If doing close-to-open, then flush and
11196 * commit all of the pages associated with this file.
11197 * Otherwise, start an asynchronous page flush to write out
11198 * any dirty pages. This will also associate a credential
11199 * with the rnode which can be used to write the pages.
11200 */
11201 /* ARGSUSED */
11202 static void
11203 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11204 {
11205 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11206 rnode4_t *rp;
11207 mntinfo4_t *mi;
11208 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11209
11210 rp = VTOR4(dmapp->vp);
11211 mi = VTOMI4(dmapp->vp);
11212
11213 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11214 ASSERT(rp->r_mapcnt >= 0);
11215
11216 /*
11217 * Initiate a page flush and potential commit if there are
11218 * pages, the file system was not mounted readonly, the segment
11219 * was mapped shared, and the pages themselves were writeable.
11220 */
11221 if (nfs4_has_pages(dmapp->vp) &&
11222 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11223 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11224 mutex_enter(&rp->r_statelock);
11225 rp->r_flags |= R4DIRTY;
11226 mutex_exit(&rp->r_statelock);
11227 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11228 dmapp->len, dmapp->cr);
11229 if (!e.error) {
11230 mutex_enter(&rp->r_statelock);
11231 e.error = rp->r_error;
11232 rp->r_error = 0;
11233 mutex_exit(&rp->r_statelock);
11234 }
11235 } else
11236 e.error = 0;
11237
11238 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11239 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11240 B_INVAL, dmapp->cr, NULL);
11241
11242 if (e.error) {
11243 e.stat = puterrno4(e.error);
11244 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11245 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11246 dmapp->caller->error = e.error;
11247 }
11248
11249 /* Check to see if we need to close the file */
11250
11251 if (dmapp->vp->v_type == VREG) {
11252 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11253 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11254
11255 if (e.error != 0 || e.stat != NFS4_OK) {
11256 /*
11257 * Since it is possible that e.error == 0 and
11258 * e.stat != NFS4_OK (and vice versa),
11259 * we do the proper checking in order to get both
11260 * e.error and e.stat reporting the correct info.
11261 */
11262 if (e.stat == NFS4_OK)
11263 e.stat = puterrno4(e.error);
11264 if (e.error == 0)
11265 e.error = geterrno4(e.stat);
11266
11267 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11268 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11269 dmapp->caller->error = e.error;
11270 }
11271 }
11272
11273 (void) as_delete_callback(as, arg);
11274 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11275 }
11276
11277
11278 static uint_t
11279 fattr4_maxfilesize_to_bits(uint64_t ll)
11280 {
11281 uint_t l = 1;
11282
11283 if (ll == 0) {
11284 return (0);
11285 }
11286
11287 if (ll & 0xffffffff00000000) {
11288 l += 32; ll >>= 32;
11289 }
11290 if (ll & 0xffff0000) {
11291 l += 16; ll >>= 16;
11292 }
11293 if (ll & 0xff00) {
11294 l += 8; ll >>= 8;
11295 }
11296 if (ll & 0xf0) {
11297 l += 4; ll >>= 4;
11298 }
11299 if (ll & 0xc) {
11300 l += 2; ll >>= 2;
11301 }
11302 if (ll & 0x2) {
11303 l += 1;
11304 }
11305 return (l);
11306 }
11307
11308 static int
11309 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11310 {
11311 vnode_t *avp = NULL;
11312 int error;
11313
11314 if ((error = nfs4lookup_xattr(vp, "", &avp,
11315 LOOKUP_XATTR, cr)) == 0)
11316 error = do_xattr_exists_check(avp, valp, cr);
11317 if (avp)
11318 VN_RELE(avp);
11319
11320 return (error);
11321 }
11322
11323 /* ARGSUSED */
11324 int
11325 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11326 caller_context_t *ct)
11327 {
11328 int error;
11329 hrtime_t t;
11330 rnode4_t *rp;
11331 nfs4_ga_res_t gar;
11332 nfs4_ga_ext_res_t ger;
11333
11334 gar.n4g_ext_res = &ger;
11335
11336 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11337 return (EIO);
11338 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11339 *valp = MAXPATHLEN;
11340 return (0);
11341 }
11342 if (cmd == _PC_ACL_ENABLED) {
11343 *valp = _ACL_ACE_ENABLED;
11344 return (0);
11345 }
11346
11347 rp = VTOR4(vp);
11348 if (cmd == _PC_XATTR_EXISTS) {
11349 /*
11350 * The existence of the xattr directory is not sufficient
11351 * for determining whether generic user attributes exists.
11352 * The attribute directory could only be a transient directory
11353 * used for Solaris sysattr support. Do a small readdir
11354 * to verify if the only entries are sysattrs or not.
11355 *
11356 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11357 * is NULL. Once the xadir vp exists, we can create xattrs,
11358 * and we don't have any way to update the "base" object's
11359 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11360 * could help out.
11361 */
11362 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11363 rp->r_xattr_dir == NULL) {
11364 return (nfs4_have_xattrs(vp, valp, cr));
11365 }
11366 } else { /* OLD CODE */
11367 if (ATTRCACHE4_VALID(vp)) {
11368 mutex_enter(&rp->r_statelock);
11369 if (rp->r_pathconf.pc4_cache_valid) {
11370 error = 0;
11371 switch (cmd) {
11372 case _PC_FILESIZEBITS:
11373 *valp =
11374 rp->r_pathconf.pc4_filesizebits;
11375 break;
11376 case _PC_LINK_MAX:
11377 *valp =
11378 rp->r_pathconf.pc4_link_max;
11379 break;
11380 case _PC_NAME_MAX:
11381 *valp =
11382 rp->r_pathconf.pc4_name_max;
11383 break;
11384 case _PC_CHOWN_RESTRICTED:
11385 *valp =
11386 rp->r_pathconf.pc4_chown_restricted;
11387 break;
11388 case _PC_NO_TRUNC:
11389 *valp =
11390 rp->r_pathconf.pc4_no_trunc;
11391 break;
11392 default:
11393 error = EINVAL;
11394 break;
11395 }
11396 mutex_exit(&rp->r_statelock);
11397 #ifdef DEBUG
11398 nfs4_pathconf_cache_hits++;
11399 #endif
11400 return (error);
11401 }
11402 mutex_exit(&rp->r_statelock);
11403 }
11404 }
11405 #ifdef DEBUG
11406 nfs4_pathconf_cache_misses++;
11407 #endif
11408
11409 t = gethrtime();
11410
11411 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11412
11413 if (error) {
11414 mutex_enter(&rp->r_statelock);
11415 rp->r_pathconf.pc4_cache_valid = FALSE;
11416 rp->r_pathconf.pc4_xattr_valid = FALSE;
11417 mutex_exit(&rp->r_statelock);
11418 return (error);
11419 }
11420
11421 /* interpret the max filesize */
11422 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11423 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11424
11425 /* Store the attributes we just received */
11426 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11427
11428 switch (cmd) {
11429 case _PC_FILESIZEBITS:
11430 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11431 break;
11432 case _PC_LINK_MAX:
11433 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11434 break;
11435 case _PC_NAME_MAX:
11436 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11437 break;
11438 case _PC_CHOWN_RESTRICTED:
11439 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11440 break;
11441 case _PC_NO_TRUNC:
11442 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11443 break;
11444 case _PC_XATTR_EXISTS:
11445 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11446 if (error = nfs4_have_xattrs(vp, valp, cr))
11447 return (error);
11448 }
11449 break;
11450 default:
11451 return (EINVAL);
11452 }
11453
11454 return (0);
11455 }
11456
11457 /*
11458 * Called by async thread to do synchronous pageio. Do the i/o, wait
11459 * for it to complete, and cleanup the page list when done.
11460 */
11461 static int
11462 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11463 int flags, cred_t *cr)
11464 {
11465 int error;
11466
11467 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11468
11469 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11470 if (flags & B_READ)
11471 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11472 else
11473 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11474 return (error);
11475 }
11476
11477 /* ARGSUSED */
11478 static int
11479 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11480 int flags, cred_t *cr, caller_context_t *ct)
11481 {
11482 int error;
11483 rnode4_t *rp;
11484
11485 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11486 return (EIO);
11487
11488 if (pp == NULL)
11489 return (EINVAL);
11490
11491 rp = VTOR4(vp);
11492 mutex_enter(&rp->r_statelock);
11493 rp->r_count++;
11494 mutex_exit(&rp->r_statelock);
11495
11496 if (flags & B_ASYNC) {
11497 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11498 nfs4_sync_pageio);
11499 } else
11500 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11501 mutex_enter(&rp->r_statelock);
11502 rp->r_count--;
11503 cv_broadcast(&rp->r_cv);
11504 mutex_exit(&rp->r_statelock);
11505 return (error);
11506 }
11507
11508 /* ARGSUSED */
11509 static void
11510 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11511 caller_context_t *ct)
11512 {
11513 int error;
11514 rnode4_t *rp;
11515 page_t *plist;
11516 page_t *pptr;
11517 offset3 offset;
11518 count3 len;
11519 k_sigset_t smask;
11520
11521 /*
11522 * We should get called with fl equal to either B_FREE or
11523 * B_INVAL. Any other value is illegal.
11524 *
11525 * The page that we are either supposed to free or destroy
11526 * should be exclusive locked and its io lock should not
11527 * be held.
11528 */
11529 ASSERT(fl == B_FREE || fl == B_INVAL);
11530 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11531
11532 rp = VTOR4(vp);
11533
11534 /*
11535 * If the page doesn't need to be committed or we shouldn't
11536 * even bother attempting to commit it, then just make sure
11537 * that the p_fsdata byte is clear and then either free or
11538 * destroy the page as appropriate.
11539 */
11540 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11541 pp->p_fsdata = C_NOCOMMIT;
11542 if (fl == B_FREE)
11543 page_free(pp, dn);
11544 else
11545 page_destroy(pp, dn);
11546 return;
11547 }
11548
11549 /*
11550 * If there is a page invalidation operation going on, then
11551 * if this is one of the pages being destroyed, then just
11552 * clear the p_fsdata byte and then either free or destroy
11553 * the page as appropriate.
11554 */
11555 mutex_enter(&rp->r_statelock);
11556 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11557 mutex_exit(&rp->r_statelock);
11558 pp->p_fsdata = C_NOCOMMIT;
11559 if (fl == B_FREE)
11560 page_free(pp, dn);
11561 else
11562 page_destroy(pp, dn);
11563 return;
11564 }
11565
11566 /*
11567 * If we are freeing this page and someone else is already
11568 * waiting to do a commit, then just unlock the page and
11569 * return. That other thread will take care of commiting
11570 * this page. The page can be freed sometime after the
11571 * commit has finished. Otherwise, if the page is marked
11572 * as delay commit, then we may be getting called from
11573 * pvn_write_done, one page at a time. This could result
11574 * in one commit per page, so we end up doing lots of small
11575 * commits instead of fewer larger commits. This is bad,
11576 * we want do as few commits as possible.
11577 */
11578 if (fl == B_FREE) {
11579 if (rp->r_flags & R4COMMITWAIT) {
11580 page_unlock(pp);
11581 mutex_exit(&rp->r_statelock);
11582 return;
11583 }
11584 if (pp->p_fsdata == C_DELAYCOMMIT) {
11585 pp->p_fsdata = C_COMMIT;
11586 page_unlock(pp);
11587 mutex_exit(&rp->r_statelock);
11588 return;
11589 }
11590 }
11591
11592 /*
11593 * Check to see if there is a signal which would prevent an
11594 * attempt to commit the pages from being successful. If so,
11595 * then don't bother with all of the work to gather pages and
11596 * generate the unsuccessful RPC. Just return from here and
11597 * let the page be committed at some later time.
11598 */
11599 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11600 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11601 sigunintr(&smask);
11602 page_unlock(pp);
11603 mutex_exit(&rp->r_statelock);
11604 return;
11605 }
11606 sigunintr(&smask);
11607
11608 /*
11609 * We are starting to need to commit pages, so let's try
11610 * to commit as many as possible at once to reduce the
11611 * overhead.
11612 *
11613 * Set the `commit inprogress' state bit. We must
11614 * first wait until any current one finishes. Then
11615 * we initialize the c_pages list with this page.
11616 */
11617 while (rp->r_flags & R4COMMIT) {
11618 rp->r_flags |= R4COMMITWAIT;
11619 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11620 rp->r_flags &= ~R4COMMITWAIT;
11621 }
11622 rp->r_flags |= R4COMMIT;
11623 mutex_exit(&rp->r_statelock);
11624 ASSERT(rp->r_commit.c_pages == NULL);
11625 rp->r_commit.c_pages = pp;
11626 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11627 rp->r_commit.c_commlen = PAGESIZE;
11628
11629 /*
11630 * Gather together all other pages which can be committed.
11631 * They will all be chained off r_commit.c_pages.
11632 */
11633 nfs4_get_commit(vp);
11634
11635 /*
11636 * Clear the `commit inprogress' status and disconnect
11637 * the list of pages to be committed from the rnode.
11638 * At this same time, we also save the starting offset
11639 * and length of data to be committed on the server.
11640 */
11641 plist = rp->r_commit.c_pages;
11642 rp->r_commit.c_pages = NULL;
11643 offset = rp->r_commit.c_commbase;
11644 len = rp->r_commit.c_commlen;
11645 mutex_enter(&rp->r_statelock);
11646 rp->r_flags &= ~R4COMMIT;
11647 cv_broadcast(&rp->r_commit.c_cv);
11648 mutex_exit(&rp->r_statelock);
11649
11650 if (curproc == proc_pageout || curproc == proc_fsflush ||
11651 nfs_zone() != VTOMI4(vp)->mi_zone) {
11652 nfs4_async_commit(vp, plist, offset, len,
11653 cr, do_nfs4_async_commit);
11654 return;
11655 }
11656
11657 /*
11658 * Actually generate the COMMIT op over the wire operation.
11659 */
11660 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11661
11662 /*
11663 * If we got an error during the commit, just unlock all
11664 * of the pages. The pages will get retransmitted to the
11665 * server during a putpage operation.
11666 */
11667 if (error) {
11668 while (plist != NULL) {
11669 pptr = plist;
11670 page_sub(&plist, pptr);
11671 page_unlock(pptr);
11672 }
11673 return;
11674 }
11675
11676 /*
11677 * We've tried as hard as we can to commit the data to stable
11678 * storage on the server. We just unlock the rest of the pages
11679 * and clear the commit required state. They will be put
11680 * onto the tail of the cachelist if they are nolonger
11681 * mapped.
11682 */
11683 while (plist != pp) {
11684 pptr = plist;
11685 page_sub(&plist, pptr);
11686 pptr->p_fsdata = C_NOCOMMIT;
11687 page_unlock(pptr);
11688 }
11689
11690 /*
11691 * It is possible that nfs4_commit didn't return error but
11692 * some other thread has modified the page we are going
11693 * to free/destroy.
11694 * In this case we need to rewrite the page. Do an explicit check
11695 * before attempting to free/destroy the page. If modified, needs to
11696 * be rewritten so unlock the page and return.
11697 */
11698 if (hat_ismod(pp)) {
11699 pp->p_fsdata = C_NOCOMMIT;
11700 page_unlock(pp);
11701 return;
11702 }
11703
11704 /*
11705 * Now, as appropriate, either free or destroy the page
11706 * that we were called with.
11707 */
11708 pp->p_fsdata = C_NOCOMMIT;
11709 if (fl == B_FREE)
11710 page_free(pp, dn);
11711 else
11712 page_destroy(pp, dn);
11713 }
11714
11715 /*
11716 * Commit requires that the current fh be the file written to.
11717 * The compound op structure is:
11718 * PUTFH(file), COMMIT
11719 */
11720 static int
11721 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11722 {
11723 COMPOUND4args_clnt args;
11724 COMPOUND4res_clnt res;
11725 COMMIT4res *cm_res;
11726 nfs_argop4 argop[2];
11727 nfs_resop4 *resop;
11728 int doqueue;
11729 mntinfo4_t *mi;
11730 rnode4_t *rp;
11731 cred_t *cred_otw = NULL;
11732 bool_t needrecov = FALSE;
11733 nfs4_recov_state_t recov_state;
11734 nfs4_open_stream_t *osp = NULL;
11735 bool_t first_time = TRUE; /* first time getting OTW cred */
11736 bool_t last_time = FALSE; /* last time getting OTW cred */
11737 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11738
11739 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11740
11741 rp = VTOR4(vp);
11742
11743 mi = VTOMI4(vp);
11744 recov_state.rs_flags = 0;
11745 recov_state.rs_num_retry_despite_err = 0;
11746 get_commit_cred:
11747 /*
11748 * Releases the osp, if a valid open stream is provided.
11749 * Puts a hold on the cred_otw and the new osp (if found).
11750 */
11751 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11752 &first_time, &last_time);
11753 args.ctag = TAG_COMMIT;
11754 recov_retry:
11755 /*
11756 * Commit ops: putfh file; commit
11757 */
11758 args.array_len = 2;
11759 args.array = argop;
11760
11761 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11762 &recov_state, NULL);
11763 if (e.error) {
11764 crfree(cred_otw);
11765 if (osp != NULL)
11766 open_stream_rele(osp, rp);
11767 return (e.error);
11768 }
11769
11770 /* putfh directory */
11771 argop[0].argop = OP_CPUTFH;
11772 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11773
11774 /* commit */
11775 argop[1].argop = OP_COMMIT;
11776 argop[1].nfs_argop4_u.opcommit.offset = offset;
11777 argop[1].nfs_argop4_u.opcommit.count = count;
11778
11779 doqueue = 1;
11780 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11781
11782 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11783 if (!needrecov && e.error) {
11784 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11785 needrecov);
11786 crfree(cred_otw);
11787 if (e.error == EACCES && last_time == FALSE)
11788 goto get_commit_cred;
11789 if (osp != NULL)
11790 open_stream_rele(osp, rp);
11791 return (e.error);
11792 }
11793
11794 if (needrecov) {
11795 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11796 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11797 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11798 &recov_state, needrecov);
11799 if (!e.error)
11800 (void) xdr_free(xdr_COMPOUND4res_clnt,
11801 (caddr_t)&res);
11802 goto recov_retry;
11803 }
11804 if (e.error) {
11805 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11806 &recov_state, needrecov);
11807 crfree(cred_otw);
11808 if (osp != NULL)
11809 open_stream_rele(osp, rp);
11810 return (e.error);
11811 }
11812 /* fall through for res.status case */
11813 }
11814
11815 if (res.status) {
11816 e.error = geterrno4(res.status);
11817 if (e.error == EACCES && last_time == FALSE) {
11818 crfree(cred_otw);
11819 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11820 &recov_state, needrecov);
11821 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11822 goto get_commit_cred;
11823 }
11824 /*
11825 * Can't do a nfs4_purge_stale_fh here because this
11826 * can cause a deadlock. nfs4_commit can
11827 * be called from nfs4_dispose which can be called
11828 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11829 * can call back to pvn_vplist_dirty.
11830 */
11831 if (e.error == ESTALE) {
11832 mutex_enter(&rp->r_statelock);
11833 rp->r_flags |= R4STALE;
11834 if (!rp->r_error)
11835 rp->r_error = e.error;
11836 mutex_exit(&rp->r_statelock);
11837 PURGE_ATTRCACHE4(vp);
11838 } else {
11839 mutex_enter(&rp->r_statelock);
11840 if (!rp->r_error)
11841 rp->r_error = e.error;
11842 mutex_exit(&rp->r_statelock);
11843 }
11844 } else {
11845 ASSERT(rp->r_flags & R4HAVEVERF);
11846 resop = &res.array[1]; /* commit res */
11847 cm_res = &resop->nfs_resop4_u.opcommit;
11848 mutex_enter(&rp->r_statelock);
11849 if (cm_res->writeverf == rp->r_writeverf) {
11850 mutex_exit(&rp->r_statelock);
11851 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11852 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11853 &recov_state, needrecov);
11854 crfree(cred_otw);
11855 if (osp != NULL)
11856 open_stream_rele(osp, rp);
11857 return (0);
11858 }
11859 nfs4_set_mod(vp);
11860 rp->r_writeverf = cm_res->writeverf;
11861 mutex_exit(&rp->r_statelock);
11862 e.error = NFS_VERF_MISMATCH;
11863 }
11864
11865 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11866 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11867 crfree(cred_otw);
11868 if (osp != NULL)
11869 open_stream_rele(osp, rp);
11870
11871 return (e.error);
11872 }
11873
11874 static void
11875 nfs4_set_mod(vnode_t *vp)
11876 {
11877 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11878
11879 /* make sure we're looking at the master vnode, not a shadow */
11880 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11881 }
11882
11883 /*
11884 * This function is used to gather a page list of the pages which
11885 * can be committed on the server.
11886 *
11887 * The calling thread must have set R4COMMIT. This bit is used to
11888 * serialize access to the commit structure in the rnode. As long
11889 * as the thread has set R4COMMIT, then it can manipulate the commit
11890 * structure without requiring any other locks.
11891 *
11892 * When this function is called from nfs4_dispose() the page passed
11893 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11894 * will skip it. This is not a problem since we initially add the
11895 * page to the r_commit page list.
11896 *
11897 */
11898 static void
11899 nfs4_get_commit(vnode_t *vp)
11900 {
11901 rnode4_t *rp;
11902 page_t *pp;
11903 kmutex_t *vphm;
11904
11905 rp = VTOR4(vp);
11906
11907 ASSERT(rp->r_flags & R4COMMIT);
11908
11909 /* make sure we're looking at the master vnode, not a shadow */
11910
11911 if (IS_SHADOW(vp, rp))
11912 vp = RTOV4(rp);
11913
11914 vphm = page_vnode_mutex(vp);
11915 mutex_enter(vphm);
11916
11917 /*
11918 * If there are no pages associated with this vnode, then
11919 * just return.
11920 */
11921 if ((pp = vp->v_pages) == NULL) {
11922 mutex_exit(vphm);
11923 return;
11924 }
11925
11926 /*
11927 * Step through all of the pages associated with this vnode
11928 * looking for pages which need to be committed.
11929 */
11930 do {
11931 /* Skip marker pages. */
11932 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11933 continue;
11934
11935 /*
11936 * First short-cut everything (without the page_lock)
11937 * and see if this page does not need to be committed
11938 * or is modified if so then we'll just skip it.
11939 */
11940 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11941 continue;
11942
11943 /*
11944 * Attempt to lock the page. If we can't, then
11945 * someone else is messing with it or we have been
11946 * called from nfs4_dispose and this is the page that
11947 * nfs4_dispose was called with.. anyway just skip it.
11948 */
11949 if (!page_trylock(pp, SE_EXCL))
11950 continue;
11951
11952 /*
11953 * Lets check again now that we have the page lock.
11954 */
11955 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11956 page_unlock(pp);
11957 continue;
11958 }
11959
11960 /* this had better not be a free page */
11961 ASSERT(PP_ISFREE(pp) == 0);
11962
11963 /*
11964 * The page needs to be committed and we locked it.
11965 * Update the base and length parameters and add it
11966 * to r_pages.
11967 */
11968 if (rp->r_commit.c_pages == NULL) {
11969 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11970 rp->r_commit.c_commlen = PAGESIZE;
11971 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11972 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11973 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11974 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11975 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11976 <= pp->p_offset) {
11977 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11978 rp->r_commit.c_commbase + PAGESIZE;
11979 }
11980 page_add(&rp->r_commit.c_pages, pp);
11981 } while ((pp = pp->p_vpnext) != vp->v_pages);
11982
11983 mutex_exit(vphm);
11984 }
11985
11986 /*
11987 * This routine is used to gather together a page list of the pages
11988 * which are to be committed on the server. This routine must not
11989 * be called if the calling thread holds any locked pages.
11990 *
11991 * The calling thread must have set R4COMMIT. This bit is used to
11992 * serialize access to the commit structure in the rnode. As long
11993 * as the thread has set R4COMMIT, then it can manipulate the commit
11994 * structure without requiring any other locks.
11995 */
11996 static void
11997 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
11998 {
11999
12000 rnode4_t *rp;
12001 page_t *pp;
12002 u_offset_t end;
12003 u_offset_t off;
12004 ASSERT(len != 0);
12005 rp = VTOR4(vp);
12006 ASSERT(rp->r_flags & R4COMMIT);
12007
12008 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12009
12010 /* make sure we're looking at the master vnode, not a shadow */
12011
12012 if (IS_SHADOW(vp, rp))
12013 vp = RTOV4(rp);
12014
12015 /*
12016 * If there are no pages associated with this vnode, then
12017 * just return.
12018 */
12019 if ((pp = vp->v_pages) == NULL)
12020 return;
12021 /*
12022 * Calculate the ending offset.
12023 */
12024 end = soff + len;
12025 for (off = soff; off < end; off += PAGESIZE) {
12026 /*
12027 * Lookup each page by vp, offset.
12028 */
12029 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12030 continue;
12031 /*
12032 * If this page does not need to be committed or is
12033 * modified, then just skip it.
12034 */
12035 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12036 page_unlock(pp);
12037 continue;
12038 }
12039
12040 ASSERT(PP_ISFREE(pp) == 0);
12041 /*
12042 * The page needs to be committed and we locked it.
12043 * Update the base and length parameters and add it
12044 * to r_pages.
12045 */
12046 if (rp->r_commit.c_pages == NULL) {
12047 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12048 rp->r_commit.c_commlen = PAGESIZE;
12049 } else {
12050 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12051 rp->r_commit.c_commbase + PAGESIZE;
12052 }
12053 page_add(&rp->r_commit.c_pages, pp);
12054 }
12055 }
12056
12057 /*
12058 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12059 * Flushes and commits data to the server.
12060 */
12061 static int
12062 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12063 {
12064 int error;
12065 verifier4 write_verf;
12066 rnode4_t *rp = VTOR4(vp);
12067
12068 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12069
12070 /*
12071 * Flush the data portion of the file and then commit any
12072 * portions which need to be committed. This may need to
12073 * be done twice if the server has changed state since
12074 * data was last written. The data will need to be
12075 * rewritten to the server and then a new commit done.
12076 *
12077 * In fact, this may need to be done several times if the
12078 * server is having problems and crashing while we are
12079 * attempting to do this.
12080 */
12081
12082 top:
12083 /*
12084 * Do a flush based on the poff and plen arguments. This
12085 * will synchronously write out any modified pages in the
12086 * range specified by (poff, plen). This starts all of the
12087 * i/o operations which will be waited for in the next
12088 * call to nfs4_putpage
12089 */
12090
12091 mutex_enter(&rp->r_statelock);
12092 write_verf = rp->r_writeverf;
12093 mutex_exit(&rp->r_statelock);
12094
12095 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12096 if (error == EAGAIN)
12097 error = 0;
12098
12099 /*
12100 * Do a flush based on the poff and plen arguments. This
12101 * will synchronously write out any modified pages in the
12102 * range specified by (poff, plen) and wait until all of
12103 * the asynchronous i/o's in that range are done as well.
12104 */
12105 if (!error)
12106 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12107
12108 if (error)
12109 return (error);
12110
12111 mutex_enter(&rp->r_statelock);
12112 if (rp->r_writeverf != write_verf) {
12113 mutex_exit(&rp->r_statelock);
12114 goto top;
12115 }
12116 mutex_exit(&rp->r_statelock);
12117
12118 /*
12119 * Now commit any pages which might need to be committed.
12120 * If the error, NFS_VERF_MISMATCH, is returned, then
12121 * start over with the flush operation.
12122 */
12123 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12124
12125 if (error == NFS_VERF_MISMATCH)
12126 goto top;
12127
12128 return (error);
12129 }
12130
12131 /*
12132 * nfs4_commit_vp() will wait for other pending commits and
12133 * will either commit the whole file or a range, plen dictates
12134 * if we commit whole file. a value of zero indicates the whole
12135 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12136 */
12137 static int
12138 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12139 cred_t *cr, int wait_on_writes)
12140 {
12141 rnode4_t *rp;
12142 page_t *plist;
12143 offset3 offset;
12144 count3 len;
12145
12146 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12147
12148 rp = VTOR4(vp);
12149
12150 /*
12151 * before we gather commitable pages make
12152 * sure there are no outstanding async writes
12153 */
12154 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12155 mutex_enter(&rp->r_statelock);
12156 while (rp->r_count > 0) {
12157 cv_wait(&rp->r_cv, &rp->r_statelock);
12158 }
12159 mutex_exit(&rp->r_statelock);
12160 }
12161
12162 /*
12163 * Set the `commit inprogress' state bit. We must
12164 * first wait until any current one finishes.
12165 */
12166 mutex_enter(&rp->r_statelock);
12167 while (rp->r_flags & R4COMMIT) {
12168 rp->r_flags |= R4COMMITWAIT;
12169 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12170 rp->r_flags &= ~R4COMMITWAIT;
12171 }
12172 rp->r_flags |= R4COMMIT;
12173 mutex_exit(&rp->r_statelock);
12174
12175 /*
12176 * Gather all of the pages which need to be
12177 * committed.
12178 */
12179 if (plen == 0)
12180 nfs4_get_commit(vp);
12181 else
12182 nfs4_get_commit_range(vp, poff, plen);
12183
12184 /*
12185 * Clear the `commit inprogress' bit and disconnect the
12186 * page list which was gathered by nfs4_get_commit.
12187 */
12188 plist = rp->r_commit.c_pages;
12189 rp->r_commit.c_pages = NULL;
12190 offset = rp->r_commit.c_commbase;
12191 len = rp->r_commit.c_commlen;
12192 mutex_enter(&rp->r_statelock);
12193 rp->r_flags &= ~R4COMMIT;
12194 cv_broadcast(&rp->r_commit.c_cv);
12195 mutex_exit(&rp->r_statelock);
12196
12197 /*
12198 * If any pages need to be committed, commit them and
12199 * then unlock them so that they can be freed some
12200 * time later.
12201 */
12202 if (plist == NULL)
12203 return (0);
12204
12205 /*
12206 * No error occurred during the flush portion
12207 * of this operation, so now attempt to commit
12208 * the data to stable storage on the server.
12209 *
12210 * This will unlock all of the pages on the list.
12211 */
12212 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12213 }
12214
12215 static int
12216 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12217 cred_t *cr)
12218 {
12219 int error;
12220 page_t *pp;
12221
12222 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12223
12224 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12225
12226 /*
12227 * If we got an error, then just unlock all of the pages
12228 * on the list.
12229 */
12230 if (error) {
12231 while (plist != NULL) {
12232 pp = plist;
12233 page_sub(&plist, pp);
12234 page_unlock(pp);
12235 }
12236 return (error);
12237 }
12238 /*
12239 * We've tried as hard as we can to commit the data to stable
12240 * storage on the server. We just unlock the pages and clear
12241 * the commit required state. They will get freed later.
12242 */
12243 while (plist != NULL) {
12244 pp = plist;
12245 page_sub(&plist, pp);
12246 pp->p_fsdata = C_NOCOMMIT;
12247 page_unlock(pp);
12248 }
12249
12250 return (error);
12251 }
12252
12253 static void
12254 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12255 cred_t *cr)
12256 {
12257
12258 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12259 }
12260
12261 /*ARGSUSED*/
12262 static int
12263 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12264 caller_context_t *ct)
12265 {
12266 int error = 0;
12267 mntinfo4_t *mi;
12268 vattr_t va;
12269 vsecattr_t nfsace4_vsap;
12270
12271 mi = VTOMI4(vp);
12272 if (nfs_zone() != mi->mi_zone)
12273 return (EIO);
12274 if (mi->mi_flags & MI4_ACL) {
12275 /* if we have a delegation, return it */
12276 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12277 (void) nfs4delegreturn(VTOR4(vp),
12278 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12279
12280 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12281 NFS4_ACL_SET);
12282 if (error) /* EINVAL */
12283 return (error);
12284
12285 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12286 /*
12287 * These are aclent_t type entries.
12288 */
12289 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12290 vp->v_type == VDIR, FALSE);
12291 if (error)
12292 return (error);
12293 } else {
12294 /*
12295 * These are ace_t type entries.
12296 */
12297 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12298 FALSE);
12299 if (error)
12300 return (error);
12301 }
12302 bzero(&va, sizeof (va));
12303 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12304 vs_ace4_destroy(&nfsace4_vsap);
12305 return (error);
12306 }
12307 return (ENOSYS);
12308 }
12309
12310 /* ARGSUSED */
12311 int
12312 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12313 caller_context_t *ct)
12314 {
12315 int error;
12316 mntinfo4_t *mi;
12317 nfs4_ga_res_t gar;
12318 rnode4_t *rp = VTOR4(vp);
12319
12320 mi = VTOMI4(vp);
12321 if (nfs_zone() != mi->mi_zone)
12322 return (EIO);
12323
12324 bzero(&gar, sizeof (gar));
12325 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12326
12327 /*
12328 * vsecattr->vsa_mask holds the original acl request mask.
12329 * This is needed when determining what to return.
12330 * (See: nfs4_create_getsecattr_return())
12331 */
12332 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12333 if (error) /* EINVAL */
12334 return (error);
12335
12336 /*
12337 * If this is a referral stub, don't try to go OTW for an ACL
12338 */
12339 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12340 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12341
12342 if (mi->mi_flags & MI4_ACL) {
12343 /*
12344 * Check if the data is cached and the cache is valid. If it
12345 * is we don't go over the wire.
12346 */
12347 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12348 mutex_enter(&rp->r_statelock);
12349 if (rp->r_secattr != NULL) {
12350 error = nfs4_create_getsecattr_return(
12351 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12352 rp->r_attr.va_gid,
12353 vp->v_type == VDIR);
12354 if (!error) { /* error == 0 - Success! */
12355 mutex_exit(&rp->r_statelock);
12356 return (error);
12357 }
12358 }
12359 mutex_exit(&rp->r_statelock);
12360 }
12361
12362 /*
12363 * The getattr otw call will always get both the acl, in
12364 * the form of a list of nfsace4's, and the number of acl
12365 * entries; independent of the value of gar.n4g_vsa.vsa_mask.
12366 */
12367 gar.n4g_va.va_mask = AT_ALL;
12368 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12369 if (error) {
12370 vs_ace4_destroy(&gar.n4g_vsa);
12371 if (error == ENOTSUP || error == EOPNOTSUPP)
12372 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12373 return (error);
12374 }
12375
12376 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12377 /*
12378 * No error was returned, but according to the response
12379 * bitmap, neither was an acl.
12380 */
12381 vs_ace4_destroy(&gar.n4g_vsa);
12382 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12383 return (error);
12384 }
12385
12386 /*
12387 * Update the cache with the ACL.
12388 */
12389 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12390
12391 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12392 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12393 vp->v_type == VDIR);
12394 vs_ace4_destroy(&gar.n4g_vsa);
12395 if ((error) && (vsecattr->vsa_mask &
12396 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12397 (error != EACCES)) {
12398 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12399 }
12400 return (error);
12401 }
12402 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12403 return (error);
12404 }
12405
12406 /*
12407 * The function returns:
12408 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12409 * - EINVAL if the passed in "acl_mask" is an invalid request.
12410 *
12411 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12412 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12413 *
12414 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12415 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12416 * - We have a count field set without the corresponding acl field set. (e.g. -
12417 * VSA_ACECNT is set, but VSA_ACE is not)
12418 */
12419 static int
12420 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12421 {
12422 /* Shortcut the masks that are always valid. */
12423 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12424 return (0);
12425 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12426 return (0);
12427
12428 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12429 /*
12430 * We can't have any VSA_ACL type stuff in the mask now.
12431 */
12432 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12433 VSA_DFACLCNT))
12434 return (EINVAL);
12435
12436 if (op == NFS4_ACL_SET) {
12437 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12438 return (EINVAL);
12439 }
12440 }
12441
12442 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12443 /*
12444 * We can't have any VSA_ACE type stuff in the mask now.
12445 */
12446 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12447 return (EINVAL);
12448
12449 if (op == NFS4_ACL_SET) {
12450 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12451 return (EINVAL);
12452
12453 if ((acl_mask & VSA_DFACLCNT) &&
12454 !(acl_mask & VSA_DFACL))
12455 return (EINVAL);
12456 }
12457 }
12458 return (0);
12459 }
12460
12461 /*
12462 * The theory behind creating the correct getsecattr return is simply this:
12463 * "Don't return anything that the caller is not expecting to have to free."
12464 */
12465 static int
12466 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12467 uid_t uid, gid_t gid, int isdir)
12468 {
12469 int error = 0;
12470 /* Save the mask since the translators modify it. */
12471 uint_t orig_mask = vsap->vsa_mask;
12472
12473 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12474 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12475
12476 if (error)
12477 return (error);
12478
12479 /*
12480 * If the caller only asked for the ace count (VSA_ACECNT)
12481 * don't give them the full acl (VSA_ACE), free it.
12482 */
12483 if (!orig_mask & VSA_ACE) {
12484 if (vsap->vsa_aclentp != NULL) {
12485 kmem_free(vsap->vsa_aclentp,
12486 vsap->vsa_aclcnt * sizeof (ace_t));
12487 vsap->vsa_aclentp = NULL;
12488 }
12489 }
12490 vsap->vsa_mask = orig_mask;
12491
12492 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12493 VSA_DFACLCNT)) {
12494 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12495 isdir, FALSE);
12496
12497 if (error)
12498 return (error);
12499
12500 /*
12501 * If the caller only asked for the acl count (VSA_ACLCNT)
12502 * and/or the default acl count (VSA_DFACLCNT) don't give them
12503 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12504 */
12505 if (!orig_mask & VSA_ACL) {
12506 if (vsap->vsa_aclentp != NULL) {
12507 kmem_free(vsap->vsa_aclentp,
12508 vsap->vsa_aclcnt * sizeof (aclent_t));
12509 vsap->vsa_aclentp = NULL;
12510 }
12511 }
12512
12513 if (!orig_mask & VSA_DFACL) {
12514 if (vsap->vsa_dfaclentp != NULL) {
12515 kmem_free(vsap->vsa_dfaclentp,
12516 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12517 vsap->vsa_dfaclentp = NULL;
12518 }
12519 }
12520 vsap->vsa_mask = orig_mask;
12521 }
12522 return (0);
12523 }
12524
12525 /* ARGSUSED */
12526 int
12527 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12528 caller_context_t *ct)
12529 {
12530 int error;
12531
12532 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12533 return (EIO);
12534 /*
12535 * check for valid cmd parameter
12536 */
12537 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12538 return (EINVAL);
12539
12540 /*
12541 * Check access permissions
12542 */
12543 if ((cmd & F_SHARE) &&
12544 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12545 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12546 return (EBADF);
12547
12548 /*
12549 * If the filesystem is mounted using local locking, pass the
12550 * request off to the local share code.
12551 */
12552 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12553 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12554
12555 switch (cmd) {
12556 case F_SHARE:
12557 case F_UNSHARE:
12558 /*
12559 * This will be properly implemented later,
12560 * see RFE: 4823948 .
12561 */
12562 error = EAGAIN;
12563 break;
12564
12565 case F_HASREMOTELOCKS:
12566 /*
12567 * NFS client can't store remote locks itself
12568 */
12569 shr->s_access = 0;
12570 error = 0;
12571 break;
12572
12573 default:
12574 error = EINVAL;
12575 break;
12576 }
12577
12578 return (error);
12579 }
12580
12581 /*
12582 * Common code called by directory ops to update the attrcache
12583 */
12584 static int
12585 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12586 hrtime_t t, vnode_t *vp, cred_t *cr)
12587 {
12588 int error = 0;
12589
12590 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12591
12592 if (status != NFS4_OK) {
12593 /* getattr not done or failed */
12594 PURGE_ATTRCACHE4(vp);
12595 return (error);
12596 }
12597
12598 if (garp) {
12599 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12600 } else {
12601 PURGE_ATTRCACHE4(vp);
12602 }
12603 return (error);
12604 }
12605
12606 /*
12607 * Update directory caches for directory modification ops (link, rename, etc.)
12608 * When dinfo is NULL, manage dircaches in the old way.
12609 */
12610 static void
12611 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12612 dirattr_info_t *dinfo)
12613 {
12614 rnode4_t *drp = VTOR4(dvp);
12615
12616 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12617
12618 /* Purge rddir cache for dir since it changed */
12619 if (drp->r_dir != NULL)
12620 nfs4_purge_rddir_cache(dvp);
12621
12622 /*
12623 * If caller provided dinfo, then use it to manage dir caches.
12624 */
12625 if (dinfo != NULL) {
12626 if (vp != NULL) {
12627 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12628 if (!VTOR4(vp)->created_v4) {
12629 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12630 dnlc_update(dvp, nm, vp);
12631 } else {
12632 /*
12633 * XXX don't update if the created_v4 flag is
12634 * set
12635 */
12636 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12637 NFS4_DEBUG(nfs4_client_state_debug,
12638 (CE_NOTE, "nfs4_update_dircaches: "
12639 "don't update dnlc: created_v4 flag"));
12640 }
12641 }
12642
12643 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12644 dinfo->di_cred, FALSE, cinfo);
12645
12646 return;
12647 }
12648
12649 /*
12650 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12651 * Since caller modified dir but didn't receive post-dirmod-op dir
12652 * attrs, the dir's attrs must be purged.
12653 *
12654 * XXX this check and dnlc update/purge should really be atomic,
12655 * XXX but can't use rnode statelock because it'll deadlock in
12656 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12657 * XXX does occur.
12658 *
12659 * XXX We also may want to check that atomic is true in the
12660 * XXX change_info struct. If it is not, the change_info may
12661 * XXX reflect changes by more than one clients which means that
12662 * XXX our cache may not be valid.
12663 */
12664 PURGE_ATTRCACHE4(dvp);
12665 if (drp->r_change == cinfo->before) {
12666 /* no changes took place in the directory prior to our link */
12667 if (vp != NULL) {
12668 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12669 if (!VTOR4(vp)->created_v4) {
12670 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12671 dnlc_update(dvp, nm, vp);
12672 } else {
12673 /*
12674 * XXX dont' update if the created_v4 flag
12675 * is set
12676 */
12677 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12678 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12679 "nfs4_update_dircaches: don't"
12680 " update dnlc: created_v4 flag"));
12681 }
12682 }
12683 } else {
12684 /* Another client modified directory - purge its dnlc cache */
12685 dnlc_purge_vp(dvp);
12686 }
12687 }
12688
12689 /*
12690 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12691 * file.
12692 *
12693 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12694 * file (ie: client recovery) and otherwise set to FALSE.
12695 *
12696 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12697 * initiated) calling functions.
12698 *
12699 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12700 * of resending a 'lost' open request.
12701 *
12702 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12703 * server that hands out BAD_SEQID on open confirm.
12704 *
12705 * Errors are returned via the nfs4_error_t parameter.
12706 */
12707 void
12708 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12709 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12710 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12711 {
12712 COMPOUND4args_clnt args;
12713 COMPOUND4res_clnt res;
12714 nfs_argop4 argop[2];
12715 nfs_resop4 *resop;
12716 int doqueue = 1;
12717 mntinfo4_t *mi;
12718 OPEN_CONFIRM4args *open_confirm_args;
12719 int needrecov;
12720
12721 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12722 #if DEBUG
12723 mutex_enter(&oop->oo_lock);
12724 ASSERT(oop->oo_seqid_inuse);
12725 mutex_exit(&oop->oo_lock);
12726 #endif
12727
12728 recov_retry_confirm:
12729 nfs4_error_zinit(ep);
12730 *retry_open = FALSE;
12731
12732 if (resend)
12733 args.ctag = TAG_OPEN_CONFIRM_LOST;
12734 else
12735 args.ctag = TAG_OPEN_CONFIRM;
12736
12737 args.array_len = 2;
12738 args.array = argop;
12739
12740 /* putfh target fh */
12741 argop[0].argop = OP_CPUTFH;
12742 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12743
12744 argop[1].argop = OP_OPEN_CONFIRM;
12745 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12746
12747 (*seqid) += 1;
12748 open_confirm_args->seqid = *seqid;
12749 open_confirm_args->open_stateid = *stateid;
12750
12751 mi = VTOMI4(vp);
12752
12753 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12754
12755 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12756 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12757 }
12758
12759 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12760 if (!needrecov && ep->error)
12761 return;
12762
12763 if (needrecov) {
12764 bool_t abort = FALSE;
12765
12766 if (reopening_file == FALSE) {
12767 nfs4_bseqid_entry_t *bsep = NULL;
12768
12769 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12770 bsep = nfs4_create_bseqid_entry(oop, NULL,
12771 vp, 0, args.ctag,
12772 open_confirm_args->seqid);
12773
12774 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12775 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12776 if (bsep) {
12777 kmem_free(bsep, sizeof (*bsep));
12778 if (num_bseqid_retryp &&
12779 --(*num_bseqid_retryp) == 0)
12780 abort = TRUE;
12781 }
12782 }
12783 if ((ep->error == ETIMEDOUT ||
12784 res.status == NFS4ERR_RESOURCE) &&
12785 abort == FALSE && resend == FALSE) {
12786 if (!ep->error)
12787 (void) xdr_free(xdr_COMPOUND4res_clnt,
12788 (caddr_t)&res);
12789
12790 delay(SEC_TO_TICK(confirm_retry_sec));
12791 goto recov_retry_confirm;
12792 }
12793 /* State may have changed so retry the entire OPEN op */
12794 if (abort == FALSE)
12795 *retry_open = TRUE;
12796 else
12797 *retry_open = FALSE;
12798 if (!ep->error)
12799 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12800 return;
12801 }
12802
12803 if (res.status) {
12804 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12805 return;
12806 }
12807
12808 resop = &res.array[1]; /* open confirm res */
12809 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12810 stateid, sizeof (*stateid));
12811
12812 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12813 }
12814
12815 /*
12816 * Return the credentials associated with a client state object. The
12817 * caller is responsible for freeing the credentials.
12818 */
12819
12820 static cred_t *
12821 state_to_cred(nfs4_open_stream_t *osp)
12822 {
12823 cred_t *cr;
12824
12825 /*
12826 * It's ok to not lock the open stream and open owner to get
12827 * the oo_cred since this is only written once (upon creation)
12828 * and will not change.
12829 */
12830 cr = osp->os_open_owner->oo_cred;
12831 crhold(cr);
12832
12833 return (cr);
12834 }
12835
12836 /*
12837 * nfs4_find_sysid
12838 *
12839 * Find the sysid for the knetconfig associated with the given mi.
12840 */
12841 static struct lm_sysid *
12842 nfs4_find_sysid(mntinfo4_t *mi)
12843 {
12844 ASSERT(nfs_zone() == mi->mi_zone);
12845
12846 /*
12847 * Switch from RDMA knconf to original mount knconf
12848 */
12849 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12850 mi->mi_curr_serv->sv_hostname, NULL));
12851 }
12852
12853 #ifdef DEBUG
12854 /*
12855 * Return a string version of the call type for easy reading.
12856 */
12857 static char *
12858 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12859 {
12860 switch (ctype) {
12861 case NFS4_LCK_CTYPE_NORM:
12862 return ("NORMAL");
12863 case NFS4_LCK_CTYPE_RECLAIM:
12864 return ("RECLAIM");
12865 case NFS4_LCK_CTYPE_RESEND:
12866 return ("RESEND");
12867 case NFS4_LCK_CTYPE_REINSTATE:
12868 return ("REINSTATE");
12869 default:
12870 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12871 "type %d", ctype);
12872 return ("");
12873 }
12874 }
12875 #endif
12876
12877 /*
12878 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12879 * Unlock requests don't have an over-the-wire locktype, so we just return
12880 * something non-threatening.
12881 */
12882
12883 static nfs_lock_type4
12884 flk_to_locktype(int cmd, int l_type)
12885 {
12886 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12887
12888 switch (l_type) {
12889 case F_UNLCK:
12890 return (READ_LT);
12891 case F_RDLCK:
12892 if (cmd == F_SETLK)
12893 return (READ_LT);
12894 else
12895 return (READW_LT);
12896 case F_WRLCK:
12897 if (cmd == F_SETLK)
12898 return (WRITE_LT);
12899 else
12900 return (WRITEW_LT);
12901 }
12902 panic("flk_to_locktype");
12903 /*NOTREACHED*/
12904 }
12905
12906 /*
12907 * Do some preliminary checks for nfs4frlock.
12908 */
12909 static int
12910 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12911 u_offset_t offset)
12912 {
12913 int error = 0;
12914
12915 /*
12916 * If we are setting a lock, check that the file is opened
12917 * with the correct mode.
12918 */
12919 if (cmd == F_SETLK || cmd == F_SETLKW) {
12920 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12921 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12922 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12923 "nfs4frlock_validate_args: file was opened with "
12924 "incorrect mode"));
12925 return (EBADF);
12926 }
12927 }
12928
12929 /* Convert the offset. It may need to be restored before returning. */
12930 if (error = convoff(vp, flk, 0, offset)) {
12931 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12932 "nfs4frlock_validate_args: convoff => error= %d\n",
12933 error));
12934 return (error);
12935 }
12936
12937 return (error);
12938 }
12939
12940 /*
12941 * Set the flock64's lm_sysid for nfs4frlock.
12942 */
12943 static int
12944 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12945 {
12946 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12947
12948 /* Find the lm_sysid */
12949 *lspp = nfs4_find_sysid(VTOMI4(vp));
12950
12951 if (*lspp == NULL) {
12952 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12953 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12954 return (ENOLCK);
12955 }
12956
12957 flk->l_sysid = lm_sysidt(*lspp);
12958
12959 return (0);
12960 }
12961
12962 /*
12963 * Do the remaining preliminary setup for nfs4frlock.
12964 */
12965 static void
12966 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12967 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12968 cred_t **cred_otw)
12969 {
12970 /*
12971 * set tick_delay to the base delay time.
12972 * (NFS4_BASE_WAIT_TIME is in secs)
12973 */
12974
12975 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12976
12977 /*
12978 * If lock is relative to EOF, we need the newest length of the
12979 * file. Therefore invalidate the ATTR_CACHE.
12980 */
12981
12982 *whencep = flk->l_whence;
12983
12984 if (*whencep == 2) /* SEEK_END */
12985 PURGE_ATTRCACHE4(vp);
12986
12987 recov_statep->rs_flags = 0;
12988 recov_statep->rs_num_retry_despite_err = 0;
12989 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
12990 }
12991
12992 /*
12993 * Initialize and allocate the data structures necessary for
12994 * the nfs4frlock call.
12995 * Allocates argsp's op array, frees up the saved_rqstpp if there is one.
12996 */
12997 static void
12998 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
12999 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13000 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13001 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13002 {
13003 int argoplist_size;
13004 int num_ops = 2;
13005
13006 *retry = FALSE;
13007 *did_start_fop = FALSE;
13008 *skip_get_err = FALSE;
13009 lost_rqstp->lr_op = 0;
13010 argoplist_size = num_ops * sizeof (nfs_argop4);
13011 /* fill array with zero */
13012 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13013
13014 *argspp = argsp;
13015 *respp = NULL;
13016
13017 argsp->array_len = num_ops;
13018 argsp->array = *argopp;
13019
13020 /* initialize in case of error; will get real value down below */
13021 argsp->ctag = TAG_NONE;
13022
13023 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13024 *op_hintp = OH_LOCKU;
13025 else
13026 *op_hintp = OH_OTHER;
13027 }
13028
13029 /*
13030 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13031 * the proper nfs4_server_t for this instance of nfs4frlock.
13032 * Returns 0 (success) or an errno value.
13033 */
13034 static int
13035 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13036 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13037 bool_t *did_start_fop, bool_t *startrecovp)
13038 {
13039 int error = 0;
13040 rnode4_t *rp;
13041
13042 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13043
13044 if (ctype == NFS4_LCK_CTYPE_NORM) {
13045 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13046 recov_statep, startrecovp);
13047 if (error)
13048 return (error);
13049 *did_start_fop = TRUE;
13050 } else {
13051 *did_start_fop = FALSE;
13052 *startrecovp = FALSE;
13053 }
13054
13055 if (!error) {
13056 rp = VTOR4(vp);
13057
13058 /* If the file failed recovery, just quit. */
13059 mutex_enter(&rp->r_statelock);
13060 if (rp->r_flags & R4RECOVERR) {
13061 error = EIO;
13062 }
13063 mutex_exit(&rp->r_statelock);
13064 }
13065
13066 return (error);
13067 }
13068
13069 /*
13070 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13071 * resend nfs4frlock call is initiated by the recovery framework.
13072 * Acquires the lop and oop seqid synchronization.
13073 */
13074 static void
13075 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13076 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13077 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13078 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13079 {
13080 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13081 int error;
13082
13083 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13084 (CE_NOTE,
13085 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13086 ASSERT(resend_rqstp != NULL);
13087 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13088 resend_rqstp->lr_op == OP_LOCKU);
13089
13090 *oopp = resend_rqstp->lr_oop;
13091 if (resend_rqstp->lr_oop) {
13092 open_owner_hold(resend_rqstp->lr_oop);
13093 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13094 ASSERT(error == 0); /* recov thread always succeeds */
13095 }
13096
13097 /* Must resend this lost lock/locku request. */
13098 ASSERT(resend_rqstp->lr_lop != NULL);
13099 *lopp = resend_rqstp->lr_lop;
13100 lock_owner_hold(resend_rqstp->lr_lop);
13101 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13102 ASSERT(error == 0); /* recov thread always succeeds */
13103
13104 *ospp = resend_rqstp->lr_osp;
13105 if (*ospp)
13106 open_stream_hold(resend_rqstp->lr_osp);
13107
13108 if (resend_rqstp->lr_op == OP_LOCK) {
13109 LOCK4args *lock_args;
13110
13111 argop->argop = OP_LOCK;
13112 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13113 lock_args->locktype = resend_rqstp->lr_locktype;
13114 lock_args->reclaim =
13115 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13116 lock_args->offset = resend_rqstp->lr_flk->l_start;
13117 lock_args->length = resend_rqstp->lr_flk->l_len;
13118 if (lock_args->length == 0)
13119 lock_args->length = ~lock_args->length;
13120 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13121 mi2clientid(mi), &lock_args->locker);
13122
13123 switch (resend_rqstp->lr_ctype) {
13124 case NFS4_LCK_CTYPE_RESEND:
13125 argsp->ctag = TAG_LOCK_RESEND;
13126 break;
13127 case NFS4_LCK_CTYPE_REINSTATE:
13128 argsp->ctag = TAG_LOCK_REINSTATE;
13129 break;
13130 case NFS4_LCK_CTYPE_RECLAIM:
13131 argsp->ctag = TAG_LOCK_RECLAIM;
13132 break;
13133 default:
13134 argsp->ctag = TAG_LOCK_UNKNOWN;
13135 break;
13136 }
13137 } else {
13138 LOCKU4args *locku_args;
13139 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13140
13141 argop->argop = OP_LOCKU;
13142 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13143 locku_args->locktype = READ_LT;
13144 locku_args->seqid = lop->lock_seqid + 1;
13145 mutex_enter(&lop->lo_lock);
13146 locku_args->lock_stateid = lop->lock_stateid;
13147 mutex_exit(&lop->lo_lock);
13148 locku_args->offset = resend_rqstp->lr_flk->l_start;
13149 locku_args->length = resend_rqstp->lr_flk->l_len;
13150 if (locku_args->length == 0)
13151 locku_args->length = ~locku_args->length;
13152
13153 switch (resend_rqstp->lr_ctype) {
13154 case NFS4_LCK_CTYPE_RESEND:
13155 argsp->ctag = TAG_LOCKU_RESEND;
13156 break;
13157 case NFS4_LCK_CTYPE_REINSTATE:
13158 argsp->ctag = TAG_LOCKU_REINSTATE;
13159 break;
13160 default:
13161 argsp->ctag = TAG_LOCK_UNKNOWN;
13162 break;
13163 }
13164 }
13165 }
13166
13167 /*
13168 * Setup the LOCKT4 arguments.
13169 */
13170 static void
13171 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13172 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13173 rnode4_t *rp)
13174 {
13175 LOCKT4args *lockt_args;
13176
13177 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13178 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13179 argop->argop = OP_LOCKT;
13180 argsp->ctag = TAG_LOCKT;
13181 lockt_args = &argop->nfs_argop4_u.oplockt;
13182
13183 /*
13184 * The locktype will be READ_LT unless it's
13185 * a write lock. We do this because the Solaris
13186 * system call allows the combination of
13187 * F_UNLCK and F_GETLK* and so in that case the
13188 * unlock is mapped to a read.
13189 */
13190 if (flk->l_type == F_WRLCK)
13191 lockt_args->locktype = WRITE_LT;
13192 else
13193 lockt_args->locktype = READ_LT;
13194
13195 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13196 /* set the lock owner4 args */
13197 nfs4_setlockowner_args(&lockt_args->owner, rp,
13198 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13199 flk->l_pid);
13200 lockt_args->offset = flk->l_start;
13201 lockt_args->length = flk->l_len;
13202 if (flk->l_len == 0)
13203 lockt_args->length = ~lockt_args->length;
13204
13205 *lockt_argsp = lockt_args;
13206 }
13207
13208 /*
13209 * If the client is holding a delegation, and the open stream to be used
13210 * with this lock request is a delegation open stream, then re-open the stream.
13211 * Sets the nfs4_error_t to all zeros unless the open stream has already
13212 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13213 * means the caller should retry (like a recovery retry).
13214 */
13215 static void
13216 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13217 {
13218 open_delegation_type4 dt;
13219 bool_t reopen_needed, force;
13220 nfs4_open_stream_t *osp;
13221 open_claim_type4 oclaim;
13222 rnode4_t *rp = VTOR4(vp);
13223 mntinfo4_t *mi = VTOMI4(vp);
13224
13225 ASSERT(nfs_zone() == mi->mi_zone);
13226
13227 nfs4_error_zinit(ep);
13228
13229 mutex_enter(&rp->r_statev4_lock);
13230 dt = rp->r_deleg_type;
13231 mutex_exit(&rp->r_statev4_lock);
13232
13233 if (dt != OPEN_DELEGATE_NONE) {
13234 nfs4_open_owner_t *oop;
13235
13236 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13237 if (!oop) {
13238 ep->stat = NFS4ERR_IO;
13239 return;
13240 }
13241 /* returns with 'os_sync_lock' held */
13242 osp = find_open_stream(oop, rp);
13243 if (!osp) {
13244 open_owner_rele(oop);
13245 ep->stat = NFS4ERR_IO;
13246 return;
13247 }
13248
13249 if (osp->os_failed_reopen) {
13250 NFS4_DEBUG((nfs4_open_stream_debug ||
13251 nfs4_client_lock_debug), (CE_NOTE,
13252 "nfs4frlock_check_deleg: os_failed_reopen set "
13253 "for osp %p, cr %p, rp %s", (void *)osp,
13254 (void *)cr, rnode4info(rp)));
13255 mutex_exit(&osp->os_sync_lock);
13256 open_stream_rele(osp, rp);
13257 open_owner_rele(oop);
13258 ep->stat = NFS4ERR_IO;
13259 return;
13260 }
13261
13262 /*
13263 * Determine whether a reopen is needed. If this
13264 * is a delegation open stream, then send the open
13265 * to the server to give visibility to the open owner.
13266 * Even if it isn't a delegation open stream, we need
13267 * to check if the previous open CLAIM_DELEGATE_CUR
13268 * was sufficient.
13269 */
13270
13271 reopen_needed = osp->os_delegation ||
13272 ((lt == F_RDLCK &&
13273 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13274 (lt == F_WRLCK &&
13275 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13276
13277 mutex_exit(&osp->os_sync_lock);
13278 open_owner_rele(oop);
13279
13280 if (reopen_needed) {
13281 /*
13282 * Always use CLAIM_PREVIOUS after server reboot.
13283 * The server will reject CLAIM_DELEGATE_CUR if
13284 * it is used during the grace period.
13285 */
13286 mutex_enter(&mi->mi_lock);
13287 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13288 oclaim = CLAIM_PREVIOUS;
13289 force = TRUE;
13290 } else {
13291 oclaim = CLAIM_DELEGATE_CUR;
13292 force = FALSE;
13293 }
13294 mutex_exit(&mi->mi_lock);
13295
13296 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13297 if (ep->error == EAGAIN) {
13298 nfs4_error_zinit(ep);
13299 ep->stat = NFS4ERR_DELAY;
13300 }
13301 }
13302 open_stream_rele(osp, rp);
13303 osp = NULL;
13304 }
13305 }
13306
13307 /*
13308 * Setup the LOCKU4 arguments.
13309 * Returns errors via the nfs4_error_t.
13310 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13311 * over-the-wire. The caller must release the
13312 * reference on *lopp.
13313 * NFS4ERR_DELAY caller should retry (like recovery retry)
13314 * (other) unrecoverable error.
13315 */
13316 static void
13317 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13318 LOCKU4args **locku_argsp, flock64_t *flk,
13319 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13320 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13321 bool_t *skip_get_err, bool_t *go_otwp)
13322 {
13323 nfs4_lock_owner_t *lop = NULL;
13324 LOCKU4args *locku_args;
13325 pid_t pid;
13326 bool_t is_spec = FALSE;
13327 rnode4_t *rp = VTOR4(vp);
13328
13329 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13330 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13331
13332 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13333 if (ep->error || ep->stat)
13334 return;
13335
13336 argop->argop = OP_LOCKU;
13337 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13338 argsp->ctag = TAG_LOCKU_REINSTATE;
13339 else
13340 argsp->ctag = TAG_LOCKU;
13341 locku_args = &argop->nfs_argop4_u.oplocku;
13342 *locku_argsp = locku_args;
13343
13344 /*
13345 * XXX what should locku_args->locktype be?
13346 * setting to ALWAYS be READ_LT so at least
13347 * it is a valid locktype.
13348 */
13349
13350 locku_args->locktype = READ_LT;
13351
13352 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13353 flk->l_pid;
13354
13355 /*
13356 * Get the lock owner stateid. If no lock owner
13357 * exists, return success.
13358 */
13359 lop = find_lock_owner(rp, pid, LOWN_ANY);
13360 *lopp = lop;
13361 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13362 is_spec = TRUE;
13363 if (!lop || is_spec) {
13364 /*
13365 * No lock owner so no locks to unlock.
13366 * Return success. If there was a failed
13367 * reclaim earlier, the lock might still be
13368 * registered with the local locking code,
13369 * so notify it of the unlock.
13370 *
13371 * If the lockowner is using a special stateid,
13372 * then the original lock request (that created
13373 * this lockowner) was never successful, so we
13374 * have no lock to undo OTW.
13375 */
13376 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13377 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13378 "(%ld) so return success", (long)pid));
13379
13380 if (ctype == NFS4_LCK_CTYPE_NORM)
13381 flk->l_pid = curproc->p_pid;
13382 nfs4_register_lock_locally(vp, flk, flag, offset);
13383 /*
13384 * Release our hold and NULL out so final_cleanup
13385 * doesn't try to end a lock seqid sync we
13386 * never started.
13387 */
13388 if (is_spec) {
13389 lock_owner_rele(lop);
13390 *lopp = NULL;
13391 }
13392 *skip_get_err = TRUE;
13393 *go_otwp = FALSE;
13394 return;
13395 }
13396
13397 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13398 if (ep->error == EAGAIN) {
13399 lock_owner_rele(lop);
13400 *lopp = NULL;
13401 return;
13402 }
13403
13404 mutex_enter(&lop->lo_lock);
13405 locku_args->lock_stateid = lop->lock_stateid;
13406 mutex_exit(&lop->lo_lock);
13407 locku_args->seqid = lop->lock_seqid + 1;
13408
13409 /* leave the ref count on lop, rele after RPC call */
13410
13411 locku_args->offset = flk->l_start;
13412 locku_args->length = flk->l_len;
13413 if (flk->l_len == 0)
13414 locku_args->length = ~locku_args->length;
13415
13416 *go_otwp = TRUE;
13417 }
13418
13419 /*
13420 * Setup the LOCK4 arguments.
13421 *
13422 * Returns errors via the nfs4_error_t.
13423 * NFS4_OK no problems
13424 * NFS4ERR_DELAY caller should retry (like recovery retry)
13425 * (other) unrecoverable error
13426 */
13427 static void
13428 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13429 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13430 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13431 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13432 {
13433 LOCK4args *lock_args;
13434 nfs4_open_owner_t *oop = NULL;
13435 nfs4_open_stream_t *osp = NULL;
13436 nfs4_lock_owner_t *lop = NULL;
13437 pid_t pid;
13438 rnode4_t *rp = VTOR4(vp);
13439
13440 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13441
13442 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13443 if (ep->error || ep->stat != NFS4_OK)
13444 return;
13445
13446 argop->argop = OP_LOCK;
13447 if (ctype == NFS4_LCK_CTYPE_NORM)
13448 argsp->ctag = TAG_LOCK;
13449 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13450 argsp->ctag = TAG_RELOCK;
13451 else
13452 argsp->ctag = TAG_LOCK_REINSTATE;
13453 lock_args = &argop->nfs_argop4_u.oplock;
13454 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13455 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13456 /*
13457 * Get the lock owner. If no lock owner exists,
13458 * create a 'temporary' one and grab the open seqid
13459 * synchronization (which puts a hold on the open
13460 * owner and open stream).
13461 * This also grabs the lock seqid synchronization.
13462 */
13463 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13464 ep->stat =
13465 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13466
13467 if (ep->stat != NFS4_OK)
13468 goto out;
13469
13470 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13471 &lock_args->locker);
13472
13473 lock_args->offset = flk->l_start;
13474 lock_args->length = flk->l_len;
13475 if (flk->l_len == 0)
13476 lock_args->length = ~lock_args->length;
13477 *lock_argsp = lock_args;
13478 out:
13479 *oopp = oop;
13480 *ospp = osp;
13481 *lopp = lop;
13482 }
13483
13484 /*
13485 * After we get the reply from the server, record the proper information
13486 * for possible resend lock requests.
13487 *
13488 * Allocates memory for the saved_rqstp if we have a lost lock to save.
13489 */
13490 static void
13491 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13492 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13493 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13494 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13495 {
13496 bool_t unlock = (flk->l_type == F_UNLCK);
13497
13498 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13499 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13500 ctype == NFS4_LCK_CTYPE_REINSTATE);
13501
13502 if (error != 0 && !unlock) {
13503 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13504 nfs4_client_lock_debug), (CE_NOTE,
13505 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13506 " for lop %p", (void *)lop));
13507 ASSERT(lop != NULL);
13508 mutex_enter(&lop->lo_lock);
13509 lop->lo_pending_rqsts = 1;
13510 mutex_exit(&lop->lo_lock);
13511 }
13512
13513 lost_rqstp->lr_putfirst = FALSE;
13514 lost_rqstp->lr_op = 0;
13515
13516 /*
13517 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13518 * recovery purposes so that the lock request that was sent
13519 * can be saved and re-issued later. Ditto for EIO from a forced
13520 * unmount. This is done to have the client's local locking state
13521 * match the v4 server's state; that is, the request was
13522 * potentially received and accepted by the server but the client
13523 * thinks it was not.
13524 */
13525 if (error == ETIMEDOUT || error == EINTR ||
13526 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13527 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13528 nfs4_client_lock_debug), (CE_NOTE,
13529 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13530 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13531 (void *)lop, (void *)oop, (void *)osp));
13532 if (unlock)
13533 lost_rqstp->lr_op = OP_LOCKU;
13534 else {
13535 lost_rqstp->lr_op = OP_LOCK;
13536 lost_rqstp->lr_locktype = locktype;
13537 }
13538 /*
13539 * Objects are held and rele'd via the recovery code.
13540 * See nfs4_save_lost_rqst.
13541 */
13542 lost_rqstp->lr_vp = vp;
13543 lost_rqstp->lr_dvp = NULL;
13544 lost_rqstp->lr_oop = oop;
13545 lost_rqstp->lr_osp = osp;
13546 lost_rqstp->lr_lop = lop;
13547 lost_rqstp->lr_cr = cr;
13548 switch (ctype) {
13549 case NFS4_LCK_CTYPE_NORM:
13550 flk->l_pid = ttoproc(curthread)->p_pid;
13551 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13552 break;
13553 case NFS4_LCK_CTYPE_REINSTATE:
13554 lost_rqstp->lr_putfirst = TRUE;
13555 lost_rqstp->lr_ctype = ctype;
13556 break;
13557 default:
13558 break;
13559 }
13560 lost_rqstp->lr_flk = flk;
13561 }
13562 }
13563
13564 /*
13565 * Update lop's seqid. Also update the seqid stored in a resend request,
13566 * if any. (Some recovery errors increment the seqid, and we may have to
13567 * send the resend request again.)
13568 */
13569
13570 static void
13571 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13572 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13573 {
13574 if (lock_args) {
13575 if (lock_args->locker.new_lock_owner == TRUE)
13576 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13577 else {
13578 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13579 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13580 }
13581 } else if (locku_args) {
13582 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13583 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13584 }
13585 }
13586
13587 /*
13588 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13589 * COMPOUND4 args/res for calls that need to retry.
13590 * Switches the *cred_otwp to base_cr.
13591 */
13592 static void
13593 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13594 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13595 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13596 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13597 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13598 {
13599 nfs4_open_owner_t *oop = *oopp;
13600 nfs4_open_stream_t *osp = *ospp;
13601 nfs4_lock_owner_t *lop = *lopp;
13602 nfs_argop4 *argop = (*argspp)->array;
13603
13604 if (*did_start_fop) {
13605 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13606 needrecov);
13607 *did_start_fop = FALSE;
13608 }
13609 ASSERT((*argspp)->array_len == 2);
13610 if (argop[1].argop == OP_LOCK)
13611 nfs4args_lock_free(&argop[1]);
13612 else if (argop[1].argop == OP_LOCKT)
13613 nfs4args_lockt_free(&argop[1]);
13614 kmem_free(argop, 2 * sizeof (nfs_argop4));
13615 if (!error)
13616 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13617 *argspp = NULL;
13618 *respp = NULL;
13619
13620 if (lop) {
13621 nfs4_end_lock_seqid_sync(lop);
13622 lock_owner_rele(lop);
13623 *lopp = NULL;
13624 }
13625
13626 /* need to free up the reference on osp for lock args */
13627 if (osp != NULL) {
13628 open_stream_rele(osp, VTOR4(vp));
13629 *ospp = NULL;
13630 }
13631
13632 /* need to free up the reference on oop for lock args */
13633 if (oop != NULL) {
13634 nfs4_end_open_seqid_sync(oop);
13635 open_owner_rele(oop);
13636 *oopp = NULL;
13637 }
13638
13639 crfree(*cred_otwp);
13640 *cred_otwp = base_cr;
13641 crhold(*cred_otwp);
13642 }
13643
13644 /*
13645 * Function to process the client's recovery for nfs4frlock.
13646 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13647 *
13648 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13649 * COMPOUND4 args/res for calls that need to retry.
13650 *
13651 * Note: the rp's r_lkserlock is *not* dropped during this path.
13652 */
13653 static bool_t
13654 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13655 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13656 LOCK4args *lock_args, LOCKU4args *locku_args,
13657 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13658 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13659 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13660 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13661 {
13662 nfs4_open_owner_t *oop = *oopp;
13663 nfs4_open_stream_t *osp = *ospp;
13664 nfs4_lock_owner_t *lop = *lopp;
13665
13666 bool_t abort, retry;
13667
13668 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13669 ASSERT((*argspp) != NULL);
13670 ASSERT((*respp) != NULL);
13671 if (lock_args || locku_args)
13672 ASSERT(lop != NULL);
13673
13674 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13675 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13676
13677 retry = TRUE;
13678 abort = FALSE;
13679 if (needrecov) {
13680 nfs4_bseqid_entry_t *bsep = NULL;
13681 nfs_opnum4 op;
13682
13683 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13684
13685 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13686 seqid4 seqid;
13687
13688 if (lock_args) {
13689 if (lock_args->locker.new_lock_owner == TRUE)
13690 seqid = lock_args->locker.locker4_u.
13691 open_owner.open_seqid;
13692 else
13693 seqid = lock_args->locker.locker4_u.
13694 lock_owner.lock_seqid;
13695 } else if (locku_args) {
13696 seqid = locku_args->seqid;
13697 } else {
13698 seqid = 0;
13699 }
13700
13701 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13702 flk->l_pid, (*argspp)->ctag, seqid);
13703 }
13704
13705 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13706 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13707 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13708 NULL, op, bsep, NULL, NULL);
13709
13710 if (bsep)
13711 kmem_free(bsep, sizeof (*bsep));
13712 }
13713
13714 /*
13715 * Return that we do not want to retry the request for 3 cases:
13716 * 1. If we received EINTR or are bailing out because of a forced
13717 * unmount, we came into this code path just for the sake of
13718 * initiating recovery, we now need to return the error.
13719 * 2. If we have aborted recovery.
13720 * 3. We received NFS4ERR_BAD_SEQID.
13721 */
13722 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13723 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13724 retry = FALSE;
13725
13726 if (*did_start_fop == TRUE) {
13727 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13728 needrecov);
13729 *did_start_fop = FALSE;
13730 }
13731
13732 if (retry == TRUE) {
13733 nfs_argop4 *argop;
13734
13735 argop = (*argspp)->array;
13736 ASSERT((*argspp)->array_len == 2);
13737
13738 if (argop[1].argop == OP_LOCK)
13739 nfs4args_lock_free(&argop[1]);
13740 else if (argop[1].argop == OP_LOCKT)
13741 nfs4args_lockt_free(&argop[1]);
13742 kmem_free(argop, 2 * sizeof (nfs_argop4));
13743 if (!ep->error)
13744 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13745 *respp = NULL;
13746 *argspp = NULL;
13747 }
13748
13749 if (lop != NULL) {
13750 nfs4_end_lock_seqid_sync(lop);
13751 lock_owner_rele(lop);
13752 }
13753
13754 *lopp = NULL;
13755
13756 /* need to free up the reference on osp for lock args */
13757 if (osp != NULL) {
13758 open_stream_rele(osp, rp);
13759 *ospp = NULL;
13760 }
13761
13762 /* need to free up the reference on oop for lock args */
13763 if (oop != NULL) {
13764 nfs4_end_open_seqid_sync(oop);
13765 open_owner_rele(oop);
13766 *oopp = NULL;
13767 }
13768
13769 return (retry);
13770 }
13771
13772 /*
13773 * Handles the successful reply from the server for nfs4frlock.
13774 */
13775 static void
13776 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13777 vnode_t *vp, int flag, u_offset_t offset,
13778 nfs4_lost_rqst_t *resend_rqstp)
13779 {
13780 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13781 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13782 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13783 if (ctype == NFS4_LCK_CTYPE_NORM) {
13784 flk->l_pid = ttoproc(curthread)->p_pid;
13785 /*
13786 * We do not register lost locks locally in
13787 * the 'resend' case since the user/application
13788 * doesn't think we have the lock.
13789 */
13790 ASSERT(!resend_rqstp);
13791 nfs4_register_lock_locally(vp, flk, flag, offset);
13792 }
13793 }
13794 }
13795
13796 /*
13797 * Handle the DENIED reply from the server for nfs4frlock.
13798 * Returns TRUE if we should retry the request; FALSE otherwise.
13799 *
13800 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13801 * COMPOUND4 args/res for calls that need to retry. Can also
13802 * drop and regrab the r_lkserlock.
13803 */
13804 static bool_t
13805 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13806 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13807 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13808 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13809 nfs4_recov_state_t *recov_statep, int needrecov,
13810 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13811 clock_t *tick_delayp, short *whencep, int *errorp,
13812 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13813 bool_t *skip_get_err)
13814 {
13815 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13816
13817 if (lock_args) {
13818 nfs4_open_owner_t *oop = *oopp;
13819 nfs4_open_stream_t *osp = *ospp;
13820 nfs4_lock_owner_t *lop = *lopp;
13821 int intr;
13822
13823 /*
13824 * Blocking lock needs to sleep and retry from the request.
13825 *
13826 * Do not block and wait for 'resend' or 'reinstate'
13827 * lock requests, just return the error.
13828 *
13829 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13830 */
13831 if (cmd == F_SETLKW) {
13832 rnode4_t *rp = VTOR4(vp);
13833 nfs_argop4 *argop = (*argspp)->array;
13834
13835 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13836
13837 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13838 recov_statep, needrecov);
13839 *did_start_fop = FALSE;
13840 ASSERT((*argspp)->array_len == 2);
13841 if (argop[1].argop == OP_LOCK)
13842 nfs4args_lock_free(&argop[1]);
13843 else if (argop[1].argop == OP_LOCKT)
13844 nfs4args_lockt_free(&argop[1]);
13845 kmem_free(argop, 2 * sizeof (nfs_argop4));
13846 if (*respp)
13847 (void) xdr_free(xdr_COMPOUND4res_clnt,
13848 (caddr_t)*respp);
13849 *argspp = NULL;
13850 *respp = NULL;
13851 nfs4_end_lock_seqid_sync(lop);
13852 lock_owner_rele(lop);
13853 *lopp = NULL;
13854 if (osp != NULL) {
13855 open_stream_rele(osp, rp);
13856 *ospp = NULL;
13857 }
13858 if (oop != NULL) {
13859 nfs4_end_open_seqid_sync(oop);
13860 open_owner_rele(oop);
13861 *oopp = NULL;
13862 }
13863
13864 nfs_rw_exit(&rp->r_lkserlock);
13865
13866 intr = nfs4_block_and_wait(tick_delayp, rp);
13867
13868 if (intr) {
13869 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13870 RW_WRITER, FALSE);
13871 *errorp = EINTR;
13872 return (FALSE);
13873 }
13874
13875 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13876 RW_WRITER, FALSE);
13877
13878 /*
13879 * Make sure we are still safe to lock with
13880 * regards to mmapping.
13881 */
13882 if (!nfs4_safelock(vp, flk, cr)) {
13883 *errorp = EAGAIN;
13884 return (FALSE);
13885 }
13886
13887 return (TRUE);
13888 }
13889 if (ctype == NFS4_LCK_CTYPE_NORM)
13890 *errorp = EAGAIN;
13891 *skip_get_err = TRUE;
13892 flk->l_whence = 0;
13893 *whencep = 0;
13894 return (FALSE);
13895 } else if (lockt_args) {
13896 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13897 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13898
13899 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13900 flk, lockt_args);
13901
13902 /* according to NLM code */
13903 *errorp = 0;
13904 *whencep = 0;
13905 *skip_get_err = TRUE;
13906 return (FALSE);
13907 }
13908 return (FALSE);
13909 }
13910
13911 /*
13912 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13913 */
13914 static void
13915 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13916 {
13917 switch (resp->status) {
13918 case NFS4ERR_ACCESS:
13919 case NFS4ERR_ADMIN_REVOKED:
13920 case NFS4ERR_BADHANDLE:
13921 case NFS4ERR_BAD_RANGE:
13922 case NFS4ERR_BAD_SEQID:
13923 case NFS4ERR_BAD_STATEID:
13924 case NFS4ERR_BADXDR:
13925 case NFS4ERR_DEADLOCK:
13926 case NFS4ERR_DELAY:
13927 case NFS4ERR_EXPIRED:
13928 case NFS4ERR_FHEXPIRED:
13929 case NFS4ERR_GRACE:
13930 case NFS4ERR_INVAL:
13931 case NFS4ERR_ISDIR:
13932 case NFS4ERR_LEASE_MOVED:
13933 case NFS4ERR_LOCK_NOTSUPP:
13934 case NFS4ERR_LOCK_RANGE:
13935 case NFS4ERR_MOVED:
13936 case NFS4ERR_NOFILEHANDLE:
13937 case NFS4ERR_NO_GRACE:
13938 case NFS4ERR_OLD_STATEID:
13939 case NFS4ERR_OPENMODE:
13940 case NFS4ERR_RECLAIM_BAD:
13941 case NFS4ERR_RECLAIM_CONFLICT:
13942 case NFS4ERR_RESOURCE:
13943 case NFS4ERR_SERVERFAULT:
13944 case NFS4ERR_STALE:
13945 case NFS4ERR_STALE_CLIENTID:
13946 case NFS4ERR_STALE_STATEID:
13947 return;
13948 default:
13949 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13950 "nfs4frlock_results_default: got unrecognizable "
13951 "res.status %d", resp->status));
13952 *errorp = NFS4ERR_INVAL;
13953 }
13954 }
13955
13956 /*
13957 * The lock request was successful, so update the client's state.
13958 */
13959 static void
13960 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13961 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13962 vnode_t *vp, flock64_t *flk, cred_t *cr,
13963 nfs4_lost_rqst_t *resend_rqstp)
13964 {
13965 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13966
13967 if (lock_args) {
13968 LOCK4res *lock_res;
13969
13970 lock_res = &resop->nfs_resop4_u.oplock;
13971 /* update the stateid with server's response */
13972
13973 if (lock_args->locker.new_lock_owner == TRUE) {
13974 mutex_enter(&lop->lo_lock);
13975 lop->lo_just_created = NFS4_PERM_CREATED;
13976 mutex_exit(&lop->lo_lock);
13977 }
13978
13979 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13980
13981 /*
13982 * If the lock was the result of a resending a lost
13983 * request, we've synched up the stateid and seqid
13984 * with the server, but now the server might be out of sync
13985 * with what the application thinks it has for locks.
13986 * Clean that up here. It's unclear whether we should do
13987 * this even if the filesystem has been forcibly unmounted.
13988 * For most servers, it's probably wasted effort, but
13989 * RFC3530 lets servers require that unlocks exactly match
13990 * the locks that are held.
13991 */
13992 if (resend_rqstp != NULL &&
13993 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
13994 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
13995 } else {
13996 flk->l_whence = 0;
13997 }
13998 } else if (locku_args) {
13999 LOCKU4res *locku_res;
14000
14001 locku_res = &resop->nfs_resop4_u.oplocku;
14002
14003 /* Update the stateid with the server's response */
14004 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14005 } else if (lockt_args) {
14006 /* Switch the lock type to express success, see fcntl */
14007 flk->l_type = F_UNLCK;
14008 flk->l_whence = 0;
14009 }
14010 }
14011
14012 /*
14013 * Do final cleanup before exiting nfs4frlock.
14014 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14015 * COMPOUND4 args/res for calls that haven't already.
14016 */
14017 static void
14018 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14019 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14020 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14021 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14022 short whence, u_offset_t offset, struct lm_sysid *ls,
14023 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14024 bool_t did_start_fop, bool_t skip_get_err,
14025 cred_t *cred_otw, cred_t *cred)
14026 {
14027 mntinfo4_t *mi = VTOMI4(vp);
14028 rnode4_t *rp = VTOR4(vp);
14029 int error = *errorp;
14030 nfs_argop4 *argop;
14031 int do_flush_pages = 0;
14032
14033 ASSERT(nfs_zone() == mi->mi_zone);
14034 /*
14035 * The client recovery code wants the raw status information,
14036 * so don't map the NFS status code to an errno value for
14037 * non-normal call types.
14038 */
14039 if (ctype == NFS4_LCK_CTYPE_NORM) {
14040 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14041 *errorp = geterrno4(resp->status);
14042 if (did_start_fop == TRUE)
14043 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14044 needrecov);
14045
14046 /*
14047 * We've established a new lock on the server, so invalidate
14048 * the pages associated with the vnode to get the most up to
14049 * date pages from the server after acquiring the lock. We
14050 * want to be sure that the read operation gets the newest data.
14051 * N.B.
14052 * We used to do this in nfs4frlock_results_ok but that doesn't
14053 * work since VOP_PUTPAGE can call nfs4_commit which calls
14054 * nfs4_start_fop. We flush the pages below after calling
14055 * nfs4_end_fop above
14056 * The flush of the page cache must be done after
14057 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14058 */
14059 if (!error && resp && resp->status == NFS4_OK)
14060 do_flush_pages = 1;
14061 }
14062 if (argsp) {
14063 ASSERT(argsp->array_len == 2);
14064 argop = argsp->array;
14065 if (argop[1].argop == OP_LOCK)
14066 nfs4args_lock_free(&argop[1]);
14067 else if (argop[1].argop == OP_LOCKT)
14068 nfs4args_lockt_free(&argop[1]);
14069 kmem_free(argop, 2 * sizeof (nfs_argop4));
14070 if (resp)
14071 (void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14072 }
14073
14074 /* free the reference on the lock owner */
14075 if (lop != NULL) {
14076 nfs4_end_lock_seqid_sync(lop);
14077 lock_owner_rele(lop);
14078 }
14079
14080 /* need to free up the reference on osp for lock args */
14081 if (osp != NULL)
14082 open_stream_rele(osp, rp);
14083
14084 /* need to free up the reference on oop for lock args */
14085 if (oop != NULL) {
14086 nfs4_end_open_seqid_sync(oop);
14087 open_owner_rele(oop);
14088 }
14089
14090 if (do_flush_pages)
14091 nfs4_flush_pages(vp, cred);
14092
14093 (void) convoff(vp, flk, whence, offset);
14094
14095 lm_rel_sysid(ls);
14096
14097 /*
14098 * Record debug information in the event we get EINVAL.
14099 */
14100 mutex_enter(&mi->mi_lock);
14101 if (*errorp == EINVAL && (lock_args || locku_args) &&
14102 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14103 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14104 zcmn_err(getzoneid(), CE_NOTE,
14105 "%s operation failed with "
14106 "EINVAL probably since the server, %s,"
14107 " doesn't support POSIX style locking",
14108 lock_args ? "LOCK" : "LOCKU",
14109 mi->mi_curr_serv->sv_hostname);
14110 mi->mi_flags |= MI4_LOCK_DEBUG;
14111 }
14112 }
14113 mutex_exit(&mi->mi_lock);
14114
14115 if (cred_otw)
14116 crfree(cred_otw);
14117 }
14118
14119 /*
14120 * This calls the server and the local locking code.
14121 *
14122 * Client locks are registerred locally by oring the sysid with
14123 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14124 * We need to distinguish between the two to avoid collision in case one
14125 * machine is used as both client and server.
14126 *
14127 * Blocking lock requests will continually retry to acquire the lock
14128 * forever.
14129 *
14130 * The ctype is defined as follows:
14131 * NFS4_LCK_CTYPE_NORM: normal lock request.
14132 *
14133 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14134 * recovery, get the pid from flk instead of curproc, and don't reregister
14135 * the lock locally.
14136 *
14137 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14138 * that we will use the information passed in via resend_rqstp to setup the
14139 * lock/locku request. This resend is the exact same request as the 'lost
14140 * lock', and is initiated by the recovery framework. A successful resend
14141 * request can initiate one or more reinstate requests.
14142 *
14143 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14144 * does not trigger additional reinstate requests. This lock call type is
14145 * set for setting the v4 server's locking state back to match what the
14146 * client's local locking state is in the event of a received 'lost lock'.
14147 *
14148 * Errors are returned via the nfs4_error_t parameter.
14149 */
14150 void
14151 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14152 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14153 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14154 {
14155 COMPOUND4args_clnt args, *argsp = NULL;
14156 COMPOUND4res_clnt res, *resp = NULL;
14157 nfs_argop4 *argop;
14158 nfs_resop4 *resop;
14159 rnode4_t *rp;
14160 int doqueue = 1;
14161 clock_t tick_delay; /* delay in clock ticks */
14162 struct lm_sysid *ls;
14163 LOCK4args *lock_args = NULL;
14164 LOCKU4args *locku_args = NULL;
14165 LOCKT4args *lockt_args = NULL;
14166 nfs4_open_owner_t *oop = NULL;
14167 nfs4_open_stream_t *osp = NULL;
14168 nfs4_lock_owner_t *lop = NULL;
14169 bool_t needrecov = FALSE;
14170 nfs4_recov_state_t recov_state;
14171 short whence;
14172 nfs4_op_hint_t op_hint;
14173 nfs4_lost_rqst_t lost_rqst;
14174 bool_t retry = FALSE;
14175 bool_t did_start_fop = FALSE;
14176 bool_t skip_get_err = FALSE;
14177 cred_t *cred_otw = NULL;
14178 bool_t recovonly; /* just queue request */
14179 int frc_no_reclaim = 0;
14180 #ifdef DEBUG
14181 char *name;
14182 #endif
14183
14184 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14185
14186 #ifdef DEBUG
14187 name = fn_name(VTOSV(vp)->sv_name);
14188 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14189 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14190 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14191 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14192 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14193 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14194 resend_rqstp ? "TRUE" : "FALSE"));
14195 kmem_free(name, MAXNAMELEN);
14196 #endif
14197
14198 nfs4_error_zinit(ep);
14199 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14200 if (ep->error)
14201 return;
14202 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14203 if (ep->error)
14204 return;
14205 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14206 vp, cr, &cred_otw);
14207
14208 recov_retry:
14209 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14210 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14211 rp = VTOR4(vp);
14212
14213 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14214 &did_start_fop, &recovonly);
14215
14216 if (ep->error)
14217 goto out;
14218
14219 if (recovonly) {
14220 /*
14221 * Leave the request for the recovery system to deal with.
14222 */
14223 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14224 ASSERT(cmd != F_GETLK);
14225 ASSERT(flk->l_type == F_UNLCK);
14226
14227 nfs4_error_init(ep, EINTR);
14228 needrecov = TRUE;
14229 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14230 if (lop != NULL) {
14231 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14232 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14233 (void) nfs4_start_recovery(ep,
14234 VTOMI4(vp), vp, NULL, NULL,
14235 (lost_rqst.lr_op == OP_LOCK ||
14236 lost_rqst.lr_op == OP_LOCKU) ?
14237 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14238 lock_owner_rele(lop);
14239 lop = NULL;
14240 }
14241 flk->l_pid = curproc->p_pid;
14242 nfs4_register_lock_locally(vp, flk, flag, offset);
14243 goto out;
14244 }
14245
14246 /* putfh directory fh */
14247 argop[0].argop = OP_CPUTFH;
14248 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14249
14250 /*
14251 * Set up the over-the-wire arguments and get references to the
14252 * open owner, etc.
14253 */
14254
14255 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14256 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14257 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14258 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14259 } else {
14260 bool_t go_otw = TRUE;
14261
14262 ASSERT(resend_rqstp == NULL);
14263
14264 switch (cmd) {
14265 case F_GETLK:
14266 case F_O_GETLK:
14267 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14268 &lockt_args, argsp, flk, rp);
14269 break;
14270 case F_SETLKW:
14271 case F_SETLK:
14272 if (flk->l_type == F_UNLCK)
14273 nfs4frlock_setup_locku_args(ctype,
14274 &argop[1], &locku_args, flk,
14275 &lop, ep, argsp,
14276 vp, flag, offset, cr,
14277 &skip_get_err, &go_otw);
14278 else
14279 nfs4frlock_setup_lock_args(ctype,
14280 &lock_args, &oop, &osp, &lop, &argop[1],
14281 argsp, flk, cmd, vp, cr, ep);
14282
14283 if (ep->error)
14284 goto out;
14285
14286 switch (ep->stat) {
14287 case NFS4_OK:
14288 break;
14289 case NFS4ERR_DELAY:
14290 /* recov thread never gets this error */
14291 ASSERT(resend_rqstp == NULL);
14292 ASSERT(did_start_fop);
14293
14294 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14295 &recov_state, TRUE);
14296 did_start_fop = FALSE;
14297 if (argop[1].argop == OP_LOCK)
14298 nfs4args_lock_free(&argop[1]);
14299 else if (argop[1].argop == OP_LOCKT)
14300 nfs4args_lockt_free(&argop[1]);
14301 kmem_free(argop, 2 * sizeof (nfs_argop4));
14302 argsp = NULL;
14303 goto recov_retry;
14304 default:
14305 ep->error = EIO;
14306 goto out;
14307 }
14308 break;
14309 default:
14310 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14311 "nfs4_frlock: invalid cmd %d", cmd));
14312 ep->error = EINVAL;
14313 goto out;
14314 }
14315
14316 if (!go_otw)
14317 goto out;
14318 }
14319
14320 /* XXX should we use the local reclock as a cache ? */
14321 /*
14322 * Unregister the lock with the local locking code before
14323 * contacting the server. This avoids a potential race where
14324 * another process gets notified that it has been granted a lock
14325 * before we can unregister ourselves locally.
14326 */
14327 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14328 if (ctype == NFS4_LCK_CTYPE_NORM)
14329 flk->l_pid = ttoproc(curthread)->p_pid;
14330 nfs4_register_lock_locally(vp, flk, flag, offset);
14331 }
14332
14333 /*
14334 * Send the server the lock request. Continually loop with a delay
14335 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14336 */
14337 resp = &res;
14338
14339 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14340 (CE_NOTE,
14341 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14342 rnode4info(rp)));
14343
14344 if (lock_args && frc_no_reclaim) {
14345 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14346 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14347 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14348 lock_args->reclaim = FALSE;
14349 if (did_reclaimp)
14350 *did_reclaimp = 0;
14351 }
14352
14353 /*
14354 * Do the OTW call.
14355 */
14356 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14357
14358 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14359 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14360
14361 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14362 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14363 "nfs4frlock: needrecov %d", needrecov));
14364
14365 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14366 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14367 args.ctag);
14368
14369 /*
14370 * Check if one of these mutually exclusive error cases has
14371 * happened:
14372 * need to swap credentials due to access error
14373 * recovery is needed
14374 * different error (only known case is missing Kerberos ticket)
14375 */
14376
14377 if ((ep->error == EACCES ||
14378 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14379 cred_otw != cr) {
14380 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14381 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14382 cr, &cred_otw);
14383 goto recov_retry;
14384 }
14385
14386 if (needrecov) {
14387 /*
14388 * LOCKT requests don't need to recover from lost
14389 * requests since they don't create/modify state.
14390 */
14391 if ((ep->error == EINTR ||
14392 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14393 lockt_args)
14394 goto out;
14395 /*
14396 * Do not attempt recovery for requests initiated by
14397 * the recovery framework. Let the framework redrive them.
14398 */
14399 if (ctype != NFS4_LCK_CTYPE_NORM)
14400 goto out;
14401 else {
14402 ASSERT(resend_rqstp == NULL);
14403 }
14404
14405 nfs4frlock_save_lost_rqst(ctype, ep->error,
14406 flk_to_locktype(cmd, flk->l_type),
14407 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14408
14409 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14410 &resp, lock_args, locku_args, &oop, &osp, &lop,
14411 rp, vp, &recov_state, op_hint, &did_start_fop,
14412 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14413
14414 if (retry) {
14415 ASSERT(oop == NULL);
14416 ASSERT(osp == NULL);
14417 ASSERT(lop == NULL);
14418 goto recov_retry;
14419 }
14420 goto out;
14421 }
14422
14423 /*
14424 * Bail out if have reached this point with ep->error set. Can
14425 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14426 * This happens if Kerberos ticket has expired or has been
14427 * destroyed.
14428 */
14429 if (ep->error != 0)
14430 goto out;
14431
14432 /*
14433 * Process the reply.
14434 */
14435 switch (resp->status) {
14436 case NFS4_OK:
14437 resop = &resp->array[1];
14438 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14439 resend_rqstp);
14440 /*
14441 * Have a successful lock operation, now update state.
14442 */
14443 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14444 resop, lop, vp, flk, cr, resend_rqstp);
14445 break;
14446
14447 case NFS4ERR_DENIED:
14448 resop = &resp->array[1];
14449 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14450 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14451 &recov_state, needrecov, &argsp, &resp,
14452 &tick_delay, &whence, &ep->error, resop, cr,
14453 &did_start_fop, &skip_get_err);
14454
14455 if (retry) {
14456 ASSERT(oop == NULL);
14457 ASSERT(osp == NULL);
14458 ASSERT(lop == NULL);
14459 goto recov_retry;
14460 }
14461 break;
14462 /*
14463 * If the server won't let us reclaim, fall-back to trying to lock
14464 * the file from scratch. Code elsewhere will check the changeinfo
14465 * to ensure the file hasn't been changed.
14466 */
14467 case NFS4ERR_NO_GRACE:
14468 if (lock_args && lock_args->reclaim == TRUE) {
14469 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14470 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14471 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14472 frc_no_reclaim = 1;
14473 /* clean up before retrying */
14474 needrecov = 0;
14475 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14476 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14477 &recov_state, op_hint, &did_start_fop, NULL, flk);
14478 goto recov_retry;
14479 }
14480 /* FALLTHROUGH */
14481
14482 default:
14483 nfs4frlock_results_default(resp, &ep->error);
14484 break;
14485 }
14486 out:
14487 /*
14488 * Process and cleanup from error. Make interrupted unlock
14489 * requests look successful, since they will be handled by the
14490 * client recovery code.
14491 */
14492 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14493 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14494 lock_args, locku_args, did_start_fop,
14495 skip_get_err, cred_otw, cr);
14496
14497 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14498 (cmd == F_SETLK || cmd == F_SETLKW))
14499 ep->error = 0;
14500 }
14501
14502 /*
14503 * nfs4_safelock:
14504 *
14505 * Return non-zero if the given lock request can be handled without
14506 * violating the constraints on concurrent mapping and locking.
14507 */
14508
14509 static int
14510 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14511 {
14512 rnode4_t *rp = VTOR4(vp);
14513 struct vattr va;
14514 int error;
14515
14516 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14517 ASSERT(rp->r_mapcnt >= 0);
14518 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14519 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14520 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14521 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14522
14523 if (rp->r_mapcnt == 0)
14524 return (1); /* always safe if not mapped */
14525
14526 /*
14527 * If the file is already mapped and there are locks, then they
14528 * should be all safe locks. So adding or removing a lock is safe
14529 * as long as the new request is safe (i.e., whole-file, meaning
14530 * length and starting offset are both zero).
14531 */
14532
14533 if (bfp->l_start != 0 || bfp->l_len != 0) {
14534 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14535 "cannot lock a memory mapped file unless locking the "
14536 "entire file: start %"PRIx64", len %"PRIx64,
14537 bfp->l_start, bfp->l_len));
14538 return (0);
14539 }
14540
14541 /* mandatory locking and mapping don't mix */
14542 va.va_mask = AT_MODE;
14543 error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14544 if (error != 0) {
14545 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14546 "getattr error %d", error));
14547 return (0); /* treat errors conservatively */
14548 }
14549 if (MANDLOCK(vp, va.va_mode)) {
14550 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14551 "cannot mandatory lock and mmap a file"));
14552 return (0);
14553 }
14554
14555 return (1);
14556 }
14557
14558
14559 /*
14560 * Register the lock locally within Solaris.
14561 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14562 * recording locks locally.
14563 *
14564 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14565 * are registered locally.
14566 */
14567 void
14568 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14569 u_offset_t offset)
14570 {
14571 int oldsysid;
14572 int error;
14573 #ifdef DEBUG
14574 char *name;
14575 #endif
14576
14577 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14578
14579 #ifdef DEBUG
14580 name = fn_name(VTOSV(vp)->sv_name);
14581 NFS4_DEBUG(nfs4_client_lock_debug,
14582 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14583 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14584 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14585 flk->l_sysid));
14586 kmem_free(name, MAXNAMELEN);
14587 #endif
14588
14589 /* register the lock with local locking */
14590 oldsysid = flk->l_sysid;
14591 flk->l_sysid |= LM_SYSID_CLIENT;
14592 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14593 #ifdef DEBUG
14594 if (error != 0) {
14595 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14596 "nfs4_register_lock_locally: could not register with"
14597 " local locking"));
14598 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14599 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14600 error, (void *)vp, flk->l_pid, flk->l_sysid));
14601 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14602 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14603 flk->l_type, flk->l_start, flk->l_len));
14604 (void) reclock(vp, flk, 0, flag, offset, NULL);
14605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14606 "blocked by pid %d sysid 0x%x type %d "
14607 "off 0x%" PRIx64 " len 0x%" PRIx64,
14608 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14609 flk->l_len));
14610 }
14611 #endif
14612 flk->l_sysid = oldsysid;
14613 }
14614
14615 /*
14616 * nfs4_lockrelease:
14617 *
14618 * Release any locks on the given vnode that are held by the current
14619 * process. Also removes the lock owner (if one exists) from the rnode's
14620 * list.
14621 */
14622 static int
14623 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14624 {
14625 flock64_t ld;
14626 int ret, error;
14627 rnode4_t *rp;
14628 nfs4_lock_owner_t *lop;
14629 nfs4_recov_state_t recov_state;
14630 mntinfo4_t *mi;
14631 bool_t possible_orphan = FALSE;
14632 bool_t recovonly;
14633
14634 ASSERT((uintptr_t)vp > KERNELBASE);
14635 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14636
14637 rp = VTOR4(vp);
14638 mi = VTOMI4(vp);
14639
14640 /*
14641 * If we have not locked anything then we can
14642 * just return since we have no work to do.
14643 */
14644 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14645 return (0);
14646 }
14647
14648 /*
14649 * We need to comprehend that another thread may
14650 * kick off recovery and the lock_owner we have stashed
14651 * in lop might be invalid so we should NOT cache it
14652 * locally!
14653 */
14654 recov_state.rs_flags = 0;
14655 recov_state.rs_num_retry_despite_err = 0;
14656 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14657 &recovonly);
14658 if (error) {
14659 mutex_enter(&rp->r_statelock);
14660 rp->r_flags |= R4LODANGLERS;
14661 mutex_exit(&rp->r_statelock);
14662 return (error);
14663 }
14664
14665 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14666
14667 /*
14668 * Check if the lock owner might have a lock (request was sent but
14669 * no response was received). Also check if there are any remote
14670 * locks on the file. (In theory we shouldn't have to make this
14671 * second check if there's no lock owner, but for now we'll be
14672 * conservative and do it anyway.) If either condition is true,
14673 * send an unlock for the entire file to the server.
14674 *
14675 * Note that no explicit synchronization is needed here. At worst,
14676 * flk_has_remote_locks() will return a false positive, in which case
14677 * the unlock call wastes time but doesn't harm correctness.
14678 */
14679
14680 if (lop) {
14681 mutex_enter(&lop->lo_lock);
14682 possible_orphan = lop->lo_pending_rqsts;
14683 mutex_exit(&lop->lo_lock);
14684 lock_owner_rele(lop);
14685 }
14686
14687 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14688
14689 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14690 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14691 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14692 (void *)lop));
14693
14694 if (possible_orphan || flk_has_remote_locks(vp)) {
14695 ld.l_type = F_UNLCK; /* set to unlock entire file */
14696 ld.l_whence = 0; /* unlock from start of file */
14697 ld.l_start = 0;
14698 ld.l_len = 0; /* do entire file */
14699
14700 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14701 cr, NULL);
14702
14703 if (ret != 0) {
14704 /*
14705 * If VOP_FRLOCK fails, make sure we unregister
14706 * local locks before we continue.
14707 */
14708 ld.l_pid = ttoproc(curthread)->p_pid;
14709 nfs4_register_lock_locally(vp, &ld, flag, offset);
14710 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14711 "nfs4_lockrelease: lock release error on vp"
14712 " %p: error %d.\n", (void *)vp, ret));
14713 }
14714 }
14715
14716 recov_state.rs_flags = 0;
14717 recov_state.rs_num_retry_despite_err = 0;
14718 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14719 &recovonly);
14720 if (error) {
14721 mutex_enter(&rp->r_statelock);
14722 rp->r_flags |= R4LODANGLERS;
14723 mutex_exit(&rp->r_statelock);
14724 return (error);
14725 }
14726
14727 /*
14728 * So, here we're going to need to retrieve the lock-owner
14729 * again (in case recovery has done a switch-a-roo) and
14730 * remove it because we can.
14731 */
14732 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14733
14734 if (lop) {
14735 nfs4_rnode_remove_lock_owner(rp, lop);
14736 lock_owner_rele(lop);
14737 }
14738
14739 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14740 return (0);
14741 }
14742
14743 /*
14744 * Wait for 'tick_delay' clock ticks.
14745 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14746 * NOTE: lock_lease_time is in seconds.
14747 *
14748 * XXX For future improvements, should implement a waiting queue scheme.
14749 */
14750 static int
14751 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14752 {
14753 long milliseconds_delay;
14754 time_t lock_lease_time;
14755
14756 /* wait tick_delay clock ticks or siginteruptus */
14757 if (delay_sig(*tick_delay)) {
14758 return (EINTR);
14759 }
14760 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14761 "reissue the lock request: blocked for %ld clock ticks: %ld "
14762 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14763
14764 /* get the lease time */
14765 lock_lease_time = r2lease_time(rp);
14766
14767 /* drv_hztousec converts ticks to microseconds */
14768 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14769 if (milliseconds_delay < lock_lease_time * 1000) {
14770 *tick_delay = 2 * *tick_delay;
14771 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14772 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14773 }
14774 return (0);
14775 }
14776
14777
14778 void
14779 nfs4_vnops_init(void)
14780 {
14781 }
14782
14783 void
14784 nfs4_vnops_fini(void)
14785 {
14786 }
14787
14788 /*
14789 * Return a reference to the directory (parent) vnode for a given vnode,
14790 * using the saved pathname information and the directory file handle. The
14791 * caller is responsible for disposing of the reference.
14792 * Returns zero or an errno value.
14793 *
14794 * Caller should set need_start_op to FALSE if it is the recovery
14795 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14796 */
14797 int
14798 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14799 {
14800 svnode_t *svnp;
14801 vnode_t *dvp = NULL;
14802 servinfo4_t *svp;
14803 nfs4_fname_t *mfname;
14804 int error;
14805
14806 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14807
14808 if (vp->v_flag & VROOT) {
14809 nfs4_sharedfh_t *sfh;
14810 nfs_fh4 fh;
14811 mntinfo4_t *mi;
14812
14813 ASSERT(vp->v_type == VREG);
14814
14815 mi = VTOMI4(vp);
14816 svp = mi->mi_curr_serv;
14817 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14818 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14819 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14820 sfh = sfh4_get(&fh, VTOMI4(vp));
14821 nfs_rw_exit(&svp->sv_lock);
14822 mfname = mi->mi_fname;
14823 fn_hold(mfname);
14824 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14825 sfh4_rele(&sfh);
14826
14827 if (dvp->v_type == VNON)
14828 dvp->v_type = VDIR;
14829 *dvpp = dvp;
14830 return (0);
14831 }
14832
14833 svnp = VTOSV(vp);
14834
14835 if (svnp == NULL) {
14836 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14837 "shadow node is NULL"));
14838 return (EINVAL);
14839 }
14840
14841 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14842 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14843 "shadow node name or dfh val == NULL"));
14844 return (EINVAL);
14845 }
14846
14847 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14848 (int)need_start_op);
14849 if (error != 0) {
14850 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14851 "nfs4_make_dotdot returned %d", error));
14852 return (error);
14853 }
14854 if (!dvp) {
14855 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14856 "nfs4_make_dotdot returned a NULL dvp"));
14857 return (EIO);
14858 }
14859 if (dvp->v_type == VNON)
14860 dvp->v_type = VDIR;
14861 ASSERT(dvp->v_type == VDIR);
14862 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14863 mutex_enter(&dvp->v_lock);
14864 dvp->v_flag |= V_XATTRDIR;
14865 mutex_exit(&dvp->v_lock);
14866 }
14867 *dvpp = dvp;
14868 return (0);
14869 }
14870
14871 /*
14872 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14873 * length that fnamep can accept, including the trailing null.
14874 * Returns 0 if okay, returns an errno value if there was a problem.
14875 */
14876
14877 int
14878 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14879 {
14880 char *fn;
14881 int err = 0;
14882 servinfo4_t *svp;
14883 svnode_t *shvp;
14884
14885 /*
14886 * If the file being opened has VROOT set, then this is
14887 * a "file" mount. sv_name will not be interesting, so
14888 * go back to the servinfo4 to get the original mount
14889 * path and strip off all but the final edge. Otherwise
14890 * just return the name from the shadow vnode.
14891 */
14892
14893 if (vp->v_flag & VROOT) {
14894
14895 svp = VTOMI4(vp)->mi_curr_serv;
14896 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14897
14898 fn = strrchr(svp->sv_path, '/');
14899 if (fn == NULL)
14900 err = EINVAL;
14901 else
14902 fn++;
14903 } else {
14904 shvp = VTOSV(vp);
14905 fn = fn_name(shvp->sv_name);
14906 }
14907
14908 if (err == 0)
14909 if (strlen(fn) < maxlen)
14910 (void) strcpy(fnamep, fn);
14911 else
14912 err = ENAMETOOLONG;
14913
14914 if (vp->v_flag & VROOT)
14915 nfs_rw_exit(&svp->sv_lock);
14916 else
14917 kmem_free(fn, MAXNAMELEN);
14918
14919 return (err);
14920 }
14921
14922 /*
14923 * Bookkeeping for a close that doesn't need to go over the wire.
14924 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14925 * it is left at 1.
14926 */
14927 void
14928 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14929 {
14930 rnode4_t *rp;
14931 mntinfo4_t *mi;
14932
14933 mi = VTOMI4(vp);
14934 rp = VTOR4(vp);
14935
14936 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14937 "rp=%p osp=%p", (void *)rp, (void *)osp));
14938 ASSERT(nfs_zone() == mi->mi_zone);
14939 ASSERT(mutex_owned(&osp->os_sync_lock));
14940 ASSERT(*have_lockp);
14941
14942 if (!osp->os_valid ||
14943 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14944 return;
14945 }
14946
14947 /*
14948 * This removes the reference obtained at OPEN; ie,
14949 * when the open stream structure was created.
14950 *
14951 * We don't have to worry about calling 'open_stream_rele'
14952 * since we our currently holding a reference to this
14953 * open stream which means the count can not go to 0 with
14954 * this decrement.
14955 */
14956 ASSERT(osp->os_ref_count >= 2);
14957 osp->os_ref_count--;
14958 osp->os_valid = 0;
14959 mutex_exit(&osp->os_sync_lock);
14960 *have_lockp = 0;
14961
14962 nfs4_dec_state_ref_count(mi);
14963 }
14964
14965 /*
14966 * Close all remaining open streams on the rnode. These open streams
14967 * could be here because:
14968 * - The close attempted at either close or delmap failed
14969 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14970 * - Someone did mknod on a regular file but never opened it
14971 */
14972 int
14973 nfs4close_all(vnode_t *vp, cred_t *cr)
14974 {
14975 nfs4_open_stream_t *osp;
14976 int error;
14977 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14978 rnode4_t *rp;
14979
14980 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14981
14982 error = 0;
14983 rp = VTOR4(vp);
14984
14985 /*
14986 * At this point, all we know is that the last time
14987 * someone called vn_rele, the count was 1. Since then,
14988 * the vnode could have been re-activated. We want to
14989 * loop through the open streams and close each one, but
14990 * we have to be careful since once we release the rnode
14991 * hash bucket lock, someone else is free to come in and
14992 * re-activate the rnode and add new open streams. The
14993 * strategy is take the rnode hash bucket lock, verify that
14994 * the count is still 1, grab the open stream off the
14995 * head of the list and mark it invalid, then release the
14996 * rnode hash bucket lock and proceed with that open stream.
14997 * This is ok because nfs4close_one() will acquire the proper
14998 * open/create to close/destroy synchronization for open
14999 * streams, and will ensure that if someone has reopened
15000 * the open stream after we've dropped the hash bucket lock
15001 * then we'll just simply return without destroying the
15002 * open stream.
15003 * Repeat until the list is empty.
15004 */
15005
15006 for (;;) {
15007
15008 /* make sure vnode hasn't been reactivated */
15009 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15010 mutex_enter(&vp->v_lock);
15011 if (vp->v_count > 1) {
15012 mutex_exit(&vp->v_lock);
15013 rw_exit(&rp->r_hashq->r_lock);
15014 break;
15015 }
15016 /*
15017 * Grabbing r_os_lock before releasing v_lock prevents
15018 * a window where the rnode/open stream could get
15019 * reactivated (and os_force_close set to 0) before we
15020 * had a chance to set os_force_close to 1.
15021 */
15022 mutex_enter(&rp->r_os_lock);
15023 mutex_exit(&vp->v_lock);
15024
15025 osp = list_head(&rp->r_open_streams);
15026 if (!osp) {
15027 /* nothing left to CLOSE OTW, so return */
15028 mutex_exit(&rp->r_os_lock);
15029 rw_exit(&rp->r_hashq->r_lock);
15030 break;
15031 }
15032
15033 mutex_enter(&rp->r_statev4_lock);
15034 /* the file can't still be mem mapped */
15035 ASSERT(rp->r_mapcnt == 0);
15036 if (rp->created_v4)
15037 rp->created_v4 = 0;
15038 mutex_exit(&rp->r_statev4_lock);
15039
15040 /*
15041 * Grab a ref on this open stream; nfs4close_one
15042 * will mark it as invalid
15043 */
15044 mutex_enter(&osp->os_sync_lock);
15045 osp->os_ref_count++;
15046 osp->os_force_close = 1;
15047 mutex_exit(&osp->os_sync_lock);
15048 mutex_exit(&rp->r_os_lock);
15049 rw_exit(&rp->r_hashq->r_lock);
15050
15051 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15052
15053 /* Update error if it isn't already non-zero */
15054 if (error == 0) {
15055 if (e.error)
15056 error = e.error;
15057 else if (e.stat)
15058 error = geterrno4(e.stat);
15059 }
15060
15061 #ifdef DEBUG
15062 nfs4close_all_cnt++;
15063 #endif
15064 /* Release the ref on osp acquired above. */
15065 open_stream_rele(osp, rp);
15066
15067 /* Proceed to the next open stream, if any */
15068 }
15069 return (error);
15070 }
15071
15072 /*
15073 * nfs4close_one - close one open stream for a file if needed.
15074 *
15075 * "close_type" indicates which close path this is:
15076 * CLOSE_NORM: close initiated via VOP_CLOSE.
15077 * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15078 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces
15079 * the close and release of client state for this open stream
15080 * (unless someone else has the open stream open).
15081 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15082 * (e.g., due to abort because of a signal).
15083 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15084 *
15085 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15086 * recovery. Instead, the caller is expected to deal with retries.
15087 *
15088 * The caller can either pass in the osp ('provided_osp') or not.
15089 *
15090 * 'access_bits' represents the access we are closing/downgrading.
15091 *
15092 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15093 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15094 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15095 *
15096 * Errors are returned via the nfs4_error_t.
15097 */
15098 void
15099 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15100 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15101 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15102 uint_t mmap_flags)
15103 {
15104 nfs4_open_owner_t *oop;
15105 nfs4_open_stream_t *osp = NULL;
15106 int retry = 0;
15107 int num_retries = NFS4_NUM_RECOV_RETRIES;
15108 rnode4_t *rp;
15109 mntinfo4_t *mi;
15110 nfs4_recov_state_t recov_state;
15111 cred_t *cred_otw = NULL;
15112 bool_t recovonly = FALSE;
15113 int isrecov;
15114 int force_close;
15115 int close_failed = 0;
15116 int did_dec_count = 0;
15117 int did_start_op = 0;
15118 int did_force_recovlock = 0;
15119 int did_start_seqid_sync = 0;
15120 int have_sync_lock = 0;
15121
15122 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15123
15124 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15125 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15126 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15127 len, maxprot, mmap_flags, access_bits));
15128
15129 nfs4_error_zinit(ep);
15130 rp = VTOR4(vp);
15131 mi = VTOMI4(vp);
15132 isrecov = (close_type == CLOSE_RESEND ||
15133 close_type == CLOSE_AFTER_RESEND);
15134
15135 /*
15136 * First get the open owner.
15137 */
15138 if (!provided_osp) {
15139 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15140 } else {
15141 oop = provided_osp->os_open_owner;
15142 ASSERT(oop != NULL);
15143 open_owner_hold(oop);
15144 }
15145
15146 if (!oop) {
15147 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15148 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15149 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15150 (void *)provided_osp, close_type));
15151 ep->error = EIO;
15152 goto out;
15153 }
15154
15155 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15156 recov_retry:
15157 osp = NULL;
15158 close_failed = 0;
15159 force_close = (close_type == CLOSE_FORCE);
15160 retry = 0;
15161 did_start_op = 0;
15162 did_force_recovlock = 0;
15163 did_start_seqid_sync = 0;
15164 have_sync_lock = 0;
15165 recovonly = FALSE;
15166 recov_state.rs_flags = 0;
15167 recov_state.rs_num_retry_despite_err = 0;
15168
15169 /*
15170 * Second synchronize with recovery.
15171 */
15172 if (!isrecov) {
15173 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15174 &recov_state, &recovonly);
15175 if (!ep->error) {
15176 did_start_op = 1;
15177 } else {
15178 close_failed = 1;
15179 /*
15180 * If we couldn't get start_fop, but have to
15181 * cleanup state, then at least acquire the
15182 * mi_recovlock so we can synchronize with
15183 * recovery.
15184 */
15185 if (close_type == CLOSE_FORCE) {
15186 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15187 RW_READER, FALSE);
15188 did_force_recovlock = 1;
15189 } else
15190 goto out;
15191 }
15192 }
15193
15194 /*
15195 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15196 * set 'recovonly' to TRUE since most likely this is due to
15197 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15198 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15199 * to retry, causing us to loop until recovery finishes. Plus we
15200 * don't need protection over the open seqid since we're not going
15201 * OTW, hence don't need to use the seqid.
15202 */
15203 if (recovonly == FALSE) {
15204 /* need to grab the open owner sync before 'os_sync_lock' */
15205 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15206 if (ep->error == EAGAIN) {
15207 ASSERT(!isrecov);
15208 if (did_start_op)
15209 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15210 &recov_state, TRUE);
15211 if (did_force_recovlock)
15212 nfs_rw_exit(&mi->mi_recovlock);
15213 goto recov_retry;
15214 }
15215 did_start_seqid_sync = 1;
15216 }
15217
15218 /*
15219 * Third get an open stream and acquire 'os_sync_lock' to
15220 * sychronize the opening/creating of an open stream with the
15221 * closing/destroying of an open stream.
15222 */
15223 if (!provided_osp) {
15224 /* returns with 'os_sync_lock' held */
15225 osp = find_open_stream(oop, rp);
15226 if (!osp) {
15227 ep->error = EIO;
15228 goto out;
15229 }
15230 } else {
15231 osp = provided_osp;
15232 open_stream_hold(osp);
15233 mutex_enter(&osp->os_sync_lock);
15234 }
15235 have_sync_lock = 1;
15236
15237 ASSERT(oop == osp->os_open_owner);
15238
15239 /*
15240 * Fourth, do any special pre-OTW CLOSE processing
15241 * based on the specific close type.
15242 */
15243 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15244 !did_dec_count) {
15245 ASSERT(osp->os_open_ref_count > 0);
15246 osp->os_open_ref_count--;
15247 did_dec_count = 1;
15248 if (osp->os_open_ref_count == 0)
15249 osp->os_final_close = 1;
15250 }
15251
15252 if (close_type == CLOSE_FORCE) {
15253 /* see if somebody reopened the open stream. */
15254 if (!osp->os_force_close) {
15255 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15256 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15257 "was reopened, vp %p", (void *)osp, (void *)vp));
15258 ep->error = 0;
15259 ep->stat = NFS4_OK;
15260 goto out;
15261 }
15262
15263 if (!osp->os_final_close && !did_dec_count) {
15264 osp->os_open_ref_count--;
15265 did_dec_count = 1;
15266 }
15267
15268 /*
15269 * We can't depend on os_open_ref_count being 0 due to the
15270 * way executables are opened (VN_RELE to match a VOP_OPEN).
15271 */
15272 #ifdef NOTYET
15273 ASSERT(osp->os_open_ref_count == 0);
15274 #endif
15275 if (osp->os_open_ref_count != 0) {
15276 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15277 "nfs4close_one: should panic here on an "
15278 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15279 "since this is probably the exec problem."));
15280
15281 osp->os_open_ref_count = 0;
15282 }
15283
15284 /*
15285 * There is the possibility that nfs4close_one()
15286 * for close_type == CLOSE_DELMAP couldn't find the
15287 * open stream, thus couldn't decrement its os_mapcnt;
15288 * therefore we can't use this ASSERT yet.
15289 */
15290 #ifdef NOTYET
15291 ASSERT(osp->os_mapcnt == 0);
15292 #endif
15293 osp->os_mapcnt = 0;
15294 }
15295
15296 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15297 ASSERT(osp->os_mapcnt >= btopr(len));
15298
15299 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15300 osp->os_mmap_write -= btopr(len);
15301 if (maxprot & PROT_READ)
15302 osp->os_mmap_read -= btopr(len);
15303 if (maxprot & PROT_EXEC)
15304 osp->os_mmap_read -= btopr(len);
15305 /* mirror the PROT_NONE check in nfs4_addmap() */
15306 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15307 !(maxprot & PROT_EXEC))
15308 osp->os_mmap_read -= btopr(len);
15309 osp->os_mapcnt -= btopr(len);
15310 did_dec_count = 1;
15311 }
15312
15313 if (recovonly) {
15314 nfs4_lost_rqst_t lost_rqst;
15315
15316 /* request should not already be in recovery queue */
15317 ASSERT(lrp == NULL);
15318 nfs4_error_init(ep, EINTR);
15319 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15320 osp, cred_otw, vp);
15321 mutex_exit(&osp->os_sync_lock);
15322 have_sync_lock = 0;
15323 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15324 lost_rqst.lr_op == OP_CLOSE ?
15325 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15326 close_failed = 1;
15327 force_close = 0;
15328 goto close_cleanup;
15329 }
15330
15331 /*
15332 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15333 * we stopped operating on the open owner's <old oo_name, old seqid>
15334 * space, which means we stopped operating on the open stream
15335 * too. So don't go OTW (as the seqid is likely bad, and the
15336 * stateid could be stale, potentially triggering a false
15337 * setclientid), and just clean up the client's internal state.
15338 */
15339 if (osp->os_orig_oo_name != oop->oo_name) {
15340 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15341 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15342 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15343 "oo_name %" PRIx64")",
15344 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15345 oop->oo_name));
15346 close_failed = 1;
15347 }
15348
15349 /* If the file failed recovery, just quit. */
15350 mutex_enter(&rp->r_statelock);
15351 if (rp->r_flags & R4RECOVERR) {
15352 close_failed = 1;
15353 }
15354 mutex_exit(&rp->r_statelock);
15355
15356 /*
15357 * If the force close path failed to obtain start_fop
15358 * then skip the OTW close and just remove the state.
15359 */
15360 if (close_failed)
15361 goto close_cleanup;
15362
15363 /*
15364 * Fifth, check to see if there are still mapped pages or other
15365 * opens using this open stream. If there are then we can't
15366 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15367 */
15368 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15369 nfs4_lost_rqst_t new_lost_rqst;
15370 bool_t needrecov = FALSE;
15371 cred_t *odg_cred_otw = NULL;
15372 seqid4 open_dg_seqid = 0;
15373
15374 if (osp->os_delegation) {
15375 /*
15376 * If this open stream was never OPENed OTW then we
15377 * surely can't DOWNGRADE it (especially since the
15378 * osp->open_stateid is really a delegation stateid
15379 * when os_delegation is 1).
15380 */
15381 if (access_bits & FREAD)
15382 osp->os_share_acc_read--;
15383 if (access_bits & FWRITE)
15384 osp->os_share_acc_write--;
15385 osp->os_share_deny_none--;
15386 nfs4_error_zinit(ep);
15387 goto out;
15388 }
15389 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15390 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15391 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15392 if (needrecov && !isrecov) {
15393 bool_t abort;
15394 nfs4_bseqid_entry_t *bsep = NULL;
15395
15396 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15397 bsep = nfs4_create_bseqid_entry(oop, NULL,
15398 vp, 0,
15399 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15400 open_dg_seqid);
15401
15402 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15403 oop, osp, odg_cred_otw, vp, access_bits, 0);
15404 mutex_exit(&osp->os_sync_lock);
15405 have_sync_lock = 0;
15406 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15407 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15408 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15409 bsep, NULL, NULL);
15410 if (odg_cred_otw)
15411 crfree(odg_cred_otw);
15412 if (bsep)
15413 kmem_free(bsep, sizeof (*bsep));
15414
15415 if (abort == TRUE)
15416 goto out;
15417
15418 if (did_start_seqid_sync) {
15419 nfs4_end_open_seqid_sync(oop);
15420 did_start_seqid_sync = 0;
15421 }
15422 open_stream_rele(osp, rp);
15423
15424 if (did_start_op)
15425 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15426 &recov_state, FALSE);
15427 if (did_force_recovlock)
15428 nfs_rw_exit(&mi->mi_recovlock);
15429
15430 goto recov_retry;
15431 } else {
15432 if (odg_cred_otw)
15433 crfree(odg_cred_otw);
15434 }
15435 goto out;
15436 }
15437
15438 /*
15439 * If this open stream was created as the results of an open
15440 * while holding a delegation, then just release it; no need
15441 * to do an OTW close. Otherwise do a "normal" OTW close.
15442 */
15443 if (osp->os_delegation) {
15444 nfs4close_notw(vp, osp, &have_sync_lock);
15445 nfs4_error_zinit(ep);
15446 goto out;
15447 }
15448
15449 /*
15450 * If this stream is not valid, we're done.
15451 */
15452 if (!osp->os_valid) {
15453 nfs4_error_zinit(ep);
15454 goto out;
15455 }
15456
15457 /*
15458 * Last open or mmap ref has vanished, need to do an OTW close.
15459 * First check to see if a close is still necessary.
15460 */
15461 if (osp->os_failed_reopen) {
15462 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15463 "don't close OTW osp %p since reopen failed.",
15464 (void *)osp));
15465 /*
15466 * Reopen of the open stream failed, hence the
15467 * stateid of the open stream is invalid/stale, and
15468 * sending this OTW would incorrectly cause another
15469 * round of recovery. In this case, we need to set
15470 * the 'os_valid' bit to 0 so another thread doesn't
15471 * come in and re-open this open stream before
15472 * this "closing" thread cleans up state (decrementing
15473 * the nfs4_server_t's state_ref_count and decrementing
15474 * the os_ref_count).
15475 */
15476 osp->os_valid = 0;
15477 /*
15478 * This removes the reference obtained at OPEN; ie,
15479 * when the open stream structure was created.
15480 *
15481 * We don't have to worry about calling 'open_stream_rele'
15482 * since we our currently holding a reference to this
15483 * open stream which means the count can not go to 0 with
15484 * this decrement.
15485 */
15486 ASSERT(osp->os_ref_count >= 2);
15487 osp->os_ref_count--;
15488 nfs4_error_zinit(ep);
15489 close_failed = 0;
15490 goto close_cleanup;
15491 }
15492
15493 ASSERT(osp->os_ref_count > 1);
15494
15495 /*
15496 * Sixth, try the CLOSE OTW.
15497 */
15498 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15499 close_type, ep, &have_sync_lock);
15500
15501 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15502 /*
15503 * Let the recovery thread be responsible for
15504 * removing the state for CLOSE.
15505 */
15506 close_failed = 1;
15507 force_close = 0;
15508 retry = 0;
15509 }
15510
15511 /* See if we need to retry with a different cred */
15512 if ((ep->error == EACCES ||
15513 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15514 cred_otw != cr) {
15515 crfree(cred_otw);
15516 cred_otw = cr;
15517 crhold(cred_otw);
15518 retry = 1;
15519 }
15520
15521 if (ep->error || ep->stat)
15522 close_failed = 1;
15523
15524 if (retry && !isrecov && num_retries-- > 0) {
15525 if (have_sync_lock) {
15526 mutex_exit(&osp->os_sync_lock);
15527 have_sync_lock = 0;
15528 }
15529 if (did_start_seqid_sync) {
15530 nfs4_end_open_seqid_sync(oop);
15531 did_start_seqid_sync = 0;
15532 }
15533 open_stream_rele(osp, rp);
15534
15535 if (did_start_op)
15536 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15537 &recov_state, FALSE);
15538 if (did_force_recovlock)
15539 nfs_rw_exit(&mi->mi_recovlock);
15540 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15541 "nfs4close_one: need to retry the close "
15542 "operation"));
15543 goto recov_retry;
15544 }
15545 close_cleanup:
15546 /*
15547 * Seventh and lastly, process our results.
15548 */
15549 if (close_failed && force_close) {
15550 /*
15551 * It's ok to drop and regrab the 'os_sync_lock' since
15552 * nfs4close_notw() will recheck to make sure the
15553 * "close"/removal of state should happen.
15554 */
15555 if (!have_sync_lock) {
15556 mutex_enter(&osp->os_sync_lock);
15557 have_sync_lock = 1;
15558 }
15559 /*
15560 * This is last call, remove the ref on the open
15561 * stream created by open and clean everything up.
15562 */
15563 osp->os_pending_close = 0;
15564 nfs4close_notw(vp, osp, &have_sync_lock);
15565 nfs4_error_zinit(ep);
15566 }
15567
15568 if (!close_failed) {
15569 if (have_sync_lock) {
15570 osp->os_pending_close = 0;
15571 mutex_exit(&osp->os_sync_lock);
15572 have_sync_lock = 0;
15573 } else {
15574 mutex_enter(&osp->os_sync_lock);
15575 osp->os_pending_close = 0;
15576 mutex_exit(&osp->os_sync_lock);
15577 }
15578 if (did_start_op && recov_state.rs_sp != NULL) {
15579 mutex_enter(&recov_state.rs_sp->s_lock);
15580 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15581 mutex_exit(&recov_state.rs_sp->s_lock);
15582 } else {
15583 nfs4_dec_state_ref_count(mi);
15584 }
15585 nfs4_error_zinit(ep);
15586 }
15587
15588 out:
15589 if (have_sync_lock)
15590 mutex_exit(&osp->os_sync_lock);
15591 if (did_start_op)
15592 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15593 recovonly ? TRUE : FALSE);
15594 if (did_force_recovlock)
15595 nfs_rw_exit(&mi->mi_recovlock);
15596 if (cred_otw)
15597 crfree(cred_otw);
15598 if (osp)
15599 open_stream_rele(osp, rp);
15600 if (oop) {
15601 if (did_start_seqid_sync)
15602 nfs4_end_open_seqid_sync(oop);
15603 open_owner_rele(oop);
15604 }
15605 }
15606
15607 /*
15608 * Convert information returned by the server in the LOCK4denied
15609 * structure to the form required by fcntl.
15610 */
15611 static void
15612 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15613 {
15614 nfs4_lo_name_t *lo;
15615
15616 #ifdef DEBUG
15617 if (denied_to_flk_debug) {
15618 lockt_denied_debug = lockt_denied;
15619 debug_enter("lockt_denied");
15620 }
15621 #endif
15622
15623 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15624 flk->l_whence = 0; /* aka SEEK_SET */
15625 flk->l_start = lockt_denied->offset;
15626 flk->l_len = lockt_denied->length;
15627
15628 /*
15629 * If the blocking clientid matches our client id, then we can
15630 * interpret the lockowner (since we built it). If not, then
15631 * fabricate a sysid and pid. Note that the l_sysid field
15632 * in *flk already has the local sysid.
15633 */
15634
15635 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15636
15637 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15638 lo = (nfs4_lo_name_t *)
15639 lockt_denied->owner.owner_val;
15640
15641 flk->l_pid = lo->ln_pid;
15642 } else {
15643 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15644 "denied_to_flk: bad lock owner length\n"));
15645
15646 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15647 }
15648 } else {
15649 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15650 "denied_to_flk: foreign clientid\n"));
15651
15652 /*
15653 * Construct a new sysid which should be different from
15654 * sysids of other systems.
15655 */
15656
15657 flk->l_sysid++;
15658 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15659 }
15660 }
15661
15662 static pid_t
15663 lo_to_pid(lock_owner4 *lop)
15664 {
15665 pid_t pid = 0;
15666 uchar_t *cp;
15667 int i;
15668
15669 cp = (uchar_t *)&lop->clientid;
15670
15671 for (i = 0; i < sizeof (lop->clientid); i++)
15672 pid += (pid_t)*cp++;
15673
15674 cp = (uchar_t *)lop->owner_val;
15675
15676 for (i = 0; i < lop->owner_len; i++)
15677 pid += (pid_t)*cp++;
15678
15679 return (pid);
15680 }
15681
15682 /*
15683 * Given a lock pointer, returns the length of that lock.
15684 * "end" is the last locked offset the "l_len" covers from
15685 * the start of the lock.
15686 */
15687 static off64_t
15688 lock_to_end(flock64_t *lock)
15689 {
15690 off64_t lock_end;
15691
15692 if (lock->l_len == 0)
15693 lock_end = (off64_t)MAXEND;
15694 else
15695 lock_end = lock->l_start + lock->l_len - 1;
15696
15697 return (lock_end);
15698 }
15699
15700 /*
15701 * Given the end of a lock, it will return you the length "l_len" for that lock.
15702 */
15703 static off64_t
15704 end_to_len(off64_t start, off64_t end)
15705 {
15706 off64_t lock_len;
15707
15708 ASSERT(end >= start);
15709 if (end == MAXEND)
15710 lock_len = 0;
15711 else
15712 lock_len = end - start + 1;
15713
15714 return (lock_len);
15715 }
15716
15717 /*
15718 * On given end for a lock it determines if it is the last locked offset
15719 * or not, if so keeps it as is, else adds one to return the length for
15720 * valid start.
15721 */
15722 static off64_t
15723 start_check(off64_t x)
15724 {
15725 if (x == MAXEND)
15726 return (x);
15727 else
15728 return (x + 1);
15729 }
15730
15731 /*
15732 * See if these two locks overlap, and if so return 1;
15733 * otherwise, return 0.
15734 */
15735 static int
15736 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15737 {
15738 off64_t llfp_end, curfp_end;
15739
15740 llfp_end = lock_to_end(llfp);
15741 curfp_end = lock_to_end(curfp);
15742
15743 if (((llfp_end >= curfp->l_start) &&
15744 (llfp->l_start <= curfp->l_start)) ||
15745 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15746 return (1);
15747 return (0);
15748 }
15749
15750 /*
15751 * Determine what the intersecting lock region is, and add that to the
15752 * 'nl_llpp' locklist in increasing order (by l_start).
15753 */
15754 static void
15755 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15756 locklist_t **nl_llpp, vnode_t *vp)
15757 {
15758 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15759 off64_t lost_flp_end, local_flp_end, len, start;
15760
15761 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15762
15763 if (!locks_intersect(lost_flp, local_flp))
15764 return;
15765
15766 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15767 "locks intersect"));
15768
15769 lost_flp_end = lock_to_end(lost_flp);
15770 local_flp_end = lock_to_end(local_flp);
15771
15772 /* Find the starting point of the intersecting region */
15773 if (local_flp->l_start > lost_flp->l_start)
15774 start = local_flp->l_start;
15775 else
15776 start = lost_flp->l_start;
15777
15778 /* Find the lenght of the intersecting region */
15779 if (lost_flp_end < local_flp_end)
15780 len = end_to_len(start, lost_flp_end);
15781 else
15782 len = end_to_len(start, local_flp_end);
15783
15784 /*
15785 * Prepare the flock structure for the intersection found and insert
15786 * it into the new list in increasing l_start order. This list contains
15787 * intersections of locks registered by the client with the local host
15788 * and the lost lock.
15789 * The lock type of this lock is the same as that of the local_flp.
15790 */
15791 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15792 intersect_llp->ll_flock.l_start = start;
15793 intersect_llp->ll_flock.l_len = len;
15794 intersect_llp->ll_flock.l_type = local_flp->l_type;
15795 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15796 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15797 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15798 intersect_llp->ll_vp = vp;
15799
15800 tmp_fllp = *nl_llpp;
15801 cur_fllp = NULL;
15802 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15803 intersect_llp->ll_flock.l_start) {
15804 cur_fllp = tmp_fllp;
15805 tmp_fllp = tmp_fllp->ll_next;
15806 }
15807 if (cur_fllp == NULL) {
15808 /* first on the list */
15809 intersect_llp->ll_next = *nl_llpp;
15810 *nl_llpp = intersect_llp;
15811 } else {
15812 intersect_llp->ll_next = cur_fllp->ll_next;
15813 cur_fllp->ll_next = intersect_llp;
15814 }
15815
15816 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15817 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15818 intersect_llp->ll_flock.l_start,
15819 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15820 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15821 }
15822
15823 /*
15824 * Our local locking current state is potentially different than
15825 * what the NFSv4 server thinks we have due to a lost lock that was
15826 * resent and then received. We need to reset our "NFSv4" locking
15827 * state to match the current local locking state for this pid since
15828 * that is what the user/application sees as what the world is.
15829 *
15830 * We cannot afford to drop the open/lock seqid sync since then we can
15831 * get confused about what the current local locking state "is" versus
15832 * "was".
15833 *
15834 * If we are unable to fix up the locks, we send SIGLOST to the affected
15835 * process. This is not done if the filesystem has been forcibly
15836 * unmounted, in case the process has already exited and a new process
15837 * exists with the same pid.
15838 */
15839 static void
15840 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15841 nfs4_lock_owner_t *lop)
15842 {
15843 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15844 mntinfo4_t *mi = VTOMI4(vp);
15845 const int cmd = F_SETLK;
15846 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15847 flock64_t ul_fl;
15848
15849 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15850 "nfs4_reinstitute_local_lock_state"));
15851
15852 /*
15853 * Find active locks for this vp from the local locking code.
15854 * Scan through this list and find out the locks that intersect with
15855 * the lost lock. Once we find the lock that intersects, add the
15856 * intersection area as a new lock to a new list "ri_llp". The lock
15857 * type of the intersection region lock added to ri_llp is the same
15858 * as that found in the active lock list, "list". The intersecting
15859 * region locks are added to ri_llp in increasing l_start order.
15860 */
15861 ASSERT(nfs_zone() == mi->mi_zone);
15862
15863 locks = flk_active_locks_for_vp(vp);
15864 ri_llp = NULL;
15865
15866 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15867 ASSERT(llp->ll_vp == vp);
15868 /*
15869 * Pick locks that belong to this pid/lockowner
15870 */
15871 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15872 continue;
15873
15874 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15875 }
15876
15877 /*
15878 * Now we have the list of intersections with the lost lock. These are
15879 * the locks that were/are active before the server replied to the
15880 * last/lost lock. Issue these locks to the server here. Playing these
15881 * locks to the server will re-establish aur current local locking state
15882 * with the v4 server.
15883 * If we get an error, send SIGLOST to the application for that lock.
15884 */
15885
15886 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15887 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15888 "nfs4_reinstitute_local_lock_state: need to issue "
15889 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15890 llp->ll_flock.l_start,
15891 llp->ll_flock.l_start + llp->ll_flock.l_len,
15892 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15893 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15894 /*
15895 * No need to relock what we already have
15896 */
15897 if (llp->ll_flock.l_type == lost_flp->l_type)
15898 continue;
15899
15900 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15901 }
15902
15903 /*
15904 * Now keeping the start of the lost lock as our reference parse the
15905 * newly created ri_llp locklist to find the ranges that we have locked
15906 * with the v4 server but not in the current local locking. We need
15907 * to unlock these ranges.
15908 * These ranges can also be reffered to as those ranges, where the lost
15909 * lock does not overlap with the locks in the ri_llp but are locked
15910 * since the server replied to the lost lock.
15911 */
15912 cur_start = lost_flp->l_start;
15913 lost_flp_end = lock_to_end(lost_flp);
15914
15915 ul_fl.l_type = F_UNLCK;
15916 ul_fl.l_whence = 0; /* aka SEEK_SET */
15917 ul_fl.l_sysid = lost_flp->l_sysid;
15918 ul_fl.l_pid = lost_flp->l_pid;
15919
15920 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15921 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15922
15923 if (llp->ll_flock.l_start <= cur_start) {
15924 cur_start = start_check(llp_ll_flock_end);
15925 continue;
15926 }
15927 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15928 "nfs4_reinstitute_local_lock_state: "
15929 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15930 cur_start, llp->ll_flock.l_start));
15931
15932 ul_fl.l_start = cur_start;
15933 ul_fl.l_len = end_to_len(cur_start,
15934 (llp->ll_flock.l_start - 1));
15935
15936 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15937 cur_start = start_check(llp_ll_flock_end);
15938 }
15939
15940 /*
15941 * In the case where the lost lock ends after all intersecting locks,
15942 * unlock the last part of the lost lock range.
15943 */
15944 if (cur_start != start_check(lost_flp_end)) {
15945 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15946 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15947 "lost lock region [%"PRIx64" - %"PRIx64"]",
15948 cur_start, lost_flp->l_start + lost_flp->l_len));
15949
15950 ul_fl.l_start = cur_start;
15951 /*
15952 * Is it an to-EOF lock? if so unlock till the end
15953 */
15954 if (lost_flp->l_len == 0)
15955 ul_fl.l_len = 0;
15956 else
15957 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15958
15959 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15960 }
15961
15962 if (locks != NULL)
15963 flk_free_locklist(locks);
15964
15965 /* Free up our newly created locklist */
15966 for (llp = ri_llp; llp != NULL; ) {
15967 tmp_llp = llp->ll_next;
15968 kmem_free(llp, sizeof (locklist_t));
15969 llp = tmp_llp;
15970 }
15971
15972 /*
15973 * Now return back to the original calling nfs4frlock()
15974 * and let us naturally drop our seqid syncs.
15975 */
15976 }
15977
15978 /*
15979 * Create a lost state record for the given lock reinstantiation request
15980 * and push it onto the lost state queue.
15981 */
15982 static void
15983 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15984 nfs4_lock_owner_t *lop)
15985 {
15986 nfs4_lost_rqst_t req;
15987 nfs_lock_type4 locktype;
15988 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15989
15990 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15991
15992 locktype = flk_to_locktype(cmd, flk->l_type);
15993 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
15994 NULL, NULL, lop, flk, &req, cr, vp);
15995 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
15996 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
15997 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
15998 NULL, NULL, NULL);
15999 }