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 (c) 2016 STRATO AG. All rights reserved.
24 */
25
26 /*
27 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
28 */
29
30 /*
31 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
32 * Use is subject to license terms.
33 */
34
35 /*
36 * Copyright 1983,1984,1985,1986,1987,1988,1989 AT&T.
37 * All Rights Reserved
38 */
39
40 /*
41 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
42 * Copyright 2022 Oxide Computer Company
43 */
44
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/cred.h>
49 #include <sys/time.h>
50 #include <sys/vnode.h>
51 #include <sys/vfs.h>
52 #include <sys/vfs_opreg.h>
53 #include <sys/file.h>
54 #include <sys/filio.h>
55 #include <sys/uio.h>
56 #include <sys/buf.h>
57 #include <sys/mman.h>
58 #include <sys/pathname.h>
59 #include <sys/dirent.h>
60 #include <sys/debug.h>
61 #include <sys/vmsystm.h>
62 #include <sys/fcntl.h>
63 #include <sys/flock.h>
64 #include <sys/swap.h>
65 #include <sys/errno.h>
66 #include <sys/strsubr.h>
67 #include <sys/sysmacros.h>
68 #include <sys/kmem.h>
69 #include <sys/cmn_err.h>
70 #include <sys/pathconf.h>
71 #include <sys/utsname.h>
72 #include <sys/dnlc.h>
73 #include <sys/acl.h>
74 #include <sys/systeminfo.h>
75 #include <sys/policy.h>
76 #include <sys/sdt.h>
77 #include <sys/list.h>
78 #include <sys/stat.h>
79 #include <sys/zone.h>
80
81 #include <rpc/types.h>
82 #include <rpc/auth.h>
83 #include <rpc/clnt.h>
84
85 #include <nfs/nfs.h>
86 #include <nfs/nfs_clnt.h>
87 #include <nfs/nfs_acl.h>
88 #include <nfs/lm.h>
89 #include <nfs/nfs4.h>
90 #include <nfs/nfs4_kprot.h>
91 #include <nfs/rnode4.h>
92 #include <nfs/nfs4_clnt.h>
93
94 #include <vm/hat.h>
95 #include <vm/as.h>
96 #include <vm/page.h>
97 #include <vm/pvn.h>
98 #include <vm/seg.h>
99 #include <vm/seg_map.h>
100 #include <vm/seg_kpm.h>
101 #include <vm/seg_vn.h>
102
103 #include <fs/fs_subr.h>
104
105 #include <sys/ddi.h>
106 #include <sys/int_fmtio.h>
107 #include <sys/fs/autofs.h>
108
109 typedef struct {
110 nfs4_ga_res_t *di_garp;
111 cred_t *di_cred;
112 hrtime_t di_time_call;
113 } dirattr_info_t;
114
115 typedef enum nfs4_acl_op {
116 NFS4_ACL_GET,
117 NFS4_ACL_SET
118 } nfs4_acl_op_t;
119
120 static struct lm_sysid *nfs4_find_sysid(mntinfo4_t *);
121
122 static void nfs4_update_dircaches(change_info4 *, vnode_t *, vnode_t *,
123 char *, dirattr_info_t *);
124
125 static void nfs4close_otw(rnode4_t *, cred_t *, nfs4_open_owner_t *,
126 nfs4_open_stream_t *, int *, int *, nfs4_close_type_t,
127 nfs4_error_t *, int *);
128 static int nfs4_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
129 cred_t *);
130 static int nfs4write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
131 stable_how4 *);
132 static int nfs4read(vnode_t *, caddr_t, offset_t, int, size_t *,
133 cred_t *, bool_t, struct uio *);
134 static int nfs4setattr(vnode_t *, struct vattr *, int, cred_t *,
135 vsecattr_t *);
136 static int nfs4openattr(vnode_t *, vnode_t **, int, cred_t *);
137 static int nfs4lookup(vnode_t *, char *, vnode_t **, cred_t *, int);
138 static int nfs4lookup_xattr(vnode_t *, char *, vnode_t **, int, cred_t *);
139 static int nfs4lookupvalidate_otw(vnode_t *, char *, vnode_t **, cred_t *);
140 static int nfs4lookupnew_otw(vnode_t *, char *, vnode_t **, cred_t *);
141 static int nfs4mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
142 int, vnode_t **, cred_t *);
143 static int nfs4open_otw(vnode_t *, char *, struct vattr *, vnode_t **,
144 cred_t *, int, int, enum createmode4, int);
145 static int nfs4rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
146 caller_context_t *);
147 static int nfs4rename_persistent_fh(vnode_t *, char *, vnode_t *,
148 vnode_t *, char *, cred_t *, nfsstat4 *);
149 static int nfs4rename_volatile_fh(vnode_t *, char *, vnode_t *,
150 vnode_t *, char *, cred_t *, nfsstat4 *);
151 static int do_nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
152 static void nfs4readdir(vnode_t *, rddir4_cache *, cred_t *);
153 static int nfs4_bio(struct buf *, stable_how4 *, cred_t *, bool_t);
154 static int nfs4_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
155 page_t *[], size_t, struct seg *, caddr_t,
156 enum seg_rw, cred_t *);
157 static void nfs4_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
158 cred_t *);
159 static int nfs4_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
160 int, cred_t *);
161 static int nfs4_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
162 int, cred_t *);
163 static int nfs4_commit(vnode_t *, offset4, count4, cred_t *);
164 static void nfs4_set_mod(vnode_t *);
165 static void nfs4_get_commit(vnode_t *);
166 static void nfs4_get_commit_range(vnode_t *, u_offset_t, size_t);
167 static int nfs4_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
168 static int nfs4_commit_vp(vnode_t *, u_offset_t, size_t, cred_t *, int);
169 static int nfs4_sync_commit(vnode_t *, page_t *, offset3, count3,
170 cred_t *);
171 static void do_nfs4_async_commit(vnode_t *, page_t *, offset3, count3,
172 cred_t *);
173 static int nfs4_update_attrcache(nfsstat4, nfs4_ga_res_t *,
174 hrtime_t, vnode_t *, cred_t *);
175 static int nfs4_open_non_reg_file(vnode_t **, int, cred_t *);
176 static int nfs4_safelock(vnode_t *, const struct flock64 *, cred_t *);
177 static void nfs4_register_lock_locally(vnode_t *, struct flock64 *, int,
178 u_offset_t);
179 static int nfs4_lockrelease(vnode_t *, int, offset_t, cred_t *);
180 static int nfs4_block_and_wait(clock_t *, rnode4_t *);
181 static cred_t *state_to_cred(nfs4_open_stream_t *);
182 static void denied_to_flk(LOCK4denied *, flock64_t *, LOCKT4args *);
183 static pid_t lo_to_pid(lock_owner4 *);
184 static void nfs4_reinstitute_local_lock_state(vnode_t *, flock64_t *,
185 cred_t *, nfs4_lock_owner_t *);
186 static void push_reinstate(vnode_t *, int, flock64_t *, cred_t *,
187 nfs4_lock_owner_t *);
188 static int open_and_get_osp(vnode_t *, cred_t *, nfs4_open_stream_t **);
189 static void nfs4_delmap_callback(struct as *, void *, uint_t);
190 static void nfs4_free_delmapcall(nfs4_delmapcall_t *);
191 static nfs4_delmapcall_t *nfs4_init_delmapcall();
192 static int nfs4_find_and_delete_delmapcall(rnode4_t *, int *);
193 static int nfs4_is_acl_mask_valid(uint_t, nfs4_acl_op_t);
194 static int nfs4_create_getsecattr_return(vsecattr_t *, vsecattr_t *,
195 uid_t, gid_t, int);
196
197 /*
198 * Routines that implement the setting of v4 args for the misc. ops
199 */
200 static void nfs4args_lock_free(nfs_argop4 *);
201 static void nfs4args_lockt_free(nfs_argop4 *);
202 static void nfs4args_setattr(nfs_argop4 *, vattr_t *, vsecattr_t *,
203 int, rnode4_t *, cred_t *, bitmap4, int *,
204 nfs4_stateid_types_t *);
205 static void nfs4args_setattr_free(nfs_argop4 *);
206 static int nfs4args_verify(nfs_argop4 *, vattr_t *, enum nfs_opnum4,
207 bitmap4);
208 static void nfs4args_verify_free(nfs_argop4 *);
209 static void nfs4args_write(nfs_argop4 *, stable_how4, rnode4_t *, cred_t *,
210 WRITE4args **, nfs4_stateid_types_t *);
211
212 /*
213 * These are the vnode ops functions that implement the vnode interface to
214 * the networked file system. See more comments below at nfs4_vnodeops.
215 */
216 static int nfs4_open(vnode_t **, int, cred_t *, caller_context_t *);
217 static int nfs4_close(vnode_t *, int, int, offset_t, cred_t *,
218 caller_context_t *);
219 static int nfs4_read(vnode_t *, struct uio *, int, cred_t *,
220 caller_context_t *);
221 static int nfs4_write(vnode_t *, struct uio *, int, cred_t *,
222 caller_context_t *);
223 static int nfs4_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
224 caller_context_t *);
225 static int nfs4_setattr(vnode_t *, struct vattr *, int, cred_t *,
226 caller_context_t *);
227 static int nfs4_access(vnode_t *, int, int, cred_t *, caller_context_t *);
228 static int nfs4_readlink(vnode_t *, struct uio *, cred_t *,
229 caller_context_t *);
230 static int nfs4_fsync(vnode_t *, int, cred_t *, caller_context_t *);
231 static int nfs4_create(vnode_t *, char *, struct vattr *, enum vcexcl,
232 int, vnode_t **, cred_t *, int, caller_context_t *,
233 vsecattr_t *);
234 static int nfs4_remove(vnode_t *, char *, cred_t *, caller_context_t *,
235 int);
236 static int nfs4_link(vnode_t *, vnode_t *, char *, cred_t *,
237 caller_context_t *, int);
238 static int nfs4_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
239 caller_context_t *, int);
240 static int nfs4_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
241 cred_t *, caller_context_t *, int, vsecattr_t *);
242 static int nfs4_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
243 caller_context_t *, int);
244 static int nfs4_symlink(vnode_t *, char *, struct vattr *, char *,
245 cred_t *, caller_context_t *, int);
246 static int nfs4_readdir(vnode_t *, struct uio *, cred_t *, int *,
247 caller_context_t *, int);
248 static int nfs4_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
249 static int nfs4_getpage(vnode_t *, offset_t, size_t, uint_t *,
250 page_t *[], size_t, struct seg *, caddr_t,
251 enum seg_rw, cred_t *, caller_context_t *);
252 static int nfs4_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
253 caller_context_t *);
254 static int nfs4_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
255 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
256 static int nfs4_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
257 uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
258 static int nfs4_cmp(vnode_t *, vnode_t *, caller_context_t *);
259 static int nfs4_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
260 struct flk_callback *, cred_t *, caller_context_t *);
261 static int nfs4_space(vnode_t *, int, struct flock64 *, int, offset_t,
262 cred_t *, caller_context_t *);
263 static int nfs4_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
264 uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
265 static int nfs4_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
266 cred_t *, caller_context_t *);
267 static void nfs4_dispose(vnode_t *, page_t *, int, int, cred_t *,
268 caller_context_t *);
269 static int nfs4_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
270 caller_context_t *);
271 /*
272 * These vnode ops are required to be called from outside this source file,
273 * e.g. by ephemeral mount stub vnode ops, and so may not be declared
274 * as static.
275 */
276 int nfs4_getattr(vnode_t *, struct vattr *, int, cred_t *,
277 caller_context_t *);
278 void nfs4_inactive(vnode_t *, cred_t *, caller_context_t *);
279 int nfs4_lookup(vnode_t *, char *, vnode_t **,
280 struct pathname *, int, vnode_t *, cred_t *,
281 caller_context_t *, int *, pathname_t *);
282 int nfs4_fid(vnode_t *, fid_t *, caller_context_t *);
283 int nfs4_rwlock(vnode_t *, int, caller_context_t *);
284 void nfs4_rwunlock(vnode_t *, int, caller_context_t *);
285 int nfs4_realvp(vnode_t *, vnode_t **, caller_context_t *);
286 int nfs4_pathconf(vnode_t *, int, ulong_t *, cred_t *,
287 caller_context_t *);
288 int nfs4_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
289 caller_context_t *);
290 int nfs4_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
291 caller_context_t *);
292
293 /*
294 * Used for nfs4_commit_vp() to indicate if we should
295 * wait on pending writes.
296 */
297 #define NFS4_WRITE_NOWAIT 0
298 #define NFS4_WRITE_WAIT 1
299
300 #define NFS4_BASE_WAIT_TIME 1 /* 1 second */
301
302 /*
303 * Error flags used to pass information about certain special errors
304 * which need to be handled specially.
305 */
306 #define NFS_EOF -98
307 #define NFS_VERF_MISMATCH -97
308
309 /*
310 * Flags used to differentiate between which operation drove the
311 * potential CLOSE OTW. (see nfs4_close_otw_if_necessary)
312 */
313 #define NFS4_CLOSE_OP 0x1
314 #define NFS4_DELMAP_OP 0x2
315 #define NFS4_INACTIVE_OP 0x3
316
317 #define ISVDEV(t) ((t == VBLK) || (t == VCHR) || (t == VFIFO))
318
319 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
320 #define ALIGN64(x, ptr, sz) \
321 x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1); \
322 if (x) { \
323 x = sizeof (uint64_t) - (x); \
324 sz -= (x); \
325 ptr += (x); \
326 }
327
328 #ifdef DEBUG
329 int nfs4_client_attr_debug = 0;
330 int nfs4_client_state_debug = 0;
331 int nfs4_client_shadow_debug = 0;
332 int nfs4_client_lock_debug = 0;
333 int nfs4_seqid_sync = 0;
334 int nfs4_client_map_debug = 0;
335 static int nfs4_pageio_debug = 0;
336 int nfs4_client_inactive_debug = 0;
337 int nfs4_client_recov_debug = 0;
338 int nfs4_client_failover_debug = 0;
339 int nfs4_client_call_debug = 0;
340 int nfs4_client_lookup_debug = 0;
341 int nfs4_client_zone_debug = 0;
342 int nfs4_lost_rqst_debug = 0;
343 int nfs4_rdattrerr_debug = 0;
344 int nfs4_open_stream_debug = 0;
345
346 int nfs4read_error_inject;
347
348 static int nfs4_create_misses = 0;
349
350 static int nfs4_readdir_cache_shorts = 0;
351 static int nfs4_readdir_readahead = 0;
352
353 static int nfs4_bio_do_stop = 0;
354
355 static int nfs4_lostpage = 0; /* number of times we lost original page */
356
357 int nfs4_mmap_debug = 0;
358
359 static int nfs4_pathconf_cache_hits = 0;
360 static int nfs4_pathconf_cache_misses = 0;
361
362 int nfs4close_all_cnt;
363 int nfs4close_one_debug = 0;
364 int nfs4close_notw_debug = 0;
365
366 int denied_to_flk_debug = 0;
367 void *lockt_denied_debug;
368
369 #endif
370
371 /*
372 * How long to wait before trying again if OPEN_CONFIRM gets ETIMEDOUT
373 * or NFS4ERR_RESOURCE.
374 */
375 static int confirm_retry_sec = 30;
376
377 static int nfs4_lookup_neg_cache = 1;
378
379 /*
380 * number of pages to read ahead
381 * optimized for 100 base-T.
382 */
383 static int nfs4_nra = 4;
384
385 static int nfs4_do_symlink_cache = 1;
386
387 static int nfs4_pathconf_disable_cache = 0;
388
389 /*
390 * These are the vnode ops routines which implement the vnode interface to
391 * the networked file system. These routines just take their parameters,
392 * make them look networkish by putting the right info into interface structs,
393 * and then calling the appropriate remote routine(s) to do the work.
394 *
395 * Note on directory name lookup cacheing: If we detect a stale fhandle,
396 * we purge the directory cache relative to that vnode. This way, the
397 * user won't get burned by the cache repeatedly. See <nfs/rnode4.h> for
398 * more details on rnode locking.
399 */
400
401 struct vnodeops *nfs4_vnodeops;
402
403 const fs_operation_def_t nfs4_vnodeops_template[] = {
404 VOPNAME_OPEN, { .vop_open = nfs4_open },
405 VOPNAME_CLOSE, { .vop_close = nfs4_close },
406 VOPNAME_READ, { .vop_read = nfs4_read },
407 VOPNAME_WRITE, { .vop_write = nfs4_write },
408 VOPNAME_IOCTL, { .vop_ioctl = nfs4_ioctl },
409 VOPNAME_GETATTR, { .vop_getattr = nfs4_getattr },
410 VOPNAME_SETATTR, { .vop_setattr = nfs4_setattr },
411 VOPNAME_ACCESS, { .vop_access = nfs4_access },
412 VOPNAME_LOOKUP, { .vop_lookup = nfs4_lookup },
413 VOPNAME_CREATE, { .vop_create = nfs4_create },
414 VOPNAME_REMOVE, { .vop_remove = nfs4_remove },
415 VOPNAME_LINK, { .vop_link = nfs4_link },
416 VOPNAME_RENAME, { .vop_rename = nfs4_rename },
417 VOPNAME_MKDIR, { .vop_mkdir = nfs4_mkdir },
418 VOPNAME_RMDIR, { .vop_rmdir = nfs4_rmdir },
419 VOPNAME_READDIR, { .vop_readdir = nfs4_readdir },
420 VOPNAME_SYMLINK, { .vop_symlink = nfs4_symlink },
421 VOPNAME_READLINK, { .vop_readlink = nfs4_readlink },
422 VOPNAME_FSYNC, { .vop_fsync = nfs4_fsync },
423 VOPNAME_INACTIVE, { .vop_inactive = nfs4_inactive },
424 VOPNAME_FID, { .vop_fid = nfs4_fid },
425 VOPNAME_RWLOCK, { .vop_rwlock = nfs4_rwlock },
426 VOPNAME_RWUNLOCK, { .vop_rwunlock = nfs4_rwunlock },
427 VOPNAME_SEEK, { .vop_seek = nfs4_seek },
428 VOPNAME_FRLOCK, { .vop_frlock = nfs4_frlock },
429 VOPNAME_SPACE, { .vop_space = nfs4_space },
430 VOPNAME_REALVP, { .vop_realvp = nfs4_realvp },
431 VOPNAME_GETPAGE, { .vop_getpage = nfs4_getpage },
432 VOPNAME_PUTPAGE, { .vop_putpage = nfs4_putpage },
433 VOPNAME_MAP, { .vop_map = nfs4_map },
434 VOPNAME_ADDMAP, { .vop_addmap = nfs4_addmap },
435 VOPNAME_DELMAP, { .vop_delmap = nfs4_delmap },
436 /* no separate nfs4_dump */
437 VOPNAME_DUMP, { .vop_dump = nfs_dump },
438 VOPNAME_PATHCONF, { .vop_pathconf = nfs4_pathconf },
439 VOPNAME_PAGEIO, { .vop_pageio = nfs4_pageio },
440 VOPNAME_DISPOSE, { .vop_dispose = nfs4_dispose },
441 VOPNAME_SETSECATTR, { .vop_setsecattr = nfs4_setsecattr },
442 VOPNAME_GETSECATTR, { .vop_getsecattr = nfs4_getsecattr },
443 VOPNAME_SHRLOCK, { .vop_shrlock = nfs4_shrlock },
444 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
445 NULL, NULL
446 };
447
448 /*
449 * The following are subroutines and definitions to set args or get res
450 * for the different nfsv4 ops
451 */
452
453 void
nfs4args_lookup_free(nfs_argop4 * argop,int arglen)454 nfs4args_lookup_free(nfs_argop4 *argop, int arglen)
455 {
456 int i;
457
458 for (i = 0; i < arglen; i++) {
459 if (argop[i].argop == OP_LOOKUP) {
460 kmem_free(
461 argop[i].nfs_argop4_u.oplookup.
462 objname.utf8string_val,
463 argop[i].nfs_argop4_u.oplookup.
464 objname.utf8string_len);
465 }
466 }
467 }
468
469 static void
nfs4args_lock_free(nfs_argop4 * argop)470 nfs4args_lock_free(nfs_argop4 *argop)
471 {
472 locker4 *locker = &argop->nfs_argop4_u.oplock.locker;
473
474 if (locker->new_lock_owner == TRUE) {
475 open_to_lock_owner4 *open_owner;
476
477 open_owner = &locker->locker4_u.open_owner;
478 if (open_owner->lock_owner.owner_val != NULL) {
479 kmem_free(open_owner->lock_owner.owner_val,
480 open_owner->lock_owner.owner_len);
481 }
482 }
483 }
484
485 static void
nfs4args_lockt_free(nfs_argop4 * argop)486 nfs4args_lockt_free(nfs_argop4 *argop)
487 {
488 lock_owner4 *lowner = &argop->nfs_argop4_u.oplockt.owner;
489
490 if (lowner->owner_val != NULL) {
491 kmem_free(lowner->owner_val, lowner->owner_len);
492 }
493 }
494
495 static void
nfs4args_setattr(nfs_argop4 * argop,vattr_t * vap,vsecattr_t * vsap,int flags,rnode4_t * rp,cred_t * cr,bitmap4 supp,int * error,nfs4_stateid_types_t * sid_types)496 nfs4args_setattr(nfs_argop4 *argop, vattr_t *vap, vsecattr_t *vsap, int flags,
497 rnode4_t *rp, cred_t *cr, bitmap4 supp, int *error,
498 nfs4_stateid_types_t *sid_types)
499 {
500 fattr4 *attr = &argop->nfs_argop4_u.opsetattr.obj_attributes;
501 mntinfo4_t *mi;
502
503 argop->argop = OP_SETATTR;
504 /*
505 * The stateid is set to 0 if client is not modifying the size
506 * and otherwise to whatever nfs4_get_stateid() returns.
507 *
508 * XXX Note: nfs4_get_stateid() returns 0 if no lockowner and/or no
509 * state struct could be found for the process/file pair. We may
510 * want to change this in the future (by OPENing the file). See
511 * bug # 4474852.
512 */
513 if (vap->va_mask & AT_SIZE) {
514
515 ASSERT(rp != NULL);
516 mi = VTOMI4(RTOV4(rp));
517
518 argop->nfs_argop4_u.opsetattr.stateid =
519 nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
520 OP_SETATTR, sid_types, FALSE);
521 } else {
522 bzero(&argop->nfs_argop4_u.opsetattr.stateid,
523 sizeof (stateid4));
524 }
525
526 *error = vattr_to_fattr4(vap, vsap, attr, flags, OP_SETATTR, supp);
527 if (*error)
528 bzero(attr, sizeof (*attr));
529 }
530
531 static void
nfs4args_setattr_free(nfs_argop4 * argop)532 nfs4args_setattr_free(nfs_argop4 *argop)
533 {
534 nfs4_fattr4_free(&argop->nfs_argop4_u.opsetattr.obj_attributes);
535 }
536
537 static int
nfs4args_verify(nfs_argop4 * argop,vattr_t * vap,enum nfs_opnum4 op,bitmap4 supp)538 nfs4args_verify(nfs_argop4 *argop, vattr_t *vap, enum nfs_opnum4 op,
539 bitmap4 supp)
540 {
541 fattr4 *attr;
542 int error = 0;
543
544 argop->argop = op;
545 switch (op) {
546 case OP_VERIFY:
547 attr = &argop->nfs_argop4_u.opverify.obj_attributes;
548 break;
549 case OP_NVERIFY:
550 attr = &argop->nfs_argop4_u.opnverify.obj_attributes;
551 break;
552 default:
553 return (EINVAL);
554 }
555 if (!error)
556 error = vattr_to_fattr4(vap, NULL, attr, 0, op, supp);
557 if (error)
558 bzero(attr, sizeof (*attr));
559 return (error);
560 }
561
562 static void
nfs4args_verify_free(nfs_argop4 * argop)563 nfs4args_verify_free(nfs_argop4 *argop)
564 {
565 switch (argop->argop) {
566 case OP_VERIFY:
567 nfs4_fattr4_free(&argop->nfs_argop4_u.opverify.obj_attributes);
568 break;
569 case OP_NVERIFY:
570 nfs4_fattr4_free(&argop->nfs_argop4_u.opnverify.obj_attributes);
571 break;
572 default:
573 break;
574 }
575 }
576
577 static void
nfs4args_write(nfs_argop4 * argop,stable_how4 stable,rnode4_t * rp,cred_t * cr,WRITE4args ** wargs_pp,nfs4_stateid_types_t * sid_tp)578 nfs4args_write(nfs_argop4 *argop, stable_how4 stable, rnode4_t *rp, cred_t *cr,
579 WRITE4args **wargs_pp, nfs4_stateid_types_t *sid_tp)
580 {
581 WRITE4args *wargs = &argop->nfs_argop4_u.opwrite;
582 mntinfo4_t *mi = VTOMI4(RTOV4(rp));
583
584 argop->argop = OP_WRITE;
585 wargs->stable = stable;
586 wargs->stateid = nfs4_get_w_stateid(cr, rp, curproc->p_pidp->pid_id,
587 mi, OP_WRITE, sid_tp);
588 wargs->mblk = NULL;
589 *wargs_pp = wargs;
590 }
591
592 void
nfs4args_copen_free(OPEN4cargs * open_args)593 nfs4args_copen_free(OPEN4cargs *open_args)
594 {
595 if (open_args->owner.owner_val) {
596 kmem_free(open_args->owner.owner_val,
597 open_args->owner.owner_len);
598 }
599 if ((open_args->opentype == OPEN4_CREATE) &&
600 (open_args->mode != EXCLUSIVE4)) {
601 nfs4_fattr4_free(&open_args->createhow4_u.createattrs);
602 }
603 }
604
605 /*
606 * XXX: This is referenced in modstubs.S
607 */
608 struct vnodeops *
nfs4_getvnodeops(void)609 nfs4_getvnodeops(void)
610 {
611 return (nfs4_vnodeops);
612 }
613
614 /*
615 * The OPEN operation opens a regular file.
616 */
617 /*ARGSUSED3*/
618 static int
nfs4_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)619 nfs4_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
620 {
621 vnode_t *dvp = NULL;
622 rnode4_t *rp, *drp;
623 int error;
624 int just_been_created;
625 char fn[MAXNAMELEN];
626
627 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4_open: "));
628 if (nfs_zone() != VTOMI4(*vpp)->mi_zone)
629 return (EIO);
630 rp = VTOR4(*vpp);
631
632 /*
633 * Check to see if opening something besides a regular file;
634 * if so skip the OTW call
635 */
636 if ((*vpp)->v_type != VREG) {
637 error = nfs4_open_non_reg_file(vpp, flag, cr);
638 return (error);
639 }
640
641 /*
642 * XXX - would like a check right here to know if the file is
643 * executable or not, so as to skip OTW
644 */
645
646 if ((error = vtodv(*vpp, &dvp, cr, TRUE)) != 0)
647 return (error);
648
649 drp = VTOR4(dvp);
650 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
651 return (EINTR);
652
653 if ((error = vtoname(*vpp, fn, MAXNAMELEN)) != 0) {
654 nfs_rw_exit(&drp->r_rwlock);
655 return (error);
656 }
657
658 /*
659 * See if this file has just been CREATEd.
660 * If so, clear the flag and update the dnlc, which was previously
661 * skipped in nfs4_create.
662 * XXX need better serilization on this.
663 * XXX move this into the nf4open_otw call, after we have
664 * XXX acquired the open owner seqid sync.
665 */
666 mutex_enter(&rp->r_statev4_lock);
667 if (rp->created_v4) {
668 rp->created_v4 = 0;
669 mutex_exit(&rp->r_statev4_lock);
670
671 dnlc_update(dvp, fn, *vpp);
672 /* This is needed so we don't bump the open ref count */
673 just_been_created = 1;
674 } else {
675 mutex_exit(&rp->r_statev4_lock);
676 just_been_created = 0;
677 }
678
679 /*
680 * If caller specified O_TRUNC/FTRUNC, then be sure to set
681 * FWRITE (to drive successful setattr(size=0) after open)
682 */
683 if (flag & FTRUNC)
684 flag |= FWRITE;
685
686 error = nfs4open_otw(dvp, fn, NULL, vpp, cr, 0, flag, 0,
687 just_been_created);
688
689 if (!error && !((*vpp)->v_flag & VROOT))
690 dnlc_update(dvp, fn, *vpp);
691
692 nfs_rw_exit(&drp->r_rwlock);
693
694 /* release the hold from vtodv */
695 VN_RELE(dvp);
696
697 /* exchange the shadow for the master vnode, if needed */
698
699 if (error == 0 && IS_SHADOW(*vpp, rp))
700 sv_exchange(vpp);
701
702 return (error);
703 }
704
705 /*
706 * See if there's a "lost open" request to be saved and recovered.
707 */
708 static void
nfs4open_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,nfs4_open_owner_t * oop,cred_t * cr,vnode_t * vp,vnode_t * dvp,OPEN4cargs * open_args)709 nfs4open_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
710 nfs4_open_owner_t *oop, cred_t *cr, vnode_t *vp,
711 vnode_t *dvp, OPEN4cargs *open_args)
712 {
713 vfs_t *vfsp;
714 char *srccfp;
715
716 vfsp = (dvp ? dvp->v_vfsp : vp->v_vfsp);
717
718 if (error != ETIMEDOUT && error != EINTR &&
719 !NFS4_FRC_UNMT_ERR(error, vfsp)) {
720 lost_rqstp->lr_op = 0;
721 return;
722 }
723
724 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
725 "nfs4open_save_lost_rqst: error %d", error));
726
727 lost_rqstp->lr_op = OP_OPEN;
728
729 /*
730 * The vp (if it is not NULL) and dvp are held and rele'd via
731 * the recovery code. See nfs4_save_lost_rqst.
732 */
733 lost_rqstp->lr_vp = vp;
734 lost_rqstp->lr_dvp = dvp;
735 lost_rqstp->lr_oop = oop;
736 lost_rqstp->lr_osp = NULL;
737 lost_rqstp->lr_lop = NULL;
738 lost_rqstp->lr_cr = cr;
739 lost_rqstp->lr_flk = NULL;
740 lost_rqstp->lr_oacc = open_args->share_access;
741 lost_rqstp->lr_odeny = open_args->share_deny;
742 lost_rqstp->lr_oclaim = open_args->claim;
743 if (open_args->claim == CLAIM_DELEGATE_CUR) {
744 lost_rqstp->lr_ostateid =
745 open_args->open_claim4_u.delegate_cur_info.delegate_stateid;
746 srccfp = open_args->open_claim4_u.delegate_cur_info.cfile;
747 } else {
748 srccfp = open_args->open_claim4_u.cfile;
749 }
750 lost_rqstp->lr_ofile.utf8string_len = 0;
751 lost_rqstp->lr_ofile.utf8string_val = NULL;
752 (void) str_to_utf8(srccfp, &lost_rqstp->lr_ofile);
753 lost_rqstp->lr_putfirst = FALSE;
754 }
755
756 struct nfs4_excl_time {
757 uint32 seconds;
758 uint32 nseconds;
759 };
760
761 /*
762 * The OPEN operation creates and/or opens a regular file
763 *
764 * ARGSUSED
765 */
766 static int
nfs4open_otw(vnode_t * dvp,char * file_name,struct vattr * in_va,vnode_t ** vpp,cred_t * cr,int create_flag,int open_flag,enum createmode4 createmode,int file_just_been_created)767 nfs4open_otw(vnode_t *dvp, char *file_name, struct vattr *in_va,
768 vnode_t **vpp, cred_t *cr, int create_flag, int open_flag,
769 enum createmode4 createmode, int file_just_been_created)
770 {
771 rnode4_t *rp;
772 rnode4_t *drp = VTOR4(dvp);
773 vnode_t *vp = NULL;
774 vnode_t *vpi = *vpp;
775 bool_t needrecov = FALSE;
776
777 int doqueue = 1;
778
779 COMPOUND4args_clnt args;
780 COMPOUND4res_clnt res;
781 nfs_argop4 *argop;
782 nfs_resop4 *resop;
783 int argoplist_size;
784 int idx_open, idx_fattr;
785
786 GETFH4res *gf_res = NULL;
787 OPEN4res *op_res = NULL;
788 nfs4_ga_res_t *garp;
789 fattr4 *attr = NULL;
790 struct nfs4_excl_time verf;
791 bool_t did_excl_setup = FALSE;
792 int created_osp;
793
794 OPEN4cargs *open_args;
795 nfs4_open_owner_t *oop = NULL;
796 nfs4_open_stream_t *osp = NULL;
797 seqid4 seqid = 0;
798 bool_t retry_open = FALSE;
799 nfs4_recov_state_t recov_state;
800 nfs4_lost_rqst_t lost_rqst;
801 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
802 hrtime_t t;
803 int acc = 0;
804 cred_t *cred_otw = NULL; /* cred used to do the RPC call */
805 cred_t *ncr = NULL;
806
807 nfs4_sharedfh_t *otw_sfh;
808 nfs4_sharedfh_t *orig_sfh;
809 int fh_differs = 0;
810 int numops, setgid_flag;
811 int num_bseqid_retry = NFS4_NUM_RETRY_BAD_SEQID + 1;
812
813 /*
814 * Make sure we properly deal with setting the right gid on
815 * a newly created file to reflect the parent's setgid bit
816 */
817 setgid_flag = 0;
818 if (create_flag && in_va) {
819
820 /*
821 * If there is grpid mount flag used or
822 * the parent's directory has the setgid bit set
823 * _and_ the client was able to get a valid mapping
824 * for the parent dir's owner_group, we want to
825 * append NVERIFY(owner_group == dva.va_gid) and
826 * SETATTR to the CREATE compound.
827 */
828 mutex_enter(&drp->r_statelock);
829 if ((VTOMI4(dvp)->mi_flags & MI4_GRPID ||
830 drp->r_attr.va_mode & VSGID) &&
831 drp->r_attr.va_gid != GID_NOBODY) {
832 in_va->va_mask |= AT_GID;
833 in_va->va_gid = drp->r_attr.va_gid;
834 setgid_flag = 1;
835 }
836 mutex_exit(&drp->r_statelock);
837 }
838
839 /*
840 * Normal/non-create compound:
841 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new)
842 *
843 * Open(create) compound no setgid:
844 * PUTFH(dfh) + SAVEFH + OPEN(create) + GETFH + GETATTR(new) +
845 * RESTOREFH + GETATTR
846 *
847 * Open(create) setgid:
848 * PUTFH(dfh) + OPEN(create) + GETFH + GETATTR(new) +
849 * SAVEFH + PUTFH(dfh) + GETATTR(dvp) + RESTOREFH +
850 * NVERIFY(grp) + SETATTR
851 */
852 if (setgid_flag) {
853 numops = 10;
854 idx_open = 1;
855 idx_fattr = 3;
856 } else if (create_flag) {
857 numops = 7;
858 idx_open = 2;
859 idx_fattr = 4;
860 } else {
861 numops = 4;
862 idx_open = 1;
863 idx_fattr = 3;
864 }
865
866 args.array_len = numops;
867 argoplist_size = numops * sizeof (nfs_argop4);
868 argop = kmem_alloc(argoplist_size, KM_SLEEP);
869
870 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw: "
871 "open %s open flag 0x%x cred %p", file_name, open_flag,
872 (void *)cr));
873
874 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
875 if (create_flag) {
876 /*
877 * We are to create a file. Initialize the passed in vnode
878 * pointer.
879 */
880 vpi = NULL;
881 } else {
882 /*
883 * Check to see if the client owns a read delegation and is
884 * trying to open for write. If so, then return the delegation
885 * to avoid the server doing a cb_recall and returning DELAY.
886 * NB - we don't use the statev4_lock here because we'd have
887 * to drop the lock anyway and the result would be stale.
888 */
889 if ((open_flag & FWRITE) &&
890 VTOR4(vpi)->r_deleg_type == OPEN_DELEGATE_READ)
891 (void) nfs4delegreturn(VTOR4(vpi), NFS4_DR_REOPEN);
892
893 /*
894 * If the file has a delegation, then do an access check up
895 * front. This avoids having to an access check later after
896 * we've already done start_op, which could deadlock.
897 */
898 if (VTOR4(vpi)->r_deleg_type != OPEN_DELEGATE_NONE) {
899 if (open_flag & FREAD &&
900 nfs4_access(vpi, VREAD, 0, cr, NULL) == 0)
901 acc |= VREAD;
902 if (open_flag & FWRITE &&
903 nfs4_access(vpi, VWRITE, 0, cr, NULL) == 0)
904 acc |= VWRITE;
905 }
906 }
907
908 drp = VTOR4(dvp);
909
910 recov_state.rs_flags = 0;
911 recov_state.rs_num_retry_despite_err = 0;
912 cred_otw = cr;
913
914 recov_retry:
915 fh_differs = 0;
916 nfs4_error_zinit(&e);
917
918 e.error = nfs4_start_op(VTOMI4(dvp), dvp, vpi, &recov_state);
919 if (e.error) {
920 if (ncr != NULL)
921 crfree(ncr);
922 kmem_free(argop, argoplist_size);
923 return (e.error);
924 }
925
926 args.ctag = TAG_OPEN;
927 args.array_len = numops;
928 args.array = argop;
929
930 /* putfh directory fh */
931 argop[0].argop = OP_CPUTFH;
932 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
933
934 /* OPEN: either op 1 or op 2 depending upon create/setgid flags */
935 argop[idx_open].argop = OP_COPEN;
936 open_args = &argop[idx_open].nfs_argop4_u.opcopen;
937 open_args->claim = CLAIM_NULL;
938
939 /* name of file */
940 open_args->open_claim4_u.cfile = file_name;
941 open_args->owner.owner_len = 0;
942 open_args->owner.owner_val = NULL;
943
944 if (create_flag) {
945 /* CREATE a file */
946 open_args->opentype = OPEN4_CREATE;
947 open_args->mode = createmode;
948 if (createmode == EXCLUSIVE4) {
949 if (did_excl_setup == FALSE) {
950 verf.seconds = zone_get_hostid(NULL);
951 if (verf.seconds != 0)
952 verf.nseconds = newnum();
953 else {
954 timestruc_t now;
955
956 gethrestime(&now);
957 verf.seconds = now.tv_sec;
958 verf.nseconds = now.tv_nsec;
959 }
960 /*
961 * Since the server will use this value for the
962 * mtime, make sure that it can't overflow. Zero
963 * out the MSB. The actual value does not matter
964 * here, only its uniqeness.
965 */
966 verf.seconds &= INT32_MAX;
967 did_excl_setup = TRUE;
968 }
969
970 /* Now copy over verifier to OPEN4args. */
971 open_args->createhow4_u.createverf = *(uint64_t *)&verf;
972 } else {
973 int v_error;
974 bitmap4 supp_attrs;
975 servinfo4_t *svp;
976
977 attr = &open_args->createhow4_u.createattrs;
978
979 svp = drp->r_server;
980 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
981 supp_attrs = svp->sv_supp_attrs;
982 nfs_rw_exit(&svp->sv_lock);
983
984 /* GUARDED4 or UNCHECKED4 */
985 v_error = vattr_to_fattr4(in_va, NULL, attr, 0, OP_OPEN,
986 supp_attrs);
987 if (v_error) {
988 bzero(attr, sizeof (*attr));
989 nfs4args_copen_free(open_args);
990 nfs4_end_op(VTOMI4(dvp), dvp, vpi,
991 &recov_state, FALSE);
992 if (ncr != NULL)
993 crfree(ncr);
994 kmem_free(argop, argoplist_size);
995 return (v_error);
996 }
997 }
998 } else {
999 /* NO CREATE */
1000 open_args->opentype = OPEN4_NOCREATE;
1001 }
1002
1003 if (recov_state.rs_sp != NULL) {
1004 mutex_enter(&recov_state.rs_sp->s_lock);
1005 open_args->owner.clientid = recov_state.rs_sp->clientid;
1006 mutex_exit(&recov_state.rs_sp->s_lock);
1007 } else {
1008 /* XXX should we just fail here? */
1009 open_args->owner.clientid = 0;
1010 }
1011
1012 /*
1013 * This increments oop's ref count or creates a temporary 'just_created'
1014 * open owner that will become valid when this OPEN/OPEN_CONFIRM call
1015 * completes.
1016 */
1017 mutex_enter(&VTOMI4(dvp)->mi_lock);
1018
1019 /* See if a permanent or just created open owner exists */
1020 oop = find_open_owner_nolock(cr, NFS4_JUST_CREATED, VTOMI4(dvp));
1021 if (!oop) {
1022 /*
1023 * This open owner does not exist so create a temporary
1024 * just created one.
1025 */
1026 oop = create_open_owner(cr, VTOMI4(dvp));
1027 ASSERT(oop != NULL);
1028 }
1029 mutex_exit(&VTOMI4(dvp)->mi_lock);
1030
1031 /* this length never changes, do alloc before seqid sync */
1032 open_args->owner.owner_len = sizeof (oop->oo_name);
1033 open_args->owner.owner_val =
1034 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1035
1036 e.error = nfs4_start_open_seqid_sync(oop, VTOMI4(dvp));
1037 if (e.error == EAGAIN) {
1038 open_owner_rele(oop);
1039 nfs4args_copen_free(open_args);
1040 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1041 if (ncr != NULL) {
1042 crfree(ncr);
1043 ncr = NULL;
1044 }
1045 goto recov_retry;
1046 }
1047
1048 /* Check to see if we need to do the OTW call */
1049 if (!create_flag) {
1050 if (!nfs4_is_otw_open_necessary(oop, open_flag, vpi,
1051 file_just_been_created, &e.error, acc, &recov_state)) {
1052
1053 /*
1054 * The OTW open is not necessary. Either
1055 * the open can succeed without it (eg.
1056 * delegation, error == 0) or the open
1057 * must fail due to an access failure
1058 * (error != 0). In either case, tidy
1059 * up and return.
1060 */
1061
1062 nfs4_end_open_seqid_sync(oop);
1063 open_owner_rele(oop);
1064 nfs4args_copen_free(open_args);
1065 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, FALSE);
1066 if (ncr != NULL)
1067 crfree(ncr);
1068 kmem_free(argop, argoplist_size);
1069 return (e.error);
1070 }
1071 }
1072
1073 bcopy(&oop->oo_name, open_args->owner.owner_val,
1074 open_args->owner.owner_len);
1075
1076 seqid = nfs4_get_open_seqid(oop) + 1;
1077 open_args->seqid = seqid;
1078 open_args->share_access = 0;
1079 if (open_flag & FREAD)
1080 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1081 if (open_flag & FWRITE)
1082 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1083 open_args->share_deny = OPEN4_SHARE_DENY_NONE;
1084
1085
1086
1087 /*
1088 * getfh w/sanity check for idx_open/idx_fattr
1089 */
1090 ASSERT((idx_open + 1) == (idx_fattr - 1));
1091 argop[idx_open + 1].argop = OP_GETFH;
1092
1093 /* getattr */
1094 argop[idx_fattr].argop = OP_GETATTR;
1095 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1096 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1097
1098 if (setgid_flag) {
1099 vattr_t _v;
1100 servinfo4_t *svp;
1101 bitmap4 supp_attrs;
1102
1103 svp = drp->r_server;
1104 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1105 supp_attrs = svp->sv_supp_attrs;
1106 nfs_rw_exit(&svp->sv_lock);
1107
1108 /*
1109 * For setgid case, we need to:
1110 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
1111 */
1112 argop[4].argop = OP_SAVEFH;
1113
1114 argop[5].argop = OP_CPUTFH;
1115 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
1116
1117 argop[6].argop = OP_GETATTR;
1118 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1119 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1120
1121 argop[7].argop = OP_RESTOREFH;
1122
1123 /*
1124 * nverify
1125 */
1126 _v.va_mask = AT_GID;
1127 _v.va_gid = in_va->va_gid;
1128 if (!(e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
1129 supp_attrs))) {
1130
1131 /*
1132 * setattr
1133 *
1134 * We _know_ we're not messing with AT_SIZE or
1135 * AT_XTIME, so no need for stateid or flags.
1136 * Also we specify NULL rp since we're only
1137 * interested in setting owner_group attributes.
1138 */
1139 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr,
1140 supp_attrs, &e.error, 0);
1141 if (e.error)
1142 nfs4args_verify_free(&argop[8]);
1143 }
1144
1145 if (e.error) {
1146 /*
1147 * XXX - Revisit the last argument to nfs4_end_op()
1148 * once 5020486 is fixed.
1149 */
1150 nfs4_end_open_seqid_sync(oop);
1151 open_owner_rele(oop);
1152 nfs4args_copen_free(open_args);
1153 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, TRUE);
1154 if (ncr != NULL)
1155 crfree(ncr);
1156 kmem_free(argop, argoplist_size);
1157 return (e.error);
1158 }
1159 } else if (create_flag) {
1160 argop[1].argop = OP_SAVEFH;
1161
1162 argop[5].argop = OP_RESTOREFH;
1163
1164 argop[6].argop = OP_GETATTR;
1165 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1166 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
1167 }
1168
1169 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1170 "nfs4open_otw: %s call, nm %s, rp %s",
1171 needrecov ? "recov" : "first", file_name,
1172 rnode4info(VTOR4(dvp))));
1173
1174 t = gethrtime();
1175
1176 rfs4call(VTOMI4(dvp), &args, &res, cred_otw, &doqueue, 0, &e);
1177
1178 if (!e.error && nfs4_need_to_bump_seqid(&res))
1179 nfs4_set_open_seqid(seqid, oop, args.ctag);
1180
1181 needrecov = nfs4_needs_recovery(&e, TRUE, dvp->v_vfsp);
1182
1183 if (e.error || needrecov) {
1184 bool_t abort = FALSE;
1185
1186 if (needrecov) {
1187 nfs4_bseqid_entry_t *bsep = NULL;
1188
1189 nfs4open_save_lost_rqst(e.error, &lost_rqst, oop,
1190 cred_otw, vpi, dvp, open_args);
1191
1192 if (!e.error && res.status == NFS4ERR_BAD_SEQID) {
1193 bsep = nfs4_create_bseqid_entry(oop, NULL,
1194 vpi, 0, args.ctag, open_args->seqid);
1195 num_bseqid_retry--;
1196 }
1197
1198 abort = nfs4_start_recovery(&e, VTOMI4(dvp), dvp, vpi,
1199 NULL, lost_rqst.lr_op == OP_OPEN ?
1200 &lost_rqst : NULL, OP_OPEN, bsep, NULL, NULL);
1201
1202 if (bsep)
1203 kmem_free(bsep, sizeof (*bsep));
1204 /* give up if we keep getting BAD_SEQID */
1205 if (num_bseqid_retry == 0)
1206 abort = TRUE;
1207 if (abort == TRUE && e.error == 0)
1208 e.error = geterrno4(res.status);
1209 }
1210 nfs4_end_open_seqid_sync(oop);
1211 open_owner_rele(oop);
1212 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1213 nfs4args_copen_free(open_args);
1214 if (setgid_flag) {
1215 nfs4args_verify_free(&argop[8]);
1216 nfs4args_setattr_free(&argop[9]);
1217 }
1218 if (!e.error)
1219 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1220 if (ncr != NULL) {
1221 crfree(ncr);
1222 ncr = NULL;
1223 }
1224 if (!needrecov || abort == TRUE || e.error == EINTR ||
1225 NFS4_FRC_UNMT_ERR(e.error, dvp->v_vfsp)) {
1226 kmem_free(argop, argoplist_size);
1227 return (e.error);
1228 }
1229 goto recov_retry;
1230 }
1231
1232 /*
1233 * Will check and update lease after checking the rflag for
1234 * OPEN_CONFIRM in the successful OPEN call.
1235 */
1236 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
1237
1238 /*
1239 * XXX what if we're crossing mount points from server1:/drp
1240 * to server2:/drp/rp.
1241 */
1242
1243 /* Signal our end of use of the open seqid */
1244 nfs4_end_open_seqid_sync(oop);
1245
1246 /*
1247 * This will destroy the open owner if it was just created,
1248 * and no one else has put a reference on it.
1249 */
1250 open_owner_rele(oop);
1251 if (create_flag && (createmode != EXCLUSIVE4) &&
1252 res.status == NFS4ERR_BADOWNER)
1253 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1254
1255 e.error = geterrno4(res.status);
1256 nfs4args_copen_free(open_args);
1257 if (setgid_flag) {
1258 nfs4args_verify_free(&argop[8]);
1259 nfs4args_setattr_free(&argop[9]);
1260 }
1261 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1262 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1263 /*
1264 * If the reply is NFS4ERR_ACCESS, it may be because
1265 * we are root (no root net access). If the real uid
1266 * is not root, then retry with the real uid instead.
1267 */
1268 if (ncr != NULL) {
1269 crfree(ncr);
1270 ncr = NULL;
1271 }
1272 if (res.status == NFS4ERR_ACCESS &&
1273 (ncr = crnetadjust(cred_otw)) != NULL) {
1274 cred_otw = ncr;
1275 goto recov_retry;
1276 }
1277 kmem_free(argop, argoplist_size);
1278 return (e.error);
1279 }
1280
1281 resop = &res.array[idx_open]; /* open res */
1282 op_res = &resop->nfs_resop4_u.opopen;
1283
1284 #ifdef DEBUG
1285 /*
1286 * verify attrset bitmap
1287 */
1288 if (create_flag &&
1289 (createmode == UNCHECKED4 || createmode == GUARDED4)) {
1290 /* make sure attrset returned is what we asked for */
1291 /* XXX Ignore this 'error' for now */
1292 if (attr->attrmask != op_res->attrset)
1293 /* EMPTY */;
1294 }
1295 #endif
1296
1297 if (op_res->rflags & OPEN4_RESULT_LOCKTYPE_POSIX) {
1298 mutex_enter(&VTOMI4(dvp)->mi_lock);
1299 VTOMI4(dvp)->mi_flags |= MI4_POSIX_LOCK;
1300 mutex_exit(&VTOMI4(dvp)->mi_lock);
1301 }
1302
1303 resop = &res.array[idx_open + 1]; /* getfh res */
1304 gf_res = &resop->nfs_resop4_u.opgetfh;
1305
1306 otw_sfh = sfh4_get(&gf_res->object, VTOMI4(dvp));
1307
1308 /*
1309 * The open stateid has been updated on the server but not
1310 * on the client yet. There is a path: makenfs4node->nfs4_attr_cache->
1311 * flush_pages->VOP_PUTPAGE->...->nfs4write where we will issue an OTW
1312 * WRITE call. That, however, will use the old stateid, so go ahead
1313 * and upate the open stateid now, before any call to makenfs4node.
1314 */
1315 if (vpi) {
1316 nfs4_open_stream_t *tmp_osp;
1317 rnode4_t *tmp_rp = VTOR4(vpi);
1318
1319 tmp_osp = find_open_stream(oop, tmp_rp);
1320 if (tmp_osp) {
1321 tmp_osp->open_stateid = op_res->stateid;
1322 mutex_exit(&tmp_osp->os_sync_lock);
1323 open_stream_rele(tmp_osp, tmp_rp);
1324 }
1325
1326 /*
1327 * We must determine if the file handle given by the otw open
1328 * is the same as the file handle which was passed in with
1329 * *vpp. This case can be reached if the file we are trying
1330 * to open has been removed and another file has been created
1331 * having the same file name. The passed in vnode is released
1332 * later.
1333 */
1334 orig_sfh = VTOR4(vpi)->r_fh;
1335 fh_differs = nfs4cmpfh(&orig_sfh->sfh_fh, &otw_sfh->sfh_fh);
1336 }
1337
1338 garp = &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res;
1339
1340 if (create_flag || fh_differs) {
1341 int rnode_err = 0;
1342
1343 vp = makenfs4node(otw_sfh, garp, dvp->v_vfsp, t, cr,
1344 dvp, fn_get(VTOSV(dvp)->sv_name, file_name, otw_sfh));
1345
1346 if (e.error)
1347 PURGE_ATTRCACHE4(vp);
1348 /*
1349 * For the newly created vp case, make sure the rnode
1350 * isn't bad before using it.
1351 */
1352 mutex_enter(&(VTOR4(vp))->r_statelock);
1353 if (VTOR4(vp)->r_flags & R4RECOVERR)
1354 rnode_err = EIO;
1355 mutex_exit(&(VTOR4(vp))->r_statelock);
1356
1357 if (rnode_err) {
1358 nfs4_end_open_seqid_sync(oop);
1359 nfs4args_copen_free(open_args);
1360 if (setgid_flag) {
1361 nfs4args_verify_free(&argop[8]);
1362 nfs4args_setattr_free(&argop[9]);
1363 }
1364 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1365 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1366 needrecov);
1367 open_owner_rele(oop);
1368 VN_RELE(vp);
1369 if (ncr != NULL)
1370 crfree(ncr);
1371 sfh4_rele(&otw_sfh);
1372 kmem_free(argop, argoplist_size);
1373 return (EIO);
1374 }
1375 } else {
1376 vp = vpi;
1377 }
1378 sfh4_rele(&otw_sfh);
1379
1380 /*
1381 * It seems odd to get a full set of attrs and then not update
1382 * the object's attrcache in the non-create case. Create case uses
1383 * the attrs since makenfs4node checks to see if the attrs need to
1384 * be updated (and then updates them). The non-create case should
1385 * update attrs also.
1386 */
1387 if (! create_flag && ! fh_differs && !e.error) {
1388 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
1389 }
1390
1391 nfs4_error_zinit(&e);
1392 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
1393 /* This does not do recovery for vp explicitly. */
1394 nfs4open_confirm(vp, &seqid, &op_res->stateid, cred_otw, FALSE,
1395 &retry_open, oop, FALSE, &e, &num_bseqid_retry);
1396
1397 if (e.error || e.stat) {
1398 nfs4_end_open_seqid_sync(oop);
1399 nfs4args_copen_free(open_args);
1400 if (setgid_flag) {
1401 nfs4args_verify_free(&argop[8]);
1402 nfs4args_setattr_free(&argop[9]);
1403 }
1404 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1405 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state,
1406 needrecov);
1407 open_owner_rele(oop);
1408 if (create_flag || fh_differs) {
1409 /* rele the makenfs4node */
1410 VN_RELE(vp);
1411 }
1412 if (ncr != NULL) {
1413 crfree(ncr);
1414 ncr = NULL;
1415 }
1416 if (retry_open == TRUE) {
1417 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1418 "nfs4open_otw: retry the open since OPEN "
1419 "CONFIRM failed with error %d stat %d",
1420 e.error, e.stat));
1421 if (create_flag && createmode == GUARDED4) {
1422 NFS4_DEBUG(nfs4_client_recov_debug,
1423 (CE_NOTE, "nfs4open_otw: switch "
1424 "createmode from GUARDED4 to "
1425 "UNCHECKED4"));
1426 createmode = UNCHECKED4;
1427 }
1428 goto recov_retry;
1429 }
1430 if (!e.error) {
1431 if (create_flag && (createmode != EXCLUSIVE4) &&
1432 e.stat == NFS4ERR_BADOWNER)
1433 nfs4_log_badowner(VTOMI4(dvp), OP_OPEN);
1434
1435 e.error = geterrno4(e.stat);
1436 }
1437 kmem_free(argop, argoplist_size);
1438 return (e.error);
1439 }
1440 }
1441
1442 rp = VTOR4(vp);
1443
1444 mutex_enter(&rp->r_statev4_lock);
1445 if (create_flag)
1446 rp->created_v4 = 1;
1447 mutex_exit(&rp->r_statev4_lock);
1448
1449 mutex_enter(&oop->oo_lock);
1450 /* Doesn't matter if 'oo_just_created' already was set as this */
1451 oop->oo_just_created = NFS4_PERM_CREATED;
1452 if (oop->oo_cred_otw)
1453 crfree(oop->oo_cred_otw);
1454 oop->oo_cred_otw = cred_otw;
1455 crhold(oop->oo_cred_otw);
1456 mutex_exit(&oop->oo_lock);
1457
1458 /* returns with 'os_sync_lock' held */
1459 osp = find_or_create_open_stream(oop, rp, &created_osp);
1460 if (!osp) {
1461 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1462 "nfs4open_otw: failed to create an open stream"));
1463 NFS4_DEBUG(nfs4_seqid_sync, (CE_NOTE, "nfs4open_otw: "
1464 "signal our end of use of the open seqid"));
1465
1466 nfs4_end_open_seqid_sync(oop);
1467 open_owner_rele(oop);
1468 nfs4args_copen_free(open_args);
1469 if (setgid_flag) {
1470 nfs4args_verify_free(&argop[8]);
1471 nfs4args_setattr_free(&argop[9]);
1472 }
1473 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1474 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1475 if (create_flag || fh_differs)
1476 VN_RELE(vp);
1477 if (ncr != NULL)
1478 crfree(ncr);
1479
1480 kmem_free(argop, argoplist_size);
1481 return (EINVAL);
1482
1483 }
1484
1485 osp->open_stateid = op_res->stateid;
1486
1487 if (open_flag & FREAD)
1488 osp->os_share_acc_read++;
1489 if (open_flag & FWRITE)
1490 osp->os_share_acc_write++;
1491 osp->os_share_deny_none++;
1492
1493 /*
1494 * Need to reset this bitfield for the possible case where we were
1495 * going to OTW CLOSE the file, got a non-recoverable error, and before
1496 * we could retry the CLOSE, OPENed the file again.
1497 */
1498 ASSERT(osp->os_open_owner->oo_seqid_inuse);
1499 osp->os_final_close = 0;
1500 osp->os_force_close = 0;
1501 #ifdef DEBUG
1502 if (osp->os_failed_reopen)
1503 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE, "nfs4open_otw:"
1504 " clearing os_failed_reopen for osp %p, cr %p, rp %s",
1505 (void *)osp, (void *)cr, rnode4info(rp)));
1506 #endif
1507 osp->os_failed_reopen = 0;
1508
1509 mutex_exit(&osp->os_sync_lock);
1510
1511 nfs4_end_open_seqid_sync(oop);
1512
1513 if (created_osp && recov_state.rs_sp != NULL) {
1514 mutex_enter(&recov_state.rs_sp->s_lock);
1515 nfs4_inc_state_ref_count_nolock(recov_state.rs_sp, VTOMI4(dvp));
1516 mutex_exit(&recov_state.rs_sp->s_lock);
1517 }
1518
1519 /* get rid of our reference to find oop */
1520 open_owner_rele(oop);
1521
1522 open_stream_rele(osp, rp);
1523
1524 /* accept delegation, if any */
1525 nfs4_delegation_accept(rp, CLAIM_NULL, op_res, garp, cred_otw);
1526
1527 nfs4_end_op(VTOMI4(dvp), dvp, vpi, &recov_state, needrecov);
1528
1529 if (createmode == EXCLUSIVE4 &&
1530 (in_va->va_mask & ~(AT_GID | AT_SIZE))) {
1531 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4open_otw:"
1532 " EXCLUSIVE4: sending a SETATTR"));
1533 /*
1534 * If doing an exclusive create, then generate
1535 * a SETATTR to set the initial attributes.
1536 * Try to set the mtime and the atime to the
1537 * server's current time. It is somewhat
1538 * expected that these fields will be used to
1539 * store the exclusive create cookie. If not,
1540 * server implementors will need to know that
1541 * a SETATTR will follow an exclusive create
1542 * and the cookie should be destroyed if
1543 * appropriate.
1544 *
1545 * The AT_GID and AT_SIZE bits are turned off
1546 * so that the SETATTR request will not attempt
1547 * to process these. The gid will be set
1548 * separately if appropriate. The size is turned
1549 * off because it is assumed that a new file will
1550 * be created empty and if the file wasn't empty,
1551 * then the exclusive create will have failed
1552 * because the file must have existed already.
1553 * Therefore, no truncate operation is needed.
1554 */
1555 in_va->va_mask &= ~(AT_GID | AT_SIZE);
1556 in_va->va_mask |= (AT_MTIME | AT_ATIME);
1557
1558 e.error = nfs4setattr(vp, in_va, 0, cr, NULL);
1559 if (e.error) {
1560 nfs4_error_t err;
1561
1562 /*
1563 * Couldn't correct the attributes of
1564 * the newly created file and the
1565 * attributes are wrong. Remove the
1566 * file and return an error to the
1567 * application.
1568 */
1569 /* XXX will this take care of client state ? */
1570 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
1571 "nfs4open_otw: EXCLUSIVE4: error %d on SETATTR:"
1572 " remove file", e.error));
1573
1574 /*
1575 * The file is currently open so try to close it first.
1576 *
1577 * If we do not close the file explicitly here then the
1578 * VN_RELE() would do an (implicit and asynchronous)
1579 * close for us. But such async close could race with
1580 * the nfs4_remove() below. If the async close is
1581 * slower than nfs4_remove() then nfs4_remove()
1582 * wouldn't remove the file but rename it to .nfsXXXX
1583 * instead.
1584 */
1585 nfs4close_one(vp, NULL, cr, open_flag, NULL, &err,
1586 CLOSE_NORM, 0, 0, 0);
1587 VN_RELE(vp);
1588 (void) nfs4_remove(dvp, file_name, cr, NULL, 0);
1589
1590 /*
1591 * Since we've reled the vnode and removed
1592 * the file we now need to return the error.
1593 * At this point we don't want to update the
1594 * dircaches, call nfs4_waitfor_purge_complete
1595 * or set vpp to vp so we need to skip these
1596 * as well.
1597 */
1598 goto skip_update_dircaches;
1599 }
1600 }
1601
1602 /*
1603 * If we created or found the correct vnode, due to create_flag or
1604 * fh_differs being set, then update directory cache attribute, readdir
1605 * and dnlc caches.
1606 */
1607 if (create_flag || fh_differs) {
1608 dirattr_info_t dinfo, *dinfop;
1609
1610 /*
1611 * Make sure getattr succeeded before using results.
1612 * note: op 7 is getattr(dir) for both flavors of
1613 * open(create).
1614 */
1615 if (create_flag && res.status == NFS4_OK) {
1616 dinfo.di_time_call = t;
1617 dinfo.di_cred = cr;
1618 dinfo.di_garp =
1619 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
1620 dinfop = &dinfo;
1621 } else {
1622 dinfop = NULL;
1623 }
1624
1625 nfs4_update_dircaches(&op_res->cinfo, dvp, vp, file_name,
1626 dinfop);
1627 }
1628
1629 /*
1630 * If the page cache for this file was flushed from actions
1631 * above, it was done asynchronously and if that is true,
1632 * there is a need to wait here for it to complete. This must
1633 * be done outside of start_fop/end_fop.
1634 */
1635 (void) nfs4_waitfor_purge_complete(vp);
1636
1637 /*
1638 * It is implicit that we are in the open case (create_flag == 0) since
1639 * fh_differs can only be set to a non-zero value in the open case.
1640 */
1641 if (fh_differs != 0 && vpi != NULL)
1642 VN_RELE(vpi);
1643
1644 /*
1645 * Be sure to set *vpp to the correct value before returning.
1646 */
1647 *vpp = vp;
1648
1649 skip_update_dircaches:
1650
1651 nfs4args_copen_free(open_args);
1652 if (setgid_flag) {
1653 nfs4args_verify_free(&argop[8]);
1654 nfs4args_setattr_free(&argop[9]);
1655 }
1656 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1657
1658 if (ncr)
1659 crfree(ncr);
1660 kmem_free(argop, argoplist_size);
1661 return (e.error);
1662 }
1663
1664 /*
1665 * Reopen an open instance. cf. nfs4open_otw().
1666 *
1667 * Errors are returned by the nfs4_error_t parameter.
1668 * - ep->error contains an errno value or zero.
1669 * - if it is zero, ep->stat is set to an NFS status code, if any.
1670 * If the file could not be reopened, but the caller should continue, the
1671 * file is marked dead and no error values are returned. If the caller
1672 * should stop recovering open files and start over, either the ep->error
1673 * value or ep->stat will indicate an error (either something that requires
1674 * recovery or EAGAIN). Note that some recovery (e.g., expired volatile
1675 * filehandles) may be handled silently by this routine.
1676 * - if it is EINTR, ETIMEDOUT, or NFS4_FRC_UNMT_ERR, recovery for lost state
1677 * will be started, so the caller should not do it.
1678 *
1679 * Gotos:
1680 * - kill_file : reopen failed in such a fashion to constitute marking the
1681 * file dead and setting the open stream's 'os_failed_reopen' as 1. This
1682 * is for cases where recovery is not possible.
1683 * - failed_reopen : same as above, except that the file has already been
1684 * marked dead, so no need to do it again.
1685 * - bailout : reopen failed but we are able to recover and retry the reopen -
1686 * either within this function immediately or via the calling function.
1687 */
1688
1689 void
nfs4_reopen(vnode_t * vp,nfs4_open_stream_t * osp,nfs4_error_t * ep,open_claim_type4 claim,bool_t frc_use_claim_previous,bool_t is_recov)1690 nfs4_reopen(vnode_t *vp, nfs4_open_stream_t *osp, nfs4_error_t *ep,
1691 open_claim_type4 claim, bool_t frc_use_claim_previous,
1692 bool_t is_recov)
1693 {
1694 COMPOUND4args_clnt args;
1695 COMPOUND4res_clnt res;
1696 nfs_argop4 argop[4];
1697 nfs_resop4 *resop;
1698 OPEN4res *op_res = NULL;
1699 OPEN4cargs *open_args;
1700 GETFH4res *gf_res;
1701 rnode4_t *rp = VTOR4(vp);
1702 int doqueue = 1;
1703 cred_t *cr = NULL, *cred_otw = NULL;
1704 nfs4_open_owner_t *oop = NULL;
1705 seqid4 seqid;
1706 nfs4_ga_res_t *garp;
1707 char fn[MAXNAMELEN];
1708 nfs4_recov_state_t recov = {NULL, 0};
1709 nfs4_lost_rqst_t lost_rqst;
1710 mntinfo4_t *mi = VTOMI4(vp);
1711 bool_t abort;
1712 char *failed_msg = "";
1713 int fh_different;
1714 hrtime_t t;
1715 nfs4_bseqid_entry_t *bsep = NULL;
1716
1717 ASSERT(nfs4_consistent_type(vp));
1718 ASSERT(nfs_zone() == mi->mi_zone);
1719
1720 nfs4_error_zinit(ep);
1721
1722 /* this is the cred used to find the open owner */
1723 cr = state_to_cred(osp);
1724 if (cr == NULL) {
1725 failed_msg = "Couldn't reopen: no cred";
1726 goto kill_file;
1727 }
1728 /* use this cred for OTW operations */
1729 cred_otw = nfs4_get_otw_cred(cr, mi, osp->os_open_owner);
1730
1731 top:
1732 nfs4_error_zinit(ep);
1733
1734 if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) {
1735 /* File system has been unmounted, quit */
1736 ep->error = EIO;
1737 failed_msg = "Couldn't reopen: file system has been unmounted";
1738 goto kill_file;
1739 }
1740
1741 oop = osp->os_open_owner;
1742
1743 ASSERT(oop != NULL);
1744 if (oop == NULL) { /* be defensive in non-DEBUG */
1745 failed_msg = "can't reopen: no open owner";
1746 goto kill_file;
1747 }
1748 open_owner_hold(oop);
1749
1750 ep->error = nfs4_start_open_seqid_sync(oop, mi);
1751 if (ep->error) {
1752 open_owner_rele(oop);
1753 oop = NULL;
1754 goto bailout;
1755 }
1756
1757 /*
1758 * If the rnode has a delegation and the delegation has been
1759 * recovered and the server didn't request a recall and the caller
1760 * didn't specifically ask for CLAIM_PREVIOUS (nfs4frlock during
1761 * recovery) and the rnode hasn't been marked dead, then install
1762 * the delegation stateid in the open stream. Otherwise, proceed
1763 * with a CLAIM_PREVIOUS or CLAIM_NULL OPEN.
1764 */
1765 mutex_enter(&rp->r_statev4_lock);
1766 if (rp->r_deleg_type != OPEN_DELEGATE_NONE &&
1767 !rp->r_deleg_return_pending &&
1768 (rp->r_deleg_needs_recovery == OPEN_DELEGATE_NONE) &&
1769 !rp->r_deleg_needs_recall &&
1770 claim != CLAIM_DELEGATE_CUR && !frc_use_claim_previous &&
1771 !(rp->r_flags & R4RECOVERR)) {
1772 mutex_enter(&osp->os_sync_lock);
1773 osp->os_delegation = 1;
1774 osp->open_stateid = rp->r_deleg_stateid;
1775 mutex_exit(&osp->os_sync_lock);
1776 mutex_exit(&rp->r_statev4_lock);
1777 goto bailout;
1778 }
1779 mutex_exit(&rp->r_statev4_lock);
1780
1781 /*
1782 * If the file failed recovery, just quit. This failure need not
1783 * affect other reopens, so don't return an error.
1784 */
1785 mutex_enter(&rp->r_statelock);
1786 if (rp->r_flags & R4RECOVERR) {
1787 mutex_exit(&rp->r_statelock);
1788 ep->error = 0;
1789 goto failed_reopen;
1790 }
1791 mutex_exit(&rp->r_statelock);
1792
1793 /*
1794 * argop is empty here
1795 *
1796 * PUTFH, OPEN, GETATTR
1797 */
1798 args.ctag = TAG_REOPEN;
1799 args.array_len = 4;
1800 args.array = argop;
1801
1802 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1803 "nfs4_reopen: file is type %d, id %s",
1804 vp->v_type, rnode4info(VTOR4(vp))));
1805
1806 argop[0].argop = OP_CPUTFH;
1807
1808 if (claim != CLAIM_PREVIOUS) {
1809 /*
1810 * if this is a file mount then
1811 * use the mntinfo parentfh
1812 */
1813 argop[0].nfs_argop4_u.opcputfh.sfh =
1814 (vp->v_flag & VROOT) ? mi->mi_srvparentfh :
1815 VTOSV(vp)->sv_dfh;
1816 } else {
1817 /* putfh fh to reopen */
1818 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
1819 }
1820
1821 argop[1].argop = OP_COPEN;
1822 open_args = &argop[1].nfs_argop4_u.opcopen;
1823 open_args->claim = claim;
1824
1825 if (claim == CLAIM_NULL) {
1826
1827 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1828 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1829 "failed for vp 0x%p for CLAIM_NULL with %m",
1830 (void *)vp);
1831 failed_msg = "Couldn't reopen: vtoname failed for "
1832 "CLAIM_NULL";
1833 /* nothing allocated yet */
1834 goto kill_file;
1835 }
1836
1837 open_args->open_claim4_u.cfile = fn;
1838 } else if (claim == CLAIM_PREVIOUS) {
1839
1840 /*
1841 * We have two cases to deal with here:
1842 * 1) We're being called to reopen files in order to satisfy
1843 * a lock operation request which requires us to explicitly
1844 * reopen files which were opened under a delegation. If
1845 * we're in recovery, we *must* use CLAIM_PREVIOUS. In
1846 * that case, frc_use_claim_previous is TRUE and we must
1847 * use the rnode's current delegation type (r_deleg_type).
1848 * 2) We're reopening files during some form of recovery.
1849 * In this case, frc_use_claim_previous is FALSE and we
1850 * use the delegation type appropriate for recovery
1851 * (r_deleg_needs_recovery).
1852 */
1853 mutex_enter(&rp->r_statev4_lock);
1854 open_args->open_claim4_u.delegate_type =
1855 frc_use_claim_previous ?
1856 rp->r_deleg_type :
1857 rp->r_deleg_needs_recovery;
1858 mutex_exit(&rp->r_statev4_lock);
1859
1860 } else if (claim == CLAIM_DELEGATE_CUR) {
1861
1862 if ((ep->error = vtoname(vp, fn, MAXNAMELEN)) != 0) {
1863 nfs_cmn_err(ep->error, CE_WARN, "nfs4_reopen: vtoname "
1864 "failed for vp 0x%p for CLAIM_DELEGATE_CUR "
1865 "with %m", (void *)vp);
1866 failed_msg = "Couldn't reopen: vtoname failed for "
1867 "CLAIM_DELEGATE_CUR";
1868 /* nothing allocated yet */
1869 goto kill_file;
1870 }
1871
1872 mutex_enter(&rp->r_statev4_lock);
1873 open_args->open_claim4_u.delegate_cur_info.delegate_stateid =
1874 rp->r_deleg_stateid;
1875 mutex_exit(&rp->r_statev4_lock);
1876
1877 open_args->open_claim4_u.delegate_cur_info.cfile = fn;
1878 }
1879 open_args->opentype = OPEN4_NOCREATE;
1880 open_args->owner.clientid = mi2clientid(mi);
1881 open_args->owner.owner_len = sizeof (oop->oo_name);
1882 open_args->owner.owner_val =
1883 kmem_alloc(open_args->owner.owner_len, KM_SLEEP);
1884 bcopy(&oop->oo_name, open_args->owner.owner_val,
1885 open_args->owner.owner_len);
1886 open_args->share_access = 0;
1887 open_args->share_deny = 0;
1888
1889 mutex_enter(&osp->os_sync_lock);
1890 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE, "nfs4_reopen: osp %p rp "
1891 "%p: read acc %"PRIu64" write acc %"PRIu64": open ref count %d: "
1892 "mmap read %"PRIu64" mmap write %"PRIu64" claim %d ",
1893 (void *)osp, (void *)rp, osp->os_share_acc_read,
1894 osp->os_share_acc_write, osp->os_open_ref_count,
1895 osp->os_mmap_read, osp->os_mmap_write, claim));
1896
1897 if (osp->os_share_acc_read || osp->os_mmap_read)
1898 open_args->share_access |= OPEN4_SHARE_ACCESS_READ;
1899 if (osp->os_share_acc_write || osp->os_mmap_write)
1900 open_args->share_access |= OPEN4_SHARE_ACCESS_WRITE;
1901 if (osp->os_share_deny_read)
1902 open_args->share_deny |= OPEN4_SHARE_DENY_READ;
1903 if (osp->os_share_deny_write)
1904 open_args->share_deny |= OPEN4_SHARE_DENY_WRITE;
1905 mutex_exit(&osp->os_sync_lock);
1906
1907 seqid = nfs4_get_open_seqid(oop) + 1;
1908 open_args->seqid = seqid;
1909
1910 /* Construct the getfh part of the compound */
1911 argop[2].argop = OP_GETFH;
1912
1913 /* Construct the getattr part of the compound */
1914 argop[3].argop = OP_GETATTR;
1915 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
1916 argop[3].nfs_argop4_u.opgetattr.mi = mi;
1917
1918 t = gethrtime();
1919
1920 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
1921
1922 if (ep->error) {
1923 if (!is_recov && !frc_use_claim_previous &&
1924 (ep->error == EINTR || ep->error == ETIMEDOUT ||
1925 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))) {
1926 nfs4open_save_lost_rqst(ep->error, &lost_rqst, oop,
1927 cred_otw, vp, NULL, open_args);
1928 abort = nfs4_start_recovery(ep,
1929 VTOMI4(vp), vp, NULL, NULL,
1930 lost_rqst.lr_op == OP_OPEN ?
1931 &lost_rqst : NULL, OP_OPEN, NULL, NULL, NULL);
1932 nfs4args_copen_free(open_args);
1933 goto bailout;
1934 }
1935
1936 nfs4args_copen_free(open_args);
1937
1938 if (ep->error == EACCES && cred_otw != cr) {
1939 crfree(cred_otw);
1940 cred_otw = cr;
1941 crhold(cred_otw);
1942 nfs4_end_open_seqid_sync(oop);
1943 open_owner_rele(oop);
1944 oop = NULL;
1945 goto top;
1946 }
1947 if (ep->error == ETIMEDOUT)
1948 goto bailout;
1949 failed_msg = "Couldn't reopen: rpc error";
1950 goto kill_file;
1951 }
1952
1953 if (nfs4_need_to_bump_seqid(&res))
1954 nfs4_set_open_seqid(seqid, oop, args.ctag);
1955
1956 switch (res.status) {
1957 case NFS4_OK:
1958 if (recov.rs_flags & NFS4_RS_DELAY_MSG) {
1959 mutex_enter(&rp->r_statelock);
1960 rp->r_delay_interval = 0;
1961 mutex_exit(&rp->r_statelock);
1962 }
1963 break;
1964 case NFS4ERR_BAD_SEQID:
1965 bsep = nfs4_create_bseqid_entry(oop, NULL, vp, 0,
1966 args.ctag, open_args->seqid);
1967
1968 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
1969 NULL, lost_rqst.lr_op == OP_OPEN ? &lost_rqst :
1970 NULL, OP_OPEN, bsep, NULL, NULL);
1971
1972 nfs4args_copen_free(open_args);
1973 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1974 nfs4_end_open_seqid_sync(oop);
1975 open_owner_rele(oop);
1976 oop = NULL;
1977 kmem_free(bsep, sizeof (*bsep));
1978
1979 goto kill_file;
1980 case NFS4ERR_NO_GRACE:
1981 nfs4args_copen_free(open_args);
1982 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1983 nfs4_end_open_seqid_sync(oop);
1984 open_owner_rele(oop);
1985 oop = NULL;
1986 if (claim == CLAIM_PREVIOUS) {
1987 /*
1988 * Retry as a plain open. We don't need to worry about
1989 * checking the changeinfo: it is acceptable for a
1990 * client to re-open a file and continue processing
1991 * (in the absence of locks).
1992 */
1993 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1994 "nfs4_reopen: CLAIM_PREVIOUS: NFS4ERR_NO_GRACE; "
1995 "will retry as CLAIM_NULL"));
1996 claim = CLAIM_NULL;
1997 nfs4_mi_kstat_inc_no_grace(mi);
1998 goto top;
1999 }
2000 failed_msg =
2001 "Couldn't reopen: tried reclaim outside grace period. ";
2002 goto kill_file;
2003 case NFS4ERR_GRACE:
2004 nfs4_set_grace_wait(mi);
2005 nfs4args_copen_free(open_args);
2006 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2007 nfs4_end_open_seqid_sync(oop);
2008 open_owner_rele(oop);
2009 oop = NULL;
2010 ep->error = nfs4_wait_for_grace(mi, &recov);
2011 if (ep->error != 0)
2012 goto bailout;
2013 goto top;
2014 case NFS4ERR_DELAY:
2015 nfs4_set_delay_wait(vp);
2016 nfs4args_copen_free(open_args);
2017 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2018 nfs4_end_open_seqid_sync(oop);
2019 open_owner_rele(oop);
2020 oop = NULL;
2021 ep->error = nfs4_wait_for_delay(vp, &recov);
2022 nfs4_mi_kstat_inc_delay(mi);
2023 if (ep->error != 0)
2024 goto bailout;
2025 goto top;
2026 case NFS4ERR_FHEXPIRED:
2027 /* recover filehandle and retry */
2028 abort = nfs4_start_recovery(ep,
2029 mi, vp, NULL, NULL, NULL, OP_OPEN, NULL, NULL, NULL);
2030 nfs4args_copen_free(open_args);
2031 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2032 nfs4_end_open_seqid_sync(oop);
2033 open_owner_rele(oop);
2034 oop = NULL;
2035 if (abort == FALSE)
2036 goto top;
2037 failed_msg = "Couldn't reopen: recovery aborted";
2038 goto kill_file;
2039 case NFS4ERR_RESOURCE:
2040 case NFS4ERR_STALE_CLIENTID:
2041 case NFS4ERR_WRONGSEC:
2042 case NFS4ERR_EXPIRED:
2043 /*
2044 * Do not mark the file dead and let the calling
2045 * function initiate recovery.
2046 */
2047 nfs4args_copen_free(open_args);
2048 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2049 nfs4_end_open_seqid_sync(oop);
2050 open_owner_rele(oop);
2051 oop = NULL;
2052 goto bailout;
2053 case NFS4ERR_ACCESS:
2054 if (cred_otw != cr) {
2055 crfree(cred_otw);
2056 cred_otw = cr;
2057 crhold(cred_otw);
2058 nfs4args_copen_free(open_args);
2059 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2060 nfs4_end_open_seqid_sync(oop);
2061 open_owner_rele(oop);
2062 oop = NULL;
2063 goto top;
2064 }
2065 /* fall through */
2066 default:
2067 NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
2068 "nfs4_reopen: r_server 0x%p, mi_curr_serv 0x%p, rnode %s",
2069 (void*)VTOR4(vp)->r_server, (void*)mi->mi_curr_serv,
2070 rnode4info(VTOR4(vp))));
2071 failed_msg = "Couldn't reopen: NFSv4 error";
2072 nfs4args_copen_free(open_args);
2073 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2074 goto kill_file;
2075 }
2076
2077 resop = &res.array[1]; /* open res */
2078 op_res = &resop->nfs_resop4_u.opopen;
2079
2080 garp = &res.array[3].nfs_resop4_u.opgetattr.ga_res;
2081
2082 /*
2083 * Check if the path we reopened really is the same
2084 * file. We could end up in a situation where the file
2085 * was removed and a new file created with the same name.
2086 */
2087 resop = &res.array[2];
2088 gf_res = &resop->nfs_resop4_u.opgetfh;
2089 (void) nfs_rw_enter_sig(&mi->mi_fh_lock, RW_READER, 0);
2090 fh_different = (nfs4cmpfh(&rp->r_fh->sfh_fh, &gf_res->object) != 0);
2091 if (fh_different) {
2092 if (mi->mi_fh_expire_type == FH4_PERSISTENT ||
2093 mi->mi_fh_expire_type & FH4_NOEXPIRE_WITH_OPEN) {
2094 /* Oops, we don't have the same file */
2095 if (mi->mi_fh_expire_type == FH4_PERSISTENT)
2096 failed_msg = "Couldn't reopen: Persistent "
2097 "file handle changed";
2098 else
2099 failed_msg = "Couldn't reopen: Volatile "
2100 "(no expire on open) file handle changed";
2101
2102 nfs4args_copen_free(open_args);
2103 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2104 nfs_rw_exit(&mi->mi_fh_lock);
2105 goto kill_file;
2106
2107 } else {
2108 /*
2109 * We have volatile file handles that don't compare.
2110 * If the fids are the same then we assume that the
2111 * file handle expired but the rnode still refers to
2112 * the same file object.
2113 *
2114 * First check that we have fids or not.
2115 * If we don't we have a dumb server so we will
2116 * just assume every thing is ok for now.
2117 */
2118 if (!ep->error && garp->n4g_va.va_mask & AT_NODEID &&
2119 rp->r_attr.va_mask & AT_NODEID &&
2120 rp->r_attr.va_nodeid != garp->n4g_va.va_nodeid) {
2121 /*
2122 * We have fids, but they don't
2123 * compare. So kill the file.
2124 */
2125 failed_msg =
2126 "Couldn't reopen: file handle changed"
2127 " due to mismatched fids";
2128 nfs4args_copen_free(open_args);
2129 xdr_free(xdr_COMPOUND4res_clnt,
2130 (caddr_t)&res);
2131 nfs_rw_exit(&mi->mi_fh_lock);
2132 goto kill_file;
2133 } else {
2134 /*
2135 * We have volatile file handles that refers
2136 * to the same file (at least they have the
2137 * same fid) or we don't have fids so we
2138 * can't tell. :(. We'll be a kind and accepting
2139 * client so we'll update the rnode's file
2140 * handle with the otw handle.
2141 *
2142 * We need to drop mi->mi_fh_lock since
2143 * sh4_update acquires it. Since there is
2144 * only one recovery thread there is no
2145 * race.
2146 */
2147 nfs_rw_exit(&mi->mi_fh_lock);
2148 sfh4_update(rp->r_fh, &gf_res->object);
2149 }
2150 }
2151 } else {
2152 nfs_rw_exit(&mi->mi_fh_lock);
2153 }
2154
2155 ASSERT(nfs4_consistent_type(vp));
2156
2157 /*
2158 * If the server wanted an OPEN_CONFIRM but that fails, just start
2159 * over. Presumably if there is a persistent error it will show up
2160 * when we resend the OPEN.
2161 */
2162 if (op_res->rflags & OPEN4_RESULT_CONFIRM) {
2163 bool_t retry_open = FALSE;
2164
2165 nfs4open_confirm(vp, &seqid, &op_res->stateid,
2166 cred_otw, is_recov, &retry_open,
2167 oop, FALSE, ep, NULL);
2168 if (ep->error || ep->stat) {
2169 nfs4args_copen_free(open_args);
2170 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2171 nfs4_end_open_seqid_sync(oop);
2172 open_owner_rele(oop);
2173 oop = NULL;
2174 goto top;
2175 }
2176 }
2177
2178 mutex_enter(&osp->os_sync_lock);
2179 osp->open_stateid = op_res->stateid;
2180 osp->os_delegation = 0;
2181 /*
2182 * Need to reset this bitfield for the possible case where we were
2183 * going to OTW CLOSE the file, got a non-recoverable error, and before
2184 * we could retry the CLOSE, OPENed the file again.
2185 */
2186 ASSERT(osp->os_open_owner->oo_seqid_inuse);
2187 osp->os_final_close = 0;
2188 osp->os_force_close = 0;
2189 if (claim == CLAIM_DELEGATE_CUR || claim == CLAIM_PREVIOUS)
2190 osp->os_dc_openacc = open_args->share_access;
2191 mutex_exit(&osp->os_sync_lock);
2192
2193 nfs4_end_open_seqid_sync(oop);
2194
2195 /* accept delegation, if any */
2196 nfs4_delegation_accept(rp, claim, op_res, garp, cred_otw);
2197
2198 nfs4args_copen_free(open_args);
2199
2200 nfs4_attr_cache(vp, garp, t, cr, TRUE, NULL);
2201
2202 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2203
2204 ASSERT(nfs4_consistent_type(vp));
2205
2206 open_owner_rele(oop);
2207 crfree(cr);
2208 crfree(cred_otw);
2209 return;
2210
2211 kill_file:
2212 nfs4_fail_recov(vp, failed_msg, ep->error, ep->stat);
2213 failed_reopen:
2214 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
2215 "nfs4_reopen: setting os_failed_reopen for osp %p, cr %p, rp %s",
2216 (void *)osp, (void *)cr, rnode4info(rp)));
2217 mutex_enter(&osp->os_sync_lock);
2218 osp->os_failed_reopen = 1;
2219 mutex_exit(&osp->os_sync_lock);
2220 bailout:
2221 if (oop != NULL) {
2222 nfs4_end_open_seqid_sync(oop);
2223 open_owner_rele(oop);
2224 }
2225 if (cr != NULL)
2226 crfree(cr);
2227 if (cred_otw != NULL)
2228 crfree(cred_otw);
2229 }
2230
2231 /* for . and .. OPENs */
2232 /* ARGSUSED */
2233 static int
nfs4_open_non_reg_file(vnode_t ** vpp,int flag,cred_t * cr)2234 nfs4_open_non_reg_file(vnode_t **vpp, int flag, cred_t *cr)
2235 {
2236 rnode4_t *rp;
2237 nfs4_ga_res_t gar;
2238
2239 ASSERT(nfs_zone() == VTOMI4(*vpp)->mi_zone);
2240
2241 /*
2242 * If close-to-open consistency checking is turned off or
2243 * if there is no cached data, we can avoid
2244 * the over the wire getattr. Otherwise, force a
2245 * call to the server to get fresh attributes and to
2246 * check caches. This is required for close-to-open
2247 * consistency.
2248 */
2249 rp = VTOR4(*vpp);
2250 if (VTOMI4(*vpp)->mi_flags & MI4_NOCTO ||
2251 (rp->r_dir == NULL && !nfs4_has_pages(*vpp)))
2252 return (0);
2253
2254 return (nfs4_getattr_otw(*vpp, &gar, cr, 0));
2255 }
2256
2257 /*
2258 * CLOSE a file
2259 */
2260 /* ARGSUSED */
2261 static int
nfs4_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)2262 nfs4_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
2263 caller_context_t *ct)
2264 {
2265 rnode4_t *rp;
2266 int error = 0;
2267 int r_error = 0;
2268 int n4error = 0;
2269 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2270
2271 /*
2272 * Remove client state for this (lockowner, file) pair.
2273 * Issue otw v4 call to have the server do the same.
2274 */
2275
2276 rp = VTOR4(vp);
2277
2278 /*
2279 * zone_enter(2) prevents processes from changing zones with NFS files
2280 * open; if we happen to get here from the wrong zone we can't do
2281 * anything over the wire.
2282 */
2283 if (VTOMI4(vp)->mi_zone != nfs_zone()) {
2284 /*
2285 * We could attempt to clean up locks, except we're sure
2286 * that the current process didn't acquire any locks on
2287 * the file: any attempt to lock a file belong to another zone
2288 * will fail, and one can't lock an NFS file and then change
2289 * zones, as that fails too.
2290 *
2291 * Returning an error here is the sane thing to do. A
2292 * subsequent call to VN_RELE() which translates to a
2293 * nfs4_inactive() will clean up state: if the zone of the
2294 * vnode's origin is still alive and kicking, the inactive
2295 * thread will handle the request (from the correct zone), and
2296 * everything (minus the OTW close call) should be OK. If the
2297 * zone is going away nfs4_async_inactive() will throw away
2298 * delegations, open streams and cached pages inline.
2299 */
2300 return (EIO);
2301 }
2302
2303 /*
2304 * If we are using local locking for this filesystem, then
2305 * release all of the SYSV style record locks. Otherwise,
2306 * we are doing network locking and we need to release all
2307 * of the network locks. All of the locks held by this
2308 * process on this file are released no matter what the
2309 * incoming reference count is.
2310 */
2311 if (VTOMI4(vp)->mi_flags & MI4_LLOCK) {
2312 cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2313 cleanshares(vp, ttoproc(curthread)->p_pid);
2314 } else
2315 e.error = nfs4_lockrelease(vp, flag, offset, cr);
2316
2317 if (e.error) {
2318 struct lm_sysid *lmsid;
2319 lmsid = nfs4_find_sysid(VTOMI4(vp));
2320 if (lmsid == NULL) {
2321 DTRACE_PROBE2(unknown__sysid, int, e.error,
2322 vnode_t *, vp);
2323 } else {
2324 cleanlocks(vp, ttoproc(curthread)->p_pid,
2325 (lm_sysidt(lmsid) | LM_SYSID_CLIENT));
2326
2327 lm_rel_sysid(lmsid);
2328 }
2329 return (e.error);
2330 }
2331
2332 if (count > 1)
2333 return (0);
2334
2335 /*
2336 * If the file has been `unlinked', then purge the
2337 * DNLC so that this vnode will get reycled quicker
2338 * and the .nfs* file on the server will get removed.
2339 */
2340 if (rp->r_unldvp != NULL)
2341 dnlc_purge_vp(vp);
2342
2343 /*
2344 * If the file was open for write and there are pages,
2345 * do a synchronous flush and commit of all of the
2346 * dirty and uncommitted pages.
2347 */
2348 ASSERT(!e.error);
2349 if ((flag & FWRITE) && nfs4_has_pages(vp))
2350 error = nfs4_putpage_commit(vp, 0, 0, cr);
2351
2352 mutex_enter(&rp->r_statelock);
2353 r_error = rp->r_error;
2354 rp->r_error = 0;
2355 mutex_exit(&rp->r_statelock);
2356
2357 /*
2358 * If this file type is one for which no explicit 'open' was
2359 * done, then bail now (ie. no need for protocol 'close'). If
2360 * there was an error w/the vm subsystem, return _that_ error,
2361 * otherwise, return any errors that may've been reported via
2362 * the rnode.
2363 */
2364 if (vp->v_type != VREG)
2365 return (error ? error : r_error);
2366
2367 /*
2368 * The sync putpage commit may have failed above, but since
2369 * we're working w/a regular file, we need to do the protocol
2370 * 'close' (nfs4close_one will figure out if an otw close is
2371 * needed or not). Report any errors _after_ doing the protocol
2372 * 'close'.
2373 */
2374 nfs4close_one(vp, NULL, cr, flag, NULL, &e, CLOSE_NORM, 0, 0, 0);
2375 n4error = e.error ? e.error : geterrno4(e.stat);
2376
2377 /*
2378 * Error reporting prio (Hi -> Lo)
2379 *
2380 * i) nfs4_putpage_commit (error)
2381 * ii) rnode's (r_error)
2382 * iii) nfs4close_one (n4error)
2383 */
2384 return (error ? error : (r_error ? r_error : n4error));
2385 }
2386
2387 /*
2388 * Initialize *lost_rqstp.
2389 */
2390
2391 static void
nfs4close_save_lost_rqst(int error,nfs4_lost_rqst_t * lost_rqstp,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,cred_t * cr,vnode_t * vp)2392 nfs4close_save_lost_rqst(int error, nfs4_lost_rqst_t *lost_rqstp,
2393 nfs4_open_owner_t *oop, nfs4_open_stream_t *osp, cred_t *cr,
2394 vnode_t *vp)
2395 {
2396 if (error != ETIMEDOUT && error != EINTR &&
2397 !NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
2398 lost_rqstp->lr_op = 0;
2399 return;
2400 }
2401
2402 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2403 "nfs4close_save_lost_rqst: error %d", error));
2404
2405 lost_rqstp->lr_op = OP_CLOSE;
2406 /*
2407 * The vp is held and rele'd via the recovery code.
2408 * See nfs4_save_lost_rqst.
2409 */
2410 lost_rqstp->lr_vp = vp;
2411 lost_rqstp->lr_dvp = NULL;
2412 lost_rqstp->lr_oop = oop;
2413 lost_rqstp->lr_osp = osp;
2414 ASSERT(osp != NULL);
2415 ASSERT(mutex_owned(&osp->os_sync_lock));
2416 osp->os_pending_close = 1;
2417 lost_rqstp->lr_lop = NULL;
2418 lost_rqstp->lr_cr = cr;
2419 lost_rqstp->lr_flk = NULL;
2420 lost_rqstp->lr_putfirst = FALSE;
2421 }
2422
2423 /*
2424 * Assumes you already have the open seqid sync grabbed as well as the
2425 * 'os_sync_lock'. Note: this will release the open seqid sync and
2426 * 'os_sync_lock' if client recovery starts. Calling functions have to
2427 * be prepared to handle this.
2428 *
2429 * 'recov' is returned as 1 if the CLOSE operation detected client recovery
2430 * was needed and was started, and that the calling function should retry
2431 * this function; otherwise it is returned as 0.
2432 *
2433 * Errors are returned via the nfs4_error_t parameter.
2434 */
2435 static void
nfs4close_otw(rnode4_t * rp,cred_t * cred_otw,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,int * recov,int * did_start_seqid_syncp,nfs4_close_type_t close_type,nfs4_error_t * ep,int * have_sync_lockp)2436 nfs4close_otw(rnode4_t *rp, cred_t *cred_otw, nfs4_open_owner_t *oop,
2437 nfs4_open_stream_t *osp, int *recov, int *did_start_seqid_syncp,
2438 nfs4_close_type_t close_type, nfs4_error_t *ep, int *have_sync_lockp)
2439 {
2440 COMPOUND4args_clnt args;
2441 COMPOUND4res_clnt res;
2442 CLOSE4args *close_args;
2443 nfs_resop4 *resop;
2444 nfs_argop4 argop[3];
2445 int doqueue = 1;
2446 mntinfo4_t *mi;
2447 seqid4 seqid;
2448 vnode_t *vp;
2449 bool_t needrecov = FALSE;
2450 nfs4_lost_rqst_t lost_rqst;
2451 hrtime_t t;
2452
2453 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
2454
2455 ASSERT(MUTEX_HELD(&osp->os_sync_lock));
2456
2457 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw"));
2458
2459 /* Only set this to 1 if recovery is started */
2460 *recov = 0;
2461
2462 /* do the OTW call to close the file */
2463
2464 if (close_type == CLOSE_RESEND)
2465 args.ctag = TAG_CLOSE_LOST;
2466 else if (close_type == CLOSE_AFTER_RESEND)
2467 args.ctag = TAG_CLOSE_UNDO;
2468 else
2469 args.ctag = TAG_CLOSE;
2470
2471 args.array_len = 3;
2472 args.array = argop;
2473
2474 vp = RTOV4(rp);
2475
2476 mi = VTOMI4(vp);
2477
2478 /* putfh target fh */
2479 argop[0].argop = OP_CPUTFH;
2480 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
2481
2482 argop[1].argop = OP_GETATTR;
2483 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
2484 argop[1].nfs_argop4_u.opgetattr.mi = mi;
2485
2486 argop[2].argop = OP_CLOSE;
2487 close_args = &argop[2].nfs_argop4_u.opclose;
2488
2489 seqid = nfs4_get_open_seqid(oop) + 1;
2490
2491 close_args->seqid = seqid;
2492 close_args->open_stateid = osp->open_stateid;
2493
2494 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
2495 "nfs4close_otw: %s call, rp %s", needrecov ? "recov" : "first",
2496 rnode4info(rp)));
2497
2498 t = gethrtime();
2499
2500 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, ep);
2501
2502 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
2503 nfs4_set_open_seqid(seqid, oop, args.ctag);
2504 }
2505
2506 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
2507 if (ep->error && !needrecov) {
2508 /*
2509 * if there was an error and no recovery is to be done
2510 * then then set up the file to flush its cache if
2511 * needed for the next caller.
2512 */
2513 mutex_enter(&rp->r_statelock);
2514 PURGE_ATTRCACHE4_LOCKED(rp);
2515 rp->r_flags &= ~R4WRITEMODIFIED;
2516 mutex_exit(&rp->r_statelock);
2517 return;
2518 }
2519
2520 if (needrecov) {
2521 bool_t abort;
2522 nfs4_bseqid_entry_t *bsep = NULL;
2523
2524 if (close_type != CLOSE_RESEND)
2525 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
2526 osp, cred_otw, vp);
2527
2528 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
2529 bsep = nfs4_create_bseqid_entry(oop, NULL, vp,
2530 0, args.ctag, close_args->seqid);
2531
2532 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2533 "nfs4close_otw: initiating recovery. error %d "
2534 "res.status %d", ep->error, res.status));
2535
2536 /*
2537 * Drop the 'os_sync_lock' here so we don't hit
2538 * a potential recursive mutex_enter via an
2539 * 'open_stream_hold()'.
2540 */
2541 mutex_exit(&osp->os_sync_lock);
2542 *have_sync_lockp = 0;
2543 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
2544 (close_type != CLOSE_RESEND &&
2545 lost_rqst.lr_op == OP_CLOSE) ? &lost_rqst : NULL,
2546 OP_CLOSE, bsep, NULL, NULL);
2547
2548 /* drop open seq sync, and let the calling function regrab it */
2549 nfs4_end_open_seqid_sync(oop);
2550 *did_start_seqid_syncp = 0;
2551
2552 if (bsep)
2553 kmem_free(bsep, sizeof (*bsep));
2554 /*
2555 * For signals, the caller wants to quit, so don't say to
2556 * retry. For forced unmount, if it's a user thread, it
2557 * wants to quit. If it's a recovery thread, the retry
2558 * will happen higher-up on the call stack. Either way,
2559 * don't say to retry.
2560 */
2561 if (abort == FALSE && ep->error != EINTR &&
2562 !NFS4_FRC_UNMT_ERR(ep->error, mi->mi_vfsp) &&
2563 close_type != CLOSE_RESEND &&
2564 close_type != CLOSE_AFTER_RESEND)
2565 *recov = 1;
2566 else
2567 *recov = 0;
2568
2569 if (!ep->error)
2570 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2571 return;
2572 }
2573
2574 if (res.status) {
2575 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2576 return;
2577 }
2578
2579 mutex_enter(&rp->r_statev4_lock);
2580 rp->created_v4 = 0;
2581 mutex_exit(&rp->r_statev4_lock);
2582
2583 resop = &res.array[2];
2584 osp->open_stateid = resop->nfs_resop4_u.opclose.open_stateid;
2585 osp->os_valid = 0;
2586
2587 /*
2588 * This removes the reference obtained at OPEN; ie, when the
2589 * open stream structure was created.
2590 *
2591 * We don't have to worry about calling 'open_stream_rele'
2592 * since we our currently holding a reference to the open
2593 * stream which means the count cannot go to 0 with this
2594 * decrement.
2595 */
2596 ASSERT(osp->os_ref_count >= 2);
2597 osp->os_ref_count--;
2598
2599 if (ep->error == 0) {
2600 mutex_exit(&osp->os_sync_lock);
2601 *have_sync_lockp = 0;
2602
2603 nfs4_attr_cache(vp,
2604 &res.array[1].nfs_resop4_u.opgetattr.ga_res,
2605 t, cred_otw, TRUE, NULL);
2606 }
2607
2608 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE, "nfs4close_otw:"
2609 " returning %d", ep->error));
2610
2611 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2612 }
2613
2614 /* ARGSUSED */
2615 static int
nfs4_read(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2616 nfs4_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2617 caller_context_t *ct)
2618 {
2619 rnode4_t *rp;
2620 u_offset_t off;
2621 offset_t diff;
2622 uint_t on;
2623 uint_t n;
2624 caddr_t base;
2625 uint_t flags;
2626 int error;
2627 mntinfo4_t *mi;
2628
2629 rp = VTOR4(vp);
2630
2631 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2632
2633 if (IS_SHADOW(vp, rp))
2634 vp = RTOV4(rp);
2635
2636 if (vp->v_type != VREG)
2637 return (EISDIR);
2638
2639 mi = VTOMI4(vp);
2640
2641 if (nfs_zone() != mi->mi_zone)
2642 return (EIO);
2643
2644 if (uiop->uio_resid == 0)
2645 return (0);
2646
2647 if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
2648 return (EINVAL);
2649
2650 mutex_enter(&rp->r_statelock);
2651 if (rp->r_flags & R4RECOVERRP)
2652 error = (rp->r_error ? rp->r_error : EIO);
2653 else
2654 error = 0;
2655 mutex_exit(&rp->r_statelock);
2656 if (error)
2657 return (error);
2658
2659 /*
2660 * Bypass VM if caching has been disabled (e.g., locking) or if
2661 * using client-side direct I/O and the file is not mmap'd and
2662 * there are no cached pages.
2663 */
2664 if ((vp->v_flag & VNOCACHE) ||
2665 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2666 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2667 size_t resid = 0;
2668
2669 return (nfs4read(vp, NULL, uiop->uio_loffset,
2670 uiop->uio_resid, &resid, cr, FALSE, uiop));
2671 }
2672
2673 error = 0;
2674
2675 do {
2676 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2677 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2678 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2679
2680 if (error = nfs4_validate_caches(vp, cr))
2681 break;
2682
2683 mutex_enter(&rp->r_statelock);
2684 while (rp->r_flags & R4INCACHEPURGE) {
2685 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2686 mutex_exit(&rp->r_statelock);
2687 return (EINTR);
2688 }
2689 }
2690 diff = rp->r_size - uiop->uio_loffset;
2691 mutex_exit(&rp->r_statelock);
2692 if (diff <= 0)
2693 break;
2694 if (diff < n)
2695 n = (uint_t)diff;
2696
2697 if (vpm_enable) {
2698 /*
2699 * Copy data.
2700 */
2701 error = vpm_data_copy(vp, off + on, n, uiop,
2702 1, NULL, 0, S_READ);
2703 } else {
2704 base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
2705 S_READ);
2706
2707 error = uiomove(base + on, n, UIO_READ, uiop);
2708 }
2709
2710 if (!error) {
2711 /*
2712 * If read a whole block or read to eof,
2713 * won't need this buffer again soon.
2714 */
2715 mutex_enter(&rp->r_statelock);
2716 if (n + on == MAXBSIZE ||
2717 uiop->uio_loffset == rp->r_size)
2718 flags = SM_DONTNEED;
2719 else
2720 flags = 0;
2721 mutex_exit(&rp->r_statelock);
2722 if (vpm_enable) {
2723 error = vpm_sync_pages(vp, off, n, flags);
2724 } else {
2725 error = segmap_release(segkmap, base, flags);
2726 }
2727 } else {
2728 if (vpm_enable) {
2729 (void) vpm_sync_pages(vp, off, n, 0);
2730 } else {
2731 (void) segmap_release(segkmap, base, 0);
2732 }
2733 }
2734 } while (!error && uiop->uio_resid > 0);
2735
2736 return (error);
2737 }
2738
2739 /* ARGSUSED */
2740 static int
nfs4_write(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)2741 nfs4_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
2742 caller_context_t *ct)
2743 {
2744 rlim64_t limit = uiop->uio_llimit;
2745 rnode4_t *rp;
2746 u_offset_t off;
2747 caddr_t base;
2748 uint_t flags;
2749 int remainder;
2750 size_t n;
2751 int on;
2752 int error;
2753 int resid;
2754 u_offset_t offset;
2755 mntinfo4_t *mi;
2756 uint_t bsize;
2757
2758 rp = VTOR4(vp);
2759
2760 if (IS_SHADOW(vp, rp))
2761 vp = RTOV4(rp);
2762
2763 if (vp->v_type != VREG)
2764 return (EISDIR);
2765
2766 mi = VTOMI4(vp);
2767
2768 if (nfs_zone() != mi->mi_zone)
2769 return (EIO);
2770
2771 if (uiop->uio_resid == 0)
2772 return (0);
2773
2774 mutex_enter(&rp->r_statelock);
2775 if (rp->r_flags & R4RECOVERRP)
2776 error = (rp->r_error ? rp->r_error : EIO);
2777 else
2778 error = 0;
2779 mutex_exit(&rp->r_statelock);
2780 if (error)
2781 return (error);
2782
2783 if (ioflag & FAPPEND) {
2784 struct vattr va;
2785
2786 /*
2787 * Must serialize if appending.
2788 */
2789 if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
2790 nfs_rw_exit(&rp->r_rwlock);
2791 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
2792 INTR4(vp)))
2793 return (EINTR);
2794 }
2795
2796 va.va_mask = AT_SIZE;
2797 error = nfs4getattr(vp, &va, cr);
2798 if (error)
2799 return (error);
2800 uiop->uio_loffset = va.va_size;
2801 }
2802
2803 offset = uiop->uio_loffset + uiop->uio_resid;
2804
2805 if (uiop->uio_loffset < (offset_t)0 || offset < 0)
2806 return (EINVAL);
2807
2808 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
2809 limit = MAXOFFSET_T;
2810
2811 /*
2812 * Check to make sure that the process will not exceed
2813 * its limit on file size. It is okay to write up to
2814 * the limit, but not beyond. Thus, the write which
2815 * reaches the limit will be short and the next write
2816 * will return an error.
2817 */
2818 remainder = 0;
2819 if (offset > uiop->uio_llimit) {
2820 remainder = offset - uiop->uio_llimit;
2821 uiop->uio_resid = uiop->uio_llimit - uiop->uio_loffset;
2822 if (uiop->uio_resid <= 0) {
2823 proc_t *p = ttoproc(curthread);
2824
2825 uiop->uio_resid += remainder;
2826 mutex_enter(&p->p_lock);
2827 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
2828 p->p_rctls, p, RCA_UNSAFE_SIGINFO);
2829 mutex_exit(&p->p_lock);
2830 return (EFBIG);
2831 }
2832 }
2833
2834 /* update the change attribute, if we have a write delegation */
2835
2836 mutex_enter(&rp->r_statev4_lock);
2837 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE)
2838 rp->r_deleg_change++;
2839
2840 mutex_exit(&rp->r_statev4_lock);
2841
2842 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp)))
2843 return (EINTR);
2844
2845 /*
2846 * Bypass VM if caching has been disabled (e.g., locking) or if
2847 * using client-side direct I/O and the file is not mmap'd and
2848 * there are no cached pages.
2849 */
2850 if ((vp->v_flag & VNOCACHE) ||
2851 (((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO)) &&
2852 rp->r_mapcnt == 0 && rp->r_inmap == 0 && !nfs4_has_pages(vp))) {
2853 size_t bufsize;
2854 int count;
2855 u_offset_t org_offset;
2856 stable_how4 stab_comm;
2857 nfs4_fwrite:
2858 if (rp->r_flags & R4STALE) {
2859 resid = uiop->uio_resid;
2860 offset = uiop->uio_loffset;
2861 error = rp->r_error;
2862 /*
2863 * A close may have cleared r_error, if so,
2864 * propagate ESTALE error return properly
2865 */
2866 if (error == 0)
2867 error = ESTALE;
2868 goto bottom;
2869 }
2870
2871 bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
2872 base = kmem_alloc(bufsize, KM_SLEEP);
2873 do {
2874 if (ioflag & FDSYNC)
2875 stab_comm = DATA_SYNC4;
2876 else
2877 stab_comm = FILE_SYNC4;
2878 resid = uiop->uio_resid;
2879 offset = uiop->uio_loffset;
2880 count = MIN(uiop->uio_resid, bufsize);
2881 org_offset = uiop->uio_loffset;
2882 error = uiomove(base, count, UIO_WRITE, uiop);
2883 if (!error) {
2884 error = nfs4write(vp, base, org_offset,
2885 count, cr, &stab_comm);
2886 if (!error) {
2887 mutex_enter(&rp->r_statelock);
2888 if (rp->r_size < uiop->uio_loffset)
2889 rp->r_size = uiop->uio_loffset;
2890 mutex_exit(&rp->r_statelock);
2891 }
2892 }
2893 } while (!error && uiop->uio_resid > 0);
2894 kmem_free(base, bufsize);
2895 goto bottom;
2896 }
2897
2898 bsize = vp->v_vfsp->vfs_bsize;
2899
2900 do {
2901 off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
2902 on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
2903 n = MIN(MAXBSIZE - on, uiop->uio_resid);
2904
2905 resid = uiop->uio_resid;
2906 offset = uiop->uio_loffset;
2907
2908 if (rp->r_flags & R4STALE) {
2909 error = rp->r_error;
2910 /*
2911 * A close may have cleared r_error, if so,
2912 * propagate ESTALE error return properly
2913 */
2914 if (error == 0)
2915 error = ESTALE;
2916 break;
2917 }
2918
2919 /*
2920 * Don't create dirty pages faster than they
2921 * can be cleaned so that the system doesn't
2922 * get imbalanced. If the async queue is
2923 * maxed out, then wait for it to drain before
2924 * creating more dirty pages. Also, wait for
2925 * any threads doing pagewalks in the vop_getattr
2926 * entry points so that they don't block for
2927 * long periods.
2928 */
2929 mutex_enter(&rp->r_statelock);
2930 while ((mi->mi_max_threads != 0 &&
2931 rp->r_awcount > 2 * mi->mi_max_threads) ||
2932 rp->r_gcount > 0) {
2933 if (INTR4(vp)) {
2934 klwp_t *lwp = ttolwp(curthread);
2935
2936 if (lwp != NULL)
2937 lwp->lwp_nostop++;
2938 if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
2939 mutex_exit(&rp->r_statelock);
2940 if (lwp != NULL)
2941 lwp->lwp_nostop--;
2942 error = EINTR;
2943 goto bottom;
2944 }
2945 if (lwp != NULL)
2946 lwp->lwp_nostop--;
2947 } else
2948 cv_wait(&rp->r_cv, &rp->r_statelock);
2949 }
2950 mutex_exit(&rp->r_statelock);
2951
2952 /*
2953 * Touch the page and fault it in if it is not in core
2954 * before segmap_getmapflt or vpm_data_copy can lock it.
2955 * This is to avoid the deadlock if the buffer is mapped
2956 * to the same file through mmap which we want to write.
2957 */
2958 uio_prefaultpages((long)n, uiop);
2959
2960 if (vpm_enable) {
2961 /*
2962 * It will use kpm mappings, so no need to
2963 * pass an address.
2964 */
2965 error = writerp4(rp, NULL, n, uiop, 0);
2966 } else {
2967 if (segmap_kpm) {
2968 int pon = uiop->uio_loffset & PAGEOFFSET;
2969 size_t pn = MIN(PAGESIZE - pon,
2970 uiop->uio_resid);
2971 int pagecreate;
2972
2973 mutex_enter(&rp->r_statelock);
2974 pagecreate = (pon == 0) && (pn == PAGESIZE ||
2975 uiop->uio_loffset + pn >= rp->r_size);
2976 mutex_exit(&rp->r_statelock);
2977
2978 base = segmap_getmapflt(segkmap, vp, off + on,
2979 pn, !pagecreate, S_WRITE);
2980
2981 error = writerp4(rp, base + pon, n, uiop,
2982 pagecreate);
2983
2984 } else {
2985 base = segmap_getmapflt(segkmap, vp, off + on,
2986 n, 0, S_READ);
2987 error = writerp4(rp, base + on, n, uiop, 0);
2988 }
2989 }
2990
2991 if (!error) {
2992 if (mi->mi_flags & MI4_NOAC)
2993 flags = SM_WRITE;
2994 else if ((uiop->uio_loffset % bsize) == 0 ||
2995 IS_SWAPVP(vp)) {
2996 /*
2997 * Have written a whole block.
2998 * Start an asynchronous write
2999 * and mark the buffer to
3000 * indicate that it won't be
3001 * needed again soon.
3002 */
3003 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
3004 } else
3005 flags = 0;
3006 if ((ioflag & (FSYNC|FDSYNC)) ||
3007 (rp->r_flags & R4OUTOFSPACE)) {
3008 flags &= ~SM_ASYNC;
3009 flags |= SM_WRITE;
3010 }
3011 if (vpm_enable) {
3012 error = vpm_sync_pages(vp, off, n, flags);
3013 } else {
3014 error = segmap_release(segkmap, base, flags);
3015 }
3016 } else {
3017 if (vpm_enable) {
3018 (void) vpm_sync_pages(vp, off, n, 0);
3019 } else {
3020 (void) segmap_release(segkmap, base, 0);
3021 }
3022 /*
3023 * In the event that we got an access error while
3024 * faulting in a page for a write-only file just
3025 * force a write.
3026 */
3027 if (error == EACCES)
3028 goto nfs4_fwrite;
3029 }
3030 } while (!error && uiop->uio_resid > 0);
3031
3032 bottom:
3033 if (error) {
3034 uiop->uio_resid = resid + remainder;
3035 uiop->uio_loffset = offset;
3036 } else {
3037 uiop->uio_resid += remainder;
3038
3039 mutex_enter(&rp->r_statev4_lock);
3040 if (rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
3041 gethrestime(&rp->r_attr.va_mtime);
3042 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3043 }
3044 mutex_exit(&rp->r_statev4_lock);
3045 }
3046
3047 nfs_rw_exit(&rp->r_lkserlock);
3048
3049 return (error);
3050 }
3051
3052 /*
3053 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
3054 */
3055 static int
nfs4_rdwrlbn(vnode_t * vp,page_t * pp,u_offset_t off,size_t len,int flags,cred_t * cr)3056 nfs4_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
3057 int flags, cred_t *cr)
3058 {
3059 struct buf *bp;
3060 int error;
3061 page_t *savepp;
3062 uchar_t fsdata;
3063 stable_how4 stab_comm;
3064
3065 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3066 bp = pageio_setup(pp, len, vp, flags);
3067 ASSERT(bp != NULL);
3068
3069 /*
3070 * pageio_setup should have set b_addr to 0. This
3071 * is correct since we want to do I/O on a page
3072 * boundary. bp_mapin will use this addr to calculate
3073 * an offset, and then set b_addr to the kernel virtual
3074 * address it allocated for us.
3075 */
3076 ASSERT(bp->b_un.b_addr == 0);
3077
3078 bp->b_edev = 0;
3079 bp->b_dev = 0;
3080 bp->b_lblkno = lbtodb(off);
3081 bp->b_file = vp;
3082 bp->b_offset = (offset_t)off;
3083 bp_mapin(bp);
3084
3085 if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
3086 freemem > desfree)
3087 stab_comm = UNSTABLE4;
3088 else
3089 stab_comm = FILE_SYNC4;
3090
3091 error = nfs4_bio(bp, &stab_comm, cr, FALSE);
3092
3093 bp_mapout(bp);
3094 pageio_done(bp);
3095
3096 if (stab_comm == UNSTABLE4)
3097 fsdata = C_DELAYCOMMIT;
3098 else
3099 fsdata = C_NOCOMMIT;
3100
3101 savepp = pp;
3102 do {
3103 pp->p_fsdata = fsdata;
3104 } while ((pp = pp->p_next) != savepp);
3105
3106 return (error);
3107 }
3108
3109 /*
3110 */
3111 static int
nfs4rdwr_check_osid(vnode_t * vp,nfs4_error_t * ep,cred_t * cr)3112 nfs4rdwr_check_osid(vnode_t *vp, nfs4_error_t *ep, cred_t *cr)
3113 {
3114 nfs4_open_owner_t *oop;
3115 nfs4_open_stream_t *osp;
3116 rnode4_t *rp = VTOR4(vp);
3117 mntinfo4_t *mi = VTOMI4(vp);
3118 int reopen_needed;
3119
3120 ASSERT(nfs_zone() == mi->mi_zone);
3121
3122
3123 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
3124 if (!oop)
3125 return (EIO);
3126
3127 /* returns with 'os_sync_lock' held */
3128 osp = find_open_stream(oop, rp);
3129 if (!osp) {
3130 open_owner_rele(oop);
3131 return (EIO);
3132 }
3133
3134 if (osp->os_failed_reopen) {
3135 mutex_exit(&osp->os_sync_lock);
3136 open_stream_rele(osp, rp);
3137 open_owner_rele(oop);
3138 return (EIO);
3139 }
3140
3141 /*
3142 * Determine whether a reopen is needed. If this
3143 * is a delegation open stream, then the os_delegation bit
3144 * should be set.
3145 */
3146
3147 reopen_needed = osp->os_delegation;
3148
3149 mutex_exit(&osp->os_sync_lock);
3150 open_owner_rele(oop);
3151
3152 if (reopen_needed) {
3153 nfs4_error_zinit(ep);
3154 nfs4_reopen(vp, osp, ep, CLAIM_NULL, FALSE, FALSE);
3155 mutex_enter(&osp->os_sync_lock);
3156 if (ep->error || ep->stat || osp->os_failed_reopen) {
3157 mutex_exit(&osp->os_sync_lock);
3158 open_stream_rele(osp, rp);
3159 return (EIO);
3160 }
3161 mutex_exit(&osp->os_sync_lock);
3162 }
3163 open_stream_rele(osp, rp);
3164
3165 return (0);
3166 }
3167
3168 /*
3169 * Write to file. Writes to remote server in largest size
3170 * chunks that the server can handle. Write is synchronous.
3171 */
3172 static int
nfs4write(vnode_t * vp,caddr_t base,u_offset_t offset,int count,cred_t * cr,stable_how4 * stab_comm)3173 nfs4write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
3174 stable_how4 *stab_comm)
3175 {
3176 mntinfo4_t *mi;
3177 COMPOUND4args_clnt args;
3178 COMPOUND4res_clnt res;
3179 WRITE4args *wargs;
3180 WRITE4res *wres;
3181 nfs_argop4 argop[2];
3182 nfs_resop4 *resop;
3183 int tsize;
3184 stable_how4 stable;
3185 rnode4_t *rp;
3186 int doqueue = 1;
3187 bool_t needrecov;
3188 nfs4_recov_state_t recov_state;
3189 nfs4_stateid_types_t sid_types;
3190 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3191 int recov;
3192
3193 rp = VTOR4(vp);
3194 mi = VTOMI4(vp);
3195
3196 ASSERT(nfs_zone() == mi->mi_zone);
3197
3198 stable = *stab_comm;
3199 *stab_comm = FILE_SYNC4;
3200
3201 needrecov = FALSE;
3202 recov_state.rs_flags = 0;
3203 recov_state.rs_num_retry_despite_err = 0;
3204 nfs4_init_stateid_types(&sid_types);
3205
3206 /* Is curthread the recovery thread? */
3207 mutex_enter(&mi->mi_lock);
3208 recov = (mi->mi_recovthread == curthread);
3209 mutex_exit(&mi->mi_lock);
3210
3211 recov_retry:
3212 args.ctag = TAG_WRITE;
3213 args.array_len = 2;
3214 args.array = argop;
3215
3216 if (!recov) {
3217 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3218 &recov_state, NULL);
3219 if (e.error)
3220 return (e.error);
3221 }
3222
3223 /* 0. putfh target fh */
3224 argop[0].argop = OP_CPUTFH;
3225 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3226
3227 /* 1. write */
3228 nfs4args_write(&argop[1], stable, rp, cr, &wargs, &sid_types);
3229
3230 do {
3231
3232 wargs->offset = (offset4)offset;
3233 wargs->data_val = base;
3234
3235 if (mi->mi_io_kstats) {
3236 mutex_enter(&mi->mi_lock);
3237 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3238 mutex_exit(&mi->mi_lock);
3239 }
3240
3241 if ((vp->v_flag & VNOCACHE) ||
3242 (rp->r_flags & R4DIRECTIO) ||
3243 (mi->mi_flags & MI4_DIRECTIO))
3244 tsize = MIN(mi->mi_stsize, count);
3245 else
3246 tsize = MIN(mi->mi_curwrite, count);
3247 wargs->data_len = (uint_t)tsize;
3248 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3249
3250 if (mi->mi_io_kstats) {
3251 mutex_enter(&mi->mi_lock);
3252 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3253 mutex_exit(&mi->mi_lock);
3254 }
3255
3256 if (!recov) {
3257 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3258 if (e.error && !needrecov) {
3259 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3260 &recov_state, needrecov);
3261 return (e.error);
3262 }
3263 } else {
3264 if (e.error)
3265 return (e.error);
3266 }
3267
3268 /*
3269 * Do handling of OLD_STATEID outside
3270 * of the normal recovery framework.
3271 *
3272 * If write receives a BAD stateid error while using a
3273 * delegation stateid, retry using the open stateid (if it
3274 * exists). If it doesn't have an open stateid, reopen the
3275 * file first, then retry.
3276 */
3277 if (!e.error && res.status == NFS4ERR_OLD_STATEID &&
3278 sid_types.cur_sid_type != SPEC_SID) {
3279 nfs4_save_stateid(&wargs->stateid, &sid_types);
3280 if (!recov)
3281 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3282 &recov_state, needrecov);
3283 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3284 goto recov_retry;
3285 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3286 sid_types.cur_sid_type == DEL_SID) {
3287 nfs4_save_stateid(&wargs->stateid, &sid_types);
3288 mutex_enter(&rp->r_statev4_lock);
3289 rp->r_deleg_return_pending = TRUE;
3290 mutex_exit(&rp->r_statev4_lock);
3291 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3292 if (!recov)
3293 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3294 &recov_state, needrecov);
3295 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3296 return (EIO);
3297 }
3298 if (!recov)
3299 nfs4_end_fop(mi, vp, NULL, OH_WRITE,
3300 &recov_state, needrecov);
3301 /* hold needed for nfs4delegreturn_thread */
3302 VN_HOLD(vp);
3303 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3304 NFS4_DR_DISCARD), FALSE);
3305 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3306 goto recov_retry;
3307 }
3308
3309 if (needrecov) {
3310 bool_t abort;
3311
3312 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3313 "nfs4write: client got error %d, res.status %d"
3314 ", so start recovery", e.error, res.status));
3315
3316 abort = nfs4_start_recovery(&e,
3317 VTOMI4(vp), vp, NULL, &wargs->stateid,
3318 NULL, OP_WRITE, NULL, NULL, NULL);
3319 if (!e.error) {
3320 e.error = geterrno4(res.status);
3321 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3322 }
3323 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3324 &recov_state, needrecov);
3325 if (abort == FALSE)
3326 goto recov_retry;
3327 return (e.error);
3328 }
3329
3330 if (res.status) {
3331 e.error = geterrno4(res.status);
3332 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3333 if (!recov)
3334 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3335 &recov_state, needrecov);
3336 return (e.error);
3337 }
3338
3339 resop = &res.array[1]; /* write res */
3340 wres = &resop->nfs_resop4_u.opwrite;
3341
3342 if ((int)wres->count > tsize) {
3343 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3344
3345 zcmn_err(getzoneid(), CE_WARN,
3346 "nfs4write: server wrote %u, requested was %u",
3347 (int)wres->count, tsize);
3348 if (!recov)
3349 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE,
3350 &recov_state, needrecov);
3351 return (EIO);
3352 }
3353 if (wres->committed == UNSTABLE4) {
3354 *stab_comm = UNSTABLE4;
3355 if (wargs->stable == DATA_SYNC4 ||
3356 wargs->stable == FILE_SYNC4) {
3357 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3358 zcmn_err(getzoneid(), CE_WARN,
3359 "nfs4write: server %s did not commit "
3360 "to stable storage",
3361 rp->r_server->sv_hostname);
3362 if (!recov)
3363 nfs4_end_fop(VTOMI4(vp), vp, NULL,
3364 OH_WRITE, &recov_state, needrecov);
3365 return (EIO);
3366 }
3367 }
3368
3369 tsize = (int)wres->count;
3370 count -= tsize;
3371 base += tsize;
3372 offset += tsize;
3373 if (mi->mi_io_kstats) {
3374 mutex_enter(&mi->mi_lock);
3375 KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
3376 KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
3377 tsize;
3378 mutex_exit(&mi->mi_lock);
3379 }
3380 lwp_stat_update(LWP_STAT_OUBLK, 1);
3381 mutex_enter(&rp->r_statelock);
3382 if (rp->r_flags & R4HAVEVERF) {
3383 if (rp->r_writeverf != wres->writeverf) {
3384 nfs4_set_mod(vp);
3385 rp->r_writeverf = wres->writeverf;
3386 }
3387 } else {
3388 rp->r_writeverf = wres->writeverf;
3389 rp->r_flags |= R4HAVEVERF;
3390 }
3391 PURGE_ATTRCACHE4_LOCKED(rp);
3392 rp->r_flags |= R4WRITEMODIFIED;
3393 gethrestime(&rp->r_attr.va_mtime);
3394 rp->r_attr.va_ctime = rp->r_attr.va_mtime;
3395 mutex_exit(&rp->r_statelock);
3396 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3397 } while (count);
3398
3399 if (!recov)
3400 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_WRITE, &recov_state,
3401 needrecov);
3402
3403 return (e.error);
3404 }
3405
3406 /*
3407 * Read from a file. Reads data in largest chunks our interface can handle.
3408 */
3409 static int
nfs4read(vnode_t * vp,caddr_t base,offset_t offset,int count,size_t * residp,cred_t * cr,bool_t async,struct uio * uiop)3410 nfs4read(vnode_t *vp, caddr_t base, offset_t offset, int count,
3411 size_t *residp, cred_t *cr, bool_t async, struct uio *uiop)
3412 {
3413 mntinfo4_t *mi;
3414 COMPOUND4args_clnt args;
3415 COMPOUND4res_clnt res;
3416 READ4args *rargs;
3417 nfs_argop4 argop[2];
3418 int tsize;
3419 int doqueue;
3420 rnode4_t *rp;
3421 int data_len;
3422 bool_t is_eof;
3423 bool_t needrecov = FALSE;
3424 nfs4_recov_state_t recov_state;
3425 nfs4_stateid_types_t sid_types;
3426 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3427
3428 rp = VTOR4(vp);
3429 mi = VTOMI4(vp);
3430 doqueue = 1;
3431
3432 ASSERT(nfs_zone() == mi->mi_zone);
3433
3434 args.ctag = async ? TAG_READAHEAD : TAG_READ;
3435
3436 args.array_len = 2;
3437 args.array = argop;
3438
3439 nfs4_init_stateid_types(&sid_types);
3440
3441 recov_state.rs_flags = 0;
3442 recov_state.rs_num_retry_despite_err = 0;
3443
3444 recov_retry:
3445 e.error = nfs4_start_fop(mi, vp, NULL, OH_READ,
3446 &recov_state, NULL);
3447 if (e.error)
3448 return (e.error);
3449
3450 /* putfh target fh */
3451 argop[0].argop = OP_CPUTFH;
3452 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3453
3454 /* read */
3455 argop[1].argop = OP_READ;
3456 rargs = &argop[1].nfs_argop4_u.opread;
3457 rargs->stateid = nfs4_get_stateid(cr, rp, curproc->p_pidp->pid_id, mi,
3458 OP_READ, &sid_types, async);
3459
3460 do {
3461 if (mi->mi_io_kstats) {
3462 mutex_enter(&mi->mi_lock);
3463 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3464 mutex_exit(&mi->mi_lock);
3465 }
3466
3467 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
3468 "nfs4read: %s call, rp %s",
3469 needrecov ? "recov" : "first",
3470 rnode4info(rp)));
3471
3472 if ((vp->v_flag & VNOCACHE) ||
3473 (rp->r_flags & R4DIRECTIO) ||
3474 (mi->mi_flags & MI4_DIRECTIO))
3475 tsize = MIN(mi->mi_tsize, count);
3476 else
3477 tsize = MIN(mi->mi_curread, count);
3478
3479 rargs->offset = (offset4)offset;
3480 rargs->count = (count4)tsize;
3481 rargs->res_data_val_alt = NULL;
3482 rargs->res_mblk = NULL;
3483 rargs->res_uiop = NULL;
3484 rargs->res_maxsize = 0;
3485 rargs->wlist = NULL;
3486
3487 if (uiop)
3488 rargs->res_uiop = uiop;
3489 else
3490 rargs->res_data_val_alt = base;
3491 rargs->res_maxsize = tsize;
3492
3493 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
3494 #ifdef DEBUG
3495 if (nfs4read_error_inject) {
3496 res.status = nfs4read_error_inject;
3497 nfs4read_error_inject = 0;
3498 }
3499 #endif
3500
3501 if (mi->mi_io_kstats) {
3502 mutex_enter(&mi->mi_lock);
3503 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3504 mutex_exit(&mi->mi_lock);
3505 }
3506
3507 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
3508 if (e.error != 0 && !needrecov) {
3509 nfs4_end_fop(mi, vp, NULL, OH_READ,
3510 &recov_state, needrecov);
3511 return (e.error);
3512 }
3513
3514 /*
3515 * Do proper retry for OLD and BAD stateid errors outside
3516 * of the normal recovery framework. There are two differences
3517 * between async and sync reads. The first is that we allow
3518 * retry on BAD_STATEID for async reads, but not sync reads.
3519 * The second is that we mark the file dead for a failed
3520 * attempt with a special stateid for sync reads, but just
3521 * return EIO for async reads.
3522 *
3523 * If a sync read receives a BAD stateid error while using a
3524 * delegation stateid, retry using the open stateid (if it
3525 * exists). If it doesn't have an open stateid, reopen the
3526 * file first, then retry.
3527 */
3528 if (e.error == 0 && (res.status == NFS4ERR_OLD_STATEID ||
3529 res.status == NFS4ERR_BAD_STATEID) && async) {
3530 nfs4_end_fop(mi, vp, NULL, OH_READ,
3531 &recov_state, needrecov);
3532 if (sid_types.cur_sid_type == SPEC_SID) {
3533 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3534 return (EIO);
3535 }
3536 nfs4_save_stateid(&rargs->stateid, &sid_types);
3537 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3538 goto recov_retry;
3539 } else if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3540 !async && sid_types.cur_sid_type != SPEC_SID) {
3541 nfs4_save_stateid(&rargs->stateid, &sid_types);
3542 nfs4_end_fop(mi, vp, NULL, OH_READ,
3543 &recov_state, needrecov);
3544 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3545 goto recov_retry;
3546 } else if (e.error == 0 && res.status == NFS4ERR_BAD_STATEID &&
3547 sid_types.cur_sid_type == DEL_SID) {
3548 nfs4_save_stateid(&rargs->stateid, &sid_types);
3549 mutex_enter(&rp->r_statev4_lock);
3550 rp->r_deleg_return_pending = TRUE;
3551 mutex_exit(&rp->r_statev4_lock);
3552 if (nfs4rdwr_check_osid(vp, &e, cr)) {
3553 nfs4_end_fop(mi, vp, NULL, OH_READ,
3554 &recov_state, needrecov);
3555 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3556 return (EIO);
3557 }
3558 nfs4_end_fop(mi, vp, NULL, OH_READ,
3559 &recov_state, needrecov);
3560 /* hold needed for nfs4delegreturn_thread */
3561 VN_HOLD(vp);
3562 nfs4delegreturn_async(rp, (NFS4_DR_PUSH|NFS4_DR_REOPEN|
3563 NFS4_DR_DISCARD), FALSE);
3564 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3565 goto recov_retry;
3566 }
3567 if (needrecov) {
3568 bool_t abort;
3569
3570 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3571 "nfs4read: initiating recovery\n"));
3572 abort = nfs4_start_recovery(&e,
3573 mi, vp, NULL, &rargs->stateid,
3574 NULL, OP_READ, NULL, NULL, NULL);
3575 nfs4_end_fop(mi, vp, NULL, OH_READ,
3576 &recov_state, needrecov);
3577 /*
3578 * Do not retry if we got OLD_STATEID using a special
3579 * stateid. This avoids looping with a broken server.
3580 */
3581 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
3582 sid_types.cur_sid_type == SPEC_SID)
3583 abort = TRUE;
3584
3585 if (abort == FALSE) {
3586 /*
3587 * Need to retry all possible stateids in
3588 * case the recovery error wasn't stateid
3589 * related or the stateids have become
3590 * stale (server reboot).
3591 */
3592 nfs4_init_stateid_types(&sid_types);
3593 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3594 goto recov_retry;
3595 }
3596
3597 if (!e.error) {
3598 e.error = geterrno4(res.status);
3599 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3600 }
3601 return (e.error);
3602 }
3603
3604 if (res.status) {
3605 e.error = geterrno4(res.status);
3606 nfs4_end_fop(mi, vp, NULL, OH_READ,
3607 &recov_state, needrecov);
3608 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3609 return (e.error);
3610 }
3611
3612 data_len = res.array[1].nfs_resop4_u.opread.data_len;
3613 count -= data_len;
3614 if (base)
3615 base += data_len;
3616 offset += data_len;
3617 if (mi->mi_io_kstats) {
3618 mutex_enter(&mi->mi_lock);
3619 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3620 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += data_len;
3621 mutex_exit(&mi->mi_lock);
3622 }
3623 lwp_stat_update(LWP_STAT_INBLK, 1);
3624 is_eof = res.array[1].nfs_resop4_u.opread.eof;
3625 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3626
3627 } while (count && !is_eof);
3628
3629 *residp = count;
3630
3631 nfs4_end_fop(mi, vp, NULL, OH_READ, &recov_state, needrecov);
3632
3633 return (e.error);
3634 }
3635
3636 /* ARGSUSED */
3637 static int
nfs4_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)3638 nfs4_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
3639 caller_context_t *ct)
3640 {
3641 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3642 return (EIO);
3643 switch (cmd) {
3644 case _FIODIRECTIO:
3645 return (nfs4_directio(vp, (int)arg, cr));
3646 default:
3647 return (ENOTTY);
3648 }
3649 }
3650
3651 /* ARGSUSED */
3652 int
nfs4_getattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)3653 nfs4_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3654 caller_context_t *ct)
3655 {
3656 int error;
3657 rnode4_t *rp = VTOR4(vp);
3658
3659 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3660 return (EIO);
3661 /*
3662 * If it has been specified that the return value will
3663 * just be used as a hint, and we are only being asked
3664 * for size, fsid or rdevid, then return the client's
3665 * notion of these values without checking to make sure
3666 * that the attribute cache is up to date.
3667 * The whole point is to avoid an over the wire GETATTR
3668 * call.
3669 */
3670 if (flags & ATTR_HINT) {
3671 if (!(vap->va_mask & ~(AT_SIZE | AT_FSID | AT_RDEV))) {
3672 mutex_enter(&rp->r_statelock);
3673 if (vap->va_mask & AT_SIZE)
3674 vap->va_size = rp->r_size;
3675 if (vap->va_mask & AT_FSID)
3676 vap->va_fsid = rp->r_attr.va_fsid;
3677 if (vap->va_mask & AT_RDEV)
3678 vap->va_rdev = rp->r_attr.va_rdev;
3679 mutex_exit(&rp->r_statelock);
3680 return (0);
3681 }
3682 }
3683
3684 /*
3685 * Only need to flush pages if asking for the mtime
3686 * and if there any dirty pages or any outstanding
3687 * asynchronous (write) requests for this file.
3688 */
3689 if (vap->va_mask & AT_MTIME) {
3690 rp = VTOR4(vp);
3691 if (nfs4_has_pages(vp)) {
3692 mutex_enter(&rp->r_statev4_lock);
3693 if (rp->r_deleg_type != OPEN_DELEGATE_WRITE) {
3694 mutex_exit(&rp->r_statev4_lock);
3695 if (rp->r_flags & R4DIRTY ||
3696 rp->r_awcount > 0) {
3697 mutex_enter(&rp->r_statelock);
3698 rp->r_gcount++;
3699 mutex_exit(&rp->r_statelock);
3700 error =
3701 nfs4_putpage(vp, (u_offset_t)0,
3702 0, 0, cr, NULL);
3703 mutex_enter(&rp->r_statelock);
3704 if (error && (error == ENOSPC ||
3705 error == EDQUOT)) {
3706 if (!rp->r_error)
3707 rp->r_error = error;
3708 }
3709 if (--rp->r_gcount == 0)
3710 cv_broadcast(&rp->r_cv);
3711 mutex_exit(&rp->r_statelock);
3712 }
3713 } else {
3714 mutex_exit(&rp->r_statev4_lock);
3715 }
3716 }
3717 }
3718 return (nfs4getattr(vp, vap, cr));
3719 }
3720
3721 int
nfs4_compare_modes(mode_t from_server,mode_t on_client)3722 nfs4_compare_modes(mode_t from_server, mode_t on_client)
3723 {
3724 /*
3725 * If these are the only two bits cleared
3726 * on the server then return 0 (OK) else
3727 * return 1 (BAD).
3728 */
3729 on_client &= ~(S_ISUID|S_ISGID);
3730 if (on_client == from_server)
3731 return (0);
3732 else
3733 return (1);
3734 }
3735
3736 /*ARGSUSED4*/
3737 static int
nfs4_setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)3738 nfs4_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3739 caller_context_t *ct)
3740 {
3741 int error;
3742
3743 if (vap->va_mask & AT_NOSET)
3744 return (EINVAL);
3745
3746 if (nfs_zone() != VTOMI4(vp)->mi_zone)
3747 return (EIO);
3748
3749 /*
3750 * Don't call secpolicy_vnode_setattr, the client cannot
3751 * use its cached attributes to make security decisions
3752 * as the server may be faking mode bits or mapping uid/gid.
3753 * Always just let the server to the checking.
3754 * If we provide the ability to remove basic priviledges
3755 * to setattr (e.g. basic without chmod) then we will
3756 * need to add a check here before calling the server.
3757 */
3758 error = nfs4setattr(vp, vap, flags, cr, NULL);
3759
3760 if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
3761 vnevent_truncate(vp, ct);
3762
3763 return (error);
3764 }
3765
3766 /*
3767 * To replace the "guarded" version 3 setattr, we use two types of compound
3768 * setattr requests:
3769 * 1. The "normal" setattr, used when the size of the file isn't being
3770 * changed - { Putfh <fh>; Setattr; Getattr }/
3771 * 2. If the size is changed, precede Setattr with: Getattr; Verify
3772 * with only ctime as the argument. If the server ctime differs from
3773 * what is cached on the client, the verify will fail, but we would
3774 * already have the ctime from the preceding getattr, so just set it
3775 * and retry. Thus the compound here is - { Putfh <fh>; Getattr; Verify;
3776 * Setattr; Getattr }.
3777 *
3778 * The vsecattr_t * input parameter will be non-NULL if ACLs are being set in
3779 * this setattr and NULL if they are not.
3780 */
3781 static int
nfs4setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,vsecattr_t * vsap)3782 nfs4setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
3783 vsecattr_t *vsap)
3784 {
3785 COMPOUND4args_clnt args;
3786 COMPOUND4res_clnt res, *resp = NULL;
3787 nfs4_ga_res_t *garp = NULL;
3788 int numops = 3; /* { Putfh; Setattr; Getattr } */
3789 nfs_argop4 argop[5];
3790 int verify_argop = -1;
3791 int setattr_argop = 1;
3792 nfs_resop4 *resop;
3793 vattr_t va;
3794 rnode4_t *rp;
3795 int doqueue = 1;
3796 uint_t mask = vap->va_mask;
3797 mode_t omode;
3798 vsecattr_t *vsp;
3799 timestruc_t ctime;
3800 bool_t needrecov = FALSE;
3801 nfs4_recov_state_t recov_state;
3802 nfs4_stateid_types_t sid_types;
3803 stateid4 stateid;
3804 hrtime_t t;
3805 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
3806 servinfo4_t *svp;
3807 bitmap4 supp_attrs;
3808
3809 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
3810 rp = VTOR4(vp);
3811 nfs4_init_stateid_types(&sid_types);
3812
3813 /*
3814 * Only need to flush pages if there are any pages and
3815 * if the file is marked as dirty in some fashion. The
3816 * file must be flushed so that we can accurately
3817 * determine the size of the file and the cached data
3818 * after the SETATTR returns. A file is considered to
3819 * be dirty if it is either marked with R4DIRTY, has
3820 * outstanding i/o's active, or is mmap'd. In this
3821 * last case, we can't tell whether there are dirty
3822 * pages, so we flush just to be sure.
3823 */
3824 if (nfs4_has_pages(vp) &&
3825 ((rp->r_flags & R4DIRTY) ||
3826 rp->r_count > 0 ||
3827 rp->r_mapcnt > 0)) {
3828 ASSERT(vp->v_type != VCHR);
3829 e.error = nfs4_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
3830 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
3831 mutex_enter(&rp->r_statelock);
3832 if (!rp->r_error)
3833 rp->r_error = e.error;
3834 mutex_exit(&rp->r_statelock);
3835 }
3836 }
3837
3838 if (mask & AT_SIZE) {
3839 /*
3840 * Verification setattr compound for non-deleg AT_SIZE:
3841 * { Putfh; Getattr; Verify; Setattr; Getattr }
3842 * Set ctime local here (outside the do_again label)
3843 * so that subsequent retries (after failed VERIFY)
3844 * will use ctime from GETATTR results (from failed
3845 * verify compound) as VERIFY arg.
3846 * If file has delegation, then VERIFY(time_metadata)
3847 * is of little added value, so don't bother.
3848 */
3849 mutex_enter(&rp->r_statev4_lock);
3850 if (rp->r_deleg_type == OPEN_DELEGATE_NONE ||
3851 rp->r_deleg_return_pending) {
3852 numops = 5;
3853 ctime = rp->r_attr.va_ctime;
3854 }
3855 mutex_exit(&rp->r_statev4_lock);
3856 }
3857
3858 recov_state.rs_flags = 0;
3859 recov_state.rs_num_retry_despite_err = 0;
3860
3861 args.ctag = TAG_SETATTR;
3862 do_again:
3863 recov_retry:
3864 setattr_argop = numops - 2;
3865
3866 args.array = argop;
3867 args.array_len = numops;
3868
3869 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
3870 if (e.error)
3871 return (e.error);
3872
3873
3874 /* putfh target fh */
3875 argop[0].argop = OP_CPUTFH;
3876 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
3877
3878 if (numops == 5) {
3879 /*
3880 * We only care about the ctime, but need to get mtime
3881 * and size for proper cache update.
3882 */
3883 /* getattr */
3884 argop[1].argop = OP_GETATTR;
3885 argop[1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3886 argop[1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3887
3888 /* verify - set later in loop */
3889 verify_argop = 2;
3890 }
3891
3892 /* setattr */
3893 svp = rp->r_server;
3894 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3895 supp_attrs = svp->sv_supp_attrs;
3896 nfs_rw_exit(&svp->sv_lock);
3897
3898 nfs4args_setattr(&argop[setattr_argop], vap, vsap, flags, rp, cr,
3899 supp_attrs, &e.error, &sid_types);
3900 stateid = argop[setattr_argop].nfs_argop4_u.opsetattr.stateid;
3901 if (e.error) {
3902 /* req time field(s) overflow - return immediately */
3903 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
3904 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
3905 opsetattr.obj_attributes);
3906 return (e.error);
3907 }
3908 omode = rp->r_attr.va_mode;
3909
3910 /* getattr */
3911 argop[numops-1].argop = OP_GETATTR;
3912 argop[numops-1].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
3913 /*
3914 * If we are setting the ACL (indicated only by vsap != NULL), request
3915 * the ACL in this getattr. The ACL returned from this getattr will be
3916 * used in updating the ACL cache.
3917 */
3918 if (vsap != NULL)
3919 argop[numops-1].nfs_argop4_u.opgetattr.attr_request |=
3920 FATTR4_ACL_MASK;
3921 argop[numops-1].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
3922
3923 /*
3924 * setattr iterates if the object size is set and the cached ctime
3925 * does not match the file ctime. In that case, verify the ctime first.
3926 */
3927
3928 do {
3929 if (verify_argop != -1) {
3930 /*
3931 * Verify that the ctime match before doing setattr.
3932 */
3933 va.va_mask = AT_CTIME;
3934 va.va_ctime = ctime;
3935 svp = rp->r_server;
3936 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3937 supp_attrs = svp->sv_supp_attrs;
3938 nfs_rw_exit(&svp->sv_lock);
3939 e.error = nfs4args_verify(&argop[verify_argop], &va,
3940 OP_VERIFY, supp_attrs);
3941 if (e.error) {
3942 /* req time field(s) overflow - return */
3943 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3944 needrecov);
3945 break;
3946 }
3947 }
3948
3949 doqueue = 1;
3950
3951 t = gethrtime();
3952
3953 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
3954
3955 /*
3956 * Purge the access cache and ACL cache if changing either the
3957 * owner of the file, the group owner, or the mode. These may
3958 * change the access permissions of the file, so purge old
3959 * information and start over again.
3960 */
3961 if (mask & (AT_UID | AT_GID | AT_MODE)) {
3962 (void) nfs4_access_purge_rp(rp);
3963 if (rp->r_secattr != NULL) {
3964 mutex_enter(&rp->r_statelock);
3965 vsp = rp->r_secattr;
3966 rp->r_secattr = NULL;
3967 mutex_exit(&rp->r_statelock);
3968 if (vsp != NULL)
3969 nfs4_acl_free_cache(vsp);
3970 }
3971 }
3972
3973 /*
3974 * If res.array_len == numops, then everything succeeded,
3975 * except for possibly the final getattr. If only the
3976 * last getattr failed, give up, and don't try recovery.
3977 */
3978 if (res.array_len == numops) {
3979 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3980 needrecov);
3981 if (! e.error)
3982 resp = &res;
3983 break;
3984 }
3985
3986 /*
3987 * if either rpc call failed or completely succeeded - done
3988 */
3989 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
3990 if (e.error) {
3991 PURGE_ATTRCACHE4(vp);
3992 if (!needrecov) {
3993 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
3994 needrecov);
3995 break;
3996 }
3997 }
3998
3999 /*
4000 * Do proper retry for OLD_STATEID outside of the normal
4001 * recovery framework.
4002 */
4003 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4004 sid_types.cur_sid_type != SPEC_SID &&
4005 sid_types.cur_sid_type != NO_SID) {
4006 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4007 needrecov);
4008 nfs4_save_stateid(&stateid, &sid_types);
4009 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4010 opsetattr.obj_attributes);
4011 if (verify_argop != -1) {
4012 nfs4args_verify_free(&argop[verify_argop]);
4013 verify_argop = -1;
4014 }
4015 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4016 goto recov_retry;
4017 }
4018
4019 if (needrecov) {
4020 bool_t abort;
4021
4022 abort = nfs4_start_recovery(&e,
4023 VTOMI4(vp), vp, NULL, NULL, NULL,
4024 OP_SETATTR, NULL, NULL, NULL);
4025 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4026 needrecov);
4027 /*
4028 * Do not retry if we failed with OLD_STATEID using
4029 * a special stateid. This is done to avoid looping
4030 * with a broken server.
4031 */
4032 if (e.error == 0 && res.status == NFS4ERR_OLD_STATEID &&
4033 (sid_types.cur_sid_type == SPEC_SID ||
4034 sid_types.cur_sid_type == NO_SID))
4035 abort = TRUE;
4036 if (!e.error) {
4037 if (res.status == NFS4ERR_BADOWNER)
4038 nfs4_log_badowner(VTOMI4(vp),
4039 OP_SETATTR);
4040
4041 e.error = geterrno4(res.status);
4042 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4043 }
4044 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4045 opsetattr.obj_attributes);
4046 if (verify_argop != -1) {
4047 nfs4args_verify_free(&argop[verify_argop]);
4048 verify_argop = -1;
4049 }
4050 if (abort == FALSE) {
4051 /*
4052 * Need to retry all possible stateids in
4053 * case the recovery error wasn't stateid
4054 * related or the stateids have become
4055 * stale (server reboot).
4056 */
4057 nfs4_init_stateid_types(&sid_types);
4058 goto recov_retry;
4059 }
4060 return (e.error);
4061 }
4062
4063 /*
4064 * Need to call nfs4_end_op before nfs4getattr to
4065 * avoid potential nfs4_start_op deadlock. See RFE
4066 * 4777612. Calls to nfs4_invalidate_pages() and
4067 * nfs4_purge_stale_fh() might also generate over the
4068 * wire calls which my cause nfs4_start_op() deadlock.
4069 */
4070 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4071
4072 /*
4073 * Check to update lease.
4074 */
4075 resp = &res;
4076 if (res.status == NFS4_OK) {
4077 break;
4078 }
4079
4080 /*
4081 * Check if verify failed to see if try again
4082 */
4083 if ((verify_argop == -1) || (res.array_len != 3)) {
4084 /*
4085 * can't continue...
4086 */
4087 if (res.status == NFS4ERR_BADOWNER)
4088 nfs4_log_badowner(VTOMI4(vp), OP_SETATTR);
4089
4090 e.error = geterrno4(res.status);
4091 } else {
4092 /*
4093 * When the verify request fails, the client ctime is
4094 * not in sync with the server. This is the same as
4095 * the version 3 "not synchronized" error, and we
4096 * handle it in a similar manner (XXX do we need to???).
4097 * Use the ctime returned in the first getattr for
4098 * the input to the next verify.
4099 * If we couldn't get the attributes, then we give up
4100 * because we can't complete the operation as required.
4101 */
4102 garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
4103 }
4104 if (e.error) {
4105 PURGE_ATTRCACHE4(vp);
4106 nfs4_purge_stale_fh(e.error, vp, cr);
4107 } else {
4108 /*
4109 * retry with a new verify value
4110 */
4111 ctime = garp->n4g_va.va_ctime;
4112 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4113 resp = NULL;
4114 }
4115 if (!e.error) {
4116 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4117 opsetattr.obj_attributes);
4118 if (verify_argop != -1) {
4119 nfs4args_verify_free(&argop[verify_argop]);
4120 verify_argop = -1;
4121 }
4122 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4123 goto do_again;
4124 }
4125 } while (!e.error);
4126
4127 if (e.error) {
4128 /*
4129 * If we are here, rfs4call has an irrecoverable error - return
4130 */
4131 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4132 opsetattr.obj_attributes);
4133 if (verify_argop != -1) {
4134 nfs4args_verify_free(&argop[verify_argop]);
4135 verify_argop = -1;
4136 }
4137 if (resp)
4138 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4139 return (e.error);
4140 }
4141
4142
4143
4144 /*
4145 * If changing the size of the file, invalidate
4146 * any local cached data which is no longer part
4147 * of the file. We also possibly invalidate the
4148 * last page in the file. We could use
4149 * pvn_vpzero(), but this would mark the page as
4150 * modified and require it to be written back to
4151 * the server for no particularly good reason.
4152 * This way, if we access it, then we bring it
4153 * back in. A read should be cheaper than a
4154 * write.
4155 */
4156 if (mask & AT_SIZE) {
4157 nfs4_invalidate_pages(vp, (vap->va_size & PAGEMASK), cr);
4158 }
4159
4160 /* either no error or one of the postop getattr failed */
4161
4162 /*
4163 * XXX Perform a simplified version of wcc checking. Instead of
4164 * have another getattr to get pre-op, just purge cache if
4165 * any of the ops prior to and including the getattr failed.
4166 * If the getattr succeeded then update the attrcache accordingly.
4167 */
4168
4169 garp = NULL;
4170 if (res.status == NFS4_OK) {
4171 /*
4172 * Last getattr
4173 */
4174 resop = &res.array[numops - 1];
4175 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4176 }
4177 /*
4178 * In certain cases, nfs4_update_attrcache() will purge the attrcache,
4179 * rather than filling it. See the function itself for details.
4180 */
4181 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4182 if (garp != NULL) {
4183 if (garp->n4g_resbmap & FATTR4_ACL_MASK) {
4184 nfs4_acl_fill_cache(rp, &garp->n4g_vsa);
4185 vs_ace4_destroy(&garp->n4g_vsa);
4186 } else {
4187 if (vsap != NULL) {
4188 /*
4189 * The ACL was supposed to be set and to be
4190 * returned in the last getattr of this
4191 * compound, but for some reason the getattr
4192 * result doesn't contain the ACL. In this
4193 * case, purge the ACL cache.
4194 */
4195 if (rp->r_secattr != NULL) {
4196 mutex_enter(&rp->r_statelock);
4197 vsp = rp->r_secattr;
4198 rp->r_secattr = NULL;
4199 mutex_exit(&rp->r_statelock);
4200 if (vsp != NULL)
4201 nfs4_acl_free_cache(vsp);
4202 }
4203 }
4204 }
4205 }
4206
4207 if (res.status == NFS4_OK && (mask & AT_SIZE)) {
4208 /*
4209 * Set the size, rather than relying on getting it updated
4210 * via a GETATTR. With delegations the client tries to
4211 * suppress GETATTR calls.
4212 */
4213 mutex_enter(&rp->r_statelock);
4214 rp->r_size = vap->va_size;
4215 mutex_exit(&rp->r_statelock);
4216 }
4217
4218 /*
4219 * Can free up request args and res
4220 */
4221 nfs4_fattr4_free(&argop[setattr_argop].nfs_argop4_u.
4222 opsetattr.obj_attributes);
4223 if (verify_argop != -1) {
4224 nfs4args_verify_free(&argop[verify_argop]);
4225 verify_argop = -1;
4226 }
4227 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4228
4229 /*
4230 * Some servers will change the mode to clear the setuid
4231 * and setgid bits when changing the uid or gid. The
4232 * client needs to compensate appropriately.
4233 */
4234 if (mask & (AT_UID | AT_GID)) {
4235 int terror, do_setattr;
4236
4237 do_setattr = 0;
4238 va.va_mask = AT_MODE;
4239 terror = nfs4getattr(vp, &va, cr);
4240 if (!terror &&
4241 (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
4242 (!(mask & AT_MODE) && va.va_mode != omode))) {
4243 va.va_mask = AT_MODE;
4244 if (mask & AT_MODE) {
4245 /*
4246 * We asked the mode to be changed and what
4247 * we just got from the server in getattr is
4248 * not what we wanted it to be, so set it now.
4249 */
4250 va.va_mode = vap->va_mode;
4251 do_setattr = 1;
4252 } else {
4253 /*
4254 * We did not ask the mode to be changed,
4255 * Check to see that the server just cleared
4256 * I_SUID and I_GUID from it. If not then
4257 * set mode to omode with UID/GID cleared.
4258 */
4259 if (nfs4_compare_modes(va.va_mode, omode)) {
4260 omode &= ~(S_ISUID|S_ISGID);
4261 va.va_mode = omode;
4262 do_setattr = 1;
4263 }
4264 }
4265
4266 if (do_setattr)
4267 (void) nfs4setattr(vp, &va, 0, cr, NULL);
4268 }
4269 }
4270
4271 return (e.error);
4272 }
4273
4274 /* ARGSUSED */
4275 static int
nfs4_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)4276 nfs4_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
4277 {
4278 COMPOUND4args_clnt args;
4279 COMPOUND4res_clnt res;
4280 int doqueue;
4281 uint32_t acc, resacc, argacc;
4282 rnode4_t *rp;
4283 cred_t *cred, *ncr, *ncrfree = NULL;
4284 nfs4_access_type_t cacc;
4285 int num_ops;
4286 nfs_argop4 argop[3];
4287 nfs_resop4 *resop;
4288 bool_t needrecov = FALSE, do_getattr;
4289 nfs4_recov_state_t recov_state;
4290 int rpc_error;
4291 hrtime_t t;
4292 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4293 mntinfo4_t *mi = VTOMI4(vp);
4294
4295 if (nfs_zone() != mi->mi_zone)
4296 return (EIO);
4297
4298 acc = 0;
4299 if (mode & VREAD)
4300 acc |= ACCESS4_READ;
4301 if (mode & VWRITE) {
4302 if ((vp->v_vfsp->vfs_flag & VFS_RDONLY) && !ISVDEV(vp->v_type))
4303 return (EROFS);
4304 if (vp->v_type == VDIR)
4305 acc |= ACCESS4_DELETE;
4306 acc |= ACCESS4_MODIFY | ACCESS4_EXTEND;
4307 }
4308 if (mode & VEXEC) {
4309 if (vp->v_type == VDIR)
4310 acc |= ACCESS4_LOOKUP;
4311 else
4312 acc |= ACCESS4_EXECUTE;
4313 }
4314
4315 if (VTOR4(vp)->r_acache != NULL) {
4316 e.error = nfs4_validate_caches(vp, cr);
4317 if (e.error)
4318 return (e.error);
4319 }
4320
4321 rp = VTOR4(vp);
4322 if (vp->v_type == VDIR)
4323 argacc = ACCESS4_READ | ACCESS4_DELETE | ACCESS4_MODIFY |
4324 ACCESS4_EXTEND | ACCESS4_LOOKUP;
4325 else
4326 argacc = ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND |
4327 ACCESS4_EXECUTE;
4328 recov_state.rs_flags = 0;
4329 recov_state.rs_num_retry_despite_err = 0;
4330
4331 cred = cr;
4332 /*
4333 * ncr and ncrfree both initially
4334 * point to the memory area returned
4335 * by crnetadjust();
4336 * ncrfree not NULL when exiting means
4337 * that we need to release it
4338 */
4339 ncr = crnetadjust(cred);
4340 ncrfree = ncr;
4341
4342 tryagain:
4343 cacc = nfs4_access_check(rp, acc, cred);
4344 if (cacc == NFS4_ACCESS_ALLOWED) {
4345 if (ncrfree != NULL)
4346 crfree(ncrfree);
4347 return (0);
4348 }
4349 if (cacc == NFS4_ACCESS_DENIED) {
4350 /*
4351 * If the cred can be adjusted, try again
4352 * with the new cred.
4353 */
4354 if (ncr != NULL) {
4355 cred = ncr;
4356 ncr = NULL;
4357 goto tryagain;
4358 }
4359 if (ncrfree != NULL)
4360 crfree(ncrfree);
4361 return (EACCES);
4362 }
4363
4364 recov_retry:
4365 /*
4366 * Don't take with r_statev4_lock here. r_deleg_type could
4367 * change as soon as lock is released. Since it is an int,
4368 * there is no atomicity issue.
4369 */
4370 do_getattr = (rp->r_deleg_type == OPEN_DELEGATE_NONE);
4371 num_ops = do_getattr ? 3 : 2;
4372
4373 args.ctag = TAG_ACCESS;
4374
4375 args.array_len = num_ops;
4376 args.array = argop;
4377
4378 if (e.error = nfs4_start_fop(mi, vp, NULL, OH_ACCESS,
4379 &recov_state, NULL)) {
4380 if (ncrfree != NULL)
4381 crfree(ncrfree);
4382 return (e.error);
4383 }
4384
4385 /* putfh target fh */
4386 argop[0].argop = OP_CPUTFH;
4387 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4388
4389 /* access */
4390 argop[1].argop = OP_ACCESS;
4391 argop[1].nfs_argop4_u.opaccess.access = argacc;
4392
4393 /* getattr */
4394 if (do_getattr) {
4395 argop[2].argop = OP_GETATTR;
4396 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4397 argop[2].nfs_argop4_u.opgetattr.mi = mi;
4398 }
4399
4400 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4401 "nfs4_access: %s call, rp %s", needrecov ? "recov" : "first",
4402 rnode4info(VTOR4(vp))));
4403
4404 doqueue = 1;
4405 t = gethrtime();
4406 rfs4call(VTOMI4(vp), &args, &res, cred, &doqueue, 0, &e);
4407 rpc_error = e.error;
4408
4409 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4410 if (needrecov) {
4411 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4412 "nfs4_access: initiating recovery\n"));
4413
4414 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4415 NULL, OP_ACCESS, NULL, NULL, NULL) == FALSE) {
4416 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_ACCESS,
4417 &recov_state, needrecov);
4418 if (!e.error)
4419 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4420 goto recov_retry;
4421 }
4422 }
4423 nfs4_end_fop(mi, vp, NULL, OH_ACCESS, &recov_state, needrecov);
4424
4425 if (e.error)
4426 goto out;
4427
4428 if (res.status) {
4429 e.error = geterrno4(res.status);
4430 /*
4431 * This might generate over the wire calls throught
4432 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4433 * here to avoid a deadlock.
4434 */
4435 nfs4_purge_stale_fh(e.error, vp, cr);
4436 goto out;
4437 }
4438 resop = &res.array[1]; /* access res */
4439
4440 resacc = resop->nfs_resop4_u.opaccess.access;
4441
4442 if (do_getattr) {
4443 resop++; /* getattr res */
4444 nfs4_attr_cache(vp, &resop->nfs_resop4_u.opgetattr.ga_res,
4445 t, cr, FALSE, NULL);
4446 }
4447
4448 if (!e.error) {
4449 nfs4_access_cache(rp, argacc, resacc, cred);
4450 /*
4451 * we just cached results with cred; if cred is the
4452 * adjusted credentials from crnetadjust, we do not want
4453 * to release them before exiting: hence setting ncrfree
4454 * to NULL
4455 */
4456 if (cred != cr)
4457 ncrfree = NULL;
4458 /* XXX check the supported bits too? */
4459 if ((acc & resacc) != acc) {
4460 /*
4461 * The following code implements the semantic
4462 * that a setuid root program has *at least* the
4463 * permissions of the user that is running the
4464 * program. See rfs3call() for more portions
4465 * of the implementation of this functionality.
4466 */
4467 /* XXX-LP */
4468 if (ncr != NULL) {
4469 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4470 cred = ncr;
4471 ncr = NULL;
4472 goto tryagain;
4473 }
4474 e.error = EACCES;
4475 }
4476 }
4477
4478 out:
4479 if (!rpc_error)
4480 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4481
4482 if (ncrfree != NULL)
4483 crfree(ncrfree);
4484
4485 return (e.error);
4486 }
4487
4488 /* ARGSUSED */
4489 static int
nfs4_readlink(vnode_t * vp,struct uio * uiop,cred_t * cr,caller_context_t * ct)4490 nfs4_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
4491 {
4492 COMPOUND4args_clnt args;
4493 COMPOUND4res_clnt res;
4494 int doqueue;
4495 rnode4_t *rp;
4496 nfs_argop4 argop[3];
4497 nfs_resop4 *resop;
4498 READLINK4res *lr_res;
4499 nfs4_ga_res_t *garp;
4500 uint_t len;
4501 char *linkdata;
4502 bool_t needrecov = FALSE;
4503 nfs4_recov_state_t recov_state;
4504 hrtime_t t;
4505 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4506
4507 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4508 return (EIO);
4509 /*
4510 * Can't readlink anything other than a symbolic link.
4511 */
4512 if (vp->v_type != VLNK)
4513 return (EINVAL);
4514
4515 rp = VTOR4(vp);
4516 if (nfs4_do_symlink_cache && rp->r_symlink.contents != NULL) {
4517 e.error = nfs4_validate_caches(vp, cr);
4518 if (e.error)
4519 return (e.error);
4520 mutex_enter(&rp->r_statelock);
4521 if (rp->r_symlink.contents != NULL) {
4522 e.error = uiomove(rp->r_symlink.contents,
4523 rp->r_symlink.len, UIO_READ, uiop);
4524 mutex_exit(&rp->r_statelock);
4525 return (e.error);
4526 }
4527 mutex_exit(&rp->r_statelock);
4528 }
4529 recov_state.rs_flags = 0;
4530 recov_state.rs_num_retry_despite_err = 0;
4531
4532 recov_retry:
4533 args.array_len = 3;
4534 args.array = argop;
4535 args.ctag = TAG_READLINK;
4536
4537 e.error = nfs4_start_op(VTOMI4(vp), vp, NULL, &recov_state);
4538 if (e.error) {
4539 return (e.error);
4540 }
4541
4542 /* 0. putfh symlink fh */
4543 argop[0].argop = OP_CPUTFH;
4544 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
4545
4546 /* 1. readlink */
4547 argop[1].argop = OP_READLINK;
4548
4549 /* 2. getattr */
4550 argop[2].argop = OP_GETATTR;
4551 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
4552 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(vp);
4553
4554 doqueue = 1;
4555
4556 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
4557 "nfs4_readlink: %s call, rp %s", needrecov ? "recov" : "first",
4558 rnode4info(VTOR4(vp))));
4559
4560 t = gethrtime();
4561
4562 rfs4call(VTOMI4(vp), &args, &res, cr, &doqueue, 0, &e);
4563
4564 needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
4565 if (needrecov) {
4566 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
4567 "nfs4_readlink: initiating recovery\n"));
4568
4569 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
4570 NULL, OP_READLINK, NULL, NULL, NULL) == FALSE) {
4571 if (!e.error)
4572 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4573
4574 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state,
4575 needrecov);
4576 goto recov_retry;
4577 }
4578 }
4579
4580 nfs4_end_op(VTOMI4(vp), vp, NULL, &recov_state, needrecov);
4581
4582 if (e.error)
4583 return (e.error);
4584
4585 /*
4586 * There is an path in the code below which calls
4587 * nfs4_purge_stale_fh(), which may generate otw calls through
4588 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
4589 * here to avoid nfs4_start_op() deadlock.
4590 */
4591
4592 if (res.status && (res.array_len < args.array_len)) {
4593 /*
4594 * either Putfh or Link failed
4595 */
4596 e.error = geterrno4(res.status);
4597 nfs4_purge_stale_fh(e.error, vp, cr);
4598 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4599 return (e.error);
4600 }
4601
4602 resop = &res.array[1]; /* readlink res */
4603 lr_res = &resop->nfs_resop4_u.opreadlink;
4604
4605 /*
4606 * treat symlink names as data
4607 */
4608 linkdata = utf8_to_str((utf8string *)&lr_res->link, &len, NULL);
4609 if (linkdata != NULL) {
4610 int uio_len = len - 1;
4611 /* len includes null byte, which we won't uiomove */
4612 e.error = uiomove(linkdata, uio_len, UIO_READ, uiop);
4613 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
4614 mutex_enter(&rp->r_statelock);
4615 if (rp->r_symlink.contents == NULL) {
4616 rp->r_symlink.contents = linkdata;
4617 rp->r_symlink.len = uio_len;
4618 rp->r_symlink.size = len;
4619 mutex_exit(&rp->r_statelock);
4620 } else {
4621 mutex_exit(&rp->r_statelock);
4622 kmem_free(linkdata, len);
4623 }
4624 } else {
4625 kmem_free(linkdata, len);
4626 }
4627 }
4628 if (res.status == NFS4_OK) {
4629 resop++; /* getattr res */
4630 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
4631 }
4632 e.error = nfs4_update_attrcache(res.status, garp, t, vp, cr);
4633
4634 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4635
4636 /*
4637 * The over the wire error for attempting to readlink something
4638 * other than a symbolic link is ENXIO. However, we need to
4639 * return EINVAL instead of ENXIO, so we map it here.
4640 */
4641 return (e.error == ENXIO ? EINVAL : e.error);
4642 }
4643
4644 /*
4645 * Flush local dirty pages to stable storage on the server.
4646 *
4647 * If FNODSYNC is specified, then there is nothing to do because
4648 * metadata changes are not cached on the client before being
4649 * sent to the server.
4650 */
4651 /* ARGSUSED */
4652 static int
nfs4_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)4653 nfs4_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
4654 {
4655 int error;
4656
4657 if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
4658 return (0);
4659 if (nfs_zone() != VTOMI4(vp)->mi_zone)
4660 return (EIO);
4661 error = nfs4_putpage_commit(vp, (offset_t)0, 0, cr);
4662 if (!error)
4663 error = VTOR4(vp)->r_error;
4664 return (error);
4665 }
4666
4667 /*
4668 * Weirdness: if the file was removed or the target of a rename
4669 * operation while it was open, it got renamed instead. Here we
4670 * remove the renamed file.
4671 */
4672 /* ARGSUSED */
4673 void
nfs4_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)4674 nfs4_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4675 {
4676 rnode4_t *rp;
4677
4678 ASSERT(vp != DNLC_NO_VNODE);
4679
4680 rp = VTOR4(vp);
4681
4682 if (IS_SHADOW(vp, rp)) {
4683 sv_inactive(vp);
4684 return;
4685 }
4686
4687 /*
4688 * If this is coming from the wrong zone, we let someone in the right
4689 * zone take care of it asynchronously. We can get here due to
4690 * VN_RELE() being called from pageout() or fsflush(). This call may
4691 * potentially turn into an expensive no-op if, for instance, v_count
4692 * gets incremented in the meantime, but it's still correct.
4693 */
4694 if (nfs_zone() != VTOMI4(vp)->mi_zone) {
4695 nfs4_async_inactive(vp, cr);
4696 return;
4697 }
4698
4699 /*
4700 * Some of the cleanup steps might require over-the-wire
4701 * operations. Since VOP_INACTIVE can get called as a result of
4702 * other over-the-wire operations (e.g., an attribute cache update
4703 * can lead to a DNLC purge), doing those steps now would lead to a
4704 * nested call to the recovery framework, which can deadlock. So
4705 * do any over-the-wire cleanups asynchronously, in a separate
4706 * thread.
4707 */
4708
4709 mutex_enter(&rp->r_os_lock);
4710 mutex_enter(&rp->r_statelock);
4711 mutex_enter(&rp->r_statev4_lock);
4712
4713 if (vp->v_type == VREG && list_head(&rp->r_open_streams) != NULL) {
4714 mutex_exit(&rp->r_statev4_lock);
4715 mutex_exit(&rp->r_statelock);
4716 mutex_exit(&rp->r_os_lock);
4717 nfs4_async_inactive(vp, cr);
4718 return;
4719 }
4720
4721 if (rp->r_deleg_type == OPEN_DELEGATE_READ ||
4722 rp->r_deleg_type == OPEN_DELEGATE_WRITE) {
4723 mutex_exit(&rp->r_statev4_lock);
4724 mutex_exit(&rp->r_statelock);
4725 mutex_exit(&rp->r_os_lock);
4726 nfs4_async_inactive(vp, cr);
4727 return;
4728 }
4729
4730 if (rp->r_unldvp != NULL) {
4731 mutex_exit(&rp->r_statev4_lock);
4732 mutex_exit(&rp->r_statelock);
4733 mutex_exit(&rp->r_os_lock);
4734 nfs4_async_inactive(vp, cr);
4735 return;
4736 }
4737 mutex_exit(&rp->r_statev4_lock);
4738 mutex_exit(&rp->r_statelock);
4739 mutex_exit(&rp->r_os_lock);
4740
4741 rp4_addfree(rp, cr);
4742 }
4743
4744 /*
4745 * nfs4_inactive_otw - nfs4_inactive, plus over-the-wire calls to free up
4746 * various bits of state. The caller must not refer to vp after this call.
4747 */
4748
4749 void
nfs4_inactive_otw(vnode_t * vp,cred_t * cr)4750 nfs4_inactive_otw(vnode_t *vp, cred_t *cr)
4751 {
4752 rnode4_t *rp = VTOR4(vp);
4753 nfs4_recov_state_t recov_state;
4754 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
4755 vnode_t *unldvp;
4756 char *unlname;
4757 cred_t *unlcred;
4758 COMPOUND4args_clnt args;
4759 COMPOUND4res_clnt res, *resp;
4760 nfs_argop4 argop[2];
4761 int doqueue;
4762 #ifdef DEBUG
4763 char *name;
4764 #endif
4765
4766 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
4767 ASSERT(!IS_SHADOW(vp, rp));
4768
4769 #ifdef DEBUG
4770 name = fn_name(VTOSV(vp)->sv_name);
4771 NFS4_DEBUG(nfs4_client_inactive_debug, (CE_NOTE, "nfs4_inactive_otw: "
4772 "release vnode %s", name));
4773 kmem_free(name, MAXNAMELEN);
4774 #endif
4775
4776 if (vp->v_type == VREG) {
4777 bool_t recov_failed = FALSE;
4778
4779 e.error = nfs4close_all(vp, cr);
4780 if (e.error) {
4781 /* Check to see if recovery failed */
4782 mutex_enter(&(VTOMI4(vp)->mi_lock));
4783 if (VTOMI4(vp)->mi_flags & MI4_RECOV_FAIL)
4784 recov_failed = TRUE;
4785 mutex_exit(&(VTOMI4(vp)->mi_lock));
4786 if (!recov_failed) {
4787 mutex_enter(&rp->r_statelock);
4788 if (rp->r_flags & R4RECOVERR)
4789 recov_failed = TRUE;
4790 mutex_exit(&rp->r_statelock);
4791 }
4792 if (recov_failed) {
4793 NFS4_DEBUG(nfs4_client_recov_debug,
4794 (CE_NOTE, "nfs4_inactive_otw: "
4795 "close failed (recovery failure)"));
4796 }
4797 }
4798 }
4799
4800 redo:
4801 if (rp->r_unldvp == NULL) {
4802 rp4_addfree(rp, cr);
4803 return;
4804 }
4805
4806 /*
4807 * Save the vnode pointer for the directory where the
4808 * unlinked-open file got renamed, then set it to NULL
4809 * to prevent another thread from getting here before
4810 * we're done with the remove. While we have the
4811 * statelock, make local copies of the pertinent rnode
4812 * fields. If we weren't to do this in an atomic way, the
4813 * the unl* fields could become inconsistent with respect
4814 * to each other due to a race condition between this
4815 * code and nfs_remove(). See bug report 1034328.
4816 */
4817 mutex_enter(&rp->r_statelock);
4818 if (rp->r_unldvp == NULL) {
4819 mutex_exit(&rp->r_statelock);
4820 rp4_addfree(rp, cr);
4821 return;
4822 }
4823
4824 unldvp = rp->r_unldvp;
4825 rp->r_unldvp = NULL;
4826 unlname = rp->r_unlname;
4827 rp->r_unlname = NULL;
4828 unlcred = rp->r_unlcred;
4829 rp->r_unlcred = NULL;
4830 mutex_exit(&rp->r_statelock);
4831
4832 /*
4833 * If there are any dirty pages left, then flush
4834 * them. This is unfortunate because they just
4835 * may get thrown away during the remove operation,
4836 * but we have to do this for correctness.
4837 */
4838 if (nfs4_has_pages(vp) &&
4839 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
4840 ASSERT(vp->v_type != VCHR);
4841 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, NULL);
4842 if (e.error) {
4843 mutex_enter(&rp->r_statelock);
4844 if (!rp->r_error)
4845 rp->r_error = e.error;
4846 mutex_exit(&rp->r_statelock);
4847 }
4848 }
4849
4850 recov_state.rs_flags = 0;
4851 recov_state.rs_num_retry_despite_err = 0;
4852 recov_retry_remove:
4853 /*
4854 * Do the remove operation on the renamed file
4855 */
4856 args.ctag = TAG_INACTIVE;
4857
4858 /*
4859 * Remove ops: putfh dir; remove
4860 */
4861 args.array_len = 2;
4862 args.array = argop;
4863
4864 e.error = nfs4_start_op(VTOMI4(unldvp), unldvp, NULL, &recov_state);
4865 if (e.error) {
4866 kmem_free(unlname, MAXNAMELEN);
4867 crfree(unlcred);
4868 VN_RELE(unldvp);
4869 /*
4870 * Try again; this time around r_unldvp will be NULL, so we'll
4871 * just call rp4_addfree() and return.
4872 */
4873 goto redo;
4874 }
4875
4876 /* putfh directory */
4877 argop[0].argop = OP_CPUTFH;
4878 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(unldvp)->r_fh;
4879
4880 /* remove */
4881 argop[1].argop = OP_CREMOVE;
4882 argop[1].nfs_argop4_u.opcremove.ctarget = unlname;
4883
4884 doqueue = 1;
4885 resp = &res;
4886
4887 #if 0 /* notyet */
4888 /*
4889 * Can't do this yet. We may be being called from
4890 * dnlc_purge_XXX while that routine is holding a
4891 * mutex lock to the nc_rele list. The calls to
4892 * nfs3_cache_wcc_data may result in calls to
4893 * dnlc_purge_XXX. This will result in a deadlock.
4894 */
4895 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4896 if (e.error) {
4897 PURGE_ATTRCACHE4(unldvp);
4898 resp = NULL;
4899 } else if (res.status) {
4900 e.error = geterrno4(res.status);
4901 PURGE_ATTRCACHE4(unldvp);
4902 /*
4903 * This code is inactive right now
4904 * but if made active there should
4905 * be a nfs4_end_op() call before
4906 * nfs4_purge_stale_fh to avoid start_op()
4907 * deadlock. See BugId: 4948726
4908 */
4909 nfs4_purge_stale_fh(error, unldvp, cr);
4910 } else {
4911 nfs_resop4 *resop;
4912 REMOVE4res *rm_res;
4913
4914 resop = &res.array[1];
4915 rm_res = &resop->nfs_resop4_u.opremove;
4916 /*
4917 * Update directory cache attribute,
4918 * readdir and dnlc caches.
4919 */
4920 nfs4_update_dircaches(&rm_res->cinfo, unldvp, NULL, NULL, NULL);
4921 }
4922 #else
4923 rfs4call(VTOMI4(unldvp), &args, &res, unlcred, &doqueue, 0, &e);
4924
4925 PURGE_ATTRCACHE4(unldvp);
4926 #endif
4927
4928 if (nfs4_needs_recovery(&e, FALSE, unldvp->v_vfsp)) {
4929 if (nfs4_start_recovery(&e, VTOMI4(unldvp), unldvp, NULL,
4930 NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
4931 if (!e.error)
4932 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
4933 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL,
4934 &recov_state, TRUE);
4935 goto recov_retry_remove;
4936 }
4937 }
4938 nfs4_end_op(VTOMI4(unldvp), unldvp, NULL, &recov_state, FALSE);
4939
4940 /*
4941 * Release stuff held for the remove
4942 */
4943 VN_RELE(unldvp);
4944 if (!e.error && resp)
4945 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
4946
4947 kmem_free(unlname, MAXNAMELEN);
4948 crfree(unlcred);
4949 goto redo;
4950 }
4951
4952 /*
4953 * Remote file system operations having to do with directory manipulation.
4954 */
4955 /* ARGSUSED3 */
4956 int
nfs4_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)4957 nfs4_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
4958 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
4959 int *direntflags, pathname_t *realpnp)
4960 {
4961 int error;
4962 vnode_t *vp, *avp = NULL;
4963 rnode4_t *drp;
4964
4965 *vpp = NULL;
4966 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
4967 return (EPERM);
4968 /*
4969 * if LOOKUP_XATTR, must replace dvp (object) with
4970 * object's attrdir before continuing with lookup
4971 */
4972 if (flags & LOOKUP_XATTR) {
4973 error = nfs4lookup_xattr(dvp, nm, &avp, flags, cr);
4974 if (error)
4975 return (error);
4976
4977 dvp = avp;
4978
4979 /*
4980 * If lookup is for "", just return dvp now. The attrdir
4981 * has already been activated (from nfs4lookup_xattr), and
4982 * the caller will RELE the original dvp -- not
4983 * the attrdir. So, set vpp and return.
4984 * Currently, when the LOOKUP_XATTR flag is
4985 * passed to VOP_LOOKUP, the name is always empty, and
4986 * shortcircuiting here avoids 3 unneeded lock/unlock
4987 * pairs.
4988 *
4989 * If a non-empty name was provided, then it is the
4990 * attribute name, and it will be looked up below.
4991 */
4992 if (*nm == '\0') {
4993 *vpp = dvp;
4994 return (0);
4995 }
4996
4997 /*
4998 * The vfs layer never sends a name when asking for the
4999 * attrdir, so we should never get here (unless of course
5000 * name is passed at some time in future -- at which time
5001 * we'll blow up here).
5002 */
5003 ASSERT(0);
5004 }
5005
5006 drp = VTOR4(dvp);
5007 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5008 return (EINTR);
5009
5010 error = nfs4lookup(dvp, nm, vpp, cr, 0);
5011 nfs_rw_exit(&drp->r_rwlock);
5012
5013 /*
5014 * If vnode is a device, create special vnode.
5015 */
5016 if (!error && ISVDEV((*vpp)->v_type)) {
5017 vp = *vpp;
5018 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
5019 VN_RELE(vp);
5020 }
5021
5022 return (error);
5023 }
5024
5025 /* ARGSUSED */
5026 static int
nfs4lookup_xattr(vnode_t * dvp,char * nm,vnode_t ** vpp,int flags,cred_t * cr)5027 nfs4lookup_xattr(vnode_t *dvp, char *nm, vnode_t **vpp, int flags, cred_t *cr)
5028 {
5029 int error;
5030 rnode4_t *drp;
5031 int cflag = ((flags & CREATE_XATTR_DIR) != 0);
5032 mntinfo4_t *mi;
5033
5034 mi = VTOMI4(dvp);
5035 if (!(mi->mi_vfsp->vfs_flag & VFS_XATTR) &&
5036 !vfs_has_feature(mi->mi_vfsp, VFSFT_SYSATTR_VIEWS))
5037 return (EINVAL);
5038
5039 drp = VTOR4(dvp);
5040 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp)))
5041 return (EINTR);
5042
5043 mutex_enter(&drp->r_statelock);
5044 /*
5045 * If the server doesn't support xattrs just return EINVAL
5046 */
5047 if (drp->r_xattr_dir == NFS4_XATTR_DIR_NOTSUPP) {
5048 mutex_exit(&drp->r_statelock);
5049 nfs_rw_exit(&drp->r_rwlock);
5050 return (EINVAL);
5051 }
5052
5053 /*
5054 * If there is a cached xattr directory entry,
5055 * use it as long as the attributes are valid. If the
5056 * attributes are not valid, take the simple approach and
5057 * free the cached value and re-fetch a new value.
5058 *
5059 * We don't negative entry cache for now, if we did we
5060 * would need to check if the file has changed on every
5061 * lookup. But xattrs don't exist very often and failing
5062 * an openattr is not much more expensive than and NVERIFY or GETATTR
5063 * so do an openattr over the wire for now.
5064 */
5065 if (drp->r_xattr_dir != NULL) {
5066 if (ATTRCACHE4_VALID(dvp)) {
5067 VN_HOLD(drp->r_xattr_dir);
5068 *vpp = drp->r_xattr_dir;
5069 mutex_exit(&drp->r_statelock);
5070 nfs_rw_exit(&drp->r_rwlock);
5071 return (0);
5072 }
5073 VN_RELE(drp->r_xattr_dir);
5074 drp->r_xattr_dir = NULL;
5075 }
5076 mutex_exit(&drp->r_statelock);
5077
5078 error = nfs4openattr(dvp, vpp, cflag, cr);
5079
5080 nfs_rw_exit(&drp->r_rwlock);
5081
5082 return (error);
5083 }
5084
5085 static int
nfs4lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr,int skipdnlc)5086 nfs4lookup(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr, int skipdnlc)
5087 {
5088 int error;
5089 rnode4_t *drp;
5090
5091 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5092
5093 /*
5094 * If lookup is for "", just return dvp. Don't need
5095 * to send it over the wire, look it up in the dnlc,
5096 * or perform any access checks.
5097 */
5098 if (*nm == '\0') {
5099 VN_HOLD(dvp);
5100 *vpp = dvp;
5101 return (0);
5102 }
5103
5104 /*
5105 * Can't do lookups in non-directories.
5106 */
5107 if (dvp->v_type != VDIR)
5108 return (ENOTDIR);
5109
5110 /*
5111 * If lookup is for ".", just return dvp. Don't need
5112 * to send it over the wire or look it up in the dnlc,
5113 * just need to check access.
5114 */
5115 if (nm[0] == '.' && nm[1] == '\0') {
5116 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5117 if (error)
5118 return (error);
5119 VN_HOLD(dvp);
5120 *vpp = dvp;
5121 return (0);
5122 }
5123
5124 drp = VTOR4(dvp);
5125 if (!(drp->r_flags & R4LOOKUP)) {
5126 mutex_enter(&drp->r_statelock);
5127 drp->r_flags |= R4LOOKUP;
5128 mutex_exit(&drp->r_statelock);
5129 }
5130
5131 *vpp = NULL;
5132 /*
5133 * Lookup this name in the DNLC. If there is no entry
5134 * lookup over the wire.
5135 */
5136 if (!skipdnlc)
5137 *vpp = dnlc_lookup(dvp, nm);
5138 if (*vpp == NULL) {
5139 /*
5140 * We need to go over the wire to lookup the name.
5141 */
5142 return (nfs4lookupnew_otw(dvp, nm, vpp, cr));
5143 }
5144
5145 /*
5146 * We hit on the dnlc
5147 */
5148 if (*vpp != DNLC_NO_VNODE ||
5149 (dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
5150 /*
5151 * But our attrs may not be valid.
5152 */
5153 if (ATTRCACHE4_VALID(dvp)) {
5154 error = nfs4_waitfor_purge_complete(dvp);
5155 if (error) {
5156 VN_RELE(*vpp);
5157 *vpp = NULL;
5158 return (error);
5159 }
5160
5161 /*
5162 * If after the purge completes, check to make sure
5163 * our attrs are still valid.
5164 */
5165 if (ATTRCACHE4_VALID(dvp)) {
5166 /*
5167 * If we waited for a purge we may have
5168 * lost our vnode so look it up again.
5169 */
5170 VN_RELE(*vpp);
5171 *vpp = dnlc_lookup(dvp, nm);
5172 if (*vpp == NULL)
5173 return (nfs4lookupnew_otw(dvp,
5174 nm, vpp, cr));
5175
5176 /*
5177 * The access cache should almost always hit
5178 */
5179 error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5180
5181 if (error) {
5182 VN_RELE(*vpp);
5183 *vpp = NULL;
5184 return (error);
5185 }
5186 if (*vpp == DNLC_NO_VNODE) {
5187 VN_RELE(*vpp);
5188 *vpp = NULL;
5189 return (ENOENT);
5190 }
5191 return (0);
5192 }
5193 }
5194 }
5195
5196 ASSERT(*vpp != NULL);
5197
5198 /*
5199 * We may have gotten here we have one of the following cases:
5200 * 1) vpp != DNLC_NO_VNODE, our attrs have timed out so we
5201 * need to validate them.
5202 * 2) vpp == DNLC_NO_VNODE, a negative entry that we always
5203 * must validate.
5204 *
5205 * Go to the server and check if the directory has changed, if
5206 * it hasn't we are done and can use the dnlc entry.
5207 */
5208 return (nfs4lookupvalidate_otw(dvp, nm, vpp, cr));
5209 }
5210
5211 /*
5212 * Go to the server and check if the directory has changed, if
5213 * it hasn't we are done and can use the dnlc entry. If it
5214 * has changed we get a new copy of its attributes and check
5215 * the access for VEXEC, then relookup the filename and
5216 * get its filehandle and attributes.
5217 *
5218 * PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR
5219 * if the NVERIFY failed we must
5220 * purge the caches
5221 * cache new attributes (will set r_time_attr_inval)
5222 * cache new access
5223 * recheck VEXEC access
5224 * add name to dnlc, possibly negative
5225 * if LOOKUP succeeded
5226 * cache new attributes
5227 * else
5228 * set a new r_time_attr_inval for dvp
5229 * check to make sure we have access
5230 *
5231 * The vpp returned is the vnode passed in if the directory is valid,
5232 * a new vnode if successful lookup, or NULL on error.
5233 */
5234 static int
nfs4lookupvalidate_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)5235 nfs4lookupvalidate_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5236 {
5237 COMPOUND4args_clnt args;
5238 COMPOUND4res_clnt res;
5239 fattr4 *ver_fattr;
5240 fattr4_change dchange;
5241 int32_t *ptr;
5242 int argoplist_size = 7 * sizeof (nfs_argop4);
5243 nfs_argop4 *argop;
5244 int doqueue;
5245 mntinfo4_t *mi;
5246 nfs4_recov_state_t recov_state;
5247 hrtime_t t;
5248 int isdotdot;
5249 vnode_t *nvp;
5250 nfs_fh4 *fhp;
5251 nfs4_sharedfh_t *sfhp;
5252 nfs4_access_type_t cacc;
5253 rnode4_t *nrp;
5254 rnode4_t *drp = VTOR4(dvp);
5255 nfs4_ga_res_t *garp = NULL;
5256 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5257
5258 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5259 ASSERT(nm != NULL);
5260 ASSERT(nm[0] != '\0');
5261 ASSERT(dvp->v_type == VDIR);
5262 ASSERT(nm[0] != '.' || nm[1] != '\0');
5263 ASSERT(*vpp != NULL);
5264
5265 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5266 isdotdot = 1;
5267 args.ctag = TAG_LOOKUP_VPARENT;
5268 } else {
5269 /*
5270 * If dvp were a stub, it should have triggered and caused
5271 * a mount for us to get this far.
5272 */
5273 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5274
5275 isdotdot = 0;
5276 args.ctag = TAG_LOOKUP_VALID;
5277 }
5278
5279 mi = VTOMI4(dvp);
5280 recov_state.rs_flags = 0;
5281 recov_state.rs_num_retry_despite_err = 0;
5282
5283 nvp = NULL;
5284
5285 /* Save the original mount point security information */
5286 (void) save_mnt_secinfo(mi->mi_curr_serv);
5287
5288 recov_retry:
5289 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5290 &recov_state, NULL);
5291 if (e.error) {
5292 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5293 VN_RELE(*vpp);
5294 *vpp = NULL;
5295 return (e.error);
5296 }
5297
5298 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5299
5300 /* PUTFH dfh NVERIFY GETATTR ACCESS LOOKUP GETFH GETATTR */
5301 args.array_len = 7;
5302 args.array = argop;
5303
5304 /* 0. putfh file */
5305 argop[0].argop = OP_CPUTFH;
5306 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5307
5308 /* 1. nverify the change info */
5309 argop[1].argop = OP_NVERIFY;
5310 ver_fattr = &argop[1].nfs_argop4_u.opnverify.obj_attributes;
5311 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5312 ver_fattr->attrlist4 = (char *)&dchange;
5313 ptr = (int32_t *)&dchange;
5314 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5315 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5316
5317 /* 2. getattr directory */
5318 argop[2].argop = OP_GETATTR;
5319 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5320 argop[2].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5321
5322 /* 3. access directory */
5323 argop[3].argop = OP_ACCESS;
5324 argop[3].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5325 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5326
5327 /* 4. lookup name */
5328 if (isdotdot) {
5329 argop[4].argop = OP_LOOKUPP;
5330 } else {
5331 argop[4].argop = OP_CLOOKUP;
5332 argop[4].nfs_argop4_u.opclookup.cname = nm;
5333 }
5334
5335 /* 5. resulting file handle */
5336 argop[5].argop = OP_GETFH;
5337
5338 /* 6. resulting file attributes */
5339 argop[6].argop = OP_GETATTR;
5340 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5341 argop[6].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5342
5343 doqueue = 1;
5344 t = gethrtime();
5345
5346 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5347
5348 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5349 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5350 if (e.error != 0 && *vpp != NULL)
5351 VN_RELE(*vpp);
5352 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5353 &recov_state, FALSE);
5354 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5355 kmem_free(argop, argoplist_size);
5356 return (e.error);
5357 }
5358
5359 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5360 /*
5361 * For WRONGSEC of a non-dotdot case, send secinfo directly
5362 * from this thread, do not go thru the recovery thread since
5363 * we need the nm information.
5364 *
5365 * Not doing dotdot case because there is no specification
5366 * for (PUTFH, SECINFO "..") yet.
5367 */
5368 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5369 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5370 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5371 &recov_state, FALSE);
5372 else
5373 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5374 &recov_state, TRUE);
5375 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5376 kmem_free(argop, argoplist_size);
5377 if (!e.error)
5378 goto recov_retry;
5379 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5380 VN_RELE(*vpp);
5381 *vpp = NULL;
5382 return (e.error);
5383 }
5384
5385 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5386 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5387 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5388 &recov_state, TRUE);
5389
5390 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5391 kmem_free(argop, argoplist_size);
5392 goto recov_retry;
5393 }
5394 }
5395
5396 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5397
5398 if (e.error || res.array_len == 0) {
5399 /*
5400 * If e.error isn't set, then reply has no ops (or we couldn't
5401 * be here). The only legal way to reply without an op array
5402 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5403 * be in the reply for all other status values.
5404 *
5405 * For valid replies without an ops array, return ENOTSUP
5406 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5407 * return EIO -- don't trust status.
5408 */
5409 if (e.error == 0)
5410 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5411 ENOTSUP : EIO;
5412 VN_RELE(*vpp);
5413 *vpp = NULL;
5414 kmem_free(argop, argoplist_size);
5415 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5416 return (e.error);
5417 }
5418
5419 if (res.status != NFS4ERR_SAME) {
5420 e.error = geterrno4(res.status);
5421
5422 /*
5423 * The NVERIFY "failed" so the directory has changed
5424 * First make sure PUTFH succeeded and NVERIFY "failed"
5425 * cleanly.
5426 */
5427 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5428 (res.array[1].nfs_resop4_u.opnverify.status != NFS4_OK)) {
5429 nfs4_purge_stale_fh(e.error, dvp, cr);
5430 VN_RELE(*vpp);
5431 *vpp = NULL;
5432 goto exit;
5433 }
5434
5435 /*
5436 * We know the NVERIFY "failed" so we must:
5437 * purge the caches (access and indirectly dnlc if needed)
5438 */
5439 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5440
5441 if (res.array[2].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5442 nfs4_purge_stale_fh(e.error, dvp, cr);
5443 VN_RELE(*vpp);
5444 *vpp = NULL;
5445 goto exit;
5446 }
5447
5448 /*
5449 * Install new cached attributes for the directory
5450 */
5451 nfs4_attr_cache(dvp,
5452 &res.array[2].nfs_resop4_u.opgetattr.ga_res,
5453 t, cr, FALSE, NULL);
5454
5455 if (res.array[3].nfs_resop4_u.opaccess.status != NFS4_OK) {
5456 nfs4_purge_stale_fh(e.error, dvp, cr);
5457 VN_RELE(*vpp);
5458 *vpp = NULL;
5459 e.error = geterrno4(res.status);
5460 goto exit;
5461 }
5462
5463 /*
5464 * Now we know the directory is valid,
5465 * cache new directory access
5466 */
5467 nfs4_access_cache(drp,
5468 args.array[3].nfs_argop4_u.opaccess.access,
5469 res.array[3].nfs_resop4_u.opaccess.access, cr);
5470
5471 /*
5472 * recheck VEXEC access
5473 */
5474 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5475 if (cacc != NFS4_ACCESS_ALLOWED) {
5476 /*
5477 * Directory permissions might have been revoked
5478 */
5479 if (cacc == NFS4_ACCESS_DENIED) {
5480 e.error = EACCES;
5481 VN_RELE(*vpp);
5482 *vpp = NULL;
5483 goto exit;
5484 }
5485
5486 /*
5487 * Somehow we must not have asked for enough
5488 * so try a singleton ACCESS, should never happen.
5489 */
5490 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5491 if (e.error) {
5492 VN_RELE(*vpp);
5493 *vpp = NULL;
5494 goto exit;
5495 }
5496 }
5497
5498 e.error = geterrno4(res.status);
5499 if (res.array[4].nfs_resop4_u.oplookup.status != NFS4_OK) {
5500 /*
5501 * The lookup failed, probably no entry
5502 */
5503 if (e.error == ENOENT && nfs4_lookup_neg_cache) {
5504 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5505 } else {
5506 /*
5507 * Might be some other error, so remove
5508 * the dnlc entry to make sure we start all
5509 * over again, next time.
5510 */
5511 dnlc_remove(dvp, nm);
5512 }
5513 VN_RELE(*vpp);
5514 *vpp = NULL;
5515 goto exit;
5516 }
5517
5518 if (res.array[5].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5519 /*
5520 * The file exists but we can't get its fh for
5521 * some unknown reason. Remove it from the dnlc
5522 * and error out to be safe.
5523 */
5524 dnlc_remove(dvp, nm);
5525 VN_RELE(*vpp);
5526 *vpp = NULL;
5527 goto exit;
5528 }
5529 fhp = &res.array[5].nfs_resop4_u.opgetfh.object;
5530 if (fhp->nfs_fh4_len == 0) {
5531 /*
5532 * The file exists but a bogus fh
5533 * some unknown reason. Remove it from the dnlc
5534 * and error out to be safe.
5535 */
5536 e.error = ENOENT;
5537 dnlc_remove(dvp, nm);
5538 VN_RELE(*vpp);
5539 *vpp = NULL;
5540 goto exit;
5541 }
5542 sfhp = sfh4_get(fhp, mi);
5543
5544 if (res.array[6].nfs_resop4_u.opgetattr.status == NFS4_OK)
5545 garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
5546
5547 /*
5548 * Make the new rnode
5549 */
5550 if (isdotdot) {
5551 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
5552 if (e.error) {
5553 sfh4_rele(&sfhp);
5554 VN_RELE(*vpp);
5555 *vpp = NULL;
5556 goto exit;
5557 }
5558 /*
5559 * XXX if nfs4_make_dotdot uses an existing rnode
5560 * XXX it doesn't update the attributes.
5561 * XXX for now just save them again to save an OTW
5562 */
5563 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
5564 } else {
5565 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
5566 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
5567 /*
5568 * If v_type == VNON, then garp was NULL because
5569 * the last op in the compound failed and makenfs4node
5570 * could not find the vnode for sfhp. It created
5571 * a new vnode, so we have nothing to purge here.
5572 */
5573 if (nvp->v_type == VNON) {
5574 vattr_t vattr;
5575
5576 vattr.va_mask = AT_TYPE;
5577 /*
5578 * N.B. We've already called nfs4_end_fop above.
5579 */
5580 e.error = nfs4getattr(nvp, &vattr, cr);
5581 if (e.error) {
5582 sfh4_rele(&sfhp);
5583 VN_RELE(*vpp);
5584 *vpp = NULL;
5585 VN_RELE(nvp);
5586 goto exit;
5587 }
5588 nvp->v_type = vattr.va_type;
5589 }
5590 }
5591 sfh4_rele(&sfhp);
5592
5593 nrp = VTOR4(nvp);
5594 mutex_enter(&nrp->r_statev4_lock);
5595 if (!nrp->created_v4) {
5596 mutex_exit(&nrp->r_statev4_lock);
5597 dnlc_update(dvp, nm, nvp);
5598 } else
5599 mutex_exit(&nrp->r_statev4_lock);
5600
5601 VN_RELE(*vpp);
5602 *vpp = nvp;
5603 } else {
5604 hrtime_t now;
5605 hrtime_t delta = 0;
5606
5607 e.error = 0;
5608
5609 /*
5610 * Because the NVERIFY "succeeded" we know that the
5611 * directory attributes are still valid
5612 * so update r_time_attr_inval
5613 */
5614 now = gethrtime();
5615 mutex_enter(&drp->r_statelock);
5616 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5617 delta = now - drp->r_time_attr_saved;
5618 if (delta < mi->mi_acdirmin)
5619 delta = mi->mi_acdirmin;
5620 else if (delta > mi->mi_acdirmax)
5621 delta = mi->mi_acdirmax;
5622 }
5623 drp->r_time_attr_inval = now + delta;
5624 mutex_exit(&drp->r_statelock);
5625 dnlc_update(dvp, nm, *vpp);
5626
5627 /*
5628 * Even though we have a valid directory attr cache
5629 * and dnlc entry, we may not have access.
5630 * This should almost always hit the cache.
5631 */
5632 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5633 if (e.error) {
5634 VN_RELE(*vpp);
5635 *vpp = NULL;
5636 }
5637
5638 if (*vpp == DNLC_NO_VNODE) {
5639 VN_RELE(*vpp);
5640 *vpp = NULL;
5641 e.error = ENOENT;
5642 }
5643 }
5644
5645 exit:
5646 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5647 kmem_free(argop, argoplist_size);
5648 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5649 return (e.error);
5650 }
5651
5652 /*
5653 * We need to go over the wire to lookup the name, but
5654 * while we are there verify the directory has not
5655 * changed but if it has, get new attributes and check access
5656 *
5657 * PUTFH dfh SAVEFH LOOKUP nm GETFH GETATTR RESTOREFH
5658 * NVERIFY GETATTR ACCESS
5659 *
5660 * With the results:
5661 * if the NVERIFY failed we must purge the caches, add new attributes,
5662 * and cache new access.
5663 * set a new r_time_attr_inval
5664 * add name to dnlc, possibly negative
5665 * if LOOKUP succeeded
5666 * cache new attributes
5667 */
5668 static int
nfs4lookupnew_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)5669 nfs4lookupnew_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
5670 {
5671 COMPOUND4args_clnt args;
5672 COMPOUND4res_clnt res;
5673 fattr4 *ver_fattr;
5674 fattr4_change dchange;
5675 int32_t *ptr;
5676 nfs4_ga_res_t *garp = NULL;
5677 int argoplist_size = 9 * sizeof (nfs_argop4);
5678 nfs_argop4 *argop;
5679 int doqueue;
5680 mntinfo4_t *mi;
5681 nfs4_recov_state_t recov_state;
5682 hrtime_t t;
5683 int isdotdot;
5684 vnode_t *nvp;
5685 nfs_fh4 *fhp;
5686 nfs4_sharedfh_t *sfhp;
5687 nfs4_access_type_t cacc;
5688 rnode4_t *nrp;
5689 rnode4_t *drp = VTOR4(dvp);
5690 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
5691
5692 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
5693 ASSERT(nm != NULL);
5694 ASSERT(nm[0] != '\0');
5695 ASSERT(dvp->v_type == VDIR);
5696 ASSERT(nm[0] != '.' || nm[1] != '\0');
5697 ASSERT(*vpp == NULL);
5698
5699 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
5700 isdotdot = 1;
5701 args.ctag = TAG_LOOKUP_PARENT;
5702 } else {
5703 /*
5704 * If dvp were a stub, it should have triggered and caused
5705 * a mount for us to get this far.
5706 */
5707 ASSERT(!RP_ISSTUB(VTOR4(dvp)));
5708
5709 isdotdot = 0;
5710 args.ctag = TAG_LOOKUP;
5711 }
5712
5713 mi = VTOMI4(dvp);
5714 recov_state.rs_flags = 0;
5715 recov_state.rs_num_retry_despite_err = 0;
5716
5717 nvp = NULL;
5718
5719 /* Save the original mount point security information */
5720 (void) save_mnt_secinfo(mi->mi_curr_serv);
5721
5722 recov_retry:
5723 e.error = nfs4_start_fop(mi, dvp, NULL, OH_LOOKUP,
5724 &recov_state, NULL);
5725 if (e.error) {
5726 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5727 return (e.error);
5728 }
5729
5730 argop = kmem_alloc(argoplist_size, KM_SLEEP);
5731
5732 /* PUTFH SAVEFH LOOKUP GETFH GETATTR RESTOREFH NVERIFY GETATTR ACCESS */
5733 args.array_len = 9;
5734 args.array = argop;
5735
5736 /* 0. putfh file */
5737 argop[0].argop = OP_CPUTFH;
5738 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(dvp)->r_fh;
5739
5740 /* 1. savefh for the nverify */
5741 argop[1].argop = OP_SAVEFH;
5742
5743 /* 2. lookup name */
5744 if (isdotdot) {
5745 argop[2].argop = OP_LOOKUPP;
5746 } else {
5747 argop[2].argop = OP_CLOOKUP;
5748 argop[2].nfs_argop4_u.opclookup.cname = nm;
5749 }
5750
5751 /* 3. resulting file handle */
5752 argop[3].argop = OP_GETFH;
5753
5754 /* 4. resulting file attributes */
5755 argop[4].argop = OP_GETATTR;
5756 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5757 argop[4].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5758
5759 /* 5. restorefh back the directory for the nverify */
5760 argop[5].argop = OP_RESTOREFH;
5761
5762 /* 6. nverify the change info */
5763 argop[6].argop = OP_NVERIFY;
5764 ver_fattr = &argop[6].nfs_argop4_u.opnverify.obj_attributes;
5765 ver_fattr->attrmask = FATTR4_CHANGE_MASK;
5766 ver_fattr->attrlist4 = (char *)&dchange;
5767 ptr = (int32_t *)&dchange;
5768 IXDR_PUT_HYPER(ptr, VTOR4(dvp)->r_change);
5769 ver_fattr->attrlist4_len = sizeof (fattr4_change);
5770
5771 /* 7. getattr directory */
5772 argop[7].argop = OP_GETATTR;
5773 argop[7].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
5774 argop[7].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
5775
5776 /* 8. access directory */
5777 argop[8].argop = OP_ACCESS;
5778 argop[8].nfs_argop4_u.opaccess.access = ACCESS4_READ | ACCESS4_DELETE |
5779 ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_LOOKUP;
5780
5781 doqueue = 1;
5782 t = gethrtime();
5783
5784 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
5785
5786 if (!isdotdot && res.status == NFS4ERR_MOVED) {
5787 e.error = nfs4_setup_referral(dvp, nm, vpp, cr);
5788 if (e.error != 0 && *vpp != NULL)
5789 VN_RELE(*vpp);
5790 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5791 &recov_state, FALSE);
5792 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5793 kmem_free(argop, argoplist_size);
5794 return (e.error);
5795 }
5796
5797 if (nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp)) {
5798 /*
5799 * For WRONGSEC of a non-dotdot case, send secinfo directly
5800 * from this thread, do not go thru the recovery thread since
5801 * we need the nm information.
5802 *
5803 * Not doing dotdot case because there is no specification
5804 * for (PUTFH, SECINFO "..") yet.
5805 */
5806 if (!isdotdot && res.status == NFS4ERR_WRONGSEC) {
5807 if ((e.error = nfs4_secinfo_vnode_otw(dvp, nm, cr)))
5808 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5809 &recov_state, FALSE);
5810 else
5811 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5812 &recov_state, TRUE);
5813 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5814 kmem_free(argop, argoplist_size);
5815 if (!e.error)
5816 goto recov_retry;
5817 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5818 return (e.error);
5819 }
5820
5821 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
5822 OP_LOOKUP, NULL, NULL, NULL) == FALSE) {
5823 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP,
5824 &recov_state, TRUE);
5825
5826 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
5827 kmem_free(argop, argoplist_size);
5828 goto recov_retry;
5829 }
5830 }
5831
5832 nfs4_end_fop(mi, dvp, NULL, OH_LOOKUP, &recov_state, FALSE);
5833
5834 if (e.error || res.array_len == 0) {
5835 /*
5836 * If e.error isn't set, then reply has no ops (or we couldn't
5837 * be here). The only legal way to reply without an op array
5838 * is via NFS4ERR_MINOR_VERS_MISMATCH. An ops array should
5839 * be in the reply for all other status values.
5840 *
5841 * For valid replies without an ops array, return ENOTSUP
5842 * (geterrno4 xlation of VERS_MISMATCH). For illegal replies,
5843 * return EIO -- don't trust status.
5844 */
5845 if (e.error == 0)
5846 e.error = (res.status == NFS4ERR_MINOR_VERS_MISMATCH) ?
5847 ENOTSUP : EIO;
5848
5849 kmem_free(argop, argoplist_size);
5850 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
5851 return (e.error);
5852 }
5853
5854 e.error = geterrno4(res.status);
5855
5856 /*
5857 * The PUTFH and SAVEFH may have failed.
5858 */
5859 if ((res.array[0].nfs_resop4_u.opputfh.status != NFS4_OK) ||
5860 (res.array[1].nfs_resop4_u.opsavefh.status != NFS4_OK)) {
5861 nfs4_purge_stale_fh(e.error, dvp, cr);
5862 goto exit;
5863 }
5864
5865 /*
5866 * Check if the file exists, if it does delay entering
5867 * into the dnlc until after we update the directory
5868 * attributes so we don't cause it to get purged immediately.
5869 */
5870 if (res.array[2].nfs_resop4_u.oplookup.status != NFS4_OK) {
5871 /*
5872 * The lookup failed, probably no entry
5873 */
5874 if (e.error == ENOENT && nfs4_lookup_neg_cache)
5875 dnlc_update(dvp, nm, DNLC_NO_VNODE);
5876 goto exit;
5877 }
5878
5879 if (res.array[3].nfs_resop4_u.opgetfh.status != NFS4_OK) {
5880 /*
5881 * The file exists but we can't get its fh for
5882 * some unknown reason. Error out to be safe.
5883 */
5884 goto exit;
5885 }
5886
5887 fhp = &res.array[3].nfs_resop4_u.opgetfh.object;
5888 if (fhp->nfs_fh4_len == 0) {
5889 /*
5890 * The file exists but a bogus fh
5891 * some unknown reason. Error out to be safe.
5892 */
5893 e.error = EIO;
5894 goto exit;
5895 }
5896 sfhp = sfh4_get(fhp, mi);
5897
5898 if (res.array[4].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5899 sfh4_rele(&sfhp);
5900 goto exit;
5901 }
5902 garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
5903
5904 /*
5905 * The RESTOREFH may have failed
5906 */
5907 if (res.array[5].nfs_resop4_u.oprestorefh.status != NFS4_OK) {
5908 sfh4_rele(&sfhp);
5909 e.error = EIO;
5910 goto exit;
5911 }
5912
5913 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4ERR_SAME) {
5914 /*
5915 * First make sure the NVERIFY failed as we expected,
5916 * if it didn't then be conservative and error out
5917 * as we can't trust the directory.
5918 */
5919 if (res.array[6].nfs_resop4_u.opnverify.status != NFS4_OK) {
5920 sfh4_rele(&sfhp);
5921 e.error = EIO;
5922 goto exit;
5923 }
5924
5925 /*
5926 * We know the NVERIFY "failed" so the directory has changed,
5927 * so we must:
5928 * purge the caches (access and indirectly dnlc if needed)
5929 */
5930 nfs4_purge_caches(dvp, NFS4_NOPURGE_DNLC, cr, TRUE);
5931
5932 if (res.array[7].nfs_resop4_u.opgetattr.status != NFS4_OK) {
5933 sfh4_rele(&sfhp);
5934 goto exit;
5935 }
5936 nfs4_attr_cache(dvp,
5937 &res.array[7].nfs_resop4_u.opgetattr.ga_res,
5938 t, cr, FALSE, NULL);
5939
5940 if (res.array[8].nfs_resop4_u.opaccess.status != NFS4_OK) {
5941 nfs4_purge_stale_fh(e.error, dvp, cr);
5942 sfh4_rele(&sfhp);
5943 e.error = geterrno4(res.status);
5944 goto exit;
5945 }
5946
5947 /*
5948 * Now we know the directory is valid,
5949 * cache new directory access
5950 */
5951 nfs4_access_cache(drp,
5952 args.array[8].nfs_argop4_u.opaccess.access,
5953 res.array[8].nfs_resop4_u.opaccess.access, cr);
5954
5955 /*
5956 * recheck VEXEC access
5957 */
5958 cacc = nfs4_access_check(drp, ACCESS4_LOOKUP, cr);
5959 if (cacc != NFS4_ACCESS_ALLOWED) {
5960 /*
5961 * Directory permissions might have been revoked
5962 */
5963 if (cacc == NFS4_ACCESS_DENIED) {
5964 sfh4_rele(&sfhp);
5965 e.error = EACCES;
5966 goto exit;
5967 }
5968
5969 /*
5970 * Somehow we must not have asked for enough
5971 * so try a singleton ACCESS should never happen
5972 */
5973 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
5974 if (e.error) {
5975 sfh4_rele(&sfhp);
5976 goto exit;
5977 }
5978 }
5979
5980 e.error = geterrno4(res.status);
5981 } else {
5982 hrtime_t now;
5983 hrtime_t delta = 0;
5984
5985 e.error = 0;
5986
5987 /*
5988 * Because the NVERIFY "succeeded" we know that the
5989 * directory attributes are still valid
5990 * so update r_time_attr_inval
5991 */
5992 now = gethrtime();
5993 mutex_enter(&drp->r_statelock);
5994 if (!(mi->mi_flags & MI4_NOAC) && !(dvp->v_flag & VNOCACHE)) {
5995 delta = now - drp->r_time_attr_saved;
5996 if (delta < mi->mi_acdirmin)
5997 delta = mi->mi_acdirmin;
5998 else if (delta > mi->mi_acdirmax)
5999 delta = mi->mi_acdirmax;
6000 }
6001 drp->r_time_attr_inval = now + delta;
6002 mutex_exit(&drp->r_statelock);
6003
6004 /*
6005 * Even though we have a valid directory attr cache,
6006 * we may not have access.
6007 * This should almost always hit the cache.
6008 */
6009 e.error = nfs4_access(dvp, VEXEC, 0, cr, NULL);
6010 if (e.error) {
6011 sfh4_rele(&sfhp);
6012 goto exit;
6013 }
6014 }
6015
6016 /*
6017 * Now we have successfully completed the lookup, if the
6018 * directory has changed we now have the valid attributes.
6019 * We also know we have directory access.
6020 * Create the new rnode and insert it in the dnlc.
6021 */
6022 if (isdotdot) {
6023 e.error = nfs4_make_dotdot(sfhp, t, dvp, cr, &nvp, 1);
6024 if (e.error) {
6025 sfh4_rele(&sfhp);
6026 goto exit;
6027 }
6028 /*
6029 * XXX if nfs4_make_dotdot uses an existing rnode
6030 * XXX it doesn't update the attributes.
6031 * XXX for now just save them again to save an OTW
6032 */
6033 nfs4_attr_cache(nvp, garp, t, cr, FALSE, NULL);
6034 } else {
6035 nvp = makenfs4node(sfhp, garp, dvp->v_vfsp, t, cr,
6036 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
6037 }
6038 sfh4_rele(&sfhp);
6039
6040 nrp = VTOR4(nvp);
6041 mutex_enter(&nrp->r_statev4_lock);
6042 if (!nrp->created_v4) {
6043 mutex_exit(&nrp->r_statev4_lock);
6044 dnlc_update(dvp, nm, nvp);
6045 } else
6046 mutex_exit(&nrp->r_statev4_lock);
6047
6048 *vpp = nvp;
6049
6050 exit:
6051 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6052 kmem_free(argop, argoplist_size);
6053 (void) check_mnt_secinfo(mi->mi_curr_serv, nvp);
6054 return (e.error);
6055 }
6056
6057 #ifdef DEBUG
6058 void
nfs4lookup_dump_compound(char * where,nfs_argop4 * argbase,int argcnt)6059 nfs4lookup_dump_compound(char *where, nfs_argop4 *argbase, int argcnt)
6060 {
6061 uint_t i, len;
6062 zoneid_t zoneid = getzoneid();
6063 char *s;
6064
6065 zcmn_err(zoneid, CE_NOTE, "%s: dumping cmpd", where);
6066 for (i = 0; i < argcnt; i++) {
6067 nfs_argop4 *op = &argbase[i];
6068 switch (op->argop) {
6069 case OP_CPUTFH:
6070 case OP_PUTFH:
6071 zcmn_err(zoneid, CE_NOTE, "\t op %d, putfh", i);
6072 break;
6073 case OP_PUTROOTFH:
6074 zcmn_err(zoneid, CE_NOTE, "\t op %d, putrootfh", i);
6075 break;
6076 case OP_CLOOKUP:
6077 s = op->nfs_argop4_u.opclookup.cname;
6078 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6079 break;
6080 case OP_LOOKUP:
6081 s = utf8_to_str(&op->nfs_argop4_u.oplookup.objname,
6082 &len, NULL);
6083 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookup %s", i, s);
6084 kmem_free(s, len);
6085 break;
6086 case OP_LOOKUPP:
6087 zcmn_err(zoneid, CE_NOTE, "\t op %d, lookupp ..", i);
6088 break;
6089 case OP_GETFH:
6090 zcmn_err(zoneid, CE_NOTE, "\t op %d, getfh", i);
6091 break;
6092 case OP_GETATTR:
6093 zcmn_err(zoneid, CE_NOTE, "\t op %d, getattr", i);
6094 break;
6095 case OP_OPENATTR:
6096 zcmn_err(zoneid, CE_NOTE, "\t op %d, openattr", i);
6097 break;
6098 default:
6099 zcmn_err(zoneid, CE_NOTE, "\t op %d, opcode %d", i,
6100 op->argop);
6101 break;
6102 }
6103 }
6104 }
6105 #endif
6106
6107 /*
6108 * nfs4lookup_setup - constructs a multi-lookup compound request.
6109 *
6110 * Given the path "nm1/nm2/.../nmn", the following compound requests
6111 * may be created:
6112 *
6113 * Note: Getfh is not be needed because filehandle attr is mandatory, but it
6114 * is faster, for now.
6115 *
6116 * l4_getattrs indicates the type of compound requested.
6117 *
6118 * LKP4_NO_ATTRIBUTE - no attributes (used by secinfo):
6119 *
6120 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn} }
6121 *
6122 * total number of ops is n + 1.
6123 *
6124 * LKP4_LAST_NAMED_ATTR - multi-component path for a named
6125 * attribute: create lookups plus one OPENATTR/GETFH/GETATTR
6126 * before the last component, and only get attributes
6127 * for the last component. Note that the second-to-last
6128 * pathname component is XATTR_RPATH, which does NOT go
6129 * over-the-wire as a lookup.
6130 *
6131 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Lookup {nmn-2};
6132 * Openattr; Getfh; Getattr; Lookup {nmn}; Getfh; Getattr }
6133 *
6134 * and total number of ops is n + 5.
6135 *
6136 * LKP4_LAST_ATTRDIR - multi-component path for the hidden named
6137 * attribute directory: create lookups plus an OPENATTR
6138 * replacing the last lookup. Note that the last pathname
6139 * component is XATTR_RPATH, which does NOT go over-the-wire
6140 * as a lookup.
6141 *
6142 * compound { Put*fh; Lookup {nm1}; Lookup {nm2}; ... Getfh; Getattr;
6143 * Openattr; Getfh; Getattr }
6144 *
6145 * and total number of ops is n + 5.
6146 *
6147 * LKP4_ALL_ATTRIBUTES - create lookups and get attributes for intermediate
6148 * nodes too.
6149 *
6150 * compound { Put*fh; Lookup {nm1}; Getfh; Getattr;
6151 * Lookup {nm2}; ... Lookup {nmn}; Getfh; Getattr }
6152 *
6153 * and total number of ops is 3*n + 1.
6154 *
6155 * All cases: returns the index in the arg array of the final LOOKUP op, or
6156 * -1 if no LOOKUPs were used.
6157 */
6158 int
nfs4lookup_setup(char * nm,lookup4_param_t * lookupargp,int needgetfh)6159 nfs4lookup_setup(char *nm, lookup4_param_t *lookupargp, int needgetfh)
6160 {
6161 enum lkp4_attr_setup l4_getattrs = lookupargp->l4_getattrs;
6162 nfs_argop4 *argbase, *argop;
6163 int arglen, argcnt;
6164 int n = 1; /* number of components */
6165 int nga = 1; /* number of Getattr's in request */
6166 char c = '\0', *s, *p;
6167 int lookup_idx = -1;
6168 int argoplist_size;
6169
6170 /* set lookuparg response result to 0 */
6171 lookupargp->resp->status = NFS4_OK;
6172
6173 /* skip leading "/" or "." e.g. ".//./" if there is */
6174 for (; ; nm++) {
6175 if (*nm != '/' && *nm != '.')
6176 break;
6177
6178 /* ".." is counted as 1 component */
6179 if (*nm == '.' && *(nm + 1) != '/')
6180 break;
6181 }
6182
6183 /*
6184 * Find n = number of components - nm must be null terminated
6185 * Skip "." components.
6186 */
6187 if (*nm != '\0')
6188 for (n = 1, s = nm; *s != '\0'; s++) {
6189 if ((*s == '/') && (*(s + 1) != '/') &&
6190 (*(s + 1) != '\0') &&
6191 !(*(s + 1) == '.' && (*(s + 2) == '/' ||
6192 *(s + 2) == '\0')))
6193 n++;
6194 }
6195 else
6196 n = 0;
6197
6198 /*
6199 * nga is number of components that need Getfh+Getattr
6200 */
6201 switch (l4_getattrs) {
6202 case LKP4_NO_ATTRIBUTES:
6203 nga = 0;
6204 break;
6205 case LKP4_ALL_ATTRIBUTES:
6206 nga = n;
6207 /*
6208 * Always have at least 1 getfh, getattr pair
6209 */
6210 if (nga == 0)
6211 nga++;
6212 break;
6213 case LKP4_LAST_ATTRDIR:
6214 case LKP4_LAST_NAMED_ATTR:
6215 nga = n+1;
6216 break;
6217 }
6218
6219 /*
6220 * If change to use the filehandle attr instead of getfh
6221 * the following line can be deleted.
6222 */
6223 nga *= 2;
6224
6225 /*
6226 * calculate number of ops in request as
6227 * header + trailer + lookups + getattrs
6228 */
6229 arglen = lookupargp->header_len + lookupargp->trailer_len + n + nga;
6230
6231 argoplist_size = arglen * sizeof (nfs_argop4);
6232 argop = argbase = kmem_alloc(argoplist_size, KM_SLEEP);
6233 lookupargp->argsp->array = argop;
6234
6235 argcnt = lookupargp->header_len;
6236 argop += argcnt;
6237
6238 /*
6239 * loop and create a lookup op and possibly getattr/getfh for
6240 * each component. Skip "." components.
6241 */
6242 for (s = nm; *s != '\0'; s = p) {
6243 /*
6244 * Set up a pathname struct for each component if needed
6245 */
6246 while (*s == '/')
6247 s++;
6248 if (*s == '\0')
6249 break;
6250
6251 for (p = s; (*p != '/') && (*p != '\0'); p++)
6252 ;
6253 c = *p;
6254 *p = '\0';
6255
6256 if (s[0] == '.' && s[1] == '\0') {
6257 *p = c;
6258 continue;
6259 }
6260 if (l4_getattrs == LKP4_LAST_ATTRDIR &&
6261 strcmp(s, XATTR_RPATH) == 0) {
6262 /* getfh XXX may not be needed in future */
6263 argop->argop = OP_GETFH;
6264 argop++;
6265 argcnt++;
6266
6267 /* getattr */
6268 argop->argop = OP_GETATTR;
6269 argop->nfs_argop4_u.opgetattr.attr_request =
6270 lookupargp->ga_bits;
6271 argop->nfs_argop4_u.opgetattr.mi =
6272 lookupargp->mi;
6273 argop++;
6274 argcnt++;
6275
6276 /* openattr */
6277 argop->argop = OP_OPENATTR;
6278 } else if (l4_getattrs == LKP4_LAST_NAMED_ATTR &&
6279 strcmp(s, XATTR_RPATH) == 0) {
6280 /* openattr */
6281 argop->argop = OP_OPENATTR;
6282 argop++;
6283 argcnt++;
6284
6285 /* getfh XXX may not be needed in future */
6286 argop->argop = OP_GETFH;
6287 argop++;
6288 argcnt++;
6289
6290 /* getattr */
6291 argop->argop = OP_GETATTR;
6292 argop->nfs_argop4_u.opgetattr.attr_request =
6293 lookupargp->ga_bits;
6294 argop->nfs_argop4_u.opgetattr.mi =
6295 lookupargp->mi;
6296 argop++;
6297 argcnt++;
6298 *p = c;
6299 continue;
6300 } else if (s[0] == '.' && s[1] == '.' && s[2] == '\0') {
6301 /* lookupp */
6302 argop->argop = OP_LOOKUPP;
6303 } else {
6304 /* lookup */
6305 argop->argop = OP_LOOKUP;
6306 (void) str_to_utf8(s,
6307 &argop->nfs_argop4_u.oplookup.objname);
6308 }
6309 lookup_idx = argcnt;
6310 argop++;
6311 argcnt++;
6312
6313 *p = c;
6314
6315 if (l4_getattrs == LKP4_ALL_ATTRIBUTES) {
6316 /* getfh XXX may not be needed in future */
6317 argop->argop = OP_GETFH;
6318 argop++;
6319 argcnt++;
6320
6321 /* getattr */
6322 argop->argop = OP_GETATTR;
6323 argop->nfs_argop4_u.opgetattr.attr_request =
6324 lookupargp->ga_bits;
6325 argop->nfs_argop4_u.opgetattr.mi =
6326 lookupargp->mi;
6327 argop++;
6328 argcnt++;
6329 }
6330 }
6331
6332 if ((l4_getattrs != LKP4_NO_ATTRIBUTES) &&
6333 ((l4_getattrs != LKP4_ALL_ATTRIBUTES) || (lookup_idx < 0))) {
6334 if (needgetfh) {
6335 /* stick in a post-lookup getfh */
6336 argop->argop = OP_GETFH;
6337 argcnt++;
6338 argop++;
6339 }
6340 /* post-lookup getattr */
6341 argop->argop = OP_GETATTR;
6342 argop->nfs_argop4_u.opgetattr.attr_request =
6343 lookupargp->ga_bits;
6344 argop->nfs_argop4_u.opgetattr.mi = lookupargp->mi;
6345 argcnt++;
6346 }
6347 argcnt += lookupargp->trailer_len; /* actual op count */
6348 lookupargp->argsp->array_len = argcnt;
6349 lookupargp->arglen = arglen;
6350
6351 #ifdef DEBUG
6352 if (nfs4_client_lookup_debug)
6353 nfs4lookup_dump_compound("nfs4lookup_setup", argbase, argcnt);
6354 #endif
6355
6356 return (lookup_idx);
6357 }
6358
6359 static int
nfs4openattr(vnode_t * dvp,vnode_t ** avp,int cflag,cred_t * cr)6360 nfs4openattr(vnode_t *dvp, vnode_t **avp, int cflag, cred_t *cr)
6361 {
6362 COMPOUND4args_clnt args;
6363 COMPOUND4res_clnt res;
6364 GETFH4res *gf_res = NULL;
6365 nfs_argop4 argop[4];
6366 nfs_resop4 *resop = NULL;
6367 nfs4_sharedfh_t *sfhp;
6368 hrtime_t t;
6369 nfs4_error_t e;
6370
6371 rnode4_t *drp;
6372 int doqueue = 1;
6373 vnode_t *vp;
6374 int needrecov = 0;
6375 nfs4_recov_state_t recov_state;
6376
6377 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
6378
6379 *avp = NULL;
6380 recov_state.rs_flags = 0;
6381 recov_state.rs_num_retry_despite_err = 0;
6382
6383 recov_retry:
6384 /* COMPOUND: putfh, openattr, getfh, getattr */
6385 args.array_len = 4;
6386 args.array = argop;
6387 args.ctag = TAG_OPENATTR;
6388
6389 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
6390 if (e.error)
6391 return (e.error);
6392
6393 drp = VTOR4(dvp);
6394
6395 /* putfh */
6396 argop[0].argop = OP_CPUTFH;
6397 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6398
6399 /* openattr */
6400 argop[1].argop = OP_OPENATTR;
6401 argop[1].nfs_argop4_u.opopenattr.createdir = (cflag ? TRUE : FALSE);
6402
6403 /* getfh */
6404 argop[2].argop = OP_GETFH;
6405
6406 /* getattr */
6407 argop[3].argop = OP_GETATTR;
6408 argop[3].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6409 argop[3].nfs_argop4_u.opgetattr.mi = VTOMI4(dvp);
6410
6411 NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
6412 "nfs4openattr: %s call, drp %s", needrecov ? "recov" : "first",
6413 rnode4info(drp)));
6414
6415 t = gethrtime();
6416
6417 rfs4call(VTOMI4(dvp), &args, &res, cr, &doqueue, 0, &e);
6418
6419 needrecov = nfs4_needs_recovery(&e, FALSE, dvp->v_vfsp);
6420 if (needrecov) {
6421 bool_t abort;
6422
6423 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
6424 "nfs4openattr: initiating recovery\n"));
6425
6426 abort = nfs4_start_recovery(&e,
6427 VTOMI4(dvp), dvp, NULL, NULL, NULL,
6428 OP_OPENATTR, NULL, NULL, NULL);
6429 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6430 if (!e.error) {
6431 e.error = geterrno4(res.status);
6432 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6433 }
6434 if (abort == FALSE)
6435 goto recov_retry;
6436 return (e.error);
6437 }
6438
6439 if (e.error) {
6440 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6441 return (e.error);
6442 }
6443
6444 if (res.status) {
6445 /*
6446 * If OTW errro is NOTSUPP, then it should be
6447 * translated to EINVAL. All Solaris file system
6448 * implementations return EINVAL to the syscall layer
6449 * when the attrdir cannot be created due to an
6450 * implementation restriction or noxattr mount option.
6451 */
6452 if (res.status == NFS4ERR_NOTSUPP) {
6453 mutex_enter(&drp->r_statelock);
6454 if (drp->r_xattr_dir)
6455 VN_RELE(drp->r_xattr_dir);
6456 VN_HOLD(NFS4_XATTR_DIR_NOTSUPP);
6457 drp->r_xattr_dir = NFS4_XATTR_DIR_NOTSUPP;
6458 mutex_exit(&drp->r_statelock);
6459
6460 e.error = EINVAL;
6461 } else {
6462 e.error = geterrno4(res.status);
6463 }
6464
6465 if (e.error) {
6466 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6467 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
6468 needrecov);
6469 return (e.error);
6470 }
6471 }
6472
6473 resop = &res.array[0]; /* putfh res */
6474 ASSERT(resop->nfs_resop4_u.opgetfh.status == NFS4_OK);
6475
6476 resop = &res.array[1]; /* openattr res */
6477 ASSERT(resop->nfs_resop4_u.opopenattr.status == NFS4_OK);
6478
6479 resop = &res.array[2]; /* getfh res */
6480 gf_res = &resop->nfs_resop4_u.opgetfh;
6481 if (gf_res->object.nfs_fh4_len == 0) {
6482 *avp = NULL;
6483 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6484 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6485 return (ENOENT);
6486 }
6487
6488 sfhp = sfh4_get(&gf_res->object, VTOMI4(dvp));
6489 vp = makenfs4node(sfhp, &res.array[3].nfs_resop4_u.opgetattr.ga_res,
6490 dvp->v_vfsp, t, cr, dvp,
6491 fn_get(VTOSV(dvp)->sv_name, XATTR_RPATH, sfhp));
6492 sfh4_rele(&sfhp);
6493
6494 if (e.error)
6495 PURGE_ATTRCACHE4(vp);
6496
6497 mutex_enter(&vp->v_lock);
6498 vp->v_flag |= V_XATTRDIR;
6499 mutex_exit(&vp->v_lock);
6500
6501 *avp = vp;
6502
6503 mutex_enter(&drp->r_statelock);
6504 if (drp->r_xattr_dir)
6505 VN_RELE(drp->r_xattr_dir);
6506 VN_HOLD(vp);
6507 drp->r_xattr_dir = vp;
6508
6509 /*
6510 * Invalidate pathconf4 cache because r_xattr_dir is no longer
6511 * NULL. xattrs could be created at any time, and we have no
6512 * way to update pc4_xattr_exists in the base object if/when
6513 * it happens.
6514 */
6515 drp->r_pathconf.pc4_xattr_valid = 0;
6516
6517 mutex_exit(&drp->r_statelock);
6518
6519 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
6520
6521 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
6522
6523 return (0);
6524 }
6525
6526 /* ARGSUSED */
6527 static int
nfs4_create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int flags,caller_context_t * ct,vsecattr_t * vsecp)6528 nfs4_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
6529 int mode, vnode_t **vpp, cred_t *cr, int flags, caller_context_t *ct,
6530 vsecattr_t *vsecp)
6531 {
6532 int error;
6533 vnode_t *vp = NULL;
6534 rnode4_t *rp;
6535 struct vattr vattr;
6536 rnode4_t *drp;
6537 vnode_t *tempvp;
6538 enum createmode4 createmode;
6539 bool_t must_trunc = FALSE;
6540 int truncating = 0;
6541
6542 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
6543 return (EPERM);
6544 if (exclusive == EXCL && (dvp->v_flag & V_XATTRDIR)) {
6545 return (EINVAL);
6546 }
6547
6548 /* . and .. have special meaning in the protocol, reject them. */
6549
6550 if (nm[0] == '.' && (nm[1] == '\0' || (nm[1] == '.' && nm[2] == '\0')))
6551 return (EISDIR);
6552
6553 drp = VTOR4(dvp);
6554
6555 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
6556 return (EINTR);
6557
6558 top:
6559 /*
6560 * We make a copy of the attributes because the caller does not
6561 * expect us to change what va points to.
6562 */
6563 vattr = *va;
6564
6565 /*
6566 * If the pathname is "", then dvp is the root vnode of
6567 * a remote file mounted over a local directory.
6568 * All that needs to be done is access
6569 * checking and truncation. Note that we avoid doing
6570 * open w/ create because the parent directory might
6571 * be in pseudo-fs and the open would fail.
6572 */
6573 if (*nm == '\0') {
6574 error = 0;
6575 VN_HOLD(dvp);
6576 vp = dvp;
6577 must_trunc = TRUE;
6578 } else {
6579 /*
6580 * We need to go over the wire, just to be sure whether the
6581 * file exists or not. Using the DNLC can be dangerous in
6582 * this case when making a decision regarding existence.
6583 */
6584 error = nfs4lookup(dvp, nm, &vp, cr, 1);
6585 }
6586
6587 if (exclusive)
6588 createmode = EXCLUSIVE4;
6589 else
6590 createmode = GUARDED4;
6591
6592 /*
6593 * error would be set if the file does not exist on the
6594 * server, so lets go create it.
6595 */
6596 if (error) {
6597 goto create_otw;
6598 }
6599
6600 /*
6601 * File does exist on the server
6602 */
6603 if (exclusive == EXCL)
6604 error = EEXIST;
6605 else if (vp->v_type == VDIR && (mode & VWRITE))
6606 error = EISDIR;
6607 else {
6608 /*
6609 * If vnode is a device, create special vnode.
6610 */
6611 if (ISVDEV(vp->v_type)) {
6612 tempvp = vp;
6613 vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
6614 VN_RELE(tempvp);
6615 }
6616 if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
6617 if ((vattr.va_mask & AT_SIZE) &&
6618 vp->v_type == VREG) {
6619 rp = VTOR4(vp);
6620 /*
6621 * Check here for large file handled
6622 * by LF-unaware process (as
6623 * ufs_create() does)
6624 */
6625 if (!(flags & FOFFMAX)) {
6626 mutex_enter(&rp->r_statelock);
6627 if (rp->r_size > MAXOFF32_T)
6628 error = EOVERFLOW;
6629 mutex_exit(&rp->r_statelock);
6630 }
6631
6632 /* if error is set then we need to return */
6633 if (error) {
6634 nfs_rw_exit(&drp->r_rwlock);
6635 VN_RELE(vp);
6636 return (error);
6637 }
6638
6639 if (must_trunc) {
6640 vattr.va_mask = AT_SIZE;
6641 error = nfs4setattr(vp, &vattr, 0, cr,
6642 NULL);
6643 } else {
6644 /*
6645 * we know we have a regular file that already
6646 * exists and we may end up truncating the file
6647 * as a result of the open_otw, so flush out
6648 * any dirty pages for this file first.
6649 */
6650 if (nfs4_has_pages(vp) &&
6651 ((rp->r_flags & R4DIRTY) ||
6652 rp->r_count > 0 ||
6653 rp->r_mapcnt > 0)) {
6654 error = nfs4_putpage(vp,
6655 (offset_t)0, 0, 0, cr, ct);
6656 if (error && (error == ENOSPC ||
6657 error == EDQUOT)) {
6658 mutex_enter(
6659 &rp->r_statelock);
6660 if (!rp->r_error)
6661 rp->r_error =
6662 error;
6663 mutex_exit(
6664 &rp->r_statelock);
6665 }
6666 }
6667 vattr.va_mask = (AT_SIZE |
6668 AT_TYPE | AT_MODE);
6669 vattr.va_type = VREG;
6670 createmode = UNCHECKED4;
6671 truncating = 1;
6672 goto create_otw;
6673 }
6674 }
6675 }
6676 }
6677 nfs_rw_exit(&drp->r_rwlock);
6678 if (error) {
6679 VN_RELE(vp);
6680 } else {
6681 vnode_t *tvp;
6682 rnode4_t *trp;
6683 tvp = vp;
6684 if (vp->v_type == VREG) {
6685 trp = VTOR4(vp);
6686 if (IS_SHADOW(vp, trp))
6687 tvp = RTOV4(trp);
6688 }
6689
6690 if (must_trunc) {
6691 /*
6692 * existing file got truncated, notify.
6693 */
6694 vnevent_create(tvp, ct);
6695 }
6696
6697 *vpp = vp;
6698 }
6699 return (error);
6700
6701 create_otw:
6702 dnlc_remove(dvp, nm);
6703
6704 ASSERT(vattr.va_mask & AT_TYPE);
6705
6706 /*
6707 * If not a regular file let nfs4mknod() handle it.
6708 */
6709 if (vattr.va_type != VREG) {
6710 error = nfs4mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
6711 nfs_rw_exit(&drp->r_rwlock);
6712 return (error);
6713 }
6714
6715 /*
6716 * It _is_ a regular file.
6717 */
6718 ASSERT(vattr.va_mask & AT_MODE);
6719 if (MANDMODE(vattr.va_mode)) {
6720 nfs_rw_exit(&drp->r_rwlock);
6721 return (EACCES);
6722 }
6723
6724 /*
6725 * If this happens to be a mknod of a regular file, then flags will
6726 * have neither FREAD or FWRITE. However, we must set at least one
6727 * for the call to nfs4open_otw. If it's open(O_CREAT) driving
6728 * nfs4_create, then either FREAD, FWRITE, or FRDWR has already been
6729 * set (based on openmode specified by app).
6730 */
6731 if ((flags & (FREAD|FWRITE)) == 0)
6732 flags |= (FREAD|FWRITE);
6733
6734 error = nfs4open_otw(dvp, nm, &vattr, vpp, cr, 1, flags, createmode, 0);
6735
6736 if (vp != NULL) {
6737 /* if create was successful, throw away the file's pages */
6738 if (!error && (vattr.va_mask & AT_SIZE))
6739 nfs4_invalidate_pages(vp, (vattr.va_size & PAGEMASK),
6740 cr);
6741 /* release the lookup hold */
6742 VN_RELE(vp);
6743 vp = NULL;
6744 }
6745
6746 /*
6747 * validate that we opened a regular file. This handles a misbehaving
6748 * server that returns an incorrect FH.
6749 */
6750 if ((error == 0) && *vpp && (*vpp)->v_type != VREG) {
6751 error = EISDIR;
6752 VN_RELE(*vpp);
6753 }
6754
6755 /*
6756 * If this is not an exclusive create, then the CREATE
6757 * request will be made with the GUARDED mode set. This
6758 * means that the server will return EEXIST if the file
6759 * exists. The file could exist because of a retransmitted
6760 * request. In this case, we recover by starting over and
6761 * checking to see whether the file exists. This second
6762 * time through it should and a CREATE request will not be
6763 * sent.
6764 *
6765 * This handles the problem of a dangling CREATE request
6766 * which contains attributes which indicate that the file
6767 * should be truncated. This retransmitted request could
6768 * possibly truncate valid data in the file if not caught
6769 * by the duplicate request mechanism on the server or if
6770 * not caught by other means. The scenario is:
6771 *
6772 * Client transmits CREATE request with size = 0
6773 * Client times out, retransmits request.
6774 * Response to the first request arrives from the server
6775 * and the client proceeds on.
6776 * Client writes data to the file.
6777 * The server now processes retransmitted CREATE request
6778 * and truncates file.
6779 *
6780 * The use of the GUARDED CREATE request prevents this from
6781 * happening because the retransmitted CREATE would fail
6782 * with EEXIST and would not truncate the file.
6783 */
6784 if (error == EEXIST && exclusive == NONEXCL) {
6785 #ifdef DEBUG
6786 nfs4_create_misses++;
6787 #endif
6788 goto top;
6789 }
6790 nfs_rw_exit(&drp->r_rwlock);
6791 if (truncating && !error && *vpp) {
6792 vnode_t *tvp;
6793 rnode4_t *trp;
6794 /*
6795 * existing file got truncated, notify.
6796 */
6797 tvp = *vpp;
6798 trp = VTOR4(tvp);
6799 if (IS_SHADOW(tvp, trp))
6800 tvp = RTOV4(trp);
6801 vnevent_create(tvp, ct);
6802 }
6803 return (error);
6804 }
6805
6806 /*
6807 * Create compound (for mkdir, mknod, symlink):
6808 * { Putfh <dfh>; Create; Getfh; Getattr }
6809 * It's okay if setattr failed to set gid - this is not considered
6810 * an error, but purge attrs in that case.
6811 */
6812 static int
call_nfs4_create_req(vnode_t * dvp,char * nm,void * data,struct vattr * va,vnode_t ** vpp,cred_t * cr,nfs_ftype4 type)6813 call_nfs4_create_req(vnode_t *dvp, char *nm, void *data, struct vattr *va,
6814 vnode_t **vpp, cred_t *cr, nfs_ftype4 type)
6815 {
6816 int need_end_op = FALSE;
6817 COMPOUND4args_clnt args;
6818 COMPOUND4res_clnt res, *resp = NULL;
6819 nfs_argop4 *argop;
6820 nfs_resop4 *resop;
6821 int doqueue;
6822 mntinfo4_t *mi;
6823 rnode4_t *drp = VTOR4(dvp);
6824 change_info4 *cinfo;
6825 GETFH4res *gf_res;
6826 struct vattr vattr;
6827 vnode_t *vp;
6828 fattr4 *crattr;
6829 bool_t needrecov = FALSE;
6830 nfs4_recov_state_t recov_state;
6831 nfs4_sharedfh_t *sfhp = NULL;
6832 hrtime_t t;
6833 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
6834 int numops, argoplist_size, setgid_flag, idx_create, idx_fattr;
6835 dirattr_info_t dinfo, *dinfop;
6836 servinfo4_t *svp;
6837 bitmap4 supp_attrs;
6838
6839 ASSERT(type == NF4DIR || type == NF4LNK || type == NF4BLK ||
6840 type == NF4CHR || type == NF4SOCK || type == NF4FIFO);
6841
6842 mi = VTOMI4(dvp);
6843
6844 /*
6845 * Make sure we properly deal with setting the right gid
6846 * on a new directory to reflect the parent's setgid bit
6847 */
6848 setgid_flag = 0;
6849 if (type == NF4DIR) {
6850 struct vattr dva;
6851
6852 va->va_mode &= ~VSGID;
6853 dva.va_mask = AT_MODE | AT_GID;
6854 if (VOP_GETATTR(dvp, &dva, 0, cr, NULL) == 0) {
6855
6856 /*
6857 * If the parent's directory has the setgid bit set
6858 * _and_ the client was able to get a valid mapping
6859 * for the parent dir's owner_group, we want to
6860 * append NVERIFY(owner_group == dva.va_gid) and
6861 * SETTATTR to the CREATE compound.
6862 */
6863 if (mi->mi_flags & MI4_GRPID || dva.va_mode & VSGID) {
6864 setgid_flag = 1;
6865 va->va_mode |= VSGID;
6866 if (dva.va_gid != GID_NOBODY) {
6867 va->va_mask |= AT_GID;
6868 va->va_gid = dva.va_gid;
6869 }
6870 }
6871 }
6872 }
6873
6874 /*
6875 * Create ops:
6876 * 0:putfh(dir) 1:savefh(dir) 2:create 3:getfh(new) 4:getattr(new)
6877 * 5:restorefh(dir) 6:getattr(dir)
6878 *
6879 * if (setgid)
6880 * 0:putfh(dir) 1:create 2:getfh(new) 3:getattr(new)
6881 * 4:savefh(new) 5:putfh(dir) 6:getattr(dir) 7:restorefh(new)
6882 * 8:nverify 9:setattr
6883 */
6884 if (setgid_flag) {
6885 numops = 10;
6886 idx_create = 1;
6887 idx_fattr = 3;
6888 } else {
6889 numops = 7;
6890 idx_create = 2;
6891 idx_fattr = 4;
6892 }
6893
6894 ASSERT(nfs_zone() == mi->mi_zone);
6895 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp))) {
6896 return (EINTR);
6897 }
6898 recov_state.rs_flags = 0;
6899 recov_state.rs_num_retry_despite_err = 0;
6900
6901 argoplist_size = numops * sizeof (nfs_argop4);
6902 argop = kmem_alloc(argoplist_size, KM_SLEEP);
6903
6904 recov_retry:
6905 if (type == NF4LNK)
6906 args.ctag = TAG_SYMLINK;
6907 else if (type == NF4DIR)
6908 args.ctag = TAG_MKDIR;
6909 else
6910 args.ctag = TAG_MKNOD;
6911
6912 args.array_len = numops;
6913 args.array = argop;
6914
6915 if (e.error = nfs4_start_op(mi, dvp, NULL, &recov_state)) {
6916 nfs_rw_exit(&drp->r_rwlock);
6917 kmem_free(argop, argoplist_size);
6918 return (e.error);
6919 }
6920 need_end_op = TRUE;
6921
6922
6923 /* 0: putfh directory */
6924 argop[0].argop = OP_CPUTFH;
6925 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6926
6927 /* 1/2: Create object */
6928 argop[idx_create].argop = OP_CCREATE;
6929 argop[idx_create].nfs_argop4_u.opccreate.cname = nm;
6930 argop[idx_create].nfs_argop4_u.opccreate.type = type;
6931 if (type == NF4LNK) {
6932 /*
6933 * symlink, treat name as data
6934 */
6935 ASSERT(data != NULL);
6936 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.clinkdata =
6937 (char *)data;
6938 }
6939 if (type == NF4BLK || type == NF4CHR) {
6940 ASSERT(data != NULL);
6941 argop[idx_create].nfs_argop4_u.opccreate.ftype4_u.devdata =
6942 *((specdata4 *)data);
6943 }
6944
6945 crattr = &argop[idx_create].nfs_argop4_u.opccreate.createattrs;
6946
6947 svp = drp->r_server;
6948 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
6949 supp_attrs = svp->sv_supp_attrs;
6950 nfs_rw_exit(&svp->sv_lock);
6951
6952 if (vattr_to_fattr4(va, NULL, crattr, 0, OP_CREATE, supp_attrs)) {
6953 nfs_rw_exit(&drp->r_rwlock);
6954 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
6955 e.error = EINVAL;
6956 kmem_free(argop, argoplist_size);
6957 return (e.error);
6958 }
6959
6960 /* 2/3: getfh fh of created object */
6961 ASSERT(idx_create + 1 == idx_fattr - 1);
6962 argop[idx_create + 1].argop = OP_GETFH;
6963
6964 /* 3/4: getattr of new object */
6965 argop[idx_fattr].argop = OP_GETATTR;
6966 argop[idx_fattr].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6967 argop[idx_fattr].nfs_argop4_u.opgetattr.mi = mi;
6968
6969 if (setgid_flag) {
6970 vattr_t _v;
6971
6972 argop[4].argop = OP_SAVEFH;
6973
6974 argop[5].argop = OP_CPUTFH;
6975 argop[5].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
6976
6977 argop[6].argop = OP_GETATTR;
6978 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
6979 argop[6].nfs_argop4_u.opgetattr.mi = mi;
6980
6981 argop[7].argop = OP_RESTOREFH;
6982
6983 /*
6984 * nverify
6985 *
6986 * XXX - Revisit the last argument to nfs4_end_op()
6987 * once 5020486 is fixed.
6988 */
6989 _v.va_mask = AT_GID;
6990 _v.va_gid = va->va_gid;
6991 if (e.error = nfs4args_verify(&argop[8], &_v, OP_NVERIFY,
6992 supp_attrs)) {
6993 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
6994 nfs_rw_exit(&drp->r_rwlock);
6995 nfs4_fattr4_free(crattr);
6996 kmem_free(argop, argoplist_size);
6997 return (e.error);
6998 }
6999
7000 /*
7001 * setattr
7002 *
7003 * We _know_ we're not messing with AT_SIZE or AT_XTIME,
7004 * so no need for stateid or flags. Also we specify NULL
7005 * rp since we're only interested in setting owner_group
7006 * attributes.
7007 */
7008 nfs4args_setattr(&argop[9], &_v, NULL, 0, NULL, cr, supp_attrs,
7009 &e.error, 0);
7010
7011 if (e.error) {
7012 nfs4_end_op(mi, dvp, *vpp, &recov_state, TRUE);
7013 nfs_rw_exit(&drp->r_rwlock);
7014 nfs4_fattr4_free(crattr);
7015 nfs4args_verify_free(&argop[8]);
7016 kmem_free(argop, argoplist_size);
7017 return (e.error);
7018 }
7019 } else {
7020 argop[1].argop = OP_SAVEFH;
7021
7022 argop[5].argop = OP_RESTOREFH;
7023
7024 argop[6].argop = OP_GETATTR;
7025 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7026 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7027 }
7028
7029 dnlc_remove(dvp, nm);
7030
7031 doqueue = 1;
7032 t = gethrtime();
7033 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7034
7035 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7036 if (e.error) {
7037 PURGE_ATTRCACHE4(dvp);
7038 if (!needrecov)
7039 goto out;
7040 }
7041
7042 if (needrecov) {
7043 if (nfs4_start_recovery(&e, mi, dvp, NULL, NULL, NULL,
7044 OP_CREATE, NULL, NULL, NULL) == FALSE) {
7045 nfs4_end_op(mi, dvp, NULL, &recov_state,
7046 needrecov);
7047 need_end_op = FALSE;
7048 nfs4_fattr4_free(crattr);
7049 if (setgid_flag) {
7050 nfs4args_verify_free(&argop[8]);
7051 nfs4args_setattr_free(&argop[9]);
7052 }
7053 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7054 goto recov_retry;
7055 }
7056 }
7057
7058 resp = &res;
7059
7060 if (res.status != NFS4_OK && res.array_len <= idx_fattr + 1) {
7061
7062 if (res.status == NFS4ERR_BADOWNER)
7063 nfs4_log_badowner(mi, OP_CREATE);
7064
7065 e.error = geterrno4(res.status);
7066
7067 /*
7068 * This check is left over from when create was implemented
7069 * using a setattr op (instead of createattrs). If the
7070 * putfh/create/getfh failed, the error was returned. If
7071 * setattr/getattr failed, we keep going.
7072 *
7073 * It might be better to get rid of the GETFH also, and just
7074 * do PUTFH/CREATE/GETATTR since the FH attr is mandatory.
7075 * Then if any of the operations failed, we could return the
7076 * error now, and remove much of the error code below.
7077 */
7078 if (res.array_len <= idx_fattr) {
7079 /*
7080 * Either Putfh, Create or Getfh failed.
7081 */
7082 PURGE_ATTRCACHE4(dvp);
7083 /*
7084 * nfs4_purge_stale_fh() may generate otw calls through
7085 * nfs4_invalidate_pages. Hence the need to call
7086 * nfs4_end_op() here to avoid nfs4_start_op() deadlock.
7087 */
7088 nfs4_end_op(mi, dvp, NULL, &recov_state,
7089 needrecov);
7090 need_end_op = FALSE;
7091 nfs4_purge_stale_fh(e.error, dvp, cr);
7092 goto out;
7093 }
7094 }
7095
7096 resop = &res.array[idx_create]; /* create res */
7097 cinfo = &resop->nfs_resop4_u.opcreate.cinfo;
7098
7099 resop = &res.array[idx_create + 1]; /* getfh res */
7100 gf_res = &resop->nfs_resop4_u.opgetfh;
7101
7102 sfhp = sfh4_get(&gf_res->object, mi);
7103 if (e.error) {
7104 *vpp = vp = makenfs4node(sfhp, NULL, dvp->v_vfsp, t, cr, dvp,
7105 fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7106 if (vp->v_type == VNON) {
7107 vattr.va_mask = AT_TYPE;
7108 /*
7109 * Need to call nfs4_end_op before nfs4getattr to avoid
7110 * potential nfs4_start_op deadlock. See RFE 4777612.
7111 */
7112 nfs4_end_op(mi, dvp, NULL, &recov_state,
7113 needrecov);
7114 need_end_op = FALSE;
7115 e.error = nfs4getattr(vp, &vattr, cr);
7116 if (e.error) {
7117 VN_RELE(vp);
7118 *vpp = NULL;
7119 goto out;
7120 }
7121 vp->v_type = vattr.va_type;
7122 }
7123 e.error = 0;
7124 } else {
7125 *vpp = vp = makenfs4node(sfhp,
7126 &res.array[idx_fattr].nfs_resop4_u.opgetattr.ga_res,
7127 dvp->v_vfsp, t, cr,
7128 dvp, fn_get(VTOSV(dvp)->sv_name, nm, sfhp));
7129 }
7130
7131 /*
7132 * If compound succeeded, then update dir attrs
7133 */
7134 if (res.status == NFS4_OK) {
7135 dinfo.di_garp = &res.array[6].nfs_resop4_u.opgetattr.ga_res;
7136 dinfo.di_cred = cr;
7137 dinfo.di_time_call = t;
7138 dinfop = &dinfo;
7139 } else
7140 dinfop = NULL;
7141
7142 /* Update directory cache attribute, readdir and dnlc caches */
7143 nfs4_update_dircaches(cinfo, dvp, vp, nm, dinfop);
7144
7145 out:
7146 if (sfhp != NULL)
7147 sfh4_rele(&sfhp);
7148 nfs_rw_exit(&drp->r_rwlock);
7149 nfs4_fattr4_free(crattr);
7150 if (setgid_flag) {
7151 nfs4args_verify_free(&argop[8]);
7152 nfs4args_setattr_free(&argop[9]);
7153 }
7154 if (resp)
7155 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7156 if (need_end_op)
7157 nfs4_end_op(mi, dvp, NULL, &recov_state, needrecov);
7158
7159 kmem_free(argop, argoplist_size);
7160 return (e.error);
7161 }
7162
7163 /* ARGSUSED */
7164 static int
nfs4mknod(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr)7165 nfs4mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
7166 int mode, vnode_t **vpp, cred_t *cr)
7167 {
7168 int error;
7169 vnode_t *vp;
7170 nfs_ftype4 type;
7171 specdata4 spec, *specp = NULL;
7172
7173 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
7174
7175 switch (va->va_type) {
7176 case VCHR:
7177 case VBLK:
7178 type = (va->va_type == VCHR) ? NF4CHR : NF4BLK;
7179 spec.specdata1 = getmajor(va->va_rdev);
7180 spec.specdata2 = getminor(va->va_rdev);
7181 specp = &spec;
7182 break;
7183
7184 case VFIFO:
7185 type = NF4FIFO;
7186 break;
7187 case VSOCK:
7188 type = NF4SOCK;
7189 break;
7190
7191 default:
7192 return (EINVAL);
7193 }
7194
7195 error = call_nfs4_create_req(dvp, nm, specp, va, &vp, cr, type);
7196 if (error) {
7197 return (error);
7198 }
7199
7200 /*
7201 * This might not be needed any more; special case to deal
7202 * with problematic v2/v3 servers. Since create was unable
7203 * to set group correctly, not sure what hope setattr has.
7204 */
7205 if (va->va_gid != VTOR4(vp)->r_attr.va_gid) {
7206 va->va_mask = AT_GID;
7207 (void) nfs4setattr(vp, va, 0, cr, NULL);
7208 }
7209
7210 /*
7211 * If vnode is a device create special vnode
7212 */
7213 if (ISVDEV(vp->v_type)) {
7214 *vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
7215 VN_RELE(vp);
7216 } else {
7217 *vpp = vp;
7218 }
7219 return (error);
7220 }
7221
7222 /*
7223 * Remove requires that the current fh be the target directory.
7224 * After the operation, the current fh is unchanged.
7225 * The compound op structure is:
7226 * PUTFH(targetdir), REMOVE
7227 *
7228 * Weirdness: if the vnode to be removed is open
7229 * we rename it instead of removing it and nfs_inactive
7230 * will remove the new name.
7231 */
7232 /* ARGSUSED */
7233 static int
nfs4_remove(vnode_t * dvp,char * nm,cred_t * cr,caller_context_t * ct,int flags)7234 nfs4_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
7235 {
7236 COMPOUND4args_clnt args;
7237 COMPOUND4res_clnt res, *resp = NULL;
7238 REMOVE4res *rm_res;
7239 nfs_argop4 argop[3];
7240 nfs_resop4 *resop;
7241 vnode_t *vp;
7242 char *tmpname;
7243 int doqueue;
7244 mntinfo4_t *mi;
7245 rnode4_t *rp;
7246 rnode4_t *drp;
7247 int needrecov = 0;
7248 nfs4_recov_state_t recov_state;
7249 int isopen;
7250 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7251 dirattr_info_t dinfo;
7252
7253 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
7254 return (EPERM);
7255 drp = VTOR4(dvp);
7256 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
7257 return (EINTR);
7258
7259 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
7260 if (e.error) {
7261 nfs_rw_exit(&drp->r_rwlock);
7262 return (e.error);
7263 }
7264
7265 if (vp->v_type == VDIR) {
7266 VN_RELE(vp);
7267 nfs_rw_exit(&drp->r_rwlock);
7268 return (EISDIR);
7269 }
7270
7271 /*
7272 * First just remove the entry from the name cache, as it
7273 * is most likely the only entry for this vp.
7274 */
7275 dnlc_remove(dvp, nm);
7276
7277 rp = VTOR4(vp);
7278
7279 /*
7280 * For regular file types, check to see if the file is open by looking
7281 * at the open streams.
7282 * For all other types, check the reference count on the vnode. Since
7283 * they are not opened OTW they never have an open stream.
7284 *
7285 * If the file is open, rename it to .nfsXXXX.
7286 */
7287 if (vp->v_type != VREG) {
7288 /*
7289 * If the file has a v_count > 1 then there may be more than one
7290 * entry in the name cache due multiple links or an open file,
7291 * but we don't have the real reference count so flush all
7292 * possible entries.
7293 */
7294 if (vp->v_count > 1)
7295 dnlc_purge_vp(vp);
7296
7297 /*
7298 * Now we have the real reference count.
7299 */
7300 isopen = vp->v_count > 1;
7301 } else {
7302 mutex_enter(&rp->r_os_lock);
7303 isopen = list_head(&rp->r_open_streams) != NULL;
7304 mutex_exit(&rp->r_os_lock);
7305 }
7306
7307 mutex_enter(&rp->r_statelock);
7308 if (isopen &&
7309 (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
7310 mutex_exit(&rp->r_statelock);
7311 tmpname = newname();
7312 e.error = nfs4rename(dvp, nm, dvp, tmpname, cr, ct);
7313 if (e.error)
7314 kmem_free(tmpname, MAXNAMELEN);
7315 else {
7316 mutex_enter(&rp->r_statelock);
7317 if (rp->r_unldvp == NULL) {
7318 VN_HOLD(dvp);
7319 rp->r_unldvp = dvp;
7320 if (rp->r_unlcred != NULL)
7321 crfree(rp->r_unlcred);
7322 crhold(cr);
7323 rp->r_unlcred = cr;
7324 rp->r_unlname = tmpname;
7325 } else {
7326 kmem_free(rp->r_unlname, MAXNAMELEN);
7327 rp->r_unlname = tmpname;
7328 }
7329 mutex_exit(&rp->r_statelock);
7330 }
7331 VN_RELE(vp);
7332 nfs_rw_exit(&drp->r_rwlock);
7333 return (e.error);
7334 }
7335 /*
7336 * Actually remove the file/dir
7337 */
7338 mutex_exit(&rp->r_statelock);
7339
7340 /*
7341 * We need to flush any dirty pages which happen to
7342 * be hanging around before removing the file.
7343 * This shouldn't happen very often since in NFSv4
7344 * we should be close to open consistent.
7345 */
7346 if (nfs4_has_pages(vp) &&
7347 ((rp->r_flags & R4DIRTY) || rp->r_count > 0)) {
7348 e.error = nfs4_putpage(vp, (u_offset_t)0, 0, 0, cr, ct);
7349 if (e.error && (e.error == ENOSPC || e.error == EDQUOT)) {
7350 mutex_enter(&rp->r_statelock);
7351 if (!rp->r_error)
7352 rp->r_error = e.error;
7353 mutex_exit(&rp->r_statelock);
7354 }
7355 }
7356
7357 mi = VTOMI4(dvp);
7358
7359 (void) nfs4delegreturn(rp, NFS4_DR_REOPEN);
7360 recov_state.rs_flags = 0;
7361 recov_state.rs_num_retry_despite_err = 0;
7362
7363 recov_retry:
7364 /*
7365 * Remove ops: putfh dir; remove
7366 */
7367 args.ctag = TAG_REMOVE;
7368 args.array_len = 3;
7369 args.array = argop;
7370
7371 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
7372 if (e.error) {
7373 nfs_rw_exit(&drp->r_rwlock);
7374 VN_RELE(vp);
7375 return (e.error);
7376 }
7377
7378 /* putfh directory */
7379 argop[0].argop = OP_CPUTFH;
7380 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
7381
7382 /* remove */
7383 argop[1].argop = OP_CREMOVE;
7384 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
7385
7386 /* getattr dir */
7387 argop[2].argop = OP_GETATTR;
7388 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7389 argop[2].nfs_argop4_u.opgetattr.mi = mi;
7390
7391 doqueue = 1;
7392 dinfo.di_time_call = gethrtime();
7393 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
7394
7395 PURGE_ATTRCACHE4(vp);
7396
7397 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
7398 if (e.error)
7399 PURGE_ATTRCACHE4(dvp);
7400
7401 if (needrecov) {
7402 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp,
7403 NULL, NULL, NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
7404 if (!e.error)
7405 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7406 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
7407 needrecov);
7408 goto recov_retry;
7409 }
7410 }
7411
7412 /*
7413 * Matching nfs4_end_op() for start_op() above.
7414 * There is a path in the code below which calls
7415 * nfs4_purge_stale_fh(), which may generate otw calls through
7416 * nfs4_invalidate_pages. Hence we need to call nfs4_end_op()
7417 * here to avoid nfs4_start_op() deadlock.
7418 */
7419 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
7420
7421 if (!e.error) {
7422 resp = &res;
7423
7424 if (res.status) {
7425 e.error = geterrno4(res.status);
7426 PURGE_ATTRCACHE4(dvp);
7427 nfs4_purge_stale_fh(e.error, dvp, cr);
7428 } else {
7429 resop = &res.array[1]; /* remove res */
7430 rm_res = &resop->nfs_resop4_u.opremove;
7431
7432 dinfo.di_garp =
7433 &res.array[2].nfs_resop4_u.opgetattr.ga_res;
7434 dinfo.di_cred = cr;
7435
7436 /* Update directory attr, readdir and dnlc caches */
7437 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
7438 &dinfo);
7439 }
7440 }
7441 nfs_rw_exit(&drp->r_rwlock);
7442 if (resp)
7443 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7444
7445 if (e.error == 0) {
7446 vnode_t *tvp;
7447 rnode4_t *trp;
7448 trp = VTOR4(vp);
7449 tvp = vp;
7450 if (IS_SHADOW(vp, trp))
7451 tvp = RTOV4(trp);
7452 vnevent_remove(tvp, dvp, nm, ct);
7453 }
7454 VN_RELE(vp);
7455 return (e.error);
7456 }
7457
7458 /*
7459 * Link requires that the current fh be the target directory and the
7460 * saved fh be the source fh. After the operation, the current fh is unchanged.
7461 * Thus the compound op structure is:
7462 * PUTFH(file), SAVEFH, PUTFH(targetdir), LINK, RESTOREFH,
7463 * GETATTR(file)
7464 */
7465 /* ARGSUSED */
7466 static int
nfs4_link(vnode_t * tdvp,vnode_t * svp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)7467 nfs4_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
7468 caller_context_t *ct, int flags)
7469 {
7470 COMPOUND4args_clnt args;
7471 COMPOUND4res_clnt res, *resp = NULL;
7472 LINK4res *ln_res;
7473 int argoplist_size = 7 * sizeof (nfs_argop4);
7474 nfs_argop4 *argop;
7475 nfs_resop4 *resop;
7476 vnode_t *realvp, *nvp;
7477 int doqueue;
7478 mntinfo4_t *mi;
7479 rnode4_t *tdrp;
7480 bool_t needrecov = FALSE;
7481 nfs4_recov_state_t recov_state;
7482 hrtime_t t;
7483 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
7484 dirattr_info_t dinfo;
7485
7486 ASSERT(*tnm != '\0');
7487 ASSERT(tdvp->v_type == VDIR);
7488 ASSERT(nfs4_consistent_type(tdvp));
7489 ASSERT(nfs4_consistent_type(svp));
7490
7491 if (nfs_zone() != VTOMI4(tdvp)->mi_zone)
7492 return (EPERM);
7493 if (VOP_REALVP(svp, &realvp, ct) == 0) {
7494 svp = realvp;
7495 ASSERT(nfs4_consistent_type(svp));
7496 }
7497
7498 tdrp = VTOR4(tdvp);
7499 mi = VTOMI4(svp);
7500
7501 if (!(mi->mi_flags & MI4_LINK)) {
7502 return (EOPNOTSUPP);
7503 }
7504 recov_state.rs_flags = 0;
7505 recov_state.rs_num_retry_despite_err = 0;
7506
7507 if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR4(tdvp)))
7508 return (EINTR);
7509
7510 recov_retry:
7511 argop = kmem_alloc(argoplist_size, KM_SLEEP);
7512
7513 args.ctag = TAG_LINK;
7514
7515 /*
7516 * Link ops: putfh fl; savefh; putfh tdir; link; getattr(dir);
7517 * restorefh; getattr(fl)
7518 */
7519 args.array_len = 7;
7520 args.array = argop;
7521
7522 e.error = nfs4_start_op(VTOMI4(svp), svp, tdvp, &recov_state);
7523 if (e.error) {
7524 kmem_free(argop, argoplist_size);
7525 nfs_rw_exit(&tdrp->r_rwlock);
7526 return (e.error);
7527 }
7528
7529 /* 0. putfh file */
7530 argop[0].argop = OP_CPUTFH;
7531 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(svp)->r_fh;
7532
7533 /* 1. save current fh to free up the space for the dir */
7534 argop[1].argop = OP_SAVEFH;
7535
7536 /* 2. putfh targetdir */
7537 argop[2].argop = OP_CPUTFH;
7538 argop[2].nfs_argop4_u.opcputfh.sfh = tdrp->r_fh;
7539
7540 /* 3. link: current_fh is targetdir, saved_fh is source */
7541 argop[3].argop = OP_CLINK;
7542 argop[3].nfs_argop4_u.opclink.cnewname = tnm;
7543
7544 /* 4. Get attributes of dir */
7545 argop[4].argop = OP_GETATTR;
7546 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7547 argop[4].nfs_argop4_u.opgetattr.mi = mi;
7548
7549 /* 5. If link was successful, restore current vp to file */
7550 argop[5].argop = OP_RESTOREFH;
7551
7552 /* 6. Get attributes of linked object */
7553 argop[6].argop = OP_GETATTR;
7554 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
7555 argop[6].nfs_argop4_u.opgetattr.mi = mi;
7556
7557 dnlc_remove(tdvp, tnm);
7558
7559 doqueue = 1;
7560 t = gethrtime();
7561
7562 rfs4call(VTOMI4(svp), &args, &res, cr, &doqueue, 0, &e);
7563
7564 needrecov = nfs4_needs_recovery(&e, FALSE, svp->v_vfsp);
7565 if (e.error != 0 && !needrecov) {
7566 PURGE_ATTRCACHE4(tdvp);
7567 PURGE_ATTRCACHE4(svp);
7568 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7569 goto out;
7570 }
7571
7572 if (needrecov) {
7573 bool_t abort;
7574
7575 abort = nfs4_start_recovery(&e, VTOMI4(svp), svp, tdvp,
7576 NULL, NULL, OP_LINK, NULL, NULL, NULL);
7577 if (abort == FALSE) {
7578 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state,
7579 needrecov);
7580 kmem_free(argop, argoplist_size);
7581 if (!e.error)
7582 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
7583 goto recov_retry;
7584 } else {
7585 if (e.error != 0) {
7586 PURGE_ATTRCACHE4(tdvp);
7587 PURGE_ATTRCACHE4(svp);
7588 nfs4_end_op(VTOMI4(svp), svp, tdvp,
7589 &recov_state, needrecov);
7590 goto out;
7591 }
7592 /* fall through for res.status case */
7593 }
7594 }
7595
7596 nfs4_end_op(VTOMI4(svp), svp, tdvp, &recov_state, needrecov);
7597
7598 resp = &res;
7599 if (res.status) {
7600 /* If link succeeded, then don't return error */
7601 e.error = geterrno4(res.status);
7602 if (res.array_len <= 4) {
7603 /*
7604 * Either Putfh, Savefh, Putfh dir, or Link failed
7605 */
7606 PURGE_ATTRCACHE4(svp);
7607 PURGE_ATTRCACHE4(tdvp);
7608 if (e.error == EOPNOTSUPP) {
7609 mutex_enter(&mi->mi_lock);
7610 mi->mi_flags &= ~MI4_LINK;
7611 mutex_exit(&mi->mi_lock);
7612 }
7613 /* Remap EISDIR to EPERM for non-root user for SVVS */
7614 /* XXX-LP */
7615 if (e.error == EISDIR && crgetuid(cr) != 0)
7616 e.error = EPERM;
7617 goto out;
7618 }
7619 }
7620
7621 /* either no error or one of the postop getattr failed */
7622
7623 /*
7624 * XXX - if LINK succeeded, but no attrs were returned for link
7625 * file, purge its cache.
7626 *
7627 * XXX Perform a simplified version of wcc checking. Instead of
7628 * have another getattr to get pre-op, just purge cache if
7629 * any of the ops prior to and including the getattr failed.
7630 * If the getattr succeeded then update the attrcache accordingly.
7631 */
7632
7633 /*
7634 * update cache with link file postattrs.
7635 * Note: at this point resop points to link res.
7636 */
7637 resop = &res.array[3]; /* link res */
7638 ln_res = &resop->nfs_resop4_u.oplink;
7639 if (res.status == NFS4_OK)
7640 e.error = nfs4_update_attrcache(res.status,
7641 &res.array[6].nfs_resop4_u.opgetattr.ga_res,
7642 t, svp, cr);
7643
7644 /*
7645 * Call makenfs4node to create the new shadow vp for tnm.
7646 * We pass NULL attrs because we just cached attrs for
7647 * the src object. All we're trying to accomplish is to
7648 * to create the new shadow vnode.
7649 */
7650 nvp = makenfs4node(VTOR4(svp)->r_fh, NULL, tdvp->v_vfsp, t, cr,
7651 tdvp, fn_get(VTOSV(tdvp)->sv_name, tnm, VTOR4(svp)->r_fh));
7652
7653 /* Update target cache attribute, readdir and dnlc caches */
7654 dinfo.di_garp = &res.array[4].nfs_resop4_u.opgetattr.ga_res;
7655 dinfo.di_time_call = t;
7656 dinfo.di_cred = cr;
7657
7658 nfs4_update_dircaches(&ln_res->cinfo, tdvp, nvp, tnm, &dinfo);
7659 ASSERT(nfs4_consistent_type(tdvp));
7660 ASSERT(nfs4_consistent_type(svp));
7661 ASSERT(nfs4_consistent_type(nvp));
7662 VN_RELE(nvp);
7663
7664 if (!e.error) {
7665 vnode_t *tvp;
7666 rnode4_t *trp;
7667 /*
7668 * Notify the source file of this link operation.
7669 */
7670 trp = VTOR4(svp);
7671 tvp = svp;
7672 if (IS_SHADOW(svp, trp))
7673 tvp = RTOV4(trp);
7674 vnevent_link(tvp, ct);
7675 }
7676 out:
7677 kmem_free(argop, argoplist_size);
7678 if (resp)
7679 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
7680
7681 nfs_rw_exit(&tdrp->r_rwlock);
7682
7683 return (e.error);
7684 }
7685
7686 /* ARGSUSED */
7687 static int
nfs4_rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct,int flags)7688 nfs4_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7689 caller_context_t *ct, int flags)
7690 {
7691 vnode_t *realvp;
7692
7693 if (nfs_zone() != VTOMI4(odvp)->mi_zone)
7694 return (EPERM);
7695 if (VOP_REALVP(ndvp, &realvp, ct) == 0)
7696 ndvp = realvp;
7697
7698 return (nfs4rename(odvp, onm, ndvp, nnm, cr, ct));
7699 }
7700
7701 /*
7702 * nfs4rename does the real work of renaming in NFS Version 4.
7703 *
7704 * A file handle is considered volatile for renaming purposes if either
7705 * of the volatile bits are turned on. However, the compound may differ
7706 * based on the likelihood of the filehandle to change during rename.
7707 */
7708 static int
nfs4rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct)7709 nfs4rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
7710 caller_context_t *ct)
7711 {
7712 int error;
7713 mntinfo4_t *mi;
7714 vnode_t *nvp = NULL;
7715 vnode_t *ovp = NULL;
7716 char *tmpname = NULL;
7717 rnode4_t *rp;
7718 rnode4_t *odrp;
7719 rnode4_t *ndrp;
7720 int did_link = 0;
7721 int do_link = 1;
7722 nfsstat4 stat = NFS4_OK;
7723
7724 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
7725 ASSERT(nfs4_consistent_type(odvp));
7726 ASSERT(nfs4_consistent_type(ndvp));
7727
7728 if (onm[0] == '.' && (onm[1] == '\0' ||
7729 (onm[1] == '.' && onm[2] == '\0')))
7730 return (EINVAL);
7731
7732 if (nnm[0] == '.' && (nnm[1] == '\0' ||
7733 (nnm[1] == '.' && nnm[2] == '\0')))
7734 return (EINVAL);
7735
7736 odrp = VTOR4(odvp);
7737 ndrp = VTOR4(ndvp);
7738 if ((intptr_t)odrp < (intptr_t)ndrp) {
7739 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp)))
7740 return (EINTR);
7741 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp))) {
7742 nfs_rw_exit(&odrp->r_rwlock);
7743 return (EINTR);
7744 }
7745 } else {
7746 if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR4(ndvp)))
7747 return (EINTR);
7748 if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR4(odvp))) {
7749 nfs_rw_exit(&ndrp->r_rwlock);
7750 return (EINTR);
7751 }
7752 }
7753
7754 /*
7755 * Lookup the target file. If it exists, it needs to be
7756 * checked to see whether it is a mount point and whether
7757 * it is active (open).
7758 */
7759 error = nfs4lookup(ndvp, nnm, &nvp, cr, 0);
7760 if (!error) {
7761 int isactive;
7762
7763 ASSERT(nfs4_consistent_type(nvp));
7764 /*
7765 * If this file has been mounted on, then just
7766 * return busy because renaming to it would remove
7767 * the mounted file system from the name space.
7768 */
7769 if (vn_ismntpt(nvp)) {
7770 VN_RELE(nvp);
7771 nfs_rw_exit(&odrp->r_rwlock);
7772 nfs_rw_exit(&ndrp->r_rwlock);
7773 return (EBUSY);
7774 }
7775
7776 /*
7777 * First just remove the entry from the name cache, as it
7778 * is most likely the only entry for this vp.
7779 */
7780 dnlc_remove(ndvp, nnm);
7781
7782 rp = VTOR4(nvp);
7783
7784 if (nvp->v_type != VREG) {
7785 /*
7786 * Purge the name cache of all references to this vnode
7787 * so that we can check the reference count to infer
7788 * whether it is active or not.
7789 */
7790 if (nvp->v_count > 1)
7791 dnlc_purge_vp(nvp);
7792
7793 isactive = nvp->v_count > 1;
7794 } else {
7795 mutex_enter(&rp->r_os_lock);
7796 isactive = list_head(&rp->r_open_streams) != NULL;
7797 mutex_exit(&rp->r_os_lock);
7798 }
7799
7800 /*
7801 * If the vnode is active and is not a directory,
7802 * arrange to rename it to a
7803 * temporary file so that it will continue to be
7804 * accessible. This implements the "unlink-open-file"
7805 * semantics for the target of a rename operation.
7806 * Before doing this though, make sure that the
7807 * source and target files are not already the same.
7808 */
7809 if (isactive && nvp->v_type != VDIR) {
7810 /*
7811 * Lookup the source name.
7812 */
7813 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7814
7815 /*
7816 * The source name *should* already exist.
7817 */
7818 if (error) {
7819 VN_RELE(nvp);
7820 nfs_rw_exit(&odrp->r_rwlock);
7821 nfs_rw_exit(&ndrp->r_rwlock);
7822 return (error);
7823 }
7824
7825 ASSERT(nfs4_consistent_type(ovp));
7826
7827 /*
7828 * Compare the two vnodes. If they are the same,
7829 * just release all held vnodes and return success.
7830 */
7831 if (VN_CMP(ovp, nvp)) {
7832 VN_RELE(ovp);
7833 VN_RELE(nvp);
7834 nfs_rw_exit(&odrp->r_rwlock);
7835 nfs_rw_exit(&ndrp->r_rwlock);
7836 return (0);
7837 }
7838
7839 /*
7840 * Can't mix and match directories and non-
7841 * directories in rename operations. We already
7842 * know that the target is not a directory. If
7843 * the source is a directory, return an error.
7844 */
7845 if (ovp->v_type == VDIR) {
7846 VN_RELE(ovp);
7847 VN_RELE(nvp);
7848 nfs_rw_exit(&odrp->r_rwlock);
7849 nfs_rw_exit(&ndrp->r_rwlock);
7850 return (ENOTDIR);
7851 }
7852 link_call:
7853 /*
7854 * The target file exists, is not the same as
7855 * the source file, and is active. We first
7856 * try to Link it to a temporary filename to
7857 * avoid having the server removing the file
7858 * completely (which could cause data loss to
7859 * the user's POV in the event the Rename fails
7860 * -- see bug 1165874).
7861 */
7862 /*
7863 * The do_link and did_link booleans are
7864 * introduced in the event we get NFS4ERR_FILE_OPEN
7865 * returned for the Rename. Some servers can
7866 * not Rename over an Open file, so they return
7867 * this error. The client needs to Remove the
7868 * newly created Link and do two Renames, just
7869 * as if the server didn't support LINK.
7870 */
7871 tmpname = newname();
7872 error = 0;
7873
7874 if (do_link) {
7875 error = nfs4_link(ndvp, nvp, tmpname, cr,
7876 NULL, 0);
7877 }
7878 if (error == EOPNOTSUPP || !do_link) {
7879 error = nfs4_rename(ndvp, nnm, ndvp, tmpname,
7880 cr, NULL, 0);
7881 did_link = 0;
7882 } else {
7883 did_link = 1;
7884 }
7885 if (error) {
7886 kmem_free(tmpname, MAXNAMELEN);
7887 VN_RELE(ovp);
7888 VN_RELE(nvp);
7889 nfs_rw_exit(&odrp->r_rwlock);
7890 nfs_rw_exit(&ndrp->r_rwlock);
7891 return (error);
7892 }
7893
7894 mutex_enter(&rp->r_statelock);
7895 if (rp->r_unldvp == NULL) {
7896 VN_HOLD(ndvp);
7897 rp->r_unldvp = ndvp;
7898 if (rp->r_unlcred != NULL)
7899 crfree(rp->r_unlcred);
7900 crhold(cr);
7901 rp->r_unlcred = cr;
7902 rp->r_unlname = tmpname;
7903 } else {
7904 if (rp->r_unlname)
7905 kmem_free(rp->r_unlname, MAXNAMELEN);
7906 rp->r_unlname = tmpname;
7907 }
7908 mutex_exit(&rp->r_statelock);
7909 }
7910
7911 (void) nfs4delegreturn(VTOR4(nvp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7912
7913 ASSERT(nfs4_consistent_type(nvp));
7914 }
7915
7916 if (ovp == NULL) {
7917 /*
7918 * When renaming directories to be a subdirectory of a
7919 * different parent, the dnlc entry for ".." will no
7920 * longer be valid, so it must be removed.
7921 *
7922 * We do a lookup here to determine whether we are renaming
7923 * a directory and we need to check if we are renaming
7924 * an unlinked file. This might have already been done
7925 * in previous code, so we check ovp == NULL to avoid
7926 * doing it twice.
7927 */
7928 error = nfs4lookup(odvp, onm, &ovp, cr, 0);
7929 /*
7930 * The source name *should* already exist.
7931 */
7932 if (error) {
7933 nfs_rw_exit(&odrp->r_rwlock);
7934 nfs_rw_exit(&ndrp->r_rwlock);
7935 if (nvp) {
7936 VN_RELE(nvp);
7937 }
7938 return (error);
7939 }
7940 ASSERT(ovp != NULL);
7941 ASSERT(nfs4_consistent_type(ovp));
7942 }
7943
7944 /*
7945 * Is the object being renamed a dir, and if so, is
7946 * it being renamed to a child of itself? The underlying
7947 * fs should ultimately return EINVAL for this case;
7948 * however, buggy beta non-Solaris NFSv4 servers at
7949 * interop testing events have allowed this behavior,
7950 * and it caused our client to panic due to a recursive
7951 * mutex_enter in fn_move.
7952 *
7953 * The tedious locking in fn_move could be changed to
7954 * deal with this case, and the client could avoid the
7955 * panic; however, the client would just confuse itself
7956 * later and misbehave. A better way to handle the broken
7957 * server is to detect this condition and return EINVAL
7958 * without ever sending the the bogus rename to the server.
7959 * We know the rename is invalid -- just fail it now.
7960 */
7961 if (ovp->v_type == VDIR && VN_CMP(ndvp, ovp)) {
7962 VN_RELE(ovp);
7963 nfs_rw_exit(&odrp->r_rwlock);
7964 nfs_rw_exit(&ndrp->r_rwlock);
7965 if (nvp) {
7966 VN_RELE(nvp);
7967 }
7968 return (EINVAL);
7969 }
7970
7971 (void) nfs4delegreturn(VTOR4(ovp), NFS4_DR_PUSH|NFS4_DR_REOPEN);
7972
7973 /*
7974 * If FH4_VOL_RENAME or FH4_VOLATILE_ANY bits are set, it is
7975 * possible for the filehandle to change due to the rename.
7976 * If neither of these bits is set, but FH4_VOL_MIGRATION is set,
7977 * the fh will not change because of the rename, but we still need
7978 * to update its rnode entry with the new name for
7979 * an eventual fh change due to migration. The FH4_NOEXPIRE_ON_OPEN
7980 * has no effect on these for now, but for future improvements,
7981 * we might want to use it too to simplify handling of files
7982 * that are open with that flag on. (XXX)
7983 */
7984 mi = VTOMI4(odvp);
7985 if (NFS4_VOLATILE_FH(mi))
7986 error = nfs4rename_volatile_fh(odvp, onm, ovp, ndvp, nnm, cr,
7987 &stat);
7988 else
7989 error = nfs4rename_persistent_fh(odvp, onm, ovp, ndvp, nnm, cr,
7990 &stat);
7991
7992 ASSERT(nfs4_consistent_type(odvp));
7993 ASSERT(nfs4_consistent_type(ndvp));
7994 ASSERT(nfs4_consistent_type(ovp));
7995
7996 if (stat == NFS4ERR_FILE_OPEN && did_link) {
7997 do_link = 0;
7998 /*
7999 * Before the 'link_call' code, we did a nfs4_lookup
8000 * that puts a VN_HOLD on nvp. After the nfs4_link
8001 * call we call VN_RELE to match that hold. We need
8002 * to place an additional VN_HOLD here since we will
8003 * be hitting that VN_RELE again.
8004 */
8005 VN_HOLD(nvp);
8006
8007 (void) nfs4_remove(ndvp, tmpname, cr, NULL, 0);
8008
8009 /* Undo the unlinked file naming stuff we just did */
8010 mutex_enter(&rp->r_statelock);
8011 if (rp->r_unldvp) {
8012 VN_RELE(ndvp);
8013 rp->r_unldvp = NULL;
8014 if (rp->r_unlcred != NULL)
8015 crfree(rp->r_unlcred);
8016 rp->r_unlcred = NULL;
8017 /* rp->r_unlanme points to tmpname */
8018 if (rp->r_unlname)
8019 kmem_free(rp->r_unlname, MAXNAMELEN);
8020 rp->r_unlname = NULL;
8021 }
8022 mutex_exit(&rp->r_statelock);
8023
8024 if (nvp) {
8025 VN_RELE(nvp);
8026 }
8027 goto link_call;
8028 }
8029
8030 if (error) {
8031 VN_RELE(ovp);
8032 nfs_rw_exit(&odrp->r_rwlock);
8033 nfs_rw_exit(&ndrp->r_rwlock);
8034 if (nvp) {
8035 VN_RELE(nvp);
8036 }
8037 return (error);
8038 }
8039
8040 /*
8041 * when renaming directories to be a subdirectory of a
8042 * different parent, the dnlc entry for ".." will no
8043 * longer be valid, so it must be removed
8044 */
8045 rp = VTOR4(ovp);
8046 if (ndvp != odvp) {
8047 if (ovp->v_type == VDIR) {
8048 dnlc_remove(ovp, "..");
8049 if (rp->r_dir != NULL)
8050 nfs4_purge_rddir_cache(ovp);
8051 }
8052 }
8053
8054 /*
8055 * If we are renaming the unlinked file, update the
8056 * r_unldvp and r_unlname as needed.
8057 */
8058 mutex_enter(&rp->r_statelock);
8059 if (rp->r_unldvp != NULL) {
8060 if (strcmp(rp->r_unlname, onm) == 0) {
8061 (void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
8062 rp->r_unlname[MAXNAMELEN - 1] = '\0';
8063 if (ndvp != rp->r_unldvp) {
8064 VN_RELE(rp->r_unldvp);
8065 rp->r_unldvp = ndvp;
8066 VN_HOLD(ndvp);
8067 }
8068 }
8069 }
8070 mutex_exit(&rp->r_statelock);
8071
8072 /*
8073 * Notify the rename vnevents to source vnode, and to the target
8074 * vnode if it already existed.
8075 */
8076 if (error == 0) {
8077 vnode_t *tvp;
8078 rnode4_t *trp;
8079 /*
8080 * Notify the vnode. Each links is represented by
8081 * a different vnode, in nfsv4.
8082 */
8083 if (nvp) {
8084 trp = VTOR4(nvp);
8085 tvp = nvp;
8086 if (IS_SHADOW(nvp, trp))
8087 tvp = RTOV4(trp);
8088 vnevent_rename_dest(tvp, ndvp, nnm, ct);
8089 }
8090
8091 /*
8092 * if the source and destination directory are not the
8093 * same notify the destination directory.
8094 */
8095 if (VTOR4(odvp) != VTOR4(ndvp)) {
8096 trp = VTOR4(ndvp);
8097 tvp = ndvp;
8098 if (IS_SHADOW(ndvp, trp))
8099 tvp = RTOV4(trp);
8100 vnevent_rename_dest_dir(tvp, ct);
8101 }
8102
8103 trp = VTOR4(ovp);
8104 tvp = ovp;
8105 if (IS_SHADOW(ovp, trp))
8106 tvp = RTOV4(trp);
8107 vnevent_rename_src(tvp, odvp, onm, ct);
8108 }
8109
8110 if (nvp) {
8111 VN_RELE(nvp);
8112 }
8113 VN_RELE(ovp);
8114
8115 nfs_rw_exit(&odrp->r_rwlock);
8116 nfs_rw_exit(&ndrp->r_rwlock);
8117
8118 return (error);
8119 }
8120
8121 /*
8122 * When the parent directory has changed, sv_dfh must be updated
8123 */
8124 static void
update_parentdir_sfh(vnode_t * vp,vnode_t * ndvp)8125 update_parentdir_sfh(vnode_t *vp, vnode_t *ndvp)
8126 {
8127 svnode_t *sv = VTOSV(vp);
8128 nfs4_sharedfh_t *old_dfh = sv->sv_dfh;
8129 nfs4_sharedfh_t *new_dfh = VTOR4(ndvp)->r_fh;
8130
8131 sfh4_hold(new_dfh);
8132 sv->sv_dfh = new_dfh;
8133 sfh4_rele(&old_dfh);
8134 }
8135
8136 /*
8137 * nfs4rename_persistent does the otw portion of renaming in NFS Version 4,
8138 * when it is known that the filehandle is persistent through rename.
8139 *
8140 * Rename requires that the current fh be the target directory and the
8141 * saved fh be the source directory. After the operation, the current fh
8142 * is unchanged.
8143 * The compound op structure for persistent fh rename is:
8144 * PUTFH(sourcdir), SAVEFH, PUTFH(targetdir), RENAME
8145 * Rather than bother with the directory postop args, we'll simply
8146 * update that a change occurred in the cache, so no post-op getattrs.
8147 */
8148 static int
nfs4rename_persistent_fh(vnode_t * odvp,char * onm,vnode_t * renvp,vnode_t * ndvp,char * nnm,cred_t * cr,nfsstat4 * statp)8149 nfs4rename_persistent_fh(vnode_t *odvp, char *onm, vnode_t *renvp,
8150 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8151 {
8152 COMPOUND4args_clnt args;
8153 COMPOUND4res_clnt res, *resp = NULL;
8154 nfs_argop4 *argop;
8155 nfs_resop4 *resop;
8156 int doqueue, argoplist_size;
8157 mntinfo4_t *mi;
8158 rnode4_t *odrp = VTOR4(odvp);
8159 rnode4_t *ndrp = VTOR4(ndvp);
8160 RENAME4res *rn_res;
8161 bool_t needrecov;
8162 nfs4_recov_state_t recov_state;
8163 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8164 dirattr_info_t dinfo, *dinfop;
8165
8166 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8167
8168 recov_state.rs_flags = 0;
8169 recov_state.rs_num_retry_despite_err = 0;
8170
8171 /*
8172 * Rename ops: putfh sdir; savefh; putfh tdir; rename; getattr tdir
8173 *
8174 * If source/target are different dirs, then append putfh(src); getattr
8175 */
8176 args.array_len = (odvp == ndvp) ? 5 : 7;
8177 argoplist_size = args.array_len * sizeof (nfs_argop4);
8178 args.array = argop = kmem_alloc(argoplist_size, KM_SLEEP);
8179
8180 recov_retry:
8181 *statp = NFS4_OK;
8182
8183 /* No need to Lookup the file, persistent fh */
8184 args.ctag = TAG_RENAME;
8185
8186 mi = VTOMI4(odvp);
8187 e.error = nfs4_start_op(mi, odvp, ndvp, &recov_state);
8188 if (e.error) {
8189 kmem_free(argop, argoplist_size);
8190 return (e.error);
8191 }
8192
8193 /* 0: putfh source directory */
8194 argop[0].argop = OP_CPUTFH;
8195 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8196
8197 /* 1: Save source fh to free up current for target */
8198 argop[1].argop = OP_SAVEFH;
8199
8200 /* 2: putfh targetdir */
8201 argop[2].argop = OP_CPUTFH;
8202 argop[2].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8203
8204 /* 3: current_fh is targetdir, saved_fh is sourcedir */
8205 argop[3].argop = OP_CRENAME;
8206 argop[3].nfs_argop4_u.opcrename.coldname = onm;
8207 argop[3].nfs_argop4_u.opcrename.cnewname = nnm;
8208
8209 /* 4: getattr (targetdir) */
8210 argop[4].argop = OP_GETATTR;
8211 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8212 argop[4].nfs_argop4_u.opgetattr.mi = mi;
8213
8214 if (ndvp != odvp) {
8215
8216 /* 5: putfh (sourcedir) */
8217 argop[5].argop = OP_CPUTFH;
8218 argop[5].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8219
8220 /* 6: getattr (sourcedir) */
8221 argop[6].argop = OP_GETATTR;
8222 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8223 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8224 }
8225
8226 dnlc_remove(odvp, onm);
8227 dnlc_remove(ndvp, nnm);
8228
8229 doqueue = 1;
8230 dinfo.di_time_call = gethrtime();
8231 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8232
8233 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8234 if (e.error) {
8235 PURGE_ATTRCACHE4(odvp);
8236 PURGE_ATTRCACHE4(ndvp);
8237 } else {
8238 *statp = res.status;
8239 }
8240
8241 if (needrecov) {
8242 if (nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8243 OP_RENAME, NULL, NULL, NULL) == FALSE) {
8244 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8245 if (!e.error)
8246 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8247 goto recov_retry;
8248 }
8249 }
8250
8251 if (!e.error) {
8252 resp = &res;
8253 /*
8254 * as long as OP_RENAME
8255 */
8256 if (res.status != NFS4_OK && res.array_len <= 4) {
8257 e.error = geterrno4(res.status);
8258 PURGE_ATTRCACHE4(odvp);
8259 PURGE_ATTRCACHE4(ndvp);
8260 /*
8261 * System V defines rename to return EEXIST, not
8262 * ENOTEMPTY if the target directory is not empty.
8263 * Over the wire, the error is NFSERR_ENOTEMPTY
8264 * which geterrno4 maps to ENOTEMPTY.
8265 */
8266 if (e.error == ENOTEMPTY)
8267 e.error = EEXIST;
8268 } else {
8269
8270 resop = &res.array[3]; /* rename res */
8271 rn_res = &resop->nfs_resop4_u.oprename;
8272
8273 if (res.status == NFS4_OK) {
8274 /*
8275 * Update target attribute, readdir and dnlc
8276 * caches.
8277 */
8278 dinfo.di_garp =
8279 &res.array[4].nfs_resop4_u.opgetattr.ga_res;
8280 dinfo.di_cred = cr;
8281 dinfop = &dinfo;
8282 } else
8283 dinfop = NULL;
8284
8285 nfs4_update_dircaches(&rn_res->target_cinfo,
8286 ndvp, NULL, NULL, dinfop);
8287
8288 /*
8289 * Update source attribute, readdir and dnlc caches
8290 *
8291 */
8292 if (ndvp != odvp) {
8293 update_parentdir_sfh(renvp, ndvp);
8294
8295 if (dinfop)
8296 dinfo.di_garp =
8297 &(res.array[6].nfs_resop4_u.
8298 opgetattr.ga_res);
8299
8300 nfs4_update_dircaches(&rn_res->source_cinfo,
8301 odvp, NULL, NULL, dinfop);
8302 }
8303
8304 fn_move(VTOSV(renvp)->sv_name, VTOSV(ndvp)->sv_name,
8305 nnm);
8306 }
8307 }
8308
8309 if (resp)
8310 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8311 nfs4_end_op(mi, odvp, ndvp, &recov_state, needrecov);
8312 kmem_free(argop, argoplist_size);
8313
8314 return (e.error);
8315 }
8316
8317 /*
8318 * nfs4rename_volatile_fh does the otw part of renaming in NFS Version 4, when
8319 * it is possible for the filehandle to change due to the rename.
8320 *
8321 * The compound req in this case includes a post-rename lookup and getattr
8322 * to ensure that we have the correct fh and attributes for the object.
8323 *
8324 * Rename requires that the current fh be the target directory and the
8325 * saved fh be the source directory. After the operation, the current fh
8326 * is unchanged.
8327 *
8328 * We need the new filehandle (hence a LOOKUP and GETFH) so that we can
8329 * update the filehandle for the renamed object. We also get the old
8330 * filehandle for historical reasons; this should be taken out sometime.
8331 * This results in a rather cumbersome compound...
8332 *
8333 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8334 * PUTFH(targetdir), RENAME, LOOKUP(trgt), GETFH(new), GETATTR
8335 *
8336 */
8337 static int
nfs4rename_volatile_fh(vnode_t * odvp,char * onm,vnode_t * ovp,vnode_t * ndvp,char * nnm,cred_t * cr,nfsstat4 * statp)8338 nfs4rename_volatile_fh(vnode_t *odvp, char *onm, vnode_t *ovp,
8339 vnode_t *ndvp, char *nnm, cred_t *cr, nfsstat4 *statp)
8340 {
8341 COMPOUND4args_clnt args;
8342 COMPOUND4res_clnt res, *resp = NULL;
8343 int argoplist_size;
8344 nfs_argop4 *argop;
8345 nfs_resop4 *resop;
8346 int doqueue;
8347 mntinfo4_t *mi;
8348 rnode4_t *odrp = VTOR4(odvp); /* old directory */
8349 rnode4_t *ndrp = VTOR4(ndvp); /* new directory */
8350 rnode4_t *orp = VTOR4(ovp); /* object being renamed */
8351 RENAME4res *rn_res;
8352 GETFH4res *ngf_res;
8353 bool_t needrecov;
8354 nfs4_recov_state_t recov_state;
8355 hrtime_t t;
8356 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8357 dirattr_info_t dinfo, *dinfop = &dinfo;
8358
8359 ASSERT(nfs_zone() == VTOMI4(odvp)->mi_zone);
8360
8361 recov_state.rs_flags = 0;
8362 recov_state.rs_num_retry_despite_err = 0;
8363
8364 recov_retry:
8365 *statp = NFS4_OK;
8366
8367 /*
8368 * There is a window between the RPC and updating the path and
8369 * filehandle stored in the rnode. Lock out the FHEXPIRED recovery
8370 * code, so that it doesn't try to use the old path during that
8371 * window.
8372 */
8373 mutex_enter(&orp->r_statelock);
8374 while (orp->r_flags & R4RECEXPFH) {
8375 klwp_t *lwp = ttolwp(curthread);
8376
8377 if (lwp != NULL)
8378 lwp->lwp_nostop++;
8379 if (cv_wait_sig(&orp->r_cv, &orp->r_statelock) == 0) {
8380 mutex_exit(&orp->r_statelock);
8381 if (lwp != NULL)
8382 lwp->lwp_nostop--;
8383 return (EINTR);
8384 }
8385 if (lwp != NULL)
8386 lwp->lwp_nostop--;
8387 }
8388 orp->r_flags |= R4RECEXPFH;
8389 mutex_exit(&orp->r_statelock);
8390
8391 mi = VTOMI4(odvp);
8392
8393 args.ctag = TAG_RENAME_VFH;
8394 args.array_len = (odvp == ndvp) ? 10 : 12;
8395 argoplist_size = args.array_len * sizeof (nfs_argop4);
8396 argop = kmem_alloc(argoplist_size, KM_SLEEP);
8397
8398 /*
8399 * Rename ops:
8400 * PUTFH(sourcdir), SAVEFH, LOOKUP(src), GETFH(old),
8401 * PUTFH(targetdir), RENAME, GETATTR(targetdir)
8402 * LOOKUP(trgt), GETFH(new), GETATTR,
8403 *
8404 * if (odvp != ndvp)
8405 * add putfh(sourcedir), getattr(sourcedir) }
8406 */
8407 args.array = argop;
8408
8409 e.error = nfs4_start_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8410 &recov_state, NULL);
8411 if (e.error) {
8412 kmem_free(argop, argoplist_size);
8413 mutex_enter(&orp->r_statelock);
8414 orp->r_flags &= ~R4RECEXPFH;
8415 cv_broadcast(&orp->r_cv);
8416 mutex_exit(&orp->r_statelock);
8417 return (e.error);
8418 }
8419
8420 /* 0: putfh source directory */
8421 argop[0].argop = OP_CPUTFH;
8422 argop[0].nfs_argop4_u.opcputfh.sfh = odrp->r_fh;
8423
8424 /* 1: Save source fh to free up current for target */
8425 argop[1].argop = OP_SAVEFH;
8426
8427 /* 2: Lookup pre-rename fh of renamed object */
8428 argop[2].argop = OP_CLOOKUP;
8429 argop[2].nfs_argop4_u.opclookup.cname = onm;
8430
8431 /* 3: getfh fh of renamed object (before rename) */
8432 argop[3].argop = OP_GETFH;
8433
8434 /* 4: putfh targetdir */
8435 argop[4].argop = OP_CPUTFH;
8436 argop[4].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8437
8438 /* 5: current_fh is targetdir, saved_fh is sourcedir */
8439 argop[5].argop = OP_CRENAME;
8440 argop[5].nfs_argop4_u.opcrename.coldname = onm;
8441 argop[5].nfs_argop4_u.opcrename.cnewname = nnm;
8442
8443 /* 6: getattr of target dir (post op attrs) */
8444 argop[6].argop = OP_GETATTR;
8445 argop[6].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8446 argop[6].nfs_argop4_u.opgetattr.mi = mi;
8447
8448 /* 7: Lookup post-rename fh of renamed object */
8449 argop[7].argop = OP_CLOOKUP;
8450 argop[7].nfs_argop4_u.opclookup.cname = nnm;
8451
8452 /* 8: getfh fh of renamed object (after rename) */
8453 argop[8].argop = OP_GETFH;
8454
8455 /* 9: getattr of renamed object */
8456 argop[9].argop = OP_GETATTR;
8457 argop[9].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8458 argop[9].nfs_argop4_u.opgetattr.mi = mi;
8459
8460 /*
8461 * If source/target dirs are different, then get new post-op
8462 * attrs for source dir also.
8463 */
8464 if (ndvp != odvp) {
8465 /* 10: putfh (sourcedir) */
8466 argop[10].argop = OP_CPUTFH;
8467 argop[10].nfs_argop4_u.opcputfh.sfh = ndrp->r_fh;
8468
8469 /* 11: getattr (sourcedir) */
8470 argop[11].argop = OP_GETATTR;
8471 argop[11].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8472 argop[11].nfs_argop4_u.opgetattr.mi = mi;
8473 }
8474
8475 dnlc_remove(odvp, onm);
8476 dnlc_remove(ndvp, nnm);
8477
8478 doqueue = 1;
8479 t = gethrtime();
8480 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8481
8482 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8483 if (e.error) {
8484 PURGE_ATTRCACHE4(odvp);
8485 PURGE_ATTRCACHE4(ndvp);
8486 if (!needrecov) {
8487 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8488 &recov_state, needrecov);
8489 goto out;
8490 }
8491 } else {
8492 *statp = res.status;
8493 }
8494
8495 if (needrecov) {
8496 bool_t abort;
8497
8498 abort = nfs4_start_recovery(&e, mi, odvp, ndvp, NULL, NULL,
8499 OP_RENAME, NULL, NULL, NULL);
8500 if (abort == FALSE) {
8501 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8502 &recov_state, needrecov);
8503 kmem_free(argop, argoplist_size);
8504 if (!e.error)
8505 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8506 mutex_enter(&orp->r_statelock);
8507 orp->r_flags &= ~R4RECEXPFH;
8508 cv_broadcast(&orp->r_cv);
8509 mutex_exit(&orp->r_statelock);
8510 goto recov_retry;
8511 } else {
8512 if (e.error != 0) {
8513 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME,
8514 &recov_state, needrecov);
8515 goto out;
8516 }
8517 /* fall through for res.status case */
8518 }
8519 }
8520
8521 resp = &res;
8522 /*
8523 * If OP_RENAME (or any prev op) failed, then return an error.
8524 * OP_RENAME is index 5, so if array len <= 6 we return an error.
8525 */
8526 if ((res.status != NFS4_OK) && (res.array_len <= 6)) {
8527 /*
8528 * Error in an op other than last Getattr
8529 */
8530 e.error = geterrno4(res.status);
8531 PURGE_ATTRCACHE4(odvp);
8532 PURGE_ATTRCACHE4(ndvp);
8533 /*
8534 * System V defines rename to return EEXIST, not
8535 * ENOTEMPTY if the target directory is not empty.
8536 * Over the wire, the error is NFSERR_ENOTEMPTY
8537 * which geterrno4 maps to ENOTEMPTY.
8538 */
8539 if (e.error == ENOTEMPTY)
8540 e.error = EEXIST;
8541 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state,
8542 needrecov);
8543 goto out;
8544 }
8545
8546 /* rename results */
8547 rn_res = &res.array[5].nfs_resop4_u.oprename;
8548
8549 if (res.status == NFS4_OK) {
8550 /* Update target attribute, readdir and dnlc caches */
8551 dinfo.di_garp =
8552 &res.array[6].nfs_resop4_u.opgetattr.ga_res;
8553 dinfo.di_cred = cr;
8554 dinfo.di_time_call = t;
8555 } else
8556 dinfop = NULL;
8557
8558 /* Update source cache attribute, readdir and dnlc caches */
8559 nfs4_update_dircaches(&rn_res->target_cinfo, ndvp, NULL, NULL, dinfop);
8560
8561 /* Update source cache attribute, readdir and dnlc caches */
8562 if (ndvp != odvp) {
8563 update_parentdir_sfh(ovp, ndvp);
8564
8565 /*
8566 * If dinfop is non-NULL, then compound succeded, so
8567 * set di_garp to attrs for source dir. dinfop is only
8568 * set to NULL when compound fails.
8569 */
8570 if (dinfop)
8571 dinfo.di_garp =
8572 &res.array[11].nfs_resop4_u.opgetattr.ga_res;
8573 nfs4_update_dircaches(&rn_res->source_cinfo, odvp, NULL, NULL,
8574 dinfop);
8575 }
8576
8577 /*
8578 * Update the rnode with the new component name and args,
8579 * and if the file handle changed, also update it with the new fh.
8580 * This is only necessary if the target object has an rnode
8581 * entry and there is no need to create one for it.
8582 */
8583 resop = &res.array[8]; /* getfh new res */
8584 ngf_res = &resop->nfs_resop4_u.opgetfh;
8585
8586 /*
8587 * Update the path and filehandle for the renamed object.
8588 */
8589 nfs4rename_update(ovp, ndvp, &ngf_res->object, nnm);
8590
8591 nfs4_end_fop(mi, odvp, ndvp, OH_VFH_RENAME, &recov_state, needrecov);
8592
8593 if (res.status == NFS4_OK) {
8594 resop++; /* getattr res */
8595 e.error = nfs4_update_attrcache(res.status,
8596 &resop->nfs_resop4_u.opgetattr.ga_res,
8597 t, ovp, cr);
8598 }
8599
8600 out:
8601 kmem_free(argop, argoplist_size);
8602 if (resp)
8603 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8604 mutex_enter(&orp->r_statelock);
8605 orp->r_flags &= ~R4RECEXPFH;
8606 cv_broadcast(&orp->r_cv);
8607 mutex_exit(&orp->r_statelock);
8608
8609 return (e.error);
8610 }
8611
8612 /* ARGSUSED */
8613 static int
nfs4_mkdir(vnode_t * dvp,char * nm,struct vattr * va,vnode_t ** vpp,cred_t * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)8614 nfs4_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
8615 caller_context_t *ct, int flags, vsecattr_t *vsecp)
8616 {
8617 int error;
8618 vnode_t *vp;
8619
8620 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8621 return (EPERM);
8622 /*
8623 * As ".." has special meaning and rather than send a mkdir
8624 * over the wire to just let the server freak out, we just
8625 * short circuit it here and return EEXIST
8626 */
8627 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8628 return (EEXIST);
8629
8630 /*
8631 * Decision to get the right gid and setgid bit of the
8632 * new directory is now made in call_nfs4_create_req.
8633 */
8634 va->va_mask |= AT_MODE;
8635 error = call_nfs4_create_req(dvp, nm, NULL, va, &vp, cr, NF4DIR);
8636 if (error)
8637 return (error);
8638
8639 *vpp = vp;
8640 return (0);
8641 }
8642
8643
8644 /*
8645 * rmdir is using the same remove v4 op as does remove.
8646 * Remove requires that the current fh be the target directory.
8647 * After the operation, the current fh is unchanged.
8648 * The compound op structure is:
8649 * PUTFH(targetdir), REMOVE
8650 */
8651 /*ARGSUSED4*/
8652 static int
nfs4_rmdir(vnode_t * dvp,char * nm,vnode_t * cdir,cred_t * cr,caller_context_t * ct,int flags)8653 nfs4_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
8654 caller_context_t *ct, int flags)
8655 {
8656 int need_end_op = FALSE;
8657 COMPOUND4args_clnt args;
8658 COMPOUND4res_clnt res, *resp = NULL;
8659 REMOVE4res *rm_res;
8660 nfs_argop4 argop[3];
8661 nfs_resop4 *resop;
8662 vnode_t *vp;
8663 int doqueue;
8664 mntinfo4_t *mi;
8665 rnode4_t *drp;
8666 bool_t needrecov = FALSE;
8667 nfs4_recov_state_t recov_state;
8668 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
8669 dirattr_info_t dinfo, *dinfop;
8670
8671 if (nfs_zone() != VTOMI4(dvp)->mi_zone)
8672 return (EPERM);
8673 /*
8674 * As ".." has special meaning and rather than send a rmdir
8675 * over the wire to just let the server freak out, we just
8676 * short circuit it here and return EEXIST
8677 */
8678 if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0')
8679 return (EEXIST);
8680
8681 drp = VTOR4(dvp);
8682 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR4(dvp)))
8683 return (EINTR);
8684
8685 /*
8686 * Attempt to prevent a rmdir(".") from succeeding.
8687 */
8688 e.error = nfs4lookup(dvp, nm, &vp, cr, 0);
8689 if (e.error) {
8690 nfs_rw_exit(&drp->r_rwlock);
8691 return (e.error);
8692 }
8693 if (vp == cdir) {
8694 VN_RELE(vp);
8695 nfs_rw_exit(&drp->r_rwlock);
8696 return (EINVAL);
8697 }
8698
8699 /*
8700 * Since nfsv4 remove op works on both files and directories,
8701 * check that the removed object is indeed a directory.
8702 */
8703 if (vp->v_type != VDIR) {
8704 VN_RELE(vp);
8705 nfs_rw_exit(&drp->r_rwlock);
8706 return (ENOTDIR);
8707 }
8708
8709 /*
8710 * First just remove the entry from the name cache, as it
8711 * is most likely an entry for this vp.
8712 */
8713 dnlc_remove(dvp, nm);
8714
8715 /*
8716 * If there vnode reference count is greater than one, then
8717 * there may be additional references in the DNLC which will
8718 * need to be purged. First, trying removing the entry for
8719 * the parent directory and see if that removes the additional
8720 * reference(s). If that doesn't do it, then use dnlc_purge_vp
8721 * to completely remove any references to the directory which
8722 * might still exist in the DNLC.
8723 */
8724 if (vp->v_count > 1) {
8725 dnlc_remove(vp, "..");
8726 if (vp->v_count > 1)
8727 dnlc_purge_vp(vp);
8728 }
8729
8730 mi = VTOMI4(dvp);
8731 recov_state.rs_flags = 0;
8732 recov_state.rs_num_retry_despite_err = 0;
8733
8734 recov_retry:
8735 args.ctag = TAG_RMDIR;
8736
8737 /*
8738 * Rmdir ops: putfh dir; remove
8739 */
8740 args.array_len = 3;
8741 args.array = argop;
8742
8743 e.error = nfs4_start_op(VTOMI4(dvp), dvp, NULL, &recov_state);
8744 if (e.error) {
8745 nfs_rw_exit(&drp->r_rwlock);
8746 return (e.error);
8747 }
8748 need_end_op = TRUE;
8749
8750 /* putfh directory */
8751 argop[0].argop = OP_CPUTFH;
8752 argop[0].nfs_argop4_u.opcputfh.sfh = drp->r_fh;
8753
8754 /* remove */
8755 argop[1].argop = OP_CREMOVE;
8756 argop[1].nfs_argop4_u.opcremove.ctarget = nm;
8757
8758 /* getattr (postop attrs for dir that contained removed dir) */
8759 argop[2].argop = OP_GETATTR;
8760 argop[2].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
8761 argop[2].nfs_argop4_u.opgetattr.mi = mi;
8762
8763 dinfo.di_time_call = gethrtime();
8764 doqueue = 1;
8765 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
8766
8767 PURGE_ATTRCACHE4(vp);
8768
8769 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
8770 if (e.error) {
8771 PURGE_ATTRCACHE4(dvp);
8772 }
8773
8774 if (needrecov) {
8775 if (nfs4_start_recovery(&e, VTOMI4(dvp), dvp, NULL, NULL,
8776 NULL, OP_REMOVE, NULL, NULL, NULL) == FALSE) {
8777 if (!e.error)
8778 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
8779
8780 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state,
8781 needrecov);
8782 need_end_op = FALSE;
8783 goto recov_retry;
8784 }
8785 }
8786
8787 if (!e.error) {
8788 resp = &res;
8789
8790 /*
8791 * Only return error if first 2 ops (OP_REMOVE or earlier)
8792 * failed.
8793 */
8794 if (res.status != NFS4_OK && res.array_len <= 2) {
8795 e.error = geterrno4(res.status);
8796 PURGE_ATTRCACHE4(dvp);
8797 nfs4_end_op(VTOMI4(dvp), dvp, NULL,
8798 &recov_state, needrecov);
8799 need_end_op = FALSE;
8800 nfs4_purge_stale_fh(e.error, dvp, cr);
8801 /*
8802 * System V defines rmdir to return EEXIST, not
8803 * ENOTEMPTY if the directory is not empty. Over
8804 * the wire, the error is NFSERR_ENOTEMPTY which
8805 * geterrno4 maps to ENOTEMPTY.
8806 */
8807 if (e.error == ENOTEMPTY)
8808 e.error = EEXIST;
8809 } else {
8810 resop = &res.array[1]; /* remove res */
8811 rm_res = &resop->nfs_resop4_u.opremove;
8812
8813 if (res.status == NFS4_OK) {
8814 resop = &res.array[2]; /* dir attrs */
8815 dinfo.di_garp =
8816 &resop->nfs_resop4_u.opgetattr.ga_res;
8817 dinfo.di_cred = cr;
8818 dinfop = &dinfo;
8819 } else
8820 dinfop = NULL;
8821
8822 /* Update dir attribute, readdir and dnlc caches */
8823 nfs4_update_dircaches(&rm_res->cinfo, dvp, NULL, NULL,
8824 dinfop);
8825
8826 /* destroy rddir cache for dir that was removed */
8827 if (VTOR4(vp)->r_dir != NULL)
8828 nfs4_purge_rddir_cache(vp);
8829 }
8830 }
8831
8832 if (need_end_op)
8833 nfs4_end_op(VTOMI4(dvp), dvp, NULL, &recov_state, needrecov);
8834
8835 nfs_rw_exit(&drp->r_rwlock);
8836
8837 if (resp)
8838 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
8839
8840 if (e.error == 0) {
8841 vnode_t *tvp;
8842 rnode4_t *trp;
8843 trp = VTOR4(vp);
8844 tvp = vp;
8845 if (IS_SHADOW(vp, trp))
8846 tvp = RTOV4(trp);
8847 vnevent_rmdir(tvp, dvp, nm, ct);
8848 }
8849
8850 VN_RELE(vp);
8851
8852 return (e.error);
8853 }
8854
8855 /* ARGSUSED */
8856 static int
nfs4_symlink(vnode_t * dvp,char * lnm,struct vattr * tva,char * tnm,cred_t * cr,caller_context_t * ct,int flags)8857 nfs4_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
8858 caller_context_t *ct, int flags)
8859 {
8860 int error;
8861 vnode_t *vp;
8862 rnode4_t *rp;
8863 char *contents;
8864 mntinfo4_t *mi = VTOMI4(dvp);
8865
8866 if (nfs_zone() != mi->mi_zone)
8867 return (EPERM);
8868 if (!(mi->mi_flags & MI4_SYMLINK))
8869 return (EOPNOTSUPP);
8870
8871 error = call_nfs4_create_req(dvp, lnm, tnm, tva, &vp, cr, NF4LNK);
8872 if (error)
8873 return (error);
8874
8875 ASSERT(nfs4_consistent_type(vp));
8876 rp = VTOR4(vp);
8877 if (nfs4_do_symlink_cache && rp->r_symlink.contents == NULL) {
8878
8879 contents = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8880
8881 if (contents != NULL) {
8882 mutex_enter(&rp->r_statelock);
8883 if (rp->r_symlink.contents == NULL) {
8884 rp->r_symlink.len = strlen(tnm);
8885 bcopy(tnm, contents, rp->r_symlink.len);
8886 rp->r_symlink.contents = contents;
8887 rp->r_symlink.size = MAXPATHLEN;
8888 mutex_exit(&rp->r_statelock);
8889 } else {
8890 mutex_exit(&rp->r_statelock);
8891 kmem_free((void *)contents, MAXPATHLEN);
8892 }
8893 }
8894 }
8895 VN_RELE(vp);
8896
8897 return (error);
8898 }
8899
8900
8901 /*
8902 * Read directory entries.
8903 * There are some weird things to look out for here. The uio_loffset
8904 * field is either 0 or it is the offset returned from a previous
8905 * readdir. It is an opaque value used by the server to find the
8906 * correct directory block to read. The count field is the number
8907 * of blocks to read on the server. This is advisory only, the server
8908 * may return only one block's worth of entries. Entries may be compressed
8909 * on the server.
8910 */
8911 /* ARGSUSED */
8912 static int
nfs4_readdir(vnode_t * vp,struct uio * uiop,cred_t * cr,int * eofp,caller_context_t * ct,int flags)8913 nfs4_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
8914 caller_context_t *ct, int flags)
8915 {
8916 int error;
8917 uint_t count;
8918 rnode4_t *rp;
8919 rddir4_cache *rdc;
8920 rddir4_cache *rrdc;
8921
8922 if (nfs_zone() != VTOMI4(vp)->mi_zone)
8923 return (EIO);
8924 rp = VTOR4(vp);
8925
8926 ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
8927
8928 /*
8929 * Make sure that the directory cache is valid.
8930 */
8931 if (rp->r_dir != NULL) {
8932 if (nfs_disable_rddir_cache != 0) {
8933 /*
8934 * Setting nfs_disable_rddir_cache in /etc/system
8935 * allows interoperability with servers that do not
8936 * properly update the attributes of directories.
8937 * Any cached information gets purged before an
8938 * access is made to it.
8939 */
8940 nfs4_purge_rddir_cache(vp);
8941 }
8942
8943 error = nfs4_validate_caches(vp, cr);
8944 if (error)
8945 return (error);
8946 }
8947
8948 count = MIN(uiop->uio_iov->iov_len, MAXBSIZE);
8949
8950 /*
8951 * Short circuit last readdir which always returns 0 bytes.
8952 * This can be done after the directory has been read through
8953 * completely at least once. This will set r_direof which
8954 * can be used to find the value of the last cookie.
8955 */
8956 mutex_enter(&rp->r_statelock);
8957 if (rp->r_direof != NULL &&
8958 uiop->uio_loffset == rp->r_direof->nfs4_ncookie) {
8959 mutex_exit(&rp->r_statelock);
8960 #ifdef DEBUG
8961 nfs4_readdir_cache_shorts++;
8962 #endif
8963 if (eofp)
8964 *eofp = 1;
8965 return (0);
8966 }
8967
8968 /*
8969 * Look for a cache entry. Cache entries are identified
8970 * by the NFS cookie value and the byte count requested.
8971 */
8972 rdc = rddir4_cache_lookup(rp, uiop->uio_loffset, count);
8973
8974 /*
8975 * If rdc is NULL then the lookup resulted in an unrecoverable error.
8976 */
8977 if (rdc == NULL) {
8978 mutex_exit(&rp->r_statelock);
8979 return (EINTR);
8980 }
8981
8982 /*
8983 * Check to see if we need to fill this entry in.
8984 */
8985 if (rdc->flags & RDDIRREQ) {
8986 rdc->flags &= ~RDDIRREQ;
8987 rdc->flags |= RDDIR;
8988 mutex_exit(&rp->r_statelock);
8989
8990 /*
8991 * Do the readdir.
8992 */
8993 nfs4readdir(vp, rdc, cr);
8994
8995 /*
8996 * Reacquire the lock, so that we can continue
8997 */
8998 mutex_enter(&rp->r_statelock);
8999 /*
9000 * The entry is now complete
9001 */
9002 rdc->flags &= ~RDDIR;
9003 }
9004
9005 ASSERT(!(rdc->flags & RDDIR));
9006
9007 /*
9008 * If an error occurred while attempting
9009 * to fill the cache entry, mark the entry invalid and
9010 * just return the error.
9011 */
9012 if (rdc->error) {
9013 error = rdc->error;
9014 rdc->flags |= RDDIRREQ;
9015 rddir4_cache_rele(rp, rdc);
9016 mutex_exit(&rp->r_statelock);
9017 return (error);
9018 }
9019
9020 /*
9021 * The cache entry is complete and good,
9022 * copyout the dirent structs to the calling
9023 * thread.
9024 */
9025 error = uiomove(rdc->entries, rdc->actlen, UIO_READ, uiop);
9026
9027 /*
9028 * If no error occurred during the copyout,
9029 * update the offset in the uio struct to
9030 * contain the value of the next NFS 4 cookie
9031 * and set the eof value appropriately.
9032 */
9033 if (!error) {
9034 uiop->uio_loffset = rdc->nfs4_ncookie;
9035 if (eofp)
9036 *eofp = rdc->eof;
9037 }
9038
9039 /*
9040 * Decide whether to do readahead. Don't if we
9041 * have already read to the end of directory.
9042 */
9043 if (rdc->eof) {
9044 /*
9045 * Make the entry the direof only if it is cached
9046 */
9047 if (rdc->flags & RDDIRCACHED)
9048 rp->r_direof = rdc;
9049 rddir4_cache_rele(rp, rdc);
9050 mutex_exit(&rp->r_statelock);
9051 return (error);
9052 }
9053
9054 /* Determine if a readdir readahead should be done */
9055 if (!(rp->r_flags & R4LOOKUP)) {
9056 rddir4_cache_rele(rp, rdc);
9057 mutex_exit(&rp->r_statelock);
9058 return (error);
9059 }
9060
9061 /*
9062 * Now look for a readahead entry.
9063 *
9064 * Check to see whether we found an entry for the readahead.
9065 * If so, we don't need to do anything further, so free the new
9066 * entry if one was allocated. Otherwise, allocate a new entry, add
9067 * it to the cache, and then initiate an asynchronous readdir
9068 * operation to fill it.
9069 */
9070 rrdc = rddir4_cache_lookup(rp, rdc->nfs4_ncookie, count);
9071
9072 /*
9073 * A readdir cache entry could not be obtained for the readahead. In
9074 * this case we skip the readahead and return.
9075 */
9076 if (rrdc == NULL) {
9077 rddir4_cache_rele(rp, rdc);
9078 mutex_exit(&rp->r_statelock);
9079 return (error);
9080 }
9081
9082 /*
9083 * Check to see if we need to fill this entry in.
9084 */
9085 if (rrdc->flags & RDDIRREQ) {
9086 rrdc->flags &= ~RDDIRREQ;
9087 rrdc->flags |= RDDIR;
9088 rddir4_cache_rele(rp, rdc);
9089 mutex_exit(&rp->r_statelock);
9090 #ifdef DEBUG
9091 nfs4_readdir_readahead++;
9092 #endif
9093 /*
9094 * Do the readdir.
9095 */
9096 nfs4_async_readdir(vp, rrdc, cr, do_nfs4readdir);
9097 return (error);
9098 }
9099
9100 rddir4_cache_rele(rp, rrdc);
9101 rddir4_cache_rele(rp, rdc);
9102 mutex_exit(&rp->r_statelock);
9103 return (error);
9104 }
9105
9106 static int
do_nfs4readdir(vnode_t * vp,rddir4_cache * rdc,cred_t * cr)9107 do_nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9108 {
9109 int error;
9110 rnode4_t *rp;
9111
9112 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
9113
9114 rp = VTOR4(vp);
9115
9116 /*
9117 * Obtain the readdir results for the caller.
9118 */
9119 nfs4readdir(vp, rdc, cr);
9120
9121 mutex_enter(&rp->r_statelock);
9122 /*
9123 * The entry is now complete
9124 */
9125 rdc->flags &= ~RDDIR;
9126
9127 error = rdc->error;
9128 if (error)
9129 rdc->flags |= RDDIRREQ;
9130 rddir4_cache_rele(rp, rdc);
9131 mutex_exit(&rp->r_statelock);
9132
9133 return (error);
9134 }
9135
9136 /*
9137 * Read directory entries.
9138 * There are some weird things to look out for here. The uio_loffset
9139 * field is either 0 or it is the offset returned from a previous
9140 * readdir. It is an opaque value used by the server to find the
9141 * correct directory block to read. The count field is the number
9142 * of blocks to read on the server. This is advisory only, the server
9143 * may return only one block's worth of entries. Entries may be compressed
9144 * on the server.
9145 *
9146 * Generates the following compound request:
9147 * 1. If readdir offset is zero and no dnlc entry for parent exists,
9148 * must include a Lookupp as well. In this case, send:
9149 * { Putfh <fh>; Readdir; Lookupp; Getfh; Getattr }
9150 * 2. Otherwise just do: { Putfh <fh>; Readdir }
9151 *
9152 * Get complete attributes and filehandles for entries if this is the
9153 * first read of the directory. Otherwise, just get fileid's.
9154 */
9155 static void
nfs4readdir(vnode_t * vp,rddir4_cache * rdc,cred_t * cr)9156 nfs4readdir(vnode_t *vp, rddir4_cache *rdc, cred_t *cr)
9157 {
9158 COMPOUND4args_clnt args;
9159 COMPOUND4res_clnt res;
9160 READDIR4args *rargs;
9161 READDIR4res_clnt *rd_res;
9162 bitmap4 rd_bitsval;
9163 nfs_argop4 argop[5];
9164 nfs_resop4 *resop;
9165 rnode4_t *rp = VTOR4(vp);
9166 mntinfo4_t *mi = VTOMI4(vp);
9167 int doqueue;
9168 u_longlong_t nodeid, pnodeid; /* id's of dir and its parents */
9169 vnode_t *dvp;
9170 nfs_cookie4 cookie = (nfs_cookie4)rdc->nfs4_cookie;
9171 int num_ops, res_opcnt;
9172 bool_t needrecov = FALSE;
9173 nfs4_recov_state_t recov_state;
9174 hrtime_t t;
9175 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
9176
9177 ASSERT(nfs_zone() == mi->mi_zone);
9178 ASSERT(rdc->flags & RDDIR);
9179 ASSERT(rdc->entries == NULL);
9180
9181 /*
9182 * If rp were a stub, it should have triggered and caused
9183 * a mount for us to get this far.
9184 */
9185 ASSERT(!RP_ISSTUB(rp));
9186
9187 num_ops = 2;
9188 if (cookie == (nfs_cookie4)0 || cookie == (nfs_cookie4)1) {
9189 /*
9190 * Since nfsv4 readdir may not return entries for "." and "..",
9191 * the client must recreate them:
9192 * To find the correct nodeid, do the following:
9193 * For current node, get nodeid from dnlc.
9194 * - if current node is rootvp, set pnodeid to nodeid.
9195 * - else if parent is in the dnlc, get its nodeid from there.
9196 * - else add LOOKUPP+GETATTR to compound.
9197 */
9198 nodeid = rp->r_attr.va_nodeid;
9199 if (vp->v_flag & VROOT) {
9200 pnodeid = nodeid; /* root of mount point */
9201 } else {
9202 dvp = dnlc_lookup(vp, "..");
9203 if (dvp != NULL && dvp != DNLC_NO_VNODE) {
9204 /* parent in dnlc cache - no need for otw */
9205 pnodeid = VTOR4(dvp)->r_attr.va_nodeid;
9206 } else {
9207 /*
9208 * parent not in dnlc cache,
9209 * do lookupp to get its id
9210 */
9211 num_ops = 5;
9212 pnodeid = 0; /* set later by getattr parent */
9213 }
9214 if (dvp)
9215 VN_RELE(dvp);
9216 }
9217 }
9218 recov_state.rs_flags = 0;
9219 recov_state.rs_num_retry_despite_err = 0;
9220
9221 /* Save the original mount point security flavor */
9222 (void) save_mnt_secinfo(mi->mi_curr_serv);
9223
9224 recov_retry:
9225 args.ctag = TAG_READDIR;
9226
9227 args.array = argop;
9228 args.array_len = num_ops;
9229
9230 if (e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9231 &recov_state, NULL)) {
9232 /*
9233 * If readdir a node that is a stub for a crossed mount point,
9234 * keep the original secinfo flavor for the current file
9235 * system, not the crossed one.
9236 */
9237 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9238 rdc->error = e.error;
9239 return;
9240 }
9241
9242 /*
9243 * Determine which attrs to request for dirents. This code
9244 * must be protected by nfs4_start/end_fop because of r_server
9245 * (which will change during failover recovery).
9246 *
9247 */
9248 if (rp->r_flags & (R4LOOKUP | R4READDIRWATTR)) {
9249 /*
9250 * Get all vattr attrs plus filehandle and rdattr_error
9251 */
9252 rd_bitsval = NFS4_VATTR_MASK |
9253 FATTR4_RDATTR_ERROR_MASK |
9254 FATTR4_FILEHANDLE_MASK;
9255
9256 if (rp->r_flags & R4READDIRWATTR) {
9257 mutex_enter(&rp->r_statelock);
9258 rp->r_flags &= ~R4READDIRWATTR;
9259 mutex_exit(&rp->r_statelock);
9260 }
9261 } else {
9262 servinfo4_t *svp = rp->r_server;
9263
9264 /*
9265 * Already read directory. Use readdir with
9266 * no attrs (except for mounted_on_fileid) for updates.
9267 */
9268 rd_bitsval = FATTR4_RDATTR_ERROR_MASK;
9269
9270 /*
9271 * request mounted on fileid if supported, else request
9272 * fileid. maybe we should verify that fileid is supported
9273 * and request something else if not.
9274 */
9275 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
9276 if (svp->sv_supp_attrs & FATTR4_MOUNTED_ON_FILEID_MASK)
9277 rd_bitsval |= FATTR4_MOUNTED_ON_FILEID_MASK;
9278 nfs_rw_exit(&svp->sv_lock);
9279 }
9280
9281 /* putfh directory fh */
9282 argop[0].argop = OP_CPUTFH;
9283 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
9284
9285 argop[1].argop = OP_READDIR;
9286 rargs = &argop[1].nfs_argop4_u.opreaddir;
9287 /*
9288 * 1 and 2 are reserved for client "." and ".." entry offset.
9289 * cookie 0 should be used over-the-wire to start reading at
9290 * the beginning of the directory excluding "." and "..".
9291 */
9292 if (rdc->nfs4_cookie == 0 ||
9293 rdc->nfs4_cookie == 1 ||
9294 rdc->nfs4_cookie == 2) {
9295 rargs->cookie = (nfs_cookie4)0;
9296 rargs->cookieverf = 0;
9297 } else {
9298 rargs->cookie = (nfs_cookie4)rdc->nfs4_cookie;
9299 mutex_enter(&rp->r_statelock);
9300 rargs->cookieverf = rp->r_cookieverf4;
9301 mutex_exit(&rp->r_statelock);
9302 }
9303 rargs->dircount = MIN(rdc->buflen, mi->mi_tsize);
9304 rargs->maxcount = mi->mi_tsize;
9305 rargs->attr_request = rd_bitsval;
9306 rargs->rdc = rdc;
9307 rargs->dvp = vp;
9308 rargs->mi = mi;
9309 rargs->cr = cr;
9310
9311
9312 /*
9313 * If count < than the minimum required, we return no entries
9314 * and fail with EINVAL
9315 */
9316 if (rargs->dircount < (DIRENT64_RECLEN(1) + DIRENT64_RECLEN(2))) {
9317 rdc->error = EINVAL;
9318 goto out;
9319 }
9320
9321 if (args.array_len == 5) {
9322 /*
9323 * Add lookupp and getattr for parent nodeid.
9324 */
9325 argop[2].argop = OP_LOOKUPP;
9326
9327 argop[3].argop = OP_GETFH;
9328
9329 /* getattr parent */
9330 argop[4].argop = OP_GETATTR;
9331 argop[4].nfs_argop4_u.opgetattr.attr_request = NFS4_VATTR_MASK;
9332 argop[4].nfs_argop4_u.opgetattr.mi = mi;
9333 }
9334
9335 doqueue = 1;
9336
9337 if (mi->mi_io_kstats) {
9338 mutex_enter(&mi->mi_lock);
9339 kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9340 mutex_exit(&mi->mi_lock);
9341 }
9342
9343 /* capture the time of this call */
9344 rargs->t = t = gethrtime();
9345
9346 rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
9347
9348 if (mi->mi_io_kstats) {
9349 mutex_enter(&mi->mi_lock);
9350 kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9351 mutex_exit(&mi->mi_lock);
9352 }
9353
9354 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
9355
9356 /*
9357 * If RPC error occurred and it isn't an error that
9358 * triggers recovery, then go ahead and fail now.
9359 */
9360 if (e.error != 0 && !needrecov) {
9361 rdc->error = e.error;
9362 goto out;
9363 }
9364
9365 if (needrecov) {
9366 bool_t abort;
9367
9368 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
9369 "nfs4readdir: initiating recovery.\n"));
9370
9371 abort = nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
9372 NULL, OP_READDIR, NULL, NULL, NULL);
9373 if (abort == FALSE) {
9374 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9375 &recov_state, needrecov);
9376 if (!e.error)
9377 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9378 if (rdc->entries != NULL) {
9379 kmem_free(rdc->entries, rdc->entlen);
9380 rdc->entries = NULL;
9381 }
9382 goto recov_retry;
9383 }
9384
9385 if (e.error != 0) {
9386 rdc->error = e.error;
9387 goto out;
9388 }
9389
9390 /* fall through for res.status case */
9391 }
9392
9393 res_opcnt = res.array_len;
9394
9395 /*
9396 * If compound failed first 2 ops (PUTFH+READDIR), then return
9397 * failure here. Subsequent ops are for filling out dot-dot
9398 * dirent, and if they fail, we still want to give the caller
9399 * the dirents returned by (the successful) READDIR op, so we need
9400 * to silently ignore failure for subsequent ops (LOOKUPP+GETATTR).
9401 *
9402 * One example where PUTFH+READDIR ops would succeed but
9403 * LOOKUPP+GETATTR would fail would be a dir that has r perm
9404 * but lacks x. In this case, a POSIX server's VOP_READDIR
9405 * would succeed; however, VOP_LOOKUP(..) would fail since no
9406 * x perm. We need to come up with a non-vendor-specific way
9407 * for a POSIX server to return d_ino from dotdot's dirent if
9408 * client only requests mounted_on_fileid, and just say the
9409 * LOOKUPP succeeded and fill out the GETATTR. However, if
9410 * client requested any mandatory attrs, server would be required
9411 * to fail the GETATTR op because it can't call VOP_LOOKUP+VOP_GETATTR
9412 * for dotdot.
9413 */
9414
9415 if (res.status) {
9416 if (res_opcnt <= 2) {
9417 e.error = geterrno4(res.status);
9418 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_READDIR,
9419 &recov_state, needrecov);
9420 nfs4_purge_stale_fh(e.error, vp, cr);
9421 rdc->error = e.error;
9422 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9423 if (rdc->entries != NULL) {
9424 kmem_free(rdc->entries, rdc->entlen);
9425 rdc->entries = NULL;
9426 }
9427 /*
9428 * If readdir a node that is a stub for a
9429 * crossed mount point, keep the original
9430 * secinfo flavor for the current file system,
9431 * not the crossed one.
9432 */
9433 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9434 return;
9435 }
9436 }
9437
9438 resop = &res.array[1]; /* readdir res */
9439 rd_res = &resop->nfs_resop4_u.opreaddirclnt;
9440
9441 mutex_enter(&rp->r_statelock);
9442 rp->r_cookieverf4 = rd_res->cookieverf;
9443 mutex_exit(&rp->r_statelock);
9444
9445 /*
9446 * For "." and ".." entries
9447 * e.g.
9448 * seek(cookie=0) -> "." entry with d_off = 1
9449 * seek(cookie=1) -> ".." entry with d_off = 2
9450 */
9451 if (cookie == (nfs_cookie4) 0) {
9452 if (rd_res->dotp)
9453 rd_res->dotp->d_ino = nodeid;
9454 if (rd_res->dotdotp)
9455 rd_res->dotdotp->d_ino = pnodeid;
9456 }
9457 if (cookie == (nfs_cookie4) 1) {
9458 if (rd_res->dotdotp)
9459 rd_res->dotdotp->d_ino = pnodeid;
9460 }
9461
9462
9463 /* LOOKUPP+GETATTR attemped */
9464 if (args.array_len == 5 && rd_res->dotdotp) {
9465 if (res.status == NFS4_OK && res_opcnt == 5) {
9466 nfs_fh4 *fhp;
9467 nfs4_sharedfh_t *sfhp;
9468 vnode_t *pvp;
9469 nfs4_ga_res_t *garp;
9470
9471 resop++; /* lookupp */
9472 resop++; /* getfh */
9473 fhp = &resop->nfs_resop4_u.opgetfh.object;
9474
9475 resop++; /* getattr of parent */
9476
9477 /*
9478 * First, take care of finishing the
9479 * readdir results.
9480 */
9481 garp = &resop->nfs_resop4_u.opgetattr.ga_res;
9482 /*
9483 * The d_ino of .. must be the inode number
9484 * of the mounted filesystem.
9485 */
9486 if (garp->n4g_va.va_mask & AT_NODEID)
9487 rd_res->dotdotp->d_ino =
9488 garp->n4g_va.va_nodeid;
9489
9490
9491 /*
9492 * Next, create the ".." dnlc entry
9493 */
9494 sfhp = sfh4_get(fhp, mi);
9495 if (!nfs4_make_dotdot(sfhp, t, vp, cr, &pvp, 0)) {
9496 dnlc_update(vp, "..", pvp);
9497 VN_RELE(pvp);
9498 }
9499 sfh4_rele(&sfhp);
9500 }
9501 }
9502
9503 if (mi->mi_io_kstats) {
9504 mutex_enter(&mi->mi_lock);
9505 KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9506 KSTAT_IO_PTR(mi->mi_io_kstats)->nread += rdc->actlen;
9507 mutex_exit(&mi->mi_lock);
9508 }
9509
9510 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
9511
9512 out:
9513 /*
9514 * If readdir a node that is a stub for a crossed mount point,
9515 * keep the original secinfo flavor for the current file system,
9516 * not the crossed one.
9517 */
9518 (void) check_mnt_secinfo(mi->mi_curr_serv, vp);
9519
9520 nfs4_end_fop(mi, vp, NULL, OH_READDIR, &recov_state, needrecov);
9521 }
9522
9523
9524 static int
nfs4_bio(struct buf * bp,stable_how4 * stab_comm,cred_t * cr,bool_t readahead)9525 nfs4_bio(struct buf *bp, stable_how4 *stab_comm, cred_t *cr, bool_t readahead)
9526 {
9527 rnode4_t *rp = VTOR4(bp->b_vp);
9528 int count;
9529 int error;
9530 cred_t *cred_otw = NULL;
9531 offset_t offset;
9532 nfs4_open_stream_t *osp = NULL;
9533 bool_t first_time = TRUE; /* first time getting otw cred */
9534 bool_t last_time = FALSE; /* last time getting otw cred */
9535
9536 ASSERT(nfs_zone() == VTOMI4(bp->b_vp)->mi_zone);
9537
9538 DTRACE_IO1(start, struct buf *, bp);
9539 offset = ldbtob(bp->b_lblkno);
9540
9541 if (bp->b_flags & B_READ) {
9542 read_again:
9543 /*
9544 * Releases the osp, if it is provided.
9545 * Puts a hold on the cred_otw and the new osp (if found).
9546 */
9547 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9548 &first_time, &last_time);
9549 error = bp->b_error = nfs4read(bp->b_vp, bp->b_un.b_addr,
9550 offset, bp->b_bcount, &bp->b_resid, cred_otw,
9551 readahead, NULL);
9552 crfree(cred_otw);
9553 if (!error) {
9554 if (bp->b_resid) {
9555 /*
9556 * Didn't get it all because we hit EOF,
9557 * zero all the memory beyond the EOF.
9558 */
9559 /* bzero(rdaddr + */
9560 bzero(bp->b_un.b_addr +
9561 bp->b_bcount - bp->b_resid, bp->b_resid);
9562 }
9563 mutex_enter(&rp->r_statelock);
9564 if (bp->b_resid == bp->b_bcount &&
9565 offset >= rp->r_size) {
9566 /*
9567 * We didn't read anything at all as we are
9568 * past EOF. Return an error indicator back
9569 * but don't destroy the pages (yet).
9570 */
9571 error = NFS_EOF;
9572 }
9573 mutex_exit(&rp->r_statelock);
9574 } else if (error == EACCES && last_time == FALSE) {
9575 goto read_again;
9576 }
9577 } else {
9578 if (!(rp->r_flags & R4STALE)) {
9579 write_again:
9580 /*
9581 * Releases the osp, if it is provided.
9582 * Puts a hold on the cred_otw and the new
9583 * osp (if found).
9584 */
9585 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
9586 &first_time, &last_time);
9587 mutex_enter(&rp->r_statelock);
9588 count = MIN(bp->b_bcount, rp->r_size - offset);
9589 mutex_exit(&rp->r_statelock);
9590 if (count < 0)
9591 cmn_err(CE_PANIC, "nfs4_bio: write count < 0");
9592 #ifdef DEBUG
9593 if (count == 0) {
9594 zoneid_t zoneid = getzoneid();
9595
9596 zcmn_err(zoneid, CE_WARN,
9597 "nfs4_bio: zero length write at %lld",
9598 offset);
9599 zcmn_err(zoneid, CE_CONT, "flags=0x%x, "
9600 "b_bcount=%ld, file size=%lld",
9601 rp->r_flags, (long)bp->b_bcount,
9602 rp->r_size);
9603 sfh4_printfhandle(VTOR4(bp->b_vp)->r_fh);
9604 if (nfs4_bio_do_stop)
9605 debug_enter("nfs4_bio");
9606 }
9607 #endif
9608 error = nfs4write(bp->b_vp, bp->b_un.b_addr, offset,
9609 count, cred_otw, stab_comm);
9610 if (error == EACCES && last_time == FALSE) {
9611 crfree(cred_otw);
9612 goto write_again;
9613 }
9614 bp->b_error = error;
9615 if (error && error != EINTR &&
9616 !(bp->b_vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
9617 /*
9618 * Don't print EDQUOT errors on the console.
9619 * Don't print asynchronous EACCES errors.
9620 * Don't print EFBIG errors.
9621 * Print all other write errors.
9622 */
9623 if (error != EDQUOT && error != EFBIG &&
9624 (error != EACCES ||
9625 !(bp->b_flags & B_ASYNC)))
9626 nfs4_write_error(bp->b_vp,
9627 error, cred_otw);
9628 /*
9629 * Update r_error and r_flags as appropriate.
9630 * If the error was ESTALE, then mark the
9631 * rnode as not being writeable and save
9632 * the error status. Otherwise, save any
9633 * errors which occur from asynchronous
9634 * page invalidations. Any errors occurring
9635 * from other operations should be saved
9636 * by the caller.
9637 */
9638 mutex_enter(&rp->r_statelock);
9639 if (error == ESTALE) {
9640 rp->r_flags |= R4STALE;
9641 if (!rp->r_error)
9642 rp->r_error = error;
9643 } else if (!rp->r_error &&
9644 (bp->b_flags &
9645 (B_INVAL|B_FORCE|B_ASYNC)) ==
9646 (B_INVAL|B_FORCE|B_ASYNC)) {
9647 rp->r_error = error;
9648 }
9649 mutex_exit(&rp->r_statelock);
9650 }
9651 crfree(cred_otw);
9652 } else {
9653 error = rp->r_error;
9654 /*
9655 * A close may have cleared r_error, if so,
9656 * propagate ESTALE error return properly
9657 */
9658 if (error == 0)
9659 error = ESTALE;
9660 }
9661 }
9662
9663 if (error != 0 && error != NFS_EOF)
9664 bp->b_flags |= B_ERROR;
9665
9666 if (osp)
9667 open_stream_rele(osp, rp);
9668
9669 DTRACE_IO1(done, struct buf *, bp);
9670
9671 return (error);
9672 }
9673
9674 /* ARGSUSED */
9675 int
nfs4_fid(vnode_t * vp,fid_t * fidp,caller_context_t * ct)9676 nfs4_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
9677 {
9678 return (EREMOTE);
9679 }
9680
9681 /* ARGSUSED2 */
9682 int
nfs4_rwlock(vnode_t * vp,int write_lock,caller_context_t * ctp)9683 nfs4_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9684 {
9685 rnode4_t *rp = VTOR4(vp);
9686
9687 if (!write_lock) {
9688 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9689 return (V_WRITELOCK_FALSE);
9690 }
9691
9692 if ((rp->r_flags & R4DIRECTIO) ||
9693 (VTOMI4(vp)->mi_flags & MI4_DIRECTIO)) {
9694 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
9695 if (rp->r_mapcnt == 0 && !nfs4_has_pages(vp))
9696 return (V_WRITELOCK_FALSE);
9697 nfs_rw_exit(&rp->r_rwlock);
9698 }
9699
9700 (void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
9701 return (V_WRITELOCK_TRUE);
9702 }
9703
9704 /* ARGSUSED */
9705 void
nfs4_rwunlock(vnode_t * vp,int write_lock,caller_context_t * ctp)9706 nfs4_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
9707 {
9708 rnode4_t *rp = VTOR4(vp);
9709
9710 nfs_rw_exit(&rp->r_rwlock);
9711 }
9712
9713 /* ARGSUSED */
9714 static int
nfs4_seek(vnode_t * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)9715 nfs4_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
9716 {
9717 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9718 return (EIO);
9719
9720 /*
9721 * Because we stuff the readdir cookie into the offset field
9722 * someone may attempt to do an lseek with the cookie which
9723 * we want to succeed.
9724 */
9725 if (vp->v_type == VDIR)
9726 return (0);
9727 if (*noffp < 0)
9728 return (EINVAL);
9729 return (0);
9730 }
9731
9732
9733 /*
9734 * Return all the pages from [off..off+len) in file
9735 */
9736 /* ARGSUSED */
9737 static int
nfs4_getpage(vnode_t * vp,offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr,caller_context_t * ct)9738 nfs4_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
9739 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9740 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
9741 {
9742 rnode4_t *rp;
9743 int error;
9744 mntinfo4_t *mi;
9745
9746 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9747 return (EIO);
9748 rp = VTOR4(vp);
9749 if (IS_SHADOW(vp, rp))
9750 vp = RTOV4(rp);
9751
9752 if (vp->v_flag & VNOMAP)
9753 return (ENOSYS);
9754
9755 if (protp != NULL)
9756 *protp = PROT_ALL;
9757
9758 /*
9759 * Now validate that the caches are up to date.
9760 */
9761 if (error = nfs4_validate_caches(vp, cr))
9762 return (error);
9763
9764 mi = VTOMI4(vp);
9765 retry:
9766 mutex_enter(&rp->r_statelock);
9767
9768 /*
9769 * Don't create dirty pages faster than they
9770 * can be cleaned so that the system doesn't
9771 * get imbalanced. If the async queue is
9772 * maxed out, then wait for it to drain before
9773 * creating more dirty pages. Also, wait for
9774 * any threads doing pagewalks in the vop_getattr
9775 * entry points so that they don't block for
9776 * long periods.
9777 */
9778 if (rw == S_CREATE) {
9779 while ((mi->mi_max_threads != 0 &&
9780 rp->r_awcount > 2 * mi->mi_max_threads) ||
9781 rp->r_gcount > 0)
9782 cv_wait(&rp->r_cv, &rp->r_statelock);
9783 }
9784
9785 /*
9786 * If we are getting called as a side effect of an nfs_write()
9787 * operation the local file size might not be extended yet.
9788 * In this case we want to be able to return pages of zeroes.
9789 */
9790 if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
9791 NFS4_DEBUG(nfs4_pageio_debug,
9792 (CE_NOTE, "getpage beyond EOF: off=%lld, "
9793 "len=%llu, size=%llu, attrsize =%llu", off,
9794 (u_longlong_t)len, rp->r_size, rp->r_attr.va_size));
9795 mutex_exit(&rp->r_statelock);
9796 return (EFAULT); /* beyond EOF */
9797 }
9798
9799 mutex_exit(&rp->r_statelock);
9800
9801 error = pvn_getpages(nfs4_getapage, vp, off, len, protp,
9802 pl, plsz, seg, addr, rw, cr);
9803 NFS4_DEBUG(nfs4_pageio_debug && error,
9804 (CE_NOTE, "getpages error %d; off=%lld, len=%lld",
9805 error, off, (u_longlong_t)len));
9806
9807 switch (error) {
9808 case NFS_EOF:
9809 nfs4_purge_caches(vp, NFS4_NOPURGE_DNLC, cr, FALSE);
9810 goto retry;
9811 case ESTALE:
9812 nfs4_purge_stale_fh(error, vp, cr);
9813 }
9814
9815 return (error);
9816 }
9817
9818 /*
9819 * Called from pvn_getpages to get a particular page.
9820 */
9821 /* ARGSUSED */
9822 static int
nfs4_getapage(vnode_t * vp,u_offset_t off,size_t len,uint_t * protp,page_t * pl[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,cred_t * cr)9823 nfs4_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
9824 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
9825 enum seg_rw rw, cred_t *cr)
9826 {
9827 rnode4_t *rp;
9828 uint_t bsize;
9829 struct buf *bp;
9830 page_t *pp;
9831 u_offset_t lbn;
9832 u_offset_t io_off;
9833 u_offset_t blkoff;
9834 u_offset_t rablkoff;
9835 size_t io_len;
9836 uint_t blksize;
9837 int error;
9838 int readahead;
9839 int readahead_issued = 0;
9840 int ra_window; /* readahead window */
9841 page_t *pagefound;
9842 page_t *savepp;
9843
9844 if (nfs_zone() != VTOMI4(vp)->mi_zone)
9845 return (EIO);
9846
9847 rp = VTOR4(vp);
9848 ASSERT(!IS_SHADOW(vp, rp));
9849 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
9850
9851 reread:
9852 bp = NULL;
9853 pp = NULL;
9854 pagefound = NULL;
9855
9856 if (pl != NULL)
9857 pl[0] = NULL;
9858
9859 error = 0;
9860 lbn = off / bsize;
9861 blkoff = lbn * bsize;
9862
9863 /*
9864 * Queueing up the readahead before doing the synchronous read
9865 * results in a significant increase in read throughput because
9866 * of the increased parallelism between the async threads and
9867 * the process context.
9868 */
9869 if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
9870 rw != S_CREATE &&
9871 !(vp->v_flag & VNOCACHE)) {
9872 mutex_enter(&rp->r_statelock);
9873
9874 /*
9875 * Calculate the number of readaheads to do.
9876 * a) No readaheads at offset = 0.
9877 * b) Do maximum(nfs4_nra) readaheads when the readahead
9878 * window is closed.
9879 * c) Do readaheads between 1 to (nfs4_nra - 1) depending
9880 * upon how far the readahead window is open or close.
9881 * d) No readaheads if rp->r_nextr is not within the scope
9882 * of the readahead window (random i/o).
9883 */
9884
9885 if (off == 0)
9886 readahead = 0;
9887 else if (blkoff == rp->r_nextr)
9888 readahead = nfs4_nra;
9889 else if (rp->r_nextr > blkoff &&
9890 ((ra_window = (rp->r_nextr - blkoff) / bsize)
9891 <= (nfs4_nra - 1)))
9892 readahead = nfs4_nra - ra_window;
9893 else
9894 readahead = 0;
9895
9896 rablkoff = rp->r_nextr;
9897 while (readahead > 0 && rablkoff + bsize < rp->r_size) {
9898 mutex_exit(&rp->r_statelock);
9899 if (nfs4_async_readahead(vp, rablkoff + bsize,
9900 addr + (rablkoff + bsize - off),
9901 seg, cr, nfs4_readahead) < 0) {
9902 mutex_enter(&rp->r_statelock);
9903 break;
9904 }
9905 readahead--;
9906 rablkoff += bsize;
9907 /*
9908 * Indicate that we did a readahead so
9909 * readahead offset is not updated
9910 * by the synchronous read below.
9911 */
9912 readahead_issued = 1;
9913 mutex_enter(&rp->r_statelock);
9914 /*
9915 * set readahead offset to
9916 * offset of last async readahead
9917 * request.
9918 */
9919 rp->r_nextr = rablkoff;
9920 }
9921 mutex_exit(&rp->r_statelock);
9922 }
9923
9924 again:
9925 if ((pagefound = page_exists(vp, off)) == NULL) {
9926 if (pl == NULL) {
9927 (void) nfs4_async_readahead(vp, blkoff, addr, seg, cr,
9928 nfs4_readahead);
9929 } else if (rw == S_CREATE) {
9930 /*
9931 * Block for this page is not allocated, or the offset
9932 * is beyond the current allocation size, or we're
9933 * allocating a swap slot and the page was not found,
9934 * so allocate it and return a zero page.
9935 */
9936 if ((pp = page_create_va(vp, off,
9937 PAGESIZE, PG_WAIT, seg, addr)) == NULL)
9938 cmn_err(CE_PANIC, "nfs4_getapage: page_create");
9939 io_len = PAGESIZE;
9940 mutex_enter(&rp->r_statelock);
9941 rp->r_nextr = off + PAGESIZE;
9942 mutex_exit(&rp->r_statelock);
9943 } else {
9944 /*
9945 * Need to go to server to get a block
9946 */
9947 mutex_enter(&rp->r_statelock);
9948 if (blkoff < rp->r_size &&
9949 blkoff + bsize > rp->r_size) {
9950 /*
9951 * If less than a block left in
9952 * file read less than a block.
9953 */
9954 if (rp->r_size <= off) {
9955 /*
9956 * Trying to access beyond EOF,
9957 * set up to get at least one page.
9958 */
9959 blksize = off + PAGESIZE - blkoff;
9960 } else
9961 blksize = rp->r_size - blkoff;
9962 } else if ((off == 0) ||
9963 (off != rp->r_nextr && !readahead_issued)) {
9964 blksize = PAGESIZE;
9965 blkoff = off; /* block = page here */
9966 } else
9967 blksize = bsize;
9968 mutex_exit(&rp->r_statelock);
9969
9970 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
9971 &io_len, blkoff, blksize, 0);
9972
9973 /*
9974 * Some other thread has entered the page,
9975 * so just use it.
9976 */
9977 if (pp == NULL)
9978 goto again;
9979
9980 /*
9981 * Now round the request size up to page boundaries.
9982 * This ensures that the entire page will be
9983 * initialized to zeroes if EOF is encountered.
9984 */
9985 io_len = ptob(btopr(io_len));
9986
9987 bp = pageio_setup(pp, io_len, vp, B_READ);
9988 ASSERT(bp != NULL);
9989
9990 /*
9991 * pageio_setup should have set b_addr to 0. This
9992 * is correct since we want to do I/O on a page
9993 * boundary. bp_mapin will use this addr to calculate
9994 * an offset, and then set b_addr to the kernel virtual
9995 * address it allocated for us.
9996 */
9997 ASSERT(bp->b_un.b_addr == 0);
9998
9999 bp->b_edev = 0;
10000 bp->b_dev = 0;
10001 bp->b_lblkno = lbtodb(io_off);
10002 bp->b_file = vp;
10003 bp->b_offset = (offset_t)off;
10004 bp_mapin(bp);
10005
10006 /*
10007 * If doing a write beyond what we believe is EOF,
10008 * don't bother trying to read the pages from the
10009 * server, we'll just zero the pages here. We
10010 * don't check that the rw flag is S_WRITE here
10011 * because some implementations may attempt a
10012 * read access to the buffer before copying data.
10013 */
10014 mutex_enter(&rp->r_statelock);
10015 if (io_off >= rp->r_size && seg == segkmap) {
10016 mutex_exit(&rp->r_statelock);
10017 bzero(bp->b_un.b_addr, io_len);
10018 } else {
10019 mutex_exit(&rp->r_statelock);
10020 error = nfs4_bio(bp, NULL, cr, FALSE);
10021 }
10022
10023 /*
10024 * Unmap the buffer before freeing it.
10025 */
10026 bp_mapout(bp);
10027 pageio_done(bp);
10028
10029 savepp = pp;
10030 do {
10031 pp->p_fsdata = C_NOCOMMIT;
10032 } while ((pp = pp->p_next) != savepp);
10033
10034 if (error == NFS_EOF) {
10035 /*
10036 * If doing a write system call just return
10037 * zeroed pages, else user tried to get pages
10038 * beyond EOF, return error. We don't check
10039 * that the rw flag is S_WRITE here because
10040 * some implementations may attempt a read
10041 * access to the buffer before copying data.
10042 */
10043 if (seg == segkmap)
10044 error = 0;
10045 else
10046 error = EFAULT;
10047 }
10048
10049 if (!readahead_issued && !error) {
10050 mutex_enter(&rp->r_statelock);
10051 rp->r_nextr = io_off + io_len;
10052 mutex_exit(&rp->r_statelock);
10053 }
10054 }
10055 }
10056
10057 if (pl == NULL)
10058 return (error);
10059
10060 if (error) {
10061 if (pp != NULL)
10062 pvn_read_done(pp, B_ERROR);
10063 return (error);
10064 }
10065
10066 if (pagefound) {
10067 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
10068
10069 /*
10070 * Page exists in the cache, acquire the appropriate lock.
10071 * If this fails, start all over again.
10072 */
10073 if ((pp = page_lookup(vp, off, se)) == NULL) {
10074 #ifdef DEBUG
10075 nfs4_lostpage++;
10076 #endif
10077 goto reread;
10078 }
10079 pl[0] = pp;
10080 pl[1] = NULL;
10081 return (0);
10082 }
10083
10084 if (pp != NULL)
10085 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
10086
10087 return (error);
10088 }
10089
10090 static void
nfs4_readahead(vnode_t * vp,u_offset_t blkoff,caddr_t addr,struct seg * seg,cred_t * cr)10091 nfs4_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
10092 cred_t *cr)
10093 {
10094 int error;
10095 page_t *pp;
10096 u_offset_t io_off;
10097 size_t io_len;
10098 struct buf *bp;
10099 uint_t bsize, blksize;
10100 rnode4_t *rp = VTOR4(vp);
10101 page_t *savepp;
10102
10103 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10104
10105 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10106
10107 mutex_enter(&rp->r_statelock);
10108 if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
10109 /*
10110 * If less than a block left in file read less
10111 * than a block.
10112 */
10113 blksize = rp->r_size - blkoff;
10114 } else
10115 blksize = bsize;
10116 mutex_exit(&rp->r_statelock);
10117
10118 pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
10119 &io_off, &io_len, blkoff, blksize, 1);
10120 /*
10121 * The isra flag passed to the kluster function is 1, we may have
10122 * gotten a return value of NULL for a variety of reasons (# of free
10123 * pages < minfree, someone entered the page on the vnode etc). In all
10124 * cases, we want to punt on the readahead.
10125 */
10126 if (pp == NULL)
10127 return;
10128
10129 /*
10130 * Now round the request size up to page boundaries.
10131 * This ensures that the entire page will be
10132 * initialized to zeroes if EOF is encountered.
10133 */
10134 io_len = ptob(btopr(io_len));
10135
10136 bp = pageio_setup(pp, io_len, vp, B_READ);
10137 ASSERT(bp != NULL);
10138
10139 /*
10140 * pageio_setup should have set b_addr to 0. This is correct since
10141 * we want to do I/O on a page boundary. bp_mapin() will use this addr
10142 * to calculate an offset, and then set b_addr to the kernel virtual
10143 * address it allocated for us.
10144 */
10145 ASSERT(bp->b_un.b_addr == 0);
10146
10147 bp->b_edev = 0;
10148 bp->b_dev = 0;
10149 bp->b_lblkno = lbtodb(io_off);
10150 bp->b_file = vp;
10151 bp->b_offset = (offset_t)blkoff;
10152 bp_mapin(bp);
10153
10154 /*
10155 * If doing a write beyond what we believe is EOF, don't bother trying
10156 * to read the pages from the server, we'll just zero the pages here.
10157 * We don't check that the rw flag is S_WRITE here because some
10158 * implementations may attempt a read access to the buffer before
10159 * copying data.
10160 */
10161 mutex_enter(&rp->r_statelock);
10162 if (io_off >= rp->r_size && seg == segkmap) {
10163 mutex_exit(&rp->r_statelock);
10164 bzero(bp->b_un.b_addr, io_len);
10165 error = 0;
10166 } else {
10167 mutex_exit(&rp->r_statelock);
10168 error = nfs4_bio(bp, NULL, cr, TRUE);
10169 if (error == NFS_EOF)
10170 error = 0;
10171 }
10172
10173 /*
10174 * Unmap the buffer before freeing it.
10175 */
10176 bp_mapout(bp);
10177 pageio_done(bp);
10178
10179 savepp = pp;
10180 do {
10181 pp->p_fsdata = C_NOCOMMIT;
10182 } while ((pp = pp->p_next) != savepp);
10183
10184 pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
10185
10186 /*
10187 * In case of error set readahead offset
10188 * to the lowest offset.
10189 * pvn_read_done() calls VN_DISPOSE to destroy the pages
10190 */
10191 if (error && rp->r_nextr > io_off) {
10192 mutex_enter(&rp->r_statelock);
10193 if (rp->r_nextr > io_off)
10194 rp->r_nextr = io_off;
10195 mutex_exit(&rp->r_statelock);
10196 }
10197 }
10198
10199 /*
10200 * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
10201 * If len == 0, do from off to EOF.
10202 *
10203 * The normal cases should be len == 0 && off == 0 (entire vp list) or
10204 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
10205 * (from pageout).
10206 */
10207 /* ARGSUSED */
10208 static int
nfs4_putpage(vnode_t * vp,offset_t off,size_t len,int flags,cred_t * cr,caller_context_t * ct)10209 nfs4_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
10210 caller_context_t *ct)
10211 {
10212 int error;
10213 rnode4_t *rp;
10214
10215 ASSERT(cr != NULL);
10216
10217 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
10218 return (EIO);
10219
10220 rp = VTOR4(vp);
10221 if (IS_SHADOW(vp, rp))
10222 vp = RTOV4(rp);
10223
10224 /*
10225 * XXX - Why should this check be made here?
10226 */
10227 if (vp->v_flag & VNOMAP)
10228 return (ENOSYS);
10229
10230 if (len == 0 && !(flags & B_INVAL) &&
10231 (vp->v_vfsp->vfs_flag & VFS_RDONLY))
10232 return (0);
10233
10234 mutex_enter(&rp->r_statelock);
10235 rp->r_count++;
10236 mutex_exit(&rp->r_statelock);
10237 error = nfs4_putpages(vp, off, len, flags, cr);
10238 mutex_enter(&rp->r_statelock);
10239 rp->r_count--;
10240 cv_broadcast(&rp->r_cv);
10241 mutex_exit(&rp->r_statelock);
10242
10243 return (error);
10244 }
10245
10246 /*
10247 * Write out a single page, possibly klustering adjacent dirty pages.
10248 */
10249 int
nfs4_putapage(vnode_t * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,cred_t * cr)10250 nfs4_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
10251 int flags, cred_t *cr)
10252 {
10253 u_offset_t io_off;
10254 u_offset_t lbn_off;
10255 u_offset_t lbn;
10256 size_t io_len;
10257 uint_t bsize;
10258 int error;
10259 rnode4_t *rp;
10260
10261 ASSERT(!(vp->v_vfsp->vfs_flag & VFS_RDONLY));
10262 ASSERT(pp != NULL);
10263 ASSERT(cr != NULL);
10264 ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI4(vp)->mi_zone);
10265
10266 rp = VTOR4(vp);
10267 ASSERT(rp->r_count > 0);
10268 ASSERT(!IS_SHADOW(vp, rp));
10269
10270 bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
10271 lbn = pp->p_offset / bsize;
10272 lbn_off = lbn * bsize;
10273
10274 /*
10275 * Find a kluster that fits in one block, or in
10276 * one page if pages are bigger than blocks. If
10277 * there is less file space allocated than a whole
10278 * page, we'll shorten the i/o request below.
10279 */
10280 pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
10281 roundup(bsize, PAGESIZE), flags);
10282
10283 /*
10284 * pvn_write_kluster shouldn't have returned a page with offset
10285 * behind the original page we were given. Verify that.
10286 */
10287 ASSERT((pp->p_offset / bsize) >= lbn);
10288
10289 /*
10290 * Now pp will have the list of kept dirty pages marked for
10291 * write back. It will also handle invalidation and freeing
10292 * of pages that are not dirty. Check for page length rounding
10293 * problems.
10294 */
10295 if (io_off + io_len > lbn_off + bsize) {
10296 ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
10297 io_len = lbn_off + bsize - io_off;
10298 }
10299 /*
10300 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10301 * consistent value of r_size. R4MODINPROGRESS is set in writerp4().
10302 * When R4MODINPROGRESS is set it indicates that a uiomove() is in
10303 * progress and the r_size has not been made consistent with the
10304 * new size of the file. When the uiomove() completes the r_size is
10305 * updated and the R4MODINPROGRESS flag is cleared.
10306 *
10307 * The R4MODINPROGRESS flag makes sure that nfs4_bio() sees a
10308 * consistent value of r_size. Without this handshaking, it is
10309 * possible that nfs4_bio() picks up the old value of r_size
10310 * before the uiomove() in writerp4() completes. This will result
10311 * in the write through nfs4_bio() being dropped.
10312 *
10313 * More precisely, there is a window between the time the uiomove()
10314 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
10315 * operation intervenes in this window, the page will be picked up,
10316 * because it is dirty (it will be unlocked, unless it was
10317 * pagecreate'd). When the page is picked up as dirty, the dirty
10318 * bit is reset (pvn_getdirty()). In nfs4write(), r_size is
10319 * checked. This will still be the old size. Therefore the page will
10320 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
10321 * the page will be found to be clean and the write will be dropped.
10322 */
10323 if (rp->r_flags & R4MODINPROGRESS) {
10324 mutex_enter(&rp->r_statelock);
10325 if ((rp->r_flags & R4MODINPROGRESS) &&
10326 rp->r_modaddr + MAXBSIZE > io_off &&
10327 rp->r_modaddr < io_off + io_len) {
10328 page_t *plist;
10329 /*
10330 * A write is in progress for this region of the file.
10331 * If we did not detect R4MODINPROGRESS here then this
10332 * path through nfs_putapage() would eventually go to
10333 * nfs4_bio() and may not write out all of the data
10334 * in the pages. We end up losing data. So we decide
10335 * to set the modified bit on each page in the page
10336 * list and mark the rnode with R4DIRTY. This write
10337 * will be restarted at some later time.
10338 */
10339 plist = pp;
10340 while (plist != NULL) {
10341 pp = plist;
10342 page_sub(&plist, pp);
10343 hat_setmod(pp);
10344 page_io_unlock(pp);
10345 page_unlock(pp);
10346 }
10347 rp->r_flags |= R4DIRTY;
10348 mutex_exit(&rp->r_statelock);
10349 if (offp)
10350 *offp = io_off;
10351 if (lenp)
10352 *lenp = io_len;
10353 return (0);
10354 }
10355 mutex_exit(&rp->r_statelock);
10356 }
10357
10358 if (flags & B_ASYNC) {
10359 error = nfs4_async_putapage(vp, pp, io_off, io_len, flags, cr,
10360 nfs4_sync_putapage);
10361 } else
10362 error = nfs4_sync_putapage(vp, pp, io_off, io_len, flags, cr);
10363
10364 if (offp)
10365 *offp = io_off;
10366 if (lenp)
10367 *lenp = io_len;
10368 return (error);
10369 }
10370
10371 static int
nfs4_sync_putapage(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)10372 nfs4_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
10373 int flags, cred_t *cr)
10374 {
10375 int error;
10376 rnode4_t *rp;
10377
10378 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
10379
10380 flags |= B_WRITE;
10381
10382 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
10383
10384 rp = VTOR4(vp);
10385
10386 if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
10387 error == EACCES) &&
10388 (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
10389 if (!(rp->r_flags & R4OUTOFSPACE)) {
10390 mutex_enter(&rp->r_statelock);
10391 rp->r_flags |= R4OUTOFSPACE;
10392 mutex_exit(&rp->r_statelock);
10393 }
10394 flags |= B_ERROR;
10395 pvn_write_done(pp, flags);
10396 /*
10397 * If this was not an async thread, then try again to
10398 * write out the pages, but this time, also destroy
10399 * them whether or not the write is successful. This
10400 * will prevent memory from filling up with these
10401 * pages and destroying them is the only alternative
10402 * if they can't be written out.
10403 *
10404 * Don't do this if this is an async thread because
10405 * when the pages are unlocked in pvn_write_done,
10406 * some other thread could have come along, locked
10407 * them, and queued for an async thread. It would be
10408 * possible for all of the async threads to be tied
10409 * up waiting to lock the pages again and they would
10410 * all already be locked and waiting for an async
10411 * thread to handle them. Deadlock.
10412 */
10413 if (!(flags & B_ASYNC)) {
10414 error = nfs4_putpage(vp, io_off, io_len,
10415 B_INVAL | B_FORCE, cr, NULL);
10416 }
10417 } else {
10418 if (error)
10419 flags |= B_ERROR;
10420 else if (rp->r_flags & R4OUTOFSPACE) {
10421 mutex_enter(&rp->r_statelock);
10422 rp->r_flags &= ~R4OUTOFSPACE;
10423 mutex_exit(&rp->r_statelock);
10424 }
10425 pvn_write_done(pp, flags);
10426 if (freemem < desfree)
10427 (void) nfs4_commit_vp(vp, (u_offset_t)0, 0, cr,
10428 NFS4_WRITE_NOWAIT);
10429 }
10430
10431 return (error);
10432 }
10433
10434 #ifdef DEBUG
10435 int nfs4_force_open_before_mmap = 0;
10436 #endif
10437
10438 /* ARGSUSED */
10439 static int
nfs4_map(vnode_t * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)10440 nfs4_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
10441 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10442 caller_context_t *ct)
10443 {
10444 struct segvn_crargs vn_a;
10445 int error = 0;
10446 rnode4_t *rp = VTOR4(vp);
10447 mntinfo4_t *mi = VTOMI4(vp);
10448
10449 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10450 return (EIO);
10451
10452 if (vp->v_flag & VNOMAP)
10453 return (ENOSYS);
10454
10455 if (off < 0 || (off + len) < 0)
10456 return (ENXIO);
10457
10458 if (vp->v_type != VREG)
10459 return (ENODEV);
10460
10461 /*
10462 * If the file is delegated to the client don't do anything.
10463 * If the file is not delegated, then validate the data cache.
10464 */
10465 mutex_enter(&rp->r_statev4_lock);
10466 if (rp->r_deleg_type == OPEN_DELEGATE_NONE) {
10467 mutex_exit(&rp->r_statev4_lock);
10468 error = nfs4_validate_caches(vp, cr);
10469 if (error)
10470 return (error);
10471 } else {
10472 mutex_exit(&rp->r_statev4_lock);
10473 }
10474
10475 /*
10476 * Check to see if the vnode is currently marked as not cachable.
10477 * This means portions of the file are locked (through VOP_FRLOCK).
10478 * In this case the map request must be refused. We use
10479 * rp->r_lkserlock to avoid a race with concurrent lock requests.
10480 *
10481 * Atomically increment r_inmap after acquiring r_rwlock. The
10482 * idea here is to acquire r_rwlock to block read/write and
10483 * not to protect r_inmap. r_inmap will inform nfs4_read/write()
10484 * that we are in nfs4_map(). Now, r_rwlock is acquired in order
10485 * and we can prevent the deadlock that would have occurred
10486 * when nfs4_addmap() would have acquired it out of order.
10487 *
10488 * Since we are not protecting r_inmap by any lock, we do not
10489 * hold any lock when we decrement it. We atomically decrement
10490 * r_inmap after we release r_lkserlock.
10491 */
10492
10493 if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR4(vp)))
10494 return (EINTR);
10495 atomic_inc_uint(&rp->r_inmap);
10496 nfs_rw_exit(&rp->r_rwlock);
10497
10498 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR4(vp))) {
10499 atomic_dec_uint(&rp->r_inmap);
10500 return (EINTR);
10501 }
10502
10503 if (vp->v_flag & VNOCACHE) {
10504 error = EAGAIN;
10505 goto done;
10506 }
10507
10508 /*
10509 * Don't allow concurrent locks and mapping if mandatory locking is
10510 * enabled.
10511 */
10512 if (flk_has_remote_locks(vp)) {
10513 struct vattr va;
10514 va.va_mask = AT_MODE;
10515 error = nfs4getattr(vp, &va, cr);
10516 if (error != 0)
10517 goto done;
10518 if (MANDLOCK(vp, va.va_mode)) {
10519 error = EAGAIN;
10520 goto done;
10521 }
10522 }
10523
10524 /*
10525 * It is possible that the rnode has a lost lock request that we
10526 * are still trying to recover, and that the request conflicts with
10527 * this map request.
10528 *
10529 * An alternative approach would be for nfs4_safemap() to consider
10530 * queued lock requests when deciding whether to set or clear
10531 * VNOCACHE. This would require the frlock code path to call
10532 * nfs4_safemap() after enqueing a lost request.
10533 */
10534 if (nfs4_map_lost_lock_conflict(vp)) {
10535 error = EAGAIN;
10536 goto done;
10537 }
10538
10539 as_rangelock(as);
10540 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
10541 if (error != 0) {
10542 as_rangeunlock(as);
10543 goto done;
10544 }
10545
10546 if (vp->v_type == VREG) {
10547 /*
10548 * We need to retrieve the open stream
10549 */
10550 nfs4_open_stream_t *osp = NULL;
10551 nfs4_open_owner_t *oop = NULL;
10552
10553 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10554 if (oop != NULL) {
10555 /* returns with 'os_sync_lock' held */
10556 osp = find_open_stream(oop, rp);
10557 open_owner_rele(oop);
10558 }
10559 if (osp == NULL) {
10560 #ifdef DEBUG
10561 if (nfs4_force_open_before_mmap) {
10562 error = EIO;
10563 goto done;
10564 }
10565 #endif
10566 /* returns with 'os_sync_lock' held */
10567 error = open_and_get_osp(vp, cr, &osp);
10568 if (osp == NULL) {
10569 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10570 "nfs4_map: we tried to OPEN the file "
10571 "but again no osp, so fail with EIO"));
10572 goto done;
10573 }
10574 }
10575
10576 if (osp->os_failed_reopen) {
10577 mutex_exit(&osp->os_sync_lock);
10578 open_stream_rele(osp, rp);
10579 NFS4_DEBUG(nfs4_open_stream_debug, (CE_NOTE,
10580 "nfs4_map: os_failed_reopen set on "
10581 "osp %p, cr %p, rp %s", (void *)osp,
10582 (void *)cr, rnode4info(rp)));
10583 error = EIO;
10584 goto done;
10585 }
10586 mutex_exit(&osp->os_sync_lock);
10587 open_stream_rele(osp, rp);
10588 }
10589
10590 vn_a.vp = vp;
10591 vn_a.offset = off;
10592 vn_a.type = (flags & MAP_TYPE);
10593 vn_a.prot = (uchar_t)prot;
10594 vn_a.maxprot = (uchar_t)maxprot;
10595 vn_a.flags = (flags & ~MAP_TYPE);
10596 vn_a.cred = cr;
10597 vn_a.amp = NULL;
10598 vn_a.szc = 0;
10599 vn_a.lgrp_mem_policy_flags = 0;
10600
10601 error = as_map(as, *addrp, len, segvn_create, &vn_a);
10602 as_rangeunlock(as);
10603
10604 done:
10605 nfs_rw_exit(&rp->r_lkserlock);
10606 atomic_dec_uint(&rp->r_inmap);
10607 return (error);
10608 }
10609
10610 /*
10611 * We're most likely dealing with a kernel module that likes to READ
10612 * and mmap without OPENing the file (ie: lookup/read/mmap), so lets
10613 * officially OPEN the file to create the necessary client state
10614 * for bookkeeping of os_mmap_read/write counts.
10615 *
10616 * Since VOP_MAP only passes in a pointer to the vnode rather than
10617 * a double pointer, we can't handle the case where nfs4open_otw()
10618 * returns a different vnode than the one passed into VOP_MAP (since
10619 * VOP_DELMAP will not see the vnode nfs4open_otw used). In this case,
10620 * we return NULL and let nfs4_map() fail. Note: the only case where
10621 * this should happen is if the file got removed and replaced with the
10622 * same name on the server (in addition to the fact that we're trying
10623 * to VOP_MAP withouth VOP_OPENing the file in the first place).
10624 */
10625 static int
open_and_get_osp(vnode_t * map_vp,cred_t * cr,nfs4_open_stream_t ** ospp)10626 open_and_get_osp(vnode_t *map_vp, cred_t *cr, nfs4_open_stream_t **ospp)
10627 {
10628 rnode4_t *rp, *drp;
10629 vnode_t *dvp, *open_vp;
10630 char file_name[MAXNAMELEN];
10631 int just_created;
10632 nfs4_open_stream_t *osp;
10633 nfs4_open_owner_t *oop;
10634 int error;
10635
10636 *ospp = NULL;
10637 open_vp = map_vp;
10638
10639 rp = VTOR4(open_vp);
10640 if ((error = vtodv(open_vp, &dvp, cr, TRUE)) != 0)
10641 return (error);
10642 drp = VTOR4(dvp);
10643
10644 if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR4(dvp))) {
10645 VN_RELE(dvp);
10646 return (EINTR);
10647 }
10648
10649 if ((error = vtoname(open_vp, file_name, MAXNAMELEN)) != 0) {
10650 nfs_rw_exit(&drp->r_rwlock);
10651 VN_RELE(dvp);
10652 return (error);
10653 }
10654
10655 mutex_enter(&rp->r_statev4_lock);
10656 if (rp->created_v4) {
10657 rp->created_v4 = 0;
10658 mutex_exit(&rp->r_statev4_lock);
10659
10660 dnlc_update(dvp, file_name, open_vp);
10661 /* This is needed so we don't bump the open ref count */
10662 just_created = 1;
10663 } else {
10664 mutex_exit(&rp->r_statev4_lock);
10665 just_created = 0;
10666 }
10667
10668 VN_HOLD(map_vp);
10669
10670 error = nfs4open_otw(dvp, file_name, NULL, &open_vp, cr, 0, FREAD, 0,
10671 just_created);
10672 if (error) {
10673 nfs_rw_exit(&drp->r_rwlock);
10674 VN_RELE(dvp);
10675 VN_RELE(map_vp);
10676 return (error);
10677 }
10678
10679 nfs_rw_exit(&drp->r_rwlock);
10680 VN_RELE(dvp);
10681
10682 /*
10683 * If nfs4open_otw() returned a different vnode then "undo"
10684 * the open and return failure to the caller.
10685 */
10686 if (!VN_CMP(open_vp, map_vp)) {
10687 nfs4_error_t e;
10688
10689 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10690 "open returned a different vnode"));
10691 /*
10692 * If there's an error, ignore it,
10693 * and let VOP_INACTIVE handle it.
10694 */
10695 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10696 CLOSE_NORM, 0, 0, 0);
10697 VN_RELE(map_vp);
10698 return (EIO);
10699 }
10700
10701 VN_RELE(map_vp);
10702
10703 oop = find_open_owner(cr, NFS4_PERM_CREATED, VTOMI4(open_vp));
10704 if (!oop) {
10705 nfs4_error_t e;
10706
10707 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "open_and_get_osp: "
10708 "no open owner"));
10709 /*
10710 * If there's an error, ignore it,
10711 * and let VOP_INACTIVE handle it.
10712 */
10713 (void) nfs4close_one(open_vp, NULL, cr, FREAD, NULL, &e,
10714 CLOSE_NORM, 0, 0, 0);
10715 return (EIO);
10716 }
10717 osp = find_open_stream(oop, rp);
10718 open_owner_rele(oop);
10719 *ospp = osp;
10720 return (0);
10721 }
10722
10723 /*
10724 * Please be aware that when this function is called, the address space write
10725 * a_lock is held. Do not put over the wire calls in this function.
10726 */
10727 /* ARGSUSED */
10728 static int
nfs4_addmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)10729 nfs4_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
10730 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
10731 caller_context_t *ct)
10732 {
10733 rnode4_t *rp;
10734 int error = 0;
10735 mntinfo4_t *mi;
10736
10737 mi = VTOMI4(vp);
10738 rp = VTOR4(vp);
10739
10740 if (nfs_zone() != mi->mi_zone)
10741 return (EIO);
10742 if (vp->v_flag & VNOMAP)
10743 return (ENOSYS);
10744
10745 /*
10746 * Don't need to update the open stream first, since this
10747 * mmap can't add any additional share access that isn't
10748 * already contained in the open stream (for the case where we
10749 * open/mmap/only update rp->r_mapcnt/server reboots/reopen doesn't
10750 * take into account os_mmap_read[write] counts).
10751 */
10752 atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
10753
10754 if (vp->v_type == VREG) {
10755 /*
10756 * We need to retrieve the open stream and update the counts.
10757 * If there is no open stream here, something is wrong.
10758 */
10759 nfs4_open_stream_t *osp = NULL;
10760 nfs4_open_owner_t *oop = NULL;
10761
10762 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
10763 if (oop != NULL) {
10764 /* returns with 'os_sync_lock' held */
10765 osp = find_open_stream(oop, rp);
10766 open_owner_rele(oop);
10767 }
10768 if (osp == NULL) {
10769 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE,
10770 "nfs4_addmap: we should have an osp"
10771 "but we don't, so fail with EIO"));
10772 error = EIO;
10773 goto out;
10774 }
10775
10776 NFS4_DEBUG(nfs4_mmap_debug, (CE_NOTE, "nfs4_addmap: osp %p,"
10777 " pages %ld, prot 0x%x", (void *)osp, btopr(len), prot));
10778
10779 /*
10780 * Update the map count in the open stream.
10781 * This is necessary in the case where we
10782 * open/mmap/close/, then the server reboots, and we
10783 * attempt to reopen. If the mmap doesn't add share
10784 * access then we send an invalid reopen with
10785 * access = NONE.
10786 *
10787 * We need to specifically check each PROT_* so a mmap
10788 * call of (PROT_WRITE | PROT_EXEC) will ensure us both
10789 * read and write access. A simple comparison of prot
10790 * to ~PROT_WRITE to determine read access is insufficient
10791 * since prot can be |= with PROT_USER, etc.
10792 */
10793
10794 /*
10795 * Unless we're MAP_SHARED, no sense in adding os_mmap_write
10796 */
10797 if ((flags & MAP_SHARED) && (maxprot & PROT_WRITE))
10798 osp->os_mmap_write += btopr(len);
10799 if (maxprot & PROT_READ)
10800 osp->os_mmap_read += btopr(len);
10801 if (maxprot & PROT_EXEC)
10802 osp->os_mmap_read += btopr(len);
10803 /*
10804 * Ensure that os_mmap_read gets incremented, even if
10805 * maxprot were to look like PROT_NONE.
10806 */
10807 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
10808 !(maxprot & PROT_EXEC))
10809 osp->os_mmap_read += btopr(len);
10810 osp->os_mapcnt += btopr(len);
10811 mutex_exit(&osp->os_sync_lock);
10812 open_stream_rele(osp, rp);
10813 }
10814
10815 out:
10816 /*
10817 * If we got an error, then undo our
10818 * incrementing of 'r_mapcnt'.
10819 */
10820
10821 if (error) {
10822 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(len));
10823 ASSERT(rp->r_mapcnt >= 0);
10824 }
10825 return (error);
10826 }
10827
10828 /* ARGSUSED */
10829 static int
nfs4_cmp(vnode_t * vp1,vnode_t * vp2,caller_context_t * ct)10830 nfs4_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
10831 {
10832
10833 return (VTOR4(vp1) == VTOR4(vp2));
10834 }
10835
10836 /* ARGSUSED */
10837 static int
nfs4_frlock(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,cred_t * cr,caller_context_t * ct)10838 nfs4_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
10839 offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
10840 caller_context_t *ct)
10841 {
10842 int rc;
10843 u_offset_t start, end;
10844 rnode4_t *rp;
10845 int error = 0, intr = INTR4(vp);
10846 nfs4_error_t e;
10847
10848 if (nfs_zone() != VTOMI4(vp)->mi_zone)
10849 return (EIO);
10850
10851 /* check for valid cmd parameter */
10852 switch (cmd) {
10853 case F_FLOCK:
10854 case F_FLOCKW:
10855 case F_OFD_GETLK:
10856 case F_OFD_SETLK:
10857 case F_OFD_SETLKW:
10858 return (EOPNOTSUPP);
10859 case F_GETLK:
10860 case F_SETLK:
10861 case F_SETLKW:
10862 break;
10863 default:
10864 return (EINVAL);
10865 }
10866
10867 /* Verify l_type. */
10868 switch (bfp->l_type) {
10869 case F_RDLCK:
10870 if (cmd != F_GETLK && !(flag & FREAD))
10871 return (EBADF);
10872 break;
10873 case F_WRLCK:
10874 if (cmd != F_GETLK && !(flag & FWRITE))
10875 return (EBADF);
10876 break;
10877 case F_UNLCK:
10878 intr = 0;
10879 break;
10880
10881 default:
10882 return (EINVAL);
10883 }
10884
10885 /* check the validity of the lock range */
10886 if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
10887 return (rc);
10888 if (rc = flk_check_lock_data(start, end, MAXEND))
10889 return (rc);
10890
10891 /*
10892 * If the filesystem is mounted using local locking, pass the
10893 * request off to the local locking code.
10894 */
10895 if (VTOMI4(vp)->mi_flags & MI4_LLOCK || vp->v_type != VREG) {
10896 if (cmd == F_SETLK || cmd == F_SETLKW) {
10897 /*
10898 * For complete safety, we should be holding
10899 * r_lkserlock. However, we can't call
10900 * nfs4_safelock and then fs_frlock while
10901 * holding r_lkserlock, so just invoke
10902 * nfs4_safelock and expect that this will
10903 * catch enough of the cases.
10904 */
10905 if (!nfs4_safelock(vp, bfp, cr))
10906 return (EAGAIN);
10907 }
10908 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
10909 }
10910
10911 rp = VTOR4(vp);
10912
10913 /*
10914 * Check whether the given lock request can proceed, given the
10915 * current file mappings.
10916 */
10917 if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
10918 return (EINTR);
10919 if (cmd == F_SETLK || cmd == F_SETLKW) {
10920 if (!nfs4_safelock(vp, bfp, cr)) {
10921 rc = EAGAIN;
10922 goto done;
10923 }
10924 }
10925
10926 /*
10927 * Flush the cache after waiting for async I/O to finish. For new
10928 * locks, this is so that the process gets the latest bits from the
10929 * server. For unlocks, this is so that other clients see the
10930 * latest bits once the file has been unlocked. If currently dirty
10931 * pages can't be flushed, then don't allow a lock to be set. But
10932 * allow unlocks to succeed, to avoid having orphan locks on the
10933 * server.
10934 */
10935 if (cmd != F_GETLK) {
10936 mutex_enter(&rp->r_statelock);
10937 while (rp->r_count > 0) {
10938 if (intr) {
10939 klwp_t *lwp = ttolwp(curthread);
10940
10941 if (lwp != NULL)
10942 lwp->lwp_nostop++;
10943 if (cv_wait_sig(&rp->r_cv,
10944 &rp->r_statelock) == 0) {
10945 if (lwp != NULL)
10946 lwp->lwp_nostop--;
10947 rc = EINTR;
10948 break;
10949 }
10950 if (lwp != NULL)
10951 lwp->lwp_nostop--;
10952 } else {
10953 cv_wait(&rp->r_cv, &rp->r_statelock);
10954 }
10955 }
10956 mutex_exit(&rp->r_statelock);
10957 if (rc != 0)
10958 goto done;
10959 error = nfs4_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
10960 if (error) {
10961 if (error == ENOSPC || error == EDQUOT) {
10962 mutex_enter(&rp->r_statelock);
10963 if (!rp->r_error)
10964 rp->r_error = error;
10965 mutex_exit(&rp->r_statelock);
10966 }
10967 if (bfp->l_type != F_UNLCK) {
10968 rc = ENOLCK;
10969 goto done;
10970 }
10971 }
10972 }
10973
10974 /*
10975 * Call the lock manager to do the real work of contacting
10976 * the server and obtaining the lock.
10977 */
10978 nfs4frlock(NFS4_LCK_CTYPE_NORM, vp, cmd, bfp, flag, offset,
10979 cr, &e, NULL, NULL);
10980 rc = e.error;
10981
10982 if (rc == 0)
10983 nfs4_lockcompletion(vp, cmd);
10984
10985 done:
10986 nfs_rw_exit(&rp->r_lkserlock);
10987
10988 return (rc);
10989 }
10990
10991 /*
10992 * Free storage space associated with the specified vnode. The portion
10993 * to be freed is specified by bfp->l_start and bfp->l_len (already
10994 * normalized to a "whence" of 0).
10995 *
10996 * This is an experimental facility whose continued existence is not
10997 * guaranteed. Currently, we only support the special case
10998 * of l_len == 0, meaning free to end of file.
10999 */
11000 /* ARGSUSED */
11001 static int
nfs4_space(vnode_t * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)11002 nfs4_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
11003 offset_t offset, cred_t *cr, caller_context_t *ct)
11004 {
11005 int error;
11006
11007 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11008 return (EIO);
11009 ASSERT(vp->v_type == VREG);
11010 if (cmd != F_FREESP)
11011 return (EINVAL);
11012
11013 error = convoff(vp, bfp, 0, offset);
11014 if (!error) {
11015 ASSERT(bfp->l_start >= 0);
11016 if (bfp->l_len == 0) {
11017 struct vattr va;
11018
11019 va.va_mask = AT_SIZE;
11020 va.va_size = bfp->l_start;
11021 error = nfs4setattr(vp, &va, 0, cr, NULL);
11022
11023 if (error == 0 && bfp->l_start == 0)
11024 vnevent_truncate(vp, ct);
11025 } else
11026 error = EINVAL;
11027 }
11028
11029 return (error);
11030 }
11031
11032 /* ARGSUSED */
11033 int
nfs4_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)11034 nfs4_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
11035 {
11036 rnode4_t *rp;
11037 rp = VTOR4(vp);
11038
11039 if (vp->v_type == VREG && IS_SHADOW(vp, rp)) {
11040 vp = RTOV4(rp);
11041 }
11042 *vpp = vp;
11043 return (0);
11044 }
11045
11046 /*
11047 * Setup and add an address space callback to do the work of the delmap call.
11048 * The callback will (and must be) deleted in the actual callback function.
11049 *
11050 * This is done in order to take care of the problem that we have with holding
11051 * the address space's a_lock for a long period of time (e.g. if the NFS server
11052 * is down). Callbacks will be executed in the address space code while the
11053 * a_lock is not held. Holding the address space's a_lock causes things such
11054 * as ps and fork to hang because they are trying to acquire this lock as well.
11055 */
11056 /* ARGSUSED */
11057 static int
nfs4_delmap(vnode_t * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,cred_t * cr,caller_context_t * ct)11058 nfs4_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
11059 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
11060 caller_context_t *ct)
11061 {
11062 int caller_found;
11063 int error;
11064 rnode4_t *rp;
11065 nfs4_delmap_args_t *dmapp;
11066 nfs4_delmapcall_t *delmap_call;
11067
11068 if (vp->v_flag & VNOMAP)
11069 return (ENOSYS);
11070
11071 /*
11072 * A process may not change zones if it has NFS pages mmap'ed
11073 * in, so we can't legitimately get here from the wrong zone.
11074 */
11075 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11076
11077 rp = VTOR4(vp);
11078
11079 /*
11080 * The way that the address space of this process deletes its mapping
11081 * of this file is via the following call chains:
11082 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11083 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs4_delmap()
11084 *
11085 * With the use of address space callbacks we are allowed to drop the
11086 * address space lock, a_lock, while executing the NFS operations that
11087 * need to go over the wire. Returning EAGAIN to the caller of this
11088 * function is what drives the execution of the callback that we add
11089 * below. The callback will be executed by the address space code
11090 * after dropping the a_lock. When the callback is finished, since
11091 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
11092 * is called again on the same segment to finish the rest of the work
11093 * that needs to happen during unmapping.
11094 *
11095 * This action of calling back into the segment driver causes
11096 * nfs4_delmap() to get called again, but since the callback was
11097 * already executed at this point, it already did the work and there
11098 * is nothing left for us to do.
11099 *
11100 * To Summarize:
11101 * - The first time nfs4_delmap is called by the current thread is when
11102 * we add the caller associated with this delmap to the delmap caller
11103 * list, add the callback, and return EAGAIN.
11104 * - The second time in this call chain when nfs4_delmap is called we
11105 * will find this caller in the delmap caller list and realize there
11106 * is no more work to do thus removing this caller from the list and
11107 * returning the error that was set in the callback execution.
11108 */
11109 caller_found = nfs4_find_and_delete_delmapcall(rp, &error);
11110 if (caller_found) {
11111 /*
11112 * 'error' is from the actual delmap operations. To avoid
11113 * hangs, we need to handle the return of EAGAIN differently
11114 * since this is what drives the callback execution.
11115 * In this case, we don't want to return EAGAIN and do the
11116 * callback execution because there are none to execute.
11117 */
11118 if (error == EAGAIN)
11119 return (0);
11120 else
11121 return (error);
11122 }
11123
11124 /* current caller was not in the list */
11125 delmap_call = nfs4_init_delmapcall();
11126
11127 mutex_enter(&rp->r_statelock);
11128 list_insert_tail(&rp->r_indelmap, delmap_call);
11129 mutex_exit(&rp->r_statelock);
11130
11131 dmapp = kmem_alloc(sizeof (nfs4_delmap_args_t), KM_SLEEP);
11132
11133 dmapp->vp = vp;
11134 dmapp->off = off;
11135 dmapp->addr = addr;
11136 dmapp->len = len;
11137 dmapp->prot = prot;
11138 dmapp->maxprot = maxprot;
11139 dmapp->flags = flags;
11140 dmapp->cr = cr;
11141 dmapp->caller = delmap_call;
11142
11143 error = as_add_callback(as, nfs4_delmap_callback, dmapp,
11144 AS_UNMAP_EVENT, addr, len, KM_SLEEP);
11145
11146 return (error ? error : EAGAIN);
11147 }
11148
11149 static nfs4_delmapcall_t *
nfs4_init_delmapcall()11150 nfs4_init_delmapcall()
11151 {
11152 nfs4_delmapcall_t *delmap_call;
11153
11154 delmap_call = kmem_alloc(sizeof (nfs4_delmapcall_t), KM_SLEEP);
11155 delmap_call->call_id = curthread;
11156 delmap_call->error = 0;
11157
11158 return (delmap_call);
11159 }
11160
11161 static void
nfs4_free_delmapcall(nfs4_delmapcall_t * delmap_call)11162 nfs4_free_delmapcall(nfs4_delmapcall_t *delmap_call)
11163 {
11164 kmem_free(delmap_call, sizeof (nfs4_delmapcall_t));
11165 }
11166
11167 /*
11168 * Searches for the current delmap caller (based on curthread) in the list of
11169 * callers. If it is found, we remove it and free the delmap caller.
11170 * Returns:
11171 * 0 if the caller wasn't found
11172 * 1 if the caller was found, removed and freed. *errp will be set
11173 * to what the result of the delmap was.
11174 */
11175 static int
nfs4_find_and_delete_delmapcall(rnode4_t * rp,int * errp)11176 nfs4_find_and_delete_delmapcall(rnode4_t *rp, int *errp)
11177 {
11178 nfs4_delmapcall_t *delmap_call;
11179
11180 /*
11181 * If the list doesn't exist yet, we create it and return
11182 * that the caller wasn't found. No list = no callers.
11183 */
11184 mutex_enter(&rp->r_statelock);
11185 if (!(rp->r_flags & R4DELMAPLIST)) {
11186 /* The list does not exist */
11187 list_create(&rp->r_indelmap, sizeof (nfs4_delmapcall_t),
11188 offsetof(nfs4_delmapcall_t, call_node));
11189 rp->r_flags |= R4DELMAPLIST;
11190 mutex_exit(&rp->r_statelock);
11191 return (0);
11192 } else {
11193 /* The list exists so search it */
11194 for (delmap_call = list_head(&rp->r_indelmap);
11195 delmap_call != NULL;
11196 delmap_call = list_next(&rp->r_indelmap, delmap_call)) {
11197 if (delmap_call->call_id == curthread) {
11198 /* current caller is in the list */
11199 *errp = delmap_call->error;
11200 list_remove(&rp->r_indelmap, delmap_call);
11201 mutex_exit(&rp->r_statelock);
11202 nfs4_free_delmapcall(delmap_call);
11203 return (1);
11204 }
11205 }
11206 }
11207 mutex_exit(&rp->r_statelock);
11208 return (0);
11209 }
11210
11211 /*
11212 * Remove some pages from an mmap'd vnode. Just update the
11213 * count of pages. If doing close-to-open, then flush and
11214 * commit all of the pages associated with this file.
11215 * Otherwise, start an asynchronous page flush to write out
11216 * any dirty pages. This will also associate a credential
11217 * with the rnode which can be used to write the pages.
11218 */
11219 /* ARGSUSED */
11220 static void
nfs4_delmap_callback(struct as * as,void * arg,uint_t event)11221 nfs4_delmap_callback(struct as *as, void *arg, uint_t event)
11222 {
11223 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11224 rnode4_t *rp;
11225 mntinfo4_t *mi;
11226 nfs4_delmap_args_t *dmapp = (nfs4_delmap_args_t *)arg;
11227
11228 rp = VTOR4(dmapp->vp);
11229 mi = VTOMI4(dmapp->vp);
11230
11231 atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
11232 ASSERT(rp->r_mapcnt >= 0);
11233
11234 /*
11235 * Initiate a page flush and potential commit if there are
11236 * pages, the file system was not mounted readonly, the segment
11237 * was mapped shared, and the pages themselves were writeable.
11238 */
11239 if (nfs4_has_pages(dmapp->vp) &&
11240 !(dmapp->vp->v_vfsp->vfs_flag & VFS_RDONLY) &&
11241 dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
11242 mutex_enter(&rp->r_statelock);
11243 rp->r_flags |= R4DIRTY;
11244 mutex_exit(&rp->r_statelock);
11245 e.error = nfs4_putpage_commit(dmapp->vp, dmapp->off,
11246 dmapp->len, dmapp->cr);
11247 if (!e.error) {
11248 mutex_enter(&rp->r_statelock);
11249 e.error = rp->r_error;
11250 rp->r_error = 0;
11251 mutex_exit(&rp->r_statelock);
11252 }
11253 } else
11254 e.error = 0;
11255
11256 if ((rp->r_flags & R4DIRECTIO) || (mi->mi_flags & MI4_DIRECTIO))
11257 (void) nfs4_putpage(dmapp->vp, dmapp->off, dmapp->len,
11258 B_INVAL, dmapp->cr, NULL);
11259
11260 if (e.error) {
11261 e.stat = puterrno4(e.error);
11262 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11263 OP_COMMIT, FALSE, NULL, 0, dmapp->vp);
11264 dmapp->caller->error = e.error;
11265 }
11266
11267 /* Check to see if we need to close the file */
11268
11269 if (dmapp->vp->v_type == VREG) {
11270 nfs4close_one(dmapp->vp, NULL, dmapp->cr, 0, NULL, &e,
11271 CLOSE_DELMAP, dmapp->len, dmapp->maxprot, dmapp->flags);
11272
11273 if (e.error != 0 || e.stat != NFS4_OK) {
11274 /*
11275 * Since it is possible that e.error == 0 and
11276 * e.stat != NFS4_OK (and vice versa),
11277 * we do the proper checking in order to get both
11278 * e.error and e.stat reporting the correct info.
11279 */
11280 if (e.stat == NFS4_OK)
11281 e.stat = puterrno4(e.error);
11282 if (e.error == 0)
11283 e.error = geterrno4(e.stat);
11284
11285 nfs4_queue_fact(RF_DELMAP_CB_ERR, mi, e.stat, 0,
11286 OP_CLOSE, FALSE, NULL, 0, dmapp->vp);
11287 dmapp->caller->error = e.error;
11288 }
11289 }
11290
11291 (void) as_delete_callback(as, arg);
11292 kmem_free(dmapp, sizeof (nfs4_delmap_args_t));
11293 }
11294
11295
11296 static uint_t
fattr4_maxfilesize_to_bits(uint64_t ll)11297 fattr4_maxfilesize_to_bits(uint64_t ll)
11298 {
11299 uint_t l = 1;
11300
11301 if (ll == 0) {
11302 return (0);
11303 }
11304
11305 if (ll & 0xffffffff00000000) {
11306 l += 32; ll >>= 32;
11307 }
11308 if (ll & 0xffff0000) {
11309 l += 16; ll >>= 16;
11310 }
11311 if (ll & 0xff00) {
11312 l += 8; ll >>= 8;
11313 }
11314 if (ll & 0xf0) {
11315 l += 4; ll >>= 4;
11316 }
11317 if (ll & 0xc) {
11318 l += 2; ll >>= 2;
11319 }
11320 if (ll & 0x2) {
11321 l += 1;
11322 }
11323 return (l);
11324 }
11325
11326 static int
nfs4_have_xattrs(vnode_t * vp,ulong_t * valp,cred_t * cr)11327 nfs4_have_xattrs(vnode_t *vp, ulong_t *valp, cred_t *cr)
11328 {
11329 vnode_t *avp = NULL;
11330 int error;
11331
11332 if ((error = nfs4lookup_xattr(vp, "", &avp,
11333 LOOKUP_XATTR, cr)) == 0)
11334 error = do_xattr_exists_check(avp, valp, cr);
11335 if (avp)
11336 VN_RELE(avp);
11337
11338 return (error);
11339 }
11340
11341 /* ARGSUSED */
11342 int
nfs4_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)11343 nfs4_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
11344 caller_context_t *ct)
11345 {
11346 int error;
11347 hrtime_t t;
11348 rnode4_t *rp;
11349 nfs4_ga_res_t gar;
11350 nfs4_ga_ext_res_t ger;
11351
11352 gar.n4g_ext_res = &ger;
11353
11354 if (nfs_zone() != VTOMI4(vp)->mi_zone)
11355 return (EIO);
11356 if (cmd == _PC_PATH_MAX || cmd == _PC_SYMLINK_MAX) {
11357 *valp = MAXPATHLEN;
11358 return (0);
11359 }
11360 if (cmd == _PC_ACL_ENABLED) {
11361 *valp = _ACL_ACE_ENABLED;
11362 return (0);
11363 }
11364
11365 rp = VTOR4(vp);
11366 if (cmd == _PC_XATTR_EXISTS) {
11367 /*
11368 * The existence of the xattr directory is not sufficient
11369 * for determining whether generic user attributes exists.
11370 * The attribute directory could only be a transient directory
11371 * used for Solaris sysattr support. Do a small readdir
11372 * to verify if the only entries are sysattrs or not.
11373 *
11374 * pc4_xattr_valid can be only be trusted when r_xattr_dir
11375 * is NULL. Once the xadir vp exists, we can create xattrs,
11376 * and we don't have any way to update the "base" object's
11377 * pc4_xattr_exists from the xattr or xadir. Maybe FEM
11378 * could help out.
11379 */
11380 if (ATTRCACHE4_VALID(vp) && rp->r_pathconf.pc4_xattr_valid &&
11381 rp->r_xattr_dir == NULL) {
11382 return (nfs4_have_xattrs(vp, valp, cr));
11383 }
11384 } else { /* OLD CODE */
11385 if (ATTRCACHE4_VALID(vp)) {
11386 mutex_enter(&rp->r_statelock);
11387 if (rp->r_pathconf.pc4_cache_valid) {
11388 error = 0;
11389 switch (cmd) {
11390 case _PC_FILESIZEBITS:
11391 *valp =
11392 rp->r_pathconf.pc4_filesizebits;
11393 break;
11394 case _PC_LINK_MAX:
11395 *valp =
11396 rp->r_pathconf.pc4_link_max;
11397 break;
11398 case _PC_NAME_MAX:
11399 *valp =
11400 rp->r_pathconf.pc4_name_max;
11401 break;
11402 case _PC_CHOWN_RESTRICTED:
11403 *valp =
11404 rp->r_pathconf.pc4_chown_restricted;
11405 break;
11406 case _PC_NO_TRUNC:
11407 *valp =
11408 rp->r_pathconf.pc4_no_trunc;
11409 break;
11410 default:
11411 error = EINVAL;
11412 break;
11413 }
11414 mutex_exit(&rp->r_statelock);
11415 #ifdef DEBUG
11416 nfs4_pathconf_cache_hits++;
11417 #endif
11418 return (error);
11419 }
11420 mutex_exit(&rp->r_statelock);
11421 }
11422 }
11423 #ifdef DEBUG
11424 nfs4_pathconf_cache_misses++;
11425 #endif
11426
11427 t = gethrtime();
11428
11429 error = nfs4_attr_otw(vp, TAG_PATHCONF, &gar, NFS4_PATHCONF_MASK, cr);
11430
11431 if (error) {
11432 mutex_enter(&rp->r_statelock);
11433 rp->r_pathconf.pc4_cache_valid = FALSE;
11434 rp->r_pathconf.pc4_xattr_valid = FALSE;
11435 mutex_exit(&rp->r_statelock);
11436 return (error);
11437 }
11438
11439 /* interpret the max filesize */
11440 gar.n4g_ext_res->n4g_pc4.pc4_filesizebits =
11441 fattr4_maxfilesize_to_bits(gar.n4g_ext_res->n4g_maxfilesize);
11442
11443 /* Store the attributes we just received */
11444 nfs4_attr_cache(vp, &gar, t, cr, TRUE, NULL);
11445
11446 switch (cmd) {
11447 case _PC_FILESIZEBITS:
11448 *valp = gar.n4g_ext_res->n4g_pc4.pc4_filesizebits;
11449 break;
11450 case _PC_LINK_MAX:
11451 *valp = gar.n4g_ext_res->n4g_pc4.pc4_link_max;
11452 break;
11453 case _PC_NAME_MAX:
11454 *valp = gar.n4g_ext_res->n4g_pc4.pc4_name_max;
11455 break;
11456 case _PC_CHOWN_RESTRICTED:
11457 *valp = gar.n4g_ext_res->n4g_pc4.pc4_chown_restricted;
11458 break;
11459 case _PC_NO_TRUNC:
11460 *valp = gar.n4g_ext_res->n4g_pc4.pc4_no_trunc;
11461 break;
11462 case _PC_XATTR_EXISTS:
11463 if (gar.n4g_ext_res->n4g_pc4.pc4_xattr_exists) {
11464 if (error = nfs4_have_xattrs(vp, valp, cr))
11465 return (error);
11466 }
11467 break;
11468 default:
11469 return (EINVAL);
11470 }
11471
11472 return (0);
11473 }
11474
11475 /*
11476 * Called by async thread to do synchronous pageio. Do the i/o, wait
11477 * for it to complete, and cleanup the page list when done.
11478 */
11479 static int
nfs4_sync_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr)11480 nfs4_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11481 int flags, cred_t *cr)
11482 {
11483 int error;
11484
11485 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11486
11487 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11488 if (flags & B_READ)
11489 pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
11490 else
11491 pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
11492 return (error);
11493 }
11494
11495 /* ARGSUSED */
11496 static int
nfs4_pageio(vnode_t * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,cred_t * cr,caller_context_t * ct)11497 nfs4_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
11498 int flags, cred_t *cr, caller_context_t *ct)
11499 {
11500 int error;
11501 rnode4_t *rp;
11502
11503 if (!(flags & B_ASYNC) && nfs_zone() != VTOMI4(vp)->mi_zone)
11504 return (EIO);
11505
11506 if (pp == NULL)
11507 return (EINVAL);
11508
11509 rp = VTOR4(vp);
11510 mutex_enter(&rp->r_statelock);
11511 rp->r_count++;
11512 mutex_exit(&rp->r_statelock);
11513
11514 if (flags & B_ASYNC) {
11515 error = nfs4_async_pageio(vp, pp, io_off, io_len, flags, cr,
11516 nfs4_sync_pageio);
11517 } else
11518 error = nfs4_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
11519 mutex_enter(&rp->r_statelock);
11520 rp->r_count--;
11521 cv_broadcast(&rp->r_cv);
11522 mutex_exit(&rp->r_statelock);
11523 return (error);
11524 }
11525
11526 /* ARGSUSED */
11527 static void
nfs4_dispose(vnode_t * vp,page_t * pp,int fl,int dn,cred_t * cr,caller_context_t * ct)11528 nfs4_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
11529 caller_context_t *ct)
11530 {
11531 int error;
11532 rnode4_t *rp;
11533 page_t *plist;
11534 page_t *pptr;
11535 offset3 offset;
11536 count3 len;
11537 k_sigset_t smask;
11538
11539 /*
11540 * We should get called with fl equal to either B_FREE or
11541 * B_INVAL. Any other value is illegal.
11542 *
11543 * The page that we are either supposed to free or destroy
11544 * should be exclusive locked and its io lock should not
11545 * be held.
11546 */
11547 ASSERT(fl == B_FREE || fl == B_INVAL);
11548 ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
11549
11550 rp = VTOR4(vp);
11551
11552 /*
11553 * If the page doesn't need to be committed or we shouldn't
11554 * even bother attempting to commit it, then just make sure
11555 * that the p_fsdata byte is clear and then either free or
11556 * destroy the page as appropriate.
11557 */
11558 if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & R4STALE)) {
11559 pp->p_fsdata = C_NOCOMMIT;
11560 if (fl == B_FREE)
11561 page_free(pp, dn);
11562 else
11563 page_destroy(pp, dn);
11564 return;
11565 }
11566
11567 /*
11568 * If there is a page invalidation operation going on, then
11569 * if this is one of the pages being destroyed, then just
11570 * clear the p_fsdata byte and then either free or destroy
11571 * the page as appropriate.
11572 */
11573 mutex_enter(&rp->r_statelock);
11574 if ((rp->r_flags & R4TRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
11575 mutex_exit(&rp->r_statelock);
11576 pp->p_fsdata = C_NOCOMMIT;
11577 if (fl == B_FREE)
11578 page_free(pp, dn);
11579 else
11580 page_destroy(pp, dn);
11581 return;
11582 }
11583
11584 /*
11585 * If we are freeing this page and someone else is already
11586 * waiting to do a commit, then just unlock the page and
11587 * return. That other thread will take care of commiting
11588 * this page. The page can be freed sometime after the
11589 * commit has finished. Otherwise, if the page is marked
11590 * as delay commit, then we may be getting called from
11591 * pvn_write_done, one page at a time. This could result
11592 * in one commit per page, so we end up doing lots of small
11593 * commits instead of fewer larger commits. This is bad,
11594 * we want do as few commits as possible.
11595 */
11596 if (fl == B_FREE) {
11597 if (rp->r_flags & R4COMMITWAIT) {
11598 page_unlock(pp);
11599 mutex_exit(&rp->r_statelock);
11600 return;
11601 }
11602 if (pp->p_fsdata == C_DELAYCOMMIT) {
11603 pp->p_fsdata = C_COMMIT;
11604 page_unlock(pp);
11605 mutex_exit(&rp->r_statelock);
11606 return;
11607 }
11608 }
11609
11610 /*
11611 * Check to see if there is a signal which would prevent an
11612 * attempt to commit the pages from being successful. If so,
11613 * then don't bother with all of the work to gather pages and
11614 * generate the unsuccessful RPC. Just return from here and
11615 * let the page be committed at some later time.
11616 */
11617 sigintr(&smask, VTOMI4(vp)->mi_flags & MI4_INT);
11618 if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
11619 sigunintr(&smask);
11620 page_unlock(pp);
11621 mutex_exit(&rp->r_statelock);
11622 return;
11623 }
11624 sigunintr(&smask);
11625
11626 /*
11627 * We are starting to need to commit pages, so let's try
11628 * to commit as many as possible at once to reduce the
11629 * overhead.
11630 *
11631 * Set the `commit inprogress' state bit. We must
11632 * first wait until any current one finishes. Then
11633 * we initialize the c_pages list with this page.
11634 */
11635 while (rp->r_flags & R4COMMIT) {
11636 rp->r_flags |= R4COMMITWAIT;
11637 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
11638 rp->r_flags &= ~R4COMMITWAIT;
11639 }
11640 rp->r_flags |= R4COMMIT;
11641 mutex_exit(&rp->r_statelock);
11642 ASSERT(rp->r_commit.c_pages == NULL);
11643 rp->r_commit.c_pages = pp;
11644 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11645 rp->r_commit.c_commlen = PAGESIZE;
11646
11647 /*
11648 * Gather together all other pages which can be committed.
11649 * They will all be chained off r_commit.c_pages.
11650 */
11651 nfs4_get_commit(vp);
11652
11653 /*
11654 * Clear the `commit inprogress' status and disconnect
11655 * the list of pages to be committed from the rnode.
11656 * At this same time, we also save the starting offset
11657 * and length of data to be committed on the server.
11658 */
11659 plist = rp->r_commit.c_pages;
11660 rp->r_commit.c_pages = NULL;
11661 offset = rp->r_commit.c_commbase;
11662 len = rp->r_commit.c_commlen;
11663 mutex_enter(&rp->r_statelock);
11664 rp->r_flags &= ~R4COMMIT;
11665 cv_broadcast(&rp->r_commit.c_cv);
11666 mutex_exit(&rp->r_statelock);
11667
11668 if (curproc == proc_pageout || curproc == proc_fsflush ||
11669 nfs_zone() != VTOMI4(vp)->mi_zone) {
11670 nfs4_async_commit(vp, plist, offset, len,
11671 cr, do_nfs4_async_commit);
11672 return;
11673 }
11674
11675 /*
11676 * Actually generate the COMMIT op over the wire operation.
11677 */
11678 error = nfs4_commit(vp, (offset4)offset, (count4)len, cr);
11679
11680 /*
11681 * If we got an error during the commit, just unlock all
11682 * of the pages. The pages will get retransmitted to the
11683 * server during a putpage operation.
11684 */
11685 if (error) {
11686 while (plist != NULL) {
11687 pptr = plist;
11688 page_sub(&plist, pptr);
11689 page_unlock(pptr);
11690 }
11691 return;
11692 }
11693
11694 /*
11695 * We've tried as hard as we can to commit the data to stable
11696 * storage on the server. We just unlock the rest of the pages
11697 * and clear the commit required state. They will be put
11698 * onto the tail of the cachelist if they are nolonger
11699 * mapped.
11700 */
11701 while (plist != pp) {
11702 pptr = plist;
11703 page_sub(&plist, pptr);
11704 pptr->p_fsdata = C_NOCOMMIT;
11705 page_unlock(pptr);
11706 }
11707
11708 /*
11709 * It is possible that nfs4_commit didn't return error but
11710 * some other thread has modified the page we are going
11711 * to free/destroy.
11712 * In this case we need to rewrite the page. Do an explicit check
11713 * before attempting to free/destroy the page. If modified, needs to
11714 * be rewritten so unlock the page and return.
11715 */
11716 if (hat_ismod(pp)) {
11717 pp->p_fsdata = C_NOCOMMIT;
11718 page_unlock(pp);
11719 return;
11720 }
11721
11722 /*
11723 * Now, as appropriate, either free or destroy the page
11724 * that we were called with.
11725 */
11726 pp->p_fsdata = C_NOCOMMIT;
11727 if (fl == B_FREE)
11728 page_free(pp, dn);
11729 else
11730 page_destroy(pp, dn);
11731 }
11732
11733 /*
11734 * Commit requires that the current fh be the file written to.
11735 * The compound op structure is:
11736 * PUTFH(file), COMMIT
11737 */
11738 static int
nfs4_commit(vnode_t * vp,offset4 offset,count4 count,cred_t * cr)11739 nfs4_commit(vnode_t *vp, offset4 offset, count4 count, cred_t *cr)
11740 {
11741 COMPOUND4args_clnt args;
11742 COMPOUND4res_clnt res;
11743 COMMIT4res *cm_res;
11744 nfs_argop4 argop[2];
11745 nfs_resop4 *resop;
11746 int doqueue;
11747 mntinfo4_t *mi;
11748 rnode4_t *rp;
11749 cred_t *cred_otw = NULL;
11750 bool_t needrecov = FALSE;
11751 nfs4_recov_state_t recov_state;
11752 nfs4_open_stream_t *osp = NULL;
11753 bool_t first_time = TRUE; /* first time getting OTW cred */
11754 bool_t last_time = FALSE; /* last time getting OTW cred */
11755 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
11756
11757 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11758
11759 rp = VTOR4(vp);
11760
11761 mi = VTOMI4(vp);
11762 recov_state.rs_flags = 0;
11763 recov_state.rs_num_retry_despite_err = 0;
11764 get_commit_cred:
11765 /*
11766 * Releases the osp, if a valid open stream is provided.
11767 * Puts a hold on the cred_otw and the new osp (if found).
11768 */
11769 cred_otw = nfs4_get_otw_cred_by_osp(rp, cr, &osp,
11770 &first_time, &last_time);
11771 args.ctag = TAG_COMMIT;
11772 recov_retry:
11773 /*
11774 * Commit ops: putfh file; commit
11775 */
11776 args.array_len = 2;
11777 args.array = argop;
11778
11779 e.error = nfs4_start_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11780 &recov_state, NULL);
11781 if (e.error) {
11782 crfree(cred_otw);
11783 if (osp != NULL)
11784 open_stream_rele(osp, rp);
11785 return (e.error);
11786 }
11787
11788 /* putfh directory */
11789 argop[0].argop = OP_CPUTFH;
11790 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
11791
11792 /* commit */
11793 argop[1].argop = OP_COMMIT;
11794 argop[1].nfs_argop4_u.opcommit.offset = offset;
11795 argop[1].nfs_argop4_u.opcommit.count = count;
11796
11797 doqueue = 1;
11798 rfs4call(mi, &args, &res, cred_otw, &doqueue, 0, &e);
11799
11800 needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
11801 if (!needrecov && e.error) {
11802 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state,
11803 needrecov);
11804 crfree(cred_otw);
11805 if (e.error == EACCES && last_time == FALSE)
11806 goto get_commit_cred;
11807 if (osp != NULL)
11808 open_stream_rele(osp, rp);
11809 return (e.error);
11810 }
11811
11812 if (needrecov) {
11813 if (nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
11814 NULL, OP_COMMIT, NULL, NULL, NULL) == FALSE) {
11815 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11816 &recov_state, needrecov);
11817 if (!e.error)
11818 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11819 goto recov_retry;
11820 }
11821 if (e.error) {
11822 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11823 &recov_state, needrecov);
11824 crfree(cred_otw);
11825 if (osp != NULL)
11826 open_stream_rele(osp, rp);
11827 return (e.error);
11828 }
11829 /* fall through for res.status case */
11830 }
11831
11832 if (res.status) {
11833 e.error = geterrno4(res.status);
11834 if (e.error == EACCES && last_time == FALSE) {
11835 crfree(cred_otw);
11836 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11837 &recov_state, needrecov);
11838 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11839 goto get_commit_cred;
11840 }
11841 /*
11842 * Can't do a nfs4_purge_stale_fh here because this
11843 * can cause a deadlock. nfs4_commit can
11844 * be called from nfs4_dispose which can be called
11845 * indirectly via pvn_vplist_dirty. nfs4_purge_stale_fh
11846 * can call back to pvn_vplist_dirty.
11847 */
11848 if (e.error == ESTALE) {
11849 mutex_enter(&rp->r_statelock);
11850 rp->r_flags |= R4STALE;
11851 if (!rp->r_error)
11852 rp->r_error = e.error;
11853 mutex_exit(&rp->r_statelock);
11854 PURGE_ATTRCACHE4(vp);
11855 } else {
11856 mutex_enter(&rp->r_statelock);
11857 if (!rp->r_error)
11858 rp->r_error = e.error;
11859 mutex_exit(&rp->r_statelock);
11860 }
11861 } else {
11862 ASSERT(rp->r_flags & R4HAVEVERF);
11863 resop = &res.array[1]; /* commit res */
11864 cm_res = &resop->nfs_resop4_u.opcommit;
11865 mutex_enter(&rp->r_statelock);
11866 if (cm_res->writeverf == rp->r_writeverf) {
11867 mutex_exit(&rp->r_statelock);
11868 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11869 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT,
11870 &recov_state, needrecov);
11871 crfree(cred_otw);
11872 if (osp != NULL)
11873 open_stream_rele(osp, rp);
11874 return (0);
11875 }
11876 nfs4_set_mod(vp);
11877 rp->r_writeverf = cm_res->writeverf;
11878 mutex_exit(&rp->r_statelock);
11879 e.error = NFS_VERF_MISMATCH;
11880 }
11881
11882 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
11883 nfs4_end_fop(VTOMI4(vp), vp, NULL, OH_COMMIT, &recov_state, needrecov);
11884 crfree(cred_otw);
11885 if (osp != NULL)
11886 open_stream_rele(osp, rp);
11887
11888 return (e.error);
11889 }
11890
11891 static void
nfs4_set_mod(vnode_t * vp)11892 nfs4_set_mod(vnode_t *vp)
11893 {
11894 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
11895
11896 /* make sure we're looking at the master vnode, not a shadow */
11897 pvn_vplist_setdirty(RTOV4(VTOR4(vp)), nfs_setmod_check);
11898 }
11899
11900 /*
11901 * This function is used to gather a page list of the pages which
11902 * can be committed on the server.
11903 *
11904 * The calling thread must have set R4COMMIT. This bit is used to
11905 * serialize access to the commit structure in the rnode. As long
11906 * as the thread has set R4COMMIT, then it can manipulate the commit
11907 * structure without requiring any other locks.
11908 *
11909 * When this function is called from nfs4_dispose() the page passed
11910 * into nfs4_dispose() will be SE_EXCL locked, and so this function
11911 * will skip it. This is not a problem since we initially add the
11912 * page to the r_commit page list.
11913 *
11914 */
11915 static void
nfs4_get_commit(vnode_t * vp)11916 nfs4_get_commit(vnode_t *vp)
11917 {
11918 rnode4_t *rp;
11919 page_t *pp;
11920 kmutex_t *vphm;
11921
11922 rp = VTOR4(vp);
11923
11924 ASSERT(rp->r_flags & R4COMMIT);
11925
11926 /* make sure we're looking at the master vnode, not a shadow */
11927
11928 if (IS_SHADOW(vp, rp))
11929 vp = RTOV4(rp);
11930
11931 vphm = page_vnode_mutex(vp);
11932 mutex_enter(vphm);
11933
11934 /*
11935 * If there are no pages associated with this vnode, then
11936 * just return.
11937 */
11938 if ((pp = vp->v_pages) == NULL) {
11939 mutex_exit(vphm);
11940 return;
11941 }
11942
11943 /*
11944 * Step through all of the pages associated with this vnode
11945 * looking for pages which need to be committed.
11946 */
11947 do {
11948 /* Skip marker pages. */
11949 if (pp->p_hash == PVN_VPLIST_HASH_TAG)
11950 continue;
11951
11952 /*
11953 * First short-cut everything (without the page_lock)
11954 * and see if this page does not need to be committed
11955 * or is modified if so then we'll just skip it.
11956 */
11957 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
11958 continue;
11959
11960 /*
11961 * Attempt to lock the page. If we can't, then
11962 * someone else is messing with it or we have been
11963 * called from nfs4_dispose and this is the page that
11964 * nfs4_dispose was called with.. anyway just skip it.
11965 */
11966 if (!page_trylock(pp, SE_EXCL))
11967 continue;
11968
11969 /*
11970 * Lets check again now that we have the page lock.
11971 */
11972 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
11973 page_unlock(pp);
11974 continue;
11975 }
11976
11977 /* this had better not be a free page */
11978 ASSERT(PP_ISFREE(pp) == 0);
11979
11980 /*
11981 * The page needs to be committed and we locked it.
11982 * Update the base and length parameters and add it
11983 * to r_pages.
11984 */
11985 if (rp->r_commit.c_pages == NULL) {
11986 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11987 rp->r_commit.c_commlen = PAGESIZE;
11988 } else if (pp->p_offset < rp->r_commit.c_commbase) {
11989 rp->r_commit.c_commlen = rp->r_commit.c_commbase -
11990 (offset3)pp->p_offset + rp->r_commit.c_commlen;
11991 rp->r_commit.c_commbase = (offset3)pp->p_offset;
11992 } else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
11993 <= pp->p_offset) {
11994 rp->r_commit.c_commlen = (offset3)pp->p_offset -
11995 rp->r_commit.c_commbase + PAGESIZE;
11996 }
11997 page_add(&rp->r_commit.c_pages, pp);
11998 } while ((pp = pp->p_vpnext) != vp->v_pages);
11999
12000 mutex_exit(vphm);
12001 }
12002
12003 /*
12004 * This routine is used to gather together a page list of the pages
12005 * which are to be committed on the server. This routine must not
12006 * be called if the calling thread holds any locked pages.
12007 *
12008 * The calling thread must have set R4COMMIT. This bit is used to
12009 * serialize access to the commit structure in the rnode. As long
12010 * as the thread has set R4COMMIT, then it can manipulate the commit
12011 * structure without requiring any other locks.
12012 */
12013 static void
nfs4_get_commit_range(vnode_t * vp,u_offset_t soff,size_t len)12014 nfs4_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
12015 {
12016
12017 rnode4_t *rp;
12018 page_t *pp;
12019 u_offset_t end;
12020 u_offset_t off;
12021 ASSERT(len != 0);
12022 rp = VTOR4(vp);
12023 ASSERT(rp->r_flags & R4COMMIT);
12024
12025 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12026
12027 /* make sure we're looking at the master vnode, not a shadow */
12028
12029 if (IS_SHADOW(vp, rp))
12030 vp = RTOV4(rp);
12031
12032 /*
12033 * If there are no pages associated with this vnode, then
12034 * just return.
12035 */
12036 if ((pp = vp->v_pages) == NULL)
12037 return;
12038 /*
12039 * Calculate the ending offset.
12040 */
12041 end = soff + len;
12042 for (off = soff; off < end; off += PAGESIZE) {
12043 /*
12044 * Lookup each page by vp, offset.
12045 */
12046 if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
12047 continue;
12048 /*
12049 * If this page does not need to be committed or is
12050 * modified, then just skip it.
12051 */
12052 if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
12053 page_unlock(pp);
12054 continue;
12055 }
12056
12057 ASSERT(PP_ISFREE(pp) == 0);
12058 /*
12059 * The page needs to be committed and we locked it.
12060 * Update the base and length parameters and add it
12061 * to r_pages.
12062 */
12063 if (rp->r_commit.c_pages == NULL) {
12064 rp->r_commit.c_commbase = (offset3)pp->p_offset;
12065 rp->r_commit.c_commlen = PAGESIZE;
12066 } else {
12067 rp->r_commit.c_commlen = (offset3)pp->p_offset -
12068 rp->r_commit.c_commbase + PAGESIZE;
12069 }
12070 page_add(&rp->r_commit.c_pages, pp);
12071 }
12072 }
12073
12074 /*
12075 * Called from nfs4_close(), nfs4_fsync() and nfs4_delmap().
12076 * Flushes and commits data to the server.
12077 */
12078 static int
nfs4_putpage_commit(vnode_t * vp,offset_t poff,size_t plen,cred_t * cr)12079 nfs4_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
12080 {
12081 int error;
12082 verifier4 write_verf;
12083 rnode4_t *rp = VTOR4(vp);
12084
12085 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12086
12087 /*
12088 * Flush the data portion of the file and then commit any
12089 * portions which need to be committed. This may need to
12090 * be done twice if the server has changed state since
12091 * data was last written. The data will need to be
12092 * rewritten to the server and then a new commit done.
12093 *
12094 * In fact, this may need to be done several times if the
12095 * server is having problems and crashing while we are
12096 * attempting to do this.
12097 */
12098
12099 top:
12100 /*
12101 * Do a flush based on the poff and plen arguments. This
12102 * will synchronously write out any modified pages in the
12103 * range specified by (poff, plen). This starts all of the
12104 * i/o operations which will be waited for in the next
12105 * call to nfs4_putpage
12106 */
12107
12108 mutex_enter(&rp->r_statelock);
12109 write_verf = rp->r_writeverf;
12110 mutex_exit(&rp->r_statelock);
12111
12112 error = nfs4_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
12113 if (error == EAGAIN)
12114 error = 0;
12115
12116 /*
12117 * Do a flush based on the poff and plen arguments. This
12118 * will synchronously write out any modified pages in the
12119 * range specified by (poff, plen) and wait until all of
12120 * the asynchronous i/o's in that range are done as well.
12121 */
12122 if (!error)
12123 error = nfs4_putpage(vp, poff, plen, 0, cr, NULL);
12124
12125 if (error)
12126 return (error);
12127
12128 mutex_enter(&rp->r_statelock);
12129 if (rp->r_writeverf != write_verf) {
12130 mutex_exit(&rp->r_statelock);
12131 goto top;
12132 }
12133 mutex_exit(&rp->r_statelock);
12134
12135 /*
12136 * Now commit any pages which might need to be committed.
12137 * If the error, NFS_VERF_MISMATCH, is returned, then
12138 * start over with the flush operation.
12139 */
12140 error = nfs4_commit_vp(vp, poff, plen, cr, NFS4_WRITE_WAIT);
12141
12142 if (error == NFS_VERF_MISMATCH)
12143 goto top;
12144
12145 return (error);
12146 }
12147
12148 /*
12149 * nfs4_commit_vp() will wait for other pending commits and
12150 * will either commit the whole file or a range, plen dictates
12151 * if we commit whole file. a value of zero indicates the whole
12152 * file. Called from nfs4_putpage_commit() or nfs4_sync_putapage()
12153 */
12154 static int
nfs4_commit_vp(vnode_t * vp,u_offset_t poff,size_t plen,cred_t * cr,int wait_on_writes)12155 nfs4_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen,
12156 cred_t *cr, int wait_on_writes)
12157 {
12158 rnode4_t *rp;
12159 page_t *plist;
12160 offset3 offset;
12161 count3 len;
12162
12163 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12164
12165 rp = VTOR4(vp);
12166
12167 /*
12168 * before we gather commitable pages make
12169 * sure there are no outstanding async writes
12170 */
12171 if (rp->r_count && wait_on_writes == NFS4_WRITE_WAIT) {
12172 mutex_enter(&rp->r_statelock);
12173 while (rp->r_count > 0) {
12174 cv_wait(&rp->r_cv, &rp->r_statelock);
12175 }
12176 mutex_exit(&rp->r_statelock);
12177 }
12178
12179 /*
12180 * Set the `commit inprogress' state bit. We must
12181 * first wait until any current one finishes.
12182 */
12183 mutex_enter(&rp->r_statelock);
12184 while (rp->r_flags & R4COMMIT) {
12185 rp->r_flags |= R4COMMITWAIT;
12186 cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
12187 rp->r_flags &= ~R4COMMITWAIT;
12188 }
12189 rp->r_flags |= R4COMMIT;
12190 mutex_exit(&rp->r_statelock);
12191
12192 /*
12193 * Gather all of the pages which need to be
12194 * committed.
12195 */
12196 if (plen == 0)
12197 nfs4_get_commit(vp);
12198 else
12199 nfs4_get_commit_range(vp, poff, plen);
12200
12201 /*
12202 * Clear the `commit inprogress' bit and disconnect the
12203 * page list which was gathered by nfs4_get_commit.
12204 */
12205 plist = rp->r_commit.c_pages;
12206 rp->r_commit.c_pages = NULL;
12207 offset = rp->r_commit.c_commbase;
12208 len = rp->r_commit.c_commlen;
12209 mutex_enter(&rp->r_statelock);
12210 rp->r_flags &= ~R4COMMIT;
12211 cv_broadcast(&rp->r_commit.c_cv);
12212 mutex_exit(&rp->r_statelock);
12213
12214 /*
12215 * If any pages need to be committed, commit them and
12216 * then unlock them so that they can be freed some
12217 * time later.
12218 */
12219 if (plist == NULL)
12220 return (0);
12221
12222 /*
12223 * No error occurred during the flush portion
12224 * of this operation, so now attempt to commit
12225 * the data to stable storage on the server.
12226 *
12227 * This will unlock all of the pages on the list.
12228 */
12229 return (nfs4_sync_commit(vp, plist, offset, len, cr));
12230 }
12231
12232 static int
nfs4_sync_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)12233 nfs4_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12234 cred_t *cr)
12235 {
12236 int error;
12237 page_t *pp;
12238
12239 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12240
12241 error = nfs4_commit(vp, (offset4)offset, (count3)count, cr);
12242
12243 /*
12244 * If we got an error, then just unlock all of the pages
12245 * on the list.
12246 */
12247 if (error) {
12248 while (plist != NULL) {
12249 pp = plist;
12250 page_sub(&plist, pp);
12251 page_unlock(pp);
12252 }
12253 return (error);
12254 }
12255 /*
12256 * We've tried as hard as we can to commit the data to stable
12257 * storage on the server. We just unlock the pages and clear
12258 * the commit required state. They will get freed later.
12259 */
12260 while (plist != NULL) {
12261 pp = plist;
12262 page_sub(&plist, pp);
12263 pp->p_fsdata = C_NOCOMMIT;
12264 page_unlock(pp);
12265 }
12266
12267 return (error);
12268 }
12269
12270 static void
do_nfs4_async_commit(vnode_t * vp,page_t * plist,offset3 offset,count3 count,cred_t * cr)12271 do_nfs4_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
12272 cred_t *cr)
12273 {
12274
12275 (void) nfs4_sync_commit(vp, plist, offset, count, cr);
12276 }
12277
12278 /*ARGSUSED*/
12279 static int
nfs4_setsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)12280 nfs4_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12281 caller_context_t *ct)
12282 {
12283 int error = 0;
12284 mntinfo4_t *mi;
12285 vattr_t va;
12286 vsecattr_t nfsace4_vsap;
12287
12288 mi = VTOMI4(vp);
12289 if (nfs_zone() != mi->mi_zone)
12290 return (EIO);
12291 if (mi->mi_flags & MI4_ACL) {
12292 /* if we have a delegation, return it */
12293 if (VTOR4(vp)->r_deleg_type != OPEN_DELEGATE_NONE)
12294 (void) nfs4delegreturn(VTOR4(vp),
12295 NFS4_DR_REOPEN|NFS4_DR_PUSH);
12296
12297 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask,
12298 NFS4_ACL_SET);
12299 if (error) /* EINVAL */
12300 return (error);
12301
12302 if (vsecattr->vsa_mask & (VSA_ACL | VSA_DFACL)) {
12303 /*
12304 * These are aclent_t type entries.
12305 */
12306 error = vs_aent_to_ace4(vsecattr, &nfsace4_vsap,
12307 vp->v_type == VDIR, FALSE);
12308 if (error)
12309 return (error);
12310 } else {
12311 /*
12312 * These are ace_t type entries.
12313 */
12314 error = vs_acet_to_ace4(vsecattr, &nfsace4_vsap,
12315 FALSE);
12316 if (error)
12317 return (error);
12318 }
12319 bzero(&va, sizeof (va));
12320 error = nfs4setattr(vp, &va, flag, cr, &nfsace4_vsap);
12321 vs_ace4_destroy(&nfsace4_vsap);
12322 return (error);
12323 }
12324 return (ENOSYS);
12325 }
12326
12327 /* ARGSUSED */
12328 int
nfs4_getsecattr(vnode_t * vp,vsecattr_t * vsecattr,int flag,cred_t * cr,caller_context_t * ct)12329 nfs4_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
12330 caller_context_t *ct)
12331 {
12332 int error;
12333 mntinfo4_t *mi;
12334 nfs4_ga_res_t gar;
12335 rnode4_t *rp = VTOR4(vp);
12336
12337 mi = VTOMI4(vp);
12338 if (nfs_zone() != mi->mi_zone)
12339 return (EIO);
12340
12341 bzero(&gar, sizeof (gar));
12342 gar.n4g_vsa.vsa_mask = vsecattr->vsa_mask;
12343
12344 /*
12345 * vsecattr->vsa_mask holds the original acl request mask.
12346 * This is needed when determining what to return.
12347 * (See: nfs4_create_getsecattr_return())
12348 */
12349 error = nfs4_is_acl_mask_valid(vsecattr->vsa_mask, NFS4_ACL_GET);
12350 if (error) /* EINVAL */
12351 return (error);
12352
12353 /*
12354 * If this is a referral stub, don't try to go OTW for an ACL
12355 */
12356 if (RP_ISSTUB_REFERRAL(VTOR4(vp)))
12357 return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
12358
12359 if (mi->mi_flags & MI4_ACL) {
12360 /*
12361 * Check if the data is cached and the cache is valid. If it
12362 * is we don't go over the wire.
12363 */
12364 if (rp->r_secattr != NULL && ATTRCACHE4_VALID(vp)) {
12365 mutex_enter(&rp->r_statelock);
12366 if (rp->r_secattr != NULL) {
12367 error = nfs4_create_getsecattr_return(
12368 rp->r_secattr, vsecattr, rp->r_attr.va_uid,
12369 rp->r_attr.va_gid,
12370 vp->v_type == VDIR);
12371 if (!error) { /* error == 0 - Success! */
12372 mutex_exit(&rp->r_statelock);
12373 return (error);
12374 }
12375 }
12376 mutex_exit(&rp->r_statelock);
12377 }
12378
12379 /*
12380 * The getattr otw call will always get both the acl, in
12381 * the form of a list of nfsace4's, and the number of acl
12382 * entries; independent of the value of gar.n4g_va.va_mask.
12383 */
12384 error = nfs4_getattr_otw(vp, &gar, cr, 1);
12385 if (error) {
12386 vs_ace4_destroy(&gar.n4g_vsa);
12387 if (error == ENOTSUP || error == EOPNOTSUPP)
12388 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12389 return (error);
12390 }
12391
12392 if (!(gar.n4g_resbmap & FATTR4_ACL_MASK)) {
12393 /*
12394 * No error was returned, but according to the response
12395 * bitmap, neither was an acl.
12396 */
12397 vs_ace4_destroy(&gar.n4g_vsa);
12398 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12399 return (error);
12400 }
12401
12402 /*
12403 * Update the cache with the ACL.
12404 */
12405 nfs4_acl_fill_cache(rp, &gar.n4g_vsa);
12406
12407 error = nfs4_create_getsecattr_return(&gar.n4g_vsa,
12408 vsecattr, gar.n4g_va.va_uid, gar.n4g_va.va_gid,
12409 vp->v_type == VDIR);
12410 vs_ace4_destroy(&gar.n4g_vsa);
12411 if ((error) && (vsecattr->vsa_mask &
12412 (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) &&
12413 (error != EACCES)) {
12414 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12415 }
12416 return (error);
12417 }
12418 error = fs_fab_acl(vp, vsecattr, flag, cr, ct);
12419 return (error);
12420 }
12421
12422 /*
12423 * The function returns:
12424 * - 0 (zero) if the passed in "acl_mask" is a valid request.
12425 * - EINVAL if the passed in "acl_mask" is an invalid request.
12426 *
12427 * In the case of getting an acl (op == NFS4_ACL_GET) the mask is invalid if:
12428 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12429 *
12430 * In the case of setting an acl (op == NFS4_ACL_SET) the mask is invalid if:
12431 * - We have a mixture of ACE and ACL requests (e.g. VSA_ACL | VSA_ACE)
12432 * - We have a count field set without the corresponding acl field set. (e.g. -
12433 * VSA_ACECNT is set, but VSA_ACE is not)
12434 */
12435 static int
nfs4_is_acl_mask_valid(uint_t acl_mask,nfs4_acl_op_t op)12436 nfs4_is_acl_mask_valid(uint_t acl_mask, nfs4_acl_op_t op)
12437 {
12438 /* Shortcut the masks that are always valid. */
12439 if (acl_mask == (VSA_ACE | VSA_ACECNT))
12440 return (0);
12441 if (acl_mask == (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT))
12442 return (0);
12443
12444 if (acl_mask & (VSA_ACE | VSA_ACECNT)) {
12445 /*
12446 * We can't have any VSA_ACL type stuff in the mask now.
12447 */
12448 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12449 VSA_DFACLCNT))
12450 return (EINVAL);
12451
12452 if (op == NFS4_ACL_SET) {
12453 if ((acl_mask & VSA_ACECNT) && !(acl_mask & VSA_ACE))
12454 return (EINVAL);
12455 }
12456 }
12457
12458 if (acl_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT)) {
12459 /*
12460 * We can't have any VSA_ACE type stuff in the mask now.
12461 */
12462 if (acl_mask & (VSA_ACE | VSA_ACECNT))
12463 return (EINVAL);
12464
12465 if (op == NFS4_ACL_SET) {
12466 if ((acl_mask & VSA_ACLCNT) && !(acl_mask & VSA_ACL))
12467 return (EINVAL);
12468
12469 if ((acl_mask & VSA_DFACLCNT) &&
12470 !(acl_mask & VSA_DFACL))
12471 return (EINVAL);
12472 }
12473 }
12474 return (0);
12475 }
12476
12477 /*
12478 * The theory behind creating the correct getsecattr return is simply this:
12479 * "Don't return anything that the caller is not expecting to have to free."
12480 */
12481 static int
nfs4_create_getsecattr_return(vsecattr_t * filled_vsap,vsecattr_t * vsap,uid_t uid,gid_t gid,int isdir)12482 nfs4_create_getsecattr_return(vsecattr_t *filled_vsap, vsecattr_t *vsap,
12483 uid_t uid, gid_t gid, int isdir)
12484 {
12485 int error = 0;
12486 /* Save the mask since the translators modify it. */
12487 uint_t orig_mask = vsap->vsa_mask;
12488
12489 if (orig_mask & (VSA_ACE | VSA_ACECNT)) {
12490 error = vs_ace4_to_acet(filled_vsap, vsap, uid, gid, FALSE);
12491
12492 if (error)
12493 return (error);
12494
12495 /*
12496 * If the caller only asked for the ace count (VSA_ACECNT)
12497 * don't give them the full acl (VSA_ACE), free it.
12498 */
12499 if (!orig_mask & VSA_ACE) {
12500 if (vsap->vsa_aclentp != NULL) {
12501 kmem_free(vsap->vsa_aclentp,
12502 vsap->vsa_aclcnt * sizeof (ace_t));
12503 vsap->vsa_aclentp = NULL;
12504 }
12505 }
12506 vsap->vsa_mask = orig_mask;
12507
12508 } else if (orig_mask & (VSA_ACL | VSA_ACLCNT | VSA_DFACL |
12509 VSA_DFACLCNT)) {
12510 error = vs_ace4_to_aent(filled_vsap, vsap, uid, gid,
12511 isdir, FALSE);
12512
12513 if (error)
12514 return (error);
12515
12516 /*
12517 * If the caller only asked for the acl count (VSA_ACLCNT)
12518 * and/or the default acl count (VSA_DFACLCNT) don't give them
12519 * the acl (VSA_ACL) or default acl (VSA_DFACL), free it.
12520 */
12521 if (!orig_mask & VSA_ACL) {
12522 if (vsap->vsa_aclentp != NULL) {
12523 kmem_free(vsap->vsa_aclentp,
12524 vsap->vsa_aclcnt * sizeof (aclent_t));
12525 vsap->vsa_aclentp = NULL;
12526 }
12527 }
12528
12529 if (!orig_mask & VSA_DFACL) {
12530 if (vsap->vsa_dfaclentp != NULL) {
12531 kmem_free(vsap->vsa_dfaclentp,
12532 vsap->vsa_dfaclcnt * sizeof (aclent_t));
12533 vsap->vsa_dfaclentp = NULL;
12534 }
12535 }
12536 vsap->vsa_mask = orig_mask;
12537 }
12538 return (0);
12539 }
12540
12541 /* ARGSUSED */
12542 int
nfs4_shrlock(vnode_t * vp,int cmd,struct shrlock * shr,int flag,cred_t * cr,caller_context_t * ct)12543 nfs4_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
12544 caller_context_t *ct)
12545 {
12546 int error;
12547
12548 if (nfs_zone() != VTOMI4(vp)->mi_zone)
12549 return (EIO);
12550 /*
12551 * check for valid cmd parameter
12552 */
12553 if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
12554 return (EINVAL);
12555
12556 /*
12557 * Check access permissions
12558 */
12559 if ((cmd & F_SHARE) &&
12560 (((shr->s_access & F_RDACC) && (flag & FREAD) == 0) ||
12561 (shr->s_access == F_WRACC && (flag & FWRITE) == 0)))
12562 return (EBADF);
12563
12564 /*
12565 * If the filesystem is mounted using local locking, pass the
12566 * request off to the local share code.
12567 */
12568 if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
12569 return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
12570
12571 switch (cmd) {
12572 case F_SHARE:
12573 case F_UNSHARE:
12574 /*
12575 * This will be properly implemented later,
12576 * see RFE: 4823948 .
12577 */
12578 error = EAGAIN;
12579 break;
12580
12581 case F_HASREMOTELOCKS:
12582 /*
12583 * NFS client can't store remote locks itself
12584 */
12585 shr->s_access = 0;
12586 error = 0;
12587 break;
12588
12589 default:
12590 error = EINVAL;
12591 break;
12592 }
12593
12594 return (error);
12595 }
12596
12597 /*
12598 * Common code called by directory ops to update the attrcache
12599 */
12600 static int
nfs4_update_attrcache(nfsstat4 status,nfs4_ga_res_t * garp,hrtime_t t,vnode_t * vp,cred_t * cr)12601 nfs4_update_attrcache(nfsstat4 status, nfs4_ga_res_t *garp,
12602 hrtime_t t, vnode_t *vp, cred_t *cr)
12603 {
12604 int error = 0;
12605
12606 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12607
12608 if (status != NFS4_OK) {
12609 /* getattr not done or failed */
12610 PURGE_ATTRCACHE4(vp);
12611 return (error);
12612 }
12613
12614 if (garp) {
12615 nfs4_attr_cache(vp, garp, t, cr, FALSE, NULL);
12616 } else {
12617 PURGE_ATTRCACHE4(vp);
12618 }
12619 return (error);
12620 }
12621
12622 /*
12623 * Update directory caches for directory modification ops (link, rename, etc.)
12624 * When dinfo is NULL, manage dircaches in the old way.
12625 */
12626 static void
nfs4_update_dircaches(change_info4 * cinfo,vnode_t * dvp,vnode_t * vp,char * nm,dirattr_info_t * dinfo)12627 nfs4_update_dircaches(change_info4 *cinfo, vnode_t *dvp, vnode_t *vp, char *nm,
12628 dirattr_info_t *dinfo)
12629 {
12630 rnode4_t *drp = VTOR4(dvp);
12631
12632 ASSERT(nfs_zone() == VTOMI4(dvp)->mi_zone);
12633
12634 /* Purge rddir cache for dir since it changed */
12635 if (drp->r_dir != NULL)
12636 nfs4_purge_rddir_cache(dvp);
12637
12638 /*
12639 * If caller provided dinfo, then use it to manage dir caches.
12640 */
12641 if (dinfo != NULL) {
12642 if (vp != NULL) {
12643 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12644 if (!VTOR4(vp)->created_v4) {
12645 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12646 dnlc_update(dvp, nm, vp);
12647 } else {
12648 /*
12649 * XXX don't update if the created_v4 flag is
12650 * set
12651 */
12652 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12653 NFS4_DEBUG(nfs4_client_state_debug,
12654 (CE_NOTE, "nfs4_update_dircaches: "
12655 "don't update dnlc: created_v4 flag"));
12656 }
12657 }
12658
12659 nfs4_attr_cache(dvp, dinfo->di_garp, dinfo->di_time_call,
12660 dinfo->di_cred, FALSE, cinfo);
12661
12662 return;
12663 }
12664
12665 /*
12666 * Caller didn't provide dinfo, then check change_info4 to update DNLC.
12667 * Since caller modified dir but didn't receive post-dirmod-op dir
12668 * attrs, the dir's attrs must be purged.
12669 *
12670 * XXX this check and dnlc update/purge should really be atomic,
12671 * XXX but can't use rnode statelock because it'll deadlock in
12672 * XXX dnlc_purge_vp, however, the risk is minimal even if a race
12673 * XXX does occur.
12674 *
12675 * XXX We also may want to check that atomic is true in the
12676 * XXX change_info struct. If it is not, the change_info may
12677 * XXX reflect changes by more than one clients which means that
12678 * XXX our cache may not be valid.
12679 */
12680 PURGE_ATTRCACHE4(dvp);
12681 if (drp->r_change == cinfo->before) {
12682 /* no changes took place in the directory prior to our link */
12683 if (vp != NULL) {
12684 mutex_enter(&VTOR4(vp)->r_statev4_lock);
12685 if (!VTOR4(vp)->created_v4) {
12686 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12687 dnlc_update(dvp, nm, vp);
12688 } else {
12689 /*
12690 * XXX dont' update if the created_v4 flag
12691 * is set
12692 */
12693 mutex_exit(&VTOR4(vp)->r_statev4_lock);
12694 NFS4_DEBUG(nfs4_client_state_debug, (CE_NOTE,
12695 "nfs4_update_dircaches: don't"
12696 " update dnlc: created_v4 flag"));
12697 }
12698 }
12699 } else {
12700 /* Another client modified directory - purge its dnlc cache */
12701 dnlc_purge_vp(dvp);
12702 }
12703 }
12704
12705 /*
12706 * The OPEN_CONFIRM operation confirms the sequence number used in OPENing a
12707 * file.
12708 *
12709 * The 'reopening_file' boolean should be set to TRUE if we are reopening this
12710 * file (ie: client recovery) and otherwise set to FALSE.
12711 *
12712 * 'nfs4_start/end_op' should have been called by the proper (ie: not recovery
12713 * initiated) calling functions.
12714 *
12715 * 'resend' is set to TRUE if this is a OPEN_CONFIRM issued as a result
12716 * of resending a 'lost' open request.
12717 *
12718 * 'num_bseqid_retryp' makes sure we don't loop forever on a broken
12719 * server that hands out BAD_SEQID on open confirm.
12720 *
12721 * Errors are returned via the nfs4_error_t parameter.
12722 */
12723 void
nfs4open_confirm(vnode_t * vp,seqid4 * seqid,stateid4 * stateid,cred_t * cr,bool_t reopening_file,bool_t * retry_open,nfs4_open_owner_t * oop,bool_t resend,nfs4_error_t * ep,int * num_bseqid_retryp)12724 nfs4open_confirm(vnode_t *vp, seqid4 *seqid, stateid4 *stateid, cred_t *cr,
12725 bool_t reopening_file, bool_t *retry_open, nfs4_open_owner_t *oop,
12726 bool_t resend, nfs4_error_t *ep, int *num_bseqid_retryp)
12727 {
12728 COMPOUND4args_clnt args;
12729 COMPOUND4res_clnt res;
12730 nfs_argop4 argop[2];
12731 nfs_resop4 *resop;
12732 int doqueue = 1;
12733 mntinfo4_t *mi;
12734 OPEN_CONFIRM4args *open_confirm_args;
12735 int needrecov;
12736
12737 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12738 #if DEBUG
12739 mutex_enter(&oop->oo_lock);
12740 ASSERT(oop->oo_seqid_inuse);
12741 mutex_exit(&oop->oo_lock);
12742 #endif
12743
12744 recov_retry_confirm:
12745 nfs4_error_zinit(ep);
12746 *retry_open = FALSE;
12747
12748 if (resend)
12749 args.ctag = TAG_OPEN_CONFIRM_LOST;
12750 else
12751 args.ctag = TAG_OPEN_CONFIRM;
12752
12753 args.array_len = 2;
12754 args.array = argop;
12755
12756 /* putfh target fh */
12757 argop[0].argop = OP_CPUTFH;
12758 argop[0].nfs_argop4_u.opcputfh.sfh = VTOR4(vp)->r_fh;
12759
12760 argop[1].argop = OP_OPEN_CONFIRM;
12761 open_confirm_args = &argop[1].nfs_argop4_u.opopen_confirm;
12762
12763 (*seqid) += 1;
12764 open_confirm_args->seqid = *seqid;
12765 open_confirm_args->open_stateid = *stateid;
12766
12767 mi = VTOMI4(vp);
12768
12769 rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
12770
12771 if (!ep->error && nfs4_need_to_bump_seqid(&res)) {
12772 nfs4_set_open_seqid((*seqid), oop, args.ctag);
12773 }
12774
12775 needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
12776 if (!needrecov && ep->error)
12777 return;
12778
12779 if (needrecov) {
12780 bool_t abort = FALSE;
12781
12782 if (reopening_file == FALSE) {
12783 nfs4_bseqid_entry_t *bsep = NULL;
12784
12785 if (!ep->error && res.status == NFS4ERR_BAD_SEQID)
12786 bsep = nfs4_create_bseqid_entry(oop, NULL,
12787 vp, 0, args.ctag,
12788 open_confirm_args->seqid);
12789
12790 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL,
12791 NULL, NULL, OP_OPEN_CONFIRM, bsep, NULL, NULL);
12792 if (bsep) {
12793 kmem_free(bsep, sizeof (*bsep));
12794 if (num_bseqid_retryp &&
12795 --(*num_bseqid_retryp) == 0)
12796 abort = TRUE;
12797 }
12798 }
12799 if ((ep->error == ETIMEDOUT ||
12800 res.status == NFS4ERR_RESOURCE) &&
12801 abort == FALSE && resend == FALSE) {
12802 if (!ep->error)
12803 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12804
12805 delay(SEC_TO_TICK(confirm_retry_sec));
12806 goto recov_retry_confirm;
12807 }
12808 /* State may have changed so retry the entire OPEN op */
12809 if (abort == FALSE)
12810 *retry_open = TRUE;
12811 else
12812 *retry_open = FALSE;
12813 if (!ep->error)
12814 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12815 return;
12816 }
12817
12818 if (res.status) {
12819 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12820 return;
12821 }
12822
12823 resop = &res.array[1]; /* open confirm res */
12824 bcopy(&resop->nfs_resop4_u.opopen_confirm.open_stateid,
12825 stateid, sizeof (*stateid));
12826
12827 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
12828 }
12829
12830 /*
12831 * Return the credentials associated with a client state object. The
12832 * caller is responsible for freeing the credentials.
12833 */
12834
12835 static cred_t *
state_to_cred(nfs4_open_stream_t * osp)12836 state_to_cred(nfs4_open_stream_t *osp)
12837 {
12838 cred_t *cr;
12839
12840 /*
12841 * It's ok to not lock the open stream and open owner to get
12842 * the oo_cred since this is only written once (upon creation)
12843 * and will not change.
12844 */
12845 cr = osp->os_open_owner->oo_cred;
12846 crhold(cr);
12847
12848 return (cr);
12849 }
12850
12851 /*
12852 * nfs4_find_sysid
12853 *
12854 * Find the sysid for the knetconfig associated with the given mi.
12855 */
12856 static struct lm_sysid *
nfs4_find_sysid(mntinfo4_t * mi)12857 nfs4_find_sysid(mntinfo4_t *mi)
12858 {
12859 ASSERT(nfs_zone() == mi->mi_zone);
12860
12861 /*
12862 * Switch from RDMA knconf to original mount knconf
12863 */
12864 return (lm_get_sysid(ORIG_KNCONF(mi), &mi->mi_curr_serv->sv_addr,
12865 mi->mi_curr_serv->sv_hostname, NULL));
12866 }
12867
12868 #ifdef DEBUG
12869 /*
12870 * Return a string version of the call type for easy reading.
12871 */
12872 static char *
nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)12873 nfs4frlock_get_call_type(nfs4_lock_call_type_t ctype)
12874 {
12875 switch (ctype) {
12876 case NFS4_LCK_CTYPE_NORM:
12877 return ("NORMAL");
12878 case NFS4_LCK_CTYPE_RECLAIM:
12879 return ("RECLAIM");
12880 case NFS4_LCK_CTYPE_RESEND:
12881 return ("RESEND");
12882 case NFS4_LCK_CTYPE_REINSTATE:
12883 return ("REINSTATE");
12884 default:
12885 cmn_err(CE_PANIC, "nfs4frlock_get_call_type: got illegal "
12886 "type %d", ctype);
12887 return ("");
12888 }
12889 }
12890 #endif
12891
12892 /*
12893 * Map the frlock cmd and lock type to the NFSv4 over-the-wire lock type
12894 * Unlock requests don't have an over-the-wire locktype, so we just return
12895 * something non-threatening.
12896 */
12897
12898 static nfs_lock_type4
flk_to_locktype(int cmd,int l_type)12899 flk_to_locktype(int cmd, int l_type)
12900 {
12901 ASSERT(l_type == F_RDLCK || l_type == F_WRLCK || l_type == F_UNLCK);
12902
12903 switch (l_type) {
12904 case F_UNLCK:
12905 return (READ_LT);
12906 case F_RDLCK:
12907 if (cmd == F_SETLK)
12908 return (READ_LT);
12909 else
12910 return (READW_LT);
12911 case F_WRLCK:
12912 if (cmd == F_SETLK)
12913 return (WRITE_LT);
12914 else
12915 return (WRITEW_LT);
12916 }
12917 panic("flk_to_locktype");
12918 /*NOTREACHED*/
12919 }
12920
12921 /*
12922 * Do some preliminary checks for nfs4frlock.
12923 */
12924 static int
nfs4frlock_validate_args(int cmd,flock64_t * flk,int flag,vnode_t * vp,u_offset_t offset)12925 nfs4frlock_validate_args(int cmd, flock64_t *flk, int flag, vnode_t *vp,
12926 u_offset_t offset)
12927 {
12928 int error = 0;
12929
12930 /*
12931 * If we are setting a lock, check that the file is opened
12932 * with the correct mode.
12933 */
12934 if (cmd == F_SETLK || cmd == F_SETLKW) {
12935 if ((flk->l_type == F_RDLCK && (flag & FREAD) == 0) ||
12936 (flk->l_type == F_WRLCK && (flag & FWRITE) == 0)) {
12937 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12938 "nfs4frlock_validate_args: file was opened with "
12939 "incorrect mode"));
12940 return (EBADF);
12941 }
12942 }
12943
12944 /* Convert the offset. It may need to be restored before returning. */
12945 if (error = convoff(vp, flk, 0, offset)) {
12946 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12947 "nfs4frlock_validate_args: convoff => error= %d\n",
12948 error));
12949 return (error);
12950 }
12951
12952 return (error);
12953 }
12954
12955 /*
12956 * Set the flock64's lm_sysid for nfs4frlock.
12957 */
12958 static int
nfs4frlock_get_sysid(struct lm_sysid ** lspp,vnode_t * vp,flock64_t * flk)12959 nfs4frlock_get_sysid(struct lm_sysid **lspp, vnode_t *vp, flock64_t *flk)
12960 {
12961 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
12962
12963 /* Find the lm_sysid */
12964 *lspp = nfs4_find_sysid(VTOMI4(vp));
12965
12966 if (*lspp == NULL) {
12967 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
12968 "nfs4frlock_get_sysid: no sysid, return ENOLCK"));
12969 return (ENOLCK);
12970 }
12971
12972 flk->l_sysid = lm_sysidt(*lspp);
12973
12974 return (0);
12975 }
12976
12977 /*
12978 * Do the remaining preliminary setup for nfs4frlock.
12979 */
12980 static void
nfs4frlock_pre_setup(clock_t * tick_delayp,nfs4_recov_state_t * recov_statep,flock64_t * flk,short * whencep,vnode_t * vp,cred_t * search_cr,cred_t ** cred_otw)12981 nfs4frlock_pre_setup(clock_t *tick_delayp, nfs4_recov_state_t *recov_statep,
12982 flock64_t *flk, short *whencep, vnode_t *vp, cred_t *search_cr,
12983 cred_t **cred_otw)
12984 {
12985 /*
12986 * set tick_delay to the base delay time.
12987 * (NFS4_BASE_WAIT_TIME is in secs)
12988 */
12989
12990 *tick_delayp = drv_usectohz(NFS4_BASE_WAIT_TIME * 1000 * 1000);
12991
12992 /*
12993 * If lock is relative to EOF, we need the newest length of the
12994 * file. Therefore invalidate the ATTR_CACHE.
12995 */
12996
12997 *whencep = flk->l_whence;
12998
12999 if (*whencep == 2) /* SEEK_END */
13000 PURGE_ATTRCACHE4(vp);
13001
13002 recov_statep->rs_flags = 0;
13003 recov_statep->rs_num_retry_despite_err = 0;
13004 *cred_otw = nfs4_get_otw_cred(search_cr, VTOMI4(vp), NULL);
13005 }
13006
13007 /*
13008 * Initialize and allocate the data structures necessary for
13009 * the nfs4frlock call.
13010 * Allocates argsp's op array.
13011 */
13012 static void
nfs4frlock_call_init(COMPOUND4args_clnt * argsp,COMPOUND4args_clnt ** argspp,nfs_argop4 ** argopp,nfs4_op_hint_t * op_hintp,flock64_t * flk,int cmd,bool_t * retry,bool_t * did_start_fop,COMPOUND4res_clnt ** respp,bool_t * skip_get_err,nfs4_lost_rqst_t * lost_rqstp)13013 nfs4frlock_call_init(COMPOUND4args_clnt *argsp, COMPOUND4args_clnt **argspp,
13014 nfs_argop4 **argopp, nfs4_op_hint_t *op_hintp, flock64_t *flk, int cmd,
13015 bool_t *retry, bool_t *did_start_fop, COMPOUND4res_clnt **respp,
13016 bool_t *skip_get_err, nfs4_lost_rqst_t *lost_rqstp)
13017 {
13018 int argoplist_size;
13019 int num_ops = 2;
13020
13021 *retry = FALSE;
13022 *did_start_fop = FALSE;
13023 *skip_get_err = FALSE;
13024 lost_rqstp->lr_op = 0;
13025 argoplist_size = num_ops * sizeof (nfs_argop4);
13026 /* fill array with zero */
13027 *argopp = kmem_zalloc(argoplist_size, KM_SLEEP);
13028
13029 *argspp = argsp;
13030 *respp = NULL;
13031
13032 argsp->array_len = num_ops;
13033 argsp->array = *argopp;
13034
13035 /* initialize in case of error; will get real value down below */
13036 argsp->ctag = TAG_NONE;
13037
13038 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK)
13039 *op_hintp = OH_LOCKU;
13040 else
13041 *op_hintp = OH_OTHER;
13042 }
13043
13044 /*
13045 * Call the nfs4_start_fop() for nfs4frlock, if necessary. Assign
13046 * the proper nfs4_server_t for this instance of nfs4frlock.
13047 * Returns 0 (success) or an errno value.
13048 */
13049 static int
nfs4frlock_start_call(nfs4_lock_call_type_t ctype,vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,bool_t * did_start_fop,bool_t * startrecovp)13050 nfs4frlock_start_call(nfs4_lock_call_type_t ctype, vnode_t *vp,
13051 nfs4_op_hint_t op_hint, nfs4_recov_state_t *recov_statep,
13052 bool_t *did_start_fop, bool_t *startrecovp)
13053 {
13054 int error = 0;
13055 rnode4_t *rp;
13056
13057 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13058
13059 if (ctype == NFS4_LCK_CTYPE_NORM) {
13060 error = nfs4_start_fop(VTOMI4(vp), vp, NULL, op_hint,
13061 recov_statep, startrecovp);
13062 if (error)
13063 return (error);
13064 *did_start_fop = TRUE;
13065 } else {
13066 *did_start_fop = FALSE;
13067 *startrecovp = FALSE;
13068 }
13069
13070 if (!error) {
13071 rp = VTOR4(vp);
13072
13073 /* If the file failed recovery, just quit. */
13074 mutex_enter(&rp->r_statelock);
13075 if (rp->r_flags & R4RECOVERR) {
13076 error = EIO;
13077 }
13078 mutex_exit(&rp->r_statelock);
13079 }
13080
13081 return (error);
13082 }
13083
13084 /*
13085 * Setup the LOCK4/LOCKU4 arguments for resending a lost lock request. A
13086 * resend nfs4frlock call is initiated by the recovery framework.
13087 * Acquires the lop and oop seqid synchronization.
13088 */
13089 static void
nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t * resend_rqstp,COMPOUND4args_clnt * argsp,nfs_argop4 * argop,nfs4_lock_owner_t ** lopp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,LOCK4args ** lock_argsp,LOCKU4args ** locku_argsp)13090 nfs4frlock_setup_resend_lock_args(nfs4_lost_rqst_t *resend_rqstp,
13091 COMPOUND4args_clnt *argsp, nfs_argop4 *argop, nfs4_lock_owner_t **lopp,
13092 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13093 LOCK4args **lock_argsp, LOCKU4args **locku_argsp)
13094 {
13095 mntinfo4_t *mi = VTOMI4(resend_rqstp->lr_vp);
13096 int error;
13097
13098 NFS4_DEBUG((nfs4_lost_rqst_debug || nfs4_client_lock_debug),
13099 (CE_NOTE,
13100 "nfs4frlock_setup_resend_lock_args: have lost lock to resend"));
13101 ASSERT(resend_rqstp != NULL);
13102 ASSERT(resend_rqstp->lr_op == OP_LOCK ||
13103 resend_rqstp->lr_op == OP_LOCKU);
13104
13105 *oopp = resend_rqstp->lr_oop;
13106 if (resend_rqstp->lr_oop) {
13107 open_owner_hold(resend_rqstp->lr_oop);
13108 error = nfs4_start_open_seqid_sync(resend_rqstp->lr_oop, mi);
13109 ASSERT(error == 0); /* recov thread always succeeds */
13110 }
13111
13112 /* Must resend this lost lock/locku request. */
13113 ASSERT(resend_rqstp->lr_lop != NULL);
13114 *lopp = resend_rqstp->lr_lop;
13115 lock_owner_hold(resend_rqstp->lr_lop);
13116 error = nfs4_start_lock_seqid_sync(resend_rqstp->lr_lop, mi);
13117 ASSERT(error == 0); /* recov thread always succeeds */
13118
13119 *ospp = resend_rqstp->lr_osp;
13120 if (*ospp)
13121 open_stream_hold(resend_rqstp->lr_osp);
13122
13123 if (resend_rqstp->lr_op == OP_LOCK) {
13124 LOCK4args *lock_args;
13125
13126 argop->argop = OP_LOCK;
13127 *lock_argsp = lock_args = &argop->nfs_argop4_u.oplock;
13128 lock_args->locktype = resend_rqstp->lr_locktype;
13129 lock_args->reclaim =
13130 (resend_rqstp->lr_ctype == NFS4_LCK_CTYPE_RECLAIM);
13131 lock_args->offset = resend_rqstp->lr_flk->l_start;
13132 lock_args->length = resend_rqstp->lr_flk->l_len;
13133 if (lock_args->length == 0)
13134 lock_args->length = ~lock_args->length;
13135 nfs4_setup_lock_args(*lopp, *oopp, *ospp,
13136 mi2clientid(mi), &lock_args->locker);
13137
13138 switch (resend_rqstp->lr_ctype) {
13139 case NFS4_LCK_CTYPE_RESEND:
13140 argsp->ctag = TAG_LOCK_RESEND;
13141 break;
13142 case NFS4_LCK_CTYPE_REINSTATE:
13143 argsp->ctag = TAG_LOCK_REINSTATE;
13144 break;
13145 case NFS4_LCK_CTYPE_RECLAIM:
13146 argsp->ctag = TAG_LOCK_RECLAIM;
13147 break;
13148 default:
13149 argsp->ctag = TAG_LOCK_UNKNOWN;
13150 break;
13151 }
13152 } else {
13153 LOCKU4args *locku_args;
13154 nfs4_lock_owner_t *lop = resend_rqstp->lr_lop;
13155
13156 argop->argop = OP_LOCKU;
13157 *locku_argsp = locku_args = &argop->nfs_argop4_u.oplocku;
13158 locku_args->locktype = READ_LT;
13159 locku_args->seqid = lop->lock_seqid + 1;
13160 mutex_enter(&lop->lo_lock);
13161 locku_args->lock_stateid = lop->lock_stateid;
13162 mutex_exit(&lop->lo_lock);
13163 locku_args->offset = resend_rqstp->lr_flk->l_start;
13164 locku_args->length = resend_rqstp->lr_flk->l_len;
13165 if (locku_args->length == 0)
13166 locku_args->length = ~locku_args->length;
13167
13168 switch (resend_rqstp->lr_ctype) {
13169 case NFS4_LCK_CTYPE_RESEND:
13170 argsp->ctag = TAG_LOCKU_RESEND;
13171 break;
13172 case NFS4_LCK_CTYPE_REINSTATE:
13173 argsp->ctag = TAG_LOCKU_REINSTATE;
13174 break;
13175 default:
13176 argsp->ctag = TAG_LOCK_UNKNOWN;
13177 break;
13178 }
13179 }
13180 }
13181
13182 /*
13183 * Setup the LOCKT4 arguments.
13184 */
13185 static void
nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype,nfs_argop4 * argop,LOCKT4args ** lockt_argsp,COMPOUND4args_clnt * argsp,flock64_t * flk,rnode4_t * rp)13186 nfs4frlock_setup_lockt_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13187 LOCKT4args **lockt_argsp, COMPOUND4args_clnt *argsp, flock64_t *flk,
13188 rnode4_t *rp)
13189 {
13190 LOCKT4args *lockt_args;
13191
13192 ASSERT(nfs_zone() == VTOMI4(RTOV4(rp))->mi_zone);
13193 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13194 argop->argop = OP_LOCKT;
13195 argsp->ctag = TAG_LOCKT;
13196 lockt_args = &argop->nfs_argop4_u.oplockt;
13197
13198 /*
13199 * The locktype will be READ_LT unless it's
13200 * a write lock. We do this because the Solaris
13201 * system call allows the combination of
13202 * F_UNLCK and F_GETLK* and so in that case the
13203 * unlock is mapped to a read.
13204 */
13205 if (flk->l_type == F_WRLCK)
13206 lockt_args->locktype = WRITE_LT;
13207 else
13208 lockt_args->locktype = READ_LT;
13209
13210 lockt_args->owner.clientid = mi2clientid(VTOMI4(RTOV4(rp)));
13211 /* set the lock owner4 args */
13212 nfs4_setlockowner_args(&lockt_args->owner, rp,
13213 ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13214 flk->l_pid);
13215 lockt_args->offset = flk->l_start;
13216 lockt_args->length = flk->l_len;
13217 if (flk->l_len == 0)
13218 lockt_args->length = ~lockt_args->length;
13219
13220 *lockt_argsp = lockt_args;
13221 }
13222
13223 /*
13224 * If the client is holding a delegation, and the open stream to be used
13225 * with this lock request is a delegation open stream, then re-open the stream.
13226 * Sets the nfs4_error_t to all zeros unless the open stream has already
13227 * failed a reopen or we couldn't find the open stream. NFS4ERR_DELAY
13228 * means the caller should retry (like a recovery retry).
13229 */
13230 static void
nfs4frlock_check_deleg(vnode_t * vp,nfs4_error_t * ep,cred_t * cr,int lt)13231 nfs4frlock_check_deleg(vnode_t *vp, nfs4_error_t *ep, cred_t *cr, int lt)
13232 {
13233 open_delegation_type4 dt;
13234 bool_t reopen_needed, force;
13235 nfs4_open_stream_t *osp;
13236 open_claim_type4 oclaim;
13237 rnode4_t *rp = VTOR4(vp);
13238 mntinfo4_t *mi = VTOMI4(vp);
13239
13240 ASSERT(nfs_zone() == mi->mi_zone);
13241
13242 nfs4_error_zinit(ep);
13243
13244 mutex_enter(&rp->r_statev4_lock);
13245 dt = rp->r_deleg_type;
13246 mutex_exit(&rp->r_statev4_lock);
13247
13248 if (dt != OPEN_DELEGATE_NONE) {
13249 nfs4_open_owner_t *oop;
13250
13251 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
13252 if (!oop) {
13253 ep->stat = NFS4ERR_IO;
13254 return;
13255 }
13256 /* returns with 'os_sync_lock' held */
13257 osp = find_open_stream(oop, rp);
13258 if (!osp) {
13259 open_owner_rele(oop);
13260 ep->stat = NFS4ERR_IO;
13261 return;
13262 }
13263
13264 if (osp->os_failed_reopen) {
13265 NFS4_DEBUG((nfs4_open_stream_debug ||
13266 nfs4_client_lock_debug), (CE_NOTE,
13267 "nfs4frlock_check_deleg: os_failed_reopen set "
13268 "for osp %p, cr %p, rp %s", (void *)osp,
13269 (void *)cr, rnode4info(rp)));
13270 mutex_exit(&osp->os_sync_lock);
13271 open_stream_rele(osp, rp);
13272 open_owner_rele(oop);
13273 ep->stat = NFS4ERR_IO;
13274 return;
13275 }
13276
13277 /*
13278 * Determine whether a reopen is needed. If this
13279 * is a delegation open stream, then send the open
13280 * to the server to give visibility to the open owner.
13281 * Even if it isn't a delegation open stream, we need
13282 * to check if the previous open CLAIM_DELEGATE_CUR
13283 * was sufficient.
13284 */
13285
13286 reopen_needed = osp->os_delegation ||
13287 ((lt == F_RDLCK &&
13288 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_READ)) ||
13289 (lt == F_WRLCK &&
13290 !(osp->os_dc_openacc & OPEN4_SHARE_ACCESS_WRITE)));
13291
13292 mutex_exit(&osp->os_sync_lock);
13293 open_owner_rele(oop);
13294
13295 if (reopen_needed) {
13296 /*
13297 * Always use CLAIM_PREVIOUS after server reboot.
13298 * The server will reject CLAIM_DELEGATE_CUR if
13299 * it is used during the grace period.
13300 */
13301 mutex_enter(&mi->mi_lock);
13302 if (mi->mi_recovflags & MI4R_SRV_REBOOT) {
13303 oclaim = CLAIM_PREVIOUS;
13304 force = TRUE;
13305 } else {
13306 oclaim = CLAIM_DELEGATE_CUR;
13307 force = FALSE;
13308 }
13309 mutex_exit(&mi->mi_lock);
13310
13311 nfs4_reopen(vp, osp, ep, oclaim, force, FALSE);
13312 if (ep->error == EAGAIN) {
13313 nfs4_error_zinit(ep);
13314 ep->stat = NFS4ERR_DELAY;
13315 }
13316 }
13317 open_stream_rele(osp, rp);
13318 osp = NULL;
13319 }
13320 }
13321
13322 /*
13323 * Setup the LOCKU4 arguments.
13324 * Returns errors via the nfs4_error_t.
13325 * NFS4_OK no problems. *go_otwp is TRUE if call should go
13326 * over-the-wire. The caller must release the
13327 * reference on *lopp.
13328 * NFS4ERR_DELAY caller should retry (like recovery retry)
13329 * (other) unrecoverable error.
13330 */
13331 static void
nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype,nfs_argop4 * argop,LOCKU4args ** locku_argsp,flock64_t * flk,nfs4_lock_owner_t ** lopp,nfs4_error_t * ep,COMPOUND4args_clnt * argsp,vnode_t * vp,int flag,u_offset_t offset,cred_t * cr,bool_t * skip_get_err,bool_t * go_otwp)13332 nfs4frlock_setup_locku_args(nfs4_lock_call_type_t ctype, nfs_argop4 *argop,
13333 LOCKU4args **locku_argsp, flock64_t *flk,
13334 nfs4_lock_owner_t **lopp, nfs4_error_t *ep, COMPOUND4args_clnt *argsp,
13335 vnode_t *vp, int flag, u_offset_t offset, cred_t *cr,
13336 bool_t *skip_get_err, bool_t *go_otwp)
13337 {
13338 nfs4_lock_owner_t *lop = NULL;
13339 LOCKU4args *locku_args;
13340 pid_t pid;
13341 bool_t is_spec = FALSE;
13342 rnode4_t *rp = VTOR4(vp);
13343
13344 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13345 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13346
13347 nfs4frlock_check_deleg(vp, ep, cr, F_UNLCK);
13348 if (ep->error || ep->stat)
13349 return;
13350
13351 argop->argop = OP_LOCKU;
13352 if (ctype == NFS4_LCK_CTYPE_REINSTATE)
13353 argsp->ctag = TAG_LOCKU_REINSTATE;
13354 else
13355 argsp->ctag = TAG_LOCKU;
13356 locku_args = &argop->nfs_argop4_u.oplocku;
13357 *locku_argsp = locku_args;
13358
13359 /* locktype should be set to any legal value */
13360 locku_args->locktype = READ_LT;
13361
13362 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pidp->pid_id :
13363 flk->l_pid;
13364
13365 /*
13366 * Get the lock owner stateid. If no lock owner
13367 * exists, return success.
13368 */
13369 lop = find_lock_owner(rp, pid, LOWN_ANY);
13370 *lopp = lop;
13371 if (lop && CLNT_ISSPECIAL(&lop->lock_stateid))
13372 is_spec = TRUE;
13373 if (!lop || is_spec) {
13374 /*
13375 * No lock owner so no locks to unlock.
13376 * Return success. If there was a failed
13377 * reclaim earlier, the lock might still be
13378 * registered with the local locking code,
13379 * so notify it of the unlock.
13380 *
13381 * If the lockowner is using a special stateid,
13382 * then the original lock request (that created
13383 * this lockowner) was never successful, so we
13384 * have no lock to undo OTW.
13385 */
13386 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13387 "nfs4frlock_setup_locku_args: LOCKU: no lock owner "
13388 "(%ld) so return success", (long)pid));
13389
13390 if (ctype == NFS4_LCK_CTYPE_NORM)
13391 flk->l_pid = curproc->p_pid;
13392 nfs4_register_lock_locally(vp, flk, flag, offset);
13393 /*
13394 * Release our hold and NULL out so final_cleanup
13395 * doesn't try to end a lock seqid sync we
13396 * never started.
13397 */
13398 if (is_spec) {
13399 lock_owner_rele(lop);
13400 *lopp = NULL;
13401 }
13402 *skip_get_err = TRUE;
13403 *go_otwp = FALSE;
13404 return;
13405 }
13406
13407 ep->error = nfs4_start_lock_seqid_sync(lop, VTOMI4(vp));
13408 if (ep->error == EAGAIN) {
13409 lock_owner_rele(lop);
13410 *lopp = NULL;
13411 return;
13412 }
13413
13414 mutex_enter(&lop->lo_lock);
13415 locku_args->lock_stateid = lop->lock_stateid;
13416 mutex_exit(&lop->lo_lock);
13417 locku_args->seqid = lop->lock_seqid + 1;
13418
13419 /* leave the ref count on lop, rele after RPC call */
13420
13421 locku_args->offset = flk->l_start;
13422 locku_args->length = flk->l_len;
13423 if (flk->l_len == 0)
13424 locku_args->length = ~locku_args->length;
13425
13426 *go_otwp = TRUE;
13427 }
13428
13429 /*
13430 * Setup the LOCK4 arguments.
13431 *
13432 * Returns errors via the nfs4_error_t.
13433 * NFS4_OK no problems
13434 * NFS4ERR_DELAY caller should retry (like recovery retry)
13435 * (other) unrecoverable error
13436 */
13437 static void
nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype,LOCK4args ** lock_argsp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,nfs_argop4 * argop,COMPOUND4args_clnt * argsp,flock64_t * flk,int cmd,vnode_t * vp,cred_t * cr,nfs4_error_t * ep)13438 nfs4frlock_setup_lock_args(nfs4_lock_call_type_t ctype, LOCK4args **lock_argsp,
13439 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13440 nfs4_lock_owner_t **lopp, nfs_argop4 *argop, COMPOUND4args_clnt *argsp,
13441 flock64_t *flk, int cmd, vnode_t *vp, cred_t *cr, nfs4_error_t *ep)
13442 {
13443 LOCK4args *lock_args;
13444 nfs4_open_owner_t *oop = NULL;
13445 nfs4_open_stream_t *osp = NULL;
13446 nfs4_lock_owner_t *lop = NULL;
13447 pid_t pid;
13448 rnode4_t *rp = VTOR4(vp);
13449
13450 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13451
13452 nfs4frlock_check_deleg(vp, ep, cr, flk->l_type);
13453 if (ep->error || ep->stat != NFS4_OK)
13454 return;
13455
13456 argop->argop = OP_LOCK;
13457 if (ctype == NFS4_LCK_CTYPE_NORM)
13458 argsp->ctag = TAG_LOCK;
13459 else if (ctype == NFS4_LCK_CTYPE_RECLAIM)
13460 argsp->ctag = TAG_RELOCK;
13461 else
13462 argsp->ctag = TAG_LOCK_REINSTATE;
13463 lock_args = &argop->nfs_argop4_u.oplock;
13464 lock_args->locktype = flk_to_locktype(cmd, flk->l_type);
13465 lock_args->reclaim = ctype == NFS4_LCK_CTYPE_RECLAIM ? 1 : 0;
13466 /*
13467 * Get the lock owner. If no lock owner exists,
13468 * create a 'temporary' one and grab the open seqid
13469 * synchronization (which puts a hold on the open
13470 * owner and open stream).
13471 * This also grabs the lock seqid synchronization.
13472 */
13473 pid = ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid : flk->l_pid;
13474 ep->stat =
13475 nfs4_find_or_create_lock_owner(pid, rp, cr, &oop, &osp, &lop);
13476
13477 if (ep->stat != NFS4_OK)
13478 goto out;
13479
13480 nfs4_setup_lock_args(lop, oop, osp, mi2clientid(VTOMI4(vp)),
13481 &lock_args->locker);
13482
13483 lock_args->offset = flk->l_start;
13484 lock_args->length = flk->l_len;
13485 if (flk->l_len == 0)
13486 lock_args->length = ~lock_args->length;
13487 *lock_argsp = lock_args;
13488 out:
13489 *oopp = oop;
13490 *ospp = osp;
13491 *lopp = lop;
13492 }
13493
13494 /*
13495 * After we get the reply from the server, record the proper information
13496 * for possible resend lock requests.
13497 */
13498 static void
nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype,int error,nfs_lock_type4 locktype,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,nfs4_lock_owner_t * lop,flock64_t * flk,nfs4_lost_rqst_t * lost_rqstp,cred_t * cr,vnode_t * vp)13499 nfs4frlock_save_lost_rqst(nfs4_lock_call_type_t ctype, int error,
13500 nfs_lock_type4 locktype, nfs4_open_owner_t *oop,
13501 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
13502 nfs4_lost_rqst_t *lost_rqstp, cred_t *cr, vnode_t *vp)
13503 {
13504 bool_t unlock = (flk->l_type == F_UNLCK);
13505
13506 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13507 ASSERT(ctype == NFS4_LCK_CTYPE_NORM ||
13508 ctype == NFS4_LCK_CTYPE_REINSTATE);
13509
13510 if (error != 0 && !unlock) {
13511 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13512 nfs4_client_lock_debug), (CE_NOTE,
13513 "nfs4frlock_save_lost_rqst: set lo_pending_rqsts to 1 "
13514 " for lop %p", (void *)lop));
13515 ASSERT(lop != NULL);
13516 mutex_enter(&lop->lo_lock);
13517 lop->lo_pending_rqsts = 1;
13518 mutex_exit(&lop->lo_lock);
13519 }
13520
13521 lost_rqstp->lr_putfirst = FALSE;
13522 lost_rqstp->lr_op = 0;
13523
13524 /*
13525 * For lock/locku requests, we treat EINTR as ETIMEDOUT for
13526 * recovery purposes so that the lock request that was sent
13527 * can be saved and re-issued later. Ditto for EIO from a forced
13528 * unmount. This is done to have the client's local locking state
13529 * match the v4 server's state; that is, the request was
13530 * potentially received and accepted by the server but the client
13531 * thinks it was not.
13532 */
13533 if (error == ETIMEDOUT || error == EINTR ||
13534 NFS4_FRC_UNMT_ERR(error, vp->v_vfsp)) {
13535 NFS4_DEBUG((nfs4_lost_rqst_debug ||
13536 nfs4_client_lock_debug), (CE_NOTE,
13537 "nfs4frlock_save_lost_rqst: got a lost %s lock for "
13538 "lop %p oop %p osp %p", unlock ? "LOCKU" : "LOCK",
13539 (void *)lop, (void *)oop, (void *)osp));
13540 if (unlock)
13541 lost_rqstp->lr_op = OP_LOCKU;
13542 else {
13543 lost_rqstp->lr_op = OP_LOCK;
13544 lost_rqstp->lr_locktype = locktype;
13545 }
13546 /*
13547 * Objects are held and rele'd via the recovery code.
13548 * See nfs4_save_lost_rqst.
13549 */
13550 lost_rqstp->lr_vp = vp;
13551 lost_rqstp->lr_dvp = NULL;
13552 lost_rqstp->lr_oop = oop;
13553 lost_rqstp->lr_osp = osp;
13554 lost_rqstp->lr_lop = lop;
13555 lost_rqstp->lr_cr = cr;
13556 switch (ctype) {
13557 case NFS4_LCK_CTYPE_NORM:
13558 flk->l_pid = ttoproc(curthread)->p_pid;
13559 lost_rqstp->lr_ctype = NFS4_LCK_CTYPE_RESEND;
13560 break;
13561 case NFS4_LCK_CTYPE_REINSTATE:
13562 lost_rqstp->lr_putfirst = TRUE;
13563 lost_rqstp->lr_ctype = ctype;
13564 break;
13565 default:
13566 break;
13567 }
13568 lost_rqstp->lr_flk = flk;
13569 }
13570 }
13571
13572 /*
13573 * Update lop's seqid. Also update the seqid stored in a resend request,
13574 * if any. (Some recovery errors increment the seqid, and we may have to
13575 * send the resend request again.)
13576 */
13577
13578 static void
nfs4frlock_bump_seqid(LOCK4args * lock_args,LOCKU4args * locku_args,nfs4_open_owner_t * oop,nfs4_lock_owner_t * lop,nfs4_tag_type_t tag_type)13579 nfs4frlock_bump_seqid(LOCK4args *lock_args, LOCKU4args *locku_args,
13580 nfs4_open_owner_t *oop, nfs4_lock_owner_t *lop, nfs4_tag_type_t tag_type)
13581 {
13582 if (lock_args) {
13583 if (lock_args->locker.new_lock_owner == TRUE)
13584 nfs4_get_and_set_next_open_seqid(oop, tag_type);
13585 else {
13586 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13587 nfs4_set_lock_seqid(lop->lock_seqid + 1, lop);
13588 }
13589 } else if (locku_args) {
13590 ASSERT(lop->lo_flags & NFS4_LOCK_SEQID_INUSE);
13591 nfs4_set_lock_seqid(lop->lock_seqid +1, lop);
13592 }
13593 }
13594
13595 /*
13596 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13597 * COMPOUND4 args/res for calls that need to retry.
13598 * Switches the *cred_otwp to base_cr.
13599 */
13600 static void
nfs4frlock_check_access(vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,bool_t * did_start_fop,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,int error,nfs4_lock_owner_t ** lopp,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,cred_t * base_cr,cred_t ** cred_otwp)13601 nfs4frlock_check_access(vnode_t *vp, nfs4_op_hint_t op_hint,
13602 nfs4_recov_state_t *recov_statep, int needrecov, bool_t *did_start_fop,
13603 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp, int error,
13604 nfs4_lock_owner_t **lopp, nfs4_open_owner_t **oopp,
13605 nfs4_open_stream_t **ospp, cred_t *base_cr, cred_t **cred_otwp)
13606 {
13607 nfs4_open_owner_t *oop = *oopp;
13608 nfs4_open_stream_t *osp = *ospp;
13609 nfs4_lock_owner_t *lop = *lopp;
13610 nfs_argop4 *argop = (*argspp)->array;
13611
13612 if (*did_start_fop) {
13613 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13614 needrecov);
13615 *did_start_fop = FALSE;
13616 }
13617 ASSERT((*argspp)->array_len == 2);
13618 if (argop[1].argop == OP_LOCK)
13619 nfs4args_lock_free(&argop[1]);
13620 else if (argop[1].argop == OP_LOCKT)
13621 nfs4args_lockt_free(&argop[1]);
13622 kmem_free(argop, 2 * sizeof (nfs_argop4));
13623 if (!error)
13624 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13625 *argspp = NULL;
13626 *respp = NULL;
13627
13628 if (lop) {
13629 nfs4_end_lock_seqid_sync(lop);
13630 lock_owner_rele(lop);
13631 *lopp = NULL;
13632 }
13633
13634 /* need to free up the reference on osp for lock args */
13635 if (osp != NULL) {
13636 open_stream_rele(osp, VTOR4(vp));
13637 *ospp = NULL;
13638 }
13639
13640 /* need to free up the reference on oop for lock args */
13641 if (oop != NULL) {
13642 nfs4_end_open_seqid_sync(oop);
13643 open_owner_rele(oop);
13644 *oopp = NULL;
13645 }
13646
13647 crfree(*cred_otwp);
13648 *cred_otwp = base_cr;
13649 crhold(*cred_otwp);
13650 }
13651
13652 /*
13653 * Function to process the client's recovery for nfs4frlock.
13654 * Returns TRUE if we should retry the lock request; FALSE otherwise.
13655 *
13656 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13657 * COMPOUND4 args/res for calls that need to retry.
13658 *
13659 * Note: the rp's r_lkserlock is *not* dropped during this path.
13660 */
13661 static bool_t
nfs4frlock_recovery(int needrecov,nfs4_error_t * ep,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,LOCK4args * lock_args,LOCKU4args * locku_args,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,rnode4_t * rp,vnode_t * vp,nfs4_recov_state_t * recov_statep,nfs4_op_hint_t op_hint,bool_t * did_start_fop,nfs4_lost_rqst_t * lost_rqstp,flock64_t * flk)13662 nfs4frlock_recovery(int needrecov, nfs4_error_t *ep,
13663 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13664 LOCK4args *lock_args, LOCKU4args *locku_args,
13665 nfs4_open_owner_t **oopp, nfs4_open_stream_t **ospp,
13666 nfs4_lock_owner_t **lopp, rnode4_t *rp, vnode_t *vp,
13667 nfs4_recov_state_t *recov_statep, nfs4_op_hint_t op_hint,
13668 bool_t *did_start_fop, nfs4_lost_rqst_t *lost_rqstp, flock64_t *flk)
13669 {
13670 nfs4_open_owner_t *oop = *oopp;
13671 nfs4_open_stream_t *osp = *ospp;
13672 nfs4_lock_owner_t *lop = *lopp;
13673
13674 bool_t abort, retry;
13675
13676 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13677 ASSERT((*argspp) != NULL);
13678 ASSERT((*respp) != NULL);
13679 if (lock_args || locku_args)
13680 ASSERT(lop != NULL);
13681
13682 NFS4_DEBUG((nfs4_client_lock_debug || nfs4_client_recov_debug),
13683 (CE_NOTE, "nfs4frlock_recovery: initiating recovery\n"));
13684
13685 retry = TRUE;
13686 abort = FALSE;
13687 if (needrecov) {
13688 nfs4_bseqid_entry_t *bsep = NULL;
13689 nfs_opnum4 op;
13690
13691 op = lock_args ? OP_LOCK : locku_args ? OP_LOCKU : OP_LOCKT;
13692
13693 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID) {
13694 seqid4 seqid;
13695
13696 if (lock_args) {
13697 if (lock_args->locker.new_lock_owner == TRUE)
13698 seqid = lock_args->locker.locker4_u.
13699 open_owner.open_seqid;
13700 else
13701 seqid = lock_args->locker.locker4_u.
13702 lock_owner.lock_seqid;
13703 } else if (locku_args) {
13704 seqid = locku_args->seqid;
13705 } else {
13706 seqid = 0;
13707 }
13708
13709 bsep = nfs4_create_bseqid_entry(oop, lop, vp,
13710 flk->l_pid, (*argspp)->ctag, seqid);
13711 }
13712
13713 abort = nfs4_start_recovery(ep, VTOMI4(vp), vp, NULL, NULL,
13714 (lost_rqstp && (lost_rqstp->lr_op == OP_LOCK ||
13715 lost_rqstp->lr_op == OP_LOCKU)) ? lost_rqstp :
13716 NULL, op, bsep, NULL, NULL);
13717
13718 if (bsep)
13719 kmem_free(bsep, sizeof (*bsep));
13720 }
13721
13722 /*
13723 * Return that we do not want to retry the request for 3 cases:
13724 * 1. If we received EINTR or are bailing out because of a forced
13725 * unmount, we came into this code path just for the sake of
13726 * initiating recovery, we now need to return the error.
13727 * 2. If we have aborted recovery.
13728 * 3. We received NFS4ERR_BAD_SEQID.
13729 */
13730 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp) ||
13731 abort == TRUE || (ep->error == 0 && ep->stat == NFS4ERR_BAD_SEQID))
13732 retry = FALSE;
13733
13734 if (*did_start_fop == TRUE) {
13735 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint, recov_statep,
13736 needrecov);
13737 *did_start_fop = FALSE;
13738 }
13739
13740 if (retry == TRUE) {
13741 nfs_argop4 *argop;
13742
13743 argop = (*argspp)->array;
13744 ASSERT((*argspp)->array_len == 2);
13745
13746 if (argop[1].argop == OP_LOCK)
13747 nfs4args_lock_free(&argop[1]);
13748 else if (argop[1].argop == OP_LOCKT)
13749 nfs4args_lockt_free(&argop[1]);
13750 kmem_free(argop, 2 * sizeof (nfs_argop4));
13751 if (!ep->error)
13752 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)*respp);
13753 *respp = NULL;
13754 *argspp = NULL;
13755 }
13756
13757 if (lop != NULL) {
13758 nfs4_end_lock_seqid_sync(lop);
13759 lock_owner_rele(lop);
13760 }
13761
13762 *lopp = NULL;
13763
13764 /* need to free up the reference on osp for lock args */
13765 if (osp != NULL) {
13766 open_stream_rele(osp, rp);
13767 *ospp = NULL;
13768 }
13769
13770 /* need to free up the reference on oop for lock args */
13771 if (oop != NULL) {
13772 nfs4_end_open_seqid_sync(oop);
13773 open_owner_rele(oop);
13774 *oopp = NULL;
13775 }
13776
13777 return (retry);
13778 }
13779
13780 /*
13781 * Handles the successful reply from the server for nfs4frlock.
13782 */
13783 static void
nfs4frlock_results_ok(nfs4_lock_call_type_t ctype,int cmd,flock64_t * flk,vnode_t * vp,int flag,u_offset_t offset,nfs4_lost_rqst_t * resend_rqstp)13784 nfs4frlock_results_ok(nfs4_lock_call_type_t ctype, int cmd, flock64_t *flk,
13785 vnode_t *vp, int flag, u_offset_t offset,
13786 nfs4_lost_rqst_t *resend_rqstp)
13787 {
13788 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13789 if ((cmd == F_SETLK || cmd == F_SETLKW) &&
13790 (flk->l_type == F_RDLCK || flk->l_type == F_WRLCK)) {
13791 if (ctype == NFS4_LCK_CTYPE_NORM) {
13792 flk->l_pid = ttoproc(curthread)->p_pid;
13793 /*
13794 * We do not register lost locks locally in
13795 * the 'resend' case since the user/application
13796 * doesn't think we have the lock.
13797 */
13798 ASSERT(!resend_rqstp);
13799 nfs4_register_lock_locally(vp, flk, flag, offset);
13800 }
13801 }
13802 }
13803
13804 /*
13805 * Handle the DENIED reply from the server for nfs4frlock.
13806 * Returns TRUE if we should retry the request; FALSE otherwise.
13807 *
13808 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
13809 * COMPOUND4 args/res for calls that need to retry. Can also
13810 * drop and regrab the r_lkserlock.
13811 */
13812 static bool_t
nfs4frlock_results_denied(nfs4_lock_call_type_t ctype,LOCK4args * lock_args,LOCKT4args * lockt_args,nfs4_open_owner_t ** oopp,nfs4_open_stream_t ** ospp,nfs4_lock_owner_t ** lopp,int cmd,vnode_t * vp,flock64_t * flk,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,COMPOUND4args_clnt ** argspp,COMPOUND4res_clnt ** respp,clock_t * tick_delayp,short * whencep,int * errorp,nfs_resop4 * resop,cred_t * cr,bool_t * did_start_fop,bool_t * skip_get_err)13813 nfs4frlock_results_denied(nfs4_lock_call_type_t ctype, LOCK4args *lock_args,
13814 LOCKT4args *lockt_args, nfs4_open_owner_t **oopp,
13815 nfs4_open_stream_t **ospp, nfs4_lock_owner_t **lopp, int cmd,
13816 vnode_t *vp, flock64_t *flk, nfs4_op_hint_t op_hint,
13817 nfs4_recov_state_t *recov_statep, int needrecov,
13818 COMPOUND4args_clnt **argspp, COMPOUND4res_clnt **respp,
13819 clock_t *tick_delayp, short *whencep, int *errorp,
13820 nfs_resop4 *resop, cred_t *cr, bool_t *did_start_fop,
13821 bool_t *skip_get_err)
13822 {
13823 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13824
13825 if (lock_args) {
13826 nfs4_open_owner_t *oop = *oopp;
13827 nfs4_open_stream_t *osp = *ospp;
13828 nfs4_lock_owner_t *lop = *lopp;
13829 int intr;
13830
13831 /*
13832 * Blocking lock needs to sleep and retry from the request.
13833 *
13834 * Do not block and wait for 'resend' or 'reinstate'
13835 * lock requests, just return the error.
13836 *
13837 * Note: reclaim requests have cmd == F_SETLK, not F_SETLKW.
13838 */
13839 if (cmd == F_SETLKW) {
13840 rnode4_t *rp = VTOR4(vp);
13841 nfs_argop4 *argop = (*argspp)->array;
13842
13843 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
13844
13845 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
13846 recov_statep, needrecov);
13847 *did_start_fop = FALSE;
13848 ASSERT((*argspp)->array_len == 2);
13849 if (argop[1].argop == OP_LOCK)
13850 nfs4args_lock_free(&argop[1]);
13851 else if (argop[1].argop == OP_LOCKT)
13852 nfs4args_lockt_free(&argop[1]);
13853 kmem_free(argop, 2 * sizeof (nfs_argop4));
13854 if (*respp)
13855 xdr_free(xdr_COMPOUND4res_clnt,
13856 (caddr_t)*respp);
13857 *argspp = NULL;
13858 *respp = NULL;
13859 nfs4_end_lock_seqid_sync(lop);
13860 lock_owner_rele(lop);
13861 *lopp = NULL;
13862 if (osp != NULL) {
13863 open_stream_rele(osp, rp);
13864 *ospp = NULL;
13865 }
13866 if (oop != NULL) {
13867 nfs4_end_open_seqid_sync(oop);
13868 open_owner_rele(oop);
13869 *oopp = NULL;
13870 }
13871
13872 nfs_rw_exit(&rp->r_lkserlock);
13873
13874 intr = nfs4_block_and_wait(tick_delayp, rp);
13875
13876 if (intr) {
13877 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13878 RW_WRITER, FALSE);
13879 *errorp = EINTR;
13880 return (FALSE);
13881 }
13882
13883 (void) nfs_rw_enter_sig(&rp->r_lkserlock,
13884 RW_WRITER, FALSE);
13885
13886 /*
13887 * Make sure we are still safe to lock with
13888 * regards to mmapping.
13889 */
13890 if (!nfs4_safelock(vp, flk, cr)) {
13891 *errorp = EAGAIN;
13892 return (FALSE);
13893 }
13894
13895 return (TRUE);
13896 }
13897 if (ctype == NFS4_LCK_CTYPE_NORM)
13898 *errorp = EAGAIN;
13899 *skip_get_err = TRUE;
13900 flk->l_whence = 0;
13901 *whencep = 0;
13902 return (FALSE);
13903 } else if (lockt_args) {
13904 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13905 "nfs4frlock_results_denied: OP_LOCKT DENIED"));
13906
13907 denied_to_flk(&resop->nfs_resop4_u.oplockt.denied,
13908 flk, lockt_args);
13909
13910 /* according to NLM code */
13911 *errorp = 0;
13912 *whencep = 0;
13913 *skip_get_err = TRUE;
13914 return (FALSE);
13915 }
13916 return (FALSE);
13917 }
13918
13919 /*
13920 * Handles all NFS4 errors besides NFS4_OK and NFS4ERR_DENIED for nfs4frlock.
13921 */
13922 static void
nfs4frlock_results_default(COMPOUND4res_clnt * resp,int * errorp)13923 nfs4frlock_results_default(COMPOUND4res_clnt *resp, int *errorp)
13924 {
13925 switch (resp->status) {
13926 case NFS4ERR_ACCESS:
13927 case NFS4ERR_ADMIN_REVOKED:
13928 case NFS4ERR_BADHANDLE:
13929 case NFS4ERR_BAD_RANGE:
13930 case NFS4ERR_BAD_SEQID:
13931 case NFS4ERR_BAD_STATEID:
13932 case NFS4ERR_BADXDR:
13933 case NFS4ERR_DEADLOCK:
13934 case NFS4ERR_DELAY:
13935 case NFS4ERR_EXPIRED:
13936 case NFS4ERR_FHEXPIRED:
13937 case NFS4ERR_GRACE:
13938 case NFS4ERR_INVAL:
13939 case NFS4ERR_ISDIR:
13940 case NFS4ERR_LEASE_MOVED:
13941 case NFS4ERR_LOCK_NOTSUPP:
13942 case NFS4ERR_LOCK_RANGE:
13943 case NFS4ERR_MOVED:
13944 case NFS4ERR_NOFILEHANDLE:
13945 case NFS4ERR_NO_GRACE:
13946 case NFS4ERR_OLD_STATEID:
13947 case NFS4ERR_OPENMODE:
13948 case NFS4ERR_RECLAIM_BAD:
13949 case NFS4ERR_RECLAIM_CONFLICT:
13950 case NFS4ERR_RESOURCE:
13951 case NFS4ERR_SERVERFAULT:
13952 case NFS4ERR_STALE:
13953 case NFS4ERR_STALE_CLIENTID:
13954 case NFS4ERR_STALE_STATEID:
13955 return;
13956 default:
13957 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
13958 "nfs4frlock_results_default: got unrecognizable "
13959 "res.status %d", resp->status));
13960 *errorp = NFS4ERR_INVAL;
13961 }
13962 }
13963
13964 /*
13965 * The lock request was successful, so update the client's state.
13966 */
13967 static void
nfs4frlock_update_state(LOCK4args * lock_args,LOCKU4args * locku_args,LOCKT4args * lockt_args,nfs_resop4 * resop,nfs4_lock_owner_t * lop,vnode_t * vp,flock64_t * flk,cred_t * cr,nfs4_lost_rqst_t * resend_rqstp)13968 nfs4frlock_update_state(LOCK4args *lock_args, LOCKU4args *locku_args,
13969 LOCKT4args *lockt_args, nfs_resop4 *resop, nfs4_lock_owner_t *lop,
13970 vnode_t *vp, flock64_t *flk, cred_t *cr,
13971 nfs4_lost_rqst_t *resend_rqstp)
13972 {
13973 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
13974
13975 if (lock_args) {
13976 LOCK4res *lock_res;
13977
13978 lock_res = &resop->nfs_resop4_u.oplock;
13979 /* update the stateid with server's response */
13980
13981 if (lock_args->locker.new_lock_owner == TRUE) {
13982 mutex_enter(&lop->lo_lock);
13983 lop->lo_just_created = NFS4_PERM_CREATED;
13984 mutex_exit(&lop->lo_lock);
13985 }
13986
13987 nfs4_set_lock_stateid(lop, lock_res->LOCK4res_u.lock_stateid);
13988
13989 /*
13990 * If the lock was the result of a resending a lost
13991 * request, we've synched up the stateid and seqid
13992 * with the server, but now the server might be out of sync
13993 * with what the application thinks it has for locks.
13994 * Clean that up here. It's unclear whether we should do
13995 * this even if the filesystem has been forcibly unmounted.
13996 * For most servers, it's probably wasted effort, but
13997 * RFC 7530 lets servers require that unlocks exactly match
13998 * the locks that are held.
13999 */
14000 if (resend_rqstp != NULL &&
14001 resend_rqstp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE) {
14002 nfs4_reinstitute_local_lock_state(vp, flk, cr, lop);
14003 } else {
14004 flk->l_whence = 0;
14005 }
14006 } else if (locku_args) {
14007 LOCKU4res *locku_res;
14008
14009 locku_res = &resop->nfs_resop4_u.oplocku;
14010
14011 /* Update the stateid with the server's response */
14012 nfs4_set_lock_stateid(lop, locku_res->lock_stateid);
14013 } else if (lockt_args) {
14014 /* Switch the lock type to express success, see fcntl */
14015 flk->l_type = F_UNLCK;
14016 flk->l_whence = 0;
14017 }
14018 }
14019
14020 /*
14021 * Do final cleanup before exiting nfs4frlock.
14022 * Calls nfs4_end_fop, drops the seqid syncs, and frees up the
14023 * COMPOUND4 args/res for calls that haven't already.
14024 */
14025 static void
nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype,COMPOUND4args_clnt * argsp,COMPOUND4res_clnt * resp,vnode_t * vp,nfs4_op_hint_t op_hint,nfs4_recov_state_t * recov_statep,int needrecov,nfs4_open_owner_t * oop,nfs4_open_stream_t * osp,nfs4_lock_owner_t * lop,flock64_t * flk,short whence,u_offset_t offset,struct lm_sysid * ls,int * errorp,LOCK4args * lock_args,LOCKU4args * locku_args,bool_t did_start_fop,bool_t skip_get_err,cred_t * cred_otw,cred_t * cred)14026 nfs4frlock_final_cleanup(nfs4_lock_call_type_t ctype, COMPOUND4args_clnt *argsp,
14027 COMPOUND4res_clnt *resp, vnode_t *vp, nfs4_op_hint_t op_hint,
14028 nfs4_recov_state_t *recov_statep, int needrecov, nfs4_open_owner_t *oop,
14029 nfs4_open_stream_t *osp, nfs4_lock_owner_t *lop, flock64_t *flk,
14030 short whence, u_offset_t offset, struct lm_sysid *ls,
14031 int *errorp, LOCK4args *lock_args, LOCKU4args *locku_args,
14032 bool_t did_start_fop, bool_t skip_get_err,
14033 cred_t *cred_otw, cred_t *cred)
14034 {
14035 mntinfo4_t *mi = VTOMI4(vp);
14036 rnode4_t *rp = VTOR4(vp);
14037 int error = *errorp;
14038 nfs_argop4 *argop;
14039 int do_flush_pages = 0;
14040
14041 ASSERT(nfs_zone() == mi->mi_zone);
14042 /*
14043 * The client recovery code wants the raw status information,
14044 * so don't map the NFS status code to an errno value for
14045 * non-normal call types.
14046 */
14047 if (ctype == NFS4_LCK_CTYPE_NORM) {
14048 if (*errorp == 0 && resp != NULL && skip_get_err == FALSE)
14049 *errorp = geterrno4(resp->status);
14050 if (did_start_fop == TRUE)
14051 nfs4_end_fop(mi, vp, NULL, op_hint, recov_statep,
14052 needrecov);
14053
14054 /*
14055 * We've established a new lock on the server, so invalidate
14056 * the pages associated with the vnode to get the most up to
14057 * date pages from the server after acquiring the lock. We
14058 * want to be sure that the read operation gets the newest data.
14059 * N.B.
14060 * We used to do this in nfs4frlock_results_ok but that doesn't
14061 * work since VOP_PUTPAGE can call nfs4_commit which calls
14062 * nfs4_start_fop. We flush the pages below after calling
14063 * nfs4_end_fop above
14064 * The flush of the page cache must be done after
14065 * nfs4_end_open_seqid_sync() to avoid a 4-way hang.
14066 */
14067 if (!error && resp && resp->status == NFS4_OK)
14068 do_flush_pages = 1;
14069 }
14070 if (argsp) {
14071 ASSERT(argsp->array_len == 2);
14072 argop = argsp->array;
14073 if (argop[1].argop == OP_LOCK)
14074 nfs4args_lock_free(&argop[1]);
14075 else if (argop[1].argop == OP_LOCKT)
14076 nfs4args_lockt_free(&argop[1]);
14077 kmem_free(argop, 2 * sizeof (nfs_argop4));
14078 if (resp)
14079 xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)resp);
14080 }
14081
14082 /* free the reference on the lock owner */
14083 if (lop != NULL) {
14084 nfs4_end_lock_seqid_sync(lop);
14085 lock_owner_rele(lop);
14086 }
14087
14088 /* need to free up the reference on osp for lock args */
14089 if (osp != NULL)
14090 open_stream_rele(osp, rp);
14091
14092 /* need to free up the reference on oop for lock args */
14093 if (oop != NULL) {
14094 nfs4_end_open_seqid_sync(oop);
14095 open_owner_rele(oop);
14096 }
14097
14098 if (do_flush_pages)
14099 nfs4_flush_pages(vp, cred);
14100
14101 (void) convoff(vp, flk, whence, offset);
14102
14103 lm_rel_sysid(ls);
14104
14105 /*
14106 * Record debug information in the event we get EINVAL.
14107 */
14108 mutex_enter(&mi->mi_lock);
14109 if (*errorp == EINVAL && (lock_args || locku_args) &&
14110 (!(mi->mi_flags & MI4_POSIX_LOCK))) {
14111 if (!(mi->mi_flags & MI4_LOCK_DEBUG)) {
14112 zcmn_err(getzoneid(), CE_NOTE,
14113 "%s operation failed with "
14114 "EINVAL probably since the server, %s,"
14115 " doesn't support POSIX style locking",
14116 lock_args ? "LOCK" : "LOCKU",
14117 mi->mi_curr_serv->sv_hostname);
14118 mi->mi_flags |= MI4_LOCK_DEBUG;
14119 }
14120 }
14121 mutex_exit(&mi->mi_lock);
14122
14123 if (cred_otw)
14124 crfree(cred_otw);
14125 }
14126
14127 /*
14128 * This calls the server and the local locking code.
14129 *
14130 * Client locks are registerred locally by oring the sysid with
14131 * LM_SYSID_CLIENT. The server registers locks locally using just the sysid.
14132 * We need to distinguish between the two to avoid collision in case one
14133 * machine is used as both client and server.
14134 *
14135 * Blocking lock requests will continually retry to acquire the lock
14136 * forever.
14137 *
14138 * The ctype is defined as follows:
14139 * NFS4_LCK_CTYPE_NORM: normal lock request.
14140 *
14141 * NFS4_LCK_CTYPE_RECLAIM: bypass the usual calls for synchronizing with client
14142 * recovery, get the pid from flk instead of curproc, and don't reregister
14143 * the lock locally.
14144 *
14145 * NFS4_LCK_CTYPE_RESEND: same as NFS4_LCK_CTYPE_RECLAIM, with the addition
14146 * that we will use the information passed in via resend_rqstp to setup the
14147 * lock/locku request. This resend is the exact same request as the 'lost
14148 * lock', and is initiated by the recovery framework. A successful resend
14149 * request can initiate one or more reinstate requests.
14150 *
14151 * NFS4_LCK_CTYPE_REINSTATE: same as NFS4_LCK_CTYPE_RESEND, except that it
14152 * does not trigger additional reinstate requests. This lock call type is
14153 * set for setting the v4 server's locking state back to match what the
14154 * client's local locking state is in the event of a received 'lost lock'.
14155 *
14156 * Errors are returned via the nfs4_error_t parameter.
14157 */
14158 void
nfs4frlock(nfs4_lock_call_type_t ctype,vnode_t * vp,int cmd,flock64_t * flk,int flag,u_offset_t offset,cred_t * cr,nfs4_error_t * ep,nfs4_lost_rqst_t * resend_rqstp,int * did_reclaimp)14159 nfs4frlock(nfs4_lock_call_type_t ctype, vnode_t *vp, int cmd, flock64_t *flk,
14160 int flag, u_offset_t offset, cred_t *cr, nfs4_error_t *ep,
14161 nfs4_lost_rqst_t *resend_rqstp, int *did_reclaimp)
14162 {
14163 COMPOUND4args_clnt args, *argsp = NULL;
14164 COMPOUND4res_clnt res, *resp = NULL;
14165 nfs_argop4 *argop;
14166 nfs_resop4 *resop;
14167 rnode4_t *rp;
14168 int doqueue = 1;
14169 clock_t tick_delay; /* delay in clock ticks */
14170 struct lm_sysid *ls;
14171 LOCK4args *lock_args = NULL;
14172 LOCKU4args *locku_args = NULL;
14173 LOCKT4args *lockt_args = NULL;
14174 nfs4_open_owner_t *oop = NULL;
14175 nfs4_open_stream_t *osp = NULL;
14176 nfs4_lock_owner_t *lop = NULL;
14177 bool_t needrecov = FALSE;
14178 nfs4_recov_state_t recov_state;
14179 short whence;
14180 nfs4_op_hint_t op_hint;
14181 nfs4_lost_rqst_t lost_rqst;
14182 bool_t retry = FALSE;
14183 bool_t did_start_fop = FALSE;
14184 bool_t skip_get_err = FALSE;
14185 cred_t *cred_otw = NULL;
14186 bool_t recovonly; /* just queue request */
14187 int frc_no_reclaim = 0;
14188 #ifdef DEBUG
14189 char *name;
14190 #endif
14191
14192 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14193
14194 #ifdef DEBUG
14195 name = fn_name(VTOSV(vp)->sv_name);
14196 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4frlock: "
14197 "%s: cmd %d, type %d, offset %llu, start %"PRIx64", "
14198 "length %"PRIu64", pid %d, sysid %d, call type %s, "
14199 "resend request %s", name, cmd, flk->l_type, offset, flk->l_start,
14200 flk->l_len, ctype == NFS4_LCK_CTYPE_NORM ? curproc->p_pid :
14201 flk->l_pid, flk->l_sysid, nfs4frlock_get_call_type(ctype),
14202 resend_rqstp ? "TRUE" : "FALSE"));
14203 kmem_free(name, MAXNAMELEN);
14204 #endif
14205
14206 nfs4_error_zinit(ep);
14207 ep->error = nfs4frlock_validate_args(cmd, flk, flag, vp, offset);
14208 if (ep->error)
14209 return;
14210 ep->error = nfs4frlock_get_sysid(&ls, vp, flk);
14211 if (ep->error)
14212 return;
14213 nfs4frlock_pre_setup(&tick_delay, &recov_state, flk, &whence,
14214 vp, cr, &cred_otw);
14215
14216 recov_retry:
14217 nfs4frlock_call_init(&args, &argsp, &argop, &op_hint, flk, cmd,
14218 &retry, &did_start_fop, &resp, &skip_get_err, &lost_rqst);
14219 rp = VTOR4(vp);
14220
14221 ep->error = nfs4frlock_start_call(ctype, vp, op_hint, &recov_state,
14222 &did_start_fop, &recovonly);
14223
14224 if (ep->error)
14225 goto out;
14226
14227 if (recovonly) {
14228 /*
14229 * Leave the request for the recovery system to deal with.
14230 */
14231 ASSERT(ctype == NFS4_LCK_CTYPE_NORM);
14232 ASSERT(cmd != F_GETLK);
14233 ASSERT(flk->l_type == F_UNLCK);
14234
14235 nfs4_error_init(ep, EINTR);
14236 needrecov = TRUE;
14237 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14238 if (lop != NULL) {
14239 nfs4frlock_save_lost_rqst(ctype, ep->error, READ_LT,
14240 NULL, NULL, lop, flk, &lost_rqst, cr, vp);
14241 (void) nfs4_start_recovery(ep,
14242 VTOMI4(vp), vp, NULL, NULL,
14243 (lost_rqst.lr_op == OP_LOCK ||
14244 lost_rqst.lr_op == OP_LOCKU) ?
14245 &lost_rqst : NULL, OP_LOCKU, NULL, NULL, NULL);
14246 lock_owner_rele(lop);
14247 lop = NULL;
14248 }
14249 flk->l_pid = curproc->p_pid;
14250 nfs4_register_lock_locally(vp, flk, flag, offset);
14251 goto out;
14252 }
14253
14254 /* putfh directory fh */
14255 argop[0].argop = OP_CPUTFH;
14256 argop[0].nfs_argop4_u.opcputfh.sfh = rp->r_fh;
14257
14258 /*
14259 * Set up the over-the-wire arguments and get references to the
14260 * open owner, etc.
14261 */
14262
14263 if (ctype == NFS4_LCK_CTYPE_RESEND ||
14264 ctype == NFS4_LCK_CTYPE_REINSTATE) {
14265 nfs4frlock_setup_resend_lock_args(resend_rqstp, argsp,
14266 &argop[1], &lop, &oop, &osp, &lock_args, &locku_args);
14267 } else {
14268 bool_t go_otw = TRUE;
14269
14270 ASSERT(resend_rqstp == NULL);
14271
14272 switch (cmd) {
14273 case F_GETLK:
14274 nfs4frlock_setup_lockt_args(ctype, &argop[1],
14275 &lockt_args, argsp, flk, rp);
14276 break;
14277 case F_SETLKW:
14278 case F_SETLK:
14279 if (flk->l_type == F_UNLCK)
14280 nfs4frlock_setup_locku_args(ctype,
14281 &argop[1], &locku_args, flk,
14282 &lop, ep, argsp,
14283 vp, flag, offset, cr,
14284 &skip_get_err, &go_otw);
14285 else
14286 nfs4frlock_setup_lock_args(ctype,
14287 &lock_args, &oop, &osp, &lop, &argop[1],
14288 argsp, flk, cmd, vp, cr, ep);
14289
14290 if (ep->error)
14291 goto out;
14292
14293 switch (ep->stat) {
14294 case NFS4_OK:
14295 break;
14296 case NFS4ERR_DELAY:
14297 /* recov thread never gets this error */
14298 ASSERT(resend_rqstp == NULL);
14299 ASSERT(did_start_fop);
14300
14301 nfs4_end_fop(VTOMI4(vp), vp, NULL, op_hint,
14302 &recov_state, TRUE);
14303 did_start_fop = FALSE;
14304 if (argop[1].argop == OP_LOCK)
14305 nfs4args_lock_free(&argop[1]);
14306 else if (argop[1].argop == OP_LOCKT)
14307 nfs4args_lockt_free(&argop[1]);
14308 kmem_free(argop, 2 * sizeof (nfs_argop4));
14309 argsp = NULL;
14310 goto recov_retry;
14311 default:
14312 ep->error = EIO;
14313 goto out;
14314 }
14315 break;
14316 default:
14317 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14318 "nfs4_frlock: invalid cmd %d", cmd));
14319 ep->error = EINVAL;
14320 goto out;
14321 }
14322
14323 if (!go_otw)
14324 goto out;
14325 }
14326
14327 /* XXX should we use the local reclock as a cache ? */
14328 /*
14329 * Unregister the lock with the local locking code before
14330 * contacting the server. This avoids a potential race where
14331 * another process gets notified that it has been granted a lock
14332 * before we can unregister ourselves locally.
14333 */
14334 if ((cmd == F_SETLK || cmd == F_SETLKW) && flk->l_type == F_UNLCK) {
14335 if (ctype == NFS4_LCK_CTYPE_NORM)
14336 flk->l_pid = ttoproc(curthread)->p_pid;
14337 nfs4_register_lock_locally(vp, flk, flag, offset);
14338 }
14339
14340 /*
14341 * Send the server the lock request. Continually loop with a delay
14342 * if get error NFS4ERR_DENIED (for blocking locks) or NFS4ERR_GRACE.
14343 */
14344 resp = &res;
14345
14346 NFS4_DEBUG((nfs4_client_call_debug || nfs4_client_lock_debug),
14347 (CE_NOTE,
14348 "nfs4frlock: %s call, rp %s", needrecov ? "recov" : "first",
14349 rnode4info(rp)));
14350
14351 if (lock_args && frc_no_reclaim) {
14352 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14353 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14354 "nfs4frlock: frc_no_reclaim: clearing reclaim"));
14355 lock_args->reclaim = FALSE;
14356 if (did_reclaimp)
14357 *did_reclaimp = 0;
14358 }
14359
14360 /*
14361 * Do the OTW call.
14362 */
14363 rfs4call(VTOMI4(vp), argsp, resp, cred_otw, &doqueue, 0, ep);
14364
14365 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14366 "nfs4frlock: error %d, status %d", ep->error, resp->status));
14367
14368 needrecov = nfs4_needs_recovery(ep, TRUE, vp->v_vfsp);
14369 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14370 "nfs4frlock: needrecov %d", needrecov));
14371
14372 if (ep->error == 0 && nfs4_need_to_bump_seqid(resp))
14373 nfs4frlock_bump_seqid(lock_args, locku_args, oop, lop,
14374 args.ctag);
14375
14376 /*
14377 * Check if one of these mutually exclusive error cases has
14378 * happened:
14379 * need to swap credentials due to access error
14380 * recovery is needed
14381 * different error (only known case is missing Kerberos ticket)
14382 */
14383
14384 if ((ep->error == EACCES ||
14385 (ep->error == 0 && resp->status == NFS4ERR_ACCESS)) &&
14386 cred_otw != cr) {
14387 nfs4frlock_check_access(vp, op_hint, &recov_state, needrecov,
14388 &did_start_fop, &argsp, &resp, ep->error, &lop, &oop, &osp,
14389 cr, &cred_otw);
14390 goto recov_retry;
14391 }
14392
14393 if (needrecov) {
14394 /*
14395 * LOCKT requests don't need to recover from lost
14396 * requests since they don't create/modify state.
14397 */
14398 if ((ep->error == EINTR ||
14399 NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) &&
14400 lockt_args)
14401 goto out;
14402 /*
14403 * Do not attempt recovery for requests initiated by
14404 * the recovery framework. Let the framework redrive them.
14405 */
14406 if (ctype != NFS4_LCK_CTYPE_NORM)
14407 goto out;
14408 else {
14409 ASSERT(resend_rqstp == NULL);
14410 }
14411
14412 nfs4frlock_save_lost_rqst(ctype, ep->error,
14413 flk_to_locktype(cmd, flk->l_type),
14414 oop, osp, lop, flk, &lost_rqst, cred_otw, vp);
14415
14416 retry = nfs4frlock_recovery(needrecov, ep, &argsp,
14417 &resp, lock_args, locku_args, &oop, &osp, &lop,
14418 rp, vp, &recov_state, op_hint, &did_start_fop,
14419 cmd != F_GETLK ? &lost_rqst : NULL, flk);
14420
14421 if (retry) {
14422 ASSERT(oop == NULL);
14423 ASSERT(osp == NULL);
14424 ASSERT(lop == NULL);
14425 goto recov_retry;
14426 }
14427 goto out;
14428 }
14429
14430 /*
14431 * Bail out if have reached this point with ep->error set. Can
14432 * happen if (ep->error == EACCES && !needrecov && cred_otw == cr).
14433 * This happens if Kerberos ticket has expired or has been
14434 * destroyed.
14435 */
14436 if (ep->error != 0)
14437 goto out;
14438
14439 /*
14440 * Process the reply.
14441 */
14442 switch (resp->status) {
14443 case NFS4_OK:
14444 resop = &resp->array[1];
14445 nfs4frlock_results_ok(ctype, cmd, flk, vp, flag, offset,
14446 resend_rqstp);
14447 /*
14448 * Have a successful lock operation, now update state.
14449 */
14450 nfs4frlock_update_state(lock_args, locku_args, lockt_args,
14451 resop, lop, vp, flk, cr, resend_rqstp);
14452 break;
14453
14454 case NFS4ERR_DENIED:
14455 resop = &resp->array[1];
14456 retry = nfs4frlock_results_denied(ctype, lock_args, lockt_args,
14457 &oop, &osp, &lop, cmd, vp, flk, op_hint,
14458 &recov_state, needrecov, &argsp, &resp,
14459 &tick_delay, &whence, &ep->error, resop, cr,
14460 &did_start_fop, &skip_get_err);
14461
14462 if (retry) {
14463 ASSERT(oop == NULL);
14464 ASSERT(osp == NULL);
14465 ASSERT(lop == NULL);
14466 goto recov_retry;
14467 }
14468 break;
14469 /*
14470 * If the server won't let us reclaim, fall-back to trying to lock
14471 * the file from scratch. Code elsewhere will check the changeinfo
14472 * to ensure the file hasn't been changed.
14473 */
14474 case NFS4ERR_NO_GRACE:
14475 if (lock_args && lock_args->reclaim == TRUE) {
14476 ASSERT(ctype == NFS4_LCK_CTYPE_RECLAIM);
14477 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14478 "nfs4frlock: reclaim: NFS4ERR_NO_GRACE"));
14479 frc_no_reclaim = 1;
14480 /* clean up before retrying */
14481 needrecov = 0;
14482 (void) nfs4frlock_recovery(needrecov, ep, &argsp, &resp,
14483 lock_args, locku_args, &oop, &osp, &lop, rp, vp,
14484 &recov_state, op_hint, &did_start_fop, NULL, flk);
14485 goto recov_retry;
14486 }
14487 /* FALLTHROUGH */
14488
14489 default:
14490 nfs4frlock_results_default(resp, &ep->error);
14491 break;
14492 }
14493 out:
14494 /*
14495 * Process and cleanup from error. Make interrupted unlock
14496 * requests look successful, since they will be handled by the
14497 * client recovery code.
14498 */
14499 nfs4frlock_final_cleanup(ctype, argsp, resp, vp, op_hint, &recov_state,
14500 needrecov, oop, osp, lop, flk, whence, offset, ls, &ep->error,
14501 lock_args, locku_args, did_start_fop,
14502 skip_get_err, cred_otw, cr);
14503
14504 if (ep->error == EINTR && flk->l_type == F_UNLCK &&
14505 (cmd == F_SETLK || cmd == F_SETLKW))
14506 ep->error = 0;
14507 }
14508
14509 /*
14510 * nfs4_safelock:
14511 *
14512 * Return non-zero if the given lock request can be handled without
14513 * violating the constraints on concurrent mapping and locking.
14514 */
14515
14516 static int
nfs4_safelock(vnode_t * vp,const struct flock64 * bfp,cred_t * cr)14517 nfs4_safelock(vnode_t *vp, const struct flock64 *bfp, cred_t *cr)
14518 {
14519 rnode4_t *rp = VTOR4(vp);
14520 struct vattr va;
14521 int error;
14522
14523 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14524 ASSERT(rp->r_mapcnt >= 0);
14525 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock %s: "
14526 "(%"PRIx64", %"PRIx64"); mapcnt = %ld", bfp->l_type == F_WRLCK ?
14527 "write" : bfp->l_type == F_RDLCK ? "read" : "unlock",
14528 bfp->l_start, bfp->l_len, rp->r_mapcnt));
14529
14530 if (rp->r_mapcnt == 0)
14531 return (1); /* always safe if not mapped */
14532
14533 /*
14534 * If the file is already mapped and there are locks, then they
14535 * should be all safe locks. So adding or removing a lock is safe
14536 * as long as the new request is safe (i.e., whole-file, meaning
14537 * length and starting offset are both zero).
14538 */
14539
14540 if (bfp->l_start != 0 || bfp->l_len != 0) {
14541 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14542 "cannot lock a memory mapped file unless locking the "
14543 "entire file: start %"PRIx64", len %"PRIx64,
14544 bfp->l_start, bfp->l_len));
14545 return (0);
14546 }
14547
14548 /* mandatory locking and mapping don't mix */
14549 va.va_mask = AT_MODE;
14550 error = VOP_GETATTR(vp, &va, 0, cr, NULL);
14551 if (error != 0) {
14552 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14553 "getattr error %d", error));
14554 return (0); /* treat errors conservatively */
14555 }
14556 if (MANDLOCK(vp, va.va_mode)) {
14557 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_safelock: "
14558 "cannot mandatory lock and mmap a file"));
14559 return (0);
14560 }
14561
14562 return (1);
14563 }
14564
14565
14566 /*
14567 * Register the lock locally within Solaris.
14568 * As the client, we "or" the sysid with LM_SYSID_CLIENT when
14569 * recording locks locally.
14570 *
14571 * This should handle conflicts/cooperation with NFS v2/v3 since all locks
14572 * are registered locally.
14573 */
14574 void
nfs4_register_lock_locally(vnode_t * vp,struct flock64 * flk,int flag,u_offset_t offset)14575 nfs4_register_lock_locally(vnode_t *vp, struct flock64 *flk, int flag,
14576 u_offset_t offset)
14577 {
14578 int oldsysid;
14579 int error;
14580 #ifdef DEBUG
14581 char *name;
14582 #endif
14583
14584 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14585
14586 #ifdef DEBUG
14587 name = fn_name(VTOSV(vp)->sv_name);
14588 NFS4_DEBUG(nfs4_client_lock_debug,
14589 (CE_NOTE, "nfs4_register_lock_locally: %s: type %d, "
14590 "start %"PRIx64", length %"PRIx64", pid %ld, sysid %d",
14591 name, flk->l_type, flk->l_start, flk->l_len, (long)flk->l_pid,
14592 flk->l_sysid));
14593 kmem_free(name, MAXNAMELEN);
14594 #endif
14595
14596 /* register the lock with local locking */
14597 oldsysid = flk->l_sysid;
14598 flk->l_sysid |= LM_SYSID_CLIENT;
14599 error = reclock(vp, flk, SETFLCK, flag, offset, NULL);
14600 #ifdef DEBUG
14601 if (error != 0) {
14602 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14603 "nfs4_register_lock_locally: could not register with"
14604 " local locking"));
14605 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14606 "error %d, vp 0x%p, pid %d, sysid 0x%x",
14607 error, (void *)vp, flk->l_pid, flk->l_sysid));
14608 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14609 "type %d off 0x%" PRIx64 " len 0x%" PRIx64,
14610 flk->l_type, flk->l_start, flk->l_len));
14611 (void) reclock(vp, flk, 0, flag, offset, NULL);
14612 NFS4_DEBUG(nfs4_client_lock_debug, (CE_CONT,
14613 "blocked by pid %d sysid 0x%x type %d "
14614 "off 0x%" PRIx64 " len 0x%" PRIx64,
14615 flk->l_pid, flk->l_sysid, flk->l_type, flk->l_start,
14616 flk->l_len));
14617 }
14618 #endif
14619 flk->l_sysid = oldsysid;
14620 }
14621
14622 /*
14623 * nfs4_lockrelease:
14624 *
14625 * Release any locks on the given vnode that are held by the current
14626 * process. Also removes the lock owner (if one exists) from the rnode's
14627 * list.
14628 */
14629 static int
nfs4_lockrelease(vnode_t * vp,int flag,offset_t offset,cred_t * cr)14630 nfs4_lockrelease(vnode_t *vp, int flag, offset_t offset, cred_t *cr)
14631 {
14632 flock64_t ld;
14633 int ret, error;
14634 rnode4_t *rp;
14635 nfs4_lock_owner_t *lop;
14636 nfs4_recov_state_t recov_state;
14637 mntinfo4_t *mi;
14638 bool_t possible_orphan = FALSE;
14639 bool_t recovonly;
14640
14641 ASSERT((uintptr_t)vp > KERNELBASE);
14642 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14643
14644 rp = VTOR4(vp);
14645 mi = VTOMI4(vp);
14646
14647 /*
14648 * If we have not locked anything then we can
14649 * just return since we have no work to do.
14650 */
14651 if (rp->r_lo_head.lo_next_rnode == &rp->r_lo_head) {
14652 return (0);
14653 }
14654
14655 /*
14656 * We need to comprehend that another thread may
14657 * kick off recovery and the lock_owner we have stashed
14658 * in lop might be invalid so we should NOT cache it
14659 * locally!
14660 */
14661 recov_state.rs_flags = 0;
14662 recov_state.rs_num_retry_despite_err = 0;
14663 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14664 &recovonly);
14665 if (error) {
14666 mutex_enter(&rp->r_statelock);
14667 rp->r_flags |= R4LODANGLERS;
14668 mutex_exit(&rp->r_statelock);
14669 return (error);
14670 }
14671
14672 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14673
14674 /*
14675 * Check if the lock owner might have a lock (request was sent but
14676 * no response was received). Also check if there are any remote
14677 * locks on the file. (In theory we shouldn't have to make this
14678 * second check if there's no lock owner, but for now we'll be
14679 * conservative and do it anyway.) If either condition is true,
14680 * send an unlock for the entire file to the server.
14681 *
14682 * Note that no explicit synchronization is needed here. At worst,
14683 * flk_has_remote_locks() will return a false positive, in which case
14684 * the unlock call wastes time but doesn't harm correctness.
14685 */
14686
14687 if (lop) {
14688 mutex_enter(&lop->lo_lock);
14689 possible_orphan = lop->lo_pending_rqsts;
14690 mutex_exit(&lop->lo_lock);
14691 lock_owner_rele(lop);
14692 }
14693
14694 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14695
14696 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14697 "nfs4_lockrelease: possible orphan %d, remote locks %d, for "
14698 "lop %p.", possible_orphan, flk_has_remote_locks(vp),
14699 (void *)lop));
14700
14701 if (possible_orphan || flk_has_remote_locks(vp)) {
14702 ld.l_type = F_UNLCK; /* set to unlock entire file */
14703 ld.l_whence = 0; /* unlock from start of file */
14704 ld.l_start = 0;
14705 ld.l_len = 0; /* do entire file */
14706
14707 ret = VOP_FRLOCK(vp, F_SETLK, &ld, flag, offset, NULL,
14708 cr, NULL);
14709
14710 if (ret != 0) {
14711 /*
14712 * If VOP_FRLOCK fails, make sure we unregister
14713 * local locks before we continue.
14714 */
14715 ld.l_pid = ttoproc(curthread)->p_pid;
14716 nfs4_register_lock_locally(vp, &ld, flag, offset);
14717 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
14718 "nfs4_lockrelease: lock release error on vp"
14719 " %p: error %d.\n", (void *)vp, ret));
14720 }
14721 }
14722
14723 recov_state.rs_flags = 0;
14724 recov_state.rs_num_retry_despite_err = 0;
14725 error = nfs4_start_fop(mi, vp, NULL, OH_LOCKU, &recov_state,
14726 &recovonly);
14727 if (error) {
14728 mutex_enter(&rp->r_statelock);
14729 rp->r_flags |= R4LODANGLERS;
14730 mutex_exit(&rp->r_statelock);
14731 return (error);
14732 }
14733
14734 /*
14735 * So, here we're going to need to retrieve the lock-owner
14736 * again (in case recovery has done a switch-a-roo) and
14737 * remove it because we can.
14738 */
14739 lop = find_lock_owner(rp, curproc->p_pid, LOWN_ANY);
14740
14741 if (lop) {
14742 nfs4_rnode_remove_lock_owner(rp, lop);
14743 lock_owner_rele(lop);
14744 }
14745
14746 nfs4_end_fop(mi, vp, NULL, OH_LOCKU, &recov_state, 0);
14747 return (0);
14748 }
14749
14750 /*
14751 * Wait for 'tick_delay' clock ticks.
14752 * Implement exponential backoff until hit the lease_time of this nfs4_server.
14753 * NOTE: lock_lease_time is in seconds.
14754 *
14755 * XXX For future improvements, should implement a waiting queue scheme.
14756 */
14757 static int
nfs4_block_and_wait(clock_t * tick_delay,rnode4_t * rp)14758 nfs4_block_and_wait(clock_t *tick_delay, rnode4_t *rp)
14759 {
14760 long milliseconds_delay;
14761 time_t lock_lease_time;
14762
14763 /* wait tick_delay clock ticks or siginteruptus */
14764 if (delay_sig(*tick_delay)) {
14765 return (EINTR);
14766 }
14767 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE, "nfs4_block_and_wait: "
14768 "reissue the lock request: blocked for %ld clock ticks: %ld "
14769 "milliseconds", *tick_delay, drv_hztousec(*tick_delay) / 1000));
14770
14771 /* get the lease time */
14772 lock_lease_time = r2lease_time(rp);
14773
14774 /* drv_hztousec converts ticks to microseconds */
14775 milliseconds_delay = drv_hztousec(*tick_delay) / 1000;
14776 if (milliseconds_delay < lock_lease_time * 1000) {
14777 *tick_delay = 2 * *tick_delay;
14778 if (drv_hztousec(*tick_delay) > lock_lease_time * 1000 * 1000)
14779 *tick_delay = drv_usectohz(lock_lease_time*1000*1000);
14780 }
14781 return (0);
14782 }
14783
14784
14785 void
nfs4_vnops_init(void)14786 nfs4_vnops_init(void)
14787 {
14788 }
14789
14790 void
nfs4_vnops_fini(void)14791 nfs4_vnops_fini(void)
14792 {
14793 }
14794
14795 /*
14796 * Return a reference to the directory (parent) vnode for a given vnode,
14797 * using the saved pathname information and the directory file handle. The
14798 * caller is responsible for disposing of the reference.
14799 * Returns zero or an errno value.
14800 *
14801 * Caller should set need_start_op to FALSE if it is the recovery
14802 * thread, or if a start_fop has already been done. Otherwise, TRUE.
14803 */
14804 int
vtodv(vnode_t * vp,vnode_t ** dvpp,cred_t * cr,bool_t need_start_op)14805 vtodv(vnode_t *vp, vnode_t **dvpp, cred_t *cr, bool_t need_start_op)
14806 {
14807 svnode_t *svnp;
14808 vnode_t *dvp = NULL;
14809 servinfo4_t *svp;
14810 nfs4_fname_t *mfname;
14811 int error;
14812
14813 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14814
14815 if (vp->v_flag & VROOT) {
14816 nfs4_sharedfh_t *sfh;
14817 nfs_fh4 fh;
14818 mntinfo4_t *mi;
14819
14820 ASSERT(vp->v_type == VREG);
14821
14822 mi = VTOMI4(vp);
14823 svp = mi->mi_curr_serv;
14824 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14825 fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
14826 fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
14827 sfh = sfh4_get(&fh, VTOMI4(vp));
14828 nfs_rw_exit(&svp->sv_lock);
14829 mfname = mi->mi_fname;
14830 fn_hold(mfname);
14831 dvp = makenfs4node_by_fh(sfh, NULL, &mfname, NULL, mi, cr, 0);
14832 sfh4_rele(&sfh);
14833
14834 if (dvp->v_type == VNON)
14835 dvp->v_type = VDIR;
14836 *dvpp = dvp;
14837 return (0);
14838 }
14839
14840 svnp = VTOSV(vp);
14841
14842 if (svnp == NULL) {
14843 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14844 "shadow node is NULL"));
14845 return (EINVAL);
14846 }
14847
14848 if (svnp->sv_name == NULL || svnp->sv_dfh == NULL) {
14849 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14850 "shadow node name or dfh val == NULL"));
14851 return (EINVAL);
14852 }
14853
14854 error = nfs4_make_dotdot(svnp->sv_dfh, 0, vp, cr, &dvp,
14855 (int)need_start_op);
14856 if (error != 0) {
14857 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14858 "nfs4_make_dotdot returned %d", error));
14859 return (error);
14860 }
14861 if (!dvp) {
14862 NFS4_DEBUG(nfs4_client_shadow_debug, (CE_NOTE, "vtodv: "
14863 "nfs4_make_dotdot returned a NULL dvp"));
14864 return (EIO);
14865 }
14866 if (dvp->v_type == VNON)
14867 dvp->v_type = VDIR;
14868 ASSERT(dvp->v_type == VDIR);
14869 if (VTOR4(vp)->r_flags & R4ISXATTR) {
14870 mutex_enter(&dvp->v_lock);
14871 dvp->v_flag |= V_XATTRDIR;
14872 mutex_exit(&dvp->v_lock);
14873 }
14874 *dvpp = dvp;
14875 return (0);
14876 }
14877
14878 /*
14879 * Copy the (final) component name of vp to fnamep. maxlen is the maximum
14880 * length that fnamep can accept, including the trailing null.
14881 * Returns 0 if okay, returns an errno value if there was a problem.
14882 */
14883
14884 int
vtoname(vnode_t * vp,char * fnamep,ssize_t maxlen)14885 vtoname(vnode_t *vp, char *fnamep, ssize_t maxlen)
14886 {
14887 char *fn;
14888 int err = 0;
14889 servinfo4_t *svp;
14890 svnode_t *shvp;
14891
14892 /*
14893 * If the file being opened has VROOT set, then this is
14894 * a "file" mount. sv_name will not be interesting, so
14895 * go back to the servinfo4 to get the original mount
14896 * path and strip off all but the final edge. Otherwise
14897 * just return the name from the shadow vnode.
14898 */
14899
14900 if (vp->v_flag & VROOT) {
14901
14902 svp = VTOMI4(vp)->mi_curr_serv;
14903 (void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
14904
14905 fn = strrchr(svp->sv_path, '/');
14906 if (fn == NULL)
14907 err = EINVAL;
14908 else
14909 fn++;
14910 } else {
14911 shvp = VTOSV(vp);
14912 fn = fn_name(shvp->sv_name);
14913 }
14914
14915 if (err == 0)
14916 if (strlen(fn) < maxlen)
14917 (void) strcpy(fnamep, fn);
14918 else
14919 err = ENAMETOOLONG;
14920
14921 if (vp->v_flag & VROOT)
14922 nfs_rw_exit(&svp->sv_lock);
14923 else
14924 kmem_free(fn, MAXNAMELEN);
14925
14926 return (err);
14927 }
14928
14929 /*
14930 * Bookkeeping for a close that doesn't need to go over the wire.
14931 * *have_lockp is set to 0 if 'os_sync_lock' is released; otherwise
14932 * it is left at 1.
14933 */
14934 void
nfs4close_notw(vnode_t * vp,nfs4_open_stream_t * osp,int * have_lockp)14935 nfs4close_notw(vnode_t *vp, nfs4_open_stream_t *osp, int *have_lockp)
14936 {
14937 rnode4_t *rp;
14938 mntinfo4_t *mi;
14939
14940 mi = VTOMI4(vp);
14941 rp = VTOR4(vp);
14942
14943 NFS4_DEBUG(nfs4close_notw_debug, (CE_NOTE, "nfs4close_notw: "
14944 "rp=%p osp=%p", (void *)rp, (void *)osp));
14945 ASSERT(nfs_zone() == mi->mi_zone);
14946 ASSERT(mutex_owned(&osp->os_sync_lock));
14947 ASSERT(*have_lockp);
14948
14949 if (!osp->os_valid ||
14950 osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
14951 return;
14952 }
14953
14954 /*
14955 * This removes the reference obtained at OPEN; ie,
14956 * when the open stream structure was created.
14957 *
14958 * We don't have to worry about calling 'open_stream_rele'
14959 * since we our currently holding a reference to this
14960 * open stream which means the count can not go to 0 with
14961 * this decrement.
14962 */
14963 ASSERT(osp->os_ref_count >= 2);
14964 osp->os_ref_count--;
14965 osp->os_valid = 0;
14966 mutex_exit(&osp->os_sync_lock);
14967 *have_lockp = 0;
14968
14969 nfs4_dec_state_ref_count(mi);
14970 }
14971
14972 /*
14973 * Close all remaining open streams on the rnode. These open streams
14974 * could be here because:
14975 * - The close attempted at either close or delmap failed
14976 * - Some kernel entity did VOP_OPEN but never did VOP_CLOSE
14977 * - Someone did mknod on a regular file but never opened it
14978 */
14979 int
nfs4close_all(vnode_t * vp,cred_t * cr)14980 nfs4close_all(vnode_t *vp, cred_t *cr)
14981 {
14982 nfs4_open_stream_t *osp;
14983 int error;
14984 nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
14985 rnode4_t *rp;
14986
14987 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
14988
14989 error = 0;
14990 rp = VTOR4(vp);
14991
14992 /*
14993 * At this point, all we know is that the last time
14994 * someone called vn_rele, the count was 1. Since then,
14995 * the vnode could have been re-activated. We want to
14996 * loop through the open streams and close each one, but
14997 * we have to be careful since once we release the rnode
14998 * hash bucket lock, someone else is free to come in and
14999 * re-activate the rnode and add new open streams. The
15000 * strategy is take the rnode hash bucket lock, verify that
15001 * the count is still 1, grab the open stream off the
15002 * head of the list and mark it invalid, then release the
15003 * rnode hash bucket lock and proceed with that open stream.
15004 * This is ok because nfs4close_one() will acquire the proper
15005 * open/create to close/destroy synchronization for open
15006 * streams, and will ensure that if someone has reopened
15007 * the open stream after we've dropped the hash bucket lock
15008 * then we'll just simply return without destroying the
15009 * open stream.
15010 * Repeat until the list is empty.
15011 */
15012
15013 for (;;) {
15014
15015 /* make sure vnode hasn't been reactivated */
15016 rw_enter(&rp->r_hashq->r_lock, RW_READER);
15017 mutex_enter(&vp->v_lock);
15018 if (vp->v_count > 1) {
15019 mutex_exit(&vp->v_lock);
15020 rw_exit(&rp->r_hashq->r_lock);
15021 break;
15022 }
15023 /*
15024 * Grabbing r_os_lock before releasing v_lock prevents
15025 * a window where the rnode/open stream could get
15026 * reactivated (and os_force_close set to 0) before we
15027 * had a chance to set os_force_close to 1.
15028 */
15029 mutex_enter(&rp->r_os_lock);
15030 mutex_exit(&vp->v_lock);
15031
15032 osp = list_head(&rp->r_open_streams);
15033 if (!osp) {
15034 /* nothing left to CLOSE OTW, so return */
15035 mutex_exit(&rp->r_os_lock);
15036 rw_exit(&rp->r_hashq->r_lock);
15037 break;
15038 }
15039
15040 mutex_enter(&rp->r_statev4_lock);
15041 /* the file can't still be mem mapped */
15042 ASSERT(rp->r_mapcnt == 0);
15043 if (rp->created_v4)
15044 rp->created_v4 = 0;
15045 mutex_exit(&rp->r_statev4_lock);
15046
15047 /*
15048 * Grab a ref on this open stream; nfs4close_one
15049 * will mark it as invalid
15050 */
15051 mutex_enter(&osp->os_sync_lock);
15052 osp->os_ref_count++;
15053 osp->os_force_close = 1;
15054 mutex_exit(&osp->os_sync_lock);
15055 mutex_exit(&rp->r_os_lock);
15056 rw_exit(&rp->r_hashq->r_lock);
15057
15058 nfs4close_one(vp, osp, cr, 0, NULL, &e, CLOSE_FORCE, 0, 0, 0);
15059
15060 /* Update error if it isn't already non-zero */
15061 if (error == 0) {
15062 if (e.error)
15063 error = e.error;
15064 else if (e.stat)
15065 error = geterrno4(e.stat);
15066 }
15067
15068 #ifdef DEBUG
15069 nfs4close_all_cnt++;
15070 #endif
15071 /* Release the ref on osp acquired above. */
15072 open_stream_rele(osp, rp);
15073
15074 /* Proceed to the next open stream, if any */
15075 }
15076 return (error);
15077 }
15078
15079 /*
15080 * nfs4close_one - close one open stream for a file if needed.
15081 *
15082 * "close_type" indicates which close path this is:
15083 * CLOSE_NORM: close initiated via VOP_CLOSE.
15084 * CLOSE_DELMAP: close initiated via VOP_DELMAP.
15085 * CLOSE_FORCE: close initiated via VOP_INACTIVE. This path forces
15086 * the close and release of client state for this open stream
15087 * (unless someone else has the open stream open).
15088 * CLOSE_RESEND: indicates the request is a replay of an earlier request
15089 * (e.g., due to abort because of a signal).
15090 * CLOSE_AFTER_RESEND: close initiated to "undo" a successful resent OPEN.
15091 *
15092 * CLOSE_RESEND and CLOSE_AFTER_RESEND will not attempt to retry after client
15093 * recovery. Instead, the caller is expected to deal with retries.
15094 *
15095 * The caller can either pass in the osp ('provided_osp') or not.
15096 *
15097 * 'access_bits' represents the access we are closing/downgrading.
15098 *
15099 * 'len', 'prot', and 'mmap_flags' are used for CLOSE_DELMAP. 'len' is the
15100 * number of bytes we are unmapping, 'maxprot' is the mmap protection, and
15101 * 'mmap_flags' tells us the type of sharing (MAP_PRIVATE or MAP_SHARED).
15102 *
15103 * Errors are returned via the nfs4_error_t.
15104 */
15105 void
nfs4close_one(vnode_t * vp,nfs4_open_stream_t * provided_osp,cred_t * cr,int access_bits,nfs4_lost_rqst_t * lrp,nfs4_error_t * ep,nfs4_close_type_t close_type,size_t len,uint_t maxprot,uint_t mmap_flags)15106 nfs4close_one(vnode_t *vp, nfs4_open_stream_t *provided_osp, cred_t *cr,
15107 int access_bits, nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
15108 nfs4_close_type_t close_type, size_t len, uint_t maxprot,
15109 uint_t mmap_flags)
15110 {
15111 nfs4_open_owner_t *oop;
15112 nfs4_open_stream_t *osp = NULL;
15113 int retry = 0;
15114 int num_retries = NFS4_NUM_RECOV_RETRIES;
15115 rnode4_t *rp;
15116 mntinfo4_t *mi;
15117 nfs4_recov_state_t recov_state;
15118 cred_t *cred_otw = NULL;
15119 bool_t recovonly = FALSE;
15120 int isrecov;
15121 int force_close;
15122 int close_failed = 0;
15123 int did_dec_count = 0;
15124 int did_start_op = 0;
15125 int did_force_recovlock = 0;
15126 int did_start_seqid_sync = 0;
15127 int have_sync_lock = 0;
15128
15129 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15130
15131 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE, "closing vp %p osp %p, "
15132 "lrp %p, close type %d len %ld prot %x mmap flags %x bits %x",
15133 (void *)vp, (void *)provided_osp, (void *)lrp, close_type,
15134 len, maxprot, mmap_flags, access_bits));
15135
15136 nfs4_error_zinit(ep);
15137 rp = VTOR4(vp);
15138 mi = VTOMI4(vp);
15139 isrecov = (close_type == CLOSE_RESEND ||
15140 close_type == CLOSE_AFTER_RESEND);
15141
15142 /*
15143 * First get the open owner.
15144 */
15145 if (!provided_osp) {
15146 oop = find_open_owner(cr, NFS4_PERM_CREATED, mi);
15147 } else {
15148 oop = provided_osp->os_open_owner;
15149 ASSERT(oop != NULL);
15150 open_owner_hold(oop);
15151 }
15152
15153 if (!oop) {
15154 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15155 "nfs4close_one: no oop, rp %p, mi %p, cr %p, osp %p, "
15156 "close type %d", (void *)rp, (void *)mi, (void *)cr,
15157 (void *)provided_osp, close_type));
15158 ep->error = EIO;
15159 goto out;
15160 }
15161
15162 cred_otw = nfs4_get_otw_cred(cr, mi, oop);
15163 recov_retry:
15164 osp = NULL;
15165 close_failed = 0;
15166 force_close = (close_type == CLOSE_FORCE);
15167 retry = 0;
15168 did_start_op = 0;
15169 did_force_recovlock = 0;
15170 did_start_seqid_sync = 0;
15171 have_sync_lock = 0;
15172 recovonly = FALSE;
15173 recov_state.rs_flags = 0;
15174 recov_state.rs_num_retry_despite_err = 0;
15175
15176 /*
15177 * Second synchronize with recovery.
15178 */
15179 if (!isrecov) {
15180 ep->error = nfs4_start_fop(mi, vp, NULL, OH_CLOSE,
15181 &recov_state, &recovonly);
15182 if (!ep->error) {
15183 did_start_op = 1;
15184 } else {
15185 close_failed = 1;
15186 /*
15187 * If we couldn't get start_fop, but have to
15188 * cleanup state, then at least acquire the
15189 * mi_recovlock so we can synchronize with
15190 * recovery.
15191 */
15192 if (close_type == CLOSE_FORCE) {
15193 (void) nfs_rw_enter_sig(&mi->mi_recovlock,
15194 RW_READER, FALSE);
15195 did_force_recovlock = 1;
15196 } else
15197 goto out;
15198 }
15199 }
15200
15201 /*
15202 * We cannot attempt to get the open seqid sync if nfs4_start_fop
15203 * set 'recovonly' to TRUE since most likely this is due to
15204 * reovery being active (MI4_RECOV_ACTIV). If recovery is active,
15205 * nfs4_start_open_seqid_sync() will fail with EAGAIN asking us
15206 * to retry, causing us to loop until recovery finishes. Plus we
15207 * don't need protection over the open seqid since we're not going
15208 * OTW, hence don't need to use the seqid.
15209 */
15210 if (recovonly == FALSE) {
15211 /* need to grab the open owner sync before 'os_sync_lock' */
15212 ep->error = nfs4_start_open_seqid_sync(oop, mi);
15213 if (ep->error == EAGAIN) {
15214 ASSERT(!isrecov);
15215 if (did_start_op)
15216 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15217 &recov_state, TRUE);
15218 if (did_force_recovlock)
15219 nfs_rw_exit(&mi->mi_recovlock);
15220 goto recov_retry;
15221 }
15222 did_start_seqid_sync = 1;
15223 }
15224
15225 /*
15226 * Third get an open stream and acquire 'os_sync_lock' to
15227 * sychronize the opening/creating of an open stream with the
15228 * closing/destroying of an open stream.
15229 */
15230 if (!provided_osp) {
15231 /* returns with 'os_sync_lock' held */
15232 osp = find_open_stream(oop, rp);
15233 if (!osp) {
15234 ep->error = EIO;
15235 goto out;
15236 }
15237 } else {
15238 osp = provided_osp;
15239 open_stream_hold(osp);
15240 mutex_enter(&osp->os_sync_lock);
15241 }
15242 have_sync_lock = 1;
15243
15244 ASSERT(oop == osp->os_open_owner);
15245
15246 /*
15247 * Fourth, do any special pre-OTW CLOSE processing
15248 * based on the specific close type.
15249 */
15250 if ((close_type == CLOSE_NORM || close_type == CLOSE_AFTER_RESEND) &&
15251 !did_dec_count) {
15252 ASSERT(osp->os_open_ref_count > 0);
15253 osp->os_open_ref_count--;
15254 did_dec_count = 1;
15255 if (osp->os_open_ref_count == 0)
15256 osp->os_final_close = 1;
15257 }
15258
15259 if (close_type == CLOSE_FORCE) {
15260 /* see if somebody reopened the open stream. */
15261 if (!osp->os_force_close) {
15262 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15263 "nfs4close_one: skip CLOSE_FORCE as osp %p "
15264 "was reopened, vp %p", (void *)osp, (void *)vp));
15265 ep->error = 0;
15266 ep->stat = NFS4_OK;
15267 goto out;
15268 }
15269
15270 if (!osp->os_final_close && !did_dec_count) {
15271 osp->os_open_ref_count--;
15272 did_dec_count = 1;
15273 }
15274
15275 /*
15276 * We can't depend on os_open_ref_count being 0 due to the
15277 * way executables are opened (VN_RELE to match a VOP_OPEN).
15278 */
15279 #ifdef NOTYET
15280 ASSERT(osp->os_open_ref_count == 0);
15281 #endif
15282 if (osp->os_open_ref_count != 0) {
15283 NFS4_DEBUG(nfs4close_one_debug, (CE_NOTE,
15284 "nfs4close_one: should panic here on an "
15285 "ASSERT(osp->os_open_ref_count == 0). Ignoring "
15286 "since this is probably the exec problem."));
15287
15288 osp->os_open_ref_count = 0;
15289 }
15290
15291 /*
15292 * There is the possibility that nfs4close_one()
15293 * for close_type == CLOSE_DELMAP couldn't find the
15294 * open stream, thus couldn't decrement its os_mapcnt;
15295 * therefore we can't use this ASSERT yet.
15296 */
15297 #ifdef NOTYET
15298 ASSERT(osp->os_mapcnt == 0);
15299 #endif
15300 osp->os_mapcnt = 0;
15301 }
15302
15303 if (close_type == CLOSE_DELMAP && !did_dec_count) {
15304 ASSERT(osp->os_mapcnt >= btopr(len));
15305
15306 if ((mmap_flags & MAP_SHARED) && (maxprot & PROT_WRITE))
15307 osp->os_mmap_write -= btopr(len);
15308 if (maxprot & PROT_READ)
15309 osp->os_mmap_read -= btopr(len);
15310 if (maxprot & PROT_EXEC)
15311 osp->os_mmap_read -= btopr(len);
15312 /* mirror the PROT_NONE check in nfs4_addmap() */
15313 if (!(maxprot & PROT_READ) && !(maxprot & PROT_WRITE) &&
15314 !(maxprot & PROT_EXEC))
15315 osp->os_mmap_read -= btopr(len);
15316 osp->os_mapcnt -= btopr(len);
15317 did_dec_count = 1;
15318 }
15319
15320 if (recovonly) {
15321 nfs4_lost_rqst_t lost_rqst;
15322
15323 /* request should not already be in recovery queue */
15324 ASSERT(lrp == NULL);
15325 nfs4_error_init(ep, EINTR);
15326 nfs4close_save_lost_rqst(ep->error, &lost_rqst, oop,
15327 osp, cred_otw, vp);
15328 mutex_exit(&osp->os_sync_lock);
15329 have_sync_lock = 0;
15330 (void) nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15331 lost_rqst.lr_op == OP_CLOSE ?
15332 &lost_rqst : NULL, OP_CLOSE, NULL, NULL, NULL);
15333 close_failed = 1;
15334 force_close = 0;
15335 goto close_cleanup;
15336 }
15337
15338 /*
15339 * If a previous OTW call got NFS4ERR_BAD_SEQID, then
15340 * we stopped operating on the open owner's <old oo_name, old seqid>
15341 * space, which means we stopped operating on the open stream
15342 * too. So don't go OTW (as the seqid is likely bad, and the
15343 * stateid could be stale, potentially triggering a false
15344 * setclientid), and just clean up the client's internal state.
15345 */
15346 if (osp->os_orig_oo_name != oop->oo_name) {
15347 NFS4_DEBUG(nfs4close_one_debug || nfs4_client_recov_debug,
15348 (CE_NOTE, "nfs4close_one: skip OTW close for osp %p "
15349 "oop %p due to bad seqid (orig oo_name %" PRIx64 " current "
15350 "oo_name %" PRIx64")",
15351 (void *)osp, (void *)oop, osp->os_orig_oo_name,
15352 oop->oo_name));
15353 close_failed = 1;
15354 }
15355
15356 /* If the file failed recovery, just quit. */
15357 mutex_enter(&rp->r_statelock);
15358 if (rp->r_flags & R4RECOVERR) {
15359 close_failed = 1;
15360 }
15361 mutex_exit(&rp->r_statelock);
15362
15363 /*
15364 * If the force close path failed to obtain start_fop
15365 * then skip the OTW close and just remove the state.
15366 */
15367 if (close_failed)
15368 goto close_cleanup;
15369
15370 /*
15371 * Fifth, check to see if there are still mapped pages or other
15372 * opens using this open stream. If there are then we can't
15373 * close yet but we can see if an OPEN_DOWNGRADE is necessary.
15374 */
15375 if (osp->os_open_ref_count > 0 || osp->os_mapcnt > 0) {
15376 nfs4_lost_rqst_t new_lost_rqst;
15377 bool_t needrecov = FALSE;
15378 cred_t *odg_cred_otw = NULL;
15379 seqid4 open_dg_seqid = 0;
15380
15381 if (osp->os_delegation) {
15382 /*
15383 * If this open stream was never OPENed OTW then we
15384 * surely can't DOWNGRADE it (especially since the
15385 * osp->open_stateid is really a delegation stateid
15386 * when os_delegation is 1).
15387 */
15388 if (access_bits & FREAD)
15389 osp->os_share_acc_read--;
15390 if (access_bits & FWRITE)
15391 osp->os_share_acc_write--;
15392 osp->os_share_deny_none--;
15393 nfs4_error_zinit(ep);
15394 goto out;
15395 }
15396 nfs4_open_downgrade(access_bits, 0, oop, osp, vp, cr,
15397 lrp, ep, &odg_cred_otw, &open_dg_seqid);
15398 needrecov = nfs4_needs_recovery(ep, TRUE, mi->mi_vfsp);
15399 if (needrecov && !isrecov) {
15400 bool_t abort;
15401 nfs4_bseqid_entry_t *bsep = NULL;
15402
15403 if (!ep->error && ep->stat == NFS4ERR_BAD_SEQID)
15404 bsep = nfs4_create_bseqid_entry(oop, NULL,
15405 vp, 0,
15406 lrp ? TAG_OPEN_DG_LOST : TAG_OPEN_DG,
15407 open_dg_seqid);
15408
15409 nfs4open_dg_save_lost_rqst(ep->error, &new_lost_rqst,
15410 oop, osp, odg_cred_otw, vp, access_bits, 0);
15411 mutex_exit(&osp->os_sync_lock);
15412 have_sync_lock = 0;
15413 abort = nfs4_start_recovery(ep, mi, vp, NULL, NULL,
15414 new_lost_rqst.lr_op == OP_OPEN_DOWNGRADE ?
15415 &new_lost_rqst : NULL, OP_OPEN_DOWNGRADE,
15416 bsep, NULL, NULL);
15417 if (odg_cred_otw)
15418 crfree(odg_cred_otw);
15419 if (bsep)
15420 kmem_free(bsep, sizeof (*bsep));
15421
15422 if (abort == TRUE)
15423 goto out;
15424
15425 if (did_start_seqid_sync) {
15426 nfs4_end_open_seqid_sync(oop);
15427 did_start_seqid_sync = 0;
15428 }
15429 open_stream_rele(osp, rp);
15430
15431 if (did_start_op)
15432 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15433 &recov_state, FALSE);
15434 if (did_force_recovlock)
15435 nfs_rw_exit(&mi->mi_recovlock);
15436
15437 goto recov_retry;
15438 } else {
15439 if (odg_cred_otw)
15440 crfree(odg_cred_otw);
15441 }
15442 goto out;
15443 }
15444
15445 /*
15446 * If this open stream was created as the results of an open
15447 * while holding a delegation, then just release it; no need
15448 * to do an OTW close. Otherwise do a "normal" OTW close.
15449 */
15450 if (osp->os_delegation) {
15451 nfs4close_notw(vp, osp, &have_sync_lock);
15452 nfs4_error_zinit(ep);
15453 goto out;
15454 }
15455
15456 /*
15457 * If this stream is not valid, we're done.
15458 */
15459 if (!osp->os_valid) {
15460 nfs4_error_zinit(ep);
15461 goto out;
15462 }
15463
15464 /*
15465 * Last open or mmap ref has vanished, need to do an OTW close.
15466 * First check to see if a close is still necessary.
15467 */
15468 if (osp->os_failed_reopen) {
15469 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15470 "don't close OTW osp %p since reopen failed.",
15471 (void *)osp));
15472 /*
15473 * Reopen of the open stream failed, hence the
15474 * stateid of the open stream is invalid/stale, and
15475 * sending this OTW would incorrectly cause another
15476 * round of recovery. In this case, we need to set
15477 * the 'os_valid' bit to 0 so another thread doesn't
15478 * come in and re-open this open stream before
15479 * this "closing" thread cleans up state (decrementing
15480 * the nfs4_server_t's state_ref_count and decrementing
15481 * the os_ref_count).
15482 */
15483 osp->os_valid = 0;
15484 /*
15485 * This removes the reference obtained at OPEN; ie,
15486 * when the open stream structure was created.
15487 *
15488 * We don't have to worry about calling 'open_stream_rele'
15489 * since we our currently holding a reference to this
15490 * open stream which means the count can not go to 0 with
15491 * this decrement.
15492 */
15493 ASSERT(osp->os_ref_count >= 2);
15494 osp->os_ref_count--;
15495 nfs4_error_zinit(ep);
15496 close_failed = 0;
15497 goto close_cleanup;
15498 }
15499
15500 ASSERT(osp->os_ref_count > 1);
15501
15502 /*
15503 * Sixth, try the CLOSE OTW.
15504 */
15505 nfs4close_otw(rp, cred_otw, oop, osp, &retry, &did_start_seqid_sync,
15506 close_type, ep, &have_sync_lock);
15507
15508 if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp)) {
15509 /*
15510 * Let the recovery thread be responsible for
15511 * removing the state for CLOSE.
15512 */
15513 close_failed = 1;
15514 force_close = 0;
15515 retry = 0;
15516 }
15517
15518 /* See if we need to retry with a different cred */
15519 if ((ep->error == EACCES ||
15520 (ep->error == 0 && ep->stat == NFS4ERR_ACCESS)) &&
15521 cred_otw != cr) {
15522 crfree(cred_otw);
15523 cred_otw = cr;
15524 crhold(cred_otw);
15525 retry = 1;
15526 }
15527
15528 if (ep->error || ep->stat)
15529 close_failed = 1;
15530
15531 if (retry && !isrecov && num_retries-- > 0) {
15532 if (have_sync_lock) {
15533 mutex_exit(&osp->os_sync_lock);
15534 have_sync_lock = 0;
15535 }
15536 if (did_start_seqid_sync) {
15537 nfs4_end_open_seqid_sync(oop);
15538 did_start_seqid_sync = 0;
15539 }
15540 open_stream_rele(osp, rp);
15541
15542 if (did_start_op)
15543 nfs4_end_fop(mi, vp, NULL, OH_CLOSE,
15544 &recov_state, FALSE);
15545 if (did_force_recovlock)
15546 nfs_rw_exit(&mi->mi_recovlock);
15547 NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
15548 "nfs4close_one: need to retry the close "
15549 "operation"));
15550 goto recov_retry;
15551 }
15552 close_cleanup:
15553 /*
15554 * Seventh and lastly, process our results.
15555 */
15556 if (close_failed && force_close) {
15557 /*
15558 * It's ok to drop and regrab the 'os_sync_lock' since
15559 * nfs4close_notw() will recheck to make sure the
15560 * "close"/removal of state should happen.
15561 */
15562 if (!have_sync_lock) {
15563 mutex_enter(&osp->os_sync_lock);
15564 have_sync_lock = 1;
15565 }
15566 /*
15567 * This is last call, remove the ref on the open
15568 * stream created by open and clean everything up.
15569 */
15570 osp->os_pending_close = 0;
15571 nfs4close_notw(vp, osp, &have_sync_lock);
15572 nfs4_error_zinit(ep);
15573 }
15574
15575 if (!close_failed) {
15576 if (have_sync_lock) {
15577 osp->os_pending_close = 0;
15578 mutex_exit(&osp->os_sync_lock);
15579 have_sync_lock = 0;
15580 } else {
15581 mutex_enter(&osp->os_sync_lock);
15582 osp->os_pending_close = 0;
15583 mutex_exit(&osp->os_sync_lock);
15584 }
15585 if (did_start_op && recov_state.rs_sp != NULL) {
15586 mutex_enter(&recov_state.rs_sp->s_lock);
15587 nfs4_dec_state_ref_count_nolock(recov_state.rs_sp, mi);
15588 mutex_exit(&recov_state.rs_sp->s_lock);
15589 } else {
15590 nfs4_dec_state_ref_count(mi);
15591 }
15592 nfs4_error_zinit(ep);
15593 }
15594
15595 out:
15596 if (have_sync_lock)
15597 mutex_exit(&osp->os_sync_lock);
15598 if (did_start_op)
15599 nfs4_end_fop(mi, vp, NULL, OH_CLOSE, &recov_state,
15600 recovonly ? TRUE : FALSE);
15601 if (did_force_recovlock)
15602 nfs_rw_exit(&mi->mi_recovlock);
15603 if (cred_otw)
15604 crfree(cred_otw);
15605 if (osp)
15606 open_stream_rele(osp, rp);
15607 if (oop) {
15608 if (did_start_seqid_sync)
15609 nfs4_end_open_seqid_sync(oop);
15610 open_owner_rele(oop);
15611 }
15612 }
15613
15614 /*
15615 * Convert information returned by the server in the LOCK4denied
15616 * structure to the form required by fcntl.
15617 */
15618 static void
denied_to_flk(LOCK4denied * lockt_denied,flock64_t * flk,LOCKT4args * lockt_args)15619 denied_to_flk(LOCK4denied *lockt_denied, flock64_t *flk, LOCKT4args *lockt_args)
15620 {
15621 nfs4_lo_name_t *lo;
15622
15623 #ifdef DEBUG
15624 if (denied_to_flk_debug) {
15625 lockt_denied_debug = lockt_denied;
15626 debug_enter("lockt_denied");
15627 }
15628 #endif
15629
15630 flk->l_type = lockt_denied->locktype == READ_LT ? F_RDLCK : F_WRLCK;
15631 flk->l_whence = 0; /* aka SEEK_SET */
15632 flk->l_start = lockt_denied->offset;
15633 flk->l_len = lockt_denied->length;
15634
15635 /*
15636 * If the blocking clientid matches our client id, then we can
15637 * interpret the lockowner (since we built it). If not, then
15638 * fabricate a sysid and pid. Note that the l_sysid field
15639 * in *flk already has the local sysid.
15640 */
15641
15642 if (lockt_denied->owner.clientid == lockt_args->owner.clientid) {
15643
15644 if (lockt_denied->owner.owner_len == sizeof (*lo)) {
15645 lo = (nfs4_lo_name_t *)
15646 lockt_denied->owner.owner_val;
15647
15648 flk->l_pid = lo->ln_pid;
15649 } else {
15650 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15651 "denied_to_flk: bad lock owner length\n"));
15652
15653 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15654 }
15655 } else {
15656 NFS4_DEBUG(nfs4_client_lock_debug, (CE_NOTE,
15657 "denied_to_flk: foreign clientid\n"));
15658
15659 /*
15660 * Construct a new sysid which should be different from
15661 * sysids of other systems.
15662 */
15663
15664 flk->l_sysid++;
15665 flk->l_pid = lo_to_pid(&lockt_denied->owner);
15666 }
15667 }
15668
15669 static pid_t
lo_to_pid(lock_owner4 * lop)15670 lo_to_pid(lock_owner4 *lop)
15671 {
15672 pid_t pid = 0;
15673 uchar_t *cp;
15674 int i;
15675
15676 cp = (uchar_t *)&lop->clientid;
15677
15678 for (i = 0; i < sizeof (lop->clientid); i++)
15679 pid += (pid_t)*cp++;
15680
15681 cp = (uchar_t *)lop->owner_val;
15682
15683 for (i = 0; i < lop->owner_len; i++)
15684 pid += (pid_t)*cp++;
15685
15686 return (pid);
15687 }
15688
15689 /*
15690 * Given a lock pointer, returns the length of that lock.
15691 * "end" is the last locked offset the "l_len" covers from
15692 * the start of the lock.
15693 */
15694 static off64_t
lock_to_end(flock64_t * lock)15695 lock_to_end(flock64_t *lock)
15696 {
15697 off64_t lock_end;
15698
15699 if (lock->l_len == 0)
15700 lock_end = (off64_t)MAXEND;
15701 else
15702 lock_end = lock->l_start + lock->l_len - 1;
15703
15704 return (lock_end);
15705 }
15706
15707 /*
15708 * Given the end of a lock, it will return you the length "l_len" for that lock.
15709 */
15710 static off64_t
end_to_len(off64_t start,off64_t end)15711 end_to_len(off64_t start, off64_t end)
15712 {
15713 off64_t lock_len;
15714
15715 ASSERT(end >= start);
15716 if (end == MAXEND)
15717 lock_len = 0;
15718 else
15719 lock_len = end - start + 1;
15720
15721 return (lock_len);
15722 }
15723
15724 /*
15725 * On given end for a lock it determines if it is the last locked offset
15726 * or not, if so keeps it as is, else adds one to return the length for
15727 * valid start.
15728 */
15729 static off64_t
start_check(off64_t x)15730 start_check(off64_t x)
15731 {
15732 if (x == MAXEND)
15733 return (x);
15734 else
15735 return (x + 1);
15736 }
15737
15738 /*
15739 * See if these two locks overlap, and if so return 1;
15740 * otherwise, return 0.
15741 */
15742 static int
locks_intersect(flock64_t * llfp,flock64_t * curfp)15743 locks_intersect(flock64_t *llfp, flock64_t *curfp)
15744 {
15745 off64_t llfp_end, curfp_end;
15746
15747 llfp_end = lock_to_end(llfp);
15748 curfp_end = lock_to_end(curfp);
15749
15750 if (((llfp_end >= curfp->l_start) &&
15751 (llfp->l_start <= curfp->l_start)) ||
15752 ((curfp->l_start <= llfp->l_start) && (curfp_end >= llfp->l_start)))
15753 return (1);
15754 return (0);
15755 }
15756
15757 /*
15758 * Determine what the intersecting lock region is, and add that to the
15759 * 'nl_llpp' locklist in increasing order (by l_start).
15760 */
15761 static void
nfs4_add_lock_range(flock64_t * lost_flp,flock64_t * local_flp,locklist_t ** nl_llpp,vnode_t * vp)15762 nfs4_add_lock_range(flock64_t *lost_flp, flock64_t *local_flp,
15763 locklist_t **nl_llpp, vnode_t *vp)
15764 {
15765 locklist_t *intersect_llp, *tmp_fllp, *cur_fllp;
15766 off64_t lost_flp_end, local_flp_end, len, start;
15767
15768 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range:"));
15769
15770 if (!locks_intersect(lost_flp, local_flp))
15771 return;
15772
15773 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15774 "locks intersect"));
15775
15776 lost_flp_end = lock_to_end(lost_flp);
15777 local_flp_end = lock_to_end(local_flp);
15778
15779 /* Find the starting point of the intersecting region */
15780 if (local_flp->l_start > lost_flp->l_start)
15781 start = local_flp->l_start;
15782 else
15783 start = lost_flp->l_start;
15784
15785 /* Find the lenght of the intersecting region */
15786 if (lost_flp_end < local_flp_end)
15787 len = end_to_len(start, lost_flp_end);
15788 else
15789 len = end_to_len(start, local_flp_end);
15790
15791 /*
15792 * Prepare the flock structure for the intersection found and insert
15793 * it into the new list in increasing l_start order. This list contains
15794 * intersections of locks registered by the client with the local host
15795 * and the lost lock.
15796 * The lock type of this lock is the same as that of the local_flp.
15797 */
15798 intersect_llp = (locklist_t *)kmem_alloc(sizeof (locklist_t), KM_SLEEP);
15799 intersect_llp->ll_flock.l_start = start;
15800 intersect_llp->ll_flock.l_len = len;
15801 intersect_llp->ll_flock.l_type = local_flp->l_type;
15802 intersect_llp->ll_flock.l_pid = local_flp->l_pid;
15803 intersect_llp->ll_flock.l_sysid = local_flp->l_sysid;
15804 intersect_llp->ll_flock.l_whence = 0; /* aka SEEK_SET */
15805 intersect_llp->ll_vp = vp;
15806
15807 tmp_fllp = *nl_llpp;
15808 cur_fllp = NULL;
15809 while (tmp_fllp != NULL && tmp_fllp->ll_flock.l_start <
15810 intersect_llp->ll_flock.l_start) {
15811 cur_fllp = tmp_fllp;
15812 tmp_fllp = tmp_fllp->ll_next;
15813 }
15814 if (cur_fllp == NULL) {
15815 /* first on the list */
15816 intersect_llp->ll_next = *nl_llpp;
15817 *nl_llpp = intersect_llp;
15818 } else {
15819 intersect_llp->ll_next = cur_fllp->ll_next;
15820 cur_fllp->ll_next = intersect_llp;
15821 }
15822
15823 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_add_lock_range: "
15824 "created lock region: start %"PRIx64" end %"PRIx64" : %s\n",
15825 intersect_llp->ll_flock.l_start,
15826 intersect_llp->ll_flock.l_start + intersect_llp->ll_flock.l_len,
15827 intersect_llp->ll_flock.l_type == F_RDLCK ? "READ" : "WRITE"));
15828 }
15829
15830 /*
15831 * Our local locking current state is potentially different than
15832 * what the NFSv4 server thinks we have due to a lost lock that was
15833 * resent and then received. We need to reset our "NFSv4" locking
15834 * state to match the current local locking state for this pid since
15835 * that is what the user/application sees as what the world is.
15836 *
15837 * We cannot afford to drop the open/lock seqid sync since then we can
15838 * get confused about what the current local locking state "is" versus
15839 * "was".
15840 *
15841 * If we are unable to fix up the locks, we send SIGLOST to the affected
15842 * process. This is not done if the filesystem has been forcibly
15843 * unmounted, in case the process has already exited and a new process
15844 * exists with the same pid.
15845 */
15846 static void
nfs4_reinstitute_local_lock_state(vnode_t * vp,flock64_t * lost_flp,cred_t * cr,nfs4_lock_owner_t * lop)15847 nfs4_reinstitute_local_lock_state(vnode_t *vp, flock64_t *lost_flp, cred_t *cr,
15848 nfs4_lock_owner_t *lop)
15849 {
15850 locklist_t *locks, *llp, *ri_llp, *tmp_llp;
15851 mntinfo4_t *mi = VTOMI4(vp);
15852 const int cmd = F_SETLK;
15853 off64_t cur_start, llp_ll_flock_end, lost_flp_end;
15854 flock64_t ul_fl;
15855
15856 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15857 "nfs4_reinstitute_local_lock_state"));
15858
15859 /*
15860 * Find active locks for this vp from the local locking code.
15861 * Scan through this list and find out the locks that intersect with
15862 * the lost lock. Once we find the lock that intersects, add the
15863 * intersection area as a new lock to a new list "ri_llp". The lock
15864 * type of the intersection region lock added to ri_llp is the same
15865 * as that found in the active lock list, "list". The intersecting
15866 * region locks are added to ri_llp in increasing l_start order.
15867 */
15868 ASSERT(nfs_zone() == mi->mi_zone);
15869
15870 locks = flk_active_locks_for_vp(vp);
15871 ri_llp = NULL;
15872
15873 for (llp = locks; llp != NULL; llp = llp->ll_next) {
15874 ASSERT(llp->ll_vp == vp);
15875 /*
15876 * Pick locks that belong to this pid/lockowner
15877 */
15878 if (llp->ll_flock.l_pid != lost_flp->l_pid)
15879 continue;
15880
15881 nfs4_add_lock_range(lost_flp, &llp->ll_flock, &ri_llp, vp);
15882 }
15883
15884 /*
15885 * Now we have the list of intersections with the lost lock. These are
15886 * the locks that were/are active before the server replied to the
15887 * last/lost lock. Issue these locks to the server here. Playing these
15888 * locks to the server will re-establish our current local locking state
15889 * with the v4 server.
15890 * If we get an error, send SIGLOST to the application for that lock.
15891 */
15892
15893 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15894 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15895 "nfs4_reinstitute_local_lock_state: need to issue "
15896 "flock: [%"PRIx64" - %"PRIx64"] : %s",
15897 llp->ll_flock.l_start,
15898 llp->ll_flock.l_start + llp->ll_flock.l_len,
15899 llp->ll_flock.l_type == F_RDLCK ? "READ" :
15900 llp->ll_flock.l_type == F_WRLCK ? "WRITE" : "INVALID"));
15901 /*
15902 * No need to relock what we already have
15903 */
15904 if (llp->ll_flock.l_type == lost_flp->l_type)
15905 continue;
15906
15907 push_reinstate(vp, cmd, &llp->ll_flock, cr, lop);
15908 }
15909
15910 /*
15911 * Now keeping the start of the lost lock as our reference parse the
15912 * newly created ri_llp locklist to find the ranges that we have locked
15913 * with the v4 server but not in the current local locking. We need
15914 * to unlock these ranges.
15915 * These ranges can also be reffered to as those ranges, where the lost
15916 * lock does not overlap with the locks in the ri_llp but are locked
15917 * since the server replied to the lost lock.
15918 */
15919 cur_start = lost_flp->l_start;
15920 lost_flp_end = lock_to_end(lost_flp);
15921
15922 ul_fl.l_type = F_UNLCK;
15923 ul_fl.l_whence = 0; /* aka SEEK_SET */
15924 ul_fl.l_sysid = lost_flp->l_sysid;
15925 ul_fl.l_pid = lost_flp->l_pid;
15926
15927 for (llp = ri_llp; llp != NULL; llp = llp->ll_next) {
15928 llp_ll_flock_end = lock_to_end(&llp->ll_flock);
15929
15930 if (llp->ll_flock.l_start <= cur_start) {
15931 cur_start = start_check(llp_ll_flock_end);
15932 continue;
15933 }
15934 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15935 "nfs4_reinstitute_local_lock_state: "
15936 "UNLOCK [%"PRIx64" - %"PRIx64"]",
15937 cur_start, llp->ll_flock.l_start));
15938
15939 ul_fl.l_start = cur_start;
15940 ul_fl.l_len = end_to_len(cur_start,
15941 (llp->ll_flock.l_start - 1));
15942
15943 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15944 cur_start = start_check(llp_ll_flock_end);
15945 }
15946
15947 /*
15948 * In the case where the lost lock ends after all intersecting locks,
15949 * unlock the last part of the lost lock range.
15950 */
15951 if (cur_start != start_check(lost_flp_end)) {
15952 NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
15953 "nfs4_reinstitute_local_lock_state: UNLOCK end of the "
15954 "lost lock region [%"PRIx64" - %"PRIx64"]",
15955 cur_start, lost_flp->l_start + lost_flp->l_len));
15956
15957 ul_fl.l_start = cur_start;
15958 /*
15959 * Is it an to-EOF lock? if so unlock till the end
15960 */
15961 if (lost_flp->l_len == 0)
15962 ul_fl.l_len = 0;
15963 else
15964 ul_fl.l_len = start_check(lost_flp_end) - cur_start;
15965
15966 push_reinstate(vp, cmd, &ul_fl, cr, lop);
15967 }
15968
15969 if (locks != NULL)
15970 flk_free_locklist(locks);
15971
15972 /* Free up our newly created locklist */
15973 for (llp = ri_llp; llp != NULL; ) {
15974 tmp_llp = llp->ll_next;
15975 kmem_free(llp, sizeof (locklist_t));
15976 llp = tmp_llp;
15977 }
15978
15979 /*
15980 * Now return back to the original calling nfs4frlock()
15981 * and let us naturally drop our seqid syncs.
15982 */
15983 }
15984
15985 /*
15986 * Create a lost state record for the given lock reinstantiation request
15987 * and push it onto the lost state queue.
15988 */
15989 static void
push_reinstate(vnode_t * vp,int cmd,flock64_t * flk,cred_t * cr,nfs4_lock_owner_t * lop)15990 push_reinstate(vnode_t *vp, int cmd, flock64_t *flk, cred_t *cr,
15991 nfs4_lock_owner_t *lop)
15992 {
15993 nfs4_lost_rqst_t req;
15994 nfs_lock_type4 locktype;
15995 nfs4_error_t e = { EINTR, NFS4_OK, RPC_SUCCESS };
15996
15997 ASSERT(nfs_zone() == VTOMI4(vp)->mi_zone);
15998
15999 locktype = flk_to_locktype(cmd, flk->l_type);
16000 nfs4frlock_save_lost_rqst(NFS4_LCK_CTYPE_REINSTATE, EINTR, locktype,
16001 NULL, NULL, lop, flk, &req, cr, vp);
16002 (void) nfs4_start_recovery(&e, VTOMI4(vp), vp, NULL, NULL,
16003 (req.lr_op == OP_LOCK || req.lr_op == OP_LOCKU) ?
16004 &req : NULL, flk->l_type == F_UNLCK ? OP_LOCKU : OP_LOCK,
16005 NULL, NULL, NULL);
16006 }
16007